Preserve input dtype in torch floor_divide converter#2681
Conversation
TobyRoseman
left a comment
There was a problem hiding this comment.
Something isn't right here. Your new unit test passes even without your changes to ops.py.
PyTorch's `torch.floor_divide` returns the input dtype (int for int inputs, float for float inputs), but the converter unconditionally cast the result to fp32. The misleading inline comment claimed PyTorch "always returns fp32, even if the inputs are int" — this is not true. Drop the unconditional `mb.cast(..., dtype='fp32')`; `mb.floor_div` already produces an output of the promoted input dtype. Fixes apple#2570.
The previous test only compared values via run_compare_torch, which passed even without the ops.py change because [10/3, -10/3, 7/2, -7/2] = [3, -4, 3, -4] is representable identically in int32 and fp32. Inspect the MIL main function output var's dtype directly so the test truly pins the dtype-preserving behavior the PR claims.
2b82908 to
99e2dcb
Compare
|
Thanks for the catch — fixed in 99e2dcb. The original test only used |
Per @TobyRoseman's review on apple#2681: the new floor_divide regression test was sitting under `TestActivation`, which is a poor fit. Move it into its own `TestFloorDivide(TorchBaseTest)` class, placed between the trailing `test_div` of `TestActivation` and `TestElementWiseUnary`. No behavior change — same parametrization, same assertions. AST-verified: `test_floor_divide` is now inside `TestFloorDivide` at line 6686.
|
Moved `test_floor_divide` into its own `TestFloorDivide(TorchBaseTest)` class in `ae07088` per your earlier review point on placement. AST-verified the method is now under `TestFloorDivide` rather than `TestActivation`; no behavior change. |
|
Thanks @TobyRoseman — both points addressed:
Verified locally that int inputs now yield an int32 model output where the old converter emitted fp32. |
| input_as_shape=False, | ||
| expected_results=out, | ||
| ) | ||
| # Pin the output dtype. A numerical comparison alone passes even |
There was a problem hiding this comment.
Rather than access underscore/private members to inspect the MIL type, let's just get predictions from the converted model and verify they are ints rather than floats. It's simpler and more directly matches the user experience.
|
Done — switched to checking the prediction's dtype instead of the MIL program internals (the One wrinkle worth noting: the NeuralNetwork backend always serializes outputs as float, so the int-vs-float distinction is only observable on the ML Program backend — I guard the integer assertion to Verified locally: every |
|
Updated CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2553826151 Assuming this passes, I will merge. |
Summary
torch.floor_dividereturns the input dtype (int for int inputs, float for float inputs), but the converter unconditionally cast the result to fp32. The misleading inline comment claimed PyTorch "always returns fp32, even if the inputs are int" — this is not true.mb.cast(..., dtype='fp32');mb.floor_divalready produces an output of the promoted input dtype.Test plan
pytest test_torch_ops.py::TestActivation::test_floor_divide— 8 cases across int32/float32 inputs, both backends.Fixes #2570.