Skip to content

Add Phi-4 Flash SambaY support - #729

Open
justinchuby wants to merge 1 commit into
mainfrom
justinchuby-add-phi4-flash-reasoning
Open

Add Phi-4 Flash SambaY support#729
justinchuby wants to merge 1 commit into
mainfrom
justinchuby-add-phi4-flash-reasoning

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Summary

  • add dedicated support for microsoft/Phi-4-mini-flash-reasoning@1dff8163d28ec880ca2411c474ddc0a927792810 with source-faithful SambaY Mamba, differential-attention, YOCO shared-memory/KV topology, and dynamic heterogeneous cache ABI
  • add graph-derived runtime metadata, clear static/paged-cache and ORT GenAI rejection, model catalog documentation, and scoped pinned CUDA reference dependencies/workflow
  • add L1 graph/config/routing coverage and a batch-two pinned-source CUDA L3 harness covering long-window prefill, decode, recurrent/local/global cache states, and post-layer hidden states

Validation

  • full non-integration suite: 9966 passed, 68 skipped
  • focused Phi-4 Flash tests: 28 passed, 4 skipped
  • pinned no-weight CUDA-targeted CLI graph export completed with the 38-input/37-output dynamic state contract
  • independent GPT-5.6 Terra review: no remaining significant findings

Limitations

  • The local environment has no CUDA, so the scoped pinned CUDA L3 workflow remains required to execute remote-source numerical parity.
  • L4/L5 checkpoint goldens and ORT GenAI packaging remain explicitly unsupported as documented: its runtime cannot represent the heterogeneous dynamic SambaY cache ABI.
  • ORT exposes no ragged dynamic-cache attention ABI matching FlashAttention unpadding; unpadded long prompts use compact native paths, while source-faithful padded prompts use a full ONNX mask and may be infeasible at 64K+.

Implement the pinned Phi-4 mini flash reasoning architecture with source-faithful Mamba, differential attention, YOCO state topology, dynamic cache ABI, exact scoped CUDA parity coverage, and explicit runtime/cache limitations.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 489bb9f5-59db-436a-a63b-eb8164e879e3
Signed-off-by: Justin Chu <[email protected]>
@justinchuby
justinchuby requested review from a team and a lite review from Copilot September 4, 2026 18:17
@@ -0,0 +1,344 @@
# Copyright (c) Microsoft Corporation.
@@ -0,0 +1,124 @@
# Copyright (c) Microsoft Corporation.
@@ -0,0 +1,477 @@
# Copyright (c) Microsoft Corporation.
@@ -0,0 +1,477 @@
# Copyright (c) Microsoft Corporation.
transient layer-16 memory and layer-17 KV within the same graph invocation.
"""

from __future__ import annotations
@@ -0,0 +1,204 @@
# Copyright (c) Microsoft Corporation.
@@ -0,0 +1,250 @@
# Copyright (c) Microsoft Corporation.
@@ -0,0 +1,250 @@
# Copyright (c) Microsoft Corporation.
def _new_remote_cache(reference_model: Any, config: Any, batch: int, max_length: int) -> Any:
"""Instantiate the source's fixed-capacity cache so decode can grow past prefill."""
module = importlib.import_module(type(reference_model.model).__module__)
cache_type = getattr(module, "SambaYCache")
"configuration_phi4flash", "modeling_phi4flash"
)
)
reference_class = getattr(reference_module, "Phi4FlashForCausalLM")
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

Performance Comparison

Comparing 8c38c06ad1860d

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0%
bert (feature-extraction) num_nodes 68 68 +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 105 105 +0.0%
gpt2 model_size_bytes 324 KB 324 KB +0.0%
gpt2 num_nodes 54 54 +0.0%
llama model_size_bytes 425 KB 425 KB +0.0%
llama num_nodes 60 60 +0.0%
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0%
llama (static-cache) num_nodes 56 56 +0.0%
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0%
mamba (ssm-text-generation) num_nodes 94 94 +0.0%
phi3 model_size_bytes 421 KB 421 KB +0.0%
phi3 num_nodes 58 58 +0.0%
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0%
phi3 (static-cache) num_nodes 54 54 +0.0%
qwen2 model_size_bytes 425 KB 425 KB +0.0%
qwen2 num_nodes 60 60 +0.0%
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0%
qwen2 (static-cache) num_nodes 56 56 +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 265 265 +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 127 127 +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 450 450 +0.0%
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0%
t5 (seq2seq) num_nodes 176 176 +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.

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.

🟡 Changes recommended

The pinned CUDA workflow can inadvertently reinstall a non-CUDA torch build due to torch==2.6.0 being pinned in requirements/ci/phi4flash.txt after installing a CUDA wheel from the PyTorch CUDA index.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds first-class support for the phi4flash (Phi-4 Flash Reasoning) architecture to mobius, including its heterogeneous SambaY cache ABI and a pinned-source CUDA parity harness that validates the exported ONNX graph against the upstream trust_remote_code implementation.

