File size: 2,539 Bytes
d64ebfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Works with transformer==4.35.2
"""

import sys
from dataclasses import dataclass
from pathlib import Path
from unittest.mock import patch


from transformers import HfArgumentParser


@dataclass
class Args:
    model_id: str = 'huggyllama/llama-7b'
    w_bit: int = 8
    q_group_size: int = 128
    dump_awq: str = None

    def __post_init__(self):
        if self.dump_awq is not None:
            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'
        Path(self.dump_awq).parent.mkdir(parents=True, exist_ok=True)


def generate_awq_models(args: Args):
    with patch.object(
        sys, 'argv',
        [
            'awq.entry',
            '--model_path', args.model_id,
            '--w_bit', str(args.w_bit),
            '--q_group_size', str(args.q_group_size),
            '--run_awq',
            '--dump_awq', str(args.dump_awq),
        ]
    ):
        from awq.entry import args as awq_args
        from awq.entry import main as awq_main
        print(awq_args)
        awq_main()


def _infer_awq_config(string):
    string = str(string)
    w_bit = None
    if '-w4asym-' in string:
        w_bit = 4
    elif '-w8asym' in string:
        w_bit = 8
    q_group_size = None
    if '-g128' in string:
        q_group_size = 128
    assert None not in [w_bit, q_group_size]
    return [
        '--w_bit', str(w_bit),
        '--q_group_size', str(q_group_size)
    ]


def apply_awq_to_model(model_id, awq_meta_path, output_folder, auto_dispatch: bool):
    extra_cmd_list = _infer_awq_config(str(awq_meta_path))
    if not auto_dispatch:
        extra_cmd_list.append('--no_auto_dispatch')
    with patch.object(
        sys, 'argv',
        [
            'awq.entry',
            '--model_path', model_id,
            '--load_awq', str(awq_meta_path),
            '--q_backend', 'fake',
            '--output_folder', str(output_folder),
            *extra_cmd_list,
        ]
    ):
        from awq.entry import args as awq_args
        from awq.entry import build_model_and_enc
        print(awq_args)
        model, _ = build_model_and_enc(model_id)
        return model


class FakeAWQModel:
    @classmethod
    def from_pretrained(cls, model_id: str, awq_meta_path: str, output_folder: str, auto_dispatch: bool = True):
        return apply_awq_to_model(model_id, awq_meta_path, output_folder, auto_dispatch)


if __name__ == '__main__':
    args = HfArgumentParser(Args).parse_args_into_dataclasses()[0]
    generate_awq_models(args)