def freeze_graph(self, use_cuda, seed, activation_quant_type, bias_correction=False, weight_quant_type='abs_max', for_ci=True, quant_skip_pattern='skip_quant'):

    def build_program(main, startup, is_test):
        main.random_seed = seed
        startup.random_seed = seed
        with fluid.unique_name.guard():
            with fluid.program_guard(main, startup):
                img = fluid.layers.data(name='image', shape=[1, 28, 28], dtype='float32')
                label = fluid.layers.data(name='label', shape=[1], dtype='int64')
                loss = conv_net(img, label, quant_skip_pattern)
                if (not is_test):
                    opt = fluid.optimizer.Adam(learning_rate=0.001)
                    opt.minimize(loss)
        return ([img, label], loss)
    random.seed(0)
    np.random.seed(0)
    main = fluid.Program()
    startup = fluid.Program()
    test_program = fluid.Program()
    (feeds, loss) = build_program(main, startup, False)
    build_program(test_program, startup, True)
    test_program = test_program.clone(for_test=True)
    main_graph = IrGraph(core.Graph(main.desc), for_test=False)
    test_graph = IrGraph(core.Graph(test_program.desc), for_test=True)
    place = (fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace())
    exe = fluid.Executor(place)
    scope = fluid.Scope()
    with fluid.scope_guard(scope):
        exe.run(startup)
    transform_pass = QuantizationTransformPass(scope=scope, place=place, activation_quantize_type=activation_quant_type, weight_quantize_type=weight_quant_type, skip_pattern=quant_skip_pattern)
    transform_pass.apply(main_graph)
    transform_pass.apply(test_graph)
    dev_name = ('_gpu_' if use_cuda else '_cpu_')
    if (not for_ci):
        marked_nodes = set()
        for op in main_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        main_graph.draw('.', (((('main' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
        marked_nodes = set()
        for op in test_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        test_graph.draw('.', (((('test' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
    build_strategy = fluid.BuildStrategy()
    build_strategy.memory_optimize = False
    build_strategy.enable_inplace = False
    build_strategy.fuse_all_reduce_ops = False
    binary = fluid.CompiledProgram(main_graph.graph).with_data_parallel(loss_name=loss.name, build_strategy=build_strategy)
    quantized_test_program = test_graph.to_program()
    iters = 5
    batch_size = 8
    train_reader = paddle.batch(paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500), batch_size=batch_size)
    test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=batch_size)
    feeder = fluid.DataFeeder(feed_list=feeds, place=place)
    with fluid.scope_guard(scope):
        for _ in range(iters):
            data = next(train_reader())
            loss_v = exe.run(binary, feed=feeder.feed(data), fetch_list=[loss])
            if (not for_ci):
                print('{}: {}'.format((((('loss' + dev_name) + activation_quant_type) + '_') + weight_quant_type), loss_v))
    test_data = next(test_reader())
    with fluid.program_guard(quantized_test_program):
        w_var = fluid.framework._get_var('conv2d_1.w_0.quantized', quantized_test_program)
    with fluid.scope_guard(scope):
        (test_loss1, w_quant) = exe.run(program=quantized_test_program, feed=feeder.feed(test_data), fetch_list=[loss, w_var])
    freeze_pass = QuantizationFreezePass(scope=scope, place=place, bias_correction=bias_correction, weight_quantize_type=weight_quant_type)
    freeze_pass.apply(test_graph)
    if (not for_ci):
        marked_nodes = set()
        for op in test_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        test_graph.draw('.', (((('test_freeze' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
    server_program = test_graph.to_program()
    with fluid.scope_guard(scope):
        (test_loss2,) = exe.run(program=server_program, feed=feeder.feed(test_data), fetch_list=[loss])
    self.assertAlmostEqual(test_loss1, test_loss2, delta=0.005)
    if (not for_ci):
        print('{}: {}'.format((((('test_loss1' + dev_name) + activation_quant_type) + '_') + weight_quant_type), test_loss1))
        print('{}: {}'.format((((('test_loss2' + dev_name) + activation_quant_type) + '_') + weight_quant_type), test_loss2))
    w_freeze = np.array(scope.find_var('conv2d_1.w_0').get_tensor())
    if (not for_ci):
        print('{}: {}'.format((((('w_freeze' + dev_name) + activation_quant_type) + '_') + weight_quant_type), np.sum(w_freeze)))
        print('{}: {}'.format((((('w_quant' + dev_name) + activation_quant_type) + '_') + weight_quant_type), np.sum(w_quant)))
    convert_int8_pass = ConvertToInt8Pass(scope=scope, place=place)
    convert_int8_pass.apply(test_graph)
    if (not for_ci):
        marked_nodes = set()
        for op in test_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        test_graph.draw('.', (((('test_int8' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
    server_program_int8 = test_graph.to_program()
    with fluid.scope_guard(scope):
        fluid.io.save_inference_model((((('server_int8' + dev_name) + activation_quant_type) + '_') + weight_quant_type), ['image', 'label'], [loss], exe, server_program_int8)
        [infer, feed, fetch] = fluid.io.load_inference_model((((('server_int8' + dev_name) + activation_quant_type) + '_') + weight_quant_type), exe)
    w_8bit = np.array(scope.find_var('conv2d_1.w_0.int8').get_tensor())
    self.assertEqual(w_8bit.dtype, np.int8)
    self.assertEqual(np.sum(w_8bit), np.sum(w_freeze))
    if (not for_ci):
        print('{}: {}'.format((((('w_8bit' + dev_name) + activation_quant_type) + '_') + weight_quant_type), np.sum(w_8bit)))
        print('{}: {}'.format((((('w_freeze' + dev_name) + activation_quant_type) + '_') + weight_quant_type), np.sum(w_freeze)))
    mobile_pass = TransformForMobilePass()
    mobile_pass.apply(test_graph)
    if (not for_ci):
        marked_nodes = set()
        for op in test_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        test_graph.draw('.', (((('test_mobile' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
    mobile_program = test_graph.to_program()
    with fluid.scope_guard(scope):
        fluid.io.save_inference_model((((('mobile_int8' + dev_name) + activation_quant_type) + '_') + weight_quant_type), ['image', 'label'], [loss], exe, mobile_program)
def freeze_graph(self, use_cuda, seed, activation_quant_type, bias_correction=False, weight_quant_type='abs_max', for_ci=True, quant_skip_pattern='skip_quant'):

    def build_program(main, startup, is_test):
        main.random_seed = seed
        startup.random_seed = seed
        with fluid.unique_name.guard():
            with fluid.program_guard(main, startup):
                img = fluid.layers.data(name='image', shape=[1, 28, 28], dtype='float32')
                label = fluid.layers.data(name='label', shape=[1], dtype='int64')
                loss = conv_net(img, label, quant_skip_pattern)
                if (not is_test):
                    opt = fluid.optimizer.Adam(learning_rate=0.001)
                    opt.minimize(loss)
        return ([img, label], loss)
    random.seed(0)
    np.random.seed(0)
    main = fluid.Program()
    startup = fluid.Program()
    test_program = fluid.Program()
    (feeds, loss) = build_program(main, startup, False)
    build_program(test_program, startup, True)
    test_program = test_program.clone(for_test=True)
    main_graph = IrGraph(core.Graph(main.desc), for_test=False)
    test_graph = IrGraph(core.Graph(test_program.desc), for_test=True)
    place = (fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace())
    exe = fluid.Executor(place)
    scope = fluid.Scope()
    with fluid.scope_guard(scope):
        exe.run(startup)
    transform_pass = QuantizationTransformPass(scope=scope, place=place, activation_quantize_type=activation_quant_type, weight_quantize_type=weight_quant_type, skip_pattern=quant_skip_pattern)
    transform_pass.apply(main_graph)
    transform_pass.apply(test_graph)
    dev_name = ('_gpu_' if use_cuda else '_cpu_')
    if (not for_ci):
        marked_nodes = set()
        for op in main_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        main_graph.draw('.', (((('main' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
        marked_nodes = set()
        for op in test_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        test_graph.draw('.', (((('test' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
    build_strategy = fluid.BuildStrategy()
    build_strategy.memory_optimize = False
    build_strategy.enable_inplace = False
    build_strategy.fuse_all_reduce_ops = False
    binary = fluid.CompiledProgram(main_graph.graph).with_data_parallel(loss_name=loss.name, build_strategy=build_strategy)
    quantized_test_program = test_graph.to_program()
    iters = 5
    batch_size = 8
    train_reader = paddle.batch(paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500), batch_size=batch_size)
    test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=batch_size)
    feeder = fluid.DataFeeder(feed_list=feeds, place=place)
    with fluid.scope_guard(scope):
        for _ in range(iters):
            data = next(train_reader())
            loss_v = exe.run(binary, feed=feeder.feed(data), fetch_list=[loss])
            if (not for_ci):
                print('{}: {}'.format((((('loss' + dev_name) + activation_quant_type) + '_') + weight_quant_type), loss_v))
    test_data = next(test_reader())
    with fluid.program_guard(quantized_test_program):
        w_var = fluid.framework._get_var('conv2d_1.w_0.quantized', quantized_test_program)
    with fluid.scope_guard(scope):
        (test_loss1, w_quant) = exe.run(program=quantized_test_program, feed=feeder.feed(test_data), fetch_list=[loss, w_var])
    freeze_pass = QuantizationFreezePass(scope=scope, place=place, bias_correction=bias_correction, weight_quantize_type=weight_quant_type)
    freeze_pass.apply(test_graph)
    if (not for_ci):
        marked_nodes = set()
        for op in test_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        test_graph.draw('.', (((('test_freeze' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
    server_program = test_graph.to_program()
    with fluid.scope_guard(scope):
        (test_loss2,) = exe.run(program=server_program, feed=feeder.feed(test_data), fetch_list=[loss])
    self.assertAlmostEqual(test_loss1, test_loss2, delta=0.005)
    if (not for_ci):
        print('{}: {}'.format((((('test_loss1' + dev_name) + activation_quant_type) + '_') + weight_quant_type), test_loss1))
        print('{}: {}'.format((((('test_loss2' + dev_name) + activation_quant_type) + '_') + weight_quant_type), test_loss2))
    w_freeze = np.array(scope.find_var('conv2d_1.w_0').get_tensor())
    if (not for_ci):
        print('{}: {}'.format((((('w_freeze' + dev_name) + activation_quant_type) + '_') + weight_quant_type), np.sum(w_freeze)))
        print('{}: {}'.format((((('w_quant' + dev_name) + activation_quant_type) + '_') + weight_quant_type), np.sum(w_quant)))
    convert_int8_pass = ConvertToInt8Pass(scope=scope, place=place)
    convert_int8_pass.apply(test_graph)
    if (not for_ci):
        marked_nodes = set()
        for op in test_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        test_graph.draw('.', (((('test_int8' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
    server_program_int8 = test_graph.to_program()
    with fluid.scope_guard(scope):
        fluid.io.save_inference_model((((('server_int8' + dev_name) + activation_quant_type) + '_') + weight_quant_type), ['image', 'label'], [loss], exe, server_program_int8)
        [infer, feed, fetch] = fluid.io.load_inference_model((((('server_int8' + dev_name) + activation_quant_type) + '_') + weight_quant_type), exe)
    w_8bit = np.array(scope.find_var('conv2d_1.w_0.int8').get_tensor())
    self.assertEqual(w_8bit.dtype, np.int8)
    self.assertEqual(np.sum(w_8bit), np.sum(w_freeze))
    if (not for_ci):
        print('{}: {}'.format((((('w_8bit' + dev_name) + activation_quant_type) + '_') + weight_quant_type), np.sum(w_8bit)))
        print('{}: {}'.format((((('w_freeze' + dev_name) + activation_quant_type) + '_') + weight_quant_type), np.sum(w_freeze)))
    mobile_pass = TransformForMobilePass()
    mobile_pass.apply(test_graph)
    if (not for_ci):
        marked_nodes = set()
        for op in test_graph.all_op_nodes():
            if (op.name().find('quantize') > (- 1)):
                marked_nodes.add(op)
        test_graph.draw('.', (((('test_mobile' + dev_name) + activation_quant_type) + '_') + weight_quant_type), marked_nodes)
    mobile_program = test_graph.to_program()
    with fluid.scope_guard(scope):
        fluid.io.save_inference_model((((('mobile_int8' + dev_name) + activation_quant_type) + '_') + weight_quant_type), ['image', 'label'], [loss], exe, mobile_program)