Skip to content

Python scalars should be promoted to the same dtype as the respective tensor #59868

@pmeier

Description

@pmeier

The array API specification stipulates that for binary operations involving a tensor and a Python scalar, the scalar needs to be converted to the same dtype as the tensor before the operation is performed. PyTorch casts the scalar to a tensor based on its dtype and afterwards performs the default type promotion for the operator.

This can lead to overflows if the tensor dtype can hold the values of the scalar, but the automatically determined dtype cannot.

>>> torch.tensor(0, dtype=torch.float32) + 2.0 ** 63
tensor(9.2234e+18)
>>> torch.tensor(0, dtype=torch.float32) + 2 ** 63
RuntimeError: Overflow when unpacking long

cc @mruberry @rgommers @pmeier @asmeurer @leofang @nairbv

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: python array apiIssues related to the Python Array APImodule: type promotionRelated to semantics of type promotionneeds designWe want to add this feature but we need to figure out how firsttriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions