gen_tf_pb.py 10.4 KB
Newer Older
Khalique's avatar
Khalique committed
1
2
3
import numpy as np
import tensorflow as tf

Khalique's avatar
Khalique committed
4

Khalique's avatar
Khalique committed
5
6
7
8
def tf_test(op_test):
    def run_test():
        g1 = tf.Graph()
        op_test(g1)
Khalique's avatar
Khalique committed
9
10
11
12
13
        tf.io.write_graph(g1,
                          '.',
                          '{}.pb'.format(op_test.__name__),
                          as_text=False)

Khalique's avatar
Khalique committed
14
    return run_test
Khalique's avatar
Khalique committed
15

Khalique's avatar
Khalique committed
16

Khalique's avatar
Khalique committed
17
18
@tf_test
def add_test(g1):
Khalique's avatar
Khalique committed
19
    with g1.as_default():
Khalique's avatar
Khalique committed
20
21
22
23
        g1_input = tf.placeholder(tf.float32, shape=(1, 2, 2, 3), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(1, 2, 2, 3), name='1')
        tf.add(g1_input, g2_input, name='add1')

Khalique's avatar
Khalique committed
24

Khalique's avatar
Khalique committed
25
26
@tf_test
def add_bcast_test(g1):
Khalique's avatar
Khalique committed
27
    with g1.as_default():
Khalique's avatar
Khalique committed
28
29
30
31
        g1_input = tf.placeholder(tf.float32, shape=(2, 3), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(2, 1), name='1')
        tf.math.add(g1_input, g2_input, name='add_bcast1')

Khalique's avatar
Khalique committed
32

Khalique's avatar
Khalique committed
33
34
@tf_test
def assert_less_equal_test(g1):
Khalique's avatar
Khalique committed
35
    with g1.as_default():
Khalique's avatar
Khalique committed
36
37
38
39
40
41
        g1_input = tf.placeholder(tf.float32, shape=(2, 3), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(2, 3), name='1')
        with tf.control_dependencies(
            [tf.assert_less_equal(g1_input, g2_input)]):
            tf.add(g1_input, g2_input, name='add1')

Khalique's avatar
Khalique committed
42

Khalique's avatar
Khalique committed
43
44
@tf_test
def batchmatmul_test(g1):
Khalique's avatar
Khalique committed
45
    with g1.as_default():
Khalique's avatar
Khalique committed
46
47
48
49
50
51
52
53
        g1_input = tf.placeholder(tf.float32, shape=(1, 2, 8, 4), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(1, 2, 4, 8), name='1')
        tf.matmul(g1_input,
                  g2_input,
                  transpose_a=True,
                  transpose_b=True,
                  name='batchmatmul1')

Khalique's avatar
Khalique committed
54

Khalique's avatar
Khalique committed
55
56
@tf_test
def batchnorm_test(g1):
Khalique's avatar
Khalique committed
57
    with g1.as_default():
Khalique's avatar
Khalique committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        g1_input = tf.placeholder(tf.float32, shape=(1, 16, 16, 32), name='0')
        g1_scale = tf.constant(1.0, dtype=tf.float32, shape=[32], name='1')
        g1_offset = tf.placeholder(tf.float32, shape=(32), name='2')
        g1_mean = tf.placeholder(tf.float32, shape=(32), name='3')
        g1_variance = tf.placeholder(tf.float32, shape=(32), name='4')
        tf.nn.fused_batch_norm(g1_input,
                               g1_scale,
                               g1_offset,
                               g1_mean,
                               g1_variance,
                               epsilon=0.00001,
                               is_training=False,
                               name='batchnorm1')

Khalique's avatar
Khalique committed
72

Khalique's avatar
Khalique committed
73
74
@tf_test
def biasadd_test(g1):
Khalique's avatar
Khalique committed
75
    with g1.as_default():
Khalique's avatar
Khalique committed
76
77
78
79
        g1_input = tf.placeholder(tf.float32, shape=(1, 1, 1, 500), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(500), name='1')
        tf.nn.bias_add(g1_input, g2_input, name='bias_add1')

Khalique's avatar
Khalique committed
80

Khalique's avatar
Khalique committed
81
82
@tf_test
def cast_test(g1):
Khalique's avatar
Khalique committed
83
    with g1.as_default():
Khalique's avatar
Khalique committed
84
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
85
86
        tf.cast(g1_input, dtype=tf.int32, name='cast1')

Khalique's avatar
Khalique committed
87

Khalique's avatar
Khalique committed
88
89
@tf_test
def concat_test(g1):
Khalique's avatar
Khalique committed
90
    with g1.as_default():
