From bc14da69cca1cf26f6437711a966ab86100c2316 Mon Sep 17 00:00:00 2001 From: Liufang Sang Date: Wed, 12 Feb 2020 00:12:16 -0600 Subject: [PATCH] update quant_post api (#105) --- docs/zh_cn/api_cn/quantization_api.rst | 4 +++- paddleslim/quant/quanter.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/zh_cn/api_cn/quantization_api.rst b/docs/zh_cn/api_cn/quantization_api.rst index e8080183b0c02..faa37918d4ec5 100644 --- a/docs/zh_cn/api_cn/quantization_api.rst +++ b/docs/zh_cn/api_cn/quantization_api.rst @@ -174,7 +174,7 @@ convert quant_post --------------- -.. py:function:: paddleslim.quant.quant_post(executor, model_dir, quantize_model_path,sample_generator, model_filename=None, params_filename=None, batch_size=16,batch_nums=None, scope=None, algo='KL', quantizable_op_type=["conv2d", "depthwise_conv2d", "mul"], is_full_quantize=False, is_use_cache_file=False, cache_dir="./temp_post_training") +.. py:function:: paddleslim.quant.quant_post(executor, model_dir, quantize_model_path,sample_generator, model_filename=None, params_filename=None, batch_size=16,batch_nums=None, scope=None, algo='KL', quantizable_op_type=["conv2d", "depthwise_conv2d", "mul"], is_full_quantize=False, weight_bits=8, activation_bits=8, is_use_cache_file=False, cache_dir="./temp_post_training") `源代码 `_ @@ -194,6 +194,8 @@ quant_post - **algo(str)** - 量化时使用的算法名称,可为 ``'KL'`` 或者 ``'direct'`` 。该参数仅针对激活值的量化,因为参数值的量化使用的方式为 ``'channel_wise_abs_max'`` . 当 ``algo`` 设置为 ``'direct'`` 时,使用校正数据的激活值的绝对值的最大值当作 ``Scale`` 值,当设置为 ``'KL'`` 时,则使用KL散度的方法来计算 ``Scale`` 值。默认值为 ``'KL'`` 。 - **quantizable_op_type(list[str])** - 需要量化的 ``op`` 类型列表。默认值为 ``["conv2d", "depthwise_conv2d", "mul"]`` 。 - **is_full_quantize(bool)** - 是否量化所有可支持的op类型。如果设置为False, 则按照 ``'quantizable_op_type'`` 的设置进行量化。 +- **weight_bits(int)** - weight的量化比特位数。 +- **activation_bits(int)** - 激活值的量化比特位数。 - **is_use_cache_file(bool)** - 是否使用硬盘对中间结果进行存储。如果为False, 则将中间结果存储在内存中。 - **cache_dir(str)** - 如果 ``'is_use_cache_file'`` 为True, 则将中间结果存储在此参数设置的路径下。 diff --git a/paddleslim/quant/quanter.py b/paddleslim/quant/quanter.py index ce99f8607250f..27003bbc94590 100755 --- a/paddleslim/quant/quanter.py +++ b/paddleslim/quant/quanter.py @@ -238,6 +238,8 @@ def quant_post(executor, algo='KL', quantizable_op_type=["conv2d", "depthwise_conv2d", "mul"], is_full_quantize=False, + weight_bits=8, + activation_bits=8, is_use_cache_file=False, cache_dir="./temp_post_training"): """ @@ -274,6 +276,8 @@ def quant_post(executor, quantizable_op_type(list[str], optional): The list of op types that will be quantized. Default: ["conv2d", "depthwise_conv2d", "mul"]. + weight_bits(int, optional): quantization bit number for weights. + activation_bits(int): quantization bit number for activation. is_full_quantize(bool): if True, apply quantization to all supported quantizable op type. If False, only apply quantization to the input quantizable_op_type. Default is False. is_use_cache_file(bool): If False, all temp data will be saved in memory. If True, @@ -295,6 +299,8 @@ def quant_post(executor, algo=algo, quantizable_op_type=quantizable_op_type, is_full_quantize=is_full_quantize, + weight_bits=weight_bits, + activation_bits=activation_bits, is_use_cache_file=is_use_cache_file, cache_dir=cache_dir) post_training_quantization.quantize()