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
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
documentation refactor
  • Loading branch information
martinPasen authored and HaoZeke committed May 29, 2022
commit c5b51ae99427ee1bfa1144b8dcf3ce010d833d36
31 changes: 15 additions & 16 deletions torch/_tensor_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2843,24 +2843,23 @@ def callable(a, b) -> number
r"""
numpy(*, force=False) -> numpy.ndarray

Returns :attr:`self` tensor as a NumPy :class:`ndarray`.

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 sparse, or :attr:`is_conj()`
is ``True``, it throws exception.

If :attr:`force` is ``True`` it tries to resolve issues that would otherwise throw an exception.
It calls :func:`torch.Tensor.detach`, :func:`torch.Tensor.cpu` and :func:`torch.Tensor.resolve_conj`.
If tensor is sparse, it still throws an exception. Since it makes a copy in some scenarios,
tensor :attr:`self` and the returned :class:`ndarray` can have separate
underlying storage and changes to :attr:`self` may not be reflected
in the :class:`ndarray` and vice versa.
Returns the tensor as a NumPy :class:`ndarray`.

If :attr:`force` is ``False`` (the default), the conversion
is performed only if the tensor is on the CPU, does not require grad,
does not have its conjugate bit set, and is a dtype and layout that
NumPy supports. The returned ndarray and the tensor will share their
storage, so changes to the tensor will be reflected in the ndarray
and vice versa.

If :attr:`force` is ``True`` this is equivalent to
calling ``t.detach().cpu().resolve_conj().numpy()``. If the tensor
isn't on the CPU or its conjugate bit is set, the tensor won't share
its storage with the returned ndarray. Setting :attr:`force` to
``True`` can be a useful shorthand.

Args:
force (bool): whether to copy/change :attr:`self` or throw exception
force (bool): if ``True``, it allows the ndarray to be a copy of the tensor instead of always sharing its memory, defaults to ``False``.
""")

add_docstr_all('orgqr',
Expand Down
0