torchrelay
Overview
|
Modules |
|
|
Extended variants of functions from the PyTorch functional API. |
|
|
Same as the itertools python package, but in PyTorch. |
|
|
Backward-compatible PyTorch API. |
|
|
TorchScript utilities. |
|
|
Backends |
|
|
Move/convert to a common dtype or device. |
|
|
Move to a common dtype or device. |
|
|
Move to a common device. |
|
|
Move to a common dtype. |
|
|
Return the backend (dtype and device) of a tensor. |
|
|
Get the (max) dtype and device. |
|
|
Find a common device for all inputs. |
|
|
Find the maximum data type from a series of inputs. |
|
|
Convert a numpy data type (or a data type name) to a torch data dtype. |
|
|
Convert a torch data type (or a data type name) to a numpy data dtype. |
|
|
Indexing |
|
|
Move elements in a tensor. |
|
|
Index a tensor along one dimensions. |
|
|
Index a tensor along one or several dimensions. |
|
|
Shapes |
|
|
Moves the position of one or more dimensions. |
|
|
Move the first N dimensions to the back. |
|
|
Move the last N dimensions to the front. |
|
|
Shift the dimensions of x by n. |
|
torch_version
Check torch version
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode |
('<', '<=', '>', '>=')
|
|
'<'
|
version |
tuple[int]
|
|
required |
Returns:
| Type | Description |
|---|---|
True if "torch.version <mode> version"
|
|
to
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
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
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
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
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
Get the (max) dtype and device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args |
tensors
|
|
()
|
Returns:
| Type | Description |
|---|---|
dict with keys 'dtype' and 'device'
|
|
max_device
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
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
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 |
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
|
Returns:
| Name | Type | Description |
|---|---|---|
dtype |
dtype
|
Torch data type |
as_numpy_dtype
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
|
Returns:
| Name | Type | Description |
|---|---|---|
dtype |
dtype
|
Numpy data type |
moveelem
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
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 |
required |
dim |
int
|
Dimension to index. |
last
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
tensor
|
Output tensor. |
slice_tensor
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 |
required |
dim |
int or sequence[int]
|
Dimensions to index. If it is a list, |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
tensor
|
Output tensor. |
movedims
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
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
output |
tensor
|
Tensor with moved dimensions. |
shiftdim
Shift the dimensions of x by n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
tensor
|
Input tensor. |
required |
n |
int
|
Shift.
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
tensor
|
Output tensor. |
n |
int, if n is None
|
Number of removed dimensions |