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)
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')
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)
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)
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)