12 KiB
12 KiB
In [ ]:
# Paste this entire file into one Colab code cell. Rerun after code updates.
# Setup and software checks only; no training or Drive mount.
from pathlib import Path
import json
import os
import platform
import subprocess
import sys
def run(command):
# Capturing subprocess output makes it visible through notebook Python stdout.
with subprocess.Popen(list(map(str, command)), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True) as process:
for line in process.stdout:
print(line, end="", flush=True)
if process.wait():
raise RuntimeError("Command failed: " + " ".join(map(str, command)))
if platform.system() != "Linux":
raise RuntimeError("Run this cell in the Colab Linux GPU runtime")
if not ((3, 10) <= sys.version_info[:2] <= (3, 13)):
raise RuntimeError("Python 3.10–3.13 required; newer versions need validation")
print("Notebook Python:", sys.version, flush=True)
run(["nvidia-smi"])
SOURCE = Path("/content/dama")
REPO = "https://github.com/s41r4j/dama.git"
if SOURCE.exists():
origin = subprocess.check_output(
["git", "-C", str(SOURCE), "remote", "get-url", "origin"], text=True
).strip()
if origin != REPO:
raise RuntimeError("/content/dama belongs to another repository")
changes = subprocess.check_output(
["git", "-C", str(SOURCE), "status", "--porcelain"], text=True
)
if changes.strip():
raise RuntimeError("Save local checkout edits before fetching updates:\n" + changes)
run(["git", "-C", str(SOURCE), "pull", "--ff-only", "origin", "main"])
else:
run(["git", "clone", "--depth", "1", "--branch", "main", REPO, str(SOURCE)])
run(["git", "-C", str(SOURCE), "log", "-1", "--oneline"])
os.chdir(SOURCE)
os.environ["TOKENIZERS_PARALLELISM"] = "false"
run([sys.executable, str(SOURCE / "scripts/bootstrap_colab.py"),
"--source", str(SOURCE)])
DAMA_PYTHON = Path("/content/dama-env/bin/python")
def dama(*args):
run([str(DAMA_PYTHON), "-m", "dama", *map(str, args)])
CONFIG = SOURCE / "configs/heads.json"
DATA = SOURCE / "fixtures/model-v1"
dama("validate-config", "--config", CONFIG)
dama("validate-data", "--input", DATA)
dama("test")
print("DAMA READY. Setup and tests passed; training has not started.", flush=True)
In [ ]:
from datetime import datetime, timezone
RUNS = Path("/content/dama-runs")
RUNS.mkdir(parents=True, exist_ok=True)
SMOKE_RUN = RUNS / ("smoke-" + datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S-%f"))
print("Smoke output:", SMOKE_RUN, flush=True)
dama("train", "--config", CONFIG, "--data", DATA, "--output", SMOKE_RUN, "--smoke")
In [ ]:
if (SMOKE_RUN / "metrics.json").exists():
print(json.loads((SMOKE_RUN / "metrics.json").read_text()))
print(json.loads((SMOKE_RUN / "run_manifest.json").read_text()))
else:
print("No cloud training results yet.")
In [ ]:
RUN_REAL_TRAINING = False
ALLOW_SYNTHETIC_EXPERIMENT = False
REAL_DATA = DATA # Replace with reviewed dataset path before research training.
REAL_CONFIG = CONFIG
REAL_RUN = RUNS / "heads-v1"
if RUN_REAL_TRAINING:
manifest = json.loads((REAL_DATA / "manifest.json").read_text())
if manifest.get("synthetic") and not ALLOW_SYNTHETIC_EXPERIMENT:
raise ValueError("Replace fixtures with reviewed data, or explicitly opt into a synthetic experiment")
extra = ["--allow-synthetic"] if ALLOW_SYNTHETIC_EXPERIMENT else []
dama("train", "--config", REAL_CONFIG, "--data", REAL_DATA, "--output", REAL_RUN, *extra)
else:
print("Real training is gated. Set RUN_REAL_TRAINING=True only when ready.")
In [ ]:
RUN_RESUME = False
RESUME_CHECKPOINT = REAL_RUN / "checkpoint-000050"
if RUN_RESUME:
if not (RESUME_CHECKPOINT / "COMPLETE").exists():
raise ValueError("Choose a completed checkpoint")
extra = ["--allow-synthetic"] if ALLOW_SYNTHETIC_EXPERIMENT else []
dama("train", "--config", REAL_CONFIG, "--data", REAL_DATA, "--output", REAL_RUN,
"--resume", RESUME_CHECKPOINT, *extra)
else:
print("Resume is gated. Select checkpoint, then set RUN_RESUME=True.")
In [ ]:
RUN_HELD_OUT_EVALUATION = False
if RUN_HELD_OUT_EVALUATION:
dama("evaluate", "--artifact", REAL_RUN / "export", "--input", REAL_DATA / "test.jsonl",
"--output", REAL_RUN / "test-eval", "--device", "cuda", "--repeats", 3)
else:
print("Test remains held out. Freeze decisions before enabling this cell.")
In [ ]:
DOWNLOAD_EXPORT = False
if DOWNLOAD_EXPORT:
import shutil
export = REAL_RUN / "export"
if not (export / "manifest.json").exists():
raise ValueError("No trained export exists yet")
archive = shutil.make_archive("/content/dama-trained-model", "zip", root_dir=export.parent, base_dir="export")
from google.colab import files
files.download(archive)
else:
print("Download is gated. The export identifies exact encoder revision and file hashes.")