diff --git a/mlx/backend/cuda/device.cpp b/mlx/backend/cuda/device.cpp index 6e40b34038..52363446b7 100644 --- a/mlx/backend/cuda/device.cpp +++ b/mlx/backend/cuda/device.cpp @@ -12,6 +12,9 @@ namespace mlx::core::cu { +// Defined in kernel_utils.cu +void* get_noop_kernel_ptr(); + namespace { bool use_cuda_graphs() { @@ -157,6 +160,11 @@ void CommandEncoder::insert_graph_dependencies(std::vector nodes) { } } active_deps_.clear(); + if (force_single_root_ && deps.empty()) { + for (auto& node : nodes) { + roots_.push_back(node.node); + } + } for (auto o : active_outputs_) { for (auto& node : nodes) { @@ -207,7 +215,12 @@ 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); worker_->start(); } @@ -506,6 +519,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 = get_noop_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 +606,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..6bc64d8df7 100644 --- a/mlx/backend/cuda/device.h +++ b/mlx/backend/cuda/device.h @@ -159,8 +159,10 @@ class MLX_API CommandEncoder { std::vector active_deps_; std::vector active_outputs_; std::unordered_map node_map_; + 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/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));