窗口注意力叠加 V 的局部卷积位置响应和全图均值,在局部相关与全局平均信息之间建立补充通路。
[(2,96,32)](2,96,32)forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片tokens 分别投影成 QK 和 V,准备窗口内交互与局部位置支路。;将 8×12 网格分为 4×4 窗口,在每头八维 QK 上施加 RoPE。;缩放点积 softmax 后加权窗口内 V,再逆重排成原 token 顺序。;重建 V 的二维图,叠加深度卷积 LePE 与全图 V 的均值。
437 scores = torch.einsum("blhe,bshe->bhls", q, k)438 scores = self.dropout(torch.softmax(scale * scores, dim=-1))439 scores = torch.einsum("bhls,bshd->blhd", scores, v)441 # putting attention back to its original image shape for later use 442 scores = scores.view(b,443 self.input_resolution[0]//self.window_size,444 self.input_resolution[1]//self.window_size,445 self.window_size, 446 self.window_size,447 num_heads, 448 head_dim).permute(0, 1, 3, 2, 4, 5, 6).contiguous() 449 scores = scores.reshape(b, n, c)
将 SEMALocalAverageAttention 接入对应数据接口。以下代码使用本页实跑配置;输入变量需由上游提供。
from SEMALocalAverageAttention import SEMAAttention model = SEMAAttention(dim=32, input_resolution=(8,12), num_heads=4, window_size=4) y = model(x, input_resolution=(8,12))
Method 动机参考:窗口注意力叠加 V 的局部卷积位置响应和全图均值,在局部相关与全局平均信息之间建立补充通路。
| 消融变体 | 结构配置 | 验证目的 |
|---|---|---|
| 局部注意力 | 仅窗口注意力支路 | 在实验副本中比较窗口建模 |
| 加局部位置 | 窗口注意力 + LePE | 测量细节指标变化 |
| 完整 SEMA | 再加全图 V 均值 | 比较全局统计补充作用,结果待训练 |