torch¶
The torch package contains data structures for multidimensional tensors and defines mathematical operations over these tensors. Additionally, it provides many utilities for efficient serialization of Tensors and arbitrary types, and other useful utilities.
It has a CUDA counterpart, that enables you to run your tensor computations on an NVIDIA GPU with compute capability >= 3.0.
Tensors¶
Returns True if obj is a PyTorch tensor. 

Returns True if obj is a PyTorch storage object. 

Returns True if the data type of 

Returns True if the 

Returns True if the data type of 

Returns True if the 

Sets the default floating point dtype to 

Get the current default floating point 

Sets the default 

Sets the default 

Returns the total number of elements in the 

Set options for printing. 

Disables denormal floating numbers on CPU. 
Creation Ops¶
Note
Random sampling creation ops are listed under Random sampling and
include:
torch.rand()
torch.rand_like()
torch.randn()
torch.randn_like()
torch.randint()
torch.randint_like()
torch.randperm()
You may also use torch.empty()
with the Inplace random sampling
methods to create torch.Tensor
s with values sampled from a broader
range of distributions.
Constructs a tensor with no autograd history (also known as a "leaf tensor", see Autograd mechanics) by copying 

Constructs a sparse tensor in COO(rdinate) format with specified values at the given 

Constructs a sparse tensor in CSR (Compressed Sparse Row) with specified values at the given 

Constructs a sparse tensor in CSC (Compressed Sparse Column) with specified values at the given 

Constructs a sparse tensor in BSR (Block Compressed Sparse Row)) with specified 2dimensional blocks at the given 

Constructs a sparse tensor in BSC (Block Compressed Sparse Column)) with specified 2dimensional blocks at the given 

Converts 

Converts 

Create a view of an existing torch.Tensor 

Creates a 

Converts a tensor from an external library into a 

Creates a 1dimensional 

Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument 

Returns a tensor filled with the scalar value 0, with the same size as 

Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument 

Returns a tensor filled with the scalar value 1, with the same size as 

Returns a 1D tensor of size $\left\lceil \frac{\text{end}  \text{start}}{\text{step}} \right\rceil$ with values from the interval 

Returns a 1D tensor of size $\left\lfloor \frac{\text{end}  \text{start}}{\text{step}} \right\rfloor + 1$ with values from 

Creates a onedimensional tensor of size 

Creates a onedimensional tensor of size 

Returns a 2D tensor with ones on the diagonal and zeros elsewhere. 

Returns a tensor filled with uninitialized data. 

Returns an uninitialized tensor with the same size as 

Creates a tensor with the specified 

Creates a tensor of size 

Returns a tensor with the same size as 

Converts a float tensor to a quantized tensor with given scale and zero point. 

Converts a float tensor to a perchannel quantized tensor with given scales and zero points. 

Returns an fp32 Tensor by dequantizing a quantized Tensor 

Constructs a complex tensor with its real part equal to 

Constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute value 

Computes the Heaviside step function for each element in 
Indexing, Slicing, Joining, Mutating Ops¶
Returns a view of the tensor conjugated and with the last two dimensions transposed. 

Returns a tensor containing the indices of all nonzero elements of 

Concatenates the given sequence of 

Alias of 

Alias of 

Returns a view of 

Attempts to split a tensor into the specified number of chunks. 

Splits 

Creates a new tensor by horizontally stacking the tensors in 

Stack tensors in sequence depthwise (along third axis). 

Gathers values along an axis specified by dim. 

Splits 

Stack tensors in sequence horizontally (column wise). 

See 

See 

See 

Returns a new tensor which indexes the 

Returns a new 1D tensor which indexes the 

Moves the dimension(s) of 

Alias for 

Returns a new tensor that is a narrowed version of 

Same as 

Returns a view of the original tensor 

Returns a tensor with the same data and number of elements as 

Alias of 

Slices the 

Outofplace version of 

Embeds the values of the 

Embeds the values of the 

Embeds the values of the 

Outofplace version of 

Outofplace version of 

Splits the tensor into chunks. 

Returns a tensor with all specified dimensions of 

Concatenates a sequence of tensors along a new dimension. 

Alias for 

Alias for 

Expects 

Returns a new tensor with the elements of 

Selects values from 

Splits a tensor into multiple subtensors, all of which are views of 

