Ejemplo n.º 1
0
 def test_conv_add_relu(self):
     tf.compat.v1.disable_eager_execution()
     x = tf.compat.v1.placeholder(tf.float32, [1, 224, 224, 3], name="input")
     conv_weights = tf.compat.v1.get_variable("weight", [3, 3, 3, 32],
                                             initializer=tf.compat.v1.random_normal_initializer())
     conv_bias = tf.compat.v1.get_variable("bias", [32],
                                         initializer=tf.compat.v1.random_normal_initializer())
     conv1 = tf.nn.conv2d(x, conv_weights, strides=[1, 1, 1, 1], padding="SAME")
     conv_bias=tf.math.add(conv1, conv_bias)
     relu=tf.nn.relu(conv_bias)
     with tf.compat.v1.Session() as sess:
         sess.run(tf.compat.v1.global_variables_initializer())
         output_graph_def = graph_util.convert_variables_to_constants(
             sess=sess,
             input_graph_def=sess.graph_def,
             output_node_names=[relu.name.split(':')[0]])
         output_graph_def = QuantizeGraphHelper.remove_training_nodes(
             output_graph_def, protected_nodes=[relu.name.split(':')[0]])
         graph_def = copy.deepcopy(output_graph_def)
         inputs = ['input']
         outputs = [relu.name.split(':')[0]]
         op_wise_config = {
             "Conv2D": (False, 'minmax', False),
         }
         graph_def=output_graph_def
         fold_graph_def = QuantizeGraphForIntel(output_graph_def,outputs,
                                                 op_wise_config,
                                                 'cpu').do_transform()
         found_QuantizedConv2DWithBiasAndRelu = False
         for i in fold_graph_def.node:
             if i.op == 'QuantizedConv2DWithBiasAndRelu':
                 found_QuantizedConv2DWithBiasAndRelu = True
                 break
         self.assertEqual(found_QuantizedConv2DWithBiasAndRelu, True)
Ejemplo n.º 2
0
    def test_first_matmul_biasadd_relu_fusion(self):
        import numpy as np
        import tensorflow.compat.v1 as tf
        tf.disable_v2_behavior()

        x_data = np.array([[0.1, 0.2], [0.2, 0.3]])
        y_data = np.array([[1, 2], [3, 4]], dtype=np.float)
        x = tf.placeholder(tf.float32, shape=[2, 2])
        y = tf.constant(y_data, dtype=tf.float32, shape=[2, 2])
        z = tf.matmul(x, y)
        z = tf.nn.bias_add(z, [1, 2])
        z = tf.nn.relu(z)

        with tf.Session() as sess:
            sess.run(z, feed_dict={x: x_data, y: y_data})
            float_graph_def = sess.graph.as_graph_def()

            float_graph_def = QuantizeGraphHelper().get_sorted_graph(
                float_graph_def, ['Placeholder'], ['Relu'])

            worker = FuseNodeStartWithMatmul(float_graph_def, 'MatMul', False,
                                             'MatMul', 'cpu', True)
            output_graph = worker.apply_the_transform()

            found_quantized_matmul = False
            for i in output_graph.node:
                if i.op == 'QuantizeV2' and i.name == 'MatMul_eightbit_quantize_Placeholder' and i.attr[
                        "T"].type == dtypes.quint8:
                    found_quantized_matmul = True
                    break

            self.assertEqual(found_quantized_matmul, True)
