-
-
Notifications
You must be signed in to change notification settings - Fork 20.3k
Feature/vit attention unification# 23880 #23978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
74e23b9
d1ed17d
495fe4a
624bcda
54160c5
5e5c80c
25f4ea4
3537659
8fc3cd6
ad3d9c3
8398155
b13f5ba
f3a28c2
0491cd5
39b4002
3fd3c9f
b4bb47b
f6a81d5
aa0c158
bb1b8c8
8bfd32e
588cc8b
b4c4037
0e7cc3c
e49b7df
9eb1db3
4b80a17
0067c04
b877af8
cdfa3bd
a43a02a
f6d9e7b
80fac65
c861dba
3315273
88ddf98
3373156
46912e3
3ecdb62
e5fee4e
2b7ece5
dcb457b
ad360e5
86f1b87
01a5410
d03c4da
77f7654
8f16d02
e3a2bab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,9 @@ | |
| RowParallelLinear) | ||
| from vllm.model_executor.layers.quantization import QuantizationConfig | ||
| from vllm.model_executor.model_loader.weight_utils import default_weight_loader | ||
| from vllm.attention.layer import MultiHeadAttention | ||
| from vllm.model_executor.sampling_metadata import SamplingMetadata | ||
|
|
||
| from vllm.multimodal import MULTIMODAL_REGISTRY, MultiModalKwargsItems | ||
| from vllm.multimodal.inputs import (MultiModalDataDict, MultiModalFieldConfig, | ||
| NestedTensors) | ||
|
|
@@ -1079,6 +1081,10 @@ def __init__( | |
| quant_config=quant_config, | ||
| prefix=f"{prefix}.o_proj", | ||
| ) | ||
|
|
||
| # Use unified MultiHeadAttention with automatic backend selection | ||
| self.attn = MultiHeadAttention( | ||
| self.n_heads, self.head_dim, 1.0 / math.sqrt(self.head_dim)) | ||
|
|
||
| def forward( | ||
| self, | ||
|
|
@@ -1098,19 +1104,13 @@ def forward( | |
| cos, sin = position_embeddings | ||
| q, k = apply_rotary_pos_emb(q, k, cos, sin, unsqueeze_dim=0) | ||
|
|
||
| if USE_XFORMERS_OPS: | ||
| # Transpose q and k back for attention | ||
| q = q.transpose(1, 2).contiguous() | ||
| k = k.transpose(1, 2).contiguous() | ||
| out = xops.memory_efficient_attention(q, | ||
| k, | ||
| v, | ||
| attn_bias=attention_mask) | ||
| else: | ||
| v = v.transpose(1, 2) | ||
| out = nn.functional.scaled_dot_product_attention( | ||
| q, k, v, attn_mask=attention_mask) | ||
| out = out.transpose(1, 2) | ||
|
Comment on lines
-1101
to
-1113
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HF format Pixtral generates gibberish with these changes because of missing attention mask. Let's leave it to be handled together with Qwen2-VL-style ViTs in following PR. |
||
| # Reshape for MultiHeadAttention: (batch, seq, hidden_size) | ||
| q_reshaped = q.transpose(1, 2).contiguous().view(batch, patches, -1) | ||
| k_reshaped = k.transpose(1, 2).contiguous().view(batch, patches, -1) | ||
| v_reshaped = v.transpose(1, 2).contiguous().view(batch, patches, -1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems there is no need to transpose |
||
|
|
||
| # Use unified MultiHeadAttention with automatic backend selection | ||
| out = self.attn(q_reshaped, k_reshaped, v_reshaped) | ||
|
|
||
| out = out.view(batch, patches, self.n_heads * self.head_dim) | ||
|
Isotr0py marked this conversation as resolved.
|
||
| attn_output, _ = self.o_proj(out) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.