Skip to content

Commit c6a9659

Browse files
Merge branch 'abetlen:main' into main
2 parents c2585b6 + de8d9a8 commit c6a9659

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

docker/Dockerfile

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Define the image argument and provide a default value
2+
ARG IMAGE=python:3-slim-bullseye
3+
4+
# Use the image as specified
5+
FROM ${IMAGE}
6+
7+
# Re-declare the ARG after FROM
8+
ARG IMAGE
9+
10+
# Update and upgrade the existing packages
11+
RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends \
12+
python3 \
13+
python3-pip \
14+
ninja-build \
15+
build-essential
16+
17+
RUN python3 -m pip install --upgrade pip pytest cmake scikit-build setuptools fastapi uvicorn sse-starlette
18+
19+
# Perform the conditional installations based on the image
20+
RUN echo "Image: ${IMAGE}" && \
21+
if [ "${IMAGE}" = "python:3-slim-bullseye" ] ; then \
22+
echo "OpenBLAS install:" && \
23+
apt-get install -y --no-install-recommends libopenblas-dev && \
24+
LLAMA_OPENBLAS=1 pip install llama-cpp-python --verbose; \
25+
else \
26+
echo "CuBLAS install:" && \
27+
LLAMA_CUBLAS=1 pip install llama-cpp-python --verbose; \
28+
fi
29+
30+
# Clean up apt cache
31+
RUN rm -rf /var/lib/apt/lists/*
32+
33+
# Set a working directory for better clarity
34+
WORKDIR /app
35+
36+
# Copy files to the app directory
37+
RUN echo "Installing model...this can take some time..."
38+
COPY ./model.bin /app/model.bin
39+
COPY ./start_server.sh /app/start_server.sh
40+
41+
# Make the server start script executable
42+
RUN chmod +x /app/start_server.sh
43+
44+
# Set environment variable for the host
45+
ENV HOST=0.0.0.0
46+
47+
# Expose a port for the server
48+
EXPOSE 8000
49+
50+
# Run the server start script
51+
CMD ["/bin/sh", "/app/start_server.sh"]
File renamed without changes.
File renamed without changes.

docker/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Dockerfiles for building the llama-cpp-python server
2+
- `Dockerfile.openblas_simple` - a simple Dockerfile for non-GPU OpenBLAS
3+
- `Dockerfile.cuda_simple` - a simple Dockerfile for CUDA accelerated CuBLAS
4+
- `hug_model.py` - a Python utility for interactively choosing and downloading the latest `5_1` quantized models from [huggingface.co/TheBloke]( https://huggingface.co/TheBloke)
5+
- `Dockerfile` - a single OpenBLAS and CuBLAS combined Dockerfile that automatically installs a previously downloaded model `model.bin`
6+
7+
# Get model from Hugging Face
8+
`python3 ./hug_model.py`
9+
10+
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.
11+
```
12+
docker $ ls -lh *.bin
13+
-rw-rw-r-- 1 user user 4.8G May 23 18:30 <downloaded-model-file>.q5_1.bin
14+
lrwxrwxrwx 1 user user 24 May 23 18:30 model.bin -> <downloaded-model-file>.q5_1.bin
15+
```
16+
**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
17+
**TWICE** as much disk space as the size of the model:
18+
19+
| Model | Quantized size |
20+
|------:|----------------:|
21+
| 7B | 5 GB |
22+
| 13B | 10 GB |
23+
| 30B | 25 GB |
24+
| 65B | 50 GB |
25+
26+
**Note #2:** If you want to pass or tune additional parameters, customise `./start_server.sh` before running `docker build ...`
27+
28+
# Install Docker Server
29+
30+
**Note #3:** 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!
31+
32+
[Install Docker Engine](https://docs.docker.com/engine/install)
33+
34+
# Use OpenBLAS
35+
Use if you don't have a NVidia GPU. Defaults to `python:3-slim-bullseye` Docker base image and OpenBLAS:
36+
## Build:
37+
`docker build --build-arg -t openblas .`
38+
## Run:
39+
`docker run --cap-add SYS_RESOURCE -t openblas`
40+
41+
# Use CuBLAS
42+
Requires a NVidia GPU with sufficient VRAM (approximately as much as the size above) and Docker NVidia support (see [container-toolkit/install-guide](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html))
43+
## Build:
44+
`docker build --build-arg IMAGE=nvidia/cuda:12.1.1-devel-ubuntu22.04 -t cublas .`
45+
## Run:
46+
`docker run --cap-add SYS_RESOURCE -t cublas`

docker/hug_model.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import requests
2+
import json
3+
import os
4+
import struct
5+
6+
def make_request(url, params=None):
7+
print(f"Making request to {url}...")
8+
response = requests.get(url, params=params)
9+
if response.status_code == 200:
10+
return json.loads(response.text)
11+
else:
12+
print(f"Request failed with status code {response.status_code}")
13+
return None
14+
15+
def check_magic_and_version(filename):
16+
with open(filename, 'rb') as f:
17+
# Read the first 6 bytes from the file
18+
data = f.read(6)
19+
20+
# Unpack the binary data, interpreting the first 4 bytes as a little-endian unsigned int
21+
# and the next 2 bytes as a little-endian unsigned short
22+
magic, version = struct.unpack('<I H', data)
23+
24+
print(f"magic: 0x{magic:08x}, version: 0x{version:04x}, file: {filename}")
25+
26+
return magic, version
27+
28+
def download_file(url, destination):
29+
print(f"Downloading {url} to {destination}...")
30+
response = requests.get(url, stream=True)
31+
if response.status_code == 200:
32+
with open(destination, 'wb') as f:
33+
total_downloaded = 0
34+
for chunk in response.iter_content(chunk_size=1024):
35+
if chunk: # filter out keep-alive new chunks
36+
f.write(chunk)
37+
total_downloaded += len(chunk)
38+
if total_downloaded >= 10485760: # 10 MB
39+
print('.', end='', flush=True)
40+
total_downloaded = 0
41+
print("\nDownload complete.")
42+
43+
# Creating a symbolic link from destination to "model.bin"
44+
if os.path.isfile("model.bin"):
45+
os.remove("model.bin") # remove the existing link if any
46+
os.symlink(destination, "model.bin")
47+
else:
48+
print(f"Download failed with status code {response.status_code}")
49+
50+
def get_user_choice(model_list):
51+
# Print the enumerated list
52+
print("\n")
53+
for i, (model_id, rfilename) in enumerate(model_list):
54+
print(f"{i+1}: Model ID: {model_id}, RFilename: {rfilename}")
55+
56+
# Get user's choice
57+
choice = input("Choose a model to download by entering the corresponding number: ")
58+
try:
59+
index = int(choice) - 1
60+
if 0 <= index < len(model_list):
61+
# Return the chosen model
62+
return model_list[index]
63+
else:
64+
print("Invalid choice.")
65+
except ValueError:
66+
print("Invalid input. Please enter a number corresponding to a model.")
67+
except IndexError:
68+
print("Invalid choice. Index out of range.")
69+
70+
return None
71+
72+
import argparse
73+
74+
def main():
75+
# Create an argument parser
76+
parser = argparse.ArgumentParser(description='Process the model version.')
77+
parser.add_argument('-v', '--version', type=int, default=0x0003,
78+
help='an integer for the version to be used')
79+
80+
# Parse the arguments
81+
args = parser.parse_args()
82+
83+
# Define the parameters
84+
params = {
85+
"author": "TheBloke", # Filter by author
86+
"tags": "llama"
87+
}
88+
89+
models = make_request('https://huggingface.co/api/models', params=params)
90+
if models is None:
91+
return
92+
93+
model_list = []
94+
# Iterate over the models
95+
for model in models:
96+
model_id = model['id']
97+
model_info = make_request(f'https://huggingface.co/api/models/{model_id}')
98+
if model_info is None:
99+
continue
100+
101+
for sibling in model_info.get('siblings', []):
102+
rfilename = sibling.get('rfilename')
103+
if rfilename and 'q5_1' in rfilename:
104+
model_list.append((model_id, rfilename))
105+
106+
model_choice = get_user_choice(model_list)
107+
if model_choice is not None:
108+
model_id, rfilename = model_choice
109+
url = f"https://huggingface.co/{model_id}/resolve/main/{rfilename}"
110+
download_file(url, rfilename)
111+
_, version = check_magic_and_version(rfilename)
112+
if version != args.version:
113+
print(f"Warning: Expected version {args.version}, but found different version in the file.")
114+
115+
if __name__ == '__main__':
116+
main()

docker/start_server.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
# For mmap support
4+
ulimit -l unlimited
5+
6+
if [ "$IMAGE" = "python:3-slim-bullseye" ]; then
7+
python3 -B -m llama_cpp.server --model /app/model.bin
8+
else
9+
# You may have to reduce --n_gpu_layers=1000 to 20 or less if you don't have enough VRAM
10+
python3 -B -m llama_cpp.server --model /app/model.bin --n_gpu_layers=1000
11+
fi

0 commit comments

Comments
 (0)