Skip to content

LutLayer internals

LutLayer is this library’s smallest building block: a layer that makes an N-input LUT (look-up table) differentiable. This page follows the math to answer “why this formula?” and “why is the backward pass written by hand?”.

1. Starting point — a LUT isn’t differentiable

Section titled “1. Starting point — a LUT isn’t differentiable”

A LUT is a lookup table returning one bit for N binary inputs. At N=2 that’s a 4-entry truth table, which can be filled in 24=162^4 = 16 ways — that is, it can express all 16 two-input Boolean functions (AND / OR / XOR / NAND …). At N=6 there are 264≈1.8×10192^{64} \approx 1.8 \times 10^{19} ways, the same expressive power as an FPGA LUT6.

The problem is that the entries of a truth table are discrete 0 or 1, so gradient descent cannot touch them. The fix is to allow continuous values between 0 and 1 during training and snap back to discrete at inference. How that continuous value is held is the parameterization, and it sits at the heart of this library’s design.

Each neuron computes:

y  =  ∑k=02N−1f(W)k⋅basisk(x0,…,xN−1)y \;=\; \sum_{k=0}^{2^N-1} f(W)_k \cdot \mathrm{basis}_k(x_0, \dots, x_{N-1}) basisk(x)  =  ∏j=0N−1xj bj(k) (1−xj) 1−bj(k)\mathrm{basis}_k(x) \;=\; \prod_{j=0}^{N-1} x_j^{\,b_j(k)} \,(1 - x_j)^{\,1 - b_j(k)}

Here bj(k)b_j(k) is bit jj of kk, and ff maps the parameters WW into “weights” (the default is softmax\mathrm{softmax}).

basisk(x)\mathrm{basis}_k(x) measures how much the input is exactly pattern kk. When the inputs are settled at 0/1 the basis becomes one-hot and the formula reduces to “read row kk of the truth table” — the definition of a LUT. When inputs take intermediate values, several entries are read out partially at once: that is the continuous relaxation.

Expanding at N=2 makes the meaning plain.

basis0=(1−x0)(1−x1)1 when both are 0basis1=x0(1−x1)1 when only x0 is 1basis2=(1−x0)x11 when only x1 is 1basis3=x0x11 when both are 1\begin{aligned} \mathrm{basis}_0 &= (1-x_0)(1-x_1) &&\text{1 when both are 0} \\ \mathrm{basis}_1 &= x_0 (1-x_1) &&\text{1 when only $x_0$ is 1} \\ \mathrm{basis}_2 &= (1-x_0) x_1 &&\text{1 when only $x_1$ is 1} \\ \mathrm{basis}_3 &= x_0 x_1 &&\text{1 when both are 1} \end{aligned}

If only w3w_3 is large you get y≈x0∧x1y \approx x_0 \land x_1 (AND); if w1,w2w_1, w_2 are large you get y≈x0⊕x1y \approx x_0 \oplus x_1 (XOR).

LutLayer holds exactly two tensors.

TensorShapeLearned
weights(out_dim, 2^N)yes (logits, pre-parameterization)
indices(N, out_dim)no (the wiring: which inputs each neuron looks at)

Fixing the indices wiring and learning only the weights is the basic stance of the DLGN family (setting connections='unique' guarantees every input is used at least once). In other words, what is being learned is not the wiring but which Boolean function each gate becomes.

3. The butterfly basis — building 2N2^N products by divide and conquer

Section titled “3. The butterfly basis — building 2N2^N2N products by divide and conquer”

Computing each basisk\mathrm{basis}_k separately from the definition costs N multiplications per entry. The butterfly basis instead reuses intermediate results, doubling the size stage by stage (the name comes from the same divide-and-conquer structure as the FFT butterfly).

graph TD
L0["Level 0<br/>[ 1 ]"]
L1a["(1-x₀)"]
L1b["x₀"]
L2a["(1-x₀)(1-x₁)<br/>= basis₀"]
L2b["x₀(1-x₁)<br/>= basis₁"]
L2c["(1-x₀)x₁<br/>= basis₂"]
L2d["x₀x₁<br/>= basis₃"]
L0 -->|"×(1-x₀)"| L1a
L0 -->|"×x₀"| L1b
L1a -->|"×(1-x₁)"| L2a
L1b -->|"×(1-x₁)"| L2b
L1a -->|"×x₁"| L2c
L1b -->|"×x₁"| L2d

The implementation is four straightforward lines (core/basis.py).

def _compute_basis_butterfly(gathered, n_inputs):
basis = torch.ones(..., 1) # Level 0: [1]
for j in range(n_inputs):
xj = gathered[..., j:j+1]
basis = torch.cat([basis * (1.0 - xj), basis * xj], dim=-1)
return basis # size 2^n_inputs

The complexity itself is the same O(N⋅2N)O(N \cdot 2^N) as the naive version. All three reasons for choosing butterfly are about the backward pass.

  • The intermediate state at each level can be saved and reused in backward (memory efficiency)
  • ∑kbasisk=1\sum_k \mathrm{basis}_k = 1 holds structurally (each level only multiplies by (1−xj)+xj=1(1-x_j) + x_j = 1, so numerical error doesn’t accumulate)
  • Backward needs no division (the naive version requires quotients like basisk/xj\mathrm{basis}_k / x_j, which break as xj→0x_j \to 0)

4. Parameterization — five choices of f(W)f(W)

Section titled “4. Parameterization — five choices of f(W)f(W)f(W)”

With the same central formula, how you map WW into weights (the choice of ff) changes expressive power, ease of discretization, and parameter count. LutLayer(parameterization=...) offers five.

Methodf(W)f(W)Params/gateConstraintOrigin
softmax (default)softmax(W)\mathrm{softmax}(W)2N2^N (16 at N=2N{=}2)∑kwk=1\sum_k w_k = 1difflogic
independentσ(W)\sigma(W)2N2^Nentries are independentBinaryBrain
rawWW as-is2N2^Nholds [0,1][0,1] directlyBinaryBrain
iwpper-input σ(αj)\sigma(\alpha_j)NNdecomposable functions onlyLDDLGN
warpWalsh–Hadamard coefficients2N2^Nunconstrained realsWARP

Mathematically the difference boils down to “do the entries compete with each other?”. Here is each method in turn.

The weights live on the simplex ∑kwk=1\sum_k w_k = 1. Think of distributing 100 yen across 2N2^N boxes: raising one automatically lowers the others. Gradients reach every parameter in a single backward pass, but it comes with a hard constraint — at most one entry can be picked for the hard table (see below).

independent / raw — entries are independent

Section titled “independent / raw — entries are independent”

Each entry independently carries a “probability of being 1”, and they do not sum to 1. Any of the 2N2^N 0/1 tables can be expressed, so the hard table is more expressive than softmax. The gradient is ∂L/∂pk=∂L/∂y⋅basisk\partial L/\partial p_k = \partial L/\partial y \cdot \mathrm{basis}_k and reaches only that entry.

One parameter per input rather than per entry (NN in total). It only learns the weight for “use xjx_j as-is or inverted”, building the function as a product of literals. Extremely light, but it cannot express functions where the combination is the point, such as XOR (the justification is LDDLGN’s empirical finding that the vast majority of gates in a trained model converge to the AND/OR/NOT family).

Learns not the truth table itself but the Walsh–Hadamard transform coefficients θ\theta of it. The output goes through logit=∑kθk⋅whk(x)\mathrm{logit} = \sum_k \theta_k \cdot \mathrm{wh}_k(x) and is smoothed with a Gumbel-Sigmoid. Because it works with unconstrained reals, it tends to be stable at N=4 and 6. Setting wh_degree_cap forces coefficients with popcount(k)>d\mathrm{popcount}(k) > d identically to 0, restricting the hard table to a degree-d polynomial threshold function (the PolyLUT approach).

For the practical side of choosing among them, see Choosing a parameterization.

5. The soft → hard collapse — what happens at inference

Section titled “5. The soft → hard collapse — what happens at inference”

Switch to model.eval() and LutLayer stops relaxing, becoming a settled Boolean circuit. What gets rounded, and how, differs per parameterization.

graph LR
A["training<br/>x ∈ [0,1] continuous<br/>f(W) = continuous distribution"] -->|"model.eval()"| B["inference<br/>x ∈ {0,1}<br/>f(W) = 0/1 truth table"]
B --> C["export_ir<br/>push all 2^N patterns through<br/>to fix the truth table"]
C --> D["IR → Verilog / VHDL"]
MethodDiscretization ruleWhat the hard table can be
softmax (N=2)arg⁡max⁡\arg\max over the 16 gate logitsall 16 Boolean functions
softmax (N>2)only entries with softmax(W)k>0.5\mathrm{softmax}(W)_k > 0.5 become 1since ∑kwk=1\sum_k w_k = 1, at most one entry can be 1. A single minterm, or the constant FALSE
independentσ(W)>0.5\sigma(W) > 0.5any 2N2^N truth table
iwpσ(α)>0.5\sigma(\alpha) > 0.5 → product of literalsdecomposable functions only
warpthreshold at logit>0\mathrm{logit} > 0any function within the degree cap

Suppressing the performance drop on the hard side (the discretization gap) is this library’s quality metric. And the downstream — IR extraction and HDL generation — duplicates none of these rules. export_ir pushes all 2N2^N patterns through the already-eval()ed model and takes the observed outputs as the truth table, so adding parameterizations requires no downstream changes (→ Architecture overview).

LutFunction (core/functions.py) does not defer to PyTorch’s autograd; its backward is written by hand. Two reasons: we don’t want to retain the entire butterfly computation graph, and we want to close the softmax backward pass analytically.

Since y=⟨basis, softmax(W)⟩y = \langle \mathrm{basis},\, \mathrm{softmax}(W) \rangle, we first collect the gradient with respect to the softmax output, then apply the softmax Jacobian Jij=wi(δij−wj)J_{ij} = w_i(\delta_{ij} - w_j) analytically.

g~=∑b∂L∂yb basisb,∂L∂W=w⊙(g~−⟨g~,w⟩)\tilde{g} = \sum_{b} \frac{\partial L}{\partial y_b} \,\mathrm{basis}_b, \qquad \frac{\partial L}{\partial W} = w \odot \bigl(\tilde{g} - \langle \tilde{g}, w \rangle\bigr)
grad_w_soft = einsum('bo,boe->oe', grad_output, basis)
dot = (grad_w_soft * w).sum(dim=-1, keepdim=True)
grad_weights = w * (grad_w_soft - dot)

Gradient of the inputs — walking the butterfly backwards

Section titled “Gradient of the inputs — walking the butterfly backwards”

What forward “branched out and spread”, backward “merges and folds back”. At each level jj, the difference between the upper half (the xjx_j side) and the lower half (the 1−xj1-x_j side) is the gradient of xjx_j.

grad_cur = einsum('bo,oe->boe', grad_output, w)
for j in reversed(range(n_inputs)):
half = grad_cur.shape[-1] // 2
grad_lo, grad_hi = grad_cur[..., :half], grad_cur[..., half:]
grad_gathered[:, :, j] = ((grad_hi - grad_lo) * intermediates[j]).sum(-1)
grad_cur = grad_lo * (1 - xj) + grad_hi * xj # back to the previous level

Because it reuses intermediates[j] (the per-level intermediate state saved in forward) directly, no division appears. Finally, the contributions from multiple neurons referencing the same input are aggregated with scatter_add_ to give grad_x.

AspectPyTorch defaultLutFunction
Computation graphretains the whole butterflysaves intermediate state only
softmax backwardvia the graphanalytic Jacobian
Numerical stabilityinvolves divisiondivision-free
Memorybaselineroughly 50–80% less

7. Residual initialization for deep stacks

Section titled “7. Residual initialization for deep stacks”

Stack logic gates deeply and the initial state is a chain of random functions, so no signal reaches the far end. Residual initialization (CDDLGN §3.2) fixes this by biasing the initial weights toward the function that passes the first input straight through.

softmax([5,0,0,0])≈[0.993, 0.002, 0.002, 0.002]\mathrm{softmax}([5, 0, 0, 0]) \approx [0.993,\ 0.002,\ 0.002,\ 0.002]

Set the logit of the pass-through entry to 5.0 and the initial network behaves almost like the identity, letting gradients travel through a deep tree. LutConv2d has structurally deep trees, so it is on by default; LutLayer defaults to off and takes residual_init=True when needed.

LutConv2d maps each output channel to a single logic-gate tree (a complete N-ary tree). At depth dd, NdN^d leaves draw inputs at random from the receptive field and (Nd−1)/(N−1)(N^d - 1)/(N-1) internal gates convolve bottom-up. Each gate computes exactly the same “butterfly basis × weights” as LutLayer, and likewise collapses into discrete Boolean operations under arg⁡max⁡\arg\max at inference.

Pooling is OrPooling2d = a wrapper around F.max_pool2d, which is the maximum t-conorm = probabilistic logical OR. On binary values it acts literally as Boolean OR.