Repository navigation
[CUDA] Serialize graph kernels on consumer Blackwell (sm_120) to fix copy_s illegal write - #4699
Closed
zhanpengwang2000 wants to merge 1 commit into
Closed
zhanpengwang2000 wants to merge 1 commit into
zhanpengwang2000 wants to merge 1 commit into
Conversation
On consumer Blackwell (sm_120) the CUDA graph is given a wide op budget
(100), so data-independent nodes run concurrently. A freshly
cudaMallocAsync'd output buffer can then be written by a copy kernel node
that has no in-graph dependency on its allocation: compute-sanitizer reports
an illegal 16-byte __global__ write in copy_s ("Access ... made before
memory is allocated", nearest allocation 128 bytes, launched via
cuGraphLaunch), and inference output is intermittently corrupted. The
identical build is correct on Ampere (sm_86). Forcing serialization via
CUDA_LAUNCH_BLOCKING=1, attaching Nsight Systems, or MLX_USE_CUDA_GRAPHS=0
all eliminate it, confirming a timing/ordering race.
Add an optional graph serialization (MLX_CUDA_GRAPH_SERIAL) that chains each
graph node after the previously added node, default-on for consumer
Blackwell (sm_120) and DGX Spark (sm_121). It only adds dependency edges, so
output is bit-identical.
zhanpengwang2000
marked this pull request as ready for review
October 14, 2026 21:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Enable graph-node serialization by default on consumer Blackwell (
sm_120),in addition to DGX Spark (
sm_121), to fix intermittent output corruption froma CUDA-graph allocation/ordering race. Adds
MLX_CUDA_GRAPH_SERIALto override.Why
On
sm_120the graph op budget is wide (100, seeget_graph_limits), sodata-independent graph nodes run concurrently. The
CommandEncoderonly addsgraph edges for input-buffer data dependencies (
insert_graph_dependencies),so a freshly
cudaMallocAsync'd output buffer can be written by a copy kernelnode that has no in-graph dependency on its allocation.
compute-sanitizerpinpoints it:
This corrupts adjacent VRAM → wrong logits → fluent-but-off-task, runaway LLM
output. It is intermittent and arch-specific: the identical build is
correct on Ampere (
sm_86). It is a timing/ordering race, not an indexoverrun — it disappears under
CUDA_LAUNCH_BLOCKING=1, under Nsight Systems,and with
MLX_USE_CUDA_GRAPHS=0.How
Reuses the serialization mechanism (chain each node after the previously added
one) and gates it on
cc == 1200 || cc == 1210, overridable viaMLX_CUDA_GRAPH_SERIAL. Serialization only adds dependency edges, so outputis bit-identical; it trades intra-graph concurrency for correctness on the
affected architectures.
Testing
compute-sanitizer --tool memcheck --kernel-name kernel_substring=copy_son an RTX 5070 Ti Laptop (sm_120,driver 595.79). After this change, memcheck no longer reports the
copy_s"before memory is allocated" write, and output is correct withoutCUDA_LAUNCH_BLOCKING=1.python python/tests/run.pypassing locally. (TODO: fill in once run.)Notes
correctness fix and extends the default gate to
sm_120. Happy to rebase on /fold into Run CUDA graph kernels one at a time on DGX Spark #4697 if preferred.
open to suggestions (e.g. a sm_120-gated test asserting correctness under the
default graph path).