Repository navigation
[c10d][nccl2] Give the profiler a per-group collective sequence number - #192114
Closed
tushar00jain wants to merge 5 commits into
Closed
tushar00jain wants to merge 5 commits into
tushar00jain wants to merge 5 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/192114
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit e46d3fb with merge base dac77b5 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Aug 4, 2026
Closed
Closed
Closed
d4l3k
approved these changes
Aug 5, 2026
d4l3k
left a comment
Member
There was a problem hiding this comment.
Re-approving due to switching to ghstack from sl
Collaborator
|
Starting merge as part of PR stack under #192115 |
pytorchmergebot
pushed a commit
that referenced
this pull request
Aug 6, 2026
…g commName (#192115) Summary: nccl2's split() copies the parent's timeout, config, group name, group description and abort policy into the child's Options, but never records the child's membership in world-rank terms. Stock ProcessGroupNCCL::split() fills global_ranks_in_group; nccl2 left it empty. nccl2 never reads that map itself, and get_process_group_ranks() does not go through it, so this is observability fidelity rather than a correctness bug. The one consumer is the opt-in FlightRecorderHook, which falls back to identity ranks when the map is absent -- so a child group's recorded membership claimed world ranks it does not contain. A second-level split was worse: the ranks it was handed are indices into the parent, so without the parent's map there is nothing to resolve them against. The child now maps each requested rank through *this* group's own global_ranks_in_group rather than the Options it was passed. That distinction matters after the per-backend Options change earlier in this stack ("Give each backend its own Options when splitting or merging a process group"): the Options reaching split() is a clone owned by the caller, while the live map belongs to the group. An empty map means "spans the world in rank order", and a merged group inherits its parent's shorter map, so a map shorter than getSize() is treated the same way instead of being indexed out of bounds -- the same read that the NCCL split fix earlier in this stack closed. Second, smaller change in the same function. config.commName was assigned name.c_str() and then stored into the child's Options, leaving the child holding a pointer that outlives the string it borrows. This is latent, not live: every consumer of a stored config assigns commName from a live string before handing it to NCCL -- createNcclComm(), reconfigure(), and split() itself -- and populateNcclConfigFromHints() borrows from the caller's live hints map rather than from a stored one. Storing nullptr records what the child actually owns instead of a pointer that happens never to be read. Both are nccl2's own defects. Stock is unaffected: its split() already records the rank map, and it does not stash a borrowed commName. Test Plan: test_split_group_child_global_ranks in test/distributed/test_c10d_nccl2.py (requires_nccl, 2 GPUs) splits a 2-rank world into halves and asserts: python test/distributed/test_c10d_nccl2.py \ ProcessGroupNCCL2ObservabilityTest.test_split_group_child_global_ranks -v - the child's global_ranks_in_group is its own half's world ranks - a second-level split maps through the parent: the grandchild reports [self.rank], which the identity fallback would report as [0] - config.comm_name is not stored on the child - an all_reduce on the child still produces sum(halves), i.e. the added bookkeeping does not disturb the communicator Pre-fix the first assertion fails with "mismatch: 0 != 2". Recorded suite runs with this change present: test_c10d_nccl2.py Ran 24 tests OK test_c10d_nccl.py Ran 285 tests OK (skipped=15) Authored with the assistance of an AI coding agent. Pull Request resolved: #192115 Approved by: https://github.com/d4l3k ghstack dependencies: #192114
ankitbhrdwj
pushed a commit
to ankitbhrdwj/pytorch
that referenced
this pull request
Aug 12, 2026
pytorch#192114) Summary: nccl2's TracingGuard kept a counter of its own: // TracingGuard.hpp:80 inline static int sequence_number_ = 0; // TracingGuard.cpp, inside the RecordFunction param list std::make_tuple(++sequence_number_, false), Three things are wrong with that, and the third is the one that matters. It is process-global where stock keeps a per-PG counter, so with more than one nccl2 PG in a process a rank's numbering depends on how it interleaved its *other* groups' collectives. Profilers pair collectives across ranks by (process group, sequence number), so they cannot match them. It increments once per *guard*, not once per collective. Every nccl2 collective constructs two guards -- the collective itself, then "wait" in WorkNCCL::synchronizeInternal -- plus one per "init". So even in a single-PG, single-threaded process the recorded numbers were twice the collective count and did not agree with the work handle's own getSequencenumber(). And the value never reached the consumer that matters. torch/csrc/profiler/util.cpp:570 reads the sequence number off ParamCommsDebugInfo, not off the param list: auto seqNum = debugInfo->getSequenceNumber(); if (seqNum >= 0) { metadata.emplace(kSeqNum, seqNum); uint64_t comms_id = static_cast<uint64_t>(c10::get_hash( debugInfo->getProcessGroupName(), seqNum, debugInfo->getIsP2P(), ...)); metadata.emplace(kCommsId, comms_id); } Stock populates that field with torch::maybeSetSequenceInfo(paramCommsInfo, seq) inside RECORD_PARAM_COMMS[_DATA] (ParamCommsUtils.hpp:159,236). TracingGuard never called it, so sequenceNumber_ stayed at its -1 default and no Seq and no Comms Id metadata was emitted for any nccl2 collective at all -- the cross-rank matching key was simply absent from every trace. The process-global counter only ever reached the raw RecordFunction inputs, where tuples do not survive chrome-trace export (they serialize as '') but are serialized by the Execution Trace observer, which is how the numbering itself is visible in the Test Plan below. This defect is nccl2's own, not one inherited from stock: TracingGuard is nccl2 code, and stock's RECORD_PARAM_COMMS macros have always both filled the param list and called maybeSetSequenceInfo. The fix follows stock's shape, which supplies the number as an argument at each record site (ProcessGroupNCCL.cpp passes isP2P ? seqP2P_ : seqCollective_, and seqCollective_ + 1 at the ~20 RECORD_PARAM_COMMS_DATA sites). nccl2 already had the right value -- the per-PG uint64_t sequence_number_ on ProcessGroupNCCL, bumped once per collective in the Backend override and stamped onto the work handle -- so: - TracingGuard's four constructors and initializeTracingCommon take a uint64_t sequence_number, and the inline static member is deleted. - the 18 call sites in nccl2/ProcessGroupNCCL.cpp pass sequence_number_. They are all ProcessGroupNCCL members, so nothing had to be plumbed. - WorkNCCL::synchronizeInternal's "wait" guard passes the work's own seq_, so the "wait" record carries the same number as the collective it waits on instead of burning a fresh one. - initializeTracingCommon calls setSequenceInfo(seq, false) on the debug info before installing the DebugInfoGuard, which is what makes Seq and Comms Id appear. - isP2P stays false everywhere. nccl2 counts p2p and collectives in the one counter and maintains no separate p2p sequence space, so claiming true would advertise a numbering it does not keep. Deliberately unchanged here. Nothing else about tracing is restructured; this is profiler metadata only. Concurrency, before and after. Before: one inline static int -- static storage duration, no atomicity -- shared by every TracingGuard in the process, i.e. by every nccl2 PG and every thread, and read-modify-written by ++sequence_number_ on whatever thread issues a collective, send/recv, barrier, init or Work::wait(). nccl2 supports multi-threaded issue (its own locking covers memory_registration_mutex_, event_pool_mutex_ and the work queue) and none of the guard's callers holds any common lock at that point: the *Impl entry points construct the guard before touching the queue, and synchronizeInternal runs on the waiter's thread. Two threads issuing on two different PGs -- the normal case for overlapping comm streams -- therefore raced on one int, losing increments on a torn read-modify-write, and it is a data race by the letter of the standard (UB, TSAN-visible) whatever the hardware happens to do. After: the counter is gone, so no new shared mutable state and no new synchronization requirement is introduced. The guard only *reads* the issuing PG's own sequence_number_; the increment stays in the Backend override, on the calling thread, which is already where work->setSequenceNumber() read it. A caller that issues on one PG from two threads already had to serialize (that pre-existing per-PG ++ is not atomic either, and is out of scope here), while the far more common two-PGs-two-threads case is now race-free where it previously raced on a process-global static. The "wait" guard reads WorkNCCL::seq_, written once by createWork() on the issuing thread before the work is published to the queue and never mutated afterwards. Cross-rank ordering is unaffected: every rank increments its per-PG counter on the same collective in the same order, because the increment sits in the collective entry point, and no collective or lockstep property depends on the guard. Test Plan: ProcessGroupNCCL2ObservabilityTest::test_profiler_sequence_number_is_per_group (4 ranks) puts two nccl2 groups in one process, runs three all_reduce's on the first and one on the second inside a profiled region, and asserts that each group's recorded Seq values are its own counter's next N values. CUDA_VISIBLE_DEVICES=0,1,2,3 python test/distributed/test_c10d_nccl2.py \ ProcessGroupNCCL2ObservabilityTest -v test_profiler_sequence_number_is_per_group ... ok With the C++ half reverse-applied and rebuilt, the same test fails as AssertionError: The length of the sequences mismatch: 0 != 3 i.e. no Seq metadata was recorded at all. The numbering itself is visible through the Execution Trace observer, which does serialize the tuple. A 1-rank probe with two PGs and 3 + 1 all_reduce's, both groups' own counters standing at 1 before the profiled region: pre-fix metadata 'Seq' per group: {} ET record_param_comms inputs: [..., [ 9, False], ['3087961...', ''], 0, 'all_reduce', ...] ET record_param_comms inputs: [..., [10, False], ['3087961...', ''], 0, 'wait', ...] ET record_param_comms inputs: [..., [11, False], ['3087961...', ''], 0, 'all_reduce', ...] ET record_param_comms inputs: [..., [12, False], ['3087961...', ''], 0, 'wait', ...] ET record_param_comms inputs: [..., [13, False], ['3087961...', ''], 0, 'all_reduce', ...] ET record_param_comms inputs: [..., [14, False], ['3087961...', ''], 0, 'wait', ...] ET record_param_comms inputs: [..., [15, False], ['e141ae2...', ''], 0, 'all_reduce', ...] ET record_param_comms inputs: [..., [16, False], ['e141ae2...', ''], 0, 'wait', ...] post-fix metadata 'Seq' per group: {'pg0': [2, 3, 4], 'pg1': [2]} ET record_param_comms inputs: [..., [5, False], ['3087961...', ''], 0, 'all_reduce', ...] ET record_param_comms inputs: [..., [5, False], ['3087961...', ''], 0, 'wait', ...] ET record_param_comms inputs: [..., [6, False], ['3087961...', ''], 0, 'all_reduce', ...] ET record_param_comms inputs: [..., [6, False], ['3087961...', ''], 0, 'wait', ...] ET record_param_comms inputs: [..., [7, False], ['3087961...', ''], 0, 'all_reduce', ...] ET record_param_comms inputs: [..., [7, False], ['3087961...', ''], 0, 'wait', ...] ET record_param_comms inputs: [..., [3, False], ['e141ae2...', ''], 0, 'all_reduce', ...] ET record_param_comms inputs: [..., [3, False], ['e141ae2...', ''], 0, 'wait', ...] Three things to read off that: the second group's collective is numbered 15 pre-fix and 3 post-fix, which is its own counter; each collective and its wait now share one number instead of burning two; and the numbers are the values the work handles carry. Note also that no Seq metadata appears at all pre-fix. Full suites, on 4 idle H100s, serially. Both were run with the second observability test present as well -- the split-child rank-map fix it belongs to lands separately -- so the count here is 22 pre-existing plus 2: CUDA_VISIBLE_DEVICES=0,1,2,3 python test/distributed/test_c10d_nccl2.py -v Ran 24 tests in 135.623s OK CUDA_VISIBLE_DEVICES=0,1,2,3 python test/distributed/test_c10d_nccl.py -v Ran 285 tests in 1797.054s OK (skipped=15) Authored with the assistance of an AI coding agent. Pull Request resolved: pytorch#192114 Approved by: https://github.com/d4l3k
ankitbhrdwj
pushed a commit
to ankitbhrdwj/pytorch
that referenced
this pull request
Aug 12, 2026
…g commName (pytorch#192115) Summary: nccl2's split() copies the parent's timeout, config, group name, group description and abort policy into the child's Options, but never records the child's membership in world-rank terms. Stock ProcessGroupNCCL::split() fills global_ranks_in_group; nccl2 left it empty. nccl2 never reads that map itself, and get_process_group_ranks() does not go through it, so this is observability fidelity rather than a correctness bug. The one consumer is the opt-in FlightRecorderHook, which falls back to identity ranks when the map is absent -- so a child group's recorded membership claimed world ranks it does not contain. A second-level split was worse: the ranks it was handed are indices into the parent, so without the parent's map there is nothing to resolve them against. The child now maps each requested rank through *this* group's own global_ranks_in_group rather than the Options it was passed. That distinction matters after the per-backend Options change earlier in this stack ("Give each backend its own Options when splitting or merging a process group"): the Options reaching split() is a clone owned by the caller, while the live map belongs to the group. An empty map means "spans the world in rank order", and a merged group inherits its parent's shorter map, so a map shorter than getSize() is treated the same way instead of being indexed out of bounds -- the same read that the NCCL split fix earlier in this stack closed. Second, smaller change in the same function. config.commName was assigned name.c_str() and then stored into the child's Options, leaving the child holding a pointer that outlives the string it borrows. This is latent, not live: every consumer of a stored config assigns commName from a live string before handing it to NCCL -- createNcclComm(), reconfigure(), and split() itself -- and populateNcclConfigFromHints() borrows from the caller's live hints map rather than from a stored one. Storing nullptr records what the child actually owns instead of a pointer that happens never to be read. Both are nccl2's own defects. Stock is unaffected: its split() already records the rank map, and it does not stash a borrowed commName. Test Plan: test_split_group_child_global_ranks in test/distributed/test_c10d_nccl2.py (requires_nccl, 2 GPUs) splits a 2-rank world into halves and asserts: python test/distributed/test_c10d_nccl2.py \ ProcessGroupNCCL2ObservabilityTest.test_split_group_child_global_ranks -v - the child's global_ranks_in_group is its own half's world ranks - a second-level split maps through the parent: the grandchild reports [self.rank], which the identity fallback would report as [0] - config.comm_name is not stored on the child - an all_reduce on the child still produces sum(halves), i.e. the added bookkeeping does not disturb the communicator Pre-fix the first assertion fails with "mismatch: 0 != 2". Recorded suite runs with this change present: test_c10d_nccl2.py Ran 24 tests OK test_c10d_nccl.py Ran 285 tests OK (skipped=15) Authored with the assistance of an AI coding agent. Pull Request resolved: pytorch#192115 Approved by: https://github.com/d4l3k ghstack dependencies: pytorch#192114
Closed
1 of 4 tasks
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.
Stack from ghstack (oldest at bottom):
Summary:
nccl2's TracingGuard kept a counter of its own:
Three things are wrong with that, and the third is the one that matters.
It is process-global where stock keeps a per-PG counter, so with more than one
nccl2 PG in a process a rank's numbering depends on how it interleaved its
other groups' collectives. Profilers pair collectives across ranks by (process
group, sequence number), so they cannot match them.
It increments once per guard, not once per collective. Every nccl2 collective
constructs two guards -- the collective itself, then "wait" in
WorkNCCL::synchronizeInternal -- plus one per "init". So even in a single-PG,
single-threaded process the recorded numbers were twice the collective count and
did not agree with the work handle's own getSequencenumber().
And the value never reached the consumer that matters.
torch/csrc/profiler/util.cpp:570 reads the sequence number off
ParamCommsDebugInfo, not off the param list:
Stock populates that field with torch::maybeSetSequenceInfo(paramCommsInfo, seq)
inside RECORD_PARAM_COMMS[DATA] (ParamCommsUtils.hpp:159,236). TracingGuard
never called it, so sequenceNumber stayed at its -1 default and no Seq and no
Comms Id metadata was emitted for any nccl2 collective at all -- the cross-rank
matching key was simply absent from every trace. The process-global counter only
ever reached the raw RecordFunction inputs, where tuples do not survive
chrome-trace export (they serialize as '') but are serialized by the Execution
Trace observer, which is how the numbering itself is visible in the Test Plan
below.
This defect is nccl2's own, not one inherited from stock: TracingGuard is nccl2
code, and stock's RECORD_PARAM_COMMS macros have always both filled the param
list and called maybeSetSequenceInfo.
The fix follows stock's shape, which supplies the number as an argument at each
record site (ProcessGroupNCCL.cpp passes isP2P ? seqP2P_ : seqCollective_, and
seqCollective_ + 1 at the ~20 RECORD_PARAM_COMMS_DATA sites). nccl2 already had
the right value -- the per-PG uint64_t sequence_number_ on ProcessGroupNCCL,
bumped once per collective in the Backend override and stamped onto the work
handle -- so:
uint64_t sequence_number, and the inline static member is deleted.
are all ProcessGroupNCCL members, so nothing had to be plumbed.
the "wait" record carries the same number as the collective it waits on
instead of burning a fresh one.
before installing the DebugInfoGuard, which is what makes Seq and Comms Id
appear.
counter and maintains no separate p2p sequence space, so claiming true would
advertise a numbering it does not keep. Deliberately unchanged here.
Nothing else about tracing is restructured; this is profiler metadata only.
Concurrency, before and after. Before: one inline static int -- static storage
duration, no atomicity -- shared by every TracingGuard in the process, i.e. by
every nccl2 PG and every thread, and read-modify-written by
++sequence_number_ on whatever thread issues a collective, send/recv, barrier,
init or Work::wait(). nccl2 supports multi-threaded issue (its own locking
covers memory_registration_mutex_, event_pool_mutex_ and the work queue) and
none of the guard's callers holds any common lock at that point: the *Impl entry
points construct the guard before touching the queue, and synchronizeInternal
runs on the waiter's thread. Two threads issuing on two different PGs -- the
normal case for overlapping comm streams -- therefore raced on one int, losing
increments on a torn read-modify-write, and it is a data race by the letter of
the standard (UB, TSAN-visible) whatever the hardware happens to do.
After: the counter is gone, so no new shared mutable state and no new
synchronization requirement is introduced. The guard only reads the issuing
PG's own sequence_number_; the increment stays in the Backend override, on the
calling thread, which is already where work->setSequenceNumber() read it. A
caller that issues on one PG from two threads already had to serialize (that
pre-existing per-PG ++ is not atomic either, and is out of scope here), while
the far more common two-PGs-two-threads case is now race-free where it
previously raced on a process-global static. The "wait" guard reads
WorkNCCL::seq_, written once by createWork() on the issuing thread before the
work is published to the queue and never mutated afterwards. Cross-rank
ordering is unaffected: every rank increments its per-PG counter on the same
collective in the same order, because the increment sits in the collective entry
point, and no collective or lockstep property depends on the guard.
Test Plan:
ProcessGroupNCCL2ObservabilityTest::test_profiler_sequence_number_is_per_group
(4 ranks) puts two nccl2 groups in one process, runs three all_reduce's on the
first and one on the second inside a profiled region, and asserts that each
group's recorded Seq values are its own counter's next N values.
With the C++ half reverse-applied and rebuilt, the same test fails as
i.e. no Seq metadata was recorded at all.
The numbering itself is visible through the Execution Trace observer, which does
serialize the tuple. A 1-rank probe with two PGs and 3 + 1 all_reduce's, both
groups' own counters standing at 1 before the profiled region:
pre-fix
post-fix
Three things to read off that: the second group's collective is numbered 15
pre-fix and 3 post-fix, which is its own counter; each collective and its wait
now share one number instead of burning two; and the numbers are the values the
work handles carry. Note also that no Seq metadata appears at all pre-fix.
Full suites, on 4 idle H100s, serially. Both were run with the second
observability test present as well -- the split-child rank-map fix it belongs to
lands separately -- so the count here is 22 pre-existing plus 2:
Authored with the assistance of an AI coding agent.