@@ -1028,7 +1028,7 @@ def prepare_remote_model_init_compat(model_id_or_path: Optional[str], config: An
10281028 input_mode_enum = getattr (remote_module , "InputMode" , None ) if remote_module is not None else None
10291029
10301030 with _MONKEY_PATCH_LOCK :
1031- if config . model_type == "minicpm" or config . model_type == "instella" :
1031+ if outer_model_cls is not None :
10321032 try_patch_legacy_flash_attn_flag (outer_model_cls )
10331033
10341034 if config .model_type == "minicpmv" or config .model_type == "minicpmo" :
@@ -1212,7 +1212,8 @@ def try_patch_legacy_flash_attn_flag(model_cls):
12121212 if model_cls is None or not isinstance (model_cls , type ):
12131213 return
12141214
1215- # Find the "source class" that defines _supports_flash_attn_2.
1215+ # Find the most specific class that explicitly declares the newer
1216+ # `_supports_flash_attn_2` flag used by newer transformers releases.
12161217 base_with_flag = None
12171218 for cls in model_cls .__mro__ :
12181219 if "_supports_flash_attn_2" in cls .__dict__ :
@@ -1222,8 +1223,15 @@ def try_patch_legacy_flash_attn_flag(model_cls):
12221223 if base_with_flag is None :
12231224 return
12241225
1226+ # Respect remote models that already define the legacy flag themselves.
1227+ for cls in model_cls .__mro__ :
1228+ if cls is base_with_flag :
1229+ break
1230+ if "_supports_flash_attn" in cls .__dict__ :
1231+ return
1232+
12251233 flash_attn_2_val = base_with_flag .__dict__ ["_supports_flash_attn_2" ]
1226- setattr (cls , "_supports_flash_attn" , bool (flash_attn_2_val ))
1234+ setattr (base_with_flag , "_supports_flash_attn" , bool (flash_attn_2_val ))
12271235
12281236
12291237def load_tokenizer (tokenizer_or_path , * , model_config : Any = None , ** kwargs ):
0 commit comments