W8A8
简介
此量化方式对权重和激活值同时进行量化,将高位浮点数转为8 bit,减少模型权重的体积。使用int8格式的数据进行计算,可以减少MatMul算子计算量,以提升推理性能。
量化后权重目录结构:
├─ 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中的部分内容:
{ "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 |
float16 |
float16 |
int32 |
float32 |
shape |
[n, k] |
[1] |
[1] |
[n] |
[n] |
生成权重
以LLaMa 2 70B为例,您可以使用以下指令生成W8A8量化权重。
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 cd ${ATB_SPEED_HOME_PATH} python examples/models/llama/convert_quant_weights.py --model_path {浮点权重路径} --save_directory {W8A8量化权重路径} --w_bit 8 --a_bit 8 --disable_level L5 --device_type npu --calib_file ${llm_path}/examples/convert/model_slim/boolq.jsonl
- 以上指令展示了生成LLaMa 2 70B W8A8权重的最优参数配置,不同模型的参数配置不同,请参考模型Readme文件。
- 生成W8A8量化权重时,支持NPU多卡校准,可以显著提升校准时间。
- W8A8量化权重的config.json中应包含quantize字段,其值为"w8a8"。
执行推理
以LLaMa 2 70B为例,您可以使用以下指令执行对话测试,推理内容为"What's deep learning"。
cd ${ATB_SPEED_HOME_PATH} bash examples/models/llama/run_pa.sh {W8A8量化权重路径}
父主题: 量化特性介绍