@@ -108,24 +108,17 @@ def _run_test_case(self, output_names_with_port, feed_dict, **kwargs):
108
108
kwargs ["constant_fold" ] = False
109
109
return self .run_test_case (feed_dict , [], output_names_with_port , ** kwargs )
110
110
111
- def _test_expand_dims (self , idx ):
111
+ def _test_expand_dims_known_rank (self , idx ):
112
112
tf .reset_default_graph ()
113
113
x_val = make_xval ([3 , 4 ])
114
114
x = tf .placeholder (tf .float32 , shape = x_val .shape , name = _TFINPUT )
115
115
op = tf .expand_dims (x , idx )
116
116
_ = tf .identity (op , name = _TFOUTPUT )
117
117
self ._run_test_case ([_OUTPUT ], {_INPUT : x_val })
118
118
119
- def test_expand_dims (self ):
119
+ def test_expand_dims_known_rank (self ):
120
120
for i in [- 1 , 0 , 1 , - 2 ]:
121
- self ._test_expand_dims (i )
122
-
123
- def test_expand_dims_dynamic_inputs (self ):
124
- x_val = make_xval ([3 , 4 ])
125
- x = tf .placeholder (tf .float32 , shape = [None , None ], name = _TFINPUT )
126
- op = tf .expand_dims (x , 0 )
127
- _ = tf .identity (op , name = _TFOUTPUT )
128
- self ._run_test_case ([_OUTPUT ], {_INPUT : x_val })
121
+ self ._test_expand_dims_known_rank (i )
129
122
130
123
def test_expand_dims_one_unknown_rank (self ):
131
124
tf .reset_default_graph ()
@@ -135,14 +128,18 @@ def test_expand_dims_one_unknown_rank(self):
135
128
_ = tf .identity (op , name = _TFOUTPUT )
136
129
self ._run_test_case ([_OUTPUT ], {_INPUT : x_val })
137
130
138
- def test_expand_dims_more_unknown_rank (self ):
131
+ def _test_expand_dims_more_unknown_rank (self , idx ):
139
132
tf .reset_default_graph ()
140
133
x_val = make_xval ([3 , 4 ])
141
134
x = tf .placeholder (tf .float32 , shape = [None , None ], name = _TFINPUT )
142
- op = tf .expand_dims (x , 0 )
135
+ op = tf .expand_dims (x , idx )
143
136
_ = tf .identity (op , name = _TFOUTPUT )
144
137
self ._run_test_case ([_OUTPUT ], {_INPUT : x_val })
145
138
139
+ def test_expand_dims_more_unknown_rank (self ):
140
+ for i in [- 1 , 0 , 1 , - 2 ]:
141
+ self ._test_expand_dims_more_unknown_rank (i )
142
+
146
143
@check_opset_min_version (9 , "ConstantOfShape" )
147
144
def test_eye_non_const1 (self ):
148
145
# tf.eye(num_rows), num_rows is not const here
@@ -444,7 +441,9 @@ def test_dropout(self):
444
441
feed_dict = {"input_1:0" : x_val }
445
442
input_names_with_port = ["input_1:0" ]
446
443
output_names_with_port = ["output:0" ]
447
- self .run_test_case (feed_dict , input_names_with_port , output_names_with_port )
444
+ self .run_test_case (feed_dict , input_names_with_port , output_names_with_port ,
445
+ graph_validator = lambda g : (check_op_count (g , "RandomUniform" , 0 ) and
446
+ check_op_count (g , "RandomUniformLike" , 0 )))
448
447
449
448
def test_nn_dropout (self ):
450
449
keep_prob = tf .placeholder_with_default (1. , (), "keep_prob" )
@@ -461,7 +460,10 @@ def test_nn_dropout(self):
461
460
output_names_with_port = ["output:0" ]
462
461
# when constant_fold is enabled, PlaceholderWithDefault will be folded into either a const or a placeholder.
463
462
# here we set it False to test PlaceholderWithDefault bug: https://github.com/onnx/tensorflow-onnx/pull/446
464
- self .run_test_case (feed_dict , input_names_with_port , output_names_with_port , constant_fold = False )
463
+ # Dropout with ratio 1.0 will be optimized so that only one Identity is left
464
+ self .run_test_case (feed_dict , input_names_with_port , output_names_with_port , constant_fold = False ,
465
+ graph_validator = lambda g : (check_op_count (g , "RandomUniform" , 0 ) and
466
+ check_op_count (g , "RandomUniformLike" , 0 )))
465
467
466
468
@check_tf_min_version ("1.13" )
467
469
def test_nn_dropout_with_rate (self ):
@@ -477,7 +479,9 @@ def test_nn_dropout_with_rate(self):
477
479
feed_dict = {"input_1:0" : x_val }
478
480
input_names_with_port = ["input_1:0" ]
479
481
output_names_with_port = ["output:0" ]
480
- self .run_test_case (feed_dict , input_names_with_port , output_names_with_port , constant_fold = False )
482
+ self .run_test_case (feed_dict , input_names_with_port , output_names_with_port , constant_fold = False ,
483
+ graph_validator = lambda g : (check_op_count (g , "RandomUniform" , 0 ) and
484
+ check_op_count (g , "RandomUniformLike" , 0 )))
481
485
482
486
def test_conv2d_with_input_transpose (self ):
483
487
x_shape = [2 , 32 , 32 , 3 ]
@@ -925,6 +929,22 @@ def test_leaky_relu_int(self):
925
929
self ._run_test_case ([_OUTPUT ], {_INPUT : x_val })
926
930
tf .reset_default_graph ()
927
931
932
+ @skip_caffe2_backend ("fails on caffe2 with dim issue" )
933
+ @check_onnxruntime_incompatibility ("Mul" )
934
+ def test_leaky_relu_with_dependency (self ):
935
+ x_val = 1000 * np .random .random_sample ([1000 , 100 ]).astype (np .float32 )
936
+ x = tf .placeholder (x_val .dtype , [None ] * x_val .ndim , name = _TFINPUT )
937
+ # simulate leaky_relu
938
+ alpha = tf .constant (0.5 )
939
+ y = alpha * x
940
+ x_ = tf .maximum (y , x )
941
+ dependency = y - 1
942
+
943
+ _ = tf .identity (x_ , name = _TFOUTPUT )
944
+ _ = tf .identity (dependency , name = _TFOUTPUT1 )
945
+ self ._run_test_case ([_OUTPUT , _OUTPUT1 ], {_INPUT : x_val })
946
+ tf .reset_default_graph ()
947
+
928
948
@skip_caffe2_backend ("fails on caffe2 with dim issue" )
929
949
@check_onnxruntime_incompatibility ("Mul" )
930
950
def test_leaky_relu_float (self ):
@@ -1057,6 +1077,15 @@ def test_slice(self):
1057
1077
_ = tf .identity (x_ , name = _TFOUTPUT )
1058
1078
self ._run_test_case ([_OUTPUT ], {_INPUT : x_val })
1059
1079
1080
+ def test_slice_neg_size (self ):
1081
+ x_val = np .array ([[1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 ]], dtype = np .float32 )
1082
+ t1 = tf .constant ([0 , 1 ], dtype = tf .int32 )
1083
+ t2 = tf .constant ([- 1 , 2 ], dtype = tf .int32 )
1084
+ x0 = tf .placeholder (tf .float32 , x_val .shape , name = _TFINPUT )
1085
+ x_ = tf .slice (x0 , t1 , t2 )
1086
+ _ = tf .identity (x_ , name = _TFOUTPUT )
1087
+ self ._run_test_case ([_OUTPUT ], {_INPUT : x_val })
1088
+
1060
1089
@check_opset_min_version (10 , "Slice in opset 10 can accept dymaic 'start' and 'ends'" )
1061
1090
def test_slice_with_non_const (self ):
1062
1091
x_val = np .array ([[1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 ]], dtype = np .float32 )
@@ -2237,7 +2266,6 @@ def test_sparse_softmax_cross_entropy_with_logits_large_class(self):
2237
2266
2238
2267
self ._run_test_case ([_OUTPUT ], {_INPUT : label_val , _INPUT1 : logits_val }, rtol = 1e-6 )
2239
2268
2240
- @skip_onnxruntime_backend ("onnxruntime Slice did not supported BOOL" )
2241
2269
def test_matrix_band_part (self ):
2242
2270
input_val = np .random .randint (0 , 666 , (10 , 15 )).astype (np .int32 )
2243
2271
input_x = tf .placeholder (dtype = tf .int32 , shape = [None , None ], name = _TFINPUT )
@@ -2247,7 +2275,6 @@ def test_matrix_band_part(self):
2247
2275
_ = tf .identity (res1 , name = _TFOUTPUT1 )
2248
2276
self ._run_test_case ([_OUTPUT , _OUTPUT1 ], {_INPUT : input_val })
2249
2277
2250
- @skip_onnxruntime_backend ("onnxruntime Slice did not supported BOOL." )
2251
2278
def test_matrix_band_part_2 (self ):
2252
2279
input_val = np .random .randint (0 , 666 , (1 , 1 )).astype (np .int32 )
2253
2280
input_x = tf .placeholder (dtype = tf .int32 , shape = [None , None ], name = _TFINPUT )
@@ -2416,7 +2443,7 @@ def test_softsign(self):
2416
2443
2417
2444
def test_batch_to_spacend (self ):
2418
2445
block_size = [2 , 2 ]
2419
- crop = [[0 , 1 ], [2 , 1 ]]
2446
+ crop = [[1 , 0 ], [2 , 1 ]]
2420
2447
2421
2448
input_val = np .random .random_sample ([40 , 3 , 5 , 100 ]).astype (np .float32 )
2422
2449
input_x = tf .placeholder (dtype = tf .float32 , shape = input_val .shape , name = _TFINPUT ) # NHWC
@@ -2551,6 +2578,80 @@ def test_selu(self):
2551
2578
_ = tf .identity (y , name = _TFOUTPUT )
2552
2579
self ._run_test_case ([_OUTPUT ], {_INPUT : x_val })
2553
2580
2581
+ # test for gemm pattern0: alpha*A*B + beta*C
2582
+ def test_gemm_pattern0 (self ):
2583
+ max_number = 10
2584
+ m = np .random .randint (max_number )
2585
+ n = np .random .randint (max_number )
2586
+ k = np .random .randint (max_number )
2587
+ x_val1 = np .random .rand (m , n ).astype ("float32" )
2588
+ x_val2 = np .random .rand (n , k ).astype ("float32" )
2589
+ x_val3 = np .random .rand (m , k ).astype ("float32" )
2590
+ a = tf .placeholder (tf .float32 , x_val1 .shape , name = _TFINPUT )
2591
+ b = tf .placeholder (tf .float32 , x_val2 .shape , name = _TFINPUT1 )
2592
+ c = tf .placeholder (tf .float32 , x_val3 .shape , name = _TFINPUT2 )
2593
+ alpha = tf .constant (1.0 , dtype = tf .float32 )
2594
+ beta = tf .constant (2.0 , dtype = tf .float32 )
2595
+ mul1 = tf .multiply (alpha , tf .matmul (a , b ))
2596
+ mul2 = tf .multiply (beta , c )
2597
+ x_ = mul1 + mul2
2598
+ _ = tf .identity (x_ , name = _TFOUTPUT )
2599
+ self ._run_test_case ([_OUTPUT ], {_INPUT : x_val1 , _INPUT1 : x_val2 , _INPUT2 : x_val3 },
2600
+ graph_validator = lambda g : check_op_count (g , "Gemm" , 1 ))
2601
+
2602
+ # test for gemm pattern1: alpha*A*B + C
2603
+ def test_gemm_pattern1 (self ):
2604
+ max_number = 10
2605
+ m = np .random .randint (max_number )
2606
+ n = np .random .randint (max_number )
2607
+ k = np .random .randint (max_number )
2608
+ x_val1 = np .random .rand (m , n ).astype ("float32" )
2609
+ x_val2 = np .random .rand (n , k ).astype ("float32" )
2610
+ x_val3 = np .random .rand (m , k ).astype ("float32" )
2611
+ a = tf .placeholder (tf .float32 , x_val1 .shape , name = _TFINPUT )
2612
+ b = tf .placeholder (tf .float32 , x_val2 .shape , name = _TFINPUT1 )
2613
+ c = tf .placeholder (tf .float32 , x_val3 .shape , name = _TFINPUT2 )
2614
+ alpha = tf .constant (1.0 , dtype = tf .float32 )
2615
+ x_ = tf .multiply (alpha , tf .matmul (a , b )) + c
2616
+ _ = tf .identity (x_ , name = _TFOUTPUT )
2617
+ self ._run_test_case ([_OUTPUT ], {_INPUT : x_val1 , _INPUT1 : x_val2 , _INPUT2 : x_val3 },
2618
+ graph_validator = lambda g : check_op_count (g , "Gemm" , 1 ))
2619
+
2620
+ # test for gemm pattern2: A*B + beta*C
2621
+ def test_gemm_pattern2 (self ):
2622
+ max_number = 10
2623
+ m = np .random .randint (max_number )
2624
+ n = np .random .randint (max_number )
2625
+ k = np .random .randint (max_number )
2626
+ x_val1 = np .random .rand (m , n ).astype ("float32" )
2627
+ x_val2 = np .random .rand (n , k ).astype ("float32" )
2628
+ x_val3 = np .random .rand (m , k ).astype ("float32" )
2629
+ a = tf .placeholder (tf .float32 , x_val1 .shape , name = _TFINPUT )
2630
+ b = tf .placeholder (tf .float32 , x_val2 .shape , name = _TFINPUT1 )
2631
+ c = tf .placeholder (tf .float32 , x_val3 .shape , name = _TFINPUT2 )
2632
+ beta = tf .constant (2.0 , dtype = tf .float32 )
2633
+ x_ = tf .matmul (a , b ) + tf .multiply (beta , c )
2634
+ _ = tf .identity (x_ , name = _TFOUTPUT )
2635
+ self ._run_test_case ([_OUTPUT ], {_INPUT : x_val1 , _INPUT1 : x_val2 , _INPUT2 : x_val3 },
2636
+ graph_validator = lambda g : check_op_count (g , "Gemm" , 1 ))
2637
+
2638
+ # test for gemm pattern3: A*B + C
2639
+ def test_gemm_pattern3 (self ):
2640
+ max_number = 10
2641
+ m = np .random .randint (max_number )
2642
+ n = np .random .randint (max_number )
2643
+ k = np .random .randint (max_number )
2644
+ x_val1 = np .random .rand (m , n ).astype ("float32" )
2645
+ x_val2 = np .random .rand (n , k ).astype ("float32" )
2646
+ x_val3 = np .random .rand (m , k ).astype ("float32" )
2647
+ a = tf .placeholder (tf .float32 , x_val1 .shape , name = _TFINPUT )
2648
+ b = tf .placeholder (tf .float32 , x_val2 .shape , name = _TFINPUT1 )
2649
+ c = tf .placeholder (tf .float32 , x_val3 .shape , name = _TFINPUT2 )
2650
+ x_ = tf .matmul (a , b ) + c
2651
+ _ = tf .identity (x_ , name = _TFOUTPUT )
2652
+ self ._run_test_case ([_OUTPUT ], {_INPUT : x_val1 , _INPUT1 : x_val2 , _INPUT2 : x_val3 },
2653
+ graph_validator = lambda g : check_op_count (g , "Gemm" , 1 ))
2654
+
2554
2655
def test_graph_matcher (self ):
2555
2656
shape = [2 , 6 ]
2556
2657
x_val = np .random .random (shape ).astype (np .float32 )
0 commit comments