8000 ENH: Add a force argument to `numpy()` by HaoZeke · Pull Request #78564 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add a force argument to numpy() #78564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cleaning of code
  • Loading branch information
martinPasen authored and HaoZeke committed May 29, 2022
commit f30497f040b051b81cdfc1a7249335eae7f801af
4 changes: 2 additions & 2 deletions test/test_numpy_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def test_to_numpy_force_argument(self, device) -> None:
for requires_grad in [False, True]:
for sparse in [False, True]:
for conj in [False, True]:
data = [[1+2j, -2+3j], [-1-2j, 3-2j]]
x = torch.tensor(data, requires_grad = requires_grad, device = device)
data = [[1 + 2j, -2 + 3j], [-1 - 2j, 3 - 2j]]
x = torch.tensor(data, requires_grad=requires_grad, device=device)
y = x
if sparse:
if requires_grad:
Expand Down
4 changes: 2 additions & 2 deletions tools/autograd/templates/python_variable_methods.cpp
CF8B
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,11 @@ static PyObject * THPVariable_numpy(PyObject* self, PyObject* args, PyObject* kw
auto& self_ = THPVariable_Unpack(self);
ParsedArgs<1> parsed_args;
auto r = parser.parse(self, args, kwargs, parsed_args);

if (r.has_torch_function()) {
return handle_torch_function(r, self, args, kwargs, THPVariableClass, "torch.Tensor");
}

jit::tracer::warn("Converting a tensor to a NumPy array", jit::tracer::WARN_PYTHON_DATAFLOW);
return torch::utils::tensor_to_numpy(self_, r.toBool(0));
END_HANDLE_TH_ERRORS
Expand Down
8 changes: 4 additions & 4 deletions torch/_tensor_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2848,15 +2848,15 @@ def callable(a, b) -> number
If :attr:`force` is ``False``, tensor :attr:`self` and the
returned :class:`ndarray` share the same underlying storage. Changes to
:attr:`self` tensor will be reflected in the :class:`ndarray` and vice versa.
If tensors storage is not CPU, or if :attr:`requires_grad` is ``True`` and
:attr:`GradMode` is ``True``, or tensor is sprase, or :attr:`is_conj()`
If tensors storage is not CPU, or if :attr:`requires_grad` is ``True`` and
:attr:`GradMode` is ``True``, or tensor is sprase, or :attr:`is_conj()`
is ``True``, it throws exception.

If :attr:`force` is ``True`` it tries to resolve issues, that would throw exception.
It calls :func:`torch.Tensor.detach`, :func:`torch.Tensor.cpu` and if :attr:`is_conj()`
It calls :func:`torch.Tensor.detach`, :func:`torch.Tensor.cpu` and if :attr:`is_conj()`
is ``True`` it calls :func:`torch.Tensor.resolve_conj`.
Therefore tensor :attr:`self` and the returned :class:`ndarray` can have separate
underlying storage and changes to :attr:`self` may not be reflected
underlying storage and changes to :attr:`self` may not be reflected
in the :class:`ndarray` and vice versa.

Args:
Expand Down
0