Skip to content

Bitwise shifts should retain the same dtype as the first input #59867

@pmeier

Description

@pmeier

The array API specification stipulates that bitwise_left_shift and bitwise_left_shift (which don't exist yet (see #58742) but their magics __lshift__ and __rshift__ do) do not participate in the default type promotion, but rather retain the dtype of the first input. PyTorch performs the standard type promotion.

>>> x1 = torch.tensor([0, 1], dtype=torch.uint8)
>>> x2 = torch.tensor([0, 1], dtype=torch.int8)
>>> (x1 << x2).dtype, x1.dtype
(torch.int16, torch.uint8)
>>> (x2 << x1).dtype, x2.dtype
(torch.int16, torch.int8)
>>> (x1 >> x2).dtype, x1.dtype
(torch.int16, torch.uint8)
>>> (x2 >> x1).dtype, x2.dtype
(torch.int16, torch.int8)

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