Skip to content

Conversation

@XGZhang11
Copy link
Contributor

@XGZhang11 XGZhang11 commented Apr 12, 2021

PR types

Performance optimization

PR changes

APIs

Describe

1.Add new methods: 'mse', 'hist', 'avg' of getting threshold values of activations for post-training quantization in 'post_training_quantization.py'.
2.Add bias correction method in of https://arxiv.org/abs/1810.05723 for post-training quantization in 'quantization_pass.py'. Bias correction changes the quantized weights by the following formulation:
image

Experimental results on 6-bit MobileNetV1 which calibrated by a batch of 32 images:
abs_max: 44.99 abs_max+bias_correction: 47.91
avg: 56.34 avg+bias_correction: 56.17
mse: 61.32 mse+bias_correction: 61.42
hist(0.9999): 60.72 hist+bias_correction: 62.44
KL: 53.11 KL+bias_correction: 58.46

Time cost: Abs_max and avg cost about 1 minute; hist and KL cost about 6 minutes; mse cost about 10 minutes. Bias correction cost little time.

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@wanghaoshuang
Copy link
Contributor

请为新增代码补充下单测。

Copy link
Contributor

@juncaipeng juncaipeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请添加相应单侧,要不然代码覆盖率达不到,参考../tests/下面的单侧示例。

不同方法的实验数据最好贴到pr上,包括量化模型精度、量化过程的时间等。

batch_size=10,
batch_nums=None,
algo="KL",
hist_perc=0.99999,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议用完整的hist_percent

hist_perc=0.99999,
quantizable_op_type=["conv2d", "depthwise_conv2d", "mul"],
is_full_quantize=False,
bias_correct=False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bias_correction?

if self._batch_nums and batch_id >= self._batch_nums:
break

if self._algo == 'avg':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这是获取阈值的逻辑,后面计算阈值的部分。

if self._algo == "abs_max":
self._sample_abs_max()
if self._algo in ["avg", "abs_max"]:
self._sample_abs_max_avg()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两个不相同的采样方式,分开成两个函数。

if mse_loss <= self._best_mse_loss[var_name]:
self._best_mse_loss[var_name] = mse_loss
best_scale = scale
if best_scale > 0.0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个判断没有必要,self._quantized_threshold[var_name] = best_scale 可以放到if mse_loss <= self._best_mse_loss[var_name]:中

save_info(
op_node, out_var_name, self._quantized_threshold,
argname_index[0] + str(argname_index[1]) + "_threshold",
"post_absmax")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

三种方法有点点差别,建议区分开来

if self._algo == 'avg':
if (var_name not in self._quantized_var_avg):
self._quantized_var_avg[var_name] = []
abs_avg_value = float(np.mean(np.max(np.abs(var_tensor.reshape(var_tensor.shape[0], -1)), axis=(1))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注意代码每行的长度

quantized_param_v = self._quant(
param_v, scale_v, self._weight_bits, quant_axis)
param_v.copy(), scale_v, self._weight_bits, quant_axis)
if self._bias_correct == True:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

将bias_correction功能独立为一个函数实现

if isinstance(scale_v, list):
if quant_axis == 0:
for i, s in enumerate(scale_v):
quantized_param_v[i] = quantized_param_v[i] * s / bnt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这是dequantized_param_v了

@XGZhang11 XGZhang11 requested a review from juncaipeng April 13, 2021 09:39
Copy link
Contributor

@juncaipeng juncaipeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wanghaoshuang wanghaoshuang merged commit 4281eb4 into PaddlePaddle:develop Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants