From 8674a13b738d58e16cccd135da3ffb0d2d69df23 Mon Sep 17 00:00:00 2001 From: Daniel Hiltgen Date: Thu, 22 Oct 2026 08:19:28 -0700 Subject: [PATCH 1/2] [CUDA] Avoid multi-root graphs on DGX/RTX spark --- mlx/backend/cuda/CMakeLists.txt | 1 + mlx/backend/cuda/device.cpp | 26 ++++++++++++++++++++++++++ mlx/backend/cuda/device.h | 2 ++ mlx/backend/cuda/graph_root.cu | 11 +++++++++++ 4 files changed, 40 insertions(+) create mode 100644 mlx/backend/cuda/graph_root.cu diff --git a/mlx/backend/cuda/CMakeLists.txt b/mlx/backend/cuda/CMakeLists.txt index 50be6f404f..c62e33ff0b 100644 --- a/mlx/backend/cuda/CMakeLists.txt +++ b/mlx/backend/cuda/CMakeLists.txt @@ -36,6 +36,7 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/gemms/cublas_gemm.cpp ${CMAKE_CURRENT_SOURCE_DIR}/gemms/gather_gemm.cu ${CMAKE_CURRENT_SOURCE_DIR}/gemms/grouped_gemm_unaligned.cu + ${CMAKE_CURRENT_SOURCE_DIR}/graph_root.cu ${CMAKE_CURRENT_SOURCE_DIR}/hadamard.cu ${CMAKE_CURRENT_SOURCE_DIR}/jit_module.cpp ${CMAKE_CURRENT_SOURCE_DIR}/indexing.cpp diff --git a/mlx/backend/cuda/device.cpp b/mlx/backend/cuda/device.cpp index 6e40b34038..b00e999867 100644 --- a/mlx/backend/cuda/device.cpp +++ b/mlx/backend/cuda/device.cpp @@ -12,6 +12,8 @@ namespace mlx::core::cu { +void* graph_root_kernel_ptr(); + namespace { bool use_cuda_graphs() { @@ -157,6 +159,11 @@ void CommandEncoder::insert_graph_dependencies(std::vector nodes) { } } active_deps_.clear(); + if (single_root_ && deps.empty()) { + for (auto& node : nodes) { + roots_.push_back(node.node); + } + } for (auto o : active_outputs_) { for (auto& node : nodes) { @@ -209,6 +216,10 @@ CommandEncoder::CommandEncoder(Device& d) worker_(std::make_shared(d)), graph_cache_("MLX_CUDA_GRAPH_CACHE_SIZE", /* default_capacity */ 400) { std::tie(max_ops_per_graph_, max_mb_per_graph_) = get_graph_limits(d); + // DGX Spark / RTX Spark run memory-bound kernels slower in graphs that + // have several root nodes. + single_root_ = + d.compute_capability_major() == 12 && d.compute_capability_minor() == 1; worker_->start(); } @@ -506,6 +517,20 @@ void CommandEncoder::commit_impl() { add_completed_handler([temporaries = std::move(temporaries_)]() {}); } if (use_cuda_graphs() && node_count_ > 0) { + // Give a graph with several roots a single no-op root. + if (roots_.size() > 1) { + cudaKernelNodeParams params = {}; + params.func = graph_root_kernel_ptr(); + params.gridDim = dim3(1); + params.blockDim = dim3(1); + cudaGraphNode_t root; + CHECK_CUDA_ERROR(cudaGraphAddKernelNode(&root, graph_, NULL, 0, ¶ms)); + for (auto node : roots_) { + from_nodes_.push_back(root); + to_nodes_.push_back(node); + } + graph_nodes_key_ += "R-"; + } if (!from_nodes_.empty()) { #if CUDART_VERSION >= 13000 CHECK_CUDA_ERROR(cudaGraphAddDependencies( @@ -579,6 +604,7 @@ void CommandEncoder::clear_graph_state() { active_deps_.clear(); active_outputs_.clear(); concurrent_nodes_.clear(); + roots_.clear(); is_graph_updatable_ = true; } diff --git a/mlx/backend/cuda/device.h b/mlx/backend/cuda/device.h index 1ec8f20fa5..bce36bd860 100644 --- a/mlx/backend/cuda/device.h +++ b/mlx/backend/cuda/device.h @@ -159,6 +159,8 @@ class MLX_API CommandEncoder { std::vector active_deps_; std::vector active_outputs_; std::unordered_map node_map_; + bool single_root_{false}; + std::vector roots_; size_t bytes_in_graph_{0}; bool is_graph_updatable_{true}; int max_ops_per_graph_; diff --git a/mlx/backend/cuda/graph_root.cu b/mlx/backend/cuda/graph_root.cu new file mode 100644 index 0000000000..64a850c744 --- /dev/null +++ b/mlx/backend/cuda/graph_root.cu @@ -0,0 +1,11 @@ +// Copyright © 2026 Apple Inc. + +namespace mlx::core::cu { + +__global__ void graph_root_kernel() {} + +void* graph_root_kernel_ptr() { + return reinterpret_cast(&graph_root_kernel); +} + +} // namespace mlx::core::cu From 3838495d90c93b2adc78d7c36f5e3aa1dbd2eb3a Mon Sep 17 00:00:00 2001 From: Cheng Date: Sun, 25 Oct 2026 03:10:03 -0700 Subject: [PATCH 2/2] nit --- mlx/backend/cuda/CMakeLists.txt | 1 - mlx/backend/cuda/device.cpp | 18 ++++++++++-------- mlx/backend/cuda/device.h | 2 +- mlx/backend/cuda/graph_root.cu | 11 ----------- mlx/backend/cuda/kernel_utils.cu | 10 ++++++++++ 5 files changed, 21 insertions(+), 21 deletions(-) delete mode 100644 mlx/backend/cuda/graph_root.cu diff --git a/mlx/backend/cuda/CMakeLists.txt b/mlx/backend/cuda/CMakeLists.txt index c62e33ff0b..50be6f404f 100644 --- a/mlx/backend/cuda/CMakeLists.txt +++ b/mlx/backend/cuda/CMakeLists.txt @@ -36,7 +36,6 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/gemms/cublas_gemm.cpp ${CMAKE_CURRENT_SOURCE_DIR}/gemms/gather_gemm.cu ${CMAKE_CURRENT_SOURCE_DIR}/gemms/grouped_gemm_unaligned.cu - ${CMAKE_CURRENT_SOURCE_DIR}/graph_root.cu ${CMAKE_CURRENT_SOURCE_DIR}/hadamard.cu ${CMAKE_CURRENT_SOURCE_DIR}/jit_module.cpp ${CMAKE_CURRENT_SOURCE_DIR}/indexing.cpp diff --git a/mlx/backend/cuda/device.cpp b/mlx/backend/cuda/device.cpp index b00e999867..52363446b7 100644 --- a/mlx/backend/cuda/device.cpp +++ b/mlx/backend/cuda/device.cpp @@ -12,7 +12,8 @@ namespace mlx::core::cu { -void* graph_root_kernel_ptr(); +// Defined in kernel_utils.cu +void* get_noop_kernel_ptr(); namespace { @@ -159,7 +160,7 @@ void CommandEncoder::insert_graph_dependencies(std::vector nodes) { } } active_deps_.clear(); - if (single_root_ && deps.empty()) { + if (force_single_root_ && deps.empty()) { for (auto& node : nodes) { roots_.push_back(node.node); } @@ -214,12 +215,13 @@ CommandEncoder::CommandEncoder(Device& d) stream_(d), graph_(d), worker_(std::make_shared(d)), - graph_cache_("MLX_CUDA_GRAPH_CACHE_SIZE", /* default_capacity */ 400) { + graph_cache_("MLX_CUDA_GRAPH_CACHE_SIZE", /* default_capacity */ 400), + // DGX Spark / RTX Spark run memory-bound kernels slower in graphs that + // have several root nodes. + force_single_root_( + d.compute_capability_major() == 12 && + d.compute_capability_minor() == 1) { std::tie(max_ops_per_graph_, max_mb_per_graph_) = get_graph_limits(d); - // DGX Spark / RTX Spark run memory-bound kernels slower in graphs that - // have several root nodes. - single_root_ = - d.compute_capability_major() == 12 && d.compute_capability_minor() == 1; worker_->start(); } @@ -520,7 +522,7 @@ void CommandEncoder::commit_impl() { // Give a graph with several roots a single no-op root. if (roots_.size() > 1) { cudaKernelNodeParams params = {}; - params.func = graph_root_kernel_ptr(); + params.func = get_noop_kernel_ptr(); params.gridDim = dim3(1); params.blockDim = dim3(1); cudaGraphNode_t root; diff --git a/mlx/backend/cuda/device.h b/mlx/backend/cuda/device.h index bce36bd860..6bc64d8df7 100644 --- a/mlx/backend/cuda/device.h +++ b/mlx/backend/cuda/device.h @@ -159,10 +159,10 @@ class MLX_API CommandEncoder { std::vector active_deps_; std::vector active_outputs_; std::unordered_map node_map_; - bool single_root_{false}; std::vector roots_; size_t bytes_in_graph_{0}; bool is_graph_updatable_{true}; + bool force_single_root_{false}; int max_ops_per_graph_; int max_mb_per_graph_; }; diff --git a/mlx/backend/cuda/graph_root.cu b/mlx/backend/cuda/graph_root.cu deleted file mode 100644 index 64a850c744..0000000000 --- a/mlx/backend/cuda/graph_root.cu +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright © 2026 Apple Inc. - -namespace mlx::core::cu { - -__global__ void graph_root_kernel() {} - -void* graph_root_kernel_ptr() { - return reinterpret_cast(&graph_root_kernel); -} - -} // namespace mlx::core::cu diff --git a/mlx/backend/cuda/kernel_utils.cu b/mlx/backend/cuda/kernel_utils.cu index 14eae1a48e..0ed0d77a0e 100644 --- a/mlx/backend/cuda/kernel_utils.cu +++ b/mlx/backend/cuda/kernel_utils.cu @@ -5,6 +5,16 @@ namespace mlx::core { +namespace cu { + +__global__ void noop() {} + +void* get_noop_kernel_ptr() { + return reinterpret_cast(&noop); +} + +} // namespace cu + dim3 get_block_dims(int dim0, int dim1, int dim2, int pow2) { Dims dims = get_block_dims_common(dim0, dim1, dim2, pow2); return dim3(std::get<0>(dims), std::get<1>(dims), std::get<2>(dims));