Transformers 文档 (Transformers documentation)
夸克 (Quark)
并获得增强的文档体验 (and get access to the augmented documentation experience)
开始使用 (to get started)
Quark
Quark 是一个深度学习量化工具包,旨在对特定的数据类型、算法和硬件保持不可知。不同的预处理策略、算法和数据类型可以在 Quark 中组合使用。
通过 🤗 Transformers 集成的 PyTorch 支持主要面向 AMD CPU 和 GPU,主要用于评估目的。例如,可以使用 lm-evaluation-harness 与 🤗 Transformers 后端,并无缝评估通过 Quark 量化的大量模型。
对 Quark 感兴趣的用户可以参考其文档,开始量化模型并在受支持的开源库中使用它们!
虽然 Quark 有自己的检查点 / 配置格式,但该库也支持生成具有与其他量化/运行时实现兼容的序列化布局的模型(AutoAWQ,🤗 Transformers 中的原生 fp8)。
为了能够在 Transformers 中加载 Quark 量化模型,首先需要安装该库
pip install amd-quark
支持矩阵 (Support matrix)
通过 Quark 量化的模型支持各种可以组合在一起的功能。所有量化模型,无论其配置如何,都可以通过 PretrainedModel.from_pretrained
无缝重新加载。
下表显示了 Quark 支持的一些特性 (features)
特性 (Feature) | Quark 中支持的子集 (Supported subset in Quark) | |
---|---|---|
数据类型 (Data types) | int8, int4, int2, bfloat16, float16, fp8_e5m2, fp8_e4m3, fp6_e3m2, fp6_e2m3, fp4, OCP MX, MX6, MX9, bfp16 | |
量化前转换 (Pre-quantization transformation) | SmoothQuant, QuaRot, SpinQuant, AWQ | |
量化算法 (Quantization algorithm) | GPTQ | |
支持的运算符 (Supported operators) | nn.Linear , nn.Conv2d , nn.ConvTranspose2d , nn.Embedding , nn.EmbeddingBag | |
粒度 (Granularity) | per-tensor, per-channel, per-block, per-layer, per-layer type | |
KV 缓存 (KV cache) | fp8 | |
激活校准 (Activation calibration) | MinMax / Percentile / MSE | |
量化策略 (Quantization strategy) | weight-only, static, dynamic, with or without output quantization |
Hugging Face Hub 上的模型 (Models on Hugging Face Hub)
使用 Quark 原生序列化的公共模型可以在 https://huggingface.co/models?other=quark 找到。
虽然 Quark 也支持 使用 quant_method="fp8"
的模型 和 使用 quant_method="awq"
的模型,但 Transformers 更倾向于通过 AutoAWQ 加载这些模型,或者使用 🤗 Transformers 中的原生 fp8 支持。
在 Transformers 中使用 Quark 模型 (Using Quark models in Transformers)
以下是如何在 Transformers 中加载 Quark 模型的示例
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "EmbeddedLLM/Llama-3.1-8B-Instruct-w_fp8_per_channel_sym"
model = AutoModelForCausalLM.from_pretrained(model_id)
model = model.to("cuda")
print(model.model.layers[0].self_attn.q_proj)
# QParamsLinear(
# (weight_quantizer): ScaledRealQuantizer()
# (input_quantizer): ScaledRealQuantizer()
# (output_quantizer): ScaledRealQuantizer()
# )
tokenizer = AutoTokenizer.from_pretrained(model_id)
inp = tokenizer("Where is a good place to cycle around Tokyo?", return_tensors="pt")
inp = inp.to("cuda")
res = model.generate(**inp, min_new_tokens=50, max_new_tokens=100)
print(tokenizer.batch_decode(res)[0])
# <|begin_of_text|>Where is a good place to cycle around Tokyo? There are several places in Tokyo that are suitable for cycling, depending on your skill level and interests. Here are a few suggestions:
# 1. Yoyogi Park: This park is a popular spot for cycling and has a wide, flat path that's perfect for beginners. You can also visit the Meiji Shrine, a famous Shinto shrine located in the park.
# 2. Imperial Palace East Garden: This beautiful garden has a large, flat path that's perfect for cycling. You can also visit the