xlnstorch.lnstensor

Contents

xlnstorch.lnstensor#

xlnstorch.lnstensor(data, from_lns=False, requires_grad=False, detach=True, f=None, b=None)#

Constructs an LNSTensor from some array-like data.

The function accepts ordinary numeric data (tensors, NumPy arrays, scalars) and every non-redundant xlns type. Redundant formats (xlnsr and xlnsnpr) are not supported.

The LNSTensor base is chosen in the following order:

  1. If f is given, base = 2.0 ^ (2 ^ -f).

  2. Else if b is given, use b (float or scalar tensor).

  3. Else, default to xlns.xlnsB (global constant).

Parameters:
  • data (LNSTensor, torch.Tensor, numpy.ndarray, numbers, xlns types) –

    • A real-valued tensor/array/scalar to encode or

    • A pre-packed representation (when from_lns is True) or

    • An existing LNSTensor (which will be copied or converted base).

  • from_lns (bool, optional) – If True, treat data as already packed. Defaults to False.

  • requires_grad (bool, optional) – If True, the LNSTensor will track gradients. Defaults to False. If a pre-packed LNSTensor or a torch.Tensor is provided, this parameter is ignored.

  • detach (bool, optional) – If True and data is a torch.Tensor and not from_lns, data will be detached from its computation graph, i.e. this tensor will become a leaf node.

  • f (int, optional) – The number of fractional exponent bits. mutually exclusive with b.

  • b (float, int, torch.Tensor, optional) – The explicit logarithm base; mutually exclusive with f.

Returns:

The constructed LNSTensor.

Return type:

LNSTensor

Raises:
  • ValueError – If both f and b are provided, or if neither can be resolved to a valid base.

  • TypeError – If data is of an unsupported type (i.e. not array-like).