Skip to content

Commit 59035fd

Browse files
authored
Avoid assumption that model has config attribute in deepspeed (#41207)
Avoid assumption that model has config in deepspeed
1 parent d973977 commit 59035fd

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/transformers/integrations/deepspeed.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,20 @@ def trainer_config_finalize(self, args, model, num_training_steps):
252252
hidden_size_auto_keys = [x for x in hidden_size_based_keys if self.is_auto(x)]
253253

254254
if len(hidden_size_auto_keys) > 0:
255-
if hasattr(model.config, "hidden_size"):
256-
hidden_size = model.config.hidden_size
257-
elif hasattr(model.config, "hidden_sizes"):
258-
# if there are many hidden sizes pick the largest one
259-
hidden_size = max(model.config.hidden_sizes)
260-
elif hasattr(model.config, "text_config") and hasattr(model.config.text_config, "hidden_size"):
261-
hidden_size = model.config.text_config.hidden_size
262-
elif hasattr(model.config, "text_config") and hasattr(model.config.text_config, "hidden_sizes"):
263-
# if there are many hidden sizes pick the largest one
264-
hidden_size = max(model.config.text_config.hidden_sizes)
265-
else:
255+
hidden_size = None
256+
if hasattr(model, "config"):
257+
if hasattr(model.config, "hidden_size"):
258+
hidden_size = model.config.hidden_size
259+
elif hasattr(model.config, "hidden_sizes"):
260+
# if there are many hidden sizes pick the largest one
261+
hidden_size = max(model.config.hidden_sizes)
262+
elif hasattr(model.config, "text_config") and hasattr(model.config.text_config, "hidden_size"):
263+
hidden_size = model.config.text_config.hidden_size
264+
elif hasattr(model.config, "text_config") and hasattr(model.config.text_config, "hidden_sizes"):
265+
# if there are many hidden sizes pick the largest one
266+
hidden_size = max(model.config.text_config.hidden_sizes)
267+
268+
if hidden_size is None:
266269
raise ValueError(
267270
"The model's config file has neither `hidden_size` nor `hidden_sizes` entry, "
268271
"therefore it's not possible to automatically fill out the following `auto` entries "

0 commit comments

Comments
 (0)