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

Better dispatch for submodules #392

Merged
merged 1 commit into from
May 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/accelerate/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,24 @@ def detach_hook(self, module):
set_module_tensor_to_device(module, name, device, value=self.weights_map.get(name, None))


def attach_execution_device_hook(module: torch.nn.Module, execution_device: Union[int, str, torch.device]):
"""
Recursively attaches `AlignDevicesHook` to all submodules of a given model to make sure they have the right
execution device

Args:
module (`torch.nn.Module`):
The module where we want to attach the hooks.
execution_device (`int`, `str` or `torch.device`):
The device on which inputs and model weights should be placed before the forward pass.
"""
if not hasattr(module, "_hf_hook") and len(module.state_dict()) > 0:
add_hook_to_module(module, AlignDevicesHook(execution_device))

for child in module.children():
attach_execution_device_hook(child, execution_device)


def attach_align_device_hook(
module: torch.nn.Module,
execution_device: Optional[torch.device] = None,
Expand Down Expand Up @@ -383,6 +401,7 @@ def attach_align_device_hook_on_blocks(
place_submodules=True,
)
add_hook_to_module(module, hook)
attach_execution_device_hook(module, execution_device[module_name])
elif module_name in execution_device:
attach_align_device_hook(
module,
Expand All @@ -395,6 +414,7 @@ def attach_align_device_hook_on_blocks(
if not hasattr(module, "_hf_hook"):
hook = AlignDevicesHook(execution_device=execution_device[module_name], io_same_device=(module_name == ""))
add_hook_to_module(module, hook)
attach_execution_device_hook(module, execution_device[module_name])
elif module_name == "":
hook = AlignDevicesHook(io_same_device=True)
add_hook_to_module(module, hook)
Expand Down