Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion mlx/backend/cuda/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace mlx::core::cu {

// Defined in kernel_utils.cu
void* get_noop_kernel_ptr();

namespace {

bool use_cuda_graphs() {
Expand Down Expand Up @@ -157,6 +160,11 @@ void CommandEncoder::insert_graph_dependencies(std::vector<GraphNode> 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) {
Expand Down Expand Up @@ -207,7 +215,12 @@ CommandEncoder::CommandEncoder(Device& d)
stream_(d),
graph_(d),
worker_(std::make_shared<Worker>(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();
}
Expand Down Expand Up @@ -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, &params));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would an empty node do the work? i.e. cudaGraphAddEmptyNode

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it doesn't work, and has the same slower performance as multi-root

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(
Expand Down Expand Up @@ -579,6 +606,7 @@ void CommandEncoder::clear_graph_state() {
active_deps_.clear();
active_outputs_.clear();
concurrent_nodes_.clear();
roots_.clear();
is_graph_updatable_ = true;
}

Expand Down
2 changes: 2 additions & 0 deletions mlx/backend/cuda/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ class MLX_API CommandEncoder {
std::vector<std::uintptr_t> active_deps_;
std::vector<std::uintptr_t> active_outputs_;
std::unordered_map<std::uintptr_t, GraphNode> node_map_;
std::vector<cudaGraphNode_t> 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_;
};
Expand Down
10 changes: 10 additions & 0 deletions mlx/backend/cuda/kernel_utils.cu
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

namespace mlx::core {

namespace cu {

__global__ void noop() {}

void* get_noop_kernel_ptr() {
return reinterpret_cast<void*>(&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));
Expand Down
Loading