def _gather_scales(self, program, scope):
    '\n        Get all scales from fake ops, save them into the corresponding ops\n        and delete all moving_average_abs_max_scale ops. \n        '

    def _gather_input_scale():
        target_ops = []
        skip_ops = (utils.fake_quantize_dequantize_op_types + ['moving_average_abs_max_scale'])
        for block in program.blocks:
            for op in block.ops:
                if (op.type not in skip_ops):
                    target_ops.append(op)
        for op in target_ops:
            for in_var_name in utils._get_op_input_var_names(op):
                previous_op = utils.find_previous_op(op.block, in_var_name)
                if ((previous_op is not None) and (('quantize_dequantize' in previous_op.type) or (previous_op.type == 'moving_average_abs_max_scale'))):
                    scale_name = previous_op.output('OutScale')[0]
                    in_scale = utils.load_variable_data(scope, scale_name)
                    in_scale = utils.fp_numpy_to_naive(in_scale)
                    (argname, index) = utils._get_input_name_index(op, in_var_name)
                    op._set_attr(((argname + str(index)) + '_threshold'), in_scale)

    def _gather_output_scale():
        target_ops = []
        for block in program.blocks:
            for op in block.ops:
                if (op.type == 'moving_average_abs_max_scale'):
                    target_ops.append(op)
        for op in target_ops:
            in_var_name = op.input('X')[0]
            out_var_name = op.output('Out')[0]
            block = op.block
            previous_op = utils.find_previous_op(block, in_var_name)
            next_ops = utils.find_next_ops(block, out_var_name)
            out_scale_name = op.output('OutScale')[0]
            out_scale = utils.load_variable_data(scope, out_scale_name)
            out_scale = utils.fp_numpy_to_naive(out_scale)
            if (previous_op.type != 'feed'):
                (argname, index) = utils._get_output_name_index(previous_op, in_var_name)
                previous_op._set_attr(((argname + str(index)) + '_threshold'), out_scale)
                previous_op._set_attr('out_threshold', out_scale)
            for next_op in next_ops:
                next_op._rename_input(out_var_name, in_var_name)
    _gather_input_scale()
    _gather_output_scale()
Get all scales from fake ops, save them into the corresponding ops
and delete all moving_average_abs_max_scale ops.
def _gather_scales(self, program, scope):
    '\n        Get all scales from fake ops, save them into the corresponding ops\n        and delete all moving_average_abs_max_scale ops. \n        '

    def _gather_input_scale():
        target_ops = []
        skip_ops = (utils.fake_quantize_dequantize_op_types + ['moving_average_abs_max_scale'])
        for block in program.blocks:
            for op in block.ops:
                if (op.type not in skip_ops):
                    target_ops.append(op)
        for op in target_ops:
            for in_var_name in utils._get_op_input_var_names(op):
                previous_op = utils.find_previous_op(op.block, in_var_name)
                if ((previous_op is not None) and (('quantize_dequantize' in previous_op.type) or (previous_op.type == 'moving_average_abs_max_scale'))):
                    scale_name = previous_op.output('OutScale')[0]
                    in_scale = utils.load_variable_data(scope, scale_name)
                    in_scale = utils.fp_numpy_to_naive(in_scale)
                    (argname, index) = utils._get_input_name_index(op, in_var_name)
                    op._set_attr(((argname + str(index)) + '_threshold'), in_scale)

    def _gather_output_scale():
        target_ops = []
        for block in program.blocks:
            for op in block.ops:
                if (op.type == 'moving_average_abs_max_scale'):
                    target_ops.append(op)
        for op in target_ops:
            in_var_name = op.input('X')[0]
            out_var_name = op.output('Out')[0]
            block = op.block
            previous_op = utils.find_previous_op(block, in_var_name)
            next_ops = utils.find_next_ops(block, out_var_name)
            out_scale_name = op.output('OutScale')[0]
            out_scale = utils.load_variable_data(scope, out_scale_name)
            out_scale = utils.fp_numpy_to_naive(out_scale)
            if (previous_op.type != 'feed'):
                (argname, index) = utils._get_output_name_index(previous_op, in_var_name)
                previous_op._set_attr(((argname + str(index)) + '_threshold'), out_scale)
                previous_op._set_attr('out_threshold', out_scale)
            for next_op in next_ops:
                next_op._rename_input(out_var_name, in_var_name)
    _gather_input_scale()
    _gather_output_scale()