pytorch - ✅(Solved) Fix [Profiler] Expose moduleHierarchy, activityType, and other KinetoEvent methods to Python [1 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#178087Fetched 2026-04-08 01:12:11
View on GitHub
Comments
1
Participants
2
Timeline
32
Reactions
0
Author
Timeline (top)
mentioned ×14subscribed ×14labeled ×2commented ×1

Fix Action

Fixed

PR fix notes

PR #178086: Expose more KinetoEvent Python bindings

Description (problem / solution / changelog)

These methods already exist on the C++ KinetoEvent class but were not bound to Python. This makes them accessible for tools that consume profiler data directly from kineto_results.events() without going through export_chrome_trace().

New bindings:

  • module_hierarchy() -> list[str]: nn.Module hierarchy path
  • activity_type() -> int: libkineto ActivityType enum value
  • get_perf_event_counters() -> list[int]: hardware perf counters
  • debug_handle() -> int: module correlation handle
  • extra_meta() -> dict[str, str]: additional key-value metadata

Follows the same lambda wrapper pattern used by existing bindings like shapes(), stack(), and metadata_json().

Fixes https://github.com/pytorch/pytorch/issues/178087

Changed files

  • test/profiler/test_profiler.py (modified, +74/-0)
  • torch/csrc/autograd/init.cpp (modified, +50/-1)
RAW_BUFFERClick to expand / collapse

🚀 The feature, motivation and pitch

Several methods exist on the C++ KinetoEvent class but are not exposed through the Python pybind11 bindings in torch/csrc/autograd/init.cpp:

  • moduleHierarchy() — maps ops to their nn.Module path (e.g. ResNet.layer1.0.conv1)
  • activityType() — distinguishes CPU_OP, CUDA_RUNTIME, CONCURRENT_KERNEL, etc.
  • getPerfEventCounters() — hardware performance counters from CUPTI
  • debugHandle() — module correlation handle
  • extraMeta() — additional key-value metadata

Currently the only way to access module hierarchy is through export_chrome_trace() and parsing the JSON output. Direct Python access enables programmatic analysis of profiler data without depending on the Chrome JSON path.

The bindings follow the exact same lambda wrapper pattern already used by shapes(), stack(), and metadata_json().

Alternatives

Parsing metadata_json() for module hierarchy works but is fragile — it's a JSON string, not structured data.

Additional context

I have a PR ready: https://github.com/aMayzner/pytorch/tree/expose-profiler-bindings

cc @robieta @chaekit @guotuofeng @guyang3532 @dzhulgakov @davidberard98 @briancoutinho @sraikund16 @sanrise @mwootton @divyanshk @jiannanWang @scotts @ryanzhang22

extent analysis

Fix Plan

To expose the missing methods from the C++ KinetoEvent class through the Python pybind11 bindings, we need to add the necessary bindings in torch/csrc/autograd/init.cpp.

Here are the steps:

  • Add the following lines to torch/csrc/autograd/init.cpp to expose the moduleHierarchy(), activityType(), getPerfEventCounters(), debugHandle(), and extraMeta() methods:
.pybind11::class_<KinetoEvent>(m, "KinetoEvent")
    // ...
    .def("moduleHierarchy", &KinetoEvent::moduleHierarchy)
    .def("activityType", &KinetoEvent::activityType)
    .def("getPerfEventCounters", &KinetoEvent::getPerfEventCounters)
    .def("debugHandle", &KinetoEvent::debugHandle)
    .def("extraMeta", &KinetoEvent::extraMeta);
  • Ensure that the KinetoEvent class has the necessary methods and that they are correctly implemented.

Verification

To verify that the fix worked, you can use the following Python code:

import torch

# Create a KinetoEvent object
event = torch.KinetoEvent()

# Test the new methods
print(event.moduleHierarchy())
print(event.activityType())
print(event.getPerfEventCounters())
print(event.debugHandle())
print(event.extraMeta())

This should print the expected output for each method.

Extra Tips

  • Make sure to rebuild and reinstall PyTorch after making changes to the C++ code.
  • You can use the pybind11 documentation to learn more about how to create Python bindings for C++ classes and methods.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING