Rate this Page

torch#

Created On: Dec 23, 2016 | Last Updated On: Jul 22, 2025

The torch package contains data structures for multi-dimensionaltensors and defines mathematical operations over these tensors.Additionally, it provides many utilities for efficient serialization ofTensors and arbitrary types, and other useful utilities.

It has a CUDA counterpart, that enables you to run your tensor computationson an NVIDIA GPU with compute capability >= 3.0.

Tensors#

is_tensor

Returns True ifobj is a PyTorch tensor.

is_storage

Returns True ifobj is a PyTorch storage object.

is_complex

Returns True if the data type ofinput is a complex data type i.e., one oftorch.complex64, andtorch.complex128.

is_conj

Returns True if theinput is a conjugated tensor, i.e. its conjugate bit is set toTrue.

is_floating_point

Returns True if the data type ofinput is a floating point data type i.e., one oftorch.float64,torch.float32,torch.float16, andtorch.bfloat16.

is_nonzero

Returns True if theinput is a single element tensor which is not equal to zero after type conversions.

set_default_dtype

Sets the default floating point dtype tod.

get_default_dtype

Get the current default floating pointtorch.dtype.

set_default_device

Sets the defaulttorch.Tensor to be allocated ondevice.

get_default_device

Gets the defaulttorch.Tensor to be allocated ondevice

set_default_tensor_type

numel

Returns the total number of elements in theinput tensor.

set_printoptions

Set options for printing.

set_flush_denormal

Disables denormal floating numbers on CPU.

Creation Ops#

Note

Random sampling creation ops are listed underRandom sampling andinclude:torch.rand()torch.rand_like()torch.randn()torch.randn_like()torch.randint()torch.randint_like()torch.randperm()You may also usetorch.empty() with theIn-place random samplingmethods to createtorch.Tensor s with values sampled from a broaderrange of distributions.

tensor

Constructs a tensor with no autograd history (also known as a "leaf tensor", seeAutograd mechanics) by copyingdata.

sparse_coo_tensor

Constructs asparse tensor in COO(rdinate) format with specified values at the givenindices.

sparse_csr_tensor

Constructs asparse tensor in CSR (Compressed Sparse Row) with specified values at the givencrow_indices andcol_indices.

sparse_csc_tensor

Constructs asparse tensor in CSC (Compressed Sparse Column) with specified values at the givenccol_indices androw_indices.

sparse_bsr_tensor

Constructs asparse tensor in BSR (Block Compressed Sparse Row)) with specified 2-dimensional blocks at the givencrow_indices andcol_indices.

sparse_bsc_tensor

Constructs asparse tensor in BSC (Block Compressed Sparse Column)) with specified 2-dimensional blocks at the givenccol_indices androw_indices.

asarray

Convertsobj to a tensor.

as_tensor

Convertsdata into a tensor, sharing data and preserving autograd history if possible.

as_strided

Create a view of an existingtorch.Tensorinput with specifiedsize,stride andstorage_offset.

from_file

Creates a CPU tensor with a storage backed by a memory-mapped file.

from_numpy

Creates aTensor from anumpy.ndarray.

from_dlpack

Converts a tensor from an external library into atorch.Tensor.

frombuffer

Creates a 1-dimensionalTensor from an object that implements the Python buffer protocol.

zeros

Returns a tensor filled with the scalar value0, with the shape defined by the variable argumentsize.

zeros_like

Returns a tensor filled with the scalar value0, with the same size asinput.

ones

Returns a tensor filled with the scalar value1, with the shape defined by the variable argumentsize.

ones_like

Returns a tensor filled with the scalar value1, with the same size asinput.

arange

Returns a 1-D tensor of sizeendstartstep\left\lceil \frac{\text{end} - \text{start}}{\text{step}} \right\rceil with values from the interval[start,end) taken with common differencestep beginning fromstart.

range

Returns a 1-D tensor of sizeendstartstep+1\left\lfloor \frac{\text{end} - \text{start}}{\text{step}} \right\rfloor + 1 with values fromstart toend with stepstep.

linspace

Creates a one-dimensional tensor of sizesteps whose values are evenly spaced fromstart toend, inclusive.

logspace

Creates a one-dimensional tensor of sizesteps whose values are evenly spaced frombasestart{{\text{{base}}}}^{{\text{{start}}}} tobaseend{{\text{{base}}}}^{{\text{{end}}}}, inclusive, on a logarithmic scale with basebase.

eye

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

empty

Returns a tensor filled with uninitialized data.

empty_like

Returns an uninitialized tensor with the same size asinput.

empty_strided

Creates a tensor with the specifiedsize andstride and filled with undefined data.

full

Creates a tensor of sizesize filled withfill_value.

full_like

Returns a tensor with the same size asinput filled withfill_value.

quantize_per_tensor

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

quantize_per_channel

Converts a float tensor to a per-channel quantized tensor with given scales and zero points.

dequantize

Returns an fp32 Tensor by dequantizing a quantized Tensor

complex

Constructs a complex tensor with its real part equal toreal and its imaginary part equal toimag.

