Skip to content

Fix bug ORTModelForFeatureExtraction#2267

Merged
IlyasMoutawwakil merged 7 commits into
mainfrom
fix-bug-ORTModelForFeatureExtraction
May 15, 2025
Merged

Fix bug ORTModelForFeatureExtraction#2267
IlyasMoutawwakil merged 7 commits into
mainfrom
fix-bug-ORTModelForFeatureExtraction

Conversation

@Abdennacer-Badaoui

Copy link
Copy Markdown
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

Copy link
Copy Markdown

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.

Comment thread optimum/onnxruntime/modeling_ort.py Outdated
Comment thread optimum/onnxruntime/modeling_ort.py Outdated
Comment thread optimum/onnxruntime/modeling_ort.py Outdated
@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
Comment thread optimum/onnxruntime/modeling_ort.py Outdated

@echarlaix echarlaix left a comment

Copy link
Copy Markdown
Collaborator

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 !

Comment thread optimum/onnxruntime/modeling_ort.py
Comment thread tests/onnxruntime/test_modeling.py
@IlyasMoutawwakil
IlyasMoutawwakil merged commit 346d022 into main May 15, 2025
@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 <[email protected]>

* 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 <[email protected]>
@malteos

malteos commented May 15, 2025

Copy link
Copy Markdown

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
Copy Markdown
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