|
""" |
|
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) |
|
|