Transformers 文件

夸克

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

夸克

Quark 是一款深度學習量化工具包,旨在與特定的資料型別、演算法和硬體無關。不同的預處理策略、演算法和資料型別可以在 Quark 中組合使用。

透過 🤗 Transformers 整合的 PyTorch 支援主要針對 AMD CPU 和 GPU,主要用於評估。例如,可以使用 lm-evaluation-harness 結合 🤗 Transformers 後端,無縫評估透過 Quark 量化的大範圍模型。

對 Quark 感興趣的使用者可以查閱其文件,開始量化模型並在受支援的開源庫中使用它們!

儘管 Quark 有其自己的檢查點 / 配置格式,該庫還支援生成符合其他量化/執行時實現(AutoAWQ🤗 Transformers 中的原生 fp8)序列化佈局的模型。

為了能夠在 Transformers 中載入 Quark 量化模型,需要先安裝該庫

pip install amd-quark

支援矩陣

透過 Quark 量化的模型支援大量功能,這些功能可以組合使用。所有量化模型,無論其配置如何,都可以透過 PretrainedModel.from_pretrained 無縫重新載入。

下表顯示了 Quark 支援的一些功能

特性 Quark 中支援的子集
資料型別 int8、int4、int2、bfloat16、float16、fp8_e5m2、fp8_e4m3、fp6_e3m2、fp6_e2m3、fp4、OCP MX、MX6、MX9、bfp16
預量化轉換 SmoothQuant、QuaRot、SpinQuant、AWQ
量化演算法 GPTQ
支援的運算子 nn.Linearnn.Conv2dnn.ConvTranspose2dnn.Embeddingnn.EmbeddingBag
粒度 逐張量、逐通道、逐塊、逐層、逐層型別
KV 快取 fp8
啟用校準 MinMax / 百分位 / MSE
量化策略 僅權重、靜態、動態、帶或不帶輸出量化

Hugging Face Hub 上的模型

使用 Quark 原生序列化的公共模型可以在 https://huggingface.co/models?other=quark 找到。

儘管 Quark 還支援使用 quant_method="fp8" 的模型使用 quant_method="awq" 的模型,但 Transformers 更傾向於透過 AutoAWQ 載入這些模型,或使用 🤗 Transformers 中的原生 fp8 支援

在 Transformers 中使用 Quark 模型

以下是如何在 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
< > 在 GitHub 上更新

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