|
| 1 | +from .base import BaseAWQForCausalLM |
| 2 | +from typing_extensions import TYPE_CHECKING |
| 3 | + |
| 4 | +if TYPE_CHECKING: |
| 5 | + from transformers.models.qwen2_5_omni.modeling_qwen2_5_omni import ( |
| 6 | + Qwen2_5OmniDecoderLayer |
| 7 | + ) |
| 8 | + from transformers import Qwen2_5OmniForConditionalGeneration |
| 9 | + |
| 10 | + |
| 11 | +class Qwen2_5_OmniAWQForConditionalGeneration(BaseAWQForCausalLM): |
| 12 | + layer_type = "Qwen2_5OmniDecoderLayer" |
| 13 | + max_seq_len_key = "max_position_embeddings" |
| 14 | + modules_to_not_convert = ["visual"] |
| 15 | + @staticmethod |
| 16 | + def get_model_layers(model: "Qwen2_5OmniForConditionalGeneration"): |
| 17 | + return model.thinker.model.layers |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def get_act_for_scaling(module: "Qwen2_5OmniForConditionalGeneration"): |
| 21 | + return dict(is_scalable=False) |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def move_embed(model: "Qwen2_5OmniForConditionalGeneration", device: str): |
| 25 | + model.thinker.model.embed_tokens = model.thinker.model.embed_tokens.to(device) |
| 26 | + model.thinker.visual = model.thinker.visual.to(device) |
| 27 | + model.thinker.audio_tower = model.thinker.audio_tower.to(device) |
| 28 | + |
| 29 | + model.thinker.visual.rotary_pos_emb = model.thinker.visual.rotary_pos_emb.to(device) |
| 30 | + model.thinker.model.rotary_emb = model.thinker.model.rotary_emb.to(device) |
| 31 | + |
| 32 | + for layer in model.thinker.model.layers: |
| 33 | + layer.self_attn.rotary_emb = layer.self_attn.rotary_emb.to(device) |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def get_layers_for_scaling( |
| 37 | + module: "Qwen2_5OmniDecoderLayer", input_feat, module_kwargs |
| 38 | + ): |
| 39 | + layers = [] |
| 40 | + |
| 41 | + # attention input |
| 42 | + layers.append( |
| 43 | + dict( |
| 44 | + prev_op=module.input_layernorm, |
| 45 | + layers=[ |
| 46 | + module.self_attn.q_proj, |
| 47 | + module.self_attn.k_proj, |
| 48 | + module.self_attn.v_proj, |
| 49 | + ], |
| 50 | + inp=input_feat["self_attn.q_proj"], |
| 51 | + module2inspect=module.self_attn, |
| 52 | + kwargs=module_kwargs, |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + # attention out |
| 57 | + # Please refer to https://github.com/mit-han-lab/llm-awq/pull/67#issue-1850622696 |
| 58 | + if module.self_attn.v_proj.weight.shape == module.self_attn.o_proj.weight.shape: |
| 59 | + layers.append( |
| 60 | + dict( |
| 61 | + prev_op=module.self_attn.v_proj, |
| 62 | + layers=[module.self_attn.o_proj], |
| 63 | + inp=input_feat["self_attn.o_proj"], |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + # linear 1 |
| 68 | + layers.append( |
| 69 | + dict( |
| 70 | + prev_op=module.post_attention_layernorm, |
| 71 | + layers=[module.mlp.gate_proj, module.mlp.up_proj], |
| 72 | + inp=input_feat["mlp.gate_proj"], |
| 73 | + module2inspect=module.mlp, |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + # linear 2 |
| 78 | + layers.append( |
| 79 | + dict( |
| 80 | + prev_op=module.mlp.up_proj, |
| 81 | + layers=[module.mlp.down_proj], |
| 82 | + inp=input_feat["mlp.down_proj"], |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + return layers |
0 commit comments