Skip to content

[kineto][rocm] Source kernel dims from dispatch records; report runtime fill/copy kernels as memset/memcpy - #200296

Closed
mwootton wants to merge 12 commits into
pytorch:mainfrom
mwootton:fix_rocprof_kernel_dims
Closed

mwootton wants to merge 12 commits into
pytorch:mainfrom
mwootton:fix_rocprof_kernel_dims

Conversation

@mwootton

@mwootton mwootton commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue or supporting maintainer

Supported by @jeffdaily

Continuation of pytorch/kineto#1418
Fixes: #198442
Fixes: #187119
Fixes: #180072
Fixes: #179944

BC-breaking?

No API change. On ROCm, trace outputs change for the same workload:

  • The runtime's fill/copy dispatches (__amd_rocclr_fill*/__amd_rocclr_copyBuffer*) move from cat: "kernel" to gpu_memset/gpu_memcpy events, as on CUDA. They are renamed, which changes key_averages() keys and FunctionEvent.name; they lose grid/block and gain bytes; the kind goes from "Dispatch Kernel" to none (memset) or a copy direction (memcpy); and they are now filtered by the GPU_MEMSET/GPU_MEMCPY masks instead of CONCURRENT_KERNEL.
  • hipExtModuleLaunchKernel/hipHccModuleLaunchKernel runtime events (Tensile Cijk* GEMMs) report grid in workgroups instead of the global work size, matching the kernel event.
  • A fill or copy that the runtime splits into several GPU records reports its bytes on the first record only, and none of the records reports a bandwidth.

Summary (human written only)

Some unit tests were still missing metadata for some kernels. This was due to a combination of bad test implementation and a unique feature of rocprofiler-sdk. Often the runtime use blit kernels for memcpy, memset, etc. Roctracer and Cupti report these as opaque memory operations, rocprofiler-sdk faithfully report the underlying kernels, e.g. __amd_rocclr_copyBuffer.kd.

The flawed tests were grabbing those implicit kernels, and not finding the metadata they were expecting.

Fix:

  • Mark those blit kernels as gpu_memset/gpu_memcpy GPU ops instead of kernel
  • Collect grid/block from the dispatch (KERNEL_DISPATCH buffer) record itself

Agent details (if applicable)

My agent came up with this analysis, but I've read and approved it.

Summary

On the rocprofiler-sdk path, kernel grid/block came only from correlating a HIP launch-API record, so kernels with no launch API (hipGraphLaunch replays, and the rocclr fill/copy kernels behind hipMemset*/hipMemcpy*) had no dims, which is why TestMetadataJsonFormat could not find grid on ROCm. This PR reads grid/block from the KERNEL_DISPATCH record (global size converted to workgroups, rounding up); the hipExtModuleLaunchKernel and hipHccModuleLaunchKernel runtime events now also report workgroups instead of the global work size, so the launch and the kernel agree.

hipMemset* calls now produce a runtime row that carries their byte count (sizeBytes, width x height x depth, or count x element size) and stream. Dispatches that share the correlation id of a hipMemset*/hipMemcpy* call are reported as gpu_memset/gpu_memcpy, as CUPTI reports them, with the call's byte count and stream, and every memset/copy call starts an ac2g flow. Kernel-implemented copies emit no MEMORY_COPY record, so nothing is counted twice. When the runtime splits one call into several GPU records, one of them carries the bytes and none reports a bandwidth. The byte count of SDMA MEMORY_COPY records also comes from the copy call's row now, so the thread_local correlation maps (grid, block and size) are all removed. Copy byte counts now cover 2D copies (width x height) and array copies (count), and the _spt variants of the copy APIs are classified like the memset _spt variants already were.

