From 3fe923dc7991f384ce2cf3020131a25632f0e01e Mon Sep 17 00:00:00 2001 From: Grigoriy Khoroshilov Date: Tue, 1 Sep 2026 14:34:11 +0300 Subject: [PATCH 1/2] [OpenCOOD] Bypass self-attention for single-CAV inputs --- OpenCOOD/opencood/models/fuse_modules/self_attn.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/OpenCOOD/opencood/models/fuse_modules/self_attn.py b/OpenCOOD/opencood/models/fuse_modules/self_attn.py index 7280dc50..73285183 100644 --- a/OpenCOOD/opencood/models/fuse_modules/self_attn.py +++ b/OpenCOOD/opencood/models/fuse_modules/self_attn.py @@ -32,6 +32,13 @@ def __init__(self, dim): self.sqrt_dim = np.sqrt(dim) def forward(self, query, key, value): + # With a single CAV, attention contains one element whose softmax is + # exactly one, so the result is the input value. Avoid dispatching the + # degenerate batched matrix multiplications to a backend-specific JIT + # kernel (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) From 7149fe8f0e2334d0ef2f889431bfdd509f6318a0 Mon Sep 17 00:00:00 2001 From: Grigoriy Khoroshilov Date: Tue, 1 Sep 2026 14:41:03 +0300 Subject: [PATCH 2/2] Shorten the comment --- OpenCOOD/opencood/models/fuse_modules/self_attn.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/OpenCOOD/opencood/models/fuse_modules/self_attn.py b/OpenCOOD/opencood/models/fuse_modules/self_attn.py index 73285183..2133ed93 100644 --- a/OpenCOOD/opencood/models/fuse_modules/self_attn.py +++ b/OpenCOOD/opencood/models/fuse_modules/self_attn.py @@ -32,10 +32,8 @@ def __init__(self, dim): self.sqrt_dim = np.sqrt(dim) def forward(self, query, key, value): - # With a single CAV, attention contains one element whose softmax is - # exactly one, so the result is the input value. Avoid dispatching the - # degenerate batched matrix multiplications to a backend-specific JIT - # kernel (for example Triton on CUDA). + # 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