Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions python/paddle/fluid/contrib/slim/quantization/imperative/ptq.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def __init__(self, quant_config=ptq_config.default_ptq_config):
Args:
quant_config(PTQConfig): the config of post training quantization.
The config has weight_quantizer and activation_quantizer.
In default, the weight_quantizer and activation_quantizer are
AbsmaxQuantizer.
In default, the weight_quantizer is PerChannelAbsmaxQuantizer
and the activation_quantizer is KLQuantizer.
"""
super(ImperativePTQ, self).__init__()

Expand All @@ -70,9 +70,9 @@ def quantize(self, model, inplace=False):
"The model must be the instance of paddle.nn.Layer."

if not inplace:
new_model = copy.deepcopy(model)
model = copy.deepcopy(model)

for name, layer in new_model.named_sublayers():
for name, layer in model.named_sublayers():
if PTQRegistry.is_supported_layer(layer) \
and utils.is_leaf_layer(layer) \
and not self._is_skip_layer(layer):
Expand All @@ -90,13 +90,13 @@ def quantize(self, model, inplace=False):
layer._forward_post_hooks.move_to_end(
quant_hook_handle._hook_id, last=False)

return new_model
return model

def save_quantized_model(self, model, path, input_spec=None, **config):
"""
1. Convert the quantized model
2. Call jit.save to save the inference model
3. Load and postprocess the inference model.
3. Post process the inference model.

Args:
model (Layer): The model to be saved.
Expand Down Expand Up @@ -207,8 +207,19 @@ def _cal_thresholds(self, model):
assert isinstance(model, paddle.nn.Layer), \
"The input model must be the instance of paddle.nn.Layer."

total_num = 0
cur_num = 0
for name, sub_layer in model.named_sublayers():
if self._is_quant_layer(sub_layer):
total_num += 1

for name, sub_layer in model.named_sublayers():
if self._is_quant_layer(sub_layer):
cur_num += 1
if cur_num % 5 == 0:
_logger.info("Process the %s / %s layer" %
(cur_num, total_num))

quant_config = sub_layer._quant_config

if quant_config.enable_in_act_quantizer:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ def __init__(self, activation_quantizer, weight_quantizer):
self.enable_in_act_quantizer = False


default_ptq_config = PTQConfig(AbsmaxQuantizer(), AbsmaxQuantizer())
default_ptq_config = PTQConfig(KLQuantizer(), PerChannelAbsmaxQuantizer())
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def download_model(self, data_url, data_md5, folder_name):
return data_cache_folder

def set_vars(self):
self.ptq = ImperativePTQ(default_ptq_config)
config = PTQConfig(AbsmaxQuantizer(), AbsmaxQuantizer())
self.ptq = ImperativePTQ(config)

self.batch_num = 10
self.batch_size = 10
Expand Down