Files
OBLITERATUS/tests/test_gemma3_multimodal_support.py
Joseph Magly 0070d28a59 fix: close #668 cleanup-record, Gemma 3 and runs-archive follow-ups
Failed-run cleanup stores the cleanup error whenever its note was not
attached, warns once (stacklevel 4, at run()/run_informed()) only when WARNING
was not emitted, and both entry points reset the record first. Gemma 3: drop
the gemma3_text->gemma3 remap in concept intervention/repair, add a bare
Gemma3Model language_model.layers fallback, and make _float16_source re-raise
only interrupts. runs archive: RecursionError, archive-root and dataset-path
failures become RunArchiveError (dataset paths checked before the run dir
exists); read_snapshot/read_ids raise InvalidArchiveRequest; a non-dict
manifest worker fails closed.

Fixes #668
2026-11-25 10:08:36 -05:00

422 lines
16 KiB
Python

"""Offline Gemma 3 layout contracts (issue #643).
Gemma 3 4B/12B/27B checkpoints load as ``Gemma3ForConditionalGeneration``
(top-level ``model_type`` "gemma3", ``text_config.model_type`` "gemma3_text"),
whose text decoder lives at ``model.language_model.layers``. The 1B text-only
checkpoint loads as ``Gemma3ForCausalLM`` (``model_type`` "gemma3_text") with
its decoder at ``model.layers``. Both layouts must resolve to the text decoder
and never to the vision tower's encoder layers.
"""
from __future__ import annotations
import math
from types import SimpleNamespace
import pytest
import torch
import torch.nn as nn
from obliteratus.models.loader import ModelHandle
from obliteratus.strategies.utils import (
get_attention_module,
get_embedding_module,
get_ffn_module,
get_layer_modules,
)
class _Gemma3Layer(nn.Module):
def __init__(self):
super().__init__()
self.self_attn = nn.Module()
self.mlp = nn.Module()
class _Gemma3TextOnlyModel(nn.Module):
"""``Gemma3ForCausalLM`` layout: ``model.layers``."""
def __init__(self):
super().__init__()
self.model = nn.Module()
self.model.embed_tokens = nn.Embedding(32, 8)
self.model.layers = nn.ModuleList([_Gemma3Layer(), _Gemma3Layer()])
self.lm_head = nn.Linear(8, 32, bias=False)
class _Gemma3MultimodalModel(nn.Module):
"""``Gemma3ForConditionalGeneration`` layout: ``model.language_model.layers``.
The vision tower registers first and has its own (longer) layer list and
embedding, so a generic walk would pick the wrong modules.
"""
def __init__(self):
super().__init__()
self.model = nn.Module()
self.model.vision_tower = nn.Module()
self.model.vision_tower.embeddings = nn.Embedding(16, 4)
self.model.vision_tower.encoder = nn.Module()
self.model.vision_tower.encoder.layers = nn.ModuleList(
[nn.Linear(4, 4) for _ in range(3)]
)
self.model.multi_modal_projector = nn.Linear(4, 8)
self.model.language_model = nn.Module()
self.model.language_model.embed_tokens = nn.Embedding(32, 8)
self.model.language_model.layers = nn.ModuleList([_Gemma3Layer(), _Gemma3Layer()])
self.lm_head = nn.Linear(8, 32, bias=False)
def _text_config():
return SimpleNamespace(
model_type="gemma3_text",
num_hidden_layers=2,
num_attention_heads=4,
hidden_size=8,
intermediate_size=32,
)
def _multimodal_config():
# Composite config: the text geometry lives only under text_config.
return SimpleNamespace(
model_type="gemma3",
architectures=["Gemma3ForConditionalGeneration"],
text_config=_text_config(),
)
def _handle(model, config):
tokenizer = SimpleNamespace(pad_token="<pad>", eos_token="<eos>")
return ModelHandle(model, tokenizer, config, "local/gemma-3", "causal_lm")
def _assert_navigates_text_decoder(handle, layers_expected, embedding_expected):
layers = get_layer_modules(handle)
assert layers is layers_expected
assert len(layers) == handle.num_layers == 2
assert get_attention_module(layers[0], handle.architecture) is layers[0].self_attn
assert get_ffn_module(layers[0], handle.architecture) is layers[0].mlp
assert get_embedding_module(handle) is embedding_expected
@pytest.mark.parametrize("model_type", ["gemma3_text", "gemma3"])
def test_gemma3_text_only_layout_resolves_primary_path(model_type):
model = _Gemma3TextOnlyModel()
config = _text_config()
config.model_type = model_type
handle = _handle(model, config)
assert handle.architecture == model_type
_assert_navigates_text_decoder(handle, model.model.layers, model.model.embed_tokens)
def test_gemma3_conditional_generation_layout_resolves_language_model_layers():
model = _Gemma3MultimodalModel()
handle = _handle(model, _multimodal_config())
assert handle.architecture == "gemma3"
assert (handle.num_layers, handle.num_heads, handle.hidden_size) == (2, 4, 8)
_assert_navigates_text_decoder(
handle, model.model.language_model.layers, model.model.language_model.embed_tokens,
)
def test_known_gemma3_layout_mismatch_lists_both_attempted_paths():
with pytest.raises(
RuntimeError,
match=r"known architecture 'gemma3'.*'model\.layers', 'model\.language_model\.layers'",
):
get_layer_modules(_handle(nn.Module(), _multimodal_config()))
with pytest.raises(RuntimeError) as excinfo:
get_layer_modules(_handle(nn.Module(), _multimodal_config()))
assert "upgrade transformers" not in str(excinfo.value)
def _transformers_major():
transformers = pytest.importorskip("transformers")
return transformers, int(transformers.__version__.split(".")[0])
_TINY_TEXT = dict(
vocab_size=64, hidden_size=16, intermediate_size=32, num_hidden_layers=2,
num_attention_heads=2, num_key_value_heads=1, head_dim=8, max_position_embeddings=32,
)
def _tiny_real_gemma3_conditional_generation(device="meta"):
transformers, major = _transformers_major()
if major < 5:
pytest.skip("The supported Gemma 3 multimodal layout is pinned on transformers >= 5")
config = transformers.Gemma3Config(
text_config=dict(_TINY_TEXT),
vision_config=dict(
hidden_size=16, intermediate_size=32, num_hidden_layers=3,
num_attention_heads=2, image_size=28, patch_size=14,
),
)
config._attn_implementation = "eager"
with torch.device(device):
model = transformers.AutoModelForCausalLM.from_config(config)
# On transformers >= 5 layout drift must fail loudly, not skip.
assert type(model).__name__ == "Gemma3ForConditionalGeneration"
assert isinstance(model.model.language_model.layers, nn.ModuleList)
return model.eval(), config
def test_real_gemma3_conditional_generation_resolves_text_decoder_without_weights():
model, config = _tiny_real_gemma3_conditional_generation()
handle = _handle(model, config)
assert config.model_type == "gemma3"
assert config.text_config.model_type == "gemma3_text"
assert handle.architecture == "gemma3"
assert handle.num_layers == 2
layers = get_layer_modules(handle)
assert layers is model.model.language_model.layers
assert len(layers) == 2
assert get_attention_module(layers[0], handle.architecture) is layers[0].self_attn
assert get_ffn_module(layers[0], handle.architecture) is layers[0].mlp
assert get_embedding_module(handle) is model.get_input_embeddings()
assert all(p.is_meta for p in model.parameters())
@pytest.mark.parametrize("phase", ["probe", "excise"])
def test_real_gemma3_conditional_generation_strict_phase_demand_is_modelled(phase):
from obliteratus.phase_admission import PhaseDemand, model_phase_demand
model, config = _tiny_real_gemma3_conditional_generation(device="cpu")
demand = model_phase_demand(_handle(model, config), phase, mode="strict")
assert isinstance(demand, PhaseDemand)
def test_real_bare_gemma3_text_model_resolves_root_layers():
transformers, major = _transformers_major()
if major < 5:
pytest.skip("Gemma 3 text layout is pinned on transformers >= 5")
config = transformers.Gemma3TextConfig(**_TINY_TEXT)
with torch.device("meta"):
model = transformers.AutoModel.from_config(config)
assert type(model).__name__ == "Gemma3TextModel"
handle = _handle(model, config)
assert handle.architecture == "gemma3_text"
assert get_layer_modules(handle) is model.layers
assert get_embedding_module(handle) is model.embed_tokens
def test_bare_gemma3_text_stub_falls_back_after_primary_path():
model = nn.Module()
model.layers = nn.ModuleList([_Gemma3Layer(), _Gemma3Layer()])
assert get_layer_modules(_handle(model, _text_config())) is model.layers
class _BareGemma3Model(nn.Module):
"""Bare ``Gemma3Model`` (AutoModel on "gemma3"): ``language_model.layers`` (#668).
The vision tower registers first and has a longer layer list, so a generic walk
would pick it; the known-architecture fallback must select the text decoder.
"""
def __init__(self):
super().__init__()
self.vision_tower = nn.Module()
self.vision_tower.embeddings = nn.Embedding(16, 4)
self.vision_tower.encoder = nn.Module()
self.vision_tower.encoder.layers = nn.ModuleList([nn.Linear(4, 4) for _ in range(3)])
self.multi_modal_projector = nn.Linear(4, 8)
self.language_model = nn.Module()
self.language_model.embed_tokens = nn.Embedding(32, 8)
self.language_model.layers = nn.ModuleList([_Gemma3Layer(), _Gemma3Layer()])
def test_bare_gemma3_model_resolves_language_model_layers_not_vision_tower():
model = _BareGemma3Model()
config = _multimodal_config()
config.architectures = ["Gemma3Model"]
handle = _handle(model, config)
assert handle.architecture == "gemma3"
_assert_navigates_text_decoder(
handle, model.language_model.layers, model.language_model.embed_tokens,
)
def test_real_bare_gemma3_model_resolves_text_decoder_without_weights():
transformers, major = _transformers_major()
if major < 5:
pytest.skip("The supported Gemma 3 multimodal layout is pinned on transformers >= 5")
config = transformers.Gemma3Config(
text_config=dict(_TINY_TEXT),
vision_config=dict(
hidden_size=16, intermediate_size=32, num_hidden_layers=3,
num_attention_heads=2, image_size=28, patch_size=14,
),
)
with torch.device("meta"):
model = transformers.AutoModel.from_config(config)
assert type(model).__name__ == "Gemma3Model"
handle = _handle(model, config)
assert handle.architecture == "gemma3"
layers = get_layer_modules(handle)
assert layers is model.language_model.layers
assert len(layers) == 2
assert get_embedding_module(handle) is model.get_input_embeddings()
def test_legacy_gemma3_layout_error_suggests_upgrading_transformers():
# transformers 4.50-4.51: Gemma3ForCausalLM nested at language_model.
model = nn.Module()
model.language_model = nn.Module()
model.language_model.model = nn.Module()
model.language_model.model.layers = nn.ModuleList([_Gemma3Layer(), _Gemma3Layer()])
with pytest.raises(RuntimeError, match=r"known architecture 'gemma3'.*upgrade transformers"):
get_layer_modules(_handle(model, _multimodal_config()))
# --- fp16 baseline hint -----------------------------------------------------
def _baseline_pipeline(dtype, model_dtype):
from obliteratus.abliterate import AbliterationPipeline
pipeline = AbliterationPipeline(
"offline", method="basic", harmful_prompts=["x"], harmless_prompts=["y"], dtype=dtype,
)
pipeline.handle = SimpleNamespace(model=nn.Linear(2, 2).to(model_dtype))
pipeline._reference_perplexity = lambda: float("inf")
return pipeline
@pytest.mark.parametrize("dtype", ["float16", "auto"])
def test_nonfinite_baseline_in_float16_suggests_bfloat16(dtype):
from obliteratus.abliterate import PipelineValidationError
pipeline = _baseline_pipeline(dtype, torch.float16)
with pytest.raises(PipelineValidationError) as excinfo:
pipeline._capture_stock_baseline()
message = str(excinfo.value)
assert "pristine checkpoint produces non-finite loss" in message
assert "--dtype bfloat16" in message
assert "float16" in message
assert math.isinf(pipeline._quality_metrics["baseline_perplexity"])
@pytest.mark.parametrize(
("dtype", "model_dtype"),
[("bfloat16", torch.bfloat16), ("float32", torch.float32), ("float16", torch.float32)],
)
def test_nonfinite_baseline_outside_float16_has_no_bfloat16_hint(dtype, model_dtype):
from obliteratus.abliterate import PipelineValidationError
pipeline = _baseline_pipeline(dtype, model_dtype)
with pytest.raises(PipelineValidationError, match="pristine checkpoint produces non-finite loss") as excinfo:
pipeline._capture_stock_baseline()
assert "--dtype bfloat16" not in str(excinfo.value)
@pytest.mark.parametrize(("dtype", "hinted"), [("float16", True), ("bfloat16", False)])
def test_nonfinite_baseline_falls_back_to_requested_dtype_when_model_dtype_unknown(dtype, hinted):
from obliteratus.abliterate import PipelineValidationError
pipeline = _baseline_pipeline(dtype, torch.float32)
pipeline.handle = SimpleNamespace(model=object())
with pytest.raises(PipelineValidationError) as excinfo:
pipeline._capture_stock_baseline()
assert ("--dtype bfloat16" in str(excinfo.value)) is hinted
class _RaisingDtypeModel(nn.Module):
"""Model whose ``dtype`` property raises (HF raises StopIteration with no floating params)."""
def __init__(self, error):
super().__init__()
self._error = error
@property
def dtype(self):
raise self._error
class _RaisingParametersModel(nn.Module):
def parameters(self, recurse=True):
raise RuntimeError("parameters boom")
class _IntegerOnlyModel(nn.Module):
def __init__(self):
super().__init__()
self.register_parameter(
"ids", nn.Parameter(torch.zeros(2, dtype=torch.int64), requires_grad=False),
)
@pytest.mark.parametrize(
"model",
[
_RaisingDtypeModel(StopIteration()),
_RaisingDtypeModel(RuntimeError("dtype boom")),
_RaisingParametersModel(),
_IntegerOnlyModel(),
],
ids=["dtype-stopiteration", "dtype-runtimeerror", "parameters-raise", "no-floating-params"],
)
@pytest.mark.parametrize(("dtype", "hinted"), [("float16", True), ("bfloat16", False)])
def test_dtype_probe_failure_never_masks_baseline_validation_error(model, dtype, hinted):
from obliteratus.abliterate import PipelineValidationError
pipeline = _baseline_pipeline(dtype, torch.float32)
pipeline.handle = SimpleNamespace(model=model)
with pytest.raises(PipelineValidationError, match="pristine checkpoint produces non-finite loss") as excinfo:
pipeline._capture_stock_baseline()
assert excinfo.value.metric == "baseline_perplexity"
assert excinfo.value.stage == "baseline"
assert math.isinf(pipeline._quality_metrics["baseline_perplexity"])
# The weights' dtype is unknown here, so only the requested dtype may drive the hint.
assert ("--dtype bfloat16" in str(excinfo.value)) is hinted
if hinted:
assert "requested in float16" in str(excinfo.value)
def test_requested_torch_float16_object_triggers_hint_when_model_dtype_unknown():
from obliteratus.abliterate import PipelineValidationError
pipeline = _baseline_pipeline(torch.float16, torch.float32)
pipeline.handle = SimpleNamespace(model=object())
with pytest.raises(PipelineValidationError) as excinfo:
pipeline._capture_stock_baseline()
assert "--dtype bfloat16" in str(excinfo.value)
class _CustomProbeError(BaseException):
pass
@pytest.mark.parametrize("error", [GeneratorExit(), _CustomProbeError("probe")],
ids=["generator-exit", "custom-base-exception"])
@pytest.mark.parametrize(("dtype", "expected"), [("float16", "requested"), ("bfloat16", None)])
def test_float16_source_suppresses_non_interrupt_base_exceptions(error, dtype, expected):
# The docstring promises the probe cannot raise except for interrupts (#668).
pipeline = _baseline_pipeline(dtype, torch.float32)
pipeline.handle = SimpleNamespace(model=_RaisingDtypeModel(error))
assert pipeline._float16_source() == expected
@pytest.mark.parametrize("interrupt", [KeyboardInterrupt, SystemExit])
def test_float16_source_propagates_interrupts(interrupt):
pipeline = _baseline_pipeline("float16", torch.float32)
raised = interrupt("stop")
pipeline.handle = SimpleNamespace(model=_RaisingDtypeModel(raised))
with pytest.raises(interrupt) as caught:
pipeline._float16_source()
assert caught.value is raised