mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
350 lines
17 KiB
Python
350 lines
17 KiB
Python
"""Offline Prompt Prism validation, numerical oracles and transaction coverage."""
|
|
|
|
from copy import deepcopy
|
|
from dataclasses import replace
|
|
import hashlib
|
|
import json
|
|
import threading
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus import neuron_lab_prism as prism
|
|
from obliteratus.ui_neuron_lab import NeuronLabSession
|
|
from tests import test_neuron_lab as lab_fixtures
|
|
|
|
|
|
pytestmark = [pytest.mark.cpu, pytest.mark.integration]
|
|
bounded_threads = lab_fixtures.bounded_threads
|
|
local_model = lab_fixtures.local_model
|
|
session = lab_fixtures.session
|
|
hooks = lab_fixtures.hooks
|
|
|
|
|
|
def digest(text):
|
|
return hashlib.sha256(text.encode("utf-8")).hexdigest()
|
|
|
|
|
|
def manual_generation(session, inputs, limit, *, scope=None):
|
|
"""Independent direct forward loop and a manually applied single-channel offset."""
|
|
ids, mask = inputs["input_ids"].clone(), inputs["attention_mask"].clone()
|
|
length = ids.shape[1]
|
|
handle = None
|
|
if scope:
|
|
def edit(module, args):
|
|
hidden = args[0].clone()
|
|
if scope == "all_tokens":
|
|
hidden[:, :, 3] += 37.
|
|
elif scope == "prompt_tokens":
|
|
hidden[:, :length, 3] += 37.
|
|
else:
|
|
hidden[:, length:, 3] += 37.
|
|
return (hidden,)
|
|
handle = session.handle.model.model.layers[0].mlp.down_proj.register_forward_pre_hook(edit)
|
|
first = None
|
|
try:
|
|
with torch.inference_mode():
|
|
for _ in range(limit):
|
|
logits = session.handle.model(input_ids=ids, attention_mask=mask, use_cache=False).logits[0, -1].float()
|
|
if first is None:
|
|
first = logits.clone()
|
|
token = logits.argmax().reshape(1, 1)
|
|
ids = torch.cat((ids, token), dim=-1)
|
|
mask = torch.cat((mask, torch.ones_like(token)), dim=-1)
|
|
if token.item() == session.handle.model.generation_config.eos_token_id:
|
|
break
|
|
finally:
|
|
if handle:
|
|
handle.remove()
|
|
return ids[0, length:].tolist(), first.double()
|
|
|
|
|
|
def test_literal_forms_are_ordered_and_do_not_claim_equivalence():
|
|
prompt = ' Hello\t"world"\nStraße '
|
|
forms = prism.prompt_forms(prompt)
|
|
assert [(form["id"], form["prompt"]) for form in forms] == [
|
|
("original", prompt), ("uppercase", ' HELLO\t"WORLD"\nSTRASSE '),
|
|
("json_string", json.dumps(prompt, ensure_ascii=False)),
|
|
("line_breaks", 'Hello\n"world"\nStraße'),
|
|
]
|
|
assert forms[2]["transformation"] == {"operation": "json_string_literal", "ensure_ascii": False}
|
|
assert forms[3]["transformation"]["separator"] == "\n"
|
|
assert "none is guaranteed to preserve meaning" in prism.QUALIFICATION
|
|
assert prism.prompt_forms("hello")[0]["prompt"] == prism.prompt_forms("hello")[3]["prompt"]
|
|
assert len(prism.prompt_forms("x" * 4096)[0]["prompt"]) == 4096
|
|
|
|
|
|
@pytest.mark.parametrize("scope", ["all_tokens", "prompt_tokens", "generated_tokens"])
|
|
def test_real_model_matches_independent_greedy_and_intervention_oracle(session, scope):
|
|
session.edit("add", 1, 0, 3, "add", 37., scope=scope)
|
|
recipe = session.recipe
|
|
model = session.handle.model
|
|
weights, initial_hooks = deepcopy(model.state_dict()), hooks(model)
|
|
session._tokenization = {"existing": ["untouched"]}
|
|
report = prism.run_prism(session, "hello world safe", 3)
|
|
assert session.recipe is recipe
|
|
assert session._tokenization == {"existing": ["untouched"]}
|
|
assert report["recipe"] == recipe.to_dict()
|
|
assert report["schema"] == prism.SCHEMA and report["runtime_only"] is True
|
|
assert report["limits"] == {"variants": 4, "conditions_per_variant": 3, "maximum_forward_calls": 36}
|
|
assert report["model"] == session.handle.model_name
|
|
for variant in report["variants"]:
|
|
inputs = session._inputs(variant["prompt"], 3)
|
|
baseline_ids, baseline_logits = manual_generation(session, inputs, 3)
|
|
edited_ids, edited_logits = manual_generation(session, inputs, 3, scope=scope)
|
|
trials, measures = variant["trials"], variant["measurements"]
|
|
assert trials["baseline"]["token_ids"] == trials["reset"]["token_ids"] == baseline_ids
|
|
assert trials["recipe"]["token_ids"] == edited_ids
|
|
assert trials["recipe"]["text"] == session.handle.tokenizer.decode(edited_ids, skip_special_tokens=True)
|
|
assert trials["recipe"]["recipe"] == recipe.to_dict()
|
|
assert trials["baseline"]["recipe"]["edits"] == trials["reset"]["recipe"]["edits"] == []
|
|
reference, edited = baseline_logits.log_softmax(-1), edited_logits.log_softmax(-1)
|
|
expected_kl = (reference.exp() * (reference - edited)).sum().item()
|
|
assert measures["first_token_kl_from_baseline"] == pytest.approx(max(0., expected_kl), abs=1e-12)
|
|
assert measures["first_token_max_abs_logit_delta"] == pytest.approx((baseline_logits - edited_logits).abs().max().item())
|
|
assert measures["recipe_token_ids_changed"] == (baseline_ids != edited_ids)
|
|
assert measures["reset_token_ids_equal"] and measures["reset_first_logits_equal"]
|
|
assert variant["input_token_ids"] == inputs["input_ids"][0].tolist()
|
|
assert variant["input_token_strings"] == session.handle.tokenizer.convert_ids_to_tokens(variant["input_token_ids"])
|
|
assert variant["input_tokens"] == len(variant["input_token_ids"])
|
|
assert variant["valid_input_tokens"] == sum(variant["attention_mask"])
|
|
assert variant["prompt_sha256"] == digest(variant["prompt"])
|
|
assert variant["tokenization"]["rendered_prompt_sha256"] == digest(variant["rendered_prompt"])
|
|
assert hooks(model) == initial_hooks
|
|
assert all(torch.equal(weights[key], value) for key, value in model.state_dict().items())
|
|
assert json.loads(json.dumps(report, allow_nan=False)) == report
|
|
|
|
|
|
def test_identity_recipe_and_all_original_comparisons_are_measured(session):
|
|
report = prism.run_prism(session, "hello", 2)
|
|
original = report["variants"][0]["trials"]
|
|
for variant in report["variants"]:
|
|
measures = variant["measurements"]
|
|
assert measures["first_token_kl_from_baseline"] == 0.
|
|
assert measures["first_token_max_abs_logit_delta"] == 0.
|
|
assert not measures["recipe_token_ids_changed"]
|
|
for condition in ("baseline", "recipe"):
|
|
assert measures[f"{condition}_tokens_differ_from_original"] == (
|
|
variant["trials"][condition]["token_ids"] != original[condition]["token_ids"])
|
|
|
|
|
|
@pytest.mark.parametrize("bad", [None, 1, True, "", " \n\t", "x" * 4097, "\ud800"])
|
|
def test_invalid_prompt_fails_before_inference(session, monkeypatch, bad):
|
|
monkeypatch.setattr(session.handle.model, "forward", lambda **kwargs: pytest.fail("invalid request ran model"))
|
|
with pytest.raises(ValueError):
|
|
prism.run_prism(session, bad)
|
|
|
|
|
|
@pytest.mark.parametrize("bad", [None, True, 0, -1, 33, 2., "2", float("nan")])
|
|
def test_invalid_generation_limit_fails_before_inference(session, monkeypatch, bad):
|
|
monkeypatch.setattr(session.handle.model, "forward", lambda **kwargs: pytest.fail("invalid request ran model"))
|
|
with pytest.raises(ValueError, match="integer from 1 to 32"):
|
|
prism.run_prism(session, "hello", bad)
|
|
|
|
|
|
def test_all_forms_are_validated_before_any_forward_and_metadata_is_restored(session, monkeypatch):
|
|
session._tokenization = {"old": 1}
|
|
tokenizer = session.handle.tokenizer
|
|
tokenizer.chat_template = "{{ messages[-1]['content'] }}{% if messages[-1]['content'][0] == '\"' %}{{ ' hello' * 512 }}{% endif %}"
|
|
monkeypatch.setattr(session.handle.model, "forward", lambda **kwargs: pytest.fail("partially prepared forms ran model"))
|
|
with pytest.raises(ValueError, match="512 tokens"):
|
|
prism.run_prism(session, "hello")
|
|
assert session._tokenization == {"old": 1}
|
|
|
|
|
|
@pytest.mark.parametrize("template", [False, True])
|
|
def test_exact_rendered_system_input_and_template_provenance(session, template):
|
|
session.set_system_prompt("You are a helpful assistant.")
|
|
if template:
|
|
session.handle.tokenizer.chat_template = (
|
|
"{% for m in messages %}{{ m['role'] }}: {{ m['content'] }}\n{% endfor %}"
|
|
"{% if add_generation_prompt %}assistant:{% endif %}"
|
|
"{% if enable_thinking %} THINK {% endif %}"
|
|
)
|
|
report = prism.run_prism(session, "hello world", 1)
|
|
assert report["system_prompt"] == session.system_prompt
|
|
assert report["system_prompt_sha256"] == digest(session.system_prompt)
|
|
for variant in report["variants"]:
|
|
metadata = variant["tokenization"]
|
|
assert metadata["system_prompt_sha256"] == report["system_prompt_sha256"]
|
|
assert metadata["chat_template_applied"] is template
|
|
assert session.system_prompt in variant["rendered_prompt"]
|
|
assert variant["prompt"] in variant["rendered_prompt"]
|
|
assert "THINK" not in variant["rendered_prompt"]
|
|
assert digest(variant["rendered_prompt"]) == metadata["rendered_prompt_sha256"]
|
|
if template:
|
|
assert metadata["chat_template_sha256"] == digest(session.handle.tokenizer.chat_template)
|
|
else:
|
|
assert variant["rendered_prompt"] == f"System: {session.system_prompt}\nUser: {variant['prompt']}\nAssistant:"
|
|
|
|
|
|
def test_unstable_rendering_is_rejected_before_forward(session, monkeypatch):
|
|
tokenizer = session.handle.tokenizer
|
|
tokenizer.chat_template = "{{ messages[-1]['content'] }}"
|
|
count = 0
|
|
def render(*args, **kwargs):
|
|
nonlocal count
|
|
count += 1
|
|
return "hello" if count == 1 else "world"
|
|
monkeypatch.setattr(tokenizer, "apply_chat_template", render)
|
|
monkeypatch.setattr(session.handle.model, "forward", lambda **kwargs: pytest.fail("unstable rendering ran model"))
|
|
with pytest.raises(ValueError, match="rendering changed"):
|
|
prism.run_prism(session, "hello")
|
|
|
|
|
|
def test_forward_budget_cache_policy_and_admission(session, monkeypatch):
|
|
session.handle.model.generation_config.eos_token_id = None
|
|
original = session.handle.model.forward
|
|
calls, admissions = [], []
|
|
def forward(*args, **kwargs):
|
|
calls.append(kwargs)
|
|
return original(*args, **kwargs)
|
|
monkeypatch.setattr(session.handle.model, "forward", forward)
|
|
monkeypatch.setattr(session, "_admit", lambda: admissions.append(len(calls)))
|
|
report = prism.run_prism(session, "hello", 32)
|
|
assert admissions == [0]
|
|
assert len(calls) == report["limits"]["maximum_forward_calls"] == 384
|
|
assert all(kwargs["use_cache"] is False and "past_key_values" not in kwargs for kwargs in calls)
|
|
assert all(trial["tokens"] == 32 for variant in report["variants"] for trial in variant["trials"].values())
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["admission", "recipe_forward", "reset_forward", "cancel", "nonfinite"])
|
|
def test_failure_cleanup_and_session_evidence_is_atomic(session, monkeypatch, failure):
|
|
session.edit("add", 1, 0, 3, "add", 37.)
|
|
before = hooks(session.handle.model)
|
|
state = {"existing": "evidence"}
|
|
session.comparison = session.inspection = session.scout = session.dialogue = state
|
|
session._tokenization = {"existing": "metadata"}
|
|
recipe = session.recipe
|
|
original_forward = session.handle.model.forward
|
|
with monkeypatch.context() as patch:
|
|
if failure == "admission":
|
|
patch.setattr(session, "_admit", lambda: (_ for _ in ()).throw(RuntimeError("admission")))
|
|
patch.setattr(session.handle.model, "forward", lambda **kwargs: pytest.fail("rejected admission ran model"))
|
|
else:
|
|
count = 0
|
|
def forward(*args, **kwargs):
|
|
nonlocal count
|
|
count += 1
|
|
if count == {"recipe_forward": 2, "reset_forward": 3}.get(failure):
|
|
raise RuntimeError("forward failed")
|
|
result = original_forward(*args, **kwargs)
|
|
if failure == "nonfinite" and count == 2:
|
|
result.logits.fill_(float("nan"))
|
|
if failure == "cancel":
|
|
session.cancel()
|
|
return result
|
|
patch.setattr(session.handle.model, "forward", forward)
|
|
with pytest.raises((ValueError, RuntimeError)):
|
|
prism.run_prism(session, "hello", 1)
|
|
assert hooks(session.handle.model) == before
|
|
assert session.recipe is recipe and session._tokenization == {"existing": "metadata"}
|
|
assert all(getattr(session, name) is state for name in ("comparison", "inspection", "scout", "dialogue"))
|
|
assert len(prism.run_prism(session, "hello", 1)["variants"]) == 4
|
|
|
|
|
|
def test_recipe_is_frozen_even_if_reference_is_replaced_during_forward(session, monkeypatch):
|
|
session.edit("add", 1, 0, 3, "add", 37.)
|
|
frozen = session.recipe.to_dict()
|
|
original = session._generate
|
|
calls = 0
|
|
def generate(*args, **kwargs):
|
|
nonlocal calls
|
|
calls += 1
|
|
if calls == 1:
|
|
session.recipe = replace(session.recipe, edits=())
|
|
return original(*args, **kwargs)
|
|
monkeypatch.setattr(session, "_generate", generate)
|
|
report = prism.run_prism(session, "hello", 1)
|
|
assert report["recipe"] == frozen
|
|
assert all(variant["trials"]["recipe"]["recipe"] == frozen for variant in report["variants"])
|
|
|
|
|
|
def test_reset_mismatch_is_reported_instead_of_assumed(session, monkeypatch):
|
|
original = session._generate
|
|
calls = 0
|
|
def generate(*args, **kwargs):
|
|
nonlocal calls
|
|
calls += 1
|
|
trial, first = original(*args, **kwargs)
|
|
if calls % 3 == 0:
|
|
trial["token_ids"] = [999]
|
|
first = first + .25
|
|
return trial, first
|
|
monkeypatch.setattr(session, "_generate", generate)
|
|
report = prism.run_prism(session, "hello", 1)
|
|
assert all(not variant["measurements"]["reset_token_ids_equal"] for variant in report["variants"])
|
|
assert all(not variant["measurements"]["reset_first_logits_equal"] for variant in report["variants"])
|
|
|
|
|
|
def test_unloaded_closed_and_busy_sessions_fail_without_inference(session):
|
|
empty = NeuronLabSession()
|
|
with pytest.raises(ValueError, match="Load"):
|
|
prism.run_prism(empty, "hello")
|
|
empty.close()
|
|
with pytest.raises(ValueError, match="expired"):
|
|
prism.run_prism(empty, "hello")
|
|
ready, release = threading.Event(), threading.Event()
|
|
def worker():
|
|
with session._operation():
|
|
ready.set()
|
|
release.wait(5)
|
|
thread = threading.Thread(target=worker)
|
|
thread.start()
|
|
assert ready.wait(5)
|
|
try:
|
|
with pytest.raises(RuntimeError, match="busy"):
|
|
prism.run_prism(session, "hello")
|
|
finally:
|
|
release.set()
|
|
thread.join(5)
|
|
|
|
|
|
def test_locked_core_preserves_cancellation_before_any_preparation(session, monkeypatch):
|
|
session._tokenization = {"existing": "metadata"}
|
|
initial_hooks = hooks(session.handle.model)
|
|
with session._operation():
|
|
session.cancel()
|
|
monkeypatch.setattr(session, "_operation", lambda: pytest.fail("nested operation cleared cancellation"))
|
|
monkeypatch.setattr(session, "_inputs", lambda *args: pytest.fail("cancelled action tokenized input"))
|
|
monkeypatch.setattr(session, "_admit", lambda: pytest.fail("cancelled action requested admission"))
|
|
monkeypatch.setattr(session.handle.model, "forward", lambda **kwargs: pytest.fail("cancelled action ran model"))
|
|
with pytest.raises(RuntimeError, match="cancelled"):
|
|
prism._run_prism_locked(session, "hello", 1)
|
|
assert session._cancel.is_set()
|
|
assert session._tokenization == {"existing": "metadata"}
|
|
assert hooks(session.handle.model) == initial_hooks
|
|
|
|
|
|
def test_export_text_privacy_and_copy_are_explicit(session):
|
|
session.set_system_prompt("Private instruction: keep this phrase local.")
|
|
report = prism.run_prism(session, "private hello world", 1)
|
|
original = deepcopy(report)
|
|
redacted = prism.prism_report(report)
|
|
assert not redacted["text_payloads_included"]
|
|
assert "system_prompt" not in redacted
|
|
for exact, variant in zip(report["variants"], redacted["variants"]):
|
|
assert not {"prompt", "rendered_prompt", "input_token_ids", "input_token_strings"} & variant.keys()
|
|
assert variant["prompt_sha256"] == exact["prompt_sha256"]
|
|
assert variant["tokenization"] == exact["tokenization"]
|
|
for name, trial in variant["trials"].items():
|
|
assert not {"text", "token_ids"} & trial.keys()
|
|
assert trial["text_sha256"] == digest(exact["trials"][name]["text"])
|
|
assert "Private instruction" not in json.dumps(redacted)
|
|
assert prism.prism_report(redacted) == redacted
|
|
assert prism.prism_report(report, True) == original == report
|
|
redacted["variants"][0]["label"] = "changed"
|
|
assert report == original
|
|
|
|
|
|
@pytest.mark.parametrize("bad", [None, {}, {"schema": "different"}, "text"])
|
|
def test_export_rejects_missing_results(bad):
|
|
with pytest.raises(ValueError, match="before exporting"):
|
|
prism.prism_report(bad)
|
|
|
|
|
|
@pytest.mark.parametrize("bad", [None, 1, "yes"])
|
|
def test_export_rejects_implicit_text_opt_in(bad):
|
|
with pytest.raises(ValueError, match="explicitly"):
|
|
prism.prism_report({}, bad)
|