W8A8
简介
此量化方式对权重和激活值均进行量化,将高位浮点数转为8 bit,减少模型权重的体积。使用int8格式的数据进行计算,可以减少MatMul算子计算量,以提升推理性能。

- 仅支持BaiChuan2-7B,BaiChuan2-13B,ChatGLM2-6B,CodeGeeX2-6B,CodeLLamA-34B,Gemma-7B,LLaMA2-13B,LLaMA2-7B,LLaMA2-70B,LLaMA3.1-70B,LLaMA-65B,DeepSeek-V2,Qwen2.5-72B,Qwen2.5-57B,Qwen2.5-32B,Qwen2.5-14B,Qwen2.5-7B,Qwen1.5-32B,Qwen1.5-chat-14B,Qwen-14B,StarCoder2-15B,StarCoder-15.5B,llava-v1.5-13B,llava-v1.5-7B,qwen-vl和Stable Diffusion XL。
- 仅支持和PD分离、SplitFuse、Prefix Cache、并行解码、长序列以及多机推理特性配合使用。
- 仅支持和Attention、KV Cache量化配合使用。
量化后权重目录结构:
├─ config.json ├─ quant_model_weight_w8a8.safetensors ├─ quant_model_description_w8a8.json ├─ tokenizer_config.json ├─ tokenizer.json └─ tokenizer.model
- 量化输出包含:权重文件quant_model_weight_w8a8.safetensors和权重描述文件quant_model_description_w8a8.json。
- 目录中的其余文件为推理时所需的配置文件,不同模型略有差异。
以下展示了量化后权重描述文件quant_model_description_w8a8.json中的部分内容:
1 2 3 4 5 6 7 8 9 |
{ "model_quant_type": "W8A8", "model.embed_tokens.weight": "FLOAT", "model.layers.0.self_attn.q_proj.weight": "W8A8", "model.layers.0.self_attn.q_proj.input_scale": "W8A8", "model.layers.0.self_attn.q_proj.input_offset": "W8A8", "model.layers.0.self_attn.q_proj.quant_bias": "W8A8", "model.layers.0.self_attn.q_proj.deq_scale": "W8A8", } |
量化后的MatMul权重新增input_scale、input_offset、quant_bias和deq_scale。其中input_scale和input_offset用于对激活值进行量化。MatMul使用量化后的激活值和量化权重进行计算。quant_bias和deq_scale用于对MatMul的计算结果进行反量化。
图1 量化权重推理时流程

此量化方式支持量化float16或bfloat16类型的原始权重。
Tensor信息 |
weight |
input_scale |
input_offset |
quant_bias |
deq_scale |
---|---|---|---|---|---|
dtype |
int8 |
float16 |
float16 |
int32 |
int64 |
shape |
[n, k] |
[1] |
[1] |
[n] |
[n] |
Tensor信息 |
weight |
input_scale |
input_offset |
quant_bias |
deq_scale |
---|---|---|---|---|---|
dtype |
int8 |
bfloat16 |
bfloat16 |
int32 |
float32 |
shape |
[n, k] |
[1] |
[1] |
[n] |
[n] |
生成权重
以LLaMA3.1-8B为例,您可以使用以下指令生成W8A8量化权重。
cd ${ATB_SPEED_HOME_PATH} python examples/models/llama3/convert_quant_weights.py --model_path {浮点权重路径} --save_directory {W8A8量化权重路径} --w_bit 8 --a_bit 8 --disable_level L0 --device_type cpu --anti_method m1 --act_method 1 --calib_file ${llm_path}/examples/convert/model_slim/boolq.jsonl
- 以上指令展示了生成LLaMA3.1-8B W8A8权重的最优参数配置,不同模型的参数配置不同,请参考模型Readme文件。
- W8A8量化权重的config.json中应包含quantize字段,其值为"w8a8"。
执行推理
以LLaMA3.1-8B为例,您可以使用以下指令执行对话测试,推理内容为"What's deep learning?",最长输出20个token。
cd ${ATB_SPEED_HOME_PATH}
bash examples/models/llama3/run_pa.sh {W8A8量化权重路径} 20
父主题: 量化特性介绍