gen_tf_pb.py 12.3 KB
Newer Older
Khalique's avatar
Khalique committed
1
2
import numpy as np
import tensorflow as tf
3
from tensorflow.core.framework import attr_value_pb2
Khalique's avatar
Khalique committed
4

Khalique's avatar
Khalique committed
5

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

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

Khalique's avatar
Khalique committed
17

Khalique's avatar
Khalique committed
18
19
@tf_test
def add_test(g1):
Khalique's avatar
Khalique committed
20
    with g1.as_default():
Khalique's avatar
Khalique committed
21
22
23
24
        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
25

Khalique's avatar
Khalique committed
26
27
@tf_test
def add_bcast_test(g1):
Khalique's avatar
Khalique committed
28
    with g1.as_default():
Khalique's avatar
Khalique committed
29
30
31
32
        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
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
@tf_test
def argmax_test(g1):
    with g1.as_default():
        g1_input = tf.placeholder(tf.float32, shape=(3, 4, 5, 6), name='0')
        tf.argmax(g1_input, axis=2, name='argmax1')


@tf_test
def argmin_test(g1):
    with g1.as_default():
        g1_input = tf.placeholder(tf.float32, shape=(3, 4, 5, 6), name='0')
        tf.argmin(g1_input, axis=2, name='argmin1')


Khalique's avatar
Khalique committed
48
49
@tf_test
def assert_less_equal_test(g1):
Khalique's avatar
Khalique committed
50
    with g1.as_default():
Khalique's avatar
Khalique committed
51
52
53
54
55
56
        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
57

Khalique's avatar
Khalique committed
58
59
@tf_test
def batchmatmul_test(g1):
Khalique's avatar
Khalique committed
60
    with g1.as_default():
Khalique's avatar
Khalique committed
61
62
63
64
65
66
67
68
        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
69

Khalique's avatar
Khalique committed
70
71
@tf_test
def batchnorm_test(g1):
Khalique's avatar
Khalique committed
72
    with g1.as_default():
Khalique's avatar
Khalique committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        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
87

Khalique's avatar
Khalique committed
88
89
@tf_test
def biasadd_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=(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
95

Khalique's avatar
Khalique committed
96
97
@tf_test
def cast_test(g1):
Khalique's avatar
Khalique committed
98
    with g1.as_default():
Khalique's avatar
Khalique committed
99
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
100
101
        tf.cast(g1_input, dtype=tf.int32, name='cast1')

Khalique's avatar
Khalique committed
102

Khalique's avatar
Khalique committed
103
104
@tf_test
def concat_test(g1):
Khalique's avatar
Khalique committed
105
    with g1.as_default():
Khalique's avatar
Khalique committed
106
107
108
109
        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
110

Khalique's avatar
Khalique committed
111
112
@tf_test
def const_test(g1):
Khalique's avatar
Khalique committed
113
    with g1.as_default():
Khalique's avatar
Khalique committed
114
115
        tf.constant(1.0, dtype=tf.float32, name='constant1')

Khalique's avatar
Khalique committed
116

Khalique's avatar
Khalique committed
117
118
@tf_test
def conv_test(g1):
Khalique's avatar
Khalique committed
119
    with g1.as_default():
Khalique's avatar
Khalique committed
120
121
122
123
124
125
126
        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
127

Khalique's avatar
Khalique committed
128
129
@tf_test
def depthwiseconv_test(g1):
Khalique's avatar
Khalique committed
130
    with g1.as_default():
Khalique's avatar
Khalique committed
131
132
133
134
135
136
137
138
139
140
        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
141

Khalique's avatar
Khalique committed
142
143
@tf_test
def expanddims_test(g1):
Khalique's avatar
Khalique committed
144
    with g1.as_default():
Khalique's avatar
Khalique committed
145
        g1_input = tf.placeholder(tf.float32, shape=(2, 3, 4), name='0')
Khalique's avatar
Khalique committed
146
147
        tf.expand_dims(g1_input, axis=-1, name='expanddims_neg')

Khalique's avatar
Khalique committed
148

Khalique's avatar
Khalique committed
149
150
@tf_test
def gather_test(g1):
Khalique's avatar
Khalique committed
151
    with g1.as_default():
Khalique's avatar
Khalique committed
152
153
154
        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
155

Khalique's avatar
Khalique committed
156
157
@tf_test
def identity_test(g1):
Khalique's avatar
Khalique committed
158
    with g1.as_default():
Khalique's avatar
Khalique committed
159
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
160
161
        tf.identity(g1_input, 'identity')

Khalique's avatar
Khalique committed
162

Khalique's avatar
Khalique committed
163
164
@tf_test
def matmul_test(g1):
Khalique's avatar
Khalique committed
165
    with g1.as_default():
Khalique's avatar
Khalique committed
166
167
168
169
170
171
172
173
        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
174

Khalique's avatar
Khalique committed
175
176
@tf_test
def mean_test(g1):
Khalique's avatar
Khalique committed
177
    with g1.as_default():
Khalique's avatar
Khalique committed
178
179
180
181
182
183
184
        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
185

Khalique's avatar
Khalique committed
186
187
@tf_test
def mean_test_nhwc(g1):
Khalique's avatar
Khalique committed
188
    with g1.as_default():
Khalique's avatar
Khalique committed
189
190
191
192
193
194
195
        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
196

Khalique's avatar
Khalique committed
197
198
@tf_test
def mul_test(g1):
Khalique's avatar
Khalique committed
199
    with g1.as_default():
Khalique's avatar
Khalique committed
200
201
        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
202
203
        tf.multiply(g1_input, g2_input, name='mul1')

Khalique's avatar
Khalique committed
204

kahmed10's avatar
kahmed10 committed
205
206
207
208
209
210
211
@tf_test
def onehot_test(g1):
    with g1.as_default():
        g1_input = tf.constant((1, 1, 1, 1, 1), dtype=tf.int32)
        tf.one_hot(g1_input, 2, name='onehot1')


Khalique's avatar
Khalique committed
212
213
@tf_test
def pack_test(g1):
Khalique's avatar
Khalique committed
214
    with g1.as_default():
Khalique's avatar
Khalique committed
215
216
217
218
219
        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
220

Khalique's avatar
Khalique committed
221
222
@tf_test
def pack_test_nhwc(g1):
Khalique's avatar
Khalique committed
223
    with g1.as_default():
Khalique's avatar
Khalique committed
224
225
226
227
228
        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
229

Khalique's avatar
Khalique committed
230
231
@tf_test
def pooling_test(g1):
Khalique's avatar
Khalique committed
232
    with g1.as_default():
Khalique's avatar
Khalique committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
        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
247

Khalique's avatar
Khalique committed
248
249
@tf_test
def pow_test(g1):
Khalique's avatar
Khalique committed
250
    with g1.as_default():
Khalique's avatar
Khalique committed
251
252
253
254
        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
255

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

Khalique's avatar
Khalique committed
262

Khalique's avatar
Khalique committed
263
264
@tf_test
def relu6_test(g1):
Khalique's avatar
Khalique committed
265
    with g1.as_default():
Khalique's avatar
Khalique committed
266
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
267
268
        tf.nn.relu6(g1_input, 'relu6')

Khalique's avatar
Khalique committed
269

Khalique's avatar
Khalique committed
270
271
@tf_test
def reshape_test(g1):
Khalique's avatar
Khalique committed
272
    with g1.as_default():
Khalique's avatar
Khalique committed
273
274
275
        g1_input = tf.placeholder(tf.float32, shape=(16), name='0')
        tf.reshape(g1_input, (1, 1, 1, 16), 'reshape')

Khalique's avatar
Khalique committed
276

Khalique's avatar
Khalique committed
277
278
@tf_test
def rsqrt_test(g1):
Khalique's avatar
Khalique committed
279
    with g1.as_default():
Khalique's avatar
Khalique committed
280
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
281
282
        tf.math.rsqrt(g1_input, 'rsqrt')

Khalique's avatar
Khalique committed
283

284
285
286
287
288
289
290
@tf_test
def shape_test(g1):
    with g1.as_default():
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
    g1.create_op(op_type='Shape', inputs=[g1_input])


Khalique's avatar
Khalique committed
291
292
@tf_test
def slice_test(g1):
Khalique's avatar
Khalique committed
293
    with g1.as_default():
Khalique's avatar
Khalique committed
294
295
296
        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
297

Khalique's avatar
Khalique committed
298
299
@tf_test
def softmax_test(g1):
Khalique's avatar
Khalique committed
300
    with g1.as_default():
Khalique's avatar
Khalique committed
301
        g1_input = tf.placeholder(tf.float32, shape=(1, 3), name='0')
Khalique's avatar
Khalique committed
302
303
        tf.nn.softmax(g1_input, name='softmax')

Khalique's avatar
Khalique committed
304

kahmed10's avatar
kahmed10 committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
@tf_test
def split_test(g1):
    with g1.as_default():
        g1_input = tf.placeholder(tf.float32, shape=(5, 30), name='0')
        split0, split1, split2 = tf.split(g1_input, 3, 1, name='split')
        tf.concat([split0, split1], axis=1, name='concat1')
        tf.concat([split1, split2], axis=1, name='concat2')


@tf_test
def split_test_one_output(g1):
    with g1.as_default():
        g1_input = tf.placeholder(tf.float32, shape=(5, 30), name='0')
        tf.split(g1_input, 1, 1, name='split')


@tf_test
def split_test_vector_as_input(g1):
    with g1.as_default():
        g1_input = tf.placeholder(tf.float32, shape=(5, 30), name='0')
        split0, split1, split2 = tf.split(g1_input, [4, 15, 11],
                                          1,
                                          name='split')
        tf.concat([split0, split1], axis=1, name='concat1')
        tf.concat([split1, split2], axis=1, name='concat2')


Khalique's avatar
Khalique committed
332
333
@tf_test
def sqdiff_test(g1):
Khalique's avatar
Khalique committed
334
    with g1.as_default():
Khalique's avatar
Khalique committed
335
336
337
338
        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
339

Khalique's avatar
Khalique committed
340
341
@tf_test
def squeeze_test(g1):
Khalique's avatar
Khalique committed
342
    with g1.as_default():
Khalique's avatar
Khalique committed
343
        g1_input = tf.placeholder(tf.float32, shape=(1, 2, 3, 1), name='0')
Khalique's avatar
Khalique committed
344
345
        tf.squeeze(g1_input, name='squeeze')

Khalique's avatar
Khalique committed
346

Khalique's avatar
Khalique committed
347
348
@tf_test
def stopgradient_test(g1):
Khalique's avatar
Khalique committed
349
    with g1.as_default():
Khalique's avatar
Khalique committed
350
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
351
352
        tf.stop_gradient(g1_input, 'stopgradient')

Khalique's avatar
Khalique committed
353

Khalique's avatar
Khalique committed
354
355
@tf_test
def stridedslice_test(g1):
Khalique's avatar
Khalique committed
356
    with g1.as_default():
Khalique's avatar
Khalique committed
357
358
359
360
361
        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
362

Khalique's avatar
Khalique committed
363
364
@tf_test
def stridedslice_masks_test(g1):
Khalique's avatar
Khalique committed
365
    with g1.as_default():
Khalique's avatar
Khalique committed
366
367
368
369
370
371
        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
372

Khalique's avatar
Khalique committed
373
374
@tf_test
def sub_test(g1):
Khalique's avatar
Khalique committed
375
    with g1.as_default():
Khalique's avatar
Khalique committed
376
377
378
379
        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
380

Khalique's avatar
Khalique committed
381
382
@tf_test
def tanh_test(g1):
Khalique's avatar
Khalique committed
383
    with g1.as_default():
Khalique's avatar
Khalique committed
384
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
Khalique's avatar
Khalique committed
385
386
        tf.tanh(g1_input, 'tanh')

Khalique's avatar
Khalique committed
387

Khalique's avatar
Khalique committed
388
389
@tf_test
def transpose_test(g1):
Khalique's avatar
Khalique committed
390
    with g1.as_default():
Khalique's avatar
Khalique committed
391
392
        g1_input = tf.placeholder(tf.float32, shape=(1, 3, 16, 16), name='0')
        tf.transpose(g1_input, perm=[0, 2, 3, 1], name='transpose')
393
394
395
396
397
398
399


@tf_test
def variable_batch_test(g1):
    with g1.as_default():
        g1_input = tf.placeholder(tf.float32, shape=(0, 3, 16, 16), name='0')
        tf.identity(g1_input, name='identity')