regenerate.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Generates bindings for the ggml library.
  2. #
  3. # cffi requires prior C preprocessing of the headers, and it uses pycparser which chokes on a couple of things
  4. # so we help it a bit (e.g. replace sizeof expressions with their value, remove exotic syntax found in Darwin headers).
  5. import os, sys, re, subprocess
  6. import cffi
  7. from stubs import generate_stubs
  8. API = os.environ.get('API', 'api.h')
  9. CC = os.environ.get('CC') or 'gcc'
  10. C_INCLUDE_DIR = os.environ.get('C_INCLUDE_DIR', '../../../llama.cpp')
  11. CPPFLAGS = [
  12. "-I", C_INCLUDE_DIR,
  13. '-D__fp16=uint16_t', # pycparser doesn't support __fp16
  14. '-D__attribute__(x)=',
  15. '-D_Static_assert(x, m)=',
  16. ] + [x for x in os.environ.get('CPPFLAGS', '').split(' ') if x != '']
  17. try: header = subprocess.run([CC, "-E", *CPPFLAGS, API], capture_output=True, text=True, check=True).stdout
  18. except subprocess.CalledProcessError as e: print(f'{e.stderr}\n{e}', file=sys.stderr); raise
  19. header = '\n'.join([l for l in header.split('\n') if '__darwin_va_list' not in l]) # pycparser hates this
  20. # Replace constant size expressions w/ their value (compile & run a mini exe for each, because why not).
  21. # First, extract anyting *inside* square brackets and anything that looks like a sizeof call.
  22. for expr in set(re.findall(f'(?<=\\[)[^\\]]+(?=])|sizeof\\s*\\([^()]+\\)', header)):
  23. if re.match(r'^(\d+|\s*)$', expr): continue # skip constants and empty bracket contents
  24. subprocess.run([CC, "-o", "eval_size_expr", *CPPFLAGS, "-x", "c", "-"], text=True, check=True,
  25. input=f'''#include <stdio.h>
  26. #include "{API}"
  27. int main() {{ printf("%lu", (size_t)({expr})); }}''')
  28. size = subprocess.run(["./eval_size_expr"], capture_output=True, text=True, check=True).stdout
  29. print(f'Computed constexpr {expr} = {size}')
  30. header = header.replace(expr, size)
  31. ffibuilder = cffi.FFI()
  32. ffibuilder.cdef(header)
  33. ffibuilder.set_source(f'ggml.cffi', None) # we're not compiling a native extension, as this quickly gets hairy
  34. ffibuilder.compile(verbose=True)
  35. with open("ggml/__init__.pyi", "wt") as f:
  36. f.write(generate_stubs(header))