-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathllava_server.cpp
293 lines (244 loc) · 9.45 KB
/
llava_server.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* llava_server.cpp
* Bart Trzynadlowski, 2023
*
* Simple LLaVA server. Use /llava endpoint to submit images and prompts. Wraps llama.cpp, which
* support LLaVA.
*
* Sample usage:
*
* bin/llava-server -m ggml-model-q5_k.gguf --mmproj mmproj-model-f16.gguf --port 8080
*
* If running on macOS, ensure ggml-metal.metal is present in the same location as the llava-server
* binary (i.e., the bin/ directory). You can find this file in llama.cpp/.
*/
#include "web_server.hpp"
#include "llama.cpp/examples/llava/clip.h"
#include "llama.cpp/examples/llava/llava-utils.h"
#include "llama.cpp/common/common.h"
#include "llama.cpp/llama.h"
#include "llama.cpp/common/stb_image.h"
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <thread>
#include <tuple>
#include <vector>
static bool clip_image_load_from_memory(std::shared_ptr<uint8_t[]> image_buffer, size_t image_buffer_size, clip_image_u8 *img)
{
int nx, ny, nc;
auto data = stbi_load_from_memory(image_buffer.get(), image_buffer_size, &nx, &ny, &nc, 3);
if (!data)
{
fprintf(stderr, "%s: failed to load image\n", __func__);
return false;
}
img->nx = nx;
img->ny = ny;
img->size = nx * ny * 3;
img->data = new uint8_t[img->size]();
memcpy(img->data, data, img->size);
stbi_image_free(data);
return true;
}
static void perform_inference(
const llava_request &request,
httplib::Response &web_response,
gpt_params ¶ms,
clip_ctx *ctx_clip,
llama_context *ctx_llama
)
{
std::cout << "Processing request:" << std::endl
<< " System prompt: " << request.system_prompt << std::endl
<< " User prompt : " << request.user_prompt << std::endl
<< " Image : " << request.image_buffer_size << " bytes" << std::endl
<< std::endl;
// load and preprocess the image
clip_image_u8 img;
clip_image_f32 img_res;
if (!clip_image_load_from_memory(request.image, request.image_buffer_size, &img))
{
web_response.set_content("{\"error\": true, \"description\": \"unable to load image\"}", "application/json");
return;
}
if (!clip_image_preprocess(ctx_clip, &img, &img_res, /*pad2square =*/ true))
{
fprintf(stderr, "%s: unable to preprocess image\n", __func__);
web_response.set_content("{\"error\": true, \"description\": \"unable to preprocess image\"}", "application/json");
return;
}
int n_img_pos = clip_n_patches(ctx_clip);
int n_img_embd = clip_n_mmproj_embd(ctx_clip);
float *image_embd = (float *) malloc(clip_embd_nbytes(ctx_clip));
if (!image_embd)
{
fprintf(stderr, "Unable to allocate memory for image embeddings\n");
web_response.set_content("{\"error\": true, \"description\": \"unable to allocate memory for image embeddings\"}", "application/json");
return;
}
const int64_t t_img_enc_start_us = ggml_time_us();
if (!clip_image_encode(ctx_clip, params.n_threads, &img_res, image_embd))
{
fprintf(stderr, "Unable to encode image\n");
web_response.set_content("{\"error\": true, \"description\": \"unable to encode image\"}", "application/json");
return;
}
const int64_t t_img_enc_end_us = ggml_time_us();
// make sure that the correct mmproj was used, i.e., compare apples to apples
int n_llama_embd = llama_n_embd(llama_get_model(ctx_llama));
if (n_img_embd != n_llama_embd)
{
printf("%s: embedding dim of the multimodal projector (%d) is not equal to that of LLaMA (%d). Make sure that you use the correct mmproj file.\n", __func__, n_img_embd, n_llama_embd);
web_response.set_content("{\"error\": true, \"description\": \"multimodal projector embedding dimensions are not equal to LLaMA, which may indicate the wrong mmproj file is being used\"}", "application/json");
free(image_embd);
return;
}
// process the prompt
// llava chat format is "<system_prompt>USER: <image_embeddings>\n<textual_prompt>\nASSISTANT:"
int n_past = 0;
const int max_tgt_len = params.n_predict < 0 ? 256 : params.n_predict;
// Clear state
llama_kv_cache_tokens_rm(ctx_llama, -1, -1);
// GG: are we sure that the should be a trailing whitespace at the end of this string?
std::string prompt = request.system_prompt + "\nUSER: ";
eval_string(ctx_llama, prompt.c_str(), params.n_batch, &n_past);
eval_image_embd(ctx_llama, image_embd, n_img_pos, params.n_batch, &n_past);
eval_string(ctx_llama, request.user_prompt.c_str(), params.n_batch, &n_past);
eval_string(ctx_llama, "\nASSISTANT:", params.n_batch, &n_past);
// generate the response
printf("\n");
std::string output;
for (int i = 0; i < max_tgt_len; i++)
{
const char * tmp = sample(ctx_llama, params, &n_past);
if (strcmp(tmp, "</s>") == 0) break;
output += tmp;
printf("%s", tmp);
fflush(stdout);
}
web_response.set_content("{\"error\": false, \"content\": \"" + escape_json(output) + "\"}", "application/json");
printf("\n");
{
const float t_img_enc_ms = (t_img_enc_end_us - t_img_enc_start_us) / 1000.0;
printf("\n%s: image encoded in %8.2f ms by CLIP (%8.2f ms per image patch)\n", __func__, t_img_enc_ms, t_img_enc_ms / n_img_pos);
}
llama_print_timings(ctx_llama);
free(image_embd);
}
static void show_additional_info(int /*argc*/, char **argv)
{
printf("\n web server options:\n");
printf(" --host HOST host to serve on (default: localhost)\n");
printf(" --port PORT port to serve on (default: 8080)\n");
printf(" --log-http enable http logging\n");
printf("\n");
printf("\n example usage: %s -m <llava-v1.5-7b/ggml-model-q5_k.gguf> --mmproj <llava-v1.5-7b/mmproj-model-f16.gguf> [--temp 0.1]\n", argv[0]);
printf(" note: a lower temperature value like 0.1 is recommended for better quality.\n");
}
static bool parse_command_line(int argc, char **argv, gpt_params ¶ms, std::string &hostname, int &port, bool &enable_http_logging)
{
// Convert to vector
std::vector<char *> args;
for (int i = 0; i < argc; i++)
{
args.emplace_back(argv[i]);
}
// First, handle our custom arguments and then remove them
for (auto it = args.begin()++; it != args.end(); )
{
if (!strcmp(*it, "--host") || !strcmp(*it, "--port"))
{
char *arg = *it;
it = args.erase(it); // remove this element, point to next one
if (it == args.end())
{
fprintf(stderr, "error: %s requires one argument.\n", arg);
return true;
}
else
{
if (!strcmp(arg, "--host"))
{
hostname = *it;
}
else
{
port = std::stoi(*it);
}
it = args.erase(it);
}
}
else if (!strcmp(*it, "--log-http"))
{
enable_http_logging = true;
it = args.erase(it);
}
else
{
++it;
}
}
// Construct new argc, argv with our custom arguments removed
int new_argc = args.size();
char **new_argv = new char *[new_argc];
for (int i = 0; i < new_argc; i++)
{
new_argv[i] = args[i];
}
// Parse using llama.cpp parser
bool success = gpt_params_parse(new_argc, new_argv, params);
// Clean up
delete [] new_argv;
return success;
}
int main(int argc, char ** argv)
{
ggml_time_init();
gpt_params params;
std::string hostname = "localhost";
int port = 8080;
bool enable_http_logging = false;
if (!parse_command_line(argc, argv, params, hostname, port, enable_http_logging))
{
show_additional_info(argc, argv);
return 1;
}
if (params.mmproj.empty())
{
gpt_print_usage(argc, argv, params);
show_additional_info(argc, argv);
return 1;
}
const char * clip_path = params.mmproj.c_str();
auto ctx_clip = clip_model_load(clip_path, /*verbosity=*/ 1);
llama_backend_init(params.numa);
llama_model_params model_params = llama_model_default_params();
llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
if (model == NULL)
{
fprintf(stderr , "%s: error: unable to load model\n" , __func__);
return 1;
}
llama_context_params ctx_params = llama_context_default_params();
ctx_params.n_ctx = params.n_ctx < 2048 ? 2048 : params.n_ctx; // we need a longer context size to process image embeddings
ctx_params.n_threads = params.n_threads;
ctx_params.n_threads_batch = params.n_threads_batch == -1 ? params.n_threads : params.n_threads_batch;
// create a llama context once that we'll reuse for each request
llama_context * ctx_llama = llama_new_context_with_model(model, ctx_params);
if (ctx_llama == NULL)
{
fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
return 1;
}
// Serve forever
std::mutex mtx;
run_web_server(hostname, port, enable_http_logging,
[&mtx, ¶ms, ctx_clip, ctx_llama](const llava_request &request, httplib::Response &response)
{
std::unique_lock lock(mtx);
perform_inference(request, response, params, ctx_clip, ctx_llama);
}
);
return 0;
}