Changes:

  • Introduces Phi4FlashConfig, Phi4FlashCausalLMModel, and Phi4FlashCausalLMTask, registers them in the model/task registries, and documents model catalog support.
  • Adds new reusable building blocks required by Phi-4 Flash (portable differential GQA attention and a state-threaded Mamba-1 block), plus targeted graph/unit coverage.
  • Adds a scoped self-hosted CUDA workflow + pinned dependency stack and a CUDA integration test for L3 numerical parity against the pinned remote implementation.
File summaries
File Description
tests/synthetic_parity_test.py Skips phi4flash in synthetic parity due to pinned CUDA reference dependency needs.
tests/phi4flash_integration_test.py New pinned-source CUDA L3 parity test validating logits, cache slots, and post-layer residual streams.
tests/model_coverage_test.py Adds coverage notes for phi4flash and its Phi4FlashForCausalLM alias.
tests/build_graph/recurrent_test.py Excludes phi4flash from generic SSM tests and adds dedicated Phi-4 Flash graph/cache-contract assertions.
tests/build_graph/_support.py Includes phi4flash and Phi4FlashForCausalLM in the hybrid/dedicated test matrix.
tests/_test_configs.py Adds a tiny Phi4FlashConfig entry to the SSM config matrix for L1 graph build coverage.
testdata/cases/causal-lm/phi4-mini-flash-reasoning.yaml New (skipped) L4/L5 case entry documenting limitations and why golden runs aren’t committed yet.
src/mobius/tasks/_phi4flash.py New task wiring for Phi-4 Flash’s dynamic heterogeneous SambaY cache ABI + metadata emission.
src/mobius/tasks/init.py Exposes Phi4FlashCausalLMTask and registers the phi4flash-text-generation task key.
src/mobius/models/phi4flash.py New Phi-4 Flash SambaY model implementation (Mamba + differential attention + YOCO shared memory/KV topology).
src/mobius/models/phi4flash_test.py Unit coverage for config extraction, weight routing, cache ABI, attention behavior, and ORT GenAI rejection.
src/mobius/models/init.py Exports Phi4FlashCausalLMModel.
src/mobius/integrations/ort_genai/auto_export.py Explicitly rejects ORT GenAI config generation for phi4flash due to unsupported cache ABI.
src/mobius/components/_mamba_block.py Adds StatefulMambaBlock plus helpers and related activation/memory-mixer utilities used by Phi-4 Flash.
src/mobius/components/_mamba_block_test.py Adds graph-construction tests for StatefulMambaBlock and FloatSwiGLU.
src/mobius/components/_differential_attention.py New portable differential grouped-query attention component with padded/unpadded branching.
src/mobius/components/_differential_attention_test.py New tests covering graph structure and basic CPU execution for differential attention branches.
src/mobius/components/init.py Re-exports DifferentialGQAAttention, StatefulMambaBlock, FloatSwiGLU, and GatedMemoryMixer.
src/mobius/_registry.py Registers phi4flash and the Phi4FlashForCausalLM alias with test model metadata.
src/mobius/_configs/_base.py Adds Phi4FlashConfig with schedule derivation, validation, and HF config extraction.
src/mobius/_configs/init.py Exports Phi4FlashConfig.
src/mobius/main.py Adds CLI/runtime guardrails rejecting static/paged cache modes and ORT GenAI runtime for phi4flash.
requirements/ci/phi4flash.txt New pinned CUDA reference dependency list for the Phi-4 Flash parity workflow.
docs/model-catalog.md Documents phi4flash in the hybrid SSM+attention model catalog.
.github/workflows/phi4flash_parity.yml New scoped self-hosted CUDA workflow to run the pinned-source L3 parity test.
Review details
  • Files reviewed: 25/25 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

# Exact CUDA reference stack for microsoft/Phi-4-mini-flash-reasoning
# at 1dff8163d28ec880ca2411c474ddc0a927792810. This is deliberately scoped
# to the Phi-4 Flash parity workflow: it must not constrain public extras.
torch==2.6.0
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 8c38c06ad1860d

Model Sub-model Changes Status
bert (feature-extraction) model 0
falcon model 0
gemma2 model 0
gemma4 (gemma4) decoder 0
gemma4 (gemma4) embedding 0
gemma4 (gemma4) vision_encoder 0
gemma4_text model 0
gpt2 model 0
llama model 0
llama (static-cache) model 0
mamba (ssm-text-generation) model 0
phi3 model 0
phi3 (static-cache) model 0
qwen model 0
qwen (static-cache) model 0
qwen2 model 0
qwen2 (static-cache) model 0
qwen2_moe model 0
qwen2_moe (static-cache) model 0
qwen3 model 0
qwen3 (static-cache) model 0
qwen3_5_moe (hybrid-text-generation) model 0
qwen3_5_text (hybrid-text-generation) model 0
qwen3_5_vl (hybrid-qwen-vl) decoder 0
qwen3_5_vl (hybrid-qwen-vl) embedding 0
qwen3_5_vl (hybrid-qwen-vl) vision_encoder 0
qwen3_moe model 0
qwen3_moe (static-cache) model 0
qwen3_next (hybrid-text-generation) model 0
t5 (seq2seq) decoder 0
t5 (seq2seq) encoder 0
whisper (speech-to-text) decoder 0
whisper (speech-to-text) encoder 0

No architecture changes detected.


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

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.

3 participants