torch.div with rounding_mode returns float for integer inputs (should be int)
🐞 Describe the bug
When converting torch.div(a, b, rounding_mode="floor") (or "trunc") where both operands are integers, the converted ML Program returns a float32 output. PyTorch returns an integer in this case (a rounding mode preserves the integer dtype; only true division returns a float).
The PyTorch frontend converter for div casts both operands to fp32 up front and never casts the rounded result back, so the integer dtype is lost. This is a separate code path from torch.floor_divide (issue #2570 / PR #2681), which only touches the floor_divide converter.
To Reproduce
import numpy as np, torch, torch.nn as nn
import coremltools as ct
class M(nn.Module):
def forward(self, a, b):
return torch.div(a, b, rounding_mode="floor")
x1 = torch.tensor([10, -10, 7, -7], dtype=torch.int32)
x2 = torch.tensor([3, 3, 2, 2], dtype=torch.int32)
print("torch:", torch.div(x1, x2, rounding_mode="floor").dtype) # torch.int32
ts = torch.jit.trace(M().eval(), (x1, x2))
ml = ct.convert(ts,
inputs=[ct.TensorType(name="a", shape=(4,), dtype=np.int32),
ct.TensorType(name="b", shape=(4,), dtype=np.int32)],
convert_to="mlprogram", minimum_deployment_target=ct.target.iOS17)
out = ml.predict({"a": x1.numpy(), "b": x2.numpy()})
print("coreml:", list(out.values())[0].dtype) # float32 <-- should be int32
torch: torch.int32
coreml: float32
Same for rounding_mode="trunc". Without a rounding mode (true division) a float result is correct and expected.
System environment
- coremltools version: 9.0
- OS: macOS
- Source: PyTorch (torch.jit.trace and torch.export both affected)
Additional context
The numeric values are correct; only the dtype is wrong (an integer quotient is representable as a float too), so it slips past numerical-only tests. Happy to submit a PR — the fix is to remember whether both operands are integers before the fp32 cast and cast the rounded result back to int32.