Tests: test_kineto now requires a device-side memcpy event on CUDA/ROCm (the CPU runtime call is also named memcpy, so the old check could not fail); new TestMetadataJsonFormatCUDA tests cover kernel dims for eager and graph-replayed kernels (and, on ROCm, equality with the launch API's grid/block), memcpy and memset reclassification with byte totals, and memset flows, each profiling after one discarded warmup session; RocmActivityProfilerTest covers copy direction, memset and copy byte counts, classification (including SDMA records, split calls and a second classification pass), the emitted name/type/metadata of reclassified rows, the output filter, and flow starts.

Not covered, and unchanged from before (these dispatches were kernels or lacked the field before this PR too): memset and copy nodes replayed by hipGraphLaunch are still reported as kernels, because their dispatches carry the graph launch's correlation id and classifying them would need the kernel name; hipMemcpy3D* and hipMemcpyParam2D* copies have no byte count (their sizes are inside a struct argument); copies issued with hipMemcpyDefault through hipMemcpy/hipMemcpyAsync are named Memcpy <unknown> (? -> ?) because the direction is not in the call's arguments. Only the copy APIs listed in isCopyApi are classified; the array copies added in HIP_RUNTIME_API_TABLE_STEP_VERSION 3 (hipMemcpyAtoD, DtoA, AtoA, AtoHAsync, HtoAAsync, 2DArrayToArray) and hipDrvMemcpy* are not, and PyTorch issues none of them.

Reland (maintainer update)

The first landing was reverted because the hipMemsetD2D* rocprofiler IDs do not exist in ROCm 7.0's SDK; 6c79b5b guards them on HIP_RUNTIME_API_TABLE_STEP_VERSION >= 14. This update merges viable/strict (kineto moved to torch/csrc/profiler/kineto; the changes carried over unchanged) and removes the two remaining ROCm skips the Fixes lines refer to: test_kineto_kernel_metadata_in_trace (#180072) and test_structured_metadata_matches_chrome_trace (#179944, whose skip was re-added by the #198324 test move after #196487 fixed it).

Test plan (done with Claude): built and ran on MI300X with ROCm 7.0.0 (rocprofiler-sdk 1.0.0, STEP_VERSION 13, so the guard drops the labels) and MI300X with ROCm 10.1. On both, every kineto source compiles with no warnings, the full test_profiler.py runs 207 tests OK with 16 skipped, RocmActivityProfilerTest passes 20/20, and each new or changed test passes when run alone in a fresh process. On a V100 with a CUDA 12.6 nightly, the changed TestMetadataJsonFormatCUDA tests and test_kineto_cuda pass alone in a fresh process. Without the change, test_kernel_metadata_has_expected_fields / test_kernel_metadata_field_types fail ('grid' not found) and so do the new tests. Sessions that got no kernel dispatch record at all on MI300X with ROCm 10.1 (rocprofiler-sdk delivering late dispatch records, ROCm/TheRock#9008) are fixed by #202758.

python test/profiler/test_profiler.py -k TestMetadataJsonFormatCUDA
python test/profiler/test_profiler.py -k test_kineto_cuda
python test/profiler/test_profiler.py -k test_kineto_kernel_metadata_in_trace
python test/profiler/test_profiler.py -k test_structured_metadata_matches_chrome_trace
python test/profiler/test_profiler.py

cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang

@pytorch-bot pytorch-bot Bot added module: rocm AMD GPU support for Pytorch topic: not user facing topic category labels Oct 8, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

🟡 AUTOMATED REVIEW - Changes requested:

On the rocprofiler-sdk path, the PR takes kernel grid/block from the
KERNEL_DISPATCH record and reports runtime fill/copy dispatches as
gpu_memset/gpu_memcpy events with bytes, streams and ac2g flows. It also removes
two ROCm test skips. Recommendation: Request Changes. Nothing found is a
correctness blocker, but before landing the PR should correct and complete the
BC section of its description, remove the duplicated filter logic, add a test
for workgroupCount, and fix the stale and misleading comments.

Findings (9):

[major] torch/csrc/profiler/kineto/libkineto/src/RocprofActivity_inl.h:179
The PR description's "BC-breaking?" section is wrong on two points and leaves
out several visible changes. Correct it so the release notes are accurate. (1)
GPU-side Tensile kernel events change too. Grid used to come from the
thread_local correlationToGrid map, which held the global work size for
hipExtModuleLaunchKernel/hipHccModuleLaunchKernel. It now comes from the
dispatch record in workgroups (RocprofActivity_inl.h:174-188,
RocprofLogger.cpp:1068-1082), so the GPU Cijk* event that occupancy tools read
changes. (2) "They lose grid/block and gain bytes" is inaccurate. Fill/copy
dispatches never had grid/block. Copies already reported bytes and bandwidth, so
only fills newly gain bytes. On split copies, only the first row keeps bytes and
no row reports bandwidth. Not listed: every isMemsetApi/isCopyApi call now
starts an ac2g flow, and calls with no GPU record (host-to-host copies) get a
flow start with no end. CPU hipMemset* events gain bytes. The 15 hipMemcpy*_spt
APIs become rocprofCopyRow and gain src/dst/bytes/kind and flows. 2D and array
copies change bytes from 0 to width*height or count.

[major]
torch/csrc/profiler/kineto/libkineto/test/RocmActivityProfilerTest.cpp:319
The mock's processActivities copies the classify/backfill/filter sequence by
hand, with its own isLogged and a hard-coded activityMask. Its comment (around
lines 361-362) says tests overwrite the mask, but none does. The earlier mock
drift on this PR shows what this costs. Have the mock call the same shared
detail:: helper as the real processActivities.

[major] torch/csrc/profiler/kineto/libkineto/src/RocprofLogger.cpp:106
The new grid conversion is untested. Nothing tests workgroupCount: not the
rounding for a partial last workgroup, not local == 0, and not the
hipExtModuleLaunchKernel/hipHccModuleLaunchKernel conversion at
RocprofLogger.cpp:845-854. workgroupCount is in an anonymous namespace, no C++
test references it, and the fixture sets gridX directly. For the Ext/Hcc APIs,
both sides of the Python equality in _assert_kernel_dims
(test_profiler.py:5570-5597) go through workgroupCount, so a wrong formula
cancels out. For hipLaunchKernel, only exact multiples are exercised. Fix:
expose workgroupCount from a shared header and add a C++ case with a
non-divisible global size.

[major] torch/csrc/profiler/kineto/libkineto/src/RocmActivityProfiler.cpp:207
Stale comment at RocmActivityProfiler.cpp:21-22. It says RocprofActivity.h must
stay in this .cpp because _inl.h references thread_local maps. This PR removes
those maps, so update or delete the comment.

[major]
torch/csrc/profiler/kineto/libkineto/test/RocmActivityProfilerTest.cpp:1393
Misleading test comment (echoed at RocmStreamQueue.h:258). "processActivities
classifies on every call; a second pass changes nothing" suggests repeated calls
happen. In production, processActivities runs once per trace
(GenericActivityProfiler.cpp:194-195, guarded by traceBuffers_) and rewrites
begin/end in place, so a second pass cannot occur. Reword it as an idempotence
check of the helper, or drop the second-pass assertions.

(4 more not shown, see the review run)

Reviewed commit: a49a226

Review run

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/200296

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit a49a226 with merge base 28e3c72 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot missing actionable issue A PR without a linked actionable issue or maintainer sponsor triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 8, 2026

@jeffdaily jeffdaily left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My agent came up with this analysis, but I've read and approved it.

  1. Please add a ROCm test that exercises the reclassification directly, e.g. profile a D2D x.clone() and assert the event is gpu_memcpy named Memcpy DtoD (Device -> Device) with Bytes, and that no __amd_rocclr_* kernel event remains. test_all_kernel_events_have_dims only covers torch.mm and does not show that fill/copy dispatches are retyped.
  2. RocmStreamQueue.h: async->op = kind + 1 maps hipMemcpyDefault to ROCPROFILER_MEMORY_COPY_LAST. Copies with no kind argument (hipMemcpyPeerAsync, used by the caching allocator for non-P2P cross-device copies, plus hipMemcpyDtoD/HtoD/DtoH) then render as Memcpy <unknown> (? -> ?). Please map explicitly, deriving the direction from the API id when the kind is Default.
  3. Memcpy bytes still come from the thread_local correlationToSize even though internalBytes is now set; use internalBytes as the memset path does.

@jeffdaily jeffdaily added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 9, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@jeffdaily jeffdaily added open source and removed missing actionable issue A PR without a linked actionable issue or maintainer sponsor bot-triaged This is a label only to be used by the auto triage bot triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module open source labels Oct 9, 2026
@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 9, 2026
…copy kernels as memset/memcpy

Kernel metadata now gets grid/block from the rocprofiler-sdk dispatch record (covers graph replays and API-less dispatches), and kernel dispatches correlated to hipMemset*/hipMemcpy* are retyped as GPU_MEMSET/GPU_MEMCPY (CUPTI parity) instead of surfacing as user kernels. Fixes missing 'grid' in TestMetadataJsonFormat on ROCm.
@mwootton
mwootton force-pushed the fix_rocprof_kernel_dims branch from 4f78c1d to 671b0d4 Compare October 14, 2026 18:16
@mwootton

mwootton commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Responses to the review (force-pushed as 671b0d489, single commit):

  1. Test — added TestMetadataJsonFormat.test_runtime_internal_dispatches_are_not_kernels: profiles a D2D x.clone() and asserts a gpu_memcpy event named Memcpy DtoD (Device -> Device) with bytes == x.nbytes, and that no __amd_rocclr_* kernel-typed event remains. Confirmed the check: on MI350X with the shipped kineto, clone() yields only __amd_rocclr_copyBuffer.kd as a kernel and no memcpy event.
  2. async->op = kind + 1 — fixed. Replaced with copyOperationFromApi(kind, apiId), which maps each hipMemcpyKind explicitly (DeviceToDeviceNoCU included) and, when the kind is Default, derives the direction from the API id: hipMemcpyPeer*/hipMemcpyDtoD* → DtoD, hipMemcpyHtoD*/HtoA/ToArray/ToSymbol* → HtoD, hipMemcpyDtoH*/AtoH/FromArray/FromSymbol* → DtoH. ROCPROFILER_MEMORY_COPY_NONE is now only used when neither the kind nor the API id tells us the direction.
  3. Memcpy bytes — fixed. Retyped memcpy dispatches now report internalBytes (same as memset) plus bandwidth; the thread_local correlationToSize lookup is left in place only for genuine MEMORY_COPY records.

@jeffdaily

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

jeffdaily
jeffdaily previously approved these changes Oct 14, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@jeffdaily
jeffdaily dismissed github-actions[bot]’s stale review November 3, 2026 19:33

Remaining automated-review items are refactors and test additions, tracked for a follow-up. Validated on ROCm 10.1 and ROCm 7.0.

@jeffdaily

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…me fill/copy kernels as memset/memcpy (pytorch#200296)

## Linked issue or supporting maintainer

Supported by @jeffdaily

Continuation of pytorch/kineto#1418
Fixes: pytorch#198442
Fixes: pytorch#187119
Fixes: pytorch#180072
Fixes: pytorch#179944

## BC-breaking?

No API change. On ROCm, trace outputs change for the same workload:

- The runtime's fill/copy dispatches (`__amd_rocclr_fill*`/`__amd_rocclr_copyBuffer*`) move from `cat: "kernel"` to `gpu_memset`/`gpu_memcpy` events, as on CUDA. They are renamed, which changes `key_averages()` keys and `FunctionEvent.name`; they lose grid/block and gain bytes; the `kind` goes from "Dispatch Kernel" to none (memset) or a copy direction (memcpy); and they are now filtered by the GPU_MEMSET/GPU_MEMCPY masks instead of CONCURRENT_KERNEL.
- hipExtModuleLaunchKernel/hipHccModuleLaunchKernel runtime events (Tensile `Cijk*` GEMMs) report `grid` in workgroups instead of the global work size, matching the kernel event.
- A fill or copy that the runtime splits into several GPU records reports its bytes on the first record only, and none of the records reports a bandwidth.

## Summary (human written only)

Some unit tests were still missing metadata for some kernels.  This was due to a combination of bad test implementation and a unique feature of rocprofiler-sdk.  Often the runtime use blit kernels for memcpy, memset, etc.  Roctracer and Cupti report these as opaque memory operations, rocprofiler-sdk faithfully report the underlying kernels, e.g. __amd_rocclr_copyBuffer.kd.

The flawed tests were grabbing those implicit kernels, and not finding the metadata they were expecting.

Fix:

- Mark those blit kernels as gpu_memset/gpu_memcpy GPU ops instead of kernel
- Collect grid/block from the dispatch (KERNEL_DISPATCH buffer) record itself

## Agent details (if applicable)

My agent came up with this analysis, but I've read and approved it.

### Summary
On the rocprofiler-sdk path, kernel grid/block came only from correlating a HIP launch-API record, so kernels with no launch API (hipGraphLaunch replays, and the rocclr fill/copy kernels behind hipMemset*/hipMemcpy*) had no dims, which is why TestMetadataJsonFormat could not find `grid` on ROCm. This PR reads grid/block from the KERNEL_DISPATCH record (global size converted to workgroups, rounding up); the hipExtModuleLaunchKernel and hipHccModuleLaunchKernel runtime events now also report workgroups instead of the global work size, so the launch and the kernel agree.

hipMemset* calls now produce a runtime row that carries their byte count (sizeBytes, width x height x depth, or count x element size) and stream. Dispatches that share the correlation id of a hipMemset*/hipMemcpy* call are reported as gpu_memset/gpu_memcpy, as CUPTI reports them, with the call's byte count and stream, and every memset/copy call starts an ac2g flow. Kernel-implemented copies emit no MEMORY_COPY record, so nothing is counted twice. When the runtime splits one call into several GPU records, one of them carries the bytes and none reports a bandwidth. The byte count of SDMA MEMORY_COPY records also comes from the copy call's row now, so the thread_local correlation maps (grid, block and size) are all removed. Copy byte counts now cover 2D copies (width x height) and array copies (count), and the `_spt` variants of the copy APIs are classified like the memset `_spt` variants already were.

Tests: test_kineto now requires a device-side memcpy event on CUDA/ROCm (the CPU runtime call is also named memcpy, so the old check could not fail); new TestMetadataJsonFormatCUDA tests cover kernel dims for eager and graph-replayed kernels (and, on ROCm, equality with the launch API's grid/block), memcpy and memset reclassification with byte totals, and memset flows, each profiling after one discarded warmup session; RocmActivityProfilerTest covers copy direction, memset and copy byte counts, classification (including SDMA records, split calls and a second classification pass), the emitted name/type/metadata of reclassified rows, the output filter, and flow starts.

Not covered, and unchanged from before (these dispatches were kernels or lacked the field before this PR too): memset and copy nodes replayed by hipGraphLaunch are still reported as kernels, because their dispatches carry the graph launch's correlation id and classifying them would need the kernel name; hipMemcpy3D* and hipMemcpyParam2D* copies have no byte count (their sizes are inside a struct argument); copies issued with hipMemcpyDefault through hipMemcpy/hipMemcpyAsync are named `Memcpy <unknown> (? -> ?)` because the direction is not in the call's arguments. Only the copy APIs listed in `isCopyApi` are classified; the array copies added in HIP_RUNTIME_API_TABLE_STEP_VERSION 3 (hipMemcpyAtoD, DtoA, AtoA, AtoHAsync, HtoAAsync, 2DArrayToArray) and hipDrvMemcpy* are not, and PyTorch issues none of them.

### Reland (maintainer update)
The first landing was reverted because the `hipMemsetD2D*` rocprofiler IDs do not exist in ROCm 7.0's SDK; 6c79b5b guards them on `HIP_RUNTIME_API_TABLE_STEP_VERSION >= 14`. This update merges viable/strict (kineto moved to torch/csrc/profiler/kineto; the changes carried over unchanged) and removes the two remaining ROCm skips the `Fixes` lines refer to: `test_kineto_kernel_metadata_in_trace` (pytorch#180072) and `test_structured_metadata_matches_chrome_trace` (pytorch#179944, whose skip was re-added by the pytorch#198324 test move after pytorch#196487 fixed it).

Test plan (done with Claude): built and ran on MI300X with ROCm 7.0.0 (rocprofiler-sdk 1.0.0, STEP_VERSION 13, so the guard drops the labels) and MI300X with ROCm 10.1. On both, every kineto source compiles with no warnings, the full test_profiler.py runs 207 tests OK with 16 skipped, RocmActivityProfilerTest passes 20/20, and each new or changed test passes when run alone in a fresh process. On a V100 with a CUDA 12.6 nightly, the changed TestMetadataJsonFormatCUDA tests and test_kineto_cuda pass alone in a fresh process. Without the change, `test_kernel_metadata_has_expected_fields` / `test_kernel_metadata_field_types` fail (`'grid' not found`) and so do the new tests. Sessions that got no kernel dispatch record at all on MI300X with ROCm 10.1 (rocprofiler-sdk delivering late dispatch records, ROCm/TheRock#9008) are fixed by pytorch#202758.
```
python test/profiler/test_profiler.py -k TestMetadataJsonFormatCUDA
python test/profiler/test_profiler.py -k test_kineto_cuda
python test/profiler/test_profiler.py -k test_kineto_kernel_metadata_in_trace
python test/profiler/test_profiler.py -k test_structured_metadata_matches_chrome_trace
python test/profiler/test_profiler.py
```

Pull Request resolved: pytorch#200296
Approved by: https://github.com/jeffdaily

Co-authored-by: Jithun Nair <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Jeff Daily <[email protected]>
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…me fill/copy kernels as memset/memcpy (pytorch#200296)

## Linked issue or supporting maintainer

Supported by @jeffdaily

Continuation of pytorch/kineto#1418
Fixes: pytorch#198442
Fixes: pytorch#187119
Fixes: pytorch#180072
Fixes: pytorch#179944

## BC-breaking?

No API change. On ROCm, trace outputs change for the same workload:

- The runtime's fill/copy dispatches (`__amd_rocclr_fill*`/`__amd_rocclr_copyBuffer*`) move from `cat: "kernel"` to `gpu_memset`/`gpu_memcpy` events, as on CUDA. They are renamed, which changes `key_averages()` keys and `FunctionEvent.name`; they lose grid/block and gain bytes; the `kind` goes from "Dispatch Kernel" to none (memset) or a copy direction (memcpy); and they are now filtered by the GPU_MEMSET/GPU_MEMCPY masks instead of CONCURRENT_KERNEL.
- hipExtModuleLaunchKernel/hipHccModuleLaunchKernel runtime events (Tensile `Cijk*` GEMMs) report `grid` in workgroups instead of the global work size, matching the kernel event.
- A fill or copy that the runtime splits into several GPU records reports its bytes on the first record only, and none of the records reports a bandwidth.

## Summary (human written only)

Some unit tests were still missing metadata for some kernels.  This was due to a combination of bad test implementation and a unique feature of rocprofiler-sdk.  Often the runtime use blit kernels for memcpy, memset, etc.  Roctracer and Cupti report these as opaque memory operations, rocprofiler-sdk faithfully report the underlying kernels, e.g. __amd_rocclr_copyBuffer.kd.

The flawed tests were grabbing those implicit kernels, and not finding the metadata they were expecting.

Fix:

- Mark those blit kernels as gpu_memset/gpu_memcpy GPU ops instead of kernel
- Collect grid/block from the dispatch (KERNEL_DISPATCH buffer) record itself

## Agent details (if applicable)

My agent came up with this analysis, but I've read and approved it.

### Summary
On the rocprofiler-sdk path, kernel grid/block came only from correlating a HIP launch-API record, so kernels with no launch API (hipGraphLaunch replays, and the rocclr fill/copy kernels behind hipMemset*/hipMemcpy*) had no dims, which is why TestMetadataJsonFormat could not find `grid` on ROCm. This PR reads grid/block from the KERNEL_DISPATCH record (global size converted to workgroups, rounding up); the hipExtModuleLaunchKernel and hipHccModuleLaunchKernel runtime events now also report workgroups instead of the global work size, so the launch and the kernel agree.

hipMemset* calls now produce a runtime row that carries their byte count (sizeBytes, width x height x depth, or count x element size) and stream. Dispatches that share the correlation id of a hipMemset*/hipMemcpy* call are reported as gpu_memset/gpu_memcpy, as CUPTI reports them, with the call's byte count and stream, and every memset/copy call starts an ac2g flow. Kernel-implemented copies emit no MEMORY_COPY record, so nothing is counted twice. When the runtime splits one call into several GPU records, one of them carries the bytes and none reports a bandwidth. The byte count of SDMA MEMORY_COPY records also comes from the copy call's row now, so the thread_local correlation maps (grid, block and size) are all removed. Copy byte counts now cover 2D copies (width x height) and array copies (count), and the `_spt` variants of the copy APIs are classified like the memset `_spt` variants already were.

Tests: test_kineto now requires a device-side memcpy event on CUDA/ROCm (the CPU runtime call is also named memcpy, so the old check could not fail); new TestMetadataJsonFormatCUDA tests cover kernel dims for eager and graph-replayed kernels (and, on ROCm, equality with the launch API's grid/block), memcpy and memset reclassification with byte totals, and memset flows, each profiling after one discarded warmup session; RocmActivityProfilerTest covers copy direction, memset and copy byte counts, classification (including SDMA records, split calls and a second classification pass), the emitted name/type/metadata of reclassified rows, the output filter, and flow starts.

Not covered, and unchanged from before (these dispatches were kernels or lacked the field before this PR too): memset and copy nodes replayed by hipGraphLaunch are still reported as kernels, because their dispatches carry the graph launch's correlation id and classifying them would need the kernel name; hipMemcpy3D* and hipMemcpyParam2D* copies have no byte count (their sizes are inside a struct argument); copies issued with hipMemcpyDefault through hipMemcpy/hipMemcpyAsync are named `Memcpy <unknown> (? -> ?)` because the direction is not in the call's arguments. Only the copy APIs listed in `isCopyApi` are classified; the array copies added in HIP_RUNTIME_API_TABLE_STEP_VERSION 3 (hipMemcpyAtoD, DtoA, AtoA, AtoHAsync, HtoAAsync, 2DArrayToArray) and hipDrvMemcpy* are not, and PyTorch issues none of them.

### Reland (maintainer update)
The first landing was reverted because the `hipMemsetD2D*` rocprofiler IDs do not exist in ROCm 7.0's SDK; 6c79b5b guards them on `HIP_RUNTIME_API_TABLE_STEP_VERSION >= 14`. This update merges viable/strict (kineto moved to torch/csrc/profiler/kineto; the changes carried over unchanged) and removes the two remaining ROCm skips the `Fixes` lines refer to: `test_kineto_kernel_metadata_in_trace` (pytorch#180072) and `test_structured_metadata_matches_chrome_trace` (pytorch#179944, whose skip was re-added by the pytorch#198324 test move after pytorch#196487 fixed it).

Test plan (done with Claude): built and ran on MI300X with ROCm 7.0.0 (rocprofiler-sdk 1.0.0, STEP_VERSION 13, so the guard drops the labels) and MI300X with ROCm 10.1. On both, every kineto source compiles with no warnings, the full test_profiler.py runs 207 tests OK with 16 skipped, RocmActivityProfilerTest passes 20/20, and each new or changed test passes when run alone in a fresh process. On a V100 with a CUDA 12.6 nightly, the changed TestMetadataJsonFormatCUDA tests and test_kineto_cuda pass alone in a fresh process. Without the change, `test_kernel_metadata_has_expected_fields` / `test_kernel_metadata_field_types` fail (`'grid' not found`) and so do the new tests. Sessions that got no kernel dispatch record at all on MI300X with ROCm 10.1 (rocprofiler-sdk delivering late dispatch records, ROCm/TheRock#9008) are fixed by pytorch#202758.
```
python test/profiler/test_profiler.py -k TestMetadataJsonFormatCUDA
python test/profiler/test_profiler.py -k test_kineto_cuda
python test/profiler/test_profiler.py -k test_kineto_kernel_metadata_in_trace
python test/profiler/test_profiler.py -k test_structured_metadata_matches_chrome_trace
python test/profiler/test_profiler.py
```

Pull Request resolved: pytorch#200296
Approved by: https://github.com/jeffdaily

Co-authored-by: Jithun Nair <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Jeff Daily <[email protected]>
jeffdaily pushed a commit to ROCm/pytorch that referenced this pull request Dec 3, 2026
…me fill/copy kernels as memset/memcpy (pytorch#200296)

Supported by @jeffdaily

Continuation of pytorch/kineto#1418
Fixes: pytorch#198442
Fixes: pytorch#187119
Fixes: pytorch#180072
Fixes: pytorch#179944

No API change. On ROCm, trace outputs change for the same workload:

- The runtime's fill/copy dispatches (`__amd_rocclr_fill*`/`__amd_rocclr_copyBuffer*`) move from `cat: "kernel"` to `gpu_memset`/`gpu_memcpy` events, as on CUDA. They are renamed, which changes `key_averages()` keys and `FunctionEvent.name`; they lose grid/block and gain bytes; the `kind` goes from "Dispatch Kernel" to none (memset) or a copy direction (memcpy); and they are now filtered by the GPU_MEMSET/GPU_MEMCPY masks instead of CONCURRENT_KERNEL.
- hipExtModuleLaunchKernel/hipHccModuleLaunchKernel runtime events (Tensile `Cijk*` GEMMs) report `grid` in workgroups instead of the global work size, matching the kernel event.
- A fill or copy that the runtime splits into several GPU records reports its bytes on the first record only, and none of the records reports a bandwidth.

Some unit tests were still missing metadata for some kernels.  This was due to a combination of bad test implementation and a unique feature of rocprofiler-sdk.  Often the runtime use blit kernels for memcpy, memset, etc.  Roctracer and Cupti report these as opaque memory operations, rocprofiler-sdk faithfully report the underlying kernels, e.g. __amd_rocclr_copyBuffer.kd.

The flawed tests were grabbing those implicit kernels, and not finding the metadata they were expecting.

Fix:

- Mark those blit kernels as gpu_memset/gpu_memcpy GPU ops instead of kernel
- Collect grid/block from the dispatch (KERNEL_DISPATCH buffer) record itself

My agent came up with this analysis, but I've read and approved it.

On the rocprofiler-sdk path, kernel grid/block came only from correlating a HIP launch-API record, so kernels with no launch API (hipGraphLaunch replays, and the rocclr fill/copy kernels behind hipMemset*/hipMemcpy*) had no dims, which is why TestMetadataJsonFormat could not find `grid` on ROCm. This PR reads grid/block from the KERNEL_DISPATCH record (global size converted to workgroups, rounding up); the hipExtModuleLaunchKernel and hipHccModuleLaunchKernel runtime events now also report workgroups instead of the global work size, so the launch and the kernel agree.

hipMemset* calls now produce a runtime row that carries their byte count (sizeBytes, width x height x depth, or count x element size) and stream. Dispatches that share the correlation id of a hipMemset*/hipMemcpy* call are reported as gpu_memset/gpu_memcpy, as CUPTI reports them, with the call's byte count and stream, and every memset/copy call starts an ac2g flow. Kernel-implemented copies emit no MEMORY_COPY record, so nothing is counted twice. When the runtime splits one call into several GPU records, one of them carries the bytes and none reports a bandwidth. The byte count of SDMA MEMORY_COPY records also comes from the copy call's row now, so the thread_local correlation maps (grid, block and size) are all removed. Copy byte counts now cover 2D copies (width x height) and array copies (count), and the `_spt` variants of the copy APIs are classified like the memset `_spt` variants already were.

Tests: test_kineto now requires a device-side memcpy event on CUDA/ROCm (the CPU runtime call is also named memcpy, so the old check could not fail); new TestMetadataJsonFormatCUDA tests cover kernel dims for eager and graph-replayed kernels (and, on ROCm, equality with the launch API's grid/block), memcpy and memset reclassification with byte totals, and memset flows, each profiling after one discarded warmup session; RocmActivityProfilerTest covers copy direction, memset and copy byte counts, classification (including SDMA records, split calls and a second classification pass), the emitted name/type/metadata of reclassified rows, the output filter, and flow starts.

Not covered, and unchanged from before (these dispatches were kernels or lacked the field before this PR too): memset and copy nodes replayed by hipGraphLaunch are still reported as kernels, because their dispatches carry the graph launch's correlation id and classifying them would need the kernel name; hipMemcpy3D* and hipMemcpyParam2D* copies have no byte count (their sizes are inside a struct argument); copies issued with hipMemcpyDefault through hipMemcpy/hipMemcpyAsync are named `Memcpy <unknown> (? -> ?)` because the direction is not in the call's arguments. Only the copy APIs listed in `isCopyApi` are classified; the array copies added in HIP_RUNTIME_API_TABLE_STEP_VERSION 3 (hipMemcpyAtoD, DtoA, AtoA, AtoHAsync, HtoAAsync, 2DArrayToArray) and hipDrvMemcpy* are not, and PyTorch issues none of them.

The first landing was reverted because the `hipMemsetD2D*` rocprofiler IDs do not exist in ROCm 7.0's SDK; 6c79b5b guards them on `HIP_RUNTIME_API_TABLE_STEP_VERSION >= 14`. This update merges viable/strict (kineto moved to torch/csrc/profiler/kineto; the changes carried over unchanged) and removes the two remaining ROCm skips the `Fixes` lines refer to: `test_kineto_kernel_metadata_in_trace` (pytorch#180072) and `test_structured_metadata_matches_chrome_trace` (pytorch#179944, whose skip was re-added by the pytorch#198324 test move after pytorch#196487 fixed it).

Test plan (done with Claude): built and ran on MI300X with ROCm 7.0.0 (rocprofiler-sdk 1.0.0, STEP_VERSION 13, so the guard drops the labels) and MI300X with ROCm 10.1. On both, every kineto source compiles with no warnings, the full test_profiler.py runs 207 tests OK with 16 skipped, RocmActivityProfilerTest passes 20/20, and each new or changed test passes when run alone in a fresh process. On a V100 with a CUDA 12.6 nightly, the changed TestMetadataJsonFormatCUDA tests and test_kineto_cuda pass alone in a fresh process. Without the change, `test_kernel_metadata_has_expected_fields` / `test_kernel_metadata_field_types` fail (`'grid' not found`) and so do the new tests. Sessions that got no kernel dispatch record at all on MI300X with ROCm 10.1 (rocprofiler-sdk delivering late dispatch records, ROCm/TheRock#9008) are fixed by pytorch#202758.
```
python test/profiler/test_profiler.py -k TestMetadataJsonFormatCUDA
python test/profiler/test_profiler.py -k test_kineto_cuda
python test/profiler/test_profiler.py -k test_kineto_kernel_metadata_in_trace
python test/profiler/test_profiler.py -k test_structured_metadata_matches_chrome_trace
python test/profiler/test_profiler.py
```

Pull Request resolved: pytorch#200296
Approved by: https://github.com/jeffdaily

Co-authored-by: Jithun Nair <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Jeff Daily <[email protected]>
(cherry picked from commit 1ab6780)

[release/2.15 adaptation, by mwootton]
- Kineto paths landed under third_party/kineto (2.15 predates the vendoring
  move to torch/csrc/profiler/kineto).
- The three prerequisite ROCm kineto fixes (pytorch#199973, pytorch#202665, pytorch#202758) were
  cherry-picked ahead of this commit; this branch's kineto snapshot (Oct 2)
  predates them.
- test_profiler.py: kept on the pre-split TestMetadataJsonFormat class (the
  CUDA split landed after the branch cut); added @instantiate_parametrized_tests
  to that class so the new parameterized test is collected; dropped the
  skipXPUIf decorator hunk (added upstream after the cut).

Fixes ROCM-31589
Fixes ROCM-29809
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ci-no-td Do not run TD on this PR ciflow/h100-symm-mem ciflow/rocm-mi300 Trigger "default" config CI on ROCm MI300 ciflow/trunk Trigger trunk jobs on your pull request in progress PR is being worked on by the author, until the automated review accepts it Merged module: rocm AMD GPU support for Pytorch open source Reverted topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

6 participants