Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModernBERT bug fixes #35404

Merged
merged 8 commits into from
Jan 9, 2025
Merged

Conversation

warner-benjamin
Copy link
Contributor

@warner-benjamin warner-benjamin commented Dec 23, 2024

This PR fixes a few issues with the ModernBERT implementation and some typos in the docs.

First, on the flash attention 2 path, ModernBERT was incorrectly always returning repadded logits without gradients (see #35386). This PR will return padded logits with gradients if no labels are passed to ModernBertForMaskedLM or if repad_logits_with_grad is set to true. We don't keep the gradient when repadding after using an internal loss function by default to save memory.

Second, this PR fixes torch.compile from being automatically set on when on CPU (#35388).

Third, I added some details to the model doc strings.

Fourth, documentation is updated to capitalize BERT in ModernBERT following the other BERT models.

cc @ArthurZucker @tomaarsen

@@ -503,7 +503,7 @@
- local: model_doc/mobilebert
title: MobileBERT
- local: model_doc/modernbert
title: ModernBert
title: ModernBERT
Copy link
Member

Choose a reason for hiding this comment

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

Good call - I got carried away with the Python class naming

if config._attn_implementation_internal is None:
config._attn_implementation_internal = "flash_attention_2"
try:
return cls._check_and_enable_flash_attn_2(
config,
torch_dtype=torch_dtype,
torch_dtype=torch.float16,
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a cleaner solution to avoid the unnecessary FP32 warning than I figured was possible, nice.

@HuggingFaceDocBuilderDev

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.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

I'll let @tomaarsen update, I think we can just check if training and labels iwthout having a new arg

…aining

I'm not 100% that the conditional with "or labels is None" makes sense though - not sure what the intention is there. Perhaps we can remove that?
Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

LGTM, thanks both!

@tomaarsen tomaarsen merged commit 1e3ddcb into huggingface:main Jan 9, 2025
10 checks passed
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