-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Casper <casperbh.96@gmail.com>
- Loading branch information
1 parent
12c91b7
commit 76bc0a8
Showing
6 changed files
with
627 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from .base import BaseAWQForCausalLM | ||
from typing_extensions import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from transformers import Qwen2VLForConditionalGeneration | ||
from transformers.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLDecoderLayer | ||
|
||
class Qwen2VLAWQForCausalLM(BaseAWQForCausalLM): | ||
layer_type = "Qwen2VLDecoderLayer" | ||
max_seq_len_key = "max_position_embeddings" | ||
modules_to_not_convert = ["visual"] | ||
|
||
@staticmethod | ||
def get_model_layers(model: "Qwen2VLForConditionalGeneration"): | ||
return model.model.layers | ||
|
||
@staticmethod | ||
def get_act_for_scaling(module: "Qwen2VLForConditionalGeneration"): | ||
return dict(is_scalable=False) | ||
|
||
@staticmethod | ||
def move_embed(model: "Qwen2VLForConditionalGeneration", device: str): | ||
model.model.embed_tokens = model.model.embed_tokens.to(device) | ||
model.visual = model.visual.to(device) | ||
|
||
@staticmethod | ||
def get_layers_for_scaling(module: "Qwen2VLDecoderLayer", input_feat, module_kwargs): | ||
layers = [] | ||
|
||
# attention input | ||
layers.append( | ||
dict( | ||
prev_op=module.input_layernorm, | ||
layers=[ | ||
module.self_attn.q_proj, | ||
module.self_attn.k_proj, | ||
module.self_attn.v_proj, | ||
], | ||
inp=input_feat["self_attn.q_proj"], | ||
module2inspect=module.self_attn, | ||
kwargs=module_kwargs, | ||
) | ||
) | ||
|
||
# attention out | ||
# Please refer to https://github.com/mit-han-lab/llm-awq/pull/67#issue-1850622696 | ||
if module.self_attn.v_proj.weight.shape == module.self_attn.o_proj.weight.shape: | ||
layers.append( | ||
dict( | ||
prev_op=module.self_attn.v_proj, | ||
layers=[module.self_attn.o_proj], | ||
inp=input_feat["self_attn.o_proj"], | ||
) | ||
) | ||
|
||
# linear 1 | ||
layers.append( | ||
dict( | ||
prev_op=module.post_attention_layernorm, | ||
layers=[module.mlp.gate_proj, module.mlp.up_proj], | ||
inp=input_feat["mlp.gate_proj"], | ||
module2inspect=module.mlp, | ||
) | ||
) | ||
|
||
# linear 2 | ||
layers.append( | ||
dict( | ||
prev_op=module.mlp.up_proj, | ||
layers=[module.mlp.down_proj], | ||
inp=input_feat["mlp.down_proj"], | ||
) | ||
) | ||
|
||
return layers |
Oops, something went wrong.