# soumya-ranjan-77/bitnet architecture

> Architecture diagram of the GitHub repository soumya-ranjan-77/bitnet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/soumya-ranjan-77/bitnet
- Repository: https://github.com/soumya-ranjan-77/bitnet

## Overview

#### 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: [`3rdparty/llama.cpp`](https://github.com/soumya-ranjan-77/bitnet/blob/main/3rdparty/llama.cpp) (llama.cpp runtime), [`gpu/model.py`](https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/model.py) (GPU model execution), [`src/ggml-bitnet-lut.cpp`](https://github.com/soumya-ranjan-77/bitnet/blob/main/src/ggml-bitnet-lut.cpp) (LUT GEMM kernels), [`include/ggml-bitnet.h`](https://github.com/soumya-ranjan-77/bitnet/blob/main/include/ggml-bitnet.h) (BitNet GEMM interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cpu["CPU Native Inference"]
  node_build["Native build<br/>CMake composition<br/>[CMakeLists.txt]"]
  node_srcbuild["BitNet native sources<br/>CMake target<br/>[CMakeLists.txt]"]
  node_llama["llama.cpp runtime<br/>Vendored runtime<br/>[llama.cpp]"]
  node_interfaces["BitNet GEMM interface<br/>Native headers<br/>[ggml-bitnet.h]"]
  node_lut["LUT GEMM kernels<br/>CPU kernel"]
  node_mad["MAD GEMM kernels<br/>CPU kernel"]
end

subgraph group_prep["Model &amp; Kernel Preparation"]
  node_presets["Pre-tuned kernels<br/>Kernel presets"]
  node_conversion["GGUF conversion<br/>Conversion utilities"]
  node_codegen["LUT kernel generation<br/>Code generator<br/>[codegen_tl1.py]"]
  node_tuning["GEMM tuning<br/>Hardware tuning utility"]
end

subgraph group_gpu["GPU Inference"]
  node_gpu_generate["GPU generation<br/>Python entry point<br/>[generate.py]"]
  node_gpu_model["GPU model execution<br/>Python runtime<br/>[model.py]"]
  node_cuda["CUDA BitNet kernels<br/>CUDA kernels<br/>[bitnet_kernels.cu]"]
  node_gpu_convert["GPU weight conversion<br/>Weight preparation"]
end

node_setup["Environment setup<br/>Python orchestration<br/>[setup_env.py]"]
node_cli["CLI inference<br/>Python entry point<br/>[run_inference.py]"]
node_server["Inference server<br/>Python entry point"]
node_gguf["GGUF model<br/>External model artifact"]

node_setup -->|"configures"| node_build
node_setup -->|"selects"| node_presets
node_conversion -->|"produces"| node_gguf
node_cli -->|"invokes"| node_llama
node_server -->|"serves via"| node_llama
node_gguf -->|"loads"| node_llama
node_build -->|"includes"| node_srcbuild
node_srcbuild -->|"extends"| node_llama
node_interfaces -->|"defines interface for"| node_lut
node_interfaces -->|"defines interface for"| node_mad
node_presets -->|"specializes"| node_lut
node_codegen -->|"generates"| node_presets
node_tuning -->|"tunes"| node_presets
node_llama -->|"dispatches quantized ops"| node_lut
node_llama -->|"dispatches quantized ops"| node_mad
node_gpu_convert -->|"prepares weights for"| node_gpu_model
node_gpu_generate -->|"runs"| node_gpu_model
node_gpu_model -->|"executes"| node_cuda

