Skip to content

[tools] Fix CUDA Graph dependency arrows - #5743

Merged
fegin merged 1 commit into
fegin/stack/2from
fegin/stack/3
Nov 3, 2026
Merged

fegin merged 1 commit into
fegin/stack/2from
fegin/stack/3

Conversation

@fegin

@fegin fegin commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[tools] Fix CUDA Graph dependency arrows

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.

@fegin

fegin commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@claude review the pr

@fegin
fegin marked this pull request as ready for review November 2, 2026 20:36
@claude

claude Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @fegin's task in 1m 40s —— View job


Approve -- see review (one optional inline suggestion).

@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 -- This fixes a real bug: a GPU dependency arrow can end up on the wrong lane, or compaction can fail. The fix is small, and the regression test fails on the base branch.

What this PR does. Before this PR, the compactor matched every GPU ac2g flow endpoint to a slice by its start time. A flow source (ph == "s") sits at the end of its source slice, though. Now sources are matched against slice ends, accepting the exact value or one float ulp on either side. Destinations still match slice starts. Endpoints that can't be matched still raise ValueError.

Example.

stream 1: A = ncclAllGather [10, 12]  -> lane "NCCL all-gather"
          B = compute       [12, 13]  -> lane "Compute"
flow s @ ts=12 (A -> ...), flow f @ ts=12

before: s looked up by start=12 -> B's lane (silently wrong arrow)
        s @ nextafter(12)        -> no match -> ValueError
after:  s looked up by end=12 (+/-1 ulp) -> A's lane; f -> B's lane

Should consider

  • compact_cuda_graph_trace.py:554 -- end_points can be built in the same loop as start_points (see the inline suggestion).
Details
  • Gate: the bug is real on the base branch. With only start-keyed lookup, the new test's first subtest maps the source to the compute lane, and the second subtest raises. The change is local to the skill script; no core code, numerics, or CI are affected.
  • The +/-1 ulp tolerance is enough if the profiler writes ts/dur as exact decimal microseconds from integer ns. In that case float(ts) + float(dur) and float(ts_flow) differ by at most ~1.5 ulp of the result, which rounds to one representable step. Wider drift would still raise rather than mis-map.
  • The test lives with the skill, reuses the existing _kernel/_flow helpers, and puts A and B in different semantic groups. That way a wrong lane assignment actually shows up.

Not verified: I did not run the unit tests or check the Muse Glimmer trace. The CI checkout didn't have the PR's files in its working tree.

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.

Optional: the second pass over by_old_stream doesn't depend on the sort, so end_points can be filled in the same loop as start_points. That keeps the two lookup tables next to each other.

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

## 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: #5743, branch: fegin/stack/3
@fegin
fegin marked this pull request as draft November 2, 2026 20:44
@fegin
fegin changed the base branch from fegin/stack/2 to main November 2, 2026 20:44
@fegin
fegin changed the base branch from main to fegin/stack/2 November 2, 2026 20:45
@fegin
fegin marked this pull request as ready for review November 2, 2026 20:45
fegin added a commit that referenced this pull request Nov 3, 2026
Stacked PRs:
 * #5746
 * #5745
 * #5744
 * #5743
 * __->__#5742


--- --- ---

[docs] Clarify PyTorch library ownership

## Summary (human-written)
Agents are confused by what "PyTorch libraries" means. They may treat
meta-pytorch repos as non-pytorch repo and request changes if we
use repos in the core parallelisms, which violates one of the core
principal.

## Why
The dependency rule does not define PyTorch libraries. This can
incorrectly exclude libraries such as meta-pytorch/attention-gym.

## What changed
- Count pytorch/pytorch and meta-pytorch repos as PyTorch libraries.
- Keep complex techniques upstream, in pytorch/pytorch or a suitable
meta-pytorch repo.

## Testing
Documentation only. `pre-commit run --files .claude/CLAUDE.md` passes.
@fegin
fegin merged commit 5bc5c9c into fegin/stack/2 Nov 3, 2026
fegin added a commit that referenced this pull request Nov 3, 2026
Stacked PRs:
 * #5746
 * #5745
 * #5744
 * __->__#5771


--- --- ---

[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.

```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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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