Skip to content

Commit

Permalink
build process
Browse files Browse the repository at this point in the history
  • Loading branch information
mtasic85 committed Jul 9, 2024
1 parent 25f11da commit d375372
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 35 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ Python binding for llama.cpp using cffi
## Build

```bash
#
# setup venv
#
python -m venv venv
source venv/bin/activate
pip install poetry
python -B examples/demo0.py

huggingface-cli download bartowski/Phi-3.1-mini-4k-instruct-GGUF Phi-3.1-mini-4k-instruct-Q4_K_M.gguf
huggingface-cli download bartowski/Phi-3.1-mini-128k-instruct-GGUF Phi-3.1-mini-128k-instruct-Q4_K_M.gguf
huggingface-cli download IndexTeam/Index-1.9B-Chat-GGUF ggml-model-Q4_K_M.gguf
huggingface-cli download mradermacher/dolphin-2.9.3-qwen2-1.5b-GGUF dolphin-2.9.3-qwen2-1.5b.Q4_K_M.gguf
huggingface-cli download mradermacher/dolphin-2.9.3-qwen2-0.5b-GGUF dolphin-2.9.3-qwen2-0.5b.Q4_K_M.gguf
huggingface-cli download mradermacher/dolphin-2.9.3-llama-3-8b-GGUF dolphin-2.9.3-llama-3-8b.Q4_K_M.gguf
huggingface-cli download NousResearch/Hermes-2-Pro-Llama-3-8B-GGUF Hermes-2-Pro-Llama-3-8B-Q4_K_M.gguf
#
# build step-by-step
#
# poetry run build-clean
# poetry run build-libllama-cli-shared
# poetry run build-libllama-cli-static
# poetry run build-llama-cli-cffi-static

CXXFLAGS="-DSHARED_LIB" LDFLAGS="-shared -o libllama-cli.so" make -j llama-cli
#
# build
#
poetry run build-all


#
# run demos
#
python -B examples/demo_cffi.py
python -B examples/demo_ctypes.py
```
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ huggingface-hub = "^0.23.4"
cffi = "^1.16.0"

[tool.poetry.scripts]
build-all = "scripts.build_all:build"
build-clean = "scripts.build_clean:clean"
build-libllama-cli-shared = "scripts.build_libllama_cli:build_shared"
build-libllama-cli-static = "scripts.build_libllama_cli:build_static"
build-llama-cli-cffi-static = "scripts.build_llama_cli_cffi:build_static"
Expand Down
16 changes: 0 additions & 16 deletions scripts/build.py

This file was deleted.

8 changes: 8 additions & 0 deletions scripts/build_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import subprocess


def build():
subprocess.run(['poetry', 'run', 'build-clean'])
subprocess.run(['poetry', 'run', 'build-libllama-cli-shared'])
subprocess.run(['poetry', 'run', 'build-libllama-cli-static'])
subprocess.run(['poetry', 'run', 'build-llama-cli-cffi-static'])
7 changes: 7 additions & 0 deletions scripts/build_clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import subprocess


def clean():
# subprocess.run(['rm', '-v', 'llama/*.so'], check=True, shell=True)
subprocess.run('ls -l llama/*.so', check=True, shell=True)
subprocess.run(["ls", "-l", "/dev/null"], check=True, shell=True)
18 changes: 8 additions & 10 deletions scripts/build_libllama_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ def build_shared():
env['CXXFLAGS'] = '-DSHARED_LIB'
env['LDFLAGS'] = '-shared -o libllama-cli.so'

subprocess.run(['rm', '-rf', 'llama.cpp'])
subprocess.run(['rm', '-rf', 'llama/libllama-cli.so'])
subprocess.run(['git', 'clone', 'https://github.com/ggerganov/llama.cpp.git'])
subprocess.run(['patch', 'llama.cpp/examples/main/main.cpp', 'main_shared_library_0.patch'])
subprocess.run(['rm', '-rf', 'llama.cpp'], check=True)
subprocess.run(['git', 'clone', 'https://github.com/ggerganov/llama.cpp.git'], check=True)
subprocess.run(['patch', 'llama.cpp/examples/main/main.cpp', 'main_shared_library_0.patch'], check=True)
subprocess.run(['make', '-C', 'llama.cpp', '-j', 'llama-cli'], check=True, env=env)

shutil.copy("llama.cpp/libllama-cli.so", "llama/libllama-cli.so")
shutil.copy("llama.cpp/libllama-cli.so", "llama/libllama-cli.so", check=True)


def build_static():
env = os.environ.copy()
env['CXXFLAGS'] = '-DSHARED_LIB'

subprocess.run(['rm', '-rf', 'llama.cpp'])
subprocess.run(['rm', '-rf', 'llama/libllama-cli.a'])
subprocess.run(['git', 'clone', 'https://github.com/ggerganov/llama.cpp.git'])
subprocess.run(['patch', 'llama.cpp/examples/main/main.cpp', 'main_shared_library_0.patch'])
subprocess.run(['patch', 'llama.cpp/Makefile', 'makefile_static_library_0.patch'])
subprocess.run(['rm', '-rf', 'llama.cpp'], check=True)
subprocess.run(['git', 'clone', 'https://github.com/ggerganov/llama.cpp.git'], check=True)
subprocess.run(['patch', 'llama.cpp/examples/main/main.cpp', 'main_shared_library_0.patch'], check=True)
subprocess.run(['patch', 'llama.cpp/Makefile', 'makefile_static_library_0.patch'], check=True)
subprocess.run(['make', '-C', 'llama.cpp', '-j', 'llama-cli-a', 'GGML_NO_OPENMP=1', 'GGML_NO_LLAMAFILE=1'], check=True, env=env)
3 changes: 3 additions & 0 deletions scripts/build_llama_cli_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@


def build_static():
subprocess.run(['rm', '-rf', 'llama.cpp'], check=True)
subprocess.run(['git', 'clone', 'https://github.com/ggerganov/llama.cpp.git'], check=True)

ffibuilder.compile(tmpdir="build", verbose=True)

for file in glob.glob('build/*.so'):
Expand Down

0 comments on commit d375372

Please sign in to comment.