跳到內容

TorchAO

TorchAO 是 PyTorch 的架構最佳化庫,它為推理和訓練提供了高效能的資料型別、最佳化技術和核心,並能與 torch.compile、FSDP 等原生 PyTorch 功能進行組合。一些基準測試結果可以在 這裡 找到。

我們建議使用以下命令安裝最新的 torchao nightly 版本:

# Install the latest TorchAO nightly build
# Choose the CUDA version that matches your system (cu126, cu128, etc.)
pip install \
    --pre torchao>=10.0.0 \
    --index-url https://download.pytorch.org/whl/nightly/cu126

量化 HuggingFace 模型

您可以使用 torchao 量化自己的 huggingface 模型,例如 transformersdiffusers,並透過以下示例程式碼將檢查點儲存到 huggingface hub,就像 這樣

程式碼
import torch
from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer
from torchao.quantization import Int8WeightOnlyConfig

model_name = "meta-llama/Meta-Llama-3-8B"
quantization_config = TorchAoConfig(Int8WeightOnlyConfig())
quantized_model = AutoModelForCausalLM.from_pretrained(
    model_name,
    dtype="auto",
    device_map="auto",
    quantization_config=quantization_config
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
input_text = "What are we having for dinner?"
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")

hub_repo = # YOUR HUB REPO ID
tokenizer.push_to_hub(hub_repo)
quantized_model.push_to_hub(hub_repo, safe_serialization=False)

或者,您可以使用 TorchAO 量化空間,透過簡單的使用者介面進行模型量化。