畳み込み・空間処理
LutConv2d
Section titled “LutConv2d”実体: griffith_logic.conv.LutConv2d(クラス)
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,)論理ゲートツリー畳み込み層(CDDLGN Section 3, Eq. 2-3)。
各カーネルはN入力LUTの完全木で構成される。 木の深さdでN^d個のリーフ入力を受容野からランダムに選択し、 ツリー状にLUT演算を適用して1出力を生成する。
同じゲート重みが全空間位置で共有される(畳み込みの性質)。 リーフの受容野内位置は既定で固定ランダム(wiring=‘fixed’)だが、 wiring=‘learnable’でDWN論文§3.2のLearnable Mappingにより勾配で 学習することもできる(配線もゲート重みも全空間位置で共有)。
CDDLGNの数式(Eq. 3): A’[k, i, j] = tree_k(A[C_M[k,:], C_H[k,:]+i, C_W[k,:]+j])
このAPIを使っている例
- 5. CNNをHDLへエクスポート —
examples/05_cnn_export_hdl.py - 拡散モデルを丸ごとブール回路にする —
examples/17_logic_diffusion.py
ConvDLGNConfig
Section titled “ConvDLGNConfig”実体: griffith_logic.models.conv_dlgn.ConvDLGNConfig(クラス)
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',)conv-DLGN の構成。既定値は先行実験 cifar10_s(cdlgn k=32)。
属性
in_shape— 入力 (C, H, W)。CIFAR-10 3 閾値(閾値 major)なら (9, 32, 32)channels— conv 木ブロックの出力チャネル列(cdlgn k=32: k, 4k, 16k, 32k)sampler— 葉の読み座標(‘random’ / ‘random8’ / ‘cross5’ / ‘dilated8’ / ‘checker13’) kernel_size / tree_depth / padding: sampler=‘random’ のときだけ使う(他は腕ごとに固定)input_channels_per_tree— 木 1 本が読む入力チャネル数(cdlgn num_chn)downsample— ‘orpool’(stride-1 conv + OR プール 2×2)/ ‘stride’(stride-2 conv)head_dims— 頭部の隠れ幅列(入力 flat を含まない)。None なら cdlgn 頭部則 [10·flat, 5·flat, 2.5·flat] × head_scale(最終層は num_classes の倍数)head_wiring— ‘rot2’(歩進付き rot。先行実験rot2_schedule)/ ‘random’ / ‘unique’ num_classes / tau / group_assign: GroupSum parameterization / estimator / residual_init / ri_mu / ri_sigma: conv 木と頭部に共通selection— 頭部 LutLayer のゲート選択(‘soft’ / ‘hard_st’ / ‘gumbel_st’)
build_conv_dlgn
Section titled “build_conv_dlgn”実体: griffith_logic.models.conv_dlgn.build_conv_dlgn(関数)
def build_conv_dlgn( cfg: ConvDLGNConfig, device: str = 'cpu', generator: torch.Generator | None = None,) -> nn.Sequentialconv-DLGN を nn.Sequential として組む(export_ir にそのまま渡せる直列構造)。
引数
cfg— 構成device— 配置先generator— random8 の葉座標・sin の RI・頭部 random 配線に使う generator (None ならグローバル RNG)
OrPooling2d
Section titled “OrPooling2d”実体: griffith_logic.pooling.OrPooling2d(クラス)
class OrPooling2d(kernel_size: int = 2, stride: int | None = None, padding: int = 0)論理ORプーリング(CDDLGN Section 3.1)。
max poolingによる確率的論理ORの連続緩和。 L_max(a, b) = max(a, b) は maximum t-conorm。
推論時のバイナリ値では完全なBoolean OR演算として動作。
このAPIを使っている例
- 5. CNNをHDLへエクスポート —
examples/05_cnn_export_hdl.py
SpatialGroupSum
Section titled “SpatialGroupSum”実体: griffith_logic.spatial_group_sum.SpatialGroupSum(クラス)
class SpatialGroupSum(n_outputs: int, scale: float = 1.0)チャンネルをグループ化して合計し、空間次元を保持する層。
LUT出力([0,1])を複数チャンネル分合計することで、 [0, group_size]の範囲の回帰値を表現する。 scaleで最終的な値域を調整する。
GroupSumの空間版(2D特徴マップ対応)。
UpSample2d
Section titled “UpSample2d”実体: griffith_logic.upsample.UpSample2d(クラス)
class UpSample2d(scale_factor: int = 2)最近傍補間によるアップサンプリング(BinaryBrainのUpSamplingと等価)。
MaxPoolingの逆操作として、空間次元をscale_factor倍に拡大する。 BinaryBrainのfill=True(デフォルト)と同じ動作。
引数
scale_factor— 拡大倍率(デフォルト2)