xlnstorch#

The xlnstorch package provides an LNSTensor class, analogous to PyTorch’s Tensor, which is designed to handle LNS arithmetic. The LNSTensor class is built on top of PyTorch’s Tensor, allowing it to leverage PyTorch’s features such as automatic differentiation, while also providing specialized methods for LNS operations.

Tensor Creation Ops#

lnstensor

Constructs an LNSTensor from some array-like data.

zeros

Returns an LNSTensor filled with zeros, with the specified shape and properties.

zeros_like

Returns an LNSTensor filled with zeros, with the same shape and properties as the input tensor.

ones

Returns an LNSTensor filled with ones, with the specified shape and properties.

ones_like

Returns an LNSTensor filled with ones, with the same shape and properties as the input tensor.

full

Returns an LNSTensor filled with fill_value, with the specified shape and properties.

full_like

Returns an LNSTensor filled with fill_value, with the same shape and properties as the input tensor.

rand

Returns an LNSTensor filled with random numbers from a uniform distribution on the interval [0, 1], with the specified shape and properties.

rand_like

Returns an LNSTensor filled with random numbers from a uniform distribution on the interval [0, 1], with the same shape and properties as the input tensor.

randn

Returns an LNSTensor filled with random numbers from a normal distribution with mean 0 and variance 1, with the specified shape and properties.

randn_like

Returns an LNSTensor filled with random numbers from a normal distribution with mean 0 and variance 1, with the same shape and properties as the input tensor.

empty

Returns an uninitialized LNSTensor with the specified shape and properties.

empty_like

Returns an uninitialized LNSTensor with the same shape and base (unless otherwise specified) as the input tensor.

Constants#

xlnstorch.LNS_ZERO#

A float64 scalar tensor representing zero in the LNS. This is a special value defined to be \(\left( -2^{52} \ll 1 \right) \mid 1 = -9007199254740991\). It is independent of an LNS object’s base.

xlnstorch.LNS_INF#

A float64 scalar tensor representing positive infinity in the LNS. This is a special value defined to be \(2^{53} = 9007199254740992\). It is independent of an LNS object’s base.

xlnstorch.LNS_NEG_INF#

A float64 scalar tensor representing negative infinity in the LNS. This is a special value defined to be \(\left( 2^{53} \right) - 1 = 9007199254740991\). It is independent of an LNS object’s base.

xlnstorch.LNS_ONE#

A float64 scalar tensor representing one in the LNS. For any LNS base \(B\), we have \(\log_B(1) = 0\), so this value is independent of base. It is defined to be \(\left( 0 \ll 1 \right) \mid 0 = 0\).

xlnstorch.LNS_NEG_ONE#

A float64 scalar tensor representing one in the LNS. For any LNS base \(B\), we have \(\log_B(1) = 0\), so this value is independent of base. It is defined to be \(\left( 0 \ll 1 \right) \mid 1 = 1\).

xlnstorch.CSRC_AVAILABLE#

A boolean flag indicating whether the C++ extension is available. When True, high-performance C++ implementations are used; when False, pure Python implementations are used as fallback. To find out why the C++ extension is not available, install the xlnstorch package with the --verbose flag to see the build logs. The internal C++ functions can be accessed via the csrc submodule.

Custom Operations#

The xlnstorch package provides a set of analogous operations to PyTorch’s built-in operations. These operations are registered with PyTorch’s internal dispatch mechanism, so that they can be used in the same way as PyTorch’s built-in operations. For example,

import xlnstorch as xltorch

x = xltorch.lnstensor([1.0, 2.0], f=23)
y = xltorch.lnstensor([3.0, 4.0], f=23)

z = torch.add(x, y)
print(z)
# LNSTensor(value=[4.0000, 6.0000], prec=23)

If you want to implement your own custom implementation of an operation, define a new operation, or use an alternative implementation of an operation, you can use the following functions.

implements

A decorator to register a custom implementation for a given torch function.

get_implementation

Get the implementation tuple for a given torch function and implementation key.

set_default_implementation

Set the default implementation for a given torch function.

get_default_implementation_key

Get the default implementation key for a given torch function.

override_implementation

Temporarily override the default implementation for a torch function within a context.

implements_sbdb

A decorator to register a custom sbdb implementation.

set_default_sbdb_implementation

Set the default implementation for the sbdb function.

override_sbdb_implementation

Temporarily override the default sbdb implementation within a context.

register_xlnsconf_implementation

Registers an implementation of the sbdb function using a ufunc from the xlnsconf packed.

sbdb

Computes the Gaussian logarithm for the given inputs z and s.

align_lnstensor_bases

Aligns the bases of a sequence of LNSTensors to a common base.

format_lnstensor_operands

Converts a variable number of operands to LNSTensor objects, aligning all operands to the base of the first operand that is an LNSTensor.