285 lines
12 KiB
Plaintext
285 lines
12 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:** Colab Python 3.13/T4 setup and 35 software tests passed. Actual training\n",
|
||
"still needs validation. Section 2 runs an explicit three-step GPU smoke test when\n",
|
||
"you execute that cell. The shipped synthetic data checks training mechanics only;\n",
|
||
"research training and held-out evaluation remain gated in later cells.\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",
|
||
"def run(command):\n",
|
||
" # Capturing subprocess output makes it visible through notebook Python stdout.\n",
|
||
" with subprocess.Popen(list(map(str, command)), stdout=subprocess.PIPE,\n",
|
||
" stderr=subprocess.STDOUT, text=True) as process:\n",
|
||
" for line in process.stdout:\n",
|
||
" print(line, end=\"\", flush=True)\n",
|
||
" if process.wait():\n",
|
||
" raise RuntimeError(\"Command failed: \" + \" \".join(map(str, command)))\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",
|
||
"run([\"nvidia-smi\"])\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",
|
||
" run([\"git\", \"-C\", str(SOURCE), \"pull\", \"--ff-only\", \"origin\", \"main\"])\n",
|
||
"else:\n",
|
||
" run([\"git\", \"clone\", \"--depth\", \"1\", \"--branch\", \"main\", REPO, str(SOURCE)])\n",
|
||
"run([\"git\", \"-C\", str(SOURCE), \"log\", \"-1\", \"--oneline\"])\n",
|
||
"os.chdir(SOURCE)\n",
|
||
"os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
|
||
"run([sys.executable, str(SOURCE / \"scripts/bootstrap_colab.py\"),\n",
|
||
" \"--source\", str(SOURCE)])\n",
|
||
"DAMA_PYTHON = Path(\"/content/dama-env/bin/python\")\n",
|
||
"def dama(*args):\n",
|
||
" run([str(DAMA_PYTHON), \"-m\", \"dama\", *map(str, args)])\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. Run the three-step GPU smoke test\n",
|
||
"Run this cell after setup to download MiniLM and perform up to three optimizer\n",
|
||
"steps on the GPU. It uses a new output folder each time, with no ZIP upload or\n",
|
||
"Drive authorization. This tests training mechanics, not model accuracy.\n",
|
||
"Outputs are under `/content/dama-runs` and are temporary until downloaded or copied.\n"
|
||
],
|
||
"id": "dama-10"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"metadata": {},
|
||
"execution_count": null,
|
||
"outputs": [],
|
||
"source": [
|
||
"from datetime import datetime, timezone\n",
|
||
"RUNS = Path(\"/content/dama-runs\")\n",
|
||
"RUNS.mkdir(parents=True, exist_ok=True)\n",
|
||
"SMOKE_RUN = RUNS / (\"smoke-\" + datetime.now(timezone.utc).strftime(\"%Y%m%d-%H%M%S-%f\"))\n",
|
||
"print(\"Smoke output:\", SMOKE_RUN, flush=True)\n",
|
||
"dama(\"train\", \"--config\", CONFIG, \"--data\", DATA, \"--output\", SMOKE_RUN, \"--smoke\")\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": [
|
||
"## 3. Prepare reviewed data and persistent storage\n",
|
||
"Before a longer run, use reviewed data and persistent storage. To save future\n",
|
||
"runs to Google Drive, mount it once and set RUNS before the next training cell:\n",
|
||
"\n",
|
||
"```python\n",
|
||
"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",
|
||
"```\n",
|
||
"\n",
|
||
"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"
|
||
}
|
||
]
|
||
}
|