class TestFoldBatchnorm(unittest.TestCase):
    tf.compat.v1.disable_eager_execution()
    x = tf.compat.v1.placeholder(tf.float32, [1, 224, 224, 3], name="input")
    conv_weights = tf.compat.v1.get_variable(
        "weight", [3, 3, 3, 32],
        initializer=tf.compat.v1.random_normal_initializer())
    conv_bias = tf.compat.v1.get_variable(
        "bias", [32], initializer=tf.compat.v1.random_normal_initializer())
    beta = tf.compat.v1.get_variable(
        name='beta',
        shape=[32],
        initializer=tf.compat.v1.random_normal_initializer())
    gamma = tf.compat.v1.get_variable(
        name='gamma',
        shape=[32],
        initializer=tf.compat.v1.random_normal_initializer())
    conv1 = tf.nn.conv2d(x, conv_weights, strides=[1, 1, 1, 1], padding="SAME")
    conv_bias = tf.nn.bias_add(conv1, conv_bias)
    normed = tf.compat.v1.layers.batch_normalization(conv_bias)
    with tf.compat.v1.Session() as sess:
        sess.run(tf.compat.v1.global_variables_initializer())
        output_graph_def = graph_util.convert_variables_to_constants(
            sess=sess,
            input_graph_def=sess.graph_def,
            output_node_names=[normed.name.split(':')[0]])
        output_graph_def = QuantizeGraphHelper.remove_training_nodes(
            output_graph_def, protected_nodes=[normed.name.split(':')[0]])
        graph_def = copy.deepcopy(output_graph_def)
        fold_graph_def = FoldBatchNormNodesOptimizer(
            output_graph_def).do_transformation()

    def test_fold_output_values(self):
        input_data = np.random.randn(1, 224, 224, 3)
        graph = tf.compat.v1.Graph()
        fold_graph = tf.compat.v1.Graph()
        with graph.as_default():
            tf.compat.v1.import_graph_def(self.graph_def, name='')

        with tf.compat.v1.Session(graph=graph) as sess:
            sess.run(tf.compat.v1.global_variables_initializer())
            x = graph.get_tensor_by_name('input:0')
            normed = graph.get_tensor_by_name(
                'batch_normalization/FusedBatchNormV3:0')
            y = sess.run(normed, feed_dict={x: input_data})

        with fold_graph.as_default():
            tf.compat.v1.import_graph_def(self.fold_graph_def, name='')
        with tf.compat.v1.Session(graph=fold_graph) as sess:
            sess.run(tf.compat.v1.global_variables_initializer())
            x = fold_graph.get_tensor_by_name('input:0')
            normed = fold_graph.get_tensor_by_name(
                'batch_normalization/FusedBatchNormV3:0')
            y_fold = sess.run(normed, feed_dict={x: input_data})
        assert np.allclose(y, y_fold, rtol=1e-05, atol=1e-05)

    def test_do_transform(self):
        for node in self.fold_graph_def.node:
            assert node.op not in ["FusedBatchNormV3"]
 def test_parse_pb_contains_share_nodes(self):
     original_graphdef = read_graph(
         os.path.join(self.unzipped_folder_name,
                      "frozen_inference_graph.pb"))
     copied_graphdef = copy.deepcopy(original_graphdef)
     parsed_graphdef = SplitSharedInputOptimizer(
         original_graphdef).do_transformation()
     legacy_graphdef = QuantizeGraphHelper.split_shared_inputs(
         copied_graphdef)
     self.assertGreater(len(parsed_graphdef.node),
                        len(original_graphdef.node))
     self.assertEqual(len(legacy_graphdef.node), len(parsed_graphdef.node))
    def test_conv_add_relu(self):
        x = tf.compat.v1.placeholder(tf.float32, [1, 224, 224, 3],
                                     name="input")

        if tf.version.VERSION <= '2.1.0':
            x = tf.nn.relu(x)
        conv_weights = tf.compat.v1.get_variable(
            "weight", [3, 3, 3, 32],
            initializer=tf.compat.v1.random_normal_initializer())
        conv_bias = tf.compat.v1.get_variable(
            "bias", [32], initializer=tf.compat.v1.random_normal_initializer())
        conv1 = tf.nn.conv2d(x,
                             conv_weights,
                             strides=[1, 1, 1, 1],
                             padding="SAME")
        conv_bias = tf.math.add(conv1, conv_bias)
        relu = tf.nn.relu(conv_bias, name='Relu_1')
        op_wise_sequences = TensorflowQuery(local_config_file=os.path.join(
            os.path.dirname(__file__),
            "../lpot/adaptor/tensorflow.yaml")).get_eightbit_patterns()
        with tf.compat.v1.Session() as sess:
            sess.run(tf.compat.v1.global_variables_initializer())
            output_graph_def = graph_util.convert_variables_to_constants(
                sess=sess,
                input_graph_def=sess.graph_def,
                output_node_names=[relu.name.split(':')[0]])
            output_graph_def = QuantizeGraphHelper.remove_training_nodes(
                output_graph_def, protected_nodes=[relu.name.split(':')[0]])

            outputs = [relu.name.split(':')[0]]
            op_wise_config = {
                "Conv2D": (False, 'minmax', False, 7.0),
            }

            fold_graph_def = QuantizeGraphForIntel(output_graph_def, outputs,
                                                   op_wise_config,
                                                   op_wise_sequences,
                                                   'cpu').do_transform()
            found_QuantizedConv2DWithBiasAndRelu = False
            for i in fold_graph_def.node:
                if i.op == 'QuantizedConv2DWithBiasAndRelu':
                    found_QuantizedConv2DWithBiasAndRelu = True
                    break
            self.assertEqual(found_QuantizedConv2DWithBiasAndRelu, True)
Ejemplo n.º 6
0
    def test_data_pipeline(self):
        tf.compat.v1.disable_eager_execution()
        raw_dataset = np.ones([100, 224, 224, 3], dtype=np.float32)
        tf_dataset = tf.compat.v1.data.Dataset.from_tensor_slices(raw_dataset)
        tf_dataset = tf_dataset.batch(1)
        ds_iterator = tf_dataset.make_initializable_iterator()
        iter_tensors = ds_iterator.get_next()

        conv_weights = tf.compat.v1.get_variable(
            "weight", [3, 3, 3, 32],
            initializer=tf.compat.v1.random_normal_initializer())
        conv_bias = tf.compat.v1.get_variable(
            "bias", [32], initializer=tf.compat.v1.random_normal_initializer())
        conv1 = tf.nn.conv2d(iter_tensors,
                             conv_weights,
                             strides=[1, 1, 1, 1],
                             padding="SAME")
        conv_bias = tf.math.add(conv1, conv_bias)
        relu = tf.nn.relu(conv_bias, name='Relu_1')

        output_names = [relu.name.split(':')[0], 'MakeIterator']

        with tf.compat.v1.Session() as sess:
            sess.run(tf.compat.v1.global_variables_initializer())

            output_graph_def = graph_util.convert_variables_to_constants(
                sess=sess,
                input_graph_def=sess.graph_def,
                output_node_names=output_names)
            output_graph_def = QuantizeGraphHelper.remove_training_nodes(
                output_graph_def, protected_nodes=output_names)
        graph = tf.Graph()
        with graph.as_default():
            tf.import_graph_def(output_graph_def, name='')
        print('graph has been generated....')

        iter_op = graph.get_operation_by_name('MakeIterator')
        output_tensor = get_tensor_by_name(graph, output_names[0])
        sess = tf.compat.v1.Session(graph=graph)
        iterator_sess_run(sess, iter_op, \
            feed_dict={}, output_tensor=output_tensor)
Ejemplo n.º 7
0
    def test_remove_training_nodes_save_last_identity(self):
        tf.compat.v1.disable_eager_execution()

        input_constant_name = "input_constant"
        relu_name = "relu"
        float_graph_def = graph_pb2.GraphDef()
        input_constant = QuantizeGraphHelper.create_constant_node(
            input_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[1, 2, 6, 1])
        float_graph_def.node.extend([input_constant])
        relu_node = QuantizeGraphHelper.create_node("Relu", relu_name,
                                                    [input_constant_name])
        QuantizeGraphHelper.set_attr_dtype(relu_node, "T", dtypes.float32)
        float_graph_def.node.extend([relu_node])

        b_constant_name = "b_constant"
        mat_mul_name = "mat_mul"
        identity_name = "identity"
        b_constant = QuantizeGraphHelper.create_constant_node(
            b_constant_name, value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=dtypes.float32, shape=[2, 6])
        float_graph_def.node.extend([b_constant])

        mat_mul_node = QuantizeGraphHelper.create_node("MatMul", mat_mul_name,
                                                       [relu_name, b_constant_name])
        QuantizeGraphHelper.set_attr_dtype(mat_mul_node, "T", dtypes.float32)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_a", False)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_b", False)
        float_graph_def.node.extend([mat_mul_node])

        identity_node = QuantizeGraphHelper.create_node("Identity", identity_name,
                                                        [mat_mul_name])
        float_graph_def.node.extend([identity_node])

        bias_add_name = "bias_add"
        offset_constant_name = "offset_constant"

        offset_constant = QuantizeGraphHelper.create_constant_node(
            offset_constant_name,
            value=[1, 2, 3, 4, 5, 6],
            dtype=dtypes.float32,
            shape=[6])
        float_graph_def.node.extend([offset_constant])
        bias_add_node = QuantizeGraphHelper.create_node(
            "BiasAdd", bias_add_name, [identity_name, offset_constant_name])
        QuantizeGraphHelper.set_attr_dtype(bias_add_node, "T", dtypes.float32)
        float_graph_def.node.extend([bias_add_node])

        post_relu_name = "post_relu"
        post_relu_node = QuantizeGraphHelper.create_node("Relu", post_relu_name,
                                                         [bias_add_name])
        float_graph_def.node.extend([post_relu_node])

        last_identity_node_name = 'last_identity'
        last_identity_node = QuantizeGraphHelper.create_node("Identity", last_identity_node_name,
                                                             [post_relu_name])
        float_graph_def.node.extend([last_identity_node])


        post_graph = QuantizeGraphHelper().remove_training_nodes(
            float_graph_def, protected_nodes=[last_identity_node_name])

        found_identity_node_name = []
        for i in post_graph.node:
            if i.op == 'Identity':
                found_identity_node_name.append(i.name)
                break

        self.assertEqual(found_identity_node_name[0], 'last_identity')
Ejemplo n.º 8
0
    def test_matmul_biasadd_relu_fusion(self):
        tf.compat.v1.disable_eager_execution()

        input_constant_name = "input_constant"
        relu_name = "relu"
        float_graph_def = graph_pb2.GraphDef()
        input_constant = QuantizeGraphHelper.create_constant_node(
            input_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[1, 2, 6, 1])
        float_graph_def.node.extend([input_constant])
        relu_node = QuantizeGraphHelper.create_node("Relu", relu_name,
                                                    [input_constant_name])
        QuantizeGraphHelper.set_attr_dtype(relu_node, "T", dtypes.float32)
        float_graph_def.node.extend([relu_node])

        b_constant_name = "b_constant"
        mat_mul_name = "mat_mul"

        b_constant = QuantizeGraphHelper.create_constant_node(
            b_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[2, 6])
        float_graph_def.node.extend([b_constant])

        mat_mul_node = QuantizeGraphHelper.create_node(
            "MatMul", mat_mul_name, [relu_name, b_constant_name])
        QuantizeGraphHelper.set_attr_dtype(mat_mul_node, "T", dtypes.float32)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_a", False)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_b", False)
        float_graph_def.node.extend([mat_mul_node])

        bias_add_name = "bias_add"
        offset_constant_name = "offset_constant"

        offset_constant = QuantizeGraphHelper.create_constant_node(
            offset_constant_name,
            value=[1, 2, 3, 4, 5, 6],
            dtype=dtypes.float32,
            shape=[6])
        float_graph_def.node.extend([offset_constant])
        bias_add_node = QuantizeGraphHelper.create_node(
            "BiasAdd", bias_add_name, [mat_mul_name, offset_constant_name])
        QuantizeGraphHelper.set_attr_dtype(bias_add_node, "T", dtypes.float32)
        float_graph_def.node.extend([bias_add_node])

        post_relu_name = "post_relu"
        post_relu_node = QuantizeGraphHelper.create_node(
            "Relu", post_relu_name, [bias_add_name])
        float_graph_def.node.extend([post_relu_node])

        worker = FuseNodeStartWithMatmul(float_graph_def, mat_mul_name, False,
                                         mat_mul_name, 'cpu', False)
        output_graph = worker.apply_the_transform()
        found_quantized_matmul = False
        for i in output_graph.node:
            if i.op == 'QuantizedMatMulWithBiasAndRelu':
                found_quantized_matmul = True
                break

        self.assertEqual(found_quantized_matmul, True)
