Conversation
torch.export and ExecuTorch pass zeros_like's dtype as a keyword argument, but the converter only read it positionally (the TorchScript form), so the result kept the input's dtype. nn.MultiheadAttention builds its float attention mask as zeros_like(bool_mask, dtype=q.dtype).masked_fill(bool_mask, -inf) (F._canonical_mask). With bool zeros the masked_fill result stays bool, and scaled_dot_product_attention then reads it with the opposite bool semantics: a causal mask is inverted (max abs error 1.18 against PyTorch in float32), and a mask that blocks nothing becomes a -3e4 bias on every logit, which costs float16 softmax most of its precision. Read the keyword the same way ones_like already does. The existing test_zeros_like_types passed despite the bug because zeros compare equal across dtypes; the new tests use the result in arithmetic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Sep 23, 2026
Merged
Collaborator
|
This change looks good. CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2898092094 |
This branch has not been deployed
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.
Summary
torch.exportand ExecuTorch passzeros_like'sdtypeas a keyword argument. The torch frontend reads it only positionally (the TorchScript form), sotorch.zeros_like(x, dtype=...)keeps the dtype ofx.This matters most in
nn.MultiheadAttention, which turns a boolattn_maskinto a float mask withtorch.zeros_like(mask, dtype=q.dtype).masked_fill_(mask, float("-inf"))(F._canonical_mask). With bool zeros:masked_fillyields a bool tensor, because it casts the fill value to the tensor's dtype.scaled_dot_product_attentionthen treats that bool tensor with SDPA's bool semantics (True = attend), the opposite of the float mask it replaced.For a causal mask the converted model attends to exactly the wrong positions. For a mask that blocks nothing, every logit gets a −3e4 bias. That cancels in exact arithmetic but costs precision, and float16 softmax on the CPU loses most of it.
On
main(and 9.0, 9.1.dev1) this prints about2.6e-04and1.18. With this change it prints1.0e-07and1.2e-07. In float16, a detection transformer whose keypoint head uses such an all-False mask lost all its detections on the CPU, because the softmax inputs sat near −30000, where float16 values are 16 apart. It recovers with this change.zeros_likenow reads the keyword the same wayones_likealready does.Testing
TestZeros::test_zeros_like_dtype_from_booluses the result in arithmetic. The existingtest_zeros_like_typesalready exercises the keyword undertorch.export, but zeros compare equal across dtypes, so it passes without the fix.TestTransformer::test_multihead_attention_bool_attn_maskcovers a causal mask and one that blocks nothing, for thetorch.exportfrontends;torch.jit.tracefails its own sanity check onnn.MultiheadAttention.Both tests fail before the change (value mismatches of 6.0 and 0.63) and pass after it.