Files
2026-07-13 10:25:34 +00:00

15 KiB
Raw Permalink Blame History

Note

本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
English · 原始项目 · 上游 README
原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。

bitnet.cpp

License: MIT version

Hugging Face 上的 BitNet 模型

通过此 演示, 试用,或在你自己的 CPUGPU. 上构建并运行。

bitnet.cpp 是 1-bit LLMLarge Language Model,大语言模型;例如 BitNet b1.58)的官方推理框架。它提供一系列优化的 kernel,在 CPU 和 GPU 上支持 1.58-bit 模型的快速无损推理(NPU 支持即将推出)。

bitnet.cpp 的首个版本支持在 CPU 上进行推理。在 ARM CPU 上,bitnet.cpp 可实现 1.37x5.07x 的加速,模型越大,性能提升越明显。此外,能耗降低 55.4%70.0%,进一步提升整体效率。在 x86 CPU 上,加速范围为 2.37x6.17x,能耗降低 71.9%82.2%。此外,bitnet.cpp 可在单颗 CPU 上运行 100B BitNet b1.58 模型,速度可达人类阅读水平(每秒 5-7 个 token),显著提升在本地设备上运行 LLM 的潜力。更多细节请参阅技术报告

最新优化引入了支持可配置 tiling 的并行 kernel 实现,以及 embedding 量化支持,在不同硬件平台和工作负载上,相比原始实现可获得 1.15x 至 2.1x 的额外加速。详细技术信息请参阅优化指南

performance_comparison

演示

在 Apple M2 上运行 BitNet b1.58 3B 模型的 bitnet.cpp 演示:

https://github.com/user-attachments/assets/7f46b736-edec-4828-b809-4be780a3e5b1

最新动态:

致谢

本项目基于 llama.cpp 框架。我们感谢所有作者对开源社区的贡献。此外,bitnet.cpp 的 kernel 构建于 T-MAC. 开创的查找表(Lookup Table)方法之上。对于三值模型之外的一般低位 LLM 推理,我们建议使用 T-MAC。

官方模型

模型 参数量 CPU Kernel
I2_S TL1 TL2
BitNet-b1.58-2B-4T 2.4B x86
ARM

支持的模型

我们使用 Hugging Face 上现有的 1-bit LLM 来展示 bitnet.cpp 的推理能力。我们希望 bitnet.cpp 的发布能够激励 1-bit LLM 在模型规模和训练 token 数量等方面的大规模发展。

模型 参数量 CPU Kernel
I2_S TL1 TL2
bitnet_b1_58-large 0.7B x86
ARM
bitnet_b1_58-3B 3.3B x86
ARM
Llama3-8B-1.58-100B-tokens 8.0B x86
ARM
Falcon3 Family 1B-10B x86
ARM
Falcon-E Family 1B-3B x86
ARM

安装

环境要求

  • python>=3.9
  • cmake>=3.22
  • clang>=18
    • 对于 Windows 用户,请安装 Visual Studio 2022.。在安装程序中,请至少勾选以下选项(这也会自动安装 CMake 等所需的其他工具):

      • Desktop-development with C++
      • C++-CMake Tools for Windows
      • Git for Windows
      • C++-Clang Compiler for Windows
      • MS-Build Support for LLVM-Toolset (clang)
    • 对于 Debian/Ubuntu 用户,可通过自动安装脚本 下载:

      bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"

  • conda(强烈推荐)

从源码构建

Important

如果你使用的是 Windows,请务必始终使用 VS2022 的 Developer Command Prompt / PowerShell 执行以下命令。如遇到问题,请参阅下方的常见问题解答(FAQs)。

  1. 克隆仓库
git clone --recursive https://github.com/microsoft/BitNet.git
cd BitNet
  1. 安装依赖
# (Recommended) Create a new conda environment
conda create -n bitnet-cpp python=3.9
conda activate bitnet-cpp

pip install -r requirements.txt
  1. 构建项目
# Manually download the model and run with local path
huggingface-cli download microsoft/BitNet-b1.58-2B-4T-gguf --local-dir models/BitNet-b1.58-2B-4T
python setup_env.py -md models/BitNet-b1.58-2B-4T -q i2_s

usage: setup_env.py [-h] [--hf-repo {1bitLLM/bitnet_b1_58-large,1bitLLM/bitnet_b1_58-3B,HF1BitLLM/Llama3-8B-1.58-100B-tokens,tiiuae/Falcon3-1B-Instruct-1.58bit,tiiuae/Falcon3-3B-Instruct-1.58bit,tiiuae/Falcon3-7B-Instruct-1.58bit,tiiuae/Falcon3-10B-Instruct-1.58bit}] [--model-dir MODEL_DIR] [--log-dir LOG_DIR] [--quant-type {i2_s,tl1}] [--quant-embd]
                    [--use-pretuned]

配置用于运行推理的环境

optional arguments: -h, --help 显示此帮助信息并退出 --hf-repo {1bitLLM/bitnet_b1_58-large,1bitLLM/bitnet_b1_58-3B,HF1BitLLM/Llama3-8B-1.58-100B-tokens,tiiuae/Falcon3-1B-Instruct-1.58bit,tiiuae/Falcon3-3B-Instruct-1.58bit,tiiuae/Falcon3-7B-Instruct-1.58bit,tiiuae/Falcon3-10B-Instruct-1.58bit}, -hr {1bitLLM/bitnet_b1_58-large,1bitLLM/bitnet_b1_58-3B,HF1BitLLM/Llama3-8B-1.58-100B-tokens,tiiuae/Falcon3-1B-Instruct-1.58bit,tiiuae/Falcon3-3B-Instruct-1.58bit,tiiuae/Falcon3-7B-Instruct-1.58bit,tiiuae/Falcon3-10B-Instruct-1.58bit} 用于推理的模型 --model-dir MODEL_DIR, -md MODEL_DIR 保存/加载模型的目录 --log-dir LOG_DIR, -ld LOG_DIR 保存日志信息的目录 --quant-type {i2_s,tl1}, -q {i2_s,tl1} 量化类型 --quant-embd 将嵌入层量化到 f16 --use-pretuned, -p 使用预调优的内核参数

用法

基本用法

# Run inference with the quantized model
python run_inference.py -m models/BitNet-b1.58-2B-4T/ggml-model-i2_s.gguf -p "You are a helpful assistant" -cnv
usage: run_inference.py [-h] [-m MODEL] [-n N_PREDICT] -p PROMPT [-t THREADS] [-c CTX_SIZE] [-temp TEMPERATURE] [-cnv]

Run inference

optional arguments:
  -h, --help            显示此帮助信息并退出
  -m MODEL, --model MODEL
                        模型文件路径
  -n N_PREDICT, --n-predict N_PREDICT
                        生成文本时要预测的 token 数量
  -p PROMPT, --prompt PROMPT
                        用于生成文本的提示词
  -t THREADS, --threads THREADS
                        使用的线程数
  -c CTX_SIZE, --ctx-size CTX_SIZE
                        提示词上下文大小
  -temp TEMPERATURE, --temperature TEMPERATURE
                        Temperature(温度),控制生成文本随机性的超参数
  -cnv, --conversation  是否启用聊天模式(适用于 instruct 模型。)
                        (启用此选项时,-p 指定的提示词将作为系统提示词使用。)

基准测试

我们提供了脚本,可在给定模型的情况下运行推理基准测试。

usage: e2e_benchmark.py -m MODEL [-n N_TOKEN] [-p N_PROMPT] [-t THREADS]  
   
Setup the environment for running the inference  
   
required arguments:  
  -m MODEL, --model MODEL  
                        Path to the model file. 
   
optional arguments:  
  -h, --help  
                        Show this help message and exit. 
  -n N_TOKEN, --n-token N_TOKEN  
                        Number of generated tokens. 
  -p N_PROMPT, --n-prompt N_PROMPT  
                        Prompt to generate text from. 
  -t THREADS, --threads THREADS  
                        Number of threads to use. 

以下简要说明各参数:

  • -m--model:模型文件路径。运行脚本时必须提供此必填参数。
  • -n--n-token:推理过程中要生成的 token 数量。可选参数,默认值为 128。
  • -p--n-prompt:用于生成文本的提示词 token 数量。可选参数,默认值为 512。
  • -t--threads:运行推理时使用的线程数。可选参数,默认值为 2。
  • -h--help:显示帮助信息并退出。使用此参数可查看用法说明。

例如:

python utils/e2e_benchmark.py -m /path/to/model -n 200 -p 256 -t 4  

此命令将使用位于 /path/to/model 的模型运行推理基准测试,从 256 个 token 的提示词生成 200 个 token,并使用 4 个线程。

对于没有任何公开模型支持的模型布局,我们提供了脚本,可根据给定模型布局生成虚拟模型,并在你的机器上运行基准测试:

python utils/generate-dummy-bitnet-model.py models/bitnet_b1_58-large --outfile models/dummy-bitnet-125m.tl1.gguf --outtype tl1 --model-size 125M

# Run benchmark with the generated model, use -m to specify the model path, -p to specify the prompt processed, -n to specify the number of token to generate
python utils/e2e_benchmark.py -m models/dummy-bitnet-125m.tl1.gguf -p 512 -n 128

.safetensors 检查点转换

# Prepare the .safetensors model file
huggingface-cli download microsoft/bitnet-b1.58-2B-4T-bf16 --local-dir ./models/bitnet-b1.58-2B-4T-bf16

# Convert to gguf model
python ./utils/convert-helper-bitnet.py ./models/bitnet-b1.58-2B-4T-bf16

常见问题(FAQ📌

Q1:构建时因在 log.cpp 中 std::chrono 相关问题导致编译 llama.cpp 失败?

A 这是 llama.cpp 近期版本中引入的问题。请参阅此 commit in the discussion 以修复此问题。

Q2:如何在 Windows 上的 conda 环境中使用 clang 构建?

A 在构建项目之前,请运行以下命令验证 clang 安装及 Visual Studio 工具是否可用:

clang -v

此命令会检查你是否使用了正确版本的 clang,以及 Visual Studio 工具是否可用。如果你看到如下错误信息:

'clang' is not recognized as an internal or external command, operable program or batch file.

这表明你的命令行窗口未正确初始化 Visual Studio 工具。

• 如果你使用命令提示符(Command Prompt),请运行:

"C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\VsDevCmd.bat" -startdir=none -arch=x64 -host_arch=x64

• 如果你使用 Windows PowerShell,请运行以下命令:

Import-Module "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" Enter-VsDevShell 3f0e31ad -SkipAutomaticLocation -DevCmdArguments "-arch=x64 -host_arch=x64"

这些步骤将初始化你的环境,使你能够使用正确的 Visual Studio 工具。