在保留 token 的九邻域索引上执行九次 gather 与矩阵乘累加;负索引代表零贡献。CPU 参考实现保持 token 数,输出通道由 4 变为 5。
特征 2×7×4;邻居索引 2×7×9输出 2×7×5185 个参数 · Cin=4,Cout=5,bias=True,局部索引;裁剪 token 的空间卷积。CPU 实跑,按 sum(p.numel() for p in model.parameters()) 统计。forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片在保留 token 的九邻域索引上执行九次 gather 与矩阵乘累加;负索引代表零贡献。CPU 参考实现保持 token 数,输出通道由 4 变为 5。
83 def forward(self,x,neighbor_indices): 86 batched=x.ndim==387 if batched: 88 batch,n,channels=x.shape89 neighbor_indices=neighbor_indices.reshape(-1,9) 90 if not self.global_indices and neighbor_indices.numel()>0 and neighbor_indices.max()<n:91 offsets=torch.arange(batch,device=neighbor_indices.device).repeat_interleave(n)[:,None]*n 92 neighbor_indices=torch.where(neighbor_indices>=0,neighbor_indices+offsets,neighbor_indices)93 x=x.reshape(-1,channels) 38def sparse_reference(x, nbr, weight, bias):39 x_pad = torch.cat([x.float(), x.new_zeros(1, x.shape[1]).float()]) 40 idx = torch.where(nbr >= 0, nbr, x.shape[0]).long()
x 为 float32 的 2×7×4,neighbor_indices 为 long 的 2×7×9;先用文件内 build_spatial_neighbor_table 将保留点转换为局部邻域索引。
from SVTADSparseConv2d import SparseConv2d conv = SparseConv2d(4, 5) y = conv(x, neighbor_indices)
Method 动机参考:让裁剪后的视觉 token 仍可沿原图邻域聚合,避免丢失局部空间结构。
| 消融变体 | 结构配置 | 验证目的 |
|---|---|---|
| 逐点线性 | 只用中心权重 | 观察邻域上下文的作用 |
| 稠密卷积 | 补零网格后 Conv2d 并取回保留点 | 比较数值和实际运行成本 |
| 稀疏参考 | 九邻域 gather-matmul | 比较任务指标与本机延迟 |