-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[CUDA] Grouped mm #4390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nastya236
wants to merge
14
commits into
main
Choose a base branch
from
gather-mm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[CUDA] Grouped mm #4390
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c3d93d4
[wip] cudnn grouped mm
nastya236 8364b1f
fix the graph caching
nastya236 6e6936e
stash
nastya236 91f6024
grouped mm
nastya236 fe805fa
int32 for offsets, python op
nastya236 49a580b
Merge branch 'main' into gather-mm
nastya236 63c3a9e
Merge branch 'main' into gather-mm
nastya236 f9dcea1
grop dead offset computation function
nastya236 f68cfdf
Merge branch 'gather-mm' of https://github.com/ml-explore/mlx into ga…
nastya236 0b7e2a7
Merge remote-tracking branch 'upstream/main' into gather-mm
nastya236 f871063
Merge remote-tracking branch 'upstream/main' into gather-mm
nastya236 86cb0dc
test, drop unused parameters
nastya236 9bc0f05
use product in test
nastya236 48551a6
Merge branch 'main' into gather-mm
nastya236 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright © 2025 Apple Inc. | ||
|
|
||
| #include "mlx/backend/cuda/gemms/grouped_gemm.h" | ||
| #include "mlx/backend/cuda/cudnn_utils.h" | ||
|
|
||
| #include <stdexcept> | ||
|
|
||
| namespace mlx::core { | ||
|
|
||
| void grouped_mm( | ||
| const array& a, | ||
| const array& b, | ||
| const array& offsets, | ||
| array& out, | ||
| cu::CommandEncoder& encoder) { | ||
| #if CUDNN_VERSION >= 91800 | ||
| cudnn_grouped_mm(a, b, offsets, out, encoder); | ||
| #else | ||
| throw std::runtime_error( | ||
| "[grouped_mm] Grouped matmul requires cuDNN 9.18 or newer."); | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace mlx::core | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright © 2025 Apple Inc. | ||
|
|
||
| #include "mlx/backend/cuda/cudnn_utils.h" | ||
| #include "mlx/backend/cuda/device.h" | ||
| #include "mlx/backend/cuda/gemms/grouped_gemm.h" | ||
| #include "mlx/backend/cuda/lru_cache.h" | ||
| #include "mlx/backend/gpu/copy.h" | ||
| #include "mlx/fast_primitives.h" | ||
|
|
||
| #include <nvtx3/nvtx3.hpp> | ||
|
|
||
| #include <optional> | ||
|
|
||
| namespace mlx::core { | ||
|
|
||
| #if CUDNN_VERSION >= 91800 | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr int GMM_NDIM = 3; | ||
|
|
||
| struct GatherMMCacheKey { | ||
| int device_id; | ||
| fe::DataType_t cudnn_dtype; | ||
| int mode; // NONE / GATHER / SCATTER | ||
| std::array<int, GMM_NDIM> x_shape; | ||
| std::array<int64_t, GMM_NDIM> x_strides; | ||
| std::array<int, GMM_NDIM> w_shape; | ||
| std::array<int64_t, GMM_NDIM> w_strides; | ||
| std::array<int, GMM_NDIM> out_shape; | ||
| }; | ||
|
|
||
| inline BytesKey<GatherMMCacheKey> build_grouped_mm_key( | ||
| cu::CommandEncoder& encoder, | ||
| const array& x, | ||
| const array& w, | ||
| const array& out) { | ||
| BytesKey<GatherMMCacheKey> key; | ||
| key.pod.device_id = encoder.device().cuda_device(); | ||
| key.pod.cudnn_dtype = dtype_to_cudnn_type(x.dtype()); | ||
| key.pod.mode = 0; // NONE | ||
| key.pod.x_shape = vector_key<GMM_NDIM>(x.shape()); | ||
| key.pod.x_strides = vector_key<GMM_NDIM>(x.strides()); | ||
| key.pod.w_shape = vector_key<GMM_NDIM>(w.shape()); | ||
| key.pod.w_strides = vector_key<GMM_NDIM>(w.strides()); | ||
| key.pod.out_shape = vector_key<GMM_NDIM>(out.shape()); | ||
| return key; | ||
| } | ||
|
|
||
| enum UIDS { X, W, TOKEN_OFFSETS, TOKEN_INDEX, O }; | ||
|
|
||
| // cudnn expects specific shape and strides for grouped matmul: | ||
| // [1, T, H] | ||
| void set_moe_layout( | ||
| std::shared_ptr<fe::graph::Tensor_attributes>& t, | ||
| const array& x) { | ||
| int64_t L = x.shape(0); | ||
| int64_t D = x.shape(-1); | ||
| int64_t sL = x.strides(0); | ||
| int64_t sD = x.strides(-1); | ||
| t->set_dim({1, L, D}).set_stride({L * sL, sL, sD}); | ||
| } | ||
|
|
||
| DnnGraph grouped_mm_graph( | ||
| cudnnHandle_t handle, | ||
| const array& x, | ||
| const array& w, | ||
| const array& token_offsets, | ||
| const array& output) { | ||
| DnnGraph graph(handle, x.dtype()); | ||
|
|
||
| auto x_ = graph.tensor("X", X, x); | ||
| set_moe_layout(x_, x); | ||
| auto w_ = graph.tensor("W", W, w); | ||
| auto token_offsets_ = | ||
| graph.tensor("TOKEN_OFFSETS", TOKEN_OFFSETS, token_offsets); | ||
|
|
||
| auto moe_grouped_matmul_attr = | ||
| fe::graph::Moe_grouped_matmul_attributes() | ||
| .set_name("grouped_matmul") | ||
| .set_mode(fe::MoeGroupedMatmulMode_t::NONE); | ||
|
|
||
| std::shared_ptr<fe::graph::Tensor_attributes> token_index = nullptr; | ||
| std::shared_ptr<fe::graph::Tensor_attributes> token_ks = nullptr; | ||
|
|
||
| auto out_ = graph.moe_grouped_matmul( | ||
| x_, w_, token_offsets_, token_index, token_ks, moe_grouped_matmul_attr); | ||
| graph.tensor(out_, O, output); | ||
| set_moe_layout(out_, output); | ||
| out_->set_output(true); | ||
|
|
||
| CHECK_CUDNN_ERROR(graph.prepare()); | ||
| graph.select_behavior_notes( | ||
| {fe::BehaviorNote_t::SUPPORTS_CUDA_GRAPH_NATIVE_API}); | ||
| CHECK_CUDNN_ERROR(graph.build()); | ||
| return graph; | ||
| } | ||
|
|
||
| auto& grouped_mm_cache() { | ||
| static thread_local LRUBytesKeyCache<GatherMMCacheKey, DnnGraph> cache( | ||
| "MLX_CUDA_GMM_CACHE_SIZE", /* default_capacity */ 256); | ||
| return cache; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void cudnn_grouped_mm( | ||
| const array& x, | ||
| const array& w, | ||
| const array& token_offsets, // precomputed offsets for each expert | ||
| array& out, | ||
| cu::CommandEncoder& encoder) { | ||
| nvtx3::scoped_range r("cudnn_grouped_mm"); | ||
|
|
||
| auto handle = get_cudnn_handle(encoder.device()); | ||
|
|
||
| encoder.set_input_array(x); | ||
| encoder.set_input_array(w); | ||
| encoder.set_input_array(token_offsets); | ||
| encoder.set_output_array(out); | ||
|
|
||
| auto cache_key = build_grouped_mm_key(encoder, x, w, out); | ||
| auto& cache = grouped_mm_cache(); | ||
| auto it = cache.find(cache_key); | ||
| if (it == cache.end()) { | ||
| auto graph = grouped_mm_graph(handle, x, w, token_offsets, out); | ||
| it = cache.emplace(cache_key, std::move(graph)).first; | ||
| } | ||
| auto& graph = it->second; | ||
|
|
||
| std::unordered_map<int64_t, void*> variant_pack{ | ||
| {X, gpu_ptr<void>(x)}, | ||
| {W, gpu_ptr<void>(w)}, | ||
| {TOKEN_OFFSETS, gpu_ptr<void>(token_offsets)}, | ||
| {O, gpu_ptr<void>(out)}}; | ||
| CHECK_CUDNN_ERROR(graph.encode_graph(encoder, std::move(variant_pack))); | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| } // namespace mlx::core |
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to check
CUDNN_VERSIONourselves, the cudnn-frontend C++ APIs we use are capable of detecting cudnn version and throw errors. And we can ensure minimum cudnn version insetup.pyby setting the version ofnvidia-cudnn-cu12/13dependencies.Also since
cudnn_grouped_mmrequires sm80 and later, this function should check it here.