Skip to content

[reland][tools] Fix CUDA Graph dependency arrows - #5771

Merged
fegin merged 1 commit into
mainfrom
fegin/stack/7
Nov 3, 2026
Merged

fegin merged 1 commit into
mainfrom
fegin/stack/7

Conversation

@fegin

@fegin fegin commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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.

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.

## 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
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 3, 2026
@fegin fegin changed the title [tools] Fix CUDA Graph dependency arrows [reland][tools] Fix CUDA Graph dependency arrows Nov 3, 2026
@pytorch-bot pytorch-bot Bot added the ci-no-td label Nov 3, 2026
@fegin
fegin marked this pull request as ready for review November 3, 2026 08:21
@fegin

fegin commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@claude review this pr

@claude

claude Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @fegin's task in 2m 17s —— View job


Approve -- review posted.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 -- build end_points in the existing loop instead of a second comprehension (inline suggestion).
  • compact_cuda_graph_trace.py:563 -- end_points (like start_points before 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 when dur is much smaller than ts, 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 when dur is about as large as ts (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.

Comment on lines +554 to +568
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

@fegin
fegin merged commit b0b0a61 into main Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants