Skip to content

Choosing a Parameterization

A logic gate is fundamentally a lookup table (LUT) that returns 0 or 1, and it is not differentiable. Parameterization is what wraps it in continuous parameters so gradients can flow, and this library implements five schemes. The choice made here determines accuracy, parameter count, and the quality of the circuit after discretization.

An NN-input LUT holds a truth table of 2N2^N entries and can express 22N2^{2^N} Boolean functions.

NNEntries 2N2^NExpressible functions 22N2^{2^N}Corresponds to
2416difflogic’s 16 gates
41665,536LUT4
664about 1.8×10191.8\times10^{19}The same expressiveness as an FPGA LUT6

During training this table is approximated by continuous values; at inference it is discretized back into a pure Boolean table. The general form is below, and the only difference between schemes is the weight transform ff.

y=∑k=02N−1f(W)k∏j=0N−1(xj if bit(k,j)=1 else 1−xj)y = \sum_{k=0}^{2^N-1} f(W)_k \prod_{j=0}^{N-1}\bigl(x_j \text{ if } \mathrm{bit}(k,j)=1 \text{ else } 1-x_j\bigr)
SchemeWeight transform ffParams/gateHard discretizationExpressible hard functionsOrigin
softmax (N=2)softmax over the 16 gates16argmax → 16-gate tableAll 16 functionsdifflogic (DLGN)
softmax (N≥4)softmax over the 2N2^N entries2N2^NOnly entries with probability > 0.5 become 1A single minterm or FALSE only—
independentPer-entry sigmoid2N2^Nσ(w)>0.5\sigma(w)>0.5All 22N2^{2^N} functionsBinaryBrain
iwpPer-input sigmoid, combined by productNNσ(w)>0.5\sigma(w)>0.5Product-form functions only (no XOR)LDDLGN
rawWeights held directly in [0,1][0,1]2N2^NThresholded at hardware lowering timeAll 22N2^{2^N} functionsBinaryBrain
warpWalsh–Hadamard coefficients, Gumbel smoothing2N2^Nlogit > 0All 22N2^{2^N} functionsWARP

The weights are constrained to sum to 1. It is like distributing 100 yen among boxes: raising one automatically lowers the others. The advantage is that gradients reach every parameter in a coupled way.

N=2 is a special case: instead of the 4 truth-table entries, it becomes a 16-gate mode that takes softmax over the 16 complete Boolean functions (the shape of weights is (out_dim, 16)). What argmax selects is a finished function such as AND/OR/XOR/NAND, so discretization loses no expressiveness.

Each entry independently carries a probability of being 1 (σ(wk)\sigma(w_k); they do not sum to 1). Because any truth table can be expressed directly, no expressiveness is lost at discretization. It maps cleanly onto an FPGA LUT6.

softmax(N≥4): [0.01, 0.02, 0.07, 0.90] sum=1 → at most 1 bit can be set
independent: [0.10, 0.80, 0.30, 0.95] independent → any bit pattern is reachable

It holds only NN parameters instead of 2N2^N. At N=6 that is 64 → 6, a more than tenfold reduction, and training is faster too. The price is that it cannot express functions like XOR where the combination itself is the point. The scheme bets on the observation that most gates in a trained model converge to AND/OR/NOT.

  • raw — keeps weights as real numbers in [0,1][0,1] (initialized as N(0.5,0.01)\mathcal{N}(0.5, 0.01)). Inference also uses continuous values, and discretization happens only at hardware lowering time.
  • warp — learns coefficients in the Walsh–Hadamard basis rather than the truth table itself, and discretizes by the sign of the logit. The discretization gap is essentially zero (measured at −0.06pp on MNIST N=2 — that is, hard beats soft) and it runs at 11–48% of softmax’s FLOPs. A degree cap (wh_degree_cap) that discards higher-order coefficients can shrink the circuit further.

The settled conclusion: softmax for N=2, independent for N≥4

Section titled “The settled conclusion: softmax for N=2, independent for N≥4”

At N≥4, softmax operates on a simplex over the 2N2^N entries, not the 16-gate table. Since the entries sum to 1, discretization (probability > 0.5) can set at most one bit — meaning the only hard tables you can build are a single minterm or constant FALSE. Everyday functions like pass-through, OR, and XOR are structurally inexpressible.

flowchart LR
subgraph S16["softmax N=2 (16 gates)"]
A1["argmax over 16 gates"] --> A2["AND / OR / XOR / NAND…<br/>all 16 functions ✅"]
end
subgraph SN["softmax N≥4 (entry simplex)"]
B1["constraint Σ=1"] --> B2["at most 1 bit set<br/>single minterm or FALSE ❌"]
end
subgraph IND["independent N≥4"]
C1["per-entry sigmoid"] --> C2["any truth table ✅"]
end
style A2 fill:#10b98126,stroke:#10b981
style B2 fill:#ef444426,stroke:#ef4444
style C2 fill:#10b98126,stroke:#10b981

Experimental evidence: in a controlled experiment on a convolutional configuration that changed nothing but softmax (entry simplex) → independent, accuracy improved by +3.2pt despite half the epochs. On fully connected MNIST, softmax N=4 tops out at hard 94.54% with a 1.12pp discretization gap, losing to warp N=4 under the same conditions (95.50% / 0.14pp). The point of this finding is that missing expressiveness cannot be made up with more training.

