Open
Conversation
Replace the host-side cuBLAS loop with a single CUTLASS grouped GEMM dispatch. A GPU-side prepare kernel builds per-segment problem sizes and pointer offsets from the segments array, eliminating the host sync that was required to read segment boundaries.
CUTLASS handles K=0 segments correctly: the mainloop iterates zero times and the epilogue writes zeros to the output.
Compare mx.segmented_mm (grouped GEMM) against MLX loop-of-matmuls baseline. Remove torch dependency. Add accuracy checks: fp32 vs numpy fp64, fp16/bf16 vs own fp32 result.
There was a problem hiding this comment.
Pull request overview
Implements mx.segmented_mm on the CUDA backend by dispatching to a CUTLASS grouped GEMM path, enabling the existing Python BLAS segmented-mm tests to run on CUDA and adding a standalone benchmark script to compare against a loop-of-matmuls baseline.
Changes:
- Enable CUDA support for the
SegmentedMMprimitive and remove the CUDA test skip. - Add
SegmentedMM::eval_gpuimplementation that prepares inputs and calls a new CUTLASS grouped-GEMM launcher. - Introduce a Python benchmark for
mx.segmented_mmperformance and numerical error checks.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| python/tests/cuda_skip.py | Removes the CUDA skip for the segmented_mm BLAS test. |
| mlx/backend/cuda/primitives.cpp | Marks SegmentedMM as supported on CUDA (removes NO_GPU). |
| mlx/backend/cuda/matmul.cpp | Adds CUDA implementation for SegmentedMM::eval_gpu. |
| mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu | Adds segment-to-grouped-GEMM argument preparation kernel and CUTLASS dispatch wrapper. |
| mlx/backend/cuda/gemms/grouped_gemm.h | Declares cutlass_segmented_mm entrypoint. |
| benchmarks/python/segmented_mm_bench.py | Adds benchmarking and correctness checking script for segmented_mm. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Implement
mx.segmented_mmfor the CUDA backend using CUTLASS grouped GEMM.Performance
MLX_ENABLE_TF32=0, random segments.float32
bfloat16
MLX ms=mx.segmented_mm(CUTLASS grouped GEMM),Loop ms= MLX loop-of-matmuls baseline.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes