Repository navigation
fix(moe): avoid incorrect Granite grouped expert execution on MPS - #1887
janmenjayap wants to merge 3 commits into
Conversation
jlarson4
left a comment
There was a problem hiding this comment.
Hi @janmenjayap, the idea behind your fix is sound, but there are some fundamental flaws that need to be resolved before this can be merged. Please take a look at my comments below, and let me know if you have any questions
|
|
||
| if ( | ||
| type(experts) is not GraniteMoeHybridExperts | ||
| or Version(torch.__version__).public != "2.11.0" |
There was a problem hiding this comment.
This gates on torch 2.11.0 and transformers 5.13.0, but the bad slice writes are not tied to those versions. On macOS 26.6 with exactly those packages, the primitive, native grouped_mm, and HF's fallback are all correct and the parity test passes without this change, yet the gate still fires, so every Granite Hybrid forward on MPS takes the eager loop and receive a false warning saying its products are wrong. Please key the fallback on a direct check of the slice-write primitive rather than on package versions.
| ) | ||
|
|
||
| if ( | ||
| type(experts) is not GraniteMoeHybridExperts |
There was a problem hiding this comment.
Only GraniteMoeHybridExperts is recognized, but the primitive that fails is shared. Every @use_experts_implementation class routes through the same _grouped_mm, and HF defaults all of them to grouped_mm with no device check. On an affected host a GraniteMoe, Mixtral, or Qwen3-MoE bridge produces the same wrong products with no fallback. Please adjust to recognize the shared dispatch decorator.
| and hidden_states.device.type == "mps" | ||
| and getattr(config, "_experts_implementation", None) == "grouped_mm" | ||
| ): | ||
| warnings.warn( |
There was a problem hiding this comment.
The warning is raised inside every expert call, and stacklevel=2 lands in jaxtyping's wrapper, so users see a library frame rather than their own call and, under non-default filters, one line per layer per forward. devices.py already keeps its MPS warnings to once per process with a module flag; consider the same here.
|
|
||
| There is also `TransformerBridge.boot_native(config)`, which builds a small randomly-initialized TL-native model on the same driver — no HuggingFace Hub call — useful for tests and toy-model experiments. | ||
|
|
||
| #### Granite MoE Hybrid grouped experts on Apple MPS |
There was a problem hiding this comment.
This subsection sits under the transformers backend in the driver guide, but it is an Apple-specific workaround. Would you please move it to the existing TRANSFORMERLENS_ALLOW_MPS section in getting_started.md? Consider reducing to a short note there, with the diagnostic kept in the test file or the issue, instead of this subsection.
Description
Fixes #1886
Granite MoE Hybrid expert execution can produce incorrect or unstable outputs on Apple MPS when HuggingFace selects
grouped_mmin the verified torch 2.11.0 / transformers 5.13.0 environment. This can make the existing SSM eager/fused parity regression fail even though the first substantial divergence occurs in the MoE expert output rather than the SSM scan.A standalone fixed-data diagnostic isolates incorrect nonzero-offset
torch.mm(..., out=output[5:])writes. HF's grouped fallback also uses output-slice writes, so disabling native grouped execution alone is insufficient.Changes
GraniteMoeHybridExpertsclass, installed throughMoEBridge.forward.grouped_mmon torch release 2.11.0 and transformers release 5.13.0. Local version suffixes are accepted; prerelease/development versions are not.The existing Granite MoE Hybrid MPS scan-parity assertion passes through the corrected production path without a changed threshold, new skip/xfail, or forced eager policy in its fixture.
Scope and limitations
RuntimeWarning; eager expert execution may be slower.Relationship to other work
This is separate from #1878, which fixes Granite embedding scaling in cache residual decomposition and component-level direct logit attribution without changing model forward execution. That analysis correction does not repair incorrect expert matrix products.
It is also independent of GPT-2 contextual decomposition.
A potentially related upstream report is pytorch/pytorch#193487, concerning allocator-dependent incorrect MPS matmul results. Its reproduction differs from the small nonzero-offset output-write reproduction here; a shared root cause has not been established. No upstream report or comment has been posted for this work.
Type of change
Validation
Before and after
On baseline
a8bc19e7, the CPU scan-parity case passed and the MPS case failed with relative difference1.0326683320021979(required< 1e-4). On this branch, the original test passes without weakened assertions.The standalone primitive diagnostic measured:
Focused regressions
Result: 49 passed, with the intended fallback warnings and other dependency/model warnings reported.
Standard checks
make format,make check-format, andgit diff --checkpassed. The skip/xfail counts above are suite outcomes, not new markers added to bypass this defect.Full PR validation remains incomplete.
make test-prcompleted unit, docstring, and acceptance tiers, then hit the 30-minute command limit during integration testing. No integration failure had been reported before interruption, but the integration tier is not claimed complete or passing. The docs build is not claimed warning-free. CUDA was not tested.Checklist
The warning checkbox is intentionally unchecked because the affected MPS path emits an explicit correctness-fallback warning. Completing the full integration tier remains a validation follow-up before merge.