Skip to content

Fix ONNX export for sequence classification head #36332

Merged
Rocketknight1 merged 4 commits into
huggingface:mainfrom
echarlaix:int32-dtype
Mar 20, 2025
Merged

Fix ONNX export for sequence classification head #36332
Rocketknight1 merged 4 commits into
huggingface:mainfrom
echarlaix:int32-dtype

Conversation

@echarlaix

Copy link
Copy Markdown
Collaborator

Since #35911, some models with a sequence classification head will be exported to ONNX with an ArgMax operator in int64, making the resulting model incompatible with ORT. Here we set token_indices to int32, fixing :

NOT_IMPLEMENTED : Could not find an implementation for ArgMax(13) node with name '/ArgMax'

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

@echarlaix

Copy link
Copy Markdown
Collaborator Author

@ArthurZucker @Rocketknight1 let me know if that works for you

@Rocketknight1 Rocketknight1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I wrote this code, but I didn't realize this would cause an issue for ONNX! The change is fine, since the output should be identical regardless.

We could also consider updating the line

pooled_logits = logits[torch.arange(batch_size, device=logits.device), last_non_pad_token]

to

pooled_logits = logits[torch.arange(batch_size, device=logits.device, dtype=torch.int32), last_non_pad_token]

I don't think it's strictly necessary, but if we don't do this then the indexing will use an int64 tensor for one dimension and an int32 tensor for another dimension. That works fine on Torch right now, but it seems like the kind of thing that might break in other frameworks or when the model is exported.

@Rocketknight1

Copy link
Copy Markdown
Member

(If you don't want to do that, though, we can just merge this PR as-is. The change I suggested is just future-proofing rather than strictly necessary!)

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

LGTM anyways

@Rocketknight1

Copy link
Copy Markdown
Member

Yeah, let's just merge this as-is! If the int64/int32 inputs thing becomes a problem when exporting later we can fix it then.

@Rocketknight1
Rocketknight1 merged commit 055afdb into huggingface:main Mar 20, 2025
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this pull request May 14, 2025
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.

4 participants