Skip to content

docs: update skills with proven Gemma 4 patterns (MoE, Any-to-Any, KV sharing) - #140

Merged
justinchuby merged 6 commits into
mainfrom
justinchu/update-skills-v2
Apr 10, 2026
Merged

docs: update skills with proven Gemma 4 patterns (MoE, Any-to-Any, KV sharing)#140
justinchuby merged 6 commits into
mainfrom
justinchu/update-skills-v2

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Updates three skills with patterns proven in the Gemma 4 implementation (reviewed and tested):

moe-models: Direct com.microsoft.MoE emission

  • EP capability check (caps.supports_fused_moe)
  • Full pre-topk router_probs [num_tokens, num_experts] requirement
  • CastLike after MoE output to preserve input dtype (not hardcoded float32)
  • preprocess_weights stacking pattern for expert weights
  • Loop-over-experts fallback when EP does not support fused MoE

multimodal-models: Any-to-Any 4-model task split

  • Tier split: E2B/E4B = 4-model Any-to-Any, 26B/31B = 3-model Image-Text-to-Text
  • 4-model structure: decoder + vision + audio + embedding
  • Audio encoder wiring: input_features [B, T, mel]audio_features [B, T//4, H]
  • Embedding model fusion of image+audio+text tokens
  • Task class tier detection pattern

adding-a-new-model: KV sharing across layers (num_kv_shared_layers)

  • Shared layers have no k_proj/v_proj weights in checkpoint
  • is_kv_shared_layer flag on attention module
  • shared_kv_states dict wired through forward loop
  • KV cache has num_hidden_layers - num_kv_shared_layers entries
  • past_key_values expansion trick for per-layer slot alignment

Reference: Gemma 4 implementation in src/mobius/models/gemma4.py and src/mobius/tasks/_gemma4.py

justinchuby and others added 3 commits April 9, 2026 21:54
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>
@github-actions

github-actions Bot commented Apr 10, 2026

Copy link
Copy Markdown

Performance Comparison

Comparing b99f532b5bb23c

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0%
bert (feature-extraction) num_nodes 61 61 +0.0%
falcon model_size_bytes 364 KB 364 KB +0.0%
falcon num_nodes 66 66 +0.0%
gemma2 model_size_bytes 428 KB 428 KB +0.0%
gemma2 num_nodes 107 107 +0.0%
gpt2 model_size_bytes 388 KB 388 KB +0.0%
gpt2 num_nodes 53 53 +0.0%
llama model_size_bytes 425 KB 425 KB +0.0%
llama num_nodes 61 61 +0.0%
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0%
llama (static-cache) num_nodes 58 58 +0.0%
mamba (ssm-text-generation) model_size_bytes 360 KB 360 KB +0.0%
mamba (ssm-text-generation) num_nodes 103 103 +0.0%
phi3 model_size_bytes 421 KB 421 KB +0.0%
phi3 num_nodes 61 61 +0.0%
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0%
phi3 (static-cache) num_nodes 58 58 +0.0%
qwen2 model_size_bytes 425 KB 425 KB +0.0%
qwen2 num_nodes 61 61 +0.0%
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0%
qwen2 (static-cache) num_nodes 58 58 +0.0%
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0%
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0%
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0%
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0%
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0%
qwen3_5_vl (hybrid-qwen-vl) num_nodes 409 409 +0.0%
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0%
t5 (seq2seq) num_nodes 174 174 +0.0%
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0%
whisper (speech-to-text) num_nodes 128 128 +0.0%

No performance regressions.

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.MoE directly (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.

Comment thread .github/skills/multimodal-models/SKILL.md Outdated
Comment thread .github/skills/multimodal-models/SKILL.md
Comment thread .github/skills/multimodal-models/SKILL.md Outdated
Comment thread .github/skills/moe-models/SKILL.md Outdated
### Emission pattern (from Gemma 4 implementation)

```python
from mobius._execution_providers import ep_capabilities
Comment thread .github/skills/moe-models/SKILL.md Outdated
Comment thread .github/skills/adding-a-new-model/SKILL.md
@codecov

codecov Bot commented Apr 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

justinchuby and others added 2 commits April 9, 2026 23:51
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>
@justinchuby
justinchuby merged commit 62a6829 into main Apr 10, 2026
20 of 21 checks passed
@justinchuby
justinchuby deleted the justinchu/update-skills-v2 branch April 10, 2026 17:42
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