← 返回模块库ECCV 2026卷积与局部细化 · 批次索引02_convolution/SVTADSparseConv2d.py

SVTADSparseConv2dSV-TAD: Native Sparse Convolutions for Efficient Temporal Action Detection

在保留 token 的九邻域索引上执行九次 gather 与矩阵乘累加;负索引代表零贡献。CPU 参考实现保持 token 数,输出通道由 4 变为 5。

来源:本地源码所列 SV-TAD sparse_reference;MIT;参数布局为 (9,Cin,Cout),使用 torch autograd。
输入形状 · 实跑配置
特征 2×7×4;邻居索引 2×7×9
输出形状 · 真实返回
输出 2×7×5
参数量与适用范围
185 个参数 · Cin=4,Cout=5,bias=True,局部索引;裁剪 token 的空间卷积。CPU 实跑,按 sum(p.numel() for p in model.parameters()) 统计。
① 批次索引 → ② 空邻居归零③ 九路线性聚合权重 9×4×5偏置 5保留 tokens3×4 网格,各留 7 点2×7×4索引批次偏移邻域输入 2×7×914×9追加零 token负索引转末行15×4九路 gather @ W每路权重 4×514×5还原批次输出通道为 52×7×5
按 forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片
批次索引空邻居处理九路累加

真实源码 · CPU 实跑SVTADSparseConv2d 数据流

特征 2×7×4邻居索引 2×7×9输出 2×7×5

这一步内部怎么算

在保留 token 的九邻域索引上执行九次 gather 与矩阵乘累加;负索引代表零贡献。CPU 参考实现保持 token 数,输出通道由 4 变为 5。

作者为什么这样设计

让裁剪后的视觉 token 仍可沿原图邻域聚合,避免丢失局部空间结构。

对应真实源码切片

SVTADSparseConv2d.py · L38–L93
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()

工程替换位点(3 行接入)

裁剪 token 的空间卷积

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 动机与消融设计

可直接参考写作

Method 动机参考:让裁剪后的视觉 token 仍可沿原图邻域聚合,避免丢失局部空间结构。

消融变体结构配置验证目的
逐点线性只用中心权重观察邻域上下文的作用
稠密卷积补零网格后 Conv2d 并取回保留点比较数值和实际运行成本
稀疏参考九邻域 gather-matmul比较任务指标与本机延迟

常见 QA 专题

点击问题展开详解
Q1输入必须 float32 吗?+
本文件显式要求输入和权重 float32;参考聚合也以 FP32 计算。
Q2邻域表中的 -1 表示什么?+
该位置没有保留邻居,内部映射到额外的零 token。
Q3这能展示原生稀疏 CUDA 加速吗?+
这是官方测试参考式的 PyTorch 版本,图示不代表 CUDA、TensorRT 内核吞吐。
SVTADSparseConv2d (ECCV 2026) · 源码级交互架构拆解