{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PI0：保护训练与评估\n",
    "\n",
    "本 Notebook 只执行 protected recipe。训练、checkpoint 保存、耗时记录、14 条固定 seed 闭环评估、视频和动作序列图都在 Notebook kernel 内完成。评估严格绑定本次训练生成的 `TRAINED_POLICY_PATH`，不会回退到历史或预训练权重。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import json\n",
    "import os\n",
    "import shlex\n",
    "import shutil\n",
    "import subprocess\n",
    "import sys\n",
    "\n",
    "try:\n",
    "    from IPython.display import HTML, Markdown, display\n",
    "except Exception:\n",
    "    class Markdown(str):\n",
    "        pass\n",
    "\n",
    "    class HTML(str):\n",
    "        pass\n",
    "\n",
    "    def display(obj):\n",
    "        print(obj)\n",
    "\n",
    "\n",
    "def find_topic_root():\n",
    "    cwd = Path.cwd().resolve()\n",
    "    for candidate in [cwd, *cwd.parents]:\n",
    "        if (candidate / \"assets\" / \"metrics_snapshot.json\").exists():\n",
    "            return candidate\n",
    "    raise RuntimeError(\"请从 AMD ROCm 专题目录或 notebooks 子目录启动 Jupyter。\")\n",
    "\n",
    "\n",
    "TOPIC_ROOT = find_topic_root()\n",
    "ASSET_DIR = TOPIC_ROOT / \"assets\"\n",
    "PROJECT_ROOT = Path(os.environ.get(\"PROJECT_ROOT\", \"/path/to/04mujoco复现ACT、Pi0、SmolVLA\"))\n",
    "DATA_ROOT = Path(os.environ.get(\"DATA_ROOT\", \"/path/to/datasets/every_embodied\"))\n",
    "MODEL_ROOT = Path(os.environ.get(\"MODEL_ROOT\", \"/path/to/model/checkpoints\"))\n",
    "OUTPUT_ROOT = Path(os.environ.get(\"OUTPUT_ROOT\", TOPIC_ROOT / 'outputs' / 'protected' / 'pi0'))\n",
    "\n",
    "# The AMD teaching workflow should be runnable from local datasets/checkpoints.\n",
    "# Avoid surprising network calls during class or when AUP/Radeon Cloud cannot\n",
    "# reach Hugging Face.\n",
    "os.environ.setdefault(\"HF_HUB_OFFLINE\", os.environ.get(\"NOTEBOOK_HF_OFFLINE\", \"1\"))\n",
    "os.environ.setdefault(\"TRANSFORMERS_OFFLINE\", os.environ.get(\"NOTEBOOK_HF_OFFLINE\", \"1\"))\n",
    "os.environ.setdefault(\"HF_DATASETS_OFFLINE\", os.environ.get(\"NOTEBOOK_HF_OFFLINE\", \"1\"))\n",
    "os.environ.setdefault(\"HF_HOME\", str(Path(os.environ.get(\"CACHE_ROOT\", OUTPUT_ROOT / \"cache\")) / \"huggingface\"))\n",
    "os.environ.setdefault(\"HF_DATASETS_CACHE\", str(Path(os.environ[\"HF_HOME\"]) / \"datasets\"))\n",
    "\n",
    "def public_path(path):\n",
    "    path = Path(path)\n",
    "    replacements = [\n",
    "        (TOPIC_ROOT, \"$TOPIC_ROOT\"),\n",
    "        (PROJECT_ROOT, \"$PROJECT_ROOT\"),\n",
    "        (DATA_ROOT, \"$DATA_ROOT\"),\n",
    "        (MODEL_ROOT, \"$MODEL_ROOT\"),\n",
    "        (OUTPUT_ROOT, \"$OUTPUT_ROOT\"),\n",
    "    ]\n",
    "    value = str(path)\n",
    "    for root, label in sorted(replacements, key=lambda item: len(str(item[0])), reverse=True):\n",
    "        root_value = str(root)\n",
    "        if root_value and value.startswith(root_value):\n",
    "            return label + value[len(root_value):]\n",
    "    return value\n",
    "\n",
    "\n",
    "print(\"TOPIC_ROOT = $TOPIC_ROOT\")\n",
    "print(\"PROJECT_ROOT =\", public_path(PROJECT_ROOT))\n",
    "print(\"DATA_ROOT =\", public_path(DATA_ROOT))\n",
    "print(\"MODEL_ROOT =\", public_path(MODEL_ROOT))\n",
    "print(\"OUTPUT_ROOT =\", public_path(OUTPUT_ROOT))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def md_table(headers, rows):\n",
    "    lines = [\"| \" + \" | \".join(headers) + \" |\", \"| \" + \" | \".join([\"---\"] * len(headers)) + \" |\"]\n",
    "    for row in rows:\n",
    "        lines.append(\"| \" + \" | \".join(public_path(x) if isinstance(x, (str, Path)) else str(x) for x in row) + \" |\")\n",
    "    display(Markdown(\"\\n\".join(lines)))\n",
    "\n",
    "\n",
    "def show_json(path, max_chars=5000):\n",
    "    path = Path(path)\n",
    "    if not path.exists():\n",
    "        print(\"文件不存在：\", public_path(path))\n",
    "        return None\n",
    "    data = json.loads(path.read_text(encoding=\"utf-8\"))\n",
    "    text = json.dumps(data, ensure_ascii=False, indent=2)\n",
    "    print(text[:max_chars] + (\"\\n...\" if len(text) > max_chars else \"\"))\n",
    "    return data\n",
    "\n",
    "\n",
    "def show_video(filename, title):\n",
    "    path = ASSET_DIR / filename\n",
    "    display(Markdown(f\"**{title}**\"))\n",
    "    if path.exists():\n",
    "        rel = f\"../assets/{filename}\"\n",
    "        display(HTML(f\"<video controls muted preload='metadata' width='960'><source src='{rel}' type='video/mp4'></video>\"))\n",
    "    else:\n",
    "        print(\"缺少视频素材：\", public_path(path))\n",
    "\n",
    "\n",
    "def show_image(filename, title, width=960):\n",
    "    path = ASSET_DIR / filename\n",
    "    display(Markdown(f\"**{title}**\"))\n",
    "    if path.exists():\n",
    "        rel = f\"../assets/{filename}\"\n",
    "        display(HTML(f\"<img src='{rel}' width='{width}'>\"))\n",
    "    else:\n",
    "        print(\"缺少图片素材：\", public_path(path))\n",
    "\n",
    "\n",
    "def run_cmd_preview(command, cwd=None):\n",
    "    shown = [public_path(x) if isinstance(x, (str, Path)) else x for x in command]\n",
    "    print(\"$\", shlex.join([str(x) for x in shown]))\n",
    "    if cwd:\n",
    "        print(\"cwd =\", public_path(cwd))\n",
    "\n",
    "\n",
    "def tail_log(log_path, lines=40):\n",
    "    path = Path(log_path)\n",
    "    if not path.exists():\n",
    "        print(\"日志不存在：\", public_path(path))\n",
    "        return\n",
    "    content = path.read_text(encoding=\"utf-8\", errors=\"replace\").splitlines()\n",
    "    print(\"\\n\".join(content[-lines:]))\n",
    "\n",
    "\n",
    "def env_flag(name, default=False):\n",
    "    value = os.environ.get(name)\n",
    "    if value is None:\n",
    "        return default\n",
    "    return value.strip().lower() in {\"1\", \"true\", \"yes\", \"y\", \"on\"}\n",
    "\n",
    "\n",
    "RUN_SMOKE = env_flag(\"RUN_SMOKE\")\n",
    "RUN_LONG_TRAIN = env_flag(\"RUN_LONG_TRAIN\", True)\n",
    "RUN_EVAL = env_flag(\"RUN_EVAL\")\n",
    "EVAL_SCRIPT = Path(os.environ.get(\"EVAL_SCRIPT\", PROJECT_ROOT / \"eval_policy_success.py\"))\n",
    "\n",
    "\n",
    "_XVFB_PROCESS = None\n",
    "\n",
    "\n",
    "def ensure_xvfb_display():\n",
    "    \"\"\"Start a lightweight virtual display for headless MuJoCo evaluation.\"\"\"\n",
    "    global _XVFB_PROCESS\n",
    "    if os.environ.get(\"DISPLAY\"):\n",
    "        print(\"DISPLAY =\", os.environ[\"DISPLAY\"])\n",
    "        return None\n",
    "    xvfb_bin = shutil.which(\"Xvfb\")\n",
    "    if not xvfb_bin:\n",
    "        print(\"没有发现 Xvfb；如遇 GLFW DISPLAY 报错，请先安装 xvfb。\")\n",
    "        return None\n",
    "    display_id = os.environ.get(\"NOTEBOOK_XVFB_DISPLAY\", \":99\")\n",
    "    _XVFB_PROCESS = subprocess.Popen(\n",
    "        [xvfb_bin, display_id, \"-screen\", \"0\", \"1280x1024x24\"],\n",
    "        stdout=subprocess.DEVNULL,\n",
    "        stderr=subprocess.DEVNULL,\n",
    "    )\n",
    "    os.environ[\"DISPLAY\"] = display_id\n",
    "    print(\"已启动 Notebook 内部 Xvfb：DISPLAY =\", display_id)\n",
    "    return _XVFB_PROCESS\n",
    "\n",
    "\n",
    "def ensure_project_layout():\n",
    "    required = [PROJECT_ROOT / \"asset\" / \"example_scene_y2.xml\", PROJECT_ROOT / \"mujoco_env\"]\n",
    "    missing = [path for path in required if not path.exists()]\n",
    "    if missing:\n",
    "        print(\"当前 PROJECT_ROOT 还不是可运行工程，缺少：\")\n",
    "        for path in missing:\n",
    "            print(\" -\", public_path(path))\n",
    "        print(\"请先设置 PROJECT_ROOT，再运行训练或评估单元。\")\n",
    "        return False\n",
    "    return True\n",
    "\n",
    "\n",
    "def write_json_yaml(path, payload):\n",
    "    path = Path(path)\n",
    "    path.parent.mkdir(parents=True, exist_ok=True)\n",
    "    try:\n",
    "        import yaml\n",
    "        text = yaml.safe_dump(payload, allow_unicode=True, sort_keys=False)\n",
    "    except Exception:\n",
    "        text = json.dumps(payload, ensure_ascii=False, indent=2) + \"\\n\"\n",
    "    path.write_text(text, encoding=\"utf-8\")\n",
    "    print(\"写出配置：\", public_path(path))\n",
    "    return path\n",
    "\n",
    "\n",
    "def make_lerobot_train_config(policy_type, dataset_repo_id, dataset_root, output_dir, steps, batch_size, chunk_size, n_action_steps, seed=42):\n",
    "    save_freq = int(os.environ.get(f\"{policy_type.upper()}_SAVE_FREQ\", os.environ.get(\"SAVE_FREQ\", str(steps))))\n",
    "    return {\n",
    "        \"dataset\": {\n",
    "            \"repo_id\": dataset_repo_id,\n",
    "            \"root\": str(dataset_root),\n",
    "            \"use_imagenet_stats\": True,\n",
    "        },\n",
    "        \"policy\": {\n",
    "            \"type\": policy_type,\n",
    "            \"chunk_size\": int(chunk_size),\n",
    "            \"n_action_steps\": int(n_action_steps),\n",
    "            \"device\": \"cuda\",\n",
    "        },\n",
    "        \"output_dir\": str(output_dir),\n",
    "        \"job_name\": Path(output_dir).name,\n",
    "        \"batch_size\": int(batch_size),\n",
    "        \"steps\": int(steps),\n",
    "        \"save_freq\": max(1, save_freq),\n",
    "        \"log_freq\": 20,\n",
    "        \"num_workers\": 4,\n",
    "        \"seed\": int(seed),\n",
    "        \"resume\": False,\n",
    "        \"eval_freq\": -1,\n",
    "        \"save_checkpoint\": True,\n",
    "        \"use_policy_training_preset\": True,\n",
    "        \"wandb\": {\"enable\": False, \"disable_artifact\": True},\n",
    "    }\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "class _NotebookCompactProgress:\n",
    "    def __init__(self, iterable, desc, total=None):\n",
    "        self.iterable = iterable\n",
    "        self.desc = desc\n",
    "        self.total = int(total if total is not None else len(iterable))\n",
    "        self.every = max(1, int(os.environ.get(\"NOTEBOOK_PROGRESS_EVERY\", \"1\")))\n",
    "        self.postfix = \"\"\n",
    "\n",
    "    def set_postfix(self, **kwargs):\n",
    "        self.postfix = \", \".join(f\"{key}={value}\" for key, value in kwargs.items())\n",
    "\n",
    "    def __iter__(self):\n",
    "        for index, value in enumerate(self.iterable, start=1):\n",
    "            yield value\n",
    "            if index == 1 or index == self.total or index % self.every == 0:\n",
    "                suffix = f\" | {self.postfix}\" if self.postfix else \"\"\n",
    "                print(f\"{self.desc}: {index}/{self.total}{suffix}\", flush=True)\n",
    "\n",
    "def notebook_progress(iterable, desc, total=None):\n",
    "    return _NotebookCompactProgress(iterable, desc, total=total)\n",
    "\n",
    "\n",
    "def train_lerobot_config_in_notebook(config_path, enabled=False, progress_name=\"train\"):\n",
    "    \"\"\"Run LeRobot offline training directly inside the notebook kernel.\n",
    "\n",
    "    The notebook cell owns dataset creation, policy creation, optimizer steps,\n",
    "    checkpoint saving, tqdm progress, and metric JSONL writing.\n",
    "    \"\"\"\n",
    "    config_path = Path(config_path)\n",
    "    print(\"config =\", public_path(config_path))\n",
    "    if not enabled:\n",
    "        print(\"未启动。设置 RUN_SMOKE=1 或 RUN_LONG_TRAIN=1 后，本单元会直接在 Notebook 内训练。\")\n",
    "        return None\n",
    "    if not ensure_project_layout():\n",
    "        return None\n",
    "\n",
    "    import time\n",
    "    from datetime import datetime, timezone\n",
    "    from contextlib import nullcontext\n",
    "\n",
    "    import draccus\n",
    "    import torch\n",
    "    from torch.amp import GradScaler\n",
    "\n",
    "    from lerobot.common.datasets.factory import make_dataset\n",
    "    from lerobot.common.datasets.sampler import EpisodeAwareSampler\n",
    "    from lerobot.common.datasets.utils import cycle\n",
    "    from lerobot.common.optim.factory import make_optimizer_and_scheduler\n",
    "    from lerobot.common.policies.factory import make_policy\n",
    "    from lerobot.common.policies.utils import get_device_from_parameters\n",
    "    from lerobot.common.utils.random_utils import set_seed\n",
    "    from lerobot.common.utils.train_utils import get_step_checkpoint_dir, save_checkpoint, update_last_checkpoint\n",
    "    from lerobot.common.utils.utils import get_safe_torch_device\n",
    "    from lerobot.configs.train import TrainPipelineConfig\n",
    "\n",
    "    class _CompactProgress:\n",
    "        def __init__(self, iterable, desc, total=None):\n",
    "            self.iterable = iterable\n",
    "            self.desc = desc\n",
    "            self.total = int(total if total is not None else len(iterable))\n",
    "            self.every = max(1, int(os.environ.get(\"NOTEBOOK_PROGRESS_EVERY\", \"100\")))\n",
    "            self.postfix = \"\"\n",
    "\n",
    "        def set_postfix(self, **kwargs):\n",
    "            self.postfix = \", \".join(f\"{key}={value}\" for key, value in kwargs.items())\n",
    "\n",
    "        def __iter__(self):\n",
    "            for index, value in enumerate(self.iterable, start=1):\n",
    "                yield value\n",
    "                if index == 1 or index == self.total or index % self.every == 0:\n",
    "                    suffix = f\" | {self.postfix}\" if self.postfix else \"\"\n",
    "                    print(f\"{self.desc}: {index}/{self.total}{suffix}\", flush=True)\n",
    "\n",
    "    def notebook_progress(iterable, desc, total=None):\n",
    "        return _CompactProgress(iterable, desc, total=total)\n",
    "\n",
    "    run_started_at = datetime.now(timezone.utc).isoformat()\n",
    "    cfg = draccus.parse(TrainPipelineConfig, config_path=config_path, args=[])\n",
    "    cfg.validate()\n",
    "    if cfg.seed is not None:\n",
    "        set_seed(cfg.seed)\n",
    "\n",
    "    device = get_safe_torch_device(cfg.policy.device, log=True)\n",
    "    torch.backends.cudnn.benchmark = True\n",
    "    torch.backends.cuda.matmul.allow_tf32 = True\n",
    "\n",
    "    print(\"Creating dataset...\")\n",
    "    dataset = make_dataset(cfg)\n",
    "    print(\"Creating policy...\")\n",
    "    pretrained_override = os.environ.get(f\"{cfg.policy.type.upper()}_PRETRAINED_PATH_OVERRIDE\") or os.environ.get(\"POLICY_PRETRAINED_PATH_OVERRIDE\")\n",
    "    if pretrained_override and not cfg.resume:\n",
    "        cfg.policy.pretrained_path = str(Path(pretrained_override))\n",
    "        print(\"pretrained override =\", public_path(cfg.policy.pretrained_path))\n",
    "    elif cfg.policy.type == \"pi0\" and not cfg.resume:\n",
    "        cfg.policy.pretrained_path = \"lerobot/pi0\"\n",
    "    elif cfg.policy.type == \"smolvla\" and not cfg.resume:\n",
    "        smolvla_base_candidates = [\n",
    "            os.environ.get(\"SMOLVLA_BASE_PATH\"),\n",
    "            os.environ.get(\"SMOLVLA_PRETRAINED_BASE_PATH\"),\n",
    "            str(MODEL_ROOT / \"smolvla_base\" / \"pretrained_model\"),\n",
    "            str(MODEL_ROOT / \"lerobot_smolvla_base_legacy\"),\n",
    "            str(MODEL_ROOT / \"lerobot_smolvla_base\"),\n",
    "        ]\n",
    "        local_smolvla_base = next((Path(p) for p in smolvla_base_candidates if p and Path(p).exists()), None)\n",
    "        if local_smolvla_base is not None:\n",
    "            cfg.policy.pretrained_path = str(local_smolvla_base)\n",
    "            print(\"local smolvla base =\", public_path(cfg.policy.pretrained_path))\n",
    "        else:\n",
    "            cfg.policy.pretrained_path = \"lerobot/smolvla_base\"\n",
    "    policy = make_policy(cfg=cfg.policy, ds_meta=dataset.meta)\n",
    "\n",
    "    # Compatibility for newer Transformers: PaliGemmaForConditionalGeneration may expose\n",
    "    # language_model as GemmaModel directly, while this LeRobot Pi0 code expects\n",
    "    # language_model.model.  Use a non-Module proxy so checkpoints/state_dict stay clean.\n",
    "    if cfg.policy.type == \"pi0\":\n",
    "        try:\n",
    "            lm = policy.model.paligemma_with_expert.paligemma.language_model\n",
    "            if not hasattr(lm, \"model\"):\n",
    "                class _LanguageModelCoreProxy:\n",
    "                    def __init__(self, core):\n",
    "                        self._core = core\n",
    "\n",
    "                    def __getattr__(self, name):\n",
    "                        return getattr(self._core, name)\n",
    "\n",
    "                object.__setattr__(lm, \"model\", _LanguageModelCoreProxy(lm))\n",
    "                print(\"patched Pi0 PaliGemma language_model.model compatibility proxy\")\n",
    "        except Exception as exc:\n",
    "            print(f\"Pi0 PaliGemma compatibility patch skipped: {exc}\")\n",
    "\n",
    "    policy.to(device)\n",
    "    policy.train()\n",
    "\n",
    "    optimizer, lr_scheduler = make_optimizer_and_scheduler(cfg, policy)\n",
    "    grad_scaler = GradScaler(device.type, enabled=cfg.policy.use_amp)\n",
    "\n",
    "    def _dataset_column_values(name):\n",
    "        hf_dataset = getattr(dataset, \"hf_dataset\", None)\n",
    "        if hf_dataset is None or name not in getattr(hf_dataset, \"column_names\", []):\n",
    "            return None\n",
    "        values = hf_dataset[name]\n",
    "        try:\n",
    "            return list(values)\n",
    "        except TypeError:\n",
    "            return [values[i] for i in range(len(values))]\n",
    "\n",
    "    def _task_name_map():\n",
    "        meta = getattr(dataset, \"meta\", None)\n",
    "        tasks = getattr(meta, \"tasks\", None)\n",
    "        if tasks is None:\n",
    "            return {}\n",
    "        if isinstance(tasks, dict):\n",
    "            return {int(k): str(v) for k, v in tasks.items()}\n",
    "        try:\n",
    "            return {int(k): str(v) for k, v in dict(tasks).items()}\n",
    "        except Exception:\n",
    "            return {}\n",
    "\n",
    "    def _make_weighted_sampler(generator):\n",
    "        mode = os.environ.get(\"NOTEBOOK_FRAME_WEIGHT_MODE\", \"\").strip().lower()\n",
    "        if not mode or mode in {\"0\", \"none\", \"off\", \"false\"}:\n",
    "            return None, {\"mode\": \"none\"}\n",
    "        weights = torch.ones(len(dataset), dtype=torch.double)\n",
    "        info = {\"mode\": mode, \"num_frames\": len(dataset)}\n",
    "\n",
    "        if \"blue\" in mode:\n",
    "            blue_weight = float(os.environ.get(\"NOTEBOOK_BLUE_WEIGHT\", \"2.0\"))\n",
    "            mask = [False] * len(dataset)\n",
    "            task_indices = _dataset_column_values(\"task_index\")\n",
    "            task_names = _task_name_map()\n",
    "            if task_indices is not None and task_names:\n",
    "                for idx, task_index in enumerate(task_indices):\n",
    "                    task_text = task_names.get(int(task_index), \"\").lower()\n",
    "                    mask[idx] = (\"blue\" in task_text) or (\"蓝\" in task_text)\n",
    "            else:\n",
    "                for column in [\"task\", \"language_instruction\", \"instruction\"]:\n",
    "                    values = _dataset_column_values(column)\n",
    "                    if values is None:\n",
    "                        continue\n",
    "                    for idx, value in enumerate(values):\n",
    "                        text = str(value).lower()\n",
    "                        mask[idx] = (\"blue\" in text) or (\"蓝\" in text)\n",
    "                    break\n",
    "            blue_count = int(sum(mask))\n",
    "            if blue_count == 0:\n",
    "                print(\"警告：NOTEBOOK_FRAME_WEIGHT_MODE=blue 但没有识别到 blue/蓝 指令帧，采样退回均匀。\")\n",
    "            else:\n",
    "                for idx, is_blue in enumerate(mask):\n",
    "                    if is_blue:\n",
    "                        weights[idx] *= blue_weight\n",
    "            info.update({\"blue_weight\": blue_weight, \"blue_frames\": blue_count})\n",
    "\n",
    "        weight_file = os.environ.get(\"NOTEBOOK_FRAME_WEIGHT_JSON\")\n",
    "        if weight_file:\n",
    "            payload = json.loads(Path(weight_file).read_text(encoding=\"utf-8\"))\n",
    "            for key, value in payload.items():\n",
    "                weights[int(key)] *= float(value)\n",
    "            info.update({\"weight_json\": public_path(weight_file), \"json_entries\": len(payload)})\n",
    "\n",
    "        if float(weights.sum()) <= 0:\n",
    "            raise ValueError(\"采样权重总和为 0。\")\n",
    "        sampler = torch.utils.data.WeightedRandomSampler(\n",
    "            weights=weights,\n",
    "            num_samples=len(weights),\n",
    "            replacement=True,\n",
    "            generator=generator,\n",
    "        )\n",
    "        info.update(\n",
    "            {\n",
    "                \"weight_min\": float(weights.min()),\n",
    "                \"weight_max\": float(weights.max()),\n",
    "                \"weight_mean\": float(weights.mean()),\n",
    "            }\n",
    "        )\n",
    "        return sampler, info\n",
    "\n",
    "    generator = torch.Generator()\n",
    "    if cfg.seed is not None:\n",
    "        generator.manual_seed(int(cfg.seed))\n",
    "\n",
    "    weighted_sampler, sampler_info = _make_weighted_sampler(generator)\n",
    "    if weighted_sampler is not None:\n",
    "        shuffle = False\n",
    "        sampler = weighted_sampler\n",
    "        print(\"Notebook weighted sampler =\", json.dumps(sampler_info, ensure_ascii=False))\n",
    "    elif hasattr(cfg.policy, \"drop_n_last_frames\"):\n",
    "        shuffle = False\n",
    "        sampler = EpisodeAwareSampler(\n",
    "            dataset.episode_data_index,\n",
    "            drop_n_last_frames=cfg.policy.drop_n_last_frames,\n",
    "            shuffle=True,\n",
    "        )\n",
    "    else:\n",
    "        shuffle = True\n",
    "        sampler = None\n",
    "\n",
    "    dataloader = torch.utils.data.DataLoader(\n",
    "        dataset,\n",
    "        num_workers=cfg.num_workers,\n",
    "        batch_size=cfg.batch_size,\n",
    "        shuffle=shuffle,\n",
    "        sampler=sampler,\n",
    "        generator=generator if sampler is None else None,\n",
    "        pin_memory=device.type != \"cpu\",\n",
    "        drop_last=False,\n",
    "    )\n",
    "    dl_iter = cycle(dataloader)\n",
    "\n",
    "    output_dir = Path(cfg.output_dir)\n",
    "    output_dir.mkdir(parents=True, exist_ok=True)\n",
    "    metrics_path = output_dir / \"notebook_train_metrics.jsonl\"\n",
    "    num_learnable = sum(p.numel() for p in policy.parameters() if p.requires_grad)\n",
    "    num_total = sum(p.numel() for p in policy.parameters())\n",
    "    print(f\"output_dir = {public_path(output_dir)}\")\n",
    "    print(f\"steps = {cfg.steps}, batch_size = {cfg.batch_size}, frames = {dataset.num_frames}, episodes = {dataset.num_episodes}\")\n",
    "    print(f\"learnable_params = {num_learnable:,}, total_params = {num_total:,}\")\n",
    "\n",
    "    last_metrics = None\n",
    "    progress = notebook_progress(range(1, cfg.steps + 1), desc=progress_name, total=cfg.steps)\n",
    "    start_all = time.perf_counter()\n",
    "    for step in progress:\n",
    "        load_start = time.perf_counter()\n",
    "        batch = next(dl_iter)\n",
    "        data_s = time.perf_counter() - load_start\n",
    "        for key, value in batch.items():\n",
    "            if isinstance(value, torch.Tensor):\n",
    "                batch[key] = value.to(device, non_blocking=True)\n",
    "\n",
    "        update_start = time.perf_counter()\n",
    "        device_from_policy = get_device_from_parameters(policy)\n",
    "        with torch.autocast(device_type=device_from_policy.type) if cfg.policy.use_amp else nullcontext():\n",
    "            loss, output_dict = policy.forward(batch)\n",
    "        grad_scaler.scale(loss).backward()\n",
    "        grad_scaler.unscale_(optimizer)\n",
    "        grad_norm = torch.nn.utils.clip_grad_norm_(\n",
    "            policy.parameters(),\n",
    "            cfg.optimizer.grad_clip_norm,\n",
    "            error_if_nonfinite=False,\n",
    "        )\n",
    "        grad_scaler.step(optimizer)\n",
    "        grad_scaler.update()\n",
    "        optimizer.zero_grad()\n",
    "        if lr_scheduler is not None:\n",
    "            lr_scheduler.step()\n",
    "        if hasattr(policy, \"update\"):\n",
    "            policy.update()\n",
    "        update_s = time.perf_counter() - update_start\n",
    "\n",
    "        is_log_step = cfg.log_freq > 0 and (step % cfg.log_freq == 0 or step == 1 or step == cfg.steps)\n",
    "        is_saving_step = cfg.save_checkpoint and (step % cfg.save_freq == 0 or step == cfg.steps)\n",
    "        if is_log_step:\n",
    "            last_metrics = {\n",
    "                \"step\": step,\n",
    "                \"loss\": float(loss.detach().cpu()),\n",
    "                \"grad_norm\": float(grad_norm.detach().cpu()) if hasattr(grad_norm, \"detach\") else float(grad_norm),\n",
    "                \"lr\": float(optimizer.param_groups[0][\"lr\"]),\n",
    "                \"update_s\": float(update_s),\n",
    "                \"data_s\": float(data_s),\n",
    "                \"elapsed_s\": float(time.perf_counter() - start_all),\n",
    "            }\n",
    "            with metrics_path.open(\"a\", encoding=\"utf-8\") as f:\n",
    "                f.write(json.dumps(last_metrics, ensure_ascii=False) + \"\\n\")\n",
    "            progress.set_postfix(\n",
    "                loss=f\"{last_metrics['loss']:.4f}\",\n",
    "                lr=f\"{last_metrics['lr']:.1e}\",\n",
    "                updt_s=f\"{last_metrics['update_s']:.3f}\",\n",
    "            )\n",
    "        if is_saving_step:\n",
    "            checkpoint_dir = get_step_checkpoint_dir(cfg.output_dir, cfg.steps, step)\n",
    "            print(f\"\\nSaving checkpoint at step {step}: {public_path(checkpoint_dir)}\")\n",
    "            save_checkpoint(checkpoint_dir, step, cfg, policy, optimizer, lr_scheduler)\n",
    "            update_last_checkpoint(checkpoint_dir)\n",
    "\n",
    "    run_finished_at = datetime.now(timezone.utc).isoformat()\n",
    "    duration_s = float(time.perf_counter() - start_all)\n",
    "    checkpoint_paths = sorted(str(path) for path in output_dir.glob(\"checkpoints/*/pretrained_model\"))\n",
    "    run_summary = {\n",
    "        \"recipe\": str(cfg.policy.type),\n",
    "        \"native_notebook_kernel\": True,\n",
    "        \"started_at_utc\": run_started_at,\n",
    "        \"finished_at_utc\": run_finished_at,\n",
    "        \"duration_s\": duration_s,\n",
    "        \"steps\": int(cfg.steps),\n",
    "        \"batch_size\": int(cfg.batch_size),\n",
    "        \"output_dir\": str(output_dir),\n",
    "        \"metrics_path\": str(metrics_path),\n",
    "        \"checkpoint_paths\": checkpoint_paths,\n",
    "        \"last_metrics\": last_metrics,\n",
    "    }\n",
    "    summary_path = output_dir / \"training_run_summary.json\"\n",
    "    summary_path.write_text(json.dumps(run_summary, ensure_ascii=False, indent=2), encoding=\"utf-8\")\n",
    "    print(\"训练完成。metrics =\", public_path(metrics_path))\n",
    "    print(\"训练耗时 =\", f\"{duration_s / 60.0:.2f} 分钟\")\n",
    "    print(\"训练摘要 =\", public_path(summary_path))\n",
    "    if last_metrics is not None:\n",
    "        print(json.dumps(last_metrics, ensure_ascii=False, indent=2))\n",
    "    return {\"output_dir\": output_dir, \"metrics_path\": metrics_path, \"summary_path\": summary_path, \"last_metrics\": last_metrics, \"duration_s\": duration_s}\n",
    "\n",
    "\n",
    "def load_eval_module():\n",
    "    import importlib.util\n",
    "\n",
    "    if not EVAL_SCRIPT.exists():\n",
    "        raise FileNotFoundError(f\"评估脚本不存在：{public_path(EVAL_SCRIPT)}\")\n",
    "    spec = importlib.util.spec_from_file_location(\"notebook_eval_policy_success\", EVAL_SCRIPT)\n",
    "    module = importlib.util.module_from_spec(spec)\n",
    "    assert spec.loader is not None\n",
    "    spec.loader.exec_module(module)\n",
    "    return module\n",
    "\n",
    "\n",
    "def run_eval_policy_in_notebook(\n",
    "    policy_name,\n",
    "    policy_path,\n",
    "    result_path,\n",
    "    episodes,\n",
    "    seed_start,\n",
    "    render=False,\n",
    "    enabled=False,\n",
    "    repo_id=None,\n",
    "    dataset_root=None,\n",
    "    video_dir=None,\n",
    "    trace_dir=None,\n",
    "    seed_list=None,\n",
    "):\n",
    "    print(\"policy =\", policy_name)\n",
    "    print(\"policy_path =\", public_path(policy_path))\n",
    "    print(\"result =\", public_path(result_path))\n",
    "    if not enabled:\n",
    "        print(\"未启动。设置 RUN_EVAL=1 后，本单元会在 Notebook 内直接加载策略并闭环评估。\")\n",
    "        return None\n",
    "    if not ensure_project_layout():\n",
    "        return None\n",
    "\n",
    "    import argparse\n",
    "    from contextlib import contextmanager\n",
    "\n",
    "    @contextmanager\n",
    "    def pushd(path):\n",
    "        old = Path.cwd()\n",
    "        os.chdir(path)\n",
    "        try:\n",
    "            yield\n",
    "        finally:\n",
    "            os.chdir(old)\n",
    "\n",
    "    ensure_xvfb_display()\n",
    "    module = load_eval_module()\n",
    "    result_path = Path(result_path)\n",
    "    result_path.parent.mkdir(parents=True, exist_ok=True)\n",
    "    if result_path.exists():\n",
    "        result_path.unlink()\n",
    "\n",
    "    args = argparse.Namespace(\n",
    "        policy=policy_name,\n",
    "        episodes=int(episodes),\n",
    "        seed_start=int(seed_start),\n",
    "        max_action_steps=int(os.environ.get(\"EVAL_MAX_ACTION_STEPS\", \"400\")),\n",
    "        hz=float(os.environ.get(\"EVAL_HZ\", \"20\")),\n",
    "        render=bool(render),\n",
    "        output_jsonl=result_path,\n",
    "        video_dir=Path(video_dir) if video_dir else None,\n",
    "        trace_dir=Path(trace_dir) if trace_dir else None,\n",
    "        device=os.environ.get(\"EVAL_DEVICE\", \"cuda\"),\n",
    "        reset_policy_each_action=env_flag(\"EVAL_RESET_POLICY_EACH_ACTION\", False),\n",
    "        act_n_action_steps=None,\n",
    "        act_force_dataset_gripper=False,\n",
    "        act_clamp_timestamp=False,\n",
    "        act_policy_path=Path(policy_path),\n",
    "        act_repo_id=repo_id or \"datawhale_eai_pnp\",\n",
    "        act_dataset_root=Path(dataset_root or \"./demo_data\"),\n",
    "        act_episode_timestamp_offsets=\"\",\n",
    "        act_episode_source_flags=\"\",\n",
    "        physical_success=env_flag(\"EVAL_PHYSICAL_SUCCESS\", True),\n",
    "        physical_min_lift=float(os.environ.get(\"EVAL_PHYSICAL_MIN_LIFT\", \"0.06\")),\n",
    "        physical_min_lift_steps=int(os.environ.get(\"EVAL_PHYSICAL_MIN_LIFT_STEPS\", \"3\")),\n",
    "        physical_final_upright_cos=float(os.environ.get(\"EVAL_PHYSICAL_FINAL_UPRIGHT_COS\", \"0.85\")),\n",
    "        smolvla_policy_path=Path(policy_path),\n",
    "        pi0_policy_path=Path(policy_path),\n",
    "        pi0_repo_id=repo_id or os.environ.get(\"PI0_DATASET_REPO_ID\", \"datawhale_eai_pnp_language\"),\n",
    "        pi0_dataset_root=Path(dataset_root or os.environ.get(\"PI0_DATASET_ROOT\", \"./demo_data_language\")),\n",
    "    )\n",
    "\n",
    "    with pushd(PROJECT_ROOT):\n",
    "        selected_cases = set()\n",
    "        selected_cases = set()\n",
    "        selected_cases = set()\n",
    "        if policy_name == \"act\":\n",
    "            policy = module.make_act_policy(\n",
    "                args.device,\n",
    "                args.act_policy_path,\n",
    "                args.act_repo_id,\n",
    "                args.act_dataset_root,\n",
    "                n_action_steps=args.act_n_action_steps,\n",
    "                episode_timestamp_offsets=args.act_episode_timestamp_offsets,\n",
    "                episode_source_flags=args.act_episode_source_flags,\n",
    "            )\n",
    "            rollout = module.rollout_act\n",
    "        elif policy_name == \"smolvla\":\n",
    "            policy = module.make_smolvla_policy(args.device, args.smolvla_policy_path)\n",
    "            rollout = module.rollout_language_policy\n",
    "        elif policy_name == \"pi0\":\n",
    "            policy = module.make_pi0_policy(args.device, args.pi0_policy_path, args.pi0_repo_id, args.pi0_dataset_root)\n",
    "            rollout = module.rollout_language_policy\n",
    "        else:\n",
    "            raise ValueError(policy_name)\n",
    "\n",
    "        rows = []\n",
    "        for offset in notebook_progress(range(args.episodes), desc=f\"{policy_name} eval\", total=args.episodes):\n",
    "            seed = (list(seed_list)[offset] if seed_list is not None else args.seed_start + offset)\n",
    "            row = rollout(args, policy, seed)\n",
    "            row = module.finalize_case_media(args, row, selected_cases)\n",
    "            rows.append(row)\n",
    "            with result_path.open(\"a\", encoding=\"utf-8\") as f:\n",
    "                f.write(json.dumps(row, ensure_ascii=False) + \"\\n\")\n",
    "            print(json.dumps(row, ensure_ascii=False))\n",
    "    summarize_jsonl(result_path)\n",
    "    return rows\n",
    "\n",
    "\n",
    "def list_checkpoints(run_dir):\n",
    "    run_dir = Path(run_dir)\n",
    "    candidates = []\n",
    "    for pattern in [\"checkpoints/*/pretrained_model\", \"checkpoint*/pretrained_model\", \"*/pretrained_model\", \"pretrained_model\"]:\n",
    "        candidates.extend(run_dir.glob(pattern))\n",
    "    unique = sorted(set(candidates))\n",
    "    if not unique:\n",
    "        print(\"尚未发现 checkpoint：\", public_path(run_dir))\n",
    "        return []\n",
    "    for path in unique:\n",
    "        print(\" -\", public_path(path))\n",
    "    return unique\n",
    "\n",
    "\n",
    "def require_trained_policy(run_dir, label=\"本轮训练\"):\n",
    "    checkpoints = list_checkpoints(run_dir)\n",
    "    if not checkpoints:\n",
    "        raise RuntimeError(\n",
    "            f\"{label}没有发现 checkpoint：{public_path(run_dir)}。\"\n",
    "            \"请先在本 Notebook 执行训练单元格，不能回退到历史或预训练权重。\"\n",
    "        )\n",
    "    path = checkpoints[-1]\n",
    "    if not path.exists():\n",
    "        raise FileNotFoundError(f\"训练 checkpoint 不存在：{public_path(path)}\")\n",
    "    print(f\"评估绑定到{label} checkpoint：\", public_path(path))\n",
    "    return path\n",
    "\n",
    "\n",
    "def summarize_jsonl(path):\n",
    "    path = Path(path)\n",
    "    if not path.exists():\n",
    "        print(\"结果 JSONL 尚不存在：\", public_path(path))\n",
    "        return None\n",
    "    rows = [json.loads(line) for line in path.read_text(encoding=\"utf-8\").splitlines() if line.strip()]\n",
    "    total = len(rows)\n",
    "    legacy = sum(bool(row.get(\"success\") or row.get(\"legacy_success\")) for row in rows)\n",
    "    if rows and all(\"physical_success\" in row for row in rows):\n",
    "        physical_count = sum(bool(row.get(\"physical_success\")) for row in rows)\n",
    "        physical_text = str(physical_count) + \"/\" + str(total)\n",
    "    else:\n",
    "        physical_text = \"未记录\"\n",
    "    md_table(\n",
    "        [\"结果文件\", \"episodes\", \"legacy_success\", \"physical_success\"],\n",
    "        [(public_path(path), total, f\"{legacy}/{total}\", physical_text)],\n",
    "    )\n",
    "    return rows\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 运行控制\n",
    "\n",
    "本 Notebook 默认执行保护训练和评估。首次运行可先将 `RUN_PROTECTED_TRAIN=False` 做环境检查；正式复现时保持 `True`。训练进度每 100 步刷新一次。\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# RUN_CONTROL_CELL\n",
    "RUN_SMOKE = False\n",
    "RUN_LONG_TRAIN = False\n",
    "RUN_EVAL = True\n",
    "RUN_PROTECTED_TRAIN = True\n",
    "os.environ[\"NOTEBOOK_PROGRESS_EVERY\"] = os.environ.get(\"NOTEBOOK_PROGRESS_EVERY\", \"100\")\n",
    "print({\"RUN_SMOKE\": RUN_SMOKE, \"RUN_LONG_TRAIN\": RUN_LONG_TRAIN, \"RUN_EVAL\": RUN_EVAL, \"RUN_PROTECTED_TRAIN\": RUN_PROTECTED_TRAIN})\n"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 保护训练配方\n",
    "\n",
    "保护初始化权重只用于训练初始化；最终评估只能使用本单元新生成的最终 checkpoint。\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "DATASET_REPO_ID = os.environ.get(\"PI0_DATASET_REPO_ID\", \"datawhale_eai_pnp_pi0_clean_oracle_y060z000_3100_3139_g8_rebuild_v1\")\n",
    "TRAIN_DATA_ROOT = Path(os.environ.get(\"PI0_TRAIN_DATA_ROOT\", DATA_ROOT / \"pi0_clean40_successonly\"))\n",
    "CONFIG_DIR = OUTPUT_ROOT / \"configs\"\n",
    "print(\"DATASET_REPO_ID =\", DATASET_REPO_ID)\n",
    "print(\"TRAIN_DATA_ROOT =\", public_path(TRAIN_DATA_ROOT))\n"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 执行 protected clean40 + blue2x 训练\n",
    "\n",
    "Pi0 会先在 Notebook 内训练 clean40 success-only parent，再从本次 parent 继续 blue2x 续训。不会跳过 parent，也不会加载外部保护结果作为评估模型。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PROTECTED_TRAIN_CELL\n",
    "protected_train_enabled = globals().get(\"RUN_PROTECTED_TRAIN\", env_flag(\"RUN_PROTECTED_TRAIN\", False))\n",
    "if not protected_train_enabled:\n",
    "    print(\"未启动。设置 RUN_PROTECTED_TRAIN=1 后，本单元会原生训练 Pi0 protected recipe。\")\n",
    "else:\n",
    "    DATASET_REPO_ID = globals().get(\"DATASET_REPO_ID\", \"datawhale_eai_pnp_pi0_clean_oracle_y060z000_3100_3139_g8_rebuild_v1\")\n",
    "    TRAIN_DATA_ROOT = globals().get(\"TRAIN_DATA_ROOT\", Path(os.environ.get(\"PI0_TRAIN_DATA_ROOT\", DATA_ROOT / \"pi0_clean40_successonly\")))\n",
    "    CONFIG_DIR = OUTPUT_ROOT / \"configs\"\n",
    "    RUN_ROOT = OUTPUT_ROOT / \"runs\" / \"pi0_protected_recipe\"\n",
    "    CLEAN_OUTPUT = RUN_ROOT / \"clean40_s7500\"\n",
    "    BLUE_OUTPUT = RUN_ROOT / \"blue2x_s8500\"\n",
    "    clean_config = make_lerobot_train_config(\n",
    "        \"pi0\", DATASET_REPO_ID, TRAIN_DATA_ROOT, CLEAN_OUTPUT,\n",
    "        steps=int(os.environ.get(\"PI0_CLEAN_STEPS\", \"7500\")),\n",
    "        batch_size=int(os.environ.get(\"PI0_BATCH_SIZE\", \"2\")),\n",
    "        chunk_size=50,\n",
    "        n_action_steps=50,\n",
    "    )\n",
    "    blue_config = make_lerobot_train_config(\n",
    "        \"pi0\", DATASET_REPO_ID, TRAIN_DATA_ROOT, BLUE_OUTPUT,\n",
    "        steps=int(os.environ.get(\"PI0_BLUE_STEPS\", \"1000\")),\n",
    "        batch_size=int(os.environ.get(\"PI0_BATCH_SIZE\", \"2\")),\n",
    "        chunk_size=50,\n",
    "        n_action_steps=50,\n",
    "    )\n",
    "    clean_path = write_json_yaml(CONFIG_DIR / \"pi0_protected_clean40_s7500.yaml\", clean_config)\n",
    "    blue_path = write_json_yaml(CONFIG_DIR / \"pi0_protected_blue2x_s8500.yaml\", blue_config)\n",
    "    clean_result = train_lerobot_config_in_notebook(clean_path, enabled=True, progress_name=\"Pi0 protected clean40\")\n",
    "    clean_checkpoints = list_checkpoints(CLEAN_OUTPUT)\n",
    "    if not clean_checkpoints:\n",
    "        raise RuntimeError(\"Pi0 protected clean40 训练没有产生父 checkpoint。\")\n",
    "    clean_ckpt = clean_checkpoints[-1]\n",
    "    old_override = os.environ.get(\"PI0_PRETRAINED_PATH_OVERRIDE\")\n",
    "    old_mode = os.environ.get(\"NOTEBOOK_FRAME_WEIGHT_MODE\")\n",
    "    old_blue = os.environ.get(\"NOTEBOOK_BLUE_WEIGHT\")\n",
    "    os.environ[\"PI0_PRETRAINED_PATH_OVERRIDE\"] = str(clean_ckpt)\n",
    "    os.environ[\"NOTEBOOK_FRAME_WEIGHT_MODE\"] = \"blue\"\n",
    "    os.environ[\"NOTEBOOK_BLUE_WEIGHT\"] = os.environ.get(\"PI0_BLUE_WEIGHT\", \"2.0\")\n",
    "    try:\n",
    "        train_lerobot_config_in_notebook(blue_path, enabled=True, progress_name=\"Pi0 protected blue2x\")\n",
    "    finally:\n",
    "        if old_override is None:\n",
    "            os.environ.pop(\"PI0_PRETRAINED_PATH_OVERRIDE\", None)\n",
    "        else:\n",
    "            os.environ[\"PI0_PRETRAINED_PATH_OVERRIDE\"] = old_override\n",
    "        if old_mode is None:\n",
    "            os.environ.pop(\"NOTEBOOK_FRAME_WEIGHT_MODE\", None)\n",
    "        else:\n",
    "            os.environ[\"NOTEBOOK_FRAME_WEIGHT_MODE\"] = old_mode\n",
    "        if old_blue is None:\n",
    "            os.environ.pop(\"NOTEBOOK_BLUE_WEIGHT\", None)\n",
    "        else:\n",
    "            os.environ[\"NOTEBOOK_BLUE_WEIGHT\"] = old_blue\n",
    "    print(\"protected candidate checkpoints:\")\n",
    "    protected_candidates = list_checkpoints(BLUE_OUTPUT)\n",
    "    if not protected_candidates:\n",
    "        raise RuntimeError(\"Pi0 protected 续训结束后没有 checkpoint，禁止进入评估。\")\n",
    "    TRAINED_POLICY_PATH = protected_candidates[-1]\n",
    "    TRAINING_SUMMARY_PATHS = [\n",
    "        CLEAN_OUTPUT / \"training_run_summary.json\",\n",
    "        BLUE_OUTPUT / \"training_run_summary.json\",\n",
    "    ]\n",
    "    print(\"本 Notebook protected checkpoint =\", public_path(TRAINED_POLICY_PATH))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 训练耗时与 checkpoint\n",
    "\n",
    "下面只读取本次训练写入的 `training_run_summary.json`，显示开始时间、结束时间、总耗时、步数和输出目录。\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "summary_paths = [Path(path) for path in globals().get(\"TRAINING_SUMMARY_PATHS\", [])]\n",
    "if not summary_paths:\n",
    "    print(\"尚未执行 protected 训练，暂无训练耗时。\")\n",
    "else:\n",
    "    summaries = []\n",
    "    for summary_path in summary_paths:\n",
    "        if summary_path.exists():\n",
    "            summaries.append(json.loads(summary_path.read_text(encoding=\"utf-8\")))\n",
    "        else:\n",
    "            print(\"尚未生成：\", public_path(summary_path))\n",
    "    if summaries:\n",
    "        md_table(\n",
    "            [\"recipe\", \"steps\", \"duration_min\", \"started_at_utc\", \"finished_at_utc\", \"output_dir\"],\n",
    "            [(item.get(\"recipe\"), item.get(\"steps\"), f\"{float(item.get('duration_s', 0.0))/60.0:.2f}\", item.get(\"started_at_utc\"), item.get(\"finished_at_utc\"), item.get(\"output_dir\")) for item in summaries],\n",
    "        )\n"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Pi0 原生 14 条评估协议\n",
    "\n",
    "Pi0 使用 EEF-absolute、median reducer 和物理成功判定。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PI0_NATIVE_FORMAL_EVAL_CELL\n",
    "import gc\n",
    "import random\n",
    "import sys\n",
    "import time\n",
    "from collections import deque\n",
    "\n",
    "import numpy as np\n",
    "import torch\n",
    "from PIL import Image\n",
    "from torchvision import transforms\n",
    "\n",
    "_PI0_NATIVE_TO_TENSOR = transforms.ToTensor()\n",
    "\n",
    "def _pi0_native_to_tensor_image(image):\n",
    "    return _PI0_NATIVE_TO_TENSOR(Image.fromarray(image).resize((256, 256)))\n",
    "\n",
    "\n",
    "def _pi0_native_make_policy(policy_path, repo_id, dataset_root, device):\n",
    "    from lerobot.common.datasets.lerobot_dataset import LeRobotDatasetMetadata\n",
    "    from lerobot.common.policies.pi0.modeling_pi0 import PI0Policy\n",
    "\n",
    "    metadata = LeRobotDatasetMetadata(repo_id, root=dataset_root)\n",
    "    policy = PI0Policy.from_pretrained(policy_path, dataset_stats=metadata.stats)\n",
    "    policy.to(device)\n",
    "    policy.eval()\n",
    "    return policy\n",
    "\n",
    "\n",
    "def _pi0_native_expected_state_dim(policy):\n",
    "    feature = policy.config.input_features[\"observation.state\"]\n",
    "    return int(tuple(feature.shape)[0])\n",
    "\n",
    "\n",
    "def _pi0_native_gripper(env):\n",
    "    try:\n",
    "        return float(np.clip(float(env.env.get_qpos_joint(\"rh_r1\")[0]), 0.0, 1.0))\n",
    "    except Exception:\n",
    "        return 0.0\n",
    "\n",
    "\n",
    "def _pi0_native_state(env, policy, action_step, hz=20.0):\n",
    "    base = np.asarray(env.get_joint_state()[:6], dtype=np.float32).reshape(-1)\n",
    "    expected = _pi0_native_expected_state_dim(policy)\n",
    "    if expected == 6:\n",
    "        return base\n",
    "    if expected == 8:\n",
    "        # Match the protected evaluator exactly: timestamp is the action\n",
    "        # schedule time, not MuJoCo wall/simulation time after the 25 physics steps.\n",
    "        timestamp = float(action_step) / float(hz)\n",
    "        return np.concatenate(\n",
    "            [base, np.asarray([_pi0_native_gripper(env), timestamp], dtype=np.float32)]\n",
    "        )\n",
    "    raise ValueError(\n",
    "        f\"Pi0 checkpoint expects observation.state={expected}D; \"\n",
    "        \"formal raw VLA protocol only supplies 6D or 8D state.\"\n",
    "    )\n",
    "\n",
    "\n",
    "def _pi0_native_debug(env, tracker):\n",
    "    target_body = env.obj_target\n",
    "    target_pos = np.asarray(env.env.get_p_body(target_body), dtype=np.float32)\n",
    "    plate_pos = np.asarray(env.env.get_p_body(\"body_obj_plate_11\"), dtype=np.float32)\n",
    "    tcp_pos = np.asarray(env.env.get_p_body(\"tcp_link\"), dtype=np.float32)\n",
    "    try:\n",
    "        upright_cos = float(env.env.get_R_body(target_body)[2, 2])\n",
    "    except Exception:\n",
    "        upright_cos = float(\"nan\")\n",
    "    terminal = bool(env.check_success())\n",
    "    lifted = int(tracker[\"lifted_steps\"]) >= 3\n",
    "    upright = bool(np.isfinite(upright_cos) and upright_cos >= 0.7)\n",
    "    return {\n",
    "        \"xy_dist\": float(np.linalg.norm(target_pos[:2] - plate_pos[:2])),\n",
    "        \"target_pos\": [round(float(x), 4) for x in target_pos],\n",
    "        \"plate_pos\": [round(float(x), 4) for x in plate_pos],\n",
    "        \"tcp_pos\": [round(float(x), 4) for x in tcp_pos],\n",
    "        \"gripper\": _pi0_native_gripper(env),\n",
    "        \"success\": terminal,\n",
    "        \"physical_success\": bool(terminal and lifted and upright),\n",
    "        \"physical_lifted_enough\": bool(lifted),\n",
    "        \"physical_final_upright\": bool(upright),\n",
    "        \"physical_min_lift\": 0.03,\n",
    "        \"physical_min_lift_steps\": 3,\n",
    "        \"physical_final_upright_cos_threshold\": 0.7,\n",
    "        \"target_body\": target_body,\n",
    "        \"initial_target_pos\": tracker[\"initial_target_pos\"],\n",
    "        \"initial_plate_pos\": tracker[\"initial_plate_pos\"],\n",
    "        \"final_target_pos\": [round(float(x), 4) for x in target_pos],\n",
    "        \"max_target_lift\": float(tracker[\"max_target_lift\"]),\n",
    "        \"lifted_steps\": int(tracker[\"lifted_steps\"]),\n",
    "        \"final_target_upright_cos\": upright_cos,\n",
    "    }\n",
    "\n",
    "\n",
    "@torch.inference_mode()\n",
    "def _pi0_native_sample_chunk(policy, batch, samples=4, exec_chunk_steps=5):\n",
    "    # This follows the configured Pi0 EEF-absolute sampling path,\n",
    "    # while keeping the implementation visible in the Notebook.\n",
    "    from lerobot.common.constants import OBS_STATE\n",
    "\n",
    "    normalized = policy.normalize_inputs(batch)\n",
    "    images, image_masks = policy.prepare_images(normalized)\n",
    "    state = policy.prepare_state(normalized)\n",
    "    language_tokens, language_masks = policy.prepare_language(normalized)\n",
    "    chunks = []\n",
    "    for _ in range(int(samples)):\n",
    "        actions = policy.model.sample_actions(\n",
    "            images, image_masks, language_tokens, language_masks, state, noise=None\n",
    "        )\n",
    "        action_dim = policy.config.action_feature.shape[0]\n",
    "        actions = actions[:, :, :action_dim]\n",
    "        actions = policy.unnormalize_outputs({\"action\": actions})[\"action\"]\n",
    "        if getattr(policy.config, \"adapt_to_pi_aloha\", False):\n",
    "            actions = policy._pi_aloha_encode_actions(actions)\n",
    "        chunks.append(actions)\n",
    "    selected = torch.stack(chunks, dim=0).median(dim=0).values\n",
    "    # Keep a coherent short suffix from one predicted chunk before replanning.\n",
    "    return deque(selected[0, : int(exec_chunk_steps)].unbind(0))\n",
    "\n",
    "\n",
    "def _pi0_native_configure_env(env):\n",
    "    # The historical protected evaluator sets this reference pose once per episode.\n",
    "    # Recomputing env.p0 on every action changes the MuJoCo eef_pose controller.\n",
    "    from mujoco_env.transforms import rpy2r\n",
    "\n",
    "    env.action_type = \"eef_pose\"\n",
    "    env.p0, _ = env.env.get_pR_body(body_name=\"tcp_link\")\n",
    "    env.R0 = rpy2r(np.deg2rad([90.0, 0.0, 90.0]))\n",
    "\n",
    "\n",
    "def _pi0_native_eef_abs_to_env(action, env, max_step=0.004):\n",
    "    current_tcp = np.asarray(env.env.get_p_body(\"tcp_link\")[:3], dtype=np.float32)\n",
    "    target_tcp = np.asarray(action[:3], dtype=np.float32)\n",
    "    env_action = np.zeros(7, dtype=np.float32)\n",
    "    env_action[:3] = np.clip(target_tcp - current_tcp, -float(max_step), float(max_step))\n",
    "    env_action[6] = float(np.clip(action[6], 0.0, 1.0))\n",
    "    return env_action\n",
    "\n",
    "\n",
    "def _pi0_native_close_env(env):\n",
    "    try:\n",
    "        env.env.close_viewer()\n",
    "    except Exception:\n",
    "        pass\n",
    "    gc.collect()\n",
    "\n",
    "\n",
    "def run_pi0_eval_native_in_notebook(\n",
    "    policy_path,\n",
    "    result_path,\n",
    "    seeds,\n",
    "    repo_id,\n",
    "    dataset_root,\n",
    "    enabled=False,\n",
    "    render=False,\n",
    "    video_dir=None,\n",
    "    trace_dir=None,\n",
    "):\n",
    "    print(\"Pi0 formal checkpoint =\", public_path(policy_path))\n",
    "    print(\"Pi0 formal seeds =\", list(seeds))\n",
    "    print(\"Pi0 protocol = eef_abs / samples=4 / reducer=median / execute=5 / state=8D / physics=25\")\n",
    "    if not enabled:\n",
    "        print(\"未启动。设置 RUN_EVAL=1 后运行 Pi0 正式保护协议。\")\n",
    "        return None\n",
    "    if not ensure_project_layout():\n",
    "        return None\n",
    "\n",
    "    from contextlib import contextmanager\n",
    "    import argparse\n",
    "    media_module = load_eval_module()\n",
    "\n",
    "    project_path = str(Path(PROJECT_ROOT).resolve())\n",
    "    if project_path not in sys.path:\n",
    "        sys.path.insert(0, project_path)\n",
    "    from mujoco_env.y_env2 import SimpleEnv2\n",
    "\n",
    "    @contextmanager\n",
    "    def pushd(path):\n",
    "        old = Path.cwd()\n",
    "        os.chdir(path)\n",
    "        try:\n",
    "            yield\n",
    "        finally:\n",
    "            os.chdir(old)\n",
    "\n",
    "    ensure_xvfb_display()\n",
    "    result_path = Path(result_path)\n",
    "    result_path.parent.mkdir(parents=True, exist_ok=True)\n",
    "    existing_rows = []\n",
    "    completed_seeds = set()\n",
    "    if result_path.exists():\n",
    "        # Resume after an interrupted long Notebook run without duplicating seeds.\n",
    "        for line in result_path.read_text(encoding=\"utf-8\").splitlines():\n",
    "            line = line.strip()\n",
    "            if not line:\n",
    "                continue\n",
    "            row = json.loads(line)\n",
    "            existing_rows.append(row)\n",
    "            if row.get(\"seed\") is not None:\n",
    "                completed_seeds.add(int(row[\"seed\"]))\n",
    "        print(\"Pi0 resume rows =\", len(existing_rows), \"completed seeds =\", sorted(completed_seeds))\n",
    "    run_seeds = [int(seed) for seed in seeds if int(seed) not in completed_seeds]\n",
    "    if not run_seeds:\n",
    "        print(\"Pi0 formal result already complete; no seed will be repeated.\")\n",
    "\n",
    "    device = os.environ.get(\"EVAL_DEVICE\", \"cuda\")\n",
    "    with pushd(PROJECT_ROOT):\n",
    "        policy = _pi0_native_make_policy(Path(policy_path), repo_id, Path(dataset_root), device)\n",
    "        expected_dim = _pi0_native_expected_state_dim(policy)\n",
    "        print(\"Pi0 observation.state dimension =\", expected_dim)\n",
    "        if expected_dim not in (6, 8):\n",
    "            raise ValueError(f\"Unsupported Pi0 state dimension: {expected_dim}\")\n",
    "\n",
    "        rows = list(existing_rows)\n",
    "        media_args = argparse.Namespace(policy=\"pi0\", video_dir=Path(video_dir) if video_dir else None, trace_dir=Path(trace_dir) if trace_dir else None)\n",
    "        selected_cases = set()\n",
    "        for seed in notebook_progress(run_seeds, desc=\"pi0 formal eval\", total=len(run_seeds)):\n",
    "            env = SimpleEnv2(\"./asset/example_scene_y2.xml\", action_type=\"joint_angle\")\n",
    "            try:\n",
    "                try:\n",
    "                    env.env.reset(step=False)\n",
    "                except TypeError:\n",
    "                    env.env.reset()\n",
    "                env.reset(seed=int(seed))\n",
    "                random.seed(int(seed))\n",
    "                np.random.seed(int(seed))\n",
    "                torch.manual_seed(int(seed))\n",
    "                if torch.cuda.is_available():\n",
    "                    torch.cuda.manual_seed_all(int(seed))\n",
    "                _pi0_native_configure_env(env)\n",
    "                policy.reset()\n",
    "                tracker = {\n",
    "                    \"initial_target_pos\": [float(x) for x in env.env.get_p_body(env.obj_target)],\n",
    "                    \"initial_plate_pos\": [float(x) for x in env.env.get_p_body(\"body_obj_plate_11\")],\n",
    "                    \"initial_target_z\": float(env.env.get_p_body(env.obj_target)[2]),\n",
    "                    \"max_target_lift\": 0.0,\n",
    "                    \"lifted_steps\": 0,\n",
    "                }\n",
    "                action_queue = deque()\n",
    "                action_steps = 0\n",
    "                sim_steps = 0\n",
    "                success_ever = False\n",
    "                physical_success_ever = False\n",
    "                first_success_step = None\n",
    "                first_physical_success_step = None\n",
    "                start = time.time()\n",
    "                frames = [] if video_dir is not None else None\n",
    "                trace = []\n",
    "                while action_steps < int(os.environ.get(\"PI0_EVAL_MAX_ACTION_STEPS\", \"700\")) and env.env.is_viewer_alive():\n",
    "                    for _ in range(25):\n",
    "                        env.step_env()\n",
    "                        sim_steps += 1\n",
    "                        target_z = float(env.env.get_p_body(env.obj_target)[2])\n",
    "                        lift = target_z - tracker[\"initial_target_z\"]\n",
    "                        tracker[\"max_target_lift\"] = max(tracker[\"max_target_lift\"], lift)\n",
    "                        if lift >= 0.03:\n",
    "                            tracker[\"lifted_steps\"] += 1\n",
    "                    debug = _pi0_native_debug(env, tracker)\n",
    "                    if debug[\"success\"]:\n",
    "                        success_ever = True\n",
    "                        first_success_step = action_steps if first_success_step is None else first_success_step\n",
    "                    if debug[\"physical_success\"]:\n",
    "                        physical_success_ever = True\n",
    "                        first_physical_success_step = action_steps if first_physical_success_step is None else first_physical_success_step\n",
    "                        break\n",
    "\n",
    "                    state = _pi0_native_state(env, policy, action_steps)\n",
    "                    image, wrist_image = env.grab_image()\n",
    "                    media_module._capture_panel(frames, image, wrist_image)\n",
    "                    batch = {\n",
    "                        \"observation.state\": torch.tensor([state], dtype=torch.float32, device=device),\n",
    "                        \"observation.image\": _pi0_native_to_tensor_image(image).unsqueeze(0).to(device),\n",
    "                        \"observation.wrist_image\": _pi0_native_to_tensor_image(wrist_image).unsqueeze(0).to(device),\n",
    "                        \"task\": [env.instruction],\n",
    "                    }\n",
    "                    if not action_queue:\n",
    "                        action_queue = _pi0_native_sample_chunk(policy, batch, samples=4, exec_chunk_steps=5)\n",
    "                    action = action_queue.popleft().detach().float().cpu().numpy()\n",
    "                    env.step(_pi0_native_eef_abs_to_env(action, env))\n",
    "                    action_steps += 1\n",
    "                    trace.append({\"step\": int(action_steps), \"action\": [float(x) for x in action]})\n",
    "                    trace.append({\"step\": int(action_steps), \"action\": [float(x) for x in action]})\n",
    "                    trace.append({\"step\": int(action_steps), \"action\": [float(x) for x in action]})\n",
    "                    if render:\n",
    "                        env.render()\n",
    "\n",
    "                    debug = _pi0_native_debug(env, tracker)\n",
    "                    if debug[\"success\"]:\n",
    "                        success_ever = True\n",
    "                        first_success_step = action_steps if first_success_step is None else first_success_step\n",
    "                    if debug[\"physical_success\"]:\n",
    "                        physical_success_ever = True\n",
    "                        first_physical_success_step = action_steps if first_physical_success_step is None else first_physical_success_step\n",
    "                        break\n",
    "\n",
    "                debug = _pi0_native_debug(env, tracker)\n",
    "                row = {\n",
    "                    \"policy\": \"pi0\",\n",
    "                    \"seed\": int(seed),\n",
    "                    \"success\": bool(success_ever or debug[\"success\"]),\n",
    "                    \"physical_success\": bool(physical_success_ever or debug[\"physical_success\"]),\n",
    "                    \"action_steps\": int(action_steps),\n",
    "                    \"sim_steps\": int(sim_steps),\n",
    "                    \"elapsed_s\": round(time.time() - start, 3),\n",
    "                    \"instruction\": getattr(env, \"instruction\", None),\n",
    "                    \"debug\": debug,\n",
    "                }\n",
    "                row[\"_media\"] = media_module._write_episode_media(media_args, seed, frames, trace)\n",
    "                row = media_module.finalize_case_media(media_args, row, selected_cases)\n",
    "                row[\"_media\"] = media_module._write_episode_media(media_args, seed, frames, trace)\n",
    "                row = media_module.finalize_case_media(media_args, row, selected_cases)\n",
    "                row[\"_media\"] = media_module._write_episode_media(media_args, seed, frames, trace)\n",
    "                row = media_module.finalize_case_media(media_args, row, selected_cases)\n",
    "                rows.append(row)\n",
    "                with result_path.open(\"a\", encoding=\"utf-8\") as f:\n",
    "                    f.write(json.dumps(row, ensure_ascii=False) + \"\\n\")\n",
    "                print(json.dumps(row, ensure_ascii=False))\n",
    "            finally:\n",
    "                _pi0_native_close_env(env)\n",
    "                if torch.cuda.is_available():\n",
    "                    torch.cuda.empty_cache()\n",
    "\n",
    "    summarize_jsonl(result_path)\n",
    "    success_count = sum(bool(row[\"success\"]) for row in rows)\n",
    "    physical_count = sum(bool(row[\"physical_success\"]) for row in rows)\n",
    "    print(f\"Pi0 formal result: success={success_count}/{len(rows)}, physical_success={physical_count}/{len(rows)}\")\n",
    "    return rows\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# FULL_14_EVAL_WITH_MEDIA\n",
    "FULL_EVAL_EPISODES = 14\n",
    "FULL_EVAL_SEEDS = [3007, 3002, 3004, 3006, 3010, 3011, 3012, 3013, 3014, 3015, 3016, 3017, 3018, 3019]\n",
    "FULL_EVAL_ROOT = OUTPUT_ROOT / \"full_eval_14\"\n",
    "FULL_EVAL_RESULT = FULL_EVAL_ROOT / \"result.jsonl\"\n",
    "if not globals().get(\"RUN_EVAL\", False):\n",
    "    print(\"未启动评估。请将 RUN_EVAL=True 后执行本单元。\")\n",
    "else:\n",
    "    if globals().get(\"TRAINED_POLICY_PATH\") is None:\n",
    "        raise RuntimeError(\"本 Notebook 没有当前训练 checkpoint；请先执行本 Notebook 的训练单元格。\")\n",
    "    FULL_EVAL_POLICY = Path(TRAINED_POLICY_PATH)\n",
    "    if not FULL_EVAL_POLICY.exists():\n",
    "        raise FileNotFoundError(f\"当前训练 checkpoint 不存在：{public_path(FULL_EVAL_POLICY)}\")\n",
    "    print(\"评估模型（来自本 Notebook 当前训练）：\", public_path(FULL_EVAL_POLICY))\n",
    "    FULL_EVAL_ROWS = run_pi0_eval_native_in_notebook(\n",
    "        FULL_EVAL_POLICY, FULL_EVAL_RESULT, FULL_EVAL_SEEDS,\n",
    "        repo_id=DATASET_REPO_ID, dataset_root=TRAIN_DATA_ROOT, render=True, enabled=True,\n",
    "        video_dir=FULL_EVAL_ROOT / \"videos\", trace_dir=FULL_EVAL_ROOT / \"traces\",\n",
    "    )\n",
    "FULL_EVAL_ROWS = run_pi0_eval_native_in_notebook(\n",
    "    FULL_EVAL_POLICY,\n",
    "    FULL_EVAL_RESULT,\n",
    "    FULL_EVAL_SEEDS,\n",
    "    repo_id=DATASET_REPO_ID,\n",
    "    dataset_root=TRAIN_DATA_ROOT,\n",
    "    render=True,\n",
    "    enabled=RUN_EVAL,\n",
    "    video_dir=FULL_EVAL_ROOT / \"videos\",\n",
    "    trace_dir=FULL_EVAL_ROOT / \"traces\",\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# FULL_14_MEDIA_REVIEW\n",
    "from IPython.display import Video\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def display_full_eval_media(result_path, title=\"pi0 - 14 episode evaluation\"):\n",
    "    result_path = Path(result_path)\n",
    "    if not result_path.exists():\n",
    "        print(\"Evaluation has not run yet:\", public_path(result_path))\n",
    "        return\n",
    "    rows = [json.loads(line) for line in result_path.read_text(encoding=\"utf-8\").splitlines() if line.strip()]\n",
    "    success = sum(bool(row.get(\"physical_success\", row.get(\"success\"))) for row in rows)\n",
    "    print(f\"{title}: physical_success={success}/{len(rows)}\")\n",
    "    md_table([[\"metric\", \"value\"]][0], [[\"physical success\", f\"{success}/{len(rows)}\"]])\n",
    "    for kind in (\"success\", \"failure\"):\n",
    "        row = next((item for item in rows if bool(item.get(\"physical_success\", item.get(\"success\"))) == (kind == \"success\") and item.get(\"video_path\")), None)\n",
    "        if row is None:\n",
    "            print(f\"No {kind} episode was found.\")\n",
    "            continue\n",
    "        print(f\"{kind} case: seed={row['seed']}\")\n",
    "        video_path = Path(row[\"video_path\"])\n",
    "        if video_path.exists():\n",
    "            display(Video(filename=str(video_path), embed=False, width=960))\n",
    "        trace_path = Path(row.get(\"trace_path\", \"\"))\n",
    "        if not trace_path.exists():\n",
    "            continue\n",
    "        trace = json.loads(trace_path.read_text(encoding=\"utf-8\"))\n",
    "        actions = np.asarray([item.get(\"action\", []) for item in trace], dtype=np.float32)\n",
    "        if actions.ndim != 2 or not len(actions):\n",
    "            continue\n",
    "        fig, ax = plt.subplots(figsize=(12, 4))\n",
    "        channels = min(actions.shape[1], 7)\n",
    "        for channel in range(channels):\n",
    "            label = \"gripper\" if channel == 6 else f\"action[{channel}]\"\n",
    "            ax.plot(actions[:, channel], label=label, linewidth=1.2)\n",
    "        ax.set_title(f\"{title} - {kind} sequence, seed={row['seed']}\")\n",
    "        ax.set_xlabel(\"policy step\")\n",
    "        ax.set_ylabel(\"normalized action / command\")\n",
    "        ax.grid(alpha=0.25)\n",
    "        ax.legend(ncol=4, fontsize=8)\n",
    "        plt.tight_layout()\n",
    "        display(fig)\n",
    "        plt.close(fig)\n",
    "\n",
    "display_full_eval_media(FULL_EVAL_RESULT)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. 复现检查\n",
    "\n",
    "评估结果必须来自本 Notebook 当前训练生成的 `TRAINED_POLICY_PATH`。没有当前 checkpoint 时，评估单元会直接报错。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
