Skip to content

Add Gemma-4 (Block-AP) support - #1

Open
icnatspell wants to merge 1 commit into
mainfrom
gemma4-integration
Open

Add Gemma-4 (Block-AP) support#1
icnatspell wants to merge 1 commit into
mainfrom
gemma4-integration

Conversation

@icnatspell

Copy link
Copy Markdown
Owner

Summary

Adds Block-AP quantization-aware training for google/gemma-4-E2B-it (Gemma-3n / MatFormer lineage: per-layer embeddings, KV-sharing, alternating sliding/global attention, multimodal wrapper). Model-specific logic is isolated in a Gemma4Adapter so quantize/block_ap.py stays model-agnostic — supporting another model is a new adapter, not a fork.

Result

RTX 3080 (10GB), w4g128, C4 calib+eval: fp16 109.09 → w4 109.59 (+0.5% PPL) — near-lossless 4-bit, ~6GB peak VRAM, ~3s/block.

Changes

  • quantize/model_adapters.pyBlockAPAdapter base + Gemma4Adapter (PLE with per-batch cache, KV producer/consumer wiring, static-kwarg capture).
  • quantize/block_ap.py — adapter-driven; follows the model's loaded dtype (Gemma needs bf16 — fp16 overflows its activations), device-selectable (cuda/cpu).
  • run_gemma4_w4.py — fp16 vs w4 PPL over BOS-prefixed deterministic windows; two eval backends (whole-model CPU, low-mem block-walk GPU validated to 0.066%); RTN ablation via --epochs 0.
  • datautils_block.py — stream C4 (new datasets lib can't load it by single-name id; full-shard download OOMs).
  • GEMMA4_QAT.md + README section + uv install instructions; pyproject.toml/uv.lock; smoke test.

Notes / gotchas baked in

  • Load bf16, not fp16 (fp16 → garbage PPL ~1e4).
  • BOS is mandatory — tokenizer has add_bos_token=False and the model is very BOS-sensitive (no-BOS PPL ~1000x inflated).
  • High absolute PPL (~60-95) is expected for an -it model on raw corpora, not a bug; only the fp16→w4 delta measures quantization quality.

Vision/audio encoders left in fp16; only the text decoder is quantized.

🤖 Generated with Claude Code

Quantize google/gemma-4-E2B-it (Gemma-3n/MatFormer: per-layer embeddings,
KV-sharing, alternating sliding/global attention, multimodal wrapper) via
Block-AP. Model-specific logic is isolated in a Gemma4Adapter so block_ap.py
stays model-agnostic.

- quantize/model_adapters.py: BlockAPAdapter base + Gemma4Adapter (PLE with
  per-batch cache, KV producer/consumer wiring, static-kwarg capture).
- quantize/block_ap.py: adapter-driven; follows the model's dtype (Gemma needs
  bf16 — fp16 overflows), device-selectable (cuda/cpu).
- run_gemma4_w4.py: fp16 vs w4 PPL with BOS-prefixed deterministic windows; two
  eval backends (whole-model CPU, low-mem block-walk GPU); RTN ablation via
  --epochs 0.
- datautils_block.py: stream C4 (new datasets lib can't load it by single-name
  id; full-shard download OOMs).
- GEMMA4_QAT.md + README section; pyproject.toml/uv.lock; smoke test.

Result (RTX 3080, w4g128, C4): fp16 109.09 -> w4 109.59 (+0.5% PPL), ~6GB VRAM.

Co-Authored-By: Claude Opus 4.8 <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