Fix CANINE fp16 dtype mismatch in attention mask causing ONNX export failure#47083
Merged
Merged
Conversation
…rward _create_3d_attention_mask_from_input_mask() always constructs the mask in float32 via hardcoded .float() calls, regardless of model dtype. When the model runs in float16, adding this float32 mask to float16 attention_scores promotes them to float32. After softmax, attention_probs stays float32 while value_layer remains float16, causing: RuntimeError: expected scalar type Float but found Half Fix: cast attention_mask to attention_scores.dtype before the addition. Only affects the shallow character encoders (ndim==3 mask path). Fixes huggingface#47050
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: canine |
4 tasks
Contributor
CI recapDashboard: View test results in Grafana |
6 tasks
Rocketknight1
approved these changes
Jul 6, 2026
Rocketknight1
left a comment
Member
There was a problem hiding this comment.
It's a code agent PR but I'll allow it because I think the bug is real here.
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #47050
Problem
_create_3d_attention_mask_from_input_mask()always builds the mask in float32 through hardcoded.float()anddtype=torch.float32, ignoring the model dtype. InCanineSelfAttention.forward()thendim==3branch adds this float32 mask toattention_scores. When the model runs in float16 the addition promotesattention_scoresto float32. After softmaxattention_probsstays float32 whilevalue_layeris still float16, so the final matmul crashes. Only the shallow character encoders hit this. The deep encoder usescreate_bidirectional_mask()which is already dtype aware.Fix
One line in
CanineSelfAttention.forward():Reproduction
Before
Runtime dtype trace at the crash point:
Result:
ONNX export never completes.
After
Same trace with the fix applied:
Result:
No crash. The dtype stays float16 through the whole attention path.
Numerical check
fp16 output compared against the fp32 baseline on cpu, same input:
Difference is within normal fp16 tolerance. The output is not corrupted.
Tests
Added
test_model_fp16_with_attention_masktoCanineModelTest. It builds the model from config only, no pretrained weights, calls.half(), forces partial masking to hit the 3D mask path, and asserts the output dtype is float16 with no NaN.Full file result:
Existing
test_onnx_export_*andtest_torch_export_*still pass, so the change introduces no regression in the export paths.Out of scope
A second independent issue exists, a device mismatch in
_repeat_molecules()that becomes visible only after this dtype fix. It is noted in #47050 and left for a follow up PR.