aboutsummaryrefslogtreecommitdiff
path: root/verif/tosa_verif_build_tests.py
blob: 19eb2f49436dddd90583d5fe317ffd7997843e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3

# Copyright (c) 2020, ARM Limited.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.


import argparse
import sys
import re
import os
import subprocess
import shlex
import json
import glob
import math
import queue
import threading
import traceback


from enum import IntEnum, Enum, unique
from datetime import datetime

# Include the ../shared directory in PYTHONPATH
parent_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(parent_dir, '..', 'scripts'))
sys.path.append(os.path.join(parent_dir, '..', 'scripts', 'xunit'))
import xunit
from tosa_serializer import *
from tosa_test_gen import TosaTestGen
import tosa

# Used for parsing a comma-separated list of integers in a string
# to an actual list of integers
def str_to_list(in_s):
    '''Converts a comma-separated list of string integers to a python list of ints'''
    lst = in_s.split(',')
    out_list = []
    for i in lst:
        out_list.append(int(i))
    return out_list

def auto_int(x):
    '''Converts hex/dec argument values to an int'''
    return int(x, 0)

def parseArgs():

    parser = argparse.ArgumentParser()
    parser.add_argument('-o', dest='output_dir', type=str, default='vtest',
                        help='Test output directory')

    parser.add_argument('--seed', dest='random_seed', default=42, type=int,
                        help='Random seed for test generation')

    parser.add_argument('--filter', dest='filter', default='', type=str,
                        help='Filter operator test names by this expression')

    parser.add_argument('-v', '--verbose', dest='verbose', action='count',
                        help='Verbose operation')

    # Constraints on tests
    parser.add_argument('--tensor-dim-range', dest='tensor_shape_range', default='1,64',
                        type=lambda x: str_to_list(x),
                        help='Min,Max range of tensor shapes')

    parser.add_argument('--max-batch-size', dest='max_batch_size', default=1, type=int,
                        help='Maximum batch size for NHWC tests')

    parser.add_argument('--max-conv-padding', dest='max_conv_padding', default=1, type=int,
                        help='Maximum padding for Conv tests')

    parser.add_argument('--max-conv-dilation', dest='max_conv_dilation', default=2, type=int,
                        help='Maximum dilation for Conv tests')

    parser.add_argument('--max-conv-stride', dest='max_conv_stride', default=2, type=int,
                        help='Maximum stride for Conv tests')

    parser.add_argument('--max-pooling-padding', dest='max_pooling_padding', default=1, type=int,
                        help='Maximum padding for pooling tests')

    parser.add_argument('--max-pooling-stride', dest='max_pooling_stride', default=2, type=int,
                        help='Maximum stride for pooling tests')

    parser.add_argument('--max-pooling-kernel', dest='max_pooling_kernel', default=2, type=int,
                        help='Maximum padding for pooling tests')

    parser.add_argument('--num-rand-permutations', dest='num_rand_permutations', default=6, type=int,
                        help='Number of random permutations for a given shape/rank for randomly-sampled parameter spaces')

    # Targetting a specific shape/rank/dtype
    parser.add_argument('--target-shape', dest='target_shapes', action='append', default=[], type=lambda x: str_to_list(x),
                        help='Create tests with a particular input tensor shape, e.g., 1,4,4,8 (may be repeated for tests that require multiple input shapes)')

    parser.add_argument('--target-rank', dest='target_ranks', action='append', default=None, type=lambda x: auto_int(x),
                        help='Create tests with a particular input tensor rank')

    parser.add_argument('--target-dtype', dest='target_dtypes', action='append', default=None, type=lambda x: dtype_str_to_val(x),
                        help='Create test with a particular DType (may be repeated)')

    args = parser.parse_args()

    return args

def main():


    args = parseArgs()

    ttg = TosaTestGen(args)

    testList = []
    for op in ttg.TOSA_OP_LIST:
        if re.match(args.filter + '.*', op):
            testList.extend(ttg.genOpTestList(op, shapeFilter=args.target_shapes, rankFilter=args.target_ranks, dtypeFilter=args.target_dtypes))

    print('{} matching tests'.format(len(testList)))
    for opName, testStr, dtype, shapeList, testArgs in testList:
        print(testStr)
        ttg.serializeTest(opName, testStr, dtype, shapeList, testArgs)
    print('Done creating {} tests'.format(len(testList)))


if __name__ == '__main__':
    exit(main())