Skip to content

[fix] Use return_dict=True in Transformer; improve how all_layer_embeddings are determined #3320

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

Merged

Conversation

tomaarsen
Copy link
Collaborator

Fixes #3318

Hello!

Pull Request overview

  • Improve how all_layer_embeddings are determined

Details

In short, instead of return_dict=False where we just get a tuple without any keys, we now use return_dict=True, allowing us to see the output names. transformers seems to have a pretty strong convention to use hidden_states for the output_hidden_states, so this should be fairly safe. Beyond that, I'm still using [0] indexing for the token embeddings (also known as last_hidden_state).

My intention is not to introduce any backwards breaking here, but there's always a risk. Let me know if your code breaks because of this!

  • Tom Aarsen

@tomaarsen tomaarsen requested a review from Copilot April 14, 2025 11:28
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

sentence_transformers/models/Transformer.py:443

  • [nitpick] Using indexing with [0] to extract token embeddings may be unclear now that a dictionary is returned; consider using outputs['last_hidden_state'] to improve code clarity and maintain consistency with typical transformers output.
token_embeddings = outputs[0]

@tomaarsen tomaarsen merged commit 03dff58 into UKPLab:master Apr 14, 2025
1 of 9 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.

ModernBERT cannot get all_layer_embeddings
1 participant