click node_setup "https://github.com/soumya-ranjan-77/bitnet/blob/main/setup_env.py"
click node_cli "https://github.com/soumya-ranjan-77/bitnet/blob/main/run_inference.py"
click node_server "https://github.com/soumya-ranjan-77/bitnet/blob/main/run_inference_server.py"
click node_build "https://github.com/soumya-ranjan-77/bitnet/blob/main/CMakeLists.txt"
click node_srcbuild "https://github.com/soumya-ranjan-77/bitnet/blob/main/src/CMakeLists.txt"
click node_llama "https://github.com/soumya-ranjan-77/bitnet/blob/main/3rdparty/llama.cpp"
click node_interfaces "https://github.com/soumya-ranjan-77/bitnet/blob/main/include/ggml-bitnet.h"
click node_lut "https://github.com/soumya-ranjan-77/bitnet/blob/main/src/ggml-bitnet-lut.cpp"
click node_mad "https://github.com/soumya-ranjan-77/bitnet/blob/main/src/ggml-bitnet-mad.cpp"
click node_conversion "https://github.com/soumya-ranjan-77/bitnet/blob/main/utils/convert-hf-to-gguf-bitnet.py"
click node_codegen "https://github.com/soumya-ranjan-77/bitnet/blob/main/utils/codegen_tl1.py"
click node_tuning "https://github.com/soumya-ranjan-77/bitnet/blob/main/utils/tune_gemm_config.py"
click node_gpu_generate "https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/generate.py"
click node_gpu_model "https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/model.py"
click node_cuda "https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/bitnet_kernels/bitnet_kernels.cu"
click node_gpu_convert "https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/convert_safetensors.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_build,node_srcbuild,node_llama,node_interfaces,node_lut,node_mad toneBlue
class node_presets,node_conversion,node_codegen,node_tuning toneAmber
class node_gpu_generate,node_gpu_model,node_cuda,node_gpu_convert toneMint
class node_setup,node_cli,node_server,node_gguf toneNeutral
```

## Components

### CPU Native Inference

CMake-built BitNet extensions integrated with vendored llama.cpp.

- [Native build](https://github.com/soumya-ranjan-77/bitnet/blob/main/CMakeLists.txt): CMake composition · `CMakeLists.txt`
- [BitNet native sources](https://github.com/soumya-ranjan-77/bitnet/blob/main/src/CMakeLists.txt): CMake target · `src/CMakeLists.txt`
- [llama.cpp runtime](https://github.com/soumya-ranjan-77/bitnet/blob/main/3rdparty/llama.cpp): Vendored runtime · Handles model loading, tokenization, and generation runtime. · `3rdparty/llama.cpp`
- [BitNet GEMM interface](https://github.com/soumya-ranjan-77/bitnet/blob/main/include/ggml-bitnet.h): Native headers · Defines BitNet operations and GEMM selection settings. · `include/ggml-bitnet.h`
- [LUT GEMM kernels](https://github.com/soumya-ranjan-77/bitnet/blob/main/src/ggml-bitnet-lut.cpp): CPU kernel · `src/ggml-bitnet-lut.cpp`
- [MAD GEMM kernels](https://github.com/soumya-ranjan-77/bitnet/blob/main/src/ggml-bitnet-mad.cpp): CPU kernel · `src/ggml-bitnet-mad.cpp`

### Model & Kernel Preparation

Converts models and creates tuned CPU kernel definitions.

- **Pre-tuned kernels**: Kernel presets · Generated headers and tile configurations for supported models.
- [GGUF conversion](https://github.com/soumya-ranjan-77/bitnet/blob/main/utils/convert-hf-to-gguf-bitnet.py): Conversion utilities · Transforms supported upstream checkpoints into BitNet GGUF. · `utils/convert-hf-to-gguf-bitnet.py`
- [LUT kernel generation](https://github.com/soumya-ranjan-77/bitnet/blob/main/utils/codegen_tl1.py): Code generator · `utils/codegen_tl1.py`
- [GEMM tuning](https://github.com/soumya-ranjan-77/bitnet/blob/main/utils/tune_gemm_config.py): Hardware tuning utility · `utils/tune_gemm_config.py`

### GPU Inference

Separate Python/CUDA execution and weight preparation stack.

- [GPU generation](https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/generate.py): Python entry point · `gpu/generate.py`
- [GPU model execution](https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/model.py): Python runtime · `gpu/model.py`
- [CUDA BitNet kernels](https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/bitnet_kernels/bitnet_kernels.cu): CUDA kernels · `gpu/bitnet_kernels/bitnet_kernels.cu`
- [GPU weight conversion](https://github.com/soumya-ranjan-77/bitnet/blob/main/gpu/convert_safetensors.py): Weight preparation · Converts and packs weights for GPU execution. · `gpu/convert_safetensors.py`

### Other components

- [Environment setup](https://github.com/soumya-ranjan-77/bitnet/blob/main/setup_env.py): Python orchestration · Selects models, kernels, presets, and drives setup. · `setup_env.py`
- [CLI inference](https://github.com/soumya-ranjan-77/bitnet/blob/main/run_inference.py): Python entry point · `run_inference.py`
- [Inference server](https://github.com/soumya-ranjan-77/bitnet/blob/main/run_inference_server.py): Python entry point · `run_inference_server.py`
- **GGUF model**: External model artifact · Durable quantized-model input boundary.

## Connections

- 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
