{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# S9 lab notebook — fine-tune the caption model on Apple Silicon\n",
    "\n",
    "The unit's base model writes fluent but generic marketing copy. Fine-tune a **LoRA adapter** on 400 synthetic catalogue rows and compare the base and tuned captions with token-F1.\n",
    "\n",
    "**Runtime:** Apple Silicon, macOS 14+, 16 GB RAM or more. The model is `mlx-community/Qwen3.5-4B-MLX-4bit` (4-bit, about 2.9 GB). The full run takes about 10–15 minutes: 40 s to load, 6–8 minutes for 100 iterations, 2–3 minutes to evaluate.\n",
    "\n",
    "**You submit:** `results/mlx_results.json`, the base-vs-tuned table, three sample captions and a three-sentence *what this proves* note.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "โมเดลฐานของหน่วยเขียนข้อความการตลาดที่ลื่นไหลแต่ทั่วไป ในโน้ตบุ๊กนี้คุณจะฝึก LoRA adapter บนข้อมูลแคตตาล็อกสังเคราะห์ 400 แถว แล้วเปรียบเทียบคำบรรยายของโมเดลฐานและโมเดลที่ปรับด้วย token-F1 ใช้เวลา 10–15 นาทีบนแมค Apple Silicon และส่งไฟล์ผลลัพธ์ ตารางเปรียบเทียบ ตัวอย่างสามรายการ และสรุปสั้น ๆ\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "单位的基础模型写出的营销文案流畅但泛泛。在这个 notebook 中，你将在 400 行的合成目录数据上训练 LoRA 适配器，然后用 token-F1 比较基础模型与微调模型的字幕。在 Apple Silicon Mac 上需要 10–15 分钟，并提交结果文件、对比表、三个条目示例和简短总结。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Install the training extra (one time, and only if it is missing)\n",
    "\n",
    "This is the single documented `pip install` exception in the course notebook standard. The cell below checks before it installs anything: if `mlx`, `mlx-metal` and `mlx-vlm` with its `train` extra are already present at the pinned versions — the case in the course kit environment — it prints **already installed — nothing to do** and runs no installer. If they are missing (a fresh virtual environment, or a kit installed without the `s09` group), it installs the verified set once and asks you to restart the kernel.\n",
    "\n",
    "The pins are not a preference, they are a compatibility fact: `mlx-vlm 0.7.12` requires `mlx>=0.32.4`, so the older `0.31.2` pin resolves to nothing at all. `mlx-metal` must match `mlx` exactly because it carries the Metal kernels for that release, and the `train` extra is what provides `mlx_vlm.trainer`; plain `mlx-vlm` ships inference only. If `import mlx.core` fails with a missing `libmlx.dylib`, reinstalling this exact trio is the fix.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "นี่เป็นข้อยกเว้นเดียวของมาตรฐานโน้ตบุ๊กที่อนุญาตให้ติดตั้งด้วย pip เซลล์ด้านล่างตรวจสอบก่อนเสมอ: ถ้า mlx, mlx-metal และ mlx-vlm พร้อม extra ชื่อ train ติดตั้งอยู่แล้วในเวอร์ชันที่กำหนด ซึ่งเป็นกรณีของสภาพแวดล้อมชุดคอร์ส เซลล์จะพิมพ์ว่าติดตั้งแล้วและไม่เรียก pip เลย ถ้ายังไม่มี เซลล์จะติดตั้งชุดที่ทดสอบแล้วหนึ่งครั้งและให้รีสตาร์ตเคอร์เนล เวอร์ชันเหล่านี้ไม่ใช่ความชอบส่วนตัวแต่เป็นข้อเท็จจริงเรื่องความเข้ากันได้: mlx-vlm 0.7.12 ต้องการ mlx อย่างน้อย 0.32.4 เวอร์ชัน 0.31.2 เดิมจึงแก้ไม่ได้เลย mlx-metal ต้องตรงกับ mlx เพราะมีเคอร์เนล Metal ของรุ่นนั้น และ extra ชื่อ train เป็นตัวที่ให้โมดูล mlx_vlm.trainer\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "这是 notebook 标准中唯一允许用 pip 安装的例外。下面的单元格总是先检查：如果 mlx、mlx-metal 和带名为 train 的 extra 的 mlx-vlm 已按指定版本安装——课程套件环境就是这种情况——单元格会打印已安装，并且完全不调用 pip。如果尚未安装，单元格会安装一次经过测试的套件，并要求重启内核。这些版本不是个人偏好，而是兼容性事实：mlx-vlm 0.7.12 至少需要 mlx 0.32.4，所以原来的 0.31.2 版本根本无法修复；mlx-metal 必须与 mlx 匹配，因为它带有该版本的 Metal 内核；而名为 train 的 extra 才是提供 mlx_vlm.trainer 模块的那一项。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import importlib.metadata\n",
    "\n",
    "# The verified Apple-silicon set. mlx-vlm 0.7.12 declares mlx>=0.32.4, so these\n",
    "# exact pins can never resolve to a lower mlx.\n",
    "PINS = {\"mlx\": \"0.32.4\", \"mlx-metal\": \"0.32.4\", \"mlx-vlm\": \"0.7.12\"}\n",
    "\n",
    "def installed_version(name):\n",
    "    try:\n",
    "        return importlib.metadata.version(name)\n",
    "    except importlib.metadata.PackageNotFoundError:\n",
    "        return None\n",
    "\n",
    "wrong = {}\n",
    "for name, want in PINS.items():\n",
    "    got = installed_version(name)\n",
    "    if got != want:\n",
    "        wrong[name] = f\"{name} {got or 'missing'} (want {want})\"\n",
    "\n",
    "try:\n",
    "    import mlx.core  # noqa: F401  (mlx-metal ships Metal kernels, not a module; checked above)\n",
    "    import mlx_vlm.trainer  # noqa: F401  (the train extra)\n",
    "    imports_ok = True\n",
    "except Exception as exc:  # a missing or mismatched mlx-metal can raise more than ImportError\n",
    "    imports_ok = False\n",
    "    wrong[\"import\"] = str(exc)\n",
    "\n",
    "if imports_ok and not wrong:\n",
    "    print(\"mlx 0.32.4, mlx-metal 0.32.4 and mlx-vlm[train] 0.7.12 are already \"\n",
    "          \"installed — nothing to do.\")\n",
    "else:\n",
    "    print(\"not usable yet:\", \"; \".join(wrong.values()))\n",
    "    print(\"installing mlx==0.32.4, mlx-metal==0.32.4, mlx-vlm[train]==0.7.12 ...\")\n",
    "    %pip install -q \"mlx==0.32.4\" \"mlx-metal==0.32.4\" \"mlx-vlm[train]==0.7.12\"\n",
    "    print(\"installed — restart the kernel once so the new modules load, \"\n",
    "          \"then run the cells below.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import random\n",
    "import re\n",
    "import sys\n",
    "import time\n",
    "from collections import Counter\n",
    "from pathlib import Path\n",
    "\n",
    "import mlx.core as mx\n",
    "import mlx.optimizers as optim\n",
    "from mlx_vlm import generate, load\n",
    "from mlx_vlm.prompt_utils import apply_chat_template\n",
    "from mlx_vlm.trainer import Trainer, VLMTrainingArgs, prepare_model_for_training\n",
    "\n",
    "SEED = 42\n",
    "random.seed(SEED)\n",
    "mx.random.seed(SEED)\n",
    "print(\"seed\", SEED)\n",
    "print(\"Python\", sys.version.split()[0])\n",
    "assert sys.version_info >= (3, 11)\n",
    "\n",
    "def data_dir():\n",
    "    # Local folder, explicit COURSE_DATA, the classic Kaggle mount, then whatever\n",
    "    # layout Kaggle actually used (it now nests attached data under /kaggle/input/datasets).\n",
    "    candidates = [Path(\"data\"), Path(\"/kaggle/input/llm-inference-finetuning\")]\n",
    "    env = os.environ.get(\"COURSE_DATA\")\n",
    "    if env:\n",
    "        candidates.append(Path(env))\n",
    "    kaggle = Path(\"/kaggle/input\")\n",
    "    if kaggle.exists():\n",
    "        candidates.extend(p.parent for p in sorted(kaggle.rglob(\"caption_catalogue.jsonl\")))\n",
    "    for path in candidates:\n",
    "        if path and (path / \"caption_catalogue.jsonl\").exists():\n",
    "            return path\n",
    "    raise FileNotFoundError(\n",
    "        \"put caption_catalogue.jsonl in data/, attach it as a Kaggle dataset, or set COURSE_DATA\")\n",
    "\n",
    "DATA = data_dir() / \"caption_catalogue.jsonl\"\n",
    "OUT = Path(\"results\")\n",
    "OUT.mkdir(exist_ok=True)\n",
    "MODEL_ID = \"mlx-community/Qwen3.5-4B-MLX-4bit\"\n",
    "ITERS = int(os.environ.get(\"S9_ITERS\", \"100\"))\n",
    "EVAL_ROWS = int(os.environ.get(\"S9_EVAL_ROWS\", \"10\"))\n",
    "LORA_RANK, LORA_ALPHA = 8, 16\n",
    "\n",
    "print(\"data\", DATA)\n",
    "print(\"device\", mx.default_device())\n",
    "print(\"iters\", ITERS, \"eval rows\", EVAL_ROWS)\n",
    "assert DATA.exists()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Load the catalogue and build chat rows\n",
    "\n",
    "Each row has metadata and a reference caption. The system prompt fixes the unit's voice; `train_on_completions=True` later masks the prompt so the adapter learns only from the caption.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "แต่ละแถวมีเมตาดาตาและคำบรรยายอ้างอิง พรอมต์ระบบกำหนดเสียงของหน่วย และการฝึกจะมาสก์พรอมต์เพื่อให้อะแดปเตอร์เรียนรู้จากคำบรรยายเท่านั้น\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "每一行都有元数据和参考字幕，系统 prompt（提示词）固定单位的语气，而训练会掩码提示词，使适配器只从字幕中学习。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SYSTEM = (\"You write one-sentence captions for a campus media unit's catalogue. \"\n",
    "          \"Use the metadata and keep the unit's concise English voice. \"\n",
    "          \"Answer with the caption only; do not show reasoning.\")\n",
    "\n",
    "def prompt_messages(row):\n",
    "    return [\n",
    "        {\"role\": \"system\", \"content\": SYSTEM},\n",
    "        {\"role\": \"user\", \"content\": \"Metadata:\\n\" + json.dumps(row[\"metadata\"], indent=2) +\n",
    "                                    \"\\n\\nWrite the caption.\"},\n",
    "    ]\n",
    "\n",
    "def train_row(row):\n",
    "    return {\"messages\": prompt_messages(row) + [{\"role\": \"assistant\", \"content\": row[\"caption\"]}]}\n",
    "\n",
    "rows = [json.loads(line) for line in DATA.open(encoding=\"utf-8\") if line.strip()]\n",
    "assert len(rows) == 1000\n",
    "train_rows = [r for r in rows if r[\"split\"] == \"train\"]\n",
    "val_rows = [r for r in rows if r[\"split\"] == \"validation\"]\n",
    "assert len(train_rows) == 800 and len(val_rows) == 200\n",
    "assert all({\"id\", \"split\", \"metadata\", \"title\", \"caption\"} <= set(r) for r in rows)\n",
    "\n",
    "train = [train_row(r) for r in train_rows][: ITERS * 4]\n",
    "val = [train_row(r) for r in val_rows][:EVAL_ROWS]\n",
    "assert len(train) == ITERS * 4\n",
    "assert len(val) == EVAL_ROWS\n",
    "print(len(train), \"train rows;\", len(val), \"validation rows\")\n",
    "print(train[0][\"messages\"][-1][\"content\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Load the 4-bit model\n",
    "\n",
    "The 4-bit conversion keeps the weights near 2.9 GB; the adapter adds a few megabytes. The base model stays frozen.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "การแปลงเป็น 4 บิตทำให้น้ำหนักประมาณ 2.9 จิกะไบต์ และอะแดปเตอร์เพิ่มเพียงไม่กี่เมกะไบต์ โมเดลฐานยังคงถูกแช่แข็ง\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "4 位量化使权重大约为 2.9 GB，而适配器只增加几 MB。基础模型保持冻结。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t0 = time.time()\n",
    "model, processor = load(MODEL_ID)\n",
    "load_s = round(time.time() - t0, 1)\n",
    "print(f\"loaded in {load_s} s\")\n",
    "assert load_s > 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Evaluate the base model\n",
    "\n",
    "Token-F1 compares the words of the prediction with the reference. It is a teaching metric: it rewards factual overlap and punishes fluent filler, but it does not judge style.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "token-F1 เปรียบเทียบคำในคำตอบกับคำอ้างอิง เป็นตัวชี้วัดเพื่อการสอน: ให้คะแนนความตรงของข้อเท็จจริงและลงโทษคำฟุ่มเฟือย แต่ไม่ได้ตัดสินสไตล์\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "token-F1 把回答中的词与参考词进行比较。它是用于教学的指标：它为事实准确性打分，并惩罚冗余词语，但不评判风格。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tokens(text):\n",
    "    return re.findall(r\"[a-z0-9]+\", text.lower())\n",
    "\n",
    "def token_f1(pred, ref):\n",
    "    p, r = Counter(tokens(pred)), Counter(tokens(ref))\n",
    "    overlap = sum((p & r).values())\n",
    "    if not overlap:\n",
    "        return 0.0\n",
    "    precision = overlap / max(1, sum(p.values()))\n",
    "    recall = overlap / max(1, sum(r.values()))\n",
    "    return 2 * precision * recall / (precision + recall)\n",
    "\n",
    "def caption(messages):\n",
    "    try:\n",
    "        prompt = apply_chat_template(processor, model.config, messages, num_images=0,\n",
    "                                     add_generation_prompt=True, enable_thinking=False)\n",
    "    except TypeError:\n",
    "        prompt = apply_chat_template(processor, model.config, messages, num_images=0,\n",
    "                                     add_generation_prompt=True)\n",
    "    return generate(model, processor, prompt, image=None, max_tokens=48,\n",
    "                    verbose=False).text.strip()\n",
    "\n",
    "def evaluate(label):\n",
    "    t = time.time()\n",
    "    scores, samples = [], []\n",
    "    for row in val_rows:\n",
    "        if len(scores) >= EVAL_ROWS:\n",
    "            break\n",
    "        pred = caption(prompt_messages(row))\n",
    "        scores.append(token_f1(pred, row[\"caption\"]))\n",
    "        if len(samples) < 3:\n",
    "            samples.append({\"reference\": row[\"caption\"], \"prediction\": pred,\n",
    "                            \"f1\": round(scores[-1], 3)})\n",
    "    result = {\"f1\": round(sum(scores) / max(1, len(scores)), 4),\n",
    "              \"seconds\": round(time.time() - t, 1), \"samples\": samples}\n",
    "    print(label, \"f1\", result[\"f1\"])\n",
    "    assert 0.0 <= result[\"f1\"] <= 1.0\n",
    "    return result\n",
    "\n",
    "base = evaluate(\"base\")\n",
    "assert len(base[\"samples\"]) >= 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Train the LoRA adapter\n",
    "\n",
    "LoRA freezes the base and trains small low-rank matrices on the language layers. `train_on_completions=True` masks the prompt and learns only from the assistant caption. The memory peak is the number to watch: it should stay near 4–5 GB on this model.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "LoRA แช่แข็งโมเดลฐานและฝึกเมทริกซ์ขนาดเล็กบนชั้นภาษา การฝึกจะมาสก์พรอมต์และเรียนรู้จากคำบรรยายของผู้ช่วยเท่านั้น ค่าความจำสูงสุดที่ควรจับตาคือประมาณ 4–5 จิกะไบต์\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "LoRA 冻结基础模型，并在语言层上训练小型矩阵。训练会掩码提示词，只从助手的字幕中学习。应当关注的峰值内存约为 4–5 GB。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = prepare_model_for_training(model, lora_rank=LORA_RANK, lora_alpha=LORA_ALPHA,\n",
    "                                   verbose=True)\n",
    "args = VLMTrainingArgs(\n",
    "    iters=ITERS,\n",
    "    batch_size=1,\n",
    "    gradient_accumulation_steps=4,\n",
    "    max_seq_length=1024,\n",
    "    train_on_completions=True,\n",
    "    val_batches=EVAL_ROWS,\n",
    "    steps_per_report=10,\n",
    "    steps_per_eval=ITERS,\n",
    "    steps_per_save=1000,\n",
    "    grad_checkpoint=True,\n",
    "    adapter_file=str(OUT / \"mlx_adapter.safetensors\"),\n",
    ")\n",
    "trainer = Trainer(\n",
    "    model, args,\n",
    "    optimizer=optim.AdamW(learning_rate=1e-5),\n",
    "    processing_class=processor,\n",
    "    train_dataset=train,\n",
    "    eval_dataset=val,\n",
    ")\n",
    "t = time.time()\n",
    "metrics = trainer.train()\n",
    "train_s = round(time.time() - t, 1)\n",
    "t = time.time()\n",
    "validation = trainer.evaluate()\n",
    "eval_s = round(time.time() - t, 1)\n",
    "trainer.save_adapter()\n",
    "assert train_s > 0 and eval_s > 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Evaluate the tuned model and save the result\n",
    "\n",
    "The tuned adapter should move the caption toward the reference: fewer generic marketing phrases, more of the metadata's facts. One iteration will not move the score much; the full run is the comparison.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "อะแดปเตอร์ที่ปรับแล้วควรทำให้คำบรรยายเข้าใกล้อ้างอิงมากขึ้น: ลดวลีการตลาดทั่วไปและเพิ่มข้อเท็จจริงจากเมตาดาตา การฝึกหนึ่งรอบจะยังไม่เปลี่ยนคะแนนมากนัก การรันเต็มจึงเป็นการเปรียบเทียบจริง\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "微调后的适配器应当让字幕更接近参考字幕：减少泛泛的营销短语，增加来自元数据的事实。训练一轮（epoch）还不会让分数变化太多，因此完整运行才是真正的比较。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuned = evaluate(\"tuned\")\n",
    "peak = None\n",
    "try:\n",
    "    peak = round(mx.get_peak_memory() / 1e9, 2)\n",
    "except Exception:\n",
    "    pass\n",
    "\n",
    "result = {\n",
    "    \"runtime\": \"mlx-vlm\",\n",
    "    \"model\": MODEL_ID,\n",
    "    \"quantization\": \"4bit\",\n",
    "    \"seed\": SEED,\n",
    "    \"load_seconds\": load_s,\n",
    "    \"train_rows\": len(train),\n",
    "    \"eval_rows\": EVAL_ROWS,\n",
    "    \"iters\": ITERS,\n",
    "    \"lora\": {\"r\": LORA_RANK, \"alpha\": LORA_ALPHA},\n",
    "    \"train_seconds\": train_s,\n",
    "    \"eval_seconds\": eval_s,\n",
    "    \"peak_memory_gb\": peak,\n",
    "    \"base\": base,\n",
    "    \"tuned\": tuned,\n",
    "    \"validation\": validation,\n",
    "}\n",
    "(OUT / \"mlx_results.json\").write_text(json.dumps(result, indent=2, default=str))\n",
    "print(json.dumps(result, indent=2, default=str))\n",
    "assert (OUT / \"mlx_results.json\").exists()\n",
    "assert len(tuned[\"samples\"]) >= 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What this proves\n",
    "\n",
    "Write three sentences: what the base model got wrong, what the tuned model changed, and why a LoRA adapter is cheaper than retraining the whole model. Attach `results/mlx_results.json` and three sample captions.\n",
    "\n",
    "If the run is slow, reduce `ITERS` to 50; if memory is tight, keep `grad_checkpoint=True` and `max_seq_length=1024`.\n",
    "\n",
    "## Verification\n",
    "\n",
    "**Mac (Apple silicon, macOS 27.0.1, Python 3.14.2, mlx 0.32.4, mlx-metal 0.32.4, mlx-vlm 0.7.12):** verified end to end on 2026-11-15. The install cell printed *already installed — nothing to do* and ran no installer. With `S9_ITERS=2` and `S9_EVAL_ROWS=2`, the run wrote `results/mlx_results.json`: the cached 4-bit model loaded in **3.9 s** (a first run downloads about 2.9 GB before that), 2 training iterations took **7.6 s**, evaluation took **1.2 s**, peak memory was **4.32 GB**, and token-F1 moved from **0.533** (base) to **0.5645** (tuned) on 2 held-out rows. A 2-iteration smoke run is not the comparison — record your own full-run time, memory and F1. The full 100-iteration prototype measured **4.42 GB peak** and about 7 minutes. The T4/RTX row is recorded at the readiness run via `notebook-cuda.ipynb`.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "เขียนสามประโยค: โมเดลฐานผิดตรงไหน โมเดลที่ปรับเปลี่ยนอะไร และทำไม LoRA จึงถูกกว่าการฝึกโมเดลทั้งหมด แนบไฟล์ผลลัพธ์และตัวอย่างสามรายการ บันทึกเวลา หน่วยความจำ และ F1 ของคุณเอง\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "写三句话：基础模型错在哪里，微调模型改变了什么，以及为什么 LoRA 比训练整个模型更便宜。附上结果文件和三个条目示例，并记录你自己的时间、内存和 F1。\n",
    "\n",
    "</details>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
