Skip to content

Array's dtype asarray(np_float32_array, device=Device("device1")) #222

Description

@betatim

Another change between 2.5 and 2.6. In 2.6 the dtype of an array can be changed when you array_api_strict.asarray(x, device=Device("device1"))

With v2.5 this snippet prints array_api_strict.float32, with v2.6 you get float64.

import numpy as np
import array_api_strict as xp

x = np.ones((2,3), dtype=np.float32)
y = xp.asarray(x, device=xp.Device("device1"))

print(y.dtype)

I think preserving the dtype (if it is supported on the target device) is what should happen (aka behaviour of v2.5). I think the default dtype is something to look at for functions like ones() and friends. For those there is no hint from the user what the type should be.

A fix could be to use:

    # numpy default dtype may differ; if so, adjust the dtype.
    # Only do this for inputs whose dtype was inferred by NumPy (Python
    # scalars / sequences). Inputs that carry their own dtype (arrays and
    # buffer-protocol objects) must have that dtype preserved.
    if dtype is None and device is not None and not _supports_buffer_protocol(obj):
        res_dtype = DType(res.dtype)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions