Convolution & Spatial
LutConv2d
Section titled “LutConv2d”Defined in: griffith_logic.conv.LutConv2d (class)
class LutConv2d( in_channels: int, out_channels: int, kernel_size: int | tuple[int, int], tree_depth: int = 3, stride: int | tuple[int, int] = 1, padding: int | tuple[int, int] = 0, n_inputs: int = 2, device: str = 'cpu', grad_factor: float = 1.0, residual_init: bool = True, input_channels_per_tree: int = 2, parameterization: str = 'softmax', wiring: str = 'fixed', lm_tau: float = 0.001, sampler: str = 'random', layer_idx: int = 0, estimator: str = 'sigmoid', ri_mu: float = 1.2, ri_sigma: float = 0.25, generator: torch.Generator | None = None,)Logic-gate tree convolution layer (CDDLGN Section 3, Eq. 2-3).
Each kernel is a complete tree of N-input LUTs. A tree of depth d randomly picks N^d leaf inputs from the receptive field and applies LUT operations tree-wise to produce one output.
The same gate weights are shared across every spatial position (the convolution property). By default the leaf positions inside the receptive field are a fixed random wiring (wiring=‘fixed’), but with wiring=‘learnable’ they can be learned by gradient descent through the Learnable Mapping of the DWN paper (Section 3.2); both the wiring and the gate weights stay shared across every spatial position.
CDDLGN equation (Eq. 3): A’[k, i, j] = tree_k(A[C_M[k,:], C_H[k,:]+i, C_W[k,:]+j])
Examples using this API
- 5. Export a CNN to HDL —
examples/05_cnn_export_hdl.py - Turning a whole diffusion model into a Boolean circuit —
examples/17_logic_diffusion.py
ConvDLGNConfig
Section titled “ConvDLGNConfig”Defined in: griffith_logic.models.conv_dlgn.ConvDLGNConfig (class)
class ConvDLGNConfig( in_shape: tuple[int, int, int], channels: tuple[int, ...] = (32, 128, 512, 1024), sampler: str = 'random8', kernel_size: int = 3, tree_depth: int = 3, padding: int = 1, input_channels_per_tree: int = 2, downsample: str = 'orpool', head_dims: tuple[int, ...] | None = None, head_wiring: str = 'rot2', head_scale: float = 1.0, num_classes: int = 10, tau: float = 20.0, group_assign: str = 'stride', parameterization: str = 'independent', estimator: str = 'sin', residual_init: bool = True, ri_mu: float = 1.2, ri_sigma: float = 0.25, selection: str = 'soft',)Configuration of a conv-DLGN. The defaults are the cifar10_s setting (cdlgn k=32) from prior internal experiments.
Attributes
in_shape— input (C, H, W). (9, 32, 32) for CIFAR-10 with 3 thresholds (threshold-major)channels— output channels of the conv tree blocks (cdlgn k=32: k, 4k, 16k, 32k)sampler— leaf read coordinates (‘random’ / ‘random8’ / ‘cross5’ / ‘dilated8’ / ‘checker13’) kernel_size / tree_depth / padding: used only when sampler=‘random’ (fixed per arm otherwise)input_channels_per_tree— number of input channels one tree reads (cdlgn num_chn)downsample— ‘orpool’ (stride-1 conv + 2×2 OR pooling) / ‘stride’ (stride-2 conv)head_dims— hidden widths of the head (excluding the flat input). If None, the cdlgn head rule [10·flat, 5·flat, 2.5·flat] × head_scale (the last layer is a multiple of num_classes)head_wiring— ‘rot2’ (rot with a step; therot2_schedulefrom prior internal experiments) / ‘random’ / ‘unique’ num_classes / tau / group_assign: GroupSum parameterization / estimator / residual_init / ri_mu / ri_sigma: shared by the conv trees and the headselection— gate selection of the head LutLayers (‘soft’ / ‘hard_st’ / ‘gumbel_st’)
build_conv_dlgn
Section titled “build_conv_dlgn”Defined in: griffith_logic.models.conv_dlgn.build_conv_dlgn (function)
def build_conv_dlgn( cfg: ConvDLGNConfig, device: str = 'cpu', generator: torch.Generator | None = None,) -> nn.SequentialBuilds a conv-DLGN as an nn.Sequential (a serial structure that can be passed to export_ir as is).
Arguments
cfg— configurationdevice— target devicegenerator— generator used for the random8 leaf coordinates, the sin RI and the head’s random wiring (global RNG if None)
OrPooling2d
Section titled “OrPooling2d”Defined in: griffith_logic.pooling.OrPooling2d (class)
class OrPooling2d(kernel_size: int = 2, stride: int | None = None, padding: int = 0)Logical OR pooling (CDDLGN Section 3.1).
A continuous relaxation of probabilistic logical OR via max pooling. L_max(a, b) = max(a, b) is the maximum t-conorm.
On binary values at inference time it behaves as an exact Boolean OR.
Examples using this API
- 5. Export a CNN to HDL —
examples/05_cnn_export_hdl.py
SpatialGroupSum
Section titled “SpatialGroupSum”Defined in: griffith_logic.spatial_group_sum.SpatialGroupSum (class)
class SpatialGroupSum(n_outputs: int, scale: float = 1.0)Layer that groups channels and sums them while preserving the spatial dimensions.
Summing LUT outputs ([0,1]) over several channels lets the layer express
a regression value in the range [0, group_size].
scale adjusts the final value range.
The spatial counterpart of GroupSum (for 2D feature maps).
UpSample2d
Section titled “UpSample2d”Defined in: griffith_logic.upsample.UpSample2d (class)
class UpSample2d(scale_factor: int = 2)Nearest-neighbor upsampling (equivalent to BinaryBrain’s UpSampling).
As the inverse of MaxPooling, it enlarges the spatial dimensions by scale_factor. Behaves the same as BinaryBrain with fill=True (the default).
Arguments
scale_factor— upscaling factor (default 2)