[LLM:Bugfix] Fix head_dim auto-detection for gated attention models (Qwen3.5)

Discussed-in: Merge-Request 26812010 , URL: https://code.alibaba-inc.com/AliNN/AliNNPrivate/codereview/26812010
GitOrigin-RevId: 3ea07dc659d9f59e800160d13281e635119ce2bc
This commit is contained in:
MNNSyncBot
2026-04-10 17:54:43 +08:00
parent ba76938ec0
commit 2625b8aed4
2 changed files with 6 additions and 12 deletions
+2 -12
View File
@@ -8,17 +8,7 @@ if ! command -v git-clang-format &> /dev/null; then
exit 1
fi
# Collect staged files with matching extensions, excluding generated headers
files=$(git diff --cached --name-only --diff-filter=ACM \
| grep -E '\.(cpp|c|h|hpp|cc|m|mm)$' \
| grep -v 'MNN_generated\.h$' \
|| true)
if [ -z "$files" ]; then
exit 0
fi
output=$(git clang-format --diff --staged --extensions cpp,c,h,hpp,cc,m,mm -- $files 2>&1)
output=$(git clang-format --diff --staged --extensions cpp,c,h,hpp,cc,m,mm 2>&1)
if [ "$output" = "no modified files to format" ] || \
[ "$output" = "clang-format did not modify any files" ]; then
@@ -31,7 +21,7 @@ if echo "$output" | grep -q "^diff"; then
echo "$output"
echo ""
echo "To fix, run:"
echo " git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|c|h|hpp|cc|m|mm)$' | grep -v 'MNN_generated\.h$' | xargs git clang-format --staged --"
echo " git clang-format --staged --extensions cpp,c,h,hpp,cc,m,mm"
echo " git add -u"
exit 1
fi
@@ -102,6 +102,10 @@ class Attention(torch.nn.Module):
# where head_dim varies per layer type: sliding=256, full=512)
if hasattr(self, 'q_proj') and self.q_proj is not None and not (hasattr(self, 'qkv_proj') and self.qkv_proj is not None):
actual_head_dim = self.q_proj.out_features // self.num_heads
# If q_norm exists, use its weight size as authoritative head_dim
# (q_proj may output 2x head_dim for gated attention, e.g. Qwen3.5)
if hasattr(self, 'q_norm') and self.q_norm is not None and hasattr(self.q_norm, 'weight'):
actual_head_dim = self.q_norm.weight.shape[0]
if actual_head_dim != self.head_dim:
self.head_dim = actual_head_dim
# Re-detect num_key_value_heads with new head_dim