gen_onnx.py 39.3 KB
Newer Older
Khalique's avatar
Khalique committed
1
2
3
4
5
6
import numpy as np
import onnx
from onnx import helper
from onnx import numpy_helper
from onnx import AttributeProto, TensorProto, GraphProto

Khalique's avatar
Khalique committed
7
8
def onnx_test(op_test):
    def run_test():
Khalique's avatar
Khalique committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        op_info = op_test()
        if len(op_info) > 3:
            graph_def = helper.make_graph(
                op_info[0],
                op_test.__name__,
                op_info[1],
                op_info[2],
                initializer=op_info[3]
            )
        else:
            graph_def = helper.make_graph(
                op_info[0], 
                op_test.__name__, 
                op_info[1], 
                op_info[2]
            )
        model_def = helper.make_model(graph_def, producer_name=op_test.__name__)
Khalique's avatar
Khalique committed
26
27
28
29
        onnx.save(model_def, '{}.onnx'.format(op_test.__name__))
    return run_test

@onnx_test
Khalique's avatar
Khalique committed
30
31
32
33
34
35
36
37
38
39
def acos_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Acos',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
40
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
41

Khalique's avatar
Khalique committed
42
@onnx_test
Khalique's avatar
Khalique committed
43
44
45
46
47
48
49
50
51
52
53
54
55
def add_bcast_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 4])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [2, 3, 4,5])

    node = onnx.helper.make_node(
        'Add',
        inputs=['0', '1'],
        broadcast=1,
        axis=1,
        outputs=['2']
    )

Khalique's avatar
Khalique committed
56
    return ([node], [x,y], [z])
Khalique's avatar
Khalique committed
57

Khalique's avatar
Khalique committed
58
@onnx_test
Khalique's avatar
Khalique committed
59
60
61
62
63
64
65
66
67
68
69
def add_fp16_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT16, [1])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT16, [1])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT16, [1])

    node = onnx.helper.make_node(
        'Add',
        inputs=['0', '1'],
        outputs=['2'],
    )

Khalique's avatar
Khalique committed
70
    return (
Khalique's avatar
Khalique committed
71
72
73
74
        [node],
        [x,y],
        [z],
        # '0' -> 1.5, '1' -> 2.5
Khalique's avatar
Khalique committed
75
76
        [onnx.helper.make_tensor('0', TensorProto.FLOAT16, [1], [15872]),
        onnx.helper.make_tensor('1', TensorProto.FLOAT16, [1], [16640])]
Khalique's avatar
Khalique committed
77
78
79
80
81
    )

    model_def = helper.make_model(graph_def, producer_name=('add-fp16-example'))
    onnx.save(model_def, 'add_fp16_test.onnx')

Khalique's avatar
Khalique committed
82
@onnx_test
Khalique's avatar
Khalique committed
83
84
85
86
87
88
89
90
91
92
93
def add_scalar_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [2, 3, 4,5])

    node = onnx.helper.make_node(
        'Add',
        inputs=['0', '1'],
        outputs=['2']
    )

Khalique's avatar
Khalique committed
94
    return (
Khalique's avatar
Khalique committed
95
96
97
        [node],
        [x,y],
        [z],
Khalique's avatar
Khalique committed
98
        [helper.make_tensor('1', TensorProto.FLOAT, [], [1])]
Khalique's avatar
Khalique committed
99
100
    )

Khalique's avatar
Khalique committed
101
@onnx_test
Khalique's avatar
Khalique committed
102
103
104
105
106
107
108
109
110
111
112
113
def argmax_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4, 6])

    node = onnx.helper.make_node(
        'ArgMax',
        inputs=['x'],
        outputs=['y'],
        axis=2,
        keepdims = 0
    )

Khalique's avatar
Khalique committed
114
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
115

Khalique's avatar
Khalique committed
116
@onnx_test
Khalique's avatar
Khalique committed
117
118
119
120
121
122
123
124
125
126
127
128
def argmin_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4, 5])

    node = onnx.helper.make_node(
        'ArgMin',
        inputs=['x'],
        outputs=['y'],
        axis=3,
        keepdims = 0
    )

Khalique's avatar
Khalique committed
129
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
130

Khalique's avatar
Khalique committed
131
@onnx_test
Khalique's avatar
Khalique committed
132
133
134
135
136
137
138
139
140
141
def asin_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Asin',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
142
143
    return ([node], [x], [y])

Khalique's avatar
Khalique committed
144

Khalique's avatar
Khalique committed
145
@onnx_test
Khalique's avatar
Khalique committed
146
147
148
149
150
151
152
153
154
155
def atan_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Atan',
        inputs=['x'],
        outputs=['y'],
    )
    
Khalique's avatar
Khalique committed
156
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
157

Khalique's avatar
Khalique committed
158
@onnx_test
Khalique's avatar
Khalique committed
159
160
161
162
163
164
165
166
167
168
169
def cast_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT16, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Cast',
        inputs=['x'],
        outputs=['y'],
        to = 1
    )
    
Khalique's avatar
Khalique committed
170
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
171

Khalique's avatar
Khalique committed
172
@onnx_test
Khalique's avatar
Khalique committed
173
174
175
176
177
178
179
180
181
182
183
184
185
def clip_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3])


    node = onnx.helper.make_node(
        'Clip',
        inputs=['0'],
        outputs=['1'],
        max=6.0,
        min=0.0
    )

Khalique's avatar
Khalique committed
186
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
187

Khalique's avatar
Khalique committed
188
@onnx_test
Khalique's avatar
Khalique committed
189
190
191
192
193
194
195
196
197
198
199
200
def concat_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 4, 3])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [7, 4, 3])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [9, 4, 3])

    node = onnx.helper.make_node(
        'Concat',
        inputs=['0', '1'],
        axis=0,
        outputs=['2'],
    )

Khalique's avatar
Khalique committed
201
    return ([node], [x,y], [z])
Khalique's avatar
Khalique committed
202

