Repository navigation
feat(gemma4): add unified Gemma 4 (E2B/E4B/26B-A4B MoE/31B) multimodal inference support - #633
Draft
yanchaomei wants to merge 1 commit into
Draft
yanchaomei wants to merge 1 commit into
yanchaomei wants to merge 1 commit into
Conversation
…l inference support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Config-driven C++ implementation of the Gemma 4 model family (
E2B,E4B,26B-A4BMoE,31B) insidecsrc/models/gemma4/, strictly followingMODELS.mdwith 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
csrc/models/gemma4/):gemma4_config.{hpp,cpp}: unified config parser forgemma4&gemma4_textcovering all 4 variants (E2B,E4B,26B-A4B,31B).gemma4_rope.{hpp,cpp}: dual RoPE supporting standardsliding_attentionRoPE (head_dim=256, theta=10000) and exact HF proportionalfull_attentionRoPE (global_head_dim=512, theta=1000000, partial_rotary_factor=0.25).gemma4_allocate_kv_cache_tensors.{hpp,cpp}: heterogeneous per-layer KV cache allocator (allocateshead_dimvsglobal_head_dimper layer and skips allocation for KV-shared layers).gemma4_attention.{hpp,cpp}:q_norm,k_norm, unscaledv_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 (withuse_double_wide_mlpon KV-shared layers) + parallel 128-expert Top-K MoE block (Gemma4TextRouter+ TP-sharded 3DGemma4TextExpertswithalpha-foldeddense_projectionandinfinicclSumAllReduce).gemma4_decoder_layer.{hpp,cpp}&gemma4_for_causal_lm.{hpp,cpp}: 4 pre/post RMSNorms, Per-Layer Embedding (PLE) gating/projection,layer_scalar, exactfinal_logit_softcapping, and multimodal placeholder masking topad_token_idbefore embedding/PLE lookup.gemma4_vision.{hpp,cpp}:Gemma4ClippableLinear(exact GPU clamp via slot-0Add/Mul/Relu), 2D axial RoPE ViT encoder, FP32k×kspatial average pooling (sqrt(H)scaling +standardize), andGemma4MultimodalEmbedder.gemma4_audio.{hpp,cpp}: 2×Conv2dsubsampling, 12-layer Conformer (feed_forward1/2, chunked relative attention with softcap 50.0, causal depthwiseLightConv1d), andoutput_proj.gemma4_processor.py+test_gemma4_parity.py):_remap_gemma4inmodeling_utils.py(KV-shared layer key filtering, 0D scalar clamping reshape, contiguous per-expert 3Dgate_up_projsplit).Gemma4Processor(gemma4/gemma4_text) +audio_urlsupport inmultimodal.pyand--audioCLI option inexamples/test_infer.py.Verification (NVIDIA A40, C550-Parity Operator Build — Zero FlashAttention)
All tests were run against HuggingFace
Gemma4ForConditionalGeneration(greedy decoding, BF16) using anInfiniOpsbuild matchingdocs/infiniops_ops_c500_basic.json(FlashAttention disabled):INFINI_OPS_TRACE_CALLS=1)google/gemma-4-E2B-it— Text Chat / Mathgoogle/gemma-4-E2B-it— Long Context (686 > 512 SW)ALPHA-7392)google/gemma-4-E2B-it— Image (16L ViT + 2D Axial RoPE)google/gemma-4-E2B-it— Audio (12L Conformer)google/gemma-4-E2B-it— Multi-GPU CLI391)google/gemma-4-26B-A4B-it(128E MoE + 27L ViT) — Text / Long (>1024 SW) / Image (bidir=vision)moe_26b_miniTP=1 & TP=2scripts/format.py --check,clang-format 21.1.8,ruff 0.15.20)