Skip to content

Commit cfd4fbb

Browse files
angtmfuntowicz
andauthored
[Backend] Add Llamacpp backend (#2975)
* Add llamacpp backend Signed-off-by: Adrien Gallouët <[email protected]> * Get rid of llama_batch_get_one() Signed-off-by: Adrien Gallouët <[email protected]> * Use max_batch_total_tokens Signed-off-by: Adrien Gallouët <[email protected]> * Handle max_batch_size Signed-off-by: Adrien Gallouët <[email protected]> * Add some input validation checks Signed-off-by: Adrien Gallouët <[email protected]> * Handle ctx args & fix sampling Signed-off-by: Adrien Gallouët <[email protected]> * Add GPU args Signed-off-by: Adrien Gallouët <[email protected]> * Add --defrag-threshold Signed-off-by: Adrien Gallouët <[email protected]> * Add a stupid batch mechanism Signed-off-by: Adrien Gallouët <[email protected]> * Cleanup Signed-off-by: Adrien Gallouët <[email protected]> * Add --numa Signed-off-by: Adrien Gallouët <[email protected]> * Fix args Signed-off-by: Adrien Gallouët <[email protected]> * Enable flash attention by default Signed-off-by: Adrien Gallouët <[email protected]> * Add --offload-kqv Signed-off-by: Adrien Gallouët <[email protected]> * Fix batch_pos Signed-off-by: Adrien Gallouët <[email protected]> * backend(llama): add CUDA Dockerfile_llamacpp for now * Only export the latest logits Signed-off-by: Adrien Gallouët <[email protected]> * Output real logprobs Signed-off-by: Adrien Gallouët <[email protected]> * Fix batching Signed-off-by: Adrien Gallouët <[email protected]> * Fix seq iterations Signed-off-by: Adrien Gallouët <[email protected]> * Auto-detect n_threads when not provided Signed-off-by: Adrien Gallouët <[email protected]> * Clear request cache after completion Signed-off-by: Adrien Gallouët <[email protected]> * Remove warmup Signed-off-by: Adrien Gallouët <[email protected]> * Cleanup Signed-off-by: Adrien Gallouët <[email protected]> * backend(llama): add CUDA architectures build argument for Dockerfile * Add specific args for batch Signed-off-by: Adrien Gallouët <[email protected]> * Add --type-v & --type-k Signed-off-by: Adrien Gallouët <[email protected]> * Bump llamacpp to b4623 Signed-off-by: Adrien Gallouët <[email protected]> * Disable graceful shutdown in debug mode Signed-off-by: Adrien Gallouët <[email protected]> * Update Dockerfile_llamacpp Signed-off-by: Adrien Gallouët <[email protected]> * Cleanup Dockerfile Signed-off-by: Adrien Gallouët <[email protected]> * Update Cargo.lock Signed-off-by: Adrien Gallouët <[email protected]> * Update args Signed-off-by: Adrien Gallouët <[email protected]> * Simplify batching logic Signed-off-by: Adrien Gallouët <[email protected]> * Set TGI_LLAMA_PKG_CUDA from CUDA_VERSION Signed-off-by: Adrien Gallouët <[email protected]> * Rename bindings Signed-off-by: Adrien Gallouët <[email protected]> * Remove n_ctx Signed-off-by: Adrien Gallouët <[email protected]> * Make max_batch_total_tokens optional Signed-off-by: Adrien Gallouët <[email protected]> * Ensure all samplers are freed on error Signed-off-by: Adrien Gallouët <[email protected]> * Initialize penalty_last_n with llamacpp default value Signed-off-by: Adrien Gallouët <[email protected]> * Cleanup Signed-off-by: Adrien Gallouët <[email protected]> * Improve default settings Signed-off-by: Adrien Gallouët <[email protected]> * Add doc Signed-off-by: Adrien Gallouët <[email protected]> * Update docs Signed-off-by: Adrien Gallouët <[email protected]> * Thanks clippy Signed-off-by: Adrien Gallouët <[email protected]> * Thanks cargo fmt Signed-off-by: Adrien Gallouët <[email protected]> * Update docs Signed-off-by: Adrien Gallouët <[email protected]> * Do not use HOSTNAME env Signed-off-by: Adrien Gallouët <[email protected]> * Bump llama.cpp & cuda Signed-off-by: Adrien Gallouët <[email protected]> * Fix requirements.txt Signed-off-by: Adrien Gallouët <[email protected]> * Fix fmt Signed-off-by: Adrien Gallouët <[email protected]> * Enable KQV offload by default Signed-off-by: Adrien Gallouët <[email protected]> * Remove Ngrok tunneling Signed-off-by: Adrien Gallouët <[email protected]> * Remove .cargo/config.toml Signed-off-by: Adrien Gallouët <[email protected]> * Fix Dockerfile Signed-off-by: Adrien Gallouët <[email protected]> * Add missing cuda prefix Signed-off-by: Adrien Gallouët <[email protected]> * Handle custom llama.cpp dir Signed-off-by: Adrien Gallouët <[email protected]> * Cleanup Signed-off-by: Adrien Gallouët <[email protected]> * Add README.md Signed-off-by: Adrien Gallouët <[email protected]> * Add HF transfer Signed-off-by: Adrien Gallouët <[email protected]> * Fix bool args Signed-off-by: Adrien Gallouët <[email protected]> * Update doc Signed-off-by: Adrien Gallouët <[email protected]> * Update doc Signed-off-by: Adrien Gallouët <[email protected]> --------- Signed-off-by: Adrien Gallouët <[email protected]> Co-authored-by: Morgan Funtowicz <[email protected]>
1 parent 6df0fc0 commit cfd4fbb

File tree

12 files changed

+1764
-421
lines changed

12 files changed

+1764
-421
lines changed

Cargo.lock

Lines changed: 504 additions & 421 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"backends/v3",
66
"backends/grpc-metadata",
77
"backends/trtllm",
8+
"backends/llamacpp",
89
"launcher",
910
"router"
1011
]

Dockerfile_llamacpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
FROM nvidia/cuda:12.8.0-cudnn-devel-ubuntu24.04 AS deps
2+
3+
ARG llamacpp_version=b4651
4+
ARG llamacpp_cuda=OFF
5+
ARG cuda_arch=75-real;80-real;86-real;89-real;90-real
6+
7+
WORKDIR /opt/src
8+
9+
ENV DEBIAN_FRONTEND=noninteractive
10+
RUN apt update && apt install -y \
11+
clang \
12+
cmake \
13+
curl \
14+
git \
15+
python3-dev \
16+
libssl-dev \
17+
pkg-config \
18+
tar
19+
20+
ADD https://github.com/ggerganov/llama.cpp/archive/refs/tags/${llamacpp_version}.tar.gz /opt/src/
21+
RUN tar -xzf ${llamacpp_version}.tar.gz \
22+
&& cd llama.cpp-${llamacpp_version} \
23+
&& cmake -B build \
24+
-DCMAKE_INSTALL_PREFIX=/usr \
25+
-DCMAKE_INSTALL_LIBDIR=/usr/lib \
26+
-DCMAKE_C_COMPILER=clang \
27+
-DCMAKE_CXX_COMPILER=clang++ \
28+
-DCMAKE_CUDA_ARCHITECTURES=${cuda_arch} \
29+
-DGGML_CUDA=${llamacpp_cuda} \
30+
-DLLAMA_BUILD_COMMON=OFF \
31+
-DLLAMA_BUILD_TESTS=OFF \
32+
-DLLAMA_BUILD_EXAMPLES=OFF \
33+
-DLLAMA_BUILD_SERVER=OFF \
34+
&& cmake --build build --parallel --config Release \
35+
&& cmake --install build
36+
37+
WORKDIR /app
38+
COPY rust-toolchain.toml rust-toolchain.toml
39+
RUN curl -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --default-toolchain none
40+
ENV PATH="/root/.cargo/bin:$PATH"
41+
RUN cargo install cargo-chef --locked
42+
43+
FROM deps AS planner
44+
COPY . .
45+
RUN cargo chef prepare --recipe-path recipe.json
46+
47+
FROM deps AS builder
48+
COPY --from=planner /app/recipe.json recipe.json
49+
RUN cargo chef cook \
50+
--recipe-path recipe.json \
51+
--profile release-opt \
52+
--package text-generation-router-llamacpp
53+
COPY . .
54+
RUN cargo build \
55+
--profile release-opt \
56+
--package text-generation-router-llamacpp --frozen
57+
58+
FROM nvidia/cuda:12.8.0-cudnn-runtime-ubuntu24.04
59+
60+
RUN apt update && apt install -y \
61+
python3-venv \
62+
python3-pip
63+
64+
RUN python3 -m venv /venv
65+
ENV PATH="/venv/bin:$PATH"
66+
67+
COPY backends/llamacpp/requirements.txt requirements.txt
68+
RUN pip3 install --no-cache-dir -r requirements.txt
69+
70+
COPY --from=builder /usr/lib/libllama.so /usr/lib/
71+
COPY --from=builder /usr/lib/libggml*.so /usr/lib/
72+
COPY --from=builder /app/target/release-opt/text-generation-router-llamacpp /usr/bin/
73+
74+
ENV HF_HUB_ENABLE_HF_TRANSFER=1
75+
76+
ENTRYPOINT ["text-generation-router-llamacpp"]

backends/llamacpp/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "text-generation-router-llamacpp"
3+
version.workspace = true
4+
edition.workspace = true
5+
authors.workspace = true
6+
homepage.workspace = true
7+
8+
[build-dependencies]
9+
bindgen = "0.71.1"
10+
pkg-config = "0.3.31"
11+
12+
[dependencies]
13+
async-trait = "0.1.85"
14+
clap = "4.5.27"
15+
num_cpus = "1.16.0"
16+
text-generation-router = { path = "../../router" }
17+
thiserror = "2.0.11"
18+
tokenizers.workspace = true
19+
tokio = "1.43.0"
20+
tokio-stream = "0.1.17"
21+
tracing = "0.1.41"

backends/llamacpp/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Llamacpp backend
2+
3+
If all your dependencies are installed at the system level, running
4+
cargo build should be sufficient. However, if you want to experiment
5+
with different versions of llama.cpp, some additional setup is required.
6+
7+
## Install llama.cpp
8+
9+
LLAMACPP_PREFIX=$(pwd)/llama.cpp.out
10+
11+
git clone https://github.com/ggerganov/llama.cpp
12+
cd llama.cpp
13+
cmake -B build \
14+
-DCMAKE_INSTALL_PREFIX="$LLAMACPP_PREFIX" \
15+
-DLLAMA_BUILD_COMMON=OFF \
16+
-DLLAMA_BUILD_TESTS=OFF \
17+
-DLLAMA_BUILD_EXAMPLES=OFF \
18+
-DLLAMA_BUILD_SERVER=OFF
19+
cmake --build build --config Release -j
20+
cmake --install build
21+
22+
## Build TGI
23+
24+
PKG_CONFIG_PATH="$LLAMACPP_PREFIX/lib/pkgconfig" cargo build

backends/llamacpp/build.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use bindgen::callbacks::{ItemInfo, ParseCallbacks};
2+
use std::env;
3+
use std::path::PathBuf;
4+
5+
#[derive(Debug)]
6+
struct PrefixStripper;
7+
8+
impl ParseCallbacks for PrefixStripper {
9+
fn generated_name_override(&self, item_info: ItemInfo<'_>) -> Option<String> {
10+
item_info.name.strip_prefix("llama_").map(str::to_string)
11+
}
12+
}
13+
14+
fn main() {
15+
if let Some(cuda_version) = option_env!("CUDA_VERSION") {
16+
let mut version: Vec<&str> = cuda_version.split('.').collect();
17+
if version.len() > 2 {
18+
version.pop();
19+
}
20+
let cuda_version = format!("cuda-{}", version.join("."));
21+
pkg_config::Config::new().probe(&cuda_version).unwrap();
22+
}
23+
let llama = pkg_config::Config::new().probe("llama").unwrap();
24+
25+
for path in &llama.link_paths {
26+
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", path.display());
27+
}
28+
println!("cargo:rustc-link-arg=-Wl,--disable-new-dtags");
29+
30+
let bindings = bindgen::Builder::default()
31+
.clang_args(
32+
llama
33+
.include_paths
34+
.iter()
35+
.map(|p| format!("-I{}", p.display())),
36+
)
37+
.header_contents("llama_bindings.h", "#include <llama.h>")
38+
.prepend_enum_name(false)
39+
.parse_callbacks(Box::new(PrefixStripper))
40+
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
41+
.generate()
42+
.expect("Unable to generate bindings");
43+
44+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
45+
bindings
46+
.write_to_file(out_path.join("llamacpp.rs"))
47+
.expect("Couldn't write bindings!");
48+
}

backends/llamacpp/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
transformers==4.48.2
2+
huggingface-hub==0.28.1
3+
hf-transfer==0.1.9

0 commit comments

Comments
 (0)