ハイブリッドAPI
LogicLinear
Section titled “LogicLinear”実体: griffith_logic.layers.logic_linear.LogicLinear(クラス)
class LogicLinear(in_features: int, out_features: int, spec: LogicSpec | None = None)nn.Linear互換の論理ゲート型レイヤ。
引数
in_features— 入力特徴数out_features— 出力特徴数spec— 生成レシピ(省略時はLogicSpec()のデフォルト)
LogicMLP
Section titled “LogicMLP”実体: griffith_logic.layers.logic_linear.LogicMLP(クラス)
class LogicMLP( in_features: int, out_features: int | None = None, spec: LogicSpec | None = None,)Transformer FFN(Linear→活性化→Linear)全体の置換体。
FFNを2つのLogicLinearで置き換えるよりLUTスタック1本の方が 効率的なため、独立クラスとして提供する。out_features省略時は in_featuresと同じ(d_model→d_modelのFFN置換)。
LogicSpec
Section titled “LogicSpec”実体: griffith_logic.hybrid.policy.LogicSpec(クラス)
class LogicSpec( n_inputs: int = 2, parameterization: str = 'softmax', selection: str = 'soft', depth: int = 2, width: int | None = None, neurons_per_output: int = 8, encoder: str = 'sigmoid', encoder_tau: float = 1.0, encoder_bits: int = 8, decoder: str = 'groupsum_centered', decoder_tau: float | None = None, connections: str = 'random', grad_factor: float = 1.0,)論理ゲート型レイヤの生成レシピ。
属性
n_inputs— LUTの入力数(2/4/6)parameterization— LutLayerのパラメータ化方式 + ‘dwn’(DWNLayer使用)selection— ‘soft’ | ‘hard_st’(CAGE方式。dwnでは無視)depth— LUT層の段数width— 中間層の幅(None → out_features × neurons_per_output)neurons_per_output— GroupSum前の出力あたり冗長ニューロン数encoder— 入力境界 ‘sigmoid’ | ‘binarize_ste’ | ‘identity’ | ‘thermometer’encoder_tau— sigmoidエンコーダの初期温度encoder_bits— thermometerエンコーダの特徴あたりビット数decoder— 出力境界 ‘groupsum_centered’ | ‘groupsum’ | ‘layernorm’ | ‘identity’decoder_tau— デコーダ温度(None → √グループサイズ)connections— ‘random’ | ‘unique’(dwnでは第1層learnable/以降random)grad_factor— LUT層の勾配スケール(深い構成では>1。dwnでは無視)
このAPIを使っている例
- 2. ハイブリッドNNを作る —
examples/02_hybrid_llvit.py
ToLogic
Section titled “ToLogic”実体: griffith_logic.encoding.bridges.ToLogic(クラス)
class ToLogic(mode: str = 'sigmoid', tau_init: float = 1.0, bits: int = 8)実数 → [0,1] 変換。
mode: ‘sigmoid’ — sigmoid(x/τ)。τは学習可能(飽和による勾配消失を緩和) ‘binarize_ste’ — 閾値0で二値化、backward はHardTanh STE ‘identity’ — 変換なし(入力が既に[0,1]のとき) ‘thermometer’ — Thermometer符号化(D → D×bits に拡張、STE)。 しきい値は初回訓練バッチで自動fit(uniform)
補足
thermometerは出力幅が変わる。後段の幅は out_width() で取得すること。
FromLogic
Section titled “FromLogic”実体: griffith_logic.encoding.bridges.FromLogic(クラス)
class FromLogic( in_width: int, out_features: int, mode: str = 'groupsum_centered', tau: float | None = None,)[0,1]論理出力 → 実数変換(グループ集約)。
in_width個の論理出力を out_features グループに分けて合計し、 ゼロ中心化+スケーリングして実数表現に戻す。
mode: ‘groupsum_centered’ — (Σ - G/2)/τ。residual接続でも平均が暴れない(推奨) ‘groupsum’ — Σ/τ(従来のGroupSumと同じ、非負) ‘layernorm’ — groupsum_centered + LayerNorm ‘identity’ — 変換なし(in_width == out_features 必須)
τのデフォルトは √G(G=グループサイズ)。二値出力の合計の標準偏差が √G/2 のため、出力がO(1)に正規化される。
replace_modules
Section titled “replace_modules”実体: griffith_logic.hybrid.replace.replace_modules(関数)
def replace_modules( model: nn.Module, match: MatchSpec, factory: Callable[[str, nn.Module], nn.Module], inplace: bool = False,) -> tuple[nn.Module, list[str]]matchに一致するサブモジュールをfactoryの生成物で置換する。
引数
match— 型 / 型タプル / 述語(name, module)->bool / 名前glob文字列factory— (name, 旧モジュール) -> 新モジュールinplace— Falseならdeepcopyしてから置換(元モデル保全)
戻り値
(置換後モデル, 置換したモジュール名リスト)
補足
一致したモジュールの内部はそれ以上走査しない (親子が両方一致する場合は親を優先)。
convert_linear_to_logic
Section titled “convert_linear_to_logic”実体: griffith_logic.hybrid.replace.convert_linear_to_logic(関数)
def convert_linear_to_logic( model: nn.Module, spec: LogicSpec | None = None, include: str | None = None, exclude: str | None = None, inplace: bool = False,) -> tuple[nn.Module, list[str]]nn.Linear を LogicLinear に置換する。
引数
spec— 論理ゲート化レシピinclude— この名前globに一致するLinearだけ置換(例: ‘.mlp.’)exclude— この名前globに一致するLinearは除外(例: ‘head’)
replace_mlp_with_lut
Section titled “replace_mlp_with_lut”実体: griffith_logic.hybrid.replace.replace_mlp_with_lut(関数)
def replace_mlp_with_lut( model: nn.Module, spec: LogicSpec | None = None, mlp_class: type | None = None, mlp_name_pattern: str = '*mlp*', inplace: bool = False,) -> tuple[nn.Module, list[str]]FFN/MLPブロック全体を LogicMLP に置換する(LL-ViT用途)。
Attention・LayerNorm・埋め込みはPyTorchのまま残し、MLP部分だけを LUTベースの論理ゲート型に置き換える。
検出条件(いずれか): - mlp_class 指定時: そのクラスのインスタンス - 名前の末尾要素が mlp_name_pattern に一致し、内部にnn.Linearを 2つ以上含むモジュール
in/out次元は内部の最初/最後のnn.Linearから自動推定する。
このAPIを使っている例
- 2. ハイブリッドNNを作る —
examples/02_hybrid_llvit.py
split_param_groups
Section titled “split_param_groups”実体: griffith_logic.hybrid.replace.split_param_groups(関数)
def split_param_groups( model: nn.Module, logic_lr: float | None = None, ann_lr: float | None = None,) -> list[dict]LUT系パラメータとANN系パラメータをoptimizer param groupに分割する。
論理ゲート層(lr=0.01前後)と実数層(lr=1e-3前後)で適切な学習率が 1桁異なるため、分離して個別指定できるようにする。
戻り値
torch.optim.*(param_groups, …) にそのまま渡せるリスト
このAPIを使っている例
- 2. ハイブリッドNNを作る —
examples/02_hybrid_llvit.py