auto.py 1.69 KB
Newer Older
1
from transformers import AutoConfig
2
from awq.models import *
3
from awq.models.base import BaseAWQForCausalLM
4
5
6

AWQ_CAUSAL_LM_MODEL_MAP = {
    "mpt": MptAWQForCausalLM,
Casper Hansen's avatar
Casper Hansen committed
7
    "llama": LlamaAWQForCausalLM,
Casper Hansen's avatar
Casper Hansen committed
8
9
    "opt": OptAWQForCausalLM,
    "RefinedWeb": FalconAWQForCausalLM,
Casper Hansen's avatar
Casper Hansen committed
10
11
    "RefinedWebModel": FalconAWQForCausalLM,
    "bloom": BloomAWQForCausalLM
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
}

def check_and_get_model_type(model_dir, trust_remote_code=True):
    config = AutoConfig.from_pretrained(model_dir, trust_remote_code=trust_remote_code)
    if config.model_type not in AWQ_CAUSAL_LM_MODEL_MAP.keys():
        raise TypeError(f"{config.model_type} isn't supported yet.")
    model_type = config.model_type
    return model_type

class AutoAWQForCausalLM:
    def __init__(self):
        raise EnvironmentError('You must instantiate AutoAWQForCausalLM with\n'
                               'AutoAWQForCausalLM.from_quantized or AutoAWQForCausalLM.from_pretrained')
    
    @classmethod
Casper Hansen's avatar
Casper Hansen committed
27
    def from_pretrained(self, model_path, trust_remote_code=True, safetensors=False) -> BaseAWQForCausalLM:
28
29
30
        model_type = check_and_get_model_type(model_path, trust_remote_code)

        return AWQ_CAUSAL_LM_MODEL_MAP[model_type].from_pretrained(
Casper Hansen's avatar
Casper Hansen committed
31
            model_path, model_type, trust_remote_code=trust_remote_code, safetensors=safetensors
32
        )
33
34

    @classmethod
35
    def from_quantized(self, quant_path, quant_filename, 
36
                       device='balanced', trust_remote_code=True) -> BaseAWQForCausalLM:
37
        model_type = check_and_get_model_type(quant_path, trust_remote_code)
38

39
        return AWQ_CAUSAL_LM_MODEL_MAP[model_type].from_quantized(
40
            quant_path, model_type, quant_filename, device, trust_remote_code=trust_remote_code
41
        )