Skip to content

[CUDA] Serialize graph kernels on consumer Blackwell (sm_120) to fix copy_s illegal write - #4699

Closed
zhanpengwang2000 wants to merge 1 commit into
ml-explore:mainfrom
zhanpengwang2000:cuda-sm120-graph-serial
Closed

zhanpengwang2000 wants to merge 1 commit into
ml-explore:mainfrom
zhanpengwang2000:cuda-sm120-graph-serial

Conversation

@zhanpengwang2000

Copy link
Copy Markdown

What

Enable graph-node serialization by default on consumer Blackwell (sm_120),
in addition to DGX Spark (sm_121), to fix intermittent output corruption from
a CUDA-graph allocation/ordering race. Adds MLX_CUDA_GRAPH_SERIAL to override.

Why

On sm_120 the graph op budget is wide (100, see get_graph_limits), so
data-independent graph nodes run concurrently. The CommandEncoder only adds
graph edges for input-buffer data dependencies (insert_graph_dependencies),
so a freshly cudaMallocAsync'd output buffer can be written by a copy kernel
node that has no in-graph dependency on its allocation. compute-sanitizer
pinpoints it:

Invalid __global__ write of size 16 bytes
  at void mlx::core::cu::copy_s<float, float, unsigned int, (int)4>(...)+0x430
  by thread (0,0,0) in block (0,0,0)
  Access to 0x... is potentially made before memory is allocated
  and is inside the nearest allocation of size 128 bytes
  Saved host backtrace ... cuGraphLaunch -> get_command_encoder
    -> Scheduler::notify_task_completion -> eval

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 index
overrun — 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 via
MLX_CUDA_GRAPH_SERIAL. Serialization only adds dependency edges, so output
is bit-identical; it trades intra-graph concurrency for correctness on the
affected architectures.

Testing

  • Reproduced and localized with compute-sanitizer --tool memcheck --kernel-name kernel_substring=copy_s on 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 without
    CUDA_LAUNCH_BLOCKING=1.
  • python python/tests/run.py passing locally. (TODO: fill in once run.)

Notes

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
zhanpengwang2000 marked this pull request as ready for review October 14, 2026 21:23

@zcbenz zcbenz left a comment

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.

This is hiding a bug that we actually want to fix, and since we are going to take #4697 I'm also closing this PR.

@zcbenz zcbenz closed this Oct 15, 2026
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.

3 participants