Skip to content

Fix bug ORTModelForFeatureExtraction #2267

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
merged 7 commits into from
May 15, 2025

Conversation

Abdennacer-Badaoui
Copy link
Member

What does this PR do?

This PR updates the ORTModelForFeatureExtraction's forward method to accept **kwargs, ensuring better compatibility with Hugging Face Transformers' conventions. In particular, it adds proper support for the return_dict argument: when explicitly passed, the output is returned as a plain dictionary instead of being wrapped in a ModelOutput. Additionally, any unused keyword arguments are logged with a warning to help users identify unsupported inputs while maintaining backward compatibility.

Fixes issue : #2266

@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.

Co-authored-by: Ilyas Moutawwakil <57442720+IlyasMoutawwakil@users.noreply.github.com>
@IlyasMoutawwakil IlyasMoutawwakil marked this pull request as draft May 14, 2025 12:44
@Abdennacer-Badaoui Abdennacer-Badaoui marked this pull request as ready for review May 14, 2025 14:41
Copy link
Collaborator

@echarlaix echarlaix left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the fix @Abdennacer-Badaoui !

@IlyasMoutawwakil IlyasMoutawwakil merged commit 346d022 into main May 15, 2025
31 checks passed
@IlyasMoutawwakil IlyasMoutawwakil deleted the fix-bug-ORTModelForFeatureExtraction branch May 15, 2025 11:14
IlyasMoutawwakil added a commit that referenced this pull request May 15, 2025
* Fix bug ORTModelForFeatureExtraction

* Update optimum/onnxruntime/modeling_ort.py

Co-authored-by: Ilyas Moutawwakil <57442720+IlyasMoutawwakil@users.noreply.github.com>

* Update optimum/onnxruntime/modeling_ort.py

* return_dict support for ORTModelForxxx

* Add _warn_on_unhandled_inputs helper method to ORTModel

* Add test for the return_dict feature

* fix style

---------

Co-authored-by: Ilyas Moutawwakil <57442720+IlyasMoutawwakil@users.noreply.github.com>
@malteos
Copy link

malteos commented May 15, 2025

Is this really fixing the underlying issue? With this PR, native Python dicts are returned. However, transformers uses ModelOutput, or?

At least for me I have this issue now: When using a sentence-transformers ONNX model, inference fails at this point: https://github.com/UKPLab/sentence-transformers/blob/master/sentence_transformers/models/Transformer.py#L449-L450

# outputs (with return_dict=True)
{'last_hidden_state': tensor([[[-3.2455e-01, -5.7767e-01, -1.2708e+00,  ...,  8.5406e-01,
           3.3558...00,
           1.3960e+00, -8.4762e-01]]])}

@IlyasMoutawwakil
Copy link
Member

oh wow ! so transformers doesn't return a dict when return_dict is True, and returns a tuple when it's False 😵

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants