コンテンツにスキップ

DWN / LLViT

実体: griffith_logic.encoding.thermometer.ThermometerEncoder(クラス)

class ThermometerEncoder(
bits: int,
mode: str = 'uniform',
input_bits: int | None = None,
)

Thermometer符号化層。

引数

  • bits — 特徴あたりのビット数 z(しきい値の個数)
  • mode — ‘uniform’(等間隔) | ‘distributive’(経験分位点)
  • input_bits — IR 化(export_ir)するときの入力の量子化ビット数 B。 None(既定)では境界(IR の外)のまま。指定すると抽出器が特徴ごとの B ビット符号 c(LSB 先頭、位置 d·B + j)を入力とし、実数 q = c / (2^B − 1) (ToTensor の uint8 → [0, 1] と同じ)としてしきい値比較を B 入力 lut_layer の 真理値表に焼く。torch の forward は従来どおり実数 q を受ける

属性

  • thresholds — (bits, n_features) しきい値(fit後に確定)

このAPIを使っている例

実体: griffith_logic.layers.dwn.DWNLayer(クラス)

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',
)

DWNのLUT層: 二値入力 → n入力LUT×n_luts → 二値出力。

引数

  • in_bits — 入力ビット数
  • n_luts — LUT(RAMノード)数 = 出力ビット数
  • n_inputs — LUTあたりの入力数 n
  • mapping — ‘random’(固定ランダム接続) | ‘learnable’(Learnable Mapping)
  • efd — EFD係数 ‘paper’(1/(H+1)) | ‘official’(α·β^H) alpha, beta: efd=‘official’の係数(None時は公式既定値)
  • ste — 出力を{0,1}に二値化する(公式既定値True)
  • clamp_luts — 訓練時に毎forwardでテーブルを[−1,1]へclamp(公式既定値True)
  • lm_tau — Learnable Mappingのsoftmax温度

このAPIを使っている例

実体: griffith_logic.layers.dwn.LearnableMapping(クラス)

class LearnableMapping(
in_features: int,
out_features: int,
tau: float = _DEFAULT_LM_TAU,
)

入力→LUTの接続を勾配で学習する層。

訓練中は重み行列 W ∈ ℝ^{in×out} を保持し、forwardは既定で x_{argmax(W[:,i])} のハード選択(推論時のオーバーヘッドなし)。

漸進配線ハード化(SparseLUT arXiv 2503.12829の2フェーズを適応): forward_mode=‘soft’ では x @ softmax(W/tau_soft, dim=0) のsoft混合 forwardになる(純autograd、選択の不連続ジャンプ無し)。 ProgressiveWiringSchedulerがtau_softを幾何減衰させ、訓練後半で forward_mode=‘hard’(従来のargmax+STE)へ切り替える。 hard選択の配線学習が崩壊したことへの対策レバー。 evalは常にhard argmax(forward_modeは訓練時のみ参照)。

引数

  • in_features — 入力ビット数 P
  • out_features — 出力ビット数 Q(= LUT数 × LUT入力数)
  • tau — backwardのsoftmax温度(公式既定値 0.001)

実体: griffith_logic.models.dwn.DWNClassifier(クラス)

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',
)

Thermometer符号化 + DWN多層LUT + popcount集約の分類器。

引数

  • in_features — 実数値入力の特徴数 D
  • n_classes — クラス数 k
  • layer_widths — 各DWNLayerのLUT数(最終層はn_classesで割り切れること)
  • bits — Thermometer符号化のビット数/特徴
  • encoder_mode — ‘uniform’ | ‘distributive’(distributiveはfit_encoder必須)
  • n_inputs — LUTあたりの入力数 n
  • mapping — 第1層の接続方式 ‘random’ | ‘learnable’(第2層以降はrandom固定)
  • efd — EFD係数 ‘paper’(1/(H+1)) | ‘official’(α·β^H)
  • tau — GroupSumの温度(公式既定値 1/0.3)

このAPIを使っている例

実体: griffith_logic.models.llvit.LLViTConfig(クラス)

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.

実体: griffith_logic.models.llvit.LLViT(クラス)

class LLViT(config: LLViTConfig, channel_mixer: str = 'llvit')

Tiny ViT backbone used for baseline and LL-ViT reproduction.

実体: griffith_logic.models.llvit.LLViTChannelMixer(クラス)

class LLViTChannelMixer(config: LLViTConfig)

LL-ViT channel mixer: thermometer -> DWN -> DWN -> conditional sum.

実体: griffith_logic.models.llvit.ConditionalSum(クラス)

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.

実体: griffith_logic.models.llvit.build_llvit_cifar10(関数)

def build_llvit_cifar10(config: LLViTConfig | None = None) -> LLViT

(docstring未整備)

実体: griffith_logic.models.llvit.build_deit_tiny_cifar10(関数)

def build_deit_tiny_cifar10(config: LLViTConfig | None = None) -> LLViT

(docstring未整備)

実体: griffith_logic.regularization.spectral.spectral_norm(関数)

def spectral_norm(
tables: torch.Tensor,
n_inputs: int,
c: torch.Tensor | None = None,
) -> torch.Tensor

LUTテーブル行列のスペクトルノルム specnorm(L) = ‖L·C‖₂ を計算する。

引数

  • tables — (u, 2^n) LUTテーブル(実数値、通常は[−1,1])
  • n_inputs — LUT入力数 n
  • c — 事前計算済み係数行列(Noneなら都度構築)

戻り値

スカラーテンソル(微分可能、正則化項として損失に加算できる)