Skip to content

Commit

Permalink
unified build.py for cffi and ctypes
Browse files Browse the repository at this point in the history
  • Loading branch information
mtasic85 committed Jul 10, 2024
1 parent 3ddd5b0 commit 79b20e7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ psutil = "^6.0.0"
cibuildwheel = "^2.19.2"

[tool.poetry.scripts]
clean = "scripts.build_clean:clean"
build = "scripts.build:build"
clean = "scripts.clean:clean"

[tool.poetry.build]
script = "scripts/build_all.py"
script = "scripts/build.py"

[tool.cibuildwheel]
build-frontend = "build"
Expand Down
80 changes: 80 additions & 0 deletions scripts/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import os
import glob
import shutil
import subprocess

from cffi import FFI

from clean import clean

ffibuilder = FFI()

ffibuilder.cdef('''
void llama_set_stdout(FILE* f);
void llama_set_stderr(FILE* f);
void llama_set_fprintf(int (*func)(FILE*, const char* format, ...));
void llama_set_fflush(int (*func)(FILE*));
const char* llama_get_metadata_as_json(int argc, char ** argv);
void llama_free_metadata_as_json(const char * c_output);
int llama_cli_main(int argc, char ** argv);
''')

ffibuilder.set_source(
'_llama_cli',
'''
#include <stdio.h>
void llama_set_stdout(FILE* f);
void llama_set_stderr(FILE* f);
void llama_set_fprintf(int (*func)(FILE*, const char* format, ...));
void llama_set_fflush(int (*func)(FILE*));
const char* llama_get_metadata_as_json(int argc, char ** argv);
void llama_free_metadata_as_json(const char * c_output);
int llama_cli_main(int argc, char ** argv);
''',
libraries=['stdc++'],
extra_objects=['../llama.cpp/libllama-cli.a'],
)


def build(*args, **kwargs):
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_1.patch'], check=True)
subprocess.run(['patch', 'llama.cpp/Makefile', 'makefile_static_library_0.patch'], check=True)

# cffi
env = os.environ.copy()
env['CXXFLAGS'] = '-DSHARED_LIB'

if 'PYODIDE' in env and env['PYODIDE'] == '1':
env['CXXFLAGS'] += ' -msimd128 -fno-rtti -DNDEBUG -flto=full -s INITIAL_MEMORY=2GB -s MAXIMUM_MEMORY=4GB -s ALLOW_MEMORY_GROWTH '
env['UNAME_M'] = 'wasm'

subprocess.run(['make', '-C', 'llama.cpp', '-j', 'llama-cli-a', 'GGML_NO_OPENMP=1', 'GGML_NO_LLAMAFILE=1'], check=True, env=env)
ffibuilder.compile(tmpdir='build', verbose=True)

# ctypes
env = os.environ.copy()
env['CXXFLAGS'] = '-DSHARED_LIB'
env['LDFLAGS'] = '-shared -o libllama-cli.so'

if 'PYODIDE' in env and env['PYODIDE'] == '1':
env['CXXFLAGS'] += ' -msimd128 -fno-rtti -DNDEBUG -flto=full -s INITIAL_MEMORY=2GB -s MAXIMUM_MEMORY=4GB -s ALLOW_MEMORY_GROWTH '
env['UNAME_M'] = 'wasm'

subprocess.run(['make', '-C', 'llama.cpp', '-j', 'llama-cli', 'GGML_NO_OPENMP=1', 'GGML_NO_LLAMAFILE=1'], check=True, env=env)

for file in glob.glob('build/*.so') + glob.glob('llama.cpp/*.so'):
shutil.move(file, 'llama/')

for file in glob.glob('build/*.dll') + glob.glob('llama.cpp/*.dll'):
shutil.move(file, 'llama/')

for file in glob.glob('build/*.dylib') + glob.glob('llama.cpp/*.dylib'):
shutil.move(file, 'llama/')


if __name__ == '__main__':
clean()
build()
File renamed without changes.

0 comments on commit 79b20e7

Please sign in to comment.