mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
417 lines
19 KiB
Python
417 lines
19 KiB
Python
"""Offline generation alignment and steering-stage regressions."""
|
|
|
|
import csv
|
|
import json
|
|
from copy import deepcopy
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
from torch import nn
|
|
from transformers import BatchEncoding
|
|
|
|
from scripts import pain_axis_replication as replication
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.mark.parametrize("invalid", [False, True])
|
|
def test_csv_rows_closes_input_and_restores_field_limit(tmp_path, monkeypatch, invalid):
|
|
path = tmp_path / "official.csv"
|
|
path.write_bytes(b"id,generation\n1,\xff\n" if invalid else b"id,generation\n1," + b"x" * 256 + b"\n")
|
|
streams = []
|
|
open_file = Path.open
|
|
|
|
def tracked_open(self, *args, **kwargs):
|
|
stream = open_file(self, *args, **kwargs)
|
|
streams.append(stream)
|
|
return stream
|
|
|
|
monkeypatch.setattr(Path, "open", tracked_open)
|
|
limit = csv.field_size_limit(32)
|
|
try:
|
|
if invalid:
|
|
with pytest.raises(UnicodeDecodeError):
|
|
replication.csv_rows(path)
|
|
else:
|
|
assert replication.csv_rows(path) == [{"id": "1", "generation": "x" * 256}]
|
|
assert streams and all(stream.closed for stream in streams)
|
|
assert csv.field_size_limit() == 32
|
|
finally:
|
|
csv.field_size_limit(limit)
|
|
for stream in streams:
|
|
stream.close()
|
|
|
|
|
|
@pytest.mark.parametrize("stage", ["vectors", "ablate", "battery", "report", "steer_report"])
|
|
def test_stage_inputs_close_before_later_failure(tmp_path, monkeypatch, stage):
|
|
data = tmp_path / "datasets"
|
|
data.mkdir()
|
|
for name in ("3.1_pain_and_control_datasets", "3.1_sadness_dataset"):
|
|
(data / f"{name}.json").write_text('{"datasets": {}}')
|
|
scenarios = [{"id": str(i), "category": "gaslighting", "text": "fixture"} for i in range(100)]
|
|
(data / "4.1_self_other_420_scenarios.json").write_text(json.dumps(scenarios))
|
|
ablation = tmp_path / "results/appC_ablation/ablation_fixture.csv"
|
|
steering = tmp_path / "results/4.2_steering/S2/fixture_steering_S2_neutral50_L0.csv"
|
|
for path, header in ((ablation, "id,condition,generation\n0,baseline,fixture\n"),
|
|
(steering, "coeff,generation\n0,fixture\n")):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(header)
|
|
args = SimpleNamespace(pin=str(tmp_path), out=str(tmp_path / "out"), name="fixture",
|
|
model="offline/fixture", tag="", conditions="baseline", steer_layer=0)
|
|
monkeypatch.setattr(replication, "MODELS", {"fixture": ("raw", 0, 0)})
|
|
monkeypatch.setattr(replication.torch, "load", lambda *a, **kw: {
|
|
key: torch.ones(1, 3) for key in ("s1", "s2", "negemotion", "fear")
|
|
})
|
|
|
|
def fail(*args, **kwargs):
|
|
raise RuntimeError("later model acquisition")
|
|
|
|
monkeypatch.setattr(replication, "load", fail)
|
|
streams = []
|
|
open_file = Path.open
|
|
|
|
def tracked_open(self, *args, **kwargs):
|
|
stream = open_file(self, *args, **kwargs)
|
|
streams.append(stream)
|
|
return stream
|
|
|
|
# Python 3.10's Path accessor caches io.open before the test can patch it.
|
|
# Observe the public boundary used by both read_text() and csv_rows().
|
|
monkeypatch.setattr(Path, "open", tracked_open)
|
|
limit = csv.field_size_limit()
|
|
try:
|
|
error = FileNotFoundError if stage in {"report", "steer_report"} else RuntimeError
|
|
with pytest.raises(error):
|
|
getattr(replication, "stage_" + stage)(args)
|
|
assert streams and all(stream.closed for stream in streams)
|
|
assert csv.field_size_limit() == limit
|
|
finally:
|
|
csv.field_size_limit(limit)
|
|
for stream in streams:
|
|
stream.close()
|
|
|
|
|
|
class Tokenizer:
|
|
pad_token_id = 0
|
|
|
|
def __call__(self, prompt, **kwargs):
|
|
return BatchEncoding({"input_ids": torch.tensor([[int(prompt)]])})
|
|
|
|
def decode(self, ids, **kwargs):
|
|
return str(ids.tolist())
|
|
|
|
|
|
class Decoder(nn.Module):
|
|
def __init__(self, fail=False):
|
|
super().__init__()
|
|
self.config = SimpleNamespace(model_type="llama", hidden_size=3, num_hidden_layers=2)
|
|
self.model = nn.Module()
|
|
self.model.layers = nn.ModuleList([nn.Identity(), nn.Identity()])
|
|
self.device = torch.device("cpu")
|
|
self.hidden = torch.tensor([[[1., 2., 3.], [4., 5., 6.]]], dtype=torch.bfloat16)
|
|
self.calls = []
|
|
self.fail = fail
|
|
|
|
def generate(self, input_ids, **kwargs):
|
|
if self.fail:
|
|
raise RuntimeError("generation failed")
|
|
output = self.hidden.clone()
|
|
for layer in self.model.layers:
|
|
output = layer(output)
|
|
self.calls.append((output.clone(), kwargs))
|
|
return torch.cat([input_ids, torch.tensor([[7]])], dim=1)
|
|
|
|
|
|
@pytest.mark.parametrize("structured", [False, True])
|
|
@pytest.mark.parametrize("return_sequences", [1, 2])
|
|
@pytest.mark.parametrize("offloaded", [False, True])
|
|
def test_ablate_matches_official_first_candidate_per_scenario(
|
|
tmp_path, monkeypatch, structured, return_sequences, offloaded,
|
|
):
|
|
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
|
|
from transformers.generation.utils import GenerateBeamDecoderOnlyOutput
|
|
|
|
scenarios = [{"id": str(i), "category": "gaslighting", "text": str(i)} for i in range(100)]
|
|
data = tmp_path / "datasets"
|
|
data.mkdir()
|
|
(data / "4.1_self_other_420_scenarios.json").write_text(json.dumps(scenarios))
|
|
results = tmp_path / "results/appC_ablation"
|
|
results.mkdir(parents=True)
|
|
with (results / "ablation_fixture.csv").open("w", newline="") as stream:
|
|
writer = csv.DictWriter(stream, fieldnames=["id", "condition", "generation"])
|
|
writer.writeheader()
|
|
writer.writerows({"id": row["id"], "condition": "baseline",
|
|
"generation": f"answer {i}" if i >= 25 else "different"}
|
|
for i, row in enumerate(scenarios))
|
|
batches, rec = [], []
|
|
|
|
class BatchTokenizer:
|
|
pad_token_id = 0
|
|
|
|
def __call__(self, texts, **kwargs):
|
|
assert kwargs == {"return_tensors": "pt", "padding": True, "padding_side": "left"}
|
|
batches.append(texts)
|
|
return BatchEncoding({"input_ids": torch.tensor([[0, int(text) + 1] for text in texts])})
|
|
|
|
def batch_decode(self, output, **kwargs):
|
|
assert output.shape[1] == 1
|
|
assert kwargs == {"skip_special_tokens": True}
|
|
return [f"answer {token - 1}" for token in output[:, 0].tolist()]
|
|
|
|
class Generator(nn.Module):
|
|
device = "meta" if offloaded else "cpu"
|
|
generation_config = SimpleNamespace(num_beams=2, num_return_sequences=return_sequences,
|
|
return_dict_in_generate=structured)
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.embedding = nn.Embedding(2, 3)
|
|
if offloaded:
|
|
add_hook_to_module(self.embedding, AlignDevicesHook(execution_device="cpu", offload=True))
|
|
|
|
def get_input_embeddings(self):
|
|
return self.embedding
|
|
|
|
def generate(self, input_ids, **kwargs):
|
|
assert input_ids.device.type == "cpu"
|
|
assert kwargs == {"max_new_tokens": 60, "do_sample": False, "pad_token_id": 0}
|
|
candidates = (input_ids[:, -1:] + 1000 * torch.arange(return_sequences)).reshape(-1, 1)
|
|
output = torch.cat([input_ids.repeat_interleave(return_sequences, dim=0), candidates], dim=1)
|
|
rec.append((0, torch.ones(input_ids.shape[0] * 2, 4)))
|
|
return GenerateBeamDecoderOnlyOutput(sequences=output) if structured else output
|
|
|
|
model = Generator()
|
|
config = vars(model.generation_config).copy()
|
|
monkeypatch.setattr(replication, "MODELS", {"fixture": ("raw", 0, 0)})
|
|
def load(repo, **kwargs):
|
|
assert kwargs == {"device_map": "auto", "max_memory": {0: "4GiB"}, "offload_folder": "backing"}
|
|
return BatchTokenizer(), model
|
|
|
|
monkeypatch.setattr(replication, "load", load)
|
|
monkeypatch.setattr(replication.torch, "load", lambda *a, **kw: {
|
|
key: torch.ones(1, 3) for key in ("s1", "s2", "negemotion", "fear")
|
|
})
|
|
monkeypatch.setattr(replication, "strict_monitor", lambda *a: (rec, []))
|
|
monkeypatch.setattr(replication.torch.cuda, "empty_cache", lambda: None)
|
|
args = SimpleNamespace(pin=str(tmp_path), out=str(tmp_path), name="fixture",
|
|
model="offline/fixture", conditions="baseline", tag="", device_map="auto",
|
|
max_memory={0: "4GiB"}, offload_folder="backing")
|
|
replication.stage_ablate(args)
|
|
|
|
rows = json.loads((tmp_path / "ablate/baseline.json").read_text())
|
|
assert rows == [{"id": row["id"], "category": row["category"], "generation": f"answer {i}"}
|
|
for i, row in enumerate(scenarios)]
|
|
summary = json.loads((tmp_path / "ablate/summary.json").read_text())["baseline"]
|
|
assert summary["exact_match_official"] == 0.75
|
|
assert summary["changed_vs_baseline_ours"] == 0
|
|
assert summary["placement"]["requested"] == {
|
|
"device_map": "auto", "max_memory": {"0": "4GiB"}, "offload_folder": "backing"}
|
|
assert summary["placement"]["input_execution_device"] == "cpu"
|
|
assert summary["placement"]["parameter_storage_devices"] == ["meta" if offloaded else "cpu"]
|
|
assert [len(batch) for batch in batches] == [8] * 12 + [4]
|
|
assert vars(model.generation_config) == config
|
|
|
|
|
|
@pytest.mark.parametrize("shape", [(2, 3), (3, 3), (5, 3), (2, 2, 3)])
|
|
def test_first_sequences_rejects_unexpected_candidate_count(shape):
|
|
model = SimpleNamespace(generation_config=SimpleNamespace(num_return_sequences=2))
|
|
with pytest.raises(ValueError, match="Pain Axis generation returned an unexpected number of sequences"):
|
|
replication.first_sequences(model, torch.ones(shape), 2)
|
|
|
|
|
|
@pytest.fixture
|
|
def harness(tmp_path, monkeypatch):
|
|
args = SimpleNamespace(pin=str(tmp_path / "pin"), out=str(tmp_path), name="fixture",
|
|
model="offline/fixture", steer_layer=1, tag="", random_arm=True)
|
|
vector = torch.tensor([3., 4., 0.])
|
|
models, cuts, runs = [], [], []
|
|
monkeypatch.setattr(replication, "MODELS", {"fixture": ("raw", 0, 0)})
|
|
monkeypatch.setattr(replication, "LADDER", [-1, 0, 0.5, 2])
|
|
monkeypatch.setattr(replication, "neutral_prompts", lambda _: [str(i) for i in range(50)])
|
|
monkeypatch.setattr(replication.torch, "load", lambda path, **kwargs: (
|
|
{"s2": vector.unsqueeze(0)} if path.name == "vectors_layerwise.pt"
|
|
else {"s2_pain_vector": vector}
|
|
))
|
|
monkeypatch.setattr(replication.torch.cuda, "empty_cache", lambda: None)
|
|
|
|
def load(_, **kwargs):
|
|
model = Decoder()
|
|
models.append(model)
|
|
return Tokenizer(), model
|
|
|
|
def orthogonalize(model, vec, layer, name, options):
|
|
cuts.append((vec.clone(), layer, name))
|
|
model.hidden = model.hidden + 1
|
|
|
|
apply = replication.apply_concept_intervention
|
|
|
|
def intervene(model, direction, mode, **kwargs):
|
|
run = apply(model, direction, mode, **kwargs)
|
|
runs.append((direction, run))
|
|
return run
|
|
|
|
monkeypatch.setattr(replication, "load", load)
|
|
monkeypatch.setattr(replication, "orthogonalize", orthogonalize)
|
|
monkeypatch.setattr(replication, "apply_concept_intervention", intervene)
|
|
return args, vector, models, cuts, runs
|
|
|
|
|
|
def test_resteer_matches_legacy_raw_addition_and_report_layout(harness):
|
|
args, vector, models, cuts, runs = harness
|
|
replication.stage_resteer(args)
|
|
random = replication.unit(torch.randn(3, generator=torch.Generator().manual_seed(1))) * vector.norm()
|
|
assert len(models) == 2 and len(cuts) == 1 and len(runs) == 4
|
|
for model in models:
|
|
assert all(not layer._forward_hooks for layer in model.model.layers)
|
|
assert len(model.calls) == 2 * len(replication.LADDER) * 50
|
|
index = 0
|
|
for vec in (vector, random):
|
|
for coeff in replication.LADDER:
|
|
# Keep the original hook's scalar multiplication and bf16 cast order.
|
|
expected = model.hidden + float(coeff) * 1.0 * 1.0 * 1.0 * vec.to(model.hidden)
|
|
for _ in range(50):
|
|
actual, options = model.calls[index]
|
|
assert torch.equal(actual, expected)
|
|
assert options == {"max_new_tokens": 120, "do_sample": False, "pad_token_id": 0}
|
|
index += 1
|
|
for extracted, run in runs:
|
|
assert extracted.extraction_layers == [0]
|
|
assert run.layers == [1]
|
|
assert run.parameters["scale"] == "vector"
|
|
assert run.config.normalize is False and run.config.norm_scaled is False
|
|
assert run.validation["override_gate"] == replication.REASON
|
|
from pathlib import Path
|
|
|
|
output = Path(args.out) / "resteer"
|
|
assert len(list(output.glob("*__*.json"))) == 4
|
|
for path in output.glob("*__*.json"):
|
|
rows = json.loads(path.read_text())
|
|
assert len(rows) == len(replication.LADDER) * 50
|
|
assert set(rows[0]) == {"coeff", "prompt_idx", "generation"}
|
|
assert rows[0]["generation"] == "[7]"
|
|
assert [r["coeff"] for r in rows[::50]] == replication.LADDER
|
|
metadata = json.loads((output / "metadata" / path.name).read_text())
|
|
assert metadata["parameters"]["scale"] == "vector"
|
|
assert metadata["extraction_layer"] == 0
|
|
for dose in metadata["doses"]:
|
|
assert dose["scale"] == "vector"
|
|
assert dose["steering"]["per_layer"]["1"]["observed_tokens"] == 100
|
|
|
|
|
|
def test_resteer_generation_failure_removes_concept_hooks(harness, monkeypatch):
|
|
args, _, _, _, runs = harness
|
|
model = Decoder(fail=True)
|
|
monkeypatch.setattr(replication, "load", lambda _, **kwargs: (Tokenizer(), model))
|
|
with pytest.raises(RuntimeError, match="generation failed"):
|
|
replication.stage_resteer(args)
|
|
assert len(runs) == 1
|
|
assert all(not layer._forward_hooks for layer in model.model.layers)
|
|
|
|
|
|
def test_loader_forwards_auto_placement_and_budgets_without_model_wide_move(monkeypatch):
|
|
calls = []
|
|
tokenizer = SimpleNamespace(pad_token=None, eos_token="eos")
|
|
model = SimpleNamespace(eval=lambda: None)
|
|
monkeypatch.setattr(replication.AutoTokenizer, "from_pretrained", lambda repo: tokenizer)
|
|
monkeypatch.setattr(replication.AutoModelForCausalLM, "from_pretrained",
|
|
lambda repo, **kwargs: calls.append(kwargs) or model)
|
|
assert replication.load("fixture", device_map="auto", max_memory={"0": "72GiB", "cpu": "8GiB"},
|
|
offload_folder="backing") == (tokenizer, model)
|
|
assert calls == [{"dtype": torch.bfloat16, "low_cpu_mem_usage": True, "device_map": "auto",
|
|
"max_memory": {0: "72GiB", "cpu": "8GiB"}, "offload_folder": "backing"}]
|
|
assert tokenizer.pad_token == "eos"
|
|
|
|
|
|
def test_resteer_records_requested_and_actual_placement(harness):
|
|
args, _, _, _, _ = harness
|
|
args.device_map = "auto"
|
|
args.max_memory = {"cpu": "2GiB"}
|
|
replication.stage_resteer(args)
|
|
from pathlib import Path
|
|
|
|
path = Path(args.out) / "resteer/metadata/pristine__s2.json"
|
|
placement = json.loads(path.read_text())["placement"]
|
|
assert placement["requested"] == {"device_map": "auto", "max_memory": {"cpu": "2GiB"}}
|
|
assert placement["input_execution_device"] == "cpu"
|
|
|
|
|
|
@pytest.mark.parametrize("backing", ["cpu", "disk", "split_disk"])
|
|
@pytest.mark.filterwarnings(
|
|
# Transformers reports meta storage as the model device despite CPU execution.
|
|
r"ignore:You are calling \.generate\(\).*`input_ids` is on cpu, whereas the model is on meta\."
|
|
r":UserWarning:transformers\.generation\.utils$"
|
|
)
|
|
def test_script_collection_edits_and_generations_match_offloaded_model(tmp_path, monkeypatch, backing):
|
|
from accelerate import cpu_offload, disk_offload, dispatch_model
|
|
from accelerate.utils.modeling import get_state_dict_offloaded_model
|
|
from transformers import LlamaConfig, LlamaForCausalLM
|
|
from scripts import pain_axis_appendix_d as appendix
|
|
|
|
torch.manual_seed(11)
|
|
config = LlamaConfig(vocab_size=16, hidden_size=8, intermediate_size=16,
|
|
num_hidden_layers=2, num_attention_heads=2, num_key_value_heads=2,
|
|
max_position_embeddings=32, bos_token_id=1, eos_token_id=2,
|
|
pad_token_id=0, attn_implementation="eager")
|
|
resident = LlamaForCausalLM(config).eval()
|
|
offloaded = deepcopy(resident)
|
|
if backing == "cpu":
|
|
cpu_offload(offloaded, execution_device=torch.device("cpu"))
|
|
elif backing == "disk":
|
|
disk_offload(offloaded, str(tmp_path / "backing"), execution_device=torch.device("cpu"))
|
|
else:
|
|
dispatch_model(offloaded, device_map={
|
|
"model.embed_tokens": "disk", "model.layers.0": "cpu", "model.layers.1": "disk",
|
|
"model.norm": "cpu", "model.rotary_emb": "cpu", "lm_head": "cpu",
|
|
}, main_device="cpu", offload_dir=str(tmp_path / "backing"), force_hooks=True)
|
|
assert offloaded.get_input_embeddings().weight.device.type == "meta"
|
|
monkeypatch.setattr(replication.torch.cuda, "empty_cache", lambda: None)
|
|
|
|
class ScriptTokenizer:
|
|
pad_token_id = 0
|
|
|
|
def __call__(self, prompt, **kwargs):
|
|
if not kwargs.get("return_tensors"):
|
|
return {"input_ids": [1, 3, 4]}
|
|
count = len(prompt) if isinstance(prompt, list) else 1
|
|
return BatchEncoding({"input_ids": torch.tensor([[1, 3, 4]] * count),
|
|
"attention_mask": torch.ones(count, 3, dtype=torch.long)})
|
|
|
|
def batch_decode(self, outputs, **kwargs):
|
|
return [str(output.tolist()) for output in outputs]
|
|
|
|
tokenizer = ScriptTokenizer()
|
|
expected = replication.collect(resident, tokenizer, ["first", "second"], None)
|
|
actual = replication.collect(offloaded, tokenizer, ["first", "second"], None)
|
|
assert torch.equal(actual, expected)
|
|
vector = torch.tensor([1., 2., 3., 4., 5., 6., 7., 8.])
|
|
args = SimpleNamespace(chunk=1, intervention_kwargs="", model="fixture")
|
|
for model in (resident, offloaded):
|
|
replication.orthogonalize(model, vector, 0, "fixture", args)
|
|
state = get_state_dict_offloaded_model(offloaded)
|
|
for name, value in resident.state_dict().items():
|
|
assert torch.equal(state[name], value), name
|
|
|
|
scenarios = [{"id": "fixture", "category": "gaslighting", "text": "fixture"}]
|
|
records, handles = replication.strict_monitor(offloaded, [vector])
|
|
reference_records, reference_handles = replication.strict_monitor(resident, [vector])
|
|
try:
|
|
expected = appendix.generate(resident, tokenizer, scenarios, "raw", 1, 3)
|
|
actual = appendix.generate(offloaded, tokenizer, scenarios, "raw", 1, 3)
|
|
assert actual == expected
|
|
assert records and all(torch.isfinite(values).all() for _, values in records)
|
|
assert [index for index, _ in records] == [index for index, _ in reference_records]
|
|
assert all(torch.equal(actual, expected) for (_, actual), (_, expected)
|
|
in zip(records, reference_records))
|
|
finally:
|
|
for handle in handles + reference_handles:
|
|
handle.remove()
|
|
|
|
|
|
def test_large_qwen_models_use_the_pinned_official_ablation_layers():
|
|
# (format, s1_layer, s2_layer) from appC_ablation/02_ablation_large_models.py at 4d75cd90;
|
|
# the s2 layers match the shipped 4.2 steering references (L38, L60).
|
|
assert replication.MODELS["Qwen_2.5_32B_instruct"] == ("chat", 32, 38)
|
|
assert replication.MODELS["Qwen_2.5_72B_instruct"] == ("chat", 48, 60)
|