Views
No views yet
| model | precision | wikitext ppl (↓) |
|---|---|---|
| meta-llama/Meta-Llama-3-8B | FP16 | 9.179 |
| yujiepan/Meta-Llama-3-8B-awq-w4g64 | w4g64 | 9.219 |
| yujiepan/Meta-Llama-3-8B-awq-w4g64-v2 | w4g64, skip last layer's FFN | 9.278 |
model = AutoModelForCausalLM.from_pretrained('<MODEL_ID>', torch_dtype=torch.float16)1from unittest.mock import patch
2
3import torch
4
5from awq import AutoAWQForCausalLM
6from awq.models.llama import LlamaAWQForCausalLM
7from transformers import AutoTokenizer
8
9module2fullname = {}
10
11
12def exclude_layers_to_not_quantize(linear_layers, modules_to_not_convert):
13 if modules_to_not_convert is None:
14 return linear_layers
15 filtered_layers = {}
16 for name, linear_layer in linear_layers.items():
17 full_name = module2fullname[linear_layer]
18 if not any(key in full_name for key in modules_to_not_convert):
19 filtered_layers[name] = linear_layer
20 else:
21 print('Skipping', full_name)
22 return filtered_layers
23
24
25class PatchedLlamaAWQForCausalLM(LlamaAWQForCausalLM):
26 @staticmethod
27 def get_layers_for_scaling(module, input_feat, module_kwargs):
28 print(input_feat.keys())
29 layers = []
30 # attention input
31 if 'self_attn.q_proj' in input_feat:
32 layers.append(
33 dict(
34 prev_op=module.input_layernorm,
35 layers=[
36 module.self_attn.q_proj,
37 module.self_attn.k_proj,
38 module.self_attn.v_proj,
39 ],
40 inp=input_feat["self_attn.q_proj"],
41 module2inspect=module.self_attn,
42 kwargs=module_kwargs,
43 )
44 )
45 # attention out
46 # Please refer to https://github.com/mit-han-lab/llm-awq/pull/67#issue-1850622696
47 if 'self_attn.o_proj' in input_feat:
48 if module.self_attn.v_proj.weight.shape == module.self_attn.o_proj.weight.shape:
49 layers.append(
50 dict(
51 prev_op=module.self_attn.v_proj,
52 layers=[module.self_attn.o_proj],
53 inp=input_feat["self_attn.o_proj"],
54 )
55 )
56
57 if 'mlp.gate_proj' in input_feat:
58 # linear 1
59 layers.append(
60 dict(
61 prev_op=module.post_attention_layernorm,
62 layers=[module.mlp.gate_proj, module.mlp.up_proj],
63 inp=input_feat["mlp.gate_proj"],
64 module2inspect=module.mlp,
65 )
66 )
67
68 if 'mlp.down_proj' in input_feat:
69 # linear 2
70 layers.append(
71 dict(
72 prev_op=module.mlp.up_proj,
73 layers=[module.mlp.down_proj],
74 inp=input_feat["mlp.down_proj"],
75 )
76 )
77 return layers
78
79
80quant_config = {
81 "zero_point": True, "q_group_size": 64, "w_bit": 4, "version": "GEMM",
82 "modules_to_not_convert": [
83 'layers.31.mlp',
84 ],
85}
86with patch('awq.quantize.quantizer.exclude_layers_to_not_quantize', exclude_layers_to_not_quantize):
87 model_path = "meta-llama/Meta-Llama-3-8B"
88 # model_path = 'yujiepan/meta-llama-3-tiny-random'
89 model = PatchedLlamaAWQForCausalLM.from_pretrained(model_path, model_type='llama', device_map='cuda')
90 tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
91 module2fullname = {module: name for name, module in model.named_modules()}
92 model.quantize(tokenizer, quant_config=quant_config)