Skip to content

BinaryBrain Compatibility

Defined in: griffith_logic.binarize.Binarize (class)

class Binarize(
threshold: float = 0.5,
hardtanh_min: float = 0.0,
hardtanh_max: float = 1.0,
)

Binarization layer (HardTanh STE).

The activation function used inside BinaryBrain’s DifferentiableLut. During training the gradient is approximated with an STE (Straight-Through Estimator); at inference it is a pure threshold binarization.

Arguments

  • threshold — binarization threshold (default 0.5)
  • hardtanh_min — lower bound of the HardTanh gradient (default 0.0)
  • hardtanh_max — upper bound of the HardTanh gradient (default 1.0)

Defined in: griffith_logic.encoding.modulation.RealToBinary (class)

class RealToBinary(frame_modulation_size: int = 15)

Binary modulation: binarizes a real-valued input at multiple thresholds, increasing the number of frames along the batch axis.

A faithful reproduction of BinaryBrain’s RealToBinary. Thresholds: (f + 0.5) / frame_modulation_size (f = 0, 1, …, frame_modulation_size-1)

No parameters. No gradient flows through it (it is preprocessing).

Arguments

  • frame_modulation_size — number of modulation frames (default 15)

Examples using this API

Defined in: griffith_logic.encoding.modulation.BinaryToReal (class)

class BinaryToReal(frame_modulation_size: int = 15)

Frame accumulation: averages the modulated frames back into real values.

A faithful reproduction of BinaryBrain’s BinaryToReal. Implemented with reshape + mean, so gradients flow automatically through PyTorch autograd.

Arguments

  • frame_modulation_size — number of modulation frames (use the same value as RealToBinary)

Examples using this API

Defined in: griffith_logic.differentiable_lut_block.DifferentiableLutBlock (class)

class DifferentiableLutBlock(
in_dim: int,
out_dim: int,
n_inputs: int = 6,
device: str = 'cpu',
connections: str = 'random',
unbinarize_bias: float = 0.25,
bn_gamma: float = 0.3,
bn_beta: float = 0.5,
)

A composite layer equivalent to BinaryBrain’s DifferentiableLut.

Internal structure (a faithful reproduction of the Forward computation in DifferentiableLutN.h): 1. Unbinarize: {0,1} → {0.25, 0.75} (input remap) 2. LutLayer(raw): N-input LUT (weights held directly in [0,1]) 3. BatchNorm1d: initialized with gamma=0.3, beta=0.5 4. Binarize: threshold at 0.5, HardTanh [0, 1] on the backward pass

Arguments

  • in_dim — number of input dimensions
  • out_dim — number of output neurons
  • n_inputs — number of LUT inputs (default 6)
  • device — compute device
  • connections — connection scheme (‘random’ or ‘unique’)
  • unbinarize_bias — bias value of the input remap (default 0.25)
  • bn_gamma — initial gamma of the BatchNorm (default 0.3)
  • bn_beta — initial beta of the BatchNorm (default 0.5)

Examples using this API

Defined in: griffith_logic.bb_conv2d.BinaryBrainConv2d (class)

class BinaryBrainConv2d(
lut_sizes: list[int],
in_channels: int | None = None,
kernel_size: int = 3,
padding: str | int = 'same',
n_inputs: int = 6,
device: str = 'cpu',
unbinarize_bias: float = 0.25,
batch_norm: bool = True,
use_checkpoint: bool = True,
)

A faithful reproduction of BinaryBrain’s Convolution2d.

Extract patches with im2col → chain of DifferentiableLutBlocks → back to space with col2im.

BinaryBrain’s Convolution2d has three stages: 1. ConvolutionIm2Col: (B, C, H, W) → patches (B, CkHkW, L) 2. sub_layer: a chain of DifferentiableLutBlocks (each position processed independently) 3. ConvolutionCol2Im: patches → (B, out_ch, H_out, W_out)

Arguments

  • lut_sizes — list of output sizes for the DifferentiableLutBlocks (e.g. [192, 32]). The last element becomes the number of output channels
  • in_channels — number of input channels (the equivalent of set_input_shape; determined automatically at forward time when None)
  • kernel_size — kernel size (default 3)
  • padding — padding scheme (‘same’ or int)
  • n_inputs — number of LUT inputs (default 6)
  • device — compute device
  • unbinarize_bias — unbinarize bias of the DifferentiableLutBlocks
  • batch_norm — whether to enable BatchNorm (sometimes set False in the final layer)