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

[Accelerator] We should not call to on modules that wraps accelerate loaded models #1172

Merged
merged 2 commits into from
Mar 15, 2023
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
10 changes: 9 additions & 1 deletion src/accelerate/accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,14 @@ def prepare_model(self, model: torch.nn.Module, device_placement=None):
device_placement = self.device_placement and self.distributed_type != DistributedType.FSDP
self._models.append(model)
# We check only for models loaded with `accelerate`

# Checks if any of the child module has the attribute `hf_device_map`.
has_hf_device_map = False
for m in model.modules():
if hasattr(m, "hf_device_map"):
has_hf_device_map = True
break

if getattr(model, "is_loaded_in_8bit", False) and getattr(model, "hf_device_map", False):
model_devices = set(model.hf_device_map.values())
if len(model_devices) > 1:
Expand All @@ -1157,7 +1165,7 @@ def prepare_model(self, model: torch.nn.Module, device_placement=None):
raise ValueError(
"You can't train a model that has been loaded in 8-bit precision with CPU or disk offload."
)
elif device_placement:
elif device_placement and not has_hf_device_map:
model = model.to(self.device)

if self.distributed_type == DistributedType.MULTI_GPU:
Expand Down