A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.
Torch defines 10 tensor types with CPU and GPU variants which are as follows:
Data type
dtype
CPU tensor
GPU tensor
32-bit floating point
torch.float32 or torch.float
torch.FloatTensor
torch.cuda.FloatTensor
64-bit floating point
torch.float64 or torch.double
torch.DoubleTensor
torch.cuda.DoubleTensor
16-bit floating point 1
torch.float16 or torch.half
torch.HalfTensor
torch.cuda.HalfTensor
16-bit floating point 2
torch.bfloat16
torch.BFloat16Tensor
torch.cuda.BFloat16Tensor
32-bit complex
torch.complex32
64-bit complex
torch.complex64
128-bit complex
torch.complex128 or torch.cdouble
8-bit integer (unsigned)
torch.uint8
torch.ByteTensor
torch.cuda.ByteTensor
8-bit integer (signed)
torch.int8
torch.CharTensor
torch.cuda.CharTensor
16-bit integer (signed)
torch.int16 or torch.short
torch.ShortTensor
torch.cuda.ShortTensor
32-bit integer (signed)
torch.int32 or torch.int
torch.IntTensor
torch.cuda.IntTensor
64-bit integer (signed)
torch.int64 or torch.long
torch.LongTensor
torch.cuda.LongTensor
Boolean
torch.bool
torch.BoolTensor
torch.cuda.BoolTensor
Sometimes referred to as binary16: uses 1 sign, 5 exponent, and 10 significand bits. Useful when precision is important at the expense of range.
Sometimes referred to as Brain Floating Point: uses 1 sign, 8 exponent, and 7 significand bits. Useful when range is important, since it has the same number of exponent bits as float32
torch.Tensor is an alias for the default tensor type (torch.FloatTensor).
A tensor can be constructed from a Python list or sequence using the torch.tensor() constructor:
Warning
torch.tensor() always copies data. If you have a Tensor data and just want to change its requires_grad flag, use requires_grad_() or detach() to avoid a copy. If you have a numpy array and want to avoid a copy, use torch.as_tensor().
A tensor of specific data type can be constructed by passing a torch.dtype and/or a torch.device to a constructor or tensor creation op:
The contents of a tensor can be accessed and modified using Python’s indexing and slicing notation:
Use torch.Tensor.item() to get a Python number from a tensor containing a single value:
A tensor can be created with requires_grad=True so that torch.autograd records operations on them for automatic differentiation.
Each tensor has an associated torch.Storage, which holds its data. The tensor class also provides multi-dimensional, strided view of a storage and defines numeric operations on it.
Note
For more information on tensor views, see Tensor Views.
Note
For more information on the torch.dtype, torch.device, and torch.layout attributes of a torch.Tensor, see Tensor Attributes.
Note
Methods which mutate a tensor are marked with an underscore suffix. For example, torch.FloatTensor.abs_() computes the absolute value in-place and returns the modified tensor, while torch.FloatTensor.abs() computes the result in a new tensor.
Note
To change an existing tensor’s torch.device and/or torch.dtype, consider using to() method on the tensor.
Warning
Current implementation of torch.Tensor introduces memory overhead, thus it might lead to unexpectedly high memory usage in the applications with many tiny tensors. If this is your case, consider using one large structure.
There are a few main ways to create a tensor, depending on your use case.
Returns a new Tensor with data as the tensor data. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Warning
new_tensor() always copies data. If you have a Tensor data and want to avoid a copy, use torch.Tensor.requires_grad_() or torch.Tensor.detach(). If you have a numpy array and want to avoid a copy, use torch.from_numpy().
Warning
When data is a tensor x, new_tensor() reads out ‘the data’ from whatever it is passed, and constructs a leaf variable. Therefore tensor.new_tensor(x) is equivalent to x.clone().detach() and tensor.new_tensor(x, requires_grad=True) is equivalent to x.clone().detach().requires_grad_(True). The equivalents using clone() and detach() are recommended.
Example:
Returns a Tensor of size size filled with fill_value. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Example:
Returns a Tensor of size size filled with uninitialized data. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Example:
Returns a Tensor of size size filled with 1. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Example:
Returns a Tensor of size size filled with 0. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Example:
Is True if the Tensor is stored on the GPU, False otherwise.
Is True if the Tensor is quantized, False otherwise.
Is True if the Tensor is a meta tensor, False otherwise. Meta tensors are like normal tensors, but they carry no data.
Is the torch.device where this Tensor is.
This attribute is None by default and becomes a Tensor the first time a call to backward() computes gradients for self. The attribute will then contain the gradients computed and future calls to backward() will accumulate (add) gradients into it.
Alias for dim()
Is this Tensor with its dimensions reversed.
If n is the number of dimensions in x, x.T is equivalent to x.permute(n-1, n-2, ..., 0).
Returns a new tensor containing real values of the self tensor. The returned tensor and self share the same underlying storage.
Warning
real() is only supported for tensors with complex dtypes.
Returns a new tensor containing imaginary values of the self tensor. The returned tensor and self share the same underlying storage.
Warning
imag() is only supported for tensors with complex dtypes.
See torch.abs()
In-place version of abs()
Alias for abs()
In-place version of absolute() Alias for abs_()
See torch.acos()
In-place version of acos()
See torch.arccos()
In-place version of arccos()
Add a scalar or tensor to self tensor. If both alpha and other are specified, each element of other is scaled by alpha before being used.
When other is a tensor, the shape of other must be broadcastable with the shape of the underlying tensor
See torch.add()
In-place version of add()
See torch.addbmm()
In-place version of addbmm()
See torch.addcdiv()
In-place version of addcdiv()
See torch.addcmul()
In-place version of addcmul()
See torch.addmm()
In-place version of addmm()
See torch.sspaddmm()
See torch.addmv()
In-place version of addmv()
See torch.addr()
In-place version of addr()
See torch.allclose()
See torch.amax()
See torch.amin()
See torch.angle()
Applies the function callable to each element in the tensor, replacing each element with the value returned by callable.
Note
This function only works with CPU tensors and should not be used in code sections that require high performance.
See torch.argmax()
See torch.argmin()
See torch.argsort()
See torch.asin()
In-place version of asin()
See torch.arcsin()
In-place version of arcsin()
See torch.as_strided()
See torch.atan()
In-place version of atan()
See torch.arctan()
In-place version of arctan()
See torch.atan2()
In-place version of atan2()
See torch.all()
See torch.any()
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 Default gradient 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 Stream semantics of backward passes.
See torch.baddbmm()
In-place version of baddbmm()
Returns a result tensor where each result[i]\texttt{result[i]}  is independently sampled from Bernoulli(self[i])\text{Bernoulli}(\texttt{self[i]}) . self must have floating point dtype, and the result will have the same dtype.
See torch.bernoulli()
Fills each location of self with an independent sample from Bernoulli(p)\text{Bernoulli}(\texttt{p}) . self can have integral dtype.
p_tensor should be a tensor containing probabilities to be used for drawing the binary random number.
The ith\text{i}^{th}  element of self tensor will be set to a value sampled from Bernoulli(p_tensor[i])\text{Bernoulli}(\texttt{p\_tensor[i]}) .
self can have integral dtype, but p_tensor must have floating point dtype.
See also bernoulli() and torch.bernoulli()
self.bfloat16() is equivalent to self.to(torch.bfloat16). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.bincount()
See torch.bitwise_not()
In-place version of bitwise_not()
See torch.bitwise_and()
In-place version of bitwise_and()
See torch.bitwise_or()
In-place version of bitwise_or()
See torch.bitwise_xor()
In-place version of bitwise_xor()
See torch.bmm()
self.bool() is equivalent to self.to(torch.bool). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
self.byte() is equivalent to self.to(torch.uint8). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.broadcast_to().
Fills the tensor with numbers drawn from the Cauchy distribution:
See torch.ceil()
In-place version of ceil()
self.char() is equivalent to self.to(torch.int8). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.cholesky()
See torch.cholesky_inverse()
See torch.cholesky_solve()
See torch.chunk()
See torch.clamp()
In-place version of clamp()
Alias for clamp().
Alias for clamp_().
See torch.clone()
Returns a contiguous in memory tensor containing the same data as self tensor. If self tensor is already in the specified memory format, this function returns the self tensor.
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.contiguous_format.
Copies the elements from src into self tensor and returns self.
The src tensor must be broadcastable with the self tensor. It may be of a different data type or reside on a different device.
See torch.conj()
See torch.copysign()
In-place version of copysign()
See torch.cos()
In-place version of cos()
See torch.cosh()
In-place version of cosh()
See torch.count_nonzero()
See torch.acosh()
In-place version of acosh()
acosh() -> Tensor
See torch.arccosh()
acosh_() -> Tensor
In-place version of arccosh()
Returns a copy of this object in CPU memory.
If this object is already in CPU memory and on the correct device, then no copy is performed and the original object is returned.
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.cross()
Returns a copy of this object in CUDA memory.
If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.
See torch.logcumsumexp()
See torch.cummax()
See torch.cummin()
See torch.cumprod()
In-place version of cumprod()
See torch.cumsum()
In-place version of cumsum()
Returns the address of the first element of self tensor.
See torch.deg2rad()
Given a quantized Tensor, dequantize it and return the dequantized float Tensor.
See torch.det()
Return the number of dense dimensions in a sparse tensor self.
Warning
Throws an error if self is not a sparse tensor.
See also Tensor.sparse_dim() and hybrid tensors.
Returns a new Tensor, detached from the current graph.
The result will never require gradient.
Note
Returned Tensor shares the same storage with the original one. In-place modifications on either of them will be seen, and may trigger errors in correctness checks. IMPORTANT NOTE: Previously, in-place size / stride / storage changes (such as resize_ / resize_as_ / set_ / transpose_) to the returned tensor also update the original tensor. Now, these in-place changes will not update the original tensor anymore, and will instead trigger an error. For sparse tensors: In-place indices / values changes (such as zero_ / copy_ / add_) to the returned tensor will not update the original tensor anymore, and will instead trigger an error.
Detaches the Tensor from the graph that created it, making it a leaf. Views cannot be detached in-place.
See torch.diag()
See torch.diag_embed()
See torch.diagflat()
See torch.diagonal()
Fill the main diagonal of a tensor that has at least 2-dimensions. When dims>2, all dimensions of input must be of equal length. This function modifies the input tensor in-place, and returns the input tensor.
Example:
See torch.fmax()
See torch.fmin()
See torch.diff()
See torch.digamma()
In-place version of digamma()
Returns the number of dimensions of self tensor.
See torch.dist()
See torch.div()
In-place version of div()
See torch.divide()
In-place version of divide()
See torch.dot()
self.double() is equivalent to self.to(torch.float64). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.eig()
Returns the size in bytes of an individual element.
Example:
See torch.eq()
In-place version of eq()
See torch.equal()
See torch.erf()
In-place version of erf()
See torch.erfc()
In-place version of erfc()
See torch.erfinv()
In-place version of erfinv()
See torch.exp()
In-place version of exp()
See torch.expm1()
In-place version of expm1()
Returns a new view of the self tensor with singleton dimensions expanded to a larger size.
Passing -1 as the size for a dimension means not changing the size of that dimension.
Tensor can be also expanded to a larger number of dimensions, and the new ones will be appended at the front. For the new dimensions, the size cannot be set to -1.
Expanding a tensor does not allocate new memory, but only creates a new view on the existing tensor where a dimension of size one is expanded to a larger size by setting the stride to 0. Any dimension of size 1 can be expanded to an arbitrary value without allocating new memory.
*sizes (torch.Size or int...) – the desired expanded size
Warning
More than one element of an expanded tensor may refer to a single memory location. As a result, in-place operations (especially ones that are vectorized) may result in incorrect behavior. If you need to write to the tensors, please clone them first.
Example:
Expand this tensor to the same size as other. self.expand_as(other) is equivalent to self.expand(other.size()).
Please see expand() for more information about expand.
other (torch.Tensor) – The result tensor has the same size as other.
Fills self tensor with elements drawn from the exponential distribution:
See torch.fix().
In-place version of fix()
Fills self tensor with the specified value.
see torch.flatten()
See torch.flip()
See torch.fliplr()
See torch.flipud()
self.float() is equivalent to self.to(torch.float32). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.float_power()
In-place version of float_power()
See torch.floor()
In-place version of floor()
See torch.floor_divide()
In-place version of floor_divide()
See torch.fmod()
In-place version of fmod()
See torch.frac()
In-place version of frac()
See torch.gather()
See torch.gcd()
In-place version of gcd()
See torch.ge().
In-place version of ge().
See torch.greater_equal().
In-place version of greater_equal().
Fills self tensor with elements drawn from the geometric distribution:
See torch.geqrf()
See torch.ger()
For CUDA tensors, this function returns the device ordinal of the GPU on which the tensor resides. For CPU tensors, an error is thrown.
Example:
See torch.gt().
In-place version of gt().
See torch.greater().
In-place version of greater().
self.half() is equivalent to self.to(torch.float16). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.nn.functional.hardshrink()
See torch.heaviside()
See torch.histc()
See torch.hypot()
In-place version of hypot()
See torch.i0()
In-place version of i0()
See torch.igamma()
In-place version of igamma()
See torch.igammac()
In-place version of igammac()
Accumulate the elements of tensor into the self tensor by adding to the indices in the order given in index. For example, if dim == 0 and index[i] == j, then the ith row of tensor is added to the jth row of self.
The dimth dimension of tensor must have the same size as the length of index (which must be a vector), and all other dimensions must match self, or an error will be raised.
Note
This operation may behave nondeterministically when given tensors on a CUDA device. See Reproducibility for more information.
Example:
Out-of-place version of torch.Tensor.index_add_(). tensor1 corresponds to self in torch.Tensor.index_add_().
Copies the elements of tensor into the self tensor by selecting the indices in the order given in index. For example, if dim == 0 and index[i] == j, then the ith row of tensor is copied to the jth row of self.
The dimth dimension of tensor must have the same size as the length of index (which must be a vector), and all other dimensions must match self, or an error will be raised.
Note
If index contains duplicate entries, multiple elements from tensor will be copied to the same index of self. The result is nondeterministic since it depends on which copy occurs last.
Example:
Out-of-place version of torch.Tensor.index_copy_(). tensor1 corresponds to self in torch.Tensor.index_copy_().
Fills the elements of the self tensor with value val by selecting the indices in the order given in index.
Out-of-place version of torch.Tensor.index_fill_(). tensor1 corresponds to self in torch.Tensor.index_fill_().
Puts values from the tensor values into the tensor self using the indices specified in indices (which is a tuple of Tensors). The expression tensor.index_put_(indices, values) is equivalent to tensor[indices] = values. Returns self.
If accumulate is True, the elements in values are added to self. If accumulate is False, the behavior is undefined if indices contain duplicate elements.
Out-place version of index_put_(). tensor1 corresponds to self in torch.Tensor.index_put_().
See torch.index_select()
Return the indices tensor of a sparse COO tensor.
Warning
Throws an error if self is not a sparse COO tensor.
See also Tensor.values().
Note
This method can only be called on a coalesced sparse tensor. See Tensor.coalesce() for details.
See torch.inner().
self.int() is equivalent to self.to(torch.int32). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
Given a quantized Tensor, self.int_repr() returns a CPU Tensor with uint8_t as data type that stores the underlying uint8_t values of the given Tensor.
See torch.inverse()
See torch.isclose()
See torch.isfinite()
See torch.isinf()
See torch.isposinf()
See torch.isneginf()
See torch.isnan()
Returns True if self tensor is contiguous in memory in the order specified by memory format.
memory_format (torch.memory_format, optional) – Specifies memory allocation order. Default: torch.contiguous_format.
Returns True if the data type of self is a complex data type.
Returns True if the data type of self is a floating point data type.
All Tensors that have requires_grad which is False will be leaf Tensors by convention.
For Tensors that have requires_grad which is True, they will be leaf Tensors if they were created by the user. This means that they are not the result of an operation and so grad_fn is None.
Only leaf Tensors will have their grad populated during a call to backward(). To get grad populated for non-leaf Tensors, you can use retain_grad().
Example:
Returns true if this tensor resides in pinned memory.
Returns True if both tensors are pointing to the exact same memory (same storage, offset, size and stride).
Checks if tensor is in shared memory.
This is always True for CUDA tensors.
Returns True if the data type of self is a signed data type.
Is True if the Tensor uses sparse storage layout, False otherwise.
See torch.istft()
See torch.isreal()
Returns the value of this tensor as a standard Python number. This only works for tensors with one element. For other cases, see tolist().
This operation is not differentiable.
Example:
See torch.kthvalue()
See torch.lcm()
In-place version of lcm()
See torch.ldexp()
In-place version of ldexp()
See torch.le().
In-place version of le().
See torch.less_equal().
In-place version of less_equal().
See torch.lerp()
In-place version of lerp()
See torch.lgamma()
In-place version of lgamma()
See torch.log()
In-place version of log()
See torch.logdet()
See torch.log10()
In-place version of log10()
See torch.log1p()
In-place version of log1p()
See torch.log2()
In-place version of log2()
Fills self tensor with numbers samples from the log-normal distribution parameterized by the given mean μ\mu  and standard deviation σ\sigma . Note that mean and std are the mean and standard deviation of the underlying normal distribution, and not of the returned distribution:
See torch.logaddexp()
See torch.logaddexp2()
See torch.logsumexp()
See torch.logical_and()
In-place version of logical_and()
See torch.logical_not()
In-place version of logical_not()
See torch.logical_or()
In-place version of logical_or()
See torch.logical_xor()
In-place version of logical_xor()
See torch.logit()
In-place version of logit()
self.long() is equivalent to self.to(torch.int64). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.lstsq()
See torch.lt().
In-place version of lt().
lt(other) -> Tensor
See torch.less().
In-place version of less().
See torch.lu()
See torch.lu_solve()
Makes a cls instance with the same data pointer as self. Changes in the output mirror changes in self, and the output stays attached to the autograd graph. cls must be a subclass of Tensor.
Applies callable for each element in self tensor and the given tensor and stores the results in self tensor. self tensor and the given tensor must be broadcastable.
The callable should have the signature:
Copies elements from source into self tensor at positions where the mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor. The source should have at least as many elements as the number of ones in mask
Note
The mask operates on the self tensor, not on the given source tensor.
Out-of-place version of torch.Tensor.masked_scatter_()
Fills elements of self tensor with value where mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor.
Out-of-place version of torch.Tensor.masked_fill_()
See torch.masked_select()
See torch.matmul()
See torch.matrix_power()
See torch.matrix_exp()
See torch.max()
See torch.maximum()
See torch.mean()
See torch.median()
See torch.nanmedian()
See torch.min()
See torch.minimum()
See torch.mm()
See torch.smm()
See torch.mode()
See torch.movedim()
See torch.moveaxis()
See torch.msort()
See torch.mul().
In-place version of mul().
See torch.multiply().
In-place version of multiply().
See torch.multinomial()
See torch.mv()
See torch.mvlgamma()
In-place version of mvlgamma()
See torch.nansum()
See torch.narrow()
Example:
Same as Tensor.narrow() except returning a copy rather than shared storage. This is primarily for sparse tensors, which do not have a shared-storage narrow method. Calling `narrow_copy with `dimemsion > self.sparse_dim()` will return a copy with the relevant dense dimension narrowed, and `self.shape` updated accordingly.
Alias for dim()
See torch.nan_to_num().
In-place version of nan_to_num().
See torch.ne().
In-place version of ne().
See torch.not_equal().
In-place version of not_equal().
See torch.neg()
In-place version of neg()
See torch.negative()
In-place version of negative()
Alias for numel()
See torch.nextafter()
In-place version of nextafter()
See torch.nonzero()
See torch.norm()
Fills self tensor with elements samples from the normal distribution parameterized by mean and std.
See torch.numel()
Returns self tensor as a NumPy ndarray. This tensor and the returned ndarray share the same underlying storage. Changes to self tensor will be reflected in the ndarray and vice versa.
See torch.orgqr()
See torch.ormqr()
See torch.outer().
Returns a view of the original tensor with its dimensions permuted.
*dims (int...) – The desired ordering of dimensions
Copies the tensor to pinned memory, if it’s not already pinned.
See torch.pinverse()
See torch.polygamma()
In-place version of polygamma()
See torch.pow()
In-place version of pow()
See torch.prod()
Copies the elements from tensor into the positions specified by indices. For the purpose of indexing, the self tensor is treated as if it were a 1-D tensor.
If accumulate is True, the elements in tensor are added to self. If accumulate is False, the behavior is undefined if indices contain duplicate elements.
Example:
See torch.qr()
Returns the quantization scheme of a given QTensor.
See torch.quantile()
See torch.nanquantile()
Given a Tensor quantized by linear(affine) quantization, returns the scale of the underlying quantizer().
Given a Tensor quantized by linear(affine) quantization, returns the zero_point of the underlying quantizer().
Given a Tensor quantized by linear (affine) per-channel quantization, returns a Tensor of scales of the underlying quantizer. It has the number of elements that matches the corresponding dimensions (from q_per_channel_axis) of the tensor.
Given a Tensor quantized by linear (affine) per-channel quantization, returns a tensor of zero_points of the underlying quantizer. It has the number of elements that matches the corresponding dimensions (from q_per_channel_axis) of the tensor.
Given a Tensor quantized by linear (affine) per-channel quantization, returns the index of dimension on which per-channel quantization is applied.
See torch.rad2deg()
Fills self tensor with numbers sampled from the discrete uniform distribution over [from, to - 1]. If not specified, the values are usually only bounded by self tensor’s data type. However, for floating point types, if unspecified, range will be [0, 2^mantissa] to ensure that every value is representable. For example, torch.tensor(1, dtype=torch.double).random_() will be uniform in [0, 2^53].
see torch.ravel()
See torch.reciprocal()
In-place version of reciprocal()
Ensures that the tensor memory is not reused for another tensor until all current work queued on stream are complete.
Note
The caching allocator is aware of only the stream where a tensor was allocated. Due to the awareness, it already correctly manages the life cycle of tensors on only one stream. But if a tensor is used on a stream different from the stream of origin, the allocator might reuse the memory unexpectedly. Calling this method lets the allocator know which streams have used the tensor.
Registers a backward hook.
The hook will be called every time a gradient with respect to the Tensor is computed. The hook should have the following signature:
The hook should not modify its argument, but it can optionally return a new gradient which will be used in place of grad.
This function returns a handle with a method handle.remove() that removes the hook from the module.
Example:
See torch.remainder()
In-place version of remainder()
See torch.renorm()
In-place version of renorm()
Repeats this tensor along the specified dimensions.
Unlike expand(), this function copies the tensor’s data.
Warning
repeat() behaves differently from numpy.repeat, but is more similar to numpy.tile. For the operator similar to numpy.repeat, see torch.repeat_interleave().
sizes (torch.Size or int...) – The number of times to repeat this tensor along each dimension
Example:
See torch.repeat_interleave().
Is True if gradients need to be computed for this Tensor, False otherwise.
Note
The fact that gradients need to be computed for a Tensor do not mean that the grad attribute will be populated, see is_leaf for more details.
Change if autograd should record operations on this tensor: sets this tensor’s requires_grad attribute in-place. Returns this tensor.
requires_grad_()’s main use case is to tell autograd to begin recording operations on a Tensor tensor. If tensor has requires_grad=False (because it was obtained through a DataLoader, or required preprocessing or initialization), tensor.requires_grad_() makes it so that autograd will begin to record operations on tensor.
requires_grad (bool) – If autograd should record operations on this tensor. Default: True.
Example:
Returns a tensor with the same data and number of elements as self but with the specified shape. This method returns a view if shape is compatible with the current shape. See torch.Tensor.view() on when it is possible to return a view.
See torch.reshape()
shape (tuple of python:ints or int...) – the desired shape
Returns this tensor as the same shape as other. self.reshape_as(other) is equivalent to self.reshape(other.sizes()). This method returns a view if other.sizes() is compatible with the current shape. See torch.Tensor.view() on when it is possible to return a view.
Please see reshape() for more information about reshape.
other (torch.Tensor) – The result tensor has the same shape as other.
Resizes self tensor to the specified size. If the number of elements is larger than the current storage size, then the underlying storage is resized to fit the new number of elements. If the number of elements is smaller, the underlying storage is not changed. Existing elements are preserved but any new memory is uninitialized.
Warning
This is a low-level method. The storage is reinterpreted as C-contiguous, ignoring the current strides (unless the target size equals the current size, in which case the tensor is left unchanged). For most purposes, you will instead want to use view(), which checks for contiguity, or reshape(), which copies data if needed. To change the size in-place with custom strides, see set_().
Example:
Resizes the self tensor to be the same size as the specified tensor. This is equivalent to self.resize_(tensor.size()).
memory_format (torch.memory_format, optional) – the desired memory format of Tensor. Default: torch.contiguous_format. Note that memory format of self is going to be unaffected if self.size() matches tensor.size().
Enables .grad attribute for non-leaf Tensors.
See torch.roll()
See torch.rot90()
See torch.round()
In-place version of round()
See torch.rsqrt()
In-place version of rsqrt()
Out-of-place version of torch.Tensor.scatter_()
Writes all values from the tensor src into self at the indices specified in the index tensor. For each value in src, its output index is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.
For a 3-D tensor, self is updated as:
This is the reverse operation of the manner described in gather().
self, index and src (if it is a Tensor) should all have the same number of dimensions. It is also required that index.size(d) <= src.size(d) for all dimensions d, and that index.size(d) <= self.size(d) for all dimensions d != dim. Note that index and src do not broadcast.
Moreover, as for gather(), the values of index must be between 0 and self.size(dim) - 1 inclusive.
Warning
When indices are not unique, the behavior is non-deterministic (one of the values from src will be picked arbitrarily) and the gradient will be incorrect (it will be propagated to all locations in the source that correspond to the same index)!
Note
The backward pass is implemented only for src.shape == index.shape.
Additionally accepts an optional reduce argument that allows specification of an optional reduction operation, which is applied to all values in the tensor src into self at the indicies specified in the index. For each value in src, the reduction operation is applied to an index in self which is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.
Given a 3-D tensor and reduction using the multiplication operation, self is updated as:
Reducing with the addition operation is the same as using scatter_add_().
Example:
Adds all values from the tensor other into self at the indices specified in the index tensor in a similar fashion as scatter_(). For each value in src, it is added to an index in self which is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.
For a 3-D tensor, self is updated as:
self, index and src should have same number of dimensions. It is also required that index.size(d) <= src.size(d) for all dimensions d, and that index.size(d) <= self.size(d) for all dimensions d != dim. Note that index and src do not broadcast.
Note
This operation may behave nondeterministically when given tensors on a CUDA device. See Reproducibility for more information.
Note
The backward pass is implemented only for src.shape == index.shape.
Example:
Out-of-place version of torch.Tensor.scatter_add_()
Slices the self tensor along the selected dimension at the given index. This function returns a view of the original tensor with the given dimension removed.
Note
select() is equivalent to slicing. For example, tensor.select(0, index) is equivalent to tensor[index] and tensor.select(2, index) is equivalent to tensor[:,:,index].
Sets the underlying storage, size, and strides. If source is a tensor, self tensor will share the same storage and have the same size and strides as source. Changes to elements in one tensor will be reflected in the other.
If source is a Storage, the method sets the underlying storage, offset, size, and stride.
Moves the underlying storage to shared memory.
This is a no-op if the underlying storage is already in shared memory and for CUDA tensors. Tensors in shared memory cannot be resized.
self.short() is equivalent to self.to(torch.int16). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.
See torch.sigmoid()
In-place version of sigmoid()
See torch.sign()
In-place version of sign()
See torch.signbit()
See torch.sgn()
In-place version of sgn()
See torch.sin()
In-place version of sin()
See torch.sinc()
In-place version of sinc()
See torch.sinh()
In-place version of sinh()
See torch.asinh()
In-place version of asinh()
See torch.arcsinh()
In-place version of arcsinh()
Returns the size of the self tensor. The returned value is a subclass of tuple.
Example:
See torch.slogdet()
See torch.solve()
See torch.sort()
See torch.split()
Returns a new sparse tensor with values from a strided tensor self filtered by the indices of the sparse tensor mask. The values of mask sparse tensor are ignored. self and mask tensors must have the same shape.
Note
The returned sparse tensor has the same indices as the sparse tensor mask, even when the corresponding values in self are zeros.
mask (Tensor) – a sparse tensor whose indices are used as a filter
Example:
Return the number of sparse dimensions in a sparse tensor self.
Warning
Throws an error if self is not a sparse tensor.
See also Tensor.dense_dim() and hybrid tensors.
See torch.sqrt()
In-place version of sqrt()
See torch.square()
In-place version of square()
See torch.squeeze()
In-place version of squeeze()
See torch.std()
See torch.stft()
Warning
This function changed signature at version 0.4.1. Calling with the previous signature may cause error or return incorrect result.
Returns the underlying storage.
Returns self tensor’s offset in the underlying storage in terms of number of storage elements (not bytes).
Example:
Returns the type of the underlying storage.
Returns the stride of self tensor.
Stride is the jump necessary to go from one element to the next one in the specified dimension dim. A tuple of all strides is returned when no argument is passed in. Otherwise, an integer value is returned as the stride in the particular dimension dim.
dim (int, optional) – the desired dimension in which stride is required
Example:
See torch.sub().
In-place version of sub()
See torch.subtract().
In-place version of subtract().
See torch.sum()
Sum this tensor to size. size must be broadcastable to this tensor size.
size (int...) – a sequence of integers defining the shape of the output tensor.
See torch.svd()
See torch.swapaxes()
See torch.swapdims()
See torch.symeig()
See torch.t()
In-place version of t()
See torch.tensor_split()
See torch.tile()
Performs Tensor dtype and/or device conversion. A torch.dtype and torch.device are inferred from the arguments of self.to(*args, **kwargs).
Note
If the self Tensor already has the correct torch.dtype and torch.device, then self is returned. Otherwise, the returned tensor is a copy of self with the desired torch.dtype and torch.device.
Here are the ways to call to:
Returns a Tensor with the specified dtype
memory_format (torch.memory_format, optional): the desired memory format of returned Tensor. Default: torch.preserve_format.
Returns a Tensor with the specified device and (optional) dtype. If dtype is None it is inferred to be self.dtype. When non_blocking, tries to convert asynchronously with respect to the host if possible, e.g., converting a CPU Tensor with pinned memory to a CUDA Tensor. When copy is set, a new Tensor is created even when the Tensor already matches the desired conversion.
memory_format (torch.memory_format, optional): the desired memory format of returned Tensor. Default: torch.preserve_format.
Returns a Tensor with same torch.dtype and torch.device as the Tensor other. When non_blocking, tries to convert asynchronously with respect to the host if possible, e.g., converting a CPU Tensor with pinned memory to a CUDA Tensor. When copy is set, a new Tensor is created even when the Tensor already matches the desired conversion.
Example:
Returns a copy of the tensor in torch.mkldnn layout.
See torch.take()
See torch.tan()
In-place version of tan()
See torch.tanh()
In-place version of tanh()
See torch.atanh()
In-place version of atanh()
See torch.arctanh()
In-place version of arctanh()
Returns the tensor as a (nested) list. For scalars, a standard Python number is returned, just like with item(). Tensors are automatically moved to the CPU first if necessary.
This operation is not differentiable.
Examples:
See torch.topk()
Returns a sparse copy of the tensor. PyTorch supports sparse tensors in coordinate format.
sparseDims (int, optional) – the number of sparse dimensions to include in the new sparse tensor
Example:
See torch.trace()
See torch.transpose()
In-place version of transpose()
See torch.triangular_solve()
See torch.tril()
In-place version of tril()
See torch.triu()
In-place version of triu()
See torch.true_divide()
In-place version of true_divide_()
See torch.trunc()
In-place version of trunc()
Returns the type if dtype is not provided, else casts this object to the specified type.
If this is already of the correct type, no copy is performed and the original object is returned.
Returns this tensor cast to the type of the given tensor.
This is a no-op if the tensor is already of the correct type. This is equivalent to self.type(tensor.type())
tensor (Tensor) – the tensor which has the desired type
See torch.unbind()
Returns a view of the original tensor which contains all slices of size size from self tensor in the dimension dimension.
Step between two slices is given by step.
If sizedim is the size of dimension dimension for self, the size of dimension dimension in the returned tensor will be (sizedim - size) / step + 1.
An additional dimension of size size is appended in the returned tensor.
Example:
Fills self tensor with numbers sampled from the continuous uniform distribution:
Returns the unique elements of the input tensor.
See torch.unique()
Eliminates all but the first element from every consecutive group of equivalent elements.
See torch.unique_consecutive()
See torch.unsqueeze()
In-place version of unsqueeze()
Return the values tensor of a sparse COO tensor.
Warning
Throws an error if self is not a sparse COO tensor.
See also Tensor.indices().
Note
This method can only be called on a coalesced sparse tensor. See Tensor.coalesce() for details.
See torch.var()
See torch.vdot()
Returns a new tensor with the same data as the self tensor but of a different shape.
The returned tensor shares the same data and must have the same number of elements, but may have a different size. For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions d,d+1,…,d+kd, d+1, \dots, d+k  that satisfy the following contiguity-like condition that ∀i=d,…,d+k−1\forall i = d, \dots, d+k-1 ,
Otherwise, it will not be possible to view self tensor as shape without copying it (e.g., via contiguous()). When it is unclear whether a view() can be performed, it is advisable to use reshape(), which returns a view if the shapes are compatible, and copies (equivalent to calling contiguous()) otherwise.
shape (torch.Size or int...) – the desired size
Example:
Returns a new tensor with the same data as the self tensor but of a different dtype. dtype must have the same number of bytes per element as self’s dtype.
Warning
This overload is not supported by TorchScript, and using it in a Torchscript program will cause undefined behavior.
dtype (torch.dtype) – the desired dtype
Example:
View this tensor as the same size as other. self.view_as(other) is equivalent to self.view(other.size()).
Please see view() for more information about view.
other (torch.Tensor) – The result tensor has the same size as other.
self.where(condition, y) is equivalent to torch.where(condition, self, y). See torch.where()
See torch.xlogy()
In-place version of xlogy()
Fills self tensor with zeros.


Constructs a tensor with data.
Warning
torch.tensor() always copies data. If you have a Tensor data and want to avoid a copy, use torch.Tensor.requires_grad_() or torch.Tensor.detach(). If you have a NumPy ndarray and want to avoid a copy, use torch.as_tensor().
Warning
When data is a tensor x, torch.tensor() reads out ‘the data’ from whatever it is passed, and constructs a leaf variable. Therefore torch.tensor(x) is equivalent to x.clone().detach() and torch.tensor(x, requires_grad=True) is equivalent to x.clone().detach().requires_grad_(True). The equivalents using clone() and detach() are recommended.
data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.
Example:


In-place version of absolute() Alias for abs_()


Add a scalar or tensor to self tensor. If both alpha and other are specified, each element of other is scaled by alpha before being used.
When other is a tensor, the shape of other must be broadcastable with the shape of the underlying tensor
See torch.add()


Permutes the dimensions of the self tensor to match the dimension order in the other tensor, adding size-one dims for any new names.
This operation is useful for explicit broadcasting by names (see examples).
All of the dims of self must be named in order to use this method. The resulting tensor is a view on the original tensor.
All dimension names of self must be present in other.names. other may contain named dimensions that are not in self.names; the output tensor has a size-one dimension for each of those new names.
To align a tensor to a specific order, use align_to().
Examples:
Warning
The named tensor API is experimental and subject to change.


Permutes the dimensions of the self tensor to match the order specified in names, adding size-one dims for any new names.
All of the dims of self must be named in order to use this method. The resulting tensor is a view on the original tensor.
All dimension names of self must be present in names. names may contain additional names that are not in self.names; the output tensor has a size-one dimension for each of those new names.
names may contain up to one Ellipsis (...). The Ellipsis is expanded to be equal to all dimension names of self that are not mentioned in names, in the order that they appear in self.
Python 2 does not support Ellipsis but one may use a string literal instead ('...').
names (iterable of str) – The desired dimension ordering of the output tensor. May contain up to one Ellipsis that is expanded to all unmentioned dim names of self.
Examples:
Warning
The named tensor API is experimental and subject to change.


Applies the function callable to each element in the tensor, replacing each element with the value returned by callable.
Note
This function only works with CPU tensors and should not be used in code sections that require high performance.


acosh_() -> Tensor
In-place version of arccosh()


Makes a cls instance with the same data pointer as self. Changes in the output mirror changes in self, and the output stays attached to the autograd graph. cls must be a subclass of Tensor.


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 Default gradient 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 Stream semantics of backward passes.


Returns a result tensor where each result[i]\texttt{result[i]}  is independently sampled from Bernoulli(self[i])\text{Bernoulli}(\texttt{self[i]}) . self must have floating point dtype, and the result will have the same dtype.
See torch.bernoulli()


Fills each location of self with an independent sample from Bernoulli(p)\text{Bernoulli}(\texttt{p}) . self can have integral dtype.
p_tensor should be a tensor containing probabilities to be used for drawing the binary random number.
The ith\text{i}^{th}  element of self tensor will be set to a value sampled from Bernoulli(p_tensor[i])\text{Bernoulli}(\texttt{p\_tensor[i]}) .
self can have integral dtype, but p_tensor must have floating point dtype.
See also bernoulli() and torch.bernoulli()


self.bfloat16() is equivalent to self.to(torch.bfloat16). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


self.bool() is equivalent to self.to(torch.bool). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


self.byte() is equivalent to self.to(torch.uint8). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Fills the tensor with numbers drawn from the Cauchy distribution:


self.char() is equivalent to self.to(torch.int8). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Returns a coalesced copy of self if self is an uncoalesced tensor.
Returns self if self is a coalesced tensor.
Warning
Throws an error if self is not a sparse COO tensor.


Returns a contiguous in memory tensor containing the same data as self tensor. If self tensor is already in the specified memory format, this function returns the self tensor.
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.contiguous_format.


Copies the elements from src into self tensor and returns self.
The src tensor must be broadcastable with the self tensor. It may be of a different data type or reside on a different device.


Returns a copy of this object in CPU memory.
If this object is already in CPU memory and on the correct device, then no copy is performed and the original object is returned.
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Returns a copy of this object in CUDA memory.
If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.


Returns the address of the first element of self tensor.


Return the number of dense dimensions in a sparse tensor self.
Warning
Throws an error if self is not a sparse tensor.
See also Tensor.sparse_dim() and hybrid tensors.


Given a quantized Tensor, dequantize it and return the dequantized float Tensor.


Returns a new Tensor, detached from the current graph.
The result will never require gradient.
Note
Returned Tensor shares the same storage with the original one. In-place modifications on either of them will be seen, and may trigger errors in correctness checks. IMPORTANT NOTE: Previously, in-place size / stride / storage changes (such as resize_ / resize_as_ / set_ / transpose_) to the returned tensor also update the original tensor. Now, these in-place changes will not update the original tensor anymore, and will instead trigger an error. For sparse tensors: In-place indices / values changes (such as zero_ / copy_ / add_) to the returned tensor will not update the original tensor anymore, and will instead trigger an error.


Detaches the Tensor from the graph that created it, making it a leaf. Views cannot be detached in-place.


Is the torch.device where this Tensor is.


Returns the number of dimensions of self tensor.


self.double() is equivalent to self.to(torch.float64). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Returns the size in bytes of an individual element.
Example:


Returns a new view of the self tensor with singleton dimensions expanded to a larger size.
Passing -1 as the size for a dimension means not changing the size of that dimension.
Tensor can be also expanded to a larger number of dimensions, and the new ones will be appended at the front. For the new dimensions, the size cannot be set to -1.
Expanding a tensor does not allocate new memory, but only creates a new view on the existing tensor where a dimension of size one is expanded to a larger size by setting the stride to 0. Any dimension of size 1 can be expanded to an arbitrary value without allocating new memory.
*sizes (torch.Size or int...) – the desired expanded size
Warning
More than one element of an expanded tensor may refer to a single memory location. As a result, in-place operations (especially ones that are vectorized) may result in incorrect behavior. If you need to write to the tensors, please clone them first.
Example:


Expand this tensor to the same size as other. self.expand_as(other) is equivalent to self.expand(other.size()).
Please see expand() for more information about expand.
other (torch.Tensor) – The result tensor has the same size as other.


Fills self tensor with elements drawn from the exponential distribution:


Fills self tensor with the specified value.


Fill the main diagonal of a tensor that has at least 2-dimensions. When dims>2, all dimensions of input must be of equal length. This function modifies the input tensor in-place, and returns the input tensor.
Example:


self.float() is equivalent to self.to(torch.float32). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Fills self tensor with elements drawn from the geometric distribution:


For CUDA tensors, this function returns the device ordinal of the GPU on which the tensor resides. For CPU tensors, an error is thrown.
Example:


This attribute is None by default and becomes a Tensor the first time a call to backward() computes gradients for self. The attribute will then contain the gradients computed and future calls to backward() will accumulate (add) gradients into it.


self.half() is equivalent to self.to(torch.float16). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Returns a new tensor containing imaginary values of the self tensor. The returned tensor and self share the same underlying storage.
Warning
imag() is only supported for tensors with complex dtypes.


Out-of-place version of torch.Tensor.index_add_(). tensor1 corresponds to self in torch.Tensor.index_add_().


Accumulate the elements of tensor into the self tensor by adding to the indices in the order given in index. For example, if dim == 0 and index[i] == j, then the ith row of tensor is added to the jth row of self.
The dimth dimension of tensor must have the same size as the length of index (which must be a vector), and all other dimensions must match self, or an error will be raised.
Note
This operation may behave nondeterministically when given tensors on a CUDA device. See Reproducibility for more information.
Example:


Out-of-place version of torch.Tensor.index_copy_(). tensor1 corresponds to self in torch.Tensor.index_copy_().


Copies the elements of tensor into the self tensor by selecting the indices in the order given in index. For example, if dim == 0 and index[i] == j, then the ith row of tensor is copied to the jth row of self.
The dimth dimension of tensor must have the same size as the length of index (which must be a vector), and all other dimensions must match self, or an error will be raised.
Note
If index contains duplicate entries, multiple elements from tensor will be copied to the same index of self. The result is nondeterministic since it depends on which copy occurs last.
Example:


Out-of-place version of torch.Tensor.index_fill_(). tensor1 corresponds to self in torch.Tensor.index_fill_().


Fills the elements of the self tensor with value val by selecting the indices in the order given in index.


Out-place version of index_put_(). tensor1 corresponds to self in torch.Tensor.index_put_().


Puts values from the tensor values into the tensor self using the indices specified in indices (which is a tuple of Tensors). The expression tensor.index_put_(indices, values) is equivalent to tensor[indices] = values. Returns self.
If accumulate is True, the elements in values are added to self. If accumulate is False, the behavior is undefined if indices contain duplicate elements.


Return the indices tensor of a sparse COO tensor.
Warning
Throws an error if self is not a sparse COO tensor.
See also Tensor.values().
Note
This method can only be called on a coalesced sparse tensor. See Tensor.coalesce() for details.


self.int() is equivalent to self.to(torch.int32). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Given a quantized Tensor, self.int_repr() returns a CPU Tensor with uint8_t as data type that stores the underlying uint8_t values of the given Tensor.


Returns True if self is a sparse COO tensor that is coalesced, False otherwise.
Warning
Throws an error if self is not a sparse COO tensor.
See coalesce() and uncoalesced tensors.


Returns True if the data type of self is a complex data type.


Returns True if self tensor is contiguous in memory in the order specified by memory format.
memory_format (torch.memory_format, optional) – Specifies memory allocation order. Default: torch.contiguous_format.


Is True if the Tensor is stored on the GPU, False otherwise.


Returns True if the data type of self is a floating point data type.


All Tensors that have requires_grad which is False will be leaf Tensors by convention.
For Tensors that have requires_grad which is True, they will be leaf Tensors if they were created by the user. This means that they are not the result of an operation and so grad_fn is None.
Only leaf Tensors will have their grad populated during a call to backward(). To get grad populated for non-leaf Tensors, you can use retain_grad().
Example:


Is True if the Tensor is a meta tensor, False otherwise. Meta tensors are like normal tensors, but they carry no data.


Returns true if this tensor resides in pinned memory.


Is True if the Tensor is quantized, False otherwise.


Returns True if both tensors are pointing to the exact same memory (same storage, offset, size and stride).


Checks if tensor is in shared memory.
This is always True for CUDA tensors.


Returns True if the data type of self is a signed data type.


Is True if the Tensor uses sparse storage layout, False otherwise.


Returns the value of this tensor as a standard Python number. This only works for tensors with one element. For other cases, see tolist().
This operation is not differentiable.
Example:


Fills self tensor with numbers samples from the log-normal distribution parameterized by the given mean μ\mu  and standard deviation σ\sigma . Note that mean and std are the mean and standard deviation of the underlying normal distribution, and not of the returned distribution:


self.long() is equivalent to self.to(torch.int64). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Applies callable for each element in self tensor and the given tensor and stores the results in self tensor. self tensor and the given tensor must be broadcastable.
The callable should have the signature:


Out-of-place version of torch.Tensor.masked_fill_()


Fills elements of self tensor with value where mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor.


Out-of-place version of torch.Tensor.masked_scatter_()


Copies elements from source into self tensor at positions where the mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor. The source should have at least as many elements as the number of ones in mask
Note
The mask operates on the self tensor, not on the given source tensor.


Stores names for each of this tensor’s dimensions.
names[idx] corresponds to the name of tensor dimension idx. Names are either a string if the dimension is named or None if the dimension is unnamed.
Dimension names may contain characters or underscore. Furthermore, a dimension name must be a valid Python variable name (i.e., does not start with underscore).
Tensors may not have two named dimensions with the same name.
Warning
The named tensor API is experimental and subject to change.


Same as Tensor.narrow() except returning a copy rather than shared storage. This is primarily for sparse tensors, which do not have a shared-storage narrow method. Calling `narrow_copy with `dimemsion > self.sparse_dim()` will return a copy with the relevant dense dimension narrowed, and `self.shape` updated accordingly.


