mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
5195 lines
184 KiB
Python
5195 lines
184 KiB
Python
"""CPU tests for the portable GSQ source adapter and workflow runner."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from math import ceil
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import torch
|
|
import yaml
|
|
from safetensors.torch import load_file, save_file
|
|
|
|
from obliteratus.gsq import coverage as gsq_coverage
|
|
from obliteratus.gsq import source, workflow
|
|
|
|
|
|
ROOT = Path(__file__).parents[1]
|
|
BANNED_DEPLOYMENT_STRINGS = (
|
|
"OLLAMA",
|
|
"ollama",
|
|
"lease",
|
|
"runuser",
|
|
"libexec",
|
|
"resident",
|
|
"supervisor",
|
|
"UUID",
|
|
"pause_for_resize",
|
|
"gsq_lease",
|
|
)
|
|
|
|
|
|
def _write_fake_tree(root: Path) -> None:
|
|
olds: dict[str, list[str]] = {}
|
|
for relative, old, _new, count in source.TEXT_PATCHES:
|
|
olds.setdefault(relative, []).extend([old] * count)
|
|
for relative, fragments in olds.items():
|
|
path = root / relative
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(
|
|
"".join(fragments) + "\ntrust_remote_code=True\n", encoding="utf-8"
|
|
)
|
|
extra = root / "tests" / "eval_humming_model.py"
|
|
extra.parent.mkdir(parents=True, exist_ok=True)
|
|
extra.write_text("value = load(name, trust_remote_code=True)\n", encoding="utf-8")
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_tree(tmp_path, monkeypatch):
|
|
root = tmp_path / "GSQ"
|
|
_write_fake_tree(root)
|
|
monkeypatch.setattr(source, "_git", lambda _root, *args: source.GSQ_REVISION)
|
|
return root
|
|
|
|
|
|
def _snapshot(root: Path) -> dict[str, bytes]:
|
|
return {
|
|
str(path.relative_to(root)): path.read_bytes()
|
|
for path in sorted(root.rglob("*"))
|
|
if path.is_file()
|
|
}
|
|
|
|
|
|
def test_prepare_applies_patches_and_is_idempotent(fake_tree):
|
|
changed = source.prepare(fake_tree)
|
|
assert changed
|
|
patched = {relative for relative, _, _, _ in source.TEXT_PATCHES}
|
|
assert patched | {"tests/eval_humming_model.py", source.GUARD_FILENAME} <= set(
|
|
changed
|
|
)
|
|
for path in fake_tree.rglob("*.py"):
|
|
assert "trust_remote_code=True" not in path.read_text(encoding="utf-8")
|
|
assert "trust_remote_code=False" in (fake_tree / "main.py").read_text()
|
|
assert "damped_cholesky" in (fake_tree / "src" / "prior" / "gptq.py").read_text()
|
|
assert "in_proj_a" in (fake_tree / "src" / "models" / "qwen35.py").read_text()
|
|
assert source.prepare(fake_tree) == []
|
|
|
|
|
|
def test_prepare_fails_closed_on_unexpected_fragment(fake_tree):
|
|
gptq = fake_tree / "src" / "prior" / "gptq.py"
|
|
gptq.write_text("changed upstream content\n", encoding="utf-8")
|
|
before = _snapshot(fake_tree)
|
|
with pytest.raises(RuntimeError, match="expected 1 source fragment"):
|
|
source.prepare(fake_tree)
|
|
assert _snapshot(fake_tree) == before
|
|
assert not (fake_tree / source.GUARD_FILENAME).exists()
|
|
|
|
|
|
def test_prepare_rejects_missing_file(fake_tree):
|
|
(fake_tree / "save_model.py").unlink()
|
|
with pytest.raises(RuntimeError, match="missing expected file"):
|
|
source.prepare(fake_tree)
|
|
|
|
|
|
def test_verify_checkout_rejects_wrong_pin(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(source, "_git", lambda _root, *args: "0" * 40)
|
|
with pytest.raises(RuntimeError, match="must be pinned"):
|
|
source.verify_checkout(tmp_path)
|
|
|
|
|
|
def test_verify_checkout_fails_closed_when_git_fails(tmp_path, monkeypatch):
|
|
def _boom(_root, *args):
|
|
raise subprocess.CalledProcessError(1, ["git"])
|
|
|
|
monkeypatch.setattr(source, "_git", _boom)
|
|
with pytest.raises(RuntimeError, match="cannot verify"):
|
|
source.verify_checkout(tmp_path)
|
|
|
|
|
|
def test_prepared_guard_copy_is_verbatim(fake_tree):
|
|
source.prepare(fake_tree)
|
|
packaged = (ROOT / "obliteratus" / "gsq" / "numerics_guard.py").read_bytes()
|
|
assert (fake_tree / source.GUARD_FILENAME).read_bytes() == packaged
|
|
|
|
|
|
def test_source_record_file_agrees_with_code():
|
|
record = json.loads((ROOT / "ci" / "gsq-source-v1.json").read_text())
|
|
assert record["repository"] == source.GSQ_REPOSITORY
|
|
assert record["revision"] == source.GSQ_REVISION
|
|
assert record["prepared_python_tree_sha256"] == source.EXPECTED_PREPARED_TREE_SHA256
|
|
assert record["open_thoughts_revision"] == source.OPEN_THOUGHTS_REVISION
|
|
|
|
|
|
def test_dataset_patches_pin_revision_and_drop_identity(fake_tree):
|
|
source.prepare(fake_tree)
|
|
dataset = (fake_tree / "src" / "data" / "dataset.py").read_text()
|
|
assert source.OPEN_THOUGHTS_REVISION in dataset
|
|
assert 'split="train"' in dataset
|
|
assert "Moonshot" not in dataset
|
|
assert "You are Kimi" not in dataset
|
|
assert "Calibration carries no system identity prompt." in dataset
|
|
assert source.prepare(fake_tree) == []
|
|
|
|
|
|
def test_config_rendering_is_memory_safe_and_worker_free(tmp_path):
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir()
|
|
text = workflow.render_gsq_config(
|
|
str(model_dir),
|
|
checkpoint_dir=str(tmp_path / "checkpoints"),
|
|
)
|
|
assert "num_workers: 0" in text
|
|
assert f"act_cache_mmap_threshold_gb: {workflow.ACT_CACHE_MMAP_THRESHOLD_GB}" in (
|
|
text
|
|
)
|
|
config = yaml.safe_load(text)
|
|
assert config["data"]["num_workers"] == 0
|
|
assert config["quantization"]["self_attn"] is True
|
|
assert config["training"]["act_cache_mmap_threshold_gb"] == 1024
|
|
assert config["gptq"]["wbits"] == 2
|
|
tiny = yaml.safe_load(
|
|
workflow.render_gsq_config(
|
|
str(model_dir),
|
|
checkpoint_dir="ckpt",
|
|
num_samples=8,
|
|
quantize_attention=False,
|
|
)
|
|
)
|
|
assert tiny["data"]["num_samples"] == 8
|
|
assert tiny["quantization"]["self_attn"] is False
|
|
|
|
|
|
def test_build_run_command_has_no_deployment_wrapping():
|
|
single = workflow.build_run_command("py", "cfg.yaml", world_size=1, max_layers=3)
|
|
assert single == [
|
|
"py",
|
|
"main.py",
|
|
"--config",
|
|
"cfg.yaml",
|
|
"--max-layers",
|
|
"3",
|
|
]
|
|
joined = " ".join(single)
|
|
assert not any(banned in joined for banned in BANNED_DEPLOYMENT_STRINGS)
|
|
with pytest.raises(ValueError, match="world_size"):
|
|
workflow.build_run_command("py", "cfg.yaml", world_size=0)
|
|
|
|
|
|
def test_build_run_command_refuses_resume():
|
|
"""Any --resume value raises: resumed runs can mix base models.
|
|
|
|
The pinned main.py reuses the previous run's completed layers while
|
|
training the rest from the current config's model, with no snapshot
|
|
check -- so the workflow offers no resume path (start a fresh run).
|
|
"""
|
|
with pytest.raises(ValueError, match="not supported.*fresh run"):
|
|
workflow.build_run_command("py", "cfg.yaml", resume="run-id")
|
|
with pytest.raises(ValueError, match="not supported.*fresh run"):
|
|
workflow.build_run_command("py", "cfg.yaml", resume="latest")
|
|
|
|
|
|
def test_build_run_command_refuses_multi_gpu():
|
|
"""world_size > 1 raises instead of building a torchrun command.
|
|
|
|
The workflow is validated single-GPU only; the parameter stays for
|
|
future multi-GPU support but must enforce == 1.
|
|
"""
|
|
with pytest.raises(ValueError, match="not supported.*world_size=2"):
|
|
workflow.build_run_command("py", "cfg.yaml", world_size=2)
|
|
with pytest.raises(ValueError, match="not supported"):
|
|
workflow.build_run_command("py", "cfg.yaml", world_size=4)
|
|
|
|
|
|
def test_run_quantization_refuses_multi_gpu_before_launching(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""run with world_size=2 refuses before launching any subprocess."""
|
|
config_path = _write_run_config(tmp_path)
|
|
calls: list = []
|
|
monkeypatch.setattr(
|
|
subprocess,
|
|
"run",
|
|
lambda *a, **k: calls.append(a[0])
|
|
or subprocess.CompletedProcess(a[0], 0),
|
|
)
|
|
with pytest.raises(ValueError, match="not supported.*world_size=2"):
|
|
workflow.run_quantization(
|
|
fake_tree,
|
|
config_path,
|
|
world_size=2,
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_cli_run_refuses_multi_gpu(fake_tree, tmp_path, capsys):
|
|
"""The CLI run path (including --dry-run) refuses --world-size 2."""
|
|
config_path = _write_run_config(tmp_path)
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"run",
|
|
str(fake_tree),
|
|
"--config",
|
|
str(config_path),
|
|
"--world-size",
|
|
"2",
|
|
"--dry-run",
|
|
"--allow-unrecorded-digest",
|
|
]
|
|
)
|
|
== 1
|
|
)
|
|
assert "not supported" in capsys.readouterr().err
|
|
|
|
|
|
def test_base_hashing_refuses_duplicate_base_tensors(tmp_path):
|
|
"""Base hashing fails closed when two shards repeat a tensor key."""
|
|
base = tmp_path / "model"
|
|
base.mkdir()
|
|
key = "model.layers.0.input_layernorm.weight"
|
|
save_file({key: torch.zeros(1)}, str(base / "b-stale.safetensors"))
|
|
save_file({key: torch.zeros(128)}, str(base / "c-fixed.safetensors"))
|
|
with pytest.raises(ValueError, match="duplicate base tensor"):
|
|
workflow._base_model_file_hashes(base)
|
|
|
|
|
|
def test_resolve_python_prefers_checkout_venv(tmp_path):
|
|
venv_python = tmp_path / ".venv" / "bin" / "python"
|
|
venv_python.parent.mkdir(parents=True)
|
|
venv_python.write_text("#!/bin/sh\n", encoding="utf-8")
|
|
assert workflow.resolve_python(tmp_path) == str(venv_python)
|
|
assert workflow.resolve_python(tmp_path, "/custom/python") == "/custom/python"
|
|
assert workflow.resolve_python(tmp_path / "missing") == sys.executable
|
|
|
|
|
|
def test_fetch_success_verifies_pin(tmp_path, monkeypatch):
|
|
calls: list[list[str]] = []
|
|
|
|
def _fake_run(command, **kwargs):
|
|
calls.append(list(command))
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _fake_run)
|
|
monkeypatch.setattr(source, "_git", lambda _root, *args: source.GSQ_REVISION)
|
|
dest = workflow.fetch(tmp_path / "GSQ")
|
|
assert dest == tmp_path / "GSQ"
|
|
assert len(calls) == 2
|
|
assert calls[0][:2] == ["git", "clone"]
|
|
assert calls[1][-2:] == ["checkout", source.GSQ_REVISION]
|
|
|
|
|
|
def test_load_model_config_rejects_bad_files(tmp_path):
|
|
bad = tmp_path / "bad.json"
|
|
bad.write_text("not json", encoding="utf-8")
|
|
with pytest.raises(RuntimeError, match="cannot load"):
|
|
workflow.load_model_config(bad)
|
|
lst = tmp_path / "list.json"
|
|
lst.write_text("[1]", encoding="utf-8")
|
|
with pytest.raises(RuntimeError, match="must be a JSON object"):
|
|
workflow.load_model_config(lst)
|
|
|
|
|
|
def _write_run_config(tmp_path, name="gsq.yaml"):
|
|
"""Render a runnable config with absolute dirs under tmp_path.
|
|
|
|
The model is a local directory, as the workflow requires: run refuses
|
|
Hub ids and placeholders that GSQ would resolve from the environment.
|
|
The model dir holds a minimal ``config.json`` declaring
|
|
``hidden_size`` so the pre-launch activation sizing (which mirrors
|
|
the pinned trainer) can prove capture stays RAM-resident.
|
|
"""
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps({"hidden_size": TINY_IN}), encoding="utf-8"
|
|
)
|
|
config_path = tmp_path / name
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(tmp_path / "checkpoints")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
return config_path
|
|
|
|
|
|
def _write_minimal_assembled(out_dir, *, ignore=(), dense_visual=False):
|
|
"""Write a minimal assembled dir the post-assembly verifier accepts.
|
|
|
|
Packed tensors for every tiny expected projection (so the retained
|
|
base check sees no missing dense weights), plus an optional dense
|
|
visual Linear that passes only when the ignore list covers it.
|
|
"""
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
|
tiny_expected = gsq_coverage.expected_gsq_modules({"num_hidden_layers": 1})
|
|
tensors: dict[str, torch.Tensor] = {}
|
|
for name in tiny_expected:
|
|
tensors[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
tensors[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
tensors[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
if dense_visual:
|
|
tensors["model.visual.blocks.0.attn.qkv.weight"] = torch.zeros((8, 8))
|
|
shard = "model-00001-of-00001.safetensors"
|
|
save_file(tensors, str(out_dir / shard))
|
|
(out_dir / "model.safetensors.index.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"metadata": {"total_size": 0},
|
|
"weight_map": {key: shard for key in tensors},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
(out_dir / "config.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"model_type": "tiny",
|
|
"quantization_config": {
|
|
"config_groups": {
|
|
"group_0": {
|
|
"input_activations": None,
|
|
"output_activations": None,
|
|
"targets": ["Linear"],
|
|
"weights": {
|
|
"num_bits": 4,
|
|
"strategy": "group",
|
|
"group_size": 128,
|
|
"symmetric": True,
|
|
"type": "int",
|
|
},
|
|
}
|
|
},
|
|
"format": "pack-quantized",
|
|
"ignore": list(ignore),
|
|
"quant_method": "compressed-tensors",
|
|
"quantization_status": "compressed",
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
return out_dir
|
|
|
|
|
|
def test_cli_run_executes_through_main(fake_tree, tmp_path, monkeypatch, capsys):
|
|
config_path = _write_run_config(tmp_path)
|
|
_mock_subprocess(monkeypatch)
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"run",
|
|
str(fake_tree),
|
|
"--config",
|
|
str(config_path),
|
|
"--allow-unrecorded-digest",
|
|
]
|
|
)
|
|
== 0
|
|
)
|
|
assert "main.py --config" in capsys.readouterr().out
|
|
|
|
|
|
def test_manifest_without_checkout_records_unverified_digest(tmp_path):
|
|
checkpoint = tmp_path / "run"
|
|
model_config, _ = _write_tiny_run(checkpoint, tmp_path / "model")
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=checkpoint,
|
|
model="tiny",
|
|
)
|
|
assert result.passed
|
|
manifest = json.loads((checkpoint / workflow.MANIFEST_FILENAME).read_text())
|
|
assert manifest["prepared_tree_verified"] is False
|
|
assert manifest["prepared_tree_sha256"] == source.EXPECTED_PREPARED_TREE_SHA256
|
|
# Without a launch binding the training source is unknown, and the
|
|
# manifest states that: no revision, no digest, not verified.
|
|
assert manifest["launch_gsq_revision"] is None
|
|
assert manifest["launch_prepared_tree_sha256"] is None
|
|
assert manifest["launch_prepared_tree_verified"] is False
|
|
assert manifest["launch_allow_unrecorded_digest"] is False
|
|
|
|
|
|
def test_fetch_failure_is_fail_closed(tmp_path, monkeypatch):
|
|
def _boom(*args, **kwargs):
|
|
raise subprocess.CalledProcessError(1, args[0])
|
|
|
|
monkeypatch.setattr(subprocess, "run", _boom)
|
|
with pytest.raises(RuntimeError, match="cannot fetch"):
|
|
workflow.fetch(tmp_path / "GSQ")
|
|
|
|
|
|
def test_run_quantization_rejects_unrecorded_digest(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
config_path = _write_run_config(tmp_path)
|
|
monkeypatch.setattr(subprocess, "run", lambda *a, **k: None)
|
|
with pytest.raises(RuntimeError, match="does not match recorded"):
|
|
workflow.run_quantization(fake_tree, config_path)
|
|
|
|
|
|
def test_run_quantization_executes_verified_command(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
config_path = _write_run_config(tmp_path)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
code = workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert code == 0
|
|
assert len(calls) == 1
|
|
assert calls[0]["cwd"] == str(fake_tree)
|
|
command = calls[0]["command"]
|
|
# Environment-selected paths may contain words such as "release".
|
|
# Verify their identity separately from the executable tool arguments.
|
|
assert command[0] == sys.executable
|
|
assert command[-1] == str(config_path.resolve())
|
|
tool_arguments = command[1:-1]
|
|
joined = " ".join(tool_arguments)
|
|
assert not any(banned in joined for banned in BANNED_DEPLOYMENT_STRINGS)
|
|
assert tool_arguments == ["main.py", "--config"]
|
|
|
|
|
|
def _checkpoint_from_config(config_path):
|
|
return Path(
|
|
yaml.safe_load(Path(config_path).read_text(encoding="utf-8"))[
|
|
"training"
|
|
]["checkpoint_dir"]
|
|
)
|
|
|
|
|
|
def test_run_unchanged_source_binds_verified(fake_tree, tmp_path, monkeypatch):
|
|
"""An unmoved training source binds verified and returns success."""
|
|
monkeypatch.setattr(
|
|
source,
|
|
"EXPECTED_PREPARED_TREE_SHA256",
|
|
source.tree_digest(fake_tree),
|
|
)
|
|
config_path = _write_run_config(tmp_path)
|
|
_mock_subprocess(monkeypatch)
|
|
assert workflow.run_quantization(fake_tree, config_path) == 0
|
|
checkpoint = _checkpoint_from_config(config_path)
|
|
binding = json.loads(
|
|
(checkpoint / "mock-run-0001" / workflow.LAUNCH_FILENAME).read_text(
|
|
encoding="utf-8"
|
|
)
|
|
)
|
|
assert binding["prepared_tree_verified"] is True
|
|
assert binding["prepared_tree_sha256"] == (
|
|
source.EXPECTED_PREPARED_TREE_SHA256
|
|
)
|
|
assert binding["gsq_revision"] == source.GSQ_REVISION
|
|
records = list(
|
|
checkpoint.glob(
|
|
f"{workflow.LAUNCH_RECORD_PREFIX}*{workflow.LAUNCH_RECORD_SUFFIX}"
|
|
)
|
|
)
|
|
assert len(records) == 1
|
|
record = json.loads(records[0].read_text(encoding="utf-8"))
|
|
assert record["prepared_tree_verified"] is True
|
|
|
|
|
|
def test_run_refuses_launch_when_source_moves_before_spawn(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A main.py edit between capture and spawn refuses to launch.
|
|
|
|
The base-model hashing inside launch-record capture takes a while
|
|
on real checkpoints: the hashing hook below edits ``main.py`` while
|
|
it runs, so the pre-spawn recheck must refuse (raising, without
|
|
spawning the child) and rewrite the launch record unverified.
|
|
"""
|
|
config_path = _write_run_config(tmp_path)
|
|
real_hash_base = workflow._base_model_file_hashes
|
|
|
|
def _mutating_hash_base(base):
|
|
main = fake_tree / "main.py"
|
|
main.write_text(
|
|
main.read_text(encoding="utf-8") + "\n# drift probe\n",
|
|
encoding="utf-8",
|
|
)
|
|
return real_hash_base(base)
|
|
|
|
monkeypatch.setattr(
|
|
workflow, "_base_model_file_hashes", _mutating_hash_base
|
|
)
|
|
calls: list = []
|
|
monkeypatch.setattr(
|
|
subprocess,
|
|
"run",
|
|
lambda *a, **k: calls.append(a[0])
|
|
or subprocess.CompletedProcess(a[0], 0),
|
|
)
|
|
with pytest.raises(RuntimeError, match="training source changed"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert calls == []
|
|
checkpoint = _checkpoint_from_config(config_path)
|
|
assert [path for path in checkpoint.iterdir() if path.is_dir()] == []
|
|
assert not (checkpoint / workflow.LAUNCH_LOCK_FILENAME).exists()
|
|
records = list(
|
|
checkpoint.glob(
|
|
f"{workflow.LAUNCH_RECORD_PREFIX}*{workflow.LAUNCH_RECORD_SUFFIX}"
|
|
)
|
|
)
|
|
assert len(records) == 1
|
|
record = json.loads(records[0].read_text(encoding="utf-8"))
|
|
assert record["prepared_tree_verified"] is False
|
|
assert record["prepared_tree_sha256"] == source.tree_digest(fake_tree)
|
|
|
|
|
|
def _mutating_success_runner(checkout_dir, expected):
|
|
"""Fake ``main.py`` runner that edits ``main.py``, then completes.
|
|
|
|
Mutates the training source mid-run (as an edit landing during a
|
|
long quantization would), then writes one valid run dir with
|
|
``progress.json`` and valid packed shards so the post-exit recheck
|
|
-- not missing output -- is what the test exercises.
|
|
"""
|
|
|
|
def _fake_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
main = Path(checkout_dir) / "main.py"
|
|
main.write_text(
|
|
main.read_text(encoding="utf-8") + "\n# drift probe\n",
|
|
encoding="utf-8",
|
|
)
|
|
checkpoint = Path(
|
|
yaml.safe_load(
|
|
Path(cmd[cmd.index("--config") + 1]).read_text(
|
|
encoding="utf-8"
|
|
)
|
|
)["training"]["checkpoint_dir"]
|
|
)
|
|
run_dir = checkpoint / "mock-run-0001"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
by_file: dict = {}
|
|
for name in expected:
|
|
kind = "mlp" if ".mlp." in name else "self_attn"
|
|
target = by_file.setdefault(
|
|
f"model_layers_0_{kind}.safetensors", {}
|
|
)
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(run_dir / fname))
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
return _fake_run
|
|
|
|
|
|
def _write_fp32_tiny_base(model_dir):
|
|
"""Write fp32 base weights plus a single-layer model config."""
|
|
config = {"num_hidden_layers": 1, "hidden_size": TINY_IN}
|
|
expected = gsq_coverage.expected_gsq_modules(config)
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros(
|
|
(TINY_OUT, TINY_IN), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps(config), encoding="utf-8"
|
|
)
|
|
return expected
|
|
|
|
|
|
def test_run_binds_unverified_when_source_moves_during_child(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A main.py edit during the child run binds unverified, then fails.
|
|
|
|
The run still binds (so completed layers are not lost) but with the
|
|
recomputed digest and ``prepared_tree_verified=False`` -- and the
|
|
successful exit raises instead of returning 0. Validation must then
|
|
record an unverified training source, and assembly must refuse the
|
|
run without ``--allow-unverified-training-source`` (while the
|
|
opt-in still binds it).
|
|
"""
|
|
model_dir = tmp_path / "model"
|
|
expected = _write_fp32_tiny_base(model_dir)
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(tmp_path / "checkpoints")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
monkeypatch.setattr(
|
|
subprocess, "run", _mutating_success_runner(fake_tree, expected)
|
|
)
|
|
with pytest.raises(RuntimeError, match="training source"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
checkpoint = _checkpoint_from_config(config_path)
|
|
run_dir = checkpoint / "mock-run-0001"
|
|
binding = json.loads(
|
|
(run_dir / workflow.LAUNCH_FILENAME).read_text(encoding="utf-8")
|
|
)
|
|
assert binding["prepared_tree_verified"] is False
|
|
assert binding["prepared_tree_sha256"] == source.tree_digest(fake_tree)
|
|
assert binding["exit_code"] == 0
|
|
assert binding["run_id"] == run_dir.name
|
|
records = list(
|
|
checkpoint.glob(
|
|
f"{workflow.LAUNCH_RECORD_PREFIX}*{workflow.LAUNCH_RECORD_SUFFIX}"
|
|
)
|
|
)
|
|
assert len(records) == 1
|
|
record = json.loads(records[0].read_text(encoding="utf-8"))
|
|
assert record["prepared_tree_verified"] is False
|
|
assert record["prepared_tree_sha256"] == binding["prepared_tree_sha256"]
|
|
# The recorded config/base hashes are untouched: only the source moved.
|
|
assert binding["gsq_config_sha256"] == record["gsq_config_sha256"]
|
|
assert binding["base_model_sha256"] == record["base_model_sha256"]
|
|
# Validation preserves the unverified source into the manifest: pin
|
|
# the recorded digest to the (mutated) tree so the checkout side is
|
|
# verified and only the launch side is unverified.
|
|
monkeypatch.setattr(
|
|
source,
|
|
"EXPECTED_PREPARED_TREE_SHA256",
|
|
source.tree_digest(fake_tree),
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
gsq_checkout=fake_tree,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
)
|
|
assert result.passed
|
|
manifest = json.loads(
|
|
(run_dir / workflow.MANIFEST_FILENAME).read_text(encoding="utf-8")
|
|
)
|
|
assert manifest["launch_prepared_tree_verified"] is False
|
|
assert manifest["prepared_tree_verified"] is False
|
|
with pytest.raises(ValueError, match="not verified"):
|
|
workflow._require_validated_manifest(
|
|
run_dir.resolve(), config_path.resolve()
|
|
)
|
|
gated = workflow._require_validated_manifest(
|
|
run_dir.resolve(),
|
|
config_path.resolve(),
|
|
allow_unverified_training_source=True,
|
|
)
|
|
assert gated["run_id"] == run_dir.name
|
|
|
|
|
|
def test_run_interruption_binds_unverified_when_source_moves(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Ctrl-C after a mid-run main.py edit still binds -- unverified."""
|
|
model_dir = tmp_path / "model"
|
|
_write_fp32_tiny_base(model_dir)
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(tmp_path / "checkpoints")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
def _interrupting_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
main = fake_tree / "main.py"
|
|
main.write_text(
|
|
main.read_text(encoding="utf-8") + "\n# drift probe\n",
|
|
encoding="utf-8",
|
|
)
|
|
checkpoint = Path(
|
|
yaml.safe_load(
|
|
Path(cmd[cmd.index("--config") + 1]).read_text(
|
|
encoding="utf-8"
|
|
)
|
|
)["training"]["checkpoint_dir"]
|
|
)
|
|
run_dir = checkpoint / "mock-run-0001"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
raise KeyboardInterrupt("ctrl-c probe")
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _interrupting_run)
|
|
with pytest.raises(KeyboardInterrupt):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
run_dir = _checkpoint_from_config(config_path) / "mock-run-0001"
|
|
binding = json.loads(
|
|
(run_dir / workflow.LAUNCH_FILENAME).read_text(encoding="utf-8")
|
|
)
|
|
assert binding["prepared_tree_verified"] is False
|
|
assert binding["prepared_tree_sha256"] == source.tree_digest(fake_tree)
|
|
assert binding["exit_code"] == workflow.INTERRUPTED_EXIT_CODE
|
|
|
|
|
|
TINY_OUT = 8
|
|
# Divisible by the default group_size 128, like every real GSQ projection:
|
|
# group quantization has no padding path, so the validators refuse any
|
|
# in_features with a remainder.
|
|
TINY_IN = 128
|
|
|
|
|
|
def _write_tiny_run(run_dir, model_dir):
|
|
"""Write a realistic single-layer dense run plus a matching base model.
|
|
|
|
Returns ``(model_config_path, expected_modules)``. Shard names match
|
|
what the assembler consumes (``layers_<N>``) and packed tensors use
|
|
real dtypes/shapes against ``[TINY_OUT, TINY_IN]`` base weights.
|
|
The config declares ``hidden_size`` (matching the base in-dim) so
|
|
config-bound paths can size activation capture the way the pinned
|
|
trainer does.
|
|
"""
|
|
config = {"num_hidden_layers": 1, "hidden_size": TINY_IN}
|
|
expected = gsq_coverage.expected_gsq_modules(config)
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
by_file: dict[str, dict] = {}
|
|
for name in expected:
|
|
kind = "mlp" if ".mlp." in name else "self_attn"
|
|
target = by_file.setdefault(f"model_layers_0_{kind}.safetensors", {})
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(run_dir / fname))
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros(
|
|
(TINY_OUT, TINY_IN), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
model_config = model_dir / "config.json"
|
|
model_config.write_text(json.dumps(config), encoding="utf-8")
|
|
return model_config, expected
|
|
|
|
|
|
def _write_tiny_checkpoint(tmp_path):
|
|
"""Legacy name: write a tiny run; config lives in a base-model dir."""
|
|
checkpoint = tmp_path / "run"
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(checkpoint, model_dir)
|
|
return model_config
|
|
|
|
|
|
def _validate_bound_run(
|
|
tmp_path, *, run_name="run-a", config_name="gsq.yaml"
|
|
):
|
|
"""Write a tiny run plus a matching config and validate with binding.
|
|
|
|
Returns ``(run_dir, config_path, model_config)``. The manifest is the
|
|
real one ``validate`` records (config path/SHA, validation settings,
|
|
shard hashes), so assembly tests exercise the production binding.
|
|
"""
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
run_dir = checkpoint_base / run_name
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(run_dir, model_dir)
|
|
config_path = tmp_path / config_name
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
# Simulate the `run` launch binding: validation with --gsq-config
|
|
# requires the run dir to hold the binding its launch recorded.
|
|
workflow.write_launch_binding(
|
|
run_dir, config_path=config_path, base_model_dir=model_dir
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
)
|
|
assert result.passed
|
|
return run_dir, config_path, model_config
|
|
|
|
|
|
def _mock_subprocess(monkeypatch):
|
|
"""Replace subprocess.run with a recorder returning success.
|
|
|
|
A ``main.py`` invocation additionally simulates a fresh pinned
|
|
launch: it reads the config's absolute training checkpoint dir and
|
|
creates exactly one new run dir holding ``progress.json``, so
|
|
``run_quantization`` can bind the launch to its run. Other commands
|
|
just succeed.
|
|
"""
|
|
calls: list[dict] = []
|
|
counter = {"n": 0}
|
|
|
|
def _fake_run(command, **kwargs):
|
|
cmd = list(command)
|
|
calls.append({"command": cmd, **kwargs})
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
config_path = Path(cmd[cmd.index("--config") + 1])
|
|
checkpoint = Path(
|
|
yaml.safe_load(config_path.read_text(encoding="utf-8"))[
|
|
"training"
|
|
]["checkpoint_dir"]
|
|
)
|
|
counter["n"] += 1
|
|
run_dir = checkpoint / f"mock-run-{counter['n']:04d}"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _fake_run)
|
|
return calls
|
|
|
|
|
|
def _mock_subprocess_with_assembled(monkeypatch, *, writer=None):
|
|
"""Mock save_model.py by writing assembled output to --out-dir.
|
|
|
|
The fresh-dir guard refuses a pre-existing non-empty output, so the
|
|
fake run must create the output (as the real save_model.py would)
|
|
instead of tests pre-creating it. ``writer`` receives the resolved
|
|
out dir; by default a minimal valid assembled dir is written.
|
|
"""
|
|
calls: list[dict] = []
|
|
|
|
def _fake_run(command, **kwargs):
|
|
calls.append({"command": list(command), **kwargs})
|
|
cmd = list(command)
|
|
if "--out-dir" in cmd:
|
|
out = Path(cmd[cmd.index("--out-dir") + 1])
|
|
if writer is not None:
|
|
writer(out)
|
|
else:
|
|
_write_minimal_assembled(out)
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _fake_run)
|
|
return calls
|
|
|
|
|
|
def test_validate_writes_manifest_next_to_checkpoint(
|
|
tmp_path, fake_tree, monkeypatch
|
|
):
|
|
checkpoint = tmp_path / "run"
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(checkpoint, model_dir)
|
|
# The fake tree cannot reproduce the recorded digest; pin the recorded
|
|
# value to the fake tree so the verified-True path is exercised honestly
|
|
# (pin matches, digest matches).
|
|
monkeypatch.setattr(
|
|
source,
|
|
"EXPECTED_PREPARED_TREE_SHA256",
|
|
source.tree_digest(fake_tree),
|
|
)
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(tmp_path / "checkpoints")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
# A verified manifest needs the launch binding too: training-source
|
|
# identity comes from the binding, the checkout state from --gsq-checkout.
|
|
workflow.write_launch_binding(
|
|
checkpoint, config_path=config_path, base_model_dir=model_dir
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=checkpoint,
|
|
gsq_checkout=fake_tree,
|
|
model="Qwen/Qwen3.5-0.8B",
|
|
model_revision="2fc06364715b967f1860aea9cf38778875588b17",
|
|
gsq_config_path=config_path,
|
|
)
|
|
assert result.passed
|
|
manifest = json.loads((checkpoint / workflow.MANIFEST_FILENAME).read_text())
|
|
assert manifest["gsq_repository"] == source.GSQ_REPOSITORY
|
|
assert manifest["gsq_revision"] == source.GSQ_REVISION
|
|
assert manifest["prepared_tree_verified"] is True
|
|
assert manifest["prepared_tree_sha256"] == source.tree_digest(fake_tree)
|
|
assert manifest["launch_gsq_revision"] == source.GSQ_REVISION
|
|
assert manifest["launch_prepared_tree_sha256"] == source.tree_digest(
|
|
fake_tree
|
|
)
|
|
assert manifest["launch_prepared_tree_verified"] is True
|
|
assert manifest["launch_allow_unrecorded_digest"] is False
|
|
assert manifest["model_revision"] == "2fc06364715b967f1860aea9cf38778875588b17"
|
|
assert manifest["coverage"]["passed"] is True
|
|
assert manifest["coverage"]["missing_modules"] == []
|
|
assert manifest["run_id"] == checkpoint.name
|
|
assert manifest["checkpoint_dir"] == str(checkpoint.resolve())
|
|
|
|
|
|
def test_cli_validate_accepts_and_rejects(tmp_path, fake_tree, capsys):
|
|
checkpoint = tmp_path / "run"
|
|
model_config, _ = _write_tiny_run(checkpoint, tmp_path / "model")
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"validate",
|
|
"--model-config",
|
|
str(model_config),
|
|
"--checkpoint-dir",
|
|
str(checkpoint),
|
|
"--no-manifest",
|
|
]
|
|
)
|
|
== 0
|
|
)
|
|
(checkpoint / "model_layers_0_mlp.safetensors").unlink()
|
|
save_file({}, str(checkpoint / "empty.safetensors"))
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"validate",
|
|
"--model-config",
|
|
str(model_config),
|
|
"--checkpoint-dir",
|
|
str(checkpoint),
|
|
"--no-manifest",
|
|
]
|
|
)
|
|
== 1
|
|
)
|
|
assert "error" in capsys.readouterr().err
|
|
|
|
|
|
def test_cli_prepare_enforces_recorded_digest(fake_tree, capsys):
|
|
assert workflow.main(["prepare", str(fake_tree)]) == 1
|
|
assert "does not match recorded" in capsys.readouterr().err
|
|
assert (
|
|
workflow.main(["prepare", str(fake_tree), "--allow-unrecorded-digest"]) == 0
|
|
)
|
|
|
|
|
|
def test_cli_config_and_dry_run(tmp_path, fake_tree, capsys):
|
|
out = tmp_path / "gsq.yaml"
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir()
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"config",
|
|
"--model",
|
|
str(model_dir),
|
|
"--out",
|
|
str(out),
|
|
"--checkpoint-dir",
|
|
str(tmp_path / "ckpt"),
|
|
"--num-samples",
|
|
"8",
|
|
]
|
|
)
|
|
== 0
|
|
)
|
|
assert yaml.safe_load(out.read_text())["data"]["num_samples"] == 8
|
|
assert (
|
|
workflow.main(
|
|
["run", str(fake_tree), "--config", str(out), "--dry-run"]
|
|
)
|
|
== 0
|
|
)
|
|
assert "main.py --config" in capsys.readouterr().out
|
|
|
|
|
|
def test_config_dataset_defaults_and_validates(tmp_path):
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir()
|
|
default = yaml.safe_load(
|
|
workflow.render_gsq_config(str(model_dir), checkpoint_dir="ckpt")
|
|
)
|
|
assert default["data"]["dataset_name"] == "open_thoughts"
|
|
for name in workflow.SUPPORTED_DATASETS:
|
|
rendered = yaml.safe_load(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir="ckpt", dataset=name
|
|
)
|
|
)
|
|
assert rendered["data"]["dataset_name"] == name
|
|
assert set(workflow.SUPPORTED_DATASETS) == {
|
|
"c4",
|
|
"fineweb_edu",
|
|
"open_thoughts",
|
|
"mixed",
|
|
}
|
|
with pytest.raises(ValueError, match="unsupported dataset"):
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir="ckpt", dataset="wiki"
|
|
)
|
|
|
|
|
|
def _write_run(base: Path, name: str, *, complete: bool = True) -> Path:
|
|
run = base / name
|
|
run.mkdir(parents=True)
|
|
if complete:
|
|
(run / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
return run
|
|
|
|
|
|
def test_resolve_run_dir_single_and_explicit(tmp_path):
|
|
base = tmp_path / "checkpoints"
|
|
only = _write_run(base, "20261007-105926_39288c")
|
|
assert workflow.resolve_run_dir(base) == only
|
|
assert workflow.resolve_run_dir(base, "20261007-105926_39288c") == only
|
|
|
|
|
|
def test_resolve_run_dir_fails_closed(tmp_path):
|
|
base = tmp_path / "checkpoints"
|
|
with pytest.raises(ValueError, match="not found"):
|
|
workflow.resolve_run_dir(base / "missing")
|
|
base.mkdir()
|
|
with pytest.raises(ValueError, match="no completed runs"):
|
|
workflow.resolve_run_dir(base)
|
|
_write_run(base, "run-a")
|
|
_write_run(base, "run-b")
|
|
with pytest.raises(ValueError, match="explicit run id"):
|
|
workflow.resolve_run_dir(base)
|
|
with pytest.raises(ValueError, match="no completed run"):
|
|
workflow.resolve_run_dir(base, "run-zzz")
|
|
_write_run(base, "run-empty", complete=False)
|
|
with pytest.raises(ValueError, match="no completed run"):
|
|
workflow.resolve_run_dir(base, "run-empty")
|
|
assert workflow.resolve_run_dir(base, "run-a") == base / "run-a"
|
|
|
|
|
|
def test_build_assemble_command_needs_explicit_partial():
|
|
command = workflow.build_assemble_command(
|
|
"py", "cfg.yaml", run_id="run-a", out_dir="out"
|
|
)
|
|
assert command == [
|
|
"py",
|
|
"save_model.py",
|
|
"--config",
|
|
"cfg.yaml",
|
|
"--run-id",
|
|
"run-a",
|
|
"--out-dir",
|
|
"out",
|
|
]
|
|
partial = workflow.build_assemble_command(
|
|
"py", "cfg.yaml", run_id="run-a", out_dir="out", allow_partial=True
|
|
)
|
|
assert partial == command + ["--allow-partial"]
|
|
|
|
|
|
def test_checkpoint_dir_from_config(tmp_path):
|
|
config_path = tmp_path / "gsq.yaml"
|
|
(tmp_path / "model").mkdir()
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(tmp_path / "model"), checkpoint_dir=str(tmp_path / "ckpt")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
assert workflow._checkpoint_dir_from_config(config_path) == str(
|
|
tmp_path / "ckpt"
|
|
)
|
|
bad = tmp_path / "bad.yaml"
|
|
bad.write_text("not: [valid", encoding="utf-8")
|
|
with pytest.raises(RuntimeError, match="cannot load GSQ config"):
|
|
workflow._checkpoint_dir_from_config(bad)
|
|
empty = tmp_path / "empty.yaml"
|
|
empty.write_text("{}\n", encoding="utf-8")
|
|
with pytest.raises(RuntimeError, match="no training.checkpoint_dir"):
|
|
workflow._checkpoint_dir_from_config(empty)
|
|
|
|
|
|
def test_assemble_checkpoint_uses_helper_not_mtime(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
first, config_path, _ = _validate_bound_run(tmp_path, run_name="run-a")
|
|
_write_run(tmp_path / "checkpoints", "run-b")
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
with pytest.raises(ValueError, match="explicit run id"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
code = workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert code == 0
|
|
assert len(calls) == 1
|
|
assert calls[0]["cwd"] == str(fake_tree)
|
|
assert calls[0]["command"] == [
|
|
"py",
|
|
"save_model.py",
|
|
"--config",
|
|
str(config_path),
|
|
"--run-id",
|
|
"run-a",
|
|
"--out-dir",
|
|
str(first / "assembled"),
|
|
"--run-dir",
|
|
str(first),
|
|
]
|
|
|
|
|
|
def test_assemble_checkpoint_rejects_unrecorded_digest(fake_tree, tmp_path):
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text("{}\n", encoding="utf-8")
|
|
with pytest.raises(RuntimeError, match="does not match recorded"):
|
|
workflow.assemble_checkpoint(fake_tree, config_path)
|
|
|
|
|
|
def test_cli_assemble_and_config_dataset(tmp_path, fake_tree, monkeypatch, capsys):
|
|
checkpoint_dir = tmp_path / "checkpoints"
|
|
run = checkpoint_dir / "run-a"
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(run, model_dir)
|
|
out_yaml = tmp_path / "gsq.yaml"
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"config",
|
|
"--model",
|
|
str(model_dir),
|
|
"--out",
|
|
str(out_yaml),
|
|
"--checkpoint-dir",
|
|
str(checkpoint_dir),
|
|
"--dataset",
|
|
"mixed",
|
|
]
|
|
)
|
|
== 0
|
|
)
|
|
assert yaml.safe_load(out_yaml.read_text())["data"]["dataset_name"] == "mixed"
|
|
workflow.write_launch_binding(
|
|
run, config_path=out_yaml, base_model_dir=model_dir
|
|
)
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"validate",
|
|
"--model-config",
|
|
str(model_config),
|
|
"--checkpoint-dir",
|
|
str(run),
|
|
"--gsq-config",
|
|
str(out_yaml),
|
|
"--model",
|
|
"tiny",
|
|
"--model-revision",
|
|
"rev",
|
|
]
|
|
)
|
|
== 0
|
|
)
|
|
def _cli_fake_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if "--out-dir" in cmd:
|
|
_write_minimal_assembled(Path(cmd[cmd.index("--out-dir") + 1]))
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _cli_fake_run)
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"assemble",
|
|
str(fake_tree),
|
|
"--config",
|
|
str(out_yaml),
|
|
"--allow-unrecorded-digest",
|
|
]
|
|
)
|
|
== 0
|
|
)
|
|
assert "save_model.py --config" in capsys.readouterr().out
|
|
|
|
|
|
def test_run_quantization_resolves_relative_paths_before_chdir(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Relative checkout/config must not resolve inside the checkout."""
|
|
config_path = _write_run_config(tmp_path)
|
|
monkeypatch.chdir(tmp_path)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
code = workflow.run_quantization(
|
|
Path("GSQ"), Path(config_path.name), allow_unrecorded_digest=True
|
|
)
|
|
assert code == 0
|
|
assert len(calls) == 1
|
|
assert calls[0]["cwd"] == str(fake_tree)
|
|
assert Path(calls[0]["cwd"]).is_absolute()
|
|
command = calls[0]["command"]
|
|
assert command[1:3] == ["main.py", "--config"]
|
|
assert command[3] == str(tmp_path / "gsq.yaml")
|
|
assert Path(command[3]).is_absolute()
|
|
assert Path(command[0]).is_absolute()
|
|
|
|
|
|
def test_resolve_interpreter_keeps_bare_commands(tmp_path, monkeypatch):
|
|
monkeypatch.chdir(tmp_path)
|
|
assert workflow._resolve_interpreter("python3") == "python3"
|
|
assert workflow._resolve_interpreter("rel/py") == str(tmp_path / "rel/py")
|
|
|
|
|
|
def test_assemble_resolves_paths_and_passes_run_dir(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""The override run dir must reach save_model.py as --run-dir."""
|
|
config_path = tmp_path / "yaml-home" / "gsq.yaml"
|
|
config_path.parent.mkdir()
|
|
decoy = tmp_path / "decoy-ckpt"
|
|
decoy.mkdir()
|
|
model_dir = tmp_path / "model"
|
|
selected_base = tmp_path / "real-ckpt"
|
|
run = selected_base / "run-a"
|
|
model_config, _ = _write_tiny_run(run, model_dir)
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(str(model_dir), checkpoint_dir=str(decoy)),
|
|
encoding="utf-8",
|
|
)
|
|
workflow.write_launch_binding(
|
|
run, config_path=config_path, base_model_dir=model_dir
|
|
)
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
)
|
|
monkeypatch.chdir(tmp_path)
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
code = workflow.assemble_checkpoint(
|
|
Path("GSQ"),
|
|
Path("yaml-home/gsq.yaml"),
|
|
checkpoint_dir=selected_base,
|
|
out_dir=Path("out"),
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert code == 0
|
|
assert len(calls) == 1
|
|
assert calls[0]["cwd"] == str(fake_tree)
|
|
command = calls[0]["command"]
|
|
assert command[command.index("--config") + 1] == str(config_path)
|
|
assert command[command.index("--run-dir") + 1] == str(run)
|
|
assert command[command.index("--run-id") + 1] == "run-a"
|
|
assert command[command.index("--out-dir") + 1] == str(tmp_path / "out")
|
|
|
|
|
|
def test_build_assemble_command_passes_run_dir():
|
|
command = workflow.build_assemble_command(
|
|
"py", "cfg.yaml", run_id="run-a", out_dir="out", run_dir="/ckpt/run-a"
|
|
)
|
|
assert command[-2:] == ["--run-dir", "/ckpt/run-a"]
|
|
|
|
|
|
def test_prepare_rejects_half_applied_patch_without_writing(fake_tree):
|
|
dataset = fake_tree / "src" / "data" / "dataset.py"
|
|
entry = next(
|
|
item
|
|
for item in source.TEXT_PATCHES
|
|
if item[0] == "src/data/dataset.py" and item[3] == 2
|
|
)
|
|
_, old, new, _ = entry
|
|
text = dataset.read_text(encoding="utf-8")
|
|
assert text.count(old) == 2
|
|
dataset.write_text(text.replace(old, new, 1), encoding="utf-8")
|
|
before = _snapshot(fake_tree)
|
|
with pytest.raises(RuntimeError, match="half-applied"):
|
|
source.prepare(fake_tree)
|
|
assert _snapshot(fake_tree) == before
|
|
assert not (fake_tree / source.GUARD_FILENAME).exists()
|
|
|
|
|
|
def test_prepare_rejects_old_plus_new_without_writing(fake_tree):
|
|
target = fake_tree / "src" / "models" / "qwen35.py"
|
|
entry = next(
|
|
item
|
|
for item in source.TEXT_PATCHES
|
|
if item[0] == "src/models/qwen35.py" and "layer_prefix" in item[1]
|
|
)
|
|
_, old, new, _ = entry
|
|
assert new.count(old) == 0
|
|
text = target.read_text(encoding="utf-8")
|
|
assert text.count(old) == 1
|
|
target.write_text(text + "\n" + new + "\n", encoding="utf-8")
|
|
before = _snapshot(fake_tree)
|
|
with pytest.raises(RuntimeError, match="both present"):
|
|
source.prepare(fake_tree)
|
|
assert _snapshot(fake_tree) == before
|
|
assert not (fake_tree / source.GUARD_FILENAME).exists()
|
|
|
|
|
|
def test_prepare_checkout_refuses_dirty_tree_before_promoting(fake_tree):
|
|
"""An unrelated .py edit refuses before any adapter file is promoted."""
|
|
(fake_tree / "extra_unrelated.py").write_text("EXTRA = 1\n", encoding="utf-8")
|
|
prospective = source.prospective_prepared_digest(fake_tree)
|
|
assert prospective != source.EXPECTED_PREPARED_TREE_SHA256
|
|
before = _snapshot(fake_tree)
|
|
with pytest.raises(RuntimeError, match="does not match recorded"):
|
|
workflow.prepare_checkout(fake_tree)
|
|
assert _snapshot(fake_tree) == before
|
|
assert not list(fake_tree.rglob(f"*{source._PREPARE_TMP_SUFFIX}"))
|
|
assert not list(fake_tree.rglob(f"*{source._PREPARE_BACKUP_SUFFIX}"))
|
|
assert not (fake_tree / source.GUARD_FILENAME).exists()
|
|
_, digest = workflow.prepare_checkout(
|
|
fake_tree, allow_unrecorded_digest=True
|
|
)
|
|
assert digest == prospective
|
|
|
|
|
|
def test_validate_prefers_weight_prefix_over_config_heuristic(tmp_path):
|
|
"""Flattened text weights keep the model prefix despite nesting."""
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir()
|
|
run_dir = tmp_path / "run"
|
|
run_dir.mkdir()
|
|
config = {"text_config": {"num_hidden_layers": 1}, "vision_config": {}}
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps(config), encoding="utf-8"
|
|
)
|
|
expected = gsq_coverage.expected_gsq_modules(config, module_prefix="model")
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros((TINY_OUT, TINY_IN))
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
by_file: dict[str, dict] = {}
|
|
for name in expected:
|
|
kind = "mlp" if ".mlp." in name else "self_attn"
|
|
target = by_file.setdefault(f"model_layers_0_{kind}.safetensors", {})
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128))
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(run_dir / fname))
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
write_manifest_file=False,
|
|
)
|
|
assert result.passed
|
|
assert all(name.startswith("model.layers.") for name in result.packed_modules)
|
|
|
|
|
|
def test_cli_validate_reports_malformed_topology(tmp_path, capsys):
|
|
bad = tmp_path / "config.json"
|
|
bad.write_text(
|
|
json.dumps({"num_hidden_layers": 1, "layer_types": 42}),
|
|
encoding="utf-8",
|
|
)
|
|
checkpoint = tmp_path / "run"
|
|
checkpoint.mkdir()
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"validate",
|
|
"--model-config",
|
|
str(bad),
|
|
"--checkpoint-dir",
|
|
str(checkpoint),
|
|
"--no-manifest",
|
|
]
|
|
)
|
|
== 1
|
|
)
|
|
assert "error" in capsys.readouterr().err
|
|
|
|
|
|
def test_gsq_package_imports_without_heavy_optional_deps():
|
|
probe = subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-c",
|
|
(
|
|
"import sys; import obliteratus.gsq; "
|
|
"import obliteratus.gsq.coverage; "
|
|
"import obliteratus.gsq.numerics_guard; "
|
|
"import obliteratus.gsq.source; "
|
|
"import obliteratus.gsq.workflow; "
|
|
"banned = ('vllm', 'compressed_tensors', 'lion_pytorch', 'wandb'); "
|
|
"bad = [m for m in sys.modules if m.split('.')[0] in banned]; "
|
|
"assert not bad, bad"
|
|
),
|
|
],
|
|
check=False,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
assert probe.returncode == 0, probe.stderr
|
|
|
|
|
|
def test_resolve_interpreter_absolutizes_dot_slash_path(tmp_path, monkeypatch):
|
|
monkeypatch.chdir(tmp_path)
|
|
assert workflow._resolve_interpreter("./python") == str(tmp_path / "python")
|
|
|
|
|
|
def test_validate_requires_explicit_module_prefix_without_weights(tmp_path):
|
|
checkpoint = tmp_path / "run"
|
|
_, expected = _write_tiny_run(checkpoint, tmp_path / "weights")
|
|
# Point the config at a weightless dir: prefix detection fails closed.
|
|
lonely = tmp_path / "lonely"
|
|
lonely.mkdir()
|
|
(lonely / "config.json").write_text(
|
|
json.dumps({"num_hidden_layers": 1}), encoding="utf-8"
|
|
)
|
|
with pytest.raises(ValueError, match="--module-prefix"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=lonely / "config.json",
|
|
checkpoint_dir=checkpoint,
|
|
write_manifest_file=False,
|
|
)
|
|
# An explicit prefix without base weights still fails closed on shapes.
|
|
with pytest.raises(ValueError, match="base-model weights|expected_shapes"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=lonely / "config.json",
|
|
checkpoint_dir=checkpoint,
|
|
module_prefix="model",
|
|
write_manifest_file=False,
|
|
)
|
|
# Explicit shapes are the documented alternative to base weights.
|
|
table = {name: (TINY_OUT, TINY_IN) for name in expected}
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=lonely / "config.json",
|
|
checkpoint_dir=checkpoint,
|
|
module_prefix="model",
|
|
expected_shapes=table,
|
|
write_manifest_file=False,
|
|
)
|
|
assert result.passed
|
|
|
|
|
|
def test_docs_record_known_limitations():
|
|
text = (ROOT / "docs" / "gsq-quantization.md").read_text(encoding="utf-8")
|
|
assert "Known limitations" in text
|
|
assert "llama" in text
|
|
assert "/models/export" in text
|
|
assert "regardless of the directory name" in text
|
|
assert "hybrid dense" in text
|
|
assert "Rename the directory" not in text
|
|
assert "rename the directory" not in text
|
|
assert "--resume" in text
|
|
assert "start-a-fresh-run" in text or "fresh run" in text
|
|
assert "gsq_launch.json" in text
|
|
assert "WORLD_SIZE=1" in text
|
|
|
|
|
|
def test_resolve_interpreter_keeps_venv_symlink(tmp_path, monkeypatch):
|
|
base = tmp_path / "base-python"
|
|
base.write_text("")
|
|
venv_bin = tmp_path / "GSQ" / ".venv" / "bin"
|
|
venv_bin.mkdir(parents=True)
|
|
link = venv_bin / "python"
|
|
link.symlink_to(base)
|
|
monkeypatch.chdir(tmp_path)
|
|
resolved = workflow._resolve_interpreter("GSQ/.venv/bin/python")
|
|
assert resolved == str(link)
|
|
assert resolved != str(base)
|
|
|
|
|
|
def test_manifest_refused_on_prepared_tree_digest_mismatch(
|
|
fake_tree, tmp_path
|
|
):
|
|
"""A pinned checkout with a modified main.py must not verify a run."""
|
|
checkpoint = tmp_path / "run"
|
|
model_config, _ = _write_tiny_run(checkpoint, tmp_path / "model")
|
|
main = fake_tree / "main.py"
|
|
main.write_text(main.read_text(encoding="utf-8") + "# tampered\n")
|
|
assert source.tree_digest(fake_tree) != source.EXPECTED_PREPARED_TREE_SHA256
|
|
with pytest.raises(RuntimeError, match="does not match recorded"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=checkpoint,
|
|
gsq_checkout=fake_tree,
|
|
model="tiny",
|
|
)
|
|
assert not (checkpoint / workflow.MANIFEST_FILENAME).is_file()
|
|
|
|
|
|
def test_rendered_config_uses_absolute_dirs(tmp_path, monkeypatch):
|
|
"""Relative dir inputs resolve against the caller cwd at config time."""
|
|
monkeypatch.chdir(tmp_path)
|
|
(tmp_path / "model").mkdir()
|
|
config = yaml.safe_load(
|
|
workflow.render_gsq_config(
|
|
str(tmp_path / "model"),
|
|
checkpoint_dir="checkpoints",
|
|
log_dir="logs",
|
|
act_cache_dir="act-cache",
|
|
)
|
|
)
|
|
assert config["training"]["checkpoint_dir"] == str(tmp_path / "checkpoints")
|
|
assert config["training"]["log_dir"] == str(tmp_path / "logs")
|
|
assert config["training"]["act_cache_dir"] == str(tmp_path / "act-cache")
|
|
|
|
|
|
def test_run_refuses_relative_checkpoint_dir(fake_tree, tmp_path, monkeypatch):
|
|
"""Training must not silently write the run inside the checkout."""
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
"training:\n checkpoint_dir: checkpoints\n", encoding="utf-8"
|
|
)
|
|
calls: list[dict] = []
|
|
|
|
def _fake_run(command, **kwargs):
|
|
calls.append({"command": list(command), **kwargs})
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _fake_run)
|
|
with pytest.raises(ValueError, match="relative training.checkpoint_dir"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, python="py", allow_unrecorded_digest=True
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_assemble_refuses_relative_config_checkpoint_dir(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Assembly shares the single absolute run location or fails closed."""
|
|
caller = tmp_path / "caller"
|
|
caller.mkdir()
|
|
(caller / "checkpoints").mkdir()
|
|
unrelated = caller / "checkpoints" / "run-x"
|
|
unrelated.mkdir()
|
|
(unrelated / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
"training:\n checkpoint_dir: checkpoints\n", encoding="utf-8"
|
|
)
|
|
monkeypatch.chdir(caller)
|
|
monkeypatch.setattr(
|
|
subprocess,
|
|
"run",
|
|
lambda *a, **k: subprocess.CompletedProcess(a[0], 0),
|
|
)
|
|
with pytest.raises(ValueError, match="relative training.checkpoint_dir"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
|
|
|
|
def test_assemble_requires_validated_manifest_binding(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Assembly binds to the validated run identity, never a guess."""
|
|
run, config_path, model_config = _validate_bound_run(tmp_path)
|
|
other_config = tmp_path / "other.yaml"
|
|
other_config.write_text(
|
|
workflow.render_gsq_config(
|
|
str(tmp_path / "model"),
|
|
checkpoint_dir=str(tmp_path / "checkpoints"),
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
|
|
def _assemble(config=config_path):
|
|
return workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
|
|
# No manifest at all: an unvalidated run is never assembled.
|
|
(run / workflow.MANIFEST_FILENAME).unlink()
|
|
with pytest.raises(ValueError, match="has no gsq_run_manifest.json"):
|
|
_assemble()
|
|
assert calls == []
|
|
# A manifest without a config binding is never assembled.
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
)
|
|
with pytest.raises(ValueError, match="no GSQ config binding"):
|
|
_assemble()
|
|
assert calls == []
|
|
# Manifest for a different config: never assemble across configs.
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=other_config,
|
|
)
|
|
with pytest.raises(ValueError, match="different config"):
|
|
_assemble()
|
|
assert calls == []
|
|
# Manifest recording a failed coverage: never assemble it.
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
)
|
|
manifest_path = run / workflow.MANIFEST_FILENAME
|
|
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
|
manifest["coverage"]["passed"] = False
|
|
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
|
with pytest.raises(ValueError, match="passing coverage"):
|
|
_assemble()
|
|
assert calls == []
|
|
# The validated manifest for this config assembles exactly this run.
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
)
|
|
assert _assemble() == 0
|
|
assert len(calls) == 1
|
|
assert calls[0]["command"][calls[0]["command"].index("--run-dir") + 1] == (
|
|
str(run.resolve())
|
|
)
|
|
|
|
|
|
def test_assemble_refuses_run_after_attention_shard_deleted(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Deleting one attention shard after validation refuses assembly.
|
|
|
|
Every layer keeps an MLP shard, so a layer-index-only check would
|
|
still see all layers; the re-verified hashes and re-run validation
|
|
must fail closed instead.
|
|
"""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
(run / "model_layers_0_self_attn.safetensors").unlink()
|
|
with pytest.raises(ValueError, match="changed since validation|coverage"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_assemble_refuses_run_after_shard_tampered(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Changing shard bytes after validation refuses assembly.
|
|
|
|
The tampered scale stays finite with a valid shape, so re-validation
|
|
alone would pass: only the recorded per-file SHA-256 catches it.
|
|
"""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
shard = run / "model_layers_0_mlp.safetensors"
|
|
tensors = load_file(str(shard))
|
|
tensors["model.layers.0.mlp.gate_proj.weight_scale"] = torch.ones(
|
|
(TINY_OUT, 1), dtype=torch.float32
|
|
)
|
|
save_file(tensors, str(shard))
|
|
with pytest.raises(ValueError, match="changed since validation"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_assemble_refuses_shard_mutated_during_assembly(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A consumed shard rewritten while save_model.py runs fails assembly.
|
|
|
|
The pre-assembly hashes certified the launch bytes and the fake
|
|
assembler writes valid output, but the mutated shard means the
|
|
certified output was merged from different bytes: the post-assembly
|
|
input re-verification must fail closed instead of reporting
|
|
success. The replacement scale keeps a valid shape/dtype so only
|
|
the hash re-check catches it.
|
|
"""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
|
|
def _mutating_writer(out):
|
|
_write_minimal_assembled(out)
|
|
shard = run / "model_layers_0_mlp.safetensors"
|
|
tensors = load_file(str(shard))
|
|
tensors["model.layers.0.mlp.gate_proj.weight_scale"] = torch.ones(
|
|
(TINY_OUT, 1), dtype=torch.float32
|
|
)
|
|
save_file(tensors, str(shard))
|
|
|
|
calls = _mock_subprocess_with_assembled(
|
|
monkeypatch, writer=_mutating_writer
|
|
)
|
|
with pytest.raises(ValueError, match="during assembly"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_refuses_base_file_mutated_during_assembly(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A base-model file rewritten while save_model.py runs fails assembly.
|
|
|
|
The swapped values keep the base dtype/shape (so the output's
|
|
retained-tensor checks still pass): only the post-assembly
|
|
base-hash re-verification catches the drift.
|
|
"""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
|
|
def _mutating_writer(out):
|
|
_write_minimal_assembled(out)
|
|
base_shard = tmp_path / "model" / "model.safetensors"
|
|
tensors = load_file(str(base_shard))
|
|
victim = next(
|
|
key for key in tensors if key.endswith(".weight")
|
|
)
|
|
tensors[victim] = tensors[victim] + 1
|
|
save_file(tensors, str(base_shard))
|
|
|
|
calls = _mock_subprocess_with_assembled(
|
|
monkeypatch, writer=_mutating_writer
|
|
)
|
|
with pytest.raises(ValueError, match="during assembly"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_refuses_gsq_config_mutated_during_assembly(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A GSQ config rewritten while save_model.py runs fails assembly.
|
|
|
|
The appended YAML comment keeps the config loadable (so output
|
|
verification still passes) but changes its bytes: the post-assembly
|
|
config-hash re-verification must fail closed.
|
|
"""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
|
|
def _mutating_writer(out):
|
|
_write_minimal_assembled(out)
|
|
with open(config_path, "a", encoding="utf-8") as handle:
|
|
handle.write("\n# touched during assembly\n")
|
|
|
|
calls = _mock_subprocess_with_assembled(
|
|
monkeypatch, writer=_mutating_writer
|
|
)
|
|
with pytest.raises(ValueError, match="during assembly"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_validate_refuses_settings_contradicting_config(tmp_path):
|
|
"""Validation settings must agree with the bound GSQ config."""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_config, _ = _write_tiny_run(run_dir, tmp_path / "model")
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(tmp_path / "model"),
|
|
checkpoint_dir=str(tmp_path / "checkpoints"),
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
workflow.write_launch_binding(
|
|
run_dir, config_path=config_path, base_model_dir=tmp_path / "model"
|
|
)
|
|
|
|
def _validate(**kwargs):
|
|
return workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
**kwargs,
|
|
)
|
|
|
|
# The rendered config quantizes attention: MLP-only coverage is refused.
|
|
with pytest.raises(ValueError, match="self_attn"):
|
|
_validate(quantize_attention=False)
|
|
# MLP is always quantized by GSQ: --no-mlp is refused with a config.
|
|
with pytest.raises(ValueError, match="always quantizes MLP"):
|
|
_validate(quantize_mlp=False)
|
|
# Scale extents must match the config's group size.
|
|
with pytest.raises(ValueError, match="groupsize"):
|
|
_validate(group_size=64)
|
|
# A config naming another model does not bind to this run.
|
|
other = tmp_path / "other-model"
|
|
other.mkdir()
|
|
foreign = tmp_path / "foreign.yaml"
|
|
foreign.write_text(
|
|
workflow.render_gsq_config(
|
|
str(other), checkpoint_dir=str(tmp_path / "checkpoints")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
with pytest.raises(ValueError, match="base model dir"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=foreign,
|
|
write_manifest_file=False,
|
|
)
|
|
# Matching settings validate.
|
|
assert _validate().passed
|
|
|
|
|
|
def _set_gsq_config_value(config_path, section, key, value):
|
|
"""Rewrite one GSQ YAML mapping value in place."""
|
|
config = yaml.safe_load(config_path.read_text(encoding="utf-8"))
|
|
config[section][key] = value
|
|
config_path.write_text(yaml.safe_dump(config), encoding="utf-8")
|
|
|
|
|
|
def _drop_gsq_config_key(config_path, section, key):
|
|
"""Delete one GSQ YAML mapping key in place."""
|
|
config = yaml.safe_load(config_path.read_text(encoding="utf-8"))
|
|
del config[section][key]
|
|
config_path.write_text(yaml.safe_dump(config), encoding="utf-8")
|
|
|
|
|
|
def _write_bound_tiny_run(tmp_path, *, config_name="gsq.yaml"):
|
|
"""Write a tiny run plus a launch-bound rendered config.
|
|
|
|
Returns ``(run_dir, config_path, model_config)`` with a real
|
|
``gsq_launch.json`` binding, so ``validate --gsq-config`` exercises
|
|
the production launch binding before reaching the method contract.
|
|
"""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_config, _ = _write_tiny_run(run_dir, tmp_path / "model")
|
|
config_path = _write_run_config(tmp_path, name=config_name)
|
|
workflow.write_launch_binding(
|
|
run_dir, config_path=config_path, base_model_dir=tmp_path / "model"
|
|
)
|
|
return run_dir, config_path, model_config
|
|
|
|
|
|
def _validate_with_config(model_config, run_dir, config_path):
|
|
return workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_validate_refuses_non_two_bit_method_contract(tmp_path):
|
|
"""A gsq_bits=4 / wbits=4 config is refused by validate --gsq-config.
|
|
|
|
Four-bit tensors share the two-bit container shapes, so packed
|
|
shape checks alone cannot tell them apart: the widths are certified
|
|
here, before any coverage is reported.
|
|
"""
|
|
run_dir, config_path, model_config = _write_bound_tiny_run(tmp_path)
|
|
_set_gsq_config_value(config_path, "quantization", "gsq_bits", 4)
|
|
_set_gsq_config_value(config_path, "gptq", "wbits", 4)
|
|
workflow.write_launch_binding(
|
|
run_dir, config_path=config_path, base_model_dir=tmp_path / "model"
|
|
)
|
|
with pytest.raises(ValueError, match="gsq_bits"):
|
|
_validate_with_config(model_config, run_dir, config_path)
|
|
|
|
|
|
def test_validate_refuses_omitted_worker_and_mmap_settings(tmp_path):
|
|
"""Omitted num_workers / mmap keys refuse instead of GSQ defaults.
|
|
|
|
The pinned ``src/config.py`` defaults (``num_workers: 8``,
|
|
``act_cache_mmap_threshold_gb: 2.0``) would otherwise silently
|
|
replace the omitted safe values with unsafe ones.
|
|
"""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_config, _ = _write_tiny_run(run_dir, tmp_path / "model")
|
|
for section, key in (
|
|
("data", "num_workers"),
|
|
("training", "act_cache_mmap_threshold_gb"),
|
|
):
|
|
config_path = _write_run_config(tmp_path, name=f"{key}.yaml")
|
|
_drop_gsq_config_key(config_path, section, key)
|
|
workflow.write_launch_binding(
|
|
run_dir, config_path=config_path, base_model_dir=tmp_path / "model"
|
|
)
|
|
with pytest.raises(ValueError, match=key):
|
|
_validate_with_config(model_config, run_dir, config_path)
|
|
|
|
|
|
def test_run_refuses_method_contract_violations(fake_tree, tmp_path):
|
|
"""run refuses 4-bit widths and omitted worker/mmap keys pre-launch."""
|
|
four_bit = _write_run_config(tmp_path, name="four-bit.yaml")
|
|
_set_gsq_config_value(four_bit, "quantization", "gsq_bits", 4)
|
|
_set_gsq_config_value(four_bit, "gptq", "wbits", 4)
|
|
cases = [(four_bit, "gsq_bits")]
|
|
for section, key in (
|
|
("data", "num_workers"),
|
|
("training", "act_cache_mmap_threshold_gb"),
|
|
):
|
|
config_path = _write_run_config(tmp_path, name=f"run-{key}.yaml")
|
|
_drop_gsq_config_key(config_path, section, key)
|
|
cases.append((config_path, key))
|
|
for config_path, pattern in cases:
|
|
with pytest.raises(ValueError, match=pattern):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
|
|
|
|
def test_tree_digest_ignores_checkout_ancestors(tmp_path):
|
|
"""A checkout under a parent named ``venv`` hashes its Python files.
|
|
|
|
Exclusions apply to checkout-relative components only: the same
|
|
tree under ``tmp/venv/GSQ`` and ``tmp/plain/GSQ`` digests
|
|
identically, the venv-parented tree still sees its files, and a
|
|
``.venv`` dir inside the checkout stays excluded.
|
|
"""
|
|
|
|
def _tree(parent):
|
|
root = tmp_path / parent / "GSQ"
|
|
(root / "pkg").mkdir(parents=True)
|
|
(root / "main.py").write_text("print('hi')\n", encoding="utf-8")
|
|
(root / "pkg" / "mod.py").write_text("X = 1\n", encoding="utf-8")
|
|
(root / ".venv" / "lib").mkdir(parents=True)
|
|
(root / ".venv" / "lib" / "skip.py").write_text(
|
|
"Y = 2\n", encoding="utf-8"
|
|
)
|
|
return root
|
|
|
|
venv_root = _tree("venv")
|
|
plain_root = _tree("plain")
|
|
assert source.tree_digest(venv_root) == source.tree_digest(plain_root)
|
|
seen = {
|
|
path.relative_to(venv_root).as_posix()
|
|
for path in source._iter_python_files(venv_root)
|
|
}
|
|
assert seen == {"main.py", "pkg/mod.py"}
|
|
|
|
|
|
def test_assemble_refuses_config_edited_after_validation(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Editing the config after validation refuses assembly."""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
with config_path.open("a", encoding="utf-8") as handle:
|
|
handle.write("# edited after validation\n")
|
|
with pytest.raises(ValueError, match="changed since validation"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_config_renders_absolute_local_model_path(tmp_path, monkeypatch):
|
|
"""A local --model path is absolutized; Hub ids bind a snapshot."""
|
|
monkeypatch.chdir(tmp_path)
|
|
(tmp_path / "models" / "abliterated").mkdir(parents=True)
|
|
local = yaml.safe_load(
|
|
workflow.render_gsq_config(
|
|
"./models/abliterated", checkpoint_dir="checkpoints"
|
|
)
|
|
)
|
|
assert local["model"]["name"] == str(tmp_path / "models" / "abliterated")
|
|
|
|
|
|
def _mock_snapshot_download(monkeypatch, tmp_path):
|
|
"""Mock huggingface_hub.snapshot_download; return seen calls + snap dir."""
|
|
import sys
|
|
import types
|
|
|
|
seen: dict[str, str] = {}
|
|
snap = tmp_path / "snap"
|
|
snap.mkdir(exist_ok=True)
|
|
hub = types.ModuleType("huggingface_hub")
|
|
|
|
def _fake_download(repo_id, revision):
|
|
seen["repo_id"] = repo_id
|
|
seen["revision"] = revision
|
|
return str(snap)
|
|
|
|
hub.snapshot_download = _fake_download
|
|
monkeypatch.setitem(sys.modules, "huggingface_hub", hub)
|
|
return seen, snap
|
|
|
|
|
|
def test_config_hub_id_requires_revision_and_resolves_snapshot(
|
|
tmp_path, monkeypatch
|
|
):
|
|
"""A Hub id without --model-revision is refused; with one it renders."""
|
|
seen, snap = _mock_snapshot_download(monkeypatch, tmp_path)
|
|
with pytest.raises(ValueError, match="model_revision"):
|
|
workflow.render_gsq_config(
|
|
"Qwen/Qwen3.5-0.8B", checkpoint_dir="checkpoints"
|
|
)
|
|
rendered = yaml.safe_load(
|
|
workflow.render_gsq_config(
|
|
"Qwen/Qwen3.5-0.8B",
|
|
checkpoint_dir="checkpoints",
|
|
model_revision="rev123",
|
|
)
|
|
)
|
|
assert rendered["model"]["name"] == str(snap)
|
|
assert seen == {"repo_id": "Qwen/Qwen3.5-0.8B", "revision": "rev123"}
|
|
|
|
|
|
def test_prepare_is_atomic_on_write_failure(fake_tree):
|
|
"""A write failure mid-prepare leaves the tree byte-identical."""
|
|
before = _snapshot(fake_tree)
|
|
target = fake_tree / "src" / "models" / "qwen35.py"
|
|
orig_write_text = Path.write_text
|
|
orig_replace = source.os.replace
|
|
|
|
def _fail_write_text(self, *args, **kwargs):
|
|
if str(self) == str(target):
|
|
raise OSError("disk full (injected)")
|
|
return orig_write_text(self, *args, **kwargs)
|
|
|
|
def _fail_replace(src, dst, *args, **kwargs):
|
|
if str(dst) == str(target):
|
|
raise OSError("disk full (injected)")
|
|
return orig_replace(src, dst, *args, **kwargs)
|
|
|
|
Path.write_text = _fail_write_text
|
|
source.os.replace = _fail_replace
|
|
try:
|
|
with pytest.raises((RuntimeError, OSError), match="disk full"):
|
|
source.prepare(fake_tree)
|
|
finally:
|
|
Path.write_text = orig_write_text
|
|
source.os.replace = orig_replace
|
|
after = {
|
|
str(path.relative_to(fake_tree)): path.read_bytes()
|
|
for path in sorted(fake_tree.rglob("*"))
|
|
if path.is_file() and source._PREPARE_TMP_SUFFIX not in path.name
|
|
}
|
|
for relative, content in before.items():
|
|
assert after.get(relative) == content, relative
|
|
assert (fake_tree / "main.py").read_bytes() == before["main.py"]
|
|
leftovers = [
|
|
path
|
|
for path in fake_tree.rglob(f"*{source._PREPARE_TMP_SUFFIX}")
|
|
if path.is_file()
|
|
]
|
|
assert leftovers == []
|
|
# A retry after the failure is idempotent and succeeds.
|
|
changed = source.prepare(fake_tree)
|
|
assert "main.py" in changed
|
|
assert source.prepare(fake_tree) == []
|
|
|
|
|
|
def test_assemble_refuses_base_model_replaced_with_same_shapes(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Swapping base weights with identical keys/shapes refuses assembly.
|
|
|
|
The replacement keeps every ``[out, in]`` shape, so re-validation
|
|
alone would pass: only the recorded base-model SHA-256 catches the
|
|
swap before ``save_model.py`` runs.
|
|
"""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
manifest = json.loads(
|
|
(run / workflow.MANIFEST_FILENAME).read_text(encoding="utf-8")
|
|
)
|
|
assert manifest["base_model_sha256"]
|
|
assert "model.safetensors" in manifest["base_model_sha256"]
|
|
assert "config.json" in manifest["base_model_sha256"]
|
|
model_file = tmp_path / "model" / "model.safetensors"
|
|
tensors = load_file(str(model_file))
|
|
swapped = {key: torch.ones_like(value) for key, value in tensors.items()}
|
|
save_file(swapped, str(model_file))
|
|
calls = _mock_subprocess(monkeypatch)
|
|
with pytest.raises(ValueError, match="base model"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_certified_dense_wrappers_use_new_compress_api(fake_tree):
|
|
"""Prepared certified wrappers must not call the removed compress API.
|
|
|
|
``llama.py`` inherits the fixed base-class saver while ``qwen3.py``
|
|
and ``qwen35.py`` are patched to ``self._compress_weight`` (the
|
|
locked compressed-tensors 0.15.0.1 no longer provides
|
|
``compressor.compress_weight``). ``gemma4.py`` is not a certified
|
|
family and keeps its upstream call.
|
|
"""
|
|
qwen3_entries = [
|
|
entry
|
|
for entry in source.TEXT_PATCHES
|
|
if entry[0] == "src/models/qwen3.py"
|
|
and "compress_weight" in entry[1]
|
|
]
|
|
assert len(qwen3_entries) == 1
|
|
_relative, old, new, count = qwen3_entries[0]
|
|
assert count == 1
|
|
assert "self.compressor.compress_weight(" in old
|
|
assert "self._compress_weight(" in new
|
|
# The qwen3 fragment must match the qwen35 fragment exactly: the
|
|
# same upstream save path broke in both wrappers.
|
|
qwen35_entries = [
|
|
entry
|
|
for entry in source.TEXT_PATCHES
|
|
if entry[0] == "src/models/qwen35.py"
|
|
and "compress_weight" in entry[1]
|
|
]
|
|
assert len(qwen35_entries) == 1
|
|
assert qwen3_entries[0][1] == qwen35_entries[0][1]
|
|
assert qwen3_entries[0][2] == qwen35_entries[0][2]
|
|
source.prepare(fake_tree)
|
|
for relative in ("src/models/qwen3.py", "src/models/qwen35.py"):
|
|
text = (fake_tree / relative).read_text(encoding="utf-8")
|
|
assert "self.compressor.compress_weight(" not in text, relative
|
|
assert "self._compress_weight(" in text, relative
|
|
|
|
|
|
def test_prepare_rolls_back_post_rename_interrupt(fake_tree):
|
|
"""A post-rename interrupt still leaves the tree byte-identical."""
|
|
before = _snapshot(fake_tree)
|
|
orig_replace = source.os.replace
|
|
calls = {"count": 0}
|
|
|
|
def _post_rename_replace(src, dst, *args, **kwargs):
|
|
result = orig_replace(src, dst, *args, **kwargs)
|
|
calls["count"] += 1
|
|
if calls["count"] == 1:
|
|
raise KeyboardInterrupt("post-rename")
|
|
return result
|
|
|
|
source.os.replace = _post_rename_replace
|
|
try:
|
|
with pytest.raises(KeyboardInterrupt, match="post-rename"):
|
|
source.prepare(fake_tree)
|
|
finally:
|
|
source.os.replace = orig_replace
|
|
assert calls["count"] == 1
|
|
after = {
|
|
str(path.relative_to(fake_tree)): path.read_bytes()
|
|
for path in sorted(fake_tree.rglob("*"))
|
|
if path.is_file()
|
|
}
|
|
assert after == before
|
|
assert [
|
|
path
|
|
for path in fake_tree.rglob(f"*{source._PREPARE_TMP_SUFFIX}")
|
|
if path.is_file()
|
|
] == []
|
|
assert [
|
|
path
|
|
for path in fake_tree.rglob(f"*{source._PREPARE_BACKUP_SUFFIX}")
|
|
if path.is_file()
|
|
] == []
|
|
changed = source.prepare(fake_tree)
|
|
assert "main.py" in changed
|
|
assert source.prepare(fake_tree) == []
|
|
|
|
|
|
def test_prepare_restores_tree_on_keyboard_interrupt(fake_tree):
|
|
"""KeyboardInterrupt between promotions restores the tree and re-raises."""
|
|
before = _snapshot(fake_tree)
|
|
orig_replace = source.os.replace
|
|
calls = {"count": 0}
|
|
|
|
def _flaky_replace(src, dst, *args, **kwargs):
|
|
calls["count"] += 1
|
|
if calls["count"] == 2:
|
|
raise KeyboardInterrupt("injected")
|
|
return orig_replace(src, dst, *args, **kwargs)
|
|
|
|
source.os.replace = _flaky_replace
|
|
try:
|
|
with pytest.raises(KeyboardInterrupt):
|
|
source.prepare(fake_tree)
|
|
finally:
|
|
source.os.replace = orig_replace
|
|
assert calls["count"] == 2
|
|
after = {
|
|
str(path.relative_to(fake_tree)): path.read_bytes()
|
|
for path in sorted(fake_tree.rglob("*"))
|
|
if path.is_file()
|
|
}
|
|
assert after == before
|
|
assert [
|
|
path
|
|
for path in fake_tree.rglob(f"*{source._PREPARE_TMP_SUFFIX}")
|
|
if path.is_file()
|
|
] == []
|
|
assert [
|
|
path
|
|
for path in fake_tree.rglob(f"*{source._PREPARE_BACKUP_SUFFIX}")
|
|
if path.is_file()
|
|
] == []
|
|
# A retry after the interrupt is idempotent and succeeds.
|
|
changed = source.prepare(fake_tree)
|
|
assert "main.py" in changed
|
|
assert source.prepare(fake_tree) == []
|
|
|
|
|
|
_OVERRIDDEN_ENV = {
|
|
"GSQ_MODEL_NAME": "/env/model",
|
|
"GSQ_CHECKPOINT_DIR": "/env/ckpt",
|
|
"GSQ_LOG_DIR": "/env/logs",
|
|
"GSQ_ACT_CACHE_DIR": "/env/act",
|
|
"GSQ_FUTURE_OVERRIDE": "x",
|
|
"WANDB_PROJECT": "env-project",
|
|
"WANDB_ENTITY": "env-entity",
|
|
}
|
|
|
|
|
|
def test_gsq_subprocess_env_strips_config_overrides(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""GSQ_* / WANDB_* in the parent env must not reach the child."""
|
|
config_path = _write_run_config(tmp_path)
|
|
for key, value in _OVERRIDDEN_ENV.items():
|
|
monkeypatch.setenv(key, value)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
assert (
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
== 0
|
|
)
|
|
assert len(calls) == 1
|
|
env = calls[0]["env"]
|
|
for key in _OVERRIDDEN_ENV:
|
|
assert key not in env, key
|
|
assert env["PATH"] == os.environ["PATH"]
|
|
|
|
|
|
def test_assemble_subprocess_env_strips_config_overrides(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Assembly also launches GSQ without the config-override variables."""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
for key, value in _OVERRIDDEN_ENV.items():
|
|
monkeypatch.setenv(key, value)
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
assert (
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
== 0
|
|
)
|
|
env = calls[0]["env"]
|
|
for key in _OVERRIDDEN_ENV:
|
|
assert key not in env, key
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("section", "field", "value"),
|
|
[
|
|
("model", "name", "${X}/model"),
|
|
("training", "checkpoint_dir", "/${X}/ckpt"),
|
|
("training", "log_dir", "${X}/logs"),
|
|
("training", "act_cache_dir", "${X}/act"),
|
|
],
|
|
)
|
|
def test_gsq_placeholder_configs_refused(
|
|
fake_tree, tmp_path, section, field, value
|
|
):
|
|
"""`${X}` placeholders are refused: GSQ would expand them from env."""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_config, _ = _write_tiny_run(run_dir, tmp_path / "model")
|
|
config = yaml.safe_load(
|
|
workflow.render_gsq_config(
|
|
str(tmp_path / "model"),
|
|
checkpoint_dir=str(tmp_path / "checkpoints"),
|
|
)
|
|
)
|
|
config[section][field] = value
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(yaml.safe_dump(config), encoding="utf-8")
|
|
with pytest.raises(ValueError, match="placeholder"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, python="py", allow_unrecorded_digest=True
|
|
)
|
|
with pytest.raises(ValueError, match="placeholder"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def _write_named_config(tmp_path, name, model_name, checkpoint_dir):
|
|
"""Write a GSQ YAML identical to the renderer except for model.name."""
|
|
model_dir = tmp_path / "model"
|
|
config = yaml.safe_load(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_dir)
|
|
)
|
|
)
|
|
config["model"]["name"] = model_name
|
|
config_path = tmp_path / name
|
|
config_path.write_text(yaml.safe_dump(config), encoding="utf-8")
|
|
return config_path
|
|
|
|
|
|
def test_validate_refuses_hub_id_config(tmp_path):
|
|
"""Validation against local model A refuses a YAML naming Hub id B."""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_config, _ = _write_tiny_run(run_dir, tmp_path / "model")
|
|
hub_config = _write_named_config(
|
|
tmp_path, "hub.yaml", "SomeOrg/SomeModel-B", tmp_path / "checkpoints"
|
|
)
|
|
with pytest.raises(ValueError, match="local"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=hub_config,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_validate_refuses_mismatched_local_model_dir(tmp_path):
|
|
"""Validation against local model A refuses a YAML naming dir B."""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_config, _ = _write_tiny_run(run_dir, tmp_path / "model")
|
|
other = tmp_path / "other-model"
|
|
other.mkdir()
|
|
foreign = _write_named_config(
|
|
tmp_path, "foreign.yaml", str(other), tmp_path / "checkpoints"
|
|
)
|
|
with pytest.raises(ValueError, match="base model dir"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=foreign,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_run_refuses_hub_model_name(fake_tree, tmp_path):
|
|
"""Run refuses a config whose model is an unresolved Hub id."""
|
|
(tmp_path / "model").mkdir()
|
|
config_path = _write_named_config(
|
|
tmp_path, "gsq.yaml", "SomeOrg/SomeModel-B", tmp_path / "checkpoints"
|
|
)
|
|
with pytest.raises(ValueError, match="local"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, python="py", allow_unrecorded_digest=True
|
|
)
|
|
|
|
|
|
def test_assemble_refuses_manifest_with_different_base_model(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Assembly refuses a manifest bound to another base model dir."""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
other = tmp_path / "other-model"
|
|
other.mkdir()
|
|
manifest_path = run / workflow.MANIFEST_FILENAME
|
|
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
|
manifest["base_model_dir"] = str(other)
|
|
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
|
calls = _mock_subprocess(monkeypatch)
|
|
with pytest.raises(ValueError, match="base model dir"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def _write_8bit_run(run_dir, model_dir):
|
|
"""Write a run packed at 8 bits: valid coverage math, refused binding.
|
|
|
|
Returns ``(model_config_path, expected_modules)``. The packed shape
|
|
``[out, in*8/32]`` passes the generic validator with ``pack_bits=8``,
|
|
but workflow validation binds the assembler's 4-bit container.
|
|
"""
|
|
model_config, expected = _write_tiny_run(run_dir, model_dir)
|
|
by_file: dict[str, dict] = {}
|
|
for name in expected:
|
|
kind = "mlp" if ".mlp." in name else "self_attn"
|
|
target = by_file.setdefault(f"model_layers_0_{kind}.safetensors", {})
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN * 8 // 32), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(run_dir / fname))
|
|
return model_config, expected
|
|
|
|
|
|
def test_validate_refuses_non_assembler_pack_width(tmp_path):
|
|
"""An 8-bit-packed fixture must fail validation: the container is 4-bit."""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_config, expected = _write_8bit_run(run_dir, tmp_path / "model")
|
|
table = {name: (TINY_OUT, TINY_IN) for name in expected}
|
|
assert gsq_coverage.validate_gsq_checkpoint(
|
|
run_dir, expected, 0, expected_shapes=table, pack_bits=8
|
|
).passed
|
|
with pytest.raises(ValueError, match="4-bit pack container"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
expected_shapes=table,
|
|
pack_bits=8,
|
|
write_manifest_file=False,
|
|
)
|
|
sane = tmp_path / "checkpoints" / "run-b"
|
|
sane_config, _ = _write_tiny_run(sane, tmp_path / "model")
|
|
assert workflow.validate_checkpoint(
|
|
model_config_path=sane_config,
|
|
checkpoint_dir=sane,
|
|
write_manifest_file=False,
|
|
).passed
|
|
|
|
|
|
def test_assemble_verifies_output_after_save_model(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A mocked save_model.py success still certifies the output metadata."""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
mode = {"bad": True}
|
|
calls: list[dict] = []
|
|
|
|
def _fake_run(command, **kwargs):
|
|
calls.append({"command": list(command), **kwargs})
|
|
cmd = list(command)
|
|
out = Path(cmd[cmd.index("--out-dir") + 1])
|
|
if mode["bad"]:
|
|
_write_minimal_assembled(out, dense_visual=True)
|
|
else:
|
|
_write_minimal_assembled(
|
|
out, ignore=["re:.*visual.*"], dense_visual=True
|
|
)
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _fake_run)
|
|
|
|
def _assemble(out):
|
|
return workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
out_dir=out,
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="ignore"):
|
|
_assemble(tmp_path / "out-bad")
|
|
assert len(calls) == 1
|
|
mode["bad"] = False
|
|
assert _assemble(tmp_path / "out-good") == 0
|
|
assert len(calls) == 2
|
|
|
|
|
|
def _dotenv_patch_entry():
|
|
"""Return the main.py TEXT_PATCH that neutralizes the .env reload."""
|
|
for entry in source.TEXT_PATCHES:
|
|
relative, old, new, count = entry
|
|
if relative == "main.py" and "load_dotenv()" in old:
|
|
return entry
|
|
raise AssertionError("main.py load_dotenv TEXT_PATCH is missing")
|
|
|
|
|
|
def test_dotenv_patch_fragment_removes_call():
|
|
"""The adapter patch must delete exactly the main-path reload call."""
|
|
_relative, old, new, count = _dotenv_patch_entry()
|
|
assert count == 1
|
|
assert "load_dotenv()" in old
|
|
assert "load_dotenv()" not in new
|
|
assert "launcher controls the environment" in new
|
|
|
|
|
|
def test_prepared_main_has_no_dotenv_call(fake_tree):
|
|
"""Prepared main.py must not reload .env (fake tree + fragment)."""
|
|
source.prepare(fake_tree)
|
|
text = (fake_tree / "main.py").read_text(encoding="utf-8")
|
|
assert "load_dotenv()" not in text
|
|
assert "launcher controls the environment" in text
|
|
prepared = source.prepare(fake_tree)
|
|
assert prepared == []
|
|
|
|
|
|
def test_save_model_audit_has_no_dotenv_call(fake_tree):
|
|
"""Entry points the workflow launches must not reload .env.
|
|
|
|
main.py's reload is removed by the adapter; save_model.py (the only
|
|
other entry point the workflow launches) must contain no
|
|
load_dotenv call either.
|
|
"""
|
|
source.prepare(fake_tree)
|
|
for entry_point in ("main.py", "save_model.py"):
|
|
text = (fake_tree / entry_point).read_text(encoding="utf-8")
|
|
assert "load_dotenv()" not in text, entry_point
|
|
|
|
|
|
def test_dotenv_file_cannot_reinject_overrides(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A checkout .env must not restore stripped GSQ_* overrides."""
|
|
source.prepare(fake_tree)
|
|
(fake_tree / ".env").write_text(
|
|
"GSQ_MODEL_NAME=/models/B\n", encoding="utf-8"
|
|
)
|
|
config_path = _write_run_config(tmp_path)
|
|
monkeypatch.setenv("GSQ_MODEL_NAME", "/env/model")
|
|
calls = _mock_subprocess(monkeypatch)
|
|
assert (
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
== 0
|
|
)
|
|
env = calls[0]["env"]
|
|
assert "GSQ_MODEL_NAME" not in env
|
|
prepared_main = (fake_tree / "main.py").read_text(encoding="utf-8")
|
|
assert "load_dotenv()" not in prepared_main
|
|
|
|
|
|
def _linear_attn_detection_patch():
|
|
"""Return the save_model TEXT_PATCH inferring Qwen3.5 linear attention."""
|
|
for entry in source.TEXT_PATCHES:
|
|
relative, old, new, count = entry
|
|
if relative == "save_model.py" and "has_linear_attn" in old:
|
|
if "qwen3_5" in new:
|
|
return entry
|
|
raise AssertionError("save_model linear-attention inference patch missing")
|
|
|
|
|
|
def _exec_patched_has_linear_attn():
|
|
"""Exec the patched detection against a minimal extracted function."""
|
|
_relative, old, new, _count = _linear_attn_detection_patch()
|
|
template = (
|
|
"def _probe(model_config):\n"
|
|
' text_config = model_config.get("text_config", {})\n'
|
|
" merged = {**model_config, **text_config}\n"
|
|
f"{old}"
|
|
" return has_linear_attn\n"
|
|
)
|
|
patched_source = template.replace(old, new, 1)
|
|
namespace: dict = {}
|
|
exec(compile(patched_source, "patched_probe.py", "exec"), namespace)
|
|
return namespace["_probe"]
|
|
|
|
|
|
def test_linear_attn_patch_fragment_covers_qwen35():
|
|
"""The inference patch must key on declared Qwen3.5 model types."""
|
|
_relative, old, new, count = _linear_attn_detection_patch()
|
|
assert count == 1
|
|
assert old.count("has_linear_attn") == 1
|
|
assert "qwen3_5" in new
|
|
assert "qwen3_5_text" in new
|
|
assert "layer_types" in new
|
|
|
|
|
|
def test_patched_detection_infers_qwen35_without_layer_types():
|
|
"""Qwen3.5 without layer_types detects linear attention; dense does not."""
|
|
probe = _exec_patched_has_linear_attn()
|
|
assert probe({"model_type": "qwen3_5", "num_hidden_layers": 4}) is True
|
|
assert probe({"text_config": {"model_type": "qwen3_5_text"}}) is True
|
|
assert (
|
|
probe(
|
|
{
|
|
"model_type": "qwen3_5",
|
|
"text_config": {"model_type": "qwen3_5_text"},
|
|
}
|
|
)
|
|
is True
|
|
)
|
|
assert probe({"model_type": "llama", "num_hidden_layers": 4}) is False
|
|
assert (
|
|
probe(
|
|
{"num_hidden_layers": 2, "layer_types": ["full_attention"] * 2}
|
|
)
|
|
is True
|
|
)
|
|
|
|
|
|
def test_mlp_only_inferred_topology_passes_verifier(tmp_path):
|
|
"""MLP-only output for inferred Qwen3.5 passes with the exclusion."""
|
|
config = {"model_type": "qwen3_5_text", "num_hidden_layers": 2}
|
|
expected = gsq_coverage.expected_gsq_modules(
|
|
config, quantize_attention=False
|
|
)
|
|
assert expected and all(".mlp." in name for name in expected)
|
|
assert any(".linear_attn." in name for name in expected) is False
|
|
out = tmp_path / "assembled"
|
|
out.mkdir()
|
|
tensors: dict = {}
|
|
for name in expected:
|
|
tensors[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
tensors[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
tensors[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
linear_dense = "model.layers.0.linear_attn.in_proj_qkv.weight"
|
|
tensors[linear_dense] = torch.zeros((TINY_OUT, TINY_IN))
|
|
shard = "model-00001-of-00001.safetensors"
|
|
save_file(tensors, str(out / shard))
|
|
(out / "model.safetensors.index.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"metadata": {"total_size": 0},
|
|
"weight_map": {key: shard for key in tensors},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
probe = _exec_patched_has_linear_attn()
|
|
assert probe(config) is True
|
|
|
|
def _write_config(ignore):
|
|
(out / "config.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"model_type": "qwen3_5_text",
|
|
"quantization_config": {
|
|
"config_groups": {
|
|
"group_0": {
|
|
"input_activations": None,
|
|
"output_activations": None,
|
|
"targets": ["Linear"],
|
|
"weights": {
|
|
"num_bits": 4,
|
|
"strategy": "group",
|
|
"group_size": 128,
|
|
"symmetric": True,
|
|
"type": "int",
|
|
},
|
|
}
|
|
},
|
|
"format": "pack-quantized",
|
|
"ignore": list(ignore),
|
|
"quant_method": "compressed-tensors",
|
|
"quantization_status": "compressed",
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
_write_config([])
|
|
with pytest.raises(ValueError, match="ignore"):
|
|
gsq_coverage.verify_assembled_checkpoint(out)
|
|
_write_config(["re:.*linear_attn.*"])
|
|
summary = gsq_coverage.verify_assembled_checkpoint(out)
|
|
assert len(summary["packed_modules"]) == len(expected)
|
|
|
|
|
|
def _write_hybrid_base(model_dir, layer_types):
|
|
"""Write base weights for a hybrid topology; return expected modules."""
|
|
config = {"num_hidden_layers": len(layer_types), "layer_types": layer_types}
|
|
expected = gsq_coverage.expected_gsq_modules(config)
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros((TINY_OUT, TINY_IN), dtype=torch.float32)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
return config, expected
|
|
|
|
|
|
def _write_run_for_expected(run_dir, expected, last_layer):
|
|
"""Write packed shards for an expected list plus progress.json."""
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
by_file: dict[str, dict] = {}
|
|
for name in expected:
|
|
idx = name.split(".layers.")[1].split(".")[0]
|
|
kind = "mlp" if ".mlp." in name else (
|
|
"linear_attn" if ".linear_attn." in name else "self_attn"
|
|
)
|
|
target = by_file.setdefault(f"model_layers_{idx}_{kind}.safetensors", {})
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(run_dir / fname))
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": last_layer}), encoding="utf-8"
|
|
)
|
|
|
|
|
|
def test_validate_binds_model_config_to_base_dir(tmp_path):
|
|
"""--base-model-dir requires --model-config to be its own config.json.
|
|
|
|
Binding is by same resolved path (recorded choice): a byte-identical
|
|
copy at a different path still refuses.
|
|
"""
|
|
run_dir = tmp_path / "run"
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(run_dir, model_dir)
|
|
other = tmp_path / "other-model"
|
|
other.mkdir()
|
|
(other / "model.safetensors").write_text("x")
|
|
import shutil
|
|
|
|
shutil.copy(str(model_dir / "model.safetensors"), str(other / "model.safetensors"))
|
|
shutil.copy(str(model_config), str(other / "config.json"))
|
|
with pytest.raises(ValueError, match="own.*config.json"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
base_model_dir=other,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_validate_refuses_shortened_config_with_extra_base_layers(tmp_path):
|
|
"""A two-layer base with a one-layer config refuses (8/15 repro)."""
|
|
base_types = ["linear_attention", "full_attention"]
|
|
_, base_expected = _write_hybrid_base(tmp_path / "model", base_types)
|
|
assert len(base_expected) == 15
|
|
short_config = {"num_hidden_layers": 1, "layer_types": ["linear_attention"]}
|
|
short_expected = gsq_coverage.expected_gsq_modules(short_config)
|
|
assert len(short_expected) == 8
|
|
(tmp_path / "model" / "config.json").write_text(
|
|
json.dumps(short_config), encoding="utf-8"
|
|
)
|
|
run_dir = tmp_path / "run"
|
|
_write_run_for_expected(run_dir, short_expected, last_layer=0)
|
|
with pytest.raises(ValueError, match="additional layer"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=tmp_path / "model" / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
write_manifest_file=False,
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=tmp_path / "model" / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
allow_partial=True,
|
|
write_manifest_file=False,
|
|
)
|
|
assert gsq_coverage.partial_layer_set(result.packed_modules) == [0]
|
|
manifest_path = run_dir / workflow.MANIFEST_FILENAME
|
|
workflow.validate_checkpoint(
|
|
model_config_path=tmp_path / "model" / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
allow_partial=True,
|
|
manifest_path=manifest_path,
|
|
)
|
|
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
|
assert manifest["validation"]["allow_partial"] is True
|
|
assert manifest["validation"]["partial_layers"] == [0]
|
|
|
|
|
|
def _dispatch_patch_entries():
|
|
"""Return the two main.py dispatch TEXT_PATCHES (override + qwen3.5)."""
|
|
first = next(
|
|
entry
|
|
for entry in source.TEXT_PATCHES
|
|
if entry[0] == "main.py" and "name_lower = model_name.lower()" in entry[1]
|
|
)
|
|
second = next(
|
|
entry
|
|
for entry in source.TEXT_PATCHES
|
|
if entry[0] == "main.py"
|
|
and "qwen3.5" in entry[1]
|
|
and "declared_model_type" in entry[2]
|
|
)
|
|
return first, second
|
|
|
|
|
|
def _exec_patched_get_model_wrapper(model_types):
|
|
"""Exec a patched get_model_wrapper with stubbed AutoConfig/wrappers.
|
|
|
|
Reconstructs the pinned dispatch shape (llama / qwen3.5 / qwen3 /
|
|
else) containing the exact TEXT_PATCH old fragments, applies the
|
|
recorded replacements, and execs with a stubbed ``transformers``
|
|
AutoConfig (mapping path -> config) and stubbed wrapper imports.
|
|
``model_types`` maps model path to ``(top_type, text_type_or_None)``;
|
|
a missing path simulates a remote-code/load failure (fallback to
|
|
name-based dispatch).
|
|
"""
|
|
import sys
|
|
import types
|
|
from unittest.mock import patch
|
|
|
|
first, second = _dispatch_patch_entries()
|
|
first_old, first_new = first[1], first[2]
|
|
second_old, second_new = second[1], second[2]
|
|
template = (
|
|
"def get_model_wrapper(model_name, world_size=1):\n"
|
|
f"{first_old}"
|
|
" from src.models.llama import LLaMAWrapper as _L\n"
|
|
" return _L\n"
|
|
f" {second_old}\n"
|
|
" return 'qwen3.5'\n"
|
|
" elif 'qwen3' in name_lower:\n"
|
|
" return 'qwen3'\n"
|
|
" else:\n"
|
|
" raise ValueError('Unsupported')\n"
|
|
)
|
|
patched = template.replace(first_old, first_new, 1).replace(
|
|
second_old, second_new, 1
|
|
)
|
|
from types import SimpleNamespace
|
|
|
|
def _fake_from_pretrained(name, **kwargs):
|
|
assert kwargs.get("trust_remote_code") is False
|
|
if name not in model_types:
|
|
raise RuntimeError("remote code")
|
|
top_type, text_type = model_types[name]
|
|
if text_type is None:
|
|
return SimpleNamespace(model_type=top_type)
|
|
return SimpleNamespace(
|
|
model_type=top_type, text_config=SimpleNamespace(model_type=text_type)
|
|
)
|
|
|
|
fake_transformers = types.ModuleType("transformers")
|
|
fake_transformers.AutoConfig = SimpleNamespace(
|
|
from_pretrained=staticmethod(_fake_from_pretrained)
|
|
)
|
|
fake_src = types.ModuleType("src")
|
|
fake_models = types.ModuleType("src.models")
|
|
fake_llama = types.ModuleType("src.models.llama")
|
|
fake_llama.LLaMAWrapper = "llama"
|
|
stubs = {
|
|
"transformers": fake_transformers,
|
|
"src": fake_src,
|
|
"src.models": fake_models,
|
|
"src.models.llama": fake_llama,
|
|
}
|
|
with patch.dict(sys.modules, stubs):
|
|
namespace: dict = {}
|
|
exec(compile(patched, "patched_dispatch.py", "exec"), namespace)
|
|
raw = namespace["get_model_wrapper"]
|
|
|
|
def dispatch(model_name, world_size=1):
|
|
with patch.dict(sys.modules, stubs):
|
|
return raw(model_name, world_size)
|
|
|
|
return dispatch
|
|
|
|
|
|
def test_dispatch_patch_covers_certified_families():
|
|
"""The dispatch patch maps all certified dense families by config."""
|
|
first, _ = _dispatch_patch_entries()
|
|
new = first[2]
|
|
assert 'declared_model_type == "llama"' in new
|
|
assert 'declared_model_type == "qwen3"' in new
|
|
assert "qwen3_5" in new and "qwen3_5_text" in new
|
|
assert 'name_lower = "llama"' in new
|
|
assert 'name_lower = "qwen3"' in new
|
|
assert 'name_lower = "qwen3.5"' in new
|
|
assert 'getattr(_cfg, "text_config", None) or _cfg' in new
|
|
assert "trust_remote_code=False" in new
|
|
assert not any("_is_moe" in entry[1] or "_is_moe" in entry[2] for entry in source.TEXT_PATCHES)
|
|
|
|
|
|
def test_patched_dispatch_uses_config_regardless_of_path():
|
|
"""Neutral paths dispatch by model_type; llama paths do not hijack."""
|
|
dispatch = _exec_patched_get_model_wrapper(
|
|
{
|
|
"/models/export": ("llama", None),
|
|
}
|
|
)
|
|
assert dispatch("/models/export") == "llama"
|
|
dispatch = _exec_patched_get_model_wrapper(
|
|
{
|
|
"/models/export": ("qwen3", None),
|
|
}
|
|
)
|
|
assert dispatch("/models/export") == "qwen3"
|
|
dispatch = _exec_patched_get_model_wrapper(
|
|
{
|
|
"/models/export": ("qwen3_5", None),
|
|
}
|
|
)
|
|
assert dispatch("/models/export") == "qwen3.5"
|
|
dispatch = _exec_patched_get_model_wrapper(
|
|
{
|
|
"/models/llama-export": ("qwen3_5", "qwen3_5_text"),
|
|
}
|
|
)
|
|
assert dispatch("/models/llama-export") == "qwen3.5"
|
|
|
|
|
|
def test_patched_dispatch_falls_back_to_name_without_config():
|
|
"""Remote-code/load failures keep name-based dispatch."""
|
|
dispatch = _exec_patched_get_model_wrapper({})
|
|
assert dispatch("/models/llama-export") == "llama"
|
|
with pytest.raises(ValueError, match="Unsupported"):
|
|
dispatch("/models/export")
|
|
|
|
|
|
def test_assemble_partial_flag_requires_matching_manifest(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Assemble --allow-partial binds to an explicit partial record."""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(run_dir, model_dir)
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(tmp_path / "checkpoints")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
workflow.write_launch_binding(
|
|
run_dir, config_path=config_path, base_model_dir=model_dir
|
|
)
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
)
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
with pytest.raises(ValueError, match="explicit.*partial"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_partial=True,
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
allow_partial=True,
|
|
)
|
|
manifest = json.loads((run_dir / workflow.MANIFEST_FILENAME).read_text())
|
|
assert manifest["validation"]["allow_partial"] is True
|
|
with pytest.raises(ValueError, match="partial validation"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_partial=False,
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
assert (
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_partial=True,
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
== 0
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_refuses_nonempty_out_dir_before_save_model(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A stale model.safetensors in the out dir refuses before save_model.py."""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
out = tmp_path / "stale-out"
|
|
out.mkdir()
|
|
save_file(
|
|
{"stale.weight": torch.zeros(2)},
|
|
str(out / "model.safetensors"),
|
|
)
|
|
with pytest.raises(ValueError, match="new or empty directory"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
out_dir=out,
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
# The verifier alone also refuses that layout.
|
|
_write_minimal_assembled(tmp_path / "assembled-good")
|
|
good = tmp_path / "assembled-good"
|
|
save_file(
|
|
{"stale.weight": torch.zeros(2)},
|
|
str(good / "model.safetensors"),
|
|
)
|
|
with pytest.raises(ValueError, match="model.safetensors"):
|
|
gsq_coverage.verify_assembled_checkpoint(good)
|
|
|
|
|
|
def test_validate_allow_partial_accepts_interrupted_run(tmp_path):
|
|
"""A 3-layer model stopped after layer 0 validates layers 0..0."""
|
|
config = {"num_hidden_layers": 3}
|
|
expected = gsq_coverage.expected_gsq_modules(config)
|
|
assert len(expected) == 21
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir()
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros((TINY_OUT, TINY_IN), dtype=torch.float32)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps(config), encoding="utf-8"
|
|
)
|
|
run_dir = tmp_path / "run"
|
|
run_dir.mkdir()
|
|
layer0 = [name for name in expected if ".layers.0." in name]
|
|
assert len(layer0) == 7
|
|
by_file: dict[str, dict] = {}
|
|
for name in layer0:
|
|
kind = "mlp" if ".mlp." in name else "self_attn"
|
|
target = by_file.setdefault(f"model_layers_0_{kind}.safetensors", {})
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(run_dir / fname))
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
with pytest.raises(ValueError, match="incomplete"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
write_manifest_file=False,
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
allow_partial=True,
|
|
write_manifest_file=False,
|
|
)
|
|
assert gsq_coverage.partial_layer_set(result.packed_modules) == [0]
|
|
assert len(result.packed_modules) == 7
|
|
manifest_target = run_dir / workflow.MANIFEST_FILENAME
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
allow_partial=True,
|
|
manifest_path=manifest_target,
|
|
)
|
|
manifest = json.loads(manifest_target.read_text(encoding="utf-8"))
|
|
assert manifest["validation"]["allow_partial"] is True
|
|
assert manifest["validation"]["partial_layers"] == [0]
|
|
assert sorted(manifest["coverage"]["packed_module_names"]) == sorted(
|
|
result.packed_modules
|
|
)
|
|
# Shards for layers beyond last_completed refuse even with the flag.
|
|
extra_tensors = {
|
|
f"{name}.weight_packed": torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
for name in expected
|
|
if ".layers.1." in name and ".mlp." in name
|
|
}
|
|
for name in list(extra_tensors):
|
|
base = name[: -len(".weight_packed")]
|
|
extra_tensors[f"{base}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, 1), dtype=torch.float32
|
|
)
|
|
extra_tensors[f"{base}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
save_file(extra_tensors, str(run_dir / "model_layers_1_mlp.safetensors"))
|
|
with pytest.raises(ValueError, match="beyond"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
allow_partial=True,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def _write_shape_bypass_run(tmp_path, *, base_out=16, explicit_out=8, inn=128):
|
|
"""Write a run whose packed trios match explicit shapes, not the base.
|
|
|
|
Base weights beside ``config.json`` are ``[base_out, inn]`` while the
|
|
checkpoint trios (and the explicit table) describe
|
|
``[explicit_out, inn]``. Without base defaulting the explicit table
|
|
would certify the run; with it validation must refuse. The config
|
|
declares ``hidden_size`` (matching the base in-dim) so config-bound
|
|
paths can size activation capture the way the pinned trainer does.
|
|
"""
|
|
config = {"num_hidden_layers": 1, "hidden_size": inn}
|
|
expected = gsq_coverage.expected_gsq_modules(config)
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros(
|
|
(base_out, inn), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps(config), encoding="utf-8"
|
|
)
|
|
run_dir = tmp_path / "run"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
by_file: dict[str, dict] = {}
|
|
for name in expected:
|
|
kind = "mlp" if ".mlp." in name else "self_attn"
|
|
target = by_file.setdefault(f"model_layers_0_{kind}.safetensors", {})
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(explicit_out, inn * 4 // 32), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(explicit_out, ceil(inn / 128)), dtype=torch.float32
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[explicit_out, inn], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(run_dir / fname))
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
table = {name: (explicit_out, inn) for name in expected}
|
|
return model_dir, run_dir, expected, table
|
|
|
|
|
|
def test_validate_explicit_shapes_do_not_bypass_base_weights(tmp_path):
|
|
"""Explicit [8,128] must refuse against real base weights [16,128]."""
|
|
model_dir, run_dir, _expected, table = _write_shape_bypass_run(tmp_path)
|
|
with pytest.raises(ValueError, match="must not override"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
expected_shapes=table,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_assemble_revalidation_refuses_explicit_shape_bypass(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Assembly revalidation drops explicit shapes when base weights exist."""
|
|
model_dir, run_dir, expected, table = _write_shape_bypass_run(tmp_path)
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
checkpoint_base.mkdir()
|
|
target_run = checkpoint_base / "run-a"
|
|
for path in run_dir.iterdir():
|
|
if path.is_file():
|
|
(target_run / path.name).parent.mkdir(parents=True, exist_ok=True)
|
|
(target_run / path.name).write_bytes(path.read_bytes())
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
# Record a pre-fix-style manifest: explicit shapes with no base dir,
|
|
# validated against the packed bytes alone (coverage-level explicit).
|
|
direct = gsq_coverage.validate_gsq_checkpoint(
|
|
target_run,
|
|
expected,
|
|
0,
|
|
expected_shapes=table,
|
|
module_prefix="model",
|
|
)
|
|
assert direct.passed
|
|
workflow.write_manifest(
|
|
target_run / workflow.MANIFEST_FILENAME,
|
|
gsq_checkout=None,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
checkpoint_dir=target_run,
|
|
result=direct,
|
|
model_config_path=model_dir / "config.json",
|
|
module_prefix="model",
|
|
quantize_attention=True,
|
|
quantize_mlp=True,
|
|
expected_count=None,
|
|
group_size=128,
|
|
pack_bits=workflow.ASSEMBLER_PACK_BITS,
|
|
base_model_dir=None,
|
|
expected_shapes=table,
|
|
launch_gsq_revision=source.GSQ_REVISION,
|
|
launch_prepared_tree_sha256=source.EXPECTED_PREPARED_TREE_SHA256,
|
|
launch_prepared_tree_verified=True,
|
|
)
|
|
# The revalidation before assembly also checks the launch binding, so
|
|
# record the launch this run would have had; the shape bypass must
|
|
# still refuse downstream of it.
|
|
workflow.write_launch_binding(
|
|
target_run, config_path=config_path, base_model_dir=model_dir
|
|
)
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
with pytest.raises(ValueError, match="weight_shape holds|must not override"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_run_quantization_refuses_resume(fake_tree, tmp_path, monkeypatch):
|
|
"""run with resume=... refuses before launching anything."""
|
|
config_path = _write_run_config(tmp_path)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
with pytest.raises(ValueError, match="not supported.*fresh run"):
|
|
workflow.run_quantization(
|
|
fake_tree,
|
|
config_path,
|
|
resume="run-a",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_cli_run_resume_is_refused(fake_tree, tmp_path, capsys):
|
|
"""The CLI run --resume flag refuses with a start-fresh error."""
|
|
config_path = _write_run_config(tmp_path)
|
|
assert (
|
|
workflow.main(
|
|
[
|
|
"run",
|
|
str(fake_tree),
|
|
"--config",
|
|
str(config_path),
|
|
"--resume",
|
|
"run-a",
|
|
"--allow-unrecorded-digest",
|
|
]
|
|
)
|
|
== 1
|
|
)
|
|
assert "not supported" in capsys.readouterr().err
|
|
|
|
|
|
def test_run_records_launch_binding_for_single_new_run_dir(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A fresh launch records its binding in the checkpoint and run dirs."""
|
|
# Pin the recorded digest to the fake tree: this exercises the strict
|
|
# verified-launch path with no opt-in flags anywhere.
|
|
monkeypatch.setattr(
|
|
source,
|
|
"EXPECTED_PREPARED_TREE_SHA256",
|
|
source.tree_digest(fake_tree),
|
|
)
|
|
model_dir = tmp_path / "model"
|
|
scratch = tmp_path / "scratch-run"
|
|
model_config, _ = _write_tiny_run(scratch, model_dir)
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
assert workflow.run_quantization(fake_tree, config_path) == 0
|
|
assert len(calls) == 1
|
|
records = list(checkpoint_base.glob(f"{workflow.LAUNCH_RECORD_PREFIX}*"))
|
|
assert len(records) == 1
|
|
run_dir = workflow.resolve_run_dir(checkpoint_base)
|
|
binding_path = run_dir / workflow.LAUNCH_FILENAME
|
|
assert binding_path.is_file()
|
|
binding = json.loads(binding_path.read_text(encoding="utf-8"))
|
|
launch = json.loads(records[0].read_text(encoding="utf-8"))
|
|
assert binding["launch_id"] == launch["launch_id"]
|
|
assert binding["run_id"] == run_dir.name
|
|
assert binding["gsq_config_sha256"] == workflow._sha256_file(
|
|
config_path.resolve()
|
|
)
|
|
assert binding["base_model_sha256"] == workflow._base_model_file_hashes(
|
|
model_dir
|
|
)
|
|
assert binding["base_model_dir"] == str(model_dir.resolve())
|
|
# The launch records the training-source identity: the checkout's
|
|
# revision, its computed digest, and the verified match.
|
|
assert binding["gsq_revision"] == source.GSQ_REVISION
|
|
assert binding["prepared_tree_sha256"] == source.tree_digest(fake_tree)
|
|
assert binding["prepared_tree_verified"] is True
|
|
assert binding["allow_unrecorded_digest"] is False
|
|
# Complete the mocked run with packed shards and certify it through
|
|
# validate (with --gsq-config) and assemble: the launch binding flow.
|
|
_write_tiny_run(run_dir, model_dir)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
)
|
|
assert result.passed
|
|
manifest = json.loads((run_dir / workflow.MANIFEST_FILENAME).read_text())
|
|
assert manifest["launch_prepared_tree_verified"] is True
|
|
assert manifest["launch_prepared_tree_sha256"] == source.tree_digest(
|
|
fake_tree
|
|
)
|
|
assemble_calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
assert (
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id=run_dir.name,
|
|
python="py",
|
|
)
|
|
== 0
|
|
)
|
|
assert len(assemble_calls) == 1
|
|
|
|
|
|
def test_run_records_unverified_training_source(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""An --allow-unrecorded-digest launch is recorded as unverified."""
|
|
config_path = _write_run_config(tmp_path)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
assert (
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
== 0
|
|
)
|
|
assert len(calls) == 1
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
run_dir = workflow.resolve_run_dir(checkpoint_base)
|
|
binding = json.loads(
|
|
(run_dir / workflow.LAUNCH_FILENAME).read_text(encoding="utf-8")
|
|
)
|
|
assert source.tree_digest(fake_tree) != source.EXPECTED_PREPARED_TREE_SHA256
|
|
assert binding["gsq_revision"] == source.GSQ_REVISION
|
|
assert binding["prepared_tree_sha256"] == source.tree_digest(fake_tree)
|
|
assert binding["prepared_tree_verified"] is False
|
|
assert binding["allow_unrecorded_digest"] is True
|
|
|
|
|
|
def test_assemble_refuses_run_trained_from_unverified_tree(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A training run from experimental code cannot be laundered approved.
|
|
|
|
Scenario: launch with ``--allow-unrecorded-digest`` on an
|
|
experimental checkout, restore the approved source, then validate
|
|
with the approved checkout. The manifest must record the approved
|
|
digest with ``prepared_tree_verified`` False (training used
|
|
different code), and assembly must refuse unless
|
|
``--allow-unverified-training-source`` is passed explicitly.
|
|
"""
|
|
# The "approved" source state: pin the recorded digest to the fake
|
|
# tree, so the current checkout verifies at validate/assemble time.
|
|
monkeypatch.setattr(
|
|
source,
|
|
"EXPECTED_PREPARED_TREE_SHA256",
|
|
source.tree_digest(fake_tree),
|
|
)
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
run_dir = checkpoint_base / "run-a"
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(run_dir, model_dir)
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
# The experimental launch: a digest that never matched the recorded
|
|
# tree, recorded with the opt-in flag.
|
|
workflow.write_launch_binding(
|
|
run_dir,
|
|
config_path=config_path,
|
|
base_model_dir=model_dir,
|
|
prepared_tree_sha256="0" * 64,
|
|
prepared_tree_verified=False,
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_checkout=fake_tree,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
)
|
|
assert result.passed
|
|
manifest = json.loads((run_dir / workflow.MANIFEST_FILENAME).read_text())
|
|
assert manifest["prepared_tree_sha256"] == source.tree_digest(fake_tree)
|
|
assert manifest["prepared_tree_verified"] is False
|
|
assert manifest["launch_prepared_tree_sha256"] == "0" * 64
|
|
assert manifest["launch_prepared_tree_verified"] is False
|
|
assert manifest["launch_allow_unrecorded_digest"] is True
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
|
|
def _assemble(**kwargs):
|
|
return workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
**kwargs,
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="was not verified"):
|
|
_assemble()
|
|
assert calls == []
|
|
with pytest.raises(ValueError, match="was not verified"):
|
|
_assemble(allow_unrecorded_digest=True)
|
|
assert calls == []
|
|
# The explicit training-source opt-in assembles.
|
|
assert _assemble(allow_unverified_training_source=True) == 0
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_refuses_manifest_without_training_source(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A manifest predating launch-source binding needs the opt-in too."""
|
|
run, config_path, _ = _validate_bound_run(tmp_path)
|
|
manifest_path = run / workflow.MANIFEST_FILENAME
|
|
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
|
for key in (
|
|
"launch_gsq_revision",
|
|
"launch_prepared_tree_sha256",
|
|
"launch_prepared_tree_verified",
|
|
"launch_allow_unrecorded_digest",
|
|
):
|
|
manifest.pop(key, None)
|
|
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
with pytest.raises(ValueError, match="was not verified"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
assert (
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
allow_unverified_training_source=True,
|
|
)
|
|
== 0
|
|
)
|
|
|
|
|
|
def test_run_refuses_ambiguous_new_run_dirs(fake_tree, tmp_path, monkeypatch):
|
|
"""Zero or several new run dirs after launch fail closed, unbound."""
|
|
|
|
def _write_config():
|
|
return _write_run_config(tmp_path)
|
|
|
|
config_path = _write_config()
|
|
checkpoint = yaml.safe_load(config_path.read_text(encoding="utf-8"))[
|
|
"training"
|
|
]["checkpoint_dir"]
|
|
|
|
def _fake_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py":
|
|
for name in ("stray-a", "stray-b"):
|
|
directory = Path(checkpoint) / name
|
|
directory.mkdir(parents=True, exist_ok=True)
|
|
(directory / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _fake_run)
|
|
with pytest.raises(RuntimeError, match="exactly one new run dir"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
|
|
def _silent_run(command, **kwargs):
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _silent_run)
|
|
with pytest.raises(RuntimeError, match="exactly one new run dir"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
|
|
|
|
def test_validate_with_config_refuses_run_without_launch_binding(tmp_path):
|
|
"""With --gsq-config, a run without a launch binding is refused."""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_config, _ = _write_tiny_run(run_dir, tmp_path / "model")
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(tmp_path / "model"),
|
|
checkpoint_dir=str(tmp_path / "checkpoints"),
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
with pytest.raises(ValueError, match="has no gsq_launch.json"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
# Without --gsq-config the same run still validates (legacy and
|
|
# real-data path: no binding required).
|
|
assert workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
write_manifest_file=False,
|
|
).passed
|
|
|
|
|
|
def _write_same_shaped_base(model_dir, expected):
|
|
"""Write same-shaped but different-bytes base weights plus config.
|
|
|
|
The config declares ``hidden_size`` so config-bound paths can size
|
|
activation capture the way the pinned trainer does.
|
|
"""
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.ones(
|
|
(TINY_OUT, TINY_IN), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps({"num_hidden_layers": 1, "hidden_size": TINY_IN}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
|
|
def test_validate_with_config_refuses_binding_for_other_base(tmp_path):
|
|
"""A run launched from snapshot A is refused with a config naming B.
|
|
|
|
Both snapshots share shapes, so only the launch binding (config SHA
|
|
plus base hashes) distinguishes them.
|
|
"""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_a = tmp_path / "model-a"
|
|
_write_tiny_run(run_dir, model_a)
|
|
expected = gsq_coverage.expected_gsq_modules({"num_hidden_layers": 1})
|
|
model_b = tmp_path / "model-b"
|
|
_write_same_shaped_base(model_b, expected)
|
|
config_a = tmp_path / "gsq-a.yaml"
|
|
config_a.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_a), checkpoint_dir=str(tmp_path / "checkpoints")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
config_b = tmp_path / "gsq-b.yaml"
|
|
config_b.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_b), checkpoint_dir=str(tmp_path / "checkpoints")
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
workflow.write_launch_binding(
|
|
run_dir, config_path=config_a, base_model_dir=model_a
|
|
)
|
|
with pytest.raises(ValueError, match="different GSQ config"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_b / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_b,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_validate_with_config_refuses_base_changed_since_launch(tmp_path):
|
|
"""Swapping same-shaped base bytes under the launch path is refused."""
|
|
run_dir, config_path, model_config = _validate_bound_run(tmp_path)
|
|
model_dir = tmp_path / "model"
|
|
expected = gsq_coverage.expected_gsq_modules({"num_hidden_layers": 1})
|
|
# Same shapes, different bytes: coverage math would still pass, so
|
|
# only the launch binding catches the swap.
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.ones(
|
|
(TINY_OUT, TINY_IN), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
with pytest.raises(ValueError, match="changed since launch"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
_DISTRIBUTED_LAUNCHER_ENV = {
|
|
"WORLD_SIZE": "2",
|
|
"RANK": "1",
|
|
"LOCAL_RANK": "1",
|
|
"LOCAL_WORLD_SIZE": "2",
|
|
"MASTER_ADDR": "10.0.0.1",
|
|
"MASTER_PORT": "9999",
|
|
"GROUP_RANK": "1",
|
|
"GROUP_WORLD_SIZE": "2",
|
|
"ROLE_RANK": "1",
|
|
"ROLE_WORLD_SIZE": "2",
|
|
"ROLE_NAME": "worker",
|
|
"TORCHELASTIC_RUN_ID": "abc123",
|
|
"TORCHELASTIC_RESTART_COUNT": "0",
|
|
}
|
|
|
|
|
|
def test_gsq_subprocess_env_pins_single_gpu_distributed(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Inherited launcher variables cannot leak into the GSQ child."""
|
|
config_path = _write_run_config(tmp_path)
|
|
for key, value in _DISTRIBUTED_LAUNCHER_ENV.items():
|
|
monkeypatch.setenv(key, value)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
assert (
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
== 0
|
|
)
|
|
assert len(calls) == 1
|
|
env = calls[0]["env"]
|
|
assert env["WORLD_SIZE"] == "1"
|
|
assert env["RANK"] == "0"
|
|
assert env["LOCAL_RANK"] == "0"
|
|
assert env["LOCAL_WORLD_SIZE"] == "1"
|
|
for key in (
|
|
"MASTER_ADDR",
|
|
"MASTER_PORT",
|
|
"GROUP_RANK",
|
|
"GROUP_WORLD_SIZE",
|
|
"ROLE_RANK",
|
|
"ROLE_WORLD_SIZE",
|
|
"ROLE_NAME",
|
|
"TORCHELASTIC_RUN_ID",
|
|
"TORCHELASTIC_RESTART_COUNT",
|
|
):
|
|
assert key not in env, key
|
|
|
|
|
|
def _write_1layer_base(model_dir):
|
|
"""Write single-layer base weights plus config; return expected.
|
|
|
|
The config declares ``hidden_size`` so config-bound paths can size
|
|
activation capture the way the pinned trainer does.
|
|
"""
|
|
expected = gsq_coverage.expected_gsq_modules({"num_hidden_layers": 1})
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros(
|
|
(TINY_OUT, TINY_IN), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps({"num_hidden_layers": 1, "hidden_size": TINY_IN}),
|
|
encoding="utf-8",
|
|
)
|
|
return expected
|
|
|
|
|
|
def test_run_preserves_prelaunch_hashes_and_refuses_tampered_base(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Replacing the base snapshot mid-run must not certify the run.
|
|
|
|
The mocked subprocess swaps same-shaped base bytes (coverage math
|
|
would still pass) and creates the single new run dir. The run-dir
|
|
binding must carry the preserved pre-launch hashes -- never the
|
|
replacement -- so the launch refuses to bind and validate refuses.
|
|
"""
|
|
model_dir = tmp_path / "model"
|
|
expected = _write_1layer_base(model_dir)
|
|
assert expected
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
pre_config_sha = workflow._sha256_file(config_path.resolve())
|
|
pre_base = workflow._base_model_file_hashes(model_dir.resolve())
|
|
|
|
def _tampering_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
cfg = Path(cmd[cmd.index("--config") + 1])
|
|
checkpoint = Path(
|
|
yaml.safe_load(cfg.read_text(encoding="utf-8"))[
|
|
"training"
|
|
]["checkpoint_dir"]
|
|
)
|
|
run_dir = checkpoint / "mock-run-0001"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.ones(
|
|
(TINY_OUT, TINY_IN), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _tampering_run)
|
|
with pytest.raises(RuntimeError, match="changed during training"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
run_dir = checkpoint_base / "mock-run-0001"
|
|
assert run_dir.is_dir()
|
|
assert not (run_dir / workflow.LAUNCH_FILENAME).is_file()
|
|
records = list(
|
|
checkpoint_base.glob(f"{workflow.LAUNCH_RECORD_PREFIX}*")
|
|
)
|
|
assert len(records) == 1
|
|
launch = json.loads(records[0].read_text(encoding="utf-8"))
|
|
assert launch["gsq_config_sha256"] == pre_config_sha
|
|
assert launch["base_model_sha256"] == pre_base
|
|
assert launch["base_model_sha256"] != workflow._base_model_file_hashes(
|
|
model_dir
|
|
)
|
|
with pytest.raises(ValueError, match="has no gsq_launch.json"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_failed_run_binds_partial_and_validates_prefix(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A non-zero exit with layer-0 shards still binds the valid prefix.
|
|
|
|
The mocked subprocess writes only layer-0 shards plus progress at
|
|
layer 0 for a 3-layer model and exits 1. Run must return 1 while
|
|
writing the binding with the exit status, so validate
|
|
--allow-partial certifies layers [0] and validation without the
|
|
flag still refuses the incomplete run.
|
|
"""
|
|
# hidden_size lets config-bound paths size activation capture the
|
|
# way the pinned trainer does.
|
|
config = {"num_hidden_layers": 3, "hidden_size": TINY_IN}
|
|
expected = gsq_coverage.expected_gsq_modules(config)
|
|
assert len(expected) == 21
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir()
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros(
|
|
(TINY_OUT, TINY_IN), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
model_config = model_dir / "config.json"
|
|
model_config.write_text(json.dumps(config), encoding="utf-8")
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
layer0 = [name for name in expected if ".layers.0." in name]
|
|
assert len(layer0) == 7
|
|
|
|
def _failing_partial_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
cfg = Path(cmd[cmd.index("--config") + 1])
|
|
checkpoint = Path(
|
|
yaml.safe_load(cfg.read_text(encoding="utf-8"))[
|
|
"training"
|
|
]["checkpoint_dir"]
|
|
)
|
|
run_dir = checkpoint / "mock-run-0001"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
by_file: dict[str, dict] = {}
|
|
for name in layer0:
|
|
kind = "mlp" if ".mlp." in name else "self_attn"
|
|
target = by_file.setdefault(
|
|
f"model_layers_0_{kind}.safetensors", {}
|
|
)
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(run_dir / fname))
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
return subprocess.CompletedProcess(command, 1)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _failing_partial_run)
|
|
assert (
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
== 1
|
|
)
|
|
run_dir = checkpoint_base / "mock-run-0001"
|
|
binding_path = run_dir / workflow.LAUNCH_FILENAME
|
|
assert binding_path.is_file()
|
|
binding = json.loads(binding_path.read_text(encoding="utf-8"))
|
|
assert binding["exit_code"] == 1
|
|
assert binding["last_completed_layer"] == 0
|
|
assert binding["run_id"] == run_dir.name
|
|
records = list(
|
|
checkpoint_base.glob(f"{workflow.LAUNCH_RECORD_PREFIX}*")
|
|
)
|
|
assert len(records) == 1
|
|
launch = json.loads(records[0].read_text(encoding="utf-8"))
|
|
assert binding["launch_id"] == launch["launch_id"]
|
|
assert binding["gsq_config_sha256"] == launch["gsq_config_sha256"]
|
|
assert binding["base_model_sha256"] == launch["base_model_sha256"]
|
|
with pytest.raises(ValueError, match="incomplete"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
allow_partial=True,
|
|
)
|
|
assert gsq_coverage.partial_layer_set(result.packed_modules) == [0]
|
|
manifest = json.loads(
|
|
(run_dir / workflow.MANIFEST_FILENAME).read_text(encoding="utf-8")
|
|
)
|
|
assert manifest["validation"]["allow_partial"] is True
|
|
assert manifest["validation"]["partial_layers"] == [0]
|
|
|
|
|
|
def test_run_refuses_nested_base_index_before_launch(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Run refuses before launch when the index points into a subdir.
|
|
|
|
GSQ training follows ``model.safetensors.index.json`` (including
|
|
nested shard paths) while the launch binding used to cover only
|
|
top-level weights: a run could train against nested snapshot B while
|
|
top-level snapshot A stayed unchanged. The index must agree with the
|
|
top-level shard set before anything launches.
|
|
"""
|
|
model_dir = tmp_path / "model"
|
|
scratch = tmp_path / "scratch-run"
|
|
_write_tiny_run(scratch, model_dir)
|
|
tiny_expected = gsq_coverage.expected_gsq_modules({"num_hidden_layers": 1})
|
|
nested = model_dir / "b"
|
|
nested.mkdir()
|
|
save_file(
|
|
load_file(str(model_dir / "model.safetensors")),
|
|
str(nested / "model.safetensors"),
|
|
)
|
|
victim = f"{tiny_expected[0]}.weight"
|
|
weight_map = {f"{name}.weight": "model.safetensors" for name in tiny_expected}
|
|
weight_map[victim] = "b/model.safetensors"
|
|
(model_dir / "model.safetensors.index.json").write_text(
|
|
json.dumps({"metadata": {}, "weight_map": weight_map}),
|
|
encoding="utf-8",
|
|
)
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
calls: list = []
|
|
|
|
def _must_not_launch(command, **kwargs):
|
|
calls.append(list(command))
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _must_not_launch)
|
|
with pytest.raises(ValueError, match="top-level"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert calls == []
|
|
assert (
|
|
list(checkpoint_base.glob(f"{workflow.LAUNCH_RECORD_PREFIX}*")) == []
|
|
)
|
|
|
|
|
|
def _launch_tiny_indexed_run(fake_tree, tmp_path, monkeypatch):
|
|
"""Launch a mocked tiny run against an agreeing indexed base.
|
|
|
|
Returns ``(run_dir, config_path, model_config, model_dir)`` with the
|
|
mocked run completed with packed shards, ready to validate.
|
|
"""
|
|
model_dir = tmp_path / "model"
|
|
scratch = tmp_path / "scratch-run"
|
|
model_config, _ = _write_tiny_run(scratch, model_dir)
|
|
tensors = load_file(str(model_dir / "model.safetensors"))
|
|
(model_dir / "model.safetensors.index.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"metadata": {"total_size": 0},
|
|
"weight_map": {key: "model.safetensors" for key in tensors},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
_mock_subprocess(monkeypatch)
|
|
assert (
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
== 0
|
|
)
|
|
run_dir = workflow.resolve_run_dir(checkpoint_base)
|
|
_write_tiny_run(run_dir, model_dir)
|
|
return run_dir, config_path, model_config, model_dir
|
|
|
|
|
|
def test_validate_refuses_base_index_removed_after_launch(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Removing the weight index after launch refuses validation.
|
|
|
|
The index is part of the launch/base hash set, so removing it after
|
|
launch is drift: validation must refuse instead of certifying the
|
|
run against the unindexed top-level files.
|
|
"""
|
|
run_dir, config_path, model_config, model_dir = _launch_tiny_indexed_run(
|
|
fake_tree, tmp_path, monkeypatch
|
|
)
|
|
(model_dir / "model.safetensors.index.json").unlink()
|
|
with pytest.raises(ValueError, match="index"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_validate_refuses_base_index_edited_after_launch(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Editing the weight index after launch refuses validation.
|
|
|
|
Any byte change to the index (even outside ``weight_map``) is drift
|
|
against the launch binding: validation must refuse instead of
|
|
certifying the run against the edited lookup.
|
|
"""
|
|
run_dir, config_path, model_config, model_dir = _launch_tiny_indexed_run(
|
|
fake_tree, tmp_path, monkeypatch
|
|
)
|
|
index_path = model_dir / "model.safetensors.index.json"
|
|
payload = json.loads(index_path.read_text(encoding="utf-8"))
|
|
payload["metadata"] = {"total_size": 1}
|
|
index_path.write_text(json.dumps(payload), encoding="utf-8")
|
|
with pytest.raises(ValueError, match="changed since launch"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_run_binds_partial_on_keyboard_interrupt_and_reraises(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Ctrl-C during training still binds the partial run, then re-raises.
|
|
|
|
A ``KeyboardInterrupt`` from the GSQ subprocess must not skip the
|
|
``gsq_launch.json`` binding: the single new run dir holding
|
|
``progress.json`` is bound with the interrupted exit status so
|
|
``validate --allow-partial --gsq-config`` can certify the valid
|
|
prefix, and the interrupt itself re-raises.
|
|
"""
|
|
# hidden_size lets config-bound paths size activation capture the
|
|
# way the pinned trainer does.
|
|
config = {"num_hidden_layers": 3, "hidden_size": TINY_IN}
|
|
expected = gsq_coverage.expected_gsq_modules(config)
|
|
assert len(expected) == 21
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir()
|
|
save_file(
|
|
{
|
|
f"{name}.weight": torch.zeros(
|
|
(TINY_OUT, TINY_IN), dtype=torch.float32
|
|
)
|
|
for name in expected
|
|
},
|
|
str(model_dir / "model.safetensors"),
|
|
)
|
|
model_config = model_dir / "config.json"
|
|
model_config.write_text(json.dumps(config), encoding="utf-8")
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
layer0 = [name for name in expected if ".layers.0." in name]
|
|
assert len(layer0) == 7
|
|
|
|
def _interrupted_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
cfg = Path(cmd[cmd.index("--config") + 1])
|
|
checkpoint = Path(
|
|
yaml.safe_load(cfg.read_text(encoding="utf-8"))["training"][
|
|
"checkpoint_dir"
|
|
]
|
|
)
|
|
interrupted = checkpoint / "mock-run-0001"
|
|
interrupted.mkdir(parents=True, exist_ok=True)
|
|
by_file: dict[str, dict] = {}
|
|
for name in layer0:
|
|
kind = "mlp" if ".mlp." in name else "self_attn"
|
|
target = by_file.setdefault(
|
|
f"model_layers_0_{kind}.safetensors", {}
|
|
)
|
|
target[f"{name}.weight_packed"] = torch.zeros(
|
|
(TINY_OUT, TINY_IN // 8), dtype=torch.int32
|
|
)
|
|
target[f"{name}.weight_scale"] = torch.zeros(
|
|
(TINY_OUT, ceil(TINY_IN / 128)), dtype=torch.float32
|
|
)
|
|
target[f"{name}.weight_shape"] = torch.tensor(
|
|
[TINY_OUT, TINY_IN], dtype=torch.int64
|
|
)
|
|
for fname, tensors in by_file.items():
|
|
save_file(tensors, str(interrupted / fname))
|
|
(interrupted / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
raise KeyboardInterrupt("simulated Ctrl-C")
|
|
|
|
monkeypatch.setattr(subprocess, "run", _interrupted_run)
|
|
with pytest.raises(KeyboardInterrupt):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
run_dir = checkpoint_base / "mock-run-0001"
|
|
binding = json.loads(
|
|
(run_dir / workflow.LAUNCH_FILENAME).read_text(encoding="utf-8")
|
|
)
|
|
assert binding["exit_code"] == workflow.INTERRUPTED_EXIT_CODE
|
|
assert binding["last_completed_layer"] == 0
|
|
assert binding["run_id"] == run_dir.name
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
gsq_config_path=config_path,
|
|
allow_partial=True,
|
|
)
|
|
assert gsq_coverage.partial_layer_set(result.packed_modules) == [0]
|
|
|
|
|
|
def _checkpoint_from_config(config_path):
|
|
"""Read the absolute training checkpoint dir from a GSQ YAML config."""
|
|
return Path(
|
|
yaml.safe_load(config_path.read_text(encoding="utf-8"))["training"][
|
|
"checkpoint_dir"
|
|
]
|
|
)
|
|
|
|
|
|
def test_run_refuses_when_launch_lock_held(fake_tree, tmp_path, monkeypatch):
|
|
"""A second launch against a locked checkpoint dir refuses pre-launch.
|
|
|
|
Concurrent launches must never create run dirs under the same
|
|
checkpoint directory: while one workflow launch holds
|
|
``gsq_launch.lock`` there, another refuses with a clear error before
|
|
launching anything, and the foreign lock is left for its owner.
|
|
"""
|
|
config_path = _write_run_config(tmp_path)
|
|
checkpoint = _checkpoint_from_config(config_path)
|
|
checkpoint.mkdir(parents=True, exist_ok=True)
|
|
lock_path = checkpoint / workflow.LAUNCH_LOCK_FILENAME
|
|
lock_path.write_text("4242:other-launch\n", encoding="utf-8")
|
|
calls: list = []
|
|
|
|
def _must_not_launch(command, **kwargs):
|
|
calls.append(list(command))
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _must_not_launch)
|
|
with pytest.raises(RuntimeError, match="launch lock|another workflow launch"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert calls == []
|
|
assert lock_path.read_text(encoding="utf-8") == "4242:other-launch\n"
|
|
|
|
|
|
def test_run_holds_and_releases_launch_lock(fake_tree, tmp_path, monkeypatch):
|
|
"""The launch lock spans the launch and always releases afterwards.
|
|
|
|
The lock file (holding ``<pid>:<launch id>``) exists while the
|
|
training subprocess runs, and is gone after success, after a failed
|
|
launch, and after a ``KeyboardInterrupt`` before any run dir exists.
|
|
"""
|
|
config_path = _write_run_config(tmp_path)
|
|
checkpoint = _checkpoint_from_config(config_path)
|
|
lock_path = checkpoint / workflow.LAUNCH_LOCK_FILENAME
|
|
|
|
def _successful_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
assert lock_path.is_file()
|
|
pid, _, launch_id = (
|
|
lock_path.read_text(encoding="utf-8").strip().partition(":")
|
|
)
|
|
assert pid == str(os.getpid())
|
|
assert launch_id
|
|
run_dir = checkpoint / "mock-run-0001"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _successful_run)
|
|
assert (
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
== 0
|
|
)
|
|
assert not lock_path.exists()
|
|
|
|
# A launch that fails without creating a run dir returns its exit
|
|
# code unbound and still releases the lock.
|
|
monkeypatch.setattr(
|
|
subprocess,
|
|
"run",
|
|
lambda *a, **k: subprocess.CompletedProcess(a[0], 3),
|
|
)
|
|
assert (
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
== 3
|
|
)
|
|
assert not lock_path.exists()
|
|
|
|
# KeyboardInterrupt before any run dir exists re-raises with the
|
|
# lock released.
|
|
def _interrupt(command, **kwargs):
|
|
raise KeyboardInterrupt("simulated Ctrl-C")
|
|
|
|
monkeypatch.setattr(subprocess, "run", _interrupt)
|
|
with pytest.raises(KeyboardInterrupt):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert not lock_path.exists()
|
|
|
|
|
|
def test_run_refuses_to_overwrite_existing_launch_binding(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A fresh run dir keeps another launch's binding: refuse, don't overwrite.
|
|
|
|
When the single new run dir already holds ``gsq_launch.json`` from
|
|
another launch, ``run`` refuses instead of rebinding that launch's
|
|
weights to this launch's config and base hashes.
|
|
"""
|
|
config_path = _write_run_config(tmp_path)
|
|
checkpoint = _checkpoint_from_config(config_path)
|
|
|
|
def _planted_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
run_dir = checkpoint / "mock-run-0001"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
(run_dir / workflow.LAUNCH_FILENAME).write_text(
|
|
json.dumps({"run_id": "mock-run-0001", "launch_id": "other"}),
|
|
encoding="utf-8",
|
|
)
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _planted_run)
|
|
with pytest.raises(RuntimeError, match="already holds gsq_launch.json"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert json.loads(
|
|
(checkpoint / "mock-run-0001" / workflow.LAUNCH_FILENAME).read_text(
|
|
encoding="utf-8"
|
|
)
|
|
) == {"run_id": "mock-run-0001", "launch_id": "other"}
|
|
assert not (checkpoint / workflow.LAUNCH_LOCK_FILENAME).exists()
|
|
|
|
|
|
def _fail_next_fsync(monkeypatch):
|
|
"""Make the next ``os.fsync`` raise ``OSError`` (simulated disk full).
|
|
|
|
The failure fires once, so a retry after the failed write succeeds.
|
|
"""
|
|
real_fsync = os.fsync
|
|
state = {"calls": 0}
|
|
|
|
def _flaky(fd):
|
|
if state["calls"] == 0:
|
|
state["calls"] += 1
|
|
raise OSError("simulated disk full")
|
|
return real_fsync(fd)
|
|
|
|
monkeypatch.setattr(workflow.os, "fsync", _flaky)
|
|
return state
|
|
|
|
|
|
def _leftover_tmps(directory):
|
|
"""Return temp files staged by the atomic JSON writers, if any."""
|
|
return [path for path in directory.iterdir() if path.suffix == ".tmp"]
|
|
|
|
|
|
def test_write_run_binding_partial_write_leaves_no_file_and_retry_succeeds(
|
|
tmp_path, monkeypatch
|
|
):
|
|
"""A failed binding write leaves no destination behind, so retry works.
|
|
|
|
The binding is staged to a same-directory temp file and published
|
|
with ``os.link``: a disk-full error during staging must leave no
|
|
``gsq_launch.json`` (and no temp litter), so the retry after the
|
|
failure binds the run instead of refusing on a truncated file.
|
|
"""
|
|
run_dir = tmp_path / "run-a"
|
|
run_dir.mkdir(parents=True)
|
|
binding = {"run_id": "run-a", "launch_id": "abc", "schema_version": 1}
|
|
_fail_next_fsync(monkeypatch)
|
|
with pytest.raises(OSError, match="simulated disk full"):
|
|
workflow._write_run_binding(run_dir, binding)
|
|
dest = run_dir / workflow.LAUNCH_FILENAME
|
|
assert not dest.exists()
|
|
assert _leftover_tmps(run_dir) == []
|
|
assert workflow._write_run_binding(run_dir, binding) == dest
|
|
assert json.loads(dest.read_text(encoding="utf-8")) == binding
|
|
assert _leftover_tmps(run_dir) == []
|
|
|
|
|
|
def test_write_json_record_partial_write_leaves_no_file_and_retry_succeeds(
|
|
tmp_path, monkeypatch
|
|
):
|
|
"""A failed launch-record write leaves no destination behind.
|
|
|
|
The overwrite path stages to a same-directory temp file and
|
|
publishes with ``os.replace``: a disk-full error must leave no
|
|
truncated record, and the retry records the binding.
|
|
"""
|
|
dest = (
|
|
tmp_path
|
|
/ f"{workflow.LAUNCH_RECORD_PREFIX}abc{workflow.LAUNCH_RECORD_SUFFIX}"
|
|
)
|
|
record = {"launch_id": "abc", "schema_version": 1}
|
|
_fail_next_fsync(monkeypatch)
|
|
with pytest.raises(OSError, match="simulated disk full"):
|
|
workflow._write_json_record(dest, record)
|
|
assert not dest.exists()
|
|
assert _leftover_tmps(tmp_path) == []
|
|
assert workflow._write_json_record(dest, record) == dest
|
|
assert json.loads(dest.read_text(encoding="utf-8")) == record
|
|
assert _leftover_tmps(tmp_path) == []
|
|
|
|
|
|
def test_write_run_binding_never_overwrites_existing(tmp_path):
|
|
"""An existing run-dir binding is preserved, never replaced."""
|
|
run_dir = tmp_path / "run-a"
|
|
run_dir.mkdir(parents=True)
|
|
dest = run_dir / workflow.LAUNCH_FILENAME
|
|
original = {"run_id": "run-a", "launch_id": "other"}
|
|
dest.write_text(json.dumps(original), encoding="utf-8")
|
|
with pytest.raises(RuntimeError, match="already holds gsq_launch.json"):
|
|
workflow._write_run_binding(run_dir, {"run_id": "run-a"})
|
|
assert json.loads(dest.read_text(encoding="utf-8")) == original
|
|
assert _leftover_tmps(run_dir) == []
|
|
|
|
|
|
def test_write_run_binding_link_race_never_overwrites(tmp_path, monkeypatch):
|
|
"""The exclusive publish refuses even when the pre-check misses.
|
|
|
|
The ``exists`` pre-check can lose a race with a concurrent bind, so
|
|
the ``os.link`` publish itself must refuse: hide the existing file
|
|
from the pre-check and the binding must still be preserved.
|
|
"""
|
|
run_dir = tmp_path / "run-a"
|
|
run_dir.mkdir(parents=True)
|
|
dest = run_dir / workflow.LAUNCH_FILENAME
|
|
original = {"run_id": "run-a", "launch_id": "other"}
|
|
dest.write_text(json.dumps(original), encoding="utf-8")
|
|
real_exists = Path.exists
|
|
state = {"calls": 0}
|
|
|
|
def _miss_once(self):
|
|
if self.name == workflow.LAUNCH_FILENAME and state["calls"] == 0:
|
|
state["calls"] += 1
|
|
return False
|
|
return real_exists(self)
|
|
|
|
monkeypatch.setattr(Path, "exists", _miss_once)
|
|
with pytest.raises(RuntimeError, match="already holds gsq_launch.json"):
|
|
workflow._write_run_binding(run_dir, {"run_id": "run-a"})
|
|
assert json.loads(dest.read_text(encoding="utf-8")) == original
|
|
assert _leftover_tmps(run_dir) == []
|
|
|
|
|
|
def test_write_manifest_atomic_on_partial_write(tmp_path, monkeypatch):
|
|
"""A failed manifest write leaves no file; a failed rewrite keeps the old one."""
|
|
run_dir = tmp_path / "run-a"
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(run_dir, model_dir)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
write_manifest_file=False,
|
|
)
|
|
assert result.passed
|
|
kwargs = {
|
|
"gsq_checkout": None,
|
|
"model": "tiny",
|
|
"model_revision": "rev",
|
|
"gsq_config_path": None,
|
|
"checkpoint_dir": run_dir,
|
|
"result": result,
|
|
"model_config_path": model_config,
|
|
"module_prefix": "model",
|
|
"quantize_attention": True,
|
|
"quantize_mlp": True,
|
|
"expected_count": None,
|
|
"group_size": gsq_coverage.DEFAULT_GROUP_SIZE,
|
|
"pack_bits": workflow.ASSEMBLER_PACK_BITS,
|
|
"base_model_dir": model_dir,
|
|
"expected_shapes": None,
|
|
}
|
|
dest = run_dir / workflow.MANIFEST_FILENAME
|
|
_fail_next_fsync(monkeypatch)
|
|
with pytest.raises(OSError, match="simulated disk full"):
|
|
workflow.write_manifest(dest, **kwargs)
|
|
assert not dest.exists()
|
|
assert _leftover_tmps(run_dir) == []
|
|
assert workflow.write_manifest(dest, **kwargs) == dest
|
|
manifest = json.loads(dest.read_text(encoding="utf-8"))
|
|
assert manifest["run_id"] == "run-a"
|
|
assert manifest["schema_version"] == 2
|
|
# A failed rewrite must keep the previously published manifest intact.
|
|
good_bytes = dest.read_bytes()
|
|
_fail_next_fsync(monkeypatch)
|
|
with pytest.raises(OSError, match="simulated disk full"):
|
|
workflow.write_manifest(dest, **kwargs)
|
|
assert dest.read_bytes() == good_bytes
|
|
assert json.loads(dest.read_text(encoding="utf-8"))["schema_version"] == 2
|
|
assert _leftover_tmps(run_dir) == []
|
|
|
|
|
|
def test_run_refuses_stale_new_run_dir(fake_tree, tmp_path, monkeypatch):
|
|
"""A launch that creates no dir cannot bind another launch's run.
|
|
|
|
When launch A fails before creating its run dir while another
|
|
launch's dir appears as the sole new dir, A must not bind B's
|
|
weights to A's config/base hashes. A dir predating the launch
|
|
start refuses on both the failed-launch and the success path, the
|
|
refused launch leaves no binding behind, and the lock releases.
|
|
"""
|
|
config_path = _write_run_config(tmp_path)
|
|
checkpoint = _checkpoint_from_config(config_path)
|
|
|
|
def _stale_run(exit_code):
|
|
def _fake_run(command, **kwargs):
|
|
cmd = list(command)
|
|
if len(cmd) >= 2 and cmd[1] == "main.py" and "--config" in cmd:
|
|
run_dir = checkpoint / f"mock-run-{exit_code:04d}"
|
|
run_dir.mkdir(parents=True, exist_ok=True)
|
|
(run_dir / "progress.json").write_text(
|
|
json.dumps({"last_completed_layer": 0}), encoding="utf-8"
|
|
)
|
|
# Backdate the dir: it belongs to another launch that
|
|
# created it before this launch started.
|
|
old = time.time() - 3600
|
|
os.utime(run_dir, (old, old))
|
|
os.utime(run_dir / "progress.json", (old, old))
|
|
return subprocess.CompletedProcess(command, exit_code)
|
|
|
|
return _fake_run
|
|
|
|
# Failed launch (exit 1): refuse instead of binding the stale dir
|
|
# with the failure status.
|
|
monkeypatch.setattr(subprocess, "run", _stale_run(1))
|
|
with pytest.raises(RuntimeError, match="older than the launch start"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert not (checkpoint / "mock-run-0001" / workflow.LAUNCH_FILENAME).exists()
|
|
|
|
# Successful launch (exit 0): the same stale dir refuses as well.
|
|
monkeypatch.setattr(subprocess, "run", _stale_run(0))
|
|
with pytest.raises(RuntimeError, match="older than the launch start"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert not (checkpoint / "mock-run-0000" / workflow.LAUNCH_FILENAME).exists()
|
|
assert not (checkpoint / workflow.LAUNCH_LOCK_FILENAME).exists()
|
|
|
|
|
|
def _write_sized_base(model_dir, hidden_size):
|
|
"""Write a minimal base model dir declaring ``hidden_size``."""
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps({"hidden_size": hidden_size}), encoding="utf-8"
|
|
)
|
|
return model_dir
|
|
|
|
|
|
def _write_huge_capture_config(
|
|
tmp_path, model_dir, name="gsq-huge.yaml", max_length=4096
|
|
):
|
|
"""Render a config whose train buffer is 2 TiB at bf16 widths.
|
|
|
|
32768 samples x 4096 tokens x hidden 8192 x 2 bytes is 2 TiB, at or
|
|
above the 1024 GiB mmap threshold, so the pinned allocator would
|
|
spill capture to disk-backed mmap.
|
|
"""
|
|
config_path = tmp_path / name
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir),
|
|
checkpoint_dir=str(tmp_path / "checkpoints"),
|
|
num_samples=32768,
|
|
val_samples=128,
|
|
gptq_nsamples=512,
|
|
max_length=max_length,
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
return config_path
|
|
|
|
|
|
def test_run_refuses_activation_capture_above_mmap_threshold(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A 2 TiB capture refuses before anything launches.
|
|
|
|
32768 samples x 4096 tokens x hidden 8192 at bf16 is 2 TiB per
|
|
train buffer, at or above the 1024 GiB threshold: ``run`` must
|
|
refuse instead of launching capture the pinned allocator would
|
|
spill to disk-backed mmap.
|
|
"""
|
|
model_dir = _write_sized_base(tmp_path / "model", 8192)
|
|
config_path = _write_huge_capture_config(tmp_path, model_dir)
|
|
calls: list = []
|
|
|
|
def _must_not_launch(command, **kwargs):
|
|
calls.append(list(command))
|
|
return subprocess.CompletedProcess(command, 0)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _must_not_launch)
|
|
with pytest.raises(ValueError, match="disk-backed"):
|
|
workflow.run_quantization(
|
|
fake_tree, config_path, allow_unrecorded_digest=True
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_activation_sizing_passes_small_capture_and_uses_text_config(tmp_path):
|
|
"""Small captures pass, with hidden_size from text_config when nested.
|
|
|
|
The top-level ``hidden_size`` is set absurdly large on purpose: the
|
|
check must use the nested ``text_config.hidden_size`` (as the pinned
|
|
trainer does), so the small capture passes.
|
|
"""
|
|
model_dir = tmp_path / "model"
|
|
model_dir.mkdir()
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"model_type": "qwen3_5",
|
|
"hidden_size": 2**31,
|
|
"text_config": {"hidden_size": 1024},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
config_path = tmp_path / "gsq-small.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir),
|
|
checkpoint_dir=str(tmp_path / "checkpoints"),
|
|
num_samples=8,
|
|
val_samples=2,
|
|
gptq_nsamples=4,
|
|
max_length=256,
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
# Largest buffer is 8 x 256 x 1024 bf16 (~4 MiB): must pass.
|
|
workflow._require_ram_resident_activation_buffers(
|
|
yaml.safe_load(config_path.read_text(encoding="utf-8")),
|
|
config_path=config_path,
|
|
base_model_dir=model_dir,
|
|
)
|
|
|
|
|
|
def test_validate_with_config_refuses_oversize_activation_capture(tmp_path):
|
|
"""``validate --gsq-config`` refuses capture that would spill to mmap."""
|
|
model_dir = _write_sized_base(tmp_path / "model", 8192)
|
|
config_path = _write_huge_capture_config(tmp_path, model_dir)
|
|
with pytest.raises(ValueError, match="disk-backed"):
|
|
workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=tmp_path / "run",
|
|
module_prefix="model",
|
|
gsq_config_path=config_path,
|
|
write_manifest_file=False,
|
|
)
|
|
|
|
|
|
def test_assemble_contract_refuses_oversize_activation_capture(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""The assemble contract check refuses capture that would spill to mmap.
|
|
|
|
Coverage passes on the tiny weights, but the declared hidden width
|
|
makes the requested capture 2 TiB: assembly must refuse before
|
|
invoking ``save_model.py``.
|
|
"""
|
|
run_dir = tmp_path / "checkpoints" / "run-a"
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(run_dir, model_dir)
|
|
(model_dir / "config.json").write_text(
|
|
json.dumps({"num_hidden_layers": 1, "hidden_size": 8192}),
|
|
encoding="utf-8",
|
|
)
|
|
huge = _write_huge_capture_config(tmp_path, model_dir)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
write_manifest_file=False,
|
|
)
|
|
assert result.passed
|
|
workflow.write_manifest(
|
|
run_dir / workflow.MANIFEST_FILENAME,
|
|
gsq_checkout=None,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=huge,
|
|
checkpoint_dir=run_dir,
|
|
result=result,
|
|
model_config_path=model_config,
|
|
module_prefix="model",
|
|
quantize_attention=True,
|
|
quantize_mlp=True,
|
|
expected_count=None,
|
|
group_size=gsq_coverage.DEFAULT_GROUP_SIZE,
|
|
pack_bits=gsq_coverage.DEFAULT_PACK_BITS,
|
|
base_model_dir=model_dir,
|
|
expected_shapes=None,
|
|
launch_gsq_revision=source.GSQ_REVISION,
|
|
launch_prepared_tree_sha256=source.EXPECTED_PREPARED_TREE_SHA256,
|
|
launch_prepared_tree_verified=True,
|
|
)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
with pytest.raises(ValueError, match="disk-backed"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
huge,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
_TEMPLATE_TOP_FILES = {
|
|
"chat_template.jinja": "hello {{ messages }}",
|
|
"chat_template.json": '{"chat_template": true}',
|
|
"added_tokens.json": '{"added_tokens": []}',
|
|
"processor_config.json": '{"processor": true}',
|
|
"video_preprocessor_config.json": '{"video": true}',
|
|
}
|
|
_TEMPLATE_SUB_FILES = {
|
|
"default.jinja": "default {{ messages }}",
|
|
"tool_call.jinja": "tool {{ messages }}",
|
|
}
|
|
|
|
|
|
def _write_template_files(base: Path) -> None:
|
|
for name, content in _TEMPLATE_TOP_FILES.items():
|
|
(base / name).write_text(content, encoding="utf-8")
|
|
sub = base / "chat_templates"
|
|
sub.mkdir(parents=True, exist_ok=True)
|
|
for name, content in _TEMPLATE_SUB_FILES.items():
|
|
(sub / name).write_text(content, encoding="utf-8")
|
|
|
|
|
|
def _copy_templates_to_out(base: Path, out: Path) -> None:
|
|
for name in _TEMPLATE_TOP_FILES:
|
|
src = base / name
|
|
if src.is_file():
|
|
(out / name).write_bytes(src.read_bytes())
|
|
sub_src = base / "chat_templates"
|
|
if sub_src.is_dir():
|
|
(out / "chat_templates").mkdir(parents=True, exist_ok=True)
|
|
for name in _TEMPLATE_SUB_FILES:
|
|
src = sub_src / name
|
|
if src.is_file():
|
|
(out / "chat_templates" / name).write_bytes(src.read_bytes())
|
|
|
|
|
|
def _validate_bound_run_with_templates(tmp_path, *, run_name="run-a"):
|
|
checkpoint_base = tmp_path / "checkpoints"
|
|
run_dir = checkpoint_base / run_name
|
|
model_dir = tmp_path / "model"
|
|
model_config, _ = _write_tiny_run(run_dir, model_dir)
|
|
_write_template_files(model_dir)
|
|
config_path = tmp_path / "gsq.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir), checkpoint_dir=str(checkpoint_base)
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
workflow.write_launch_binding(
|
|
run_dir, config_path=config_path, base_model_dir=model_dir
|
|
)
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_config,
|
|
checkpoint_dir=run_dir,
|
|
model="tiny",
|
|
model_revision="rev",
|
|
gsq_config_path=config_path,
|
|
)
|
|
assert result.passed
|
|
return run_dir, config_path, model_config, model_dir
|
|
|
|
|
|
def _chat_template_patch_entries():
|
|
entries = [
|
|
entry
|
|
for entry in source.TEXT_PATCHES
|
|
if entry[0] == "save_model.py"
|
|
and (
|
|
"chat_template.jinja" in entry[1] + entry[2]
|
|
or "chat_templates" in entry[1] + entry[2]
|
|
)
|
|
]
|
|
return entries
|
|
|
|
|
|
def test_chat_template_patches_have_exact_counts():
|
|
entries = _chat_template_patch_entries()
|
|
assert len(entries) == 2
|
|
for _relative, _old, _new, count in entries:
|
|
assert count == 1
|
|
patterns_entry = next(
|
|
entry for entry in entries if "chat_template.jinja" in entry[2]
|
|
)
|
|
_relative, old, new, _count = patterns_entry
|
|
for name in _TEMPLATE_TOP_FILES:
|
|
assert name in new
|
|
assert old in new
|
|
subdir_entry = next(
|
|
entry for entry in entries if "chat_templates_src" in entry[2]
|
|
)
|
|
_relative, _old, new, _count = subdir_entry
|
|
assert "chat_templates" in new
|
|
assert '.jinja' in new
|
|
assert "os.makedirs" in new
|
|
|
|
|
|
def test_prepare_applies_chat_template_patches_and_is_idempotent(fake_tree):
|
|
changed = source.prepare(fake_tree)
|
|
assert "save_model.py" in changed
|
|
text = (fake_tree / "save_model.py").read_text(encoding="utf-8")
|
|
for name in _TEMPLATE_TOP_FILES:
|
|
assert name in text
|
|
assert "chat_templates_src" in text
|
|
assert "chat_templates_dst" in text
|
|
assert source.prepare(fake_tree) == []
|
|
prospective = source.prospective_prepared_digest(fake_tree)
|
|
assert prospective == source.tree_digest(fake_tree)
|
|
|
|
|
|
def test_base_hashes_bind_template_files(tmp_path):
|
|
base = tmp_path / "model"
|
|
base.mkdir()
|
|
(base / "config.json").write_text(
|
|
json.dumps({"num_hidden_layers": 1}), encoding="utf-8"
|
|
)
|
|
_write_template_files(base)
|
|
(base / "chat_templates" / "notes.txt").write_text(
|
|
"not a template", encoding="utf-8"
|
|
)
|
|
hashes = workflow._base_model_file_hashes(base)
|
|
for name, content in _TEMPLATE_TOP_FILES.items():
|
|
assert name in hashes
|
|
assert hashes[name] == workflow._sha256_file(base / name)
|
|
for name in _TEMPLATE_SUB_FILES:
|
|
key = f"chat_templates/{name}"
|
|
assert key in hashes
|
|
assert hashes[key] == workflow._sha256_file(base / key)
|
|
assert "chat_templates/notes.txt" not in hashes
|
|
assert set(hashes) == set(workflow._base_model_file_hashes(base))
|
|
|
|
|
|
def test_base_hash_binding_agrees_with_coverage_template_set():
|
|
assert gsq_coverage._TEMPLATE_NON_WEIGHT_FILENAMES == frozenset(
|
|
_TEMPLATE_TOP_FILES
|
|
)
|
|
assert gsq_coverage.CHAT_TEMPLATES_SUBDIR == "chat_templates"
|
|
assert gsq_coverage._TEMPLATE_NON_WEIGHT_FILENAMES <= (
|
|
workflow._BASE_NON_WEIGHT_FILENAMES
|
|
)
|
|
|
|
|
|
def test_assemble_refuses_template_drift_before_launch(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
run, config_path, _model_config, model_dir = (
|
|
_validate_bound_run_with_templates(tmp_path)
|
|
)
|
|
manifest = json.loads(
|
|
(run / workflow.MANIFEST_FILENAME).read_text(encoding="utf-8")
|
|
)
|
|
assert "chat_template.jinja" in manifest["base_model_sha256"]
|
|
assert (
|
|
"chat_templates/default.jinja" in manifest["base_model_sha256"]
|
|
)
|
|
calls = _mock_subprocess(monkeypatch)
|
|
(model_dir / "chat_template.jinja").write_text(
|
|
"edited {{ messages }}", encoding="utf-8"
|
|
)
|
|
with pytest.raises(ValueError, match="base model"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
|
|
|
|
def test_assemble_refuses_template_drift_during_assembly(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
run, config_path, _model_config, model_dir = (
|
|
_validate_bound_run_with_templates(tmp_path)
|
|
)
|
|
|
|
def _mutating_writer(out):
|
|
# Mutate the base first, then copy the mutated bytes to the
|
|
# output: the output verifier sees matching (new) bytes, so
|
|
# only the post-assembly input re-verification catches the
|
|
# drift against the manifest's recorded (old) hashes.
|
|
(model_dir / "chat_template.jinja").write_text(
|
|
"edited during assembly", encoding="utf-8"
|
|
)
|
|
_write_minimal_assembled(out)
|
|
_copy_templates_to_out(model_dir, out)
|
|
|
|
calls = _mock_subprocess_with_assembled(
|
|
monkeypatch, writer=_mutating_writer
|
|
)
|
|
with pytest.raises(ValueError, match="during assembly"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_rejects_missing_template_in_output(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
_run, config_path, _model_config, _model_dir = (
|
|
_validate_bound_run_with_templates(tmp_path)
|
|
)
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
with pytest.raises(ValueError, match="template"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_rejects_altered_template_in_output(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
_run, config_path, _model_config, model_dir = (
|
|
_validate_bound_run_with_templates(tmp_path)
|
|
)
|
|
|
|
def _altered_writer(out):
|
|
_write_minimal_assembled(out)
|
|
_copy_templates_to_out(model_dir, out)
|
|
(out / "chat_template.jinja").write_text(
|
|
"altered output", encoding="utf-8"
|
|
)
|
|
|
|
calls = _mock_subprocess_with_assembled(
|
|
monkeypatch, writer=_altered_writer
|
|
)
|
|
with pytest.raises(ValueError, match="template"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_rejects_missing_subdir_template_in_output(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
_run, config_path, _model_config, model_dir = (
|
|
_validate_bound_run_with_templates(tmp_path)
|
|
)
|
|
|
|
def _missing_subdir_writer(out):
|
|
_write_minimal_assembled(out)
|
|
for name in _TEMPLATE_TOP_FILES:
|
|
(out / name).write_bytes((model_dir / name).read_bytes())
|
|
|
|
calls = _mock_subprocess_with_assembled(
|
|
monkeypatch, writer=_missing_subdir_writer
|
|
)
|
|
with pytest.raises(ValueError, match="template"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_preserves_matching_templates_in_output(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
_run, config_path, _model_config, model_dir = (
|
|
_validate_bound_run_with_templates(tmp_path)
|
|
)
|
|
|
|
def _preserving_writer(out):
|
|
_write_minimal_assembled(out)
|
|
_copy_templates_to_out(model_dir, out)
|
|
|
|
calls = _mock_subprocess_with_assembled(
|
|
monkeypatch, writer=_preserving_writer
|
|
)
|
|
assert (
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
== 0
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def _drift_probe_text(path: Path) -> None:
|
|
"""Append a comment to a checkout file, moving the prepared digest."""
|
|
path.write_text(
|
|
path.read_text(encoding="utf-8") + "\n# drift probe\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
|
|
def test_assemble_refuses_when_source_moves_before_spawn(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A save_model.py edit during revalidation refuses without spawning.
|
|
|
|
The manifest revalidation inside ``assemble`` takes a while on real
|
|
checkpoints: the hook below edits ``save_model.py`` while it runs,
|
|
so the pre-spawn source recheck must refuse (raising, without
|
|
spawning the child) and leave no output dir behind.
|
|
"""
|
|
_run, config_path, _ = _validate_bound_run(tmp_path)
|
|
real_revalidate = workflow._revalidate_before_assemble
|
|
|
|
def _mutating_revalidate(run_dir, config_abs, manifest):
|
|
_drift_probe_text(fake_tree / "save_model.py")
|
|
return real_revalidate(run_dir, config_abs, manifest)
|
|
|
|
monkeypatch.setattr(
|
|
workflow, "_revalidate_before_assemble", _mutating_revalidate
|
|
)
|
|
calls = _mock_subprocess_with_assembled(monkeypatch)
|
|
out = tmp_path / "out"
|
|
with pytest.raises(RuntimeError, match="training source"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
out_dir=out,
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert calls == []
|
|
assert not out.exists()
|
|
|
|
|
|
def test_assemble_fails_when_source_moves_during_save_model(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A save_model.py edit while assembling fails instead of certifying.
|
|
|
|
The fake assembler mutates the training source mid-run (as an edit
|
|
landing during a long assembly would), then writes valid output: the
|
|
post-exit source recheck -- not missing output -- must fail closed
|
|
instead of reporting success.
|
|
"""
|
|
_run, config_path, _ = _validate_bound_run(tmp_path)
|
|
|
|
def _mutating_writer(out):
|
|
_drift_probe_text(fake_tree / "save_model.py")
|
|
_write_minimal_assembled(out)
|
|
|
|
calls = _mock_subprocess_with_assembled(
|
|
monkeypatch, writer=_mutating_writer
|
|
)
|
|
with pytest.raises(RuntimeError, match="training source"):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_assemble_returns_exit_code_when_source_moves_and_child_fails(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""A failing save_model.py still reports its exit code with drift.
|
|
|
|
As in :func:`run_quantization`, a non-zero exit already fails, so
|
|
the post-exit source recheck does not mask it: the exit code is
|
|
returned (never success) even when the source moved mid-run.
|
|
"""
|
|
_run, config_path, _ = _validate_bound_run(tmp_path)
|
|
|
|
def _failing_run(command, **kwargs):
|
|
_drift_probe_text(fake_tree / "save_model.py")
|
|
return subprocess.CompletedProcess(command, 1)
|
|
|
|
monkeypatch.setattr(subprocess, "run", _failing_run)
|
|
assert (
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|
|
== 1
|
|
)
|
|
|
|
|
|
def test_assemble_interruption_propagates_when_source_moves(
|
|
fake_tree, tmp_path, monkeypatch
|
|
):
|
|
"""Ctrl-C during save_model.py still propagates with drift.
|
|
|
|
As in :func:`run_quantization`, an interruption certifies nothing:
|
|
the original ``KeyboardInterrupt`` propagates (not success) even
|
|
when the source moved mid-run.
|
|
"""
|
|
_run, config_path, _ = _validate_bound_run(tmp_path)
|
|
|
|
def _interrupting_run(command, **kwargs):
|
|
_drift_probe_text(fake_tree / "save_model.py")
|
|
raise KeyboardInterrupt("ctrl-c probe")
|
|
|
|
monkeypatch.setattr(subprocess, "run", _interrupting_run)
|
|
with pytest.raises(KeyboardInterrupt):
|
|
workflow.assemble_checkpoint(
|
|
fake_tree,
|
|
config_path,
|
|
run_id="run-a",
|
|
python="py",
|
|
allow_unrecorded_digest=True,
|
|
)
|