Skip to content

🚨 Modify ModernBERT's default attention implementation to stop using FA - #43764

Merged
vasqu merged 4 commits into
huggingface:mainfrom
YangKai0616:modernbert-attn
Feb 6, 2026
Merged

vasqu merged 4 commits into
huggingface:mainfrom
YangKai0616:modernbert-attn

Conversation

@YangKai0616

Copy link
Copy Markdown
Contributor

Background: PR #43672

This PR modifies the default attention implementation in modernbert, switching away from FlashAttention. It also restores the related tests that were previously overridden.

@vasqu , please help review again, thank you very much!

logits_audio_text = logits_audio_text * self.text_audio_logit_scale.to(
logits_audio_text.device
) + self.text_audio_logit_bias.to(logits_audio_text.device)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fix this because the test tests/models/pe_audio/test_modeling_pe_audio.py::PeAudioModelTest::test_model_parallelism will report error RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0!.

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

Much better, can you add 🚨 to the title since it is breaking (in a good way)

@vasqu

vasqu commented Feb 5, 2026 •

Copy link
Copy Markdown
Collaborator

Also check modern_bert decoder, it inherited from modernbert (should be a simple removal of the function there in modular) + should we add a small note in the docs / update them for the padding free example

with torch.no_grad():
output = model(**inputs)[0]
expected_shape = torch.Size((1, 6, model.config.vocab_size))
expected_shape = torch.Size((1, 7, model.config.vocab_size))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The current version of the tokens output template has add_eos_token=True, and the actual output here is: ['[CLS]', 'Paris', ' is', ' the', ' capital', ' of', '[SEP]']. Therefore, expected_shape needs to be modified.

@github-actions

github-actions Bot commented Feb 6, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: modernbert, modernbert_decoder, pe_audio, pe_video

@YangKai0616

Copy link
Copy Markdown
Contributor Author

Also check modern_bert decoder, it inherited from modernbert (should be a simple removal of the function there in modular) + should we add a small note in the docs / update them for the padding free example

Done.

@YangKai0616

Copy link
Copy Markdown
Contributor Author

Much better, can you add 🚨 to the title since it is breaking (in a good way)

It seems I don’t have permission to edit the title. The Edit button next to the title is gone.
Could you please help me change the title? Thank you!

@vasqu vasqu changed the title Modify ModernBERT's default attention implementation to stop using FA 🚨 Modify ModernBERT's default attention implementation to stop using FA Feb 6, 2026

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

Perfect, it's an important step imo. It already started to grow on a few models so this was going to be a nightmare (maintenance-wise)

No idea why you can't modify the title 😭 I just did, no worries

@vasqu
vasqu enabled auto-merge (squash) February 6, 2026 11:17
@vasqu
vasqu merged commit 2db2c88 into huggingface:main Feb 6, 2026
9 checks passed
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

jiosephlee pushed a commit to jiosephlee/transformers_latest that referenced this pull request Feb 11, 2026
…FA (huggingface#43764)

* Modify ModernBERT's default attention implementation to stop using FA

* Style

* Revised based on comments

* Update doc
tarekziade added a commit that referenced this pull request Sep 7, 2026
…the rebuild

Skipping `update_post_processor()` whenever `tokenizer.json` ships a post-processor
makes the call added by #47988 dead code -- the base class already updates the
post-processor in exactly those cases (`_should_update_post_processor`,
`tokenization_utils_tokenizers.py`) -- so it reintroduces the OLMo bug. Verified on
`allenai/OLMo-7B-hf`: with that patch every input gets `<|endoftext|>` appended again.

The actual root cause is that `_from_pretrained` drops `add_bos_token`/`add_eos_token`
from the init kwargs when a `tokenizer.json` is present, so #47988 rebuilds the
post-processor from flags that have just been erased. The result is "no bos, no eos"
for every GPTNeoX checkpoint: correct for OLMo (which saves `false`/`false`), wrong for
any checkpoint whose post-processor adds special tokens -- `blab-jhu/test-32m-dec`
went from `[CLS] Paris is the capital of` to `Paris is the capital of`.

Keep the flags for the tokenizers whose class opts in, and let the base class rebuild
the post-processor from them. This restores the pre-#42563 behaviour on both
checkpoints (verified locally):

- `allenai/OLMo-7B-hf` (`add_bos_token=false`, `add_eos_token=false`): no EOS appended.
- `blab-jhu/test-32m-dec` (`add_bos_token=true`): `[CLS] Paris is the capital of`, i.e.
  6 tokens. The trailing `[SEP]` its `tokenizer.json` adds is the same pathology as
  OLMo's EOS on a causal LM, and 6 was the expected value from #38967 until #43764
  bumped it to 7 as collateral of the tokenizer refactor. The expected logit slices
  are unchanged, as only the trailing token disappears.

Co-Authored-By: Claude Opus 5 (1M context) <[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.

3 participants