{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# S10 lab notebook — choosing the accelerator\n",
    "\n",
    "The campus media unit wants a small product-image classifier for the catalogue. Train the same small CNN on three devices and compare time, memory and cost, so the accelerator choice becomes a measurement rather than a guess.\n",
    "\n",
    "- **MacBook (MPS):** run at home, free.\n",
    "- **Kaggle T4 (CUDA):** free, 16 GB, 30 GPU-hours/week.\n",
    "- **Faculty RTX Pro 6000 (CUDA):** one card per student, 96 GB.\n",
    "\n",
    "Run this notebook once per device, changing `S10_LABEL` (or `LABEL` in the settings cell). The Mac run is the pre-lab task; the T4 and RTX runs happen in class.\n",
    "\n",
    "**You submit:** `results/comparison.csv`, the three JSONs, a 150–250 word *what this proves* note, a GPU time/utilization screenshot, and the capstone proposal outline.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "หน่วยสื่อต้องการตัวจำแนกภาพสินค้าขนาดเล็กสำหรับแคตตาล็อก ให้ฝึก CNN เดียวกันบนสามอุปกรณ์แล้วเปรียบเทียบเวลา หน่วยความจำ และต้นทุน เพื่อให้การเลือกอุปกรณ์เป็นการวัด ไม่ใช่การเดา รันโน้ตบุ๊กครั้งละอุปกรณ์โดยเปลี่ยน LABEL การรันบนแมคเป็นงานก่อนแล็บ ส่วน T4 และ RTX ทำในคาบเรียน แล้วรวมผลเป็นตารางเดียว\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "媒体单位需要一个用于目录的小型商品图像分类器。在三种设备上训练同一个 CNN，然后比较时间、内存和成本，让设备的选择成为一次测量，而不是猜测。每次在一个设备上运行 notebook 并修改 LABEL。Mac 上的运行是实验前的任务，T4 和 RTX 的运行在课上完成，然后把结果合并成一张表。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import time\n",
    "from pathlib import Path\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.utils.data import DataLoader, TensorDataset\n",
    "\n",
    "SEED = 42\n",
    "torch.manual_seed(SEED)\n",
    "np.random.seed(SEED)\n",
    "\n",
    "def data_dir():\n",
    "    candidates = [Path(\"data\"), Path(\".\"), Path(\"/kaggle/input/image-ml-gpu\")]\n",
    "    env = os.environ.get(\"COURSE_DATA\")\n",
    "    if env:\n",
    "        candidates.append(Path(env))\n",
    "    # Kaggle mounts datasets under /kaggle/input/datasets/<owner>/<slug>/.\n",
    "    root = Path(\"/kaggle/input\")\n",
    "    if root.is_dir():\n",
    "        candidates += sorted({p.parent for p in root.glob(\"**/fashion_subset.npz\")})\n",
    "    for path in candidates:\n",
    "        if path and (path / \"fashion_subset.npz\").exists():\n",
    "            return path\n",
    "    raise FileNotFoundError(\n",
    "        \"put fashion_subset.npz in data/, the working directory, or set COURSE_DATA\")\n",
    "\n",
    "DATA = data_dir() / \"fashion_subset.npz\"\n",
    "OUT = Path(\"results\")\n",
    "OUT.mkdir(exist_ok=True)\n",
    "\n",
    "LABEL = os.environ.get(\"S10_LABEL\", \"mps\")   # \"mps\", \"t4\" or \"rtx\"\n",
    "EPOCHS = int(os.environ.get(\"S10_EPOCHS\", \"20\"))\n",
    "BATCH = 128\n",
    "LR = 1e-3\n",
    "HOURLY_COST = float(os.environ.get(\"S10_HOURLY_COST\", \"1.69\" if LABEL == \"rtx\" else \"0.0\"))\n",
    "\n",
    "if torch.cuda.is_available():\n",
    "    DEVICE = \"cuda\"\n",
    "elif torch.backends.mps.is_available():\n",
    "    DEVICE = \"mps\"\n",
    "else:\n",
    "    DEVICE = \"cpu\"\n",
    "\n",
    "assert DATA.exists()\n",
    "if LABEL in {\"t4\", \"rtx\"}:\n",
    "    assert DEVICE == \"cuda\", f\"LABEL={LABEL} needs a CUDA GPU; this notebook sees {DEVICE}\"\n",
    "if LABEL == \"mps\":\n",
    "    assert DEVICE in {\"mps\", \"cpu\"}, \"LABEL=mps is for the Mac path\"\n",
    "print(\"seed\", SEED)\n",
    "print(\"device\", DEVICE, \"| label\", LABEL, \"| torch\", torch.__version__)\n",
    "print(\"data\", DATA, \"| epochs\", EPOCHS)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Data\n",
    "\n",
    "Fashion-MNIST, 500 training and 100 validation images per class, bundled as a 2.6 MB `.npz`. No download, no people and no brands. The normalization constants are the dataset's mean and standard deviation.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "Fashion-MNIST มีภาพฝึก 500 และตรวจสอบ 100 ภาพต่อคลาส รวมในไฟล์ npz ขนาด 2.6 เมกะไบต์ ไม่ต้องดาวน์โหลด ไม่มีบุคคลหรือแบรนด์ ค่าคงที่ที่ใช้ปรับมาตรฐานคือค่าเฉลี่ยและส่วนเบี่ยงเบนมาตรฐานของชุดข้อมูล\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "Fashion-MNIST 每个类有 500 张训练图像和 100 张验证图像，打包在一个 2.6 MB 的 npz 文件中。不需要下载，不包含人物或品牌。用于标准化的常数是该数据集的均值和标准差。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = np.load(DATA)\n",
    "x_train = (torch.from_numpy(d[\"train_images\"]).float().unsqueeze(1) / 255.0 - 0.2860) / 0.3530\n",
    "y_train = torch.from_numpy(d[\"train_labels\"]).long()\n",
    "x_val = (torch.from_numpy(d[\"val_images\"]).float().unsqueeze(1) / 255.0 - 0.2860) / 0.3530\n",
    "y_val = torch.from_numpy(d[\"val_labels\"]).long()\n",
    "classes = [str(c) for c in d[\"classes\"]]\n",
    "\n",
    "assert x_train.shape == (5000, 1, 28, 28) and x_val.shape == (1000, 1, 28, 28)\n",
    "assert len(classes) == 10\n",
    "assert all(int((y_train == i).sum()) == 500 for i in range(10))\n",
    "print(\"train\", len(x_train), \"| validation\", len(x_val))\n",
    "\n",
    "fig, axes = plt.subplots(2, 5, figsize=(10, 4))\n",
    "for ax, i in zip(axes.ravel(), range(10)):\n",
    "    idx = int((y_train == i).nonzero()[0])\n",
    "    ax.imshow(d[\"train_images\"][idx], cmap=\"gray\")\n",
    "    ax.set_title(classes[i], fontsize=9)\n",
    "    ax.axis(\"off\")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. The model\n",
    "\n",
    "Three convolution blocks and a small linear head: 298,858 parameters. The model is deliberately small — the lab is about the device comparison, not state-of-the-art accuracy.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "สามบล็อกคอนโวลูชันและหัวเชิงเส้นขนาดเล็ก มีพารามิเตอร์ 298,858 ตัว โมเดลเล็กโดยตั้งใจ เพราะแล็บนี้เน้นการเปรียบเทียบอุปกรณ์ ไม่ใช่ความแม่นยำระดับแนวหน้า\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "三个卷积块和一个小型线性头，共有 298,858 个参数。模型故意做得很小，因为这次实验的重点是设备比较，而不是最前沿的准确率。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class SmallCNN(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        def block(i, o):\n",
    "            return nn.Sequential(\n",
    "                nn.Conv2d(i, o, 3, padding=1), nn.BatchNorm2d(o), nn.ReLU(inplace=True),\n",
    "                nn.Conv2d(o, o, 3, padding=1), nn.BatchNorm2d(o), nn.ReLU(inplace=True),\n",
    "                nn.MaxPool2d(2),\n",
    "            )\n",
    "        self.features = nn.Sequential(block(1, 32), block(32, 64), block(64, 128))\n",
    "        self.head = nn.Sequential(nn.Flatten(), nn.Dropout(0.3), nn.Linear(128 * 3 * 3, 10))\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.head(self.features(x))\n",
    "\n",
    "model = SmallCNN().to(DEVICE)\n",
    "parameters = sum(p.numel() for p in model.parameters())\n",
    "print(\"parameters\", parameters)\n",
    "assert parameters == 298858"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Train and evaluate\n",
    "\n",
    "The same seed, data and model on every device; only the accelerator changes. The first MPS epoch includes graph compilation, so it is slower than the rest; the comparison reports the mean seconds per epoch, the first epoch separately and the total.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "ใช้ seed ข้อมูล และโมเดลเดียวกันบนทุกอุปกรณ์ เปลี่ยนเฉพาะตัวเร่งความเร็ว รอบแรกบน MPS มีการคอมไพล์กราฟ ดังนั้นตารางเปรียบเทียบจึงรายงานเวลาเฉลี่ยต่อรอบและเวลารวมแยกกัน\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "在每个设备上使用相同的 seed、数据和模型，只更换加速器。MPS 上的第一轮包含图编译，因此比较表分别报告每轮的平均时间和总时间。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.manual_seed(SEED)\n",
    "np.random.seed(SEED)\n",
    "train_loader = DataLoader(TensorDataset(x_train, y_train), batch_size=BATCH,\n",
    "                          shuffle=True, num_workers=0)\n",
    "val_loader = DataLoader(TensorDataset(x_val, y_val), batch_size=BATCH,\n",
    "                        shuffle=False, num_workers=0)\n",
    "opt = torch.optim.Adam(model.parameters(), lr=LR)\n",
    "loss_fn = nn.CrossEntropyLoss()\n",
    "\n",
    "def sync():\n",
    "    if DEVICE == \"cuda\":\n",
    "        torch.cuda.synchronize()\n",
    "    elif DEVICE == \"mps\":\n",
    "        torch.mps.synchronize()\n",
    "\n",
    "if DEVICE == \"cuda\":\n",
    "    torch.cuda.reset_peak_memory_stats()\n",
    "\n",
    "epoch_times, val_acc = [], []\n",
    "t_start = time.time()\n",
    "for epoch in range(1, EPOCHS + 1):\n",
    "    sync()\n",
    "    t0 = time.time()\n",
    "    model.train()\n",
    "    running = 0.0\n",
    "    for xb, yb in train_loader:\n",
    "        xb, yb = xb.to(DEVICE), yb.to(DEVICE)\n",
    "        opt.zero_grad(set_to_none=True)\n",
    "        loss = loss_fn(model(xb), yb)\n",
    "        loss.backward()\n",
    "        opt.step()\n",
    "        running += loss.item() * len(xb)\n",
    "    sync()\n",
    "    epoch_times.append(time.time() - t0)\n",
    "\n",
    "    model.eval()\n",
    "    correct = total = 0\n",
    "    with torch.no_grad():\n",
    "        for xb, yb in val_loader:\n",
    "            xb, yb = xb.to(DEVICE), yb.to(DEVICE)\n",
    "            pred = model(xb).argmax(1)\n",
    "            correct += (pred == yb).sum().item()\n",
    "            total += len(yb)\n",
    "    val_acc.append(correct / total)\n",
    "    print(f\"epoch {epoch:02d}/{EPOCHS} loss={running / len(x_train):.4f} \"\n",
    "          f\"val_acc={val_acc[-1]:.4f} time={epoch_times[-1]:.2f}s\")\n",
    "\n",
    "total_seconds = time.time() - t_start\n",
    "peak = None\n",
    "if DEVICE == \"cuda\":\n",
    "    peak = round(torch.cuda.max_memory_allocated() / 1e9, 2)\n",
    "elif DEVICE == \"mps\":\n",
    "    try:\n",
    "        peak = round(torch.mps.driver_allocated_memory() / 1e9, 2)\n",
    "    except Exception:\n",
    "        pass\n",
    "\n",
    "result = {\n",
    "    \"label\": LABEL,\n",
    "    \"device\": DEVICE,\n",
    "    \"device_name\": (torch.cuda.get_device_name(0) if DEVICE == \"cuda\"\n",
    "                    else \"Apple Silicon (MPS)\" if DEVICE == \"mps\" else \"CPU\"),\n",
    "    \"torch\": torch.__version__,\n",
    "    \"seed\": SEED,\n",
    "    \"parameters\": parameters,\n",
    "    \"train_images\": len(x_train),\n",
    "    \"val_images\": len(x_val),\n",
    "    \"epochs\": EPOCHS,\n",
    "    \"batch\": BATCH,\n",
    "    \"lr\": LR,\n",
    "    \"train_seconds\": round(total_seconds, 1),\n",
    "    \"seconds_per_epoch\": round(float(np.mean(epoch_times)), 2),\n",
    "    \"epoch_times\": [round(t, 2) for t in epoch_times],\n",
    "    \"val_accuracy\": round(val_acc[-1], 4),\n",
    "    \"peak_memory_gb\": peak,\n",
    "    \"notional_cost_usd\": round(total_seconds / 3600 * HOURLY_COST, 3),\n",
    "    \"hourly_cost\": HOURLY_COST,\n",
    "}\n",
    "(OUT / f\"{LABEL}.json\").write_text(json.dumps(result, indent=2) + \"\\n\")\n",
    "print(json.dumps(result, indent=2))\n",
    "assert len(epoch_times) == EPOCHS\n",
    "assert result[\"val_accuracy\"] > 0.80, \"The model did not learn; check the data and the device.\"\n",
    "assert (OUT / f\"{LABEL}.json\").exists()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Run on the other devices\n",
    "\n",
    "1. **Mac:** keep `LABEL=\"mps\"`, `HOURLY_COST=0`, run the cells above. You should have `results/mps.json`.\n",
    "2. **Kaggle T4:** upload `data/fashion_subset.npz`, set `LABEL=\"t4\"`, `HOURLY_COST=0`, run, and download `results/t4.json`.\n",
    "3. **Faculty RTX Pro 6000:** set `LABEL=\"rtx\"`, `HOURLY_COST=1.69` (the **notional** RunPod price; the faculty card is free), run, and copy `results/rtx.json` back.\n",
    "4. Put all three JSONs in `results/` and run the merge cell.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "รันบนแมคก่อนด้วย LABEL=mps จากนั้นอัปโหลดชุดข้อมูลและตั้ง LABEL=t4 บน Kaggle หรือ LABEL=rtx บนการ์ดของคณะ เก็บไฟล์ JSON ทั้งสามแล้วรวมเป็นตารางเดียว ราคา RunPod เป็นเพียงราคาเปรียบเทียบ การ์ดของคณะไม่มีค่าใช้จ่าย\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "先在 Mac 上用 LABEL=mps 运行，然后在 Kaggle 上上传数据集并设置 LABEL=t4，或在学院的卡上设置 LABEL=rtx。保存三个 JSON 文件并合并成一张表。RunPod 的价格只是用于比较的价格，学院的卡不收费。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Merge the comparison\n",
    "\n",
    "The merge reads every JSON in `results/`, prints the table and writes `comparison.csv`. One row is enough for the Mac pre-lab; three rows make the argument.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "การรวมอ่านไฟล์ JSON ทุกไฟล์ใน results/ พิมพ์ตารางและเขียน comparison.csv หนึ่งแถวพอสำหรับงานก่อนแล็บบนแมค สามแถวทำให้การโต้แย้งสมบูรณ์\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "合并过程读取 results/ 中的每个 JSON 文件，打印表格并写出 comparison.csv。对于 Mac 上的实验前任务，一行就够了；三行才能让论证完整。\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "rows = [json.loads(p.read_text()) for p in sorted(OUT.glob(\"*.json\"))]\n",
    "assert rows, \"No result JSONs yet.\"\n",
    "assert all(\"label\" in row and \"val_accuracy\" in row for row in rows)\n",
    "df = pd.DataFrame(rows)\n",
    "columns = [\"label\", \"device_name\", \"epochs\", \"batch\", \"parameters\",\n",
    "           \"train_seconds\", \"seconds_per_epoch\", \"val_accuracy\",\n",
    "           \"peak_memory_gb\", \"notional_cost_usd\"]\n",
    "table = df[[c for c in columns if c in df.columns]]\n",
    "print(table.to_string(index=False))\n",
    "table.to_csv(OUT / \"comparison.csv\", index=False)\n",
    "print(\"wrote\", OUT / \"comparison.csv\")\n",
    "assert (OUT / \"comparison.csv\").exists()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What this proves\n",
    "\n",
    "Write 150–250 words: which device was fastest per epoch, how much of the difference is the accelerator versus the first-epoch warm-up, what the peak memory means for a T4 versus the RTX card, and which device you would recommend for the unit's monthly retraining. Attach `results/comparison.csv`, the three JSONs, a GPU utilization screenshot, and the capstone proposal outline.\n",
    "\n",
    "**If the RTX card is unavailable:** use the Kaggle T4 as the third configuration and say so. If both GPUs are unavailable, use the instructor's precomputed `rtx.json` and label the row as borrowed.\n",
    "\n",
    "## Verification\n",
    "\n",
    "**Mac (M1 Max, Python 3.14, torch 2.10.0, numpy 2.5.4):** the shipping notebook was re-run on this Mac with the bundled data and measured **17.5 s** for 20 epochs, 0.81 s/epoch after a 1.23 s first-epoch warm-up, **88.8% validation accuracy**, **1.13 GB peak**, 298,858 parameters. The earlier prototype run measured 21.6 s and 87.5%; the shipping re-run supersedes it. **Kaggle T4 (Tesla T4, torch 2.11.0+cu130):** the same notebook in a private Kaggle kernel measured **13.9 s** for 20 epochs, 0.66 s/epoch after a 4.42 s first epoch, **88.7% validation accuracy** and **0.16 GB peak**. **Faculty RTX Pro 6000 (torch 2.12.1+cu130, driver 610.57.04):** **2.2 s**, 0.10 s/epoch after a 0.44 s first epoch, **88.0% validation accuracy** and **0.19 GB peak**. Repeated runs move by a few percent in time, and GPU accuracy by a point or two; each row records one run.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>สรุปภาษาไทย</summary>\n",
    "\n",
    "เขียนสรุป 150–250 คำว่าอุปกรณ์ใดเร็วที่สุดต่อรอบ ความแตกต่างมาจากตัวเร่งความเร็วหรือการอุ่นรอบแรกมากน้อยเพียงใด หน่วยความจำสูงสุดหมายถึงอะไรสำหรับ T4 เทียบกับการ์ด RTX และจะแนะนำอุปกรณ์ใดสำหรับการฝึกใหม่รายเดือนของหน่วย แนบตาราง ไฟล์ JSON ภาพหน้าจอการใช้งาน GPU และโครงร่างข้อเสนอแคปสโตน\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>中文总结</summary>\n",
    "\n",
    "写一份 150–250 词的总结：哪个设备每轮最快，这个差异有多少来自加速器、多少来自第一轮的预热，峰值内存对 T4 与 RTX 卡分别意味着什么，以及会为单位每月重新训练推荐哪个设备。附上表格、JSON 文件、GPU 利用率截图和 capstone 提案大纲。\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
}
