Skip to content

Commit 8340e87

Browse files
authored
Use OSError (#38712)
Signed-off-by: cyy <[email protected]>
1 parent 8257734 commit 8340e87

20 files changed

+54
-56
lines changed

src/transformers/generation/configuration_utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,13 +1049,13 @@ def from_pretrained(
10491049
_commit_hash=commit_hash,
10501050
)
10511051
commit_hash = extract_commit_hash(resolved_config_file, commit_hash)
1052-
except EnvironmentError:
1052+
except OSError:
10531053
# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted to
10541054
# the original exception.
10551055
raise
10561056
except Exception:
10571057
# For any other exception, we throw a generic error.
1058-
raise EnvironmentError(
1058+
raise OSError(
10591059
f"Can't load the configuration of '{pretrained_model_name}'. If you were trying to load it"
10601060
" from 'https://huggingface.co/models', make sure you don't have a local directory with the same"
10611061
f" name. Otherwise, make sure '{pretrained_model_name}' is the correct path to a directory"
@@ -1067,9 +1067,7 @@ def from_pretrained(
10671067
config_dict = cls._dict_from_json_file(resolved_config_file)
10681068
config_dict["_commit_hash"] = commit_hash
10691069
except (json.JSONDecodeError, UnicodeDecodeError):
1070-
raise EnvironmentError(
1071-
f"It looks like the config file at '{resolved_config_file}' is not a valid JSON file."
1072-
)
1070+
raise OSError(f"It looks like the config file at '{resolved_config_file}' is not a valid JSON file.")
10731071

10741072
if is_local:
10751073
logger.info(f"loading configuration file {resolved_config_file}")

src/transformers/integrations/integration_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ def _log_model_checkpoint(self, source_directory: str, checkpoint: str):
16231623
copy_path = os.path.join(consistent_checkpoint_path, cpkt_path)
16241624
shutil.copytree(relative_path, copy_path)
16251625
target_path = consistent_checkpoint_path
1626-
except IOError as e:
1626+
except OSError as e:
16271627
logger.warning(
16281628
"NeptuneCallback was unable to made a copy of checkpoint due to I/O exception: '{}'. "
16291629
"Could fail trying to upload.".format(e)

src/transformers/integrations/tensor_parallel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def initialize_tensor_parallelism(tp_plan, tp_size=None):
4848
return None, None, None
4949

5050
if not is_torch_greater_or_equal("2.5"):
51-
raise EnvironmentError("Tensor parallel is only supported for `torch>=2.5`.")
51+
raise OSError("Tensor parallel is only supported for `torch>=2.5`.")
5252

5353
# Detect the accelerator on the machine. If no accelerator is available, it returns CPU.
5454
device_type = torch._C._get_accelerator().type
@@ -70,7 +70,7 @@ def initialize_tensor_parallelism(tp_plan, tp_size=None):
7070
current_device.set_device(local_rank)
7171

7272
except Exception as e:
73-
raise EnvironmentError(
73+
raise OSError(
7474
"We tried to initialize torch.distributed for you, but it failed. Make "
7575
"sure you init torch distributed in your script to use `tp_plan='auto'`."
7676
) from e

src/transformers/modeling_flax_pytorch_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def load_flax_checkpoint_in_pytorch_model(model, flax_checkpoint_path):
347347
try:
348348
flax_state_dict = from_bytes(flax_cls, state_f.read())
349349
except UnpicklingError:
350-
raise EnvironmentError(f"Unable to convert {flax_checkpoint_path} to Flax deserializable object. ")
350+
raise OSError(f"Unable to convert {flax_checkpoint_path} to Flax deserializable object. ")
351351

352352
return load_flax_weights_in_pytorch_model(model, flax_state_dict)
353353

src/transformers/modeling_flax_utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def load_flax_weights(cls, resolved_archive_file):
435435
else:
436436
raise ValueError from e
437437
except (UnicodeDecodeError, ValueError):
438-
raise EnvironmentError(f"Unable to convert {resolved_archive_file} to Flax deserializable object. ")
438+
raise OSError(f"Unable to convert {resolved_archive_file} to Flax deserializable object. ")
439439

440440
return state
441441

@@ -476,7 +476,7 @@ def load_flax_sharded_weights(cls, shard_files):
476476
else:
477477
raise ValueError from e
478478
except (UnicodeDecodeError, ValueError):
479-
raise EnvironmentError(f"Unable to convert {shard_file} to Flax deserializable object. ")
479+
raise OSError(f"Unable to convert {shard_file} to Flax deserializable object. ")
480480

481481
state = flatten_dict(state, sep="/")
482482
state_sharded_dict.update(state)
@@ -738,13 +738,13 @@ def from_pretrained(
738738
is_sharded = True
739739
raise NotImplementedError("Support for sharded checkpoints using safetensors is coming soon!")
740740
elif os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, WEIGHTS_NAME)):
741-
raise EnvironmentError(
741+
raise OSError(
742742
f"Error no file named {FLAX_WEIGHTS_NAME} found in directory {pretrained_model_name_or_path} "
743743
"but there is a file for PyTorch weights. Use `from_pt=True` to load this model from those "
744744
"weights."
745745
)
746746
else:
747-
raise EnvironmentError(
747+
raise OSError(
748748
f"Error no file named {FLAX_WEIGHTS_NAME} or {WEIGHTS_NAME} found in directory "
749749
f"{pretrained_model_name_or_path}."
750750
)
@@ -820,29 +820,29 @@ def from_pretrained(
820820
"Support for sharded checkpoints using safetensors is coming soon!"
821821
)
822822
elif has_file(pretrained_model_name_or_path, WEIGHTS_NAME, **has_file_kwargs):
823-
raise EnvironmentError(
823+
raise OSError(
824824
f"{pretrained_model_name_or_path} does not appear to have a file named"
825825
f" {FLAX_WEIGHTS_NAME} but there is a file for PyTorch weights. Use `from_pt=True` to"
826826
" load this model from those weights."
827827
)
828828
elif has_file(pretrained_model_name_or_path, WEIGHTS_INDEX_NAME, **has_file_kwargs):
829-
raise EnvironmentError(
829+
raise OSError(
830830
f"{pretrained_model_name_or_path} does not appear to have a file named"
831831
f" {FLAX_WEIGHTS_INDEX_NAME} but there is a sharded file for PyTorch weights. Use"
832832
" `from_pt=True` to load this model from those weights."
833833
)
834834
else:
835-
raise EnvironmentError(
835+
raise OSError(
836836
f"{pretrained_model_name_or_path} does not appear to have a file named"
837837
f" {FLAX_WEIGHTS_NAME} or {WEIGHTS_NAME}."
838838
)
839-
except EnvironmentError:
839+
except OSError:
840840
# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted
841841
# to the original exception.
842842
raise
843843
except Exception:
844844
# For any other exception, we throw a generic error.
845-
raise EnvironmentError(
845+
raise OSError(
846846
f"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it"
847847
" from 'https://huggingface.co/models', make sure you don't have a local directory with the"
848848
f" same name. Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a"

src/transformers/modeling_tf_utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,21 +2760,21 @@ def from_pretrained(
27602760

27612761
# At this stage we don't have a weight file so we will raise an error.
27622762
elif use_safetensors:
2763-
raise EnvironmentError(
2763+
raise OSError(
27642764
f"Error no file named {SAFE_WEIGHTS_NAME} or {SAFE_WEIGHTS_INDEX_NAME} found in directory {pretrained_model_name_or_path}. "
27652765
f"Please make sure that the model has been saved with `safe_serialization=True` or do not "
27662766
f"set `use_safetensors=True`."
27672767
)
27682768
elif os.path.isfile(os.path.join(pretrained_model_name_or_path, WEIGHTS_NAME)) or os.path.isfile(
27692769
os.path.join(pretrained_model_name_or_path, WEIGHTS_INDEX_NAME)
27702770
):
2771-
raise EnvironmentError(
2771+
raise OSError(
27722772
f"Error no file named {TF2_WEIGHTS_NAME} or {SAFE_WEIGHTS_NAME} found in directory {pretrained_model_name_or_path} "
27732773
"but there is a file for PyTorch weights. Use `from_pt=True` to load this model from those "
27742774
"weights."
27752775
)
27762776
else:
2777-
raise EnvironmentError(
2777+
raise OSError(
27782778
f"Error no file named {TF2_WEIGHTS_NAME}, {SAFE_WEIGHTS_NAME} or {WEIGHTS_NAME} found in directory "
27792779
f"{pretrained_model_name_or_path}."
27802780
)
@@ -2850,25 +2850,25 @@ def from_pretrained(
28502850
if has_file(pretrained_model_name_or_path, SAFE_WEIGHTS_INDEX_NAME, **has_file_kwargs):
28512851
is_sharded = True
28522852
elif has_file(pretrained_model_name_or_path, WEIGHTS_NAME, **has_file_kwargs):
2853-
raise EnvironmentError(
2853+
raise OSError(
28542854
f"{pretrained_model_name_or_path} does not appear to have a file named"
28552855
f" {TF2_WEIGHTS_NAME} but there is a file for PyTorch weights. Use `from_pt=True` to"
28562856
" load this model from those weights."
28572857
)
28582858
else:
2859-
raise EnvironmentError(
2859+
raise OSError(
28602860
f"{pretrained_model_name_or_path} does not appear to have a file named {WEIGHTS_NAME},"
28612861
f" {TF2_WEIGHTS_NAME} or {TF_WEIGHTS_NAME}"
28622862
)
28632863

2864-
except EnvironmentError:
2864+
except OSError:
28652865
# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted
28662866
# to the original exception.
28672867
raise
28682868
except Exception:
28692869
# For any other exception, we throw a generic error.
28702870

2871-
raise EnvironmentError(
2871+
raise OSError(
28722872
f"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it"
28732873
" from 'https://huggingface.co/models', make sure you don't have a local directory with the"
28742874
f" same name. Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a"

src/transformers/modeling_utils.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,26 +1069,26 @@ def _get_resolved_checkpoint_files(
10691069
os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, TF_WEIGHTS_NAME + ".index"))
10701070
or os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, TF2_WEIGHTS_NAME))
10711071
):
1072-
raise EnvironmentError(
1072+
raise OSError(
10731073
f"Error no file named {_add_variant(WEIGHTS_NAME, variant)} found in directory"
10741074
f" {pretrained_model_name_or_path} but there is a file for TensorFlow weights. Use"
10751075
" `from_tf=True` to load this model from those weights."
10761076
)
10771077
elif not use_safetensors and os.path.isfile(
10781078
os.path.join(pretrained_model_name_or_path, subfolder, FLAX_WEIGHTS_NAME)
10791079
):
1080-
raise EnvironmentError(
1080+
raise OSError(
10811081
f"Error no file named {_add_variant(WEIGHTS_NAME, variant)} found in directory"
10821082
f" {pretrained_model_name_or_path} but there is a file for Flax weights. Use `from_flax=True`"
10831083
" to load this model from those weights."
10841084
)
10851085
elif use_safetensors:
1086-
raise EnvironmentError(
1086+
raise OSError(
10871087
f"Error no file named {_add_variant(SAFE_WEIGHTS_NAME, variant)} found in directory"
10881088
f" {pretrained_model_name_or_path}."
10891089
)
10901090
else:
1091-
raise EnvironmentError(
1091+
raise OSError(
10921092
f"Error no file named {_add_variant(WEIGHTS_NAME, variant)}, {_add_variant(SAFE_WEIGHTS_NAME, variant)},"
10931093
f" {TF2_WEIGHTS_NAME}, {TF_WEIGHTS_NAME + '.index'} or {FLAX_WEIGHTS_NAME} found in directory"
10941094
f" {pretrained_model_name_or_path}."
@@ -1156,7 +1156,7 @@ def _get_resolved_checkpoint_files(
11561156
)
11571157
cached_file_kwargs["revision"] = revision
11581158
if resolved_archive_file is None:
1159-
raise EnvironmentError(
1159+
raise OSError(
11601160
f"{pretrained_model_name_or_path} does not appear to have a file named"
11611161
f" {_add_variant(SAFE_WEIGHTS_NAME, variant)} or {_add_variant(SAFE_WEIGHTS_INDEX_NAME, variant)} "
11621162
"and thus cannot be loaded with `safetensors`. Please make sure that the model has "
@@ -1222,39 +1222,39 @@ def _get_resolved_checkpoint_files(
12221222
"local_files_only": local_files_only,
12231223
}
12241224
if has_file(pretrained_model_name_or_path, TF2_WEIGHTS_NAME, **has_file_kwargs):
1225-
raise EnvironmentError(
1225+
raise OSError(
12261226
f"{pretrained_model_name_or_path} does not appear to have a file named"
12271227
f" {_add_variant(WEIGHTS_NAME, variant)} but there is a file for TensorFlow weights."
12281228
" Use `from_tf=True` to load this model from those weights."
12291229
)
12301230
elif has_file(pretrained_model_name_or_path, FLAX_WEIGHTS_NAME, **has_file_kwargs):
1231-
raise EnvironmentError(
1231+
raise OSError(
12321232
f"{pretrained_model_name_or_path} does not appear to have a file named"
12331233
f" {_add_variant(WEIGHTS_NAME, variant)} but there is a file for Flax weights. Use"
12341234
" `from_flax=True` to load this model from those weights."
12351235
)
12361236
elif variant is not None and has_file(
12371237
pretrained_model_name_or_path, WEIGHTS_NAME, **has_file_kwargs
12381238
):
1239-
raise EnvironmentError(
1239+
raise OSError(
12401240
f"{pretrained_model_name_or_path} does not appear to have a file named"
12411241
f" {_add_variant(WEIGHTS_NAME, variant)} but there is a file without the variant"
12421242
f" {variant}. Use `variant=None` to load this model from those weights."
12431243
)
12441244
else:
1245-
raise EnvironmentError(
1245+
raise OSError(
12461246
f"{pretrained_model_name_or_path} does not appear to have a file named"
12471247
f" {_add_variant(WEIGHTS_NAME, variant)}, {_add_variant(SAFE_WEIGHTS_NAME, variant)},"
12481248
f" {TF2_WEIGHTS_NAME}, {TF_WEIGHTS_NAME} or {FLAX_WEIGHTS_NAME}."
12491249
)
12501250

1251-
except EnvironmentError:
1251+
except OSError:
12521252
# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted
12531253
# to the original exception.
12541254
raise
12551255
except Exception as e:
12561256
# For any other exception, we throw a generic error.
1257-
raise EnvironmentError(
1257+
raise OSError(
12581258
f"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it"
12591259
" from 'https://huggingface.co/models', make sure you don't have a local directory with the"
12601260
f" same name. Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a"

src/transformers/models/auto/auto_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ class _BaseAutoModelClass:
415415
_model_mapping = None
416416

417417
def __init__(self, *args, **kwargs) -> None:
418-
raise EnvironmentError(
418+
raise OSError(
419419
f"{self.__class__.__name__} is designed to be instantiated "
420420
f"using the `{self.__class__.__name__}.from_pretrained(pretrained_model_name_or_path)` or "
421421
f"`{self.__class__.__name__}.from_config(config)` methods."

src/transformers/models/auto/configuration_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ class AutoConfig:
10471047
"""
10481048

10491049
def __init__(self) -> None:
1050-
raise EnvironmentError(
1050+
raise OSError(
10511051
"AutoConfig is designed to be instantiated "
10521052
"using the `AutoConfig.from_pretrained(pretrained_model_name_or_path)` method."
10531053
)

src/transformers/models/auto/feature_extraction_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class AutoFeatureExtractor:
255255
"""
256256

257257
def __init__(self):
258-
raise EnvironmentError(
258+
raise OSError(
259259
"AutoFeatureExtractor is designed to be instantiated "
260260
"using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method."
261261
)

src/transformers/models/auto/image_processing_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class AutoImageProcessor:
338338
"""
339339

340340
def __init__(self):
341-
raise EnvironmentError(
341+
raise OSError(
342342
"AutoImageProcessor is designed to be instantiated "
343343
"using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method."
344344
)

src/transformers/models/auto/processing_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class AutoProcessor:
176176
"""
177177

178178
def __init__(self):
179-
raise EnvironmentError(
179+
raise OSError(
180180
"AutoProcessor is designed to be instantiated "
181181
"using the `AutoProcessor.from_pretrained(pretrained_model_name_or_path)` method."
182182
)

src/transformers/models/auto/tokenization_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ class AutoTokenizer:
819819
"""
820820

821821
def __init__(self):
822-
raise EnvironmentError(
822+
raise OSError(
823823
"AutoTokenizer is designed to be instantiated "
824824
"using the `AutoTokenizer.from_pretrained(pretrained_model_name_or_path)` method."
825825
)

src/transformers/models/auto/video_processing_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class AutoVideoProcessor:
205205
"""
206206

207207
def __init__(self):
208-
raise EnvironmentError(
208+
raise OSError(
209209
"AutoVideoProcessor is designed to be instantiated "
210210
"using the `AutoVideoProcessor.from_pretrained(pretrained_model_name_or_path)` method."
211211
)

src/transformers/models/deprecated/transfo_xl/tokenization_transfo_xl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ def from_pretrained(cls, pretrained_model_name_or_path, cache_dir=None, *inputs,
693693
# redirect to the cache, if necessary
694694
try:
695695
resolved_corpus_file = cached_file(pretrained_model_name_or_path, CORPUS_NAME, cache_dir=cache_dir)
696-
except EnvironmentError:
696+
except OSError:
697697
logger.error(
698698
f"Corpus '{pretrained_model_name_or_path}' was not found in corpus list"
699699
f" ({', '.join(PRETRAINED_CORPUS_ARCHIVE_MAP.keys())}. We assumed '{pretrained_model_name_or_path}'"

0 commit comments

Comments
 (0)