Skip to content
This repository was archived by the owner on Sep 30, 2026. It is now read-only.

map kernel and copy events to logical stream ID instead of HSA queue ID (#1424) - #1424

Closed
hannaxu wants to merge 1 commit into
pytorch:mainfrom
hannaxu:export-D107306915
Closed

hannaxu wants to merge 1 commit into
pytorch:mainfrom
hannaxu:export-D107306915

Conversation

@hannaxu

@hannaxu hannaxu commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Previously, resourceId() propagated the HSA queue ID for kernel events, rather than the logical stream ID. The AMD default is GPU_MAX_HW_QUEUES=4, which follows why stream interleaving was noticeable only when number of workers (total) >= 5 which is common on models with remote (additional remote workers). Therefore, there was stream interleaving as we were mapping > 5 streams to 4 HSA queues. The separate backfillAsyncCopyStreams for H2D/D2D was also propagating HW queue IDs.

Switch to int64_t for stream ids to avoid truncation; HW queue IDs fit fine in int32. Return streamId with queueId fallback inresourceId(). Recover the real HIP stream from the correlated runtime row, resolving both kernels and copies to stream ID via backfillAsyncStreams. Remove separate backfillAsyncCopyStreams. Async rows carry only the HW queue, so we map correlation --> HIP stream from the runtime rows.

Process

  1. Infer stream from queue when a queue maps unambiguously to one stream
  2. Remap raw 64-bit HIP stream pointers to per-device indices since Perfetto misrenders large id values onto the same track
  3. Kernels and copies share one per-device index space, so ops on the same HIP stream land on the same track. resourceId() returns the resolved streamId with a queueId <--> unambiguous stream map fallback

Add the HSA queue ID as a field in the trace, but not as the track provenance in next diff D107568026

Differential Revision: D107306915

@meta-cla meta-cla Bot added the cla signed label Jun 5, 2026
@meta-codesync

meta-codesync Bot commented Jun 5, 2026

Copy link
Copy Markdown

@hannaxu has exported this pull request. If you are a Meta employee, you can view the originating Diff in D107306915.

@meta-codesync meta-codesync Bot changed the title map kernel and copy events to logical stream ID instead of HSA queue ID map kernel and copy events to logical stream ID instead of HSA queue ID (#1424) Jun 5, 2026
@hannaxu
hannaxu force-pushed the export-D107306915 branch 2 times, most recently from 1484cd9 to 7275e8a Compare June 5, 2026 18:33
…ID (pytorch#1424)

Summary:

Previously, `resourceId()` propagated the HSA queue ID for kernel events, rather than the logical stream ID. The AMD default is `GPU_MAX_HW_QUEUES=4`, which follows why stream interleaving was noticeable only when number of workers (total) >= 5 which is common on models with remote (additional remote workers). Therefore, there was stream interleaving as we were mapping > 5 streams to 4 HSA queues. The separate `backfillAsyncCopyStreams` for H2D/D2D was also propagating HW queue IDs. 

Switch to int64_t for stream ids to avoid truncation; HW queue IDs fit fine in int32. Return streamId with queueId fallback in`resourceId()`. Recover the real HIP stream from the correlated runtime row, resolving both kernels and copies to stream ID via `backfillAsyncStreams`. Remove separate `backfillAsyncCopyStreams`. Async rows carry only the HW queue, so we map correlation --> HIP stream from the runtime rows.

*Process*
1. Infer stream from queue when a queue maps unambiguously to one stream
2. Remap raw 64-bit HIP stream pointers to per-device indices since Perfetto misrenders large id values onto the same track
3. Kernels and copies share one per-device index space, so ops on the same HIP stream land on the same track. `resourceId()` returns the resolved streamId with a queueId <--> unambiguous stream map fallback


Add the HSA queue ID as a field in the trace, but not as the track provenance in next diff D107568026

Differential Revision: D107306915
@hannaxu
hannaxu force-pushed the export-D107306915 branch from 7275e8a to 237fc78 Compare June 9, 2026 15:27
hannaxu added a commit to hannaxu/kineto that referenced this pull request Jun 9, 2026
…ID (pytorch#1424)

Summary:

Previously, `resourceId()` propagated the HSA queue ID for kernel events, rather than the logical stream ID. The AMD default is `GPU_MAX_HW_QUEUES=4`, which follows why stream interleaving was noticeable only when number of workers (total) >= 5 which is common on models with remote (additional remote workers). Therefore, there was stream interleaving as we were mapping > 5 streams to 4 HSA queues. The separate `backfillAsyncCopyStreams` for H2D/D2D was also propagating HW queue IDs. 

Switch to int64_t for stream ids to avoid truncation; HW queue IDs fit fine in int32. Return streamId with queueId fallback in`resourceId()`. Recover the real HIP stream from the correlated runtime row, resolving both kernels and copies to stream ID via `backfillAsyncStreams`. Remove separate `backfillAsyncCopyStreams`. Async rows carry only the HW queue, so we map correlation --> HIP stream from the runtime rows.

*Process*
1. Infer stream from queue when a queue maps unambiguously to one stream
2. Remap raw 64-bit HIP stream pointers to per-device indices since Perfetto misrenders large id values onto the same track
3. Kernels and copies share one per-device index space, so ops on the same HIP stream land on the same track. `resourceId()` returns the resolved streamId with a queueId <--> unambiguous stream map fallback


Add the HSA queue ID as a field in the trace, but not as the track provenance in next diff D107568026

Differential Revision: D107306915

@sanrise sanrise 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.

LGTM, thanks for adding this -

@meta-codesync meta-codesync Bot closed this in 25856f7 Jun 10, 2026
@meta-codesync meta-codesync Bot added the Merged label Jun 10, 2026
@meta-codesync

meta-codesync Bot commented Jun 10, 2026

Copy link
Copy Markdown

This pull request has been merged in 25856f7.

scotts added a commit to scotts/pytorch that referenced this pull request Jun 16, 2026
Includes the following commits:

- surface HSA queue ID (pytorch/kineto#1425) 1d43601
- Add TypedMetadata to CUPTI activities (pytorch/kineto#1434) 461a26c
- Move around methods so they are accurately described by the comments (pytorch/kineto#1435) ff95265
- Introduce TypedMetadata structs (pytorch/kineto#1433) 965a14d
- Add unit tests for sync/async interleaving behavior (pytorch/kineto#1431) 67612e6
- Restrict on-demand trace output path to a safe directory (pytorch/kineto#1426) 9ea826e
- Add a cancellation state to the state machine and remove syncTraceActive_ (pytorch/kineto#1355) 3289139
- Move state machine from profiler into async handler (pytorch/kineto#1352) eb578c5
- map kernel and copy events to logical stream ID instead of HSA queue ID (pytorch/kineto#1424) 25856f7
- Speedup and fix PyTorch CI build (pytorch/kineto#1428) e675bbb
- Add logging for async rejections (pytorch/kineto#1422) 14becfb
- Make sure we clear the logger observer before starting a new session (pytorch/kineto#1387) a8cae92
- Add a scope guard for logging to help us capture all exit paths (pytorch/kineto#1383) ce13b13
- Remove roctracer support (pytorch/kineto#1419) 2a3a002
pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Jun 16, 2026
Includes the following commits:

- surface HSA queue ID (pytorch/kineto#1425) 1d43601
- Add TypedMetadata to CUPTI activities (pytorch/kineto#1434) 461a26c
- Move around methods so they are accurately described by the comments (pytorch/kineto#1435) ff95265
- Introduce TypedMetadata structs (pytorch/kineto#1433) 965a14d
- Add unit tests for sync/async interleaving behavior (pytorch/kineto#1431) 67612e6
- Restrict on-demand trace output path to a safe directory (pytorch/kineto#1426) 9ea826e
- Add a cancellation state to the state machine and remove syncTraceActive_ (pytorch/kineto#1355) 3289139
- Move state machine from profiler into async handler (pytorch/kineto#1352) eb578c5
- map kernel and copy events to logical stream ID instead of HSA queue ID (pytorch/kineto#1424) 25856f7
- Speedup and fix PyTorch CI build (pytorch/kineto#1428) e675bbb
- Add logging for async rejections (pytorch/kineto#1422) 14becfb
- Make sure we clear the logger observer before starting a new session (pytorch/kineto#1387) a8cae92
- Add a scope guard for logging to help us capture all exit paths (pytorch/kineto#1383) ce13b13
- Remove roctracer support (pytorch/kineto#1419) 2a3a002
Pull Request resolved: #187440
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Includes the following commits:

- surface HSA queue ID (pytorch/kineto#1425) 1d43601
- Add TypedMetadata to CUPTI activities (pytorch/kineto#1434) 461a26c
- Move around methods so they are accurately described by the comments (pytorch/kineto#1435) ff95265
- Introduce TypedMetadata structs (pytorch/kineto#1433) 965a14d
- Add unit tests for sync/async interleaving behavior (pytorch/kineto#1431) 67612e6
- Restrict on-demand trace output path to a safe directory (pytorch/kineto#1426) 9ea826e
- Add a cancellation state to the state machine and remove syncTraceActive_ (pytorch/kineto#1355) 3289139
- Move state machine from profiler into async handler (pytorch/kineto#1352) eb578c5
- map kernel and copy events to logical stream ID instead of HSA queue ID (pytorch/kineto#1424) 25856f7
- Speedup and fix PyTorch CI build (pytorch/kineto#1428) e675bbb
- Add logging for async rejections (pytorch/kineto#1422) 14becfb
- Make sure we clear the logger observer before starting a new session (pytorch/kineto#1387) a8cae92
- Add a scope guard for logging to help us capture all exit paths (pytorch/kineto#1383) ce13b13
- Remove roctracer support (pytorch/kineto#1419) 2a3a002
Pull Request resolved: pytorch#187440
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Includes the following commits:

- surface HSA queue ID (pytorch/kineto#1425) 1d43601
- Add TypedMetadata to CUPTI activities (pytorch/kineto#1434) 461a26c
- Move around methods so they are accurately described by the comments (pytorch/kineto#1435) ff95265
- Introduce TypedMetadata structs (pytorch/kineto#1433) 965a14d
- Add unit tests for sync/async interleaving behavior (pytorch/kineto#1431) 67612e6
- Restrict on-demand trace output path to a safe directory (pytorch/kineto#1426) 9ea826e
- Add a cancellation state to the state machine and remove syncTraceActive_ (pytorch/kineto#1355) 3289139
- Move state machine from profiler into async handler (pytorch/kineto#1352) eb578c5
- map kernel and copy events to logical stream ID instead of HSA queue ID (pytorch/kineto#1424) 25856f7
- Speedup and fix PyTorch CI build (pytorch/kineto#1428) e675bbb
- Add logging for async rejections (pytorch/kineto#1422) 14becfb
- Make sure we clear the logger observer before starting a new session (pytorch/kineto#1387) a8cae92
- Add a scope guard for logging to help us capture all exit paths (pytorch/kineto#1383) ce13b13
- Remove roctracer support (pytorch/kineto#1419) 2a3a002
Pull Request resolved: pytorch#187440
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
haishuok0525 pushed a commit to haishuok0525/kineto that referenced this pull request Sep 11, 2026
processActivities() walks rows_ and externalCorrelations_ without holding
their mutexes, while the rocprofiler buffer callback keeps appending to both
from its own thread. A push_back that reallocates during the walk invalidates
the iteration and the process dies during profiler teardown. pytorch#1424 added a
third unlocked traversal of rows_ via backfillAsyncStreams, so the container
is taken under its mutex before that call rather than after it.

RocprofLogger::opString bounds-checks the operation index but not the kind.
name_info::operator[] forwards to std::vector::at, so an unrecognised kind
throws std::out_of_range from a path that writes trace output and has no
handler, terminating the process. The kind is now bounded the same way the
operation already was.

Reproduced by replaying a HIP graph around a profiling session on a single
GPU: 12 of 30 runs abort before this change, 0 of 30 after. It needs graph
replays, as graph-launched dispatch rows reach the callback thread only after
hipGraphLaunch returns, which is what keeps the container growing into
teardown. Traces are unchanged: five runs per arm with four concurrent
streams match on event counts per category, external id coverage, per-stream
kernel distribution and kernel to runtime correlation pairing. Teardown time
is unchanged.
haishuok0525 pushed a commit to haishuok0525/kineto that referenced this pull request Sep 12, 2026
processActivities() walks rows_ and externalCorrelations_ without holding
their mutexes, while the rocprofiler buffer callback keeps appending to both
from its own thread. A push_back that reallocates during the walk invalidates
the iteration and the process dies during profiler teardown. pytorch#1424 added a
third unlocked traversal of rows_ via backfillAsyncStreams, so the container
is taken under its mutex before that call rather than after it.

clearLogs() cleared the same two containers unlocked, so it is now covered by
the same mutexes.

Reproduced by replaying a HIP graph around a profiling session on a single
GPU: 12 of 30 runs abort before this change, 0 of 30 after. It needs graph
replays, as graph-launched dispatch rows reach the callback thread only after
hipGraphLaunch returns, which is what keeps the container growing into
teardown. Traces are unchanged: five runs per arm with four concurrent
streams match on event counts per category, external id coverage, per-stream
kernel distribution and kernel to runtime correlation pairing. Teardown time
is unchanged.

Co-authored-by: Cursor <[email protected]>
haishuok0525 pushed a commit to haishuok0525/kineto that referenced this pull request Sep 15, 2026
stopLogging() flushed the buffer once and returned. But rocprofiler-sdk does
not emplace a kernel dispatch record when the flush runs: the record is
produced from the SDK's own signal handler thread once the dispatch
completion signal fires, so records keep arriving for tens of milliseconds
after collection has stopped. Whatever had not arrived by the time
processActivities() ran was dropped, with no error and no warning.

A single-GPU HIP graph replay that dispatches exactly 6400 kernels reports
between 1251 and 5681 of them, a median of 1636, about 26% of the trace. Not
one of 15 surviving runs was complete. The loss is FIFO rather than random,
so a 32-node graph replayed 200 times shows every node 54 to 58 times
instead of 200 and the result still looks plausible. The same shape appears
on a production SGLang trace: 1507 kernels over 32 node ids, each appearing
44 to 49 times.

Flushing until the row count stops growing recovers the whole trace. It
converges in 3 flushes on 27 of 30 runs and 1 or 2 on the rest, so teardown
grows by roughly 30ms. The attempt bound is there so that a pathological
producer cannot hang teardown; hitting it warns and publishes what arrived,
which is today's behaviour.

Measured over 30 runs per arm on one GPU of one node, under gdb: teardown
segfaults on 15 of 30 runs before this change and 0 of 30 after, always at
the same site, and every run after reports all 6400 kernels instead of a
median 1636.

Two simpler explanations do not hold. Flushing before stopping the context
rather than after changes nothing. Calling hipDeviceSynchronize() first also
changes nothing, because it only guarantees that the GPU work finished and
the completion signals fired, not that the SDK has run its handler for them.

The loop cannot be replaced by borrowing the SDK's own barrier.
rocprofiler-sdk does count in-flight dispatches in Queue::_active_kernels,
decremented only after the record has been emplaced, and Queue::sync() waits
on that count. But sync() is a single ~5s wait returning bool, which the
SDK's own kernel replay drain has to call in a loop of up to 12 slices, and
that drain only converges because replay holds a per-agent writer lock that
blocks new dispatches while it waits. A profiler stopping a trace holds no
such lock and the application often keeps dispatching, so zero in flight is
not a state guaranteed to arrive. What would collapse this into one call is
not sync() being exposed but a documented guarantee that
rocprofiler_flush_buffer() delivers records whose dispatch had already
completed when it was called. Raised with the SDK separately.

Separately, processActivities() walked rows_ and externalCorrelations_
without holding their mutexes while the buffer callback appended to both from
its own thread. A push_back that reallocates during the walk invalidates the
iteration and the process dies during profiler teardown. pytorch#1424 added a third
unlocked traversal of rows_ via backfillAsyncStreams, so each container is
taken under its mutex before that call rather than after it. clearLogs()
cleared the same two containers unlocked and is now covered by the same
mutexes. Draining first makes a late append unlikely but not impossible, so
this still has to hold for the case where the bounded wait expires.

Co-authored-by: Cursor <[email protected]>
haishuok0525 pushed a commit to haishuok0525/kineto that referenced this pull request Sep 15, 2026
stopLogging() flushed the buffer once and returned. But rocprofiler-sdk does
not emplace a kernel dispatch record when the flush runs: the record is
produced from the SDK's own signal handler thread once the dispatch
completion signal fires, so records keep arriving for tens of milliseconds
after collection has stopped. Whatever had not arrived by the time
processActivities() ran was dropped, with no error and no warning.

A single-GPU HIP graph replay that dispatches exactly 6400 kernels reports
between 1251 and 5681 of them, a median of 1874, about 29% of the trace. Not
one of 15 surviving runs was complete. The loss is FIFO rather than random,
so a 32-node graph replayed 200 times shows every node 54 to 58 times
instead of 200 and the result still looks plausible. The same shape appears
on a production SGLang trace: 1507 kernels over 32 node ids, each appearing
44 to 49 times.

Flushing until the row count stops growing recovers the whole trace. It
converges in 3 flushes on 27 of 30 runs and 1 or 2 on the rest, so teardown
grows by roughly 30ms. The attempt bound is there so that a pathological
producer cannot hang teardown; hitting it warns and publishes what arrived,
which is today's behaviour.

Measured over 30 runs per arm on one GPU of one node, under gdb: teardown
segfaults on 15 of 30 runs before this change and 0 of 30 after, always at
the same site, and every run after reports all 6400 kernels instead of a
median 1874.

Two simpler explanations do not hold. Flushing before stopping the context
rather than after changes nothing. Calling hipDeviceSynchronize() first also
changes nothing, because it only guarantees that the GPU work finished and
the completion signals fired, not that the SDK has run its handler for them.

The loop cannot be replaced by borrowing the SDK's own barrier.
rocprofiler-sdk does count in-flight dispatches in Queue::_active_kernels,
decremented only after the record has been emplaced, and Queue::sync() waits
on that count. But sync() is a single ~5s wait returning bool, which the
SDK's own kernel replay drain has to call in a loop of up to 12 slices, and
that drain only converges because replay holds a per-agent writer lock that
blocks new dispatches while it waits. A profiler stopping a trace holds no
such lock and the application often keeps dispatching, so zero in flight is
not a state guaranteed to arrive. What would collapse this into one call is
not sync() being exposed but a documented guarantee that
rocprofiler_flush_buffer() delivers records whose dispatch had already
completed when it was called. Raised with the SDK separately.

Separately, processActivities() walked rows_ and externalCorrelations_
without holding their mutexes while the buffer callback appended to both from
its own thread. A push_back that reallocates during the walk invalidates the
iteration and the process dies during profiler teardown. pytorch#1424 added a third
unlocked traversal of rows_ via backfillAsyncStreams, so each container is
taken under its mutex before that call rather than after it. clearLogs()
cleared the same two containers unlocked and is now covered by the same
mutexes. Draining first makes a late append unlikely but not impossible, so
this still has to hold for the case where the bounded wait expires.

Co-authored-by: Cursor <[email protected]>
haishuok0525 pushed a commit to haishuok0525/kineto that referenced this pull request Sep 18, 2026
stopLogging() flushed the buffer once and returned. But rocprofiler-sdk does
not emplace a kernel dispatch record when the flush runs: the record is
produced from the SDK's own signal handler thread once the dispatch
completion signal fires, so records keep arriving for tens of milliseconds
after collection has stopped. Whatever had not arrived by the time
processActivities() ran was dropped, with no error and no warning.

A single-GPU HIP graph replay that dispatches exactly 6400 kernels reports
between 1251 and 5681 of them, a median of 1874, about 29% of the trace. Not
one of 15 surviving runs was complete. The loss is FIFO rather than random,
so a 32-node graph replayed 200 times shows every node 54 to 58 times
instead of 200 and the result still looks plausible. The same shape appears
on a production SGLang trace: 1507 kernels over 32 node ids, each appearing
44 to 49 times.

Flushing until the row count stops growing recovers the whole trace. It
converges in 3 flushes on 27 of 30 runs and 1 or 2 on the rest, so teardown
grows by roughly 30ms. The attempt bound is there so that a pathological
producer cannot hang teardown; hitting it warns and publishes what arrived,
which is today's behaviour.

Measured over 30 runs per arm on one GPU of one node, under gdb: teardown
segfaults on 15 of 30 runs before this change and 0 of 30 after, always at
the same site, and every run after reports all 6400 kernels instead of a
median 1874.

Two simpler explanations do not hold. Flushing before stopping the context
rather than after changes nothing. Calling hipDeviceSynchronize() first also
changes nothing, because it only guarantees that the GPU work finished and
the completion signals fired, not that the SDK has run its handler for them.

The loop cannot be replaced by borrowing the SDK's own barrier.
rocprofiler-sdk does count in-flight dispatches in Queue::_active_kernels,
decremented only after the record has been emplaced, and Queue::sync() waits
on that count. But sync() is a single ~5s wait returning bool, which the
SDK's own kernel replay drain has to call in a loop of up to 12 slices, and
that drain only converges because replay holds a per-agent writer lock that
blocks new dispatches while it waits. A profiler stopping a trace holds no
such lock and the application often keeps dispatching, so zero in flight is
not a state guaranteed to arrive. What would collapse this into one call is
not sync() being exposed but a documented guarantee that
rocprofiler_flush_buffer() delivers records whose dispatch had already
completed when it was called. Raised with the SDK separately.

Separately, processActivities() walked rows_ and externalCorrelations_
without holding their mutexes while the buffer callback appended to both from
its own thread. A push_back that reallocates during the walk invalidates the
iteration and the process dies during profiler teardown. pytorch#1424 added a third
unlocked traversal of rows_ via backfillAsyncStreams, so each container is
taken under its mutex before that call rather than after it. clearLogs()
cleared the same two containers unlocked and is now covered by the same
mutexes. Draining first makes a late append unlikely but not impossible, so
this still has to hold for the case where the bounded wait expires.

Co-authored-by: Cursor <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants