-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.cpp
280 lines (229 loc) · 9.45 KB
/
binding.cpp
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
#include "chatglm.h"
#include "binding.h"
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <cassert>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <signal.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif
#if defined (_WIN32)
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
void sigint_handler(int signo) {
if (signo == SIGINT) {
_exit(130);
}
}
#endif
// stream for callback go function, copy from chatglm::TextStreamer
class TextBindStreamer : public chatglm::BaseStreamer {
public:
TextBindStreamer(chatglm::BaseTokenizer *tokenizer, void* draft_pipe)
: draft_pipe(draft_pipe), tokenizer_(tokenizer), is_prompt_(true), print_len_(0) {}
void put(const std::vector<int> &output_ids) override;
void end() override;
private:
void* draft_pipe;
chatglm::BaseTokenizer *tokenizer_;
bool is_prompt_;
std::vector<int> token_cache_;
int print_len_;
};
std::vector<chatglm::ChatMessage> create_chat_message_vector(void** history, int count) {
std::vector<chatglm::ChatMessage>* vec = new std::vector<chatglm::ChatMessage>;
for (int i = 0; i < count; i++) {
chatglm::ChatMessage* msg = (chatglm::ChatMessage*) history[i];
vec->push_back(*msg);
}
return *vec;
}
std::vector<chatglm::ToolCallMessage> create_tool_call_vector(void** tool_calls, int count) {
std::vector<chatglm::ToolCallMessage>* vec = new std::vector<chatglm::ToolCallMessage>;
for (int i = 0; i < count; i++) {
chatglm::ToolCallMessage* msg = (chatglm::ToolCallMessage*) tool_calls[i];
vec->push_back(*msg);
}
return *vec;
}
std::string decode_with_special_tokens(chatglm::ChatGLM3Tokenizer* tokenizer, const std::vector<int> &ids) {
std::vector<std::string> pieces;
for (int id : ids) {
auto pos = tokenizer->index_special_tokens.find(id);
if (pos != tokenizer->index_special_tokens.end()) {
// special tokens
pieces.emplace_back(pos->second);
} else {
// normal tokens
pieces.emplace_back(tokenizer->sp.IdToPiece(id));
}
}
std::string text = tokenizer->sp.DecodePieces(pieces);
return text;
}
void* load_model(const char *name) {
return new chatglm::Pipeline(name);
}
int chat(void* pipe_pr, void** history, int history_count, void* params_ptr, char* result) {
std::vector<chatglm::ChatMessage> vectors = create_chat_message_vector(history, history_count);
chatglm::Pipeline* pipe_p = (chatglm::Pipeline*) pipe_pr;
chatglm::GenerationConfig* params = (chatglm::GenerationConfig*) params_ptr;
chatglm::ChatMessage res = pipe_p->chat(vectors, *params);
std::string out = res.content;
// ChatGLM3Tokenizer::decode_message change origin output, convert it to ChatMessage
// So we need to convert it back
if (pipe_p->model->config.model_type == chatglm::ModelType::CHATGLM3) {
std::vector<chatglm::ChatMessage>* resultVec = new std::vector<chatglm::ChatMessage>{res};
chatglm::ChatGLM3Tokenizer* tokenizer = dynamic_cast<chatglm::ChatGLM3Tokenizer*>(pipe_p->tokenizer.get());
std::vector<int> input_ids = tokenizer->encode_messages(*resultVec, params->max_context_length);
out = decode_with_special_tokens(tokenizer, input_ids);
}
strcpy(result, out.c_str());
vectors.clear();
return 0;
}
int stream_chat(void* pipe_pr, void** history, int history_count,void* params_ptr, char* result) {
std::vector<chatglm::ChatMessage> vectors = create_chat_message_vector(history, history_count);
chatglm::Pipeline* pipe_p = (chatglm::Pipeline*) pipe_pr;
chatglm::GenerationConfig* params = (chatglm::GenerationConfig*) params_ptr;
TextBindStreamer* text_stream = new TextBindStreamer(pipe_p->tokenizer.get(), pipe_pr);
chatglm::ChatMessage res = pipe_p->chat(vectors, *params, text_stream);
std::string out = res.content;
// ChatGLM3Tokenizer::decode_message change origin output, convert it to ChatMessage
// So we need to convert it back
if (pipe_p->model->config.model_type == chatglm::ModelType::CHATGLM3) {
std::vector<chatglm::ChatMessage>* resultVec = new std::vector<chatglm::ChatMessage>{res};
chatglm::ChatGLM3Tokenizer* tokenizer = dynamic_cast<chatglm::ChatGLM3Tokenizer*>(pipe_p->tokenizer.get());
std::vector<int> input_ids = tokenizer->encode_messages(*resultVec, params->max_context_length);
out = decode_with_special_tokens(tokenizer, input_ids);
}
strcpy(result, out.c_str());
vectors.clear();
return 0;
}
int generate(void* pipe_pr, const char *prompt, void* params_ptr, char* result) {
chatglm::Pipeline* pipe_p = (chatglm::Pipeline*) pipe_pr;
chatglm::GenerationConfig* params = (chatglm::GenerationConfig*) params_ptr;
std::string res = pipe_p->generate(std::string(prompt), *params);
strcpy(result, res.c_str());
return 0;
}
int stream_generate(void* pipe_pr, const char *prompt, void* params_ptr, char* result) {
chatglm::Pipeline* pipe_p = (chatglm::Pipeline*) pipe_pr;
chatglm::GenerationConfig* params = (chatglm::GenerationConfig*) params_ptr;
TextBindStreamer* text_stream = new TextBindStreamer(pipe_p->tokenizer.get(), pipe_pr);
std::string res = pipe_p->generate(std::string(prompt), *params, text_stream);
strcpy(result, res.c_str());
return 0;
}
int get_embedding(void* pipe_pr, const char *prompt, int max_length, int * result) {
chatglm::Pipeline* pipe_p = (chatglm::Pipeline*) pipe_pr;
std::vector<int> embeddings = pipe_p->tokenizer->encode(prompt, max_length);
for (size_t i = 0; i < embeddings.size(); i++) {
result[i]=embeddings[i];
}
return 0;
}
void* allocate_params(int max_length, int max_context_length, bool do_sample, int top_k,
float top_p, float temperature, float repetition_penalty, int num_threads) {
chatglm::GenerationConfig* gen_config = new chatglm::GenerationConfig;
gen_config->max_length = max_length;
gen_config->max_context_length = max_context_length;
gen_config->do_sample = do_sample;
gen_config->top_k = top_k;
gen_config->top_p = top_p;
gen_config->temperature = temperature;
gen_config->repetition_penalty = repetition_penalty;
gen_config->num_threads = num_threads;
return gen_config;
}
void free_params(void* params_ptr) {
chatglm::GenerationConfig* params = (chatglm::GenerationConfig*) params_ptr;
delete params;
}
void free_model(void* pipe_pr) {
chatglm::Pipeline* pipe_p = (chatglm::Pipeline*) pipe_pr;
delete pipe_p;
}
void* create_chat_message(const char* role, const char *content, void** tool_calls, int tool_calls_count) {
std::vector<chatglm::ToolCallMessage> vector = create_tool_call_vector(tool_calls, tool_calls_count);
return new chatglm::ChatMessage(role, content, vector);
}
void* create_tool_call(const char* type, void* codeOrFunc) {
if (type == chatglm::ToolCallMessage::TYPE_FUNCTION) {
chatglm::FunctionMessage* function_p = (chatglm::FunctionMessage*) codeOrFunc;
return new chatglm::ToolCallMessage(*function_p);
} else if (type == chatglm::ToolCallMessage::TYPE_CODE) {
chatglm::CodeMessage* code_p = (chatglm::CodeMessage*) codeOrFunc;
return new chatglm::ToolCallMessage(*code_p);
}
return nullptr;
}
void* create_function(const char* name, const char *arguments) {
return new chatglm::FunctionMessage(name, arguments);
}
void* create_code(const char* input) {
return new chatglm::CodeMessage(input);
}
char* get_model_type(void* pipe_pr) {
chatglm::Pipeline* pipe_p = (chatglm::Pipeline*) pipe_pr;
chatglm::ModelLoader loader(pipe_p->mapped_file->data, pipe_p->mapped_file->size);
loader.read_string(4);
return strdup(chatglm::to_string((chatglm::ModelType)loader.read_basic<int>()).data());
}
// copy from chatglm::TextStreamer
void TextBindStreamer::put(const std::vector<int> &output_ids) {
if (is_prompt_) {
// skip prompt
is_prompt_ = false;
return;
}
static const std::vector<char> puncts{',', '!', ':', ';', '?'};
token_cache_.insert(token_cache_.end(), output_ids.begin(), output_ids.end());
std::string text = tokenizer_->decode(token_cache_);
if (text.empty()) {
return;
}
std::string printable_text;
if (text.back() == '\n') {
// flush the cache after newline
printable_text = text.substr(print_len_);
token_cache_.clear();
print_len_ = 0;
} else if (std::find(puncts.begin(), puncts.end(), text.back()) != puncts.end()) {
// last symbol is a punctuation, hold on
} else if (text.size() >= 3 && text.compare(text.size() - 3, 3, "�") == 0) {
// ends with an incomplete token, hold on
} else {
printable_text = text.substr(print_len_);
print_len_ = text.size();
}
// callback go function
if (!streamCallback(draft_pipe, printable_text.data())) {
return;
}
}
// copy from chatglm::TextStreamer
void TextBindStreamer::end() {
std::string text = tokenizer_->decode(token_cache_);
// callback go function
if (!streamCallback(draft_pipe, text.substr(print_len_).data())) {
return;
}
is_prompt_ = true;
token_cache_.clear();
print_len_ = 0;
}