Repository navigation
Add ModernBERT Decoder Models - ModernBERT, but trained with CLM! - #38967
Conversation
|
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! |
|
cc @ArthurZucker if you think this is appropriate for the library! I'm uncertain - I still think a smaller |
|
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 EDIT, to quote the last comment from @Rocketknight1 in #35946
We can't change ModernBERT to have a |
|
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
left a comment
There was a problem hiding this comment.
Thanks for the PR!
|
Super interested in this PR as well. Sending cheers to @orionw to boost your motivation. |
|
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. |
|
Hi @ArthurZucker, just a ping if you have a sec :) |
|
Sorry, reviewing again! |
|
Thanks @ArthurZucker, but unfortunately I don't see the review. Do you mind double checking? |
ArthurZucker
left a comment
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
|
@ArthurZucker I merged in main but it seems like the recent lfm2 merge broke some things unrelated to this. Otherwise all the tests pass.
|
|
[For maintainers] Suggested jobs to run (before merge) run-slow: auto, modernbert_decoder |
|
Merged in the recent fixes from main, everything passes. Ready @ArthurZucker! |
ArthurZucker
left a comment
There was a problem hiding this comment.
Perfect thanks a lot for your patience, I am sorry this took so long an kudos! 🤗 Merging !
|
Thank you so much @ArthurZucker!! Appreciate your patience as I learn the new stuff :) |
…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]>
…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]>
…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]>
…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]>
…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]>
…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]>
…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]>
…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]>
…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]>
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 (
headvslm_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
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@Rocketknight1 @gante @ArthurZucker