Returns a Tensor of size size filled with uninitialized data. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Example:


Returns a Tensor of size size filled with fill_value. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Example:


Returns a Tensor of size size filled with 1. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Example:


Returns a new Tensor with data as the tensor data. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Warning
new_tensor() always copies data. If you have a Tensor data and want to avoid a copy, use torch.Tensor.requires_grad_() or torch.Tensor.detach(). If you have a numpy array and want to avoid a copy, use torch.from_numpy().
Warning
When data is a tensor x, new_tensor() reads out ‘the data’ from whatever it is passed, and constructs a leaf variable. Therefore tensor.new_tensor(x) is equivalent to x.clone().detach() and tensor.new_tensor(x, requires_grad=True) is equivalent to x.clone().detach().requires_grad_(True). The equivalents using clone() and detach() are recommended.
Example:


Returns a Tensor of size size filled with 0. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
Example:


Fills self tensor with elements samples from the normal distribution parameterized by mean and std.


Returns self tensor as a NumPy ndarray. This tensor and the returned ndarray share the same underlying storage. Changes to self tensor will be reflected in the ndarray and vice versa.


Returns a view of the original tensor with its dimensions permuted.
*dims (int...) – The desired ordering of dimensions


Copies the tensor to pinned memory, if it’s not already pinned.


Copies the elements from tensor into the positions specified by indices. For the purpose of indexing, the self tensor is treated as if it were a 1-D tensor.
If accumulate is True, the elements in tensor are added to self. If accumulate is False, the behavior is undefined if indices contain duplicate elements.
Example:


Returns the quantization scheme of a given QTensor.


Given a Tensor quantized by linear (affine) per-channel quantization, returns the index of dimension on which per-channel quantization is applied.


Given a Tensor quantized by linear (affine) per-channel quantization, returns a Tensor of scales of the underlying quantizer. It has the number of elements that matches the corresponding dimensions (from q_per_channel_axis) of the tensor.


Given a Tensor quantized by linear (affine) per-channel quantization, returns a tensor of zero_points of the underlying quantizer. It has the number of elements that matches the corresponding dimensions (from q_per_channel_axis) of the tensor.


Given a Tensor quantized by linear(affine) quantization, returns the scale of the underlying quantizer().


Given a Tensor quantized by linear(affine) quantization, returns the zero_point of the underlying quantizer().


Fills self tensor with numbers sampled from the discrete uniform distribution over [from, to - 1]. If not specified, the values are usually only bounded by self tensor’s data type. However, for floating point types, if unspecified, range will be [0, 2^mantissa] to ensure that every value is representable. For example, torch.tensor(1, dtype=torch.double).random_() will be uniform in [0, 2^53].