Khalique's avatar
Khalique committed
203
@onnx_test
Khalique's avatar
Khalique committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def constant_test():
    x = np.array([0, 1, 2])
    y = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3])
    
    node = onnx.helper.make_node(
        'Constant',
        inputs=[],
        outputs=['0'],
        value=onnx.helper.make_tensor(
            name='const_tensor',
            data_type=TensorProto.FLOAT,
            dims=x.shape,
            vals=x.flatten().astype(float),
        ),
    )

Khalique's avatar
Khalique committed
220
    return ([node], [], [y])
Khalique's avatar
Khalique committed
221

Khalique's avatar
Khalique committed
222
@onnx_test
Khalique's avatar
Khalique committed
223
def constant_fill_test():
Khalique's avatar
Khalique committed
224
225
226
227
228
229
230
231
232
233
234
235
    value = helper.make_tensor_value_info('value', TensorProto.FLOAT, [2, 3])

    node = onnx.helper.make_node(
        'ConstantFill',
        inputs=[],
        outputs=['value'],
        dtype = 1,
        value = 1.0,
        shape = [2, 3],
        input_as_shape = 0,
    )

Khalique's avatar
Khalique committed
236
    return ([node], [], [value])
Khalique's avatar
Khalique committed
237

Khalique's avatar
Khalique committed
238
@onnx_test
Khalique's avatar
Khalique committed
239
def constant_fill_input_as_shape_test():
Khalique's avatar
Khalique committed
240
    np_shape = np.array([2, 3])
Khalique's avatar
Khalique committed
241
242
243
    shape = helper.make_tensor_value_info('shape', TensorProto.INT32, [2])
    value = helper.make_tensor_value_info('value', TensorProto.FLOAT, [2, 3])

Khalique's avatar
Khalique committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    ts_shape = helper.make_tensor(
        name = 'shape_tensor',
        data_type = TensorProto.INT32,
        dims = np_shape.shape,
        vals = np_shape.flatten().astype(int)
    )

    const_shape_node = onnx.helper.make_node(
        'Constant',
        inputs=[],
        outputs=['shape'],
        value=ts_shape,
    )

    node = onnx.helper.make_node(
        'ConstantFill',
        inputs=['shape'],
        outputs=['value'],
        dtype = 1,
        value = 1.0,
        input_as_shape = 1,
    )

Khalique's avatar
Khalique committed
267
    return ([const_shape_node, node], [], [value])
Khalique's avatar
Khalique committed
268

Khalique's avatar
Khalique committed
269
@onnx_test
Khalique's avatar
Khalique committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
def constant_scalar_test():
    x = np.array([1])
    y = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1])

    node = onnx.helper.make_node(
        'Constant',
        inputs=[],
        outputs=['0'],
        value=onnx.helper.make_tensor(
            name='const_tensor',
            data_type=TensorProto.INT32,
            dims=x.shape,
            vals=x.flatten().astype(int),
        ),
    )

Khalique's avatar
Khalique committed
286
    return ([node], [], [y])
Khalique's avatar
Khalique committed
287

Khalique's avatar
Khalique committed
288
@onnx_test
Khalique's avatar
Khalique committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
def const_of_shape_empty_input_test():
    tensor_val = onnx.helper.make_tensor(
        'value',
        onnx.TensorProto.INT64, [1],[10]
    )
    shape_val = np.array([2, 3, 4]).astype(np.int64)
    empty_val = np.array([]).astype(np.int64)
    empty_ts = helper.make_tensor(
        name='empty_tensor',
        data_type = TensorProto.INT32,
        dims=empty_val.shape,
        vals=empty_val.flatten().astype(int)
    )
    shape_const = helper.make_node(
        'Constant',
        inputs=[],
        outputs=['shape'],
        value=empty_ts,
    )
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [2, 3, 4])

    node = onnx.helper.make_node(
        'ConstantOfShape',
        inputs=['shape'],
        outputs=['y'],
        value = tensor_val,
    )

Khalique's avatar
Khalique committed
317
    return ([shape_const, node], [], [y])
Khalique's avatar
Khalique committed
318

Khalique's avatar
Khalique committed
319
@onnx_test
Khalique's avatar
Khalique committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
def const_of_shape_float_test():
    tensor_val = onnx.helper.make_tensor(
        'value',
        onnx.TensorProto.FLOAT, [1],[10])

    shape_val = np.array([2, 3, 4]).astype(np.int64)
    shape_ts = helper.make_tensor(
        name = 'shape_tensor',
        data_type = TensorProto.INT32,
        dims = shape_val.shape,
        vals = shape_val.flatten().astype(int)
    )

    shape_const = helper.make_node(
        'Constant',
        inputs=[],
        outputs=['shape'],
        value=shape_ts,
    )
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [2, 3, 4])

    node = onnx.helper.make_node(
        'ConstantOfShape',
        inputs=['shape'],
        outputs=['y'],
        value = tensor_val
    )

Khalique's avatar
Khalique committed
348
    return ([shape_const, node], [], [y])
Khalique's avatar
Khalique committed
349

Khalique's avatar
Khalique committed
350
@onnx_test
Khalique's avatar
Khalique committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
def const_of_shape_int64_test():
    tensor_val = onnx.helper.make_tensor(
        'value',
        onnx.TensorProto.INT64, [1],[10]
    )
    shape_val = np.array([2, 3, 4]).astype(np.int64)
    shape_ts = helper.make_tensor(
        name = 'shape_tensor',
        data_type = TensorProto.INT32,
        dims = shape_val.shape,
        vals = shape_val.flatten().astype(int)
    )
    shape_const = helper.make_node(
            'Constant',
            inputs=[],
            outputs=['shape'],
            value=shape_ts,
    )
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [2, 3, 4])
    
    node = onnx.helper.make_node(
        'ConstantOfShape',
        inputs=['shape'],
        outputs=['y'],
        value = tensor_val
    )

Khalique's avatar
Khalique committed
378
    return ([shape_const, node], [], [y])
Khalique's avatar
Khalique committed
379

Khalique's avatar
Khalique committed
380
@onnx_test
Khalique's avatar
Khalique committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
def const_of_shape_no_value_attr_test():
    shape_val = np.array([2, 3, 4]).astype(np.int64)
    shape_ts = helper.make_tensor(
        name = 'shape_tensor',
        data_type = TensorProto.INT32,
        dims = shape_val.shape,
        vals = shape_val.flatten().astype(int)
    )
    shape_const = helper.make_node(
        'Constant',
        inputs=[],
        outputs=['shape'],
        value=shape_ts,
    )
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [2, 3, 4])
    
    node = onnx.helper.make_node(
        'ConstantOfShape',
        inputs=['shape'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
403
    return ([shape_const, node], [], [y])
Khalique's avatar
Khalique committed
404

Khalique's avatar
Khalique committed
405
@onnx_test
Khalique's avatar
Khalique committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
def conv_autopad_fail_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 3, 32, 32])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 3, 1, 1])
    out = helper.make_tensor_value_info('2', TensorProto.FLOAT, [1, 1, 34, 34])

    node = onnx.helper.make_node(
        'Conv',
        inputs=['0', '1'],
        outputs=['2'],
        dilations = [1, 1], 
        strides = [1, 1],
        auto_pad = 'SAME',
        pads = [0,0,1,1,0,0,1,1]
    )

Khalique's avatar
Khalique committed
421
    return ([node], [x,y], [out])
Khalique's avatar
Khalique committed
422

Khalique's avatar
Khalique committed
423
@onnx_test
Khalique's avatar
Khalique committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
def conv_bias_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 3, 32, 32])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 3, 5, 5])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [1])
    out = helper.make_tensor_value_info('3', TensorProto.FLOAT, [1, 2, 28, 28])

    node = onnx.helper.make_node(
        'Conv',
        inputs=['0', '1', '2'],
        outputs=['3'],
        dilations = [1, 1], 
        strides = [1, 1]
    )

Khalique's avatar
Khalique committed
438
    return ([node], [x,y,z], [out])
Khalique's avatar
Khalique committed
439

Khalique's avatar
Khalique committed
440
@onnx_test
Khalique's avatar
Khalique committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
def conv_bn_relu_maxpool_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 3, 32, 32])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 3, 5, 5])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [1])
    m = helper.make_tensor_value_info('3', TensorProto.FLOAT, [1])
    n = helper.make_tensor_value_info('4', TensorProto.FLOAT, [1])
    k = helper.make_tensor_value_info('5', TensorProto.FLOAT, [1])
    l = helper.make_tensor_value_info('6', TensorProto.FLOAT, [1])
    out = helper.make_tensor_value_info('10', TensorProto.FLOAT, [1, 1, 14, 14])

    node0 = onnx.helper.make_node(
        'Conv',
        inputs=['0', '1', '2'],
        outputs=['7'],
        dilations = [1, 1], 
        strides = [1, 1],
        pads = [0, 0, 0, 0]
    )

    node1 = onnx.helper.make_node(
        'BatchNormalization',
        inputs=['7', '3', '4', '5', '6'],
        outputs=['8'],
        epsilon = 9.99999974737875e-06, 
        momentum = 0.899999976158142
    )

    node2 = onnx.helper.make_node(
        'Relu',
        inputs=['8'],
        outputs=['9']
    )
    node3 = onnx.helper.make_node(
        'MaxPool',
        inputs=['9'],
        outputs=['10'],
        pads = [0, 0, 0, 0], 
        strides = [2, 2],
        kernel_shape=[2,2]
    )

Khalique's avatar
Khalique committed
482
    return (
Khalique's avatar
Khalique committed
483
484
        [node0, node1, node2, node3],
        [x, y, z, m, n, k, l],
Khalique's avatar
Khalique committed
485
        [out]
Khalique's avatar
Khalique committed
486
487
    )

Khalique's avatar
Khalique committed
488
@onnx_test
Khalique's avatar
Khalique committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
def conv_relu_maxpool_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 3, 32, 32])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 3, 5, 5])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [1])
    out = helper.make_tensor_value_info('5', TensorProto.FLOAT, [1, 1, 14, 14])

    node1 = onnx.helper.make_node(
        'Conv',
        inputs=['0', '1', '2'],
        outputs=['3'],
        dilations = [1, 1], 
        strides = [1, 1],
        pads = [0, 0, 0, 0]
    )

    node2 = onnx.helper.make_node(
        'Relu',
        inputs=['3'],
        outputs=['4']
    )

    node3 = onnx.helper.make_node(
        'MaxPool',
        inputs=['4'],
        outputs=['5'],
        pads = [0, 0, 0, 0], 
        strides = [2, 2],
        kernel_shape=[2,2]
    )

Khalique's avatar
Khalique committed
519
    return (
Khalique's avatar
Khalique committed
520
521
        [node1, node2, node3],
        [x, y, z],
Khalique's avatar
Khalique committed
522
        [out]
Khalique's avatar
Khalique committed
523
524
    )

Khalique's avatar
Khalique committed
525
@onnx_test
Khalique's avatar
Khalique committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
def conv_relu_maxpool_x2_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 3, 32, 32])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [5, 3, 5, 5])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [5])
    m = helper.make_tensor_value_info('3', TensorProto.FLOAT, [1, 5, 5, 5])
    n = helper.make_tensor_value_info('4', TensorProto.FLOAT, [1])
    out = helper.make_tensor_value_info('10', TensorProto.FLOAT, [1, 1, 5, 5])

    node1 = onnx.helper.make_node(
        'Conv',
        inputs=['0', '1', '2'],
        outputs=['5'],
        dilations = [1, 1], 
        strides = [1, 1],
        pads = [0, 0, 0, 0]
    )

    node2 = onnx.helper.make_node(
        'Relu',
        inputs=['5'],
        outputs=['6']
    )

    node3 = onnx.helper.make_node(
        'MaxPool',
        inputs=['6'],
        outputs=['7'],
        pads = [0, 0, 0, 0], 
        strides = [2, 2],
        kernel_shape=[2,2]
    )

    node4 = onnx.helper.make_node(
        'Conv',
        inputs=['7', '3', '4'],
        outputs=['8'],
        dilations = [1, 1], 
        strides = [1, 1],
        pads = [0, 0, 0, 0]
    )

    node5 = onnx.helper.make_node(
        'Relu',
        inputs=['8'],
        outputs=['9']
    )

    node6 = onnx.helper.make_node(
        'MaxPool',
        inputs=['9'],
        outputs=['10'],
        pads = [0, 0, 0, 0], 
        strides = [2, 2],
        kernel_shape=[2,2]
        )

