betterwithage's picture
lambda-gate pure nn.Module layers
c93e9be verified
Raw
History Blame Contribute Delete
1.9 kB
# SPDX-License-Identifier: Apache-2.0
# 漏 2026 SZL Holdings 路 Stephen P. Lutar 路 ORCID 0009-0001-0110-4173
"""Hub-compliant kernel layer for the szl-lambda-gate kernel.
Per the Kernel Hub `kernel-requirements`, layers exposed for extension must be
PURE torch.nn.Module subclasses:
- no custom __init__,
- no class variables,
- only a `forward` method.
The layer therefore reads its parameters (weights / threshold) off the module
instance it is bound to (set by the host model) and only defines `forward`.
HONESTY: `LambdaGate` emits an ADVISORY governance signal (the weighted
geometric mean 螞 plus a pass/fail vs threshold). 螞 is NOT proven trust; its
uniqueness is Conjecture 1 (open).
"""
import torch
from torch import nn
from ._lambda import lambda_aggregate, lambda_gate
class LambdaGate(nn.Module):
"""Pure 螞-gate layer.
Reads optional ``self.weights`` (1-D, length k) and ``self.threshold``
(float, default 0.5) off the bound module instance.
forward(axes) -> LambdaGateResult(score, passed, threshold, advisory) where
``score`` = 螞(axes) over the last dim and ``passed`` = score >= threshold.
Differentiable in ``score`` w.r.t. ``axes``.
"""
def forward(self, axes: torch.Tensor):
weights = getattr(self, "weights", None)
threshold = getattr(self, "threshold", 0.5)
return lambda_gate(axes, weights=weights, threshold=float(threshold))
class LambdaAggregate(nn.Module):
"""Pure 螞-aggregator layer: forward(axes) -> 螞(axes) tensor in [0,1].
Reads optional ``self.weights`` (1-D, length k) off the bound module
instance; uniform weights when absent. Returns just the score (no gate),
fully differentiable w.r.t. ``axes``.
"""
def forward(self, axes: torch.Tensor) -> torch.Tensor:
weights = getattr(self, "weights", None)
return lambda_aggregate(axes, weights=weights)