independent goes through a sigmoid, and combined with a saturating initialization (logits around ±5) it makes gradients decay rapidly through deep serial composition. Measurements showed gradient norms across all layers dropping to around 10−510^{-5}, and at the default lr=0.02 the soft accuracy did not move past its first digit even after 10 epochs. Training does progress at lr ≥ 0.3, so the standard move when using independent is to raise the learning rate by an order of magnitude first and isolate the problem from there.

ターミナルウィンドウ
# N=6 (FPGA LUT6 compatible) + independent + a higher learning rate
uv run griffith train configs/recipes/dlgn_mnist_quick.yaml \
model.n_inputs=6 model.k=4000 model.parameterization=independent optim.lr=0.3
flowchart TD
START["Choose a parameterization"] --> Q1{"What is the gate fan-in N?"}
Q1 -->|"N=2<br/>(difflogic compatible; good for CPU bit-parallel)"| SM["softmax<br/>16-gate mode<br/>start here"]
Q1 -->|"N=4 / N=6<br/>(good for FPGA LUT4/LUT6)"| Q2{"What matters most?"}
Q2 -->|"hard accuracy / circuit quality"| IND["independent<br/>+ raise lr to 0.1–0.3"]
Q2 -->|"minimal discretization gap / training speed"| WARP["warp<br/>(WH coefficients + Gumbel)"]
Q2 -->|"parameter count / memory"| IWP["iwp<br/>only N params. Give up on XOR"]
IND --> Q3{"soft is high but<br/>hard drops?"}
Q3 -->|"yes"| MORE["train more epochs<br/>→ if that fails, use warp"]
Q3 -->|"no"| OK["done"]
SM --> OK
WARP --> OK
IWP --> OK
style SM fill:#10b98126,stroke:#10b981
style IND fill:#10b98126,stroke:#10b981
style WARP fill:#38bdf826,stroke:#38bdf8
style IWP fill:#eab30826,stroke:#eab308

Measured reference (MNIST, 6 layers, τ=10, 50 epochs, bs=128, lr=0.01)

Section titled “Measured reference (MNIST, 6 layers, τ=10, 50 epochs, bs=128, lr=0.01)”
softmax N=2warp N=2warp N=4softmax N=4
hard accuracy97.74%96.11%95.50%94.54%
soft accuracy98.30%96.05%95.64%95.66%
Discretization gap0.56pp−0.06pp0.14pp1.12pp
Gate count96,00096,00024,00024,000
Parameter count1,536,000384,000384,000384,000
Training time5.0 min3.1 min2.1 min1.7 min

What to take away:

  • N=2 softmax leads on accuracy by a clear margin — but it costs 4× the parameters.
  • warp has essentially zero discretization gap. The soft score carries over to the circuit as-is.
  • Only softmax N=4 has a large gap (1.12pp). The missing expressiveness described above shows up directly in the numbers.
  • After discretization every scheme yields the same kind of LUT circuit. There is no difference in inference performance; the difference exists only during training.
# recipe YAML
model:
n_inputs: 6
parameterization: independent
ターミナルウィンドウ
# CLI override
uv run griffith train <recipe>.yaml model.parameterization=independent optim.lr=0.3
# Python API
from griffith_logic.layers.lut import LutLayer
layer = LutLayer(in_dim=784, out_dim=4000, n_inputs=6,
parameterization='independent')
# LutLayer(in=784, out=4000, n_inputs=6, entries=64, param=independent)
print(layer) # the repr shows the scheme you picked, as-is

LutLayer.PARAMETERIZATIONS defines the list of valid values, and an unknown string raises an error immediately at initialization.

Addendum: the sin estimator (paper IWP) and gates other than LUTs

Section titled “Addendum: the sin estimator (paper IWP) and gates other than LUTs”

For independent, the estimator ρ\rho that maps Ωk\Omega_k to the entry probability ωk=ρ(Ωk)\omega_k = \rho(\Omega_k) can be the default sigmoid or estimator: sin (ρ(Ω)=0.5+0.5sin⁡Ω\rho(\Omega) = 0.5 + 0.5\sin\Omega, the setting used in the experiments of Light Differentiable Logic Gate Networks). Residual initialization then becomes Ω∼N(±μ,σ)\Omega \sim \mathcal N(\pm\mu, \sigma) (defaults μ=1.2\mu=1.2, σ=0.25\sigma=0.25). The same option works for both LutLayer and LutConv2d (LutConv2d(..., parameterization='independent', estimator='sin')).

There is also a gate that does not hold a truth table directly: LTTLayer (linear threshold gate). Gate j computes yj=1[∑kwjkxjk+bj>0]y_j = 1[\sum_k w_{jk} x_{jk} + b_j > 0] with N+1 parameters per gate. It can only express linearly separable Boolean functions (for N=2, the 14 functions other than XOR / XNOR), but training always uses hard values plus STE (ste='clip'|'identity'|'sigmoid'), so there is no discretization gap by construction, and in the IR it becomes an ordinary N-input LUT (export/discretize.py::truth_tables_from_ltt enumerates all 2N2^N patterns).