Skip to content

llama.cpp Class Encapsulation and Wrapper to provide similar interfaces as Hugging Face and llama.cpp Python bindings.

Notifications You must be signed in to change notification settings

cshbli/llama-cpp-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

llama.cpp Class Encapsulation and Wrapper

This project uses the find_package() CMake command to conveniently include llama.cpp in projects which live outside of the source tree.

Building

It is important to first build/install llama.cpp using CMake. An example is provided here, but please see the llama.cpp build instructions for more detailed build instructions.

git clone https://github.com/ggerganov/llama.cpp
# Or clone from my fork with slim version
# git clonehttps://github.com/cshbli/llama.cpp.git
cd llama.cpp
cmake -S . -B build
cmake --build build
cmake --install build --prefix inst

Examples

Deliberate design decision for C Interface

  1. Ensures Compatibility

A C interface ensures compatibility with a broader range of programming languages and tools, as C APIs are:

  • Easily callable from other languages such as Python, Java, Rust, or even C++ itself.
  • Universally supported in cross-language bindings and FFI (Foreign Function Interfaces).

For example:

  • Libraries with a C API can be easily wrapped for Python (e.g., via ctypes or cffi).
  • They are also compatible with systems or frameworks that require C-style linkage. In contrast, C++ classes and features like inheritance, templates, or virtual functions can be complex to map to other languages.
  1. Simplified Binary Interface (ABI Stability)

C has a more stable ABI (Application Binary Interface) compared to C++:

  • Different C++ compilers (e.g., GCC, Clang, MSVC) often produce incompatible name mangling for symbols, making binaries less portable.
  • By exposing a flat C API, llama.cpp avoids these issues and ensures consistent symbol names, even when used with different compilers or environments.
  1. Easier for Embedding in Low-Level Systems

C interfaces are ideal for:

  • Embedding the library in low-level environments like game engines, operating systems, or hardware platforms where C is dominant.
  • C++ abstractions like exceptions or RTTI (Run-Time Type Information) can add overhead and complexity, which may not be desirable for performance-critical applications like llama.cpp.
  1. Lightweight and Minimalist Design

A C interface aligns with the lightweight philosophy of llama.cpp:

  • It avoids introducing complex object-oriented abstractions that might make the code harder to understand, debug, or optimize.
  • A flat procedural API is easier for developers to trace and use in performance-critical scenarios like machine learning inference.
  1. User-Controlled Encapsulation

Instead of enforcing encapsulation through C++ classes, llama.cpp lets developers implement their own abstractions:

  • A user of the library can wrap the C API in their preferred C++ classes or use it directly, giving them flexibility over how the library is integrated.

About

llama.cpp Class Encapsulation and Wrapper to provide similar interfaces as Hugging Face and llama.cpp Python bindings.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages