|
@@ -1,3 +1,97 @@
|
|
|
# bitsandbytes
|
|
|
|
|
|
-高效量化和低精度训练工具库,减少模型训练和推理的计算资源消耗
|
|
|
+高效量化和低精度训练工具库,减少模型训练和推理的计算资源消耗
|
|
|
+
|
|
|
+### 安装
|
|
|
+
|
|
|
+可以通过 pip 安装 BitsAndBytes:
|
|
|
+
|
|
|
+```
|
|
|
+pip install bitsandbytes
|
|
|
+```
|
|
|
+
|
|
|
+### 示例代码
|
|
|
+
|
|
|
+以下是使用 BitsAndBytes 进行 8 位量化和混合精度训练的基本示例:
|
|
|
+
|
|
|
+#### 8 位量化示例
|
|
|
+
|
|
|
+```
|
|
|
+import torch
|
|
|
+from bitsandbytes.optim import Adam8bit
|
|
|
+
|
|
|
+# 定义一个简单的模型
|
|
|
+model = torch.nn.Linear(10, 2)
|
|
|
+
|
|
|
+# 将模型移动到 GPU 上
|
|
|
+model.cuda()
|
|
|
+
|
|
|
+# 使用 8 位量化的 Adam 优化器
|
|
|
+optimizer = Adam8bit(model.parameters(), lr=0.001)
|
|
|
+
|
|
|
+# 生成一些随机输入数据
|
|
|
+inputs = torch.randn(16, 10).cuda()
|
|
|
+targets = torch.randint(0, 2, (16,)).cuda()
|
|
|
+
|
|
|
+# 定义损失函数
|
|
|
+criterion = torch.nn.CrossEntropyLoss()
|
|
|
+
|
|
|
+# 前向传播
|
|
|
+outputs = model(inputs)
|
|
|
+loss = criterion(outputs, targets)
|
|
|
+
|
|
|
+# 反向传播和优化
|
|
|
+loss.backward()
|
|
|
+optimizer.step()
|
|
|
+```
|
|
|
+
|
|
|
+#### 混合精度训练示例
|
|
|
+
|
|
|
+```
|
|
|
+python
|
|
|
+Copy code
|
|
|
+import torch
|
|
|
+from torch.cuda.amp import autocast, GradScaler
|
|
|
+from bitsandbytes.optim import AdamW
|
|
|
+
|
|
|
+# 定义一个简单的模型
|
|
|
+model = torch.nn.Linear(10, 2)
|
|
|
+
|
|
|
+# 将模型移动到 GPU 上
|
|
|
+model.cuda()
|
|
|
+
|
|
|
+# 使用 AdamW 优化器
|
|
|
+optimizer = AdamW(model.parameters(), lr=0.001)
|
|
|
+
|
|
|
+# 混合精度训练的梯度缩放器
|
|
|
+scaler = GradScaler()
|
|
|
+
|
|
|
+# 生成一些随机输入数据
|
|
|
+inputs = torch.randn(16, 10).cuda()
|
|
|
+targets = torch.randint(0, 2, (16,)).cuda()
|
|
|
+
|
|
|
+# 定义损失函数
|
|
|
+criterion = torch.nn.CrossEntropyLoss()
|
|
|
+
|
|
|
+# 前向传播和反向传播
|
|
|
+with autocast():
|
|
|
+ outputs = model(inputs)
|
|
|
+ loss = criterion(outputs, targets)
|
|
|
+
|
|
|
+# 缩放损失
|
|
|
+scaler.scale(loss).backward()
|
|
|
+
|
|
|
+# 优化器步骤
|
|
|
+scaler.step(optimizer)
|
|
|
+scaler.update()
|
|
|
+```
|
|
|
+
|
|
|
+### 应用场景
|
|
|
+
|
|
|
+1. **大规模模型训练**: 通过量化和低精度训练减少内存和计算需求,加速大规模模型的训练过程。
|
|
|
+2. **推理优化**: 在推理阶段使用 8 位量化来减少模型大小,提高推理效率,特别适用于边缘设备和资源受限的环境。
|
|
|
+3. **节省计算资源**: 在现有计算资源不变的情况下,通过更高效的计算实现更大规模的模型训练,节省硬件成本。
|
|
|
+
|
|
|
+###
|
|
|
+
|
|
|
+- GitHub 仓库: [BitsAndBytes](https://github.com/TimDettmers/bitsandbytes)
|