Beispiel #1
0
    def grad(self):
        """
        This attribute is ``None`` by default and becomes a Tensor the first time a call to
        :func:`backward` computes gradients for ``self``.
        The attribute will then contain the gradients computed and future calls to
        :func:`backward` will accumulate (add) gradients into it.
        """
        if has_torch_function_unary(self):
            # TODO mypy doesn't support @property, see: https://github.com/python/mypy/issues/6185
            return handle_torch_function(Tensor.grad.__get__, (self, ),
                                         self)  # type: ignore[attr-defined]

        if self.requires_grad and not hasattr(
                self,
                "retains_grad") and not self.is_leaf and self._grad is None:
            warnings.warn(
                "The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad "
                "attribute won't be populated during autograd.backward(). If you indeed want the gradient "
                "for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the "
                "non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See "
                "github.com/pytorch/pytorch/pull/30531 for more informations.",
                stacklevel=2)
        return self._grad
Beispiel #2
0
    def lu(self, pivot=True, get_infos=False):
        r"""See :func:`torch.lu`"""
        # If get_infos is True, then we don't need to check for errors and vice versa
        if has_torch_function_unary(self):
            return handle_torch_function(Tensor.lu, (self, ),
                                         self,
                                         pivot=pivot,
                                         get_infos=get_infos)

        if not torch._jit_internal.is_scripting():
            if self.requires_grad:
                if not (self.size(-2) == self.size(-1) and
                        (self.dtype.is_floating_point) or self.is_complex):
                    raise ValueError(
                        'lu.backward works only with batches of squared full-rank matrices'
                        ' of floating or complex types.')

                from torch._autograd_functions import _LU
                LU, pivots, infos = _LU.apply(self, pivot, get_infos)
                if get_infos:
                    return LU, pivots, infos
                else:
                    return LU, pivots
        else:
            if self.requires_grad:
                raise RuntimeError(
                    'Script and require gradients is not supported at the moment.'
                    'If you just want to do the forward, use .detach()'
                    'on the input before calling the function.')

        LU, pivots, infos = torch._lu_with_info(self,
                                                pivot=pivot,
                                                check_errors=(not get_infos))
        if get_infos:
            return LU, pivots, infos
        else:
            return LU, pivots
Beispiel #3
0
    def unflatten(self, dim, sizes):
        r"""Expands the dimension :attr:`dim` of the :attr:`self` tensor over multiple dimensions
        of sizes given by :attr:`sizes`.

        * :attr:`sizes` is the new shape of the unflattened dimension and it can be a `Tuple[int]` as well
          as `torch.Size` if :attr:`self` is a `Tensor`, or `namedshape` (Tuple[(name: str, size: int)])
          if :attr:`self` is a `NamedTensor`. The total number of elements in sizes must match the number
          of elements in the original dim being unflattened.

        Args:
            dim (Union[int, str]): Dimension to unflatten
            sizes (Union[Tuple[int] or torch.Size, Tuple[Tuple[str, int]]]): New shape of the unflattened dimension

        Examples:
            >>> torch.randn(3, 4, 1).unflatten(1, (2, 2)).shape
            torch.Size([3, 2, 2, 1])
            >>> torch.randn(2, 4, names=('A', 'B')).unflatten('B', (('B1', 2), ('B2', 2)))
            tensor([[[-1.1772,  0.0180],
                    [ 0.2412,  0.1431]],

                    [[-1.1819, -0.8899],
                    [ 1.5813,  0.2274]]], names=('A', 'B1', 'B2'))

        .. warning::
            The named tensor API is experimental and subject to change.
        """
        if has_torch_function_unary(self):
            return handle_torch_function(Tensor.unflatten, (self,), self, dim, sizes)

        if not sizes:
            raise RuntimeError("unflatten: sizes must be non-empty")

        names = None
        if isinstance(sizes, OrderedDict) or (isinstance(sizes, (tuple, list)) and isinstance(sizes[0], (tuple, list))):
            names, sizes = unzip_namedshape(sizes)
        return super(Tensor, self).unflatten(dim, sizes, names)
Beispiel #4
0
    def __cuda_array_interface__(self):
        """Array view description for cuda tensors.

        See:
        https://numba.pydata.org/numba-doc/latest/cuda/cuda_array_interface.html
        """
        if has_torch_function_unary(self):
            # TODO mypy doesn't support @property, see: https://github.com/python/mypy/issues/6185
            return handle_torch_function(
                Tensor.__cuda_array_interface__.__get__, (self, ),
                self)  # type: ignore[attr-defined]

        # raise AttributeError for unsupported tensors, so that
        # hasattr(cpu_tensor, "__cuda_array_interface__") is False.
        if not self.is_cuda:
            raise AttributeError(
                "Can't get __cuda_array_interface__ on non-CUDA tensor type: %s "
                "If CUDA data is required use tensor.cuda() to copy tensor to device memory."
                % self.type())

        if self.is_sparse:
            raise AttributeError(
                "Can't get __cuda_array_interface__ on sparse type: %s "
                "Use Tensor.to_dense() to convert to a dense tensor first." %
                self.type())

        # RuntimeError, matching tensor.__array__() behavior.
        if self.requires_grad:
            raise RuntimeError(
                "Can't get __cuda_array_interface__ on Variable that requires grad. "
                "If gradients aren't required, use var.detach() to get Variable that doesn't require grad."
            )

        # CUDA devices are little-endian and tensors are stored in native byte
        # order. 1-byte entries are endian-agnostic.
        typestr = {
            torch.complex64: "<c8",
            torch.complex128: "<c16",
            torch.float16: "<f2",
            torch.float32: "<f4",
            torch.float64: "<f8",
            torch.uint8: "|u1",
            torch.int8: "|i1",
            torch.int16: "<i2",
            torch.int32: "<i4",
            torch.int64: "<i8",
        }[self.dtype]

        itemsize = self.storage().element_size()

        shape = tuple(self.shape)
        if self.is_contiguous():
            # __cuda_array_interface__ v2 requires the strides to be omitted
            # (either not set or set to None) for C-contiguous arrays.
            strides = None
        else:
            strides = tuple(s * itemsize for s in self.stride())
        data_ptr = self.data_ptr() if self.numel() > 0 else 0
        data = (data_ptr, False)  # read-only is false

        return dict(typestr=typestr,
                    shape=shape,
                    strides=strides,
                    data=data,
                    version=2)
Beispiel #5
0
 def __hash__(self):
     if has_torch_function_unary(self):
         return handle_torch_function(Tensor.__hash__, (self, ), self)
     return id(self)
Beispiel #6
0
 def __len__(self):
     if has_torch_function_unary(self):
         return handle_torch_function(Tensor.__len__, (self, ), self)
     if self.dim() == 0:
         raise TypeError("len() of a 0-d tensor")
     return self.shape[0]
Beispiel #7
0
 def resize(self, *sizes):
     if has_torch_function_unary(self):
         return handle_torch_function(Tensor.resize, (self, ), self, *sizes)
     warnings.warn("non-inplace resize is deprecated")
     from torch.autograd._functions import Resize
     return Resize.apply(self, sizes)
Beispiel #8
0
 def __repr__(self):
     if has_torch_function_unary(self):
         return handle_torch_function(Tensor.__repr__, (self, ), self)
     # All strings are unicode in Python 3.
     return torch._tensor_str._str(self)
Beispiel #9
0
 def _reduce_ex_internal(self, proto):
     if has_torch_function_unary(self):
         return handle_torch_function(Tensor.__reduce_ex__, (self, ), self,
                                      proto)
     check_serializing_named_tensor(self)
     # See Note [Don't serialize hooks]
     torch.utils.hooks.warn_if_has_hooks(self)
     backward_hooks: Dict[Any, Any] = OrderedDict()
     # Note: Numpy array is chosen to be the rebuild component for XLA Tensor.
     # We considered a few options:
     # 1. CPU tensor can't be used here.
     #    Otherwise in torch.load CPU storage is reconstructed with randomly
     #    initialized data, moved onto XLA device, and then storage is updated
     #    to the serialized content. This works perfectly for CPU/CUDA but not XLA.
     #    XLA tensor is disconnected with storage so it doesn't get the update.
     # 2. Python list is not a good fit due to performance reason.
     #    `tolist()` converts every single element in the tensor into python objects
     #    and serialize them one by one.
     if self.device.type == 'xla':
         arg_xla = (self.cpu().numpy(), self.dtype, str(self.device),
                    self.requires_grad)
         return (torch._utils._rebuild_xla_tensor, arg_xla)
     if self.device.type == 'mlc':
         arg_mlc = (self.cpu().numpy(), self.dtype, str(self.device),
                    self.requires_grad)
         return (torch._utils._rebuild_mlc_tensor, arg_mlc)
     if self.is_quantized:
         # quantizer_params can be different type based on torch attribute
         quantizer_params: Union[Tuple[torch.qscheme, float, int],
                                 Tuple[Any, Tensor, Tensor, int]]
         if self.qscheme() == torch.per_tensor_affine:
             quantizer_params = (torch.per_tensor_affine, self.q_scale(),
                                 self.q_zero_point())
         elif self.qscheme() in (torch.per_channel_affine,
                                 torch.per_channel_affine_float_qparams):
             # convert scales and zero points to tuple to avoid recursive calls
             # when/if we get multi-axis quantized tensors in the future, the shape
             # is recoverable from the main tensor shape
             quantizer_params = (torch.per_channel_affine,
                                 self.q_per_channel_scales(),
                                 self.q_per_channel_zero_points(),
                                 self.q_per_channel_axis())
         else:
             raise RuntimeError(
                 f"Serialization is not supported for tensors of type {self.qscheme()}"
             )
         args_qtensor = (self.storage(), self.storage_offset(),
                         tuple(self.size()), self.stride(),
                         quantizer_params, self.requires_grad,
                         backward_hooks)
         return (torch._utils._rebuild_qtensor, args_qtensor)
     elif self.is_sparse:
         if self.layout == torch.sparse_coo:
             args_sparse = (self.layout, (self._indices(), self._values(),
                                          self.size()))
         else:
             raise NotImplementedError(
                 'sparse tensor __reduce_ex__ for layout `%s`' %
                 (self.layout))
         return (torch._utils._rebuild_sparse_tensor, args_sparse)
     else:
         args = (self.storage(), self.storage_offset(), tuple(self.size()),
                 self.stride(), self.requires_grad, backward_hooks
                 )  # previously was self._backward_hooks
         return (torch._utils._rebuild_tensor_v2, args)
Beispiel #10
0
 def grad(self):
     if has_torch_function_unary(self):
         # TODO mypy doesn't support @property, see: https://github.com/python/mypy/issues/6185
         return handle_torch_function(Tensor.grad.__delete__, (self, ),
                                      self)  # type: ignore[attr-defined]
     del self._grad
Beispiel #11
0
    def backward(self,
                 gradient=None,
                 retain_graph=None,
                 create_graph=False,
                 inputs=None):
        r"""Computes the gradient of current tensor w.r.t. graph leaves.

        The graph is differentiated using the chain rule. If the tensor is
        non-scalar (i.e. its data has more than one element) and requires
        gradient, the function additionally requires specifying ``gradient``.
        It should be a tensor of matching type and location, that contains
        the gradient of the differentiated function w.r.t. ``self``.

        This function accumulates gradients in the leaves - you might need to zero
        ``.grad`` attributes or set them to ``None`` before calling it.
        See :ref:`Default gradient layouts<default-grad-layouts>`
        for details on the memory layout of accumulated gradients.

        .. note::

            If you run any forward ops, create ``gradient``, and/or call ``backward``
            in a user-specified CUDA stream context, see
            :ref:`Stream semantics of backward passes<bwd-cuda-stream-semantics>`.

        .. note::

            When ``inputs`` are provided and a given input is not a leaf,
            the current implementation will call its grad_fn (though it is not strictly needed to get this gradients).
            It is an implementation detail on which the user should not rely.
            See https://github.com/pytorch/pytorch/pull/60521#issuecomment-867061780 for more details.

        Args:
            gradient (Tensor or None): Gradient w.r.t. the
                tensor. If it is a tensor, it will be automatically converted
                to a Tensor that does not require grad unless ``create_graph`` is True.
                None values can be specified for scalar Tensors or ones that
                don't require grad. If a None value would be acceptable then
                this argument is optional.
            retain_graph (bool, optional): If ``False``, the graph used to compute
                the grads will be freed. Note that in nearly all cases setting
                this option to True is not needed and often can be worked around
                in a much more efficient way. Defaults to the value of
                ``create_graph``.
            create_graph (bool, optional): If ``True``, graph of the derivative will
                be constructed, allowing to compute higher order derivative
                products. Defaults to ``False``.
            inputs (sequence of Tensor): Inputs w.r.t. which the gradient will be
                accumulated into ``.grad``. All other Tensors will be ignored. If not
                provided, the gradient is accumulated into all the leaf Tensors that were
                used to compute the attr::tensors.
        """
        if has_torch_function_unary(self):
            return handle_torch_function(Tensor.backward, (self, ),
                                         self,
                                         gradient=gradient,
                                         retain_graph=retain_graph,
                                         create_graph=create_graph,
                                         inputs=inputs)
        torch.autograd.backward(self,
                                gradient,
                                retain_graph,
                                create_graph,
                                inputs=inputs)
Beispiel #12
0
 def __format__(self, format_spec):
     if has_torch_function_unary(self):
         return handle_torch_function(Tensor.__format__, (self,), self, format_spec)
     if self.dim() == 0:
         return self.item().__format__(format_spec)
     return object.__format__(self, format_spec)
Beispiel #13
0
 def norm(self, p="fro", dim=None, keepdim=False, dtype=None):
     r"""See :func:`torch.norm`"""
     if has_torch_function_unary(self):
         return handle_torch_function(Tensor.norm, (self,), self, p=p, dim=dim, keepdim=keepdim, dtype=dtype)
     return torch.norm(self, p, dim, keepdim, dtype=dtype)