Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FSDP] Add record_function for explicit prefetching #105985

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions torch/distributed/fsdp/_runtime_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ def _pre_forward_unshard(
_unshard(state, handle, state._unshard_stream, state._pre_unshard_stream)
state._needs_pre_forward_unshard[handle] = False
state._device_handle.current_stream().wait_stream(state._unshard_stream)
_prefetch_handle(state, handle, _PrefetchMode.FORWARD)
with torch.profiler.record_function(
"FullyShardedDataParallel._pre_forward_prefetch"
):
_prefetch_handle(state, handle, _PrefetchMode.FORWARD)


@no_type_check
Expand Down Expand Up @@ -703,7 +706,10 @@ def _pre_backward_hook(
# Set this to `False` to ensure that a mistargeted prefetch does not
# actually unshard these handles
state._needs_pre_backward_unshard[handle] = False
_prefetch_handle(state, handle, _PrefetchMode.BACKWARD)
with torch.profiler.record_function(
"FullyShardedDataParallel._pre_backward_prefetch"
):
_prefetch_handle(state, handle, _PrefetchMode.BACKWARD)
handle.prepare_gradient_for_backward()
state._ran_pre_backward_hook[handle] = True

Expand Down Expand Up @@ -911,7 +917,10 @@ def _post_backward_reshard(
# TODO: Post-backward prefetching does not support the multiple handles
# per module case since the post-backward hook runs per handle, not per
# group of handles.
_prefetch_handle(state, handle, _PrefetchMode.BACKWARD)
with torch.profiler.record_function(
"FullyShardedDataParallel._post_backward_prefetch"
):
_prefetch_handle(state, handle, _PrefetchMode.BACKWARD)


@no_type_check
Expand Down