Skip to content

[Model] Refactor modernbert with the attention interface - #43030

Merged
vasqu merged 55 commits into
huggingface:mainfrom
YangKai0616:Refactor-dev
Jan 29, 2026
Merged

vasqu merged 55 commits into
huggingface:mainfrom
YangKai0616:Refactor-dev

Conversation

@YangKai0616

@YangKai0616 YangKai0616 commented Dec 24, 2025 •

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR:

  1. Refactors ModernBERT's Flash Attention implementation using the built-in interfaces from Transformers.
  2. Fixes some UT issues in the tests/models/modernbert/test_modeling_modernbert.py and tests/models/pe_video/test_modeling_pe_video.py test files.

Background PR: #42952.

@YangKai0616

Copy link
Copy Markdown
Contributor Author

Hey @vasqu , could you please review it again? Thank you very much!

@vasqu

vasqu commented Jan 5, 2026

Copy link
Copy Markdown
Collaborator

Nice thx a lot @YangKai0616, I'll take a look tomorrow, just coming back from the holidays 🤗

@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 needed! I left some comments, especially the masking can be further improved along possibly adding check_model_inputs decorator with the usage of **kwargs.

Lmk if I can help out there, the masking is pretty core so it might be a bit too much

@YangKai0616

Copy link
Copy Markdown
Contributor Author

Hey @vasqu, I've updated the code based on your comments and removed the unrelated UT changes. Please take another look. Thanks!

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

We are getting there :D another round, these are less critical IMO - the general direction is already pretty good

Comment thread src/transformers/masking_utils.py Resolved
Comment thread src/transformers/masking_utils.py Resolved
Comment thread src/transformers/masking_utils.py Resolved
Comment thread src/transformers/masking_utils.py Resolved
Comment thread src/transformers/masking_utils.py Resolved
@YangKai0616

Copy link
Copy Markdown
Contributor Author

I’ve modified the code according to your suggestions. I’ll find some time tomorrow to test the related unit tests. If everything looks good, we can continue with the review. Thank you for your patience!

@vasqu

vasqu commented Jan 12, 2026

Copy link
Copy Markdown
Collaborator

@YangKai0616 just ping me again when you think it's ready for another review 🫡

@YangKai0616

Copy link
Copy Markdown
Contributor Author

The CI error seems unrelated to this PR. I was able to test it successfully on my local CUDA and XPU. @vasqu let's review it again, thank you !

@vasqu
vasqu requested a review from ArthurZucker January 22, 2026 19:42
@github-actions

Copy link
Copy Markdown
Contributor

@tomaarsen

tomaarsen commented Jan 23, 2026 •

Copy link
Copy Markdown
Member

Hello!

This is an extremely impressive PR, wow. The structure of the modeling/modular files look very strong as a result of all of this. I ran some downstream tests using Sentence Transformers with https://huggingface.co/nomic-ai/modernbert-embed-base, and there are some very slight differences in scores:

sdpa <v5:
{'NanoBEIR_mean_cosine_ndcg@10': 0.5147433277390966,
 'NanoMSMARCO_cosine_ndcg@10': 0.5867981583748159,
 'NanoNFCorpus_cosine_ndcg@10': 0.329223326613997,
 'NanoNQ_cosine_ndcg@10': 0.6282084982284769}
Evaluation took 27.23 seconds

eager <v5:
{'NanoBEIR_mean_cosine_ndcg@10': 0.5123706576330123,
 'NanoMSMARCO_cosine_ndcg@10': 0.5770369329466852,
 'NanoNFCorpus_cosine_ndcg@10': 0.32729203600264306,
 'NanoNQ_cosine_ndcg@10': 0.6327830039497084}
Evaluation took 44.04 seconds

fa2 <v5: I don't have flash_attn installed in the right venv at the moment; goes to show how nice having kernels support is

sdpa pr-43030:
{'NanoBEIR_mean_cosine_ndcg@10': 0.5110831146710934,
 'NanoMSMARCO_cosine_ndcg@10': 0.5828975140199241,
 'NanoNFCorpus_cosine_ndcg@10': 0.32718235725979056,
 'NanoNQ_cosine_ndcg@10': 0.6231694727335656}
Evaluation took 32.49 seconds

eager pr-43030:
{'NanoBEIR_mean_cosine_ndcg@10': 0.5099581159107758,
 'NanoMSMARCO_cosine_ndcg@10': 0.5843932028670678,
 'NanoNFCorpus_cosine_ndcg@10': 0.32340108499481846,
 'NanoNQ_cosine_ndcg@10': 0.622080059870441}
Evaluation took 54.74 seconds

fa2 pr-43030:
{'NanoBEIR_mean_cosine_ndcg@10': 0.5119965071683512,
 'NanoMSMARCO_cosine_ndcg@10': 0.581536709004633,
 'NanoNFCorpus_cosine_ndcg@10': 0.3271691052363954,
 'NanoNQ_cosine_ndcg@10': 0.6272837072640253}
Evaluation took 25.31 seconds

When I implemented this architecture, these forms of slight differences also existed between e.g. eager/sdpa/fa2, and it's extremely hard to narrow down the source (and from experience: often enough even with the source known, it's not possible to do anything about it).

The old implementation automatically 'unpadded' and 'repadded' during inference. I take it this variant doesn't do that? You write about a DataCollatorWithFlattening etc., is this required now for this speedup? Is there any way to know whether any given transformers models supports this class? It would be valuable for me to know for Sentence Transformers.

Also, this might be an issue on my side, but I wasn't able to get torch.compile working:

torch._dynamo.exc.Unsupported: Data-dependent branching
  Explanation: Detected data-dependent branching (e.g. `if my_tensor.sum() > 0:`). Dynamo does not support tracing dynamic control flow.
  Hint: This graph break is fundamental - it is unlikely that Dynamo will ever be able to trace through your code. Consider finding a workaround.
  Hint: Use `torch.cond` to express dynamic control flow.

In short: you've done some real magic here, and it's (at the least) very similar in accuracy, albeit likely quite a bit slower due to not forcing the unpadding/repadding (iiuc). However, that was never very transformers-y, and with this PR it should be much easier to build on top of ModernBERT again.

  • Tom Aarsen

@vasqu

vasqu commented Jan 23, 2026 •

Copy link
Copy Markdown
Collaborator

The old implementation automatically 'unpadded' and 'repadded' during inference. I take it this variant doesn't do that? You write about a DataCollatorWithFlattening etc., is this required now for this speedup? Is there any way to know whether any given transformers models supports this class? It would be valuable for me to know for Sentence Transformers.

Pretty much any model that supports attention backend should be able to do this. I refactored Bert for exactly the same reason for example (vLLM needs this as well). Transformers was never meant to manually pad/unpad during the forward, that's what the collator is for and is recommended for causal models like llama for quite a while now - this enables complete compile compatibility with FA.

Some side note: we manually pad/unpad ourselves already in FA (given a non all true attention mask)

albeit likely quite a bit slower due to not forcing the unpadding/repadding (iiuc)

Should be fast when properly preparing the data :D

Also, this might be an issue on my side, but I wasn't able to get torch.compile working

Can you share a script and env? The example works on my end (FA), also check SDPA with non fullgraph. Note

  • The data collator is necessary for clean compile compatibility - attention masks should also work but not full graph and minor graph breaks
  • Always compile the forward only
  • SDPA does not need the collator, it's only for FA

@tomaarsen

Copy link
Copy Markdown
Member

The data collator is necessary for clean compile compatibility

I see, I might've gone wrong there. I'll run more tests later. I was testing with Sentence Transformers, which doesn't use the collator.

  • Tom Aarsen

@YangKai0616

Copy link
Copy Markdown
Contributor Author

Hi everyone, any updates? 😀

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

@YangKai0616 great great PR! 🤗
Really aligned with the philosophy and is a clear simplification! Kudos

Thanks @vasqu for the thorough review.

Only 1 nit on sliding window settings based off layer_types if possible but otherwise LGTM

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.

great new integration test, I don't think we still need the other custom tests as now the code is quite standard (like disk offload)

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: modernbert, modernbert_decoder, pe_video

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: modernbert, modernbert_decoder, pe_video

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

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: modernbert, modernbert_decoder, pe_video

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: modernbert, modernbert_decoder, pe_video

@vasqu

vasqu commented Jan 29, 2026

Copy link
Copy Markdown
Collaborator

run-slow: modernbert, modernbert_decoder

@github-actions

Copy link
Copy Markdown
Contributor

This comment contains run-slow, running the specified jobs:

models: ["models/modernbert", "models/modernbert_decoder"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

✅ No failing test specific to this PR 🎉 !

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: modernbert, modernbert_decoder, pe_video

@vasqu
vasqu enabled auto-merge (squash) January 29, 2026 14:11
@vasqu

vasqu commented Jan 29, 2026

Copy link
Copy Markdown
Collaborator

Green CI, no regression, huge work @YangKai0616! Thanks a lot for this PR and iterating time and time again ❤️ it will be merged after the docs are built

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.

5 participants