Fix tensor shape mismatch in AAttn when dim is not divisible by num_heads - #173
Open
ZoomZoneZero wants to merge 1 commit into
Open
Fix tensor shape mismatch in AAttn when dim is not divisible by num_heads#173ZoomZoneZero wants to merge 1 commit into
ZoomZoneZero wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
I noticed that the
AAttnclass attempts to handle cases wheredimis not divisible bynum_headsby defining theall_head_dimvariable. 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:ValueError): Inself.pe, the module is initialized within_channels=all_head_dimbutgroups=dim. This triggersValueError: in_channels must be divisible by groups.RuntimeError): In the forward pass,xcannot be properly reshaped because it forces the use of the originalC(dim) instead of the internally processed channel size.x + ppresults in a tensor withdimchannels, but the projection layerself.projis initialized to accept an input ofall_head_dimchannels. Passing it toself.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 tohead_dim * num_heads) for intermediate operations.diminput, fulfilling the original intent of definingall_head_dimand 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)