Returns a new tensor containing real values of the self tensor. The returned tensor and self share the same underlying storage.
Warning
real() is only supported for tensors with complex dtypes.


Ensures that the tensor memory is not reused for another tensor until all current work queued on stream are complete.
Note
The caching allocator is aware of only the stream where a tensor was allocated. Due to the awareness, it already correctly manages the life cycle of tensors on only one stream. But if a tensor is used on a stream different from the stream of origin, the allocator might reuse the memory unexpectedly. Calling this method lets the allocator know which streams have used the tensor.


Refines the dimension names of self according to names.
Refining is a special case of renaming that “lifts” unnamed dimensions. A None dim can be refined to have any name; a named dim can only be refined to have the same name.
Because named tensors can coexist with unnamed tensors, refining names gives a nice way to write named-tensor-aware code that works with both named and unnamed tensors.
names may contain up to one Ellipsis (...). The Ellipsis is expanded greedily; it is expanded in-place to fill names to the same length as self.dim() using names from the corresponding indices of self.names.
Python 2 does not support Ellipsis but one may use a string literal instead ('...').
names (iterable of str) – The desired names of the output tensor. May contain up to one Ellipsis.
Examples:
Warning
The named tensor API is experimental and subject to change.


Registers a backward hook.
The hook will be called every time a gradient with respect to the Tensor is computed. The hook should have the following signature:
The hook should not modify its argument, but it can optionally return a new gradient which will be used in place of grad.
This function returns a handle with a method handle.remove() that removes the hook from the module.
Example:


Renames dimension names of self.
There are two main usages:
self.rename(**rename_map) returns a view on tensor that has dims renamed as specified in the mapping rename_map.
self.rename(*names) returns a view on tensor, renaming all dimensions positionally using names. Use self.rename(None) to drop names on a tensor.
One cannot specify both positional args names and keyword args rename_map.
Examples:
Warning
The named tensor API is experimental and subject to change.


Repeats this tensor along the specified dimensions.
Unlike expand(), this function copies the tensor’s data.
Warning
repeat() behaves differently from numpy.repeat, but is more similar to numpy.tile. For the operator similar to numpy.repeat, see torch.repeat_interleave().
sizes (torch.Size or int...) – The number of times to repeat this tensor along each dimension
Example:


Is True if gradients need to be computed for this Tensor, False otherwise.
Note
The fact that gradients need to be computed for a Tensor do not mean that the grad attribute will be populated, see is_leaf for more details.


Change if autograd should record operations on this tensor: sets this tensor’s requires_grad attribute in-place. Returns this tensor.
requires_grad_()’s main use case is to tell autograd to begin recording operations on a Tensor tensor. If tensor has requires_grad=False (because it was obtained through a DataLoader, or required preprocessing or initialization), tensor.requires_grad_() makes it so that autograd will begin to record operations on tensor.
requires_grad (bool) – If autograd should record operations on this tensor. Default: True.
Example:


Returns a tensor with the same data and number of elements as self but with the specified shape. This method returns a view if shape is compatible with the current shape. See torch.Tensor.view() on when it is possible to return a view.
See torch.reshape()
shape (tuple of python:ints or int...) – the desired shape


Returns this tensor as the same shape as other. self.reshape_as(other) is equivalent to self.reshape(other.sizes()). This method returns a view if other.sizes() is compatible with the current shape. See torch.Tensor.view() on when it is possible to return a view.
Please see reshape() for more information about reshape.
other (torch.Tensor) – The result tensor has the same shape as other.


Resizes self tensor to the specified size. If the number of elements is larger than the current storage size, then the underlying storage is resized to fit the new number of elements. If the number of elements is smaller, the underlying storage is not changed. Existing elements are preserved but any new memory is uninitialized.
Warning
This is a low-level method. The storage is reinterpreted as C-contiguous, ignoring the current strides (unless the target size equals the current size, in which case the tensor is left unchanged). For most purposes, you will instead want to use view(), which checks for contiguity, or reshape(), which copies data if needed. To change the size in-place with custom strides, see set_().
Example:


Resizes the self tensor to be the same size as the specified tensor. This is equivalent to self.resize_(tensor.size()).
memory_format (torch.memory_format, optional) – the desired memory format of Tensor. Default: torch.contiguous_format. Note that memory format of self is going to be unaffected if self.size() matches tensor.size().


Enables .grad attribute for non-leaf Tensors.


Out-of-place version of torch.Tensor.scatter_()


Writes all values from the tensor src into self at the indices specified in the index tensor. For each value in src, its output index is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.
For a 3-D tensor, self is updated as:
This is the reverse operation of the manner described in gather().
self, index and src (if it is a Tensor) should all have the same number of dimensions. It is also required that index.size(d) <= src.size(d) for all dimensions d, and that index.size(d) <= self.size(d) for all dimensions d != dim. Note that index and src do not broadcast.
Moreover, as for gather(), the values of index must be between 0 and self.size(dim) - 1 inclusive.
Warning
When indices are not unique, the behavior is non-deterministic (one of the values from src will be picked arbitrarily) and the gradient will be incorrect (it will be propagated to all locations in the source that correspond to the same index)!
Note
The backward pass is implemented only for src.shape == index.shape.
Additionally accepts an optional reduce argument that allows specification of an optional reduction operation, which is applied to all values in the tensor src into self at the indicies specified in the index. For each value in src, the reduction operation is applied to an index in self which is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.
Given a 3-D tensor and reduction using the multiplication operation, self is updated as:
Reducing with the addition operation is the same as using scatter_add_().
Example:


Out-of-place version of torch.Tensor.scatter_add_()


Adds all values from the tensor other into self at the indices specified in the index tensor in a similar fashion as scatter_(). For each value in src, it is added to an index in self which is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.
For a 3-D tensor, self is updated as:
self, index and src should have same number of dimensions. It is also required that index.size(d) <= src.size(d) for all dimensions d, and that index.size(d) <= self.size(d) for all dimensions d != dim. Note that index and src do not broadcast.
Note
This operation may behave nondeterministically when given tensors on a CUDA device. See Reproducibility for more information.
Note
The backward pass is implemented only for src.shape == index.shape.
Example:


Slices the self tensor along the selected dimension at the given index. This function returns a view of the original tensor with the given dimension removed.
Note
select() is equivalent to slicing. For example, tensor.select(0, index) is equivalent to tensor[index] and tensor.select(2, index) is equivalent to tensor[:,:,index].


Sets the underlying storage, size, and strides. If source is a tensor, self tensor will share the same storage and have the same size and strides as source. Changes to elements in one tensor will be reflected in the other.
If source is a Storage, the method sets the underlying storage, offset, size, and stride.


Moves the underlying storage to shared memory.
This is a no-op if the underlying storage is already in shared memory and for CUDA tensors. Tensors in shared memory cannot be resized.


self.short() is equivalent to self.to(torch.int16). See to().
memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.preserve_format.


Returns the size of the self tensor. The returned value is a subclass of tuple.
Example:


Return the number of sparse dimensions in a sparse tensor self.
Warning
Throws an error if self is not a sparse tensor.
See also Tensor.dense_dim() and hybrid tensors.


Returns a new sparse tensor with values from a strided tensor self filtered by the indices of the sparse tensor mask. The values of mask sparse tensor are ignored. self and mask tensors must have the same shape.
Note
The returned sparse tensor has the same indices as the sparse tensor mask, even when the corresponding values in self are zeros.
mask (Tensor) – a sparse tensor whose indices are used as a filter
Example:


Resizes self sparse tensor to the desired size and the number of sparse and dense dimensions.
Note
If the number of specified elements in self is zero, then size, sparse_dim, and dense_dim can be any size and positive integers such that len(size) == sparse_dim +
dense_dim.
If self specifies one or more elements, however, then each dimension in size must not be smaller than the corresponding dimension of self, sparse_dim must equal the number of sparse dimensions in self, and dense_dim must equal the number of dense dimensions in self.
Warning
Throws an error if self is not a sparse tensor.


Removes all specified elements from a sparse tensor self and resizes self to the desired size and the number of sparse and dense dimensions.


See torch.stft()
Warning
This function changed signature at version 0.4.1. Calling with the previous signature may cause error or return incorrect result.


Returns self tensor’s offset in the underlying storage in terms of number of storage elements (not bytes).
Example:


Returns the type of the underlying storage.


Returns the stride of self tensor.
Stride is the jump necessary to go from one element to the next one in the specified dimension dim. A tuple of all strides is returned when no argument is passed in. Otherwise, an integer value is returned as the stride in the particular dimension dim.
dim (int, optional) – the desired dimension in which stride is required
Example:


Sum this tensor to size. size must be broadcastable to this tensor size.
size (int...) – a sequence of integers defining the shape of the output tensor.


Is this Tensor with its dimensions reversed.
If n is the number of dimensions in x, x.T is equivalent to x.permute(n-1, n-2, ..., 0).


Performs Tensor dtype and/or device conversion. A torch.dtype and torch.device are inferred from the arguments of self.to(*args, **kwargs).
Note
If the self Tensor already has the correct torch.dtype and torch.device, then self is returned. Otherwise, the returned tensor is a copy of self with the desired torch.dtype and torch.device.
Here are the ways to call to:
Returns a Tensor with the specified dtype
memory_format (torch.memory_format, optional): the desired memory format of returned Tensor. Default: torch.preserve_format.
Returns a Tensor with the specified device and (optional) dtype. If dtype is None it is inferred to be self.dtype. When non_blocking, tries to convert asynchronously with respect to the host if possible, e.g., converting a CPU Tensor with pinned memory to a CUDA Tensor. When copy is set, a new Tensor is created even when the Tensor already matches the desired conversion.
memory_format (torch.memory_format, optional): the desired memory format of returned Tensor. Default: torch.preserve_format.
Returns a Tensor with same torch.dtype and torch.device as the Tensor other. When non_blocking, tries to convert asynchronously with respect to the host if possible, e.g., converting a CPU Tensor with pinned memory to a CUDA Tensor. When copy is set, a new Tensor is created even when the Tensor already matches the desired conversion.
Example:


Returns the tensor as a (nested) list. For scalars, a standard Python number is returned, just like with item(). Tensors are automatically moved to the CPU first if necessary.
This operation is not differentiable.
Examples:


Creates a strided copy of self.
Warning
Throws an error if self is a strided tensor.
Example:


Returns a copy of the tensor in torch.mkldnn layout.


Returns a sparse copy of the tensor. PyTorch supports sparse tensors in coordinate format.
sparseDims (int, optional) – the number of sparse dimensions to include in the new sparse tensor
Example:


Returns the type if dtype is not provided, else casts this object to the specified type.
If this is already of the correct type, no copy is performed and the original object is returned.


Returns this tensor cast to the type of the given tensor.
This is a no-op if the tensor is already of the correct type. This is equivalent to self.type(tensor.type())
tensor (Tensor) – the tensor which has the desired type


Expands the dimension dim of the self tensor over multiple dimensions of sizes given by sizes.
[[-1.1819, -0.8899], [ 1.5813, 0.2274]]], names=(‘A’, ‘B1’, ‘B2’))
Warning
The named tensor API is experimental and subject to change.


Returns a view of the original tensor which contains all slices of size size from self tensor in the dimension dimension.
Step between two slices is given by step.
If sizedim is the size of dimension dimension for self, the size of dimension dimension in the returned tensor will be (sizedim - size) / step + 1.
An additional dimension of size size is appended in the returned tensor.
Example:


Fills self tensor with numbers sampled from the continuous uniform distribution:


Returns the unique elements of the input tensor.
See torch.unique()


Eliminates all but the first element from every consecutive group of equivalent elements.
See torch.unique_consecutive()


Return the values tensor of a sparse COO tensor.
Warning
Throws an error if self is not a sparse COO tensor.
See also Tensor.indices().
Note
This method can only be called on a coalesced sparse tensor. See Tensor.coalesce() for details.


Returns a new tensor with the same data as the self tensor but of a different shape.
The returned tensor shares the same data and must have the same number of elements, but may have a different size. For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions d,d+1,…,d+kd, d+1, \dots, d+k  that satisfy the following contiguity-like condition that ∀i=d,…,d+k−1\forall i = d, \dots, d+k-1 ,
Otherwise, it will not be possible to view self tensor as shape without copying it (e.g., via contiguous()). When it is unclear whether a view() can be performed, it is advisable to use reshape(), which returns a view if the shapes are compatible, and copies (equivalent to calling contiguous()) otherwise.
shape (torch.Size or int...) – the desired size
Example:
Returns a new tensor with the same data as the self tensor but of a different dtype. dtype must have the same number of bytes per element as self’s dtype.
Warning
This overload is not supported by TorchScript, and using it in a Torchscript program will cause undefined behavior.
dtype (torch.dtype) – the desired dtype
Example:


View this tensor as the same size as other. self.view_as(other) is equivalent to self.view(other.size()).
Please see view() for more information about view.
other (torch.Tensor) – The result tensor has the same size as other.


self.where(condition, y) is equivalent to torch.where(condition, self, y). See torch.where()


Returns a contraction of a and b over multiple dimensions.
tensordot implements a generalized matrix product.
When called with a non-negative integer argument dims = dd , and the number of dimensions of a and b is mm  and nn , respectively, tensordot() computes
When called with dims of the list form, the given dimensions will be contracted in place of the last dd  of a and the first dd  of bb . The sizes in these dimensions must match, but tensordot() will deal with broadcasted dimensions.
Examples:


Splits a tensor into multiple sub-tensors, all of which are views of input, along dimension dim according to the indices or number of sections specified by indices_or_sections. This function is based on NumPy’s numpy.array_split().
indices_or_sections (Tensor, int or list or tuple of python:ints) –
If indices_or_sections is an integer n or a zero dimensional long tensor with value n, input is split into n sections along dimension dim. If input is divisible by n along dimension dim, each section will be of equal size, input.size(dim) / n. If input is not divisible by n, the sizes of the first int(input.size(dim) % n) sections will have size int(input.size(dim) / n) + 1, and the rest will have size int(input.size(dim) / n).
If indices_or_sections is a list or tuple of ints, or a one-dimensional long tensor, then input is split along dimension dim at each of the indices in the list, tuple or tensor. For instance, indices_or_sections=[2, 3] and dim=0 would result in the tensors input[:2], input[2:3], and input[3:].
If indices_or_sections is a tensor, it must be a zero-dimensional or one-dimensional long tensor on the CPU.


Constructs a tensor by repeating the elements of input. The reps argument specifies the number of repetitions in each dimension.
If reps specifies fewer dimensions than input has, then ones are prepended to reps until all dimensions are specified. For example, if input has shape (8, 6, 4, 2) and reps is (2, 2), then reps is treated as (1, 1, 2, 2).
Analogously, if input has fewer dimensions than reps specifies, then input is treated as if it were unsqueezed at dimension zero until it has as many dimensions as reps specifies. For example, if input has shape (4, 2) and reps is (3, 3, 2, 2), then input is treated as if it had the shape (1, 1, 4, 2).
Note
This function is similar to NumPy’s tile function.
Example:


Returns the k largest elements of the given input tensor along a given dimension.
If dim is not given, the last dimension of the input is chosen.
If largest is False then the k smallest elements are returned.
A namedtuple of (values, indices) is returned, where the indices are the indices of the elements in the original input tensor.
The boolean option sorted if True, will make sure that the returned k elements are themselves sorted
out (tuple, optional) – the output tuple of (Tensor, LongTensor) that can be optionally given to be used as output buffers
Example:


Returns the sum of the elements of the diagonal of the input 2-D matrix.
Example:


Returns a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.
The resulting out tensor shares its underlying storage with the input tensor, so changing the content of one would change the content of the other.
Example:


Estimate ∫ydx\int y\,dx  along dim, using the trapezoid rule.
A Tensor with the same shape as the input, except with dim removed. Each element of the returned tensor represents the estimated integral ∫ydx\int y\,dx  along dim.
Example:
As above, but the sample points are spaced uniformly at a distance of dx.
y (Tensor) – The values of the function to integrate
A Tensor with the same shape as the input, except with dim removed. Each element of the returned tensor represents the estimated integral ∫ydx\int y\,dx  along dim.


Solves a system of equations with a triangular coefficient matrix AA  and multiple right-hand sides bb .
In particular, solves AX=bAX = b  and assumes AA  is upper-triangular with the default keyword arguments.
torch.triangular_solve(b, A) can take in 2D inputs b, A or inputs that are batches of 2D matrices. If the inputs are batches, then returns batched outputs X
Supports real-valued and complex-valued inputs.
A namedtuple (solution, cloned_coefficient) where cloned_coefficient is a clone of AA  and solution is the solution XX  to AX=bAX = b  (or whatever variant of the system of equations, depending on the keyword arguments.)
Examples:


Returns the lower triangular part of the matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.
The lower triangular part of the matrix is defined as the elements on and below the diagonal.
The argument diagonal controls which diagonal to consider. If diagonal = 0, all elements on and below the main diagonal are retained. A positive value includes just as many diagonals above the main diagonal, and similarly a negative value excludes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace  for i∈[0,min{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1]  where d1,d2d_{1}, d_{2}  are the dimensions of the matrix.
out (Tensor, optional) – the output tensor.
Example:


Returns the indices of the lower triangular part of a row-by- col matrix in a 2-by-N Tensor, where the first row contains row coordinates of all indices and the second row contains column coordinates. Indices are ordered based on rows and then columns.
The lower triangular part of the matrix is defined as the elements on and below the diagonal.
The argument offset controls which diagonal to consider. If offset = 0, all elements on and below the main diagonal are retained. A positive value includes just as many diagonals above the main diagonal, and similarly a negative value excludes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace  for i∈[0,min{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1]  where d1,d2d_{1}, d_{2}  are the dimensions of the matrix.
Note
When running on CUDA, row * col must be less than 2592^{59}  to prevent overflow during calculation.


Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.
The upper triangular part of the matrix is defined as the elements on and above the diagonal.
The argument diagonal controls which diagonal to consider. If diagonal = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace  for i∈[0,min{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1]  where d1,d2d_{1}, d_{2}  are the dimensions of the matrix.
out (Tensor, optional) – the output tensor.
Example:


Returns the indices of the upper triangular part of a row by col matrix in a 2-by-N Tensor, where the first row contains row coordinates of all indices and the second row contains column coordinates. Indices are ordered based on rows and then columns.
The upper triangular part of the matrix is defined as the elements on and above the diagonal.
The argument offset controls which diagonal to consider. If offset = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace  for i∈[0,min{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1]  where d1,d2d_{1}, d_{2}  are the dimensions of the matrix.
Note
When running on CUDA, row * col must be less than 2592^{59}  to prevent overflow during calculation.


Alias for torch.div() with rounding_mode=None.


Returns a new tensor with the truncated integer values of the elements of input.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


Removes a tensor dimension.
Returns a tuple of all slices along a given dimension, already without it.
Example:


Returns the unique elements of the input tensor.
Note
This function is different from torch.unique_consecutive() in the sense that this function also eliminates non-consecutive duplicate values.
Note
Currently in the CUDA implementation and the CPU implementation when dim is specified, torch.unique always sort the tensor at the beginning regardless of the sort argument. Sorting could be slow, so if your input tensor is already sorted, it is recommended to use torch.unique_consecutive() which avoids the sorting.
A tensor or a tuple of tensors containing
(Tensor, Tensor (optional), Tensor (optional))
Example:


Eliminates all but the first element from every consecutive group of equivalent elements.
Note
This function is different from torch.unique() in the sense that this function only eliminates consecutive duplicate values. This semantics is similar to std::unique in C++.
A tensor or a tuple of tensors containing
(Tensor, Tensor (optional), Tensor (optional))
Example:


Returns a new tensor with a dimension of size one inserted at the specified position.
The returned tensor shares the same underlying data with this tensor.
A dim value within the range [-input.dim() - 1, input.dim() + 1) can be used. Negative dim will correspond to unsqueeze() applied at dim = dim + input.dim() + 1.
Example:


Sets whether PyTorch operations must use “deterministic” algorithms. That is, algorithms which, given the same input, and when run on the same software and hardware, always produce the same output. When True, operations will use deterministic algorithms when available, and if only nondeterministic algorithms are available they will throw a :class:RuntimeError when called.
Warning
This feature is in beta, and its design and implementation may change in the future.
The following normally-nondeterministic operations will act deterministically when d=True:
The following normally-nondeterministic operations will throw a RuntimeError when d=True:
torch.nn.functional.interpolate() when called on a CUDA tensor that requires grad and one of the following modes is used:
A handful of CUDA operations are nondeterministic if the CUDA version is 10.2 or greater, unless the environment variable CUBLAS_WORKSPACE_CONFIG=:4096:8 or CUBLAS_WORKSPACE_CONFIG=:16:8 is set. See the CUDA documentation for more details: https://docs.nvidia.com/cuda/cublas/index.html#cublasApi_reproducibility If one of these environment variable configurations is not set, a RuntimeError will be raised from these operations when called with CUDA tensors:
Note that deterministic operations tend to have worse performance than non-deterministic operations.
d (bool) – If True, force operations to be deterministic. If False, allow non-deterministic operations.


Helper class for measuring execution time of PyTorch statements.
For a full tutorial on how to use this class, see: https://pytorch.org/tutorials/recipes/recipes/benchmark.html
The PyTorch Timer is based on timeit.Timer (and in fact uses timeit.Timer internally), but with several key differences:
Timer will perform warmups (important as some elements of PyTorch are lazily initialized), set threadpool size so that comparisons are apples-to-apples, and synchronize asynchronous CUDA functions when necessary.
When measuring code, and particularly complex kernels / models, run-to-run variation is a significant confounding factor. It is expected that all measurements should include replicates to quantify noise and allow median computation, which is more robust than mean. To that effect, this class deviates from the timeit API by conceptually merging timeit.Timer.repeat and timeit.Timer.autorange. (Exact algorithms are discussed in method docstrings.) The timeit method is replicated for cases where an adaptive strategy is not desired.
When defining a Timer, one can optionally specify label, sub_label, description, and env. (Defined later) These fields are included in the representation of result object and by the Compare class to group and display results for comparison.
In addition to wall times, Timer can run a statement under Callgrind and report instructions executed.
Directly analogous to timeit.Timer constructor arguments:
stmt, setup, timer, globals
PyTorch Timer specific constructor arguments:
label, sub_label, description, env, num_threads
sub_label –
Provide supplemental information to disambiguate measurements with identical stmt or label. For instance, in our example above sub_label might be “float” or “int”, so that it is easy to differentiate: “ReLU(x + 1): (float)”
”ReLU(x + 1): (int)” when printing Measurements or summarizing using Compare.
description –
String to distinguish measurements with identical label and sub_label. The principal use of description is to signal to Compare the columns of data. For instance one might set it based on the input size to create a table of the form:
using Compare. It is also included when printing a Measurement.
Measure many replicates while keeping timer overhead to a minimum.
At a high level, blocked_autorange executes the following pseudo-code:
Note the variable block_size in the inner loop. The choice of block size is important to measurement quality, and must balance two competing objectives:
blocked_autorange sets block_size by running a warmup period, increasing block size until timer overhead is less than 0.1% of the overall computation. This value is then used for the main measurement loop.
A Measurement object that contains measured runtimes and repetition counts, and can be used to compute statistics. (mean, median, etc.)
Collect instruction counts using Callgrind.
Unlike wall times, instruction counts are deterministic (modulo non-determinism in the program itself and small amounts of jitter from the Python interpreter.) This makes them ideal for detailed performance analysis. This method runs stmt in a separate process so that Valgrind can instrument the program. Performance is severely degraded due to the instrumentation, howevever this is ameliorated by the fact that a small number of iterations is generally sufficient to obtain good measurements.
In order to to use this method valgrind, callgrind_control, and callgrind_annotate must be installed.
Because there is a process boundary between the caller (this process) and the stmt execution, globals cannot contain arbitrary in-memory data structures. (Unlike timing methods) Instead, globals are restricted to builtins, nn.Modules’s, and TorchScripted functions/modules to reduce the surprise factor from serialization and subsequent deserialization. The GlobalsBridge class provides more detail on this subject. Take particular care with nn.Modules: they rely on pickle and you may need to add an import to setup for them to transfer properly.
By default, a profile for an empty statement will be collected and cached to indicate how many instructions are from the Python loop which drives stmt.
A CallgrindStats object which provides instruction counts and some basic facilities for analyzing and manipulating results.
Mirrors the semantics of timeit.Timer.timeit().
Execute the main statement (stmt) number times. https://docs.python.org/3/library/timeit.html#timeit.Timer.timeit
The result of a Timer measurement.
This class stores one or more measurements of a given statement. It is serializable and provides several convenience methods (including a detailed __repr__) for downstream consumers.
Convenience method for merging replicates.
Merge will extrapolate times to number_per_run=1 and will not transfer any metadata. (Since it might differ between replicates)
Approximate significant figure estimate.
This property is intended to give a convenient way to estimate the precision of a measurement. It only uses the interquartile region to estimate statistics to try to mitigate skew from the tails, and uses a static z value of 1.645 since it is not expected to be used for small values of n, so z can approximate t.
The significant figure estimation used in conjunction with the trim_sigfig method to provide a more human interpretable data summary. __repr__ does not use this method; it simply displays raw values. Significant figure estimation is intended for Compare.
Top level container for Callgrind results collected by Timer.
Manipulation is generally done using the FunctionCounts class, which is obtained by calling CallgrindStats.stats(…). Several convenience methods are provided as well; the most significant is CallgrindStats.as_standardized().
Strip library names and some prefixes from function strings.
When comparing two different sets of instruction counts, on stumbling block can be path prefixes. Callgrind includes the full filepath when reporting a function (as it should). However, this can cause issues when diffing profiles. If a key component such as Python or PyTorch was built in separate locations in the two profiles, which can result in something resembling:
Stripping prefixes can ameliorate this issue by regularizing the strings and causing better cancellation of equivilent call sites when diffing.
Returns the total number of instructions executed.
See FunctionCounts.denoise() for an explation of the denoise arg.
Diff two sets of counts.
One common reason to collect instruction counts is to determine the the effect that a particular change will have on the number of instructions needed to perform some unit of work. If a change increases that number, the next logical question is “why”. This generally involves looking at what part if the code increased in instruction count. This function automates that process so that one can easily diff counts on both an inclusive and exclusive basis. The subtract_baselines argument allows one to disable baseline correction, though in most cases it shouldn’t matter as the baselines are expected to more or less cancel out.
Returns detailed function counts.
Conceptually, the FunctionCounts returned can be thought of as a tuple of (count, path_and_function_name) tuples.
inclusive matches the semantics of callgrind. If True, the counts include instructions executed by children. inclusive=True is useful for identifying hot spots in code; inclusive=False is useful for reducing noise when diffing counts from two different runs. (See CallgrindStats.delta(…) for more details)
Container for manipulating Callgrind results.
Remove known noisy instructions.
Several instructions in the CPython interpreter are rather noisy. These instructions involve unicode to dictionary lookups which Python uses to map variable names. FunctionCounts is generally a content agnostic container, however this is sufficiently important for obtaining reliable results to warrant an exception.
Keep only the elements where filter_fn applied to function name returns True.
Apply map_fn to all of the function names.
This can be used to regularize function names (e.g. stripping irrelevant parts of the file path), coalesce entries by mapping multiple functions to the same name (in which case the counts are added together), etc.