# FBGEMM

[FBGEMM (Facebook GEneral Matrix Multiplication)](https://github.com/pytorch/FBGEMM) là thư viện nhân ma trận độ chính xác thấp cho kích thước lô nhỏ và hỗ trợ các kỹ thuật giảm thiểu mất độ chính xác như lượng tử hóa theo hàng và lượng tử hóa nhận diện ngoại lệ. Với FBGEMM, hãy lượng tử hóa trọng số của mô hình xuống 8-bit/kênh và kích hoạt xuống 8-bit/token (còn được gọi là fp8 hoặc w8a8).

> [!TIP]
> Bạn cần một GPU có [compute capability 9+](https://developer.nvidia.com/cuda-gpus#collapseOne) như H100.

Cài đặt gói FBGEMM_GPU bằng lệnh bên dưới để đảm bảo bạn có phiên bản mới nhất.

```bash
pip install --upgrade accelerate fbgemm-gpu torch
```

Nếu bạn gặp sự cố khi cài đặt, hãy thử cài đặt [bản phát hành hàng đêm](https://pytorch.org/FBGEMM/fbgemm_gpu-development/InstallationInstructions.html#fbgemm-gpu-install-libraries:~:text=found%20here.-,Install%20the%20FBGEMM_GPU%20Package,-Install%20through%20PyTorch).

Tạo một [FbgemmFp8Config](/docs/transformers/v5.23.0/vi/main_classes/quantization#transformers.FbgemmFp8Config) và truyền nó vào [from_pretrained()](/docs/transformers/v5.23.0/vi/main_classes/model#transformers.PreTrainedModel.from_pretrained) để lượng tử hóa mô hình sang fp8.

```py
from transformers import FbgemmFp8Config, AutoModelForCausalLM

quantization_config = FbgemmFp8Config()
quantized_model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Meta-Llama-3-8B",
    dtype="auto",
    device_map="auto",
    quantization_config=quantization_config
)
```

[save_pretrained()](/docs/transformers/v5.23.0/vi/main_classes/model#transformers.PreTrainedModel.save_pretrained) và [from_pretrained()](/docs/transformers/v5.23.0/vi/main_classes/model#transformers.PreTrainedModel.from_pretrained) cho phép lưu và tải một mô hình đã lượng tử hóa.

```py
quant_path = "/path/to/save/quantized/model"
quantized_model.save_pretrained(quant_path)
model = AutoModelForCausalLM.from_pretrained(quant_path, device_map="auto")
```

## Tài nguyên

Đọc bài đăng trên blog [Open-sourcing FBGEMM for state-of-the-art server-side inference](https://engineering.fb.com/2018/11/07/ml-applications/fbgemm/) để biết thêm chi tiết về FBGEMM.

