"""Observation execution options: chat templates, sampling, batching and devices.""" from __future__ import annotations from copy import deepcopy import hashlib import json from types import SimpleNamespace import pytest import torch from obliteratus.analysis import capture_identity as identity_module from obliteratus.analysis.capture_identity import capture_identity, digest, require_dense_device from obliteratus.experiments import ( InterventionStateRefused, ObservationStore, SpecError, compare_results, load_spec, run_observation, spec_digest, ) from obliteratus.experiments import generation from tests.test_observation_runs import CharTokenizer, build_pack, factory, make_spec, tiny_gpt2 pytestmark = pytest.mark.cpu # Digests computed with the observation runner at upstream d311c94, before these options existed. PRE_CHANGE_SPECS = [ ({"capture": {"sites": [0, 1], "budget_bytes": 1000000}, "profile": True, "seed": 7}, "sha256:1e6f57780479593c5ac438f5b31f1108f21b1c313138c94f0001a7f084dff9f3"), ({}, "sha256:1ae1c7fa9619debf667d06c8560cb4734df7d2e94f328ad0312ae8cf014aa7c5"), ] CHAT_TEMPLATE = ("{% for m in messages %}{{ m['role'] }} {{ m['content'] }} {% endfor %}" "{% if add_generation_prompt %}assistant {% endif %}") def chat_tokenizer(template=CHAT_TEMPLATE): """A real fast tokenizer built in memory (no download) over the tiny model's vocabulary. Character-level BPE without merges; ``apply_chat_template`` runs the library's own Jinja rendering of ``template``. """ from tokenizers import Tokenizer, models, normalizers from transformers import PreTrainedTokenizerFast vocab = CharTokenizer().get_vocab() core = Tokenizer(models.BPE(vocab=vocab, merges=[], unk_token=" ")) core.normalizer = normalizers.Lowercase() return PreTrainedTokenizerFast(tokenizer_object=core, bos_token="", eos_token="", pad_token="", unk_token=" ", chat_template=template, name_or_path="local/chat") class NoPadTokenizer(CharTokenizer): pad_token_id = None @pytest.fixture def pack_dir(tmp_path): return build_pack(tmp_path / "pack") def run(pack_dir, store, model_factory=None, **changes): changes.setdefault("profile", False) return run_observation(make_spec(pack_dir, **changes), store, model_factory=model_factory or factory()) def generations(result, task="answers"): root = result["location"]["run_dir"] path = f"{root}/{result['steps'][f'generate-{task}']['path']}/generations.json" with open(path, encoding="utf-8") as handle: return json.load(handle) def standalone(**changes): spec = {"schema_version": "1", "kind": "baseline_observation", "dataset": {"pack": "./pack", "revision": "sha256:" + "ab" * 32, "role": "select"}, "model": {"id": "local/tiny-gpt2", "dtype": "float32"}, "decoding": {"max_new_tokens": 3}, "tasks": [{"name": "answers", "scorer": {"id": "exact_answer", "version": 1}, "target_field": "answer"}]} for key, value in changes.items(): spec[key] = {**spec[key], **value} if isinstance(spec.get(key), dict) else value return spec @pytest.mark.parametrize("extra,expected", PRE_CHANGE_SPECS) def test_pre_change_spec_digests_are_unchanged(extra, expected): from jsonschema import Draft202012Validator from obliteratus.experiments.spec import schema spec = standalone(**extra) Draft202012Validator(schema()).validate(spec) assert spec_digest(load_spec(spec)) == expected explicit = standalone(**extra, decoding={"batch_size": 1, "strategy": "greedy"}, prompt={"chat_template": False}, model={"device": "cpu"}) assert spec_digest(load_spec(explicit)) == expected assert spec_digest(load_spec(standalone(**extra, prompt={}))) == expected def test_new_options_change_identity_and_sampling_settings_are_explicit(): from jsonschema import Draft202012Validator from obliteratus.experiments.spec import schema variants = [ {"prompt": {"chat_template": True}}, {"prompt": {"chat_template": True, "system": "be brief"}}, {"decoding": {"strategy": "sample"}}, {"decoding": {"strategy": "sample", "top_k": 5}}, {"decoding": {"batch_size": 2}}, {"model": {"device": "cuda"}}, {"model": {"device": "cuda:1"}}, ] digests = set() for change in variants: spec = standalone(**change) Draft202012Validator(schema()).validate(spec) digests.add(spec_digest(load_spec(spec))) assert len(digests) == len(variants) and PRE_CHANGE_SPECS[1][1] not in digests filled = load_spec(standalone(decoding={"strategy": "sample", "temperature": 1})) assert filled["decoding"] == {"strategy": "sample", "max_new_tokens": 3, "temperature": 1.0, "top_p": 1.0, "top_k": 0} assert spec_digest(filled) == spec_digest(load_spec(standalone( decoding={"strategy": "sample", "temperature": 1.0, "top_p": 1, "top_k": 0}))) @pytest.mark.parametrize("change,message", [ ({"decoding": {"temperature": 0.7}}, "forbidden"), ({"decoding": {"top_k": 3, "strategy": "greedy"}}, "forbidden"), ({"decoding": {"strategy": "sample", "batch_size": 2}}, "requires batch_size 1"), ({"decoding": {"strategy": "sample", "temperature": 0}}, "bounds"), ({"decoding": {"batch_size": 0}}, "bounds"), ({"model": {"device": "mps"}}, "not qualified"), ({"model": {"device": "mps:0"}}, "not qualified"), ({"model": {"device": "cuda:x"}}, "invalid format"), ({"prompt": {"system": "be brief"}}, "missing chat_template"), ({"prompt": {"chat_template": False, "system": "x"}}, "unsupported value"), ({"prompt": {"template": True}}, "unknown field"), ]) def test_invalid_execution_options_are_refused(change, message): from jsonschema import Draft202012Validator from obliteratus.experiments.spec import schema spec = standalone(**change) with pytest.raises(SpecError, match=message): load_spec(spec) if "mps" not in json.dumps(change) and "batch_size\": 2" not in json.dumps(change): assert list(Draft202012Validator(schema()).iter_errors(spec)) def test_chat_template_renders_prompts_and_changes_identity(pack_dir, tmp_path): tokenizer = chat_tokenizer() plain = run(pack_dir, tmp_path / "plain", factory(tokenizer=tokenizer)) chat = run(pack_dir, tmp_path / "chat", factory(tokenizer=tokenizer), prompt={"chat_template": True}) system = run(pack_dir, tmp_path / "system", factory(tokenizer=tokenizer), prompt={"chat_template": True, "system": "answer briefly"}) assert len({plain["spec_digest"], chat["spec_digest"], system["spec_digest"]}) == 3 assert chat["execution"]["chat_template"] is True and plain["execution"]["chat_template"] is False step = {r["spec_digest"]: r["steps"]["generate-answers"]["identity_sha256"] for r in (plain, chat, system)} assert len(set(step.values())) == 3 assert chat["steps"]["capture"]["identity_sha256"] != plain["steps"]["capture"]["identity_sha256"] rows = generations(chat)["rows"] examples = {e.example_id: e.prompt for e in load_examples(pack_dir)} for row in rows: text = tokenizer.apply_chat_template([{"role": "user", "content": examples[row["example_id"]]}], tokenize=False, add_generation_prompt=True) assert text.startswith("user ") and text.endswith(" assistant ") assert row["rendered_prompt_sha256"] == digest(text) assert row["prompt_tokens"] == len(tokenizer(text, add_special_tokens=False)["input_ids"]) assert "rendered_prompt_sha256" not in generations(plain)["rows"][0] assert generations(system)["rows"][0]["rendered_prompt_sha256"] != rows[0]["rendered_prompt_sha256"] # A different template is a different tokenizer identity and rendered prompt. other = run(pack_dir, tmp_path / "other", factory(tokenizer=chat_tokenizer( CHAT_TEMPLATE.replace("assistant ", "bot "))), prompt={"chat_template": True}) assert other["inputs"]["model"]["tokenizer"]["chat_template"] != \ chat["inputs"]["model"]["tokenizer"]["chat_template"] def load_examples(pack_dir): from obliteratus.concepts.pack import ConceptPack from obliteratus.concepts.scoring import examples_from_dataset examples, _ = examples_from_dataset(ConceptPack.load(pack_dir).dataset(), "select", "answer") return examples def test_chat_template_requires_a_tokenizer_template(pack_dir, tmp_path): with pytest.raises(SpecError, match="chat template"): run(pack_dir, tmp_path / "store", prompt={"chat_template": True}) run_record = ObservationStore(tmp_path / "store").get( spec_digest(load_spec(make_spec(pack_dir, profile=False, prompt={"chat_template": True})))) assert [a["state"] for a in run_record.history()] == ["failed"] def test_example_seed_derivation_is_documented_and_stable(): expected = int.from_bytes(hashlib.sha256(b"7:p3").digest()[:8], "big") assert generation.example_seed(7, "p3") == expected assert generation.example_seed(7, "p4") != expected != generation.example_seed(8, "p3") def test_sampled_runs_reproduce_with_the_seed_and_differ_without_it(pack_dir, tmp_path): sample = {"strategy": "sample", "max_new_tokens": 8, "temperature": 1.5} first = run(pack_dir, tmp_path / "a", decoding=sample, seed=3) again = run(pack_dir, tmp_path / "b", decoding=sample, seed=3) other = run(pack_dir, tmp_path / "c", decoding=sample, seed=4) greedy = run(pack_dir, tmp_path / "d", decoding={"max_new_tokens": 8}, seed=3) assert first["execution"]["strategy"] == "sample" report = compare_results(first, again) assert report["equal"] is True and report["outcomes"] == {"compared_exactly": True} assert "scorecard.tasks.answers.records" in {e["field"] for e in report["exact"]} completions = [row["completion"] for row in generations(first)["rows"]] assert completions == [row["completion"] for row in generations(again)["rows"]] assert completions != [row["completion"] for row in generations(other)["rows"]] assert completions != [row["completion"] for row in generations(greedy)["rows"]] differs = compare_results(first, other) assert differs["outcomes"]["compared_exactly"] is False assert "seed 3 vs 4" in differs["outcomes"]["reason"] assert "scorecard.tasks.answers.records" not in {e["field"] for e in differs["exact"]} assert "scorecard.tasks.answers.n" in {e["field"] for e in differs["exact"]} assert {"field": "scorecard.tasks.*.outcomes", "reason": differs["outcomes"]["reason"]} \ in differs["not_compared"] moved = deepcopy(again) moved["execution"]["device"] = "cuda:0" reason = compare_results(first, moved)["outcomes"]["reason"] assert "device 'cpu' vs 'cuda:0'" in reason mixed = compare_results(first, greedy) assert "strategy 'sample' vs 'greedy'" in mixed["outcomes"]["reason"] def varied_pack(root): """The observation pack with select-role prompts of different lengths.""" from obliteratus.concepts.pack import ConceptPack from tests.test_concept_datasets import alter build_pack(root) files = dict(ConceptPack.load(root).files) def vary(rows): for index, row in enumerate(rows): row["text"] = row["text"] + " again" * index files = alter(files, "positive.jsonl", vary) for name, data in files.items(): (root / name).write_bytes(data) return root def test_batched_greedy_matches_unbatched_per_example(tmp_path): pack_dir = varied_pack(tmp_path / "varied") decoding = {"max_new_tokens": 12} single = run(pack_dir, tmp_path / "one", decoding=decoding) lengths = {row["prompt_tokens"] for row in generations(single)["rows"]} assert len(lengths) > 1, "fixture prompts must differ in length to exercise padding" for size in (2, 3): batched = run(pack_dir, tmp_path / f"b{size}", decoding={**decoding, "batch_size": size}) assert batched["execution"]["batch_size"] == size report = compare_results(single, batched) assert set(report["differences"]) <= {"spec_digest", "execution", "steps.*.identity_sha256"} for task in ("answers", "aliases"): ours, theirs = generations(single, task), generations(batched, task) assert theirs["padding"] == {"side": "left", "pad_token_id": 0, "source": "pad_token"} assert "padding" not in ours assert [{k: r[k] for k in ("example_id", "prompt_tokens", "generated_tokens", "completion")} for r in theirs["rows"]] == ours["rows"] # Missing pad token: eos pads, and the substitution is recorded. eos = run(pack_dir, tmp_path / "eos", factory(tokenizer=NoPadTokenizer()), decoding={**decoding, "batch_size": 2}) assert generations(eos)["padding"] == {"side": "left", "pad_token_id": 2, "source": "eos_token"} def test_batch_padding_and_eos_trimming_helpers(): encoded = [{"input_ids": torch.tensor([[5, 6, 7]]), "attention_mask": torch.ones(1, 3, dtype=torch.long)}, {"input_ids": torch.tensor([[8]]), "attention_mask": torch.ones(1, 1, dtype=torch.long)}] batch = generation.left_pad(encoded, 0) assert batch["input_ids"].tolist() == [[5, 6, 7], [0, 0, 8]] assert batch["attention_mask"].tolist() == [[1, 1, 1], [0, 0, 1]] assert generation._until_eos(torch.tensor([4, 2, 0, 0]), {2}).tolist() == [4, 2] assert generation._until_eos(torch.tensor([4, 5]), {2}).tolist() == [4, 5] model = SimpleNamespace(generation_config=SimpleNamespace(eos_token_id=[2, 3])) assert generation._eos_ids(model, CharTokenizer()) == {2, 3} assert generation._eos_ids(SimpleNamespace(), CharTokenizer()) == {2} none = SimpleNamespace(pad_token_id=None, eos_token_id=None) assert generation.pad_token(none) == (None, None) with pytest.raises(SpecError, match="pad or eos"): generation.step_identity({"decoding": {"batch_size": 2}}, none, {}) def test_greedy_result_is_unchanged_by_the_new_options(pack_dir, tmp_path): result = run(pack_dir, tmp_path / "store") assert result["execution"] == {"device": "cpu", "dtype": "float32", "strategy": "greedy", "seed": 0, "batch_size": 1, "chat_template": False} stored = generations(result) assert set(stored) == {"task", "rows"} assert set(stored["rows"][0]) == {"example_id", "prompt_tokens", "generated_tokens", "completion"} legacy = deepcopy(result) del legacy["execution"] report = compare_results(legacy, result) assert report["differences"] == ["execution"] and report["outcomes"]["compared_exactly"] def meta_model(): from transformers import GPT2Config, GPT2LMHeadModel with torch.device("meta"): return GPT2LMHeadModel(GPT2Config(n_layer=2, n_embd=16, n_head=2, vocab_size=40)).eval() def offloaded(model): from accelerate.hooks import ModelHook, add_hook_to_module add_hook_to_module(model.transformer.h[0], ModelHook()) return model @pytest.mark.parametrize("prepare,message", [ (lambda m: setattr(m.config, "quantization_config", {"load_in_4bit": True}) or m, "quantized"), (lambda m: setattr(m, "is_loaded_in_8bit", True) or m, "quantized"), (offloaded, "offloaded or dispatched"), (lambda m: setattr(m, "hf_device_map", {"transformer": "cpu", "lm_head": "disk"}) or m, "offloaded or dispatched"), (lambda _m: meta_model(), "meta tensors"), ]) def test_quantized_offloaded_and_meta_models_are_refused(pack_dir, tmp_path, prepare, message): model = prepare(tiny_gpt2()) model.generate = lambda *a, **k: pytest.fail("A refused model must not generate") with pytest.raises(ValueError, match=message): run(pack_dir, tmp_path / "store", factory(model=model)) run_record = ObservationStore(tmp_path / "store").get( spec_digest(load_spec(make_spec(pack_dir, profile=False)))) [attempt] = run_record.history() assert attempt["state"] == "failed" and not (run_record.attempt_path(1) / "steps").exists() def test_mps_and_wrong_devices_are_refused(pack_dir, tmp_path, monkeypatch): model = tiny_gpt2() with pytest.raises(ValueError, match="not the requested device cuda"): run(pack_dir, tmp_path / "cuda", factory(model=model), model={"device": "cuda"}) with pytest.raises(ValueError, match="'mps' is not qualified"): require_dense_device(model, "mps") monkeypatch.setattr(identity_module, "_tensor_devices", lambda _m: {torch.device("mps")}) with pytest.raises(ValueError, match="mps are not qualified"): run(pack_dir, tmp_path / "mps", factory(model=model)) monkeypatch.setattr(identity_module, "_tensor_devices", lambda _m: {torch.device("cpu"), torch.device("cuda:0")}) with pytest.raises(ValueError, match="one device"): require_dense_device(model, "cpu") def test_intervention_refusal_precedes_the_device_check(pack_dir, tmp_path): model = tiny_gpt2() model.transformer.h[0].register_forward_hook(lambda *a: None) with pytest.raises(InterventionStateRefused, match="forward_hooks"): run(pack_dir, tmp_path / "store", factory(model=model), model={"device": "cuda"}) def test_capture_records_execution_and_keeps_cpu_identity_shape(pack_dir, tmp_path): result = run(pack_dir, tmp_path / "store") root = f"{result['location']['run_dir']}/{result['capture']['path']}" with open(f"{root}/manifest.json", encoding="utf-8") as handle: manifest = json.load(handle) assert manifest["execution"] == {"device": "cpu", "dtype": "float32"} assert "device" not in manifest["identity"] model = tiny_gpt2() layers = {"layer.0": model.transformer.h[0]} batch = {"input_ids": torch.tensor([[4, 5]]), "attention_mask": torch.ones(1, 2, dtype=torch.long)} assert "device" not in capture_identity(model, CharTokenizer(), layers, [batch], model_id="m", phase="prompt") def test_default_model_factory_moves_dense_models_to_the_spec_device(monkeypatch): from obliteratus.experiments import inputs moved = [] handle = SimpleNamespace(model=SimpleNamespace(to=moved.append), tokenizer=None) calls = [] monkeypatch.setattr("obliteratus.models.loader.load_model", lambda *a, **k: calls.append((a, k)) or handle) section = {"id": "m", "dtype": "float16", "revision": None, "device": "cuda:0"} assert inputs.default_model_factory(section) is handle assert moved == ["cuda:0"] and calls[0][1]["device"] == "cpu" inputs.default_model_factory({**section, "device": "cpu"}) assert moved == ["cuda:0"] def test_fingerprint_mode_is_a_spec_option_and_reaches_the_model_identity(pack_dir, tmp_path): """`model.fingerprint` selects the weight digest; the default leaves digests unchanged.""" base = standalone() assert spec_digest(load_spec(standalone(model={"fingerprint": "sampled"}))) == \ spec_digest(load_spec(base)) assert spec_digest(load_spec(standalone(model={"fingerprint": "full"}))) != \ spec_digest(load_spec(base)) sampled = run(pack_dir, tmp_path / "sampled") full = run(pack_dir, tmp_path / "full", model={"fingerprint": "full"}) assert sampled["inputs"]["model"]["weights"]["mode"] == "sampled" assert full["inputs"]["model"]["weights"]["mode"] == "full" assert full["inputs"]["model"]["device"] == "cpu" def test_full_fingerprint_spec_detects_an_edit_the_sampled_one_misses(pack_dir, tmp_path): from obliteratus.analysis.capture_identity import weight_fingerprint from obliteratus.experiments.inputs import model_identity model, tokenizer = tiny_gpt2(), CharTokenizer() spec = load_spec(make_spec(pack_dir, profile=False)) before = {mode: model_identity(spec, model, tokenizer, {"state": "none"}, weight_digest=mode) for mode in ("sampled", "full")} weight = model.transformer.h[0].mlp.c_fc.weight flat = weight.data.view(-1) sampled_at = set(identity_module._sample_indices(flat.numel(), 256, "cpu").tolist()) untouched = next(i for i in range(1, flat.numel()) if i not in sampled_at) with torch.no_grad(): flat[untouched] += 1.0 after = {mode: model_identity(spec, model, tokenizer, {"state": "none"}, weight_digest=mode) for mode in ("sampled", "full")} assert after["sampled"]["weights"] == before["sampled"]["weights"] assert after["full"]["weights"] != before["full"]["weights"] assert weight_fingerprint(model, mode="full") == after["full"]["weights"]