Skip to content

Fix tensor shape mismatch in AAttn when dim is not divisible by num_heads - #173

Open
ZoomZoneZero wants to merge 1 commit into
sunsmarterjie:mainfrom
ZoomZoneZero:patch-1
Open

Fix tensor shape mismatch in AAttn when dim is not divisible by num_heads#173
ZoomZoneZero wants to merge 1 commit into
sunsmarterjie:mainfrom
ZoomZoneZero:patch-1

Conversation

@ZoomZoneZero

@ZoomZoneZero ZoomZoneZero commented Apr 3, 2026

Copy link
Copy Markdown

Description

I noticed that the AAttn class attempts to handle cases where dim is not divisible by num_heads by defining the all_head_dim variable. However, the current implementation doesn't use it consistently, which leads to crashes in both initialization and forward passes.

Specifically, when dim % num_heads != 0, the original code encounters 3 critical issues:

  1. Initialization Crash (ValueError): In self.pe, the module is initialized with in_channels=all_head_dim but groups=dim. This triggers ValueError: in_channels must be divisible by groups.
2026-04-02 221235
  1. Reshape Crash (RuntimeError): In the forward pass, x cannot be properly reshaped because it forces the use of the original C (dim) instead of the internally processed channel size.
image
  1. Projection Dimension Mismatch: The addition x + pp results in a tensor with dim channels, but the projection layer self.proj is initialized to accept an input of all_head_dim channels. Passing it to self.proj(x) inevitably triggers a dimension mismatch error during the forward pass.

How this PR fixes it

This PR corrects the internal channel logic to consistently use the calculated all_head_dim (which resolves to head_dim * num_heads) for intermediate operations.

  • No structural changes: It does not change the final input/output shapes.
  • Logic preserved: All underlying operations (group convolutions, dimensional transformations) are kept exactly as intended.
  • Robustness: The code now successfully handles ANY dim input, fulfilling the original intent of defining all_head_dim and significantly improving the module's robustness for custom/lightweight model designs.

Note on Reliability

This exact logic fix has been thoroughly tested and already merged into the official Ultralytics repository (PR #24114). I am submitting this PR to help keep the original research codebase robust and synchronized with industry standards.


Minimum Reproducible Example (Before this PR)

import torch

# This will crash immediately before this PR
model = AAttn(dim=18, num_heads=4, area=2)
input_tensor = torch.randn(1, 18, 8, 8)
output = model(input_tensor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant