Skip to content

Commit 3bff5c0

Browse files
authored
Merge branch 'LostRuins:concedo' into koboldcpp-rocm
2 parents a7e74b3 + 266d47a commit 3bff5c0

15 files changed

+1625
-504
lines changed

examples/metal/metal.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ int main(int argc, char ** argv) {
4040
// this allocates all Metal resources and memory buffers
4141
auto * ctx_metal = ggml_metal_init();
4242

43-
ggml_metal_add_buffer(ctx_metal, "data", ggml_get_mem_buffer(ctx_data), ggml_get_mem_size(ctx_data));
44-
ggml_metal_add_buffer(ctx_metal, "eval", ggml_get_mem_buffer(ctx_eval), ggml_get_mem_size(ctx_eval));
43+
const size_t max_size_data = ggml_get_max_tensor_size(ctx_data);
44+
const size_t max_size_eval = ggml_get_max_tensor_size(ctx_eval);
45+
ggml_metal_add_buffer(ctx_metal, "data", ggml_get_mem_buffer(ctx_data), ggml_get_mem_size(ctx_data), max_size_data);
46+
ggml_metal_add_buffer(ctx_metal, "eval", ggml_get_mem_buffer(ctx_eval), ggml_get_mem_size(ctx_eval), max_size_eval);
4547

4648
// main
4749
{

examples/server/README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Command line options:
2121
- `-to N`, `--timeout N`: Server read/write timeout in seconds. Default `600`.
2222
- `--host`: Set the hostname or ip address to listen. Default `127.0.0.1`.
2323
- `--port`: Set the port to listen. Default: `8080`.
24+
- `--embedding`: Enable embedding extraction, Default: disabled.
2425

2526
## Build
2627

@@ -119,14 +120,14 @@ node .
119120

120121
`top_p`: Limit the next token selection to a subset of tokens with a cumulative probability above a threshold P (default: 0.9).
121122

122-
`n_predict`: Set the number of tokens to predict when generating text. **Note:** May exceed the set limit slightly if the last token is a partial multibyte character. (default: 128, -1 = infinity).
123+
`n_predict`: Set the number of tokens to predict when generating text. **Note:** May exceed the set limit slightly if the last token is a partial multibyte character. When 0, no tokens will be generated but the prompt is evaluated into the cache. (default: 128, -1 = infinity).
123124

124125
`n_keep`: Specify the number of tokens from the initial prompt to retain when the model resets its internal context.
125126
By default, this value is set to 0 (meaning no tokens are kept). Use `-1` to retain all tokens from the initial prompt.
126127

127128
`stream`: It allows receiving each predicted token in real-time instead of waiting for the completion to finish. To enable this, set to `true`.
128129

129-
`prompt`: Provide a prompt. Internally, the prompt is compared, and it detects if a part has already been evaluated, and the remaining part will be evaluate.
130+
`prompt`: Provide a prompt. Internally, the prompt is compared, and it detects if a part has already been evaluated, and the remaining part will be evaluate. A space is inserted in the front like main.cpp does.
130131

131132
`stop`: Specify a JSON array of stopping strings.
132133
These words will not be included in the completion, so make sure to add them to the prompt for the next iteration (default: []).
@@ -163,6 +164,14 @@ node .
163164

164165
`content`: Set the text to tokenize.
165166

167+
Note that the special `BOS` token is not added in fron of the text and also a space character is not inserted automatically as it is for `/completion`.
168+
169+
- **POST** `/embedding`: Generate embedding of a given text just as [the embedding example](../embedding) does.
170+
171+
*Options:*
172+
173+
`content`: Set the text to process.
174+
166175
## More examples
167176

168177
### Interactive mode

examples/server/server.cpp

+43-1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ struct llama_server_context {
254254
n_past += n_eval;
255255
}
256256

257+
if (params.n_predict == 0) {
258+
has_next_token = false;
259+
return llama_token_eos();
260+
}
261+
257262
// out of user input, sample next token
258263
const float temp = params.temp;
259264
const int32_t top_k = params.top_k <= 0 ? llama_n_vocab(ctx) : params.top_k;
@@ -419,6 +424,19 @@ struct llama_server_context {
419424

420425
return token_text;
421426
}
427+
428+
std::vector<float> getEmbedding() {
429+
static const int n_embd = llama_n_embd(ctx);
430+
if (!params.embedding) {
431+
LOG_WARNING("embedding disabled", {
432+
{ "params.embedding", params.embedding },
433+
});
434+
return std::vector<float>(n_embd, 0.0f);
435+
}
436+
const float * data = llama_get_embeddings(ctx);
437+
std::vector<float> embedding(data, data + n_embd);
438+
return embedding;
439+
}
422440
};
423441

424442
static void server_print_usage(const char * argv0, const gpt_params & params,
@@ -457,6 +475,7 @@ static void server_print_usage(const char * argv0, const gpt_params & params,
457475
fprintf(stderr, " --host ip address to listen (default (default: %s)\n", sparams.hostname.c_str());
458476
fprintf(stderr, " --port PORT port to listen (default (default: %d)\n", sparams.port);
459477
fprintf(stderr, " -to N, --timeout N server read/write timeout in seconds (default: %d)\n", sparams.read_timeout);
478+
fprintf(stderr, " --embedding enable embedding vector output (default: %s)\n", params.embedding ? "enabled" : "disabled");
460479
fprintf(stderr, "\n");
461480
}
462481

@@ -603,6 +622,8 @@ static void server_params_parse(int argc, char ** argv, server_params & sparams,
603622
params.use_mlock = true;
604623
} else if (arg == "--no-mmap") {
605624
params.use_mmap = false;
625+
} else if (arg == "--embedding") {
626+
params.embedding = true;
606627
} else {
607628
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
608629
server_print_usage(argv[0], default_params, default_sparams);
@@ -646,6 +667,12 @@ static json format_generation_settings(llama_server_context & llama) {
646667
};
647668
}
648669

670+
static json format_embedding_response(llama_server_context & llama) {
671+
return json {
672+
{ "embedding", llama.getEmbedding() },
673+
};
674+
}
675+
649676
static json format_final_response(llama_server_context & llama, const std::string & content) {
650677
return json {
651678
{ "content", content },
@@ -881,12 +908,27 @@ int main(int argc, char ** argv) {
881908

882909
svr.Post("/tokenize", [&llama](const Request & req, Response & res) {
883910
const json body = json::parse(req.body);
884-
const std::string content = body["content"].get<std::string>();
911+
const std::string content = body.value("content", "");
885912
const std::vector<llama_token> tokens = llama_tokenize(llama.ctx, content, false);
886913
const json data = format_tokenizer_response(tokens);
887914
return res.set_content(data.dump(), "application/json");
888915
});
889916

917+
svr.Post("/embedding", [&llama](const Request & req, Response & res) {
918+
const json body = json::parse(req.body);
919+
920+
llama.rewind();
921+
llama_reset_timings(llama.ctx);
922+
llama.params.prompt = body.value("content", "");
923+
llama.params.n_predict = 0;
924+
llama.loadPrompt();
925+
llama.beginCompletion();
926+
llama.doCompletion();
927+
928+
const json data = format_embedding_response(llama);
929+
return res.set_content(data.dump(), "application/json");
930+
});
931+
890932
svr.set_logger(log_server_request);
891933

892934
svr.set_exception_handler([](const Request &, Response & res, std::exception_ptr ep) {

0 commit comments

Comments
 (0)