# bitsandbytes

**bitsandbytes** 是一个开源、采用 MIT 许可证的 Python 库，通过面向 PyTorch 的 k 比特量化，降低模型推理和训练所需的内存，让大语言模型更易使用<sup>[1](https://huggingface.co/docs/bitsandbytes/v0.43.3/en/index)</sup>。它提供三大主要功能：使用分块量化、以极小内存代价维持 32 比特性能的 8 比特优化器；用于推理、内存占用约为一半的 [LLM.int8](https://www.edgepedia.org/zh/llm-int8)() 8 比特量化；以及用于训练大语言模型的 QLoRA 4 比特量化<sup>[1](https://huggingface.co/docs/bitsandbytes/v0.43.3/en/index)</sup>。该库由量化方法研究者 Tim Dettmers 与相关论文的合作者共同创建<sup>[2](https://arxiv.org/abs/2110.02861)</sup>。

| 关键事实 | 详情 |
|---|---|
| 用途 | 面向 PyTorch 的 k 比特量化，削减大语言模型推理和训练的内存消耗<sup>[1](https://huggingface.co/docs/bitsandbytes/v0.43.3/en/index)</sup> |
| 核心方法 | 8 比特优化器（分块量化）、LLM.int8()（8 比特推理）、QLoRA（4 比特训练）<sup>[1](https://huggingface.co/docs/bitsandbytes/v0.43.3/en/index)</sup> |
| LLM.int8() 结果 | 参数量最高达 175B 的大语言模型推理无性能退化；超过 99.9% 的数值以 8 比特相乘<sup>[3](https://arxiv.org/abs/2208.07339)</sup> |
| QLoRA 结果 | 在单块 48GB GPU 上微调 65B 参数模型，同时保持完整的 16 比特微调任务性能<sup>[4](https://arxiv.org/abs/2305.14314)</sup> |
| 4 比特数据类型 | NF4（4 比特 NormalFloat），在标准正态分布下具有 16 个等概率量化分箱<sup>[5](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/agents/architecture_guide.md)</sup> |
| 许可证与平台 | MIT 许可证；最低要求 PyTorch 2.4；支持 NVIDIA CUDA、AMD ROCm、Apple Silicon（MPS）、Intel XPU 与 Gaudi，以及 CPU 后端<sup>[1](https://huggingface.co/docs/bitsandbytes/v0.43.3/en/index)</sup><sup> • </sup><sup>[6](https://github.com/bitsandbytes-foundation/bitsandbytes/releases)</sup> |
| 最新版本 | 0.50.2，新增面向 Linux 和 Windows 的 ROCm 10.0 构建<sup>[6](https://github.com/bitsandbytes-foundation/bitsandbytes/releases)</sup> |

## 量化方法

**8 比特优化器。** Adam 等有状态优化器会随时间保留梯度统计量，这一状态会占用本可用于存放模型参数的内存。8 比特优化器论文引入了首批使用 8 比特统计量、同时保持 32 比特优化器状态性能的优化器，其做法是通过分块动态量化将张量划分为更小的独立量化块，并结合稳定的嵌入层来降低梯度方差<sup>[2](https://arxiv.org/abs/2110.02861)</sup>。该方法在 15 亿参数语言建模、GLUE 微调、ImageNet 分类和 WMT'14 机器翻译等任务上均保持了 32 比特性能，且无需改动原有的优化器超参数，并以即插即用的替代方案发布，只需改动两行代码<sup>[2](https://arxiv.org/abs/2110.02861)</sup>。

**LLM.int8()。** 该方法对 Transformer 中的前馈层和注意力投影层执行 8 比特矩阵乘法，将推理所需内存减半，同时保持全精度性能<sup>[3](https://arxiv.org/abs/2208.07339)</sup>。它采用向量级量化，为每个内积设置独立的归一化常数，并用混合精度分解方案处理涌现出的离群特征维度，将这些维度隔离到 16 比特矩阵乘法中，而超过 99.9% 的数值仍以 8 比特相乘<sup>[3](https://arxiv.org/abs/2208.07339)</sup>。该库的实现会将绝对值达到或超过阈值（常用值为 6.0）的数值识别为离群值，并单独返回其列索引<sup>[7](https://deepwiki.com/bitsandbytes-foundation/bitsandbytes/3.1-quantization-types-and-formats)</sup>。

**QLoRA 与 4 比特量化。** QLoRA 通过一个冻结的、4 比特量化的预训练语言模型，将梯度反向传播到低秩适应（LoRA）适配器中<sup>[4](https://arxiv.org/abs/2305.14314)</sup>。它引入了三项节省内存的技术：4 比特 NormalFloat（NF4），一种被描述为对正态分布权重在信息论意义上最优的数据类型；双重量化，即对量化常数本身再进行量化，以降低平均内存占用；以及分页优化器，用于应对内存峰值<sup>[4](https://arxiv.org/abs/2305.14314)</sup>。在 NF4 中，16 个量化分箱在标准正态分布下各自概率相等，每字节打包两个 4 比特值<sup>[5](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/agents/architecture_guide.md)</sup>。在训练和推理过程中，权重仅在需要时从存储数据类型反量化为计算数据类型，因此内存占用保持在较低水平<sup>[8](https://github.com/huggingface/blog/blob/main/4bit-transformers-bitsandbytes.md?plain=1)</sup>。

QLoRA 论文报告称，在 8 个指令数据集和多种模型规模上微调了超过 1,000 个模型，而这些规模用常规微调方式是无法实现的；其 Guanaco 模型系列在单块 GPU 上微调 24 小时后，在 Vicuna 基准上达到 ChatGPT 性能水平的 99.3%<sup>[4](https://arxiv.org/abs/2305.14314)</sup>。

## 生态集成

bitsandbytes 已集成到 [Hugging Face](https://www.edgepedia.org/zh/hugging-face) 及更广泛的 PyTorch 生态中<sup>[9](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/docs/source/integrations.mdx)</sup>：

- **Transformers** 可以 4 比特或 8 比特加载任意模型并即时量化，参数通过 `BitsAndBytesConfig` 类配置；在硬件支持的情况下，推荐使用 bfloat16 作为计算数据类型，它兼具 float32 的数值稳定性和 16 比特类型的内存占用<sup>[9](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/docs/source/integrations.mdx)</sup>。
- **PEFT** 在 [Transformers](https://www.edgepedia.org/zh/hugging-face-transformers) 集成的基础上支持训练：`prepare_model_for_kbit_training` 用于准备 4 比特模型，`LoraConfig` 加 `get_peft_model` 则可创建可训练的 QLoRA 模型<sup>[9](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/docs/source/integrations.mdx)</sup>。
- **Accelerate** 可以通过向 `load_and_quantize_model` 传入 `BnbQuantizationConfig` 来量化任意 PyTorch 模型<sup>[9](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/docs/source/integrations.mdx)</sup>。
- **PyTorch Lightning、Lightning Fabric 和 Lit-GPT** 支持在训练、微调和推理过程中使用 bitsandbytes 量化<sup>[9](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/docs/source/integrations.mdx)</sup>。

文本生成推理服务器同样接受将 `bitsandbytes` 作为量化选项，与 gptq、awq 等并列<sup>[10](https://huggingface.co/docs/text-generation-inference/basic_tutorials/preparing_model)</sup>。

## 硬件支持与近期版本

bitsandbytes 最初是一个重度依赖 CUDA 的库，此后不断扩展其后端。0.49.0 版本新增了面向 Linux x86-64 的实验性 AMD ROCm wheel、带有 4 比特和 8 比特量化慢速 MPS 实现的 macOS 14+ wheel，以及针对 AVX512 和 AVX512BF16 优化的 x86-64 CPU 内核路径<sup>[6](https://github.com/bitsandbytes-foundation/bitsandbytes/releases)</sup>。0.50.0 版本引入了融合的 4 比特反量化加 GEMM 内核，使 4 比特推理在批大小为 2 至 64 时、在从 Turing 到 Blackwell 的各代 GPU 上速度最高达到原来的 4 倍<sup>[6](https://github.com/bitsandbytes-foundation/bitsandbytes/releases)</sup>。0.50.1 版本新增了对 NVIDIA RTX Spark（Windows on ARM64）和 AMD CDNA5 硬件的支持，0.50.2 版本新增了面向 Linux 和 Windows 的 ROCm 10.0 构建<sup>[6](https://github.com/bitsandbytes-foundation/bitsandbytes/releases)</sup>。该库要求 PyTorch 2.4 或更高版本<sup>[11](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/README.md?plain=1)</sup>。

## 实际性能

项目自身的基准测试（截至 v0.45.0）显示，相较 v0.44.1 有大幅提升：在 Turing/Ampere/Ada GPU 上，LLM.int8() 的每 token 吞吐量提升了 60-85%；在 H100 上，对于 Llama 3.1 70B，新的 LLM.int8() 在批大小为 8 及以上时始终优于 NF4<sup>[12](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/benchmarking/README.md)</sup>。

与其他 4 比特格式相比，bitsandbytes 以速度换取简单性和训练支持。在 A100 GPU 上，其 4 比特推理通常比 AWQ 慢 20-40%，因为其 CUDA 内核的吞吐优化程度较低；它在关键路径上反量化为 BF16，而 GPTQ 和 AWQ 使用预量化的模型文件和校准<sup>[13](https://mljourney.com/awq-vs-gptq-vs-bitsandbytes-llm-quantization-methods-compared/)</sup><sup> • </sup><sup>[14](https://pythondatabench.com/article/llm-quantization-gptq-awq-bitsandbytes-gguf-2026)</sup>。其独特优势在于 QLoRA 微调，NF4 是支持梯度流经量化基座模型的格式，并且它执行即时量化，无需预量化的模型文件<sup>[13](https://mljourney.com/awq-vs-gptq-vs-bitsandbytes-llm-quantization-methods-compared/)</sup><sup> • </sup><sup>[14](https://pythondatabench.com/article/llm-quantization-gptq-awq-bitsandbytes-gguf-2026)</sup>。

独立研究量化了质量影响。一项 2025 年关于前摄干扰的研究发现，通过 bitsandbytes 进行 INT4/NF4 量化，在所有受测模型中都会在高干扰条件下显著降低准确率，例如 Qwen2.5-7B-Instruct 从 81.0% 降至 68.3%，而 INT8 在三个模型中的两个里带来了较小但真实的性能损失<sup>[15](https://arxiv.org/abs/2608.18578)</sup>。另一项工作将 NF4 反量化路径识别为受内存限制的瓶颈，并展示了在 Gemma 27B、Qwen3 32B 和 Llama3.3 70B 上，共享内存内核相较开源实现实现了 2.0-2.2 倍的内核加速<sup>[16](https://arxiv.org/abs/2604.02556)</sup>。

## References

1. [bitsandbytes documentation, Hugging Face](https://huggingface.co/docs/bitsandbytes/v0.43.3/en/index)
2. [Tim Dettmers, Mike Lewis, Sam Shleifer, Luke Zettlemoyer (2021). 8-bit Optimizers via Block-wise Quantization. arXiv:2110.02861](https://arxiv.org/abs/2110.02861)
3. [Tim Dettmers and colleagues (2022). LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale. arXiv:2208.07339](https://arxiv.org/abs/2208.07339)
4. [Tim Dettmers, Artidoro Pagnoni, Ari Holtzman, Luke Zettlemoyer (2023). QLoRA: Efficient Finetuning of Quantized LLMs. arXiv:2305.14314](https://arxiv.org/abs/2305.14314)
5. [bitsandbytes architecture guide, bitsandbytes-foundation GitHub](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/agents/architecture_guide.md)
6. [Releases, bitsandbytes-foundation/bitsandbytes GitHub](https://github.com/bitsandbytes-foundation/bitsandbytes/releases)
7. [Quantization and Dequantization Operations, DeepWiki](https://deepwiki.com/bitsandbytes-foundation/bitsandbytes/3.1-quantization-types-and-formats)
8. [Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA, Hugging Face blog](https://github.com/huggingface/blog/blob/main/4bit-transformers-bitsandbytes.md?plain=1)
9. [Integrations guide, bitsandbytes-foundation GitHub](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/docs/source/integrations.mdx)
10. [Preparing the Model, Text Generation Inference documentation](https://huggingface.co/docs/text-generation-inference/basic_tutorials/preparing_model)
11. [bitsandbytes README, bitsandbytes-foundation GitHub](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/README.md?plain=1)
12. [Benchmarking README, bitsandbytes-foundation GitHub](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/benchmarking/README.md)
13. [AWQ vs GPTQ vs bitsandbytes: LLM Quantization Methods Compared, ML Journey](https://mljourney.com/awq-vs-gptq-vs-bitsandbytes-llm-quantization-methods-compared/)
14. [LLM Quantization Python: GPTQ vs AWQ vs bitsandbytes vs GGUF, Python Data Bench](https://pythondatabench.com/article/llm-quantization-gptq-awq-bitsandbytes-gguf-2026)
15. [Compress and Forget: bitsandbytes Quantization Amplifies Proactive Interference in LLMs. arXiv:2608.18578](https://arxiv.org/abs/2608.18578)
16. [Fast NF4 Dequantization Kernels for Large Language Model Inference. arXiv:2604.02556](https://arxiv.org/abs/2604.02556)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Inference, serving, and efficiency of foundation models

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/bitsandbytes

English original: https://www.edgepedia.org/bitsandbytes
