Skip to content

feat(gemma4): add unified Gemma 4 (E2B/E4B/26B-A4B MoE/31B) multimodal inference support - #633

Draft
yanchaomei wants to merge 1 commit into
InfiniTensor:mainfrom
yanchaomei:feat/gemma4-support
Draft

yanchaomei wants to merge 1 commit into
InfiniTensor:mainfrom
yanchaomei:feat/gemma4-support

Conversation

@yanchaomei

Copy link
Copy Markdown

Summary

Config-driven C++ implementation of the Gemma 4 model family (E2B, E4B, 26B-A4B MoE, 31B) inside csrc/models/gemma4/, strictly following MODELS.md with zero changes to framework C++ files and zero CPU fallback under the MetaX C550 slot-0 operator set (docs/infiniops_ops_c500_basic.json).

Architecture & Key Features

  • Self-contained C++ model (csrc/models/gemma4/):
    • gemma4_config.{hpp,cpp}: unified config parser for gemma4 & gemma4_text covering all 4 variants (E2B, E4B, 26B-A4B, 31B).
    • gemma4_rope.{hpp,cpp}: dual RoPE supporting standard sliding_attention RoPE (head_dim=256, theta=10000) and exact HF proportional full_attention RoPE (global_head_dim=512, theta=1000000, partial_rotary_factor=0.25).
    • gemma4_allocate_kv_cache_tensors.{hpp,cpp}: heterogeneous per-layer KV cache allocator (allocates head_dim vs global_head_dim per layer and skips allocation for KV-shared layers).
    • gemma4_attention.{hpp,cpp}: q_norm, k_norm, unscaled v_norm, scaling=1.0, attention_k_eq_v, KV cache sharing, exact sliding window on prefill & decode, bidirectional vision-block masking (use_bidirectional_attention="vision" for 26B-A4B/31B), TP KV-head replication (total_kv_heads < tp_size), and zero FlashAttention dependency on the static path (C550-compatible) plus paged-attention path.
    • gemma4_mlp.{hpp,cpp} & gemma4_moe.{hpp,cpp}: GeGLU dense MLP (with use_double_wide_mlp on KV-shared layers) + parallel 128-expert Top-K MoE block (Gemma4TextRouter + TP-sharded 3D Gemma4TextExperts with alpha-folded dense_projection and infinicclSum AllReduce).
    • gemma4_decoder_layer.{hpp,cpp} & gemma4_for_causal_lm.{hpp,cpp}: 4 pre/post RMSNorms, Per-Layer Embedding (PLE) gating/projection, layer_scalar, exact final_logit_softcapping, and multimodal placeholder masking to pad_token_id before embedding/PLE lookup.
    • gemma4_vision.{hpp,cpp}: Gemma4ClippableLinear (exact GPU clamp via slot-0 Add/Mul/Relu), 2D axial RoPE ViT encoder, FP32 k×k spatial average pooling (sqrt(H) scaling + standardize), and Gemma4MultimodalEmbedder.
    • gemma4_audio.{hpp,cpp}: 2× Conv2d subsampling, 12-layer Conformer (feed_forward1/2, chunked relative attention with softcap 50.0, causal depthwise LightConv1d), and output_proj.
  • Minimal Python glue (6 modified files + gemma4_processor.py + test_gemma4_parity.py):
    • _remap_gemma4 in modeling_utils.py (KV-shared layer key filtering, 0D scalar clamping reshape, contiguous per-expert 3D gate_up_proj split).
    • Gemma4Processor (gemma4 / gemma4_text) + audio_url support in multimodal.py and --audio CLI option in examples/test_infer.py.

Verification (NVIDIA A40, C550-Parity Operator Build — Zero FlashAttention)

All tests were run against HuggingFace Gemma4ForConditionalGeneration (greedy decoding, BF16) using an InfiniOps build matching docs/infiniops_ops_c500_basic.json (FlashAttention disabled):

Model / Suite Hardware / TP Result Throughput / Latency CPU Fallback (INFINI_OPS_TRACE_CALLS=1)
google/gemma-4-E2B-it — Text Chat / Math 1× A40 (TP=1) 100% exact token parity 24 tok in 481 ms / 79.38 tok/s (128 tok) 0 CPU ops
google/gemma-4-E2B-it — Long Context (686 > 512 SW) 1× A40 (TP=1) 100% exact token parity (ALPHA-7392) 7 tok in 160 ms 0 CPU ops
google/gemma-4-E2B-it — Image (16L ViT + 2D Axial RoPE) 1× A40 (TP=1) 100% exact token parity 9 tok in 362 ms 15,155 GPU / 0 CPU ops
google/gemma-4-E2B-it — Audio (12L Conformer) 1× A40 (TP=1) 100% exact token parity 7 tok in 141 ms 19,046 GPU / 0 CPU ops
google/gemma-4-E2B-it — Multi-GPU CLI 2× A40 (TP=2) PASS (391) — 0 CPU ops
google/gemma-4-26B-A4B-it (128E MoE + 27L ViT) — Text / Long (>1024 SW) / Image (bidir=vision) 2× A40 (TP=2) 4/4 100% exact token parity 31.34 tok/s (64 tok; 2.52× faster than HF on chat) 262,568 GPU / 0 CPU ops
Synthetic moe_26b_mini TP=1 & TP=2 1–2× A40 2/2 100% exact parity — 0 CPU ops
InfiniLM Host & Native Unit Tests CPU / CUDA 9 + 206 host tests, 4/4 native C++ tests PASS — —
Format Check (scripts/format.py --check, clang-format 21.1.8, ruff 0.15.20) Local PASS — —

Draft status: Initial CUDA + C550-parity verified implementation for 2026 Autumn Qiyuan AI Contest (Track 3 Problem 1 — Gemma 4, solo team 启忆智存). MetaX C550 hardware profiling & further throughput optimizations will follow once C550 compute access is provisioned.

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