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

Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (#1350) - #1350

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

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

Conversation

@scotts

@scotts scotts commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

CuptiCallbackApi::singleton() returns std::shared_ptr<CuptiCallbackApi> by
value. Every call copies the shared_ptr, incurring two atomic ref-count
operations (increment on copy, decrement on destruction). The
callback_switchboard function -- the CUPTI hot path fired on every
cudaLaunchKernel -- calls singleton() on each invocation. On ARM/aarch64,
atomic operations are more expensive than on x86 due to load-exclusive /
store-exclusive barriers, making this overhead measurable during profiling.

This diff changes singleton() to return CuptiCallbackApi& from a Meyers
singleton (static CuptiCallbackApi instance), eliminating the per-call atomic
overhead. This matches the pattern already used by CuptiActivityApi::singleton().

SIOF safety analysis:

D39942067 / PR #661 introduced the shared_ptr to fix a SIOF crash on aarch64. We
analyzed all static initialization dependencies and found:

  1. CuptiCallbackApi's constructor is = default with all trivially
    constructible members. It has zero static dependencies at construction time.

  2. The actual SIOF was between initCallbackApi() and GLOG -- there is an
    explicit comment in the code: "TODO: Remove temporarily to work around
    static initialization order issue between this and GLOG." The problematic
    LOG() call was already commented out as a workaround.

  3. The shared_ptr does not meaningfully fix SIOF: no caller stores the
    shared_ptr long-term, so the lifetime-extension property is never used.
    Both the shared_ptr static and a Meyers singleton have identical
    construction timing (first call) and destruction timing (static destruction).

  4. A Meyers singleton is safe here because CuptiCallbackApi() has no
    dependencies on other statics. The SIOF risk is in initCallbackApi()
    (which calls CUPTI and potentially GLOG), not in singleton construction.

Differential Revision: D99843809

@meta-codesync

meta-codesync Bot commented Apr 7, 2026

Copy link
Copy Markdown

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

@meta-cla meta-cla Bot added the cla signed label Apr 7, 2026
@meta-codesync meta-codesync Bot changed the title Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (#1350) Apr 7, 2026
@scotts
scotts force-pushed the export-D99843809 branch from c8fd694 to cde2f82 Compare April 7, 2026 16:45
scotts added a commit to scotts/kineto that referenced this pull request Apr 7, 2026
pytorch#1350)

Summary:

`CuptiCallbackApi::singleton()` returns `std::shared_ptr<CuptiCallbackApi>` by
value. Every call copies the shared_ptr, incurring two atomic ref-count
operations (increment on copy, decrement on destruction). The
`callback_switchboard` function -- the CUPTI hot path fired on every
`cudaLaunchKernel` -- calls `singleton()` on each invocation. On ARM/aarch64,
atomic operations are more expensive than on x86 due to load-exclusive /
store-exclusive barriers, making this overhead measurable during profiling.

This diff changes `singleton()` to return `CuptiCallbackApi&` from a Meyers
singleton (`static CuptiCallbackApi instance`), eliminating the per-call atomic
overhead. This matches the pattern already used by `CuptiActivityApi::singleton()`.

**SIOF safety analysis:**

D39942067 introduced the `shared_ptr` to fix a SIOF crash on aarch64. We
analyzed all static initialization dependencies and found:

1. `CuptiCallbackApi`'s constructor is `= default` with all trivially
   constructible members. It has zero static dependencies at construction time.

2. The actual SIOF was between `initCallbackApi()` and GLOG -- there is an
   explicit comment in the code: "TODO: Remove temporarily to work around
   static initialization order issue between this and GLOG." The problematic
   `LOG()` call was already commented out as a workaround.

3. The `shared_ptr` does not meaningfully fix SIOF: no caller stores the
   `shared_ptr` long-term, so the lifetime-extension property is never used.
   Both the `shared_ptr` static and a Meyers singleton have identical
   construction timing (first call) and destruction timing (static destruction).

4. A Meyers singleton is safe here because `CuptiCallbackApi()` has no
   dependencies on other statics. The SIOF risk is in `initCallbackApi()`
   (which calls CUPTI and potentially GLOG), not in singleton construction.

Differential Revision: D99843809
scotts added a commit to scotts/kineto that referenced this pull request Apr 7, 2026
pytorch#1350)

Summary:
Pull Request resolved: pytorch#1350

`CuptiCallbackApi::singleton()` returns `std::shared_ptr<CuptiCallbackApi>` by
value. Every call copies the shared_ptr, incurring two atomic ref-count
operations (increment on copy, decrement on destruction). The
`callback_switchboard` function -- the CUPTI hot path fired on every
`cudaLaunchKernel` -- calls `singleton()` on each invocation. On ARM/aarch64,
atomic operations are more expensive than on x86 due to load-exclusive /
store-exclusive barriers, making this overhead measurable during profiling.

This diff changes `singleton()` to return `CuptiCallbackApi&` from a Meyers
singleton (`static CuptiCallbackApi instance`), eliminating the per-call atomic
overhead. This matches the pattern already used by `CuptiActivityApi::singleton()`.

**SIOF safety analysis:**

D39942067 introduced the `shared_ptr` to fix a SIOF crash on aarch64. We
analyzed all static initialization dependencies and found:

1. `CuptiCallbackApi`'s constructor is `= default` with all trivially
   constructible members. It has zero static dependencies at construction time.

2. The actual SIOF was between `initCallbackApi()` and GLOG -- there is an
   explicit comment in the code: "TODO: Remove temporarily to work around
   static initialization order issue between this and GLOG." The problematic
   `LOG()` call was already commented out as a workaround.

3. The `shared_ptr` does not meaningfully fix SIOF: no caller stores the
   `shared_ptr` long-term, so the lifetime-extension property is never used.
   Both the `shared_ptr` static and a Meyers singleton have identical
   construction timing (first call) and destruction timing (static destruction).

4. A Meyers singleton is safe here because `CuptiCallbackApi()` has no
   dependencies on other statics. The SIOF risk is in `initCallbackApi()`
   (which calls CUPTI and potentially GLOG), not in singleton construction.

Differential Revision: D99843809
@scotts
scotts force-pushed the export-D99843809 branch from cde2f82 to 70348c0 Compare April 7, 2026 16:49
scotts added a commit to scotts/kineto that referenced this pull request Apr 7, 2026
pytorch#1350)

Summary:

`CuptiCallbackApi::singleton()` returns `std::shared_ptr<CuptiCallbackApi>` by
value. Every call copies the shared_ptr, incurring two atomic ref-count
operations (increment on copy, decrement on destruction). The
`callback_switchboard` function -- the CUPTI hot path fired on every
`cudaLaunchKernel` -- calls `singleton()` on each invocation. On ARM/aarch64,
atomic operations are more expensive than on x86 due to load-exclusive /
store-exclusive barriers, making this overhead measurable during profiling.

This diff changes `singleton()` to return `CuptiCallbackApi&` from a Meyers
singleton (`static CuptiCallbackApi instance`), eliminating the per-call atomic
overhead. This matches the pattern already used by `CuptiActivityApi::singleton()`.

**SIOF safety analysis:**

D39942067 / PR pytorch#661 introduced the `shared_ptr` to fix a SIOF crash on aarch64. We
analyzed all static initialization dependencies and found:

1. `CuptiCallbackApi`'s constructor is `= default` with all trivially
   constructible members. It has zero static dependencies at construction time.

2. The actual SIOF was between `initCallbackApi()` and GLOG -- there is an
   explicit comment in the code: "TODO: Remove temporarily to work around
   static initialization order issue between this and GLOG." The problematic
   `LOG()` call was already commented out as a workaround.

3. The `shared_ptr` does not meaningfully fix SIOF: no caller stores the
   `shared_ptr` long-term, so the lifetime-extension property is never used.
   Both the `shared_ptr` static and a Meyers singleton have identical
   construction timing (first call) and destruction timing (static destruction).

4. A Meyers singleton is safe here because `CuptiCallbackApi()` has no
   dependencies on other statics. The SIOF risk is in `initCallbackApi()`
   (which calls CUPTI and potentially GLOG), not in singleton construction.

Differential Revision: D99843809
@scotts
scotts force-pushed the export-D99843809 branch from 70348c0 to af8fe8c Compare April 7, 2026 17:05
pytorch#1350)

Summary:

`CuptiCallbackApi::singleton()` returns `std::shared_ptr<CuptiCallbackApi>` by
value. Every call copies the shared_ptr, incurring two atomic ref-count
operations (increment on copy, decrement on destruction). The
`callback_switchboard` function -- the CUPTI hot path fired on every
`cudaLaunchKernel` -- calls `singleton()` on each invocation. On ARM/aarch64,
atomic operations are more expensive than on x86 due to load-exclusive /
store-exclusive barriers, making this overhead measurable during profiling.

This diff changes `singleton()` to return `CuptiCallbackApi&` from a Meyers
singleton (`static CuptiCallbackApi instance`), eliminating the per-call atomic
overhead. This matches the pattern already used by `CuptiActivityApi::singleton()`.

**SIOF safety analysis:**

D39942067 / PR pytorch#661 introduced the `shared_ptr` to fix a SIOF crash on aarch64. We
analyzed all static initialization dependencies and found:

1. `CuptiCallbackApi`'s constructor is `= default` with all trivially
   constructible members. It has zero static dependencies at construction time.

2. The actual SIOF was between `initCallbackApi()` and GLOG -- there is an
   explicit comment in the code: "TODO: Remove temporarily to work around
   static initialization order issue between this and GLOG." The problematic
   `LOG()` call was already commented out as a workaround.

3. The `shared_ptr` does not meaningfully fix SIOF: no caller stores the
   `shared_ptr` long-term, so the lifetime-extension property is never used.
   Both the `shared_ptr` static and a Meyers singleton have identical
   construction timing (first call) and destruction timing (static destruction).

4. A Meyers singleton is safe here because `CuptiCallbackApi()` has no
   dependencies on other statics. The SIOF risk is in `initCallbackApi()`
   (which calls CUPTI and potentially GLOG), not in singleton construction.

Differential Revision: D99843809
@scotts
scotts force-pushed the export-D99843809 branch from af8fe8c to e47f246 Compare April 8, 2026 14:28
@meta-codesync meta-codesync Bot closed this in f287ee7 Apr 8, 2026
@meta-codesync

meta-codesync Bot commented Apr 8, 2026

Copy link
Copy Markdown

This pull request has been merged in f287ee7.

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

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

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

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

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

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
Pull Request resolved: pytorch#179906
Approved by: https://github.com/ryanzhang22
ani300 pushed a commit to IBM/kineto-spyre that referenced this pull request Jul 28, 2026
…i (#1350)

Summary:
Pull Request resolved: pytorch/kineto#1350

`CuptiCallbackApi::singleton()` returns `std::shared_ptr<CuptiCallbackApi>` by
value. Every call copies the shared_ptr, incurring two atomic ref-count
operations (increment on copy, decrement on destruction). The
`callback_switchboard` function -- the CUPTI hot path fired on every
`cudaLaunchKernel` -- calls `singleton()` on each invocation. On ARM/aarch64,
atomic operations are more expensive than on x86 due to load-exclusive /
store-exclusive barriers, making this overhead measurable during profiling.

This diff changes `singleton()` to return `CuptiCallbackApi&` from a Meyers
singleton (`static CuptiCallbackApi instance`), eliminating the per-call atomic
overhead. This matches the pattern already used by `CuptiActivityApi::singleton()`.

**SIOF safety analysis:**

D39942067 / PR #661 introduced the `shared_ptr` to fix a SIOF crash on aarch64. We
analyzed all static initialization dependencies and found:

1. `CuptiCallbackApi`'s constructor is `= default` with all trivially
   constructible members. It has zero static dependencies at construction time.

2. The actual SIOF was between `initCallbackApi()` and GLOG -- there is an
   explicit comment in the code: "TODO: Remove temporarily to work around
   static initialization order issue between this and GLOG." The problematic
   `LOG()` call was already commented out as a workaround.

3. The `shared_ptr` does not meaningfully fix SIOF: no caller stores the
   `shared_ptr` long-term, so the lifetime-extension property is never used.
   Both the `shared_ptr` static and a Meyers singleton have identical
   construction timing (first call) and destruction timing (static destruction).

4. A Meyers singleton is safe here because `CuptiCallbackApi()` has no
   dependencies on other statics. The SIOF risk is in `initCallbackApi()`
   (which calls CUPTI and potentially GLOG), not in singleton construction.

Reviewed By: ryanzhang22

Differential Revision: D99843809

fbshipit-source-id: b5910f400b6c47f05d9709bade894a2e1071fc81
Signed-off-by: Antoni Viros i Martin <[email protected]>
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
Pull Request resolved: pytorch#179906
Approved by: https://github.com/ryanzhang22
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