Skip to content

Lower float zeros_like with dynamic shape as fill instead of x minus x - #2936

Open
SharvenRane wants to merge 1 commit into
apple:mainfrom
SharvenRane:fix-zeros-like-nonfinite
Open

SharvenRane wants to merge 1 commit into
apple:mainfrom
SharvenRane:fix-zeros-like-nonfinite

Conversation

@SharvenRane

Copy link
Copy Markdown

With a dynamic input shape, torch.zeros_like(x) converts to sub(x, x). That is NaN wherever x is inf or NaN, while PyTorch returns 0. For example, torch.zeros_like(x) + 1 on [inf, -inf, nan, 1] gives [nan, nan, nan, 1] instead of all ones. Both the TorchScript and torch.export frontends produce this.

This change emits fill with 0 for float inputs. Integer inputs keep sub, which is exact there, and bool keeps the existing path.

Test: TestZeros::test_zeros_like_dynamic_non_finite_input asserts the program has no sub op and, where prediction runs, compares against PyTorch on inf and NaN inputs. It fails before this change.

Note: #2865 touches the same function to read the dtype keyword. The two changes are independent but may need a small rebase against each other.

x - x is nan where x is inf or nan, while torch.zeros_like gives 0.

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