Skip to content

Quickstart

This page assumes the package is already installed (see Installation). In the next five minutes you’ll solve XOR with logic gates.

ターミナルウィンドウ
uv sync --extra dev --extra cpu # setup (use --extra cu128 on GPU machines)
uv run python examples/01_xor.py # learn XOR → accuracy 1.00 on the discretized circuit
make test # fast test suite (under 2 minutes, always green)

LutLayer is an ordinary nn.Module, so you can drop it anywhere inside an existing model. Below is a three-layer N=4 LUT network sized for MNIST.

import torch
import griffith_logic as gl # `gl` is the idiomatic alias
# A 3-layer N=4 LUT network (MNIST-sized)
model = torch.nn.Sequential(
torch.nn.Flatten(),
gl.LutLayer(in_dim=784, out_dim=16000, n_inputs=4),
gl.LutLayer(in_dim=16000, out_dim=16000, n_inputs=4),
gl.LutLayer(in_dim=16000, out_dim=16000, n_inputs=4),
gl.GroupSum(k=10, tau=30.0),
)
y = model(torch.rand(32, 784)) # (32, 10) class logits

Three things are worth internalizing.

  • gl.LutLayer(in_dim, out_dim, n_inputs=N) — every output neuron is one N-input LUT (truth table). N=2 is difflogic-compatible; N=6 maps onto an FPGA LUT6.
  • gl.GroupSum(k, tau) — splits the final gate outputs evenly across k classes, sums each group and divides by tau to produce logits. It’s the exit from the world of logic gates into classification scores.
  • model.eval() — during training gradients flow through a continuous relaxation (softmax and friends); switching to eval applies argmax discretization and the model collapses into a pure Boolean circuit. The accuracy after that collapse (hard accuracy) is the real performance.

XOR is the smallest problem that isn’t linearly separable, and the classic way to sanity-check the expressive power of a logic-gate circuit.

  1. Run the example as-is.

    ターミナルウィンドウ
    uv run python examples/01_xor.py
  2. Check the output.

    XOR: soft acc=1.00, hard acc=1.00

    hard acc=1.00 means all four input patterns are correct even after discretizing into a pure Boolean circuit.

  3. Read through it one cell at a time — there’s a walkthrough with real executed output.

    → Appendix: XOR Internals

The actual code is about 30 lines, and the whole idea is “train with a continuous relaxation, then collapse into a circuit with eval()”.

model = nn.Sequential(
gl.LutLayer(2, 16, n_inputs=2), # softmax over 16 gates (difflogic-compatible)
gl.LutLayer(16, 16, n_inputs=2),
gl.GroupSum(k=2, tau=2.0),
)
opt = torch.optim.Adam(model.parameters(), lr=0.05)
for step in range(500):
opt.zero_grad()
F.cross_entropy(model(x), y).backward()
opt.step()
model.eval() # discretize (argmax collapses it into a complete Boolean circuit)
acc_hard = (model(x).argmax(-1) == y).float().mean()

The XOR circuit before (left) and after (right) training

On the left, before training: each node is still a mixture of the 16 candidate Boolean functions, so it can only be labelled LUT, and the paler the color the less decided the node is. On the right, after training: argmax has settled on one function per node, so each node now carries its name (AND / XOR / NAND / !A …). Training is the act of picking one gate out of 16, over and over. Green = logic gates, orange = the real-valued part (GroupSum), gray = input bits and wiring.

Here is the test accuracy when the exact same code is scaled up to MNIST (400-bit input, 4000 gates). Orange is the continuous relaxation used during training (soft); green is the discretized circuit itself after argmax (hard), and the latter is the real performance.

soft / hard accuracy on MNIST

→ The full, already-executed walkthrough is in 1. Train on MNIST.

A trained model can be turned into Verilog / VHDL by way of the IR (JSON). This is the exit ramp where the GPU disappears.

from griffith_logic.export import export_ir
from griffith_logic.export.verilog import write_verilog
from griffith_logic.ir import validate
model.eval()
ir = export_ir(model, input_width=2, name='xor')
validate(ir, target='hdl')
write_verilog(ir, 'out_examples/xor.v')

To follow training, Verilog generation and equivalence checking end to end in one go, head to the end-to-end walkthrough.