Ejemplo n.º 9
0
    def test_graph_fold_bn(self):

        input_constant_name = "input_constant"
        relu_name = "relu"
        float_graph_def = graph_pb2.GraphDef()
        input_constant = QuantizeGraphHelper.create_constant_node(
            input_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[1, 2, 6, 1])
        float_graph_def.node.extend([input_constant])
        relu_node = QuantizeGraphHelper.create_node("Relu", relu_name,
                                                    [input_constant_name])
        QuantizeGraphHelper.set_attr_dtype(relu_node, "T", dtypes.float32)
        float_graph_def.node.extend([relu_node])

        b_constant_name = "b_constant"
        conv2d_name = "conv2d_1"
        b_constant = QuantizeGraphHelper.create_constant_node(
            b_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[1, 2, 3, 4])
        float_graph_def.node.extend([b_constant])

        conv2d_node = QuantizeGraphHelper.create_node(
            "Conv2D", conv2d_name, [relu_name, b_constant_name])
        QuantizeGraphHelper.set_attr_dtype(conv2d_node, "T", dtypes.float32)

        float_graph_def.node.extend([conv2d_node])

        bias_add_name = "bias_add"
        offset_constant_name = "offset_constant"

        offset_constant = QuantizeGraphHelper.create_constant_node(
            offset_constant_name,
            value=[1, 2, 3, 4, 5, 6],
            dtype=dtypes.float32,
            shape=[6])
        float_graph_def.node.extend([offset_constant])

        bias_add_node = QuantizeGraphHelper.create_node(
            "BiasAdd", bias_add_name, [conv2d_name, offset_constant_name])
        QuantizeGraphHelper.set_attr_dtype(bias_add_node, "T", dtypes.float32)
        float_graph_def.node.extend([bias_add_node])

        bn_scale_name = 'bn_scale'
        bn_scale_node = QuantizeGraphHelper.create_constant_node(
            bn_scale_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[12, 1])
        bn_offset_name = 'bn_offset'
        bn_offset_node = QuantizeGraphHelper.create_constant_node(
            bn_offset_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[12, 1])
        bn_mean_name = 'bn_mean'
        bn_mean_node = QuantizeGraphHelper.create_constant_node(
            bn_mean_name, value=[
                1,
                2,
            ], dtype=dtypes.float32, shape=[
                2,
            ])
        bn_var_name = 'bn_var'
        bn_var_node = QuantizeGraphHelper.create_constant_node(
            bn_var_name, value=[], dtype=dtypes.float32, shape=[0])
        fused_bn_node_name = 'bn'
        fused_bn_node = QuantizeGraphHelper.create_node(
            "FusedBatchNormV3", fused_bn_node_name, [
                bias_add_name, bn_scale_name, bn_offset_name, bn_mean_name,
                bn_var_name
            ])
        QuantizeGraphHelper.set_attr_dtype(fused_bn_node, "T", dtypes.float32)
        QuantizeGraphHelper.set_attr_dtype(fused_bn_node, "U", dtypes.float32)
        float_graph_def.node.extend([
            fused_bn_node, bn_scale_node, bn_offset_node, bn_mean_node,
            bn_var_node
        ])

        post_relu_name = "post_relu"
        post_relu_node = QuantizeGraphHelper.create_node(
            "Relu", post_relu_name, [fused_bn_node_name])
        float_graph_def.node.extend([post_relu_node])

        post_graph = FoldBatchNormNodesOptimizer(
            float_graph_def).do_transformation()

        bn_not_fused = False
        for i in post_graph.node:
            if i.op == 'FusedBatchNormV3':
                bn_not_fused = True
                break

        self.assertEqual(bn_not_fused, True)
