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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

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

@@ -892,6 +900,13 @@ def _maybe_set_compile(self):
)
self.config.reference_compile = False

if self.device.type == "cpu":
logger.warning_once(
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 we should wrap this in if self.config.reference_compile: just like above so users don't get warned when they never manually specified anything related to reference_compile.

Unless you'd rather always give this warning so people aren't confused why there's a small precision-esque difference between GPU setups?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was my intention, I forgot the if statement. Fixed in ab11657.

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.

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