Cast int input to fp32 in torch reciprocal converter#2683
Open
john-rocky wants to merge 1 commit intoapple:mainfrom
Open
Cast int input to fp32 in torch reciprocal converter#2683john-rocky wants to merge 1 commit intoapple:mainfrom
john-rocky wants to merge 1 commit intoapple:mainfrom
Conversation
`torch.reciprocal` returns a float for int inputs in PyTorch, but `mb.inverse` only accepts fp16/fp32. As a result, common patterns like `1 / x.shape[0]` (which TorchScript traces as `reciprocal(prim::NumToTensor(int))`) failed conversion with: Op (op_type: inverse) Input x expects tensor or scalar of dtype from type domain ['fp16', 'fp32'] but got tensor[1, int32] Insert a fp32 cast before `mb.inverse` when the input dtype is integer, mirroring the pattern already used by `log`, `sqrt`, and other unary ops that share the same MIL constraint. Verified end-to-end on the issue repro and a representative RoPE-style inverse-frequency expression. Fixes apple#2579.
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.reciprocalreturns a float for int inputs in PyTorch, butmb.inverseonly accepts fp16/fp32. Common patterns like1 / x.shape[0](which TorchScript traces asreciprocal(prim::NumToTensor(int))) failed to convert with:Insert a fp32 cast before
mb.inversewhen the input dtype is integer, mirroring the pattern already used bylog,sqrt, and other unary ops that share the same MIL constraint.Test plan
pytest test_torch_ops.py::TestReciprocal::test_reciprocal_int_shape— TorchScript path; the torch.export path folds the shape-derived constant and is skipped.1 / theta**(2i/d)expressions, verified separately.Fixes #2579.