ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / python / README.md
| 1 | # Simple autogenerated Python bindings for ggml |
| 2 | |
| 3 | This folder contains: |
| 4 | |
| 5 | - Scripts to generate full Python bindings from ggml headers (+ stubs for autocompletion in IDEs) |
| 6 | - Some barebones utils (see [ggml/utils.py](./ggml/utils.py)): |
| 7 | - `ggml.utils.init` builds a context that's freed automatically when the pointer gets GC'd |
| 8 | - `ggml.utils.copy` **copies between same-shaped tensors (numpy or ggml), w/ automatic (de/re)quantization** |
| 9 | - `ggml.utils.numpy` returns a numpy view over a ggml tensor; if it's quantized, it returns a copy (requires `allow_copy=True`) |
| 10 | - Very basic examples (anyone wants to port [llama2.c](https://github.com/karpathy/llama2.c)?) |
| 11 | |
| 12 | Provided you set `GGML_LIBRARY=.../path/to/libggml_shared.so` (see instructions below), it's trivial to do some operations on quantized tensors: |
| 13 | |
| 14 | ```python |
| 15 | # Make sure libllama.so is in your [DY]LD_LIBRARY_PATH, or set GGML_LIBRARY=.../libggml_shared.so |
| 16 | |
| 17 | from ggml import lib, ffi |
| 18 | from ggml.utils import init, copy, numpy |
| 19 | import numpy as np |
| 20 | |
| 21 | ctx = init(mem_size=12*1024*1024) |
| 22 | n = 256 |
| 23 | n_threads = 4 |
| 24 | |
| 25 | a = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_Q5_K, n) |
| 26 | b = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, n) # Can't both be quantized |
| 27 | sum = lib.ggml_add(ctx, a, b) # all zeroes for now. Will be quantized too! |
| 28 | |
| 29 | gf = ffi.new('struct ggml_cgraph*') |
| 30 | lib.ggml_build_forward_expand(gf, sum) |
| 31 | |
| 32 | copy(np.array([i for i in range(n)], np.float32), a) |
| 33 | copy(np.array([i*100 for i in range(n)], np.float32), b) |
| 34 | |
| 35 | lib.ggml_graph_compute_with_ctx(ctx, gf, n_threads) |
| 36 | |
| 37 | print(numpy(a, allow_copy=True)) |
| 38 | # 0. 1.0439453 2.0878906 3.131836 4.1757812 5.2197266. ... |
| 39 | print(numpy(b)) |
| 40 | # 0. 100. 200. 300. 400. 500. ... |
| 41 | print(numpy(sum, allow_copy=True)) |
| 42 | # 0. 105.4375 210.875 316.3125 421.75 527.1875 ... |
| 43 | ``` |
| 44 | |
| 45 | ### Prerequisites |
| 46 | |
| 47 | You'll need a shared library of ggml to use the bindings. |
| 48 | |
| 49 | #### Build libggml_shared.so or libllama.so |
| 50 | |
| 51 | As of this writing the best is to use [ggerganov/llama.cpp](https://github.com/ggerganov/llama.cpp)'s generated `libggml_shared.so` or `libllama.so`, which you can build as follows: |
| 52 | |
| 53 | ```bash |
| 54 | git clone https://github.com/ggerganov/llama.cpp |
| 55 | # On a CUDA-enabled system add -DLLAMA_CUDA=1 |
| 56 | # On a Mac add -DLLAMA_METAL=1 |
| 57 | cmake llama.cpp \ |
| 58 | -B llama_build \ |
| 59 | -DCMAKE_C_FLAGS=-Ofast \ |
| 60 | -DLLAMA_NATIVE=1 \ |
| 61 | -DLLAMA_LTO=1 \ |
| 62 | -DBUILD_SHARED_LIBS=1 \ |
| 63 | -DLLAMA_MPI=1 \ |
| 64 | -DLLAMA_BUILD_TESTS=0 \ |
| 65 | -DLLAMA_BUILD_EXAMPLES=0 |
| 66 | ( cd llama_build && make -j ) |
| 67 | |
| 68 | # On Mac, this will be libggml_shared.dylib instead |
| 69 | export GGML_LIBRARY=$PWD/llama_build/libggml_shared.so |
| 70 | # Alternatively, you can just copy it to your system's lib dir, e.g /usr/local/lib |
| 71 | ``` |
| 72 | |
| 73 | #### (Optional) Regenerate the bindings and stubs |
| 74 | |
| 75 | If you added or changed any signatures of the C API, you'll want to regenerate the bindings ([ggml/cffi.py](./ggml/cffi.py)) and stubs ([ggml/__init__.pyi](./ggml/__init__.pyi)). |
| 76 | |
| 77 | Luckily it's a one-liner using [regenerate.py](./regenerate.py): |
| 78 | |
| 79 | ```bash |
| 80 | pip install -q cffi |
| 81 | |
| 82 | python regenerate.py |
| 83 | ``` |
| 84 | |
| 85 | By default it assumes `llama.cpp` was cloned in ../../../llama.cpp (alongside the ggml folder). You can override this with: |
| 86 | |
| 87 | ```bash |
| 88 | C_INCLUDE_DIR=$LLAMA_CPP_DIR python regenerate.py |
| 89 | ``` |
| 90 | |
| 91 | You can also edit [api.h](./api.h) to control which files should be included in the generated bindings (defaults to `llama.cpp/ggml*.h`) |
| 92 | |
| 93 | In fact, if you wanted to only generate bindings for the current version of the `ggml` repo itself (instead of `llama.cpp`; you'd loose support for k-quants), you could run: |
| 94 | |
| 95 | ```bash |
| 96 | API=../../include/ggml.h python regenerate.py |
| 97 | ``` |
| 98 | |
| 99 | ## Develop |
| 100 | |
| 101 | Run tests: |
| 102 | |
| 103 | ```bash |
| 104 | pytest |
| 105 | ``` |
| 106 | |
| 107 | ### Alternatives |
| 108 | |
| 109 | This example's goal is to showcase [cffi](https://cffi.readthedocs.io/)-generated bindings that are trivial to use and update, but there are already alternatives in the wild: |
| 110 | |
| 111 | - https://github.com/abetlen/ggml-python: these bindings seem to be hand-written and use [ctypes](https://docs.python.org/3/library/ctypes.html). It has [high-quality API reference docs](https://ggml-python.readthedocs.io/en/latest/api-reference/#ggml.ggml) that can be used with these bindings too, but it doesn't expose Metal, CUDA, MPI or OpenCL calls, doesn't support transparent (de/re)quantization like this example does (see [ggml.utils](./ggml/utils.py) module), and won't pick up your local changes. |
| 112 | |
| 113 | - https://github.com/abetlen/llama-cpp-python: these expose the C++ `llama.cpp` interface, which this example cannot easily be extended to support (`cffi` only generates bindings of C libraries) |
| 114 | |
| 115 | - [pybind11](https://github.com/pybind/pybind11) and [nanobind](https://github.com/wjakob/nanobind) are two alternatives to cffi that support binding C++ libraries, but it doesn't seem either of them have an automatic generator (writing bindings is rather time-consuming). |