Gemma4 Moe and Prefill changes - #1249
Open
tchawada wants to merge 4 commits into
Open
Conversation
Optimized the moe MLP block by processing only the top-K selected experts for each token. The implementation gathers expert-specific projection weights (gate, up, and down), performs batched matrix multiplications across all (token, expert) pairs, applies the gated activation (act_fn(gate) * up), and combines expert outputs using router-provided top-K weights. Changes are suggested by Mrunal Kshirsagar. Signed-off-by: Tanisha Chawada <tchawada@qti.qualcomm.com> Co-authored-by: Hem Agnihotri <hemagnih@qti.qualcomm.com> Signed-off-by: Tanisha Chawada <tchawada@qti.qualcomm.com>
Enable the compute-context-length (CCL) feature for the Gemma4 and Qwen3.5 image-text-to-text model families and fix CCL specialization gating so it also works for disaggregated (separate prefill/decode) serving. Gemma4 (modeling_gemma4.py): - Thread comp_ctx_lengths through the text model, decoder layer, and attention so the exported ONNX actually consumes it: slice the attention mask to the CCL width and pass "CCL" into cache_kwargs (mirrors Gemma3). - Fix the dummy comp_ctx_lengths input dtype (int8 -> int64) so the traced ONNX input matches the int64 runtime buffer. - Split the "prefill and decode" specialization gate into an OR so CCL is honored when only one of the prefill/decode lists is provided. Qwen3.5 (modeling_qwen3_5.py, modeling_qwen3_5_moe.py): - Fix the dummy comp_ctx_lengths input dtype (int8 -> int64). - Add the missing comp_ctx_lengths pass-through on the MoE full-model forwards (QEffQwen3_5MoeModel, QEffQwen3_5MoeForConditionalGeneration) to match the non-MoE variant. - Allow decode-only CCL specialization (gate -> OR, guard prefill loop). Qwen3.5-VL (modeling_qwen3_vl.py, modeling_qwen3_vl_moe.py): - Allow decode-only CCL specialization and guard both prefill/decode loops with `or []` to avoid a TypeError when one list is None (disaggregated serving). Examples (gemma4_example.py, gemma4_utils.py): - Carry comp_ctx_lengths_prefill/decode through build_compile_kwargs and document CCL activation in the example. Important points to consider: - In this support, for gemma4 and qwen3_5 models the use_onnx_subfunctions should be False to have CCL feature actually work for these models otherwise it will be compiled but CCL won't be effective. Related ongoing work is in progress to solve the source of this issue. - I've already raised the following Jira ticket for this issue. Please track the changes there: https://jira-dc.qualcomm.com/jira/browse/QRANIUMSW-63142 --------- Signed-off-by: Vahid Janfaza <vjanfaza@qti.qualcomm.com> Signed-off-by: Tanisha Chawada <tchawada@qti.qualcomm.com>
Optimized the moe MLP block by processing only the top-K selected experts for each token. The implementation gathers expert-specific projection weights (gate, up, and down), performs batched matrix multiplications across all (token, expert) pairs, applies the gated activation (act_fn(gate) * up), and combines expert outputs using router-provided top-K weights. NSP-parallel expert-blocked dispatch to the chunked prefill MoE path, replacing the sequential per-expert loop with a batched packed-prefix approach. --------- Signed-off-by: Tanisha Chawada <tchawada@qti.qualcomm.com> Signed-off-by: Vahid Janfaza <vjanfaza@qti.qualcomm.com> Co-authored-by: Vahid Janfaza <vjanfaza@qti.qualcomm.com> Co-authored-by: Hem Agnihotri <hemagnih@qti.qualcomm.com> Signed-off-by: Tanisha Chawada <tchawada@qti.qualcomm.com>
Signed-off-by: Tanisha Chawada <tchawada@qti.qualcomm.com>
tchawada
force-pushed
the
gemma4_release
branch
from
August 7, 2026 08:32
7518c1d to
3b81f94
Compare
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.
Optimized the moe MLP block by processing only the top-K selected experts for each token. The implementation gathers expert-specific projection weights (gate, up, and down), performs batched matrix multiplications across all (token, expert) pairs, applies the gated activation (act_fn(gate) * up), and combines expert outputs using router-provided top-K weights.
NSP-parallel expert-blocked dispatch to the chunked prefill MoE path, replacing the sequential per-expert loop with a batched packed-prefix approach.