Skip to content

Commit

Permalink
asarray: take the default device into consideration.
Browse files Browse the repository at this point in the history
Fix: #106773

This PR makes it so `asarray` takes the default device into consideration when called with
a Python sequence as the data.

ghstack-source-id: 92adeacccc0cfadb8268996a2e8a784c254debed
Pull Request resolved: #106779
  • Loading branch information
ysiraichi committed Aug 8, 2023
1 parent 03c9321 commit ae39a87
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions test/test_tensor_creation_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3966,6 +3966,11 @@ def test_numpy_scalars(self, device):
self.assertEqual(tensor.item(), zerodim_arr.item())
self.assertEqual(tensor.dtype, torch.int32)

def test_default_device(self, device):
with torch.device(device):
tensor = torch.asarray(3)
self.assertEqual(tensor.device.type, device)


instantiate_device_type_tests(TestTensorCreation, globals())
instantiate_device_type_tests(TestRandomTensorCreation, globals())
Expand Down
5 changes: 3 additions & 2 deletions torch/_torch_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ def merge_dicts(*dicts):
When :attr:`obj` is none of the above but a scalar, or a sequence of scalars then the
returned tensor will, by default, infer its datatype from the scalar values, be on the
CPU device, and not share its memory.
current default device, and not share its memory.
.. seealso::
Expand All @@ -1282,7 +1282,8 @@ def merge_dicts(*dicts):
If ``False`` then the returned tensor shares its memory with :attr:`obj` and an
error is thrown if it cannot.
device (:class:`torch.device`, optional): the device of the returned tensor.
Default: ``None``, which causes the device of :attr:`obj` to be used.
Default: ``None``, which causes the device of :attr:`obj` to be used. Or, if
:attr:`obj` is a Python sequence, the current default device will be used.
requires_grad (bool, optional): whether the returned tensor requires grad.
Default: ``False``, which causes the returned tensor not to require a gradient.
If ``True``, then the returned tensor will require a gradient, and if :attr:`obj`
Expand Down
8 changes: 7 additions & 1 deletion torch/csrc/utils/tensor_new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1633,8 +1633,14 @@ Tensor asarray(
bool force_alias = !copy.value_or(true);
bool should_warn_numpy_not_writable = false;

// Used when:
// 1. 'obj' implements the buffer protocol and no type is given.
// 2. creating a new tensor from a Python sequence.
auto dtype_unwrapped =
dtype.value_or(torch::tensors::get_default_scalar_type());
// Used when creating a new tensor from a Python sequence.
auto device_unwrapped =
device.value_or(torch::tensors::get_default_device());

// Check whether 'obj' is a 'Tensor'
if (THPVariable_Check(obj)) {
Expand Down Expand Up @@ -1753,7 +1759,7 @@ Tensor asarray(
tensor = internal_new_from_data(
TensorOptions(),
dtype_unwrapped,
device,
device_unwrapped,
obj,
/* copy_variables = */ false,
/* copy_numpy = */ false,
Expand Down

0 comments on commit ae39a87

Please sign in to comment.