Skip to content

[Test] Refactor test_profiler.py tests to enhance device compatibility[5/N] - #198324

Closed
can-gaa-hou wants to merge 5 commits into
gh/can-gaa-hou/55/basefrom
gh/can-gaa-hou/55/head
Closed

can-gaa-hou wants to merge 5 commits into
gh/can-gaa-hou/55/basefrom
gh/can-gaa-hou/55/head

Conversation

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit cdb797a with merge base ecc974c (image):

NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following job failed, but the AI CI Advisor judged the failure not to be evidence against this change:

  • pull / dynamo-cpython-test / test (dynamo_cpython, 1, 1, mt-l-x86iavx512-8-64) (gh)
    test/cpython/v3_13/test_call.py::CFunctionCallsErrorMessages::test_varargs2min

    AI verdict: AI verdict: not related

    The suspect commit only adds 3 lines to test/profiler/test_profiler.py. The failing test is test_call.py::CFunctionCallsErrorMessages::test_varargs2min, which reports 'Unexpected success, please remove test/dynamo_expected_failures/...test_varargs2min'. The merge-base run (ecc974c) fails with exactly this error and is also marked FAILED CONSISTENTLY, so the failure is older than the suspect.

    Full reasoning on HUD →

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

[ghstack-poisoned]
can-gaa-hou added a commit that referenced this pull request Sep 23, 2026
[ghstack-poisoned]
can-gaa-hou added a commit that referenced this pull request Sep 23, 2026
Comment thread test/profiler/test_profiler.py Outdated
"""Tests validating parity between events() and export_chrome_trace() JSON."""

def test_python_function_events_in_events(self):
hw_classification = HardwareClassification.ACCELERATOR

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.

Suggested change
hw_classification = HardwareClassification.ACCELERATOR
hw_classification = HardwareClassification.GENERIC

Comment thread test/profiler/test_profiler.py Outdated
def test_python_function_events_in_events(self):
hw_classification = HardwareClassification.ACCELERATOR

def test_python_function_events_in_events(self, device):

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.

Restore this one, this testcase should be GENERIC

Comment thread test/profiler/test_profiler.py Outdated
self.assertEqual(fe_mod.python_module_id, args["Python module id"])

def test_key_averages_excludes_python_functions_by_default(self):
def test_key_averages_excludes_python_functions_by_default(self, device):

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.

Restore it

Comment thread test/profiler/test_profiler.py Outdated
"""Verify that async CPU->GPU flow fields on events() match Chrome trace JSON."""
with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA]) as prof:
x = torch.randn(32, 32, device="cuda")
with profile(activities=get_profiler_activities(self.device_type)) as prof:

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.

Suggested change
with profile(activities=get_profiler_activities(self.device_type)) as prof:
with profile(activities=get_profiler_activities(device)) as prof:

And migrate it to new Accelerator-agnostic class

Comment thread test/profiler/test_profiler.py Outdated
)

def test_profiler_fwdbwd_flow_events_parity(self):
def test_profiler_fwdbwd_flow_events_parity(self, device):

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.

Restore it , GENERIC here

"""Inline CUDA-graph annotations, driven through stub activities so the branches
are covered without a capture or a live profiler."""

def _export(self, activities, **kwargs):

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.

Missing hw_classicification here?

Comment thread test/profiler/test_profiler.py Outdated
)

def test_kernel_metadata_field_types(self):
def test_kernel_metadata_field_types(self, device):

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.

Restore it.

Comment thread test/profiler/test_profiler.py Outdated
self.assertEqual(len(parsed[key]), 3)

def test_metadata_json_key_value_format(self):
def test_metadata_json_key_value_format(self, device):

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.

Restore it.

Comment thread test/profiler/test_profiler.py Outdated
Comment on lines +5246 to +5248
instantiate_device_type_tests(TestMetadataJsonFormat, globals(), only_for=("cuda",))


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.

Restore it.



@unittest.skipIf(not kineto_available(), "Kineto is required")
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is required")

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.

Restore it.

@fffrog fffrog moved this from Backlog to Developing in PyTorch Test Refactoring Sep 23, 2026
@fffrog fffrog added the module: tests/simple This label covers all the testcase that are very straightforward label Sep 23, 2026
[ghstack-poisoned]
can-gaa-hou added a commit that referenced this pull request Sep 23, 2026
@fffrog fffrog moved this from Developing to Ready for Merge in PyTorch Test Refactoring Sep 23, 2026
[ghstack-poisoned]
can-gaa-hou added a commit that referenced this pull request Sep 23, 2026
@can-gaa-hou can-gaa-hou added the ciflow/crcr/npu Label for Ascend NPU in CRCR label Sep 23, 2026
@can-gaa-hou

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -i

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 23, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@github-project-automation github-project-automation Bot moved this from Ready for Merge to Done in PyTorch Test Refactoring Sep 23, 2026
pytorchmergebot pushed a commit that referenced this pull request Oct 9, 2026
Part of #142029.

Enables the device-generic tests in `test/profiler/test_profiler.py` on XPU. Most of the hardware classification this PR used to carry landed upstream in #198321-#198324. What is left, reviewable in order:

1. Classify the inline-annotation tests as generic (they need no CUDA) and name the CUDA-gated metadata test class for its gate.
2. Prime Kineto with the current accelerator instead of CUDA only. Without it, a CPU-only first session leaves XPU collection dead process-wide (`kernel`, `ac2g`, `xpu_runtime` all 0). The CUDA priming helper used by `test_cuspy.py` and `test_linalg.py` is unchanged.
3. Run the device-generic tests on XPU, drop a stale skip for the now-closed torch-xpu-ops#2263, and widen `test_kineto` to XPU.

### Coverage delta

Intel Data Center GPU Max 1550, oneAPI 2026.1.1.

| XPU test IDs | before | after |
| ------------ | ------ | ----- |
| total        | 0      | 20    |
| passed       | 0      | 18    |
| skipped      | 0      | 1     |
| failed       | 0      | 1     |

### Follow-ups filed against intel/torch-xpu-ops

- intel/torch-xpu-ops#5284 - XPU kernel activities carry `sycl queue` / `l0 queue` but no `stream`, `grid` or `block`. Skips `test_kineto_kernel_metadata_in_trace` on XPU, and blocks the CUDA metadata-format tests from being enabled as written.
- intel/torch-xpu-ops#5287 - `os.fork()` deadlocks after Kineto XPU init. Skips `test_forked_process` on XPU builds.
Pull Request resolved: #196466
Approved by: https://github.com/pbielak, https://github.com/etaf, https://github.com/jansel

Co-authored-by: Claude Opus 5 <[email protected]>
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
Part of pytorch#142029.

Enables the device-generic tests in `test/profiler/test_profiler.py` on XPU. Most of the hardware classification this PR used to carry landed upstream in pytorch#198321-pytorch#198324. What is left, reviewable in order:

1. Classify the inline-annotation tests as generic (they need no CUDA) and name the CUDA-gated metadata test class for its gate.
2. Prime Kineto with the current accelerator instead of CUDA only. Without it, a CPU-only first session leaves XPU collection dead process-wide (`kernel`, `ac2g`, `xpu_runtime` all 0). The CUDA priming helper used by `test_cuspy.py` and `test_linalg.py` is unchanged.
3. Run the device-generic tests on XPU, drop a stale skip for the now-closed torch-xpu-ops#2263, and widen `test_kineto` to XPU.

### Coverage delta

Intel Data Center GPU Max 1550, oneAPI 2026.1.1.

| XPU test IDs | before | after |
| ------------ | ------ | ----- |
| total        | 0      | 20    |
| passed       | 0      | 18    |
| skipped      | 0      | 1     |
| failed       | 0      | 1     |

### Follow-ups filed against intel/torch-xpu-ops

- intel/torch-xpu-ops#5284 - XPU kernel activities carry `sycl queue` / `l0 queue` but no `stream`, `grid` or `block`. Skips `test_kineto_kernel_metadata_in_trace` on XPU, and blocks the CUDA metadata-format tests from being enabled as written.
- intel/torch-xpu-ops#5287 - `os.fork()` deadlocks after Kineto XPU init. Skips `test_forked_process` on XPU builds.
Pull Request resolved: pytorch#196466
Approved by: https://github.com/pbielak, https://github.com/etaf, https://github.com/jansel

Co-authored-by: Claude Opus 5 <[email protected]>
jeffdaily added a commit to mwootton/pytorch that referenced this pull request Oct 28, 2026
test_kineto_kernel_metadata_in_trace (pytorch#180072) failed on ROCm because runtime blit kernels and graph replays had no grid/block; this PR's kineto change fixes that. test_structured_metadata_matches_chrome_trace (pytorch#179944) was fixed by pytorch#196487 and its skip was re-added when pytorch#198324 moved it into TestProfilerEventsParityCudaOnly. Also drop a skipIf on test_graph_kernel_events_have_dims that its class already applies.

Authored with Claude.
pytorchmergebot pushed a commit that referenced this pull request Nov 3, 2026
…me fill/copy kernels as memset/memcpy (#200296)

## 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
```

Pull Request resolved: #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 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]>
@github-actions
github-actions Bot deleted the gh/can-gaa-hou/55/head branch November 30, 2026 01:46
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

ciflow/crcr/npu Label for Ascend NPU in CRCR ciflow/trunk Trigger trunk jobs on your pull request Merged module: tests/simple This label covers all the testcase that are very straightforward open source topic: not user facing topic category

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants