Files
OBLITERATUS/tests/conditional/test_analysis_first_cuda.py
Joseph MaglyandClaude Opus 5.5 bbb0177915 feat(analysis): fill baseline profile work counters
Baseline profiles reported unique examples, token presentations,
forwards, generated tokens and tensor bytes as null. Count them from
load completion through cleanup with pass-through observers: a forward
pre-hook on the loaded model and instance-level wrappers on generate and
activation collection, all removed on exit and after failures.

Counters are recorded per stage and totalled. A counter that cannot be
observed stays null with a reason (stages before load, calls without an
attention mask or input ids, batched generation, forwards outside
activation collection, or embedding calls that bypass the watched
model). Changes are additive within profile schema version 1; default
baseline artifacts are unchanged, profiled or not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:35:46 -05:00

245 lines
12 KiB
Python

"""Manual, offline CUDA proof for analysis-first diagnostics and profiling.
Select explicitly with OBLITERATUS_ANALYSIS_GPU_PROOF=1. Random local weights
establish software contracts only, never pretrained-model efficacy.
"""
from dataclasses import asdict
import hashlib
import json
import os
from pathlib import Path
import time
import pytest
import torch
from examples.analysis_first_baseline import make_fixture
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.analysis.riemannian_manifold import RiemannianManifoldAnalyzer
from obliteratus.concepts.baseline import run_baseline
from obliteratus.concepts.direction import ConceptDirection
pytestmark = pytest.mark.gpu
DTYPES = {"float32": torch.float32, "float16": torch.float16, "bfloat16": torch.bfloat16}
@pytest.fixture(scope="module", autouse=True)
def explicit_cuda_proof():
if os.environ.get("OBLITERATUS_ANALYSIS_GPU_PROOF") != "1":
pytest.skip("manual proof requires OBLITERATUS_ANALYSIS_GPU_PROOF=1")
assert torch.cuda.is_available(), "Selected hardware proof cannot fall back to CPU"
assert torch.cuda.is_bf16_supported(including_emulation=False)
total = torch.cuda.get_device_properties(0).total_memory
previous_fraction = torch.cuda.get_per_process_memory_fraction(0)
torch.cuda.set_per_process_memory_fraction(min(previous_fraction, 4 * 1024**3 / total), 0)
previous = torch.get_num_threads()
torch.set_num_threads(1)
try:
yield
finally:
torch.set_num_threads(previous)
torch.cuda.empty_cache()
torch.cuda.set_per_process_memory_fraction(previous_fraction, 0)
def assert_report_equal(actual, expected):
if isinstance(expected, dict):
assert actual.keys() == expected.keys()
for key in expected:
assert_report_equal(actual[key], expected[key])
elif isinstance(expected, float):
assert actual == pytest.approx(expected, rel=1e-8, abs=1e-10)
else:
assert actual == expected
@pytest.mark.parametrize("dtype_name", DTYPES)
@pytest.mark.parametrize("cloud,rank", [("sphere", 2), ("plane", 2), ("line", 1), ("constant", 0)])
def test_cuda_geometry_coordinate_oracles_and_cpu_parity(dtype_name, cloud, rank):
points = {
"sphere": torch.eye(3) * 2,
"plane": torch.tensor([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], [1., 1., 0.]]),
"line": torch.tensor([[0., 0., 0.], [1., 0., 0.], [2., 0., 0.]]),
"constant": torch.zeros(4, 3),
}[cloud].to(DTYPES[dtype_name])
analyzer = RiemannianManifoldAnalyzer(n_sample_points=8, n_geodesic_steps=4)
expected = analyzer.analyze({0: points}, {0: torch.zeros_like(points)})
gpu = points.to("cuda:0")
before = gpu.clone()
rng_before = torch.cuda.get_rng_state(0)
actual = analyzer.analyze({0: gpu}, {0: torch.zeros_like(gpu)})
torch.cuda.synchronize(0)
assert torch.equal(torch.cuda.get_rng_state(0), rng_before)
assert torch.equal(gpu, before)
assert actual.intrinsic_dimension == rank
assert actual.mean_sectional_curvature is None
assert actual.is_approximately_flat is None
assert actual.recommendation == "unsupported"
assert_report_equal(asdict(actual), asdict(expected))
if cloud in {"line", "constant"}:
assert actual.layer_chord_angle_excess_per_area == {0: None}
else:
assert abs(actual.layer_chord_angle_excess_per_area[0]) < 1e-10
if cloud == "constant":
assert actual.dimension_ratio == 0
assert actual.measurement_evidence["intrinsic_dimension"].status == "diagnostic"
@pytest.mark.parametrize("dtype_name", DTYPES)
def test_cuda_projection_is_unchanged_noop_with_explicit_unsupported_fields(dtype_name):
x = torch.tensor([3., 4., 0.], dtype=DTYPES[dtype_name], device="cuda:0")
direction = torch.tensor([2., 0., 0.], dtype=x.dtype, device=x.device)
before = x.clone()
analyzer = RiemannianManifoldAnalyzer()
result = analyzer.compute_geodesic_projection(x, direction, x.unsqueeze(0))
assert result.original_refusal_component == 3
assert result.linear_residual == 0
assert result.geodesic_residual is result.improvement_factor is result.effective_curvature is None
assert result.correction_vector.device == x.device
assert result.correction_vector.dtype == x.dtype
assert torch.equal(result.correction_vector, torch.zeros_like(x))
assert torch.equal(x, before)
assert result.measurement_evidence["correction_vector"].status == "unsupported"
missing = analyzer.compute_geodesic_projection(x, torch.zeros_like(direction), x.unsqueeze(0))
assert missing.original_refusal_component is missing.linear_residual is None
assert missing.measurement_evidence["linear_residual"].status == "unavailable"
def file_hashes(directory):
return {str(p.relative_to(directory)): hashlib.sha256(p.read_bytes()).hexdigest()
for p in sorted(directory.rglob("*")) if p.is_file()}
@pytest.mark.parametrize("dtype_name", DTYPES)
def test_cuda_sparse_and_nonfinite_inputs_remain_explicit(dtype_name):
analyzer = RiemannianManifoldAnalyzer(n_sample_points=8, n_geodesic_steps=4)
points = torch.eye(3, device="cuda:0", dtype=DTYPES[dtype_name])
for count in (0, 1, 2):
result = analyzer.analyze({0: points[:count]}, {0: torch.zeros_like(points[:count])})
assert result.layer_chord_angle_excess_per_area == {0: None}
assert result.mean_sectional_curvature is None
assert result.recommendation == "unsupported"
if count < 2:
assert result.intrinsic_dimension is None
assert result.measurement_evidence["intrinsic_dimension"].status == "unavailable"
for invalid in (float("nan"), float("inf")):
bad = points.clone()
bad[0, 0] = invalid
with pytest.raises(ValueError, match="finite"):
analyzer.analyze({0: bad}, {0: torch.zeros_like(bad)})
def forbid_mutation(*args, **kwargs):
pytest.fail("Observational baseline attempted mutation or checkpoint publication")
@pytest.mark.parametrize("dtype_name", DTYPES)
def test_cuda_baseline_profile_toggle_preserves_outputs_and_weights(tmp_path, monkeypatch, dtype_name):
make_fixture(tmp_path)
source_hashes = file_hashes(tmp_path / "model")
summon, cleanup = AbliterationPipeline._summon, AbliterationPipeline.cleanup_failed_run
observed = {"forwards": 0, "cleanups": 0}
def observe_load(pipeline):
summon(pipeline)
model = pipeline.handle.model
assert all(p.device == torch.device("cuda:0") for p in model.parameters())
assert all(p.dtype == DTYPES[dtype_name] for p in model.parameters())
pipeline._proof_weights = {k: v.detach().cpu().clone() for k, v in model.state_dict().items()}
def forward_hook(module, args, output):
assert output.logits.is_cuda
observed["forwards"] += 1
pipeline._proof_hook = model.register_forward_hook(forward_hook)
def observe_cleanup(pipeline):
assert all(torch.equal(value, pipeline.handle.model.state_dict()[key].cpu())
for key, value in pipeline._proof_weights.items())
pipeline._proof_hook.remove()
cleanup(pipeline)
observed["cleanups"] += 1
monkeypatch.setattr(AbliterationPipeline, "_summon", observe_load)
monkeypatch.setattr(AbliterationPipeline, "cleanup_failed_run", observe_cleanup)
monkeypatch.setattr(AbliterationPipeline, "_excise", forbid_mutation)
monkeypatch.setattr(AbliterationPipeline, "_rebirth", forbid_mutation)
reference = None
runs = []
for index, profile in enumerate([False, True, False, True]):
output = tmp_path / f"run-{index}"
prior_forwards = observed["forwards"]
torch.cuda.synchronize(0)
torch.cuda.reset_peak_memory_stats(0)
started = time.perf_counter()
run_baseline(tmp_path / "pack", tmp_path / "model", output,
seed=42, device="cuda:0", dtype=dtype_name, max_seq_length=8,
extraction_layer=0, cache_dir=tmp_path / "cache", profile=profile)
torch.cuda.synchronize(0)
elapsed = time.perf_counter() - started
assert observed["forwards"] > prior_forwards
directions = ConceptDirection.load(output / "direction.safetensors")
validation = json.loads((output / "validation.json").read_text())
numerical = {"directions": {str(k): v.tolist() for k, v in directions.directions.items()},
"metrics": validation["metrics"], "verdict": validation["verdict"]}
if reference is None:
reference = numerical
assert numerical == reference
expected = {"direction.json", "direction.safetensors", "validation.json"}
if profile:
expected.add("cost-profile.json")
report = json.loads((output / "cost-profile.json").read_text())
assert report["status"] == "success" and report["schema_version"] == 1
assert report["counters"]["forwards"] == observed["forwards"] - prior_forwards
assert report["counters"]["generated_tokens"] == 0
assert set(report["counter_reasons"].values()) == {None}
assert report["gpu_seconds"] is report["dollar_cost"] is report["energy_joules"] is None
assert "unsynchronized" in report["timing_scope"]
assert all(stage["status"] == "success" for stage in report["stages"])
assert report["stages"][-1]["name"] == "cleanup"
assert report["wall_seconds"] >= sum(s["wall_seconds"] for s in report["stages"])
assert {p.name for p in output.iterdir()} == expected
assert file_hashes(tmp_path / "model") == source_hashes
runs.append({"profile": profile, "synchronized_workflow_wall_seconds": elapsed,
"peak_allocated_bytes": torch.cuda.max_memory_allocated(0),
"peak_reserved_bytes": torch.cuda.max_memory_reserved(0),
"actual_cuda_forwards": observed["forwards"] - prior_forwards})
assert observed["cleanups"] == 4
evidence = {"dtype": dtype_name, "runs": runs, "numerical_outputs_equal": True,
"weights_unchanged": True, "model_file_sha256": source_hashes,
"validation_verdict": reference["verdict"],
"scope": "one-layer width-16 random GPT-2, 16 rows, length 8; no efficacy claim"}
destination = Path(os.environ.get("OBLITERATUS_ANALYSIS_GPU_EVIDENCE_DIR", tmp_path))
destination.mkdir(parents=True, exist_ok=True)
(destination / f"baseline-{dtype_name}.json").write_text(json.dumps(evidence, indent=2, allow_nan=False) + "\n")
def test_cuda_probe_failure_still_records_profile_and_releases_model(tmp_path, monkeypatch):
make_fixture(tmp_path)
original_probe = AbliterationPipeline._probe
observed = []
def fail_after_cuda_probe(pipeline):
original_probe(pipeline)
assert next(pipeline.handle.model.parameters()).is_cuda
observed.append(True)
raise RuntimeError("controlled GPU probe failure")
monkeypatch.setattr(AbliterationPipeline, "_probe", fail_after_cuda_probe)
monkeypatch.setattr(AbliterationPipeline, "_excise", forbid_mutation)
monkeypatch.setattr(AbliterationPipeline, "_rebirth", forbid_mutation)
torch.cuda.empty_cache()
before = torch.cuda.memory_allocated(0)
with pytest.raises(RuntimeError, match="controlled GPU probe failure"):
run_baseline(tmp_path / "pack", tmp_path / "model", tmp_path / "failed",
device="cuda:0", max_seq_length=8, extraction_layer=0,
cache_dir=tmp_path / "cache", profile=True)
report = json.loads((tmp_path / "failed/cost-profile.json").read_text())
assert observed == [True]
assert report["status"] == "failure" and report["error_type"] == "RuntimeError"
assert [(s["name"], s["status"]) for s in report["stages"]][-2:] == [
("probe", "failure"), ("cleanup", "success")]
torch.cuda.synchronize(0)
assert torch.cuda.memory_allocated(0) <= before + 1024**2