微软发布 bitnet.cpp 1-bit LLM 推理框架及 BitNet b1.58 系列模型
microsoft / BitNet
微软开源 bitnet.cpp,这是面向 1-bit LLM(如 BitNet b1.58)的官方推理框架,提供在 CPU 和 GPU 上快速无损推理的优化 kernel,NPU 支持后续推出。
微软开源 1-bit LLM 推理框架 bitnet.cpp,给出 CPU/GPU 上的加速与能耗数据,并放出官方 1-bit 模型与嵌入模型。
概述
bitnet.cpp 是 1-bit LLM(例如 BitNet b1.58)的官方推理框架。它提供了一套优化的内核,支持在 CPU 和 GPU 上对 1.58-bit 模型进行快速且无损的推理(NPU 支持即将推出)。
通过此在线演示试用,或在您自己的 CPU 或 GPU 上构建并运行它。
bitnet.cpp 在 ARM CPU 上实现了 1.37 倍至 5.07 倍的加速,模型越大性能提升越明显。此外,它还将能耗降低了 55.4% 至 70.0%,进一步提升了整体效率。在 x86 CPU 上,加速比在 2.37 倍至 6.17 倍之间,能耗降低 71.9% 至 82.2%。此外,bitnet.cpp 可以在单个 CPU 上运行 100B 的 BitNet b1.58 模型,速度可与人类阅读速度相媲美(每秒 5-7 个 token),显著增强了在本地设备上运行 LLM 的潜力。更多详情请参阅技术报告。
模型发布
1. BitNet-b1.58-2B-4T - 1-bit 大型语言模型
BitNet-b1.58-2B-4T 是首个官方 BitNet b1.58 模型,拥有 24 亿参数,在 4 万亿 token 上训练。它是一个三值(1.58-bit)语言模型,在提供与类似规模的全精度模型相竞争的性能的同时,实现了显著更快、更节能的推理。
- 快速 CPU 推理:与全精度模型相比,在 x86 CPU 上实现高达 6.17 倍加速,在 ARM CPU 上实现 5.07 倍加速。
- 节能高效:在 x86 上能耗降低高达 82.2%,在 ARM 上降低 70.0%。
- GPU 支持:提供官方 GPU 推理内核,用于加速部署。
- 聊天就绪:支持对话模式,用于交互式使用。
🤗 Hugging Face | 🔗 在线演示 | 📄 技术报告
2. BitNet-embedding-0.6B - 1-bit 嵌入模型
BitNet-embedding-0.6B 是一个 0.6B 参数的 1-bit 嵌入模型,在实现具有竞争力的嵌入质量的同时,显著加快了 CPU 推理速度。它是首个证明三值权重可以在嵌入任务上提供强大性能的模型。
- 在预填充阶段(8 线程,x86)相比 F16 实现 1.42 倍至 2.28 倍加速
- 无损质量:每个权重 2 比特,嵌入质量具有竞争力
- I2_S 内核:支持在 x86 CPU 上进行优化的 I2_S 转换
3. BitNet-embedding-270M - 轻量级 1-bit 嵌入模型
BitNet-embedding-270M 是一个紧凑的 270M 参数1-bit 嵌入模型,专为资源受限环境设计,提供快速推理和极小的内存占用。
- 在预填充阶段(8 线程,x86)相比 F16 实现 1.32 倍至 1.74 倍加速
- 无损质量:每个权重 2 比特,嵌入质量具有竞争力
- 轻量级:仅 270M 参数,适用于边缘部署场景
支持的模型
| 模型 | 参数 | CPU | 内核 | ||
|---|---|---|---|---|---|
| I2_S | TL1 | TL2 | |||
| 官方模型 | |||||
| BitNet-b1.58-2B-4T | 2.4B | x86 | ✅ | ❌ | ✅ |
| ARM | ✅ | ✅ | ❌ | ||
| BitNet-embedding-0.6B | 0.6B | x86 | ✅ | ❌ | ❌ |
| ARM | ❌ | ❌ | ❌ | ||
| BitNet-embedding-270M | 270M | x86 | ✅ | ❌ | ❌ |
| ARM | ❌ | ❌ | ❌ | ||
| 社区模型 | |||||
| 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 | ✅ | ✅ | ❌ | ||
❗️我们使用 Hugging Face 上现有的 1-bit LLM 来展示 bitnet.cpp 的推理能力。我们希望 bitnet.cpp 的发布能够激发在模型规模和训练 token 方面大规模场景下 1-bit LLM 的发展。
安装
要求
- python>=3.10
- cmake>=3.22
- clang>=18
-
对于 Windows 用户,请安装 Visual Studio 2022。在安装程序中,至少勾选以下选项(这也会自动安装所需的附加工具,如 CMake):
- 使用 C++ 的桌面开发
- 适用于 Windows 的 C++-CMake 工具
- 适用于 Windows 的 Git
- 适用于 Windows 的 C++-Clang 编译器
- MS-Build 对 LLVM-Toolset (clang) 的支持
-
对于 Debian/Ubuntu 用户,您可以使用 自动安装脚本 下载
bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
-
- conda(强烈推荐)
从源码构建
重要
如果您使用的是 Windows,请记住始终使用 VS2022 的开发人员命令提示符 / PowerShell 来执行以下命令。如果遇到任何问题,请参阅下面的常见问题解答。
- 克隆仓库
git clone --recursive https://github.com/microsoft/BitNet.git
cd BitNet- 安装依赖项
# (Recommended) Create a new conda environment
conda create -n bitnet-cpp python=3.10
conda activate bitnet-cpp
pip install -r requirements.txt- 构建项目
# 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]
Setup the environment for running inference
optional arguments:
-h, --help show this help message and exit
--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 used for inference
--model-dir MODEL_DIR, -md MODEL_DIR
Directory to save/load the model
--log-dir LOG_DIR, -ld LOG_DIR
Directory to save the logging info
--quant-type {i2_s,tl1}, -q {i2_s,tl1}
Quantization type
--quant-embd Quantize the embeddings to f16
--use-pretuned, -p Use the pretuned kernel parameters
用法
基本用法
# 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 show this help message and exit
-m MODEL, --model MODEL
Path to model file
-n N_PREDICT, --n-predict N_PREDICT
Number of tokens to predict when generating text
-p PROMPT, --prompt PROMPT
Prompt to generate text from
-t THREADS, --threads THREADS
Number of threads to use
-c CTX_SIZE, --ctx-size CTX_SIZE
Size of the prompt context
-temp TEMPERATURE, --temperature TEMPERATURE
Temperature, a hyperparameter that controls the randomness of the generated text
-cnv, --conversation Whether to enable chat mode or not (for instruct models.)
(When this option is turned on, the prompt specified by -p will be used as the system prompt.)
演示
bitnet.cpp 在 Apple M2 上运行 BitNet b1.58 3B 模型的演示:
demo.mp4
基准测试
我们提供脚本来运行给定模型的推理基准测试。
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
致谢
本项目基于 llama.cpp 框架。我们要感谢所有作者对开源社区的贡献。此外,bitnet.cpp 的内核建立在 T-MAC 中开创的查找表方法之上。对于三元模型之外的通用低位 LLM 的推理,我们建议使用 T-MAC。
常见问题解答 (FAQ)📌
Q1:由于 log.cpp 中的 std::chrono 问题,构建 llama.cpp 时出现错误导致构建失败?
A: 这是 llama.cpp 近期版本中引入的问题。请参考 讨论 中的这个 提交 来修复此问题。
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 工具正确初始化。
• 如果你使用的是命令提示符,请运行:
"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 工具。
来源:web-github-trending · github.com