Ejemplo n.º 10
0
    def test_insert_logging_on_ordinary_op(self):
        tf.compat.v1.disable_eager_execution()

        input_constant_name = "input_constant"
        relu_name = "relu"
        float_graph_def = graph_pb2.GraphDef()
        input_constant = QuantizeGraphHelper.create_constant_node(
            input_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[1, 2, 6, 1])
        float_graph_def.node.extend([input_constant])
        relu_node = QuantizeGraphHelper.create_node("Relu", relu_name, [input_constant_name])
        QuantizeGraphHelper.set_attr_dtype(relu_node, "T", dtypes.float32)
        float_graph_def.node.extend([relu_node])

        b_constant_name = "b_constant"
        mat_mul_name = "mat_mul"
        identity_name = "identity"
        b_constant = QuantizeGraphHelper.create_constant_node(
            b_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[2, 6])
        float_graph_def.node.extend([b_constant])

        mat_mul_node = QuantizeGraphHelper.create_node("MatMul", mat_mul_name,
                                                       [relu_name, b_constant_name])
        QuantizeGraphHelper.set_attr_dtype(mat_mul_node, "T", dtypes.float32)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_a", False)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_b", False)
        float_graph_def.node.extend([mat_mul_node])

        identity_node = QuantizeGraphHelper.create_node("Identity", identity_name, [mat_mul_name])
        float_graph_def.node.extend([identity_node])

        bias_add_name = "bias_add"
        offset_constant_name = "offset_constant"

        offset_constant = QuantizeGraphHelper.create_constant_node(offset_constant_name,
                                                                   value=[1, 2, 3, 4, 5, 6],
                                                                   dtype=dtypes.float32,
                                                                   shape=[6])
        float_graph_def.node.extend([offset_constant])
        bias_add_node = QuantizeGraphHelper.create_node("BiasAdd", bias_add_name,
                                                        [identity_name, offset_constant_name])
        QuantizeGraphHelper.set_attr_dtype(bias_add_node, "T", dtypes.float32)
        float_graph_def.node.extend([bias_add_node])

        post_relu_name = "post_relu"
        post_relu_node = QuantizeGraphHelper.create_node("Relu", post_relu_name, [bias_add_name])
        float_graph_def.node.extend([post_relu_node])

        last_max_node_name = 'Max'
        last_max_node = QuantizeGraphHelper.create_node("Max", last_max_node_name,
                                                        [post_relu_name])
        float_graph_def.node.extend([last_max_node])

        post_graph = InsertLoggingTransformer(float_graph_def,
                                              target_op_types=['Max'],
                                              message="__max:").do_transformation()

        has_print_op = False

        for node in post_graph.node:
            if node.op == 'Print':
                has_print_op = True
                break

        self.assertEqual(has_print_op, False)
    def test_graph_cse(self):
        tf.compat.v1.disable_eager_execution()

        input_constant_name = "input_constant"
        relu_name = "relu"
        float_graph_def = graph_pb2.GraphDef()
        input_constant = QuantizeGraphHelper.create_constant_node(
            input_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[1, 2, 6, 1])
        float_graph_def.node.extend([input_constant])
        relu_node = QuantizeGraphHelper.create_node("Relu", relu_name,
                                                    [input_constant_name])
        QuantizeGraphHelper.set_attr_dtype(relu_node, "T", dtypes.float32)
        float_graph_def.node.extend([relu_node])

        b_constant_name = "b_constant"
        mat_mul_name = "mat_mul"
        b_constant = QuantizeGraphHelper.create_constant_node(
            b_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[2, 6])
        float_graph_def.node.extend([b_constant])

        mat_mul_node = QuantizeGraphHelper.create_node(
            "MatMul", mat_mul_name, [relu_name, b_constant_name])
        QuantizeGraphHelper.set_attr_dtype(mat_mul_node, "T", dtypes.float32)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_a", False)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_b", False)
        float_graph_def.node.extend([mat_mul_node])

        bias_add_name = "bias_add"
        offset_constant_name = "offset_constant"

        offset_constant = QuantizeGraphHelper.create_constant_node(
            offset_constant_name,
            value=[1, 2, 3, 4, 5, 6],
            dtype=dtypes.float32,
            shape=[6])
        float_graph_def.node.extend([offset_constant])
        bias_add_node = QuantizeGraphHelper.create_node(
            "BiasAdd", bias_add_name, [mat_mul_name, offset_constant_name])
        QuantizeGraphHelper.set_attr_dtype(bias_add_node, "T", dtypes.float32)
        float_graph_def.node.extend([bias_add_node])

        post_relu_name = "post_relu"
        post_relu_node = QuantizeGraphHelper.create_node(
            "Relu", post_relu_name, [bias_add_name])
        float_graph_def.node.extend([post_relu_node])

        last_identity_node_name = 'last_identity'
        last_identity_node = QuantizeGraphHelper.create_node(
            "Identity", last_identity_node_name, [post_relu_name])
        float_graph_def.node.extend([last_identity_node])

        analyzer = GraphAnalyzer()
        analyzer.graph = float_graph_def
        analyzer.parse_graph()
        res = analyzer.query_fusion_pattern_nodes([['MatMul'], ("BiasAdd"),
                                                   ("Relu")])
        self.assertEqual(3, len(res[0][-1]))
    def test_graph_cse(self):

        input_constant_name = "input_constant"
        relu_name = "relu"
        float_graph_def = graph_pb2.GraphDef()
        input_constant = QuantizeGraphHelper.create_constant_node(
            input_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[1, 2, 6, 1])
        float_graph_def.node.extend([input_constant])
        relu_node = QuantizeGraphHelper.create_node("Relu", relu_name,
                                                    [input_constant_name])
        QuantizeGraphHelper.set_attr_dtype(relu_node, "T", dtypes.float32)
        float_graph_def.node.extend([relu_node])

        b_constant_name = "b_constant"
        mat_mul_name = "mat_mul"
        identity_name = "identity"
        b_constant = QuantizeGraphHelper.create_constant_node(
            b_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[2, 6])
        float_graph_def.node.extend([b_constant])

        mat_mul_node = QuantizeGraphHelper.create_node(
            "MatMul", mat_mul_name, [relu_name, b_constant_name])
        QuantizeGraphHelper.set_attr_dtype(mat_mul_node, "T", dtypes.float32)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_a", False)
        QuantizeGraphHelper.set_attr_bool(mat_mul_node, "transpose_b", False)
        float_graph_def.node.extend([mat_mul_node])

        identity_node = QuantizeGraphHelper.create_node(
            "Identity", identity_name, [mat_mul_name])
        float_graph_def.node.extend([identity_node])

        bias_add_name = "bias_add"
        offset_constant_name = "offset_constant"

        offset_constant = QuantizeGraphHelper.create_constant_node(
            offset_constant_name,
            value=[1, 2, 3, 4, 5, 6],
            dtype=dtypes.float32,
            shape=[6])
        float_graph_def.node.extend([offset_constant])
        bias_add_node = QuantizeGraphHelper.create_node(
            "BiasAdd", bias_add_name, [identity_name, offset_constant_name])
        QuantizeGraphHelper.set_attr_dtype(bias_add_node, "T", dtypes.float32)
        float_graph_def.node.extend([bias_add_node])

        post_relu_name = "post_relu"
        post_relu_node = QuantizeGraphHelper.create_node(
            "Relu", post_relu_name, [bias_add_name])
        float_graph_def.node.extend([post_relu_node])

        last_identity_node_name = 'last_identity'
        last_identity_node = QuantizeGraphHelper.create_node(
            "Identity", last_identity_node_name, [post_relu_name])
        float_graph_def.node.extend([last_identity_node])

        left_relu_name = "final_left_relu"
        left_relu_node = QuantizeGraphHelper.create_node(
            "Relu", left_relu_name, [last_identity_node_name])
        float_graph_def.node.extend([left_relu_node])
        right_relu_name = "final_right_relu"
        right_relu_node = QuantizeGraphHelper.create_node(
            "Relu", right_relu_name, [last_identity_node_name])
        float_graph_def.node.extend([right_relu_node])

        cse_left_node_name = "cse_left_node"
        cse_left_node = QuantizeGraphHelper.create_node(
            "Identity", cse_left_node_name, [left_relu_name])
        float_graph_def.node.extend([cse_left_node])

        cse_right_node_name = "cse_right_node"
        cse_right_node = QuantizeGraphHelper.create_node(
            "Identity", cse_right_node_name, [right_relu_name])
        float_graph_def.node.extend([cse_right_node])

        # post_graph = QuantizeGraphHelper().graph_cse_optimization (
        #     float_graph_def)
        post_graph = GraphCseOptimizer(float_graph_def).do_transformation()

        right_relu_optimized_flag = True
        for i in post_graph.node:
            if i.name == right_relu_name:
                right_relu_optimized_flag = False
                break

        self.assertEqual(right_relu_optimized_flag, True)
