Update as strided doc (#149146)

Make it clearer why it is not recommended to use it and when the resulting Tensor will have undefined behavior.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/149146
Approved by: https://github.com/gchanan, https://github.com/jbschlosser
This commit is contained in:
albanD
2025-03-14 19:49:55 +00:00
committed by PyTorch MergeBot
parent 69aeb87eca
commit 1bdbf12672

View File

@ -918,13 +918,15 @@ Create a view of an existing `torch.Tensor` :attr:`input` with specified
:attr:`size`, :attr:`stride` and :attr:`storage_offset`.
.. warning::
Prefer using other view functions, like :meth:`torch.Tensor.expand`,
to setting a view's strides manually with `as_strided`, as this
function's behavior depends on the implementation of a tensor's storage.
The constructed view of the storage must only refer to elements within
the storage or a runtime error will be thrown, and if the view is
"overlapped" (with multiple indices referring to the same element in
memory) its behavior is undefined.
Prefer using other view functions, like :meth:`torch.Tensor.view` or
:meth:`torch.Tensor.expand`, to setting a view's strides manually with
`as_strided`, as this function will throw an error on non-standard Pytorch
backends (that do not have a concept of stride) and the result will depend
on the current layout in memory. The constructed view must only refer to
elements within the Tensor's storage or a runtime error will be thrown.
If the generated view is "overlapped" (with multiple indices referring to
the same element in memory), the behavior of inplace operations on this view
is undefined (and might not throw runtime errors).
Args:
{input}