mixle.engines.lns_nn module

Neural-network operations in a logarithmic number system.

Built on mixle.engines.lns.LogNumberSystem. Two families of op benefit from LNS, both because they fight transcendentals (exp/log), not BLAS:

  • softmax / cross-entropy / log-softmax: the normalizer is a logsumexp over logits (the LM head over the vocabulary, a classifier over classes, an attention/MoE-router softmax). The integer logsumexp replaces the exp``+``log with integer max + a LUT (~2x measured). The model’s logits are quantized by the same log step; only softmax-back-to-linear still needs an exp.

  • sum-product circuits / probabilistic circuits: the whole forward pass is sums and products of probabilities, so in LNS every product node is an integer ADD and every sum node an integer logadd. The entire network runs in integer log-space, not only the normalizer.

These are inference and scoring operations. The gradient path stays in floating autograd, while LNS provides a compact integer representation for log-space math.

log_softmax(logits, lns, axis=-1)[source]

Compute log-softmax through the integer log-partition.

Parameters:
  • logits (Any)

  • lns (LogNumberSystem)

  • axis (int)

Return type:

ndarray

softmax(logits, lns, axis=-1)[source]

Softmax with the normalizer computed in LNS; back-to-linear needs one exp (for attention.V etc.).

Parameters:
  • logits (Any)

  • lns (LogNumberSystem)

  • axis (int)

Return type:

ndarray

cross_entropy(logits, targets, lns, axis=-1)[source]

Mean negative log-likelihood mean(logsumexp(logits) - logit[target]) via the integer normalizer.

The LM / classifier loss: the log-partition over the vocab/classes is an integer logsumexp; the target logit is gathered from the same quantized logits, so the loss is integer until the final scale.

Parameters:
  • logits (Any)

  • targets (Any)

  • lns (LogNumberSystem)

  • axis (int)

Return type:

float

class SumProductCircuit(nodes)[source]

Bases: object

A probabilistic circuit evaluated entirely in integer log-space (product=add, sum=logadd).

nodes is a topologically ordered list (children before parents), each a tuple:
  • ("leaf", leaf_id) – an input whose log-value is supplied at evaluation,

  • ("product", [child indices]) – log-output = sum of children (integer ADD),

  • ("sum", [child indices], [log_weights]) – log-output = logsumexp of weighted children.

The root is the last node. evaluate_lns runs the whole forward pass on integers; evaluate_float is the float64 reference. Leaf values may be scalars or arrays (the forward broadcasts).

Parameters:

nodes (list[tuple])

evaluate_lns(lns, leaf_values)[source]

Evaluate the circuit with log-number-system arithmetic.

Parameters:
  • lns (LogNumberSystem)

  • leaf_values (dict[Any, Any])

Return type:

ndarray

evaluate_float(leaf_values)[source]

Evaluate the circuit with float64 reference arithmetic.

Parameters:

leaf_values (dict[Any, Any])

Return type:

ndarray