cassetta.functional
Overview
This module contains utility functions that we find are either incomplete or entirely missing in PyTorch. All of them are rather low-level functions, and do not form an object-oriented API like the rest of the package.
cassetta.functional.jit
|
[ |
Convert linear indices into sub indices (i, j, k). |
|
[ |
Convert sub indices (i, j, k) into linear indices. |
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
Meshgrid with |
|
[ |
Meshgrid with |
|
[ |
Pad a list |
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
|
|
[ |
Cumulative product |
|
[ |
|
|
[ |
|
|
[ |
|
cassetta.functional.jit.indexing
ind2sub
Convert linear indices into sub indices (i, j, k).
The rightmost dimension is the most rapidly changing one -> if shape == [D, H, W], the strides are therefore [H*W, W, 1]
| PARAMETER | DESCRIPTION |
|---|---|
ind |
Linear indices
TYPE:
|
shape |
Size of each dimension.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
subs
|
Sub-indices.
TYPE:
|
Source code in cassetta/functional/jit/indexing.py
sub2ind
Convert sub indices (i, j, k) into linear indices.
The rightmost dimension is the most rapidly changing one -> if shape == [D, H, W], the strides are therefore [H*W, W, 1]
| PARAMETER | DESCRIPTION |
|---|---|
subs |
List of sub-indices. The first dimension is the number of dimension. Each element should have the same number of elements and shape.
TYPE:
|
shape |
Size of each dimension. Its length should be the same as the
first dimension of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ind
|
Linear indices
TYPE:
|
Source code in cassetta/functional/jit/indexing.py
sub2ind_list
Convert sub indices (i, j, k) into linear indices.
The rightmost dimension is the most rapidly changing one -> if shape == [D, H, W], the strides are therefore [H*W, W, 1]
| PARAMETER | DESCRIPTION |
|---|---|
subs |
List of sub-indices. The first dimension is the number of dimension. Each element should have the same number of elements and shape.
TYPE:
|
shape |
Size of each dimension. Its length should be the same as the
first dimension of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ind
|
Linear indices
TYPE:
|
Source code in cassetta/functional/jit/indexing.py
cassetta.functional.jit.math
square
square_
cube
cube_
pow4
pow4_
pow5
pow5_
pow6
pow6_
pow7
pow7_
floor_div
floor_div_int
trunc_div
cassetta.functional.jit.meshgrid
Eager vs TorchScript
The signature of torch.meshgrid differs between eager and torchscript modes.
In eager mode, meshgrid takes an unpacked list of tensors as inputs:
In torchscript mode, it takes a (packed) list of tensors instead:
This makes writing code that works in both eager (by setting the
environment variable PYTORCH_JIT=0) and torchscript modes (with the
default PYTORCH_JIT=1) complicated. Instead, we define functions with
explicit names (meshgrid_list_*) that always take a (packed) list of
tensors as input.
Backward compatibility
For torch<1.10, torch.meshgrid worked in "ij" mode, meaning that
the first tensor of coordinates was mapped to the first dimension of the
output grid and the second tensor of coordinates was mapped to the
second dimension of the output grid. Starting with torch=1.10,
the keyword argument indexing, which takes value "ij" or "xy", was
introduced. Furthermore, the default behavior of the function when indexing
is not used will change from "ij" to "xy" in the future.
To make any code backward compatible, we define explicit functions
postfixed by either _ij or _xy.
meshgrid_list_ij
Creates grids of coordinates specified by the 1D inputs in tensors.
This is helpful when you want to visualize data over some range of inputs.
Given \(N\) 1D tensors \(T_0, \dots, T_{N-1}\) as inputs with corresponding sizes \(S_0, \dots, S_{N-1}\), this creates \(N\) N-dimensional tensors \(G_0, \dots, G_{N-1}\), each with shape \((S_0, \dots, S_{N-1})\) where the output \(G_i\) is constructed by expanding \(T_i\) to the result shape.
Note
0D inputs are treated equivalently to 1D inputs of a single element.
| PARAMETER | DESCRIPTION |
|---|---|
tensors |
list of scalars or 1 dimensional tensors. Scalars will be treated as tensors of size \((1,)\) automatically
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
seq
|
list of expanded tensors
TYPE:
|
Source code in cassetta/functional/jit/meshgrid.py
meshgrid_list_xy
Creates grids of coordinates specified by the 1D inputs in tensors.
This is helpful when you want to visualize data over some range of inputs.
Given \(N\) 1D tensors \(T_0, \dots, T_{N-1}\) as inputs with corresponding sizes \(S_0, \dots, S_{N-1}\), this creates \(N\) N-dimensional tensors \(G_0, \dots, G_{N-1}\), each with shape \((S_0, \dots, S_{N-1})\) where the output \(G_i\) is constructed by expanding \(T_i\) to the result shape.
Note
0D inputs are treated equivalently to 1D inputs of a single element.
Warning
In mode xy, the first dimension of the output corresponds to the
cardinality of the second input and the second dimension of the output
corresponds to the cardinality of the first input.
| PARAMETER | DESCRIPTION |
|---|---|
tensors |
list of scalars or 1 dimensional tensors. Scalars will be treated as tensors of size \((1,)\) automatically
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
seq
|
list of expanded tensors
TYPE:
|
Source code in cassetta/functional/jit/meshgrid.py
cassetta.functional.jit.python
TorchScript compatible functions that act on Python bultins.
pad_list_int
Pad/crop a list of int until it reaches a target length.
Note
If padding, the last element gets replicated.
| PARAMETER | DESCRIPTION |
|---|---|
x |
List of int
TYPE:
|
length |
Target length
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
x
|
List of length
TYPE:
|
Source code in cassetta/functional/jit/python.py
pad_list_float
See [pad_list_int][cassetta.functional.jit.pad_list_int].
Source code in cassetta/functional/jit/python.py
pad_list_str
See [pad_list_int][cassetta.functional.jit.pad_list_int].
Source code in cassetta/functional/jit/python.py
any_list_bool
all_list_bool
prod_list_int
Compute the product of elements in the list
sum_list_int
Compute the sum of elements in the list. Equivalent to sum(x).
reverse_list_int
cumprod_list_int
Cumulative product of elements in the list
| PARAMETER | DESCRIPTION |
|---|---|
x |
List of integers
TYPE:
|
reverse |
Cumulative product from right to left. Else, cumulative product from left to right (default).
TYPE:
|
exclusive |
Start series from 1. Else start series from first element (default).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
Cumulative product
TYPE:
|
Source code in cassetta/functional/jit/python.py
cassetta.functional.jit.tensors
prod_list_tensor
Compute the product of tensors in the list.
Source code in cassetta/functional/jit/tensors.py
sum_list_tensor
Compute the sum of tensors in the list. Equivalent to sum(x).
Source code in cassetta/functional/jit/tensors.py
movedim
Backward compatible torch.movedim