{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "id": "step-0",
   "metadata": {},
   "source": [
    "# LeRobot ACT \u00b7 PushT\n",
    "\n",
    "Train an imitation policy from the public lerobot/pusht dataset. No robot hardware needed. The short run checks the training path, not policy quality. Requires network access and a compatible NVIDIA driver. [LeRobot](https://github.com/huggingface/lerobot) \u00b7 [dataset](https://huggingface.co/datasets/lerobot/pusht)\n",
    "\n",
    "Run cells in order. Package and weight downloads use instance time and disk. Export outputs before releasing the instance."
   ]
  },
  {
   "cell_type": "code",
   "id": "step-1",
   "metadata": {},
   "source": [
    "import sys, subprocess\n",
    "subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", 'uv'])"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "step-2",
   "metadata": {},
   "source": [
    "from pathlib import Path\n",
    "env = Path.home() / \".cloudgpu-lerobot-070\"\n",
    "uv = [sys.executable, \"-m\", \"uv\"]\n",
    "if not (env / \"bin/python\").exists():\n",
    "    subprocess.run(uv + [\"venv\", \"--python\", \"3.12\", str(env)], check=True)\n",
    "python = str(env / \"bin/python\")\n",
    "subprocess.run(uv + [\"pip\", \"install\", \"--python\", python, \"lerobot[training,av-dep]==0.7.0\"], check=True)\n",
    "subprocess.run([python, \"-c\", \"import torch; assert torch.cuda.is_available(), 'Check NVIDIA driver / PyTorch CUDA compatibility'; print(torch.cuda.get_device_name())\"], check=True)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "step-3",
   "metadata": {},
   "source": [
    "from datetime import datetime, timezone\n",
    "output = Path.cwd() / (\"act-pusht-\" + datetime.now(timezone.utc).strftime(\"%Y%m%dT%H%M%SZ\"))\n",
    "train = str(env / \"bin/lerobot-train\")\n",
    "subprocess.run([train, \"--dataset.repo_id=lerobot/pusht\", \"--dataset.video_backend=pyav\",\n",
    "                \"--dataset.revision=7628202a2180972f291ba1bc6723834921e72c19\",\n",
    "                \"--policy.type=act\", \"--policy.device=cuda\", \"--policy.push_to_hub=false\",\n",
    "                \"--trackio.enable=false\", \"--steps=20\", \"--save_freq=10\", \"--batch_size=2\",\n",
    "                \"--num_workers=0\", \"--output_dir=\" + str(output)], check=True)\n",
    "configs = sorted(output.glob(\"checkpoints/*/pretrained_model/train_config.json\"))\n",
    "assert configs, \"Expected a saved training configuration\"\n",
    "print(\"Export the entire checkpoint directory:\", configs[-1].parent.parent)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "id": "step-4",
   "metadata": {},
   "source": [
    "## Resume an interrupted run\n",
    "Use the same environment and the checkpoint on disk. In a terminal, run `~/.cloudgpu-lerobot-070/bin/lerobot-train --config_path=/absolute/path/to/checkpoints/last/pretrained_model/train_config.json --resume=true`. A completed 20-step job already reached its configured target."
   ]
  }
 ]
}
