Split work wait hooks into pre and post#2354
Open
pavanbalaji wants to merge 4 commits intometa-pytorch:mainfrom
Open
Split work wait hooks into pre and post#2354pavanbalaji wants to merge 4 commits intometa-pytorch:mainfrom
pavanbalaji wants to merge 4 commits intometa-pytorch:mainfrom
Conversation
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
Contributor
|
@pavanbalaji has exported this pull request. If you are a Meta employee, you can view the originating Diff in D103448992. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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