Khalique's avatar
Khalique committed
91
92
93
94
        g1_input = tf.placeholder(tf.float32, shape=(4, 7, 3), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(4, 2, 3), name='1')
        tf.concat([g1_input, g2_input], axis=1, name='concat1')

Khalique's avatar
Khalique committed
95

Khalique's avatar
Khalique committed
96
97
@tf_test
def const_test(g1):
Khalique's avatar
Khalique committed
98
    with g1.as_default():
Khalique's avatar
Khalique committed
99
100
        tf.constant(1.0, dtype=tf.float32, name='constant1')

Khalique's avatar
Khalique committed
101

Khalique's avatar
Khalique committed
102
103
@tf_test
def conv_test(g1):
Khalique's avatar
Khalique committed
104
    with g1.as_default():
Khalique's avatar
Khalique committed
105
106
107
108
109
110
111
        g1_input = tf.placeholder(tf.float32, shape=(1, 16, 16, 3), name='0')
        g1_weights = tf.constant(value=1.0,
                                 dtype=tf.float32,
                                 shape=(3, 3, 3, 32),
                                 name='1')
        tf.nn.conv2d(g1_input, g1_weights, [1, 1, 1, 1], "SAME", name='conv1')

Khalique's avatar
Khalique committed
112

Khalique's avatar
Khalique committed
113
114
@tf_test
def depthwiseconv_test(g1):
Khalique's avatar
Khalique committed
115
    with g1.as_default():
Khalique's avatar
Khalique committed
116
117
118
119
120
121
122
123
124
125
        g1_input = tf.placeholder(tf.float32, shape=(1, 16, 16, 3), name='0')
        g1_weights = tf.constant(value=1.0,
                                 dtype=tf.float32,
                                 shape=(3, 3, 3, 1),
                                 name='1')
        tf.nn.depthwise_conv2d_native(g1_input,
                                      g1_weights, [1, 1, 1, 1],
                                      "SAME",
                                      name='depthwiseconv1')

Khalique's avatar
Khalique committed
126

Khalique's avatar
Khalique committed
127
128
@tf_test
def expanddims_test(g1):
Khalique's avatar
Khalique committed
129
    with g1.as_default():
Khalique's avatar
Khalique committed
130
        g1_input = tf.placeholder(tf.float32, shape=(2, 3, 4), name='0')
Khalique's avatar
Khalique committed
131
132
        tf.expand_dims(g1_input, axis=-1, name='expanddims_neg')

Khalique's avatar
Khalique committed
133

Khalique's avatar
Khalique committed
134
135
@tf_test
def gather_test(g1):
Khalique's avatar
Khalique committed
136
    with g1.as_default():
Khalique's avatar
Khalique committed
137
138
139
        g1_input = tf.placeholder(tf.float32, shape=(2, 4), name='0')
        tf.gather(g1_input, [1, 1], axis=1, name='gather1')

Khalique's avatar
Khalique committed
140

Khalique's avatar
Khalique committed
141
142
@tf_test
def identity_test(g1):
Khalique's avatar
Khalique committed
143
    with g1.as_default():
Khalique's avatar
Khalique committed
144
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
145
146
        tf.identity(g1_input, 'identity')

Khalique's avatar
Khalique committed
147

Khalique's avatar
Khalique committed
148
149
@tf_test
def matmul_test(g1):
Khalique's avatar
Khalique committed
150
    with g1.as_default():
Khalique's avatar
Khalique committed
151
152
153
154
155
156
157
158
        g1_input = tf.placeholder(tf.float32, shape=(8, 4), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(4, 8), name='1')
        tf.matmul(g1_input,
                  g2_input,
                  transpose_a=True,
                  transpose_b=True,
                  name='matmul1')

Khalique's avatar
Khalique committed
159

Khalique's avatar
Khalique committed
160
161
@tf_test
def mean_test(g1):
Khalique's avatar
Khalique committed
162
    with g1.as_default():
Khalique's avatar
Khalique committed
163
164
165
166
167
168
169
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
        tf.math.reduce_mean(g1_input, axis=(2, 3), keepdims=True, name='mean1')
        tf.math.reduce_mean(g1_input,
                            axis=(2, 3),
                            keepdims=False,
                            name='mean2')

Khalique's avatar
Khalique committed
170

Khalique's avatar
Khalique committed
171
172
@tf_test
def mean_test_nhwc(g1):
Khalique's avatar
Khalique committed
173
    with g1.as_default():
