Skip to content

feat(tools): canonical Gemma 4 Unified .tern-model PPL loader - #59

Open
synapticode-ai wants to merge 1 commit into
mainfrom
feat/gemma4-unified-ppl-loader
Open

feat(tools): canonical Gemma 4 Unified .tern-model PPL loader#59
synapticode-ai wants to merge 1 commit into
mainfrom
feat/gemma4-unified-ppl-loader

Conversation

@synapticode-ai

Copy link
Copy Markdown
Collaborator

What

Adds tools/gemma4_unified_ppl.py — the canonical text-only WikiText-2 PPL path for Gemma 4 Unified (gemma4_unified / Gemma4UnifiedForConditionalGeneration) .tern-model artefacts.

Why

tern_ppl_bench --tern-model-path drives TernModelReader.load_packed_model, whose module-tree walker raises on the Unified manifest:

  • text-tower tensors are stored under logical Gemma names (model.layers.*, model.embed_tokens.*, model.norm.*) while transformers 5.10 nests the decoder under model.language_model.*;
  • the FP16-retained inline multimodal projectors (embed_vision / embed_audio / vision_embedder) have no submodule when the checkpoint is loaded as a text AutoModelForCausalLM, and are irrelevant to a text-only forward.

How

A tolerant streaming overlay:

  1. bridges logical → transformers-5.10 names via the existing GEMMA4_MULTIMODAL_TRANSFORMERS_5_5 preset;
  2. places reconstructed weights by flat state-dict key, one layer at a time (bounded memory — no full second state-dict);
  3. skips text-irrelevant multimodal keys absent from the text model, while hard-failing if any ternary/INT4 entry fails to place (a silent decoder-tower miss would corrupt the measurement);
  4. computes PPL with the harness's canonical R7-A evaluate_ppl — methodology-identical to the FP baseline.

Validation

google/gemma-4-12B base (Q15 quality leg): BF16 PPL 8.4638 → t0.7 ternary 1.44e9 (overlay placed=667 / fail=0, only the 10 vision projectors skipped). Evidence on ecc-ternary results branch (compression-quality/GEMMA4_12B_BASE_QUALITY_20260610T235557Z).

Tests

tests/test_gemma4_unified_ppl_loader.py — name-bridge + strict-quant gate (placement, multimodal skip, missing-quant abort, shape mismatch) with lightweight fakes, no model download. 5/5 pass locally.

🤖 Generated with Claude Code

The stock tern_ppl_bench --tern-model-path path drives
TernModelReader.load_packed_model, whose module-tree walker raises on the
Gemma 4 Unified manifest: text-tower tensors are stored under logical Gemma
names (model.layers.*) while transformers 5.10 nests the decoder under
model.language_model.*, and the FP16 inline multimodal projectors
(embed_vision/embed_audio/vision_embedder) have no submodule on a text
AutoModelForCausalLM.

This adds tools/gemma4_unified_ppl.py: a tolerant streaming overlay that
(1) bridges logical names to the transformers-5.10 tree via the canonical
GEMMA4_MULTIMODAL_TRANSFORMERS_5_5 preset, (2) places reconstructed weights
by flat state-dict key one layer at a time (bounded memory), (3) skips
text-irrelevant multimodal keys while HARD-FAILING if any ternary/INT4 entry
fails to place, then (4) computes PPL with the harness's canonical R7-A
evaluate_ppl so the number is methodology-identical to the FP baseline.

Validated on google/gemma-4-12B base (Q15): BF16 8.4638 -> t0.7 ternary
1.44e9, overlay placed=667/fail=0 (only 10 vision projectors skipped).

Adds tests/test_gemma4_unified_ppl_loader.py — name-bridge + strict-quant
gate (placement, multimodal skip, missing-quant abort, shape mismatch),
lightweight fakes, no model download. 5/5 pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant