Skip to content

Add ModernBERT Decoder Models - ModernBERT, but trained with CLM! - #38967

Merged
ArthurZucker merged 32 commits into
huggingface:mainfrom
JHU-CLSP:modernbertdecoder
Jul 15, 2025
Merged

ArthurZucker merged 32 commits into
huggingface:mainfrom
JHU-CLSP:modernbertdecoder

Conversation

@orionw

@orionw orionw commented Jun 22, 2025 •

Copy link
Copy Markdown
Contributor

This PR adds the new ModernBERTDecoder architecture: it's ModernBERT but trained from scratch as a CLM! This replaces my attempt in #35946 to fit them all in one class. Instead I'll have a script to convert them from encoders to decoders and vice versa by renaming the class and head name (head vs lm_head)

The models/paper are ready to go, but we wanted to have this up before we do the official release so we can have all the release artifacts ready. Thanks again for all your help on the last PR and for suggesting this approach!

FAQs:

Q: Why don't we just add this to ModernBERT?
A: Because we discussed in #35946 that it was too complicated to add the causal attention combined with everything else. This keeps it simpler and uses the standard attention functions

Q: How can BERT be a decoder?
A: It's really the "ModernBERT" architecture trained from scratch via CLM. It's not MLM at all. We could rename the architecture to whatever we decide to name it in the paper, if that's preferable. But I thought this was the easiest to understand since they share the same everything else.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@Rocketknight1 @gante @ArthurZucker

@orionw

orionw commented Jun 22, 2025 •

Copy link
Copy Markdown
Contributor Author

Hi @Rocketknight1 @gante, this is the new PR after our last conversation. I think it's much simpler as it's just a generic decoder with re-used ModernBERT parts. Thanks again for the help!

@Rocketknight1

Copy link
Copy Markdown
Member

cc @ArthurZucker if you think this is appropriate for the library! I'm uncertain - I still think a smaller ForCausalLM wrapper that doesn't replace ModernBERT attention layers might be better, like RobertaForCausalLM.

@orionw

orionw commented Jun 23, 2025 •

Copy link
Copy Markdown
Contributor Author

Just to clarify though this does use normal causal attention like any other decoder, unlike ModernBERT (encoder).

I'm a bit confused though, in the last PR ((#35946) the attention layers were what was deemed too complicated (dealing with local vs global and causal vs non-casual at the same time). e.g. the last PR was the ForCausalLM wrapper you mention, but it can't really be much "smaller" due to the complexity of handling that.

EDIT, to quote the last comment from @Rocketknight1 in #35946

Adding large tokenizer / internal layer changes to an MLM can create bugs, reduce performance and make it harder to maintain the core MLM functionality, which we don't want! Running MLMs as CLMs is a very niche thing to do.

We can't change ModernBERT to have a ForCausalLM without changing the internal attention layers. See @ArthurZucker's comment on my last PR in the attention layer where he said to make a new class, which is this PR.

@orionw

orionw commented Jun 23, 2025 •

Copy link
Copy Markdown
Contributor Author

Adding one more comment here to reinforce this - this isn't for a simple continued pre-train on top of ModernBERT.

This is a brand new from scratch decoder model suite (17m to 1B sizes) trained on 2T of open-data but that uses the same architecture as ModernBERT in terms of shapes. It is essentially a new decoder-LM for the library, it also just happens to be the same architecture which is great for comparisons.

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

Thanks for the PR!

@jbdel

jbdel commented Jun 27, 2025

Copy link
Copy Markdown

Super interested in this PR as well. Sending cheers to @orionw to boost your motivation.
JB

@orionw

orionw commented Jun 27, 2025

Copy link
Copy Markdown
Contributor Author

Appreciate the support @jbdel! :)

And thanks @ArthurZucker, sorry I didn't notice the new setup. It definitely looks a lot cleaner with those changes!

I got everything you suggested, so I think it's good to go! Please let me know if there's anything else.

@orionw

orionw commented Jul 2, 2025

Copy link
Copy Markdown
Contributor Author

Hi @ArthurZucker, just a ping if you have a sec :)

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Sorry, reviewing again!

@orionw

orionw commented Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

Thanks @ArthurZucker, but unfortunately I don't see the review. Do you mind double checking?

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

Thanks, there are still a few things to adress:

  • from the look of it the main different with say gemma2 is that it does not have sofcap, or with qwen2 I am not sure BUT let's make sure it's standardized!
  • have a look at modular Gemma2 for example!

once this is fixed we can merge! Let's also disable the compilation it is not a good idea in the scope of transformers, and it is something that users can granurally do themselves!

@orionw orionw left a comment •

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.

Thanks so much @ArthurZucker! And for the pointer to the new @check_model_inputs decorator.

It is very basic, similar to Gemma3 without the softcap, exactly. I updated it to follow Gemma3 for the sliding windows and otherwise keep Llama's new updates.

Also I love that this PR has dropped 500+ lines of code due to all your recent refactors - amazing!

I think it's ready for a re-review when you get a chance.

Comment on lines +693 to +709
def get_input_embeddings(self):
return self.model.embeddings.tok_embeddings

def set_input_embeddings(self, value):
self.model.embeddings.tok_embeddings = value

def get_output_embeddings(self):
return self.decoder

def set_output_embeddings(self, new_embeddings):
self.decoder = new_embeddings

def set_decoder(self, decoder):
self.model = decoder

def get_decoder(self):
return self.model

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.

I had to keep the set_decoder and get_decoder because they aren't inherited (and are needed for the tied weights), but I removed the others.

@orionw

orionw commented Jul 10, 2025

Copy link
Copy Markdown
Contributor Author

@ArthurZucker I merged in main but it seems like the recent lfm2 merge broke some things unrelated to this. Otherwise all the tests pass.

FAILED tests/models/lfm2/test_modeling_lfm2.py::Lfm2ModelTest::test_sample_generate - AttributeError: 'Lfm2HybridConvCache' object has no attribute '_seen_tokens'

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: auto, modernbert_decoder

@orionw

orionw commented Jul 10, 2025 •

Copy link
Copy Markdown
Contributor Author

Merged in the recent fixes from main, everything passes. Ready @ArthurZucker!

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

Perfect thanks a lot for your patience, I am sorry this took so long an kudos! 🤗 Merging !

@ArthurZucker
ArthurZucker merged commit 0e4b793 into huggingface:main Jul 15, 2025
@orionw

orionw commented Jul 15, 2025 •

Copy link
Copy Markdown
Contributor Author

Thank you so much @ArthurZucker!! Appreciate your patience as I learn the new stuff :)

rjgleaton pushed a commit to rjgleaton/transformers that referenced this pull request Jul 17, 2025
…ggingface#38967)

* working locally; need to style and test

* added docs and initial tests; need to debug and flesh out

* fixed tests

* working long context; batches

* working fa2 and eager

* update tests

* add missing confnigs

* remove default autoset

* fix spacing

* fix most tests

* fixed tests

* fix to init

* refactor to match new transformers updates

* remove static cache option

* fa2 fix

* fix docs

* in progress

* working on tests

* fixed issue with attn outputs

* remove debug

* fix local config attr

* update doc string

* fix docstring

* add docs to toc

* correct typo in toc

* add new updates from main w.r.t. ModernBERT RoPE

* fix local param

---------

Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
…ggingface#38967)

* working locally; need to style and test

* added docs and initial tests; need to debug and flesh out

* fixed tests

* working long context; batches

* working fa2 and eager

* update tests

* add missing confnigs

* remove default autoset

* fix spacing

* fix most tests

* fixed tests

* fix to init

* refactor to match new transformers updates

* remove static cache option

* fa2 fix

* fix docs

* in progress

* working on tests

* fixed issue with attn outputs

* remove debug

* fix local config attr

* update doc string

* fix docstring

* add docs to toc

* correct typo in toc

* add new updates from main w.r.t. ModernBERT RoPE

* fix local param

---------

Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
…ggingface#38967)

* working locally; need to style and test

* added docs and initial tests; need to debug and flesh out

* fixed tests

* working long context; batches

* working fa2 and eager

* update tests

* add missing confnigs

* remove default autoset

* fix spacing

* fix most tests

* fixed tests

* fix to init

* refactor to match new transformers updates

* remove static cache option

* fa2 fix

* fix docs

* in progress

* working on tests

* fixed issue with attn outputs

* remove debug

* fix local config attr

* update doc string

* fix docstring

* add docs to toc

* correct typo in toc

* add new updates from main w.r.t. ModernBERT RoPE

* fix local param

---------

Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
…ggingface#38967)

* working locally; need to style and test

* added docs and initial tests; need to debug and flesh out

* fixed tests

* working long context; batches

* working fa2 and eager

* update tests

* add missing confnigs

* remove default autoset

* fix spacing

* fix most tests

* fixed tests

* fix to init

* refactor to match new transformers updates

* remove static cache option

* fa2 fix

* fix docs

* in progress

* working on tests

* fixed issue with attn outputs

* remove debug

* fix local config attr

* update doc string

* fix docstring

* add docs to toc

* correct typo in toc

* add new updates from main w.r.t. ModernBERT RoPE

* fix local param

---------

Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
…ggingface#38967)

* working locally; need to style and test

* added docs and initial tests; need to debug and flesh out

* fixed tests

* working long context; batches

* working fa2 and eager

* update tests

* add missing confnigs

* remove default autoset

* fix spacing

* fix most tests

* fixed tests

* fix to init

* refactor to match new transformers updates

* remove static cache option

* fa2 fix

* fix docs

* in progress

* working on tests

* fixed issue with attn outputs

* remove debug

* fix local config attr

* update doc string

* fix docstring

* add docs to toc

* correct typo in toc

* add new updates from main w.r.t. ModernBERT RoPE

* fix local param

---------

Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
…ggingface#38967)

* working locally; need to style and test

* added docs and initial tests; need to debug and flesh out

* fixed tests

* working long context; batches

* working fa2 and eager

* update tests

* add missing confnigs

* remove default autoset

* fix spacing

* fix most tests

* fixed tests

* fix to init

* refactor to match new transformers updates

* remove static cache option

* fa2 fix

* fix docs

* in progress

* working on tests

* fixed issue with attn outputs

* remove debug

* fix local config attr

* update doc string

* fix docstring

* add docs to toc

* correct typo in toc

* add new updates from main w.r.t. ModernBERT RoPE

* fix local param

---------

Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
…ggingface#38967)

* working locally; need to style and test

* added docs and initial tests; need to debug and flesh out

* fixed tests

* working long context; batches

* working fa2 and eager

* update tests

* add missing confnigs

* remove default autoset

* fix spacing

* fix most tests

* fixed tests

* fix to init

* refactor to match new transformers updates

* remove static cache option

* fa2 fix

* fix docs

* in progress

* working on tests

* fixed issue with attn outputs

* remove debug

* fix local config attr

* update doc string

* fix docstring

* add docs to toc

* correct typo in toc

* add new updates from main w.r.t. ModernBERT RoPE

* fix local param

---------

Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
…ggingface#38967)

* working locally; need to style and test

* added docs and initial tests; need to debug and flesh out

* fixed tests

* working long context; batches

* working fa2 and eager

* update tests

* add missing confnigs

* remove default autoset

* fix spacing

* fix most tests

* fixed tests

* fix to init

* refactor to match new transformers updates

* remove static cache option

* fa2 fix

* fix docs

* in progress

* working on tests

* fixed issue with attn outputs

* remove debug

* fix local config attr

* update doc string

* fix docstring

* add docs to toc

* correct typo in toc

* add new updates from main w.r.t. ModernBERT RoPE

* fix local param

---------

Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
Co-authored-by: oweller2 <[email protected]>
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.

4 participants