Skip to content

Type promotion is wrong in some cases for tensor - scalar operations #56

@sbrunk

Description

@sbrunk

The Promoted type can cause a mismatch with the runtime promotion in certain cases when combining tensors with scalar values.

The reason is, that the type promotion , somewhat unintuitively, does not promote scalars the same way as tensors, but only to the next category. So if we combine a float32 tensor with a double/float64 scalar, the result will be a float32 tensor:

import torch.*

Tensor(1f) + Tensor(1d)
// res1: Tensor[Float64] = tensor dtype=float64, shape=[], device=CPU 
// 2.0000

Tensor(1f) + 1d
// res2: Tensor[Float64] = tensor dtype=float32, shape=[], device=CPU 
// 2.0000
1f + Tensor(1d)
// res3: Tensor[Float64] = tensor dtype=float64, shape=[], device=CPU 
// 2.0000
Tensor(1) + 1d
// res4: Tensor[Float64] = tensor dtype=float32, shape=[], device=CPU 
// 2.0000

More details are in the type promotion docs. So we need a diffrent Promoted type for tensor/scalar ops, taking that into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions