267 lines
11 KiB
Plaintext
267 lines
11 KiB
Plaintext
{
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5,
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"name": "python"
|
||
},
|
||
"colab": {
|
||
"name": "DAMA_System_One_Colab.ipynb"
|
||
},
|
||
"accelerator": "GPU"
|
||
},
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# DAMA — System One decision model\n",
|
||
"\n",
|
||
"Train a shared pretrained encoder + parallel decision heads. No agent framework,\n",
|
||
"Primary Agent, database or generative decoder is involved.\n",
|
||
"\n",
|
||
"**Status:** model implementation and local forward-only tests exist; CUDA training\n",
|
||
"has not run. Shipped synthetic data only validates training mechanics. Nothing in\n",
|
||
"this notebook starts a real run without changing the explicit gate below.\n"
|
||
],
|
||
"id": "dama-00"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 1. Fetch updates, install and check\n",
|
||
"Select a GPU runtime, then run this cell. Rerun it after code fixes are pushed.\n",
|
||
"It uses the current Python (3.10–3.13), installs DAMA separately from the notebook kernel,\n",
|
||
"and runs config/data checks and software tests. No ZIP upload, Drive mount or training.\n",
|
||
"Setup output is saved to `/content/dama-setup.log`. CUDA validation must pass here.\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Paste this entire file into one Colab code cell. Rerun after code updates.\n",
|
||
"# Setup and software checks only; no training or Drive mount.\n",
|
||
"from pathlib import Path\n",
|
||
"import json\n",
|
||
"import os\n",
|
||
"import platform\n",
|
||
"import subprocess\n",
|
||
"import sys\n",
|
||
"\n",
|
||
"if platform.system() != \"Linux\":\n",
|
||
" raise RuntimeError(\"Run this cell in the Colab Linux GPU runtime\")\n",
|
||
"if not ((3, 10) <= sys.version_info[:2] <= (3, 13)):\n",
|
||
" raise RuntimeError(\"Python 3.10–3.13 required; newer versions need validation\")\n",
|
||
"print(\"Notebook Python:\", sys.version, flush=True)\n",
|
||
"subprocess.run([\"nvidia-smi\"], check=True)\n",
|
||
"SOURCE = Path(\"/content/dama\")\n",
|
||
"REPO = \"https://github.com/s41r4j/dama.git\"\n",
|
||
"if SOURCE.exists():\n",
|
||
" origin = subprocess.check_output(\n",
|
||
" [\"git\", \"-C\", str(SOURCE), \"remote\", \"get-url\", \"origin\"], text=True\n",
|
||
" ).strip()\n",
|
||
" if origin != REPO:\n",
|
||
" raise RuntimeError(\"/content/dama belongs to another repository\")\n",
|
||
" changes = subprocess.check_output(\n",
|
||
" [\"git\", \"-C\", str(SOURCE), \"status\", \"--porcelain\"], text=True\n",
|
||
" )\n",
|
||
" if changes.strip():\n",
|
||
" raise RuntimeError(\"Save local checkout edits before fetching updates:\\n\" + changes)\n",
|
||
" subprocess.run([\"git\", \"-C\", str(SOURCE), \"pull\", \"--ff-only\", \"origin\", \"main\"], check=True)\n",
|
||
"else:\n",
|
||
" subprocess.run([\"git\", \"clone\", \"--depth\", \"1\", \"--branch\", \"main\", REPO, str(SOURCE)], check=True)\n",
|
||
"subprocess.run([\"git\", \"-C\", str(SOURCE), \"log\", \"-1\", \"--oneline\"], check=True)\n",
|
||
"os.chdir(SOURCE)\n",
|
||
"os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
|
||
"subprocess.run([sys.executable, str(SOURCE / \"scripts/bootstrap_colab.py\"),\n",
|
||
" \"--source\", str(SOURCE)], check=True)\n",
|
||
"DAMA_PYTHON = Path(\"/content/dama-env/bin/python\")\n",
|
||
"def dama(*args):\n",
|
||
" subprocess.run([str(DAMA_PYTHON), \"-m\", \"dama\", *map(str, args)], check=True)\n",
|
||
"CONFIG = SOURCE / \"configs/heads.json\"\n",
|
||
"DATA = SOURCE / \"fixtures/model-v1\"\n",
|
||
"dama(\"validate-config\", \"--config\", CONFIG)\n",
|
||
"dama(\"validate-data\", \"--input\", DATA)\n",
|
||
"dama(\"test\")\n",
|
||
"print(\"DAMA READY. Setup and tests passed; training has not started.\", flush=True)\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 2. Persistent storage and explicit training smoke\n",
|
||
"Only run this cell when ready to perform up to three CUDA optimizer steps.\n",
|
||
"It checks mechanics, not memory-model capability. Choose a new SMOKE_RUN name\n",
|
||
"when repeating so earlier outputs remain reviewable.\n"
|
||
],
|
||
"id": "dama-10"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"metadata": {},
|
||
"execution_count": null,
|
||
"outputs": [],
|
||
"source": [
|
||
"from google.colab import drive\n",
|
||
"drive.mount(\"/content/drive\")\n",
|
||
"RUNS = Path(\"/content/drive/MyDrive/DAMA/model-runs\")\n",
|
||
"RUNS.mkdir(parents=True, exist_ok=True)\n",
|
||
"SMOKE_RUN = RUNS / \"smoke-v1\"\n",
|
||
"RUN_SMOKE = False # Change to True to explicitly run the cloud smoke test.\n",
|
||
"if RUN_SMOKE:\n",
|
||
" dama(\"train\", \"--config\", CONFIG, \"--data\", DATA, \"--output\", SMOKE_RUN, \"--smoke\")\n",
|
||
"else:\n",
|
||
" print(\"Smoke test is gated. Set RUN_SMOKE=True when ready.\")\n"
|
||
],
|
||
"id": "dama-11"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"metadata": {},
|
||
"execution_count": null,
|
||
"outputs": [],
|
||
"source": [
|
||
"if (SMOKE_RUN / \"metrics.json\").exists():\n",
|
||
" print(json.loads((SMOKE_RUN / \"metrics.json\").read_text()))\n",
|
||
" print(json.loads((SMOKE_RUN / \"run_manifest.json\").read_text()))\n",
|
||
"else:\n",
|
||
" print(\"No cloud training results yet.\")\n"
|
||
],
|
||
"id": "dama-12"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Start with frozen encoder + heads (`heads.json`). Compare `accuracy-frozen.json` and `accuracy.json` for a stronger BGE-base candidate (frozen/LoRA). Select the fastest model satisfying the held-out accuracy constraint; no configuration has proven superior. Review labels and peak VRAM first. The synthetic fixture gate needs a separate explicit override.\n"
|
||
],
|
||
"id": "dama-13"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"metadata": {},
|
||
"execution_count": null,
|
||
"outputs": [],
|
||
"source": [
|
||
"RUN_REAL_TRAINING = False\n",
|
||
"ALLOW_SYNTHETIC_EXPERIMENT = False\n",
|
||
"REAL_DATA = DATA # Replace with reviewed dataset path before research training.\n",
|
||
"REAL_CONFIG = CONFIG\n",
|
||
"REAL_RUN = RUNS / \"heads-v1\"\n",
|
||
"if RUN_REAL_TRAINING:\n",
|
||
" manifest = json.loads((REAL_DATA / \"manifest.json\").read_text())\n",
|
||
" if manifest.get(\"synthetic\") and not ALLOW_SYNTHETIC_EXPERIMENT:\n",
|
||
" raise ValueError(\"Replace fixtures with reviewed data, or explicitly opt into a synthetic experiment\")\n",
|
||
" extra = [\"--allow-synthetic\"] if ALLOW_SYNTHETIC_EXPERIMENT else []\n",
|
||
" dama(\"train\", \"--config\", REAL_CONFIG, \"--data\", REAL_DATA, \"--output\", REAL_RUN, *extra)\n",
|
||
"else:\n",
|
||
" print(\"Real training is gated. Set RUN_REAL_TRAINING=True only when ready.\")\n"
|
||
],
|
||
"id": "dama-14"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 4. Resume safely\n",
|
||
"Only completed checkpoints may resume. Fingerprints must match the original\n",
|
||
"config and dataset. Changing total-step/epoch limits requires a new experiment.\n",
|
||
"The checkpoint stores RNG, optimizer, scaler and exact next microbatch position.\n"
|
||
],
|
||
"id": "dama-15"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"metadata": {},
|
||
"execution_count": null,
|
||
"outputs": [],
|
||
"source": [
|
||
"RUN_RESUME = False\n",
|
||
"RESUME_CHECKPOINT = REAL_RUN / \"checkpoint-000050\"\n",
|
||
"if RUN_RESUME:\n",
|
||
" if not (RESUME_CHECKPOINT / \"COMPLETE\").exists():\n",
|
||
" raise ValueError(\"Choose a completed checkpoint\")\n",
|
||
" extra = [\"--allow-synthetic\"] if ALLOW_SYNTHETIC_EXPERIMENT else []\n",
|
||
" dama(\"train\", \"--config\", REAL_CONFIG, \"--data\", REAL_DATA, \"--output\", REAL_RUN,\n",
|
||
" \"--resume\", RESUME_CHECKPOINT, *extra)\n",
|
||
"else:\n",
|
||
" print(\"Resume is gated. Select checkpoint, then set RUN_RESUME=True.\")\n"
|
||
],
|
||
"id": "dama-16"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 5. Held-out evaluation and export\n",
|
||
"Freeze model and thresholds using dev before accessing test. This measures the\n",
|
||
"standalone model, not downstream answer quality. All confidences are uncalibrated.\n",
|
||
"The trainer automatically exports a self-contained encoder + heads; a checkpoint\n",
|
||
"can also be exported with `dama export --checkpoint ... --output ...`.\n"
|
||
],
|
||
"id": "dama-17"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"metadata": {},
|
||
"execution_count": null,
|
||
"outputs": [],
|
||
"source": [
|
||
"RUN_HELD_OUT_EVALUATION = False\n",
|
||
"if RUN_HELD_OUT_EVALUATION:\n",
|
||
" dama(\"evaluate\", \"--artifact\", REAL_RUN / \"export\", \"--input\", REAL_DATA / \"test.jsonl\",\n",
|
||
" \"--output\", REAL_RUN / \"test-eval\", \"--device\", \"cuda\", \"--repeats\", 3)\n",
|
||
"else:\n",
|
||
" print(\"Test remains held out. Freeze decisions before enabling this cell.\")\n"
|
||
],
|
||
"id": "dama-18"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"metadata": {},
|
||
"execution_count": null,
|
||
"outputs": [],
|
||
"source": [
|
||
"DOWNLOAD_EXPORT = False\n",
|
||
"if DOWNLOAD_EXPORT:\n",
|
||
" import shutil\n",
|
||
" export = REAL_RUN / \"export\"\n",
|
||
" if not (export / \"manifest.json\").exists():\n",
|
||
" raise ValueError(\"No trained export exists yet\")\n",
|
||
" archive = shutil.make_archive(\"/content/dama-trained-model\", \"zip\", root_dir=export.parent, base_dir=\"export\")\n",
|
||
" from google.colab import files\n files.download(archive)\n",
|
||
"else:\n",
|
||
" print(\"Download is gated. The export identifies exact encoder revision and file hashes.\")\n"
|
||
],
|
||
"id": "dama-19"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 6. Later local loading\n",
|
||
"\n",
|
||
"```bash\n",
|
||
"dama predict --artifact downloaded/export --input fixtures/inputs/correction.json --device cpu\n",
|
||
"```\n",
|
||
"\n",
|
||
"CPU inference is supported. No training is permitted on the Mac. An optional\n",
|
||
"LoRA export includes the full model state, not a decoder/GGUF adapter. No quality,\n",
|
||
"calibration, speed or cost reduction has been established until measured on\n",
|
||
"reviewed held-out data and an actual trained artifact.\n"
|
||
],
|
||
"id": "dama-20"
|
||
}
|
||
]
|
||
}
|