@@ -1219,13 +1219,13 @@ def _get_dtype(
12191219 dtype = getattr (torch , dtype )
12201220 config .dtype = dtype
12211221 for sub_config_key in config .sub_configs :
1222- sub_config = getattr (config , sub_config_key )
1223- sub_config .dtype = dtype
1222+ if ( sub_config : = getattr (config , sub_config_key )) is not None :
1223+ sub_config .dtype = dtype
12241224 elif isinstance (dtype , torch .dtype ):
12251225 config .dtype = dtype
12261226 for sub_config_key in config .sub_configs :
1227- sub_config = getattr (config , sub_config_key )
1228- sub_config .dtype = dtype
1227+ if ( sub_config : = getattr (config , sub_config_key )) is not None :
1228+ sub_config .dtype = dtype
12291229 elif isinstance (dtype , dict ):
12301230 for key , curr_dtype in dtype .items ():
12311231 if hasattr (config , key ):
@@ -1250,8 +1250,8 @@ def _get_dtype(
12501250 default_dtype = torch .get_default_dtype ()
12511251 config .dtype = default_dtype
12521252 for key in config .sub_configs :
1253- value = getattr (config , key )
1254- value .dtype = default_dtype
1253+ if ( sub_config : = getattr (config , key )) is not None :
1254+ sub_config .dtype = default_dtype
12551255
12561256 return config , dtype , dtype_orig
12571257
@@ -2700,34 +2700,34 @@ def set_attn_implementation(self, attn_implementation: Union[str, dict]):
27002700
27012701 # We need this as some old and badly designed models use subconfigs without declaring the corresponding modules as PreTrainedModel
27022702 for subconfig_key in self .config .sub_configs :
2703- subconfig = getattr (self .config , subconfig_key )
2704- sub_implementation = (
2705- requested_implementation
2706- if not isinstance (attn_implementation , dict )
2707- else attn_implementation .get (subconfig_key , subconfig ._attn_implementation )
2708- )
2709- # This means we did not perform any check above for this particular subconfig -> set it in the dark if it is registered
2710- if (
2711- not hasattr (subconfig , "_attn_was_changed" )
2712- # If it's already the same, then no need to enter here and raise warnings
2713- and sub_implementation != subconfig ._attn_implementation
2714- ):
2715- if sub_implementation not in ["eager" ] + ALL_ATTENTION_FUNCTIONS .valid_keys ():
2716- raise ValueError (
2717- f'Specified `attn_implementation="{ sub_implementation } "` is not supported for { subconfig_key } . '
2718- 'The only possible arguments are "eager" (manual attention implementation)'
2719- f"or one of the following: { list (ALL_ATTENTION_FUNCTIONS .valid_keys ())} "
2720- )
2721- subconfig ._attn_implementation_internal = sub_implementation
2722- logger .warning (
2723- f"We set the attention implementation for the sub-config `{ subconfig_key } ` to `{ sub_implementation } ` "
2724- "without finding the associated sub-model. For this reason we could not check if the model supports it. "
2725- "You may encounter undefined behavior."
2703+ if (subconfig := getattr (self .config , subconfig_key )) is not None :
2704+ sub_implementation = (
2705+ requested_implementation
2706+ if not isinstance (attn_implementation , dict )
2707+ else attn_implementation .get (subconfig_key , subconfig ._attn_implementation )
27262708 )
2727- # Unset the attribute in this case, to avoid issues in the future
2728- else :
2729- if hasattr (subconfig , "_attn_was_changed" ):
2730- del subconfig ._attn_was_changed
2709+ # This means we did not perform any check above for this particular subconfig -> set it in the dark if it is registered
2710+ if (
2711+ not hasattr (subconfig , "_attn_was_changed" )
2712+ # If it's already the same, then no need to enter here and raise warnings
2713+ and sub_implementation != subconfig ._attn_implementation
2714+ ):
2715+ if sub_implementation not in ["eager" ] + ALL_ATTENTION_FUNCTIONS .valid_keys ():
2716+ raise ValueError (
2717+ f'Specified `attn_implementation="{ sub_implementation } "` is not supported for { subconfig_key } . '
2718+ 'The only possible arguments are "eager" (manual attention implementation)'
2719+ f"or one of the following: { list (ALL_ATTENTION_FUNCTIONS .valid_keys ())} "
2720+ )
2721+ subconfig ._attn_implementation_internal = sub_implementation
2722+ logger .warning (
2723+ f"We set the attention implementation for the sub-config `{ subconfig_key } ` to `{ sub_implementation } ` "
2724+ "without finding the associated sub-model. For this reason we could not check if the model supports it. "
2725+ "You may encounter undefined behavior."
2726+ )
2727+ # Unset the attribute in this case, to avoid issues in the future
2728+ else :
2729+ if hasattr (subconfig , "_attn_was_changed" ):
2730+ del subconfig ._attn_was_changed
27312731
27322732 def enable_input_require_grads (self ):
27332733 """
0 commit comments