Skip to content

torchrelay

Overview

Modules

extra

Extended variants of functions from the PyTorch functional API.

itertools

Same as the itertools python package, but in PyTorch.

multivers

Backward-compatible PyTorch API.

jit

TorchScript utilities.

Backends

to

Move/convert to a common dtype or device.

to_max_backend

Move to a common dtype or device.

to_max_device

Move to a common device.

to_max_dtype

Move to a common dtype.

get_backend

Return the backend (dtype and device) of a tensor.

max_backend

Get the (max) dtype and device.

max_device

Find a common device for all inputs.

max_dtype

Find the maximum data type from a series of inputs.

as_torch_dtype

Convert a numpy data type (or a data type name) to a torch data dtype.

as_numpy_dtype

Convert a torch data type (or a data type name) to a numpy data dtype.

Indexing

moveelem

Move elements in a tensor.

slice_tensor_along

Index a tensor along one dimensions.

slice_tensor

Index a tensor along one or several dimensions.

Shapes

movedims

Moves the position of one or more dimensions.

movedims_front2back

Move the first N dimensions to the back.

movedims_back2front

Move the last N dimensions to the front.

shiftdim

Shift the dimensions of x by n.

torch_version

torch_version(mode, version)

Check torch version

Parameters:

Name Type Description Default
mode ('<', '<=', '>', '>=')
'<'
version tuple[int]
required

Returns:

Type Description
True if "torch.version <mode> version"

to

to(*args, dtype=None, device=None)

Move/convert to a common dtype or device.

Parameters:

Name Type Description Default
*args tensor_like

Input tensors or tensor-like objects

()
dtype str or dtype

Target data type

None
device str or device

Target device

None

Returns:

Name Type Description
*args tensor_like

Converted tensors

to_max_backend

to_max_backend(*args, force_float=False, dtype=None, device=None)

Move to a common dtype and device.

See max_dtype and max_device.

Parameters:

Name Type Description Default
*args tensor_like
()
force_float bool
False

Returns:

Name Type Description
*args_to tensor

to_max_device

to_max_device(*args)

Move to a common device.

See max_device.

Parameters:

Name Type Description Default
*args tensor_like
()

Returns:

Name Type Description
*args_to tensor

to_max_dtype

to_max_dtype(*args)

Move to a common data type.

See max_dtype.

Parameters:

Name Type Description Default
*args tensor_like
()

Returns:

Name Type Description
*args_to tensor

get_backend

get_backend(x)

Return the backend (dtype and device) of a tensor

Parameters:

Name Type Description Default
x tensor
required

Returns:

Type Description
dict with keys 'dtype' and 'device'

max_backend

max_backend(*args, dtype=None, device=None)

Get the (max) dtype and device.

Parameters:

Name Type Description Default
args tensors
()

Returns:

Type Description
dict with keys 'dtype' and 'device'

max_device

max_device(*args)

Find a common device for all inputs.

If at least one input object is on a CUDA device:

  • if all cuda object are on the same cuda device, return it
  • if some objects are on different cuda devices, return device('cuda') without an index.

Else, return device('cpu') or None.

Parameters:

Name Type Description Default
*args tensor_like or device_like
()

Returns:

Name Type Description
device device

max_dtype

max_dtype(*args, force_float=False)

Find the maximum data type from a series of inputs.

The returned dtype is the best one to use for upcasting the objects.

  • Tensors and arrays have priority python objects.
  • Tensors and arrays with non-null dimensionality have priority over scalars.
  • If any of the torch/numpy objects have a floating point type a floating point type is returned.
  • If any of the objects is complex, a complex type is returned.
  • If all torch/numpy objects have an integer type and there is an integer type that avoids overflowing, it is returned.
  • If no integer type that ensures underflowing exists, the default floating point data type is returned.
  • If force_float is True, a floating point data type is returned even if all input objects have an integer data type.

Parameters:

Name Type Description Default
*args tensor_like or type_like
()
force_float bool
False

Returns:

Name Type Description
dtype dtype

as_torch_dtype

as_torch_dtype(dtype, byteswap=True, upcast=False)

Convert a numpy data type (or a data type name) to a torch data dtype.

Warning

Builtin Python data types int and float are mapped to torch.int32 and torch.float32, to match torch.as_tensor's behavior. It differs from as_numpy_dtype, which maps these types to np.int64 and np.float64.

