mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Integrate the weight fingerprint from #451 with CUDA execution: - model_identity is now device-aware (require_dense_device) and always includes the weight fingerprint, so CUDA runs carry it too; execution_identity simply delegates. The fingerprint is device-independent: the same weights give the same digest on CPU and CUDA. - New optional spec setting model.fingerprint ("sampled" default, "full"), dropped from the filled spec at its default so existing digests are unchanged. It applies to the run's model identity and therefore every step's reuse check; capture manifests keep a sampled fingerprint. - Resolve additive conflicts in capture_identity.py, inputs.py, the risk map and OBSERVATION_RUNS.md by keeping both sides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
4.7 KiB
Python
98 lines
4.7 KiB
Python
"""CUDA observation runs against the CPU reference on a tiny local model.
|
|
|
|
Random local weights establish software contracts only (device placement,
|
|
per-example greedy agreement, capture identity), never pretrained behaviour.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis.activation_capture import load_capture
|
|
from obliteratus.analysis.capture_identity import capture_identity
|
|
from obliteratus.experiments import (
|
|
InterventionStateRefused, compare_results, load_spec, run_observation,
|
|
)
|
|
from obliteratus.experiments import generation, inputs
|
|
from tests.test_observation_execution import generations, varied_pack
|
|
from tests.test_observation_runs import CharTokenizer, factory, make_spec, tiny_gpt2
|
|
|
|
pytestmark = pytest.mark.gpu
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def require_cuda():
|
|
if not torch.cuda.is_available():
|
|
pytest.skip("requires a CUDA runner; set ENABLE_CUDA_GATE and attach the cuda label")
|
|
|
|
|
|
def cuda_factory():
|
|
def make(section):
|
|
return factory(model=tiny_gpt2().to(section["device"]))(section)
|
|
return make
|
|
|
|
|
|
def test_cuda_greedy_matches_cpu_and_capture_loads_back(tmp_path):
|
|
pack = varied_pack(tmp_path / "pack")
|
|
decoding = {"max_new_tokens": 8}
|
|
cpu = run_observation(make_spec(pack, profile=False, decoding=decoding), tmp_path / "cpu",
|
|
model_factory=factory())
|
|
cuda = run_observation(make_spec(pack, profile=False, decoding=decoding,
|
|
model={"device": "cuda"}),
|
|
tmp_path / "cuda", model_factory=cuda_factory())
|
|
assert cuda["execution"]["device"] == "cuda:0" and cuda["inputs"]["model"]["device"] == "cuda:0"
|
|
# The weight fingerprint is device-independent: same weights, same digest.
|
|
assert cuda["inputs"]["model"]["weights"] == cpu["inputs"]["model"]["weights"]
|
|
report = compare_results(cpu, cuda)
|
|
assert report["outcomes"]["compared_exactly"] is True
|
|
# Identities differ by device; per-example outcomes and activations agree.
|
|
assert set(report["differences"]) <= {"spec_digest", "execution", "inputs.model",
|
|
"steps.*.identity_sha256", "capture.identity_sha256"}
|
|
for task in ("answers", "aliases"):
|
|
assert generations(cpu, task)["rows"] == generations(cuda, task)["rows"]
|
|
|
|
batched = run_observation(make_spec(pack, profile=False, model={"device": "cuda"},
|
|
decoding={**decoding, "batch_size": 3}),
|
|
tmp_path / "batched", model_factory=cuda_factory())
|
|
assert set(compare_results(cuda, batched)["differences"]) <= {
|
|
"spec_digest", "execution", "steps.*.identity_sha256"}
|
|
|
|
root = Path(cuda["location"]["run_dir"]) / cuda["capture"]["path"]
|
|
manifest = json.loads((root / "manifest.json").read_text())
|
|
assert manifest["execution"] == {"device": "cuda:0", "dtype": "float32"}
|
|
assert manifest["identity"]["device"] == "cuda:0"
|
|
model = tiny_gpt2().cuda()
|
|
spec = load_spec(make_spec(pack, model={"device": "cuda"}))
|
|
dataset, tasks, _ = inputs.resolve_dataset(spec, None)
|
|
settings, seen, batches = generation.prompt_settings(spec), set(), []
|
|
for task in tasks:
|
|
prompts = generation.render_prompts(CharTokenizer(), task.examples, settings)
|
|
for example in task.examples:
|
|
if example.example_id not in seen:
|
|
seen.add(example.example_id)
|
|
batches.append(generation.encode_prompt(CharTokenizer(), prompts[example.example_id],
|
|
settings))
|
|
expected = capture_identity(model, CharTokenizer(), inputs.capture_sites(model, [0, 1]), batches,
|
|
model_id="local/tiny-gpt2", phase="prompt", dataset=dataset)
|
|
loaded = load_capture(root, expected)
|
|
assert len(loaded.shards) == 3
|
|
assert all(value.device.type == "cpu" for shard in loaded.shards for value in shard["values"].values())
|
|
|
|
|
|
def test_cuda_sampling_reproduces_and_hooks_are_refused(tmp_path):
|
|
pack = varied_pack(tmp_path / "pack")
|
|
spec = make_spec(pack, profile=False, capture=None, model={"device": "cuda"},
|
|
decoding={"strategy": "sample", "max_new_tokens": 6}, seed=5)
|
|
first = run_observation(spec, tmp_path / "a", model_factory=cuda_factory())
|
|
again = run_observation(spec, tmp_path / "b", model_factory=cuda_factory())
|
|
assert compare_results(first, again)["equal"] is True
|
|
|
|
model = tiny_gpt2().cuda()
|
|
model.transformer.h[0].register_forward_hook(lambda *a: None)
|
|
with pytest.raises(InterventionStateRefused, match="forward_hooks"):
|
|
run_observation(spec, tmp_path / "hooked", model_factory=factory(model=model))
|