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

use folly::Singleton to avoid SIOF - #661

Closed
pallab-zz wants to merge 1 commit into
pytorch:mainfrom
pallab-zz:export-D39942067
Closed

pallab-zz wants to merge 1 commit into
pytorch:mainfrom
pallab-zz:export-D39942067

Conversation

@pallab-zz

Copy link
Copy Markdown

Differential Revision: D39942067

@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D39942067

pallab-zz pushed a commit to pallab-zz/kineto that referenced this pull request Sep 30, 2022
Summary: Pull Request resolved: pytorch#661

Differential Revision: D39942067

fbshipit-source-id: 2a13f295beff4aab24c2d873b65fa04999c920ae
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D39942067

pallab-zz pushed a commit to pallab-zz/kineto that referenced this pull request Oct 1, 2022
Summary: Pull Request resolved: pytorch#661

Differential Revision: D39942067

fbshipit-source-id: 001f1986e786f93304e7dbf81015eaff9ff33607
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D39942067

Summary: Pull Request resolved: pytorch#661

Differential Revision: D39942067

fbshipit-source-id: 1cc722c710452bf8037a85c25024a6b91cb3ab7d
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D39942067

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 added a commit to scotts/kineto that referenced this pull request Apr 8, 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
meta-codesync Bot pushed a commit that referenced this pull request Apr 8, 2026
#1350)

Summary:
Pull Request resolved: #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
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.

2 participants