Skip to content

Add FA2 fallback (kernel) support to the modernbert model - #42952

Closed
YangKai0616 wants to merge 4 commits into
huggingface:mainfrom
YangKai0616:main
Closed

YangKai0616 wants to merge 4 commits into
huggingface:mainfrom
YangKai0616:main

Conversation

@YangKai0616

@YangKai0616 YangKai0616 commented Dec 19, 2025 •

Copy link
Copy Markdown
Contributor

What does this PR do?

  1. Add FA2 fallback (kernel) support to the modernbert model.
    Related UT:
FAILED tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_cpu_offload - KeyError: 'kernels-community/flash-attn2'
FAILED tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_determinism - KeyError: 'kernels-community/flash-attn2'
FAILED tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_disk_offload_bin - KeyError: 'kernels-community/flash-attn2'
FAILED tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_disk_offload_safetensors - KeyError: 'kernels-community/flash-attn2'
FAILED tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_model - KeyError: 'kernels-community/flash-attn2'
FAILED tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_model_outputs_equivalence - KeyError: 'kernels-community/flash-attn2'
FAILED tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_model_parallelism - KeyError: 'kernels-community/flash-attn2'
  1. When FA2 is available, tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_init_weights_can_init_buffers will report an error
    AssertionError: False is not true : The following buffers are not properly handled in `_init_weights()` (the model should be able to reinitialize them correctly if t....

Root cause: The inv_freq buffer in ModernBertUnpaddedRotaryEmbedding (inherited from flash_attn's RotaryEmbedding) was not properly initialized when loading from meta device. Added
reinitialization logic in _init_weights() to compute and copy correct inv_freq values.

@YangKai0616 YangKai0616 reopened this Dec 19, 2025
@YangKai0616

Copy link
Copy Markdown
Contributor Author

Hey, @vasqu , pls help review. Thx!

@vasqu vasqu 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.

Sorry about being strict here but I'd rather that we refactor modernbert with the attention interface. This model in general needs a big refactor as it does not follow many of our standards.

This feels like it will only create tech debt and many other models would fall under similar conditions where we could manually update them but it is not the way to go.

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: modernbert

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