polar

Constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute valueabs and angleangle.

heaviside

Computes the Heaviside step function for each element ininput.

Indexing, Slicing, Joining, Mutating Ops#

adjoint

Returns a view of the tensor conjugated and with the last two dimensions transposed.

argwhere

Returns a tensor containing the indices of all non-zero elements ofinput.

cat

Concatenates the given sequence of tensors intensors in the given dimension.

concat

Alias oftorch.cat().

concatenate

Alias oftorch.cat().

conj

Returns a view ofinput with a flipped conjugate bit.

chunk

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

dsplit

Splitsinput, a tensor with three or more dimensions, into multiple tensors depthwise according toindices_or_sections.

column_stack

Creates a new tensor by horizontally stacking the tensors intensors.

dstack

Stack tensors in sequence depthwise (along third axis).

gather

Gathers values along an axis specified bydim.

hsplit

Splitsinput, a tensor with one or more dimensions, into multiple tensors horizontally according toindices_or_sections.

hstack

Stack tensors in sequence horizontally (column wise).

index_add

Seeindex_add_() for function description.

index_copy

Seeindex_add_() for function description.

index_reduce

Seeindex_reduce_() for function description.

index_select

Returns a new tensor which indexes theinput tensor along dimensiondim using the entries inindex which is aLongTensor.

masked_select

Returns a new 1-D tensor which indexes theinput tensor according to the boolean maskmask which is aBoolTensor.

movedim

Moves the dimension(s) ofinput at the position(s) insource to the position(s) indestination.

moveaxis

Alias fortorch.movedim().

narrow

Returns a new tensor that is a narrowed version ofinput tensor.

narrow_copy

Same asTensor.narrow() except this returns a copy rather than shared storage.

nonzero

permute

Returns a view of the original tensorinput with its dimensions permuted.

reshape

Returns a tensor with the same data and number of elements asinput, but with the specified shape.

row_stack

Alias oftorch.vstack().

select

Slices theinput tensor along the selected dimension at the given index.

scatter

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

diagonal_scatter

Embeds the values of thesrc tensor intoinput along the diagonal elements ofinput, with respect todim1 anddim2.

select_scatter

Embeds the values of thesrc tensor intoinput at the given index.

slice_scatter

Embeds the values of thesrc tensor intoinput at the given dimension.

scatter_add

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

scatter_reduce

Out-of-place version oftorch.Tensor.scatter_reduce_()

segment_reduce

Perform a segment reduction operation on the input tensor along the specified axis.

split

Splits the tensor into chunks.

squeeze

Returns a tensor with all specified dimensions ofinput of size1 removed.

stack

Concatenates a sequence of tensors along a new dimension.

swapaxes

Alias fortorch.transpose().

swapdims

Alias fortorch.transpose().

t

Expectsinput to be <= 2-D tensor and transposes dimensions 0 and 1.

take

Returns a new tensor with the elements ofinput at the given indices.

take_along_dim

Selects values frominput at the 1-dimensional indices fromindices along the givendim.

tensor_split

Splits a tensor into multiple sub-tensors, all of which are views ofinput, along dimensiondim according to the indices or number of sections specified byindices_or_sections.

tile

Constructs a tensor by repeating the elements ofinput.

transpose

Returns a tensor that is a transposed version ofinput.

unbind

Removes a tensor dimension.

unravel_index

Converts a tensor of flat indices into a tuple of coordinate tensors that index into an arbitrary tensor of the specified shape.

unsqueeze

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

vsplit

Splitsinput, a tensor with two or more dimensions, into multiple tensors vertically according toindices_or_sections.

vstack

Stack tensors in sequence vertically (row wise).

where

Return a tensor of elements selected from eitherinput orother, depending oncondition.

Accelerators#

Within the PyTorch repo, we define an “Accelerator” as atorch.device that is being usedalongside a CPU to speed up computation. These device use an asynchronous execution scheme,usingtorch.Stream andtorch.Event as their main way to perform synchronization.We also assume that only one such accelerator can be available at once on a given host. This allowsus to use the current accelerator as the default device for relevant concepts such as pinned memory,Stream device_type, FSDP, etc.

As of today, accelerator devices are (in no particular order)“CUDA”,“MTIA”,“XPU”,“MPS”, “HPU”, and PrivateUse1 (many device not in the PyTorch repo itself).

Many tools in the PyTorch Ecosystem use fork to create subprocesses (for example dataloadingor intra-op parallelism), it is thus important to delay as much as possible anyoperation that would prevent further forks. This is especially important here as most accelerator’s initialization has such effect.In practice, you should keep in mind that checkingtorch.accelerator.current_accelerator()is a compile-time check by default, it is thus always fork-safe.On the contrary, passing thecheck_available=True flag to this function or callingtorch.accelerator.is_available() will usually prevent later fork.

Some backends provide an experimental opt-in option to make the runtime availabilitycheck fork-safe. When using the CUDA devicePYTORCH_NVML_BASED_CUDA_CHECK=1 can beused for example.

Stream

