How soumya-ranjan-77/bitnet works
Purpose and execution surface
bitnet.cpp is a local inference framework for ternary/1.58-bit LLMs, optimized for CPU and GPU execution. It builds on a vendored llama.cpp runtime and adds BitNet-specific quantized matrix kernels. Primary user entry points are run_inference.py, run_inference_server.py, and environment/build orchestration in setup_env.py.
Native CPU inference integration
The core CPU path extends ggml/llama.cpp with BitNet lookup-table (LUT) and multiply-add kernels. Native build composition is defined by CMakeLists.txt and src/CMakeLists.txt; BitNet ggml interfaces and GEMM selection settings are in include/ggml-bitnet.h and include/gemm-config.h. Runtime execution is supplied by the 3rdparty/llama.cpp submodule, creating the principal boundary between upstream model/runtime handling and repo-specific tensor operations.
Quantized GEMM kernel variants
CPU kernels implement alternative execution strategies: LUT-based and MAD-based operations in src/ggml-bitnet-lut.cpp and src/ggml-bitnet-mad.cpp. The selected model quantization/kernel type must match the architecture and model layout. Generated/pre-tuned kernel headers and tile configurations are distributed under preset_kernels/, for example preset_kernels/bitnet_b1_58-3B/bitnet-lut-kernels-tl1.h and preset_kernels/bitnet_b1_58-3B/kernel_config_tl1.ini.
Setup, model acquisition, and compilation flow
setup_env.py accepts a Hugging Face model selection or local model directory, chooses quantization/kernel parameters, optionally applies pre-tuned configurations, and drives project setup. Models are external GGUF artifacts, commonly downloaded from Hugging Face; the framework itself does not train or host models. Build-time requirements include Python, CMake, and Clang/Visual Studio LLVM tooling; CPU targets include x86 and ARM.
Inference control flow
run_inference.py accepts a GGUF model, prompt, context/token limits, thread count, temperature, and optional conversation mode. It delegates generation to the built native llama.cpp executable/runtime after model loading. run_inference_server.py exposes a server-oriented variant. The key data flow is: user prompt → tokenizer/model runtime → BitNet quantized tensor ops → autoregressive tokens/text. Model files form the durable input boundary; generated text is the primary output.
Model conversion and kernel preparation
Utilities transform upstream checkpoint formats into inference-ready GGUF and prepare BitNet weights: utils/convert-helper-bitnet.py, utils/convert-hf-to-gguf-bitnet.py, and utils/convert-ms-to-gguf-bitnet.py. Code generators utils/codegen_tl1.py and utils/codegen_tl2.py produce specialized LUT kernel definitions, while utils/tune_gemm_config.py supports hardware/config tuning. This separates model-format ingestion and hardware specialization from inference serving.
GPU execution path
The GPU implementation is a parallel Python/CUDA stack rather than merely a CMake backend. gpu/generate.py is its generation entry point; gpu/model.py defines model execution, and gpu/bitnet_kernels/bitnet_kernels.cu supplies CUDA kernels with Python packaging/build support in gpu/bitnet_kernels/setup.py. GPU conversion and weight-packing stages (gpu/convert_safetensors.py, gpu/pack_weight.py) create the GPU-ready representation, with dependencies isolated in gpu/requirements.txt.
Main entry points: llama.cpp llama.cpp runtime · model.py GPU model execution · ggml-bitnet-lut.cpp LUT GEMM kernels · ggml-bitnet.h BitNet GEMM interface
Components 18
CPU Native Inference
CMake-built BitNet extensions integrated with vendored llama.cpp.
- Native build
CMakeLists.txt - BitNet native sources
src/CMakeLists.txt - llama.cpp runtime · Handles model loading, tokenization, and generation runtime.
3rdparty/llama.cpp - BitNet GEMM interface · Defines BitNet operations and GEMM selection settings.
include/ggml-bitnet.h - LUT GEMM kernels
src/ggml-bitnet-lut.cpp - MAD GEMM kernels
src/ggml-bitnet-mad.cpp
Model & Kernel Preparation
Converts models and creates tuned CPU kernel definitions.
- Pre-tuned kernels · Generated headers and tile configurations for supported models.
- GGUF conversion · Transforms supported upstream checkpoints into BitNet GGUF.
utils/convert-hf-to-gguf-bitnet.py - LUT kernel generation
utils/codegen_tl1.py - GEMM tuning
utils/tune_gemm_config.py
GPU Inference
Separate Python/CUDA execution and weight preparation stack.
- GPU generation
gpu/generate.py - GPU model execution
gpu/model.py - CUDA BitNet kernels
gpu/bitnet_kernels/bitnet_kernels.cu - GPU weight conversion · Converts and packs weights for GPU execution.
gpu/convert_safetensors.py
Other components
- Environment setup · Selects models, kernels, presets, and drives setup.
setup_env.py - CLI inference
run_inference.py - Inference server
run_inference_server.py - GGUF model · Durable quantized-model input boundary.
Connections 18
- Environment setup → Native build: configures
- Environment setup → Pre-tuned kernels: selects
- GGUF conversion → GGUF model: produces
- CLI inference → llama.cpp runtime: invokes
- Inference server → llama.cpp runtime: serves via
- GGUF model → llama.cpp runtime: loads
- Native build → BitNet native sources: includes
- BitNet native sources → llama.cpp runtime: extends
- BitNet GEMM interface → LUT GEMM kernels: defines interface for
- BitNet GEMM interface → MAD GEMM kernels: defines interface for
- Pre-tuned kernels → LUT GEMM kernels: specializes
- LUT kernel generation → Pre-tuned kernels: generates
- GEMM tuning → Pre-tuned kernels: tunes
- llama.cpp runtime → LUT GEMM kernels: dispatches quantized ops
- llama.cpp runtime → MAD GEMM kernels: dispatches quantized ops
- GPU weight conversion → GPU model execution: prepares weights for
- GPU generation → GPU model execution: runs
- GPU model execution → CUDA BitNet kernels: executes