Skip to content

Commit 217a25c

Browse files
authored
supports strategy 'qat' (PaddlePaddle#3271)
1 parent c00d28f commit 217a25c

File tree

5 files changed

+505
-253
lines changed

5 files changed

+505
-253
lines changed

docs/compression.md

+51-35
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* [Step1:获取模型压缩参数 compression_args](#获取模型压缩参数compression_args)
77
* [Step2:实例化 Trainer 并调用 compress()](#实例化Trainer并调用compress())
88
* [Trainer 实例化参数介绍](#Trainer实例化参数介绍)
9-
* [Step3:实现自定义评估函数和 loss 计算函数(按需可选)](#实现自定义评估函数和loss计算函数(按需可选))
9+
* [Step3:实现自定义评估函数(按需可选)](#实现自定义评估函数(按需可选))
1010
* [Step4:传参并运行压缩脚本](#传参并运行压缩脚本)
1111
* [CompressionArguments 参数介绍](#CompressionArguments参数介绍)
1212
* [三大场景模型压缩 API 使用示例](#三大场景模型压缩API使用示例)
@@ -118,11 +118,45 @@ compression_args = parser.parse_args_into_dataclasses()
118118
#### Trainer 实例化参数介绍
119119

120120
- **--model** 待压缩的模型,目前支持 ERNIE、BERT、RoBERTa、ERNIE-M、ELECTRA、ERNIE-Gram、PP-MiniLM、TinyBERT 等结构相似的模型,是在下游任务中微调后的模型,当预训练模型选择 ERNIE 时,需要继承 `ErniePretrainedModel`。以分类任务为例,可通过`AutoModelForSequenceClassification.from_pretrained(model_name_or_path)` 等方式来获取,这种情况下,`model_name_or_path`目录下需要有 model_config.json, model_state.pdparams 文件;
121-
- **--data_collator** 三类任务均可使用 PaddleNLP 预定义好的 [DataCollator 类](../../paddlenlp/data/data_collator.py)`data_collator` 可对数据进行 `Pad` 等操作。使用方法参考 [示例代码](../model_zoo/ernie-3.0/compress_seq_cls.py) 即可;
121+
- **--data_collator** 三类任务均可使用 PaddleNLP 预定义好的 [DataCollator 类](../paddlenlp/data/data_collator.py)`data_collator` 可对数据进行 `Pad` 等操作。使用方法参考 [示例代码](../model_zoo/ernie-3.0/compress_seq_cls.py) 即可;
122122
- **--train_dataset** 裁剪训练需要使用的训练集,是任务相关的数据。自定义数据集的加载可参考 [文档](https://huggingface.co/docs/datasets/loading)。不启动裁剪时,可以为 None;
123123
- **--eval_dataset** 裁剪训练使用的评估集,也是量化使用的校准数据,是任务相关的数据。自定义数据集的加载可参考 [文档](https://huggingface.co/docs/datasets/loading)。是 Trainer 的必选参数;
124124
- **--tokenizer** 模型 `model` 对应的 `tokenizer`,可使用 `AutoTokenizer.from_pretrained(model_name_or_path)` 来获取。
125-
- **--criterion** 模型的 loss 对象,是一个 nn.Layer 对象,用于在 ofa_utils.py 计算模型的 loss 用于计算梯度从而确定神经元重要程度。
125+
- **--criterion** 模型的 loss 计算方法,可以是一个 nn.Layer 对象,也可以是一个函数,用于在 ofa_utils.py 计算模型的 loss 用于计算梯度从而确定神经元重要程度。
126+
127+
其中,`criterion` 函数定义示例:
128+
129+
```python
130+
# 支持的形式一:
131+
def criterion(logits, labels):
132+
loss_fct = paddle.nn.BCELoss()
133+
start_ids, end_ids = labels
134+
start_prob, end_prob = outputs
135+
start_ids = paddle.cast(start_ids, 'float32')
136+
end_ids = paddle.cast(end_ids, 'float32')
137+
loss_start = loss_fct(start_prob, start_ids)
138+
loss_end = loss_fct(end_prob, end_ids)
139+
loss = (loss_start + loss_end) / 2.0
140+
return loss
141+
142+
# 支持的形式二:
143+
class CrossEntropyLossForSQuAD(paddle.nn.Layer):
144+
145+
def __init__(self):
146+
super(CrossEntropyLossForSQuAD, self).__init__()
147+
148+
def forward(self, y, label):
149+
start_logits, end_logits = y
150+
start_position, end_position = label
151+
start_position = paddle.unsqueeze(start_position, axis=-1)
152+
end_position = paddle.unsqueeze(end_position, axis=-1)
153+
start_loss = paddle.nn.functional.cross_entropy(input=start_logits,
154+
label=start_position)
155+
end_loss = paddle.nn.functional.cross_entropy(input=end_logits,
156+
label=end_position)
157+
loss = (start_loss + end_loss) / 2
158+
return loss
159+
```
126160

127161
用以上参数实例化 Trainer 对象,之后直接调用 `compress()``compress()` 会根据选择的策略进入不同的分支,以进行裁剪或者量化的过程。
128162

@@ -147,11 +181,11 @@ trainer = Trainer(
147181
trainer.compress()
148182
```
149183

150-
<a name="实现自定义评估函数和loss计算函数(按需可选)"></a>
184+
<a name="实现自定义评估函数(按需可选)"></a>
151185

152-
### Step3:实现自定义评估函数和 loss 计算函数(按需可选),以适配自定义压缩任务
186+
### Step3:实现自定义评估函数,以适配自定义压缩任务
153187

154-
当使用 DynaBERT 裁剪功能时,如果模型、Metrics 不符合下表的情况,那么模型压缩 API 中自带的评估函数和计算 loss 的参数可能需要自定义
188+
当使用 DynaBERT 裁剪功能时,如果模型、Metrics 不符合下表的情况,那么模型压缩 API 中评估函数需要自定义
155189

156190
目前 DynaBERT 裁剪功能只支持 SequenceClassification 等三类 PaddleNLP 内置 class,并且内置评估器对应为 Accuracy、F1、Squad。
157191

@@ -163,33 +197,26 @@ trainer.compress()
163197

164198
- 如果模型是自定义模型,需要继承 `XXXPretrainedModel`,例如当预训练模型选择 ERNIE 时,继承 `ErniePretrainedModel`,模型需要支持调用 `from_pretrained()` 导入模型,且只含 `pretrained_model_name_or_path` 一个必选参数,`forward` 函数返回 `logits` 或者 `tuple of logits`
165199

166-
- 如果模型是自定义模型,或者数据集比较特殊,压缩 API 中 loss 的计算不符合使用要求,需要自定义 `custom_dynabert_calc_loss` 函数。计算 loss 后计算梯度,从而得出计算神经元的重要性以便裁剪使用。可参考下方示例代码。
167-
- 输入每个 batch 的数据,返回模型的 loss。
168-
- 将该函数传入 `compress()` 中的 `custom_dynabert_calc_loss` 参数;
169-
170-
- 如果评估器也不满足上述所支持情况,需实现自定义 `custom_dynabert_evaluate` 评估函数,需要同时支持 `paddleslim.nas.ofa.OFA` 模型和 `paddle.nn.layer` 模型。可参考下方示例代码。
200+
- 如果模型是自定义模型,或者数据集比较特殊,压缩 API 中 loss 的计算不符合使用要求,需要自定义 `custom_evaluate` 评估函数,需要同时支持 `paddleslim.nas.ofa.OFA` 模型和 `paddle.nn.layer` 模型。可参考下方示例代码。
171201
- 输入`model``dataloader`,返回模型的评价指标(单个 float 值)。
172-
- 将该函数传入 `compress()` 中的 `custom_dynabert_evaluate` 参数;
202+
- 将该函数传入 `compress()` 中的 `custom_evaluate` 参数;
173203

174-
`custom_dynabert_evaluate()` 函数定义示例:
204+
`custom_evaluate()` 函数定义示例:
175205

176206
```python
177207
import paddle
178208
from paddle.metric import Accuracy
179-
from paddleslim.nas.ofa import OFA
180209

181210
@paddle.no_grad()
182-
def evaluate_seq_cls(model, data_loader):
211+
def evaluate_seq_cls(self, model, data_loader):
183212
metric = Accuracy()
184213
model.eval()
185214
metric.reset()
186215
for batch in data_loader:
187216
logits = model(input_ids=batch['input_ids'],
188-
token_type_ids=batch['token_type_ids'],
189-
#必须写这一行
190-
attention_mask=[None, None])
217+
token_type_ids=batch['token_type_ids'])
191218
# Supports paddleslim.nas.ofa.OFA model and nn.layer model.
192-
if isinstance(model, OFA):
219+
if isinstance(model, paddleslim.nas.ofa.OFA):
193220
logits = logits[0]
194221
correct = metric.compute(logits, batch['labels'])
195222
metric.update(correct)
@@ -199,22 +226,11 @@ trainer.compress()
199226
return res
200227
```
201228

202-
`custom_dynabert_calc_loss` 函数定义示例:
203229

204-
```python
205-
def calc_loss(loss_fct, model, batch, head_mask):
206-
logits = model(input_ids=batch["input_ids"],
207-
token_type_ids=batch["token_type_ids"],
208-
# 必须写下面这行
209-
attention_mask=[None, head_mask])
210-
loss = loss_fct(logits, batch["labels"])
211-
return loss
212-
```
213-
在调用 `compress()` 时传入这 2 个自定义函数:
230+
在调用 `compress()` 时传入这个自定义函数:
214231

215232
```python
216-
trainer.compress(custom_dynabert_evaluate=evaluate_seq_cls,
217-
custom_dynabert_calc_loss=calc_loss)
233+
trainer.compress(custom_evaluate=evaluate_seq_cls)
218234
```
219235

220236

@@ -253,8 +269,8 @@ python compress.py \
253269

254270
公共参数中的参数和具体的压缩策略无关。
255271

256-
- **--strategy** 模型压缩策略,目前支持 `'dynabert+ptq'``'dynabert'` `'ptq'`
257-
其中 `'dynabert'` 代表基于 DynaBERT 的宽度裁剪策略,`'ptq'` 表示静态离线量化, `'dynabert+ptq'` 代表先裁剪后量化。默认是 `'dynabert+ptq'`
272+
- **--strategy** 模型压缩策略,目前支持 `'dynabert+ptq'``'dynabert'` `'ptq'``'qat'`
273+
其中 `'dynabert'` 代表基于 DynaBERT 的宽度裁剪策略,`'ptq'` 表示静态离线量化, `'dynabert+ptq'` 代表先裁剪后量化。`qat` 表示量化训练。默认是 `'dynabert+ptq'`
258274

259275
- **--output_dir** 模型压缩后模型保存目录;
260276

@@ -387,7 +403,7 @@ python compress_qa.py \
387403

388404
### Paddle2ONNX 部署
389405

390-
ONNX 导出及 ONNXRuntime 部署请参考:[ONNX 导出及 ONNXRuntime 部署指南](./deploy/paddle2onnx/README.md)
406+
ONNX 导出及 ONNXRuntime 部署请参考:[ONNX 导出及 ONNXRuntime 部署指南](../model_zoo/ernie-3.0/deploy/paddle2onnx/README.md)
391407

392408

393409
### Paddle Lite 移动端部署

model_zoo/ernie-3.0/compress_qa.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from functools import partial
1818

1919
import paddle
20+
import paddle.nn.functional as F
2021

2122
from paddlenlp.data import DataCollatorWithPadding
2223
from paddlenlp.trainer import PdArgumentParser, CompressionArguments, Trainer
@@ -114,6 +115,16 @@ def post_processing_function(examples, features, predictions, stage="eval"):
114115
} for ex in examples]
115116
return EvalPrediction(predictions=predictions, label_ids=references)
116117

118+
def criterion(outputs, label):
119+
start_logits, end_logits = outputs
120+
start_position, end_position = label
121+
start_position = paddle.unsqueeze(start_position, axis=-1)
122+
end_position = paddle.unsqueeze(end_position, axis=-1)
123+
start_loss = F.cross_entropy(input=start_logits, label=start_position)
124+
end_loss = F.cross_entropy(input=end_logits, label=end_position)
125+
loss = (start_loss + end_loss) / 2
126+
return loss
127+
117128
trainer = QuestionAnsweringTrainer(
118129
model=model,
119130
args=compression_args,
@@ -122,7 +133,8 @@ def post_processing_function(examples, features, predictions, stage="eval"):
122133
eval_examples=eval_examples,
123134
data_collator=data_collator,
124135
post_process_function=post_processing_function,
125-
tokenizer=tokenizer)
136+
tokenizer=tokenizer,
137+
criterion=criterion)
126138

127139
compression_args.print_config()
128140

paddlenlp/trainer/compression_args.py

+61-13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CompressionArguments(TrainingArguments):
4646
default="dynabert+ptq",
4747
metadata={
4848
"help":
49-
"Compression strategy. It supports 'dynabert+ptq', 'dynabert' and 'ptq' now."
49+
"Compression strategy. It supports 'dynabert+ptq', 'dynabert', 'ptq' and 'qat' now."
5050
},
5151
)
5252
# dynabert
@@ -69,6 +69,25 @@ class CompressionArguments(TrainingArguments):
6969
metadata={
7070
"help": "Linear warmup over warmup_ratio fraction of total steps."
7171
})
72+
# quant
73+
weight_quantize_type: Optional[str] = field(
74+
default='channel_wise_abs_max',
75+
metadata={
76+
"help":
77+
"Quantization type for weights. Supports 'abs_max' and 'channel_wise_abs_max'. " \
78+
"This param only specifies the fake ops in saving quantized model, and " \
79+
"we save the scale obtained by post training quantization in fake ops. " \
80+
"Compared to 'abs_max' the model accuracy is usually higher when it is " \
81+
"'channel_wise_abs_max'."
82+
}, )
83+
activation_quantize_type: Optional[str] = field(
84+
default=None,
85+
metadata={
86+
"help":
87+
"Support 'abs_max', 'range_abs_max' and 'moving_average_abs_max'. " \
88+
"In strategy 'ptq', it defaults to 'range_abs_max' and in strategy " \
89+
"'qat', it defaults to 'moving_average_abs_max'."
90+
}, )
7291
# ptq:
7392
algo_list: Optional[List[str]] = field(
7493
default=None,
@@ -100,15 +119,7 @@ class CompressionArguments(TrainingArguments):
100119
"List of batch_size. 'batch_size' is the batch of data loader."
101120
},
102121
)
103-
weight_quantize_type: Optional[str] = field(
104-
default='channel_wise_abs_max',
105-
metadata={
106-
"help":
107-
"Support 'abs_max' and 'channel_wise_abs_max'. This param only specifies " \
108-
"the fake ops in saving quantized model, and we save the scale obtained " \
109-
"by post training quantization in fake ops. Compared to 'abs_max', " \
110-
"the model accuracy is usually higher when it is 'channel_wise_abs_max'."
111-
}, )
122+
112123
round_type: Optional[str] = field(
113124
default='round',
114125
metadata={
@@ -135,16 +146,45 @@ class CompressionArguments(TrainingArguments):
135146
"is None."
136147
},
137148
)
149+
# qat
150+
activation_preprocess_type: Optional[str] = field(
151+
default=None,
152+
metadata={
153+
"help":
154+
"Method of preprocessing the activation value of the quantitative " \
155+
"model. Currently, PACT method is supported. If necessary, it can be " \
156+
"set to 'PACT'. The default value is None, which means that no " \
157+
"preprocessing is performed on the active value."
158+
},
159+
)
160+
weight_preprocess_type: Optional[str] = field(
161+
default=None,
162+
metadata={
163+
"help":
164+
"Method of preprocessing the weight parameters of the quantitative " \
165+
"model. Currently, method 'PACT' is supported. If necessary, it can " \
166+
"be set to 'PACT'. The default value is None, which means that " \
167+
"no preprocessing is performed on weights."
168+
},
169+
)
170+
moving_rate: Optional[float] = field(
171+
default=0.9,
172+
metadata={
173+
"help": "The decay coefficient of moving average. Defaults to 0.9."
174+
},
175+
)
138176

139177
def print_config(self, args=None, key=""):
140178
"""
141179
Prints all config values.
142180
"""
143181

144182
compression_arg_name = [
145-
'width_mult_list', 'batch_num_list', 'bias_correction',
183+
'strategy', 'width_mult_list', 'batch_num_list', 'bias_correction',
146184
'round_type', 'algo_list', 'batch_size_list', 'strategy',
147-
'weight_quantize_type', 'input_infer_model_path'
185+
'weight_quantize_type', 'activation_quantize_type',
186+
'input_infer_model_path', 'activation_preprocess_type',
187+
'weight_preprocess_type', 'moving_rate'
148188
]
149189
default_arg_dict = {
150190
"width_mult_list": ['3/4'],
@@ -163,7 +203,9 @@ def print_config(self, args=None, key=""):
163203
"'dynabert' and 'ptq'. `width_mult_list` is needed in " \
164204
"`dynabert`, and `algo_list`, `batch_num_list`, `batch_size_list`," \
165205
" `round_type`, `bias_correction`, `weight_quantize_type`, " \
166-
"`input_infer_model_path` are needed in 'ptq'. "
206+
"`input_infer_model_path` are needed in 'ptq'. `activation_preprocess_type'`, " \
207+
"'weight_preprocess_type', 'moving_rate', 'weight_quantize_type', " \
208+
"and 'activation_quantize_type' are needed in 'qat'."
167209
)
168210
logger.info('{:30}:{}'.format("paddle commit id",
169211
paddle.version.commit))
@@ -176,6 +218,12 @@ def print_config(self, args=None, key=""):
176218
if v is None and arg in default_arg_dict:
177219
v = default_arg_dict[arg]
178220
setattr(args, arg, v)
221+
elif v is None and arg == 'activation_quantize_type':
222+
if key == "Compression" and 'ptq' in args.strategy:
223+
setattr(args, arg, 'range_abs_max')
224+
elif key == "Compression" and 'qat' in args.strategy:
225+
setattr(args, arg, 'moving_average_abs_max')
226+
179227
if not isinstance(v, types.MethodType):
180228
logger.info('{:30}:{}'.format(arg, v))
181229

0 commit comments

Comments
 (0)