hf_model.py 6.61 KB
Newer Older
1
2
3
4
from fastllm_pytools import llm;
import torch;
import ctypes;
import numpy as np;
zhouxiang's avatar
zhouxiang committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

fastllm_data_type_dict = {
    "int4": 8,
    "int8": 3,
    "float16": 7
}
fastllm_weight_type_dict = {
    "linear": 1,
    "embedding": 2,
    "QuantizedLinear": 111
}

def create(model,
           tokenizer = None,
           pre_prompt = None,
           user_role = None,
           bot_role = None,
           history_sep = None,
           dtype = "float16"):
    if (dtype not in fastllm_data_type_dict):
25
26
        print("dtype should in ", list(fastllm_data_type_dict.keys()));
        exit(0);
zhouxiang's avatar
zhouxiang committed
27
28
29
30
31
32

    # 0.1 model info
    modelInfo = model.config.__dict__
    if model.generation_config is not None:
        modelInfo.update(model.generation_config.__dict__)
    if (pre_prompt):
33
        modelInfo["pre_prompt"] = pre_prompt;
zhouxiang's avatar
zhouxiang committed
34
    if (user_role):
35
        modelInfo["user_role"] = user_role;
zhouxiang's avatar
zhouxiang committed
36
    if (bot_role):
37
        modelInfo["bot_role"] = bot_role;
zhouxiang's avatar
zhouxiang committed
38
    if (history_sep):
39
        modelInfo["history_sep"] = history_sep;
zhouxiang's avatar
zhouxiang committed
40
41
    if (modelInfo["model_type"] == "baichuan" and hasattr(model, "model") and hasattr(model.model, "get_alibi_mask")):
        # Baichuan 2代
42
43
44
45
46
        modelInfo["use_alibi"] = "1";
        modelInfo["pre_prompt"] = "";
        modelInfo["user_role"] = ("<FLM_FIX_TOKEN_" + str(model.generation_config.user_token_id) + "> ") if hasattr(model.generation_config, "user_token_id") else "";
        modelInfo["bot_role"] = ("<FLM_FIX_TOKEN_" + str(model.generation_config.assistant_token_id) + ">") if hasattr(model.generation_config, "assistant_token_id") else "";
        modelInfo["history_sep"] = "";
zhouxiang's avatar
zhouxiang committed
47
48
49
50
51
52
    if (modelInfo["model_type"] == "qwen"):
        if modelInfo["chat_format"] == "chatml":
            modelInfo["im_end_id"] = tokenizer.im_end_id
            modelInfo["im_start_id"] = tokenizer.im_start_id


53
54
55
    weight_type_dict = {};
    module_dict = {};
    weight_bits = {};
zhouxiang's avatar
zhouxiang committed
56
57
    for key, m in model.named_modules():
        if (str(type(m)).find("QuantizedLinear") != -1):
58
59
            weight_type_dict[key + ".weight"] = "QuantizedLinear";
            weight_bits[key + ".weight"] = m.weight_bit_width;
zhouxiang's avatar
zhouxiang committed
60
        if (isinstance(m, torch.nn.Linear)):
61
62
            weight_type_dict[key + ".weight"] = "linear";
            module_dict[key + ".weight"] = m;
zhouxiang's avatar
zhouxiang committed
63
        if (isinstance(m, torch.nn.Embedding)):
64
            weight_type_dict[key] = "embedding";
zhouxiang's avatar
zhouxiang committed
65
66
67
68
69

    peft_config = {}
    active_adapter = ""
    if hasattr(model, "peft_config"):
        peft_config = model.peft_config
70
71
    if hasattr(model, "active_adapter") and isinstance(model.active_adapter, str):
        # in transformers >= 4.33.0, active_adapter is a funtion in model, ignore it now
zhouxiang's avatar
zhouxiang committed
72
73
        active_adapter = model.active_adapter

74
75
76
77
    model = model.cpu();
    dict = model.state_dict();
    model_type = model.config.__dict__["model_type"];
    model = llm.fastllm_lib.create_empty_llm_model(model_type.encode());
zhouxiang's avatar
zhouxiang committed
78
    for it in modelInfo.keys():
79
        llm.fastllm_lib.add_dict_llm_model(model, str(it).encode(), str(modelInfo[it]).encode());
zhouxiang's avatar
zhouxiang committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93

    for adapter_name in peft_config.keys():
        adapter_dict = peft_config[adapter_name].__dict__
        for it in adapter_dict.keys():
            llm.fastllm_lib.add_adapter_dict_llm_model(model, str(adapter_name).encode(), str(it).encode(), str(adapter_dict[it]).encode())
    if len(active_adapter) != 0:
        llm.fastllm_lib.set_adapter(model, str(active_adapter).encode())

    # 1. vocab
    if (tokenizer):
        if (hasattr(tokenizer, "tokenizer")):
            if modelInfo["model_type"] == "qwen":
                pass
            else:
94
                tokenizer = tokenizer.tokenizer;
zhouxiang's avatar
zhouxiang committed
95
        if (hasattr(tokenizer, "sp_model")):
96
            piece_size = tokenizer.sp_model.piece_size();
zhouxiang's avatar
zhouxiang committed
97
98
            for i in range(piece_size):
                llm.fastllm_lib.add_tokenizer_word_llm_model(model, tokenizer.sp_model.id_to_piece(i).encode(),
99
                                                             i, ctypes.c_float(tokenizer.sp_model.get_score(i)));
zhouxiang's avatar
zhouxiang committed
100
        else:
101
            vocab = tokenizer.get_vocab();
zhouxiang's avatar
zhouxiang committed
102
103
            for v in vocab.keys():
                if (modelInfo["model_type"] == "moss"):
104
105
                    vv = [(ord(c) if c not in tokenizer.byte_decoder else tokenizer.byte_decoder[c]) for c in v];
                    llm.fastllm_lib.add_tokenizer_word_llm_model(model, vv, vocab[v], ctypes.c_float(1.0));
zhouxiang's avatar
zhouxiang committed
106
                elif (modelInfo["model_type"] == "qwen"):
107
                    llm.fastllm_lib.add_tokenizer_word_llm_model(model, v, vocab[v], ctypes.c_float(1.0));
zhouxiang's avatar
zhouxiang committed
108
                else:
109
110
                    llm.fastllm_lib.add_tokenizer_word_llm_model(model, v.encode(), vocab[v], ctypes.c_float(1.0));
    tot = 0;
zhouxiang's avatar
zhouxiang committed
111
    for key in dict:
112
113
114
        ori_data_type = 0;
        ori_np_data_type = np.float32;
        cur_weight_type = 0;
zhouxiang's avatar
zhouxiang committed
115
        if (key in weight_type_dict and weight_type_dict[key] in fastllm_weight_type_dict):
116
117
            cur_weight_type = fastllm_weight_type_dict[weight_type_dict[key]];
        to_data_type = 0;
zhouxiang's avatar
zhouxiang committed
118
119

        if (cur_weight_type == 1):
120
            to_data_type = fastllm_data_type_dict[dtype];
zhouxiang's avatar
zhouxiang committed
121
            if (to_data_type == 7):
122
123
                ori_data_type = 7;
                ori_np_data_type = np.float16;
zhouxiang's avatar
zhouxiang committed
124
125
        elif (cur_weight_type == 2):
            # TODO bfloat
126
            to_data_type = 0;
zhouxiang's avatar
zhouxiang committed
127
128
129
130
131
132
133
134
135
136

        weight_name = key
        if peft_config is not None:
            weight_name = weight_name.replace('base_model.model.', '')
        if (cur_weight_type == 111):
            llm.fastllm_lib.add_qlinear_weight_llm_model(model, weight_name.encode(),
                                                 len(dict[key].shape),
                                                 (ctypes.c_int * len(dict[key].shape))(*list(dict[key].shape)),
                                                 weight_bits[key],
                                                 dict[key + "_scale"].numpy().astype(np.float32).ctypes.data_as(ctypes.c_void_p),
137
                                                 dict[key].numpy().ctypes.data_as(ctypes.c_void_p));
zhouxiang's avatar
zhouxiang committed
138
139
140
141
142
        else:
            llm.fastllm_lib.add_weight_llm_model(model, weight_name.encode(),
                                             len(dict[key].shape),
                                             (ctypes.c_int * len(dict[key].shape))(*list(dict[key].shape)),
                                             to_data_type, cur_weight_type, ori_data_type,
143
144
145
                                             dict[key].numpy().astype(ori_np_data_type).ctypes.data_as(ctypes.c_void_p));
        tot += 1;
        print("convert (", tot, "/", len(dict), end = " )\r");
zhouxiang's avatar
zhouxiang committed
146

147
148
149
150
151
    print("");
    llm.fastllm_lib.init_params_llm_model(model);
    llm.fastllm_lib.warmup_llm_model(model);
    ret = llm.model("", id = model);
    return ret;
zhouxiang's avatar
zhouxiang committed
152