"""Exercise the lab with local random-init Llama weights and a real tokenizer.""" from copy import deepcopy import json from pathlib import Path import threading from types import SimpleNamespace import pytest import torch from transformers import LlamaConfig, LlamaForCausalLM from obliteratus import ui_neuron_lab as lab from obliteratus.interventions import NeuronEdit, NeuronRecipe from tests.fixtures.tiny_offline_model import FIXTURE_VOCAB, _fixture_tokenizer pytestmark = [pytest.mark.cpu, pytest.mark.integration] @pytest.fixture(scope="module") def local_model(tmp_path_factory): destination = tmp_path_factory.mktemp("neuron-model") with torch.random.fork_rng(): torch.manual_seed(5249) model = LlamaForCausalLM(LlamaConfig( vocab_size=len(FIXTURE_VOCAB), hidden_size=16, intermediate_size=24, num_hidden_layers=2, num_attention_heads=2, num_key_value_heads=2, max_position_embeddings=1024, pad_token_id=0, eos_token_id=1, bos_token_id=1, )) model.save_pretrained(destination) _fixture_tokenizer().save_pretrained(destination) return destination @pytest.fixture(autouse=True, scope="module") def bounded_threads(): previous = torch.get_num_threads() torch.set_num_threads(1) yield torch.set_num_threads(previous) @pytest.fixture def session(local_model): session = lab.NeuronLabSession() session.load(str(local_model)) yield session session.close() def hooks(model): return {name: (len(module._forward_pre_hooks), len(module._forward_hooks)) for name, module in model.named_modules()} def test_loader_policy_geometry_and_close(local_model, monkeypatch): actual = lab.load_model calls = [] def loader(*args, **kwargs): calls.append((args, kwargs)) return actual(*args, **kwargs) monkeypatch.setattr(lab, "load_model", loader) session = lab.NeuronLabSession() directory = session.directory session.load(str(local_model)) assert calls[0][1] == { "device": "cpu", "dtype": "float32", "trust_remote_code": False, "skip_snapshot": True, "phase_admission": True, "local_files_only": True, "revision": None, "config_validator": lab._supported_config, } assert [(entry["layer"], entry["neurons"]) for entry in session.layers] == [(0, 24), (1, 24)] assert session.recipe.model_id == str(local_model) assert session.handle._restore_point is None assert not session.handle.model.training session.close() session.close() assert not directory.exists() and session.handle is None with pytest.raises(ValueError, match="expired"): session.load(str(local_model)) def test_inspector_matches_real_down_projection_input(session): model = session.handle.model before = hooks(model) weights = deepcopy(model.state_dict()) session.inspect("hello world safe", 1, 3) captured = [] hook = model.model.layers[1].mlp.down_proj.register_forward_pre_hook( lambda module, args: captured.append(args[0].detach().clone()), ) try: with torch.inference_mode(): model(**session._inputs("hello world safe"), use_cache=False) finally: hook.remove() expected = captured[0][0, :, 3].float() report = session.inspection assert report["tokens"] == [[i, token, value] for i, (token, value) in enumerate( zip(["hello", "world", "safe"], expected.tolist()))] assert report["statistics"]["mean"] == pytest.approx(expected.mean().item()) assert report["statistics"]["std"] == pytest.approx(expected.std(unbiased=False).item()) assert report["statistics"]["down_column_norm"] > 0 assert hooks(model) == before assert all(torch.equal(weights[key], value) for key, value in model.state_dict().items()) def test_identity_recipe_and_reset_preserve_baseline(session): model = session.handle.model initial_hooks = hooks(model) session.edit("add", 1, 0, 3, "add", 0.) session.edit("add", 1, 1, 2, "scale", 1.) session.compare("hello world safe", 4) first = session.comparison["trials"] assert first["baseline"]["token_ids"] == first["recipe"]["token_ids"] assert first["recipe"]["first_token_kl_from_baseline"] == 0 baseline_ids = first["baseline"]["token_ids"] session.edit("replace", 1, 0, 3, "set", 25.) session.compare("hello world safe", 4) assert session.comparison["trials"]["recipe"]["first_token_kl_from_baseline"] > 0 session.reset() assert session.recipe.edits == () and session.inspection is session.comparison is None session.compare("hello world safe", 4) assert session.comparison["trials"]["baseline"]["token_ids"] == baseline_ids assert session.comparison["trials"]["recipe"]["token_ids"] == baseline_ids assert hooks(model) == initial_hooks def test_generated_scope_preserves_first_prediction_and_never_uses_cache(session, monkeypatch): session.edit("add", 1, 0, 3, "set", 100., scope="generated_tokens") original = session.handle.model.forward forwards = [] def forward(*args, **kwargs): forwards.append((kwargs["input_ids"].shape[-1], kwargs["use_cache"], kwargs.get("past_key_values"))) return original(*args, **kwargs) monkeypatch.setattr(session.handle.model, "forward", forward) session.compare("hello world", 3) trials = session.comparison["trials"] assert trials["recipe"]["first_token_kl_from_baseline"] == 0 assert trials["baseline"]["token_ids"][0] == trials["recipe"]["token_ids"][0] assert all(cache is False and past is None for _, cache, past in forwards) assert forwards[0][0] == 2 assert sum(length == 2 for length, _, _ in forwards) == 2 assert session.comparison["decoding"]["chat_template_applied"] is False def test_chat_template_is_identical_across_conditions_and_recorded(session, monkeypatch): tokenizer = session.handle.tokenizer tokenizer.chat_template = ( "hello {{ messages[0]['content'] }} " "{% if add_generation_prompt %}answer{% endif %}" "{% if enable_thinking %} harmful{% endif %}" ) original = session.handle.model.forward prompts = [] def forward(*args, **kwargs): prompts.append(kwargs["input_ids"][0].tolist()) return original(*args, **kwargs) monkeypatch.setattr(session.handle.model, "forward", forward) session.compare("world", 1) assert prompts == [[7, 8, 6], [7, 8, 6]] decoding = session.comparison["decoding"] assert decoding["format"] == "single_user_chat" assert decoding["chat_template_applied"] is True assert len(decoding["chat_template_sha256"]) == 64 assert decoding["template_options"] == {"add_generation_prompt": True, "enable_thinking": False} def test_ordered_editor_moves_disables_and_validates_before_commit(session): session.edit("add", 1, 0, 2, "add", 1.) session.edit("add", 1, 0, 2, "scale", 2., enabled=False) assert [row[4] for row in session.rows()] == ["add", "scale"] session.edit("up", 2) assert [row[4] for row in session.rows()] == ["scale", "add"] assert session.rows()[0][1] is False session.edit("down", 1) session.edit("replace", 2, 1, 4, "clamp", lower=-2., upper=2., scope="prompt_tokens") assert session.recipe.edits[1] == NeuronEdit(1, 4, "clamp", lower=-2., upper=2.) before = session.recipe with pytest.raises(ValueError): session.edit("add", 1, 1, 999, "zero") assert session.recipe is before with pytest.raises(ValueError, match="edge"): session.edit("up", 1) with pytest.raises(ValueError, match="range"): session.edit("delete", 3) session.edit("delete", 1, scope="prompt_tokens") assert len(session.recipe.edits) == 1 and session.recipe.scope == "prompt_tokens" def test_saved_recipe_control_and_experiment_round_trip(session, tmp_path): session.edit("add", 1, 0, 2, "zero") session.inspect("hello world", 0, 2) session.export_recipe() original = session.recipe copied = tmp_path / "recipe.json" copied.write_text(Path(session.recipe_file).read_text()) session.reset() session.import_recipe(copied) assert session.recipe == original session.import_recipe(copied, control=True) session.inspect("hello world", 0, 2) session.compare("hello world", 3, True) trials = session.comparison["trials"] assert trials["control"]["token_ids"] == trials["recipe"]["token_ids"] session.export_experiment() report = json.loads(Path(session.experiment_file).read_text()) assert report["runtime_only"] is True assert report["text_payloads_included"] is False assert "prompt" not in report and "prompt_token_ids" not in report assert "tokens" not in report["inspection"] assert all("text" not in trial and "token_ids" not in trial for trial in report["trials"].values()) assert report["model"] == session.handle.model_name and report["dtype"] == "torch.float32" assert len(report["prompt_sha256"]) == 64 session.export_experiment(True) full = json.loads(Path(session.experiment_file).read_text()) assert full["prompt"] == "hello world" assert full["prompt_token_ids"] == [7, 8] assert full["inspection"]["tokens"][0][1] == "hello" assert "text" in full["trials"]["baseline"] assert session.comparison["prompt"] == "hello world" def test_import_model_mismatch_is_transactional(session, tmp_path): before = session.recipe path = tmp_path / "wrong.json" NeuronRecipe("different-model", (NeuronEdit(0, 1, "zero"),)).save(path) with pytest.raises(ValueError, match="model"): session.import_recipe(path) assert session.recipe is before and session.control is None with pytest.raises(ValueError, match="Choose"): session.import_recipe(None) def test_exports_are_repeatable_and_previous_artifacts_are_immutable(session): session.export_recipe() first_recipe = Path(session.recipe_file) first_bytes = first_recipe.read_bytes() session.edit("add", 1, 0, 1, "zero") session.export_recipe() assert Path(session.recipe_file) != first_recipe and first_recipe.read_bytes() == first_bytes session.compare("hello world", 1) session.export_experiment() first_report = Path(session.experiment_file) report_bytes = first_report.read_bytes() session.export_experiment(True) assert Path(session.experiment_file) != first_report and first_report.read_bytes() == report_bytes def test_session_state_copy_never_shares_live_models(session): empty = lab.NeuronLabSession() copied = deepcopy(empty) try: assert empty is not copied and empty._lock is not copied._lock assert empty.directory != copied.directory with pytest.raises(TypeError, match="cannot be copied"): deepcopy(session) finally: empty.close() copied.close() @pytest.mark.parametrize("value", [True, 1.5, float("inf"), float("nan"), "2"]) def test_integer_inputs_reject_ambiguous_values(session, value): with pytest.raises(ValueError, match="integer"): session.inspect("hello", value, 0) @pytest.mark.parametrize("prompt,tokens,message", [ ("", 1, "Enter a prompt"), ("x" * 16385, 1, "characters"), ("hello " * 513, 1, "512"), ("hello", 0, "range"), ("hello", 129, "range"), ]) def test_input_bounds_fail_before_model_forward(session, monkeypatch, prompt, tokens, message): def forbidden(*args, **kwargs): raise AssertionError("invalid request reached model") monkeypatch.setattr(session.handle.model, "forward", forbidden) with pytest.raises(ValueError, match=message): session.compare(prompt, tokens) def test_context_length_and_control_export_preconditions(session): session.handle.config.max_position_embeddings = 3 with pytest.raises(ValueError, match="context length"): session.compare("hello world", 2) with pytest.raises(ValueError, match="control recipe"): session.compare("hello", 1, True) with pytest.raises(ValueError, match="comparison"): session.export_experiment() def test_failed_requests_clear_previous_inspection_and_comparison(session): session.inspect("hello world", 0, 1) session.compare("hello world", 1) with pytest.raises(ValueError): session.inspect("", 0, 1) assert session.inspection is None with pytest.raises(ValueError): session.compare("", 1) assert session.comparison is None def test_forward_failure_and_cancellation_remove_every_hook(session, monkeypatch): session.edit("add", 1, 0, 0, "zero") model = session.handle.model before = hooks(model) original = model.forward def failing(*args, **kwargs): original(*args, **kwargs) raise RuntimeError("injected forward failure") monkeypatch.setattr(model, "forward", failing) with pytest.raises(RuntimeError, match="injected"): session.compare("hello world", 3) assert hooks(model) == before and session.comparison is None def cancelling(*args, **kwargs): result = original(*args, **kwargs) session.cancel() return result monkeypatch.setattr(model, "forward", cancelling) with pytest.raises(RuntimeError, match="cancelled"): session.compare("hello world", 3) assert hooks(model) == before and session.comparison is None monkeypatch.setattr(model, "forward", original) session.compare("hello world", 1) assert session.comparison is not None def test_nonfinite_logits_reject_comparison_and_cleanup(session, monkeypatch): model = session.handle.model before = hooks(model) monkeypatch.setattr(model, "forward", lambda **kwargs: SimpleNamespace( logits=torch.full((1, 2, len(FIXTURE_VOCAB)), float("nan")), )) with pytest.raises(ValueError, match="nonfinite logits"): session.compare("hello world", 2) assert hooks(model) == before def test_sessions_have_independent_models_recipes_and_locks(session, local_model): other = lab.NeuronLabSession() try: other.load(str(local_model)) other.compare("hello world", 2) original = other.comparison["trials"]["baseline"]["token_ids"] session.edit("add", 1, 0, 2, "set", 100.) session.compare("hello world", 2) assert other.handle.model is not session.handle.model assert other.recipe.edits == () session.close() other.compare("hello world", 2) assert other.comparison["trials"]["baseline"]["token_ids"] == original finally: other.close() def test_busy_session_rejects_concurrent_mutation_and_cancel_is_nonblocking(session): acquired, release = threading.Event(), threading.Event() def hold(): with session._lock: acquired.set() release.wait(5) thread = threading.Thread(target=hold) thread.start() try: assert acquired.wait(5) with pytest.raises(RuntimeError, match="busy"): session.reset() session.cancel() assert session._cancel.is_set() finally: release.set() thread.join(5) assert not thread.is_alive() def test_failed_model_validation_cleans_new_handle(local_model, monkeypatch): actual = lab.load_model cleaned = [] def loader(*args, **kwargs): handle = actual(*args, **kwargs) original = handle.cleanup def cleanup(): cleaned.append(handle.model_name) original() handle.cleanup = cleanup return handle monkeypatch.setattr(lab, "load_model", loader) monkeypatch.setattr(lab, "validate_neuron_model", lambda handle: (_ for _ in ()).throw( ValueError("unsupported site"))) session = lab.NeuronLabSession() try: with pytest.raises(ValueError, match="unsupported site"): session.load(str(local_model)) assert cleaned and session.handle is None finally: session.close() def test_unloaded_device_and_architecture_guards(monkeypatch): session = lab.NeuronLabSession() try: with pytest.raises(ValueError, match="Load"): session.reset() for model, device, message in [("", "cpu", "model id"), ("model", "auto", "explicitly")]: with pytest.raises(ValueError, match=message): session.load(model, device) monkeypatch.setattr(torch.cuda, "is_available", lambda: False) monkeypatch.setattr(torch.backends.mps, "is_available", lambda: False) for device in ("cuda", "mps"): with pytest.raises(ValueError, match="not available"): session.load("model", device) with pytest.raises(ValueError, match="supported dense"): lab._supported_config(SimpleNamespace(model_type="mixtral")) finally: session.close() def test_verified_reset_replays_frozen_input_and_retains_evidence(session, tmp_path): before = {k: v.clone() for k, v in session.handle.model.state_dict().items()} session.edit("add", 1, 0, 2, "add", 5.) session.compare("hello world", 3) original = deepcopy(session.comparison) session.verify_reset() receipt = session.comparison assert receipt["run_id"] == original["run_id"] assert receipt["trials"]["recipe"] == original["trials"]["recipe"] assert receipt["trials"]["reset"]["token_ids"] == original["trials"]["baseline"]["token_ids"] assert receipt["verification"]["status"] == "passed" assert receipt["verification"]["first_logits_max_abs_error"] == 0 assert receipt["verification"]["hook_registry_restored"] is True assert receipt["verification"]["weight_checksum_checked"] is False assert not session.recipe.edits and session.control is None assert all(torch.equal(before[k], v) for k, v in session.handle.model.state_dict().items()) session.export_experiment() report = json.loads(Path(session.experiment_file).read_text()) assert report["verification"]["status"] == "passed" assert all("text" not in t and "token_ids" not in t for t in report["trials"].values()) session.verify_reset() # A repeated replay retains the original comparison, not a new baseline. assert session.comparison["run_id"] == original["run_id"] def test_reset_verification_does_not_certify_matching_text_with_changed_logits(session, monkeypatch): session.compare("hello", 2) generate = session._generate def drift(*args): trial, logits = generate(*args) return trial, logits + .01 monkeypatch.setattr(session, "_generate", drift) session.verify_reset() check = session.comparison["verification"] assert check["status"] == "mismatch" and check["token_ids_exact"] assert check["first_logits_exact"] is False and check["first_logits_max_abs_error"] > 0 def test_reset_verification_failure_clears_edits_without_stale_success(session, monkeypatch): session.edit("add", 1, 0, 2, "zero") session.compare("hello", 2) before = hooks(session.handle.model) def failure(*args): raise RuntimeError("cancelled") monkeypatch.setattr(session, "_generate", failure) with pytest.raises(RuntimeError, match="cancelled"): session.verify_reset() assert not session.recipe.edits assert session.comparison["verification"]["status"] == "failed" assert "reset" not in session.comparison["trials"] assert hooks(session.handle.model) == before with pytest.raises(ValueError, match="Run a comparison"): session.verify_reset() def test_new_context_or_recipe_invalidates_reset_reference(session): session.compare("hello", 2) session.set_system_prompt("helpful") with pytest.raises(ValueError, match="Run a comparison"): session.verify_reset() session.compare("world", 2) session.edit("add", 1, 0, 1, "zero") assert session._comparison_reference is None session.compare("hello", 2) session.reset() assert session._comparison_reference is None def test_reset_reports_hook_registry_change_and_preserves_external_hook(session): session.compare("hello", 2) external = session.handle.model.register_forward_hook(lambda *args: None) try: session.verify_reset() assert session.comparison["verification"]["status"] == "mismatch" assert not session.comparison["verification"]["hook_registry_restored"] assert external.id in session.handle.model._forward_hooks finally: external.remove() def test_generation_records_actual_stop_reason(session): session.handle.model.generation_config.eos_token_id = list(range(len(FIXTURE_VOCAB))) session.compare("hello", 4) assert all(t["stop_reason"] == "eos" and t["tokens"] == 1 for t in session.comparison["trials"].values()) session.handle.model.generation_config.eos_token_id = -1 session.compare("hello", 2) assert all(t["stop_reason"] == "token_limit" and t["tokens"] == 2 for t in session.comparison["trials"].values())