Skip to content

[CUDA] Avoid multi-root graphs on DGX/RTX spark - #4745

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
dhiltgen:cuda-graph-single-root
Oct 25, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
dhiltgen:cuda-graph-single-root

Conversation

@dhiltgen

Copy link
Copy Markdown
Contributor

Related to #4697

I've been trying to figure out the slowdown on RTX/DGX Spark systems with CUDA graphs. What appears to be going on is these systems are much less efficient when there are multiuple roots in the CUDA graph.

Instead of serializing the graphs, this PR aims to mitigate this by adding a single dummy root kernel in cases where MLX would have created multiple root nodes. This is limited to just CC 12.1 to avoid any impact to other systems which don't exhibit the negative impact with multiple roots. This achives significant speedup on prompt and modest generation performance improvements on these systems.

Prompt processing (tok/s)

mlx-lm benchmarks

Gemma 4 26B-A4B (nvfp4)

GPU prompt main branch change
DGX Spark 512 743.7 1095.0 +47.2%
DGX Spark 2048 703.6 1061.6 +50.9%
DGX Spark 8192 675.7 1021.0 +51.1%

Qwen 3.6 35B-A3B (nvfp4)

GPU prompt main branch change
DGX Spark 512 271.7 364.7 +34.3%
DGX Spark 2048 285.3 370.8 +29.9%
DGX Spark 8192 276.6 363.1 +31.3%

Qwen 3.6 27B (nvfp4)

GPU prompt main branch change
DGX Spark 512 107.1 155.8 +45.4%
DGX Spark 2048 105.7 154.2 +45.9%
DGX Spark 8192 105.1 151.8 +44.5%

Generation (tok/s)

128 token generations:

Gemma 4 26B-A4B (nvfp4)

GPU prompt main branch change
DGX Spark 512 62.8 67.6 +7.7%
DGX Spark 2048 57.4 64.5 +12.4%
DGX Spark 8192 54.1 57.4 +6.2%

Qwen 3.6 35B-A3B (nvfp4)

GPU prompt main branch change
DGX Spark 512 54.3 55.4 +2.2%
DGX Spark 2048 52.3 54.7 +4.7%
DGX Spark 8192 51.2 55.0 +7.6%

Qwen 3.6 27B (nvfp4)

GPU prompt main branch change
DGX Spark 512 13.5 13.7 +1.9%
DGX Spark 2048 13.6 13.8 +1.7%
DGX Spark 8192 12.5 13.2 +5.5%
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: co-developed with coding agent

@dhiltgen
dhiltgen force-pushed the cuda-graph-single-root branch from d30f34c to 127f757 Compare October 22, 2026 15:37
@dhiltgen

Copy link
Copy Markdown
Contributor Author

Prompt processing (tok/s)

mlx-lm benchmarks

Gemma 4 26B-A4B (nvfp4)

GPU prompt main branch change
RTX Spark 512 884.7 986.3 +11.5%
RTX Spark 2048 972.9 971.1 -0.2%
RTX Spark 8192 933.5 949.2 +1.7%

Qwen 3.6 35B-A3B (nvfp4)

GPU prompt main branch change
RTX Spark 512 167.1 198.7 +18.9%
RTX Spark 2048 236.0 347.5 +47.2%
RTX Spark 8192 232.9 342.7 +47.1%

Qwen 3.6 27B (nvfp4)

GPU prompt main branch change
RTX Spark 512 103.8 137.5 +32.4%
RTX Spark 2048 96.3 125.8 +30.6%
RTX Spark 8192 101.8 131.6 +29.3%

Generation (tok/s)

128 token generations:

Gemma 4 26B-A4B (nvfp4)

GPU prompt main branch change
RTX Spark 512 37.8 73.7 +94.7%
RTX Spark 2048 36.1 68.0 +88.6%
RTX Spark 8192 34.5 62.0 +79.6%

Qwen 3.6 35B-A3B (nvfp4)

GPU prompt main branch change
RTX Spark 512 33.5 66.2 +97.7%
RTX Spark 2048 32.9 65.2 +98.1%
RTX Spark 8192 32.2 60.0 +86.2%

Qwen 3.6 27B (nvfp4)

GPU prompt main branch change
RTX Spark 512 14.1 16.4 +15.7%
RTX Spark 2048 13.8 16.2 +17.8%
RTX Spark 8192 13.5 15.8 +16.8%

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

@zcbenz
zcbenz force-pushed the cuda-graph-single-root branch from 127f757 to 3838495 Compare October 25, 2026 10:10
@zcbenz
zcbenz merged commit 1e547d8 into ml-explore:main Oct 25, 2026
30 checks passed
@dhiltgen
dhiltgen deleted the cuda-graph-single-root branch October 25, 2026 18:33
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
Brooooooklyn added a commit to mlx-node/mlx that referenced this pull request Nov 29, 2026
…aph limits, and Win32 NVRTC/PTX fixes

Adapted for upstream:
- ml-explore#4885: use private cache_memory_locked() and memory_limit_locked() while
  holding mutex_ in malloc_async()/free() to avoid recursive mutex deadlock,
  preserving overflow asserts.
- ml-explore#4868: preserve move_to_device_memory() / cuda_malloc_async() / cuda_free_async().
- ml-explore#4711: preserve debug track_allocation/untrack_allocation and registered_data_ptrs_.
- ml-explore#4706: preserve BIN_DIR macro and try/catch in delayload.cpp alongside
  resolve_bin_dir(), std::error_code guards, and unconditional nvrtc-builtins preload.
- ml-explore#4745: preserve force_single_root_/roots_/get_noop_kernel_ptr() in cuda/device.{h,cpp}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants