Skip to content

Match scalar constants to fp16 inputs in log2, log10, expm1, asinh, acosh, hardtanh and atan2 - #2894

Open
usmanmateen wants to merge 1 commit into
apple:mainfrom
usmanmateen:fix/fp16-scalar-constants
Open

usmanmateen wants to merge 1 commit into
apple:mainfrom
usmanmateen:fix/fp16-scalar-constants

Conversation

@usmanmateen

Copy link
Copy Markdown

Problem

log2, log10, expm1, asinh, acosh and hardtanh pass Python floats such as 1 / np.log(2.0) or 1.0 straight into MIL ops, and atan2 builds all of its constants as fp32. MIL turns a Python float into an fp32 constant. When the model is fp16 and goes through torch.export, the other input is fp16 and conversion fails:

import coremltools as ct
import torch


class Model(torch.nn.Module):
    def forward(self, x):
        return torch.log2(x)


x = torch.tensor([0.5, 1.5, 2.0, 3.0], dtype=torch.float16)
ep = torch.export.export(Model(), (x,)).run_decompositions({})
ct.convert(ep, minimum_deployment_target=ct.target.iOS17)
ValueError: In op, of type mul, named mul_1, the named input `y` must have the same data type as the named input `x`. However, y has dtype fp32 whereas x has dtype fp16.

The same happens for log10 (mul), expm1 (sub), asinh (add), acosh (sub), hardtanh (clip's alpha) and atan2 (less). The TorchScript frontend converts these models fine, so this only shows up with torch.export / ExecuTorch, which is where fp16 models usually come from.

Fix

  • log2, log10, expm1, asinh, acosh: build the scalar in the input's dtype with nptype_from_builtin(x.dtype), as isinf / isfinite already do.
  • hardtanh: cast min_val / max_val to 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_fp16 and TestActivation::test_hardtanh_fp16, each with an fp16 input and minimum_deployment_target=iOS16.

The test suite only adds the TORCHEXPORT frontend 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 set TORCH_FRONTENDS=TORCHSCRIPT,TORCHEXPORT. Prediction is skipped on Linux, so these runs check conversion. Your macOS CI will also run prediction.

  • On main, all 7 TORCHEXPORT cases of the new tests fail with the error above, and the TORCHSCRIPT cases pass.
  • With this change, the new tests pass: 14 passed, 14 skipped (the neuralnetwork backend 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.

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

Copy link
Copy Markdown
Author

Hi @TobyRoseman, gentle ping on this when you have a moment. Happy to adjust anything.

@usmanmateen

Copy link
Copy Markdown
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.
@usmanmateen

Copy link
Copy Markdown
Author

@TobyRoseman gentle ping on this one when you have a moment.

This branch has not been deployed

No deployments
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.

1 participant