-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
168 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# llama-cpp-cffi | ||
|
||
## Build | ||
|
||
```bash | ||
# | ||
# setup venv | ||
# | ||
python -m venv venv | ||
source venv/bin/activate | ||
pip install poetry | ||
|
||
# x86_64 | ||
poetry run cibuildwheel --output-dir wheelhouse --platform linux --arch x86_64 . | ||
|
||
# aarch64 | ||
docker run --rm --privileged linuxkit/binfmt:v0.8 | ||
poetry run cibuildwheel --output-dir wheelhouse --platform linux --arch aarch64 . | ||
|
||
# pyodide, pyscript, wasm (NOTE: cannot be published to PyPI) | ||
# poetry run cibuildwheel --output-dir wheelhouse --platform pyodide . | ||
|
||
# | ||
# publish | ||
# | ||
poetry publish --dist-dir wheelhouse | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,73 @@ | ||
# llama-cpp-cffi | ||
|
||
Python binding for llama.cpp using cffi | ||
<!-- | ||
[![Build][build-image]]() | ||
[![Status][status-image]][pypi-project-url] | ||
[![Stable Version][stable-ver-image]][pypi-project-url] | ||
[![Coverage][coverage-image]]() | ||
[![Python][python-ver-image]][pypi-project-url] | ||
[![License][mit-image]][mit-url] | ||
--> | ||
[![Downloads](https://img.shields.io/pypi/dm/llama-cli-cffi)](https://pypistats.org/packages/llama-cli-cffi) | ||
[![Supported Versions](https://img.shields.io/pypi/pyversions/llama-cli-cffi)](https://pypi.org/project/llama-cli-cffi) | ||
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT) | ||
|
||
## Build | ||
**Python** binding for [llama.cpp](https://github.com/ggerganov/llama.cpp) using **cffi** and **ctypes**. Supports **CPU** and **CUDA 12.5** execution. | ||
|
||
## Install | ||
|
||
```bash | ||
# | ||
# setup venv | ||
# | ||
python -m venv venv | ||
source venv/bin/activate | ||
pip install poetry | ||
pip install llama-cli-cffi | ||
``` | ||
|
||
# | ||
# build | ||
# | ||
## Example | ||
|
||
# x86_64 | ||
poetry run cibuildwheel --output-dir wheelhouse --platform linux --arch x86_64 . | ||
```python | ||
from llama.llama_cli_cffi_cpu import llama_generate, Model, Options | ||
# from llama.llama_cli_cffi_cuda_12_5 import llama_generate, Model, Options | ||
# from llama.llama_cli_ctypes_cuda import llama_generate, Model, Options | ||
# from llama.llama_cli_ctypes_cuda_12_5 import llama_generate, Model, Options | ||
|
||
# aarch64 | ||
docker run --rm --privileged linuxkit/binfmt:v0.8 | ||
poetry run cibuildwheel --output-dir wheelhouse --platform linux --arch aarch64 . | ||
from llama.formatter import get_config | ||
|
||
# pyodide, pyscript, wasm (NOTE: cannot be published to PyPI) | ||
# poetry run cibuildwheel --output-dir wheelhouse --platform pyodide . | ||
model = Model( | ||
'TinyLlama/TinyLlama-1.1B-Chat-v1.0', | ||
'TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF', | ||
'tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf', | ||
) | ||
|
||
# | ||
# publish | ||
# | ||
poetry publish --dist-dir wheelhouse | ||
config = get_config(model.creator_hf_repo) | ||
|
||
messages = [ | ||
{'role': 'system', 'content': 'You are a helpful assistant.'}, | ||
{'role': 'user', 'content': 'Evaluate 1 + 2 in Python.'}, | ||
] | ||
|
||
options = Options( | ||
ctx_size=config.max_position_embeddings, | ||
predict=-2, | ||
model=model, | ||
prompt=messages, | ||
) | ||
|
||
for chunk in llama_generate(options): | ||
print(chunk, flush=True, end='') | ||
|
||
# newline | ||
print() | ||
``` | ||
|
||
## Demos | ||
|
||
```BASH | ||
# | ||
# run demos | ||
# | ||
python -B examples/demo_cffi.py | ||
python -B examples/demo_cffi_cpu.py | ||
python -B examples/demo_cffi_cuda_12_5.py | ||
|
||
python -B examples/demo_ctypes_cpu.py | ||
python -B examples/demo_ctypes_cuda_12_5.py | ||
python -m http.server -d examples/demo_pyonide -b "0.0.0.0" 5000 | ||
``` | ||
|
||
```bash | ||
make -j llama-cli-shared llama-cli-static GGML_NO_OPENMP=1 GGML_NO_LLAMAFILE=1 | ||
``` | ||
# python -m http.server -d examples/demo_pyonide -b "0.0.0.0" 5000 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from llama.llama_cli_cffi_cpu import llama_generate, Model, Options | ||
from llama.formatter import get_config | ||
|
||
model = Model( | ||
'TinyLlama/TinyLlama-1.1B-Chat-v1.0', | ||
'TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF', | ||
'tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf', | ||
) | ||
|
||
config = get_config(model.creator_hf_repo) | ||
|
||
messages = [ | ||
{'role': 'system', 'content': 'You are a helpful assistant.'}, | ||
{'role': 'user', 'content': 'Evaluate 1 + 2 in Python.'}, | ||
] | ||
|
||
options = Options( | ||
ctx_size=config.max_position_embeddings, | ||
predict=-2, | ||
model=model, | ||
prompt=messages, | ||
) | ||
|
||
for chunk in llama_generate(options): | ||
print(chunk, flush=True, end='') | ||
|
||
# newline | ||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# import os | ||
# import sys | ||
# sys.path.append(os.path.abspath('.')) | ||
|
||
from llama.llama_cli_cffi_cuda_12_5 import llama_generate, Model, Options | ||
from llama.formatter import get_config | ||
|
||
from demo_models import models | ||
|
||
|
||
def demo_model(model: Model, messages: list[dict]): | ||
config = get_config(model.creator_hf_repo) | ||
|
||
options = Options( | ||
ctx_size=32 * 1024 if model.creator_hf_repo == 'microsoft/Phi-3-mini-128k-instruct' else config.max_position_embeddings, | ||
predict=-2, | ||
gpu_layers=19 if model.creator_hf_repo == 'microsoft/Phi-3-mini-128k-instruct' else 99, | ||
# log_disable=False, | ||
model=model, | ||
prompt=messages, | ||
) | ||
|
||
for chunk in llama_generate(options): | ||
print(chunk, flush=True, end='') | ||
|
||
print() | ||
|
||
|
||
if __name__ == '__main__': | ||
messages = [ | ||
{'role': 'system', 'content': 'You are a helpful assistant.'}, | ||
{'role': 'user', 'content': 'Evaluate 1 + 2 in Python.'}, | ||
] | ||
|
||
for model in models: | ||
config = get_config(model.creator_hf_repo) | ||
print(f'{model = }, {config.max_position_embeddings = }') | ||
demo_model(model, messages) | ||
print('-' * 80) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters