KromHC以 Kronecker 双随机矩阵混合多条残差流;pre 门读取到宿主分支,post 门再将分支输出分配回各流。示例使用四流与两层 MLP。
多流输入 8×5×8多流输出 8×5×8711 个参数 · S=4,D=8;含 8→16→8 宿主 MLP;Transformer 多流残差连接。CPU 实跑,按 sum(p.numel() for p in model.parameters()) 统计。forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片KromHC以 Kronecker 双随机矩阵混合多条残差流;pre 门读取到宿主分支,post 门再将分支输出分配回各流。示例使用四流与两层 MLP。
195 def width_connection(self, residuals): 196 streams = self.num_residual_streams197 maybe_transformed_residuals = self.residual_transform(residuals) 200 residuals = self.split_fracs(residuals)201 residuals = rearrange(residuals, '(b s) ... d -> b ... s d', s=streams) 202 normed = rearrange(residuals, 'b ... s d -> b ... (s d)', s=streams)203 normed = self.norm(normed) 208 combined_weight = normed @ fused_weights177 combined_coeffs = self.residual_scale * dynamic_coeffs + static_coeffs 179 coeffs_reshaped = combined_coeffs.view(*batch_shape, self.num_factors, 2)180 weights = F.softmax(coeffs_reshaped, dim=-1) 181 p = weights[..., 0]
branch 是 Linear(8,16)、GELU、Linear(16,8);residuals 为 8×5×8,代表 B=2、S=4。宿主负责在网络入口展开流并在出口汇聚。
from KromHCKroneckerConnection import KromHC connection = KromHC(4, dim=8, branch=branch, layer_index=0) y = connection(residuals)
Method 动机参考:用结构化连接矩阵调节跨流信息传播,在分支变换之外提供可学习残差路由。
| 消融变体 | 结构配置 | 验证目的 |
|---|---|---|
| 单流残差 | 相同宿主 MLP | 比较基础容量与训练成本 |
| 静态连接 | 固定动态连接贡献为零 | 隔离输入条件的作用 |
| 完整四流 | 动态 pre/post 与 Kronecker Hres | 比较精度、参数与显存 |