Constructs a tensor by repeating the elements of 

Returns a tensor that is a transposed version of 

Removes a tensor dimension. 

Returns a new tensor with a dimension of size one inserted at the specified position. 

Splits 

Stack tensors in sequence vertically (row wise). 

Return a tensor of elements selected from either 
Generators¶
Creates and returns a generator object that manages the state of the algorithm which produces pseudo random numbers. 
Random sampling¶
Sets the seed for generating random numbers to a nondeterministic random number. 

Sets the seed for generating random numbers. 

Returns the initial seed for generating random numbers as a Python long. 

Returns the random number generator state as a torch.ByteTensor. 

Sets the random number generator state. 
 torch.default_generator Returns the default CPU torch.Generator¶
Draws binary random numbers (0 or 1) from a Bernoulli distribution. 

Returns a tensor where each row contains 

Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given. 

Returns a tensor of the same size as 

Returns a tensor filled with random numbers from a uniform distribution on the interval $[0, 1)$ 

Returns a tensor with the same size as 

Returns a tensor filled with random integers generated uniformly between 

Returns a tensor with the same shape as Tensor 

Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution). 

Returns a tensor with the same size as 

Returns a random permutation of integers from 
Inplace random sampling¶
There are a few more inplace random sampling functions defined on Tensors as well. Click through to refer to their documentation:
torch.Tensor.bernoulli_()
 inplace version oftorch.bernoulli()
torch.Tensor.cauchy_()
 numbers drawn from the Cauchy distributiontorch.Tensor.exponential_()
 numbers drawn from the exponential distributiontorch.Tensor.geometric_()
 elements drawn from the geometric distributiontorch.Tensor.log_normal_()
 samples from the lognormal distributiontorch.Tensor.normal_()
 inplace version oftorch.normal()
torch.Tensor.random_()
 numbers sampled from the discrete uniform distributiontorch.Tensor.uniform_()
 numbers sampled from the continuous uniform distribution
Quasirandom sampling¶
The 
Serialization¶
Saves an object to a disk file. 

Loads an object saved with 
Parallelism¶
Returns the number of threads used for parallelizing CPU operations 

Sets the number of threads used for intraop parallelism on CPU. 

