Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix shared config mutation issue in flash_attn_from_config#45678

Merged
ydshieh merged 2 commits into
huggingface:mainfrom
kaixuanliu:config-copy
May 7, 2026
Merged

Fix shared config mutation issue in flash_attn_from_config#45678
ydshieh merged 2 commits into
huggingface:mainfrom
kaixuanliu:config-copy

Conversation

@kaixuanliu

@kaixuanliu kaixuanliu commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a test isolation bug where test_from_pretrained_no_checkpoint and test_load_save_without_tied_weights fail when run after test_flash_attn_2_from_config in the same session (with RUN_SLOW=1).

Root cause

_from_config() mutates sub-configs in-place when setting dtype:

for sub_config_key in config.sub_configs:    sub_config.dtype = dtype
Some model testers (e.g. Phi4MultimodalModelTester) use mutable default arguments for sub-configs like vision_config=Phi4MultimodalVisionConfig(...). These objects are created once at class definition time and shared across all calls to prepare_config_and_inputs_for_common().

When flash_attn_from_config calls _from_config(config, dtype=torch.bfloat16), it permanently sets vision_config.dtype = torch.bfloat16 on the shared sub-config object. All subsequent tests then create models with bfloat16 vision weights instead of float32, causing weight mismatches.

Fix

deepcopy the config before passing it to _from_config in flash_attn_from_config, preventing the mutation from leaking across tests. This is a general fix that protects all models.

Tests

RUN_SLOW=1 pytest tests/models/phi4_multimodal/test_modeling_phi4_multimodal.py -k "not deepspeed"
# Before: 2 failed (test_from_pretrained_no_checkpoint, test_load_save_without_tied_weights)
# After: all pass

@ydshieh pls help review, thx!

@ydshieh

ydshieh commented Apr 28, 2026

Copy link
Copy Markdown
Collaborator

We should avoid using mutable object in arguments. But to fix this failing test, let's use

class Phi4MultimodalModelTester:
        ...
        self.audio_config = deepcopy(audio_config)
        self.vision_config = deepcopy(vision_config)

Signed-off-by: Liu, Kaixuan <[email protected]>
@kaixuanliu

Copy link
Copy Markdown
Contributor Author

Thx for your advice. Looks much better now.

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: phi4_multimodal

@ydshieh ydshieh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@ydshieh ydshieh merged commit 10f5397 into huggingface:main May 7, 2026
17 checks passed
@kaixuanliu kaixuanliu deleted the config-copy branch May 8, 2026 01:56
louzongzhi pushed a commit to louzongzhi/transformers that referenced this pull request May 11, 2026
…ce#45678)

* Fix shared config mutation issue in flash_attn_from_config

Signed-off-by: Liu, Kaixuan <[email protected]>

* update code

Signed-off-by: Liu, Kaixuan <[email protected]>

---------

Signed-off-by: Liu, Kaixuan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants