Repository navigation
[reland][tools] Fix CUDA Graph dependency arrows - #5771
Conversation
## Summary (human-written) Fix a bug of cudagraph dependency arrow. The AI summary shows an example that can cause errors. This PR fixes the issue. ## Why The trace compactor matches GPU dependency arrows to kernel start times. An arrow's source is at the source kernel's end. This can attach the arrow to the next kernel or make compaction fail. ```text Kernel A: [10, 12] Kernel B: [12, 14] At time 12, an arrow source belongs to A and a destination belongs to B. ``` ## What changed - Match GPU arrow sources to slice ends and destinations to slice starts. - Accept one adjacent floating-point value when matching a source timestamp. This handles rounding in timestamp + duration. - Keep errors for endpoints that cannot be matched. ## Numerics / performance This tool processes saved profiler traces. Kernel timestamps and durations are preserved. Training code is outside the change. ## Testing - Six tests pass: `python -m unittest discover -s .claude/skills/cuda_graph_trace_compaction/scripts -p test_compact_cuda_graph_trace.py`. - The regression test checks adjacent kernels, a rounded source timestamp, and preservation of the arrow timestamp. - Saved Muse Glimmer trace validation maps all 4228 and 3372 GPU flow endpoints. stack-info: PR: #5771, branch: fegin/stack/7
|
@claude review this pr |
There was a problem hiding this comment.
Verdict: Approve -- GPU-to-GPU dependency arrows now start where the source kernel ends, which fixes the case where two kernels touch at the same timestamp. The change is small and only affects the trace-compaction skill script.
What this PR does. Before this PR, every GPU ac2g flow endpoint was matched to the start time of a slice on the same stream. A GPU dependency arrow's source (ph == "s") sits at its source kernel's end. So when kernel A ends at the same timestamp kernel B starts, the source landed on B's lane. If there was no matching start, compaction raised an error. Now sources are looked up by slice end, allowing one ULP either way because ts + dur can round, and destinations are still looked up by slice start. Endpoints that still don't match keep raising.
Example.
stream 1: A=AllGather [10, 12] -> lane nccl_all_gather
B=compute [12, 13] -> lane compute
flow s @ 12, flow f @ 12
before: s -> compute lane (B's start); s @ nextafter(12) -> ValueError
after: s -> nccl_all_gather lane (A's end); f -> compute lane (B's start)
The new test covers both "before" outcomes: the cross-group lanes make the first one fail the tid assertion, and the second one raises.
Should consider
compact_cuda_graph_trace.py:554-568-- buildend_pointsin the existing loop instead of a second comprehension (inline suggestion).compact_cuda_graph_trace.py:563--end_points(likestart_pointsbefore it) silently keeps only the last slice when two slices on one stream share a key. With real streams this needs a zero-duration slice that sits exactly at a neighbor's end, so it is unlikely. If it happens, the arrow lands on the wrong lane without an error. Raising on a duplicate key would fit the tool's raise-on-unmapped-endpoint policy, but it's optional.
Details
- Necessity gate: the problem is real (adjacent kernels on one stream are common in CUDA graph replays), the fix is in the layer that owns the mapping, and nothing new is added beyond the end-point index. Only the skill script and its colocated test change; core training code is not touched.
- One-ULP tolerance: comparing
fl(ts + dur)with the parsed end timestamp gives an error of at most about 1.5 ULP of the end value whenduris much smaller thants, which is the usual case with absolute profiler timestamps. Since both values sit on the float grid, that comes to at most 1 ULP. Only whenduris about as large asts(timestamps close to zero) could the gap reach 2 ULP, and that case still raises instead of misplacing the arrow. ph == "t"(step) events, if any appear, still go through start matching, the same as before.
Not verified: I didn't run the unit tests or the saved Muse Glimmer trace validation, because executing scripts was blocked in this sandbox.
| start_points = {} | ||
| for item in assignments: | ||
| by_old_stream[(item.pid, item.old_tid)].append(item) | ||
| exact_points[(item.pid, item.old_tid, item.start)] = item.new_tid | ||
| start_points[(item.pid, item.old_tid, item.start)] = item.new_tid | ||
| for values in by_old_stream.values(): | ||
| values.sort(key=lambda item: item.start) | ||
| starts = { | ||
| key: [item.start for item in values] for key, values in by_old_stream.items() | ||
| } | ||
| return starts, by_old_stream, exact_points | ||
| end_points = { | ||
| (item.pid, item.old_tid, item.end): item.new_tid | ||
| for values in by_old_stream.values() | ||
| for item in values | ||
| } | ||
| return starts, by_old_stream, start_points, end_points |
There was a problem hiding this comment.
Should consider: end_points can be filled in the existing for item in assignments loop next to start_points. That removes the second nested comprehension over by_old_stream and keeps the two maps built the same way. It saves 4 lines.
| start_points = {} | |
| for item in assignments: | |
| by_old_stream[(item.pid, item.old_tid)].append(item) | |
| exact_points[(item.pid, item.old_tid, item.start)] = item.new_tid | |
| start_points[(item.pid, item.old_tid, item.start)] = item.new_tid | |
| for values in by_old_stream.values(): | |
| values.sort(key=lambda item: item.start) | |
| starts = { | |
| key: [item.start for item in values] for key, values in by_old_stream.items() | |
| } | |
| return starts, by_old_stream, exact_points | |
| end_points = { | |
| (item.pid, item.old_tid, item.end): item.new_tid | |
| for values in by_old_stream.values() | |
| for item in values | |
| } | |
| return starts, by_old_stream, start_points, end_points | |
| start_points = {} | |
| end_points = {} | |
| for item in assignments: | |
| by_old_stream[(item.pid, item.old_tid)].append(item) | |
| start_points[(item.pid, item.old_tid, item.start)] = item.new_tid | |
| end_points[(item.pid, item.old_tid, item.end)] = item.new_tid | |
| for values in by_old_stream.values(): | |
| values.sort(key=lambda item: item.start) | |
| starts = { | |
| key: [item.start for item in values] for key, values in by_old_stream.items() | |
| } | |
| return starts, by_old_stream, start_points, end_points |
Stacked PRs:
[tools] Fix CUDA Graph dependency arrows
Reland #5743, accidentally land #5743 to an incorrect branch.
Summary (human-written)
Fix a bug of cudagraph dependency arrow. The AI summary shows an example
that can cause errors. This PR fixes the issue.
Why
The trace compactor matches GPU dependency arrows to kernel start times. An arrow's source is at the source kernel's end. This can attach the arrow to the next kernel or make compaction fail.
What changed
Numerics / performance
This tool processes saved profiler traces. Kernel timestamps and durations are preserved. Training code is outside the change.
Testing
python -m unittest discover -s .claude/skills/cuda_graph_trace_compaction/scripts -p test_compact_cuda_graph_trace.py.