Skip to content

Gemma4 Moe and Prefill changes - #1249

Open
tchawada wants to merge 4 commits into
quic:release/v1.22.0from
tchawada:gemma4_release
Open

Gemma4 Moe and Prefill changes#1249
tchawada wants to merge 4 commits into
quic:release/v1.22.0from
tchawada:gemma4_release

Conversation

@tchawada

@tchawada tchawada commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

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.

tchawada and others added 4 commits August 7, 2026 14:02
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>
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.

2 participants