"""CPU tests for the generic GSQ coverage validator.""" from __future__ import annotations import json import os import re import struct from math import ceil from pathlib import Path import pytest import torch from safetensors.torch import load_file, save_file from obliteratus.gsq import coverage as gsq_coverage from obliteratus.gsq import source FAKE_OUT = 8 # Divisible by the default group_size 128, like every real GSQ projection: # group quantization has no padding path, so the validators refuse any # in_features with a remainder (see the divisibility regression tests). FAKE_IN = 128 def _hybrid_config(n_linear=1, n_full=1, multimodal=False): layer_types = ["linear_attention"] * n_linear + ["full_attention"] * n_full config = { "num_hidden_layers": n_linear + n_full, "layer_types": layer_types, } if multimodal: return {"text_config": config, "vision_config": {"hidden_size": 8}} return config def _packed_tensors(out=FAKE_OUT, inn=FAKE_IN, group_size=128, pack_bits=4): """Realistic small quantized tensors: int32 packed, float scale.""" return { "weight_packed": torch.zeros( (out, inn * pack_bits // 32), dtype=torch.int32 ), "weight_scale": torch.zeros( (out, ceil(inn / group_size)), dtype=torch.float32 ), "weight_shape": torch.tensor([out, inn], dtype=torch.int64), } def _layer_key_parts(name): """Split an expected module into (layer index, shard kind).""" idx = name.split(".layers.")[1].split(".")[0] if ".mlp." in name: kind = "mlp" elif ".linear_attn." in name: kind = "linear_attn" else: kind = "self_attn" return idx, kind def _write_checkpoint( tmp_path, expected, *, drop=(), extra=(), progress=1, out=FAKE_OUT, inn=FAKE_IN, prefix="model", ): """Write per-layer shards the assembler consumes plus progress.json. Shard names mirror GSQ's (``layers__``) so the ``layers_(\\d+)`` filename regex matches and filename/key layer indices agree. ``extra`` keys land in a shard for their own key layer index. """ stem = prefix.replace(".", "_") by_file: dict[str, dict] = {} for name in expected: if name in drop: continue idx, kind = _layer_key_parts(name) target = by_file.setdefault(f"{stem}_layers_{idx}_{kind}.safetensors", {}) for suffix, tensor in _packed_tensors(out, inn).items(): target[f"{name}.{suffix}"] = tensor for key in extra: match = re.search(r"\.layers\.(\d+)\.", key) idx = match.group(1) if match else "0" target = by_file.setdefault(f"{stem}_layers_{idx}_extra.safetensors", {}) target[key] = torch.zeros((4, 4), dtype=torch.float32) for fname, tensors in by_file.items(): save_file(tensors, str(tmp_path / fname)) (tmp_path / "progress.json").write_text( json.dumps({"last_completed_layer": progress}), encoding="utf-8" ) def _write_base_model(model_dir, expected, *, out=FAKE_OUT, inn=FAKE_IN): """Write a fake base model dir: single weights shard plus config.""" model_dir.mkdir(parents=True, exist_ok=True) weights = { f"{name}.weight": torch.zeros((out, inn), dtype=torch.float32) for name in expected } save_file(weights, str(model_dir / "model.safetensors")) return model_dir def _write_valid_run(tmp_path, config=None, *, multimodal=False, prefix=None): """Write a checkpoint plus matching base model; return run details.""" config = config if config is not None else _hybrid_config(multimodal=multimodal) run_dir = tmp_path / "run" run_dir.mkdir(exist_ok=True) model_dir = tmp_path / "model" prefix = prefix or gsq_coverage.detect_module_prefix(config) expected = gsq_coverage.expected_gsq_modules(config, module_prefix=prefix) _write_checkpoint(run_dir, expected, prefix=prefix) _write_base_model(model_dir, expected) (model_dir / "config.json").write_text( json.dumps(config), encoding="utf-8" ) return run_dir, model_dir, expected def _validate(run_dir, model_dir, expected, last_layer=1, **kwargs): kwargs.setdefault("base_model_dir", model_dir) return gsq_coverage.validate_gsq_checkpoint( run_dir, expected, last_layer, **kwargs ) def test_qwen38_topology_resolves_to_496_modules_including_a_and_b(): layer_types = gsq_coverage.qwen38_layer_types() assert len(layer_types) == 64 assert sum(t == "linear_attention" for t in layer_types) == 48 assert sum(t == "full_attention" for t in layer_types) == 16 modules = gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 64, "layer_types": list(layer_types)}, expected_count=gsq_coverage.QWEN38_MODULE_COUNT, ) assert len(modules) == len(set(modules)) == 496 assert sum(".mlp." in name for name in modules) == 192 assert sum(".linear_attn." in name for name in modules) == 240 assert sum(".self_attn." in name for name in modules) == 64 assert sum(name.endswith(".linear_attn.in_proj_a") for name in modules) == 48 assert sum(name.endswith(".linear_attn.in_proj_b") for name in modules) == 48 def test_qwen38_contract_constants_agree(): assert ( gsq_coverage.QWEN38_LINEAR_ATTENTION_LAYERS * 5 + gsq_coverage.QWEN38_FULL_ATTENTION_LAYERS * 4 + gsq_coverage.QWEN38_NUM_LAYERS * 3 ) == gsq_coverage.QWEN38_MODULE_COUNT == 496 def test_expected_count_mismatch_raises(): with pytest.raises(ValueError, match="expected 400"): gsq_coverage.expected_gsq_modules( { "num_hidden_layers": 64, "layer_types": list(gsq_coverage.qwen38_layer_types()), }, expected_count=400, ) def test_multimodal_config_uses_language_model_prefix(): modules = gsq_coverage.expected_gsq_modules(_hybrid_config(multimodal=True)) assert all(name.startswith("model.language_model.") for name in modules) assert gsq_coverage.detect_module_prefix({"text_config": {}}) == ( "model.language_model" ) def test_text_only_config_uses_model_prefix(): modules = gsq_coverage.expected_gsq_modules(_hybrid_config()) assert all(name.startswith("model.layers.") for name in modules) assert gsq_coverage.detect_module_prefix({}) == "model" def test_dense_config_without_layer_types(): modules = gsq_coverage.expected_gsq_modules({"num_hidden_layers": 4}) assert len(modules) == 4 * (4 + 3) assert all(".self_attn." in name or ".mlp." in name for name in modules) def test_attention_and_mlp_flags_narrow_targets(): config = _hybrid_config() attn_only = gsq_coverage.expected_gsq_modules(config, quantize_mlp=False) assert attn_only and all(".mlp." not in name for name in attn_only) mlp_only = gsq_coverage.expected_gsq_modules(config, quantize_attention=False) assert mlp_only and all(".mlp." in name for name in mlp_only) with pytest.raises(ValueError, match="at least one"): gsq_coverage.expected_gsq_modules( config, quantize_attention=False, quantize_mlp=False ) def test_invalid_topology_rejected(): with pytest.raises(ValueError, match="unsupported layer type"): gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 1, "layer_types": ["sliding_window"]} ) with pytest.raises(ValueError, match="num_hidden_layers"): gsq_coverage.expected_gsq_modules({}) def test_complete_checkpoint_accepted(tmp_path): run_dir, model_dir, expected = _write_valid_run(tmp_path) result = _validate(run_dir, model_dir, expected) assert result.passed assert result.expected_modules == expected assert len(result.packed_modules) == len(expected) assert result.missing_modules == () assert result.dense_fallback_modules == () assert gsq_coverage.require_complete_coverage(result) is result @pytest.mark.parametrize( "missing", [ "model.layers.0.linear_attn.in_proj_a", "model.layers.0.linear_attn.in_proj_b", "model.layers.1.self_attn.q_proj", "model.layers.1.mlp.gate_proj", ], ) def test_validator_rejects_checkpoint_missing_any_targeted_projection( tmp_path, missing ): expected = gsq_coverage.expected_gsq_modules(_hybrid_config()) assert missing in expected _write_checkpoint(tmp_path, expected, drop=(missing,)) model_dir = _write_base_model(tmp_path / "model", expected) result = _validate(tmp_path, model_dir, expected) assert not result.passed assert result.missing_modules == (missing,) with pytest.raises(ValueError, match=re.escape(missing)): gsq_coverage.require_complete_coverage(result) def test_dense_fallback_rejected(tmp_path): expected = gsq_coverage.expected_gsq_modules(_hybrid_config()) fallback = "model.layers.0.linear_attn.in_proj_a" _write_checkpoint( tmp_path, expected, drop=(fallback,), extra=(f"{fallback}.weight",) ) model_dir = _write_base_model(tmp_path / "model", expected) result = _validate(tmp_path, model_dir, expected) assert not result.passed assert fallback in result.missing_modules assert result.dense_fallback_modules == (fallback,) with pytest.raises(ValueError, match="dense fallback"): gsq_coverage.require_complete_coverage(result) def test_incomplete_progress_rejected(tmp_path): expected = gsq_coverage.expected_gsq_modules(_hybrid_config()) _write_checkpoint(tmp_path, expected, progress=0) with pytest.raises(ValueError, match="incomplete"): gsq_coverage.validate_gsq_checkpoint(tmp_path, expected, 1) def test_missing_progress_rejected(tmp_path): expected = gsq_coverage.expected_gsq_modules(_hybrid_config()) with pytest.raises(ValueError, match="progress.json"): gsq_coverage.validate_gsq_checkpoint(tmp_path, expected, 1) def test_duplicate_keys_rejected(tmp_path): expected = gsq_coverage.expected_gsq_modules(_hybrid_config()) _write_checkpoint(tmp_path, expected) first = tmp_path / "model_layers_0_mlp.safetensors" (tmp_path / "model_layers_0_mlp_copy.safetensors").write_bytes( first.read_bytes() ) model_dir = _write_base_model(tmp_path / "model", expected) with pytest.raises(ValueError, match="duplicate"): _validate(tmp_path, model_dir, expected) def test_incomplete_metadata_rejected(tmp_path): expected = gsq_coverage.expected_gsq_modules(_hybrid_config()) by_file: dict[str, dict] = {} for name in expected: idx, kind = _layer_key_parts(name) target = by_file.setdefault(f"model_layers_{idx}_{kind}.safetensors", {}) target[f"{name}.weight_packed"] = torch.zeros( (FAKE_OUT, FAKE_IN // 8), dtype=torch.int32 ) if not name.endswith("mlp.down_proj"): target[f"{name}.weight_scale"] = torch.zeros((FAKE_OUT, 1)) target[f"{name}.weight_shape"] = torch.tensor( [FAKE_OUT, FAKE_IN], dtype=torch.int64 ) for fname, tensors in by_file.items(): save_file(tensors, str(tmp_path / fname)) (tmp_path / "progress.json").write_text( json.dumps({"last_completed_layer": 1}), encoding="utf-8" ) model_dir = _write_base_model(tmp_path / "model", expected) with pytest.raises(ValueError, match="incomplete quantization metadata"): _validate(tmp_path, model_dir, expected) def test_empty_checkpoint_dir_rejected(tmp_path): (tmp_path / "progress.json").write_text( json.dumps({"last_completed_layer": 0}), encoding="utf-8" ) with pytest.raises(ValueError, match="no safetensor"): gsq_coverage.validate_gsq_checkpoint(tmp_path, (), 0) def test_malformed_progress_rejected(tmp_path): expected = gsq_coverage.expected_gsq_modules(_hybrid_config()) (tmp_path / "progress.json").write_text("not json", encoding="utf-8") with pytest.raises(ValueError, match="not valid JSON"): gsq_coverage.validate_gsq_checkpoint(tmp_path, expected, 1) (tmp_path / "progress.json").write_text("[1, 2]", encoding="utf-8") with pytest.raises(ValueError, match="must be a JSON object"): gsq_coverage.validate_gsq_checkpoint(tmp_path, expected, 1) def test_layer_count_inferred_from_layer_types(): config = {"layer_types": ["linear_attention", "full_attention"]} assert gsq_coverage.num_config_layers(config) == 2 assert len(gsq_coverage.expected_gsq_modules(config)) == 5 + 4 + 6 def test_qwen38_helper_rejects_bad_interval(): with pytest.raises(ValueError, match="full_interval"): gsq_coverage.qwen38_layer_types(full_interval=0) def test_qwen35_without_layer_types_derives_hybrid_topology(): modules = gsq_coverage.expected_gsq_modules( {"model_type": "qwen3_5_text", "num_hidden_layers": 4} ) assert len(modules) == 31 assert sum(".linear_attn." in name for name in modules) == 3 * 5 assert sum(".self_attn." in name for name in modules) == 4 assert sum(".mlp." in name for name in modules) == 4 * 3 assert "model.layers.3.self_attn.q_proj" in modules assert "model.layers.0.linear_attn.in_proj_a" in modules def test_qwen35_derivation_uses_full_attention_interval(): modules = gsq_coverage.expected_gsq_modules( { "model_type": "qwen3_5", "text_config": { "model_type": "qwen3_5_text", "num_hidden_layers": 4, "full_attention_interval": 2, }, } ) assert len(modules) == 2 * (5 + 3) + 2 * (4 + 3) assert all(name.startswith("model.language_model.") for name in modules) with pytest.raises(ValueError, match="full_attention_interval"): gsq_coverage.expected_gsq_modules( { "model_type": "qwen3_5_text", "num_hidden_layers": 4, "full_attention_interval": 0, } ) @pytest.mark.parametrize( "field", ["num_experts", "n_routed_experts", "num_local_experts"] ) def test_expert_counts_rejected(field): with pytest.raises(ValueError, match="MoE"): gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 2, field: 8} ) with pytest.raises(ValueError, match="MoE"): gsq_coverage.expected_gsq_modules( {"text_config": {"num_hidden_layers": 2, field: 4}} ) allowed = gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 1, field: 0} ) assert len(allowed) == 7 def test_mlp_only_layers_rejected(): layer_types = ["linear_attention", "full_attention"] with pytest.raises(ValueError, match="mlp_only_layers"): gsq_coverage.expected_gsq_modules( { "num_hidden_layers": 2, "layer_types": layer_types, "mlp_only_layers": [0], } ) allowed = gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 1, "mlp_only_layers": []} ) assert len(allowed) == 7 def test_decoder_sparse_step_rejected(): with pytest.raises(ValueError, match="decoder_sparse_step"): gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 4, "decoder_sparse_step": 1} ) allowed = gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 1, "decoder_sparse_step": 0} ) assert len(allowed) == 7 def test_malformed_topology_rejected(): with pytest.raises(ValueError, match="layer_types"): gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 1, "layer_types": 42} ) with pytest.raises(ValueError, match="must not be empty"): gsq_coverage.expected_gsq_modules( {"num_hidden_layers": 0, "layer_types": []} ) for bad in (0, -2, True, "4", 2.5): with pytest.raises(ValueError, match="num_hidden_layers"): gsq_coverage.expected_gsq_modules({"num_hidden_layers": bad}) def test_checkpoint_prefix_detection_from_names(): assert gsq_coverage.detect_checkpoint_prefix( ["model.layers.0.self_attn.q_proj"] ) == "model" assert gsq_coverage.detect_checkpoint_prefix( ["model.language_model.layers.0.mlp.gate_proj"] ) == "model.language_model" assert gsq_coverage.detect_checkpoint_prefix( [ "model.layers.0.mlp.gate_proj", "model.language_model.layers.1.mlp.gate_proj", ] ) == "model" with pytest.raises(ValueError, match="unsupported checkpoint layout"): gsq_coverage.detect_checkpoint_prefix(["other.weight"]) def test_prefix_from_index_weight_map(tmp_path): (tmp_path / "config.json").write_text("{}", encoding="utf-8") (tmp_path / "model.safetensors.index.json").write_text( json.dumps( { "metadata": {}, "weight_map": { "model.language_model.layers.0.mlp.gate_proj": ( "shard.safetensors" ) }, } ), encoding="utf-8", ) assert ( gsq_coverage.detect_module_prefix_from_model_weights(tmp_path) == "model.language_model" ) def test_prefix_from_single_safetensors_file(tmp_path): tensors = {"model.layers.0.mlp.gate_proj": torch.zeros(2)} save_file(tensors, str(tmp_path / "model.safetensors")) assert ( gsq_coverage.detect_module_prefix_from_model_weights(tmp_path) == "model" ) def test_prefix_unavailable_without_weights(tmp_path): assert gsq_coverage.detect_module_prefix_from_model_weights(tmp_path) is None def test_moe_model_type_rejected_with_layer_types(): """MoE model_type is rejected even with layer_types, sans experts.""" layer_types = ["linear_attention", "full_attention"] with pytest.raises(ValueError, match="MoE"): gsq_coverage.expected_gsq_modules( { "model_type": "qwen3_moe", "num_hidden_layers": 2, "layer_types": layer_types, } ) with pytest.raises(ValueError, match="MoE"): gsq_coverage.expected_gsq_modules( { "text_config": { "model_type": "qwen3_5_moe", "num_hidden_layers": 2, "layer_types": layer_types, } } ) def test_qwen35_derivation_merges_text_config_overrides(): """Top-level model_type merges with nested interval/count overrides.""" modules = gsq_coverage.expected_gsq_modules( { "model_type": "qwen3_5", "text_config": { "num_hidden_layers": 4, "full_attention_interval": 2, }, } ) assert len(modules) == 2 * (5 + 3) + 2 * (4 + 3) assert all(name.startswith("model.language_model.") for name in modules) assert "model.language_model.layers.1.self_attn.q_proj" in modules assert "model.language_model.layers.3.self_attn.q_proj" in modules assert "model.language_model.layers.0.linear_attn.in_proj_a" in modules def test_prefix_both_layouts_prefers_text_only(tmp_path): (tmp_path / "model.safetensors.index.json").write_text( json.dumps( { "weight_map": { "model.layers.0.mlp.gate_proj": "a.safetensors", "model.language_model.layers.0.mlp.gate_proj": ( "b.safetensors" ), } } ), encoding="utf-8", ) assert ( gsq_coverage.detect_module_prefix_from_model_weights(tmp_path) == "model" ) def test_renamed_shard_invisible_to_assembly_is_rejected(tmp_path): """A shard renamed away from layers_ must fail, not certify coverage.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) renamed = run_dir / "model_layers_1_self_attn.safetensors" renamed.rename(run_dir / "attention-backup.safetensors") with pytest.raises(ValueError, match="unexpected safetensors file"): _validate(run_dir, model_dir, expected) def test_stray_safetensors_copy_is_rejected(tmp_path): """An extra unconsumed safetensors file fails closed.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) save_file( {"stray.weight": torch.zeros(2)}, str(run_dir / "notes.safetensors"), ) with pytest.raises(ValueError, match="unexpected safetensors file"): _validate(run_dir, model_dir, expected) def test_filename_layer_index_mismatch_is_rejected(tmp_path): """A file whose keys name a different layer than its filename fails.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) renamed = run_dir / "model_layers_1_self_attn.safetensors" renamed.rename(run_dir / "model_layers_0_self_attn.safetensors") with pytest.raises(ValueError, match="names layer"): _validate(run_dir, model_dir, expected) def test_multimodal_run_with_realistic_filenames_accepted(tmp_path): """Checkpoints shaped like real GSQ runs validate against base weights.""" config = _hybrid_config(multimodal=True) prefix = "model.language_model" expected = gsq_coverage.expected_gsq_modules(config, module_prefix=prefix) run_dir = tmp_path / "run" run_dir.mkdir() model_dir = tmp_path / "model" _write_checkpoint(run_dir, expected, prefix=prefix) _write_base_model(model_dir, expected) result = _validate(run_dir, model_dir, expected) assert result.passed assert len(result.packed_modules) == len(expected) def test_empty_float_tensors_are_rejected(tmp_path): """Empty floating tensors hold no quantized projections; must fail.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) for shard in run_dir.glob("*.safetensors"): shard.unlink() by_file: dict[str, dict] = {} for name in expected: idx, kind = _layer_key_parts(name) target = by_file.setdefault(f"model_layers_{idx}_{kind}.safetensors", {}) target[f"{name}.weight_packed"] = torch.zeros(0) target[f"{name}.weight_scale"] = torch.zeros(0) target[f"{name}.weight_shape"] = torch.zeros(0) for fname, tensors in by_file.items(): save_file(tensors, str(run_dir / fname)) with pytest.raises(ValueError, match="is empty|dtype"): _validate(run_dir, model_dir, expected) @pytest.mark.parametrize( ("suffix", "tensor", "pattern"), [ ( "weight_packed", torch.zeros((FAKE_OUT, FAKE_IN // 8), dtype=torch.float32), "weight_packed.*dtype", ), ( "weight_scale", torch.zeros((FAKE_OUT, 1), dtype=torch.int32), "weight_scale.*dtype", ), ( "weight_shape", torch.zeros((2,), dtype=torch.float32), "weight_shape.*dtype", ), ], ) def test_wrong_dtypes_are_rejected(tmp_path, suffix, tensor, pattern): """Packed must be int32, scale floating, shape int64 with shape (2,).""" run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) tensors[f"{victim}.{suffix}"] = tensor save_file(tensors, str(shard)) with pytest.raises(ValueError, match=pattern): _validate(run_dir, model_dir, expected) @pytest.mark.parametrize( ("setup", "pattern"), [ ("packed_shape", r"weight_packed has shape"), ("scale_shape", r"weight_scale has shape"), ("shape_values", r"weight_shape holds"), ], ) def test_mismatched_shapes_are_rejected(tmp_path, setup, pattern): """Packed/scale extents and recorded [out, in] must match base weights.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) if setup == "packed_shape": tensors[f"{victim}.weight_packed"] = torch.zeros( (FAKE_OUT, FAKE_IN // 8 + 1), dtype=torch.int32 ) elif setup == "scale_shape": tensors[f"{victim}.weight_scale"] = torch.zeros((FAKE_OUT, 2)) else: tensors[f"{victim}.weight_shape"] = torch.tensor( [FAKE_OUT, FAKE_IN + 8], dtype=torch.int64 ) save_file(tensors, str(shard)) with pytest.raises(ValueError, match=pattern): _validate(run_dir, model_dir, expected) def test_shape_checks_fail_closed_without_base_weights(tmp_path): """Without base weights or explicit shapes, validation must not skip.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) with pytest.raises(ValueError, match="without base-model weights"): gsq_coverage.validate_gsq_checkpoint(run_dir, expected, 1) def test_explicit_shapes_accepted_without_base_weights(tmp_path): """Explicit [out, in] shapes are an alternative to base weights.""" run_dir, _model_dir, expected = _write_valid_run(tmp_path) table = {name: (FAKE_OUT, FAKE_IN) for name in expected} result = gsq_coverage.validate_gsq_checkpoint( run_dir, expected, 1, expected_shapes=table ) assert result.passed bad = dict(table) bad[expected[0]] = (FAKE_OUT, FAKE_IN + 8) with pytest.raises(ValueError, match="weight_shape holds"): gsq_coverage.validate_gsq_checkpoint( run_dir, expected, 1, expected_shapes=bad ) def test_base_shapes_read_from_weight_index(tmp_path): """Indexed (sharded) base models provide shapes via weight_map.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) shard = model_dir / "model.safetensors" indexed = model_dir / "weights.safetensors" shard.rename(indexed) (model_dir / "model.safetensors.index.json").write_text( json.dumps( { "metadata": {}, "weight_map": { f"{name}.weight": "weights.safetensors" for name in expected }, } ), encoding="utf-8", ) result = _validate(run_dir, model_dir, expected) assert result.passed def test_extra_lm_head_scale_rejected(tmp_path): """Unknown quantization metadata must fail: it drops a base weight. The assembler's merge drops ``.weight`` whenever ``.`` is present, so ``lm_head.weight_scale`` in a correctly named layer shard would delete ``lm_head.weight`` with no packed replacement. """ run_dir, model_dir, expected = _write_valid_run(tmp_path) shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) tensors["lm_head.weight_scale"] = torch.zeros((4, 4)) save_file(tensors, str(shard)) with pytest.raises(ValueError, match="lm_head"): _validate(run_dir, model_dir, expected) def test_in_layer_unknown_quant_metadata_rejected(tmp_path): """A quantization suffix for a non-projection module must fail.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) tensors["model.layers.0.mlp.gate.weight_scale"] = torch.zeros((4, 4)) save_file(tensors, str(shard)) with pytest.raises(ValueError, match="not an expected projection"): _validate(run_dir, model_dir, expected) def test_non_layer_key_rejected(tmp_path): """A key outside its file's layer namespace must fail.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) tensors["stray.weight"] = torch.zeros((4, 4)) save_file(tensors, str(shard)) with pytest.raises(ValueError, match="does not belong to layer"): _validate(run_dir, model_dir, expected) def test_layer_owned_non_projection_keys_accepted(tmp_path): """Matching layernorm overwrites validate (assembly keeps base keys).""" run_dir, model_dir, expected = _write_valid_run(tmp_path) norm_key = "model.layers.0.input_layernorm.weight" base_tensors = load_file(str(model_dir / "model.safetensors")) base_tensors[norm_key] = torch.zeros(FAKE_OUT) save_file(base_tensors, str(model_dir / "model.safetensors")) shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) tensors[norm_key] = torch.zeros(FAKE_OUT) save_file(tensors, str(shard)) assert _validate(run_dir, model_dir, expected).passed @pytest.mark.parametrize("bad", ["nan", "inf"]) def test_nonfinite_scales_rejected(tmp_path, bad): """Correctly shaped NaN/Inf scales must fail: they dequantize badly.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) scale = tensors[f"{victim}.weight_scale"].clone() scale.fill_(float("nan") if bad == "nan" else float("inf")) tensors[f"{victim}.weight_scale"] = scale save_file(tensors, str(shard)) with pytest.raises(ValueError, match="not finite"): _validate(run_dir, model_dir, expected) def _rewrite_run_scale(run_dir, victim, scale): """Replace one run-shard scale tensor, keeping packed/shape as-is.""" for shard in sorted(run_dir.glob("*.safetensors")): tensors = load_file(str(shard)) if f"{victim}.weight_scale" in tensors: tensors[f"{victim}.weight_scale"] = scale save_file(tensors, str(shard)) return raise AssertionError(f"no scale for {victim} in {run_dir}") @pytest.mark.parametrize( "dtype", [torch.float32, torch.float16, torch.bfloat16] ) def test_run_shard_decode_overflow_rejected(tmp_path, dtype): """Finite max scales that decode to infinite weights must fail. Zero packed words unpack to the container minimum (``-8`` for the 4-bit container), so an ``finfo(dtype).max`` scale decodes to ``-inf`` in the scale dtype -- exactly what the pack-quantized decompressor computes (``unpacked * scale`` in the scale dtype). """ run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" limit = torch.finfo(dtype).max _rewrite_run_scale( run_dir, victim, torch.full((FAKE_OUT, 1), limit, dtype=dtype) ) with pytest.raises(ValueError, match="decodes to non-finite"): _validate(run_dir, model_dir, expected) @pytest.mark.parametrize( "dtype", [torch.float32, torch.float16, torch.bfloat16] ) def test_run_shard_decode_near_limit_accepted(tmp_path, dtype): """A max/8 scale times unpacked ``-8`` is exactly finite: accept it.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" _rewrite_run_scale( run_dir, victim, torch.full((FAKE_OUT, 1), torch.finfo(dtype).max / 8, dtype=dtype), ) assert _validate(run_dir, model_dir, expected).passed def test_run_shard_decode_zero_codes_accept_max_scale(tmp_path): """All-zero unpacked codes decode to zeros at any finite scale. Every nibble ``8`` unpacks to ``q = 0``, so even an fp32-max scale decodes finitely. The check must use each group's ACTUAL unpacked maximum, not the container bound (which would wrongly refuse). """ run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) # 0x88888888: every 4-bit nibble is 8 -> q = 8 - 8 = 0. tensors[f"{victim}.weight_packed"] = torch.full( (FAKE_OUT, FAKE_IN * 4 // 32), -2004318072, dtype=torch.int32 ) tensors[f"{victim}.weight_scale"] = torch.full( (FAKE_OUT, 1), torch.finfo(torch.float32).max ) save_file(tensors, str(shard)) assert _validate(run_dir, model_dir, expected).passed def test_run_shard_load_dtype_cast_overflow_rejected(tmp_path): """An fp32 1e10 scale is finite stored but +inf in fp16: reject it. Transformers casts floating checkpoint tensors (including ``weight_scale``) to the requested load dtype on load, and the pack-quantized decompressor then decodes in that dtype -- so the stored-dtype check alone would certify weights that load as ``inf``. """ run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" _rewrite_run_scale( run_dir, victim, torch.full((FAKE_OUT, 1), 1e10, dtype=torch.float32) ) # Stored-dtype certification still passes: the scale is finite fp32 # and 8 * 1e10 is finite fp32. assert _validate(run_dir, model_dir, expected).passed with pytest.raises(ValueError, match="not finite after casting"): _validate(run_dir, model_dir, expected, load_dtype=torch.float16) def test_run_shard_load_dtype_product_overflow_rejected(tmp_path): """A scale finite in both dtypes can still overflow decode at load. fp32 ``40000.0`` survives the fp16 cast (fp16 max is 65504) but times unpacked ``-8`` (zero packed words) decodes to ``-inf`` in fp16 while staying finite fp32. """ run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" _rewrite_run_scale( run_dir, victim, torch.full((FAKE_OUT, 1), 40000.0, dtype=torch.float32), ) assert _validate(run_dir, model_dir, expected).passed with pytest.raises(ValueError, match="decodes to non-finite weights in"): _validate(run_dir, model_dir, expected, load_dtype=torch.float16) def test_run_shard_load_dtype_near_limit_accepted(tmp_path): """BF16 near-limit scales still validate under a bf16 load dtype. fp32 ``1e30`` casts finitely to bf16 and ``8 * 1e30`` is finite in bf16, so both the cast check and the load-dtype product check pass (dtype names parse like torch dtypes). """ run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" _rewrite_run_scale( run_dir, victim, torch.full((FAKE_OUT, 1), 1e30, dtype=torch.float32) ) assert _validate(run_dir, model_dir, expected, load_dtype="bfloat16").passed def test_run_shard_rejects_indivisible_group_size(tmp_path): """in_features 16 with group_size 128 must fail: no padding path exists. compressed-tensors 0.15.0.1 apportions group columns with ``unflatten(-1, (num_groups, group_size))`` -- only BLOCK quantization pads -- so a 16-wide projection with 128-wide groups fails loading in ``unflatten`` and must not validate even though its ``[out, ceil(in/group_size)]`` scale shape is self-consistent. """ config = _hybrid_config() expected = gsq_coverage.expected_gsq_modules(config) run_dir = tmp_path / "run" run_dir.mkdir() _write_checkpoint(run_dir, expected, out=FAKE_OUT, inn=16) table = {name: (FAKE_OUT, 16) for name in expected} with pytest.raises(ValueError, match="not divisible by"): gsq_coverage.validate_gsq_checkpoint( run_dir, expected, 1, expected_shapes=table, group_size=128 ) def test_run_shard_accepts_divisible_group_size(tmp_path): """A 256-wide projection with group_size 128 validates (2 groups).""" config = _hybrid_config() expected = gsq_coverage.expected_gsq_modules(config) run_dir = tmp_path / "run" run_dir.mkdir() _write_checkpoint(run_dir, expected, out=FAKE_OUT, inn=256) table = {name: (FAKE_OUT, 256) for name in expected} assert gsq_coverage.validate_gsq_checkpoint( run_dir, expected, 1, expected_shapes=table, group_size=128 ).passed def _materialized_decode_finite(packed, scale, *, in_features, pack_bits): """Independent oracle: per-element unpack, then full float decode. Uses a different code path from ``_require_decoded_weights_finite`` (plain-Python bit loop plus a materialized ``q * s`` product in the scale dtype) so agreement corroborates the bounded check. """ dtype = scale.dtype words = packed.tolist() rows = [ [ ((word >> (pack_bits * i)) & ((1 << pack_bits) - 1)) - (1 << (pack_bits - 1)) for word in row for i in range(32 // pack_bits) ][:in_features] for row in words ] num_groups = scale.shape[1] group_size = in_features // num_groups for row, scales in zip(rows, scale.tolist()): for group, scale_value in enumerate(scales): for code in row[group * group_size : (group + 1) * group_size]: if not bool( torch.isfinite( torch.tensor(code, dtype=dtype) * torch.tensor(scale_value, dtype=dtype) ) ): return False return True @pytest.mark.parametrize( "dtype", [torch.float32, torch.float16, torch.bfloat16] ) def test_decode_check_matches_materialized_decode(dtype, monkeypatch): """The bounded check agrees with full decode over adversarial scales. Scales span zero, subnormal-ish, fp16-relevant (8188/8189/65504), large, and per-dtype near-max values; packed patterns span the container minimum (``-8``), zero codes, maximum (``+7``), and seeded random words. The battery runs with a tiny row-chunk bound to exercise chunking. """ generator = torch.Generator().manual_seed(248) out, inn, pack_bits = 5, 32, 4 groups = 2 limit = torch.finfo(dtype).max magnitudes = [ 0.0, 1e-30, 0.5, 2.0, 8188.0, 8189.0, 65504.0, 1e10, 1e30, limit / 8, limit / 4, limit / 2, limit, ] patterns = { "min": torch.zeros((out, inn * pack_bits // 32), dtype=torch.int32), "zero": torch.full( (out, inn * pack_bits // 32), -2004318072, dtype=torch.int32 ), "max": torch.full( (out, inn * pack_bits // 32), -1, dtype=torch.int32 ), "random": torch.randint( -(2**31), 2**31 - 1, (out, inn * pack_bits // 32), dtype=torch.int32, generator=generator, ), } monkeypatch.setattr( gsq_coverage, "_DECODE_CHECK_CHUNK_ELEMS", 64 ) checked = 0 for name, packed in patterns.items(): for magnitude in magnitudes: # Via float64: magnitudes no dtype can hold saturate to # (non-finite) inf and skip, instead of raising at build. scale = torch.full( (out, groups), magnitude, dtype=torch.float64 ).to(dtype) if not bool(torch.isfinite(scale).all()): continue expected = _materialized_decode_finite( packed, scale, in_features=inn, pack_bits=pack_bits ) try: gsq_coverage._require_decoded_weights_finite( packed, scale, in_features=inn, pack_bits=pack_bits, module=f"probe.{name}", ) verdict = True except ValueError: verdict = False assert verdict == expected, ( f"{dtype} pattern={name} scale={magnitude!r}:" f" check={verdict} oracle={expected}" ) checked += 1 assert checked >= 40, f"battery too small: {checked}" def test_nonpositive_recorded_shape_rejected(tmp_path): """A weight_shape holding non-positive extents must fail.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) victim = "model.layers.0.mlp.gate_proj" shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) tensors[f"{victim}.weight_shape"] = torch.tensor( [FAKE_OUT, 0], dtype=torch.int64 ) save_file(tensors, str(shard)) with pytest.raises(ValueError, match="positive"): _validate(run_dir, model_dir, expected) def test_group_size_and_pack_bits_parameters(tmp_path): """Scale/packed extents follow the group_size and pack_bits arguments.""" expected = gsq_coverage.expected_gsq_modules(_hybrid_config()) run_dir = tmp_path / "run" run_dir.mkdir() by_file: dict[str, dict] = {} for name in expected: idx, kind = _layer_key_parts(name) target = by_file.setdefault(f"model_layers_{idx}_{kind}.safetensors", {}) for suffix, tensor in _packed_tensors( group_size=8, pack_bits=8 ).items(): target[f"{name}.{suffix}"] = tensor for fname, tensors in by_file.items(): save_file(tensors, str(run_dir / fname)) (run_dir / "progress.json").write_text( json.dumps({"last_completed_layer": 1}), encoding="utf-8" ) table = {name: (FAKE_OUT, FAKE_IN) for name in expected} result = gsq_coverage.validate_gsq_checkpoint( run_dir, expected, 1, expected_shapes=table, group_size=8, pack_bits=8 ) assert result.passed with pytest.raises(ValueError, match="weight_scale has shape"): gsq_coverage.validate_gsq_checkpoint( run_dir, expected, 1, expected_shapes=table, group_size=128, pack_bits=8, ) with pytest.raises(ValueError, match="weight_packed has shape"): gsq_coverage.validate_gsq_checkpoint( run_dir, expected, 1, expected_shapes=table, group_size=8, pack_bits=4, ) def _realistic_quantization_config(ignore, *, group_size=128): """Return quantization_config as save_model.inject_quantization_config writes. Mirrors the pinned writer (single ``group_0`` targeting ``Linear`` with 4-bit grouped symmetric int weights) and the real assembled output, so fixtures exercise the verifier's exact-metadata checks. """ return { "config_groups": { "group_0": { "input_activations": None, "output_activations": None, "targets": ["Linear"], "weights": { "num_bits": 4, "strategy": "group", "group_size": group_size, "symmetric": True, "type": "int", }, } }, "format": "pack-quantized", "ignore": list(ignore), "quant_method": "compressed-tensors", "quantization_status": "compressed", } def _write_assembled( out_dir, *, ignore, packed_modules, dense_modules=(), quantization_config=None, ): """Write a fake assembled dir: packed modules plus dense 2-D weights. The config carries realistic pack-quantized metadata by default; pass an explicit ``quantization_config`` dict to exercise metadata rejection paths. """ out_dir.mkdir(parents=True, exist_ok=True) tensors: dict[str, torch.Tensor] = {} for module in packed_modules: for suffix, tensor in _packed_tensors().items(): tensors[f"{module}.{suffix}"] = tensor for module in dense_modules: tensors[f"{module}.weight"] = torch.zeros((FAKE_OUT, FAKE_IN)) shard = "model-00001-of-00001.safetensors" save_file(tensors, str(out_dir / shard)) (out_dir / "model.safetensors.index.json").write_text( json.dumps( { "metadata": {"total_size": 0}, "weight_map": {key: shard for key in tensors}, } ), encoding="utf-8", ) if quantization_config is None: quantization_config = _realistic_quantization_config(ignore) (out_dir / "config.json").write_text( json.dumps( { "model_type": "tiny", "quantization_config": quantization_config, } ), encoding="utf-8", ) return out_dir def test_verify_assembled_checkpoint_accepts_packed_and_ignored_dense( tmp_path, ): """Packed modules pass with scale/shape; ignored dense 2-D pass.""" out = _write_assembled( tmp_path / "assembled", ignore=["re:.*visual.*", "lm_head"], packed_modules=["model.layers.0.mlp.gate_proj"], dense_modules=[ "model.visual.blocks.0.attn.qkv", "lm_head", ], ) summary = gsq_coverage.verify_assembled_checkpoint(out) assert summary["packed_modules"] == ["model.layers.0.mlp.gate_proj"] assert summary["ignored_dense_modules"] == [ "lm_head", "model.visual.blocks.0.attn.qkv", ] def test_verify_assembled_checkpoint_rejects_unignored_dense_visual( tmp_path, ): """A dense visual Linear without an ignore entry fails closed.""" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=["model.layers.0.mlp.gate_proj"], dense_modules=["model.visual.blocks.0.attn.qkv"], ) with pytest.raises(ValueError, match="ignore"): gsq_coverage.verify_assembled_checkpoint(out) out = _write_assembled( tmp_path / "covered", ignore=["re:.*visual.*"], packed_modules=["model.layers.0.mlp.gate_proj"], dense_modules=["model.visual.blocks.0.attn.qkv"], ) assert gsq_coverage.verify_assembled_checkpoint(out)["packed_modules"] def test_verify_assembled_checkpoint_rejects_ignored_packed(tmp_path): """A packed module listed in ignore fails closed.""" out = _write_assembled( tmp_path / "assembled", ignore=["re:.*mlp.*"], packed_modules=["model.layers.0.mlp.gate_proj"], ) with pytest.raises(ValueError, match="ignored"): gsq_coverage.verify_assembled_checkpoint(out) def test_verify_assembled_checkpoint_rejects_packed_without_scale( tmp_path, ): """Packed metadata without scale/shape fails closed.""" out = tmp_path / "assembled" out.mkdir() tensors = { "model.layers.0.mlp.gate_proj.weight_packed": torch.zeros( (FAKE_OUT, FAKE_IN // 8), dtype=torch.int32 ), } save_file(tensors, str(out / "model.safetensors")) (out / "config.json").write_text( json.dumps( { "quantization_config": { "format": "pack-quantized", "ignore": [], "quant_method": "compressed-tensors", } } ), encoding="utf-8", ) with pytest.raises(ValueError, match="weight_scale"): gsq_coverage.verify_assembled_checkpoint(out) def _added_save_model_ignore_patterns(): """Return the re: entries the adapter adds to _build_ignore_list.""" for relative, old, new, _ in source.TEXT_PATCHES: if relative == "save_model.py" and "vision_tower" in old: old_entries = set(re.findall(r'"(re:[^"]*)"', old)) added = [ entry for entry in re.findall(r'"(re:[^"]*)"', new) if entry not in old_entries ] assert added, "visual/mtp ignore patch adds no pattern" return added raise AssertionError("visual/mtp ignore patch is missing") def _qwen_snapshot_dir() -> Path: override = os.environ.get("OBLITERATUS_QWEN_SNAPSHOT") if override: return Path(override) return ( Path.home() / ".cache" / "huggingface" / "hub" / "models--Qwen--Qwen3.5-0.8B" / "snapshots" / "2fc06364715b967f1860aea9cf38778875588b17" ) def _safetensors_header_shapes(shard: Path) -> dict[str, tuple[int, ...]]: """Read ``{tensor name: shape}`` from a safetensors header only.""" with open(shard, "rb") as handle: (size,) = struct.unpack("-of-.safetensors`` as ``save_model.py`` writes) rejects it. """ victim = "model.layers.0.mlp.gate_proj" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=[victim] ) shard = out / "model-00001-of-00001.safetensors" shard.rename(out / "custom.safetensors") _rewrite_assembled_weight_map(out, "custom.safetensors") with pytest.raises(ValueError, match="assembler's shard naming"): gsq_coverage.verify_assembled_checkpoint(out) def test_verify_assembled_refuses_symlink_escaping_output_dir(tmp_path): """A shard symlink resolving outside the output dir fails verification. The index reference itself is a valid assembler shard name, so only resolved containment inside the output directory rejects it. """ victim = "model.layers.0.mlp.gate_proj" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=[victim] ) shard = out / "model-00001-of-00001.safetensors" external = tmp_path / "external" external.mkdir() moved = external / shard.name shard.rename(moved) shard.symlink_to(moved) with pytest.raises(ValueError, match="outside the output dir"): gsq_coverage.verify_assembled_checkpoint(out) def test_verify_assembled_refuses_unindexed_shard_tensor(tmp_path): """A valid trio plus an unindexed NaN weight_zero_point is refused. The extra tensor sits in the referenced shard but is deliberately left out of the index ``weight_map``: the expected packed-set check still matches, yet loaders would read the unindexed tensor, so verification must fail closed on the shard/index mismatch. """ victim = "model.layers.0.mlp.gate_proj" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=[victim] ) assert gsq_coverage.verify_assembled_checkpoint(out)["packed_modules"] == [ victim ] shard = out / "model-00001-of-00001.safetensors" tensors = load_file(str(shard)) bad = torch.zeros((FAKE_OUT, 1), dtype=torch.float32) bad.fill_(float("nan")) tensors[f"{victim}.weight_zero_point"] = bad save_file(tensors, str(shard)) # The index is deliberately not updated: the zero point is in the # shard but unlisted in weight_map for that file. with pytest.raises(ValueError, match="not listed in the index"): gsq_coverage.verify_assembled_checkpoint(out) def test_verify_assembled_refuses_numbered_shards_without_index( tmp_path, ): """Numbered shards without an index fail verification. Transformers loads either a single ``model.safetensors`` or sharded weights via ``model.safetensors.index.json``: numbered shards alone (``model-00001-of-00001.safetensors`` with no index) are not a loadable layout, so the verifier must fail closed on them. """ victim = "model.layers.0.mlp.gate_proj" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=[victim] ) (out / "model.safetensors.index.json").unlink() with pytest.raises(ValueError, match="model.safetensors"): gsq_coverage.verify_assembled_checkpoint(out) def test_verify_assembled_accepts_single_model_safetensors_without_index( tmp_path, ): """A single ``model.safetensors`` with no index verifies its own keys.""" victim = "model.layers.0.mlp.gate_proj" out = tmp_path / "single" out.mkdir() tensors: dict[str, torch.Tensor] = {} for suffix, tensor in _packed_tensors().items(): tensors[f"{victim}.{suffix}"] = tensor save_file(tensors, str(out / "model.safetensors")) (out / "config.json").write_text( json.dumps( { "model_type": "tiny", "quantization_config": _realistic_quantization_config([]), } ), encoding="utf-8", ) summary = gsq_coverage.verify_assembled_checkpoint(out) assert summary["packed_modules"] == [victim] def test_base_index_must_cover_exactly_top_level_shards(tmp_path): """A top-level shard the index does not reference fails validation.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) save_file( {"unrelated.weight": torch.zeros(2)}, str(model_dir / "other.safetensors"), ) (model_dir / "model.safetensors.index.json").write_text( json.dumps( { "metadata": {}, "weight_map": { f"{name}.weight": "model.safetensors" for name in expected }, } ), encoding="utf-8", ) with pytest.raises(ValueError, match="not referenced by the index"): _validate(run_dir, model_dir, expected) def test_base_index_key_must_live_in_mapped_file(tmp_path): """An index entry naming a tensor its file lacks fails validation.""" run_dir, model_dir, expected = _write_valid_run(tmp_path) first, second = expected[0], expected[1] tensors = load_file(str(model_dir / "model.safetensors")) first_tensor = tensors.pop(f"{first}.weight") save_file(tensors, str(model_dir / "model.safetensors")) save_file( {f"{first}.weight": first_tensor}, str(model_dir / "extra.safetensors"), ) weight_map = {f"{name}.weight": "model.safetensors" for name in expected} weight_map[f"{second}.weight"] = "extra.safetensors" (model_dir / "model.safetensors.index.json").write_text( json.dumps({"metadata": {}, "weight_map": weight_map}), encoding="utf-8", ) with pytest.raises(ValueError, match="does not hold"): _validate(run_dir, model_dir, expected) def test_base_index_omitting_a_shard_key_fails(tmp_path): """An index that omits a shard tensor fails, covering every shard. Regression: GSQ training loads only indexed names, so an omitted norm stays on ``meta`` while validation and assembly see it through the shard contents. The ``weight_map`` key set must equal exactly the union of top-level shard keys -- before launch (``run``) and in ``validate``, which both call ``require_base_index_agreement``. """ run_dir, model_dir, expected = _write_valid_run(tmp_path) tensors = load_file(str(model_dir / "model.safetensors")) tensors["model.norm.weight"] = torch.zeros(FAKE_IN) save_file(tensors, str(model_dir / "model.safetensors")) full_map = {f"{name}.weight": "model.safetensors" for name in expected} full_map["model.norm.weight"] = "model.safetensors" index_path = model_dir / "model.safetensors.index.json" index_path.write_text( json.dumps({"metadata": {}, "weight_map": full_map}), encoding="utf-8", ) gsq_coverage.require_base_index_agreement(model_dir) assert _validate(run_dir, model_dir, expected).passed partial_map = dict(full_map) del partial_map["model.norm.weight"] index_path.write_text( json.dumps({"metadata": {}, "weight_map": partial_map}), encoding="utf-8", ) with pytest.raises(ValueError, match="omits"): gsq_coverage.require_base_index_agreement(model_dir) with pytest.raises(ValueError, match="omits"): _validate(run_dir, model_dir, expected) def test_ignore_matching_uses_match_not_search(): """``re:`` ignore entries anchor at the start (compressed-tensors). Mirrors ``compressed_tensors.utils.match.match_name`` (verified against the installed compressed-tensors 0.15.0.1 source): ``re:visual`` must NOT match ``model.visual.blocks.0.attn.qkv`` (``re.match`` anchors at the start), while the generated ``re:.*visual.*`` still matches and plain entries compare exactly. """ module = "model.visual.blocks.0.attn.qkv" assert not gsq_coverage._ignore_entry_matches("re:visual", module) assert not gsq_coverage._ignore_entry_matches("re:visual.*", module) assert gsq_coverage._ignore_entry_matches("re:.*visual.*", module) assert gsq_coverage._ignore_entry_matches("re:model\\.visual.*", module) assert gsq_coverage._ignore_entry_matches(module, module) assert not gsq_coverage._ignore_entry_matches("visual", module) with pytest.raises(ValueError, match="invalid"): gsq_coverage._ignore_entry_matches("re:([", module) def test_assembled_rejects_unanchored_visual_ignore(tmp_path): """An unanchored ``re:visual`` entry does not certify dense vision.""" out = _write_assembled( tmp_path / "unanchored", ignore=["re:visual"], packed_modules=["model.layers.0.mlp.gate_proj"], dense_modules=["model.visual.blocks.0.attn.qkv"], ) with pytest.raises(ValueError, match="ignore"): gsq_coverage.verify_assembled_checkpoint(out) out = _write_assembled( tmp_path / "anchored", ignore=["re:.*visual.*"], packed_modules=["model.layers.0.mlp.gate_proj"], dense_modules=["model.visual.blocks.0.attn.qkv"], ) assert gsq_coverage.verify_assembled_checkpoint(out)["packed_modules"] def test_ignore_matching_covers_class_names(): """Plain ``Linear``/``Module`` entries match projections by class. Mirrors compressed-tensors 0.15.0.1 ``is_match`` (name OR class; verified live against the installed source): every packed projection is a ``torch.nn.Linear`` whose MRO contributes exactly ``Linear`` and ``Module``, so both entries mark it ignored, while ``re:.*Linear`` matches nothing -- regexes never match class names (``":"`` cannot appear in one) and lowercase module names hold no ``Linear`` substring. """ module = "model.layers.0.mlp.gate_proj" assert gsq_coverage._ignore_matches(["Linear"], module) assert gsq_coverage._ignore_matches(["Module"], module) assert not gsq_coverage._ignore_matches(["re:.*Linear"], module) assert gsq_coverage._ignore_matches(["re:.*mlp.*"], module) assert gsq_coverage._ignore_matches([module], module) assert not gsq_coverage._ignore_matches([], module) assert not gsq_coverage._ignore_matches(["lm_head"], module) @pytest.mark.parametrize("entry", ["Linear", "Module"]) def test_verify_assembled_rejects_class_name_ignored_packed( tmp_path, entry ): """``ignore: ["Linear"]`` (or ``["Module"]``) fails the packed check. The loader would skip every packed projection, so the verifier must refuse even though no ignore entry names the module. """ out = _write_assembled( tmp_path / "assembled", ignore=[entry], packed_modules=["model.layers.0.mlp.gate_proj"], ) with pytest.raises(ValueError, match="loaders would skip"): gsq_coverage.verify_assembled_checkpoint(out) def test_verify_assembled_regex_linear_matches_nothing(tmp_path): """``ignore: ["re:.*Linear"]`` covers neither packed nor dense modules. Locks the installed loader semantics: the regex matches no class name and no lowercase module name, so packed projections verify -- while a dense Linear left to such a list still fails closed (the loader would quantize it, since nothing ignores it). """ out = _write_assembled( tmp_path / "packed", ignore=["re:.*Linear"], packed_modules=["model.layers.0.mlp.gate_proj"], ) assert gsq_coverage.verify_assembled_checkpoint(out)[ "packed_modules" ] == ["model.layers.0.mlp.gate_proj"] out = _write_assembled( tmp_path / "dense", ignore=["re:.*Linear"], packed_modules=["model.layers.0.mlp.gate_proj"], dense_modules=["model.layers.0.mlp.down_proj"], ) with pytest.raises(ValueError, match="ignore"): gsq_coverage.verify_assembled_checkpoint(out) # The ignore list pinned ``save_model._build_ignore_list`` returns for a # Qwen3.5 (``qwen3_5*`` model type) config with ``quantize_attention=True`` # and all layers quantized: no attention/partial-run entries, with the # Qwen3.5 shared-expert exclusion. Any new strictness must accept it. _REAL_QWEN35_IGNORE = [ "lm_head", "re:.*embed_tokens.*", "re:.*input_layernorm.*", "re:.*post_attention_layernorm.*", r"re:.*\.norm$", "re:.*visual.*", "re:.*mtp.*", "re:.*shared_expert.*", ] def test_verify_assembled_accepts_real_save_model_ignore_list(tmp_path): """The real Qwen3.5 ignore list verifies: dense covered, packed kept.""" out = _write_assembled( tmp_path / "assembled", ignore=_REAL_QWEN35_IGNORE, packed_modules=[ "model.layers.0.mlp.gate_proj", "model.layers.0.mlp.down_proj", ], dense_modules=[ "lm_head", "model.embed_tokens", "model.layers.0.input_layernorm", "model.layers.0.post_attention_layernorm", "model.norm", "model.visual.blocks.0.attn.qkv", "mtp.fc", "model.layers.0.mlp.shared_expert.gate_proj", ], ) summary = gsq_coverage.verify_assembled_checkpoint(out) assert summary["packed_modules"] == [ "model.layers.0.mlp.down_proj", "model.layers.0.mlp.gate_proj", ] assert sorted(summary["ignored_dense_modules"]) == [ "lm_head", "model.embed_tokens", "model.layers.0.input_layernorm", "model.layers.0.mlp.shared_expert.gate_proj", "model.layers.0.post_attention_layernorm", "model.norm", "model.visual.blocks.0.attn.qkv", "mtp.fc", ] def _rewrite_assembled_scale(out_dir, module, scale): """Replace one assembled-shard scale tensor, keeping the rest.""" index_path = out_dir / "model.safetensors.index.json" weight_map = json.loads(index_path.read_text(encoding="utf-8"))[ "weight_map" ] shard = out_dir / weight_map[f"{module}.weight_scale"] tensors = load_file(str(shard)) tensors[f"{module}.weight_scale"] = scale save_file(tensors, str(shard)) @pytest.mark.parametrize( "dtype", [torch.float32, torch.float16, torch.bfloat16] ) def test_verify_assembled_rejects_decode_overflow(tmp_path, dtype): """Assembled finite max scales that decode to ``-inf`` must fail.""" victim = "model.layers.0.mlp.gate_proj" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=[victim], ) _rewrite_assembled_scale( out, victim, torch.full((FAKE_OUT, 1), torch.finfo(dtype).max, dtype=dtype) ) with pytest.raises(ValueError, match="decodes to non-finite"): gsq_coverage.verify_assembled_checkpoint(out) @pytest.mark.parametrize( "dtype", [torch.float32, torch.float16, torch.bfloat16] ) def test_verify_assembled_accepts_decode_near_limit(tmp_path, dtype): """An assembled max/8 scale times unpacked ``-8`` is finite: accept.""" victim = "model.layers.0.mlp.gate_proj" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=[victim], ) _rewrite_assembled_scale( out, victim, torch.full( (FAKE_OUT, 1), torch.finfo(dtype).max / 8, dtype=dtype ), ) assert gsq_coverage.verify_assembled_checkpoint(out)[ "packed_modules" ] == [victim] def test_verify_assembled_rejects_load_dtype_cast_overflow(tmp_path): """Assembled fp32 1e10 scales must fail an fp16 load dtype. The load dtype binds three ways -- explicitly, from the bound base weights, and from the assembled config's own ``torch_dtype`` -- and each must refuse scales that overflow it, since the loader casts scales before decoding. """ victim = "model.layers.0.mlp.gate_proj" big = torch.full((FAKE_OUT, 1), 1e10, dtype=torch.float32) out = _write_assembled( tmp_path / "explicit", ignore=[], packed_modules=[victim] ) _rewrite_assembled_scale(out, victim, big) assert gsq_coverage.verify_assembled_checkpoint(out)["packed_modules"] with pytest.raises(ValueError, match="not finite after casting"): gsq_coverage.verify_assembled_checkpoint( out, load_dtype=torch.float16 ) base = tmp_path / "fp16-base" base.mkdir() save_file( {f"{victim}.weight": torch.zeros((FAKE_OUT, FAKE_IN), dtype=torch.float16)}, str(base / "model.safetensors"), ) with pytest.raises(ValueError, match="not finite after casting"): gsq_coverage.verify_assembled_checkpoint(out, base_model_dir=base) out_config = _write_assembled( tmp_path / "declared", ignore=[], packed_modules=[victim] ) _rewrite_assembled_scale(out_config, victim, big.clone()) config_path = out_config / "config.json" config = json.loads(config_path.read_text(encoding="utf-8")) config["torch_dtype"] = "float16" config_path.write_text(json.dumps(config), encoding="utf-8") with pytest.raises(ValueError, match="not finite after casting"): gsq_coverage.verify_assembled_checkpoint(out_config) def test_verify_assembled_accepts_bf16_near_limit_scales(tmp_path): """BF16 near-limit scales validate under a bf16 load (GPU-run shape). The pinned saver writes scales in the run dtype, so a real bf16 run ships bf16 scales that load as bf16: fp32 ``1e30`` (finite in bf16, decoding finitely in bf16) must pass whether the load dtype binds explicitly or via the assembled ``torch_dtype`` declaration. """ victim = "model.layers.0.mlp.gate_proj" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=[victim] ) _rewrite_assembled_scale( out, victim, torch.full((FAKE_OUT, 1), 1e30, dtype=torch.float32) ) assert gsq_coverage.verify_assembled_checkpoint( out, load_dtype=torch.bfloat16 )["packed_modules"] == [victim] config_path = out / "config.json" config = json.loads(config_path.read_text(encoding="utf-8")) config["torch_dtype"] = "bfloat16" config_path.write_text(json.dumps(config), encoding="utf-8") assert gsq_coverage.verify_assembled_checkpoint(out)[ "packed_modules" ] == [victim] def test_verify_assembled_rejects_indivisible_group_size(tmp_path): """An assembled [8,16] trio with group_size 128 must fail to verify. The trio is internally self-consistent (packed ``[8,2]``, scale ``[8,1]``, shape ``[8,16]``), but group quantization has no padding path -- the loader apportions columns with ``unflatten(-1, (num_groups, group_size))`` -- so the projection could never load. """ victim = "model.layers.0.mlp.gate_proj" out = _write_assembled( tmp_path / "assembled", ignore=[], packed_modules=[victim] ) shard = out / "model-00001-of-00001.safetensors" tensors = load_file(str(shard)) tensors[f"{victim}.weight_packed"] = torch.zeros((8, 2), dtype=torch.int32) tensors[f"{victim}.weight_scale"] = torch.zeros((8, 1), dtype=torch.float32) tensors[f"{victim}.weight_shape"] = torch.tensor([8, 16], dtype=torch.int64) save_file(tensors, str(shard)) with pytest.raises(ValueError, match="not divisible by"): gsq_coverage.verify_assembled_checkpoint(out) _TEMPLATE_TOP_CONTENTS = { "chat_template.jinja": "hello {{ messages }}", "chat_template.json": '{"chat_template": true}', "added_tokens.json": '{"added_tokens": []}', "processor_config.json": '{"processor": true}', "video_preprocessor_config.json": '{"video": true}', } _TEMPLATE_SUB_CONTENTS = { "default.jinja": "default {{ messages }}", "tool_call.jinja": "tool {{ messages }}", } def _write_template_base(base: Path) -> None: base.mkdir(parents=True, exist_ok=True) for name, content in _TEMPLATE_TOP_CONTENTS.items(): (base / name).write_text(content, encoding="utf-8") sub = base / "chat_templates" sub.mkdir(parents=True, exist_ok=True) for name, content in _TEMPLATE_SUB_CONTENTS.items(): (sub / name).write_text(content, encoding="utf-8") def _copy_templates_to_assembled(base: Path, out: Path) -> None: for name in _TEMPLATE_TOP_CONTENTS: (out / name).write_bytes((base / name).read_bytes()) (out / "chat_templates").mkdir(parents=True, exist_ok=True) for name in _TEMPLATE_SUB_CONTENTS: (out / "chat_templates" / name).write_bytes( (base / "chat_templates" / name).read_bytes() ) def test_template_files_present_in_base_lists_each_kind(tmp_path): base = tmp_path / "base" _write_template_base(base) (base / "chat_templates" / "notes.txt").write_text( "not a template", encoding="utf-8" ) keys = gsq_coverage._template_files_present_in_base(base) for name in _TEMPLATE_TOP_CONTENTS: assert name in keys for name in _TEMPLATE_SUB_CONTENTS: assert f"chat_templates/{name}" in keys assert "chat_templates/notes.txt" not in keys def test_require_copied_template_files_accepts_matching(tmp_path): base = tmp_path / "base" out = tmp_path / "out" _write_template_base(base) out.mkdir() _copy_templates_to_assembled(base, out) gsq_coverage._require_copied_template_files(base, out) def test_require_copied_template_files_rejects_missing(tmp_path): base = tmp_path / "base" out = tmp_path / "out" _write_template_base(base) out.mkdir() with pytest.raises(ValueError, match="missing base template file"): gsq_coverage._require_copied_template_files(base, out) def test_require_copied_template_files_rejects_altered(tmp_path): base = tmp_path / "base" out = tmp_path / "out" _write_template_base(base) out.mkdir() _copy_templates_to_assembled(base, out) (out / "chat_template.jinja").write_text( "altered", encoding="utf-8" ) with pytest.raises(ValueError, match="differs"): gsq_coverage._require_copied_template_files(base, out) def test_verify_assembled_rejects_missing_template_with_base(tmp_path): config = _hybrid_config() expected = gsq_coverage.expected_gsq_modules(config) base = _write_base_with_embed(tmp_path / "base", expected) _write_template_base(base) out = _write_assembled( tmp_path / "assembled", ignore=["re:.*embed_tokens.*"], packed_modules=list(expected), dense_modules=["model.embed_tokens"], ) with pytest.raises(ValueError, match="template"): gsq_coverage.verify_assembled_checkpoint(out, base_model_dir=base) def test_verify_assembled_accepts_matching_templates_with_base(tmp_path): config = _hybrid_config() expected = gsq_coverage.expected_gsq_modules(config) base = _write_base_with_embed(tmp_path / "base", expected) _write_template_base(base) out = _write_assembled( tmp_path / "assembled", ignore=["re:.*embed_tokens.*"], packed_modules=list(expected), dense_modules=["model.embed_tokens"], ) _copy_templates_to_assembled(base, out) summary = gsq_coverage.verify_assembled_checkpoint( out, base_model_dir=base ) assert len(summary["packed_modules"]) == len(expected) def test_non_projection_load_dtype_cast_overflow_rejected(tmp_path): """A stored-finite overwrite that overflows the load dtype must fail. An fp32 layernorm of ``1e10`` is finite stored but ``+inf`` in fp16: the loader casts retained weights to the load dtype, so assembly would overwrite a valid base norm with weights that load as inf. """ config = _hybrid_config() expected = gsq_coverage.expected_gsq_modules(config) run_dir = tmp_path / "run" run_dir.mkdir() model_dir = tmp_path / "model" _write_checkpoint(run_dir, expected) _write_base_with_norm(model_dir, expected, norm_shape=128) shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) tensors["model.layers.0.input_layernorm.weight"] = torch.full( (128,), 1e10, dtype=torch.float32 ) save_file(tensors, str(shard)) # Stored-dtype certification still passes: the norm is finite fp32. assert _validate(run_dir, model_dir, expected).passed with pytest.raises(ValueError, match="not finite after casting"): _validate(run_dir, model_dir, expected, load_dtype=torch.float16) def test_non_projection_load_dtype_near_limit_accepted(tmp_path): """A bf16-finite overwrite still validates under a bf16 load dtype.""" config = _hybrid_config() expected = gsq_coverage.expected_gsq_modules(config) run_dir = tmp_path / "run" run_dir.mkdir() model_dir = tmp_path / "model" _write_checkpoint(run_dir, expected) _write_base_with_norm(model_dir, expected, norm_shape=128) shard = run_dir / "model_layers_0_mlp.safetensors" tensors = load_file(str(shard)) tensors["model.layers.0.input_layernorm.weight"] = torch.full( (128,), 1e30, dtype=torch.float32 ) save_file(tensors, str(shard)) assert _validate( run_dir, model_dir, expected, load_dtype="bfloat16" ).passed def test_assembled_retained_load_dtype_cast_overflow_fails(tmp_path): """A stored-finite retained norm that overflows the load dtype fails.""" out = tmp_path / "assembled" base = tmp_path / "base" big = torch.full((128,), 1e10, dtype=torch.float32) expected = _write_assembled_with_norm( out, 128, big, ignore=["re:.*input_layernorm.*"] ) _write_base_with_norm(base, expected, norm_shape=128) # Stored-dtype certification still passes: the norm is finite fp32. gsq_coverage.verify_assembled_checkpoint(out, base_model_dir=base) with pytest.raises(ValueError, match="not finite after casting"): gsq_coverage.verify_assembled_checkpoint( out, base_model_dir=base, load_dtype=torch.float16 ) def test_assembled_retained_config_dtype_cast_overflow_fails(tmp_path): """The assembled config's own load dtype also binds retained weights. An fp32 ``1e10`` retained norm is finite stored, but the assembled ``config.json`` declaring ``torch_dtype`` fp16 means the loader casts it to fp16 (``+inf``) on load -- so verification must refuse even without an explicit ``load_dtype``. """ out = tmp_path / "assembled" base = tmp_path / "base" big = torch.full((128,), 1e10, dtype=torch.float32) expected = _write_assembled_with_norm( out, 128, big, ignore=["re:.*input_layernorm.*"] ) _write_base_with_norm(base, expected, norm_shape=128) config_path = out / "config.json" config = json.loads(config_path.read_text(encoding="utf-8")) config["torch_dtype"] = "float16" config_path.write_text(json.dumps(config), encoding="utf-8") with pytest.raises(ValueError, match="not finite after casting"): gsq_coverage.verify_assembled_checkpoint(out, base_model_dir=base) def test_load_dtype_cast_check_covers_chunks_beyond_the_first(): """The chunked cast check must not stop at the first chunk. A multi-chunk fp32 tensor that is finite everywhere except an overflowing ``1e10`` tail element must fail under fp16, while the all-finite prefix alone passes. """ key, what = "model.layers.0.input_layernorm.weight", "run tensor" size = gsq_coverage._LOAD_DTYPE_CHECK_CHUNK_ELEMS + 100 prefix = torch.ones(size, dtype=torch.float32) gsq_coverage._require_finite_after_cast_to_load_dtypes( prefix, key=key, what=what, load_dtypes=(torch.float16,) ) prefix[0] = 1e10 with pytest.raises(ValueError, match="not finite after casting"): gsq_coverage._require_finite_after_cast_to_load_dtypes( prefix, key=key, what=what, load_dtypes=(torch.float16,) ) prefix[0] = 1.0 prefix[-1] = 1e10 with pytest.raises(ValueError, match="not finite after casting"): gsq_coverage._require_finite_after_cast_to_load_dtypes( prefix, key=key, what=what, load_dtypes=(torch.float16,) )