Khalique's avatar
Khalique committed
174
175
176
177
178
179
180
        g1_input = tf.placeholder(tf.float32, shape=(1, 16, 16, 3), name='0')
        tf.math.reduce_mean(g1_input, axis=(1, 2), keepdims=True, name='mean1')
        tf.math.reduce_mean(g1_input,
                            axis=(1, 2),
                            keepdims=False,
                            name='mean2')

Khalique's avatar
Khalique committed
181

Khalique's avatar
Khalique committed
182
183
@tf_test
def mul_test(g1):
Khalique's avatar
Khalique committed
184
    with g1.as_default():
Khalique's avatar
Khalique committed
185
186
        g1_input = tf.placeholder(tf.float32, shape=(1, 1, 1, 16), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(1, 1, 1, 16), name='1')
Khalique's avatar
Khalique committed
187
188
        tf.multiply(g1_input, g2_input, name='mul1')

Khalique's avatar
Khalique committed
189

Khalique's avatar
Khalique committed
190
191
@tf_test
def pack_test(g1):
Khalique's avatar
Khalique committed
192
    with g1.as_default():
Khalique's avatar
Khalique committed
193
194
195
196
197
        g1_input = tf.placeholder(tf.float32, shape=(2), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(2), name='1')
        g3_input = tf.placeholder(tf.float32, shape=(2), name='2')
        tf.stack([g1_input, g2_input, g3_input], axis=1, name='pack1')

Khalique's avatar
Khalique committed
198

Khalique's avatar
Khalique committed
199
200
@tf_test
def pack_test_nhwc(g1):
Khalique's avatar
Khalique committed
201
    with g1.as_default():
Khalique's avatar
Khalique committed
202
203
204
205
206
        g1_input = tf.placeholder(tf.float32, shape=(1, 1, 1, 2), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(1, 1, 1, 2), name='1')
        g3_input = tf.placeholder(tf.float32, shape=(1, 1, 1, 2), name='2')
        tf.stack([g1_input, g2_input, g3_input], axis=3, name='pack1')

Khalique's avatar
Khalique committed
207

Khalique's avatar
Khalique committed
208
209
@tf_test
def pooling_test(g1):
Khalique's avatar
Khalique committed
210
    with g1.as_default():
Khalique's avatar
Khalique committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
        g1_input = tf.placeholder(tf.float32, shape=(1, 16, 16, 3), name='0')
        tf.nn.avg_pool(value=g1_input,
                       ksize=(1, 2, 2, 1),
                       strides=(1, 2, 2, 1),
                       padding='VALID',
                       data_format='NHWC',
                       name='avg_pooling')
        tf.nn.max_pool(value=g1_input,
                       ksize=(1, 2, 2, 1),
                       strides=(1, 2, 2, 1),
                       padding='VALID',
                       data_format='NHWC',
                       name='max_pooling')

Khalique's avatar
Khalique committed
225

Khalique's avatar
Khalique committed
226
227
@tf_test
def pow_test(g1):
Khalique's avatar
Khalique committed
228
    with g1.as_default():
Khalique's avatar
Khalique committed
229
230
231
232
        g1_input = tf.placeholder(tf.float32, shape=(1, 2, 2, 3), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(1, 2, 2, 3), name='1')
        tf.pow(g1_input, g2_input, name='pow1')

Khalique's avatar
Khalique committed
233

Khalique's avatar
Khalique committed
234
235
@tf_test
def relu_test(g1):
Khalique's avatar
Khalique committed
236
    with g1.as_default():
Khalique's avatar
Khalique committed
237
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
238
239
        tf.nn.relu(g1_input, 'relu')

Khalique's avatar
Khalique committed
240

Khalique's avatar
Khalique committed
241
242
@tf_test
def relu6_test(g1):
Khalique's avatar
Khalique committed
243
    with g1.as_default():
Khalique's avatar
Khalique committed
244
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
245
246
        tf.nn.relu6(g1_input, 'relu6')

Khalique's avatar
Khalique committed
247

Khalique's avatar
Khalique committed
248
249
@tf_test
def reshape_test(g1):
Khalique's avatar
Khalique committed
250
    with g1.as_default():
Khalique's avatar
Khalique committed
251
252
253
        g1_input = tf.placeholder(tf.float32, shape=(16), name='0')
        tf.reshape(g1_input, (1, 1, 1, 16), 'reshape')

Khalique's avatar
Khalique committed
254

Khalique's avatar
Khalique committed
255
256
@tf_test
def rsqrt_test(g1):
Khalique's avatar
Khalique committed
257
    with g1.as_default():
