Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type promotion for pow. #6745

Merged
merged 3 commits into from
Mar 18, 2024
Merged

Conversation

ysiraichi
Copy link
Collaborator

Fix: #6521

This PR fixes how PyTorch/XLA promotes types for pow function. It does so by resolving the resulting type of the operation by calling at::result_type (similar to how add is implemented in PyTorch/XLA). This aligns the implementation with PyTorch's. See the example below:

def foo(a):
    return torch.pow(a, 3.0)

a = torch.arange(10).to(torch.bfloat16)

print(foo(a))
# tensor([  0.,   1.,   8.,  27.,  64., 125., 216., 344., 512., 728.],
#        dtype=torch.bfloat16)

print(foo(a.to(xm.xla_device())))
# tensor([  0.,   1.,   8.,  27.,  64., 125., 216., 343., 512., 729.],
#        device='xla:0')

cc @miladm @JackCaoG

@ysiraichi
Copy link
Collaborator Author

I will add a test case for this, later.

Comment on lines -2184 to -2191
// Float scalar literal in Python defaults to F64. But we want to produce
// F32 as this is the default Pytorch behavior.
if (xla_type == xla::PrimitiveType::F64) {
xla_type = xla::PrimitiveType::F32;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @qihqi added this downcasting rule and you removed it in this pr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this anymore, since at::result_type resolves to the appropriate type, as we can observe with the tests.

Copy link
Collaborator

@JackCaoG JackCaoG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test to test/test_operation.py for the result type?

@ysiraichi ysiraichi requested a review from qihqi March 14, 2024 12:12
@JackCaoG
Copy link
Collaborator

not sure why CI is not running...

@ysiraichi
Copy link
Collaborator Author

Maybe due to merge conflicts? I will try rebasing.

@ysiraichi ysiraichi force-pushed the ysiraichi/fix-pow-type-promotion branch from 5c9d1df to aba3534 Compare March 15, 2024 12:22
@JackCaoG JackCaoG merged commit 902aa50 into master Mar 18, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[torchbench] hf_GPT2 (large, too) fails to run on bfloat16 dtype.
2 participants