From 5993dde3e52bdb540ff443c41cdc29592f0437b5 Mon Sep 17 00:00:00 2001 From: cc <52520497+juncaipeng@users.noreply.github.com> Date: Wed, 3 Jun 2020 10:48:52 +0800 Subject: [PATCH] [Cherry-pick] Fix some bugs for quantization (#24852) * Update sigmoid output from Y to out, test=develop (#24765) * Collecting concat output threshold, test=develop (#24742) * Add output threshold for ops that have several output activations, test=develop (#24726) * [Fix bug] Init scale node in OutScaleForTrainingPass and enable test_quantization_scale_pass UT (#24393) * Init scale node in OutScaleForTrainingPass, test=develop * Enable test_quantization_scale, test=develop --- .../slim/quantization/quantization_pass.py | 43 +++++++++++-------- .../fluid/contrib/slim/tests/CMakeLists.txt | 3 -- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/python/paddle/fluid/contrib/slim/quantization/quantization_pass.py b/python/paddle/fluid/contrib/slim/quantization/quantization_pass.py index cde41e687f..f04869156f 100644 --- a/python/paddle/fluid/contrib/slim/quantization/quantization_pass.py +++ b/python/paddle/fluid/contrib/slim/quantization/quantization_pass.py @@ -43,7 +43,7 @@ _fake_quant_dequant_op_list = [ _out_scale_op_list = [ "conv2d", "depthwise_conv2d", "mul", "matmul", "relu", "leaky_relu", "relu6", "sigmoid", "tanh", "prelu", "swish", "softmax", "batch_norm", - "elementwise_add", "pool2d", "reshape2", "transpose2" + "elementwise_add", "pool2d", "reshape2", "transpose2", "concat" ] # list op real input and output names, to avoid processing input such as AxisTensor. @@ -83,7 +83,7 @@ _op_real_in_out_name = { "swish": [["X"], ["Out"]], "dropout": [["X"], ["Out"]], "batch_norm": [["X"], ["Y"]], - "sigmoid": [["X"], ["Y"]], + "sigmoid": [["X"], ["Out"]], } @@ -1156,20 +1156,27 @@ class OutScaleForTrainingPass(object): assert isinstance(graph, IrGraph), 'graph must be the instance of IrGraph.' self._is_test = graph.is_test() - ops = graph.all_op_nodes() - for op_node in ops: - name = op_node.name() - if name in self._teller_set: - if len(op_node.output_arg_names()) != 1: - continue - in_node = graph._find_node_by_name( - op_node.outputs, op_node.output_arg_names()[0]) + target_ops = [] + for op in graph.all_op_nodes(): + if op.name() in self._teller_set: + target_ops.append(op) + for op in target_ops: + for output_var_name in _get_op_output_var_names(op): + in_node = graph._find_node_by_name(op.outputs, output_var_name) out_node = graph.create_var_node_from_desc(in_node.var()) scale_node = graph.create_persistable_node( name=self._scale_name(in_node.name()), var_type=core.VarDesc.VarType.LOD_TENSOR, shape=[1], var_dtype=in_node.dtype()) + data_type = 'float64' if in_node.dtype() \ + == core.VarDesc.VarType.FP64 else 'float32' + _init_var_node( + scale_node, + np.ones( + [1], dtype=data_type), + self._scope, + self._place) ins = {'X': in_node} outs = {'Out': out_node, 'OutScale': scale_node} if not self._is_test: @@ -1178,8 +1185,6 @@ class OutScaleForTrainingPass(object): var_type=core.VarDesc.VarType.LOD_TENSOR, var_dtype=in_node.dtype(), shape=[1]) - data_type = 'float64' if in_node.dtype( - ) == core.VarDesc.VarType.FP64 else 'float32' _init_var_node( state_in_node, np.ones( @@ -1257,13 +1262,13 @@ class OutScaleForInferencePass(object): """ assert isinstance(graph, IrGraph), 'graph must be the instance of IrGraph.' - ops = graph.all_op_nodes() - for op_node in ops: - name = op_node.name() - if name in self._teller_set: - if len(op_node.output_arg_names()) != 1: - continue - scale_name = self._scale_name(op_node.output_arg_names()[0]) + op_nodes = graph.all_op_nodes() + for op_node in op_nodes: + if op_node.name() in self._teller_set: + output_var_name = _get_op_output_var_names(op_node) + assert len(output_var_name) == 1, "Only support collecting " \ + "output for op that only has an activation output for now." + scale_name = self._scale_name(output_var_name[0]) scale_v = np.array( self._scope.find_var(scale_name).get_tensor())[0] op_node.op()._set_attr("out_threshold", float(scale_v)) diff --git a/python/paddle/fluid/contrib/slim/tests/CMakeLists.txt b/python/paddle/fluid/contrib/slim/tests/CMakeLists.txt index 737d67051a..5aa05a158d 100644 --- a/python/paddle/fluid/contrib/slim/tests/CMakeLists.txt +++ b/python/paddle/fluid/contrib/slim/tests/CMakeLists.txt @@ -114,9 +114,6 @@ if(WIN32) list(REMOVE_ITEM TEST_OPS test_weight_quantization_mobilenetv1) endif() -# Disable unittest for random error temporary -list(REMOVE_ITEM TEST_OPS test_quantization_scale_pass) - if(LINUX AND WITH_MKLDNN) #### Image classification dataset: ImageNet (small) -- GitLab