Skip to content

Commit

Permalink
Fix balanced and auto device_map (#22271)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgugger committed Mar 20, 2023
1 parent 2355e46 commit 1e39734
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2563,7 +2563,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
elif device_map in ["balanced", "balanced_low_0"] and get_balanced_memory is None:
raise ValueError(f"`device_map={device_map}` requires a source install of Accelerate.")

kwargs = {"no_split_module_classes": no_split_modules, "max_memory": max_memory}
kwargs = {"no_split_module_classes": no_split_modules}
if "special_dtypes" in inspect.signature(infer_auto_device_map).parameters:
kwargs["special_dtypes"] = special_dtypes
elif len(special_dtypes) > 0:
Expand All @@ -2578,6 +2578,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
low_zero=(device_map == "balanced_low_0"),
**kwargs,
)
kwargs["max_memory"] = max_memory
# Make sure tied weights are tied before creating the device map.
model.tie_weights()
device_map = infer_auto_device_map(model, dtype=torch_dtype if not load_in_8bit else torch.int8, **kwargs)
Expand Down

0 comments on commit 1e39734

Please sign in to comment.