|
| 1 | +import tqdm |
| 2 | +from typing import List, Tuple |
| 3 | +from .base import BaseAWQForCausalLM |
| 4 | + |
| 5 | + |
| 6 | +class DeepseekV3AWQForCausalLM(BaseAWQForCausalLM): |
| 7 | + layer_type = "DeepseekV3DecoderLayer" |
| 8 | + max_seq_len_key = "max_position_embeddings" |
| 9 | + |
| 10 | + @staticmethod |
| 11 | + def get_model_layers(model): |
| 12 | + return model.model.layers |
| 13 | + |
| 14 | + @staticmethod |
| 15 | + def get_act_for_scaling(module): |
| 16 | + return dict(is_scalable=False) |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def move_embed(model, device: str): |
| 20 | + model.model.embed_tokens = model.model.embed_tokens.to(device) |
| 21 | + |
| 22 | + @staticmethod |
| 23 | + def get_layers_for_scaling( |
| 24 | + module, input_feat, module_kwargs |
| 25 | + ): |
| 26 | + layers = [] |
| 27 | + |
| 28 | + if hasattr(module.self_attn, "q_proj"): |
| 29 | + # attention input |
| 30 | + layers.append( |
| 31 | + dict( |
| 32 | + prev_op=module.input_layernorm, |
| 33 | + layers=[ |
| 34 | + module.self_attn.q_proj, |
| 35 | + module.self_attn.kv_a_proj_with_mqa, |
| 36 | + ], |
| 37 | + inp=input_feat["self_attn.q_proj"], |
| 38 | + module2inspect=module.self_attn, |
| 39 | + kwargs=module_kwargs, |
| 40 | + ) |
| 41 | + ) |
| 42 | + else: |
| 43 | + # attention input |
| 44 | + layers.append( |
| 45 | + dict( |
| 46 | + prev_op=module.input_layernorm, |
| 47 | + layers=[ |
| 48 | + module.self_attn.q_a_proj, |
| 49 | + module.self_attn.kv_a_proj_with_mqa, |
| 50 | + ], |
| 51 | + inp=input_feat["self_attn.q_a_proj"], |
| 52 | + module2inspect=module.self_attn, |
| 53 | + kwargs=module_kwargs, |
| 54 | + ) |
| 55 | + ) |
| 56 | + layers.append( |
| 57 | + dict( |
| 58 | + prev_op=module.self_attn.q_a_layernorm, |
| 59 | + layers=[ |
| 60 | + module.self_attn.q_b_proj, |
| 61 | + ], |
| 62 | + inp=input_feat["self_attn.q_b_proj"], |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + # kv layernorm |
| 67 | + layers.append( |
| 68 | + dict( |
| 69 | + prev_op=module.self_attn.kv_a_layernorm, |
| 70 | + layers=[ |
| 71 | + module.self_attn.kv_b_proj, |
| 72 | + ], |
| 73 | + inp=input_feat["self_attn.kv_b_proj"], |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + if hasattr(module.mlp, "gate"): |
| 78 | + # linear in |
| 79 | + layers.append( |
| 80 | + dict( |
| 81 | + prev_op=module.post_attention_layernorm, |
| 82 | + layers=[ |
| 83 | + w |
| 84 | + for expert in module.mlp.experts |
| 85 | + for w in [expert.gate_proj, expert.up_proj] |
| 86 | + ] + [module.mlp.shared_experts.gate_proj, module.mlp.shared_experts.up_proj], |
| 87 | + inp=input_feat["mlp"], |
| 88 | + module2inspect=module.mlp, |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + # linear out |
| 93 | + for i, expert in enumerate(module.mlp.experts): |
| 94 | + layers.append( |
| 95 | + dict( |
| 96 | + prev_op=expert.up_proj, |
| 97 | + layers=[expert.down_proj], |
| 98 | + inp=input_feat[f"mlp.experts.{i}.down_proj"], |
| 99 | + ) |
| 100 | + ) |
| 101 | + layers.append( |
| 102 | + dict( |
| 103 | + prev_op=module.mlp.shared_experts.up_proj, |
| 104 | + layers=[module.mlp.shared_experts.down_proj], |
| 105 | + inp=input_feat[f"mlp.shared_experts.down_proj"], |
| 106 | + ) |
| 107 | + ) |
| 108 | + else: |
| 109 | + # linear 1 |
| 110 | + layers.append( |
| 111 | + dict( |
| 112 | + prev_op=module.post_attention_layernorm, |
| 113 | + layers=[module.mlp.gate_proj, module.mlp.up_proj], |
| 114 | + inp=input_feat["mlp.gate_proj"], |
| 115 | + module2inspect=module.mlp, |
| 116 | + ) |
| 117 | + ) |
| 118 | + |
| 119 | + # linear 2 |
| 120 | + layers.append( |
| 121 | + dict( |
| 122 | + prev_op=module.mlp.up_proj, |
| 123 | + layers=[module.mlp.down_proj], |
| 124 | + inp=input_feat["mlp.down_proj"], |
| 125 | + ) |
| 126 | + ) |
| 127 | + |
| 128 | + return layers |
0 commit comments