Returns the number of threads used for interop parallelism on CPU (e.g. 

Sets the number of threads used for interop parallelism (e.g. 
Locally disabling gradient computation¶
The context managers torch.no_grad()
, torch.enable_grad()
, and
torch.set_grad_enabled()
are helpful for locally disabling and enabling
gradient computation. See Locally disabling gradient computation for more details on
their usage. These context managers are thread local, so they won’t
work if you send work to another thread using the threading
module, etc.
Examples:
>>> x = torch.zeros(1, requires_grad=True)
>>> with torch.no_grad():
... y = x * 2
>>> y.requires_grad
False
>>> is_train = False
>>> with torch.set_grad_enabled(is_train):
... y = x * 2
>>> y.requires_grad
False
>>> torch.set_grad_enabled(True) # this can also be used as a function
>>> y = x * 2
>>> y.requires_grad
True
>>> torch.set_grad_enabled(False)
>>> y = x * 2
>>> y.requires_grad
False
Contextmanager that disables gradient calculation. 

Contextmanager that enables gradient calculation. 

Contextmanager that sets gradient calculation on or off. 

Returns True if grad mode is currently enabled. 

Contextmanager that enables or disables inference mode 

Returns True if inference mode is currently enabled. 
Math operations¶
Pointwise Ops¶
Computes the absolute value of each element in 

Alias for 

Computes the inverse cosine of each element in 

Alias for 

Returns a new tensor with the inverse hyperbolic cosine of the elements of 

Alias for 

Adds 

Performs the elementwise division of 

Performs the elementwise multiplication of 

Computes the elementwise angle (in radians) of the given 

Returns a new tensor with the arcsine of the elements of 

Alias for 

Returns a new tensor with the inverse hyperbolic sine of the elements of 

Alias for 

Returns a new tensor with the arctangent of the elements of 

Alias for 

Returns a new tensor with the inverse hyperbolic tangent of the elements of 

Alias for 

Elementwise arctangent of $\text{input}_{i} / \text{other}_{i}$ with consideration of the quadrant. 

Alias for 

Computes the bitwise NOT of the given input tensor. 

Computes the bitwise AND of 

Computes the bitwise OR of 

Computes the bitwise XOR of 

Computes the left arithmetic shift of 

Computes the right arithmetic shift of 

Returns a new tensor with the ceil of the elements of 

Alias for 

Computes the elementwise conjugate of the given 

Create a new floatingpoint tensor with the magnitude of 

Returns a new tensor with the cosine of the elements of 

Returns a new tensor with the hyperbolic cosine of the elements of 

Returns a new tensor with each of the elements of 

Divides each element of the input 

Alias for 

Alias for 

Alias for 

Alias for 

Alias for 

Returns a new tensor with the exponential of the elements of the input tensor 

Alias for 

Alias for 

Returns a new tensor with the data in 

Returns a new tensor with the data in 

Alias for 

Raises 

Returns a new tensor with the floor of the elements of 

Applies C++'s std::fmod entrywise. 

Computes the fractional portion of each element in 

Decomposes 

Estimates the gradient of a function $g : \mathbb{R}^n \rightarrow \mathbb{R}$ in one or more dimensions using the secondorder accurate central differences method and either first or second order estimates at the boundaries. 

Returns a new tensor containing imaginary values of the 

Multiplies 

Does a linear interpolation of two tensors 

Computes the natural logarithm of the absolute value of the gamma function on 

Returns a new tensor with the natural logarithm of the elements of 

Returns a new tensor with the logarithm to the base 10 of the elements of 

Returns a new tensor with the natural logarithm of (1 + 

Returns a new tensor with the logarithm to the base 2 of the elements of 

Logarithm of the sum of exponentiations of the inputs. 

Logarithm of the sum of exponentiations of the inputs in base2. 

Computes the elementwise logical AND of the given input tensors. 

Computes the elementwise logical NOT of the given input tensor. 

Computes the elementwise logical OR of the given input tensors. 

Computes the elementwise logical XOR of the given input tensors. 

Alias for 

Given the legs of a right triangle, return its hypotenuse. 

Alias for 

Alias for 

Alias for 

Multiplies 

Alias for 

Alias for 

Replaces 

Returns a new tensor with the negative of the elements of 

Alias for 

Return the next floatingpoint value after 

Alias for 

Returns 

Takes the power of each element in 

Applies batch normalization on a 4D (NCHW) quantized tensor. 

Applies a 1D max pooling over an input quantized tensor composed of several input planes. 

Applies a 2D max pooling over an input quantized tensor composed of several input planes. 

Returns a new tensor with each of the elements of 

Returns a new tensor containing real values of the 

Returns a new tensor with the reciprocal of the elements of 

Computes Python's modulus operation entrywise. 

Rounds elements of 

Returns a new tensor with the reciprocal of the squareroot of each of the elements of 

Alias for 

Returns a new tensor with the signs of the elements of 

This function is an extension of torch.sign() to complex tensors. 

Tests if each element of 

Returns a new tensor with the sine of the elements of 

Alias for 

Returns a new tensor with the hyperbolic sine of the elements of 

Alias for 

Returns a new tensor with the squareroot of the elements of 

Returns a new tensor with the square of the elements of 

Subtracts 

Alias for 

Returns a new tensor with the tangent of the elements of 

Returns a new tensor with the hyperbolic tangent of the elements of 

Alias for 

Returns a new tensor with the truncated integer values of the elements of 

Alias for 
Reduction Ops¶
Returns the indices of the maximum value of all elements in the 

Returns the indices of the minimum value(s) of the flattened tensor or along a dimension 

Returns the maximum value of each slice of the 

Returns the minimum value of each slice of the 

Computes the minimum and maximum values of the 

Tests if all elements in 

Tests if any element in 

Returns the maximum value of all elements in the 

Returns the minimum value of all elements in the 

Returns the pnorm of ( 

Returns the log of summed exponentials of each row of the 

Returns the mean value of all elements in the 

Computes the mean of all nonNaN elements along the specified dimensions. 

Returns the median of the values in 

Returns the median of the values in 

Returns a namedtuple 

Returns the matrix norm or vector norm of a given tensor. 

Returns the sum of all elements, treating Not a Numbers (NaNs) as zero. 

Returns the product of all elements in the 

Computes the qth quantiles of each row of the 

This is a variant of 

Calculates the standard deviation over the dimensions specified by 

Calculates the standard deviation and mean over the dimensions specified by 

Returns the sum of all elements in the 

Returns the unique elements of the input tensor. 

Eliminates all but the first element from every consecutive group of equivalent elements. 

Calculates the variance over the dimensions specified by 

Calculates the variance and mean over the dimensions specified by 

Counts the number of nonzero values in the tensor 
Comparison Ops¶
This function checks if 

Returns the indices that sort a tensor along a given dimension in ascending order by value. 

Computes elementwise equality 



Computes $\text{input} \geq \text{other}$ elementwise. 

Alias for 

Computes $\text{input} > \text{other}$ elementwise. 

Alias for 

Returns a new tensor with boolean elements representing if each element of 

Returns a new tensor with boolean elements representing if each element is finite or not. 

Tests if each element of 

Tests if each element of 

Tests if each element of 

Tests if each element of 

Returns a new tensor with boolean elements representing if each element of 

Returns a new tensor with boolean elements representing if each element of 

Returns a namedtuple 

Computes $\text{input} \leq \text{other}$ elementwise. 

Alias for 

Computes $\text{input} < \text{other}$ elementwise. 

Alias for 

Computes the elementwise maximum of 

Computes the elementwise minimum of 

Computes the elementwise maximum of 

Computes the elementwise minimum of 

Computes $\text{input} \neq \text{other}$ elementwise. 

Alias for 

Sorts the elements of the 

Returns the 

Sorts the elements of the 
Spectral Ops¶
Shorttime Fourier transform (STFT). 

Inverse short time Fourier Transform. 

Bartlett window function. 

Blackman window function. 

Hamming window function. 

Hann window function. 

Computes the Kaiser window with window length 
Other Operations¶
Returns a 1dimensional view of each input tensor with zero dimensions. 

Returns a 2dimensional view of each input tensor with zero dimensions. 

Returns a 3dimensional view of each input tensor with zero dimensions. 

Count the frequency of each value in an array of nonnegative ints. 

Create a block diagonal matrix from provided tensors. 

Broadcasts the given tensors according to Broadcasting semantics. 

Broadcasts 

Similar to 

Returns the indices of the buckets to which each value in the 

Do cartesian product of the given sequence of tensors. 

Computes batched the pnorm distance between each pair of the two collections of row vectors. 

Returns a copy of 

Compute combinations of length $r$ of the given tensor. 

Estimates the Pearson productmoment correlation coefficient matrix of the variables given by the 

Estimates the covariance matrix of the variables given by the 

Returns the cross product of vectors in dimension 

Returns a namedtuple 

Returns a namedtuple 

Returns the cumulative product of elements of 

Returns the cumulative sum of elements of 



Creates a tensor whose diagonals of certain 2D planes (specified by 



Returns a partial view of 

Computes the nth forward difference along the given dimension. 

Sums the product of the elements of the input 

Flattens 

Reverse the order of an nD tensor along given axis in dims. 

Flip tensor in the left/right direction, returning a new tensor. 

Flip tensor in the up/down direction, returning a new tensor. 

Computes the Kronecker product, denoted by $\otimes$, of 

Rotate an nD tensor by 90 degrees in the plane specified by dims axis. 

Computes the elementwise greatest common divisor (GCD) of 

Computes the histogram of a tensor. 

Computes a histogram of the values in a tensor. 

Computes a multidimensional histogram of the values in a tensor. 

Creates grids of coordinates specified by the 1D inputs in attr:tensors. 

Computes the elementwise least common multiple (LCM) of 

Returns the logarithm of the cumulative summation of the exponentiation of elements of 

Return a contiguous flattened tensor. 

Returns a tensor where each subtensor of 

Repeat elements of a tensor. 

Roll the tensor 

Find the indices from the innermost dimension of 

Returns a contraction of a and b over multiple dimensions. 

Returns the sum of the elements of the diagonal of the input 2D matrix. 

Returns the lower triangular part of the matrix (2D tensor) or batch of matrices 

Returns the indices of the lower triangular part of a 

Returns the upper triangular part of a matrix (2D tensor) or batch of matrices 

Returns the indices of the upper triangular part of a 

Expands a dimension of the input tensor over multiple dimensions. 

Generates a Vandermonde matrix. 

Returns a view of 

Returns a view of 

Returns a new tensor with materialized conjugation if 

Returns a new tensor with materialized negation if 
BLAS and LAPACK Operations¶
Performs a batch matrixmatrix product of matrices stored in 

Performs a matrix multiplication of the matrices 

Performs a matrixvector product of the matrix 

Performs the outerproduct of vectors 

Performs a batch matrixmatrix product of matrices in 

Performs a batch matrixmatrix product of matrices stored in 

Returns the matrix product of the $N$ 2D tensors. 

Computes the Cholesky decomposition of a symmetric positivedefinite matrix $A$ or for batches of symmetric positivedefinite matrices. 

Computes the inverse of a symmetric positivedefinite matrix $A$ using its Cholesky factor $u$: returns matrix 

Solves a linear system of equations with a positive semidefinite matrix to be inverted given its Cholesky factor matrix $u$. 

Computes the dot product of two 1D tensors. 

This is a lowlevel function for calling LAPACK's geqrf directly. 

Alias of 

Computes the dot product for 1D tensors. 

Alias for 

Alias for 

Calculates log determinant of a square matrix or batches of square matrices. 

Alias for 

Computes the LU factorization of a matrix or batches of matrices 

Returns the LU solve of the linear system $Ax = b$ using the partially pivoted LU factorization of A from 

Unpacks the LU decomposition returned by 

Matrix product of two tensors. 

Alias for 

Alias for 

Performs a matrix multiplication of the matrices 

Performs a matrixvector product of the matrix 

Alias for 

Computes the matrixmatrix multiplication of a product of Householder matrices with a general matrix. 

Outer product of 

Alias for 

Computes the QR decomposition of a matrix or a batch of matrices 

Computes the singular value decomposition of either a matrix or batch of matrices 

Return the singular value decomposition 

Performs linear Principal Component Analysis (PCA) on a lowrank matrix, batches of such matrices, or sparse matrix. 

Find the k largest (or smallest) eigenvalues and the corresponding eigenvectors of a symmetric positive definite generalized eigenvalue problem using matrixfree LOBPCG methods. 

Alias for 

Computes the trapezoidal rule along 

Cumulatively computes the trapezoidal rule along 

Solves a system of equations with a square upper or lower triangular invertible matrix $A$ and multiple righthand sides $b$. 

Computes the dot product of two 1D vectors along a dimension. 
Foreach Operations¶
Warning
This API is in beta and subject to future changes. Forwardmode AD is not supported.
Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 

Apply 
Utilities¶
Returns whether PyTorch was built with _GLIBCXX_USE_CXX11_ABI=1 

Returns the 

Determines if a type conversion is allowed under PyTorch casting rules described in the type promotion documentation. 

Returns the 

Sets whether PyTorch operations must use "deterministic" algorithms. 

Returns True if the global deterministic flag is turned on. 

Returns True if the global deterministic flag is set to warn only. 

Sets the debug mode for deterministic operations. 

Returns the current value of the debug mode for deterministic operations. 

Sets the internal precision of float32 matrix multiplications. 

Returns the current value of float32 matrix multiplication precision. 

When this flag is False (default) then some PyTorch warnings may only appear once per process. 

Returns True if the global warn_always flag is turned on. 

vmap is the vectorizing map; 

A wrapper around Python's assert which is symbolically traceable. 
Symbolic Numbers¶
 class torch.SymInt(node)[source]¶
Like an int (including magic methods), but redirects all operations on the wrapped node. This is used in particular to symbolically record operations in the symbolic shape workflow.
 class torch.SymFloat(node)[source]¶
Like an float (including magic methods), but redirects all operations on the wrapped node. This is used in particular to symbolically record operations in the symbolic shape workflow.
 class torch.SymBool(node)[source]¶
Like an bool (including magic methods), but redirects all operations on the wrapped node. This is used in particular to symbolically record operations in the symbolic shape workflow.
Unlike regular bools, regular boolean operators will force extra guards instead of symbolically evaluate. Use the bitwise operators instead to handle this.
SymIntaware utility for float casting. 

SymIntaware utility for int casting. 

SymIntaware utility for max(). 

SymIntaware utility for max(). 

SymIntaware utility for logical negation. 
Export Path¶
Warning
This feature is a prototype and may have compatibility breaking changes in the future.
export generated/exportdb/index
Optimizations¶
Optimizes given model/function using TorchDynamo and specified backend. 