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

[fix] Prevent IndexError if output_hidden_states & ONNX #3008

Merged

Conversation

tomaarsen
Copy link
Collaborator

@tomaarsen tomaarsen commented Oct 21, 2024

Resolves #3004

Hello!

Pull Request overview

  • Prevent IndexError if output_hidden_states & ONNX

Details

Some models specify output_hidden_states in their config, but the ONNX model doesn't respect this. As a result, the output_states is just 1 value: the last_hidden_states, whereas the code expects that there's also a all_hidden_states value.

Now we ensure that there's at least 2 outputs to prevent a crash.

Code

from sentence_transformers import SentenceTransformer

model = SentenceTransformer(
    "distiluse-base-multilingual-cased",
    backend="onnx",
    model_kwargs={"provider": "CPUExecutionProvider"},
)

sentences = ["This is an example sentence", "Each sentence is converted"]
embeddings = model.encode(sentences)
print(embeddings.shape)

This script used to fail, see #3004.

  • Tom Aarsen

@tomaarsen tomaarsen merged commit f286d9f into UKPLab:master Oct 21, 2024
11 checks passed
@tomaarsen tomaarsen deleted the fix/output_hidden_states_and_onnx branch October 21, 2024 10:58
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.

backend="onnx" IndexError: tuple index out of range
1 participant