An in-order queue of executing the respective tasks asynchronously in first in first out (FIFO) order.

Event

Query and record Stream status to identify or control dependencies across Stream and measure timing.

Generators#

Generator

Creates and returns a generator object that manages the state of the algorithm which produces pseudo random numbers.

Random sampling#

seed

Sets the seed for generating random numbers to a non-deterministic random number on all devices.

manual_seed

Sets the seed for generating random numbers on all devices.

initial_seed

Returns the initial seed for generating random numbers as a Pythonlong.

get_rng_state

Returns the random number generator state as atorch.ByteTensor.

set_rng_state

Sets the random number generator state.

torch.default_generatorReturnsthedefaultCPUtorch.Generator#

bernoulli

Draws binary random numbers (0 or 1) from a Bernoulli distribution.

multinomial

Returns a tensor where each row containsnum_samples indices sampled from the multinomial (a stricter definition would be multivariate, refer totorch.distributions.multinomial.Multinomial for more details) probability distribution located in the corresponding row of tensorinput.

normal

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

poisson

Returns a tensor of the same size asinput with each element sampled from a Poisson distribution with rate parameter given by the corresponding element ininput i.e.,

rand

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

rand_like

Returns a tensor with the same size asinput that is filled with random numbers from a uniform distribution on the interval[0,1)[0, 1).

randint

Returns a tensor filled with random integers generated uniformly betweenlow (inclusive) andhigh (exclusive).

randint_like

Returns a tensor with the same shape as Tensorinput filled with random integers generated uniformly betweenlow (inclusive) andhigh (exclusive).

randn

Returns a tensor filled with random numbers from a normal distribution with mean0 and variance1 (also called the standard normal distribution).

randn_like

Returns a tensor with the same size asinput that is filled with random numbers from a normal distribution with mean 0 and variance 1.

randperm

Returns a random permutation of integers from0 ton-1.

In-place random sampling#

There are a few more in-place random sampling functions defined on Tensors as well. Click through to refer to their documentation:

Quasi-random sampling#

quasirandom.SobolEngine

Thetorch.quasirandom.SobolEngine is an engine for generating (scrambled) Sobol sequences.

Serialization#

save

Saves an object to a disk file.

load

Loads an object saved withtorch.save() from a file.

Parallelism#

get_num_threads

Returns the number of threads used for parallelizing CPU operations

set_num_threads

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

get_num_interop_threads

Returns the number of threads used for inter-op parallelism on CPU (e.g.

set_num_interop_threads

Sets the number of threads used for interop parallelism (e.g.

Locally disabling gradient computation#

The context managerstorch.no_grad(),torch.enable_grad(), andtorch.set_grad_enabled() are helpful for locally disabling and enablinggradient computation. SeeLocally disabling gradient computation for more details ontheir usage. These context managers are thread local, so they won’twork if you send work to another thread using thethreading module, etc.

Examples:

>>>x=torch.zeros(1,requires_grad=True)>>>withtorch.no_grad():...y=x*2>>>y.requires_gradFalse>>>is_train=False>>>withtorch.set_grad_enabled(is_train):...y=x*2>>>y.requires_gradFalse>>>torch.set_grad_enabled(True)# this can also be used as a function>>>y=x*2>>>y.requires_gradTrue>>>torch.set_grad_enabled(False)>>>y=x*2>>>y.requires_gradFalse

no_grad

Context-manager that disables gradient calculation.

enable_grad

Context-manager that enables gradient calculation.

autograd.grad_mode.set_grad_enabled

Context-manager that sets gradient calculation on or off.

is_grad_enabled

Returns True if grad mode is currently enabled.

autograd.grad_mode.inference_mode

Context manager that enables or disables inference mode.

is_inference_mode_enabled

Returns True if inference mode is currently enabled.

Math operations#

Constants#

inf

A floating-point positive infinity. Alias formath.inf.

nan

A floating-point “not a number” value. This value is not a legal number. Alias formath.nan.

Pointwise Ops#

abs

Computes the absolute value of each element ininput.

absolute

Alias fortorch.abs()

acos

Computes the inverse cosine of each element ininput.

arccos

Alias fortorch.acos().

acosh

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

arccosh

Alias fortorch.acosh().

add

Addsother, scaled byalpha, toinput.

addcdiv

Performs the element-wise division oftensor1 bytensor2, multiplies the result by the scalarvalue and adds it toinput.

addcmul

Performs the element-wise multiplication oftensor1 bytensor2, multiplies the result by the scalarvalue and adds it toinput.

angle

Computes the element-wise angle (in radians) of the giveninput tensor.

asin

Returns a new tensor with the arcsine of the elements ofinput.

arcsin

Alias fortorch.asin().

asinh

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

arcsinh

Alias fortorch.asinh().

atan

Returns a new tensor with the arctangent of the elements ofinput.

arctan

Alias fortorch.atan().

atanh

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

arctanh

Alias fortorch.atanh().

atan2

Element-wise arctangent ofinputi/otheri\text{input}_{i} / \text{other}_{i} with consideration of the quadrant.

arctan2

Alias fortorch.atan2().

bitwise_not

Computes the bitwise NOT of the given input tensor.

bitwise_and

Computes the bitwise AND ofinput andother.

bitwise_or

Computes the bitwise OR ofinput andother.

bitwise_xor

Computes the bitwise XOR ofinput andother.

bitwise_left_shift

Computes the left arithmetic shift ofinput byother bits.

bitwise_right_shift

Computes the right arithmetic shift ofinput byother bits.

ceil

Returns a new tensor with the ceil of the elements ofinput, the smallest integer greater than or equal to each element.

clamp

Clamps all elements ininput into the range[min,max].

clip

Alias fortorch.clamp().

conj_physical

Computes the element-wise conjugate of the giveninput tensor.

copysign

Create a new floating-point tensor with the magnitude ofinput and the sign ofother, elementwise.

cos

Returns a new tensor with the cosine of the elements ofinput.

cosh

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

deg2rad

Returns a new tensor with each of the elements ofinput converted from angles in degrees to radians.

div

Divides each element of the inputinput by the corresponding element ofother.

divide

Alias fortorch.div().

digamma

Alias fortorch.special.digamma().

erf

Alias fortorch.special.erf().

erfc

Alias fortorch.special.erfc().

erfinv

Alias fortorch.special.erfinv().

exp

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

exp2

Alias fortorch.special.exp2().

expm1

Alias fortorch.special.expm1().

fake_quantize_per_channel_affine

Returns a new tensor with the data ininput fake quantized per channel usingscale,zero_point,quant_min andquant_max, across the channel specified byaxis.

fake_quantize_per_tensor_affine

Returns a new tensor with the data ininput fake quantized usingscale,zero_point,quant_min andquant_max.

fix

Alias fortorch.trunc()

float_power

Raisesinput to the power ofexponent, elementwise, in double precision.

floor

Returns a new tensor with the floor of the elements ofinput, the largest integer less than or equal to each element.

floor_divide

fmod

Applies C++'sstd::fmod entrywise.

frac

Computes the fractional portion of each element ininput.

frexp

Decomposesinput into mantissa and exponent tensors such thatinput=mantissa×2exponent\text{input} = \text{mantissa} \times 2^{\text{exponent}}.

gradient

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

imag

Returns a new tensor containing imaginary values of theself tensor.

ldexp

Multipliesinput by 2 **other.

lerp

Does a linear interpolation of two tensorsstart (given byinput) andend based on a scalar or tensorweight and returns the resultingout tensor.

lgamma

Computes the natural logarithm of the absolute value of the gamma function oninput.

log

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

log10

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

log1p

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

log2

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

logaddexp

Logarithm of the sum of exponentiations of the inputs.

logaddexp2

Logarithm of the sum of exponentiations of the inputs in base-2.

logical_and

Computes the element-wise logical AND of the given input tensors.

logical_not

Computes the element-wise logical NOT of the given input tensor.

logical_or

Computes the element-wise logical OR of the given input tensors.

logical_xor

Computes the element-wise logical XOR of the given input tensors.

logit

Alias fortorch.special.logit().

hypot

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

i0

Alias fortorch.special.i0().

igamma

Alias fortorch.special.gammainc().

igammac

Alias fortorch.special.gammaincc().

mul

Multipliesinput byother.

multiply

Alias fortorch.mul().

mvlgamma

Alias fortorch.special.multigammaln().

nan_to_num

ReplacesNaN, positive infinity, and negative infinity values ininput with the values specified bynan,posinf, andneginf, respectively.

neg

Returns a new tensor with the negative of the elements ofinput.

negative

Alias fortorch.neg()

nextafter

Return the next floating-point value afterinput towardsother, elementwise.

polygamma

Alias fortorch.special.polygamma().

positive

Returnsinput.

pow

Takes the power of each element ininput withexponent and returns a tensor with the result.

quantized_batch_norm

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

quantized_max_pool1d

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

quantized_max_pool2d

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

rad2deg

Returns a new tensor with each of the elements ofinput converted from angles in radians to degrees.

real

Returns a new tensor containing real values of theself tensor.

reciprocal

Returns a new tensor with the reciprocal of the elements ofinput

remainder

ComputesPython's modulus operation entrywise.

round

Rounds elements ofinput to the nearest integer.

rsqrt

Returns a new tensor with the reciprocal of the square-root of each of the elements ofinput.

sigmoid

Alias fortorch.special.expit().

sign

Returns a new tensor with the signs of the elements ofinput.

sgn

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

signbit

Tests if each element ofinput has its sign bit set or not.

sin

Returns a new tensor with the sine of the elements ofinput.

sinc

Alias fortorch.special.sinc().

sinh

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

softmax

Alias fortorch.nn.functional.softmax().

sqrt

Returns a new tensor with the square-root of the elements ofinput.

square

Returns a new tensor with the square of the elements ofinput.

sub

Subtractsother, scaled byalpha, frominput.

subtract

Alias fortorch.sub().

tan

Returns a new tensor with the tangent of the elements ofinput.

tanh

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

true_divide

Alias fortorch.div() withrounding_mode=None.

trunc

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

xlogy

Alias fortorch.special.xlogy().

Reduction Ops#

argmax

Returns the indices of the maximum value of all elements in theinput tensor.

argmin

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

amax

Returns the maximum value of each slice of theinput tensor in the given dimension(s)dim.

amin

Returns the minimum value of each slice of theinput tensor in the given dimension(s)dim.

aminmax

Computes the minimum and maximum values of theinput tensor.

all

Tests if all elements ininput evaluate toTrue.

any

Tests if any element ininput evaluates toTrue.

max

Returns the maximum value of all elements in theinput tensor.

min

Returns the minimum value of all elements in theinput tensor.

dist

Returns the p-norm of (input -other)

logsumexp

Returns the log of summed exponentials of each row of theinput tensor in the given dimensiondim.

mean

nanmean

Computes the mean of allnon-NaN elements along the specified dimensions.

median

Returns the median of the values ininput.

nanmedian

Returns the median of the values ininput, ignoringNaN values.

mode

Returns a namedtuple(values,indices) wherevalues is the mode value of each row of theinput tensor in the given dimensiondim, i.e. a value which appears most often in that row, andindices is the index location of each mode value found.

norm

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

nansum

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

prod

Returns the product of all elements in theinput tensor.

quantile

Computes the q-th quantiles of each row of theinput tensor along the dimensiondim.

nanquantile

This is a variant oftorch.quantile() that "ignores"NaN values, computing the quantilesq as ifNaN values ininput did not exist.

std

Calculates the standard deviation over the dimensions specified bydim.

std_mean

Calculates the standard deviation and mean over the dimensions specified bydim.

sum

Returns the sum of all elements in theinput tensor.

unique

Returns the unique elements of the input tensor.

unique_consecutive

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

var

Calculates the variance over the dimensions specified bydim.

var_mean

Calculates the variance and mean over the dimensions specified bydim.

count_nonzero

Counts the number of non-zero values in the tensorinput along the givendim.

hash_tensor

Returns a hash of all elements in theinput tensor.

Comparison Ops#

allclose

This function checks ifinput andother satisfy the condition:

argsort

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

eq

Computes element-wise equality

equal

True if two tensors have the same size and elements,False otherwise.

ge

Computesinputother\text{input} \geq \text{other} element-wise.

greater_equal

Alias fortorch.ge().

gt

Computesinput>other\text{input} > \text{other} element-wise.

greater

Alias fortorch.gt().

isclose

Returns a new tensor with boolean elements representing if each element ofinput is "close" to the corresponding element ofother.

isfinite

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

isin

Tests if each element ofelements is intest_elements.

isinf

Tests if each element ofinput is infinite (positive or negative infinity) or not.

isposinf

Tests if each element ofinput is positive infinity or not.

isneginf

Tests if each element ofinput is negative infinity or not.

isnan

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

isreal

Returns a new tensor with boolean elements representing if each element ofinput is real-valued or not.

kthvalue

Returns a namedtuple(values,indices) wherevalues is thek th smallest element of each row of theinput tensor in the given dimensiondim.

le

Computesinputother\text{input} \leq \text{other} element-wise.

less_equal

Alias fortorch.le().

lt

Computesinput<other\text{input} < \text{other} element-wise.

less

Alias fortorch.lt().

maximum

Computes the element-wise maximum ofinput andother.

minimum

Computes the element-wise minimum ofinput andother.

fmax

Computes the element-wise maximum ofinput andother.

fmin

Computes the element-wise minimum ofinput andother.

ne

Computesinputother\text{input} \neq \text{other} element-wise.

not_equal

Alias fortorch.ne().

sort

Sorts the elements of theinput tensor along a given dimension in ascending order by value.

topk

Returns thek largest elements of the giveninput tensor along a given dimension.

msort

Sorts the elements of theinput tensor along its first dimension in ascending order by value.

Spectral Ops#

stft

Short-time Fourier transform (STFT).

istft

Inverse short time Fourier Transform.

bartlett_window

Bartlett window function.

blackman_window

Blackman window function.

hamming_window

Hamming window function.

hann_window

Hann window function.

kaiser_window

Computes the Kaiser window with window lengthwindow_length and shape parameterbeta.

Other Operations#

atleast_1d

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

atleast_2d

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

atleast_3d

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

bincount

Count the frequency of each value in an array of non-negative ints.

block_diag

Create a block diagonal matrix from provided tensors.

broadcast_tensors

Broadcasts the given tensors according toBroadcasting semantics.

broadcast_to

Broadcastsinput to the shapeshape.

broadcast_shapes

Similar tobroadcast_tensors() but for shapes.

bucketize

Returns the indices of the buckets to which each value in theinput belongs, where the boundaries of the buckets are set byboundaries.

cartesian_prod

Do cartesian product of the given sequence of tensors.

cdist

Computes batched the p-norm distance between each pair of the two collections of row vectors.

clone

Returns a copy ofinput.

combinations

Compute combinations of lengthrr of the given tensor.

corrcoef

Estimates the Pearson product-moment correlation coefficient matrix of the variables given by theinput matrix, where rows are the variables and columns are the observations.

cov

Estimates the covariance matrix of the variables given by theinput matrix, where rows are the variables and columns are the observations.

cross

Returns the cross product of vectors in dimensiondim ofinput andother.

cummax

Returns a namedtuple(values,indices) wherevalues is the cumulative maximum of elements ofinput in the dimensiondim.

cummin

Returns a namedtuple(values,indices) wherevalues is the cumulative minimum of elements ofinput in the dimensiondim.

cumprod

Returns the cumulative product of elements ofinput in the dimensiondim.

cumsum

Returns the cumulative sum of elements ofinput in the dimensiondim.

diag

  • Ifinput is a vector (1-D tensor), then returns a 2-D square tensor

diag_embed

Creates a tensor whose diagonals of certain 2D planes (specified bydim1 anddim2) are filled byinput.

diagflat

  • Ifinput is a vector (1-D tensor), then returns a 2-D square tensor

diagonal

Returns a partial view ofinput with the its diagonal elements with respect todim1 anddim2 appended as a dimension at the end of the shape.

diff

Computes the n-th forward difference along the given dimension.

einsum

Sums the product of the elements of the inputoperands along dimensions specified using a notation based on the Einstein summation convention.

flatten

Flattensinput by reshaping it into a one-dimensional tensor.

flip

Reverse the order of an n-D tensor along given axis in dims.

fliplr

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

flipud

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

kron

Computes the Kronecker product, denoted by\otimes, ofinput andother.

rot90

Rotate an n-D tensor by 90 degrees in the plane specified by dims axis.

gcd

Computes the element-wise greatest common divisor (GCD) ofinput andother.

histc

Computes the histogram of a tensor.

histogram

Computes a histogram of the values in a tensor.

histogramdd

Computes a multi-dimensional histogram of the values in a tensor.

meshgrid

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

lcm

Computes the element-wise least common multiple (LCM) ofinput andother.

logcumsumexp

Returns the logarithm of the cumulative summation of the exponentiation of elements ofinput in the dimensiondim.

ravel

Return a contiguous flattened tensor.

renorm

Returns a tensor where each sub-tensor ofinput along dimensiondim is normalized such that thep-norm of the sub-tensor is lower than the valuemaxnorm

repeat_interleave

Repeat elements of a tensor.

roll

Roll the tensorinput along the given dimension(s).

searchsorted

Find the indices from theinnermost dimension ofsorted_sequence such that, if the corresponding values invalues were inserted before the indices, when sorted, the order of the correspondinginnermost dimension withinsorted_sequence would be preserved.

tensordot

Returns a contraction of a and b over multiple dimensions.

trace

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

tril

Returns the lower triangular part of the matrix (2-D tensor) or batch of matricesinput, the other elements of the result tensorout are set to 0.

tril_indices

Returns the indices of the lower triangular part of arow-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.

triu

Returns the upper triangular part of a matrix (2-D tensor) or batch of matricesinput, the other elements of the result tensorout are set to 0.

triu_indices

Returns the indices of the upper triangular part of arow bycol matrix in a 2-by-N Tensor, where the first row contains row coordinates of all indices and the second row contains column coordinates.

unflatten

Expands a dimension of the input tensor over multiple dimensions.

vander

Generates a Vandermonde matrix.

view_as_real

Returns a view ofinput as a real tensor.

view_as_complex

Returns a view ofinput as a complex tensor.

resolve_conj

Returns a new tensor with materialized conjugation ifinput's conjugate bit is set toTrue, else returnsinput.

resolve_neg

Returns a new tensor with materialized negation ifinput's negative bit is set toTrue, else returnsinput.

BLAS and LAPACK Operations#

addbmm

Performs a batch matrix-matrix product of matrices stored inbatch1 andbatch2, with a reduced add step (all matrix multiplications get accumulated along the first dimension).

addmm

Performs a matrix multiplication of the matricesmat1 andmat2.

addmv

Performs a matrix-vector product of the matrixmat and the vectorvec.

addr

Performs the outer-product of vectorsvec1 andvec2 and adds it to the matrixinput.

baddbmm

Performs a batch matrix-matrix product of matrices inbatch1 andbatch2.

bmm

Performs a batch matrix-matrix product of matrices stored ininput andmat2.

chain_matmul

Returns the matrix product of theNN 2-D tensors.

cholesky

Computes the Cholesky decomposition of a symmetric positive-definite matrixAA or for batches of symmetric positive-definite matrices.

cholesky_inverse

Computes the inverse of a complex Hermitian or real symmetric positive-definite matrix given its Cholesky decomposition.

cholesky_solve

Computes the solution of a system of linear equations with complex Hermitian or real symmetric positive-definite lhs given its Cholesky decomposition.

dot

Computes the dot product of two 1D tensors.

geqrf

This is a low-level function for calling LAPACK's geqrf directly.

ger

Alias oftorch.outer().

inner

Computes the dot product for 1D tensors.

inverse

Alias fortorch.linalg.inv()

det

Alias fortorch.linalg.det()

logdet

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

slogdet

Alias fortorch.linalg.slogdet()

lu

Computes the LU factorization of a matrix or batches of matricesA.

lu_solve

Returns the LU solve of the linear systemAx=bAx = b using the partially pivoted LU factorization of A fromlu_factor().

lu_unpack

Unpacks the LU decomposition returned bylu_factor() into theP, L, U matrices.

matmul

Matrix product of two tensors.

matrix_power

Alias fortorch.linalg.matrix_power()

matrix_exp

Alias fortorch.linalg.matrix_exp().

mm

Performs a matrix multiplication of the matricesinput andmat2.

mv

Performs a matrix-vector product of the matrixinput and the vectorvec.

orgqr

Alias fortorch.linalg.householder_product().

ormqr

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

outer

Outer product ofinput andvec2.

pinverse

Alias fortorch.linalg.pinv()

qr

Computes the QR decomposition of a matrix or a batch of matricesinput, and returns a namedtuple (Q, R) of tensors such thatinput=QR\text{input} = Q R withQQ being an orthogonal matrix or batch of orthogonal matrices andRR being an upper triangular matrix or batch of upper triangular matrices.

svd

Computes the singular value decomposition of either a matrix or batch of matricesinput.

svd_lowrank

Return the singular value decomposition(U,S,V) of a matrix, batches of matrices, or a sparse matrixAA such thatAUdiag(S)VHA \approx U \operatorname{diag}(S) V^{\text{H}}.

pca_lowrank

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

lobpcg

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

trapz

Alias fortorch.trapezoid().

trapezoid

Computes thetrapezoidal rule alongdim.

cumulative_trapezoid

Cumulatively computes thetrapezoidal rule alongdim.

triangular_solve

Solves a system of equations with a square upper or lower triangular invertible matrixAA and multiple right-hand sidesbb.

vdot

Computes the dot product of two 1D vectors along a dimension.

Foreach Operations#

Warning

This API is in beta and subject to future changes.Forward-mode AD is not supported.

_foreach_abs

Applytorch.abs() to each Tensor of the input list.

_foreach_abs_

Applytorch.abs() to each Tensor of the input list.

_foreach_acos

Applytorch.acos() to each Tensor of the input list.

_foreach_acos_

Applytorch.acos() to each Tensor of the input list.

_foreach_asin

Applytorch.asin() to each Tensor of the input list.

_foreach_asin_

Applytorch.asin() to each Tensor of the input list.

_foreach_atan

Applytorch.atan() to each Tensor of the input list.

_foreach_atan_

Applytorch.atan() to each Tensor of the input list.

_foreach_ceil

Applytorch.ceil() to each Tensor of the input list.

_foreach_ceil_

Applytorch.ceil() to each Tensor of the input list.

_foreach_cos

Applytorch.cos() to each Tensor of the input list.

_foreach_cos_

Applytorch.cos() to each Tensor of the input list.

_foreach_cosh

Applytorch.cosh() to each Tensor of the input list.

_foreach_cosh_

Applytorch.cosh() to each Tensor of the input list.

_foreach_erf

Applytorch.erf() to each Tensor of the input list.

_foreach_erf_

Applytorch.erf() to each Tensor of the input list.

_foreach_erfc

Applytorch.erfc() to each Tensor of the input list.

_foreach_erfc_

Applytorch.erfc() to each Tensor of the input list.

_foreach_exp

Applytorch.exp() to each Tensor of the input list.

_foreach_exp_

Applytorch.exp() to each Tensor of the input list.

_foreach_expm1

Applytorch.expm1() to each Tensor of the input list.

_foreach_expm1_

Applytorch.expm1() to each Tensor of the input list.

_foreach_floor

Applytorch.floor() to each Tensor of the input list.

_foreach_floor_

Applytorch.floor() to each Tensor of the input list.

_foreach_log

Applytorch.log() to each Tensor of the input list.

_foreach_log_

Applytorch.log() to each Tensor of the input list.

_foreach_log10

Applytorch.log10() to each Tensor of the input list.

_foreach_log10_

Applytorch.log10() to each Tensor of the input list.

_foreach_log1p

Applytorch.log1p() to each Tensor of the input list.

_foreach_log1p_

Applytorch.log1p() to each Tensor of the input list.

_foreach_log2

Applytorch.log2() to each Tensor of the input list.

_foreach_log2_

Applytorch.log2() to each Tensor of the input list.

_foreach_neg

Applytorch.neg() to each Tensor of the input list.

_foreach_neg_

Applytorch.neg() to each Tensor of the input list.

_foreach_tan

Applytorch.tan() to each Tensor of the input list.

_foreach_tan_

Applytorch.tan() to each Tensor of the input list.

_foreach_sin

Applytorch.sin() to each Tensor of the input list.

_foreach_sin_

Applytorch.sin() to each Tensor of the input list.

_foreach_sinh

Applytorch.sinh() to each Tensor of the input list.

_foreach_sinh_

Applytorch.sinh() to each Tensor of the input list.

_foreach_round

Applytorch.round() to each Tensor of the input list.

_foreach_round_

Applytorch.round() to each Tensor of the input list.

_foreach_sqrt

Applytorch.sqrt() to each Tensor of the input list.

_foreach_sqrt_

Applytorch.sqrt() to each Tensor of the input list.

_foreach_lgamma

Applytorch.lgamma() to each Tensor of the input list.

_foreach_lgamma_

Applytorch.lgamma() to each Tensor of the input list.

_foreach_frac

Applytorch.frac() to each Tensor of the input list.

_foreach_frac_

Applytorch.frac() to each Tensor of the input list.

_foreach_reciprocal

Applytorch.reciprocal() to each Tensor of the input list.

_foreach_reciprocal_

Applytorch.reciprocal() to each Tensor of the input list.

_foreach_sigmoid

Applytorch.sigmoid() to each Tensor of the input list.

_foreach_sigmoid_

Applytorch.sigmoid() to each Tensor of the input list.

_foreach_trunc

Applytorch.trunc() to each Tensor of the input list.

_foreach_trunc_

Applytorch.trunc() to each Tensor of the input list.

_foreach_zero_

Applytorch.zero() to each Tensor of the input list.

Utilities#

compiled_with_cxx11_abi

Returns whether PyTorch was built with _GLIBCXX_USE_CXX11_ABI=1

result_type

Returns thetorch.dtype that would result from performing an arithmetic operation on the provided input tensors.

can_cast

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

promote_types

Returns thetorch.dtype with the smallest size and scalar kind that is not smaller nor of lower kind than eithertype1 ortype2.

use_deterministic_algorithms

Sets whether PyTorch operations must use "deterministic" algorithms.

are_deterministic_algorithms_enabled

Returns True if the global deterministic flag is turned on.

is_deterministic_algorithms_warn_only_enabled

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

set_deterministic_debug_mode

Sets the debug mode for deterministic operations.

get_deterministic_debug_mode

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

set_float32_matmul_precision

Sets the internal precision of float32 matrix multiplications.

get_float32_matmul_precision

Returns the current value of float32 matrix multiplication precision.

set_warn_always

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

get_device_module

Returns the module associated with a given device(e.g., torch.device('cuda'), "mtia:0", "xpu", ...).

is_warn_always_enabled

Returns True if the global warn_always flag is turned on.

vmap

vmap is the vectorizing map;vmap(func) returns a new function that mapsfunc over some dimension of the inputs.

_assert

A wrapper around Python's assert which is symbolically traceable.

Symbolic Numbers#

classtorch.SymInt(node)[source]#

Like an int (including magic methods), but redirects all operations on thewrapped node. This is used in particular to symbolically record operationsin the symbolic shape workflow.

as_integer_ratio()[source]#

Represent this int as an exact integer ratio

Return type

tuple[‘SymInt’,int]

classtorch.SymFloat(node)[source]#

Like a float (including magic methods), but redirects all operations on thewrapped node. This is used in particular to symbolically record operationsin the symbolic shape workflow.

as_integer_ratio()[source]#

Represent this float as an exact integer ratio

Return type

tuple[int,int]

conjugate()[source]#

Returns the complex conjugate of the float.

Return type

SymFloat

hex()[source]#

Returns the hexadecimal representation of the float.

Return type

str

is_integer()[source]#

Return True if the float is an integer.

classtorch.SymBool(node)[source]#

Like a bool (including magic methods), but redirects all operations on thewrapped node. This is used in particular to symbolically record operationsin the symbolic shape workflow.

Unlike regular bools, regular boolean operators will force extra guards insteadof symbolically evaluate. Use the bitwise operators instead to handle this.

sym_float

SymInt-aware utility for float casting.

sym_fresh_size

sym_int

SymInt-aware utility for int casting.

sym_max

SymInt-aware utility for max which avoids branching on a < b.

sym_min

SymInt-aware utility for min().

sym_not

SymInt-aware utility for logical negation.

sym_ite

SymInt-aware utility for ternary operator (tifbelsef.)

sym_sum

N-ary add which is faster to compute for long lists than iterated binary addition.

Export Path#

Warning

This feature is a prototype and may have compatibility breaking changes in the future.

exportgenerated/exportdb/index

Control Flow#

Warning

This feature is a prototype and may have compatibility breaking changes in the future.

cond

Conditionally appliestrue_fn orfalse_fn.

Optimizations#

compile

Optimizes given model/function using TorchDynamo and specified backend.

torch.compile documentation

Operator Tags#

classtorch.Tag#

Members:

core

cudagraph_unsafe

data_dependent_output

dynamic_output_shape

flexible_layout

generated

inplace_view

maybe_aliasing_or_mutating

needs_contiguous_strides

needs_exact_strides

needs_fixed_stride_order

nondeterministic_bitwise

nondeterministic_seeded

pointwise

pt2_compliant_tag

view_copy

propertyname#