Transformers documentation

FP-Quant

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v5.22.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

FP-Quant

FP-Quant 是一组专为 Nvidia Blackwell 代 GPU 设计的量化算法。其目标是支持大语言模型(LLM)在 MXFP4 和 NVFP4 数据类型 下高效地进行训练后量化(PTQ)和量化感知训练(QAT)。

此集成随 弥合微缩放 FP4 量化中承诺与性能之间的差距 预印本一同发布。

目前,仅支持在 pseudoquantization=True 下使用 QAT。模型可以通过 quantization_config=FPQuantConfig() 进行即时量化:

from transformers import AutoModelForCausalLM, AutoTokenizer, FPQuantConfig
import torch

model = AutoModelForCausalLM.from_pretrained(
    "qwen/Qwen3-8B",
    quantization_config=FPQuantConfig(),
    device_map="auto",
    dtype=torch.bfloat16,
)

或经过 GPTQ 预处理以获得更好的质量(参见 FP 格式量化测试框架)。

您可以通过 FPQuantConfig(forward_dtype="mxfp4") 在 MXFP4 和 NVFP4 之间进行选择。NVFP4 的质量更好,但会占用稍多的内存。

运行这些内核需要 Blackwell 代 GPU。FP-Quant 的运行时支持通过 QuTLASS 库和一个轻量级 PyTorch 接口库 fp_quant 实现。我们建议 从源码 安装前者,并使用 pip install fp_quant 安装后者。

没有 Blackwell 代 GPU 的用户,可以使用 quantization_config=FPQuantConfig(pseudoquantization=True) 来运行该方法,而无需安装 QuTLASS。这种方式不会带来任何加速,但能完全模拟量化的效果。

在 ISTA-DASLab 的官方集合中查找使用 FP-Quant 预量化的模型。

torch.compile

FP-Quant 与 torch.compile 完全兼容。

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, FPQuantConfig

model = AutoModelForCausalLM.from_pretrained(
    "qwen/Qwen3-8B",
    quantization_config=FPQuantConfig(),
    device_map="auto",
    dtype=torch.bfloat16,
)

model.forward = torch.compile(model.forward, mode="max-autotune", fullgraph=True)

加速

FP-Quant 目前在大批量处理时表现最佳。

有关加速效果,请参阅 QuTLASS README。

Update on GitHub