Ejemplo n.º 13
0
    def test_scale_propagation(self):
        """Test scale propagation for below pattern
        requantize + quantizedavgpool+ quantized conv2d + requantize.
        """
        tf.compat.v1.disable_eager_execution()

        input_constant_name = "input_constant"
        relu_name = "relu"
        float_graph_def = graph_pb2.GraphDef()
        input_constant = QuantizeGraphHelper.create_constant_node(
            input_constant_name,
            value=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
            dtype=dtypes.float32,
            shape=[1, 2, 6, 1])
        float_graph_def.node.extend([input_constant])
        requantize_min_name = "requantize_min_const"
        requantize_min = QuantizeGraphHelper.create_constant_node(
            requantize_min_name,
            value=1,
            dtype=dtypes.float32,
        )
        float_graph_def.node.extend([requantize_min])
        requantize_max_name = "requantize_max_const"
        requantize_max = QuantizeGraphHelper.create_constant_node(
            requantize_max_name,
            value=5,
            dtype=dtypes.float32,
        )
        float_graph_def.node.extend([requantize_max])
        relu_node = QuantizeGraphHelper.create_node("Requantize", relu_name, [
            input_constant_name, input_constant_name + ':1',
            input_constant_name + ':2', requantize_min_name,
            requantize_max_name
        ])
        QuantizeGraphHelper.set_attr_dtype(relu_node, "Tinput", dtypes.qint32)
        QuantizeGraphHelper.set_attr_dtype(relu_node, "out_type",
                                           dtypes.quint8)
        float_graph_def.node.extend([relu_node])

        b_constant_name = "b_constant"
        mat_mul_name = "mat_mul"
        b_constant = QuantizeGraphHelper.create_constant_node(
            b_constant_name, value=[0], dtype=dtypes.float32, shape=[
                1,
            ])
        float_graph_def.node.extend([b_constant])

        avgpool_max_constant_name = "avgpool_max_constant"
        mat_mul_name = "mat_mul"
        avgpool_max = QuantizeGraphHelper.create_constant_node(
            avgpool_max_constant_name,
            value=[10],
            dtype=dtypes.float32,
            shape=[
                1,
            ])
        float_graph_def.node.extend([avgpool_max])
        quantized_avgpool = QuantizeGraphHelper.create_node(
            "QuantizedAvgPool", mat_mul_name,
            [relu_name, b_constant_name, avgpool_max_constant_name])
        QuantizeGraphHelper.set_attr_dtype(quantized_avgpool, "T",
                                           dtypes.float32)

        float_graph_def.node.extend([quantized_avgpool])

        bias_add_name = "bias_add"
        offset_constant_name = "offset_constant"

        offset_constant = QuantizeGraphHelper.create_constant_node(
            offset_constant_name,
            value=[1, 2, 3, 4, 5, 6],
            dtype=dtypes.float32,
            shape=[6])
        float_graph_def.node.extend([offset_constant])
        bias_add_node = QuantizeGraphHelper.create_node(
            "QuantizedConv2DWithBiasAndRelu", bias_add_name,
            [mat_mul_name, offset_constant_name])
        QuantizeGraphHelper.set_attr_dtype(bias_add_node, "T", dtypes.float32)
        float_graph_def.node.extend([bias_add_node])
        post_min_value = -1
        post_max_value = 7
        post_requantize_min_name = "post_requantize_min_const"
        post_requantize_min = QuantizeGraphHelper.create_constant_node(
            post_requantize_min_name,
            value=post_min_value,
            dtype=dtypes.float32,
        )
        float_graph_def.node.extend([post_requantize_min])
        post_requantize_max_name = "post_requantize_max_const"
        post_requantize_max = QuantizeGraphHelper.create_constant_node(
            post_requantize_max_name,
            value=post_max_value,
            dtype=dtypes.float32,
        )
        float_graph_def.node.extend([post_requantize_max])

        post_requantize_name = "post_requantize"
        post_requantize_node = QuantizeGraphHelper.create_node(
            "Requantize", post_requantize_name, [
                bias_add_name, bias_add_name + ':1', bias_add_name + ':2',
                post_requantize_min_name, post_requantize_max_name
            ])
        float_graph_def.node.extend([post_requantize_node])

        optimized_graph = ScaleProPagationTransformer(
            float_graph_def).do_transformation()
        update_min_value = None
        update_max_value = None
        for node in optimized_graph.node:
            if node.name == 'relu_cac_requantize_min_value':
                update_min_value = node.attr['value'].tensor.float_val[0]

            if node.name == 'relu_cac_requantize_max_value':
                update_max_value = node.attr['value'].tensor.float_val[0]

        self.assertEqual(update_min_value, post_min_value)
        self.assertEqual(update_max_value, post_max_value)