Khalique's avatar
Khalique committed
582
    return (
Khalique's avatar
Khalique committed
583
584
        [node1, node2, node3, node4, node5, node6],
        [x, y, z, m, n],
Khalique's avatar
Khalique committed
585
        [out]
Khalique's avatar
Khalique committed
586
587
    )

Khalique's avatar
Khalique committed
588
@onnx_test
Khalique's avatar
Khalique committed
589
590
591
592
593
594
595
596
597
598
def cos_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Cos',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
599
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
600

Khalique's avatar
Khalique committed
601
@onnx_test
Khalique's avatar
Khalique committed
602
603
604
605
606
607
608
609
610
611
def cosh_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [1])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [1])

    node = onnx.helper.make_node(
        'Cosh',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
612
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
613

Khalique's avatar
Khalique committed
614
@onnx_test
Khalique's avatar
Khalique committed
615
616
617
618
619
620
621
622
623
624
def dropout_test():
        x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 3, 2, 2])
        y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 3, 2, 2])

        node = onnx.helper.make_node(
            'Dropout',
            inputs=['0'],
            outputs=['1'],
        )

Khalique's avatar
Khalique committed
625
        return ([node], [x], [y])
Khalique's avatar
Khalique committed
626

Khalique's avatar
Khalique committed
627
@onnx_test
Khalique's avatar
Khalique committed
628
629
630
631
632
633
634
635
636
637
638
def elu_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3])

    node = onnx.helper.make_node(
        'Elu',
        inputs=['0'],
        outputs=['1'],
        alpha=0.01
    )

Khalique's avatar
Khalique committed
639
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
640

Khalique's avatar
Khalique committed
641
@onnx_test
Khalique's avatar
Khalique committed
642
643
644
645
646
647
648
649
650
651
def erf_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10, 15])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10, 15])

    node = onnx.helper.make_node(
        'Erf',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
652
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
653

Khalique's avatar
Khalique committed
654
@onnx_test
Khalique's avatar
Khalique committed
655
656
657
658
659
660
661
662
663
664
def exp_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Exp',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
665
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
666

Khalique's avatar
Khalique committed
667
@onnx_test
Khalique's avatar
Khalique committed
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
def expand_test():
    shape_val = np.array([2, 3, 4, 5]).astype(np.int64)
    shape_ts = helper.make_tensor(
        name = 'shape_tensor',
        data_type = TensorProto.INT32,
        dims = shape_val.shape,
        vals = shape_val.flatten().astype(int)
    )
    shape_const = helper.make_node(
        'Constant',
        inputs=[],
        outputs=['shape'],
        value=shape_ts,
    )
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 1, 1])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [2, 3, 4, 5])
    
    node = onnx.helper.make_node(
        'Expand',
        inputs=['x', 'shape'],
        outputs=['y']
    )

Khalique's avatar
Khalique committed
691
    return ([shape_const,node], [x], [y])
Khalique's avatar
Khalique committed
692

Khalique's avatar
Khalique committed
693
@onnx_test
Khalique's avatar
Khalique committed
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
def flatten_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    y = helper.make_tensor_value_info('2', TensorProto.FLOAT, [6,  20])
    y2 = helper.make_tensor_value_info('3', TensorProto.FLOAT, [2, 60])

    node = onnx.helper.make_node(
        'Flatten',
        inputs=['0'],
        axis=2,
        outputs=['2']
    )

    node2 = onnx.helper.make_node(
        'Flatten',
        inputs=['0'],
        outputs=['3']
    )

Khalique's avatar
Khalique committed
712
    return ([node,node2], [x], [y,y2])
Khalique's avatar
Khalique committed
713

Khalique's avatar
Khalique committed
714
@onnx_test
Khalique's avatar
Khalique committed
715
716
717
718
719
720
721
722
723
724
725
726
def gather_test():
    x = helper.make_tensor_value_info('data', TensorProto.FLOAT, [3, 4, 5, 6])
    i = helper.make_tensor_value_info('indices', TensorProto.INT32, [2, 3, 4, 5])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [2, 3, 4, 5])

    node = onnx.helper.make_node(
        'Gather',
        inputs=['data', 'indices'],
        outputs=['y'],
        axis=1,
    )

Khalique's avatar
Khalique committed
727
    return ([node], [x,i], [y])
Khalique's avatar
Khalique committed
728

Khalique's avatar
Khalique committed
729
@onnx_test
Khalique's avatar
Khalique committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
def gemm_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [5, 7])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [11, 5])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [])
    a = helper.make_tensor_value_info('3', TensorProto.FLOAT, [7, 11])

    node = onnx.helper.make_node(
        'Gemm',
        inputs=['0', '1', '2'],
        outputs=['3'],
        alpha=2.0,
        beta=2.0,
        transA=1,
        transB=1
    )

Khalique's avatar
Khalique committed
746
    return ([node], [x,y,z], [a])
Khalique's avatar
Khalique committed
747

Khalique's avatar
Khalique committed
748
@onnx_test
Khalique's avatar
Khalique committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
def gemm_ex_test():
    m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 1, 5, 6])
    m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [1, 1, 5, 7])
    m3 = helper.make_tensor_value_info('3', TensorProto.FLOAT, [1, 1, 6, 7])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [1, 1, 6, 7])

    node = onnx.helper.make_node(
        'Gemm',
        inputs=['1', '2', '3'],
        outputs=['y'],
        alpha = 0.5,
        beta = 0.8,
        transA = 1
    )

Khalique's avatar
Khalique committed
764
    return ([node], [m1,m2,m3], [y])
Khalique's avatar
Khalique committed
765

