Skip to content

to_device should not copy when the operation is a no op #645

Closed
@asmeurer

Description

@asmeurer

The spec for to_device() says:

Copy the array from the device on which it currently resides to the specified device.

It doesn't say anything about the case where the given array is already on the specified device.

Note that PyTorch's to has a copy flag, which is False by default, https://pytorch.org/docs/stable/generated/torch.Tensor.to.html, which prevents copying when the device is the same. to_device should probably behave the same. We may or may not want to add a copy flag to to_device as well (but note that you can control this explicitly using asarray).

This came up thinking about this code in scikit-learn https://github.com/scikit-learn/scikit-learn/pull/26315/files/42524bd42900d8ea5f4a334780387a72c6f9580d#diff-86c94a3ca33490c6190f488f5d40b01bf0fd29be36da0b4497ef0da1fda4148a.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions