Skip to content

Commit ac47d55

Browse files
committed
Merge branch 'main' into v0.2-wip
2 parents cf405f6 + 3f8bc41 commit ac47d55

File tree

9 files changed

+208
-124
lines changed

9 files changed

+208
-124
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.1.79]
11+
12+
### Added
13+
14+
- GGUF Support (breaking change requiring new model format)
15+
1016
## [0.1.78]
1117

1218
### Added

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ This package provides:
1717

1818
Documentation is available at [https://llama-cpp-python.readthedocs.io/en/latest](https://llama-cpp-python.readthedocs.io/en/latest).
1919

20+
> [!WARNING]
21+
> Starting with version 0.1.79 the model format has changed from `ggmlv3` to `gguf`. Old model files can be converted using the `convert-llama-ggmlv3-to-gguf.py` script in [`llama.cpp`](https://github.com/ggerganov/llama.cpp)
22+
2023

2124
## Installation from PyPI (recommended)
2225

@@ -201,7 +204,7 @@ This package is under active development and I welcome any contributions.
201204
To get started, clone the repository and install the package in editable / development mode:
202205

203206
```bash
204-
git clone --recurse-submodules git@github.com:abetlen/llama-cpp-python.git
207+
git clone --recurse-submodules https://github.com/abetlen/llama-cpp-python.git
205208
cd llama-cpp-python
206209

207210
# Upgrade pip (required for editable mode)

docker/README.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,55 @@
1-
# Install Docker Server
2-
3-
**Note #1:** This was tested with Docker running on Linux. If you can get it working on Windows or MacOS, please update this `README.md` with a PR!
1+
### Install Docker Server
2+
> [!IMPORTANT]
3+
> This was tested with Docker running on Linux. <br>If you can get it working on Windows or MacOS, please update this `README.md` with a PR!<br>
44
55
[Install Docker Engine](https://docs.docker.com/engine/install)
66

7-
**Note #2:** NVidia GPU CuBLAS support requires a NVidia GPU with sufficient VRAM (approximately as much as the size in the table below) and Docker NVidia support (see [container-toolkit/install-guide](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html))
87

9-
# Simple Dockerfiles for building the llama-cpp-python server with external model bin files
10-
## openblas_simple - a simple Dockerfile for non-GPU OpenBLAS, where the model is located outside the Docker image
8+
## Simple Dockerfiles for building the llama-cpp-python server with external model bin files
9+
### openblas_simple
10+
A simple Dockerfile for non-GPU OpenBLAS, where the model is located outside the Docker image:
1111
```
1212
cd ./openblas_simple
1313
docker build -t openblas_simple .
14-
docker run -e USE_MLOCK=0 -e MODEL=/var/model/<model-path> -v <model-root-path>:/var/model -t openblas_simple
14+
docker run --cap-add SYS_RESOURCE -e USE_MLOCK=0 -e MODEL=/var/model/<model-path> -v <model-root-path>:/var/model -t openblas_simple
1515
```
1616
where `<model-root-path>/<model-path>` is the full path to the model file on the Docker host system.
1717

18-
## cuda_simple - a simple Dockerfile for CUDA accelerated CuBLAS, where the model is located outside the Docker image
18+
### cuda_simple
19+
> [!WARNING]
20+
> Nvidia GPU CuBLAS support requires an Nvidia GPU with sufficient VRAM (approximately as much as the size in the table below) and Docker Nvidia support (see [container-toolkit/install-guide](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html)) <br>
21+
22+
A simple Dockerfile for CUDA-accelerated CuBLAS, where the model is located outside the Docker image:
23+
1924
```
2025
cd ./cuda_simple
2126
docker build -t cuda_simple .
22-
docker run -e USE_MLOCK=0 -e MODEL=/var/model/<model-path> -v <model-root-path>:/var/model -t cuda_simple
27+
docker run --gpus=all --cap-add SYS_RESOURCE -e USE_MLOCK=0 -e MODEL=/var/model/<model-path> -v <model-root-path>:/var/model -t cuda_simple
2328
```
2429
where `<model-root-path>/<model-path>` is the full path to the model file on the Docker host system.
2530

26-
# "Open-Llama-in-a-box"
27-
## Download an Apache V2.0 licensed 3B paramter Open Llama model and install into a Docker image that runs an OpenBLAS-enabled llama-cpp-python server
31+
--------------------------------------------------------------------------
32+
33+
### "Open-Llama-in-a-box"
34+
Download an Apache V2.0 licensed 3B params Open LLaMA model and install into a Docker image that runs an OpenBLAS-enabled llama-cpp-python server:
2835
```
2936
$ cd ./open_llama
3037
./build.sh
3138
./start.sh
3239
```
3340

34-
# Manually choose your own Llama model from Hugging Face
41+
### Manually choose your own Llama model from Hugging Face
3542
`python3 ./hug_model.py -a TheBloke -t llama`
3643
You should now have a model in the current directory and `model.bin` symlinked to it for the subsequent Docker build and copy step. e.g.
3744
```
3845
docker $ ls -lh *.bin
3946
-rw-rw-r-- 1 user user 4.8G May 23 18:30 <downloaded-model-file>q5_1.bin
4047
lrwxrwxrwx 1 user user 24 May 23 18:30 model.bin -> <downloaded-model-file>q5_1.bin
4148
```
42-
**Note #1:** Make sure you have enough disk space to download the model. As the model is then copied into the image you will need at least
43-
**TWICE** as much disk space as the size of the model:
49+
50+
> [!NOTE]
51+
> Make sure you have enough disk space to download the model. As the model is then copied into the image you will need at least
52+
**TWICE** as much disk space as the size of the model:<br>
4453
4554
| Model | Quantized size |
4655
|------:|----------------:|
@@ -50,17 +59,6 @@ lrwxrwxrwx 1 user user 24 May 23 18:30 model.bin -> <downloaded-model-file>q5_
5059
| 33B | 25 GB |
5160
| 65B | 50 GB |
5261

53-
**Note #2:** If you want to pass or tune additional parameters, customise `./start_server.sh` before running `docker build ...`
54-
55-
## Use OpenBLAS
56-
Use if you don't have a NVidia GPU. Defaults to `python:3-slim-bullseye` Docker base image and OpenBLAS:
57-
### Build:
58-
`docker build -t openblas .`
59-
### Run:
60-
`docker run --cap-add SYS_RESOURCE -t openblas`
6162

62-
## Use CuBLAS
63-
### Build:
64-
`docker build --build-arg IMAGE=nvidia/cuda:12.1.1-devel-ubuntu22.04 -t cublas .`
65-
### Run:
66-
`docker run --cap-add SYS_RESOURCE -t cublas`
63+
> [!NOTE]
64+
> If you want to pass or tune additional parameters, customise `./start_server.sh` before running `docker build ...`

docker/cuda_simple/Dockerfile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ FROM nvidia/cuda:${CUDA_IMAGE}
44
# We need to set the host to 0.0.0.0 to allow outside access
55
ENV HOST 0.0.0.0
66

7+
RUN apt-get update && apt-get upgrade -y \
8+
&& apt-get install -y git build-essential \
9+
python3 python3-pip gcc wget \
10+
ocl-icd-opencl-dev opencl-headers clinfo \
11+
libclblast-dev libopenblas-dev \
12+
&& mkdir -p /etc/OpenCL/vendors && echo "libnvidia-opencl.so.1" > /etc/OpenCL/vendors/nvidia.icd
13+
714
COPY . .
815

9-
# Install the package
10-
RUN apt update && apt install -y python3 python3-pip
16+
# setting build related env vars
17+
ENV CUDA_DOCKER_ARCH=all
18+
ENV LLAMA_CUBLAS=1
19+
20+
# Install depencencies
1121
RUN python3 -m pip install --upgrade pip pytest cmake scikit-build setuptools fastapi uvicorn sse-starlette pydantic-settings
1222

13-
RUN LLAMA_CUBLAS=1 pip install llama-cpp-python
23+
# Install llama-cpp-python (build with cuda)
24+
RUN CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python
1425

1526
# Run the server
1627
CMD python3 -m llama_cpp.server

llama_cpp/llama.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def __init__(
234234
rope_freq_scale: float = 1.0,
235235
n_gqa: Optional[int] = None, # (TEMPORARY) must be 8 for llama2 70b
236236
rms_norm_eps: Optional[float] = None, # (TEMPORARY)
237-
mul_mat_q: Optional[bool] = None, # (TEMPORARY)
237+
mul_mat_q: Optional[bool] = None,
238238
verbose: bool = True,
239239
):
240240
"""Load a llama.cpp model from `model_path`.
@@ -297,11 +297,6 @@ def __init__(
297297
self.params.rope_freq_base = rope_freq_base
298298
self.params.rope_freq_scale = rope_freq_scale
299299

300-
if n_gqa is not None:
301-
self.params.n_gqa = n_gqa
302-
303-
if rms_norm_eps is not None:
304-
self.params.rms_norm_eps = rms_norm_eps
305300

306301
if mul_mat_q is not None:
307302
self.params.mul_mat_q = mul_mat_q
@@ -420,11 +415,11 @@ def tokenize(self, text: bytes, add_bos: bool = True) -> List[int]:
420415
Returns:
421416
A list of tokens.
422417
"""
423-
assert self.ctx is not None
418+
assert self.model is not None
424419
n_ctx = self._n_ctx
425420
tokens = (llama_cpp.llama_token * n_ctx)()
426-
n_tokens = llama_cpp.llama_tokenize(
427-
self.ctx,
421+
n_tokens = llama_cpp.llama_tokenize_with_model(
422+
self.model,
428423
text,
429424
tokens,
430425
llama_cpp.c_int(n_ctx),
@@ -433,8 +428,8 @@ def tokenize(self, text: bytes, add_bos: bool = True) -> List[int]:
433428
if n_tokens < 0:
434429
n_tokens = abs(n_tokens)
435430
tokens = (llama_cpp.llama_token * n_tokens)()
436-
n_tokens = llama_cpp.llama_tokenize(
437-
self.ctx,
431+
n_tokens = llama_cpp.llama_tokenize_with_model(
432+
self.model,
438433
text,
439434
tokens,
440435
llama_cpp.c_int(n_tokens),
@@ -455,17 +450,19 @@ def detokenize(self, tokens: List[int]) -> bytes:
455450
Returns:
456451
The detokenized string.
457452
"""
458-
assert self.ctx is not None
453+
assert self.model is not None
459454
output = b""
460-
buffer_size = 32
461-
buffer = (ctypes.c_char * buffer_size)()
455+
size = 8
456+
buffer = (ctypes.c_char * size)()
462457
for token in tokens:
463-
n = llama_cpp.llama_token_to_str(
464-
self.ctx, llama_cpp.llama_token(token), buffer, buffer_size
458+
n = llama_cpp.llama_token_to_str_with_model(
459+
self.model, llama_cpp.llama_token(token), buffer, size
465460
)
466-
assert n <= buffer_size
461+
assert n <= size
467462
output += bytes(buffer[:n])
468-
return output
463+
# NOTE: Llama1 models automatically added a space at the start of the prompt
464+
# this line removes a leading space if the first token is a beginning of sentence token
465+
return output[1:] if len(tokens) > 0 and tokens[0] == self.token_bos() else output
469466

470467
def set_cache(self, cache: Optional[BaseLlamaCache]):
471468
"""Set the cache.
@@ -892,7 +889,7 @@ def _create_completion(
892889
created: int = int(time.time())
893890
completion_tokens: List[int] = []
894891
# Add blank space to start of prompt to match OG llama tokenizer
895-
prompt_tokens: List[int] = self.tokenize(b" " + prompt.encode("utf-8"))
892+
prompt_tokens: List[int] = self.tokenize(prompt.encode("utf-8")) if prompt != "" else [self.token_bos()]
896893
text: bytes = b""
897894
returned_tokens: int = 0
898895
stop = (
@@ -1590,13 +1587,7 @@ def __getstate__(self):
15901587
lora_base=self.lora_base,
15911588
lora_path=self.lora_path,
15921589
tensor_split=self.tensor_split,
1593-
### TEMPORARY ###
1594-
n_gqa=self.params.n_gqa,
1595-
rms_norm_eps=self.params.rms_norm_eps,
1596-
### TEMPORARY ###
1597-
### DEPRECATED ###
1598-
n_parts=self.n_parts,
1599-
### DEPRECATED ###
1590+
mul_mat_q=self.params.mul_mat_q,
16001591
)
16011592

16021593
def __setstate__(self, state):
@@ -1618,14 +1609,8 @@ def __setstate__(self, state):
16181609
lora_base=state["lora_base"],
16191610
lora_path=state["lora_path"],
16201611
tensor_split=state["tensor_split"],
1612+
mul_mat_q=state["mul_mat_q"],
16211613
verbose=state["verbose"],
1622-
### TEMPORARY ###
1623-
n_gqa=state["n_gqa"],
1624-
rms_norm_eps=state["rms_norm_eps"],
1625-
### TEMPORARY ###
1626-
### DEPRECATED ###
1627-
n_parts=state["n_parts"],
1628-
### DEPRECATED ###
16291614
)
16301615

16311616
def save_state(self) -> LlamaState:

0 commit comments

Comments
 (0)