Skip to content

Commit d831289

Browse files
committed
Patch remote flash attention compat
1 parent 30c7201 commit d831289

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

gptqmodel/utils/hf.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

12291237
def load_tokenizer(tokenizer_or_path, *, model_config: Any = None, **kwargs):

tests/test_hf_config_compat.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ def tie_weights(self):
377377
assert getattr(DummyRemoteModel, "_gptqmodel_tie_weights_kwargs_patch", False) is True
378378

379379

380+
def test_prepare_remote_model_init_compat_backfills_legacy_flash_attn_flag(monkeypatch):
381+
class DummyRemoteBase:
382+
_supports_flash_attn_2 = True
383+
384+
class DummyRemoteModel(DummyRemoteBase):
385+
__module__ = "transformers_modules.fake_bailing.modeling_bailing_moe_v2"
386+
387+
monkeypatch.setattr(
388+
"transformers.dynamic_module_utils.get_class_from_dynamic_module",
389+
lambda class_ref, model_id_or_path, **kwargs: DummyRemoteModel,
390+
)
391+
392+
config = SimpleNamespace(
393+
model_type="bailing_moe",
394+
auto_map={"AutoModelForCausalLM": "modeling_bailing_moe_v2.BailingMoeV2ForCausalLM"},
395+
)
396+
397+
assert "_supports_flash_attn" not in DummyRemoteBase.__dict__
398+
399+
prepare_remote_model_init_compat("/tmp/ling", config)
400+
401+
assert DummyRemoteBase._supports_flash_attn is True
402+
assert DummyRemoteModel._supports_flash_attn is True
403+
404+
380405
def test_prepare_remote_model_init_compat_accepts_tokenizers_backend_for_ovis(monkeypatch):
381406
class DummyRemoteModel:
382407
__module__ = "transformers_modules.fake_ovis.modeling_ovis"

0 commit comments

Comments
 (0)