[CUDA] Add GatherQMM for quantized gather matmul#3321
Open
Lyxot wants to merge 5 commits intoml-explore:mainfrom
Open
[CUDA] Add GatherQMM for quantized gather matmul#3321Lyxot wants to merge 5 commits intoml-explore:mainfrom
Lyxot wants to merge 5 commits intoml-explore:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a CUDA implementation path for GatherQMM (quantized gather + matmul), enabling GPU execution for MoE-style gathered expert matmuls. It also refactors the existing qmv CUDA kernel to share a common dequantize+FMA+reduce compute body with the new gather kernel.
Changes:
- Add
GatherQMM::eval_gpuCUDA dispatch that routes to a newgather_qmvkernel. - Extract shared device compute body (
qmv_impl) fromqmv_kerneland reuse it ingather_qmv_kernel. - Expose
gather_qmvin the CUDA quantized qmm header and enable GPU support forGatherQMM.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| mlx/backend/cuda/quantized/quantized.cpp | Adds CUDA GatherQMM::eval_gpu implementation that allocates output and launches gather_qmv. |
| mlx/backend/cuda/quantized/qmm/qmv.cu | Refactors qmv compute into qmv_impl and adds the new gather_qmv_kernel + host launcher. |
| mlx/backend/cuda/quantized/qmm/qmm.h | Declares the new gather_qmv entry point. |
| mlx/backend/cuda/primitives.cpp | Removes NO_GPU(GatherQMM) to allow CUDA backend execution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
zcbenz
reviewed
Mar 26, 2026
Broadcast index arrays (e.g., from default lhs_indices) may have stride-0 dimensions backed by fewer elements than the logical shape. The gather_qmv kernel reads indices linearly after collapse, causing out-of-bounds access. Fix by copying to contiguous layout first.
zcbenz
reviewed
Mar 26, 2026
zcbenz
approved these changes
Mar 27, 2026
Collaborator
zcbenz
left a comment
There was a problem hiding this comment.
Looks good to me, thanks!
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.
Proposed Changes
Adds CUDA
GatherQMMimplementation, enabling quantized MoE inference on GPU. Also extractsqmv_impldevice function fromqmv_kernel, so bothqmv_kernelandgather_qmv_kernelshare the same dequant+FMA+reduce compute body.What's supported
transpose=trueWhat's next
Dedicatedgather_fp_qmvto inherit architecture-specific FP-mode optimizations fromfp_qmvgather_qmmkernelPerformance
4-bit affine quantization, M=1:
Loop baseline: individual
quantized_matmulcalls per expert, stacked.