docs: update skills with proven Gemma 4 patterns (MoE, Any-to-Any, KV sharing) - #140
Merged
Conversation
Document the proven pattern from Gemma 4 implementation (PR #134 GQA pattern applied to MoE): EP capability check, full pre-topk router_probs requirement, CastLike after MoE output to preserve dtype, preprocess_weights stacking, and loop-fallback when EP doesn't support fused MoE. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Document the Gemma 4 Any-to-Any task structure (decoder + vision + audio + embedding), tier split between small Any-to-Any (E2B/E4B) and large Image-Text-to-Text (26B/31B), audio encoder wiring, embedding model fusion, and task class tier detection pattern. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Document the proven pattern from Gemma 4: shared layers flag, absent k/v proj weights, shared_kv_states dict wiring through the forward loop, KV cache size = num_hidden_layers - num_kv_shared_layers, and the past_key_values expansion trick for non-shared-layer slot alignment. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Performance Comparison
|
Fix two issues in the adding-a-new-model KV sharing section: 1. Add provides_shared_kv attribute to __init__ example with correct reverse-scan logic (was referenced in forward() but not defined) 2. Update text model forward loop to use enumerate(zip(...)) pattern matching Gemma4TextModel exactly, with clearer comment about why the past_kvs expansion is needed Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Updates Mobius “skills” documentation to capture Gemma 4–inspired patterns for multimodal task splitting, direct fused MoE op emission, and KV sharing across decoder layers.
Changes:
- Add an “Any-to-Any” 4-model split pattern (decoder/vision/audio/embedding) to the multimodal skills doc.
- Add guidance for emitting
com.microsoft.MoEdirectly (with EP capability gating and fallback) to the MoE skills doc. - Add a KV-sharing (
num_kv_shared_layers) pattern write-up to the “adding a new model” skills doc.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| .github/skills/multimodal-models/SKILL.md | Adds a tiered 3-model vs 4-model multimodal export pattern and wiring notes. |
| .github/skills/moe-models/SKILL.md | Documents a recommended direct com.microsoft.MoE emission approach with EP capability checks and fallback. |
| .github/skills/adding-a-new-model/SKILL.md | Documents an approach for KV-sharing across layers and KV-cache shape implications. |
| ### Emission pattern (from Gemma 4 implementation) | ||
|
|
||
| ```python | ||
| from mobius._execution_providers import ep_capabilities |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Match the actual attribute name used in the Gemma 4 implementation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
- adding-a-new-model: fix preprocess_weights to use self.config (not bare config which is out of scope in the method body) - moe-models: fix ep_capabilities import path (_build_context, not _execution_providers); fix TopK fallback to use op.Constant tensor for k (not Python list literal) - multimodal-models: fix tier detection wording to use config.audio is not None; fix vision/audio I/O shapes to match actual implementation ([B,3,H,W] → [num_image_tokens,H]); fix Phi4MM file path to _phi4mm_multimodal.py; clarify structural vs I/O parity with Phi4MM Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
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.
Updates three skills with patterns proven in the Gemma 4 implementation (reviewed and tested):
moe-models: Direct
com.microsoft.MoEemissioncaps.supports_fused_moe)router_probs [num_tokens, num_experts]requirementCastLikeafter MoE output to preserve input dtype (not hardcoded float32)preprocess_weightsstacking pattern for expert weightsmultimodal-models: Any-to-Any 4-model task split
input_features [B, T, mel]→audio_features [B, T//4, H]adding-a-new-model: KV sharing across layers (
num_kv_shared_layers)k_proj/v_projweights in checkpointis_kv_shared_layerflag on attention moduleshared_kv_statesdict wired through forward loopnum_hidden_layers - num_kv_shared_layersentriespast_key_valuesexpansion trick for per-layer slot alignmentReference: Gemma 4 implementation in
src/mobius/models/gemma4.pyandsrc/mobius/tasks/_gemma4.py