Constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute value abs and angle angle.
out (Tensor) – If the inputs are torch.float32, must be torch.complex64. If the inputs are torch.float64, must be torch.complex128.


Computes the nthn^{th}  derivative of the digamma function on input. n≥0n \geq 0  is called the order of the polygamma function.
Note
This function is implemented only for nonnegative integers n≥0n \geq 0 .
out (Tensor, optional) – the output tensor.


Takes the power of each element in input with exponent and returns a tensor with the result.
exponent can be either a single float number or a Tensor with the same number of elements as input.
When exponent is a scalar value, the operation applied is:
When exponent is a tensor, the operation applied is:
When exponent is a tensor, the shapes of input and exponent must be broadcastable.
out (Tensor, optional) – the output tensor.
Example:
self is a scalar float value, and exponent is a tensor. The returned tensor out is of the same shape as exponent
The operation applied is:
out (Tensor, optional) – the output tensor.
Example:


Returns the product of all elements in the input tensor.
input (Tensor) – the input tensor.
dtype (torch.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.
Example:
Returns the product of each row of the input tensor in the given dimension dim.
If keepdim is True, the output tensor is of the same size as input except in the dimension dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 fewer dimension than input.
dtype (torch.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.
Example:


Returns the torch.dtype with the smallest size and scalar kind that is not smaller nor of lower kind than either type1 or type2. See type promotion documentation for more information on the type promotion logic.
Example:


Computes the QR decomposition of a matrix or a batch of matrices input, and returns a namedtuple (Q, R) of tensors such that input=QR\text{input} = Q R  with QQ  being an orthogonal matrix or batch of orthogonal matrices and RR  being an upper triangular matrix or batch of upper triangular matrices.
If some is True, then this function returns the thin (reduced) QR factorization. Otherwise, if some is False, this function returns the complete QR factorization.
Warning
torch.qr is deprecated. Please use torch.linalg.qr() instead.
Differences with torch.linalg.qr:
torch.linalg.qr takes a string parameter mode instead of some:
Warning
If you plan to backpropagate through QR, note that the current backward implementation is only well-defined when the first min(input.size(−1),input.size(−2))\min(input.size(-1), input.size(-2))  columns of input are linearly independent. This behavior will propably change once QR supports pivoting.
Note
This function uses LAPACK for CPU inputs and MAGMA for CUDA inputs, and may produce different (valid) decompositions on different device types or different platforms.
some (bool, optional) –
Set to True for reduced QR decomposition and False for complete QR decomposition. If k = min(m, n) then:
out (tuple, optional) – tuple of Q and R tensors. The dimensions of Q and R are detailed in the description of some above.
Example:


Returns the q-th quantiles of all elements in the input tensor, doing a linear interpolation when the q-th quantile lies between two data points.
Example:
Returns the q-th quantiles of each row of the input tensor along the dimension dim, doing a linear interpolation when the q-th quantile lies between two data points. By default, dim is None resulting in the input tensor being flattened before computation.
If keepdim is True, the output dimensions are of the same size as input except in the dimensions being reduced (dim or all if dim is None) where they have size 1. Otherwise, the dimensions being reduced are squeezed (see torch.squeeze()). If q is a 1D tensor, an extra dimension is prepended to the output tensor with the same size as q which represents the quantiles.
out (Tensor, optional) – the output tensor.
Example:


This module implements the functions you call directly to convert your model from FP32 to quantized form. For example the prepare() is used in post training quantization to prepares your model for the calibration step and convert() actually converts the weights to int8 and replaces the operations with their quantized counterparts. There are other helper functions for things like quantizing the input to your model and performing critical fusions like conv+relu.
Quantize the input float model with post training static quantization.
First it will prepare the model for calibration, then it calls run_fn which will run the calibration step, after that we will convert the model to a quantized model.
Quantized model.
Converts a float model to dynamic (i.e. weights-only) quantized model.
Replaces specified modules with dynamic weight-only quantized versions and output the quantized model.
For simplest usage provide dtype argument that can be float16 or qint8. Weight-only quantization by default is performed for layers with large weights size - i.e. Linear and RNN variants.
Fine grained control is possible with qconfig and mapping that act similarly to quantize(). If qconfig is provided, the dtype argument is ignored.
qconfig_spec –
Either:
Do quantization aware training and output a quantized model
Quantized model.
Prepares a copy of the model for quantization calibration or quantization-aware training.
Quantization configuration should be assigned preemptively to individual submodules in .qconfig attribute.
The model will be attached with observer or fake quant modules, and qconfig will be propagated.
Prepares a copy of the model for quantization calibration or quantization-aware training and converts it to quantized version.
Quantization configuration should be assigned preemptively to individual submodules in .qconfig attribute.
Converts submodules in input module to a different module according to mapping by calling from_float method on the target module class. And remove qconfig at the end if remove_qconfig is set to True.
Describes how to quantize a layer or a part of the network by providing settings (observer classes) for activations and weights respectively.
Note that QConfig needs to contain observer classes (like MinMaxObserver) or a callable that returns instances on invocation, not the concrete observer instances themselves. Quantization preparation function will instantiate observers multiple times for each of the layers.
Observer classes have usually reasonable default arguments, but they can be overwritten with with_args method (that behaves like functools.partial):
my_qconfig = QConfig(activation=MinMaxObserver.with_args(dtype=torch.qint8), weight=default_observer.with_args(dtype=torch.qint8))
Describes how to dynamically quantize a layer or a part of the network by providing settings (observer classes) for weights.
It’s like QConfig, but for dynamic quantization.
Note that QConfigDynamic needs to contain observer classes (like MinMaxObserver) or a callable that returns instances on invocation, not the concrete observer instances themselves. Quantization function will instantiate observers multiple times for each of the layers.
Observer classes have usually reasonable default arguments, but they can be overwritten with with_args method (that behaves like functools.partial):
my_qconfig = QConfigDynamic(weight=default_observer.with_args(dtype=torch.qint8))
Fuses a list of modules into a single module
Fuses only the following sequence of modules: conv, bn conv, bn, relu conv, relu linear, relu bn, relu All other sequences are left unchanged. For these sequences, replaces the first item in the list with the fused module, replacing the rest of the modules with identity.
model with fused modules. A new copy is created if inplace=True.
Examples:
Quantize stub module, before calibration, this is same as an observer, it will be swapped as nnq.Quantize in convert.
qconfig – quantization configuration for the tensor, if qconfig is not provided, we will get qconfig from parent modules
Dequantize stub module, before calibration, this is same as identity, this will be swapped as nnq.DeQuantize in convert.
A wrapper class that wraps the input module, adds QuantStub and DeQuantStub and surround the call to module with call to quant and dequant modules.
This is used by the quantization utility functions to add the quant and dequant modules, before convert function QuantStub will just be observer, it observes the input tensor, after convert, QuantStub will be swapped to nnq.Quantize which does actual quantization. Similarly for DeQuantStub.
Wrap the leaf child module in QuantWrapper if it has a valid qconfig Note that this function will modify the children of module inplace and it can return a new module which wraps the input module as well.
Either the inplace modified module with submodules wrapped in QuantWrapper based on qconfig or a new QuantWrapper module which wraps the input module, the latter case only happens when the input module is a leaf module and we want to quantize it.
Add observer for the leaf child of the module.
This function insert observer module to all leaf child module that has a valid qconfig attribute.
None, module is modified inplace with added observer modules and forward_hooks
Swaps the module if it has a quantized counterpart and it has an observer attached.
The corresponding quantized module of mod
Propagate qconfig through the module hierarchy and assign qconfig attribute on each leaf module
None, module is modified inplace with qconfig attached
Default evaluation function takes a torch.utils.data.Dataset or a list of input Tensors and run the model on the dataset
Base observer Module. Any observer implementation should derive from this class.
Concrete observers should follow the same API. In forward, they will update the statistics of the observed Tensor. And they should provide a calculate_qparams function that computes the quantization parameters given the collected statistics.
dtype – Quantized data type
Wrapper that allows creation of class factories.
This can be useful when there is a need to create classes with the same constructor arguments, but different instances.
Example:
Observer module for computing the quantization parameters based on the running min and max values.
This observer uses the tensor min/max statistics to compute the quantization parameters. The module records the running minimum and maximum of incoming tensors, and uses this statistic to compute the quantization parameters.
Given running min/max as xminx_\text{min}  and xmaxx_\text{max} , scale ss  and zero point zz  are computed as:
The running minimum/maximum xmin/maxx_\text{min/max}  is computed as:
where XX  is the observed tensor.
The scale ss  and zero point zz  are then computed as:
where QminQ_\text{min}  and QmaxQ_\text{max}  are the minimum and maximum of the quantized data type.
Warning
Only works with torch.per_tensor_symmetric quantization scheme
Warning
dtype can only take torch.qint8 or torch.quint8.
Note
If the running minimum equals to the running maximum, the scale and zero_point are set to 1.0 and 0.
Observer module for computing the quantization parameters based on the moving average of the min and max values.
This observer computes the quantization parameters based on the moving averages of minimums and maximums of the incoming tensors. The module records the average minimum and maximum of incoming tensors, and uses this statistic to compute the quantization parameters.
The moving average min/max is computed as follows
where xmin/maxx_\text{min/max}  is the running average min/max, XX  is is the incoming tensor, and cc  is the averaging_constant.
The scale and zero point are then computed as in MinMaxObserver.
Note
Only works with torch.per_tensor_affine quantization scheme.
Note
If the running minimum equals to the running maximum, the scale and zero_point are set to 1.0 and 0.
Observer module for computing the quantization parameters based on the running per channel min and max values.
This observer uses the tensor min/max statistics to compute the per channel quantization parameters. The module records the running minimum and maximum of incoming tensors, and uses this statistic to compute the quantization parameters.
The quantization parameters are computed the same way as in MinMaxObserver, with the difference that the running min/max values are stored per channel. Scales and zero points are thus computed per channel as well.
Note
If the running minimum equals to the running maximum, the scales and zero_points are set to 1.0 and 0.
Observer module for computing the quantization parameters based on the running per channel min and max values.
This observer uses the tensor min/max statistics to compute the per channel quantization parameters. The module records the running minimum and maximum of incoming tensors, and uses this statistic to compute the quantization parameters.
The quantization parameters are computed the same way as in MovingAverageMinMaxObserver, with the difference that the running min/max values are stored per channel. Scales and zero points are thus computed per channel as well.
Note
If the running minimum equals to the running maximum, the scales and zero_points are set to 1.0 and 0.
The module records the running histogram of tensor values along with min/max values. calculate_qparams will calculate scale and zero_point.
The scale and zero point are computed as follows:
The histogram is computed continuously, and the ranges per bin change with every new tensor observed.
The search for the min/max values ensures the minimization of the quantization error with respect to the floating point model.
MinMaxObserver
Simulate the quantize and dequantize operations in training time. The output of this module is given by
x_out = (clamp(round(x/scale + zero_point), quant_min, quant_max)-zero_point)*scale
allowable values are torch.qint8 and torch.quint8. The values of quant_min and quant_max should be chosen to be consistent with the dtype
~FakeQuantize.observer (Module) – User provided module that collects statistics on the input tensor and provides a method to calculate scale and zero-point.
Observer that doesn’t do anything and just passes its configuration to the quantized module’s .from_float().
Primarily used for quantization to float16 which doesn’t require determining ranges.
Traverse the modules and save all observers into dict. This is mainly used for quantization accuracy debug :param mod: the top module we want to save all observers :param prefix: the prefix for the current module :param target_dict: the dictionary used to save all the observers
The module is mainly for debug and records the tensor values during runtime.
nn.intrinsic


Add observer for the leaf child of the module.
This function insert observer module to all leaf child module that has a valid qconfig attribute.
None, module is modified inplace with added observer modules and forward_hooks


Wrap the leaf child module in QuantWrapper if it has a valid qconfig Note that this function will modify the children of module inplace and it can return a new module which wraps the input module as well.
Either the inplace modified module with submodules wrapped in QuantWrapper based on qconfig or a new QuantWrapper module which wraps the input module, the latter case only happens when the input module is a leaf module and we want to quantize it.


Converts submodules in input module to a different module according to mapping by calling from_float method on the target module class. And remove qconfig at the end if remove_qconfig is set to True.


Default evaluation function takes a torch.utils.data.Dataset or a list of input Tensors and run the model on the dataset


Dequantize stub module, before calibration, this is same as identity, this will be swapped as nnq.DeQuantize in convert.


Simulate the quantize and dequantize operations in training time. The output of this module is given by
x_out = (clamp(round(x/scale + zero_point), quant_min, quant_max)-zero_point)*scale
allowable values are torch.qint8 and torch.quint8. The values of quant_min and quant_max should be chosen to be consistent with the dtype
~FakeQuantize.observer (Module) – User provided module that collects statistics on the input tensor and provides a method to calculate scale and zero-point.


Fuses a list of modules into a single module
Fuses only the following sequence of modules: conv, bn conv, bn, relu conv, relu linear, relu bn, relu All other sequences are left unchanged. For these sequences, replaces the first item in the list with the fused module, replacing the rest of the modules with identity.
model with fused modules. A new copy is created if inplace=True.
Examples:


Traverse the modules and save all observers into dict. This is mainly used for quantization accuracy debug :param mod: the top module we want to save all observers :param prefix: the prefix for the current module :param target_dict: the dictionary used to save all the observers


The module records the running histogram of tensor values along with min/max values. calculate_qparams will calculate scale and zero_point.
The scale and zero point are computed as follows:
The histogram is computed continuously, and the ranges per bin change with every new tensor observed.
The search for the min/max values ensures the minimization of the quantization error with respect to the floating point model.
MinMaxObserver


Observer module for computing the quantization parameters based on the running min and max values.
This observer uses the tensor min/max statistics to compute the quantization parameters. The module records the running minimum and maximum of incoming tensors, and uses this statistic to compute the quantization parameters.
Given running min/max as xminx_\text{min}  and xmaxx_\text{max} , scale ss  and zero point zz  are computed as:
The running minimum/maximum xmin/maxx_\text{min/max}  is computed as:
where XX  is the observed tensor.
The scale ss  and zero point zz  are then computed as:
where QminQ_\text{min}  and QmaxQ_\text{max}  are the minimum and maximum of the quantized data type.
Warning
Only works with torch.per_tensor_symmetric quantization scheme
Warning
dtype can only take torch.qint8 or torch.quint8.
Note
If the running minimum equals to the running maximum, the scale and zero_point are set to 1.0 and 0.


Observer module for computing the quantization parameters based on the moving average of the min and max values.
This observer computes the quantization parameters based on the moving averages of minimums and maximums of the incoming tensors. The module records the average minimum and maximum of incoming tensors, and uses this statistic to compute the quantization parameters.
The moving average min/max is computed as follows
where xmin/maxx_\text{min/max}  is the running average min/max, XX  is is the incoming tensor, and cc  is the averaging_constant.
The scale and zero point are then computed as in MinMaxObserver.
Note
Only works with torch.per_tensor_affine quantization scheme.
Note
If the running minimum equals to the running maximum, the scale and zero_point are set to 1.0 and 0.


Observer module for computing the quantization parameters based on the running per channel min and max values.
This observer uses the tensor min/max statistics to compute the per channel quantization parameters. The module records the running minimum and maximum of incoming tensors, and uses this statistic to compute the quantization parameters.
The quantization parameters are computed the same way as in MovingAverageMinMaxObserver, with the difference that the running min/max values are stored per channel. Scales and zero points are thus computed per channel as well.
Note
If the running minimum equals to the running maximum, the scales and zero_points are set to 1.0 and 0.


Observer that doesn’t do anything and just passes its configuration to the quantized module’s .from_float().
Primarily used for quantization to float16 which doesn’t require determining ranges.


Base observer Module. Any observer implementation should derive from this class.
Concrete observers should follow the same API. In forward, they will update the statistics of the observed Tensor. And they should provide a calculate_qparams function that computes the quantization parameters given the collected statistics.
dtype – Quantized data type
Wrapper that allows creation of class factories.
This can be useful when there is a need to create classes with the same constructor arguments, but different instances.
Example:


Wrapper that allows creation of class factories.
This can be useful when there is a need to create classes with the same constructor arguments, but different instances.
Example:


Observer module for computing the quantization parameters based on the running per channel min and max values.
This observer uses the tensor min/max statistics to compute the per channel quantization parameters. The module records the running minimum and maximum of incoming tensors, and uses this statistic to compute the quantization parameters.
The quantization parameters are computed the same way as in MinMaxObserver, with the difference that the running min/max values are stored per channel. Scales and zero points are thus computed per channel as well.
Note
If the running minimum equals to the running maximum, the scales and zero_points are set to 1.0 and 0.


Prepares a copy of the model for quantization calibration or quantization-aware training.
Quantization configuration should be assigned preemptively to individual submodules in .qconfig attribute.
The model will be attached with observer or fake quant modules, and qconfig will be propagated.


Prepares a copy of the model for quantization calibration or quantization-aware training and converts it to quantized version.
Quantization configuration should be assigned preemptively to individual submodules in .qconfig attribute.


Propagate qconfig through the module hierarchy and assign qconfig attribute on each leaf module
None, module is modified inplace with qconfig attached


Describes how to quantize a layer or a part of the network by providing settings (observer classes) for activations and weights respectively.
Note that QConfig needs to contain observer classes (like MinMaxObserver) or a callable that returns instances on invocation, not the concrete observer instances themselves. Quantization preparation function will instantiate observers multiple times for each of the layers.
Observer classes have usually reasonable default arguments, but they can be overwritten with with_args method (that behaves like functools.partial):
my_qconfig = QConfig(activation=MinMaxObserver.with_args(dtype=torch.qint8), weight=default_observer.with_args(dtype=torch.qint8))


Describes how to dynamically quantize a layer or a part of the network by providing settings (observer classes) for weights.
It’s like QConfig, but for dynamic quantization.
Note that QConfigDynamic needs to contain observer classes (like MinMaxObserver) or a callable that returns instances on invocation, not the concrete observer instances themselves. Quantization function will instantiate observers multiple times for each of the layers.
Observer classes have usually reasonable default arguments, but they can be overwritten with with_args method (that behaves like functools.partial):
my_qconfig = QConfigDynamic(weight=default_observer.with_args(dtype=torch.qint8))


Quantize the input float model with post training static quantization.
First it will prepare the model for calibration, then it calls run_fn which will run the calibration step, after that we will convert the model to a quantized model.
Quantized model.


Converts a float model to dynamic (i.e. weights-only) quantized model.
Replaces specified modules with dynamic weight-only quantized versions and output the quantized model.
For simplest usage provide dtype argument that can be float16 or qint8. Weight-only quantization by default is performed for layers with large weights size - i.e. Linear and RNN variants.
Fine grained control is possible with qconfig and mapping that act similarly to quantize(). If qconfig is provided, the dtype argument is ignored.
qconfig_spec –
Either:


Do quantization aware training and output a quantized model
Quantized model.


Quantize stub module, before calibration, this is same as an observer, it will be swapped as nnq.Quantize in convert.
qconfig – quantization configuration for the tensor, if qconfig is not provided, we will get qconfig from parent modules


A wrapper class that wraps the input module, adds QuantStub and DeQuantStub and surround the call to module with call to quant and dequant modules.
This is used by the quantization utility functions to add the quant and dequant modules, before convert function QuantStub will just be observer, it observes the input tensor, after convert, QuantStub will be swapped to nnq.Quantize which does actual quantization. Similarly for DeQuantStub.


The module is mainly for debug and records the tensor values during runtime.


Swaps the module if it has a quantized counterpart and it has an observer attached.
The corresponding quantized module of mod


Converts a float tensor to a per-channel quantized tensor with given scales and zero points.
A newly quantized tensor
Tensor
Example:


Converts a float tensor to a quantized tensor with given scale and zero point.
A newly quantized tensor
Tensor
Example:


The torch.quasirandom.SobolEngine is an engine for generating (scrambled) Sobol sequences. Sobol sequences are an example of low discrepancy quasi-random sequences.
This implementation of an engine for Sobol sequences is capable of sampling sequences up to a maximum dimension of 21201. It uses direction numbers from https://web.maths.unsw.edu.au/~fkuo/sobol/ obtained using the search criterion D(6) up to the dimension 21201. This is the recommended choice by the authors.
Examples:
Function to draw a sequence of n points from a Sobol sequence. Note that the samples are dependent on the previous samples. The size of the result is (n,dimension)(n, dimension) .
Function to draw a sequence of 2**m points from a Sobol sequence. Note that the samples are dependent on the previous samples. The size of the result is (2∗∗m,dimension)(2**m, dimension) .
Function to fast-forward the state of the SobolEngine by n steps. This is equivalent to drawing n samples without using the samples.
n (Int) – The number of steps to fast-forward by.
Function to reset the SobolEngine to base state.


Function to draw a sequence of n points from a Sobol sequence. Note that the samples are dependent on the previous samples. The size of the result is (n,dimension)(n, dimension) .


Function to draw a sequence of 2**m points from a Sobol sequence. Note that the samples are dependent on the previous samples. The size of the result is (2∗∗m,dimension)(2**m, dimension) .


Function to fast-forward the state of the SobolEngine by n steps. This is equivalent to drawing n samples without using the samples.
n (Int) – The number of steps to fast-forward by.


Function to reset the SobolEngine to base state.


Returns a new tensor with each of the elements of input converted from angles in radians to degrees.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


Returns a tensor filled with random numbers from a uniform distribution on the interval [0,1)[0, 1)
The shape of the tensor is defined by the variable argument size.
size (int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
Example:


Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).
The shape of the tensor is defined by the variable argument size.
Note
With the global dtype default (torch.float32), this function returns a tensor with dtype torch.int64.
Example:


Returns a tensor with the same shape as Tensor input filled with random integers generated uniformly between low (inclusive) and high (exclusive).


Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).
The shape of the tensor is defined by the variable argument size.
size (int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
Example:


Returns a tensor with the same size as input that is filled with random numbers from a normal distribution with mean 0 and variance 1. torch.randn_like(input) is equivalent to torch.randn(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).
input (Tensor) – the size of input will determine size of the output tensor.


Forks the RNG, so that when you return, the RNG is reset to the state that it was previously in.
Returns the random number generator state as a torch.ByteTensor.
Returns the initial seed for generating random numbers as a Python long.
Sets the seed for generating random numbers. Returns a torch.Generator object.
seed (int) – The desired seed. Value must be within the inclusive range [-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError is raised. Negative inputs are remapped to positive values with the formula 0xffff_ffff_ffff_ffff + seed.
Sets the seed for generating random numbers to a non-deterministic random number. Returns a 64 bit number used to seed the RNG.
Sets the random number generator state.
new_state (torch.ByteTensor) – The desired state


Forks the RNG, so that when you return, the RNG is reset to the state that it was previously in.


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


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


Sets the seed for generating random numbers. Returns a torch.Generator object.
seed (int) – The desired seed. Value must be within the inclusive range [-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError is raised. Negative inputs are remapped to positive values with the formula 0xffff_ffff_ffff_ffff + seed.


Sets the seed for generating random numbers to a non-deterministic random number. Returns a 64 bit number used to seed the RNG.


Sets the random number generator state.
new_state (torch.ByteTensor) – The desired state


Returns a random permutation of integers from 0 to n - 1.
n (int) – the upper bound (exclusive)
Example:


Returns a tensor with the same size as input that is filled with random numbers from a uniform distribution on the interval [0,1)[0, 1) . torch.rand_like(input) is equivalent to torch.rand(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).
input (Tensor) – the size of input will determine size of the output tensor.


Returns a 1-D tensor of size ⌊end−startstep⌋+1\left\lfloor \frac{\text{end} - \text{start}}{\text{step}} \right\rfloor + 1  with values from start to end with step step. Step is the gap between two values in the tensor.
Warning
This function is deprecated and will be removed in a future release because its behavior is inconsistent with Python’s range builtin. Instead, use torch.arange(), which produces values in [start, end).
Example:


Return a contiguous flattened tensor. A copy is made only if needed.
input (Tensor) – the input tensor.
Example:


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.
input (Tensor) – the input tensor.


Returns a new tensor with the reciprocal of the elements of input
Note
Unlike NumPy’s reciprocal, torch.reciprocal supports integral inputs. Integral inputs to reciprocal are automatically promoted to the default scalar type.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


Computes the element-wise remainder of division.
The dividend and divisor may contain both for integer and floating point numbers. The remainder has the same sign as the divisor other.
Supports broadcasting to a common shape, type promotion, and integer and float inputs.
Note
Complex inputs are not supported. In some cases, it is not mathematically possible to satisfy the definition of a modulo operation with complex numbers. See torch.fmod() for how division by zero is handled.
out (Tensor, optional) – the output tensor.
Example:
See also
torch.fmod(), which computes the element-wise remainder of division equivalently to the C library function fmod().


Returns a tensor where each sub-tensor of input along dimension dim is normalized such that the p-norm of the sub-tensor is lower than the value maxnorm
Note
If the norm of a row is lower than maxnorm, the row is unchanged
out (Tensor, optional) – the output tensor.
Example:


Repeat elements of a tensor.
Warning
This is different from torch.Tensor.repeat() but similar to numpy.repeat.
Repeated tensor which has the same shape as input, except along the given axis.
Tensor
Example:
If the repeats is tensor([n1, n2, n3, …]), then the output will be tensor([0, 0, …, 1, 1, …, 2, 2, …, …]) where 0 appears n1 times, 1 appears n2 times, 2 appears n3 times, etc.


Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.
See torch.Tensor.view() on when it is possible to return a view.
A single dimension may be -1, in which case it’s inferred from the remaining dimensions and the number of elements in input.
Example:


Returns the torch.dtype that would result from performing an arithmetic operation on the provided input tensors. See type promotion documentation for more information on the type promotion logic.
Example:


Roll the tensor along the given dimension(s). Elements that are shifted beyond the last position are re-introduced at the first position. If a dimension is not specified, the tensor will be flattened before rolling and then restored to the original shape.
Example:


Rotate a n-D tensor by 90 degrees in the plane specified by dims axis. Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0.
Example:


Returns a new tensor with each of the elements of input rounded to the closest integer.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


Returns a new tensor with the reciprocal of the square-root of each of the elements of input.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


Saves an object to a disk file.
See also: saving-loading-tensors
Note
A common PyTorch convention is to save tensors using .pt file extension.
Note
PyTorch preserves storage sharing across serialization. See preserve-storage-sharing for more details.
Note
The 1.6 release of PyTorch switched torch.save to use a new zipfile-based file format. torch.load still retains the ability to load files in the old format. If for any reason you want torch.save to use the old format, pass the kwarg _use_new_zipfile_serialization=False.


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


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


Find the indices from the innermost dimension of sorted_sequence such that, if the corresponding values in values were inserted before the indices, the order of the corresponding innermost dimension within sorted_sequence would be preserved. Return a new tensor with the same size as values. If right is False (default), then the left boundary of sorted_sequence is closed. More formally, the returned index satisfies the following rules:
sorted_sequence
right
returned index satisfies
1-D
False
sorted_sequence[i-1] < values[m][n]...[l][x] <= sorted_sequence[i]
1-D
True
sorted_sequence[i-1] <= values[m][n]...[l][x] < sorted_sequence[i]
N-D
False
sorted_sequence[m][n]...[l][i-1] < values[m][n]...[l][x] <= sorted_sequence[m][n]...[l][i]
N-D
True
sorted_sequence[m][n]...[l][i-1] <= values[m][n]...[l][x] < sorted_sequence[m][n]...[l][i]
Note
If your use case is always 1-D sorted sequence, torch.bucketize() is preferred, because it has fewer dimension checks resulting in slightly better performance.
Example:


Sets the seed for generating random numbers to a non-deterministic random number. Returns a 64 bit number used to seed the RNG.


Sets the default floating point dtype to d. This dtype is:
The default floating point dtype is initially torch.float32.
d (torch.dtype) – the floating point dtype to make the default


Sets the default torch.Tensor type to floating point tensor type t. This type will also be used as default floating point type for type inference in torch.tensor().
The default floating point tensor type is initially torch.FloatTensor.
t (type or string) – the floating point tensor type or its name
Example:


Disables denormal floating numbers on CPU.
Returns True if your system supports flushing denormal numbers and it successfully configures flush denormal mode. set_flush_denormal() is only supported on x86 architectures supporting SSE3.
mode (bool) – Controls whether to enable flush denormal mode or not
Example:


Context-manager that sets gradient calculation to on or off.
set_grad_enabled will enable or disable grads based on its argument mode. It can be used as a context-manager or as a function.
This context manager is thread local; it will not affect computation in other threads.
mode (bool) – Flag whether to enable grad (True), or disable (False). This can be used to conditionally enable gradients.
Example:


Sets the number of threads used for interop parallelism (e.g. in JIT interpreter) on CPU.
Warning
Can only be called once and before any inter-op parallel work is started (e.g. JIT execution).


Sets the number of threads used for intraop parallelism on CPU.
Warning
To ensure that the correct number of threads is used, set_num_threads must be called before running eager, JIT or autograd code.


Set options for printing. Items shamelessly taken from NumPy


Sets the random number generator state.
new_state (torch.ByteTensor) – The desired state


For complex tensors, this function returns a new tensor whose elemants have the same angle as that of the elements of input and absolute value 1. For a non-complex tensor, this function returns the signs of the elements of input (see torch.sign()).
outi=0\text{out}_{i} = 0 , if ∣inputi∣==0|{\text{{input}}_i}| == 0  outi=inputi∣inputi∣\text{out}_{i} = \frac{{\text{{input}}_i}}{|{\text{{input}}_i}|} , otherwise
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


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


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


Tests if each element of input has its sign bit set (is less than zero) or not.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


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


Computes the normalized sinc of input.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


Returns a new tensor with the hyperbolic sine of the elements of input.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:
Note
When input is on the CPU, the implementation of torch.sinh may use the Sleef library, which rounds very large results to infinity or negative infinity. See here for details.


Calculates the sign and log absolute value of the determinant(s) of a square matrix or batches of square matrices.
Note
torch.slogdet() is deprecated. Please use torch.linalg.slogdet() instead.
Note
If input has zero determinant, this returns (0, -inf).
Note
Backward through slogdet() internally uses SVD results when input is not invertible. In this case, double backward through slogdet() will be unstable in when input doesn’t have distinct singular values. See svd() for details.
input (Tensor) – the input tensor of size (*, n, n) where * is zero or more batch dimensions.
A namedtuple (sign, logabsdet) containing the sign of the determinant, and the log value of the absolute determinant.
Example:


Performs a matrix multiplication of the sparse matrix input with the dense matrix mat.


This function returns the solution to the system of linear equations represented by AX=BAX = B  and the LU factorization of A, in order as a namedtuple solution, LU.
LU contains L and U factors for LU factorization of A.
torch.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 solution, LU.
Supports real-valued and complex-valued inputs.
Note
Irrespective of the original strides, the returned matrices solution and LU will be transposed, i.e. with strides like B.contiguous().transpose(-1, -2).stride() and A.contiguous().transpose(-1, -2).stride() respectively.
out ((Tensor, Tensor), optional) – optional output tuple.
Example:


Sorts the elements of the input tensor along a given dimension in ascending order by value.
If dim is not given, the last dimension of the input is chosen.
If descending is True then the elements are sorted in descending order by value.
A namedtuple of (values, indices) is returned, where the values are the sorted values and indices are the indices of the elements in the original input tensor.
out (tuple, optional) – the output tuple of (Tensor, LongTensor) that can be optionally given to be used as output buffers
Example:


PyTorch provides torch.Tensor to represent a multi-dimensional array containing elements of a single data type. By default, array elements are stored contiguously in memory leading to efficient implementations of various array processing algorithms that relay on the fast access to array elements. However, there exists an important class of multi-dimensional arrays, so-called sparse arrays, where the contiguous memory storage of array elements turns out to be suboptimal. Sparse arrays have a property of having a vast portion of elements being equal to zero which means that a lot of memory as well as processor resources can be spared if only the non-zero elements are stored or/and processed. Various sparse storage formats (such as COO, CSR/CSC, LIL, etc.) have been developed that are optimized for a particular structure of non-zero elements in sparse arrays as well as for specific operations on the arrays.
Note
When talking about storing only non-zero elements of a sparse array, the usage of adjective “non-zero” is not strict: one is allowed to store also zeros in the sparse array data structure. Hence, in the following, we use “specified elements” for those array elements that are actually stored. In addition, the unspecified elements are typically assumed to have zero value, but not only, hence we use the term “fill value” to denote such elements.
Note
Using a sparse storage format for storing sparse arrays can be advantageous only when the size and sparsity levels of arrays are high. Otherwise, for small-sized or low-sparsity arrays using the contiguous memory storage format is likely the most efficient approach.
Warning
The PyTorch API of sparse tensors is in beta and may change in the near future.
Currently, PyTorch implements the so-called Coordinate format, or COO format, as the default sparse storage format for storing sparse tensors. In COO format, the specified elements are stored as tuples of element indices and the corresponding values. In particular,
where ndim is the dimensionality of the tensor and nse is the number of specified elements.
Note
The memory consumption of a sparse COO tensor is at least (ndim *
8 + <size of element type in bytes>) * nse bytes (plus a constant overhead from storing other tensor data).
The memory consumption of a strided tensor is at least product(<tensor shape>) * <size of element type in bytes>.
For example, the memory consumption of a 10 000 x 10 000 tensor with 100 000 non-zero 32-bit floating point numbers is at least (2 * 8 + 4) * 100 000 = 2 000 000 bytes when using COO tensor layout and 10 000 * 10 000 * 4 = 400 000 000 bytes when using the default strided tensor layout. Notice the 200 fold memory saving from using the COO storage format.
A sparse COO tensor can be constructed by providing the two tensors of indices and values, as well as the size of the sparse tensor (when it cannot be inferred from the indices and values tensors) to a function torch.sparse_coo_tensor().
Suppose we want to define a sparse tensor with the entry 3 at location (0, 2), entry 4 at location (1, 0), and entry 5 at location (1, 2). Unspecified elements are assumed to have the same value, fill value, which is zero by default. We would then write:
Note that the input i is NOT a list of index tuples. If you want to write your indices this way, you should transpose before passing them to the sparse constructor:
An empty sparse COO tensor can be constructed by specifying its size only:
Pytorch implements an extension of sparse tensors with scalar values to sparse tensors with (contiguous) tensor values. Such tensors are called hybrid tensors.
PyTorch hybrid COO tensor extends the sparse COO tensor by allowing the values tensor to be a multi-dimensional tensor so that we have:
Note
We use (M + K)-dimensional tensor to denote a N-dimensional hybrid sparse tensor, where M and K are the numbers of sparse and dense dimensions, respectively, such that M + K == N holds.
Suppose we want to create a (2 + 1)-dimensional tensor with the entry [3, 4] at location (0, 2), entry [5, 6] at location (1, 0), and entry [7, 8] at location (1, 2). We would write
In general, if s is a sparse COO tensor and M =
s.sparse_dim(), K = s.dense_dim(), then we have the following invariants:
Note
Dense dimensions always follow sparse dimensions, that is, mixing of dense and sparse dimensions is not supported.
PyTorch sparse COO tensor format permits uncoalesced sparse tensors, where there may be duplicate coordinates in the indices; in this case, the interpretation is that the value at that index is the sum of all duplicate value entries. For example, one can specify multiple values, 3 and 4, for the same index 1, that leads to an 1-D uncoalesced tensor:
while the coalescing process will accumulate the multi-valued elements into a single value using summation:
In general, the output of torch.Tensor.coalesce() method is a sparse tensor with the following properties:
Note
For the most part, you shouldn’t have to care whether or not a sparse tensor is coalesced or not, as most operations will work identically given a coalesced or uncoalesced sparse tensor.
However, some operations can be implemented more efficiently on uncoalesced tensors, and some on coalesced tensors.
For instance, addition of sparse COO tensors is implemented by simply concatenating the indices and values tensors:
If you repeatedly perform an operation that can produce duplicate entries (e.g., torch.Tensor.add()), you should occasionally coalesce your sparse tensors to prevent them from growing too large.
On the other hand, the lexicographical ordering of indices can be advantageous for implementing algorithms that involve many element selection operations, such as slicing or matrix products.
Let’s consider the following example:
As mentioned above, a sparse COO tensor is a torch.Tensor instance and to distinguish it from the Tensor instances that use some other layout, on can use torch.Tensor.is_sparse or torch.Tensor.layout properties:
The number of sparse and dense dimensions can be acquired using methods torch.Tensor.sparse_dim() and torch.Tensor.dense_dim(), respectively. For instance:
If s is a sparse COO tensor then its COO format data can be acquired using methods torch.Tensor.indices() and torch.Tensor.values().
Note
Currently, one can acquire the COO format data only when the tensor instance is coalesced:
For acquiring the COO format data of an uncoalesced tensor, use torch.Tensor._values() and torch.Tensor._indices():
Constructing a new sparse COO tensor results a tensor that is not coalesced:
but one can construct a coalesced copy of a sparse COO tensor using the torch.Tensor.coalesce() method:
When working with uncoalesced sparse COO tensors, one must take into an account the additive nature of uncoalesced data: the values of the same indices are the terms of a sum that evaluation gives the value of the corresponding tensor element. For example, the scalar multiplication on an uncoalesced sparse tensor could be implemented by multiplying all the uncoalesced values with the scalar because c *
(a + b) == c * a + c * b holds. However, any nonlinear operation, say, a square root, cannot be implemented by applying the operation to uncoalesced data because sqrt(a + b) == sqrt(a) + sqrt(b) does not hold in general.
Slicing (with positive step) of a sparse COO tensor is supported only for dense dimensions. Indexing is supported for both sparse and dense dimensions:
In PyTorch, the fill value of a sparse tensor cannot be specified explicitly and is assumed to be zero in general. However, there exists operations that may interpret the fill value differently. For instance, torch.sparse.softmax() computes the softmax with the assumption that the fill value is negative infinity.
The following table summarizes supported Linear Algebra operations on sparse matrices where the operands layouts may vary. Here T[layout] denotes a tensor with a given layout. Similarly, M[layout] denotes a matrix (2-D PyTorch tensor), and V[layout] denotes a vector (1-D PyTorch tensor). In addition, f denotes a scalar (float or 0-D PyTorch tensor), * is element-wise multiplication, and @ is matrix multiplication.
PyTorch operation
Sparse grad?
Layout signature
torch.mv()
no
M[sparse_coo] @ V[strided] -> V[strided]
torch.matmul()
no
M[sparse_coo] @ M[strided] -> M[strided]
torch.mm()
no
M[sparse_coo] @ M[strided] -> M[strided]
torch.sparse.mm()
yes
M[sparse_coo] @ M[strided] -> M[strided]
torch.smm()
no
M[sparse_coo] @ M[strided] -> M[sparse_coo]
torch.hspmm()
no
M[sparse_coo] @ M[strided] -> M[hybrid sparse_coo]
torch.bmm()
no
T[sparse_coo] @ T[strided] -> T[strided]
torch.addmm()
no
f * M[strided] + f * (M[sparse_coo] @ M[strided]) -> M[strided]
torch.sparse.addmm()
yes
f * M[strided] + f * (M[sparse_coo] @ M[strided]) -> M[strided]
torch.sspaddmm()
no
f * M[sparse_coo] + f * (M[sparse_coo] @ M[strided]) -> M[sparse_coo]
torch.lobpcg()
no
GENEIG(M[sparse_coo]) -> M[strided], M[strided]
torch.pca_lowrank()
yes
PCA(M[sparse_coo]) -> M[strided], M[strided], M[strided]
torch.svd_lowrank()
yes
SVD(M[sparse_coo]) -> M[strided], M[strided], M[strided]
where “Sparse grad?” column indicates if the PyTorch operation supports backward with respect to sparse matrix argument. All PyTorch operations, except torch.smm(), support backward with respect to strided matrix arguments.
Note
Currently, PyTorch does not support matrix multiplication with the layout signature M[strided] @ M[sparse_coo]. However, applications can still compute this using the matrix relation D @
S == (S.t() @ D.t()).t().
The following methods are specific to sparse tensors:
Is True if the Tensor uses sparse storage layout, False otherwise.
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.
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.
Creates a strided copy of self.
Warning
Throws an error if self is a strided tensor.
Example:
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 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 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.
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.
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.
The following torch.Tensor methods support sparse COO tensors:
add() add_() addmm() addmm_() any() asin() asin_() arcsin() arcsin_() bmm() clone() deg2rad() deg2rad_() detach() detach_() dim() div() div_() floor_divide() floor_divide_() get_device() index_select() isnan() log1p() log1p_() mm() mul() mul_() mv() narrow_copy() neg() neg_() negative() negative_() numel() rad2deg() rad2deg_() resize_as_() size() pow() sqrt() square() smm() sspaddmm() sub() sub_() t() t_() transpose() transpose_() zero_()
Constructs a sparse tensor in COO(rdinate) format with specified values at the given indices.
Note
This function returns an uncoalesced tensor.
Example:
Returns the sum of each row of the sparse tensor input in the given dimensions dim. If dim is a list of dimensions, reduce over all of them. When sum over all sparse_dim, this method returns a dense tensor instead of a sparse tensor.
All summed dim are squeezed (see torch.squeeze()), resulting an output tensor having dim fewer dimensions than input.
During backward, only gradients at nnz locations of input will propagate back. Note that the gradients of input is coalesced.
Example:
This function does exact same thing as torch.addmm() in the forward, except that it supports backward for sparse matrix mat1. mat1 need to have sparse_dim = 2. Note that the gradients of mat1 is a coalesced sparse tensor.
Performs a matrix multiplication of the sparse matrix mat1 and the (sparse or strided) matrix mat2. Similar to torch.mm(), If mat1 is a (n×m)(n \times m)  tensor, mat2 is a (m×p)(m \times p)  tensor, out will be a (n×p)(n \times p)  tensor. mat1 need to have sparse_dim = 2. This function also supports backward for both matrices. Note that the gradients of mat1 is a coalesced sparse tensor.
The format of the output tensor of this function follows: - sparse x sparse -> sparse - sparse x dense -> dense
Example:
Matrix multiplies a sparse tensor mat1 with a dense tensor mat2, then adds the sparse tensor input to the result.
Note: This function is equivalent to torch.addmm(), except input and mat1 are sparse.
Performs a matrix multiplication of a sparse COO matrix mat1 and a strided matrix mat2. The result is a (1 + 1)-dimensional hybrid COO matrix.
{out} –
Performs a matrix multiplication of the sparse matrix input with the dense matrix mat.
Applies a softmax function.
Softmax is defined as:
Softmax(xi)=exp(xi)∑jexp(xj)\text{Softmax}(x_{i}) = \frac{exp(x_i)}{\sum_j exp(x_j)}
where i,ji, j  run over sparse tensor indices and unspecified entries are ignores. This is equivalent to defining unspecified entries as negative infinity so that exp(xk)=0exp(x_k) = 0  when the entry with index kk  has not specified.
It is applied to all slices along dim, and will re-scale them so that the elements lie in the range [0, 1] and sum to 1.
Applies a softmax function followed by logarithm.
See softmax for more details.
The following torch functions support sparse COO tensors:
cat() dstack() empty() empty_like() hstack() index_select() is_complex() is_floating_point() is_nonzero() is_same_size() is_signed() is_tensor() lobpcg() mm() native_norm() pca_lowrank() select() stack() svd_lowrank() unsqueeze() vstack() zeros() zeros_like()


This function does exact same thing as torch.addmm() in the forward, except that it supports backward for sparse matrix mat1. mat1 need to have sparse_dim = 2. Note that the gradients of mat1 is a coalesced sparse tensor.


Applies a softmax function followed by logarithm.
See softmax for more details.


Performs a matrix multiplication of the sparse matrix mat1 and the (sparse or strided) matrix mat2. Similar to torch.mm(), If mat1 is a (n×m)(n \times m)  tensor, mat2 is a (m×p)(m \times p)  tensor, out will be a (n×p)(n \times p)  tensor. mat1 need to have sparse_dim = 2. This function also supports backward for both matrices. Note that the gradients of mat1 is a coalesced sparse tensor.
The format of the output tensor of this function follows: - sparse x sparse -> sparse - sparse x dense -> dense
Example:


Applies a softmax function.
Softmax is defined as:
Softmax(xi)=exp(xi)∑jexp(xj)\text{Softmax}(x_{i}) = \frac{exp(x_i)}{\sum_j exp(x_j)}
where i,ji, j  run over sparse tensor indices and unspecified entries are ignores. This is equivalent to defining unspecified entries as negative infinity so that exp(xk)=0exp(x_k) = 0  when the entry with index kk  has not specified.
It is applied to all slices along dim, and will re-scale them so that the elements lie in the range [0, 1] and sum to 1.


Returns the sum of each row of the sparse tensor input in the given dimensions dim. If dim is a list of dimensions, reduce over all of them. When sum over all sparse_dim, this method returns a dense tensor instead of a sparse tensor.
All summed dim are squeezed (see torch.squeeze()), resulting an output tensor having dim fewer dimensions than input.
During backward, only gradients at nnz locations of input will propagate back. Note that the gradients of input is coalesced.
Example:


Constructs a sparse tensor in COO(rdinate) format with specified values at the given indices.
Note
This function returns an uncoalesced tensor.
Example:


Splits the tensor into chunks. Each chunk is a view of the original tensor.
If split_size_or_sections is an integer type, then tensor will be split into equally sized chunks (if possible). Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by split_size.
If split_size_or_sections is a list, then tensor will be split into len(split_size_or_sections) chunks with sizes in dim according to split_size_or_sections.


Returns a new tensor with the square-root of the elements of input.
input (Tensor) – the input tensor.
out (Tensor, optional) – the output tensor.
Example:


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


Returns a tensor with all the dimensions of input of size 1 removed.
For example, if input is of shape: (A×1×B×C×1×D)(A \times 1 \times B \times C \times 1 \times D)  then the out tensor will be of shape: (A×B×C×D)(A \times B \times C \times D) .
When dim is given, a squeeze operation is done only in the given dimension. If input is of shape: (A×1×B)(A \times 1 \times B) , squeeze(input, 0) leaves the tensor unchanged, but squeeze(input, 1) will squeeze the tensor to the shape (A×B)(A \times B) .
Note
The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other.
Warning
If the tensor has a batch dimension of size 1, then squeeze(input) will also remove the batch dimension, which can lead to unexpected errors.
out (Tensor, optional) – the output tensor.
Example:


Matrix multiplies a sparse tensor mat1 with a dense tensor mat2, then adds the sparse tensor input to the result.
Note: This function is equivalent to torch.addmm(), except input and mat1 are sparse.


Concatenates a sequence of tensors along a new dimension.
All tensors need to be of the same size.
out (Tensor, optional) – the output tensor.


Returns the standard-deviation of all elements in the input tensor.
If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.
Example:
Returns the standard-deviation of each row of the input tensor in the dimension dim. If dim is a list of dimensions, reduce over all of them.
If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).
If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.
out (Tensor, optional) – the output tensor.
Example:


Returns the standard-deviation and mean of all elements in the input tensor.
If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.
Example:
Returns the standard-deviation and mean of each row of the input tensor in the dimension dim. If dim is a list of dimensions, reduce over all of them.
If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).
If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.
Example:


Short-time Fourier transform (STFT).
Warning
From version 1.8.0, return_complex must always be given explicitly for real inputs and return_complex=False has been deprecated. Strongly prefer return_complex=True as in a future pytorch release, this function will only return complex tensors.
Note that torch.view_as_real() can be used to recover a real tensor with an extra last dimension for real and imaginary components.
The STFT computes the Fourier transform of short overlapping windows of the input. This giving frequency components of the signal as they change over time. The interface of this function is modeled after the librosa stft function.
Ignoring the optional batch dimension, this method computes the following expression:
where mm  is the index of the sliding window, and ω\omega  is the frequency that 0≤ω<n_fft0 \leq \omega < \text{n\_fft} . When onesided is the default value True,
Returns either a complex tensor of size (∗×N×T)(* \times N \times T)  if return_complex is true, or a real tensor of size (∗×N×T×2)(* \times N \times T \times 2) . Where ∗*  is the optional batch size of input, NN  is the number of frequencies where STFT is applied and TT  is the total number of frames used.
Warning
This function changed signature at version 0.4.1. Calling with the previous signature may cause error or return incorrect result.
A tensor containing the STFT result with shape described above
Tensor


A torch.Storage is a contiguous, one-dimensional array of a single data type.
Every torch.Tensor has a corresponding storage of the same data type.
Casts this storage to bfloat16 type
Casts this storage to bool type
Casts this storage to byte type
Casts this storage to char type
Returns a copy of this storage
Casts this storage to complex double type
Casts this storage to complex float type
Returns a CPU copy of this storage if it’s not already on the CPU
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.
Casts this storage to double type
Casts this storage to float type
If shared is True, then memory is shared between all processes. All changes are written to the file. If shared is False, then the changes on the storage do not affect the file.
size is the number of elements in the storage. If shared is False, then the file must contain at least size * sizeof(Type) bytes (Type is the type of storage). If shared is True the file will be created if needed.
Casts this storage to half type
Casts this storage to int type
Casts this storage to long type
Copies the storage to pinned memory, if it’s not already pinned.
Moves the storage to shared memory.
This is a no-op for storages already in shared memory and for CUDA storages, which do not need to be moved for sharing across processes. Storages in shared memory cannot be resized.
Returns: self
Casts this storage to short type
Returns a list containing the elements of this storage
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.


Subtracts other, scaled by alpha, from input.
Supports broadcasting to a common shape, type promotion, and integer, float, and complex inputs.
Example:


Returns the sum of all elements in the input tensor.
input (Tensor) – the input tensor.
dtype (torch.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.
Example:
Returns the sum of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them.
If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).
dtype (torch.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.
Example:


Computes the singular value decomposition of either a matrix or batch of matrices input. The singular value decomposition is represented as a namedtuple (U,S,V), such that input = U diag(S) Vᴴ, where Vᴴ is the transpose of V for the real-valued inputs, or the conjugate transpose of V for the complex-valued inputs. If input is a batch of tensors, then U, S, and V are also batched with the same batch dimensions as input.
If some is True (default), the method returns the reduced singular value decomposition i.e., if the last two dimensions of input are m and n, then the returned U and V matrices will contain only min(n, m) orthonormal columns.
If compute_uv is False, the returned U and V will be zero-filled matrices of shape (m × m) and (n × n) respectively, and the same device as input. The some argument has no effect when compute_uv is False.
Supports input of float, double, cfloat and cdouble data types. The dtypes of U and V are the same as input’s. S will always be real-valued, even if input is complex.
Warning
torch.svd() is deprecated. Please use torch.linalg.svd() instead, which is similar to NumPy’s numpy.linalg.svd.
Note
Differences with torch.linalg.svd():
Note
The singular values are returned in descending order. If input is a batch of matrices, then the singular values of each matrix in the batch is returned in descending order.
Note
The implementation of SVD on CPU uses the LAPACK routine ?gesdd (a divide-and-conquer algorithm) instead of ?gesvd for speed. Analogously, the SVD on GPU uses the cuSOLVER routines gesvdj and gesvdjBatched on CUDA 10.1.243 and later, and uses the MAGMA routine gesdd on earlier versions of CUDA.
Note
The returned matrix U will be transposed, i.e. with strides U.contiguous().transpose(-2, -1).stride().
Note
Gradients computed using U and V may be unstable if input is not full rank or has non-unique singular values.
Note
When some = False, the gradients on U[..., :, min(m, n):] and V[..., :, min(m, n):] will be ignored in backward as those vectors can be arbitrary bases of the subspaces.
Note
The S tensor can only be used to compute gradients if compute_uv is True.
Note
With the complex-valued input the backward operation works correctly only for gauge invariant loss functions. Please look at Gauge problem in AD for more details.
Note
Since U and V of an SVD is not unique, each vector can be multiplied by an arbitrary phase factor eiϕe^{i \phi}  while the SVD result is still correct. Different platforms, like Numpy, or inputs on different device types, may produce different U and V tensors.
out (tuple, optional) – the output tuple of tensors
Example:


Return the singular value decomposition (U, S, V) of a matrix, batches of matrices, or a sparse matrix AA  such that A≈Udiag(S)VTA \approx U diag(S) V^T . In case MM  is given, then SVD is computed for the matrix A−MA - M .
Note
The implementation is based on the Algorithm 5.1 from Halko et al, 2009.
Note
To obtain repeatable results, reset the seed for the pseudorandom number generator
Note
The input is assumed to be a low-rank matrix.
Note
In general, use the full-rank SVD implementation torch.svd for dense matrices due to its 10-fold higher performance characteristics. The low-rank SVD will be useful for huge sparse matrices that torch.svd cannot handle.
A (Tensor): the input tensor of size (∗,m,n)(*, m, n)
q (int, optional): a slightly overestimated rank of A.
conduct; niter must be a nonnegative integer, and defaults to 2
(∗,1,n)(*, 1, n) .


Alias for torch.transpose().
This function is equivalent to NumPy’s swapaxes function.
Examples:


This function returns eigenvalues and eigenvectors of a real symmetric matrix input or a batch of real symmetric matrices, represented by a namedtuple (eigenvalues, eigenvectors).
This function calculates all eigenvalues (and vectors) of input such that input=Vdiag(e)VT\text{input} = V \text{diag}(e) V^T .
The boolean argument eigenvectors defines computation of both eigenvectors and eigenvalues or eigenvalues only.
If it is False, only eigenvalues are computed. If it is True, both eigenvalues and eigenvectors are computed.
Since the input matrix input is supposed to be symmetric, only the upper triangular portion is used by default.
If upper is False, then lower triangular portion is used.
Note
The eigenvalues are returned in ascending order. If input is a batch of matrices, then the eigenvalues of each matrix in the batch is returned in ascending order.
Note
Irrespective of the original strides, the returned matrix V will be transposed, i.e. with strides V.contiguous().transpose(-1, -2).stride().
Warning
Extra care needs to be taken when backward through outputs. Such operation is only stable when all eigenvalues are distinct and becomes less stable the smaller mini≠j∣λi−λj∣\min_{i \neq j} |\lambda_i - \lambda_j|  is.
out (tuple, optional) – the output tuple of (Tensor, Tensor)
A namedtuple (eigenvalues, eigenvectors) containing
(Tensor, Tensor)
Examples:


Expects input to be <= 2-D tensor and transposes dimensions 0 and 1.
0-D and 1-D tensors are returned as is. When input is a 2-D tensor this is equivalent to transpose(input, 0, 1).
input (Tensor) – the input tensor.
Example:


Returns a new tensor with the elements of input at the given indices. The input tensor is treated as if it were viewed as a 1-D tensor. The result takes the same shape as the indices.
Example:


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


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