forked from b4rtaz/distributed-llama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllama-api.cpp
592 lines (512 loc) · 19.7 KB
/
dllama-api.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
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
324
325
326
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include <cstring>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <cassert>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#endif
#include "tokenizer.hpp"
#include "app.hpp"
#include "json.hpp"
#include "api-types.hpp"
#include "nn/nn-network.hpp"
typedef unsigned int pos_t;
using json = nlohmann::json;
enum class HttpMethod {
METHOD_GET = 0,
METHOD_POST = 1,
METHOD_PUT = 2,
METHOD_DELETE = 3,
METHOD_UNKNOWN = 4
};
class HttpRequest {
public:
static HttpRequest read(int serverSocket) {
HttpRequest req(serverSocket);
std::vector<char> httpRequest = req.readHttpRequest();
// Parse the HTTP request
std::string data = std::string(httpRequest.begin(), httpRequest.end());
// Split request into lines
std::istringstream iss(data);
std::string line;
std::getline(iss, line);
// Parse request line
std::istringstream lineStream(line);
std::string methodStr, path;
lineStream >> methodStr >> path;
req.method = parseMethod(methodStr);
req.path = path;
// Parse headers
while (std::getline(iss, line) && line != "\r") {
size_t pos = line.find(':');
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 2); // Skip ': ' after key
// Trim whitespace and non-printable characters from header value
value.erase(std::remove_if(value.begin(), value.end(), [](unsigned char c) {
return std::isspace(c) || !std::isprint(c);
}), value.end());
req.headers[key] = value;
}
}
// Parse body
std::getline(iss, req.body, '\0');
if (req.body.size() > 0) {
// printf("body: %s\n", req.body.c_str());
req.parsedJson = json::parse(req.body);
}
return req;
}
static HttpMethod parseMethod(const std::string& method) {
if (method == "GET") return HttpMethod::METHOD_GET;
if (method == "POST") return HttpMethod::METHOD_POST;
if (method == "PUT") return HttpMethod::METHOD_PUT;
if (method == "DELETE") return HttpMethod::METHOD_DELETE;
return HttpMethod::METHOD_UNKNOWN;
}
private:
int serverSocket;
public:
std::string path;
std::unordered_map<std::string, std::string> headers;
std::string body;
json parsedJson;
HttpMethod method;
HttpRequest(int serverSocket) {
this->serverSocket = serverSocket;
}
std::vector<char> readHttpRequest() {
std::string httpRequest;
char buffer[1024 * 64];
ssize_t bytesRead;
// First, read all headers
std::string headerData;
size_t headerEnd;
bool headerDone = false;
std::string extraReadPastHeader;
while (!headerDone) {
bytesRead = recv(serverSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesRead <= 0) {
throw std::runtime_error("Error while reading headers from socket");
}
buffer[bytesRead] = '\0';
headerData.append(buffer);
// Check for end of headers (http header says "\r\n\r\n")
headerEnd = headerData.find("\r\n\r\n");
if (headerEnd != std::string::npos) {
headerDone = true;
if (headerEnd < headerData.size()-4) {
// We read something past the header
extraReadPastHeader = headerData.substr(headerEnd+4);
}
}
}
httpRequest.append(headerData);
// Next, find Content-Length header for body length
std::istringstream headerStream(headerData);
std::string line;
ssize_t contentLength = 0;
while (std::getline(headerStream, line) && line != "\r") {
size_t pos = line.find(':');
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 2); // Skip ': ' after key
if (key == "Content-Length") {
try {
contentLength = std::stoi(value); // stoi ignores any whitespace
} catch (const std::invalid_argument& e) {
throw std::runtime_error("Bad Content-Length header - not a number");
}
break;
}
}
}
// Now read the full content body
if (contentLength > 0) {
// If we read any extra past the header before, read that much less now
// But first, sanity check to make sure Content-Length isn't lying and there is actually more
if (extraReadPastHeader.size() > static_cast<size_t>(contentLength)) {
throw std::runtime_error("Received more body data than Content-Length header said");
}
contentLength -= extraReadPastHeader.size();
std::vector<char> body(contentLength);
ssize_t totalRead = 0;
while (totalRead < contentLength) {
bytesRead = recv(serverSocket, body.data() + totalRead, contentLength - totalRead, 0);
if (bytesRead <= 0) {
throw std::runtime_error("Error while reading body from socket");
}
totalRead += bytesRead;
}
if (body.size() > 0) {
httpRequest.append(body.data(), contentLength);
}
}
return std::vector<char>(httpRequest.begin(), httpRequest.end());
}
std::string getMethod() {
if (method == HttpMethod::METHOD_GET) return "GET";
if (method == HttpMethod::METHOD_POST) return "POST";
if (method == HttpMethod::METHOD_PUT) return "PUT";
if (method == HttpMethod::METHOD_DELETE) return "DELETE";
return "UNKNOWN";
}
void writeNotFound() {
std::ostringstream buffer;
buffer << "HTTP/1.1 404 Not Found\r\n"
<< "Content-Length: 9\r\n\r\nNot Found";
std::string data = buffer.str();
writeSocket(serverSocket, data.c_str(), data.size());
}
void writeJson(std::string json) {
std::ostringstream buffer;
buffer << "HTTP/1.1 200 OK\r\n"
<< "Content-Type: application/json; charset=utf-8\r\n"
<< "Content-Length: " << json.length() << "\r\n\r\n" << json;
std::string data = buffer.str();
writeSocket(serverSocket, data.c_str(), data.size());
}
void writeStreamStartChunk() {
std::ostringstream buffer;
buffer << "HTTP/1.1 200 OK\r\n"
<< "Content-Type: text/event-stream; charset=utf-8\r\n"
<< "Connection: close\r\n"
<< "Transfer-Encoding: chunked\r\n\r\n";
std::string data = buffer.str();
writeSocket(serverSocket, data.c_str(), data.size());
}
void writeStreamChunk(const std::string data) {
std::ostringstream buffer;
buffer << std::hex << data.size() << "\r\n" << data << "\r\n";
std::string d = buffer.str();
writeSocket(serverSocket, d.c_str(), d.size());
}
void writeStreamEndChunk() {
const char *endChunk = "0000\r\n\r\n";
writeSocket(serverSocket, endChunk, strlen(endChunk));
}
};
struct Route {
std::string path;
HttpMethod method;
std::function<void(HttpRequest&)> handler;
};
class Router {
public:
static void resolve(HttpRequest& request, std::vector<Route>& routes) {
for (const auto& route : routes) {
if (request.method == route.method && request.path == route.path) {
route.handler(request);
return;
}
}
request.writeNotFound();
}
};
void writeChatCompletionChunk(HttpRequest &request, const std::string &delta, const bool stop){
ChunkChoice choice;
if (stop) {
choice.finish_reason = "stop";
} else {
choice.delta = ChatMessageDelta("assistant", delta);
}
ChatCompletionChunk chunk = ChatCompletionChunk(choice);
std::ostringstream buffer;
buffer << "data: " << ((json)chunk).dump() << "\r\n\r\n";
request.writeStreamChunk(buffer.str());
if (stop) {
request.writeStreamChunk("data: [DONE]");
request.writeStreamEndChunk();
}
}
class NaiveCacheItem {
public:
pos_t endPos;
ChatMessage message;
NaiveCacheItem(pos_t endPos, ChatMessage message) {
this->endPos = endPos;
this->message = message;
}
};
class NaiveCache {
private:
std::vector<NaiveCacheItem> cache;
public:
void push(NaiveCacheItem item) {
cache.push_back(item);
}
void clear() {
cache.clear();
}
bool resolveDeltaPrompt(std::vector<ChatMessage>& messages, pos_t& startPos) {
size_t cacheSize = cache.size();
if (cacheSize == 0)
return false;
if (messages.size() > cacheSize) {
size_t i = 0;
while (i < cacheSize) {
if (
cache[i].message.role != messages[i].role ||
cache[i].message.content != messages[i].content
) break;
i++;
}
if (i == cacheSize) {
startPos = cache[i - 1].endPos;
messages.erase(messages.begin(), messages.begin() + i);
printf("🐤 Found naive cache for %zu messages, pos=%d\n", i, startPos);
return true;
}
}
cache.clear();
return false;
}
};
class ApiServer {
private:
RootLlmInference *inference;
Tokenizer *tokenizer;
Sampler *sampler;
AppCliArgs *args;
LlmHeader *header;
EosDetector *eosDetector;
ChatTemplateGenerator *templateGenerator;
NaiveCache naiveCache;
public:
ApiServer(RootLlmInference *inference, Tokenizer *tokenizer, Sampler *sampler, AppCliArgs *args, LlmHeader *header, EosDetector *eosDetector, ChatTemplateGenerator *templateGenerator) {
this->inference = inference;
this->tokenizer = tokenizer;
this->sampler = sampler;
this->args = args;
this->header = header;
this->eosDetector = eosDetector;
this->templateGenerator = templateGenerator;
}
void complete(HttpRequest& request) {
InferenceParams params = parseRequest(request);
pos_t startPos = 0;
std::vector<ChatMessage> deltaPrompt = params.messages;
naiveCache.resolveDeltaPrompt(deltaPrompt, startPos);
size_t nInputItems = deltaPrompt.size();
std::unique_ptr<ChatItem[]> inputItemsPtr(new ChatItem[nInputItems]);
ChatItem *inputItems = inputItemsPtr.get();
for (size_t i = 0; i < nInputItems; i++) {
inputItems[i].role = deltaPrompt[i].role;
inputItems[i].message = deltaPrompt[i].content;
}
GeneratedChat inputPrompt = templateGenerator->generate(nInputItems, inputItems, true);
printf("🔹%s🔸", inputPrompt.content);
int nPromptTokens;
std::unique_ptr<int[]> promptTokensPtr(new int[inputPrompt.length + 2]);
int *promptTokens = promptTokensPtr.get();
bool addBos = startPos == 0;
tokenizer->encode((char*)inputPrompt.content, promptTokens, &nPromptTokens, addBos, true);
pos_t promptEndPos = startPos + nPromptTokens - 1;
if (promptEndPos > header->seqLen)
promptEndPos = header->seqLen;
pos_t maxPredPos = params.max_tokens > 0 ? (promptEndPos + params.max_tokens) : header->seqLen;
if (maxPredPos > header->seqLen)
maxPredPos = header->seqLen;
for (size_t j = 0; j < deltaPrompt.size(); j++) {
naiveCache.push(NaiveCacheItem(promptEndPos, deltaPrompt[j]));
}
std::string buffer;
if (params.stream)
request.writeStreamStartChunk();
if (inputPrompt.publicPrompt != nullptr) {
if (params.stream)
writeChatCompletionChunk(request, inputPrompt.publicPrompt, false);
buffer += inputPrompt.publicPrompt;
}
NnSize pos = startPos;
int token;
for (NnSize i = 0; ;) {
long remainingTokens = promptEndPos - pos;
if (remainingTokens <= 0)
break;
NnSize batchSize = remainingTokens < args->nBatches
? remainingTokens
: args->nBatches;
inference->setBatchSize(batchSize);
inference->setPosition(pos);
for (NnSize j = 0; j < batchSize; j++)
inference->setToken(j, promptTokens[i + j]);
inference->forward();
i += batchSize;
pos += batchSize;
token = promptTokens[i + 1];
}
inference->setBatchSize(1);
tokenizer->resetDecoder();
for (; pos < maxPredPos;) {
inference->setPosition(pos);
inference->setToken(0, token);
inference->forward();
token = sampler->sample(inference->logitsPipe);
char *piece = tokenizer->decode(token);
EosDetectorType eosType = eosDetector->append(token, piece);
if (piece != nullptr) {
printf("%s", piece);
fflush(stdout);
}
if (eosType == NOT_EOS || eosType == EOS) {
char *delta = eosDetector->getDelta();
if (delta != nullptr) {
std::string deltaStr(delta);
if (params.stream)
writeChatCompletionChunk(request, deltaStr, false);
buffer += deltaStr;
}
eosDetector->reset();
}
pos++;
if (eosType == EOS) break;
}
ChatMessage chatMessage("assistant", buffer);
if (pos == header->seqLen) {
naiveCache.clear();
} else {
naiveCache.push(NaiveCacheItem(pos, chatMessage));
}
if (params.stream) {
writeChatCompletionChunk(request, "", true);
} else {
int nCompletionTokens = pos - promptEndPos;
ChatUsage usage(nPromptTokens, nCompletionTokens, nPromptTokens + nCompletionTokens);
Choice choice(chatMessage);
ChatCompletion completion(choice, usage);
std::string chatJson = ((json)completion).dump();
request.writeJson(chatJson);
}
printf("🔶\n");
fflush(stdout);
}
private:
InferenceParams parseRequest(HttpRequest& request) {
InferenceParams params;
params.temperature = args->temperature;
params.top_p = args->topp;
params.seed = args->seed;
params.stream = false;
params.messages = parseChatMessages(request.parsedJson["messages"]);
params.max_tokens = -1;
if (request.parsedJson.contains("stream")) {
params.stream = request.parsedJson["stream"].get<bool>();
}
if (request.parsedJson.contains("temperature")) {
params.temperature = request.parsedJson["temperature"].template get<float>();
}
if (request.parsedJson.contains("seed")) {
params.seed = request.parsedJson["seed"].template get<unsigned long long>();
sampler->setSeed(params.seed);
}
if (request.parsedJson.contains("max_tokens")) {
params.max_tokens = request.parsedJson["max_tokens"].template get<int>();
}
if (request.parsedJson.contains("stop")) {
params.stop = request.parsedJson["stop"].template get<std::vector<std::string>>();
} else {
const std::string defaultStop = "<|eot_id|>";
params.stop = std::vector<std::string>{defaultStop};
}
return params;
}
};
void handleCompletionsRequest(HttpRequest& request, ApiServer *api) {
api->complete(request);
}
void handleModelsRequest(HttpRequest& request, const char* modelPath) {
std::string path(modelPath);
size_t pos = path.find_last_of("/\\");
std::string modelName = (pos == std::string::npos) ? path : path.substr(pos+1);
request.writeJson(
"{ \"object\": \"list\","
"\"data\": ["
"{ \"id\": \"" + modelName + "\", \"object\": \"model\", \"created\": 0, \"owned_by\": \"user\" }"
"] }");
}
static void server(AppInferenceContext *context) {
int serverSocket = createServerSocket(context->args->port);
TokenizerChatStops stops(context->tokenizer);
ChatTemplateGenerator templateGenerator(context->args->chatTemplateType, context->tokenizer->chatTemplate, stops.stops[0]);
EosDetector eosDetector(stops.nStops, context->tokenizer->eosTokenIds.data(), stops.stops, stops.maxStopLength, stops.maxStopLength);
ApiServer api(context->inference, context->tokenizer, context->sampler, context->args, context->header, &eosDetector, &templateGenerator);
printf("Server URL: http://127.0.0.1:%d/v1/\n", context->args->port);
std::vector<Route> routes = {
{
"/v1/chat/completions",
HttpMethod::METHOD_POST,
std::bind(&handleCompletionsRequest, std::placeholders::_1, &api)
},
{
"/v1/models",
HttpMethod::METHOD_GET,
std::bind(&handleModelsRequest, std::placeholders::_1, context->args->modelPath)
}
};
while (true) {
try {
int clientSocket = acceptSocket(serverSocket);
HttpRequest request = HttpRequest::read(clientSocket);
printf("🔷 %s %s\n", request.getMethod().c_str(), request.path.c_str());
Router::resolve(request, routes);
} catch (NnReadNetworkException& ex) {
printf("Read socket error: %d %s\n", ex.code, ex.message);
} catch (NnWriteNetworkException& ex) {
printf("Write socket error: %d %s\n", ex.code, ex.message);
}
}
closeServerSocket(serverSocket);
}
#ifdef _WIN32
#define EXECUTABLE_NAME "dllama-api.exe"
#else
#define EXECUTABLE_NAME "dllama-api"
#endif
void usage() {
fprintf(stderr, "Usage: %s {--model <path>} {--tokenizer <path>} [--port <p>]\n", EXECUTABLE_NAME);
fprintf(stderr, " [--buffer-float-type {f32|f16|q40|q80}]\n");
fprintf(stderr, " [--weights-float-type {f32|f16|q40|q80}]\n");
fprintf(stderr, " [--max-seq-len <max>]\n");
fprintf(stderr, " [--nthreads <n>]\n");
fprintf(stderr, " [--workers <ip:port> ...]\n");
fprintf(stderr, " [--temperature <temp>]\n");
fprintf(stderr, " [--topp <t>]\n");
fprintf(stderr, " [--seed <s>]\n");
fprintf(stderr, "Example:\n");
fprintf(stderr, " sudo nice -n -20 ./dllama-api --port 9990 --nthreads 4 \\\n");
fprintf(stderr, " --model dllama_model_llama3_2_3b_instruct_q40.m \\\n");
fprintf(stderr, " --tokenizer dllama_tokenizer_llama3_2_3b_instruct_q40.t \\\n");
fprintf(stderr, " --buffer-float-type q80 --max-seq-len 8192 \\\n");
fprintf(stderr, " --workers 10.0.0.2:9998 10.0.0.3:9998 10.0.0.4:9998\n");
fflush(stderr);
}
int main(int argc, char *argv[]) {
initQuants();
initSockets();
int returnCode = EXIT_SUCCESS;
try {
AppCliArgs args = AppCliArgs::parse(argc, argv, false);
if (args.help) {
usage();
} else {
runInferenceApp(&args, server);
}
} catch (std::exception &e) {
printf("🚨 Critical error: %s\n", e.what());
returnCode = EXIT_FAILURE;
}
cleanupSockets();
return returnCode;
}