以外部倒数尺度 rstd 缩放末维特征,再经过可学习 λ 的 tanh 和逐通道增益;输出形状不变。
(2,12,16) / (2,12,1);x 与可广播 rstd;末维 D=16(2,12,16);保留 B、token 数和末维维度17 个总参数;其中 17 个可训练。hidden_size=16、lam=2、learnable_lam=True;Transformer 子层前的幅度控制forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片主模块只执行 λ·rstd·x、tanh 和通道增益。示例先调用 LayerWiseVarApprox 产生 rstd,再传给 BHyT;辅助类没有参数,不计入主模块。 本页使用 hidden_size=16、lam=2、learnable_lam=True,以真实模块 forward_hook 记录子层输入输出,并追踪本文件执行行上的张量形状。
240 def forward(self, hidden_states: torch.Tensor, rstd: torch.Tensor) -> torch.Tensor: 241 tanh_out = torch.tanh(self.lam * rstd * hidden_states)242 if self.training:243 self._last_tanh_out = tanh_out.detach()244 # norm_fold: when gamma is pre-folded into the next Linear, return without weight mul. 245 if getattr(self, "_bhyt_fold_active", False):246 return tanh_out247 return self.weight * tanh_out
令 rstd 与 token 轴对齐,再将映射结果送入宿主注意力或前馈层。
from BHyTBoundedNormalizer import BHyT norm = BHyT(hidden_size=16, learnable_lam=True) y = norm(x, rstd)
Method 动机参考:在残差流中限制非线性映射的幅度,同时让宿主网络管理层间尺度估计,减少重复计算统计量的需求。
| 消融变体 | 结构配置 | 验证目的 |
|---|---|---|
| 基线 | 宿主原归一化 | 比较训练稳定性、速度与任务指标 |
| 固定 λ | learnable_lam=False | 测试缩放学习的作用 |
| 通道增益 | off_gain=True / False | 比较通道幅度适配能力 |
rstd;可由 LayerWiseVarApprox 或宿主尺度策略产生。