ext_server.cpp 12.3 KB
Newer Older
1
#include "ext_server.h"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
2
#include <atomic>
3
4
5
6

// Necessary evil since the server types are not defined in a header
#include "server.cpp"

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Low level API access to verify GPU access
#if defined(GGML_USE_CUBLAS)
#if defined(GGML_USE_HIPBLAS)
#include <hip/hip_runtime.h>
#include <hipblas/hipblas.h>
#include <hip/hip_fp16.h>
#ifdef __HIP_PLATFORM_AMD__
// for rocblas_initialize()
#include "rocblas/rocblas.h"
#endif // __HIP_PLATFORM_AMD__
#define cudaGetDevice hipGetDevice
#define cudaError_t hipError_t
#define cudaSuccess hipSuccess
#define cudaGetErrorString hipGetErrorString
#else
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <cuda_fp16.h>
#endif // defined(GGML_USE_HIPBLAS)
#endif // GGML_USE_CUBLAS

28
// Expose the llama server as a callable extern "C" API
29
server_context *llama = NULL;
30
std::thread ext_server_thread;
Daniel Hiltgen's avatar
Daniel Hiltgen committed
31
32
bool shutting_down = false;
std::atomic_int recv_counter;
33

Daniel Hiltgen's avatar
Daniel Hiltgen committed
34
35
36
37
38
39
40
41
42
43
44
45
46
// RAII wrapper for tracking in-flight recv calls
class atomicRecv {
  public:
    atomicRecv(std::atomic<int> &atomic) : atomic(atomic) {
      ++this->atomic;
    }
    ~atomicRecv() {
      --this->atomic;
    }
  private:
    std::atomic<int> &atomic;
};
 
47
void llama_server_init(ext_server_params *sparams, ext_server_resp_t *err) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
48
  recv_counter = 0;
49
  assert(err != NULL && sparams != NULL);
Daniel Hiltgen's avatar
Daniel Hiltgen committed
50
51
  log_set_target(stderr);
  if (!sparams->verbose_logging) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
52
    server_verbose = true;
Daniel Hiltgen's avatar
Daniel Hiltgen committed
53
54
55
56
    log_disable();
  }

  LOG_TEE("system info: %s\n", llama_print_system_info());
57
58
59
  err->id = 0;
  err->msg[0] = '\0';
  try {
60
    llama = new server_context;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    gpt_params params;
    params.n_ctx = sparams->n_ctx;
    params.n_batch = sparams->n_batch;
    if (sparams->n_threads > 0) {
      params.n_threads = sparams->n_threads;
    }
    params.n_parallel = sparams->n_parallel;
    params.rope_freq_base = sparams->rope_freq_base;
    params.rope_freq_scale = sparams->rope_freq_scale;

    if (sparams->memory_f16) {
      params.cache_type_k = "f16";
      params.cache_type_v = "f16";
    } else {
      params.cache_type_k = "f32";
      params.cache_type_v = "f32";
    }

    params.n_gpu_layers = sparams->n_gpu_layers;
    params.main_gpu = sparams->main_gpu;
    params.use_mlock = sparams->use_mlock;
    params.use_mmap = sparams->use_mmap;
83
    params.numa = (ggml_numa_strategy)sparams->numa;
84
85
86
87
88
    params.embedding = sparams->embedding;
    if (sparams->model != NULL) {
      params.model = sparams->model;
    }

89
90
91
92
93
94
95
    if (sparams->lora_adapters != NULL) {
      for (ext_server_lora_adapter *la = sparams->lora_adapters; la != NULL;
          la = la->next) {
        params.lora_adapter.push_back(std::make_tuple(la->adapter, la->scale));
      }

      params.use_mmap = false;
96
97
98
99
100
101
    }

    if (sparams->mmproj != NULL) {
      params.mmproj = std::string(sparams->mmproj);
    }

102
103
104
105
106
107
108
109
110
111
112
113
#if defined(GGML_USE_CUBLAS)
    // Before attempting to init the backend which will assert on error, verify the CUDA/ROCM GPU is accessible
    LOG_TEE("Performing pre-initialization of GPU\n");
    int id;
    cudaError_t cudaErr = cudaGetDevice(&id);
    if (cudaErr != cudaSuccess) {
      err->id = -1;
      snprintf(err->msg, err->msg_len, "Unable to init GPU: %s", cudaGetErrorString(cudaErr));
      return;
    }
#endif

114
115
    llama_backend_init();
    llama_numa_init(params.numa);
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

    // load the model
    if (!llama->load_model(params)) {
      // TODO - consider modifying the logging logic or patching load_model so
      // we can capture more detailed error messages and pass them back to the
      // caller for better UX
      err->id = -1;
      snprintf(err->msg, err->msg_len, "error loading model %s",
               params.model.c_str());
      return;
    }

    llama->initialize();
  } catch (std::exception &e) {
    err->id = -1;
    snprintf(err->msg, err->msg_len, "exception %s", e.what());
  } catch (...) {
    err->id = -1;
    snprintf(err->msg, err->msg_len,
             "Unknown exception initializing llama server");
  }
}

void llama_server_start() {
  assert(llama != NULL);
  // TODO mutex to protect thread creation
  ext_server_thread = std::thread([&]() {
    try {
      LOG_TEE("llama server main loop starting\n");
      ggml_time_init();
Daniel Hiltgen's avatar
Daniel Hiltgen committed
146
      llama->queue_tasks.on_new_task(std::bind(
147
        &server_context::process_single_task, llama, std::placeholders::_1));
Daniel Hiltgen's avatar
Daniel Hiltgen committed
148
      llama->queue_tasks.on_finish_multitask(std::bind(
149
        &server_context::on_finish_multitask, llama, std::placeholders::_1));
150
      llama->queue_tasks.on_run_slots(std::bind(
151
        &server_context::update_slots, llama));
Daniel Hiltgen's avatar
Daniel Hiltgen committed
152
      llama->queue_results.on_multitask_update(std::bind(
153
          &server_queue::update_multitask,
Daniel Hiltgen's avatar
Daniel Hiltgen committed
154
155
156
157
158
159
          &llama->queue_tasks,
          std::placeholders::_1,
          std::placeholders::_2,
          std::placeholders::_3
        ));
      llama->queue_tasks.start_loop();
160
161
162
163
164
165
166
167
168
169
170
171
    } catch (std::exception &e) {
      LOG_TEE("caught exception in llama server main loop: %s\n", e.what());
    } catch (...) {
      LOG_TEE("caught unknown exception in llama server main loop\n");
    }
    LOG_TEE("\nllama server shutting down\n");
    llama_backend_free();
  });
}

void llama_server_stop() {
  assert(llama != NULL);
Daniel Hiltgen's avatar
Daniel Hiltgen committed
172
  // Shutdown any in-flight requests and block incoming requests.
Daniel Hiltgen's avatar
Daniel Hiltgen committed
173
  LOG_TEE("\ninitiating shutdown - draining remaining tasks...\n");
Daniel Hiltgen's avatar
Daniel Hiltgen committed
174
175
176
177
178
179
  shutting_down = true;

  while (recv_counter.load() > 0) {
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
  }

Daniel Hiltgen's avatar
Daniel Hiltgen committed
180
181
182
  // This may take a while for any pending tasks to drain
  // TODO - consider a timeout to cancel tasks if it's taking too long
  llama->queue_tasks.terminate();
183
184
185
186
  ext_server_thread.join();
  delete llama;
  llama = NULL;
  LOG_TEE("llama server shutdown complete\n");
187
  shutting_down = false;
188
189
190
191
192
193
194
}

void llama_server_completion(const char *json_req, ext_server_resp_t *resp) {
  assert(llama != NULL && json_req != NULL && resp != NULL);
  resp->id = -1;
  resp->msg[0] = '\0';
  try {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
195
196
197
    if (shutting_down) {
      throw std::runtime_error("server shutting down");
    }
198
    json data = json::parse(json_req);
Daniel Hiltgen's avatar
Daniel Hiltgen committed
199
200
    resp->id = llama->queue_tasks.get_new_id();
    llama->queue_results.add_waiting_task_id(resp->id);
201
    llama->request_completion(resp->id, -1, data, false, false);
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  } catch (std::exception &e) {
    snprintf(resp->msg, resp->msg_len, "exception %s", e.what());
  } catch (...) {
    snprintf(resp->msg, resp->msg_len, "Unknown exception during completion");
  }
}

void llama_server_completion_next_result(const int task_id,
                                         ext_server_task_result_t *resp) {
  assert(llama != NULL && resp != NULL);
  resp->id = -1;
  resp->stop = false;
  resp->error = false;
  resp->json_resp = NULL;
  std::string result_json;
  try {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
218
    atomicRecv ar(recv_counter);
219
    server_task_result result = llama->queue_results.recv(task_id);
220
    result_json =
221
        result.data.dump(-1, ' ', false, json::error_handler_t::replace);
222
223
224
225
    resp->id = result.id;
    resp->stop = result.stop;
    resp->error = result.error;
    if (result.error) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
226
      LOG_TEE("next result cancel on error\n");
227
      llama->request_cancel(task_id);
Daniel Hiltgen's avatar
Daniel Hiltgen committed
228
229
      LOG_TEE("next result removing waiting tak ID: %d\n", task_id);
      llama->queue_results.remove_waiting_task_id(task_id);
230
    } else if (result.stop) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
231
      LOG_TEE("next result cancel on stop\n");
232
      llama->request_cancel(task_id);
Daniel Hiltgen's avatar
Daniel Hiltgen committed
233
234
      LOG_TEE("next result removing waiting task ID: %d\n", task_id);
      llama->queue_results.remove_waiting_task_id(task_id);
Daniel Hiltgen's avatar
Daniel Hiltgen committed
235
236
237
238
239
    } else if (shutting_down) {
      LOG_TEE("aborting completion due to shutdown %d\n", task_id);
      llama->request_cancel(task_id);
      llama->queue_results.remove_waiting_task_id(task_id);
      resp->stop = true;
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
    }
  } catch (std::exception &e) {
    resp->error = true;
    resp->id = -1;
    result_json = "{\"error\":\"exception " + std::string(e.what()) + "\"}";
    LOG_TEE("llama server completion exception %s\n", e.what());
  } catch (...) {
    resp->error = true;
    resp->id = -1;
    result_json = "{\"error\":\"Unknown exception during completion\"}";
    LOG_TEE("llama server completion unknown exception\n");
  }
  const std::string::size_type size = result_json.size() + 1;
  resp->json_resp = new char[size];
  snprintf(resp->json_resp, size, "%s", result_json.c_str());
}

void llama_server_release_task_result(ext_server_task_result_t *result) {
  if (result == NULL || result->json_resp == NULL) {
    return;
  }
  delete[] result->json_resp;
}

void llama_server_completion_cancel(const int task_id, ext_server_resp_t *err) {
  assert(llama != NULL && err != NULL);
  err->id = 0;
  err->msg[0] = '\0';
  try {
    llama->request_cancel(task_id);
Daniel Hiltgen's avatar
Daniel Hiltgen committed
270
    llama->queue_results.remove_waiting_task_id(task_id);
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  } catch (std::exception &e) {
    err->id = -1;
    snprintf(err->msg, err->msg_len, "exception %s", e.what());
  } catch (...) {
    err->id = -1;
    snprintf(err->msg, err->msg_len,
             "Unknown exception completion cancel in llama server");
  }
}

void llama_server_tokenize(const char *json_req, char **json_resp,
                           ext_server_resp_t *err) {
  assert(llama != NULL && json_req != NULL && json_resp != NULL && err != NULL);
  *json_resp = NULL;
  err->id = 0;
  err->msg[0] = '\0';
  try {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
288
289
290
    if (shutting_down) {
      throw std::runtime_error("server shutting down");
    }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    const json body = json::parse(json_req);
    std::vector<llama_token> tokens;
    if (body.count("content") != 0) {
      tokens = llama->tokenize(body["content"], false);
    }
    const json data = format_tokenizer_response(tokens);
    std::string result_json = data.dump();
    const std::string::size_type size = result_json.size() + 1;
    *json_resp = new char[size];
    snprintf(*json_resp, size, "%s", result_json.c_str());
  } catch (std::exception &e) {
    err->id = -1;
    snprintf(err->msg, err->msg_len, "exception %s", e.what());
  } catch (...) {
    err->id = -1;
    snprintf(err->msg, err->msg_len, "Unknown exception during tokenize");
  }
}

void llama_server_release_json_resp(char **json_resp) {
  if (json_resp == NULL || *json_resp == NULL) {
    return;
  }
  delete[] *json_resp;
}

void llama_server_detokenize(const char *json_req, char **json_resp,
                             ext_server_resp_t *err) {
  assert(llama != NULL && json_req != NULL && json_resp != NULL && err != NULL);
  *json_resp = NULL;
  err->id = 0;
  err->msg[0] = '\0';
  try {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
324
325
326
    if (shutting_down) {
      throw std::runtime_error("server shutting down");
    }
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
    const json body = json::parse(json_req);
    std::string content;
    if (body.count("tokens") != 0) {
      const std::vector<llama_token> tokens = body["tokens"];
      content = tokens_to_str(llama->ctx, tokens.cbegin(), tokens.cend());
    }
    const json data = format_detokenized_response(content);
    std::string result_json = data.dump();
    const std::string::size_type size = result_json.size() + 1;
    *json_resp = new char[size];
    snprintf(*json_resp, size, "%s", result_json.c_str());
  } catch (std::exception &e) {
    err->id = -1;
    snprintf(err->msg, err->msg_len, "exception %s", e.what());
  } catch (...) {
    err->id = -1;
    snprintf(err->msg, err->msg_len, "Unknown exception during detokenize");
  }
}

void llama_server_embedding(const char *json_req, char **json_resp,
                            ext_server_resp_t *err) {
  assert(llama != NULL && json_req != NULL && json_resp != NULL && err != NULL);
  *json_resp = NULL;
  err->id = 0;
  err->msg[0] = '\0';
  try {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
354
355
356
    if (shutting_down) {
      throw std::runtime_error("server shutting down");
    }
357
358
359
360
361
362
363
    const json body = json::parse(json_req);
    json prompt;
    if (body.count("content") != 0) {
      prompt = body["content"];
    } else {
      prompt = "";
    }
Daniel Hiltgen's avatar
Daniel Hiltgen committed
364
365
    const int task_id = llama->queue_tasks.get_new_id();
    llama->queue_results.add_waiting_task_id(task_id);
366
    llama->request_completion(task_id, -1, {{"prompt", prompt}, {"n_predict", 0}}, false, true);
Daniel Hiltgen's avatar
Daniel Hiltgen committed
367
    atomicRecv ar(recv_counter);
368
369
    server_task_result result = llama->queue_results.recv(task_id);
    std::string result_json = result.data.dump();
370
371
372
    const std::string::size_type size = result_json.size() + 1;
    *json_resp = new char[size];
    snprintf(*json_resp, size, "%s", result_json.c_str());
Daniel Hiltgen's avatar
Daniel Hiltgen committed
373
    llama->queue_results.remove_waiting_task_id(task_id);
374
375
376
377
378
379
380
381
  } catch (std::exception &e) {
    err->id = -1;
    snprintf(err->msg, err->msg_len, "exception %s", e.what());
  } catch (...) {
    err->id = -1;
    snprintf(err->msg, err->msg_len, "Unknown exception during embedding");
  }
}