DWN / LLViT
ThermometerEncoder
Section titled “ThermometerEncoder”Defined in: griffith_logic.encoding.thermometer.ThermometerEncoder (class)
class ThermometerEncoder( bits: int, mode: str = 'uniform', input_bits: int | None = None,)Thermometer encoding layer.
Arguments
bits— number of bits z per feature (the number of thresholds)mode— ‘uniform’ (evenly spaced) | ‘distributive’ (empirical quantiles)input_bits— number of quantization bits B for the input when converting to IR (export_ir). With None (the default) the encoder stays at the boundary (outside the IR). When set, the extractor takes the per-feature B-bit code c (LSB first, position d·B + j) as input, treats it as the real value q = c / (2^B − 1) (the same as ToTensor’s uint8 → [0, 1]), and bakes the threshold comparison into the truth table of a B-input lut_layer. The torch forward still receives the real value q as before
Attributes
thresholds— (bits, n_features) thresholds (fixed after fitting)
Examples using this API
- DWN — MNIST with Thermometer Encoding and n-Input LUTs —
examples/16_dwn.py
DWNLayer
Section titled “DWNLayer”Defined in: griffith_logic.layers.dwn.DWNLayer (class)
class DWNLayer( in_bits: int, n_luts: int, n_inputs: int = 6, mapping: str = 'random', efd: str = 'paper', alpha: float | None = None, beta: float | None = None, ste: bool = True, clamp_luts: bool = True, lm_tau: float = _DEFAULT_LM_TAU, device: str = 'cpu',)The DWN LUT layer: binary input → n-input LUT × n_luts → binary output.
Arguments
in_bits— number of input bitsn_luts— number of LUTs (RAM nodes) = number of output bitsn_inputs— number of inputs n per LUTmapping— ‘random’ (fixed random connections) | ‘learnable’ (Learnable Mapping)efd— EFD coefficient: ‘paper’ (1/(H+1)) | ‘official’ (α·β^H) alpha, beta: coefficients for efd=‘official’ (the official defaults when None)ste— binarize the output to {0,1} (official default True)clamp_luts— clamp the tables to [−1,1] on every forward during training (official default True)lm_tau— softmax temperature of the Learnable Mapping
Examples using this API
- DWN — MNIST with Thermometer Encoding and n-Input LUTs —
examples/16_dwn.py
LearnableMapping
Section titled “LearnableMapping”Defined in: griffith_logic.layers.dwn.LearnableMapping (class)
class LearnableMapping( in_features: int, out_features: int, tau: float = _DEFAULT_LM_TAU,)Layer that learns the input→LUT connections by gradient descent.
During training it keeps a weight matrix W ∈ ℝ^{in×out}, and by default the forward is the hard selection x_{argmax(W[:,i])} (no inference-time overhead).
Progressive wiring hardening (adapted from the two-phase scheme of SparseLUT arXiv 2503.12829): With forward_mode=‘soft’ the forward becomes the soft mixture x @ softmax(W/tau_soft, dim=0) (pure autograd, no discontinuous jumps in selection). ProgressiveWiringScheduler decays tau_soft geometrically and switches to forward_mode=‘hard’ (the usual argmax+STE) later in training. This is the lever introduced after wiring learning with hard selection collapsed in. Eval is always hard argmax (forward_mode is only consulted during training).
Arguments
in_features— number of input bits Pout_features— number of output bits Q (= number of LUTs × inputs per LUT)tau— softmax temperature for the backward pass (official default 0.001)
DWNClassifier
Section titled “DWNClassifier”Defined in: griffith_logic.models.dwn.DWNClassifier (class)
class DWNClassifier( in_features: int, n_classes: int, layer_widths: tuple[int, ...] = (1000, 500), bits: int = 3, encoder_mode: str = 'distributive', n_inputs: int = 6, mapping: str = 'learnable', efd: str = 'paper', tau: float = _DEFAULT_TAU, device: str = 'cpu',)Classifier built from thermometer encoding + multi-layer DWN LUTs + popcount aggregation.
Arguments
in_features— number of real-valued input features Dn_classes— number of classes klayer_widths— number of LUTs in each DWNLayer (the last one must be divisible by n_classes)bits— bits per feature for thermometer encodingencoder_mode— ‘uniform’ | ‘distributive’ (distributive requires fit_encoder)n_inputs— number of inputs n per LUTmapping— connection scheme of the first layer: ‘random’ | ‘learnable’ (later layers are fixed to random)efd— EFD coefficient: ‘paper’ (1/(H+1)) | ‘official’ (α·β^H)tau— GroupSum temperature (official default 1/0.3)
Examples using this API
- DWN — MNIST with Thermometer Encoding and n-Input LUTs —
examples/16_dwn.py
LLViTConfig
Section titled “LLViTConfig”Defined in: griffith_logic.models.llvit.LLViTConfig (class)
class LLViTConfig( image_size: int = 224, patch_size: int = 16, in_channels: int = 3, num_classes: int = 10, dim: int = 192, depth: int = 12, heads: int = 3, mlp_ratio: int = 4, thermometer_bits: int = 8, thermometer_mode: Literal['uniform', 'distributive'] = 'uniform', lut_hidden: int = 768, lut_out: int = 192, lut_inputs: int = 6, lut_mapping: Literal['random', 'learnable'] = 'learnable', lut_efd: Literal['paper', 'official'] = 'official', lut_ste: bool = True, conditional_sum_bits: int = 4, dropout: float = 0.0, attention_dropout: float = 0.0,)Configuration matching the LL-ViT CIFAR-10 target by default.
Defined in: griffith_logic.models.llvit.LLViT (class)
class LLViT(config: LLViTConfig, channel_mixer: str = 'llvit')Tiny ViT backbone used for baseline and LL-ViT reproduction.
LLViTChannelMixer
Section titled “LLViTChannelMixer”Defined in: griffith_logic.models.llvit.LLViTChannelMixer (class)
class LLViTChannelMixer(config: LLViTConfig)LL-ViT channel mixer: thermometer -> DWN -> DWN -> conditional sum.
ConditionalSum
Section titled “ConditionalSum”Defined in: griffith_logic.models.llvit.ConditionalSum (class)
class ConditionalSum( in_features: int, out_features: int | None = None, quant_bits: int = 4,)Dense encoded-value summation after binary LUT outputs.
During training this uses fp32 encoded values. When
use_quantized_eval=True and the module is in eval mode, the encoded
values are symmetrically quantized to int4-style levels and dequantized for
forward execution.
build_llvit_cifar10
Section titled “build_llvit_cifar10”Defined in: griffith_logic.models.llvit.build_llvit_cifar10 (function)
def build_llvit_cifar10(config: LLViTConfig | None = None) -> LLViT(no docstring yet)
build_deit_tiny_cifar10
Section titled “build_deit_tiny_cifar10”Defined in: griffith_logic.models.llvit.build_deit_tiny_cifar10 (function)
def build_deit_tiny_cifar10(config: LLViTConfig | None = None) -> LLViT(no docstring yet)
spectral_norm
Section titled “spectral_norm”Defined in: griffith_logic.regularization.spectral.spectral_norm (function)
def spectral_norm( tables: torch.Tensor, n_inputs: int, c: torch.Tensor | None = None,) -> torch.TensorComputes the spectral norm of the LUT table matrix, specnorm(L) = ‖L·C‖₂.
Arguments
tables— (u, 2^n) LUT tables (real-valued, usually in [−1,1])n_inputs— number of LUT inputs nc— precomputed coefficient matrix (built on the fly when None)
Returns
A scalar tensor (differentiable, so it can be added to the loss as a regularization term)