Skip to content
Closed
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
24 changes: 24 additions & 0 deletions mlx/backend/cuda/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ void CommandEncoder::insert_graph_dependencies(std::vector<GraphNode> nodes) {
}
}
}
if (serial_graph_) {
// Each node also waits for the one added before it, so the graph runs
// its kernels one at a time (see get_graph_serial).
for (auto& prev : last_nodes_) {
auto [_, inserted] = set_deps.insert(prev.node);
if (inserted) {
deps.push_back(prev);
}
}
last_nodes_ = nodes;
}
}
active_deps_.clear();

Expand Down Expand Up @@ -202,13 +213,25 @@ std::pair<int, int> get_graph_limits(Device& d) {
return {env::max_ops_per_buffer(ops), env::max_mb_per_buffer(mb)};
}

// Whether a graph's kernels run one at a time; can be set with
// MLX_CUDA_GRAPH_SERIAL. On consumer Blackwell (sm_120) concurrent graph
// nodes expose an allocation->first-writer ordering hazard (illegal write in
// copy_s under compute-sanitizer, corrupted output); serializing removes the
// window and is bit-identical. Default on for sm_120 and DGX Spark (sm_121).
bool get_graph_serial(Device& d) {
auto cc =
d.compute_capability_major() * 100 + d.compute_capability_minor() * 10;
return env::get_var("MLX_CUDA_GRAPH_SERIAL", cc == 1200 || cc == 1210);
}

CommandEncoder::CommandEncoder(Device& d)
: device_(d),
stream_(d),
graph_(d),
worker_(std::make_shared<Worker>(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);
serial_graph_ = get_graph_serial(d);
worker_->start();
}

Expand Down Expand Up @@ -579,6 +602,7 @@ void CommandEncoder::clear_graph_state() {
active_deps_.clear();
active_outputs_.clear();
concurrent_nodes_.clear();
last_nodes_.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 @@ -154,6 +154,7 @@ class MLX_API CommandEncoder {
std::string graph_nodes_key_;
std::string graph_deps_key_;
std::vector<GraphNode> concurrent_nodes_;
std::vector<GraphNode> last_nodes_;
std::vector<std::shared_ptr<array::Data>> temporaries_;
LRUCache<std::string, CudaGraphExec> graph_cache_;
std::vector<std::uintptr_t> active_deps_;
Expand All @@ -163,6 +164,7 @@ class MLX_API CommandEncoder {
bool is_graph_updatable_{true};
int max_ops_per_graph_;
int max_mb_per_graph_;
bool serial_graph_{false};
};

class MLX_API Device {
Expand Down