Skip to content

Commit 0e9059d

Browse files
authored
Merge branch 'ikawrakow:main' into main
2 parents 586a6e6 + e672bc5 commit 0e9059d

File tree

9 files changed

+141
-32
lines changed

9 files changed

+141
-32
lines changed

.devops/llama-server-cuda.Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ ENV CUDA_DOCKER_ARCH=${CUDA_DOCKER_ARCH}
2424
ENV GGML_CUDA=1
2525
# Enable cURL
2626
ENV LLAMA_CURL=1
27+
# Must be set to 0.0.0.0 so it can listen to requests from host machine
28+
ENV LLAMA_ARG_HOST=0.0.0.0
2729

2830
RUN make -j$(nproc) llama-server
2931

.devops/llama-server-intel.Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ RUN apt-get update && \
2626
COPY --from=build /app/build/bin/llama-server /llama-server
2727

2828
ENV LC_ALL=C.utf8
29+
# Must be set to 0.0.0.0 so it can listen to requests from host machine
30+
ENV LLAMA_ARG_HOST=0.0.0.0
2931

3032
HEALTHCHECK CMD [ "curl", "-f", "http://localhost:8080/health" ]
3133

.devops/llama-server-rocm.Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ ENV GPU_TARGETS=${ROCM_DOCKER_ARCH}
3939
ENV GGML_HIPBLAS=1
4040
ENV CC=/opt/rocm/llvm/bin/clang
4141
ENV CXX=/opt/rocm/llvm/bin/clang++
42+
# Must be set to 0.0.0.0 so it can listen to requests from host machine
43+
ENV LLAMA_ARG_HOST=0.0.0.0
4244

4345
# Enable cURL
4446
ENV LLAMA_CURL=1

.devops/llama-server-vulkan.Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ RUN cp /app/build/bin/llama-server /llama-server && \
2323
rm -rf /app
2424

2525
ENV LC_ALL=C.utf8
26+
# Must be set to 0.0.0.0 so it can listen to requests from host machine
27+
ENV LLAMA_ARG_HOST=0.0.0.0
2628

2729
HEALTHCHECK CMD [ "curl", "-f", "http://localhost:8080/health" ]
2830

.devops/llama-server.Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ RUN apt-get update && \
2121
COPY --from=build /app/llama-server /llama-server
2222

2323
ENV LC_ALL=C.utf8
24+
# Must be set to 0.0.0.0 so it can listen to requests from host machine
25+
ENV LLAMA_ARG_HOST=0.0.0.0
2426

2527
HEALTHCHECK CMD [ "curl", "-f", "http://localhost:8080/health" ]
2628

common/common.cpp

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,41 @@
8787
#endif
8888
using json = nlohmann::ordered_json;
8989

90+
//
91+
// Environment variable utils
92+
//
93+
94+
template<typename T>
95+
static typename std::enable_if<std::is_same<T, std::string>::value, void>::type
96+
get_env(std::string name, T & target) {
97+
char * value = std::getenv(name.c_str());
98+
target = value ? std::string(value) : target;
99+
}
100+
101+
template<typename T>
102+
static typename std::enable_if<!std::is_same<T, bool>::value && std::is_integral<T>::value, void>::type
103+
get_env(std::string name, T & target) {
104+
char * value = std::getenv(name.c_str());
105+
target = value ? std::stoi(value) : target;
106+
}
107+
108+
template<typename T>
109+
static typename std::enable_if<std::is_floating_point<T>::value, void>::type
110+
get_env(std::string name, T & target) {
111+
char * value = std::getenv(name.c_str());
112+
target = value ? std::stof(value) : target;
113+
}
114+
115+
template<typename T>
116+
static typename std::enable_if<std::is_same<T, bool>::value, void>::type
117+
get_env(std::string name, T & target) {
118+
char * value = std::getenv(name.c_str());
119+
if (value) {
120+
std::string val(value);
121+
target = val == "1" || val == "true";
122+
}
123+
}
124+
90125
//
91126
// CPU utils
92127
//
@@ -239,12 +274,6 @@ static std::string parse_device_list(const std::string& value) {
239274
// CLI argument parsing
240275
//
241276

242-
void gpt_params_handle_hf_token(gpt_params & params) {
243-
if (params.hf_token.empty() && std::getenv("HF_TOKEN")) {
244-
params.hf_token = std::getenv("HF_TOKEN");
245-
}
246-
}
247-
248277
void gpt_params_handle_model_default(gpt_params & params) {
249278
if (!params.hf_repo.empty()) {
250279
// short-hand to avoid specifying --hf-file -> default it to --model
@@ -292,7 +321,9 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
292321

293322
gpt_params_handle_model_default(params);
294323

295-
gpt_params_handle_hf_token(params);
324+
if (params.hf_token.empty()) {
325+
get_env("HF_TOKEN", params.hf_token);
326+
}
296327

297328
if (params.escape) {
298329
if (!params.prompt_is_binary) {
@@ -329,6 +360,32 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
329360
return true;
330361
}
331362

363+
void gpt_params_parse_from_env(gpt_params & params) {
364+
// we only care about server-related params for now
365+
get_env("LLAMA_ARG_MODEL", params.model);
366+
get_env("LLAMA_ARG_MODEL_URL", params.model_url);
367+
get_env("LLAMA_ARG_MODEL_ALIAS", params.model_alias);
368+
get_env("LLAMA_ARG_HF_REPO", params.hf_repo);
369+
get_env("LLAMA_ARG_HF_FILE", params.hf_file);
370+
get_env("LLAMA_ARG_THREADS", params.n_threads);
371+
get_env("LLAMA_ARG_CTX_SIZE", params.n_ctx);
372+
get_env("LLAMA_ARG_N_PARALLEL", params.n_parallel);
373+
get_env("LLAMA_ARG_BATCH", params.n_batch);
374+
get_env("LLAMA_ARG_UBATCH", params.n_ubatch);
375+
get_env("LLAMA_ARG_N_GPU_LAYERS", params.n_gpu_layers);
376+
get_env("LLAMA_ARG_THREADS_HTTP", params.n_threads_http);
377+
get_env("LLAMA_ARG_CHAT_TEMPLATE", params.chat_template);
378+
get_env("LLAMA_ARG_N_PREDICT", params.n_predict);
379+
get_env("LLAMA_ARG_ENDPOINT_METRICS", params.endpoint_metrics);
380+
get_env("LLAMA_ARG_ENDPOINT_SLOTS", params.endpoint_slots);
381+
get_env("LLAMA_ARG_EMBEDDINGS", params.embedding);
382+
get_env("LLAMA_ARG_FLASH_ATTN", params.flash_attn);
383+
get_env("LLAMA_ARG_DEFRAG_THOLD", params.defrag_thold);
384+
get_env("LLAMA_ARG_CONT_BATCHING", params.cont_batching);
385+
get_env("LLAMA_ARG_HOST", params.hostname);
386+
get_env("LLAMA_ARG_PORT", params.port);
387+
}
388+
332389
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
333390
const auto params_org = params; // the example can modify the default params
334391

common/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ struct gpt_params {
369369
bool sweep_bench_output_jsonl = false;
370370
};
371371

372-
void gpt_params_handle_hf_token(gpt_params & params);
372+
void gpt_params_parse_from_env(gpt_params & params);
373373
void gpt_params_handle_model_default(gpt_params & params);
374374

375375
bool gpt_params_parse_ex (int argc, char ** argv, gpt_params & params);

examples/server/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,51 @@ logging:
255255
--log-append Don't truncate the old log file.
256256
```
257257

258+
Available environment variables (if specified, these variables will override parameters specified in arguments):
259+
260+
- `LLAMA_CACHE`: cache directory, used by `--hf-repo`
261+
- `HF_TOKEN`: Hugging Face access token, used when accessing a gated model with `--hf-repo`
262+
- `LLAMA_ARG_MODEL`: equivalent to `-m`
263+
- `LLAMA_ARG_MODEL_URL`: equivalent to `-mu`
264+
- `LLAMA_ARG_MODEL_ALIAS`: equivalent to `-a`
265+
- `LLAMA_ARG_HF_REPO`: equivalent to `--hf-repo`
266+
- `LLAMA_ARG_HF_FILE`: equivalent to `--hf-file`
267+
- `LLAMA_ARG_THREADS`: equivalent to `-t`
268+
- `LLAMA_ARG_CTX_SIZE`: equivalent to `-c`
269+
- `LLAMA_ARG_N_PARALLEL`: equivalent to `-np`
270+
- `LLAMA_ARG_BATCH`: equivalent to `-b`
271+
- `LLAMA_ARG_UBATCH`: equivalent to `-ub`
272+
- `LLAMA_ARG_N_GPU_LAYERS`: equivalent to `-ngl`
273+
- `LLAMA_ARG_THREADS_HTTP`: equivalent to `--threads-http`
274+
- `LLAMA_ARG_CHAT_TEMPLATE`: equivalent to `--chat-template`
275+
- `LLAMA_ARG_N_PREDICT`: equivalent to `-n`
276+
- `LLAMA_ARG_ENDPOINT_METRICS`: if set to `1`, it will enable metrics endpoint (equivalent to `--metrics`)
277+
- `LLAMA_ARG_ENDPOINT_SLOTS`: if set to `0`, it will **disable** slots endpoint (equivalent to `--no-slots`). This feature is enabled by default.
278+
- `LLAMA_ARG_EMBEDDINGS`: if set to `1`, it will enable embeddings endpoint (equivalent to `--embeddings`)
279+
- `LLAMA_ARG_FLASH_ATTN`: if set to `1`, it will enable flash attention (equivalent to `-fa`)
280+
- `LLAMA_ARG_CONT_BATCHING`: if set to `0`, it will **disable** continuous batching (equivalent to `--no-cont-batching`). This feature is enabled by default.
281+
- `LLAMA_ARG_DEFRAG_THOLD`: equivalent to `-dt`
282+
- `LLAMA_ARG_HOST`: equivalent to `--host`
283+
- `LLAMA_ARG_PORT`: equivalent to `--port`
284+
285+
Example usage of docker compose with environment variables:
286+
287+
```yml
288+
services:
289+
llamacpp-server:
290+
image: ghcr.io/ggerganov/llama.cpp:server
291+
ports:
292+
- 8080:8080
293+
volumes:
294+
- ./models:/models
295+
environment:
296+
# alternatively, you can use "LLAMA_ARG_MODEL_URL" to download the model
297+
LLAMA_ARG_MODEL: /models/my_model.gguf
298+
LLAMA_ARG_CTX_SIZE: 4096
299+
LLAMA_ARG_N_PARALLEL: 2
300+
LLAMA_ARG_ENDPOINT_METRICS: 1 # to disable, either remove or set to 0
301+
LLAMA_ARG_PORT: 8080
302+
```
258303
259304
## Build
260305

examples/server/server.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,7 @@ struct server_context {
11461146

11471147
bool clean_kv_cache = true;
11481148
bool add_bos_token = true;
1149+
bool has_eos_token = false;
11491150

11501151
// For speculative decoding
11511152
llama_model * model_draft = nullptr;
@@ -1232,7 +1233,7 @@ struct server_context {
12321233
n_ctx = llama_n_ctx(ctx);
12331234

12341235
add_bos_token = llama_should_add_bos_token(model);
1235-
GGML_ASSERT(llama_add_eos_token(model) != 1);
1236+
has_eos_token = llama_add_eos_token(model) != 1;
12361237

12371238
chat_templates = common_chat_templates_init(model, params.chat_template);
12381239
try {
@@ -1350,13 +1351,13 @@ struct server_context {
13501351
default_generation_settings_for_props = get_formated_generation(slots.front());
13511352
default_generation_settings_for_props["seed"] = -1;
13521353

1353-
// the update_slots() logic will always submit a maximum of n_batch tokens
1354+
// the update_slots() logic will always submit a maximum of n_batch or n_parallel tokens
13541355
// note that n_batch can be > n_ctx (e.g. for non-causal attention models such as BERT where the KV cache is not used)
13551356
{
13561357
const int32_t n_batch = llama_n_batch(ctx);
13571358

13581359
// only a single seq_id per token is needed
1359-
batch = llama_batch_init(n_batch, 0, 1);
1360+
batch = llama_batch_init(std::max(n_batch, params.n_parallel), 0, 1);
13601361
}
13611362

13621363
metrics.init();
@@ -1784,7 +1785,7 @@ struct server_context {
17841785
{
17851786
slot.sparams.logit_bias.clear();
17861787

1787-
if (json_value(data, "ignore_eos", false)) {
1788+
if (json_value(data, "ignore_eos", false) && has_eos_token) {
17881789
slot.sparams.logit_bias[llama_token_eos(model)] = -INFINITY;
17891790
}
17901791

@@ -1889,28 +1890,19 @@ struct server_context {
18891890
if (!system_prompt.empty()) {
18901891
system_tokens = ::llama_tokenize(ctx, system_prompt, true);
18911892

1892-
llama_batch_clear(batch);
1893+
const int32_t n_batch = llama_n_batch(ctx);
1894+
const int32_t n_tokens_prompt = system_tokens.size();
18931895

1894-
for (int i = 0; i < (int)system_tokens.size(); ++i) {
1895-
llama_batch_add(batch, system_tokens[i], i, { 0 }, false);
1896-
}
1896+
for (int32_t i = 0; i < n_tokens_prompt; i += n_batch) {
1897+
const int32_t n_tokens = std::min(n_batch, n_tokens_prompt - i);
18971898

1898-
const int32_t n_batch = llama_n_batch(ctx);
1899+
llama_batch_clear(batch);
18991900

1900-
for (int32_t i = 0; i < batch.n_tokens; i += n_batch) {
1901-
const int32_t n_tokens = std::min(params.n_batch, batch.n_tokens - i);
1902-
llama_batch batch_view = {
1903-
n_tokens,
1904-
batch.token + i,
1905-
nullptr,
1906-
batch.pos + i,
1907-
batch.n_seq_id + i,
1908-
batch.seq_id + i,
1909-
batch.logits + i,
1910-
0, 0, 0, // unused
1911-
};
1901+
for (int32_t j = 0; j < n_tokens; ++j) {
1902+
llama_batch_add(batch, system_tokens[i + j], i + j, { 0 }, false);
1903+
}
19121904

1913-
if (llama_decode(ctx, batch_view) != 0) {
1905+
if (llama_decode(ctx, batch) != 0) {
19141906
LOG_ERROR("llama_decode() failed", {});
19151907
return;
19161908
}
@@ -2114,7 +2106,7 @@ struct server_context {
21142106

21152107
return json {
21162108
{"n_ctx", slot.n_ctx},
2117-
{"n_predict", slot.n_predict},
2109+
{"n_predict", slot.n_predict}, // Server configured n_predict
21182110
{"model", params.model_alias},
21192111
{"seed", slot.sparams.seed},
21202112
{"temperature", slot.sparams.temp},
@@ -2141,7 +2133,7 @@ struct server_context {
21412133
{"mirostat_eta", slot.sparams.mirostat_eta},
21422134
{"penalize_nl", slot.sparams.penalize_nl},
21432135
{"stop", slot.params.antiprompt},
2144-
{"n_predict", slot.params.n_predict}, // TODO: fix duplicate key n_predict
2136+
{"max_tokens", slot.params.n_predict}, // User configured n_predict
21452137
{"n_keep", slot.params.n_keep},
21462138
{"n_discard", slot.params.n_discard},
21472139
{"ignore_eos", ignore_eos},
@@ -2673,6 +2665,8 @@ struct server_context {
26732665
llama_lora_adapters_apply(ctx, lora_adapters);
26742666
server_task_result result;
26752667
result.id = task.id;
2668+
result.stop = true;
2669+
result.error = false;
26762670
result.data = json{{ "success", true }};
26772671
queue_results.send(result);
26782672
} break;
@@ -3619,6 +3613,9 @@ int main(int argc, char ** argv) {
36193613
return 1;
36203614
}
36213615

3616+
// parse arguments from environment variables
3617+
gpt_params_parse_from_env(params);
3618+
36223619
// TODO: not great to use extern vars
36233620
server_log_json = params.log_json;
36243621
server_verbose = params.verbosity > 0;

0 commit comments

Comments
 (0)