Bitsandbytes 文件

整合

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

整合

bitsandbytes 已廣泛整合到 Hugging Face 和更廣泛的 PyTorch 生態系統中的許多庫中。本指南簡要概述了這些整合以及如何使用 bitsandbytes。有關更多詳細資訊,您應參考每個庫的連結文件。

Transformers

在 bitsandbytes Transformers 整合指南中瞭解更多。

使用 Transformers,可以很容易地以 4 位或 8 位載入任何模型並動態量化它們。要配置量化引數,請在 BitsAndBytesConfig 類中指定它們。

例如,要將模型載入並量化為 4 位,並使用 bfloat16 資料型別進行計算

如果您的硬體支援,bfloat16 是理想的 `compute_dtype`。雖然預設的 `compute_dtype` float32 確保了向後相容性(由於硬體支援範圍廣)和數值穩定性,但它體積大且會減慢計算速度。相比之下,float16 更小更快,但可能導致數值不穩定。bfloat16 結合了兩者的優點;它提供了 float32 的數值穩定性以及 16 位資料型別的較小記憶體佔用和速度。檢查您的硬體是否支援 bfloat16,並使用 BitsAndBytesConfig 中的 `bnb_4bit_compute_dtype` 引數進行配置!

from transformers import AutoModelForCausalLM, BitsAndBytesConfig

quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16)
model_4bit = AutoModelForCausalLM.from_pretrained(
    "bigscience/bloom-1b7",
    device_map=device_map,
    quantization_config=quantization_config,
)

8 位最佳化器

您可以在初始化時將任何 8 位或分頁最佳化器傳遞給 Trainer 類,以便在 Transformers 中使用。所有 bitsandbytes 最佳化器都受支援,只需在 TrainingArguments 的 `optim` 引數中傳遞正確的字串即可。例如,載入一個 PagedAdamW32bit 最佳化器

from transformers import TrainingArguments, Trainer

training_args = TrainingArguments(
    ...,
    optim="paged_adamw_32bit",
)
trainer = Trainer(model, training_args, ...)
trainer.train()

PEFT

在 bitsandbytes PEFT 整合指南中瞭解更多。

PEFT 建立在 bitsandbytes Transformers 整合之上,並通過幾個額外的步驟將其擴充套件到訓練。讓我們為訓練準備上一節中的 4 位模型。

呼叫 `~peft.prepare_model_for_kbit_training` 方法來準備模型進行訓練。這僅適用於 Transformers 模型!

from peft import prepare_model_for_kbit_training

model_4bit = prepare_model_for_kbit_training(model_4bit)

設定一個 `~peft.LoraConfig` 以使用 QLoRA

from peft import LoraConfig

config = LoraConfig(
    r=16,
    lora_alpha=8,
    target_modules="all-linear",
    lora_dropout=0.05
    bias="none",
    task_type="CAUSAL_LM"
)

現在在您的模型和配置上呼叫 `~peft.get_peft_model` 函式,以建立一個可訓練的 `PeftModel`。

from peft import get_peft_model

model = get_peft_model(model_4bit, config)

Accelerate

在 bitsandbytes Accelerate 整合指南中瞭解更多。

bitsandbytes 也很容易從 Accelerate 中使用,您可以透過傳遞一個帶有您期望設定的 BnbQuantizationConfig 來量化任何 PyTorch 模型,然後呼叫 load_and_quantize_model 函式來量化它。

from accelerate import init_empty_weights
from accelerate.utils import BnbQuantizationConfig, load_and_quantize_model
from mingpt.model import GPT

model_config = GPT.get_default_config()
model_config.model_type = 'gpt2-xl'
model_config.vocab_size = 50257
model_config.block_size = 1024

with init_empty_weights():
    empty_model = GPT(model_config)

bnb_quantization_config = BnbQuantizationConfig(
  load_in_4bit=True,
  bnb_4bit_compute_dtype=torch.bfloat16,  # optional
  bnb_4bit_use_double_quant=True,         # optional
  bnb_4bit_quant_type="nf4"               # optional
)

quantized_model = load_and_quantize_model(
  empty_model,
  weights_location=weights_location,
  bnb_quantization_config=bnb_quantization_config,
  device_map = "auto"
)

PyTorch Lightning and Lightning Fabric

bitsandbytes 可用於

  • PyTorch Lightning,一個為需要最大靈活性而又不犧牲大規模效能的專業 AI 研究人員和機器學習工程師設計的深度學習框架。
  • Lightning Fabric,一種無需樣板程式碼即可擴充套件 PyTorch 模型的快速輕量級方法。

在 bitsandbytes PyTorch Lightning 整合指南中瞭解更多。

Lit-GPT

bitsandbytes 已與 Lit-GPT 整合,這是一個可自由修改的最先進開源大型語言模型實現。Lit-GPT 基於 Lightning Fabric,可用於訓練、微調和推理過程中的量化。

在 bitsandbytes Lit-GPT 整合指南中瞭解更多。

部落格文章

要更詳細地瞭解 bitsandbytes 的一些整合,請檢視以下部落格文章

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.