Fix fp8 MoE on the sparse_matmul path - #4957
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces safety checks and integration tests for Mixture of Experts (MoE) models using the sparse_matmul path with FP8 quantization. It adds a warning log when FP8 quantization is combined with sparse_matmul because the expert matmuls remain unquantized on this path. It also updates get_quantization_dtypes to safely access quant_dg using getattr to prevent errors when using FP8 schemes. Finally, it adds integration tests to ensure that the MoE layer builds and trains successfully under these configurations. There are no review comments, so I have no feedback to provide.
`get_quantization_dtypes` reads `self.quant.quant_dg` whenever a quantization is set, but only AQT carries one, so a MoE model with `sparse_matmul=True` dies before it ever reaches the gmm: fp8 AttributeError: 'Fp8Quantization' object has no attribute 'quant_dg' nanoo_fp8 AttributeError: 'NANOOFp8Quantization' object has no attribute 'quant_dg' 55c368d already settled what should happen here: schemes that define no gmm quantization rule "execute unquantized GMM". That change handled the qwix side and left this read alone, so fp8 crashes instead of taking the fallback it was given. Read `quant_dg` defensively so it gets there. Expert matmuls running in the compute dtype while the dense layers run fp8 is easy to miss from the config alone, so the layer says so once when it is built. The two new tests carry no hardware marker: the fp8 schemes are emulated in XLA, so a tiny Mixtral trains on CPU in seconds. Both fail before this change.
6926628 to
2bc82fd
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Problem
Any MoE model with
sparse_matmul=Trueand an fp8 quantization crashes while the layer is being traced:get_quantization_dtypesreadsself.quant.quant_dgwhenever a quantization is configured, butquant_dgis an AQTnotion — the fp8 classes have never had one. Since
sparse_matmul=Trueis the default, this is what a user hits firstwhen combining fp8 with a MoE model.
Fix
55c368d already decided what should happen for schemes that define no gmm quantization rule: they "execute
unquantized GMM". That change handled the qwix rule and left this read untouched, so fp8 crashes instead of taking the
fallback it was given. Reading
quant_dgdefensively is enough to get it there.Expert matmuls silently running in the compute dtype while the dense layers run fp8 is easy to miss from the config
alone, so the layer logs that once when it is built, pointing at
sparse_matmul=Falsefor anyone who wants the expertsquantized too.
Not addressed here: actually quantizing the gmm for
fp8/nanoo_fp8. That needs kernel support, andfp8_fullremains the scheme that quantizes the expert matmuls.
Tests
Two tests on a tiny Mixtral,
sparse_matmul=Truewithfp8andnanoo_fp8. No hardware marker, since the fp8schemes are emulated in XLA and the failure is backend independent; they run on CPU in about 14s together, and both
fail before this change.
Related
dense_matmulpath, where every quantization fails for an unrelated reason(Linen einsums with no scope to bind to after the NNX migration). The two are independent, but together they make
quantized MoE work on both paths. There is a small textual overlap in
tests/integration/train_tests.py; happy torebase whichever lands second.
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.