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

Stop allocating CUPTI buffers after exceeding max buffer count (#1362) - #1362

Closed
scotts wants to merge 1 commit into
pytorch:mainfrom
scotts:export-D100731798
Closed

scotts wants to merge 1 commit into
pytorch:mainfrom
scotts:export-D100731798

Conversation

@scotts

@scotts scotts commented Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Fixes a runaway-memory allocation bug. When the allocated buffer count exceeds maxGpuBufferCount_, the code set stopCollection=true but continued to allocate a new 4MB buffer on every callback invocation. Since CUPTI keeps calling bufferRequested from its internal thread and the profiler runloop only checks stopCollection every ~1s (kControllerIntervalMsecs), this caused unbounded memory growth.

Fix: return *buffer = nullptr when the limit is hit. Per the CUPTI documentation for CUpti_BuffersCallbackRequestFunc, "If set to NULL then no buffer is returned." CUPTI will drop subsequent activity records, which are counted via cuptiActivityGetNumDroppedRecords.

XLA follows a similar pattern when it wants to indicate a problem with buffer allocation: https://github.com/openxla/xla/blob/a8d210262bee397cfaec30fe6f24b165da7cb21d/xla/backends/profiler/gpu/cupti_tracer.cc#L1679-L1684

Reviewed By: ryanzhang22

Differential Revision: D100731798

@meta-cla meta-cla Bot added the cla signed label Apr 14, 2026
@meta-codesync

meta-codesync Bot commented Apr 14, 2026

Copy link
Copy Markdown

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

@scotts
scotts force-pushed the export-D100731798 branch from fa742c6 to 962f502 Compare April 14, 2026 12:42
@meta-codesync meta-codesync Bot changed the title Stop allocating CUPTI buffers after exceeding max buffer count Stop allocating CUPTI buffers after exceeding max buffer count (#1362) Apr 14, 2026
scotts added a commit to scotts/kineto that referenced this pull request Apr 14, 2026
…ch#1362)

Summary:

Fixes a runaway-memory allocation bug. When the allocated buffer count exceeds `maxGpuBufferCount_`, the code set `stopCollection=true` but continued to allocate a new 4MB buffer on every callback invocation. Since CUPTI keeps calling bufferRequested from its internal thread and the profiler runloop only checks `stopCollection` every ~1s (`kControllerIntervalMsecs`), this caused unbounded memory growth.

Fix: `return *buffer = nullptr` when the limit is hit. Per the CUPTI documentation for `CUpti_BuffersCallbackRequestFunc`, "If set to NULL then no buffer is returned." CUPTI will drop subsequent activity records, which are counted via `cuptiActivityGetNumDroppedRecords`.

XLA follows a similar pattern when it wants to indicate a problem with buffer allocation: https://github.com/openxla/xla/blob/a8d210262bee397cfaec30fe6f24b165da7cb21d/xla/backends/profiler/gpu/cupti_tracer.cc#L1679-L1684

Reviewed By: ryanzhang22

Differential Revision: D100731798
@scotts
scotts force-pushed the export-D100731798 branch from 962f502 to 261cfc5 Compare April 14, 2026 16:09
scotts added a commit to scotts/kineto that referenced this pull request Apr 14, 2026
…ch#1362)

Summary:

Fixes a runaway-memory allocation bug. When the allocated buffer count exceeds `maxGpuBufferCount_`, the code set `stopCollection=true` but continued to allocate a new 4MB buffer on every callback invocation. Since CUPTI keeps calling bufferRequested from its internal thread and the profiler runloop only checks `stopCollection` every ~1s (`kControllerIntervalMsecs`), this caused unbounded memory growth.

Fix: `return *buffer = nullptr` when the limit is hit. Per the CUPTI documentation for `CUpti_BuffersCallbackRequestFunc`, "If set to NULL then no buffer is returned." CUPTI will drop subsequent activity records, which are counted via `cuptiActivityGetNumDroppedRecords`.

XLA follows a similar pattern when it wants to indicate a problem with buffer allocation: https://github.com/openxla/xla/blob/a8d210262bee397cfaec30fe6f24b165da7cb21d/xla/backends/profiler/gpu/cupti_tracer.cc#L1679-L1684

Reviewed By: ryanzhang22

Differential Revision: D100731798
@scotts
scotts force-pushed the export-D100731798 branch from 261cfc5 to b5f4f1c Compare April 14, 2026 17:35
scotts added a commit to scotts/kineto that referenced this pull request Apr 14, 2026
…ch#1362)

Summary:
Pull Request resolved: pytorch#1362

Fixes a runaway-memory allocation bug. When the allocated buffer count exceeds `maxGpuBufferCount_`, the code set `stopCollection=true` but continued to allocate a new 4MB buffer on every callback invocation. Since CUPTI keeps calling bufferRequested from its internal thread and the profiler runloop only checks `stopCollection` every ~1s (`kControllerIntervalMsecs`), this caused unbounded memory growth.

Fix: `return *buffer = nullptr` when the limit is hit. Per the CUPTI documentation for `CUpti_BuffersCallbackRequestFunc`, "If set to NULL then no buffer is returned." CUPTI will drop subsequent activity records, which are counted via `cuptiActivityGetNumDroppedRecords`.

XLA follows a similar pattern when it wants to indicate a problem with buffer allocation: https://github.com/openxla/xla/blob/a8d210262bee397cfaec30fe6f24b165da7cb21d/xla/backends/profiler/gpu/cupti_tracer.cc#L1679-L1684

Reviewed By: ryanzhang22

Differential Revision: D100731798
@scotts
scotts force-pushed the export-D100731798 branch from b5f4f1c to 18efd17 Compare April 14, 2026 17:46
scotts added a commit to scotts/kineto that referenced this pull request Apr 14, 2026
…ch#1362)

Summary:

Fixes a runaway-memory allocation bug. When the allocated buffer count exceeds `maxGpuBufferCount_`, the code set `stopCollection=true` but continued to allocate a new 4MB buffer on every callback invocation. Since CUPTI keeps calling bufferRequested from its internal thread and the profiler runloop only checks `stopCollection` every ~1s (`kControllerIntervalMsecs`), this caused unbounded memory growth.

Fix: `return *buffer = nullptr` when the limit is hit. Per the CUPTI documentation for `CUpti_BuffersCallbackRequestFunc`, "If set to NULL then no buffer is returned." CUPTI will drop subsequent activity records, which are counted via `cuptiActivityGetNumDroppedRecords`.

XLA follows a similar pattern when it wants to indicate a problem with buffer allocation: https://github.com/openxla/xla/blob/a8d210262bee397cfaec30fe6f24b165da7cb21d/xla/backends/profiler/gpu/cupti_tracer.cc#L1679-L1684

Reviewed By: ryanzhang22

Differential Revision: D100731798
@scotts
scotts force-pushed the export-D100731798 branch from 18efd17 to 195a3b9 Compare April 14, 2026 19:10
…ch#1362)

Summary:
Pull Request resolved: pytorch#1362

Fixes a runaway-memory allocation bug. When the allocated buffer count exceeds `maxGpuBufferCount_`, the code set `stopCollection=true` but continued to allocate a new 4MB buffer on every callback invocation. Since CUPTI keeps calling bufferRequested from its internal thread and the profiler runloop only checks `stopCollection` every ~1s (`kControllerIntervalMsecs`), this caused unbounded memory growth.

Fix: `return *buffer = nullptr` when the limit is hit. Per the CUPTI documentation for `CUpti_BuffersCallbackRequestFunc`, "If set to NULL then no buffer is returned." CUPTI will drop subsequent activity records, which are counted via `cuptiActivityGetNumDroppedRecords`.

XLA follows a similar pattern when it wants to indicate a problem with buffer allocation: https://github.com/openxla/xla/blob/a8d210262bee397cfaec30fe6f24b165da7cb21d/xla/backends/profiler/gpu/cupti_tracer.cc#L1679-L1684

Reviewed By: ryanzhang22

Differential Revision: D100731798
@meta-codesync

meta-codesync Bot commented Apr 15, 2026

Copy link
Copy Markdown

This pull request has been merged in 666f62c.

ryanzhang22 added a commit to ryanzhang22/pytorch that referenced this pull request Apr 15, 2026
Includes the following commits:

- Stop allocating CUPTI buffers after exceeding max buffer count (pytorch/kineto#1362) 666f62c
- Add XPU workflow (pytorch/kineto#1302) 11cc1e0
ryanzhang22 added a commit to ryanzhang22/pytorch that referenced this pull request Apr 15, 2026
Includes the following commits:

- Stop allocating CUPTI buffers after exceeding max buffer count (pytorch/kineto#1362) 666f62c
- Add XPU workflow (pytorch/kineto#1302) 11cc1e0
ryanzhang22 added a commit to ryanzhang22/pytorch that referenced this pull request Apr 15, 2026
Includes the following commits:

- Stop allocating CUPTI buffers after exceeding max buffer count (pytorch/kineto#1362) 666f62c
- Add XPU workflow (pytorch/kineto#1302) 11cc1e0
- Remove RocprofActivity.h/RoctracerActivity.h from RocmActivityProfiler.h (pytorch/kineto#1357) 896068d
- Split ActivityProfilerController into Sync and Async Handlers (pytorch/kineto#1269) 6d7f045
- Add priority field to kernel metadata (pytorch/kineto#1361) f2a7423
- Add kineto-release skill (pytorch/kineto#1360) 675b6cd
scotts added a commit to scotts/kineto that referenced this pull request Apr 15, 2026
Summary:
When the sync profiling path is active and we've reached our limit of CUPTI activity buffers, `CuptiActivityApi::bufferRequested()` sets `stopCollection = true`. The async path already checks this via `isGpuCollectionStopped()` in `performRunLoopStep()`, but the sync path has no way to query it — the method is protected on `GenericActivityProfiler` and not exposed through the public `ActivityProfilerInterface`.

This diff plumbs this status up through to the public interface.

A follow-up change in PyTorch will use this API in the Python profiler's `step()` method to detect that the profiling had to stop early and force an early `stop_trace`. 

We've already fixed it so that when there's too many activity buffers, CUPTI should stop requesting buffers (D100731798 / pytorch#1362). But we still don't have a way to tell the Python layer that profiling should stop in synchronous tracing.

Differential Revision: D101044411
scotts added a commit to scotts/kineto that referenced this pull request Apr 16, 2026
…aking into trace

PR pytorch#1362 (D100731798) fixed a bug in activityBuffers() where
readyGpuTraceBuffers_ was ignored when allocatedGpuTraceBuffers_ was empty.
A side effect is that CUPTI buffers flushed during
toggleCollectionDynamic(false) are now correctly returned during trace
processing. This surfaced a latent issue: synchronizeGpuDevice() calls
cudaDeviceSynchronize() while CUPTI is still enabled, so CUPTI captures
that call as a "cudaDeviceSynchronize" runtime activity. The event contains
"cuda" in its name and fails the PyTorch test_dynamic_toggle assertion that
no GPU events appear after the toggle.

Fix by disabling CUPTI activity collection before the device sync when
toggling off. The sync still serves its purpose — draining in-flight GPU
work and flushing pre-existing CUPTI buffers — but the sync call itself
is no longer instrumented.
@scotts scotts mentioned this pull request Apr 16, 2026
meta-codesync Bot pushed a commit that referenced this pull request Apr 16, 2026
Summary:
Fix test_dynamic_toggle failure caused by toggle's own CUDA events leaking into trace.

PR #1362 (D100731798) fixed a bug in `activityBuffers()` where `readyGpuTraceBuffers_` was ignored when `allocatedGpuTraceBuffers_` was empty. A side effect is that CUPTI buffers flushed during `toggleCollectionDynamic(false)` are now correctly returned during trace processing. This surfaced a latent issue: `synchronizeGpuDevice()` calls `cudaDeviceSynchronize()` while CUPTI is still enabled, so CUPTI captures that call as a "cudaDeviceSynchronize" runtime activity. The event contains "cuda" in its name and fails the PyTorch `test_dynamic_toggle` assertion that no GPU events appear after the toggle.

Fix by disabling CUPTI activity collection before the device sync when toggling off. The sync still serves its purpose — draining in-flight GPU work and flushing pre-existing CUPTI buffers — but the sync call itself is no longer instrumented.

Pull Request resolved: #1369

Reviewed By: ryanzhang22

Differential Revision: D101110738

Pulled By: scotts

fbshipit-source-id: 862c37459cf260679fc54d372f5bc4a1ccdd503e
meta-codesync Bot pushed a commit that referenced this pull request Apr 16, 2026
Summary:
Pull Request resolved: #1367

When the sync profiling path is active and we've reached our limit of CUPTI activity buffers, `CuptiActivityApi::bufferRequested()` sets `stopCollection = true`. The async path already checks this via `isGpuCollectionStopped()` in `performRunLoopStep()`, but the sync path has no way to query it — the method is protected on `GenericActivityProfiler` and not exposed through the public `ActivityProfilerInterface`.

This diff plumbs this status up through to the public interface.

A follow-up change in PyTorch will use this API in the Python profiler's `step()` method to detect that the profiling had to stop early and force an early `stop_trace`.

We've already fixed it so that when there's too many activity buffers, CUPTI should stop requesting buffers (D100731798 / #1362). But we still don't have a way to tell the Python layer that profiling should stop in synchronous tracing.

Reviewed By: ryanzhang22

Differential Revision: D101044411

fbshipit-source-id: 92754dd825b2d4ecaeceff94d2cab6bdf05876d5
scotts added a commit to scotts/pytorch that referenced this pull request Apr 16, 2026
Includes the following commits:

- Fix stream wait events referencing future correlation IDs (pytorch/kineto#1339) 23b5bb5
- Remove kineto tb_plugin directory entirely (pytorch/kineto#1368) 9497960
- Move Stream Sync events to a new row in JSON trace export (pytorch/kineto#1356) 041e7ce
- Expose isGpuCollectionStopped() through Kineto's public API (pytorch/kineto#1367) 17708f5
- Fix toggle test (pytorch/kineto#1369) ee2103c
- Link to correct fmt repo (pytorch/kineto#1345) 3447834
- Fix data race on CuptiActivityApi::externalCorrelationEnabled_ (pytorch/kineto#1365) 0e86499
- Stop allocating CUPTI buffers after exceeding max buffer count (pytorch/kineto#1362) 666f62c
- Add XPU workflow (pytorch/kineto#1302) 11cc1e0
- Remove RocprofActivity.h/RoctracerActivity.h from RocmActivityProfiler.h (pytorch/kineto#1357) 896068d
- Split ActivityProfilerController into Sync and Async Handlers (pytorch/kineto#1269) 6d7f045
- Add priority field to kernel metadata (pytorch/kineto#1361) f2a7423
- Add kineto-release skill (pytorch/kineto#1360) 675b6cd

Authored with Claude.
pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Apr 17, 2026
Includes the following commits:

- Fix stream wait events referencing future correlation IDs (pytorch/kineto#1339) 23b5bb5
- Remove kineto tb_plugin directory entirely (pytorch/kineto#1368) 9497960
- Move Stream Sync events to a new row in JSON trace export (pytorch/kineto#1356) 041e7ce
- Expose isGpuCollectionStopped() through Kineto's public API (pytorch/kineto#1367) 17708f5
- Fix toggle test (pytorch/kineto#1369) ee2103c
- Link to correct fmt repo (pytorch/kineto#1345) 3447834
- Fix data race on CuptiActivityApi::externalCorrelationEnabled_ (pytorch/kineto#1365) 0e86499
- Stop allocating CUPTI buffers after exceeding max buffer count (pytorch/kineto#1362) 666f62c
- Add XPU workflow (pytorch/kineto#1302) 11cc1e0
- Remove RocprofActivity.h/RoctracerActivity.h from RocmActivityProfiler.h (pytorch/kineto#1357) 896068d
- Split ActivityProfilerController into Sync and Async Handlers (pytorch/kineto#1269) 6d7f045
- Add priority field to kernel metadata (pytorch/kineto#1361) f2a7423
- Add kineto-release skill (pytorch/kineto#1360) 675b6cd

Authored with Claude.
Pull Request resolved: #180606
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
Includes the following commits:

- Fix stream wait events referencing future correlation IDs (pytorch/kineto#1339) 23b5bb5
- Remove kineto tb_plugin directory entirely (pytorch/kineto#1368) 9497960
- Move Stream Sync events to a new row in JSON trace export (pytorch/kineto#1356) 041e7ce
- Expose isGpuCollectionStopped() through Kineto's public API (pytorch/kineto#1367) 17708f5
- Fix toggle test (pytorch/kineto#1369) ee2103c
- Link to correct fmt repo (pytorch/kineto#1345) 3447834
- Fix data race on CuptiActivityApi::externalCorrelationEnabled_ (pytorch/kineto#1365) 0e86499
- Stop allocating CUPTI buffers after exceeding max buffer count (pytorch/kineto#1362) 666f62c
- Add XPU workflow (pytorch/kineto#1302) 11cc1e0
- Remove RocprofActivity.h/RoctracerActivity.h from RocmActivityProfiler.h (pytorch/kineto#1357) 896068d
- Split ActivityProfilerController into Sync and Async Handlers (pytorch/kineto#1269) 6d7f045
- Add priority field to kernel metadata (pytorch/kineto#1361) f2a7423
- Add kineto-release skill (pytorch/kineto#1360) 675b6cd

Authored with Claude.
Pull Request resolved: pytorch#180606
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Includes the following commits:

- Fix stream wait events referencing future correlation IDs (pytorch/kineto#1339) 23b5bb5
- Remove kineto tb_plugin directory entirely (pytorch/kineto#1368) 9497960
- Move Stream Sync events to a new row in JSON trace export (pytorch/kineto#1356) 041e7ce
- Expose isGpuCollectionStopped() through Kineto's public API (pytorch/kineto#1367) 17708f5
- Fix toggle test (pytorch/kineto#1369) ee2103c
- Link to correct fmt repo (pytorch/kineto#1345) 3447834
- Fix data race on CuptiActivityApi::externalCorrelationEnabled_ (pytorch/kineto#1365) 0e86499
- Stop allocating CUPTI buffers after exceeding max buffer count (pytorch/kineto#1362) 666f62c
- Add XPU workflow (pytorch/kineto#1302) 11cc1e0
- Remove RocprofActivity.h/RoctracerActivity.h from RocmActivityProfiler.h (pytorch/kineto#1357) 896068d
- Split ActivityProfilerController into Sync and Async Handlers (pytorch/kineto#1269) 6d7f045
- Add priority field to kernel metadata (pytorch/kineto#1361) f2a7423
- Add kineto-release skill (pytorch/kineto#1360) 675b6cd

Authored with Claude.
Pull Request resolved: pytorch#180606
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Includes the following commits:

- Fix stream wait events referencing future correlation IDs (pytorch/kineto#1339) 23b5bb5
- Remove kineto tb_plugin directory entirely (pytorch/kineto#1368) 9497960
- Move Stream Sync events to a new row in JSON trace export (pytorch/kineto#1356) 041e7ce
- Expose isGpuCollectionStopped() through Kineto's public API (pytorch/kineto#1367) 17708f5
- Fix toggle test (pytorch/kineto#1369) ee2103c
- Link to correct fmt repo (pytorch/kineto#1345) 3447834
- Fix data race on CuptiActivityApi::externalCorrelationEnabled_ (pytorch/kineto#1365) 0e86499
- Stop allocating CUPTI buffers after exceeding max buffer count (pytorch/kineto#1362) 666f62c
- Add XPU workflow (pytorch/kineto#1302) 11cc1e0
- Remove RocprofActivity.h/RoctracerActivity.h from RocmActivityProfiler.h (pytorch/kineto#1357) 896068d
- Split ActivityProfilerController into Sync and Async Handlers (pytorch/kineto#1269) 6d7f045
- Add priority field to kernel metadata (pytorch/kineto#1361) f2a7423
- Add kineto-release skill (pytorch/kineto#1360) 675b6cd

Authored with Claude.
Pull Request resolved: pytorch#180606
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
scotts added a commit to scotts/kineto that referenced this pull request Aug 14, 2026
**Problem**

Kineto drops activity recorded during the warmup period so it does not
show up in the collected trace, and it drops leftover activity when
resetting between sessions. Both go through clearActivities(), which
gives up early whenever no buffers are in flight with CUPTI.

That test is wrong for the work being skipped. Having nothing in flight
means there is nothing to ask CUPTI to flush; it says nothing about
whether CUPTI has already handed back completed buffers that still need
to go. So in the common case - CUPTI returns buffers promptly, and the
warmup flush runs on a runloop tick after everything has come back -
the early return fires and the warmup records survive. They either land
in the trace the user collects or carry over into the next session.

The memory is not leaked; the next clearActivities() or activityBuffers()
frees it.

**Fix**

Release the completed buffers before returning. This mirrors what pytorch#1362
did one function down in activityBuffers().

The new ClearsCompletedBuffers case fails without the fix. It also
documents the two preconditions that keep the buffer tests away from
live CUPTI - a null CUcontext that reaches cuptiActivityGetNumDroppedRecords
when run at verbosity 1, and the drained allocated-buffer map that lets
activityBuffers() and clearActivities() short-circuit instead of asking
CUPTI to flush. Both are load-bearing and were undocumented.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant