Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/186034
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New FailureAs of commit 7e38c4f with merge base 3e663d6 ( NEW FAILURE - The following job has failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
| ) | ||
|
|
||
| self._lib = ctypes.CDLL(_find_cupti_library()) | ||
| self._setup_prototypes() |
There was a problem hiding this comment.
Depending on ctypes feels quite brittle long-term. If we get good signal from this prototype, I think we should explore where a natural boundary is between what should be done in Python, and what should be done in C++.
There was a problem hiding this comment.
Yeah we will move mostly to cupti-python, @dolpm 's follow up goes in that direction
| else: | ||
| arr = (ctypes.c_uint8 * self.buffer_size)() | ||
| ptr = ctypes.addressof(arr) | ||
| self._buffer_keepalive[ptr] = arr |
There was a problem hiding this comment.
So _buffer_keepalive grows unbounded. Does that cause problems during, say, a 12 hour run? We might have to come up with some rationale about when to drop data, or doing period flushing to disk to avoid the in-memory buffer from growing unbounded.
|
This is exciting! When we get more signal from this experimental feature, I think we'll want to harden it more. That may mean figuring out how to do some of this in a C++ layer, potentially integrating it with what we already have. |
|
@pytorchbot drci |
|
@pytorchbot merge |
Merge startedYour 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 |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / linux-jammy-cuda13.0-py3.10-gcc11 / test-osdc (distributed, 2, 3, mt-l-x86iavx512-45-172-t4-4) Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 1 checks: trunk / linux-jammy-cuda13.0-py3.10-gcc11 / test-osdc (distributed, 2, 3, mt-l-x86iavx512-45-172-t4-4) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Derive the Kineto cpuOnly flag from the requested activity set rather than from whether CUDA or another device backend is present on the machine. This keeps CPU-only profiling from arming GPU tracing when the user did not request any device activities, while preserving the default behavior when available device activities are included. Pull Request resolved: #186036 Approved by: https://github.com/dolpm, https://github.com/scotts ghstack dependencies: #186034
Add a hidden experimental profiler backend that uses the CUPTI monitor for GPU collection while reusing the existing CPU profiler path. This commit wires the monitor into profiler scheduling and export, constructs merged Chrome traces, recreates GPU-side user annotations for Python record_function scopes, maps CUDA runtime events onto the same CPU thread lanes as stock traces, and keeps eager/graph traces close to the existing profiler behavior. Pull Request resolved: #186037 Approved by: https://github.com/scotts ghstack dependencies: #186034, #186036
…ck (#186295) The experimental CUPTI monitor (torch.profiler._cupti_monitor) uses the CUPTI Activity API, which hands completed records to us only through two callbacks that CUPTI invokes itself: buffer-requested (asks us for an empty buffer to fill) and buffer-completed (returns a filled one). The Activity API has no polling alternative, so using it means registering these callbacks, and CUPTI calls them synchronously from its own threads while holding its internal lock. Previously the callbacks were Python/ctypes closures, so CUPTI acquired the GIL while holding that lock. A thread that holds the GIL and then enters CUPTI/CUDA can deadlock against such a callback (GIL <-> CUPTI lock inversion). The callbacks are mandatory and the hazard is intrinsic to running any Python inside them, so the only fix is to make the callback bodies native. This moves the buffer pool and completed-buffer queue into C++ (torch/csrc/profiler/cupti_monitor_native.{h,cpp}). The buffer-requested and buffer-completed callback bodies are now pure C++: they only touch a mutex-guarded free list and a completed-buffer queue, never Python. The Python decode thread pulls completed buffers via _cupti_monitor_get_completed(), which releases the GIL while it blocks on a condition variable, and hands them back with _cupti_monitor_return_buffer(). Registration with CUPTI still happens on the Python side via ctypes using the callback addresses exposed in init.cpp, so the pool/queue/callbacks stay reusable for the per-subscriber _v2 Activity APIs later: only the registration call would gain a subscriber handle. The free list is LIFO so a returned buffer is reused while it is still the warmest in cache. The new torch._C._profiler symbols are added to the _profiler.pyi stub so callers use direct attribute access instead of getattr. v2 (subscriber-scoped) Activity API support The same native pool is exposed for cuptiActivityRegisterCallbacks_v2 so a prototype can build on the v2 / user-defined-record API (CUPTI ≥ 13.2) without any further C++: - _cupti_monitor_buffer_request_callback_address(version) / _cupti_monitor_buffer_complete_callback_address(version) take a version (1 or 2, default 1) and return native callbacks with the v1 or v2 CUPTI ABI. Both feed the same pool/queue. - v2 does not deliver CUcontext/streamId to the completion callback (they become selectable record fields), so buffers completed on the v2 path report ctx/stream as 0. - The v2 completion callback snapshots the user-defined record layout — which CUPTI only makes valid for the duration of the callback — once per session (the layout is fixed by the field selection). It's exposed via _cupti_monitor_record_layouts() as [(kind, record_size, [(field_id, offset, size), ...]), ...] so a decode thread can parse field-selected records after the callback returns. The CUPTI v2 structs are mirrored by ABI in the .cpp so the build needs no v2 header. All registration (cuptiSubscribe / cuptiActivityRegisterCallbacks_v2 / cuptiActivityEnable_v2 / the USER_DEFINED_RECORDS attribute) stays on the Python/ctypes side for a future prototype. Test Plan: ``` python test/profiler/test_profiler.py TestProfiler.test_cupti_monitor_buffer_pool_reuse python test/profiler/test_profiler.py TestProfilerCUDA -k cupti_monitor ``` The first test drives the native callbacks directly via ctypes (no CUDA or cupti-python) and asserts returned buffers are recycled rather than reallocated; the second exercises the full path through real CUPTI, including a new assertion that the native pool was actually used. Authored-with: Claude (Anthropic AI assistant) Pull Request resolved: #186295 Approved by: https://github.com/dolpm ghstack dependencies: #186034, #186036, #186037
Introduce a hidden experimental CUPTI monitor module for profiler experimentation. The monitor owns the Activity API callback registration and collection lifecycle, supports raw always-on buffer dumping, and exposes an early HES enable hook for environments that opt into hardware timestamp collection before CUDA context creation. The commit also adds basic monitor smoke tests covering HES guard behavior and monitor collection lifecycle. Pull Request resolved: pytorch#186034 Approved by: https://github.com/dolpm, https://github.com/scotts
…#186036) Derive the Kineto cpuOnly flag from the requested activity set rather than from whether CUDA or another device backend is present on the machine. This keeps CPU-only profiling from arming GPU tracing when the user did not request any device activities, while preserving the default behavior when available device activities are included. Pull Request resolved: pytorch#186036 Approved by: https://github.com/dolpm, https://github.com/scotts ghstack dependencies: pytorch#186034
…186037) Add a hidden experimental profiler backend that uses the CUPTI monitor for GPU collection while reusing the existing CPU profiler path. This commit wires the monitor into profiler scheduling and export, constructs merged Chrome traces, recreates GPU-side user annotations for Python record_function scopes, maps CUDA runtime events onto the same CPU thread lanes as stock traces, and keeps eager/graph traces close to the existing profiler behavior. Pull Request resolved: pytorch#186037 Approved by: https://github.com/scotts ghstack dependencies: pytorch#186034, pytorch#186036
…ck (pytorch#186295) The experimental CUPTI monitor (torch.profiler._cupti_monitor) uses the CUPTI Activity API, which hands completed records to us only through two callbacks that CUPTI invokes itself: buffer-requested (asks us for an empty buffer to fill) and buffer-completed (returns a filled one). The Activity API has no polling alternative, so using it means registering these callbacks, and CUPTI calls them synchronously from its own threads while holding its internal lock. Previously the callbacks were Python/ctypes closures, so CUPTI acquired the GIL while holding that lock. A thread that holds the GIL and then enters CUPTI/CUDA can deadlock against such a callback (GIL <-> CUPTI lock inversion). The callbacks are mandatory and the hazard is intrinsic to running any Python inside them, so the only fix is to make the callback bodies native. This moves the buffer pool and completed-buffer queue into C++ (torch/csrc/profiler/cupti_monitor_native.{h,cpp}). The buffer-requested and buffer-completed callback bodies are now pure C++: they only touch a mutex-guarded free list and a completed-buffer queue, never Python. The Python decode thread pulls completed buffers via _cupti_monitor_get_completed(), which releases the GIL while it blocks on a condition variable, and hands them back with _cupti_monitor_return_buffer(). Registration with CUPTI still happens on the Python side via ctypes using the callback addresses exposed in init.cpp, so the pool/queue/callbacks stay reusable for the per-subscriber _v2 Activity APIs later: only the registration call would gain a subscriber handle. The free list is LIFO so a returned buffer is reused while it is still the warmest in cache. The new torch._C._profiler symbols are added to the _profiler.pyi stub so callers use direct attribute access instead of getattr. v2 (subscriber-scoped) Activity API support The same native pool is exposed for cuptiActivityRegisterCallbacks_v2 so a prototype can build on the v2 / user-defined-record API (CUPTI ≥ 13.2) without any further C++: - _cupti_monitor_buffer_request_callback_address(version) / _cupti_monitor_buffer_complete_callback_address(version) take a version (1 or 2, default 1) and return native callbacks with the v1 or v2 CUPTI ABI. Both feed the same pool/queue. - v2 does not deliver CUcontext/streamId to the completion callback (they become selectable record fields), so buffers completed on the v2 path report ctx/stream as 0. - The v2 completion callback snapshots the user-defined record layout — which CUPTI only makes valid for the duration of the callback — once per session (the layout is fixed by the field selection). It's exposed via _cupti_monitor_record_layouts() as [(kind, record_size, [(field_id, offset, size), ...]), ...] so a decode thread can parse field-selected records after the callback returns. The CUPTI v2 structs are mirrored by ABI in the .cpp so the build needs no v2 header. All registration (cuptiSubscribe / cuptiActivityRegisterCallbacks_v2 / cuptiActivityEnable_v2 / the USER_DEFINED_RECORDS attribute) stays on the Python/ctypes side for a future prototype. Test Plan: ``` python test/profiler/test_profiler.py TestProfiler.test_cupti_monitor_buffer_pool_reuse python test/profiler/test_profiler.py TestProfilerCUDA -k cupti_monitor ``` The first test drives the native callbacks directly via ctypes (no CUDA or cupti-python) and asserts returned buffers are recycled rather than reallocated; the second exercises the full path through real CUPTI, including a new assertion that the native pool was actually used. Authored-with: Claude (Anthropic AI assistant) Pull Request resolved: pytorch#186295 Approved by: https://github.com/dolpm ghstack dependencies: pytorch#186034, pytorch#186036, pytorch#186037
Introduce a hidden experimental CUPTI monitor module for profiler experimentation. The monitor owns the Activity API callback registration and collection lifecycle, supports raw always-on buffer dumping, and exposes an early HES enable hook for environments that opt into hardware timestamp collection before CUDA context creation. The commit also adds basic monitor smoke tests covering HES guard behavior and monitor collection lifecycle. Pull Request resolved: pytorch#186034 Approved by: https://github.com/dolpm, https://github.com/scotts
…#186036) Derive the Kineto cpuOnly flag from the requested activity set rather than from whether CUDA or another device backend is present on the machine. This keeps CPU-only profiling from arming GPU tracing when the user did not request any device activities, while preserving the default behavior when available device activities are included. Pull Request resolved: pytorch#186036 Approved by: https://github.com/dolpm, https://github.com/scotts ghstack dependencies: pytorch#186034
…186037) Add a hidden experimental profiler backend that uses the CUPTI monitor for GPU collection while reusing the existing CPU profiler path. This commit wires the monitor into profiler scheduling and export, constructs merged Chrome traces, recreates GPU-side user annotations for Python record_function scopes, maps CUDA runtime events onto the same CPU thread lanes as stock traces, and keeps eager/graph traces close to the existing profiler behavior. Pull Request resolved: pytorch#186037 Approved by: https://github.com/scotts ghstack dependencies: pytorch#186034, pytorch#186036
…ck (pytorch#186295) The experimental CUPTI monitor (torch.profiler._cupti_monitor) uses the CUPTI Activity API, which hands completed records to us only through two callbacks that CUPTI invokes itself: buffer-requested (asks us for an empty buffer to fill) and buffer-completed (returns a filled one). The Activity API has no polling alternative, so using it means registering these callbacks, and CUPTI calls them synchronously from its own threads while holding its internal lock. Previously the callbacks were Python/ctypes closures, so CUPTI acquired the GIL while holding that lock. A thread that holds the GIL and then enters CUPTI/CUDA can deadlock against such a callback (GIL <-> CUPTI lock inversion). The callbacks are mandatory and the hazard is intrinsic to running any Python inside them, so the only fix is to make the callback bodies native. This moves the buffer pool and completed-buffer queue into C++ (torch/csrc/profiler/cupti_monitor_native.{h,cpp}). The buffer-requested and buffer-completed callback bodies are now pure C++: they only touch a mutex-guarded free list and a completed-buffer queue, never Python. The Python decode thread pulls completed buffers via _cupti_monitor_get_completed(), which releases the GIL while it blocks on a condition variable, and hands them back with _cupti_monitor_return_buffer(). Registration with CUPTI still happens on the Python side via ctypes using the callback addresses exposed in init.cpp, so the pool/queue/callbacks stay reusable for the per-subscriber _v2 Activity APIs later: only the registration call would gain a subscriber handle. The free list is LIFO so a returned buffer is reused while it is still the warmest in cache. The new torch._C._profiler symbols are added to the _profiler.pyi stub so callers use direct attribute access instead of getattr. v2 (subscriber-scoped) Activity API support The same native pool is exposed for cuptiActivityRegisterCallbacks_v2 so a prototype can build on the v2 / user-defined-record API (CUPTI ≥ 13.2) without any further C++: - _cupti_monitor_buffer_request_callback_address(version) / _cupti_monitor_buffer_complete_callback_address(version) take a version (1 or 2, default 1) and return native callbacks with the v1 or v2 CUPTI ABI. Both feed the same pool/queue. - v2 does not deliver CUcontext/streamId to the completion callback (they become selectable record fields), so buffers completed on the v2 path report ctx/stream as 0. - The v2 completion callback snapshots the user-defined record layout — which CUPTI only makes valid for the duration of the callback — once per session (the layout is fixed by the field selection). It's exposed via _cupti_monitor_record_layouts() as [(kind, record_size, [(field_id, offset, size), ...]), ...] so a decode thread can parse field-selected records after the callback returns. The CUPTI v2 structs are mirrored by ABI in the .cpp so the build needs no v2 header. All registration (cuptiSubscribe / cuptiActivityRegisterCallbacks_v2 / cuptiActivityEnable_v2 / the USER_DEFINED_RECORDS attribute) stays on the Python/ctypes side for a future prototype. Test Plan: ``` python test/profiler/test_profiler.py TestProfiler.test_cupti_monitor_buffer_pool_reuse python test/profiler/test_profiler.py TestProfilerCUDA -k cupti_monitor ``` The first test drives the native callbacks directly via ctypes (no CUDA or cupti-python) and asserts returned buffers are recycled rather than reallocated; the second exercises the full path through real CUPTI, including a new assertion that the native pool was actually used. Authored-with: Claude (Anthropic AI assistant) Pull Request resolved: pytorch#186295 Approved by: https://github.com/dolpm ghstack dependencies: pytorch#186034, pytorch#186036, pytorch#186037
Stack from ghstack (oldest at bottom):
Introduce a hidden experimental CUPTI monitor module for profiler
experimentation.
The monitor owns the Activity API callback registration and collection
lifecycle, supports raw always-on buffer dumping, and exposes an early
HES enable hook for environments that opt into hardware timestamp
collection before CUDA context creation.
The commit also adds basic monitor smoke tests covering HES guard
behavior and monitor collection lifecycle.