Khalique's avatar
Khalique committed
766
@onnx_test
Khalique's avatar
Khalique committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
def gemm_ex_brcst_test():
    m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 1, 5, 6])
    m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [1, 1, 5, 7])
    m3 = helper.make_tensor_value_info('3', TensorProto.FLOAT, [1, 1, 6, 1])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [1, 1, 6, 7])

    node = onnx.helper.make_node(
        'Gemm',
        inputs=['1', '2', '3'],
        outputs=['y'],
        alpha = 0.5,
        beta = 0.8,
        transA = 1
    )

Khalique's avatar
Khalique committed
782
    return ([node], [m1,m2,m3], [y])
Khalique's avatar
Khalique committed
783

Khalique's avatar
Khalique committed
784
@onnx_test
Khalique's avatar
Khalique committed
785
786
787
788
789
790
791
792
793
794
def globalavgpool_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1,3,16,16])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1,3,1,1])

    node = onnx.helper.make_node(
        'GlobalAveragePool',
        inputs=['0'],
        outputs=['1'],
    )

Khalique's avatar
Khalique committed
795
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
796

Khalique's avatar
Khalique committed
797
@onnx_test
Khalique's avatar
Khalique committed
798
799
800
801
802
803
804
805
806
807
def globalmaxpool_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1,3,16,16])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1,3,1,1])

    node = onnx.helper.make_node(
        'GlobalMaxPool',
        inputs=['0'],
        outputs=['1'],
    )

Khalique's avatar
Khalique committed
808
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
809

Khalique's avatar
Khalique committed
810
@onnx_test
Khalique's avatar
Khalique committed
811
812
813
814
815
816
817
818
819
820
821
822
def group_conv_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 4, 16, 16])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [4, 1, 3, 3])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [1, 4, 14, 14])

    node = onnx.helper.make_node(
        'Conv',
        inputs=['0', '1'],
        group=4,
        outputs=['2'],
    )

Khalique's avatar
Khalique committed
823
    return ([node], [x,y], [z])
Khalique's avatar
Khalique committed
824

Khalique's avatar
Khalique committed
825
@onnx_test
Khalique's avatar
Khalique committed
826
827
828
829
830
831
832
833
834
835
836
837
def imagescaler_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1,3,16,16])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1,3,16,16])

    node = onnx.helper.make_node(
        'ImageScaler',
        inputs=['0'],
        outputs=['1'],
        bias=[0.01,0.02,0.03],
        scale=0.5
    )

Khalique's avatar
Khalique committed
838
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
839

Khalique's avatar
Khalique committed
840
@onnx_test
Khalique's avatar
Khalique committed
841
842
843
844
845
846
847
848
849
850
851
def implicit_add_bcast_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 4, 1])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [2, 3, 4, 5])

    node = onnx.helper.make_node(
        'Add',
        inputs=['0', '1'],
        outputs=['2'],
    )

Khalique's avatar
Khalique committed
852
    return ([node], [x,y], [z])
Khalique's avatar
Khalique committed
853

Khalique's avatar
Khalique committed
854
@onnx_test
Khalique's avatar
Khalique committed
855
856
857
858
859
860
861
862
863
864
865
def implicit_pow_bcast_test():
    arg0 = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    arg1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 4, 1])
    arg_out = helper.make_tensor_value_info('out', TensorProto.FLOAT, [2, 3, 4, 5])

    node = onnx.helper.make_node(
        'Pow',
        inputs=['0', '1'],
        outputs=['out'],
    )

Khalique's avatar
Khalique committed
866
    return ([node], [arg0,arg1], [arg_out])
Khalique's avatar
Khalique committed
867

Khalique's avatar
Khalique committed
868
@onnx_test
Khalique's avatar
Khalique committed
869
870
871
872
873
874
875
876
877
878
879
def implicit_sub_bcast_test():
    arg0 = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    arg1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [4, 5])
    arg_out = helper.make_tensor_value_info('out', TensorProto.FLOAT, [2, 3, 4, 5])

    node = onnx.helper.make_node(
        'Sub',
        inputs=['0', '1'],
        outputs=['out'],
    )

Khalique's avatar
Khalique committed
880
    return ([node], [arg0,arg1], [arg_out])
Khalique's avatar
Khalique committed
881

Khalique's avatar
Khalique committed
882
@onnx_test
Khalique's avatar
Khalique committed
883
884
885
886
887
888
889
890
891
892
893
def leaky_relu_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3])

    node = onnx.helper.make_node(
        'LeakyRelu',
        inputs=['0'],
        outputs=['1'],
        alpha=0.01
    )

Khalique's avatar
Khalique committed
894
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
895

Khalique's avatar
Khalique committed
896
@onnx_test
Khalique's avatar
Khalique committed
897
898
899
900
901
902
903
904
905
906
def log_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Log',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
907
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
908

Khalique's avatar
Khalique committed
909
@onnx_test
Khalique's avatar
Khalique committed
910
911
912
913
914
915
916
917
918
919
920
def logsoftmax_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4, 5, 6])

    node = onnx.helper.make_node(
        'LogSoftmax',
        inputs=['x'],
        outputs=['y'],
        axis = 1
    )

Khalique's avatar
Khalique committed
921
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
922

Khalique's avatar
Khalique committed
923
@onnx_test
Khalique's avatar
Khalique committed
924
925
926
927
928
929
930
931
932
933
934
935
936
937
def lrn_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 28, 24, 24])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 28, 24, 24])

    node = onnx.helper.make_node(
        'LRN',
        inputs=['0'],
        size=5,
        alpha=0.0001,
        beta=0.75,
        bias=1.0,
        outputs=['1']
    )

Khalique's avatar
Khalique committed
938
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
939

Khalique's avatar
Khalique committed
940
@onnx_test
Khalique's avatar
Khalique committed
941
942
943
944
945
946
947
948
949
950
951
def matmul_bmbm_test():
    m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 6, 7])
    m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [5, 2, 1, 7, 8])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [5, 2, 3, 6, 8])

    node = onnx.helper.make_node(
        'MatMul',
        inputs=['1', '2'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
952
    return ([node], [m1,m2], [y])
Khalique's avatar
Khalique committed
953

Khalique's avatar
Khalique committed
954
@onnx_test
Khalique's avatar
Khalique committed
955
956
957
958
959
960
961
962
963
964
965
def matmul_bmv_test():
    m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 6, 7])
    m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [7])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 6])

    node = onnx.helper.make_node(
        'MatMul',
        inputs=['1', '2'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
966
    return ([node], [m1,m2], [y])
Khalique's avatar
Khalique committed
967

Khalique's avatar
Khalique committed
968
@onnx_test
Khalique's avatar
Khalique committed
969
970
971
972
973
974
975
976
977
978
979
def matmul_mv_test():
    m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [6, 7])
    m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [7])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [6])

    node = onnx.helper.make_node(
        'MatMul',
        inputs=['1', '2'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
980
    return ([node], [m1,m2], [y])
Khalique's avatar
Khalique committed
981

Khalique's avatar
Khalique committed
982
@onnx_test
Khalique's avatar
Khalique committed
983
984
985
986
987
988
989
990
991
992
993
def matmul_vbm_test():
    m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [7])
    m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [5, 7, 8])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [5, 8])

    node = onnx.helper.make_node(
        'MatMul',
        inputs=['1', '2'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
994
    return ([node], [m1,m2], [y])
Khalique's avatar
Khalique committed
995

Khalique's avatar
Khalique committed
996
@onnx_test
Khalique's avatar
Khalique committed
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
def matmul_vm_test():
    m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [7])
    m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [7, 8])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [8])

    node = onnx.helper.make_node(
        'MatMul',
        inputs=['1', '2'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
1008
    return ([node], [m1,m2], [y])
Khalique's avatar
Khalique committed
1009

Khalique's avatar
Khalique committed
1010
@onnx_test
Khalique's avatar
Khalique committed
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
def matmul_vv_test():
    m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [7])
    m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [7])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [1])

    node = onnx.helper.make_node(
        'MatMul',
        inputs=['1', '2'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
1022
    return ([node], [m1,m2], [y])
Khalique's avatar
Khalique committed
1023

Khalique's avatar
Khalique committed
1024
@onnx_test
Khalique's avatar
Khalique committed
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
def max_test():
    a = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3])
    b = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3])
    c = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3])
    y = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3])

    node = onnx.helper.make_node(
        'Max',
        inputs=['0', '1', '2'],
        outputs=['3'],
    )

Khalique's avatar
Khalique committed
1037
    return ([node], [a,b,c], [y])
Khalique's avatar
Khalique committed
1038

Khalique's avatar
Khalique committed
1039
@onnx_test
Khalique's avatar
Khalique committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
def min_test():
    a = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3])
    b = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3])
    c = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3])
    y = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3])

    node = onnx.helper.make_node(
        'Min',
        inputs=['0', '1', '2'],
        outputs=['3'],
    )

Khalique's avatar
Khalique committed
1052
    return ([node], [a,b,c], [y])
Khalique's avatar
Khalique committed
1053

Khalique's avatar
Khalique committed
1054
@onnx_test
Khalique's avatar
Khalique committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
def no_pad_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 2])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [2, 2])

    node = onnx.helper.make_node(
        'Pad',
        inputs=['0'],
        pads=[0,0,0,0],
        outputs=['1']
    )

Khalique's avatar
Khalique committed
1066
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1067

Khalique's avatar
Khalique committed
1068
@onnx_test
Khalique's avatar
Khalique committed
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
def pad_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 2])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [4, 4])

    node = onnx.helper.make_node(
        'Pad',
        inputs=['0'],
        pads=[1,1,1,1],
        outputs=['1']
    )

Khalique's avatar
Khalique committed
1080
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1081

Khalique's avatar
Khalique committed
1082
@onnx_test
Khalique's avatar
Khalique committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
def pow_test():
    arg0 = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    arg1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [2, 3, 4, 5])
    arg_out = helper.make_tensor_value_info('out', TensorProto.FLOAT, [2, 3, 4, 5])

    node = onnx.helper.make_node(
        'Pow',
        inputs=['0', '1'],
        outputs=['out'],
    )


Khalique's avatar
Khalique committed
1095
    return ([node], [arg0, arg1], [arg_out])
Khalique's avatar
Khalique committed
1096

Khalique's avatar
Khalique committed
1097
@onnx_test
Khalique's avatar
Khalique committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
def reducemean_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4])
    axes=[2, 3]

    node = onnx.helper.make_node(
        'ReduceMean',
        inputs=['x'],
        outputs=['y'],
        axes=axes,
        keepdims = 0
    )

Khalique's avatar
Khalique committed
1111
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1112

Khalique's avatar
Khalique committed
1113
@onnx_test
Khalique's avatar
Khalique committed
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
def reducemean_keepdims_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4, 1, 6])
    axes=[2]

    node = onnx.helper.make_node(
        'ReduceMean',
        inputs=['x'],
        outputs=['y'],
        axes=axes,
        keepdims = 1
    )

Khalique's avatar
Khalique committed
1127
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1128

Khalique's avatar
Khalique committed
1129
@onnx_test
Khalique's avatar
Khalique committed
1130
def reducesum_test():
Khalique's avatar
Khalique committed
1131
1132
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4, 1, 1])
Khalique's avatar
Khalique committed
1133
    axes=[2]
Khalique's avatar
Khalique committed
1134

Khalique's avatar
Khalique committed
1135
1136
1137
1138
1139
1140
1141
1142
    node = onnx.helper.make_node(
        'ReduceSum',
        inputs=['x'],
        outputs=['y'],
        axes=axes,
        keepdims = 0
    )

Khalique's avatar
Khalique committed
1143
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1144

Khalique's avatar
Khalique committed
1145
@onnx_test
Khalique's avatar
Khalique committed
1146
1147
1148
def reducesum_multiaxis_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4, 1, 1])
Khalique's avatar
Khalique committed
1149
    axes=[2, 3]
Khalique's avatar
Khalique committed
1150
1151
1152
1153
1154
1155

    node = onnx.helper.make_node(
            'ReduceSum',
            inputs=['x'],
            outputs=['y'],
            axes=axes,
Khalique's avatar
Khalique committed
1156
            keepdims = 0
Khalique's avatar
Khalique committed
1157
1158
            )

Khalique's avatar
Khalique committed
1159
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1160

Khalique's avatar
Khalique committed
1161
@onnx_test
Khalique's avatar
Khalique committed
1162
1163
1164
1165
1166
def reducesum_keepdims_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4, 1, 1])
    axes=[2, 3]

Khalique's avatar
Khalique committed
1167
1168
1169
1170
1171
1172
1173
1174
    node = onnx.helper.make_node(
            'ReduceSum',
            inputs=['x'],
            outputs=['y'],
            axes=axes,
            keepdims = 1
            )

Khalique's avatar
Khalique committed
1175
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1176

Khalique's avatar
Khalique committed
1177
@onnx_test
Khalique's avatar
Khalique committed
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
def reshape_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [4, 2, 3])
    x_shape = helper.make_tensor_value_info('1', TensorProto.INT64, [2])
    x_shape_list = [3,8]
    y = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3, 8])
    y2 = helper.make_tensor_value_info('3', TensorProto.FLOAT, [3, 8])

    node = onnx.helper.make_node(
        'Reshape',
        inputs=['0', '1'],
        outputs=['2']
    )

    node2 = onnx.helper.make_node(
        'Reshape',
        inputs=['0'],
        shape=x_shape_list,
        outputs=['3']
    )

Khalique's avatar
Khalique committed
1198
    return (
Khalique's avatar
Khalique committed
1199
1200
1201
        [node,node2],
        [x, x_shape],
        [y,y2],
Khalique's avatar
Khalique committed
1202
        [helper.make_tensor('1', TensorProto.INT64, [2], [3, 8])]
Khalique's avatar
Khalique committed
1203
1204
    )

Khalique's avatar
Khalique committed
1205
@onnx_test
Khalique's avatar
Khalique committed
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
def reshape_non_standard_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [2, 3, 4])
    trans_x = helper.make_tensor_value_info('trans_x', TensorProto.FLOAT, [2, 4, 3])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [4, 3, 2])

    trans = helper.make_node(
        'Transpose',
        inputs=['x'],
        outputs=['trans_x'],
        perm=[0, 2, 1],
    )

    res = onnx.helper.make_node(
        'Reshape',
        inputs=['trans_x'],
        outputs=['y'],
        shape=[4, 3, 2]
    )

Khalique's avatar
Khalique committed
1225
    return ([trans,res], [x], [y])
Khalique's avatar
Khalique committed
1226

Khalique's avatar
Khalique committed
1227
@onnx_test
Khalique's avatar
Khalique committed
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
def shape_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
    y = helper.make_tensor_value_info('y', TensorProto.INT64, [4])

    node = onnx.helper.make_node(
        'Shape',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
1238
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1239

Khalique's avatar
Khalique committed
1240
@onnx_test
Khalique's avatar
Khalique committed
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
def shape_gather_test():
    values = np.array([1])
    value = helper.make_tensor_value_info('value', TensorProto.INT32, [1])
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [7, 3, 10])
    y = helper.make_tensor_value_info('y', TensorProto.INT64, [3])
    z = helper.make_tensor_value_info('z', TensorProto.FLOAT, [1])

    value_tensor = helper.make_tensor(
            name = 'const_tensor',
            data_type = TensorProto.INT32,
            dims = values.shape,
            vals = values.flatten().astype(int))

    node_const = onnx.helper.make_node(
        'Constant',
        inputs=[],
        outputs=['value'],
        value=value_tensor,
    )

    node_shape = onnx.helper.make_node(
        'Shape',
        inputs=['x'],
        outputs=['y'],
    )

    node_gather = helper.make_node(
        'Gather',
        inputs=['y', 'value'],
        outputs=['z'],
        axis=0,
    )

Khalique's avatar
Khalique committed
1274
    return ([node_const,node_shape,node_gather], [x], [z])
Khalique's avatar
Khalique committed
1275

Khalique's avatar
Khalique committed
1276
@onnx_test
Khalique's avatar
Khalique committed
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
def sign_test():
    x = helper.make_tensor_value_info('x', TensorProto.DOUBLE, [10, 5])
    y = helper.make_tensor_value_info('y', TensorProto.DOUBLE, [10, 5])

    node = onnx.helper.make_node(
        'Sign',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
1287
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1288

Khalique's avatar
Khalique committed
1289
@onnx_test
Khalique's avatar
Khalique committed
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
def sin_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Sin',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
1300
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1301

Khalique's avatar
Khalique committed
1302
@onnx_test
Khalique's avatar
Khalique committed
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
def sinh_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
        'Sinh',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
1313
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1314

Khalique's avatar
Khalique committed
1315
@onnx_test
Khalique's avatar
Khalique committed
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
def slice_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3, 2])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 2])

    node = onnx.helper.make_node(
        'Slice',
        inputs=['0'],
        axes=[0, 1],
        starts=[1,0],
        ends=[2, 2],
        outputs=['1']
    )

Khalique's avatar
Khalique committed
1329
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1330

Khalique's avatar
Khalique committed
1331
@onnx_test
Khalique's avatar
Khalique committed
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
def softmax_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 3])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 3])

    node = onnx.helper.make_node(
        'Softmax',
        inputs=['0'],
        outputs=['1']
    )

Khalique's avatar
Khalique committed
1342
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1343

Khalique's avatar
Khalique committed
1344
@onnx_test
Khalique's avatar
Khalique committed
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
def sqrt_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10, 15])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10, 15])

    node = onnx.helper.make_node(
        'Sqrt',
        inputs=['x'],
        outputs=['y'],
    )

Khalique's avatar
Khalique committed
1355
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1356