Khalique's avatar
Khalique committed
258
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
259
260
        tf.math.rsqrt(g1_input, 'rsqrt')

Khalique's avatar
Khalique committed
261

Khalique's avatar
Khalique committed
262
263
@tf_test
def slice_test(g1):
Khalique's avatar
Khalique committed
264
    with g1.as_default():
Khalique's avatar
Khalique committed
265
266
267
        g1_input = tf.placeholder(tf.float32, shape=(5, 10), name='0')
        tf.slice(g1_input, [1, 0], [2, -1], name='slice1')

Khalique's avatar
Khalique committed
268

Khalique's avatar
Khalique committed
269
270
@tf_test
def softmax_test(g1):
Khalique's avatar
Khalique committed
271
    with g1.as_default():
Khalique's avatar
Khalique committed
272
        g1_input = tf.placeholder(tf.float32, shape=(1, 3), name='0')
Khalique's avatar
Khalique committed
273
274
        tf.nn.softmax(g1_input, name='softmax')

Khalique's avatar
Khalique committed
275

Khalique's avatar
Khalique committed
276
277
@tf_test
def sqdiff_test(g1):
Khalique's avatar
Khalique committed
278
    with g1.as_default():
Khalique's avatar
Khalique committed
279
280
281
282
        g1_input = tf.placeholder(tf.float32, shape=(1, 2, 2, 3), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(1, 2, 2, 3), name='1')
        tf.squared_difference(g1_input, g2_input, name='sqdiff')

Khalique's avatar
Khalique committed
283

Khalique's avatar
Khalique committed
284
285
@tf_test
def squeeze_test(g1):
Khalique's avatar
Khalique committed
286
    with g1.as_default():
Khalique's avatar
Khalique committed
287
        g1_input = tf.placeholder(tf.float32, shape=(1, 2, 3, 1), name='0')
Khalique's avatar
Khalique committed
288
289
        tf.squeeze(g1_input, name='squeeze')

Khalique's avatar
Khalique committed
290

Khalique's avatar
Khalique committed
291
292
@tf_test
def stopgradient_test(g1):
Khalique's avatar
Khalique committed
293
    with g1.as_default():
Khalique's avatar
Khalique committed
294
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
295
296
        tf.stop_gradient(g1_input, 'stopgradient')

Khalique's avatar
Khalique committed
297

Khalique's avatar
Khalique committed
298
299
@tf_test
def stridedslice_test(g1):
Khalique's avatar
Khalique committed
300
    with g1.as_default():
Khalique's avatar
Khalique committed
301
302
303
304
305
        g1_input = tf.placeholder(tf.float32, shape=(1, 1, 1, 10), name='0')
        tf.strided_slice(g1_input, [0, 0, 0, 0], [1, 1, 1, 5], [1, 1, 1, 1],
                         shrink_axis_mask=2,
                         name='stridedslice1')

Khalique's avatar
Khalique committed
306

Khalique's avatar
Khalique committed
307
308
@tf_test
def stridedslice_masks_test(g1):
Khalique's avatar
Khalique committed
309
    with g1.as_default():
Khalique's avatar
Khalique committed
310
311
312
313
314
315
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 3, 10), name='0')
        tf.strided_slice(g1_input, [0, 1, 1, 0], [0, 0, 0, 0], [1, 1, 1, 1],
                         begin_mask=9,
                         end_mask=15,
                         name='stridedslice1')

Khalique's avatar
Khalique committed
316

Khalique's avatar
Khalique committed
317
318
@tf_test
def sub_test(g1):
Khalique's avatar
Khalique committed
319
    with g1.as_default():
Khalique's avatar
Khalique committed
320
321
322
323
        g1_input = tf.placeholder(tf.float32, shape=(1, 2, 2, 3), name='0')
        g2_input = tf.placeholder(tf.float32, shape=(1, 2, 2, 3), name='1')
        tf.subtract(g1_input, g2_input, name='sub1')

Khalique's avatar
Khalique committed
324

Khalique's avatar
Khalique committed
325
326
@tf_test
def tanh_test(g1):
Khalique's avatar
Khalique committed
327
    with g1.as_default():
Khalique's avatar
Khalique committed
328
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
329
330
        tf.tanh(g1_input, 'tanh')

Khalique's avatar
Khalique committed
331

Khalique's avatar
Khalique committed
332
333
@tf_test
def transpose_test(g1):
Khalique's avatar
Khalique committed
334
    with g1.as_default():
Khalique's avatar
Khalique committed
335
336
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
        tf.transpose(g1_input, perm=[0, 2, 3, 1], name='transpose')