Files
OBLITERATUS/tests/test_gsq_coverage_edges.py
Joseph MaglyandClaude Opus 5.5 3010f7c2c5 test(gsq): cover fail-closed edges of source, workflow, and coverage (#304)
Add CPU-only offline tests for the GSQ adapter, workflow runner, and
coverage certifier guard rails that the release lane did not reach:
real git checkout verification in a temp repo, non-UTF-8/unreadable
checkout files, plan-vs-write tree drift, the post-promotion
trust_remote_code sweep, staging/backup/promotion rollback branches,
launch-binding and manifest tampering, post-assembly drift, pre-spawn
source re-verification for run/assemble, header-only safetensors
parsing, weight-index agreement, explicit shape tables, assembled
index/config/packed-content checks, and retained base tensors.

GSQ is never executed: subprocess launches are faked and the checkout
is the fake adapter tree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 16:43:16 -04:00

515 lines
20 KiB
Python

"""CPU tests for fail-closed edges of the GSQ coverage certifier.
``test_gsq_coverage`` drives realistic checkpoints end to end; these pin
the individual guard rails it does not reach: dtype-name parsing, base
safetensors header reads, weight-index agreement, explicit shape tables,
non-projection overwrites, assembled-output index/config/packed-content
checks, and retained base tensors. Inputs are tiny tensors written to
temp dirs; nothing touches a GPU or the external GSQ toolchain.
"""
from __future__ import annotations
import json
import struct
from pathlib import Path
import pytest
import torch
from safetensors.torch import save_file
from obliteratus.gsq import coverage as cov
# --- dtype names ----------------------------------------------------------
def test_torch_dtype_from_name_accepts_spellings_and_rejects_others():
assert cov._torch_dtype_from_name(None) is None
assert cov._torch_dtype_from_name(torch.float16) is torch.float16
assert cov._torch_dtype_from_name(" torch.BFloat16 ") is torch.bfloat16
assert cov._torch_dtype_from_name("half") is torch.float16
with pytest.raises(ValueError, match="must be a torch dtype or dtype name"):
cov._torch_dtype_from_name(16)
with pytest.raises(ValueError, match="unusable load dtype"):
cov._torch_dtype_from_name("float64")
def test_assembled_config_load_dtypes_dedupes_spellings():
config = {"torch_dtype": "bfloat16", "dtype": "bf16"}
assert cov._assembled_config_load_dtypes(config) == [torch.bfloat16]
config = {"torch_dtype": "float16", "dtype": "float32"}
assert cov._assembled_config_load_dtypes(config) == [
torch.float16,
torch.float32,
]
assert cov._assembled_config_load_dtypes({}) == []
# --- base float load dtypes (header-only reads) ---------------------------
@pytest.mark.parametrize(
("payload", "message"),
[
(b"\x00\x01", "is truncated"),
(struct.pack("<Q", 512 * 1024**2), "implausible header length"),
(struct.pack("<Q", 64) + b"{}", "is truncated"),
(struct.pack("<Q", 3) + b"{x}", "cannot read safetensors header"),
(struct.pack("<Q", 2) + b"[]", "must be a JSON object"),
],
)
def test_base_float_load_dtypes_fail_closed(tmp_path, payload, message):
(tmp_path / "model.safetensors").write_bytes(payload)
with pytest.raises(ValueError, match=message):
cov._base_float_load_dtypes(tmp_path)
def test_base_float_load_dtypes_reads_only_float_matrices(tmp_path):
header = {
"__metadata__": {"format": "pt"},
"odd": "not-an-entry",
"vec": {"dtype": "F16", "shape": [4], "data_offsets": [0, 8]},
"ids": {"dtype": "I64", "shape": [2, 2], "data_offsets": [8, 40]},
"w": {"dtype": "BF16", "shape": [2, 2], "data_offsets": [40, 48]},
}
raw = json.dumps(header).encode()
(tmp_path / "model.safetensors").write_bytes(
struct.pack("<Q", len(raw)) + raw + bytes(48)
)
assert cov._base_float_load_dtypes(tmp_path) == [torch.bfloat16]
with pytest.raises(ValueError, match="cannot list base model dir"):
cov._base_float_load_dtypes(tmp_path / "missing")
# --- module prefix / topology ---------------------------------------------
def test_prefix_from_weights_index_and_empty_shard(tmp_path):
index = tmp_path / "model.safetensors.index.json"
index.write_text("{", encoding="utf-8")
with pytest.raises(ValueError, match="cannot read model weight index"):
cov.detect_module_prefix_from_model_weights(tmp_path)
index.write_text(json.dumps({"weight_map": {}}), encoding="utf-8")
with pytest.raises(ValueError, match="no usable weight_map"):
cov.detect_module_prefix_from_model_weights(tmp_path)
index.write_text(
json.dumps(
{"weight_map": {"model.language_model.layers.0.x": "a.safetensors"}}
),
encoding="utf-8",
)
assert (
cov.detect_module_prefix_from_model_weights(tmp_path)
== cov.MULTIMODAL_PREFIX
)
index.unlink()
save_file({}, str(tmp_path / "model.safetensors"))
with pytest.raises(ValueError, match="hold no tensors"):
cov.detect_module_prefix_from_model_weights(tmp_path)
def test_qwen35_derivation_rejects_moe_types():
with pytest.raises(ValueError, match="unsupported MoE topology"):
cov._derive_qwen35_layer_types({"model_type": "Qwen3_5_MoE"}, 4)
def test_layer_types_length_must_match_layer_count():
with pytest.raises(ValueError, match="layer_types has 2 entries"):
cov.expected_gsq_modules(
{
"num_hidden_layers": 3,
"layer_types": ["full_attention", "full_attention"],
}
)
def test_derive_expected_prefix_fails_closed():
with pytest.raises(ValueError, match="names no .layers.<N>. segment"):
cov._derive_expected_prefix(("lm_head",))
with pytest.raises(ValueError, match="span several layer prefixes"):
cov._derive_expected_prefix(
("model.layers.0.mlp.up_proj",
"model.language_model.layers.0.mlp.up_proj")
)
assert cov._derive_expected_prefix(("model.layers.3.mlp.up_proj",)) == "model"
def test_optional_projection_tensors_only_refused_for_targets():
target = "model.layers.0.mlp.up_proj"
# Optional metadata for an untargeted module is left to other checks.
cov._require_no_optional_projection_tensors(
{"model.layers.0.mlp.gate_proj.weight_zero_point": "f"}, (target,)
)
with pytest.raises(ValueError, match="optional quantization metadata"):
cov._require_no_optional_projection_tensors(
{f"{target}.weight_g_idx": "f"}, (target,)
)
def test_checkpoint_tensors_without_ownership_check(tmp_path):
save_file(
{"model.layers.0.anything.weight": torch.zeros(1)},
str(tmp_path / "model_layers_0_mlp.safetensors"),
)
keys = cov._checkpoint_tensors(tmp_path)
assert keys == {"model.layers.0.anything.weight": "model_layers_0_mlp.safetensors"}
def test_partial_layer_set_ignores_non_layer_names():
assert cov.partial_layer_set(
["lm_head", "model.layers.2.mlp.up_proj", "model.layers.0.x"]
) == [0, 2]
def test_require_complete_coverage_reports_dense_only():
coverage = cov.GSQCoverage(
expected_modules=("a",),
packed_modules=(),
missing_modules=(),
dense_fallback_modules=("a",),
)
with pytest.raises(ValueError) as info:
cov.require_complete_coverage(coverage)
assert "dense fallback for 1" in str(info.value)
assert "missing" not in str(info.value)
# --- base weight index agreement ------------------------------------------
def _base_with_index(tmp_path, weight_map) -> Path:
save_file({"a": torch.zeros(2, 2)}, str(tmp_path / "s1.safetensors"))
(tmp_path / "model.safetensors.index.json").write_text(
json.dumps({"weight_map": weight_map}), encoding="utf-8"
)
return tmp_path
def test_index_agreement_refusals(tmp_path):
index = tmp_path / "model.safetensors.index.json"
_base_with_index(tmp_path, {"a": "s1.safetensors"})
cov.require_base_index_agreement(tmp_path)
index.write_text("{", encoding="utf-8")
with pytest.raises(ValueError, match="cannot read model weight index"):
cov.require_base_index_agreement(tmp_path)
index.write_text(json.dumps({"weight_map": []}), encoding="utf-8")
with pytest.raises(ValueError, match="no usable weight_map"):
cov.require_base_index_agreement(tmp_path)
index.write_text(
json.dumps({"weight_map": {"a": "s2.safetensors"}}), encoding="utf-8"
)
with pytest.raises(ValueError, match="not a top-level safetensors file"):
cov.require_base_index_agreement(tmp_path)
def test_index_agreement_refuses_unreadable_mapped_shard(tmp_path):
_base_with_index(
tmp_path, {"a": "s1.safetensors", "b": "broken.safetensors"}
)
(tmp_path / "broken.safetensors").write_bytes(b"\x00" * 4)
with pytest.raises(ValueError, match="cannot read base model shard"):
cov.require_base_index_agreement(tmp_path)
# --- base weight shapes / explicit shape table ----------------------------
def test_read_base_weight_shapes_fail_closed(tmp_path):
with pytest.raises(ValueError, match="no base-model weights"):
cov.read_base_weight_shapes(tmp_path, ("m",))
save_file(
{"m.weight": torch.zeros(2, 2, 2)}, str(tmp_path / "model.safetensors")
)
with pytest.raises(ValueError, match="lack 'other.weight'"):
cov.read_base_weight_shapes(tmp_path, ("other",))
with pytest.raises(ValueError, match="expected \\[out, in\\]"):
cov.read_base_weight_shapes(tmp_path, ("m",))
@pytest.mark.parametrize(
("shapes", "message"),
[
({}, "has no entry"),
({"m": [1]}, "must be \\[out, in\\]"),
({"m": ["x", 2]}, "must be \\[out, in\\]"),
({"m": [0, 2]}, "must be positive"),
],
)
def test_explicit_shape_table_fails_closed(shapes, message):
with pytest.raises(ValueError, match=message):
cov._checked_shape_table(("m",), shapes, None)
def test_explicit_shapes_used_when_base_dir_has_no_weights(tmp_path):
table = cov._checked_shape_table(("m",), {"m": (4, 8)}, tmp_path)
assert table == {"m": (4, 8)}
with pytest.raises(ValueError, match="without base-model weights"):
cov._checked_shape_table(("m",), None, None)
def test_require_base_topology_matches_needs_weights(tmp_path):
with pytest.raises(ValueError, match="no base-model weights"):
cov.require_base_topology_matches(("m",), tmp_path, 1)
save_file({"x.weight": torch.zeros(1, 1)}, str(tmp_path / "m.safetensors"))
with pytest.raises(ValueError, match="lack 'model.layers.0.p.weight'"):
cov.require_base_topology_matches(("model.layers.0.p",), tmp_path, 1)
@pytest.mark.parametrize(
("kwargs", "message"),
[
({"group_size": True}, "group_size must be an int"),
({"group_size": 0}, "group_size must be positive"),
({"pack_bits": 4.0}, "pack_bits must be an int"),
({"pack_bits": -4}, "pack_bits must be positive"),
],
)
def test_validate_checkpoint_rejects_bad_widths(tmp_path, kwargs, message):
with pytest.raises(ValueError, match=message):
cov.validate_gsq_checkpoint(tmp_path, (), 0, **kwargs)
# --- non-projection overwrites --------------------------------------------
def test_non_projection_tensors_fail_closed(tmp_path):
run = tmp_path / "run"
run.mkdir()
key = "model.layers.0.input_layernorm.weight"
fname = "model_layers_0_mlp.safetensors"
key_files = {key: fname}
save_file({key: torch.ones(4)}, str(run / fname))
# Nothing to check when every key is a projection.
cov._require_non_projection_tensors(run, {}, (), base_model_dir=None)
with pytest.raises(ValueError, match="without base-model"):
cov._require_non_projection_tensors(
run, key_files, (), base_model_dir=None
)
base = tmp_path / "base"
base.mkdir()
with pytest.raises(ValueError, match="no base-model weights"):
cov._require_non_projection_tensors(
run, key_files, (), base_model_dir=base
)
save_file({key: torch.ones(4, dtype=torch.float16)},
str(base / "model.safetensors"))
with pytest.raises(ValueError, match="has dtype torch.float32"):
cov._require_non_projection_tensors(
run, key_files, (), base_model_dir=base
)
save_file({key: torch.ones(0)}, str(run / fname))
with pytest.raises(ValueError, match="is empty"):
cov._require_non_projection_tensors(
run, key_files, (), base_model_dir=base
)
# --- assembled output: index, filenames, competing files ------------------
def test_assembled_key_files_index_failures(tmp_path):
index = tmp_path / "model.safetensors.index.json"
index.write_text("{", encoding="utf-8")
with pytest.raises(ValueError, match="cannot read assembled weight index"):
cov._assembled_key_files(tmp_path)
index.write_text(json.dumps({"weight_map": {}}), encoding="utf-8")
with pytest.raises(ValueError, match="no usable weight_map"):
cov._assembled_key_files(tmp_path)
index.write_text(json.dumps({"weight_map": {"a": 3}}), encoding="utf-8")
with pytest.raises(ValueError, match="malformed"):
cov._assembled_key_files(tmp_path)
index.write_text(
json.dumps({"weight_map": {"a": "model-00001-of-00001.bin"}}),
encoding="utf-8",
)
with pytest.raises(ValueError, match="expected a .safetensors shard"):
cov._assembled_key_files(tmp_path)
index.unlink()
with pytest.raises(ValueError, match="holds no safetensors shards"):
cov._assembled_key_files(tmp_path)
def test_competing_bin_without_index_is_refused(tmp_path):
save_file({"a": torch.zeros(1)}, str(tmp_path / "model.safetensors"))
cov._require_no_competing_weight_files(tmp_path, {"a": "model.safetensors"})
(tmp_path / "pytorch_model.bin").write_bytes(b"x")
with pytest.raises(ValueError, match="competing weight file"):
cov._require_no_competing_weight_files(
tmp_path, {"a": "model.safetensors"}
)
@pytest.mark.parametrize(
("quantization", "message"),
[
({"config_groups": {"g": "x"}}, "unusable config group"),
(
{"config_groups": {"g": {"targets": ["Linear"], "weights": 4}}},
"no usable weights block",
),
],
)
def test_assembled_pack_params_group_shapes(tmp_path, quantization, message):
base = {
"quant_method": "compressed-tensors",
"format": "pack-quantized",
"quantization_status": "compressed",
}
with pytest.raises(ValueError, match=message):
cov._assembled_pack_params(
{**base, **quantization}, config_path=tmp_path / "config.json"
)
@pytest.mark.parametrize(
("config", "message"),
[
(None, "cannot load assembled config"),
("[]", "must be a JSON object"),
("{}", "no quantization_config"),
(json.dumps({"quantization_config": {"ignore": "lm_head"}}),
"no usable quantization_config.ignore"),
(json.dumps({"quantization_config": {"ignore": [1]}}),
"no usable quantization_config.ignore"),
],
)
def test_verify_assembled_config_fail_closed(tmp_path, config, message):
if config is not None:
(tmp_path / "config.json").write_text(config, encoding="utf-8")
with pytest.raises(ValueError, match=message):
cov.verify_assembled_checkpoint(tmp_path)
def test_verify_assembled_requires_directory(tmp_path):
with pytest.raises(ValueError, match="assembled dir not found"):
cov.verify_assembled_checkpoint(tmp_path / "missing")
# --- assembled packed contents --------------------------------------------
MODULE = "model.layers.0.mlp.up_proj"
def _packed_tensors(**overrides):
tensors = {
f"{MODULE}.weight_packed": torch.zeros((4, 16), dtype=torch.int32),
f"{MODULE}.weight_scale": torch.ones((4, 1), dtype=torch.float32),
f"{MODULE}.weight_shape": torch.tensor([4, 128], dtype=torch.int64),
}
for suffix, value in overrides.items():
key = f"{MODULE}.{suffix}"
if value is None:
tensors.pop(key)
else:
tensors[key] = value
return tensors
def _check_packed(tmp_path, tensors, *, group_size=128):
save_file(tensors, str(tmp_path / "model.safetensors"))
key_files = {key: "model.safetensors" for key in tensors}
cov._require_assembled_packed_contents(
tmp_path, key_files, group_size=group_size, pack_bits=4
)
def test_assembled_packed_contents_accepts_honest_module(tmp_path):
_check_packed(tmp_path, _packed_tensors())
# Incomplete trios are reported elsewhere; contents skip them.
_check_packed(tmp_path, _packed_tensors(weight_shape=None))
# No packed modules at all: nothing to check.
cov._require_assembled_packed_contents(
tmp_path, {}, group_size=128, pack_bits=4
)
@pytest.mark.parametrize(
("overrides", "message"),
[
({"weight_packed": torch.zeros((4, 16), dtype=torch.int64)},
"weight_packed has dtype"),
({"weight_scale": torch.ones((4, 1), dtype=torch.int32)},
"expected a floating dtype"),
({"weight_scale": torch.full((4, 1), float("nan"))}, "is not finite"),
({"weight_shape": torch.tensor([4, 128], dtype=torch.int32)},
"expected int64 with shape"),
({"weight_shape": torch.tensor([0, 128], dtype=torch.int64)},
"positive \\[out, in\\]"),
({"weight_packed": torch.zeros((0, 16), dtype=torch.int32)},
"weight_packed is empty"),
({"weight_shape": torch.tensor([4, 4], dtype=torch.int64)},
"not divisible for a 4-bit"),
({"weight_shape": torch.tensor([4, 64], dtype=torch.int64)},
"not divisible by group_size"),
({"weight_packed": torch.zeros((4, 8), dtype=torch.int32)},
"weight_packed has shape"),
({"weight_scale": torch.ones((4, 2), dtype=torch.float32)},
"weight_scale has shape"),
],
)
def test_assembled_packed_contents_refusals(tmp_path, overrides, message):
with pytest.raises(ValueError, match=message):
_check_packed(tmp_path, _packed_tensors(**overrides))
def test_packed_shapes_match_base_without_packed_modules(tmp_path):
# No packed modules: no base read is attempted, even without weights.
cov._require_packed_shapes_match_base(tmp_path, {}, tmp_path / "missing")
# --- retained base tensors ------------------------------------------------
def test_retained_base_tensors(tmp_path):
base = tmp_path / "base"
out = tmp_path / "out"
base.mkdir()
out.mkdir()
key = "model.norm.weight"
key_files = {key: "model.safetensors"}
with pytest.raises(ValueError, match="no base-model weights"):
cov._require_retained_base_tensors(base, key_files)
save_file({key: torch.ones(4)}, str(base / "model.safetensors"))
# Key presence only, when no output dir is given.
cov._require_retained_base_tensors(base, key_files)
save_file({key: torch.full((4,), float("inf"))},
str(out / "model.safetensors"))
with pytest.raises(ValueError, match="is not finite"):
cov._require_retained_base_tensors(base, key_files, out_dir=out)
save_file({key: torch.ones(4, dtype=torch.float16)},
str(out / "model.safetensors"))
with pytest.raises(ValueError, match="has dtype torch.float16"):
cov._require_retained_base_tensors(base, key_files, out_dir=out)
save_file({key: torch.ones(0)}, str(out / "model.safetensors"))
with pytest.raises(ValueError, match="is empty"):
cov._require_retained_base_tensors(base, key_files, out_dir=out)
save_file({key: torch.ones(5)}, str(out / "model.safetensors"))
with pytest.raises(ValueError, match="expected base shape \\[4\\]"):
cov._require_retained_base_tensors(base, key_files, out_dir=out)
save_file({key: torch.ones(4)}, str(out / "model.safetensors"))
cov._require_retained_base_tensors(base, key_files, out_dir=out)
def test_template_listing_skips_non_file_jinja(tmp_path):
chat = tmp_path / cov.CHAT_TEMPLATES_SUBDIR
chat.mkdir()
(chat / "dir.jinja").mkdir()
(chat / "notes.txt").write_text("x", encoding="utf-8")
(chat / "tool.jinja").write_text("{{ x }}", encoding="utf-8")
(tmp_path / "chat_template.jinja").write_text("t", encoding="utf-8")
assert cov._template_files_present_in_base(tmp_path) == [
"chat_template.jinja",
f"{cov.CHAT_TEMPLATES_SUBDIR}/tool.jinja",
]
def test_base_keys_top_level(tmp_path):
with pytest.raises(ValueError, match="no base-model weights"):
cov._base_keys_top_level(tmp_path)
save_file({"a": torch.zeros(1)}, str(tmp_path / "a.safetensors"))
save_file({"b": torch.zeros(1)}, str(tmp_path / "b.safetensors"))
assert cov._base_keys_top_level(Path(tmp_path)) == {"a", "b"}