Create do_awq.py
Browse files
do_awq.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Works with transformer==4.35.2
|
3 |
+
"""
|
4 |
+
|
5 |
+
import sys
|
6 |
+
from dataclasses import dataclass
|
7 |
+
from pathlib import Path
|
8 |
+
from unittest.mock import patch
|
9 |
+
|
10 |
+
|
11 |
+
from transformers import HfArgumentParser
|
12 |
+
|
13 |
+
|
14 |
+
@dataclass
|
15 |
+
class Args:
|
16 |
+
model_id: str = 'huggyllama/llama-7b'
|
17 |
+
w_bit: int = 8
|
18 |
+
q_group_size: int = 128
|
19 |
+
dump_awq: str = None
|
20 |
+
|
21 |
+
def __post_init__(self):
|
22 |
+
if self.dump_awq is not None:
|
23 |
+
self.dump_awq = f'./logs/evals/{self.model_id}/awq-w{self.w_bit}asym-g{self.q_group_size}/generate-awq-meta/awq-meta.pt'
|
24 |
+
Path(self.dump_awq).parent.mkdir(parents=True, exist_ok=True)
|
25 |
+
|
26 |
+
|
27 |
+
def generate_awq_models(args: Args):
|
28 |
+
with patch.object(
|
29 |
+
sys, 'argv',
|
30 |
+
[
|
31 |
+
'awq.entry',
|
32 |
+
'--model_path', args.model_id,
|
33 |
+
'--w_bit', str(args.w_bit),
|
34 |
+
'--q_group_size', str(args.q_group_size),
|
35 |
+
'--run_awq',
|
36 |
+
'--dump_awq', str(args.dump_awq),
|
37 |
+
]
|
38 |
+
):
|
39 |
+
from awq.entry import args as awq_args
|
40 |
+
from awq.entry import main as awq_main
|
41 |
+
print(awq_args)
|
42 |
+
awq_main()
|
43 |
+
|
44 |
+
|
45 |
+
def _infer_awq_config(string):
|
46 |
+
string = str(string)
|
47 |
+
w_bit = None
|
48 |
+
if '-w4asym-' in string:
|
49 |
+
w_bit = 4
|
50 |
+
elif '-w8asym' in string:
|
51 |
+
w_bit = 8
|
52 |
+
q_group_size = None
|
53 |
+
if '-g128' in string:
|
54 |
+
q_group_size = 128
|
55 |
+
assert None not in [w_bit, q_group_size]
|
56 |
+
return [
|
57 |
+
'--w_bit', str(w_bit),
|
58 |
+
'--q_group_size', str(q_group_size)
|
59 |
+
]
|
60 |
+
|
61 |
+
|
62 |
+
def apply_awq_to_model(model_id, awq_meta_path, output_folder, auto_dispatch: bool):
|
63 |
+
extra_cmd_list = _infer_awq_config(str(awq_meta_path))
|
64 |
+
if not auto_dispatch:
|
65 |
+
extra_cmd_list.append('--no_auto_dispatch')
|
66 |
+
with patch.object(
|
67 |
+
sys, 'argv',
|
68 |
+
[
|
69 |
+
'awq.entry',
|
70 |
+
'--model_path', model_id,
|
71 |
+
'--load_awq', str(awq_meta_path),
|
72 |
+
'--q_backend', 'fake',
|
73 |
+
'--output_folder', str(output_folder),
|
74 |
+
*extra_cmd_list,
|
75 |
+
]
|
76 |
+
):
|
77 |
+
from awq.entry import args as awq_args
|
78 |
+
from awq.entry import build_model_and_enc
|
79 |
+
print(awq_args)
|
80 |
+
model, _ = build_model_and_enc(model_id)
|
81 |
+
return model
|
82 |
+
|
83 |
+
|
84 |
+
class FakeAWQModel:
|
85 |
+
@classmethod
|
86 |
+
def from_pretrained(cls, model_id: str, awq_meta_path: str, output_folder: str, auto_dispatch: bool = True):
|
87 |
+
return apply_awq_to_model(model_id, awq_meta_path, output_folder, auto_dispatch)
|
88 |
+
|
89 |
+
|
90 |
+
if __name__ == '__main__':
|
91 |
+
args = HfArgumentParser(Args).parse_args_into_dataclasses()[0]
|
92 |
+
generate_awq_models(args)
|