Preserve input dtype in torch floor_divide converter#2681
Open
john-rocky wants to merge 2 commits into
Open
Conversation
TobyRoseman
requested changes
May 5, 2026
Collaborator
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.
| compute_units, backends, frontends, [np.float32, np.int32] | ||
| ), | ||
| ) | ||
| def test_floor_divide(self, compute_unit, backend, frontend, dtype): |
Collaborator
There was a problem hiding this comment.
There probably a better place to put this unit test than under the TestActivation class.
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
Contributor
Author
|
Thanks for the catch — fixed in 99e2dcb. The original test only used |
3 tasks
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.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.