Khalique's avatar
Khalique committed
1357
@onnx_test
Khalique's avatar
Khalique committed
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
def squeeze_unsqueeze_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 3, 1, 1, 2, 1])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 2])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [1, 1, 3, 1, 2, 1])

    node = onnx.helper.make_node(
        'Squeeze',
        inputs=['0'],
        axes=[0, 2, 3, 5],
        outputs=['1']
    )

    node2 = onnx.helper.make_node(
        'Unsqueeze',
        inputs=['1'],
        axes=[0, 1, 3, 5],
        outputs=['2']
    )

Khalique's avatar
Khalique committed
1377
    return ([node,node2], [x], [z])
Khalique's avatar
Khalique committed
1378

Khalique's avatar
Khalique committed
1379
@onnx_test
Khalique's avatar
Khalique committed
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
def sub_bcast_test():
    arg0 = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    arg1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 4])
    arg_out = helper.make_tensor_value_info('out', TensorProto.FLOAT, [2, 3, 4, 5])

    node = onnx.helper.make_node(
        'Sub',
        inputs=['0', '1'],
        outputs=['out'],
        broadcast = 1,
        axis = 1,
    )

Khalique's avatar
Khalique committed
1393
    return ([node], [arg0,arg1], [arg_out])
Khalique's avatar
Khalique committed
1394

Khalique's avatar
Khalique committed
1395
@onnx_test
Khalique's avatar
Khalique committed
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
def sub_scalar_test():
    values = np.array([1])
    arg_node = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    arg_out = helper.make_tensor_value_info('out', TensorProto.FLOAT, [2, 3, 4, 5])

    values_tensor = helper.make_tensor(
        name = 'const',
        data_type = TensorProto.FLOAT,
        dims = values.shape,
        vals = values.flatten().astype(float)
    )

    arg_const = onnx.helper.make_node(
        'Constant',
        inputs=[],
        outputs=['arg_const'],
        value=values_tensor,
    )


    node = onnx.helper.make_node(
        'Sub',
        inputs=['0', 'arg_const'],
        outputs=['out'],
    )

Khalique's avatar
Khalique committed
1422
    return ([arg_const,node], [arg_node], [arg_out])
Khalique's avatar
Khalique committed
1423

Khalique's avatar
Khalique committed
1424
@onnx_test
Khalique's avatar
Khalique committed
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
def sum_test():
    a = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3])
    b = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3])
    c = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3])

    y = helper.make_tensor_value_info('3', TensorProto.FLOAT, [3])

    node = onnx.helper.make_node(
        'Sum',
        inputs=['0', '1', '2'],
        outputs=['3'],
    )

Khalique's avatar
Khalique committed
1438
    return ([node], [a,b,c], [y])
Khalique's avatar
Khalique committed
1439

Khalique's avatar
Khalique committed
1440
@onnx_test
Khalique's avatar
Khalique committed
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
def sum_test():
    a = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3])
    b = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3])
    c = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3])
    y = helper.make_tensor_value_info('3', TensorProto.FLOAT, [3])

    node = onnx.helper.make_node(
        'Sum',
        inputs=['0', '1', '2'],
        outputs=['3'],
    )

Khalique's avatar
Khalique committed
1453
    return ([node], [a,b,c], [y])
Khalique's avatar
Khalique committed
1454

Khalique's avatar
Khalique committed
1455
@onnx_test
Khalique's avatar
Khalique committed
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
def tan_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [10])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [10])

    node = onnx.helper.make_node(
            'Tan',
            inputs=['x'],
            outputs=['y'],
            )

Khalique's avatar
Khalique committed
1466
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1467

Khalique's avatar
Khalique committed
1468
@onnx_test
Khalique's avatar
Khalique committed
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
def tanh_test():
    x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [1])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [1])

    node = onnx.helper.make_node(
            'Tanh',
            inputs=['x'],
            outputs=['y'],
            )

Khalique's avatar
Khalique committed
1479
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1480

Khalique's avatar
Khalique committed
1481
@onnx_test
Khalique's avatar
Khalique committed
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
def transpose_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [1, 2, 2, 3])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [1, 3, 2, 2])

    node = onnx.helper.make_node(
        'Transpose',
        perm=[0, 3, 1, 2],
        inputs=['0'],
        outputs=['1'],
    )

Khalique's avatar
Khalique committed
1493
    return ([node], [x], [y])
Khalique's avatar
Khalique committed
1494

Khalique's avatar
Khalique committed
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
@onnx_test
def transpose_gather_test():
    x = helper.make_tensor_value_info('data', TensorProto.FLOAT, [3, 5, 4, 6])
    i = helper.make_tensor_value_info('indices', TensorProto.INT32, [2, 4, 3, 5])
    y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 2, 3, 4, 5, 4, 5, 6])

    td = onnx.helper.make_node(
        'Transpose',
        inputs=['data'],
        outputs=['tdata'],
        perm=[0, 2, 1, 3],
    )

    ti = onnx.helper.make_node(
        'Transpose',
        inputs=['indices'],
        outputs=['tindices'],
        perm=[0, 2, 1, 3]
    )

    node = onnx.helper.make_node(
        'Gather',
        inputs=['tdata', 'tindices'],
        outputs=['y'],
        axis=1,
    )


Khalique's avatar
Khalique committed
1523
    return ([td, ti, node], [x, i], [y])
Khalique's avatar
Khalique committed
1524

Khalique's avatar
Khalique committed
1525
@onnx_test
Khalique's avatar
Khalique committed
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
def unknown_test():
    x = helper.make_tensor_value_info('0', TensorProto.FLOAT, [2, 3, 4, 5])
    y = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 4])
    z = helper.make_tensor_value_info('2', TensorProto.FLOAT, [2, 3, 4, 5])
    a = helper.make_tensor_value_info('3', TensorProto.FLOAT, [2, 3, 4, 5])

    node = onnx.helper.make_node(
        'Unknown',
        inputs=['0', '1'],
        outputs=['2']
    )

    node2 = onnx.helper.make_node(
        'Unknown',
        inputs=['2'],
        outputs=['3']
    )

Khalique's avatar
Khalique committed
1544
    return ([node,node2], [x,y], [a])