Skip to content

Commit caa0e6c

Browse files
Fix NameError in accelerate.py and clarify error for PyTorch < 2.6 (#50089)
Co-authored-by: Meghana Zagabathuni <mz4156@nyu.edu>
1 parent bb666bd commit caa0e6c

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎src/transformers/integrations/accelerate.py‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
and simplicity/ease of use.
1717
"""
1818

19+
from __future__ import annotations
20+
1921
import copy
2022
import functools
2123
import inspect
@@ -93,7 +95,7 @@ def get_module_size_with_ties(
9395
return module_size_with_ties, tied_module_names, tied_modules
9496

9597

96-
def check_and_set_device_map(device_map: "torch.device | int | str | dict | None") -> dict | str | None:
98+
def check_and_set_device_map(device_map: torch.device | int | str | dict | None) -> dict | str | None:
9799
from ..modeling_utils import get_torch_context_manager_or_global_device
98100

99101
# Potentially detect context manager or global device, and use it (only if no device_map was provided)
@@ -142,8 +144,8 @@ def check_and_set_device_map(device_map: "torch.device | int | str | dict | None
142144

143145

144146
def compute_module_sizes(
145-
model: "PreTrainedModel",
146-
hf_quantizer: "HfQuantizer | None" = None,
147+
model: PreTrainedModel,
148+
hf_quantizer: HfQuantizer | None = None,
147149
buffers_only: bool = False,
148150
only_modules: bool = True,
149151
) -> tuple[dict[str, int], dict[str, int]]:
@@ -191,7 +193,7 @@ def all_tensors():
191193
return all_module_sizes, leaves_module_sizes
192194

193195

194-
def compute_module_total_buffer_size(model: nn.Module, hf_quantizer: "HfQuantizer | None" = None):
196+
def compute_module_total_buffer_size(model: nn.Module, hf_quantizer: HfQuantizer | None = None):
195197
"""
196198
Compute the total size of buffers in each submodule of a given model.
197199
"""
@@ -238,10 +240,10 @@ def get_max_memory(max_memory: dict[int | str, int | str] | None = None):
238240

239241

240242
def get_balanced_memory(
241-
model: "PreTrainedModel",
243+
model: PreTrainedModel,
242244
max_memory: dict[int | str, int | str] | None = None,
243245
no_split_module_classes: set[str] | None = None,
244-
hf_quantizer: "HfQuantizer | None" = None,
246+
hf_quantizer: HfQuantizer | None = None,
245247
low_zero: bool = False,
246248
):
247249
"""
@@ -336,10 +338,10 @@ def get_balanced_memory(
336338

337339

338340
def _get_device_map(
339-
model: "PreTrainedModel",
341+
model: PreTrainedModel,
340342
device_map: dict | str | None,
341343
max_memory: dict | None,
342-
hf_quantizer: "HfQuantizer | None",
344+
hf_quantizer: HfQuantizer | None,
343345
) -> dict:
344346
"""Compute the final `device_map` to use if we passed a value in ['auto', 'balanced', 'balanced_low_0', 'sequential'].
345347
Otherwise, we check for any device inconsistencies in the device_map.
@@ -457,7 +459,7 @@ def get_device(device_map: dict | None, param_name: str, valid_torch_device: boo
457459

458460

459461
def accelerate_disk_offload(
460-
model: "PreTrainedModel",
462+
model: PreTrainedModel,
461463
disk_offload_folder: str | None,
462464
checkpoint_files: list[str] | None,
463465
device_map: dict,
@@ -543,7 +545,7 @@ def offload_weight(weight: torch.Tensor, weight_name: str, offload_folder: str |
543545
return offload_index
544546

545547

546-
def load_offloaded_parameter(model: "PreTrainedModel", param_name: str) -> torch.Tensor:
548+
def load_offloaded_parameter(model: PreTrainedModel, param_name: str) -> torch.Tensor:
547549
"""Load `param_name` from disk, if it was offloaded due to the device_map, and thus lives as a meta parameter
548550
inside `model`.
549551
This is needed when resaving a model, when some parameters were offloaded (we need to load them from disk, to
@@ -574,7 +576,7 @@ def _init_infer_auto_device_map(
574576
max_memory: dict[int | str, int | str] | None = None,
575577
no_split_module_classes: set[str] | None = None,
576578
tied_parameters: list[list[str]] | None = None,
577-
hf_quantizer: "HfQuantizer | None" = None,
579+
hf_quantizer: HfQuantizer | None = None,
578580
) -> tuple[
579581
list[int | str],
580582
dict[int | str, int | str],
@@ -647,7 +649,7 @@ def infer_auto_device_map(
647649
clean_result: bool = True,
648650
offload_buffers: bool = False,
649651
tied_parameters: list[list[str]] | None = None,
650-
hf_quantizer: "HfQuantizer | None" = None,
652+
hf_quantizer: HfQuantizer | None = None,
651653
no_placement_params: set[str] | None = None,
652654
):
653655
"""

‎src/transformers/utils/import_utils.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,8 +2090,9 @@ def is_training_run_on_sagemaker() -> bool:
20902090

20912091
# docstyle-ignore
20922092
PYTORCH_IMPORT_ERROR = """
2093-
{0} requires the PyTorch library but it was not found in your environment. Check out the instructions on the
2094-
installation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.
2093+
{0} requires PyTorch >= 2.6, but PyTorch was not found or an older version is installed.
2094+
Please install or upgrade PyTorch by following the instructions on the installation page:
2095+
https://pytorch.org/get-started/locally/
20952096
Please note that you may need to restart your runtime after installation.
20962097
"""
20972098

0 commit comments

Comments
 (0)