Parameters:

Name Type Description Default
dtype str or dtype or dtype

Input data type

required
byteswap bool

If the data type is not implemented in PyTorch but its byteswapped version is, return the byteswapped version. If False, raise a TypeError.

True
upcast bool

If the data type is not implemented in PyTorch, but its values can be represented by a larger data type that exists in PyTorch, return the larger data type. If False, raise a TypeError.

False

Returns:

Name Type Description
dtype dtype

Torch data type

as_numpy_dtype

as_numpy_dtype(dtype, upcast=False)

Convert a torch data type (or a data type name) to a torch data dtype.

Warning

Builtin Python data types int and float are mapped to np.int64 and np.float64, to match np.asarray's behavior. It differs from as_torch_dtype, which maps these types to np.int32 and np.float32.

Parameters:

Name Type Description Default
dtype str or dtype or dtype

Input data type

required
upcast bool

If the data type is not implemented in NumPy, but its values can be represented by a larger data type that exists in NumPy, return the larger data type. If False, raise a TypeError.

False

Returns:

Name Type Description
dtype dtype

Numpy data type

moveelem

moveelem(input, source, destination, dim=-1)

Move elements in a tensor

Parameters:

Name Type Description Default
input tensor

Input tensor

required
source [sequence of] int

Source indices of elements to move

required
destination [sequence of] int

Target indices of moved elements

required
dim int

Dimension along which to move elements

-1

Returns:

Name Type Description
output tensor

slice_tensor_along

slice_tensor_along(x, index, dim=-1)

Index a tensor along one dimensions.

This function is relatively similar to torch.index_select, except that it uses the native indexing mechanism and can therefore returns a tensor that use the same storage as the input tensor.

It is faster but less versatile than slice_tensor.

Parameters:

Name Type Description Default
x tensor

Input tensor.

required
index int or list[int] or slice

Indices to select along dim.

required
dim int

Dimension to index.

last

Returns:

Name Type Description
y tensor

Output tensor.

slice_tensor

slice_tensor(x, index, dim=None)

Index a tensor along one or several dimensions.

This function is relatively similar to torch.index_select, except that it uses the native indexing mechanism and can therefore returns a tensor that use the same storage as the input tensor.

Parameters:

Name Type Description Default
x tensor

Input tensor.

required
index index_like or tuple[index_like]

Indices to select along each dimension in dim. If multiple dimensions are indexed, they must be held in a tuple (not a list). Each index can be a long, list of long, slice or tensor of long, but cannot be an ellipsis or tensor of bool.

required
dim int or sequence[int]

Dimensions to index. If it is a list, index must be a tuple. By default, the last n dimensions (where n is the number of indices in index) are used.

None

Returns:

Name Type Description
y tensor

Output tensor.

movedims

movedims(input, source, destination)

Moves the position of one or more dimensions

Other dimensions that are not explicitly moved remain in their original order and appear at the positions not specified in destination.

Parameters:

Name Type Description Default
input tensor

Input tensor

required
source int or sequence[int]

Original positions of the dims to move. These must be unique.

required
destination int or sequence[int]

Destination positions for each of the original dims. These must also be unique.

If a single destination is provided

  • if it is negative, the last source dimension is moved to destination and all other source dimensions are moved to its left.
  • if it is positive, the first source dimension is moved to destination and all other source dimensions are moved to its right.
required

Returns:

Name Type Description
output tensor

Tensor with moved dimensions.

movedims_front2back

movedims_front2back(tensor, dim)

Move the first N dimensions to the back

movedims_back2front

movedims_back2front(tensor, dim)

Move the last N dimensions to the front

shiftdim

shiftdim(x, n=None)

Shift the dimensions of x by n.

Parameters:

Name Type Description Default
x tensor

Input tensor.

required
n int

Shift.

  • When n is positive, shiftdim shifts the dimensions to the left and wraps the N leading dimensions to the end.
  • When n is negative, shiftdim shifts the dimensions to the right and pads with singletons.
  • When n is None, shiftdim removes all leading singleton dimensions. The number of removed dimensions is returned as well.
None

Returns:

Name Type Description
x tensor

Output tensor.

n int, if n is None

Number of removed dimensions