Skip to content

Split work wait hooks into pre and post#2354

Open
pavanbalaji wants to merge 4 commits intometa-pytorch:mainfrom
pavanbalaji:export-D103448992
Open

Split work wait hooks into pre and post#2354
pavanbalaji wants to merge 4 commits intometa-pytorch:mainfrom
pavanbalaji:export-D103448992

Conversation

@pavanbalaji
Copy link
Copy Markdown
Contributor

Summary:
Rename registerWorkWaitHook to registerWorkWaitPreHook and
add registerWorkWaitPostHook. The pre hook fires before the
actual wait/sync (existing behavior), while the post hook
fires after the stream dependency is established.

This enables hooks like chash to launch GPU kernels after
the collective output is ready, rather than before the sync
where the output buffer may contain stale data.

Updated all backend wait() implementations to call
runWaitPreHooks() at the start and runWaitPostHooks() at the
end (and at each early return).

Differential Revision: D103448992

Summary:
Previously, hook implementations (ClogHook, FlightRecorderHook,
NanCheckHook) had an explicit unregister() method that had to be
called before comm destruction but after all work completed. This
created a race between hook finalize and comm finalize: if
unregister was called too early, in-flight lifecycle callbacks
could be missed; if called too late, the hook object could be
destroyed while callbacks still referenced it.

This diff adds finalize as a new operation in the hook system
(OpName::finalize, FinalizePreHookArgs, FinalizePostHookArgs)
so that hooks receive a callback when a communicator is being
finalized. With this signal, hooks can self-clean in response
to the finalize event, eliminating the need for explicit
unregister() calls entirely.

Hook lambdas now capture weak_from_this() instead of raw this,
so the hook manages its own lifetime via the weak reference.
The RemovableHandle returned by registration is no longer
relevant after registration and can be freely discarded by the
caller.

Changes:
- Add finalize to OpName enum, opToString, PreHookArgs/PostHookArgs
  variants in TorchCommHooks.hpp
- Fire pre/post hooks in TorchComm::finalize()
- Remove unregister() from ClogHook, FlightRecorderHook, and
  NanCheckHook (C++ and Python bindings)
- Simplify CommRegistration structs (no longer store handles)
- Update all tests to remove unregister() calls
- Add finalize to Python OpName enum and type stubs
- FlightRecorderHook skips finalize in onPreHook and onPostHook
  to avoid calling recorder after teardown begins
- Hook lambdas capture weak_from_this() instead of raw this
  to prevent use-after-free if hook is destroyed before comm
- FlightRecorderHook inherits enable_shared_from_this

Differential Revision: D103097395
Summary:
When lifecycle logging is enabled, ClogHook tracks expected events
(S, E, W) per work_id in work_events_map_. If a work object is
dropped without triggering all callbacks, entries leak permanently.

This change reports those leaks in the clog log file when all
registered communicators have finalized. Each comm identifies its
own leaked work_ids during its finalize post-hook (using the
comm_name stored in WorkInfo), and accumulates them into a shared
vector. When the last comm finalizes (active_comm_count_ reaches
zero), all leaked lines are written to the log.

Changes:
- Add forEach() to ThreadSafeMap for iteration
- Add comm_name to WorkInfo for per-comm leak attribution
- Pass comm_name to onPostHook via lambda capture
- Track active_comm_count_ (incremented on register, decremented
  on finalize)
- Accumulate per-comm leak lines, flush on last finalize

Differential Revision: D103099065
Summary:
Move ThreadSafeLogFile from ClogHook.hpp into a shared header
at hooks/common/ThreadSafeLogFile.hpp so it can be reused by
other hook implementations. ClogHook now includes the shared
header instead of defining the class inline.

Differential Revision: D103448993
Summary:
Rename registerWorkWaitHook to registerWorkWaitPreHook and
add registerWorkWaitPostHook. The pre hook fires before the
actual wait/sync (existing behavior), while the post hook
fires after the stream dependency is established.

This enables hooks like chash to launch GPU kernels after
the collective output is ready, rather than before the sync
where the output buffer may contain stale data.

Updated all backend wait() implementations to call
runWaitPreHooks() at the start and runWaitPostHooks() at the
end (and at each early return).

Differential Revision: D103448992
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 4, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 4, 2026

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant