Repository navigation
Match scalar constants to fp16 inputs in log2, log10, expm1, asinh, acosh, hardtanh and atan2 - #2894
Open
usmanmateen wants to merge 1 commit into
Open
usmanmateen wants to merge 1 commit into
usmanmateen wants to merge 1 commit into
Conversation
…cosh, hardtanh and atan2 These converters passed Python floats to MIL ops, which become fp32 constants. With an fp16 model exported with torch.export, the ops then mix fp16 and fp32 inputs and conversion fails with "y has dtype fp32 whereas x has dtype fp16". Build the constants in the input's dtype, and compute atan2 in fp32 for fp16 inputs since it uses many fp32 constants and casts.
Author
|
Hi @TobyRoseman, gentle ping on this when you have a moment. Happy to adjust anything. |
Author
|
@TobyRoseman, could you take a look at this when you have a moment? Happy to adjust anything. |
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this pull request
Nov 3, 2026
For an int input, torch returns floor, ceil and round of it unchanged and computes relu, relu6 and hardtanh in int. torch.nn.ReLU6 and torch.nn.Hardtanh export as hardtanh. coremltools passes the input on to the Core ML op, which takes only float, so lowering fails. For example, x.long().floor() fails with "expects tensor or scalar of dtype from type domain ['fp16', 'fp32'] but got tensor[2,3,int32]". hardtanh also fails on a float input with int bounds, which F.relu6(x, inplace=True) passes, or on a float16 input, since coremltools hands the bounds to Core ML's clip as they are, and clip needs them in the dtype of the input. The backend now converts these ops for an int input as torch does: the input itself for floor, ceil and round, and the input clipped in int for relu, relu6 and hardtanh. Torch drops the fraction of each hardtanh bound, so hardtanh(x, -1.5, 2.7) clips an int x at -1 and 2. For a float input, hardtanh passes its bounds to clip in the dtype of the input, as apple/coremltools#2894 does upstream, and the other ops keep the coremltools conversion. A narrower int, which Core ML has from iOS 17, still fails for relu, relu6 and hardtanh, since Core ML's maximum takes int32 but no narrower int. The new test lowers each op on an int input, and hardtanh with int bounds on a float16 input, checks that everything goes to Core ML, and on macOS compares the output with eager.
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this pull request
Nov 3, 2026
For an int input, torch returns floor, ceil and round of it unchanged and computes relu, relu6 and hardtanh in int. torch.nn.ReLU6 and torch.nn.Hardtanh export as hardtanh. coremltools passes the input on to the Core ML op, which takes only float, so lowering fails. For example, x.long().floor() fails with "expects tensor or scalar of dtype from type domain ['fp16', 'fp32'] but got tensor[2,3,int32]". hardtanh also fails on a float input with int bounds, which F.relu6(x, inplace=True) passes, or on a float16 input, since coremltools hands the bounds to Core ML's clip as they are, and clip needs them in the dtype of the input. It also fails on torch.arange(4, dtype=torch.float32), which coremltools makes int32. The backend now converts these ops for an int input as torch does: the input itself for floor, ceil and round, and the input clipped in int for relu, relu6 and hardtanh. Torch drops the fraction of each hardtanh bound, so hardtanh(x, -1.5, 2.7) clips an int x at -1 and 2. For a float input, hardtanh passes its bounds to clip in the dtype of the input, as apple/coremltools#2894 does upstream, and the other ops keep the coremltools conversion. hardtanh clips in the dtype torch gives its input, not in the one coremltools gives it, so hardtanh(torch.arange(4, dtype=torch.float32), 0.5, 2.5) clips at 0.5 and 2.5. A TorchScript node carries no dtype, so it keeps the coremltools converter. A narrower int, which Core ML has from iOS 17, still fails for relu and relu6, since Core ML's maximum takes int32 but no narrower int. hardtanh works there, since it casts such an input to int32 first. The new tests lower each op on an int input, hardtanh with int bounds on a float16 input, and hardtanh of a float arange, check that everything goes to Core ML, and on macOS compare the output with eager. The arange test also checks the bounds of the clip in the converted program, and converts a TorchScript hardtanh of a float input.
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this pull request
Nov 3, 2026
For an int input, torch returns floor, ceil and round of it unchanged and computes relu, relu6 and hardtanh in int. torch.nn.ReLU6 and torch.nn.Hardtanh export as hardtanh. coremltools passes the input on to the Core ML op, which takes only float, so lowering fails. For example, x.long().floor() fails with "expects tensor or scalar of dtype from type domain ['fp16', 'fp32'] but got tensor[2,3,int32]". hardtanh also fails on a float input with int bounds, which F.relu6(x, inplace=True) passes, or on a float16 input, since coremltools hands the bounds to Core ML's clip as they are, and clip needs them in the dtype of the input. It also fails on torch.arange(4, dtype=torch.float32), which coremltools makes int32. The backend now converts these ops for an int input as torch does: the input itself for floor, ceil and round, and the input clipped in int for relu, relu6 and hardtanh. Torch drops the fraction of each hardtanh bound, so hardtanh(x, -1.5, 2.7) clips an int x at -1 and 2. For a float input, hardtanh passes its bounds to clip in the dtype of the input, as apple/coremltools#2894 does upstream, and the other ops keep the coremltools conversion. hardtanh clips in the dtype torch gives its input, not in the one coremltools gives it, so hardtanh(torch.arange(4, dtype=torch.float32), 0.5, 2.5) clips at 0.5 and 2.5. A TorchScript node carries no dtype, so it keeps the coremltools converter. A narrower int, which Core ML has from iOS 17, still fails for relu and relu6, since Core ML's maximum takes int32 but no narrower int. hardtanh works there, since it casts such an input to int32 first. The new tests lower each op on an int input, hardtanh with int bounds on a float16 input, and hardtanh of a float arange, check that everything goes to Core ML, and on macOS compare the output with eager. The arange test also checks the bounds of the clip in the converted program, and converts a TorchScript hardtanh of a float input.
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this pull request
Nov 3, 2026
For an int input, torch returns floor, ceil and round of it unchanged and computes relu, relu6 and hardtanh in int. torch.nn.ReLU6 and torch.nn.Hardtanh export as hardtanh. coremltools passes the input on to the Core ML op, which takes only float, so lowering fails. For example, x.long().floor() fails with "expects tensor or scalar of dtype from type domain ['fp16', 'fp32'] but got tensor[2,3,int32]". hardtanh also fails on a float input with int bounds, which F.relu6(x, inplace=True) passes, or on a float16 input, since coremltools hands the bounds to Core ML's clip as they are, and clip needs them in the dtype of the input. It also fails on torch.arange(4, dtype=torch.float32), which coremltools makes int32. The backend now converts these ops for an int input as torch does: the input itself for floor, ceil and round, and the input clipped in int for relu, relu6 and hardtanh. Torch drops the fraction of each hardtanh bound, so hardtanh(x, -1.5, 2.7) clips an int x at -1 and 2. For a float input, hardtanh passes its bounds to clip in the dtype of the input, as apple/coremltools#2894 does upstream, and the other ops keep the coremltools conversion. hardtanh clips in the dtype torch gives its input, not in the one coremltools gives it, so hardtanh(torch.arange(4, dtype=torch.float32), 0.5, 2.5) clips at 0.5 and 2.5. A TorchScript node carries no dtype, so it keeps the coremltools converter. A narrower int, which Core ML has from iOS 17, still fails for relu and relu6, since Core ML's maximum takes int32 but no narrower int. hardtanh works there, since it casts such an input to int32 first. The new tests lower each op on an int input, hardtanh with int bounds on a float16 input, and hardtanh of a float arange, check that everything goes to Core ML, and on macOS compare the output with eager. The arange test also checks the bounds of the clip in the converted program, and converts a TorchScript hardtanh of a float input.
Author
|
@TobyRoseman gentle ping on this one when you have a moment. |
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.
Problem
log2,log10,expm1,asinh,acoshandhardtanhpass Python floats such as1 / np.log(2.0)or1.0straight into MIL ops, andatan2builds all of its constants as fp32. MIL turns a Python float into an fp32 constant. When the model is fp16 and goes throughtorch.export, the other input is fp16 and conversion fails:The same happens for
log10(mul),expm1(sub),asinh(add),acosh(sub),hardtanh(clip'salpha) andatan2(less). The TorchScript frontend converts these models fine, so this only shows up withtorch.export/ ExecuTorch, which is where fp16 models usually come from.Fix
log2,log10,expm1,asinh,acosh: build the scalar in the input's dtype withnptype_from_builtin(x.dtype), asisinf/isfinitealready do.hardtanh: castmin_val/max_valto the input's dtype.atan2: it creates many fp32 constants and casts the comparison masks to fp32, so it now casts fp16 inputs to fp32 (as it already did for integer inputs), computes in fp32, and casts the result back to fp16.fp32 models are unchanged: the constants have the same values and dtype as before.
Testing
Added
TestElementWiseUnary::test_elementwise_fp16(log2,log10,expm1,asinh,acosh),TestAtan2::test_atan2_fp16andTestActivation::test_hardtanh_fp16, each with an fp16 input andminimum_deployment_target=iOS16.The test suite only adds the
TORCHEXPORTfrontend on non-x86_64 machines, and I am on Linux x86_64. To run it, I disabled that check locally (not part of this PR) and setTORCH_FRONTENDS=TORCHSCRIPT,TORCHEXPORT. Prediction is skipped on Linux, so these runs check conversion. Your macOS CI will also run prediction.main, all 7TORCHEXPORTcases of the new tests fail with the error above, and theTORCHSCRIPTcases pass.neuralnetworkbackend does not support iOS16).-k "TestElementWiseUnary or TestAtan2 or hardtanh"with both frontends: 320 passed, 40 skipped, 0 failed.Tested with torch 2.14.1 on Python 3.12.