This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
qwenc.py
288 lines (246 loc) · 10.1 KB
/
qwenc.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import ctypes
import numpy as np
MAX_TOKENS = 10000;
MAX_LEN = 512;
def make2_c_uint64_list(my_list):
return (ctypes.c_uint64 * len(my_list))(*my_list)
def make2_c_int_list(my_list):
return (ctypes.c_int * len(my_list))(*my_list)
def char_point_2_str(char_point):
return ctypes.string_at(char_point).decode('utf-8')
def make_np2c(np_array):
if np_array.flags['CONTIGUOUS'] == False:
# info users
np_array = np.ascontiguousarray(np_array)
return np_array.ctypes.data_as(ctypes.c_void_p)
def str2char_point(string):
return ctypes.c_char_p(string.encode('utf-8'))
class StringVector(ctypes.Structure):
_fields_ = [
('data', ctypes.POINTER(ctypes.c_char_p)),
('size', ctypes.c_size_t),
]
class Slice(ctypes.Structure):
_fields_ = [
('data', ctypes.c_int * 10000),
('size', ctypes.c_size_t),
]
lib = ctypes.cdll.LoadLibrary('./libqwenc.so')
lib.QwenChat_with_device_model.restype = ctypes.c_void_p
lib.QwenChat_with_device_model.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_char_p]
def QwenChat_with_device_model(devid, bmodel_path, tokenizer_path) -> ctypes.c_void_p:
"""
QwenChat* QwenChat_with_device_model(int devid, const char* bmodel_path, const char* tokenizer_path);
:param devid: ctypes.c_int
:param bmodel_path: ctypes.c_char_p
:param tokenizer_path: ctypes.c_char_p
"""
return lib.QwenChat_with_device_model(ctypes.c_int(devid), str2char_point(bmodel_path), str2char_point(tokenizer_path))
lib.QwenChat_with_devices_models.restype = ctypes.c_void_p
lib.QwenChat_with_devices_models.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_char_p, ctypes.c_char_p]
def QwenChat_with_devices_models(devids, device_len, bmodel_path, tokenizer_path) -> ctypes.c_void_p:
"""
QwenChat* QwenChat_with_devices_models(int* devids,int device_len , const char* bmodel_path, const char* tokenizer_path);
:param devids: ctypes.POINTER(ctypes.c_int)
:param device_len: ctypes.c_int
:param bmodel_path: ctypes.c_char_p
:param tokenizer_path: ctypes.c_char_p
"""
return lib.QwenChat_with_devices_models(make2_c_int_list(devids), ctypes.c_int(device_len), str2char_point(bmodel_path), str2char_point(tokenizer_path))
lib.QwenChat_deinit.restype = None
lib.QwenChat_deinit.argtypes = [ctypes.c_void_p]
def QwenChat_deinit(chat) -> None:
"""
void QwenChat_deinit( QwenChat* chat );
:param chat: ctypes.c_void_p
"""
return lib.QwenChat_deinit(chat)
lib.get_history.restype = StringVector
lib.get_history.argtypes = [ctypes.c_void_p]
def get_history(chat) -> StringVector:
"""
StringVector get_history( QwenChat* chat );
:param chat: ctypes.c_void_p
"""
return lib.get_history(chat)
lib.set_history.restype = None
lib.set_history.argtypes = [ctypes.c_void_p, StringVector]
def set_history(chat, history_c) -> None:
"""
void set_history( QwenChat* chat, StringVector history_c );
:param chat: ctypes.c_void_p
:param history_c: StringVector
"""
return lib.set_history(chat, history_c)
lib.predict_first_token_with_str.restype = ctypes.c_int
lib.predict_first_token_with_str.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
def predict_first_token_with_str(chat, input_str) -> ctypes.c_int:
"""
int predict_first_token_with_str( QwenChat* chat, const char* input_str );
:param chat: ctypes.c_void_p
:param input_str: ctypes.c_char_p
"""
return lib.predict_first_token_with_str(chat, str2char_point(input_str))
lib.predict_first_token_with_tokens.restype = ctypes.c_int
lib.predict_first_token_with_tokens.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.c_int]
def predict_first_token_with_tokens(chat, tokens) -> ctypes.c_int:
"""
modified by zwy
int predict_first_token_with_tokens( QwenChat* chat, int* tokens, int token_len ) ;
:param chat: ctypes.c_void_p
:param tokens: ctypes.POINTER(ctypes.c_int)
:param token_len: ctypes.c_int
"""
token_len = len(tokens)
return lib.predict_first_token_with_tokens(chat, make2_c_int_list(tokens), ctypes.c_int(token_len))
lib.predict_next_token.restype = ctypes.c_int
lib.predict_next_token.argtypes = [ctypes.c_void_p]
def predict_next_token(chat) -> ctypes.c_int:
"""
int predict_next_token( QwenChat* chat );
:param chat: ctypes.c_void_p
"""
return lib.predict_next_token(chat)
lib.tokens2str.restype = None
lib.tokens2str.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_char_p]
def tokens2str(chat, tokens):
"""
modifed by zwy
void tokens2str( QwenChat* chat, int* tokens, int token_len, char* str );
:param chat: ctypes.c_void_p
:param tokens: ctypes.POINTER(ctypes.c_int)
:param token_len: ctypes.c_int
:param str: ctypes.c_char_p
"""
token_len = len(tokens)
str = ctypes.create_string_buffer(MAX_TOKENS)
lib.tokens2str(chat, make2_c_int_list(tokens), ctypes.c_int(token_len), str)
return str.value.decode('utf-8', 'ignore')
lib.token2str_with_pre.restype = None
lib.token2str_with_pre.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_char_p]
def token2str_with_pre(chat, token) -> None:
"""
modifed by zwy
void token2str_with_pre( QwenChat* chat, int token, char* str );
:param chat: ctypes.c_void_p
:param token: ctypes.c_int
:param str: ctypes.c_char_p
"""
str = ctypes.create_string_buffer(MAX_TOKENS)
lib.token2str_with_pre(chat, ctypes.c_int(token), str)
return str.value.decode('utf-8', 'ignore')
lib.str2tokens.restype = None
lib.str2tokens.argtypes = [ctypes.c_void_p, ctypes.POINTER(StringVector), ctypes.POINTER(Slice)]
def str2tokens(chat, history_c, slice_data) -> None:
"""
void str2tokens( QwenChat* chat, StringVector* history_c, Slice* slice_data) ;
:param chat: ctypes.c_void_p
:param history_c: ctypes.POINTER(StringVector)
:param slice_data: ctypes.POINTER(Slice)
"""
return lib.str2tokens(chat, ctypes.byref(history_c), ctypes.byref(slice_data))
lib.get_token_length.restype = ctypes.c_int
lib.get_token_length.argtypes = [ctypes.c_void_p]
def get_token_length(chat) -> ctypes.c_int:
"""
int get_token_length( QwenChat* chat );
:param chat: ctypes.c_void_p
"""
return lib.get_token_length(chat)
version='2023-12-18-16-00-15'
lib.get_eos.restype = ctypes.c_int
lib.get_eos.argtypes = [ctypes.c_void_p]
class QwenChat:
def __init__(self, devid=0, bmodel_path="/data/Qwen/qwen-7b_int4_fast.bmodel", tokenizer_path="/data/Qwen/qwen.tiktoken"):
self.chat = QwenChat_with_device_model(devid, bmodel_path, tokenizer_path)
self.history = []
self.history_c = StringVector()
self.tokens = Slice()
self.tokens.size = 0
self.eos = lib.get_eos(self.chat)
self.tokenizer_path = tokenizer_path
self.bmodel_path = bmodel_path
self.cur_token = None
def __del__(self):
pass
@property
def token_len(self):
return get_token_length(self.chat)
def history_to_c(self):
self.history_c.size = len(self.history)
c_strings = [ctypes.c_char_p(string.encode()) for string in self.history]
self.history_c.data = (ctypes.c_char_p * len(c_strings))(*c_strings)
def historyc_to_history(self):
# decoder c string vector
history = []
size = self.history_c.size
data = self.history_c.data
for i in range(size):
history.append(char_point_2_str(data[i]))
return history
def add_history(self, content:str):
pass
def predict_next_token(self):
pass
def handle_token_in_single_conversation(self):
pass
def predict(self, context:str):
# 如果当前的token length 小于多少就得减少历史记录
# 判断当前的context
# 假设都符合
# 是否降低hisoty todo by 路程
self.history.append(context)
self.history_to_c()
str2tokens(self.chat, self.history_c, self.tokens)
first_token = predict_first_token_with_tokens(self.chat, self.tokens.data[:self.tokens.size])
first_str = token2str_with_pre(self.chat, first_token)
print(first_str, end="")
res = first_str
self.cur_token = first_token
while True:
if self.cur_token == self.eos:
break
if self.token_len >= MAX_LEN:
print("......\n Warning: cleanup early history")
break
next_token = predict_next_token(self.chat)
next_str = token2str_with_pre(self.chat, next_token)
print(next_str, end='',flush=True)
res += next_str
self.cur_token = next_token
self.history.append(res)
return res
def predict_no_state(self, question: str, history: list):
# question: "你多大了?" history: ["你是谁?", "我是Qwen。"]
self.history = history
self.history.append(question)
self.history_to_c()
str2tokens(self.chat, self.history_c, self.tokens)
first_token = predict_first_token_with_tokens(self.chat, self.tokens.data[:self.tokens.size])
first_str = token2str_with_pre(self.chat, first_token)
res = first_str
self.cur_token = first_token
while True:
if self.cur_token == self.eos:
break
if self.token_len >= MAX_LEN:
print("......\n Warning: cleanup early history")
break
next_token = predict_next_token(self.chat)
next_str = token2str_with_pre(self.chat, next_token)
# print(next_str, end='',flush=True)
res += next_str
self.cur_token = next_token
yield {
"data": res
}
if __name__=="__main__":
qwen = QwenChat()
while True:
try:
context = input("Qwen: ")
qwen.predict(context)
print("\n")
except KeyboardInterrupt:
break
pass