项目文件夹

文件
2026-07-13 10:25:34 +00:00

347 行
15 KiB
Markdown

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
<!-- WEHUB_ZH_README -->
> [!NOTE]
> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
> [English](./README.en.md) · [原始项目](https://github.com/microsoft/BitNet) · [上游 README](https://github.com/microsoft/BitNet/blob/HEAD/README.md)
> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
# bitnet.cpp
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
![version](https://img.shields.io/badge/version-1.0-blue)
[<img src="./assets/header_model_release.png" alt="Hugging Face 上的 BitNet 模型" width="800"/>](https://huggingface.co/microsoft/BitNet-b1.58-2B-4T)
通过此 [演示](https://demo-bitnet-h0h8hcfqeqhrf5gf.canadacentral-01.azurewebsites.net/), 试用,或在你自己的 [CPU](https://github.com/microsoft/BitNet?tab=readme-ov-file#build-from-source) 或 [GPU](https://github.com/microsoft/BitNet/blob/main/gpu/README.md). 上构建并运行。
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.37x****5.07x** 的加速,模型越大,性能提升越明显。此外,能耗降低 **55.4%****70.0%**,进一步提升整体效率。在 x86 CPU 上,加速范围为 **2.37x****6.17x**,能耗降低 **71.9%****82.2%**。此外,bitnet.cpp 可在单颗 CPU 上运行 100B BitNet b1.58 模型,速度可达人类阅读水平(每秒 5-7 个 token),显著提升在本地设备上运行 LLM 的潜力。更多细节请参阅[技术报告](https://arxiv.org/abs/2410.16144)。
**最新优化**引入了支持可配置 tiling 的并行 kernel 实现,以及 embedding 量化支持,在不同硬件平台和工作负载上,相比原始实现可获得 **1.15x 至 2.1x** 的额外加速。详细技术信息请参阅[优化指南](src/README.md)。
<img src="./assets/performance.png" alt="performance_comparison" width="800"/>
## 演示
在 Apple M2 上运行 BitNet b1.58 3B 模型的 bitnet.cpp 演示:
https://github.com/user-attachments/assets/7f46b736-edec-4828-b809-4be780a3e5b1
## 最新动态:
- 01/15/2026 [BitNet CPU Inference Optimization](https://github.com/microsoft/BitNet/blob/main/src/README.md) ![NEW](https://img.shields.io/badge/NEW-red)
- 05/20/2025 [BitNet Official GPU inference kernel](https://github.com/microsoft/BitNet/blob/main/gpu/README.md)
- 04/14/2025 [BitNet Official 2B Parameter Model on Hugging Face](https://huggingface.co/microsoft/BitNet-b1.58-2B-4T)
- 02/18/2025 [Bitnet.cpp: Efficient Edge Inference for Ternary LLMs](https://arxiv.org/abs/2502.11880)
- 11/08/2024 [BitNet a4.8: 4-bit Activations for 1-bit LLMs](https://arxiv.org/abs/2411.04965)
- 10/21/2024 [1-bit AI Infra: Part 1.1, Fast and Lossless BitNet b1.58 Inference on CPUs](https://arxiv.org/abs/2410.16144)
- 10/17/2024 bitnet.cpp 1.0 released.
- 03/21/2024 [The-Era-of-1-bit-LLMs__Training_Tips_Code_FAQ](https://github.com/microsoft/unilm/blob/master/bitnet/The-Era-of-1-bit-LLMs__Training_Tips_Code_FAQ.pdf)
- 02/27/2024 [The Era of 1-bit LLMs: All Large Language Models are in 1.58 Bits](https://arxiv.org/abs/2402.17764)
- 10/17/2023 [BitNet: Scaling 1-bit Transformers for Large Language Models](https://arxiv.org/abs/2310.11453)
## 致谢
本项目基于 [llama.cpp](https://github.com/ggerganov/llama.cpp) 框架。我们感谢所有作者对开源社区的贡献。此外,bitnet.cpp 的 kernel 构建于 [T-MAC](https://github.com/microsoft/T-MAC/). 开创的查找表(Lookup Table)方法之上。对于三值模型之外的一般低位 LLM 推理,我们建议使用 T-MAC。
## 官方模型
<table>
</tr>
<tr>
<th rowspan="2">模型</th>
<th rowspan="2">参数量</th>
<th rowspan="2">CPU</th>
<th colspan="3">Kernel</th>
</tr>
<tr>
<th>I2_S</th>
<th>TL1</th>
<th>TL2</th>
</tr>
<tr>
<td rowspan="2"><a href="https://huggingface.co/microsoft/BitNet-b1.58-2B-4T">BitNet-b1.58-2B-4T</a></td>
<td rowspan="2">2.4B</td>
<td>x86</td>
<td>&#9989;</td>
<td>&#10060;</td>
<td>&#9989;</td>
</tr>
<tr>
<td>ARM</td>
<td>&#9989;</td>
<td>&#9989;</td>
<td>&#10060;</td>
</tr>
</table>
## 支持的模型
❗️**我们使用 [Hugging Face](https://huggingface.co/) 上现有的 1-bit LLM 来展示 bitnet.cpp 的推理能力。我们希望 bitnet.cpp 的发布能够激励 1-bit LLM 在模型规模和训练 token 数量等方面的大规模发展。**
<table>
</tr>
<tr>
<th rowspan="2">模型</th>
<th rowspan="2">参数量</th>
<th rowspan="2">CPU</th>
<th colspan="3">Kernel</th>
</tr>
<tr>
<th>I2_S</th>
<th>TL1</th>
<th>TL2</th>
</tr>
<tr>
<td rowspan="2"><a href="https://huggingface.co/1bitLLM/bitnet_b1_58-large">bitnet_b1_58-large</a></td>
<td rowspan="2">0.7B</td>
<td>x86</td>
<td>&#9989;</td>
<td>&#10060;</td>
<td>&#9989;</td>
</tr>
<tr>
<td>ARM</td>
<td>&#9989;</td>
<td>&#9989;</td>
<td>&#10060;</td>
</tr>
<tr>
<td rowspan="2"><a href="https://huggingface.co/1bitLLM/bitnet_b1_58-3B">bitnet_b1_58-3B</a></td>
<td rowspan="2">3.3B</td>
<td>x86</td>
<td>&#10060;</td>
<td>&#10060;</td>
<td>&#9989;</td>
</tr>
<tr>
<td>ARM</td>
<td>&#10060;</td>
<td>&#9989;</td>
<td>&#10060;</td>
</tr>
<tr>
<td rowspan="2"><a href="https://huggingface.co/HF1BitLLM/Llama3-8B-1.58-100B-tokens">Llama3-8B-1.58-100B-tokens</a></td>
<td rowspan="2">8.0B</td>
<td>x86</td>
<td>&#9989;</td>
<td>&#10060;</td>
<td>&#9989;</td>
</tr>
<tr>
<td>ARM</td>
<td>&#9989;</td>
<td>&#9989;</td>
<td>&#10060;</td>
</tr>
<tr>
<td rowspan="2"><a href="https://huggingface.co/collections/tiiuae/falcon3-67605ae03578be86e4e87026">Falcon3 Family</a></td>
<td rowspan="2">1B-10B</td>
<td>x86</td>
<td>&#9989;</td>
<td>&#10060;</td>
<td>&#9989;</td>
</tr>
<tr>
<td>ARM</td>
<td>&#9989;</td>
<td>&#9989;</td>
<td>&#10060;</td>
</tr>
<tr>
<td rowspan="2"><a href="https://huggingface.co/collections/tiiuae/falcon-edge-series-6804fd13344d6d8a8fa71130">Falcon-E Family</a></td>
<td rowspan="2">1B-3B</td>
<td>x86</td>
<td>&#9989;</td>
<td>&#10060;</td>
<td>&#9989;</td>
</tr>
<tr>
<td>ARM</td>
<td>&#9989;</td>
<td>&#9989;</td>
<td>&#10060;</td>
</tr>
</table>
## 安装
### 环境要求
- python>=3.9
- cmake>=3.22
- clang>=18
- 对于 Windows 用户,请安装 [Visual Studio 2022](https://visualstudio.microsoft.com/downloads/).。在安装程序中,请至少勾选以下选项(这也会自动安装 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 用户,可通过[自动安装脚本](https://apt.llvm.org/) 下载:
`bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"`
- conda(强烈推荐)
### 从源码构建
> [!IMPORTANT]
> 如果你使用的是 Windows,请务必始终使用 VS2022 的 Developer Command Prompt / PowerShell 执行以下命令。如遇到问题,请参阅下方的常见问题解答(FAQs)。
1. 克隆仓库
```bash
git clone --recursive https://github.com/microsoft/BitNet.git
cd BitNet
```
2. 安装依赖
```bash
# (Recommended) Create a new conda environment
conda create -n bitnet-cpp python=3.9
conda activate bitnet-cpp
pip install -r requirements.txt
```
3. 构建项目
```bash
# 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
```
<pre>
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]
</pre>
配置用于运行推理的环境
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 使用预调优的内核参数
</pre>
## 用法
### 基本用法
```bash
# 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
```
<pre>
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 指定的提示词将作为系统提示词使用。)
</pre>
### 基准测试
我们提供了脚本,可在给定模型的情况下运行推理基准测试。
```
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`:显示帮助信息并退出。使用此参数可查看用法说明。
例如:
```sh
python utils/e2e_benchmark.py -m /path/to/model -n 200 -p 256 -t 4
```
此命令将使用位于 `/path/to/model` 的模型运行推理基准测试,从 256 个 token 的提示词生成 200 个 token,并使用 4 个线程。
对于没有任何公开模型支持的模型布局,我们提供了脚本,可根据给定模型布局生成虚拟模型,并在你的机器上运行基准测试:
```bash
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` 检查点转换
```sh
# 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](https://github.com/tinglou/llama.cpp/commit/4e3db1e3d78cc1bcd22bcb3af54bd2a4628dd323) in the [discussion](https://github.com/abetlen/llama-cpp-python/issues/1942) 以修复此问题。
#### 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 工具。