diff --git a/OpenCOOD/opencood/models/fuse_modules/self_attn.py b/OpenCOOD/opencood/models/fuse_modules/self_attn.py index 7280dc50..2133ed93 100644 --- a/OpenCOOD/opencood/models/fuse_modules/self_attn.py +++ b/OpenCOOD/opencood/models/fuse_modules/self_attn.py @@ -32,6 +32,11 @@ def __init__(self, dim): self.sqrt_dim = np.sqrt(dim) def forward(self, query, key, value): + # Single-element attention is an identity operation. Skip backend kernel + # dispatch for this degenerate case (for example, Triton on CUDA). + if query.size(1) == 1 and key.size(1) == 1 and value.size(1) == 1: + return value + score = torch.bmm(query, key.transpose(1, 2)) / self.sqrt_dim attn = F.softmax(score, -1) context = torch.bmm(attn, value)