Operators#
As part of the xlnstorch package, we provide a set of operations
that are analogous to PyTorch’s built-in operations. These operations
are registered with PyTorch’s internal dispatch mechanism, allowing
them to be used in the same way as PyTorch’s built-in operations.
These operations can be accessed via the traditional PyTorch mechanism,
such as torch.add() or torch.matmul().
C++ Implementations#
For operations that are computationally intensive, we provide C++ implementations that can be used to accelerate the computation. These implementations are available if the package is built with C++ extensions enabled and are enabled by default. You can toggle the use of C++ implementations with the following function:
from xlnstorch.operators import toggle_cpp_implementations
toggle_cpp_implementations(True) # enable C++ implementations
toggle_cpp_implementations(False) # disable C++ implementations
Currently, there are C++ implementations for the following operations:
Addition
Summation
Matrix Multiplication
Convolution functions
Custom SBDB functions#
In LNS, numbers are represented by their logarithms. To implement addition and subtraction, we work with their logarithmic forms and a special function, commonly known as the “sum/difference in the log domain” (sbdb) function, which is closely related to Gaussian logarithms.
Suppose two numbers \(X\) and \(Y\) are represented as:
To compute \(\log_B \left( \vert X \rvert + \lvert Y \rvert \right)\) and \(\log_B \left( \vert X \rvert - \lvert Y \rvert \right)\), we use the identities:
where the “sum” and “difference” helper functions are defined as:
However, computing \(s_B(z)\) and \(d_B(z)\) directly is computationally expensive, especially on hardware that does not support efficient logarithm and exponentiation. For this reason, xlnstorch provides fast, approximate implementations of these functions to accelerate LNS addition and subtraction.
By using these approximations, we achieve a good trade-off between numerical accuracy
and computational efficiency in LNS arithmetic within the package. See xlnsconf
for more details on these implementations and the papers that describe them.
Tab#
The ‘tab’ method provides fast LNS addition and subtraction by precomputing the values of \(s_B(z)\) and \(d_B(z)\) and storing them in lookup tables. During computation, these tables are used to quickly retrieve approximate results instead of calculating the logarithms and exponentials directly. This approach enables rapid and efficient evaluation of LNS arithmetic operations with minimal computational cost, at the expense of increased memory usage and a fixed precision determined by the table resolution.
To use the ‘tab’ method, you must first initialize the lookup table with the desired base or precision, and a filestem to store the table.
import xlnstorch as xltorch
xltorch.operators.implementations.tab.get_table("filestem", f=10)
xltorch.set_default_sbdb_implementation("tab")
a = xltorch.lnstensor([1.0, 2.0], f=10)
b = xltorch.lnstensor([3.0, 4.0], f=10)
c = torch.add(a, b) # uses the tab implementation
Utah-Tayco#
The ‘utah_tayco’ method is an approximate implementation of the sbdb function that uses unpartitioned linear Taylor interpolation and/or cotransformation of the Gaussian logarithm.
To use the ‘utah_tayco’ method, there is no initialization required and can be used as follows:
import xlnstorch as xltorch
xltorch.set_default_sbdb_implementation("utah_tayco")
a = xltorch.lnstensor([1.0, 2.0], f=10)
b = xltorch.lnstensor([3.0, 4.0], f=10)
c = torch.add(a, b) # uses the utah_tayco implementation
Internal Operators#
These are the internal operations performed on torch.Tensor internal
representations of LNSTensor objects related to arithmetic operations.
These operations are useful if you want to implement your own custom
operations. For the most part, these internal operator functions wrap the
apply_lns_op() function.
Arithmetic Operations#
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
Comparison Operations#
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
Miscellaneous Operations#
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
Loss Operations#
Note that in practice, you should use the standard PyTorch
loss classes, such as torch.nn.MSELoss, which are already
implemented to work with LNSTensor objects. However, if you
want to implement your own custom loss functions, or want more
control over the loss computation, you can use the following
functions.
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
Activation Operations#
Again, in practice, you should use the standard PyTorch
activation classes, such as torch.nn.ReLU, which are
already implemented to work with LNSTensor objects.
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
Layer Operations#
As per usual, you should use the standard PyTorch
layer classes, such as torch.nn.Linear, which
support LNSTensor objects.
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |
|
See docs for |