Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 3 | # Copyright (c) 2020-2021, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | |
| 18 | import numpy as np |
| 19 | import argparse |
| 20 | import sys |
| 21 | import re |
| 22 | import os |
| 23 | import subprocess |
| 24 | import shlex |
| 25 | import json |
| 26 | import glob |
| 27 | import math |
| 28 | import queue |
| 29 | import threading |
| 30 | import traceback |
| 31 | import math |
Jeremy Johnson | a618557 | 2021-06-21 15:55:35 +0100 | [diff] [blame] | 32 | import itertools |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 33 | |
| 34 | from enum import IntEnum, Enum, unique |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 35 | from tosa_ref_run import TosaReturnCode |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 36 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 37 | # Include the ../thirdparty/serialization_lib/python directory in PYTHONPATH |
| 38 | parent_dir = os.path.dirname(os.path.realpath(__file__)) |
| 39 | sys.path.append( |
| 40 | os.path.join(parent_dir, "..", "thirdparty", "serialization_lib", "python") |
| 41 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 42 | import tosa_serializer as ts |
| 43 | from tosa_serializer import * |
| 44 | import tosa |
| 45 | |
| 46 | # Convenience variables to the flatc-generated types that should be enums, but aren't |
| 47 | DType = tosa.DType.DType() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 48 | Op = tosa.Op.Op() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 49 | ResizeMode = tosa.ResizeMode.ResizeMode() |
| 50 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 51 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 52 | class TosaQuantGen: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 53 | """QuantizedInfo random generator helper functions. Specify with 'qgen': in the operator defintion""" |
| 54 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 55 | def __init__(self): |
| 56 | pass |
| 57 | |
| 58 | @staticmethod |
Les Bell | 30e4680 | 2021-07-23 09:43:31 +0100 | [diff] [blame] | 59 | def getQinfo(testGen, dtype): |
| 60 | if dtype == DType.INT8: |
| 61 | return testGen.randInt(-128, 128) |
| 62 | if dtype == DType.UINT8: |
| 63 | return testGen.randInt(0, 256) |
| 64 | return 0 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 65 | |
| 66 | @staticmethod |
| 67 | def qgUnary(testGen, op, dtype): |
| 68 | qinfo = ts.TosaSerializerQuantInfo() |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 69 | qinfo.UnaryQuantInfo( |
| 70 | TosaQuantGen.getQinfo(testGen, dtype), TosaQuantGen.getQinfo(testGen, dtype) |
| 71 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 72 | return qinfo |
| 73 | |
| 74 | @staticmethod |
Les Bell | 30e4680 | 2021-07-23 09:43:31 +0100 | [diff] [blame] | 75 | def qgConv(testGen, op, dtype_or_dtypeList): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 76 | qinfo = ts.TosaSerializerQuantInfo() |
Les Bell | 30e4680 | 2021-07-23 09:43:31 +0100 | [diff] [blame] | 77 | if isinstance(dtype_or_dtypeList, list): |
| 78 | # a list of [input, weights, accumulator] dtypes |
| 79 | dtypeList = dtype_or_dtypeList |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 80 | else: |
Les Bell | 30e4680 | 2021-07-23 09:43:31 +0100 | [diff] [blame] | 81 | # an int, [input, weights, accumulator] dtypes are the same |
| 82 | dtypeList = [dtype_or_dtypeList] * 3 |
| 83 | input_zp = TosaQuantGen.getQinfo(testGen, dtypeList[0]) |
| 84 | weights_zp = TosaQuantGen.getQinfo(testGen, dtypeList[1]) |
| 85 | qinfo.ConvQuantInfo(input_zp, weights_zp) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 86 | return qinfo |
| 87 | |
| 88 | @staticmethod |
| 89 | def qgMatmul(testGen, op, dtype): |
| 90 | qinfo = ts.TosaSerializerQuantInfo() |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 91 | qinfo.MatMulQuantInfo( |
| 92 | TosaQuantGen.getQinfo(testGen, dtype), TosaQuantGen.getQinfo(testGen, dtype) |
| 93 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 94 | return qinfo |
| 95 | |
| 96 | @staticmethod |
| 97 | def qgPad(testGen, op, dtype): |
| 98 | qinfo = ts.TosaSerializerQuantInfo() |
Les Bell | 30e4680 | 2021-07-23 09:43:31 +0100 | [diff] [blame] | 99 | qinfo.PadQuantInfo(TosaQuantGen.getQinfo(testGen, dtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 100 | return qinfo |
| 101 | |
| 102 | @staticmethod |
| 103 | def computeMultiplierAndShift(scaleFp, scale32): |
| 104 | # Derived from computeMultiplierAndShiftTosaScale32 |
| 105 | # Provide a floating-point scaling factor and the scale32 parameter |
| 106 | # to compute the multiplier and shift |
| 107 | |
| 108 | if scale32: |
| 109 | scaleBits = 31 |
| 110 | else: |
| 111 | scaleBits = 15 |
| 112 | |
| 113 | m, shift = math.frexp(scaleFp) |
| 114 | |
| 115 | if scaleFp < 0.0: |
| 116 | m = -m |
| 117 | |
| 118 | multiplier = round(m * (1 << scaleBits)) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 119 | assert multiplier <= (1 << scaleBits) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 120 | |
| 121 | if multiplier == (1 << scaleBits): |
| 122 | multiplier = multiplier // 2 |
| 123 | shift = shift + 1 |
| 124 | |
| 125 | shift = (-shift) + scaleBits |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 126 | #print('scalefp {} scaleBits {} m {} mult {} shift {}'.format(scaleFp, scaleBits, m, multiplier, shift)) |
| 127 | |
| 128 | # Adjust multiplier such that shift is in allowed value range. |
| 129 | if shift == 0: |
| 130 | multiplier = multiplier // 4 |
| 131 | shift = shift + 2 |
| 132 | elif shift == 1: |
| 133 | multiplier = multiplier // 2 |
| 134 | shift = shift + 1 |
| 135 | elif shift == 63: |
| 136 | multiplier = multiplier * 2 |
| 137 | shift = shift - 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 138 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 139 | assert multiplier <= (1 << scaleBits) |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 140 | assert shift >= 2 and shift <= 62 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 141 | |
| 142 | return multiplier, shift |
| 143 | |
| 144 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 145 | class TosaTensorGen: |
| 146 | """Tensor generators create a shape list for the placeholder and const tensor |
| 147 | data operands for the operator. The actual random data is generated separately for each test.""" |
| 148 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 149 | def __init__(self): |
| 150 | pass |
| 151 | |
| 152 | @staticmethod |
| 153 | def tgBasic(testGen, opName, rank): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 154 | pl, const = opName["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 155 | shape = testGen.makeShape(rank) |
| 156 | |
| 157 | shape_list = [] |
| 158 | for i in range(pl + const): |
| 159 | shape_list.append(shape.copy()) |
| 160 | |
| 161 | return shape_list |
| 162 | |
| 163 | @staticmethod |
| 164 | def tgNHWC(testGen, opName, rank): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 165 | pl, const = opName["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 166 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 167 | assert rank == 4 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 168 | |
| 169 | shape = testGen.makeShape(rank) |
| 170 | |
| 171 | # Constrict the batch size? |
| 172 | if testGen.args.max_batch_size: |
| 173 | shape[0] = (shape[0] % testGen.args.max_batch_size) + 1 |
| 174 | |
| 175 | shape_list = [] |
| 176 | for i in range(pl + const): |
| 177 | shape_list.append(shape.copy()) |
| 178 | |
| 179 | return shape_list |
| 180 | |
| 181 | @staticmethod |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 182 | def tgScatter(testGen, opName, rank): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 183 | pl, const = opName["operands"] |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 184 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 185 | assert pl == 2 |
| 186 | assert const == 0 |
| 187 | assert rank == 3 |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 188 | |
| 189 | values_in_shape = testGen.makeShape(rank) |
| 190 | |
Matthew Haddon | 4b2881a | 2021-08-24 14:25:43 +0100 | [diff] [blame] | 191 | # ignore max batch size if target shape is set |
| 192 | if testGen.args.max_batch_size and not testGen.args.target_shapes: |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 193 | values_in_shape[0] = (values_in_shape[0] % testGen.args.max_batch_size) + 1 |
| 194 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 195 | W = testGen.randInt( |
| 196 | testGen.args.tensor_shape_range[0], testGen.args.tensor_shape_range[1] |
| 197 | ) |
Matthew Haddon | 4b2881a | 2021-08-24 14:25:43 +0100 | [diff] [blame] | 198 | # Constrict W if one dimension is too large to keep tensor size reasonable |
| 199 | if max(values_in_shape) > 5000: |
| 200 | W = testGen.randInt(0, 16) |
| 201 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 202 | input_shape = [values_in_shape[0], W, values_in_shape[2]] |
| 203 | |
| 204 | shape_list = [] |
| 205 | shape_list.append(values_in_shape.copy()) |
| 206 | shape_list.append(input_shape.copy()) |
| 207 | |
| 208 | return shape_list |
| 209 | |
| 210 | @staticmethod |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 211 | def tgBroadcastFuzz(testGen, op, rank): |
| 212 | shape = testGen.makeShape(rank) |
| 213 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 214 | pl, const = op["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 215 | |
| 216 | shape_list = [] |
| 217 | |
| 218 | # Choose one of the inputs to broadcast |
| 219 | bcast_idx = testGen.randInt(0, pl + const) |
| 220 | for i in range(pl + const): |
| 221 | shape_bcast = shape.copy() |
| 222 | |
| 223 | # If the chosen input, pick a random index to broadcast |
| 224 | if i == bcast_idx: |
| 225 | fuzz_idx = testGen.randInt(0, rank) |
| 226 | shape_bcast[fuzz_idx] = 1 |
| 227 | |
| 228 | shape_list.append(shape_bcast) |
| 229 | |
| 230 | return shape_list |
| 231 | |
| 232 | @staticmethod |
| 233 | def tgConv2D(testGen, op, rank): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 234 | pl, const = op["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 235 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 236 | assert rank == 4 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 237 | |
| 238 | # IFM dimensions are NHWC |
| 239 | ifm_shape = testGen.makeShape(rank) |
| 240 | |
| 241 | # Constrict the batch size? |
| 242 | if testGen.args.max_batch_size: |
| 243 | ifm_shape[0] = (ifm_shape[0] % testGen.args.max_batch_size) + 1 |
| 244 | |
| 245 | # Get the filter height/width from the operator parameters |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 246 | filter_hw = op["filter"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 247 | |
| 248 | # Generate a random OFM depth |
| 249 | ofm_depth = testGen.makeShape(1)[0] |
| 250 | |
| 251 | # The filter dimensions are OHWI |
| 252 | filter_shape = np.asarray([ofm_depth, filter_hw[0], filter_hw[1], ifm_shape[3]]) |
| 253 | |
| 254 | # The bias is OC |
| 255 | bias_shape = np.asarray([ofm_depth]) |
| 256 | |
| 257 | return [ifm_shape, filter_shape, bias_shape] |
| 258 | |
| 259 | @staticmethod |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 260 | def tgConv3D(testGen, op, rank): |
| 261 | pl, const = op["operands"] |
| 262 | |
| 263 | assert rank == 5 |
| 264 | |
| 265 | # IFM dimensions are NDHWC |
| 266 | ifm_shape = testGen.makeShape(rank) |
| 267 | |
| 268 | # Constrict the batch size? |
| 269 | if testGen.args.max_batch_size: |
| 270 | ifm_shape[0] = (ifm_shape[0] % testGen.args.max_batch_size) + 1 |
| 271 | |
| 272 | # Get the filter depth/height/width from the operator parameters |
| 273 | filter_dhw = op["filter"] |
| 274 | |
| 275 | # Generate a random OFM channel |
| 276 | ofm_channel = testGen.makeShape(1)[0] |
| 277 | |
| 278 | # The filter dimensions are ODHWI |
| 279 | filter_shape = np.asarray( |
| 280 | [ofm_channel, filter_dhw[0], filter_dhw[1], filter_dhw[2], ifm_shape[4]] |
| 281 | ) |
| 282 | |
| 283 | # The bias is OC |
| 284 | bias_shape = np.asarray([ofm_channel]) |
| 285 | |
| 286 | return [ifm_shape, filter_shape, bias_shape] |
| 287 | |
| 288 | @staticmethod |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 289 | def tgTransposeConv2D(testGen, op, rank): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 290 | pl, const = op["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 291 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 292 | assert rank == 4 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 293 | |
| 294 | # IFM dimensions are NHWC |
| 295 | ifm_shape = testGen.makeShape(rank) |
| 296 | |
| 297 | # Constrict the batch size? |
| 298 | if testGen.args.max_batch_size: |
| 299 | ifm_shape[0] = (ifm_shape[0] % testGen.args.max_batch_size) + 1 |
| 300 | |
| 301 | # Get the filter height/width from the operator parameters |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 302 | filter_hw = op["filter"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 303 | |
| 304 | # Generate a random OFM depth |
| 305 | ofm_depth = testGen.makeShape(1)[0] |
| 306 | |
| 307 | # The filter dimensions are OHWI |
| 308 | filter_shape = np.asarray([ofm_depth, filter_hw[0], filter_hw[1], ifm_shape[3]]) |
| 309 | |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 310 | # The bias is OC |
| 311 | bias_shape = np.asarray([ofm_depth]) |
| 312 | |
| 313 | return [ifm_shape, filter_shape, bias_shape] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 314 | |
| 315 | @staticmethod |
| 316 | def tgDepthwiseConv2D(testGen, op, rank): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 317 | pl, const = op["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 318 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 319 | assert rank == 4 |
| 320 | assert pl == 1 and const == 2 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 321 | |
| 322 | # IFM dimensions are NHWC |
| 323 | ifm_shape = testGen.makeShape(rank) |
| 324 | |
| 325 | # Constrict the batch size? |
| 326 | if testGen.args.max_batch_size: |
| 327 | ifm_shape[0] = (ifm_shape[0] % testGen.args.max_batch_size) + 1 |
| 328 | |
| 329 | # Get the filter height/width from the operator parameters |
| 330 | # Filter is KH, HW, C, M |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 331 | filter_hw = op["filter"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 332 | |
| 333 | # Generate a random OFM depth, but don't let it get too big because |
| 334 | # the output depth is M * C |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 335 | filter_m = ( |
| 336 | testGen.makeShape(1)[0] % (testGen.args.tensor_shape_range[1] // 4) |
| 337 | ) + 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 338 | |
| 339 | # The filter dimensions are HWCM |
| 340 | filter_shape = np.asarray([filter_hw[0], filter_hw[1], ifm_shape[3], filter_m]) |
| 341 | |
| 342 | # The bias is M * C |
| 343 | bias_shape = np.asarray([ifm_shape[3] * filter_m]) |
| 344 | |
| 345 | return [ifm_shape, filter_shape, bias_shape] |
| 346 | |
| 347 | @staticmethod |
| 348 | def tgFullyConnected(testGen, op, rank): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 349 | pl, const = op["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 350 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 351 | assert rank == 2 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 352 | |
| 353 | input_shape = testGen.makeShape(rank) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 354 | filter_oc = testGen.rng.integers( |
| 355 | low=testGen.args.tensor_shape_range[0], |
| 356 | high=testGen.args.tensor_shape_range[1], |
| 357 | size=1, |
| 358 | )[0] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 359 | filter_shape = np.asarray([filter_oc, input_shape[1]]) |
| 360 | |
| 361 | bias_shape = np.asarray([filter_oc]) |
| 362 | |
| 363 | return [input_shape, filter_shape, bias_shape] |
| 364 | |
| 365 | @staticmethod |
| 366 | def tgMatmul(testGen, op, rank): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 367 | pl, const = op["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 368 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 369 | assert rank == 3 |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 370 | assert pl == 2 and const == 0 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 371 | |
| 372 | a_shape = testGen.makeShape(rank) |
Matthew Haddon | 68e7aee | 2021-08-16 11:20:25 +0100 | [diff] [blame] | 373 | # Get a random number for b_oc even if target shape is defined |
| 374 | b_oc = np.int32( |
| 375 | testGen.rng.integers( |
| 376 | low=testGen.args.tensor_shape_range[0], |
| 377 | high=testGen.args.tensor_shape_range[1], |
| 378 | size=1, |
| 379 | ) |
| 380 | )[0] |
| 381 | # If N or H is large let b_oc be 1 to reduce output tensor size |
| 382 | if max(a_shape) > 1000: |
| 383 | b_oc = 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 384 | |
Matthew Haddon | 68e7aee | 2021-08-16 11:20:25 +0100 | [diff] [blame] | 385 | b_shape = np.asarray([a_shape[0], a_shape[2], b_oc]) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 386 | return [a_shape, b_shape] |
| 387 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 388 | @staticmethod |
| 389 | def tgConcat(testGen, opName, rank): |
| 390 | pl, const = opName["operands"] |
| 391 | shape = testGen.makeShape(rank) |
| 392 | |
| 393 | # Create extra tensors to concat. |
| 394 | # Take into account value of pl when getting maximum number of concats |
| 395 | num_tensors = testGen.randInt(0, 4) |
| 396 | shape_list = [] |
| 397 | for i in range(pl + const + num_tensors): |
| 398 | shape_list.append(shape.copy()) |
| 399 | |
| 400 | return shape_list |
| 401 | |
| 402 | @staticmethod |
| 403 | def tgConcatConstInput(testGen, shapeList, axis): |
| 404 | # Split concat shape along axis to allow for multiple const inputs |
| 405 | # without making too many large tensors |
| 406 | shape = shapeList[0] |
| 407 | if len(shapeList) == 2 or shape[axis] < len(shapeList): |
| 408 | return shapeList |
| 409 | |
| 410 | new_shapeList = [shape.copy()] |
| 411 | length_on_axis = shape[axis] |
| 412 | remaining_length = length_on_axis |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 413 | for i in range(len(shapeList) - 2): |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 414 | # Calculate split on axis and remaining value |
| 415 | split_shape_val = int(shape[axis] / 2) |
| 416 | remaining_length = remaining_length - split_shape_val |
| 417 | |
| 418 | # Append new shape, and set remaining shape |
| 419 | shape[axis] = split_shape_val |
| 420 | new_shapeList.append(shape.copy()) |
| 421 | shape[axis] = remaining_length |
| 422 | if i == len(shapeList) - 3: |
| 423 | new_shapeList.append(shape.copy()) |
| 424 | |
| 425 | return new_shapeList |
| 426 | |
| 427 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 428 | class TosaArgGen: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 429 | """Argument generators create exhaustive or random lists of attributes for operators that take |
| 430 | attributes or other parameters. The return value is a list of (descriptive_name, [arglist]) |
| 431 | tuples where the descriptive_name is appended to the test name and the arglist is expanded |
| 432 | as arguments to the operator build function.""" |
| 433 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 434 | def __init__(self): |
| 435 | pass |
| 436 | |
| 437 | @staticmethod |
| 438 | def agNone(testGen, opName, shapeList, dtype): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 439 | """A trivial argument generator for operators that don't take any |
| 440 | non-tensor arguments""" |
| 441 | return [("", [])] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 442 | |
| 443 | @staticmethod |
| 444 | def agAxis(testGen, opName, shapeList, dtype): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 445 | """Build the axis argument for operators that take a single axis""" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 446 | axes = [] |
| 447 | |
| 448 | shape = shapeList[0] |
| 449 | |
| 450 | for a in range(0, len(shape)): |
Matthew Haddon | 43e3719 | 2021-07-09 14:13:02 +0100 | [diff] [blame] | 451 | axes.append(("axis{}".format(a), [a])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 452 | return axes |
| 453 | |
| 454 | @staticmethod |
| 455 | def agConv2D(testGen, opName, shapeList, dtype): |
| 456 | arg_list = [] |
| 457 | |
| 458 | ifm_shape = shapeList[0] |
| 459 | filter_shape = shapeList[1] |
| 460 | |
| 461 | # Must be rank 4 |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 462 | assert len(ifm_shape) == 4 |
| 463 | assert len(filter_shape) == 4 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 464 | |
| 465 | maxStride = testGen.args.max_conv_stride |
| 466 | maxPadding = testGen.args.max_conv_padding + 1 |
| 467 | maxDilation = testGen.args.max_conv_dilation |
| 468 | |
| 469 | # Strides, padding, dilations |
| 470 | for stride in range(0, maxStride ** 2): |
| 471 | for padding in range(0, (maxPadding) ** 4): |
| 472 | for dilation in range(0, maxDilation ** 2): |
| 473 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 474 | s = [stride // maxStride + 1, stride % maxStride + 1] |
| 475 | p = [ |
| 476 | (padding // (maxPadding * 4)) % maxPadding, |
| 477 | (padding // (maxPadding * 2)) % maxPadding, |
| 478 | (padding // (maxPadding * 1)) % maxPadding, |
| 479 | padding % maxPadding, |
| 480 | ] |
| 481 | d = [dilation // maxDilation + 1, dilation % maxDilation + 1] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 482 | |
| 483 | # 4 padding parameters for regular conv2d |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 484 | arg_list.append( |
| 485 | ( |
| 486 | "st{}{}_pad{}{}{}{}_dilat{}{}".format( |
| 487 | s[0], s[1], p[0], p[1], p[2], p[3], d[0], d[1] |
| 488 | ), |
| 489 | [s, p, d], |
| 490 | ) |
| 491 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 492 | return arg_list |
| 493 | |
| 494 | @staticmethod |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 495 | def agConv3D(testGen, opName, shapeList, dtype): |
| 496 | arg_list = [] |
| 497 | |
| 498 | ifm_shape = shapeList[0] |
| 499 | filter_shape = shapeList[1] |
| 500 | |
| 501 | # Must be rank 5 |
| 502 | assert len(ifm_shape) == 5 |
| 503 | assert len(filter_shape) == 5 |
| 504 | |
Les Bell | f414b3c | 2021-09-06 11:29:46 +0100 | [diff] [blame] | 505 | # Generate comprehensive argument list |
| 506 | p_range = [x for x in range(0, testGen.args.max_conv_padding + 1)] |
| 507 | paddings = [x for x in itertools.product(*([p_range] * 6))] |
| 508 | s_range = [x for x in range(1, testGen.args.max_conv_stride + 1)] |
| 509 | strides = [x for x in itertools.product(*([s_range] * 3))] |
| 510 | d_range = [x for x in range(1, testGen.args.max_conv_dilation + 1)] |
| 511 | dilations = [x for x in itertools.product(*([d_range] * 3))] |
| 512 | |
| 513 | # There are too many parameter combinations, so generate them sparsely |
| 514 | # To get a variety of parameter combinations sparsity should not be a multiple of 2, or 3 |
| 515 | # TODO: make sparsity a CLI option |
| 516 | sparsity = 37 |
| 517 | n = 0 |
| 518 | |
| 519 | for s in strides: |
| 520 | for p in paddings: |
| 521 | for d in dilations: |
| 522 | if n % sparsity == 0: |
| 523 | arg_list.append( |
| 524 | ( |
| 525 | "st{}_pad{}_dilat{}".format( |
| 526 | "".join([str(x) for x in s]), |
| 527 | "".join([str(x) for x in p]), |
| 528 | "".join([str(x) for x in d]), |
| 529 | ), |
| 530 | [s, p, d], |
| 531 | ) |
| 532 | ) |
| 533 | n += 1 |
| 534 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 535 | return arg_list |
| 536 | |
| 537 | @staticmethod |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 538 | def agTransposeConv2D(testGen, opName, shapeList, dtype): |
| 539 | arg_list = [] |
| 540 | |
| 541 | ifm_shape = shapeList[0] |
| 542 | filter_shape = shapeList[1] |
| 543 | |
| 544 | # Must be rank 4 |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 545 | assert len(ifm_shape) == 4 |
| 546 | assert len(filter_shape) == 4 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 547 | |
| 548 | maxStride = testGen.args.max_conv_stride |
| 549 | maxPadding = testGen.args.max_conv_padding + 1 |
| 550 | maxDilation = testGen.args.max_conv_dilation |
| 551 | |
| 552 | # Strides, padding, dilations |
| 553 | for stride in range(0, maxStride ** 2): |
| 554 | for out_padding in range(0, (maxPadding) ** 2): |
| 555 | for dilation in range(0, maxDilation ** 2): |
| 556 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 557 | s = [stride // maxStride + 1, stride % maxStride + 1] |
| 558 | p = [ |
| 559 | (out_padding // (maxPadding * 1)) % maxPadding, |
| 560 | out_padding % maxPadding, |
| 561 | ] |
| 562 | d = [dilation // maxDilation + 1, dilation % maxDilation + 1] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 563 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 564 | oh = ( |
| 565 | ifm_shape[1] |
| 566 | - filter_shape[1] |
| 567 | - (filter_shape[1] - 1) * (d[0] - 1) |
| 568 | + 2 * p[0] |
| 569 | ) // s[0] + 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 570 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 571 | ow = ( |
| 572 | ifm_shape[2] |
| 573 | - filter_shape[2] |
| 574 | - (filter_shape[2] - 1) * (d[1] - 1) |
| 575 | + 2 * p[1] |
| 576 | ) // s[1] + 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 577 | |
| 578 | # Output shape |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 579 | os = [ifm_shape[0], oh, ow, filter_shape[0]] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 580 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 581 | arg_list.append( |
| 582 | ( |
| 583 | "st{}{}_outpad{}{}_dilat{}{}_os{}x{}x{}x{}".format( |
| 584 | s[0], |
| 585 | s[1], |
| 586 | p[0], |
| 587 | p[1], |
| 588 | d[0], |
| 589 | d[1], |
| 590 | os[0], |
| 591 | os[1], |
| 592 | os[2], |
| 593 | os[3], |
| 594 | ), |
| 595 | [s, p, d, os], |
| 596 | ) |
| 597 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 598 | |
| 599 | return arg_list |
| 600 | |
| 601 | @staticmethod |
| 602 | def agPad(testGen, opName, shapeList, dtype): |
| 603 | arg_list = [] |
| 604 | rank = len(shapeList[0]) |
| 605 | |
Les Bell | 7ffccce | 2021-07-28 15:37:02 +0100 | [diff] [blame] | 606 | # Exhaustively test combinations of padding on each side of each dimension |
| 607 | # - the range of padding values is defined by pad_min and pad_max |
| 608 | # - for padding >9, the name format needs to be more distinctive |
| 609 | pad_min, pad_max = 0, 1 |
| 610 | pad_values = [x for x in range(pad_min, pad_max + 1)] |
| 611 | axis_pad_values = [x for x in itertools.product(pad_values, pad_values)] |
| 612 | shape_pad_values = itertools.product(*([axis_pad_values] * rank)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 613 | |
Les Bell | 7ffccce | 2021-07-28 15:37:02 +0100 | [diff] [blame] | 614 | for paddings in shape_pad_values: |
| 615 | name = "pad" |
| 616 | for r in range(rank): |
| 617 | before, after = paddings[r] |
| 618 | name = f"{name}{before}{after}" |
| 619 | arg_list.append((name, [np.array(paddings)])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 620 | |
| 621 | return arg_list |
| 622 | |
| 623 | @staticmethod |
| 624 | def agPooling(testGen, opName, shapeList, dtype): |
| 625 | arg_list = [] |
| 626 | |
| 627 | shape = shapeList[0] |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 628 | assert len(shape) == 4 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 629 | |
| 630 | maxStride = testGen.args.max_pooling_stride |
| 631 | maxKernel = testGen.args.max_pooling_kernel |
| 632 | maxPadding = testGen.args.max_pooling_padding + 1 |
| 633 | |
| 634 | for kernel in range(0, maxKernel ** 2): |
| 635 | for stride in range(0, maxStride ** 2): |
| 636 | for padding in range(0, maxPadding ** 4): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 637 | s = [stride // maxStride + 1, stride % maxStride + 1] |
| 638 | k = [(kernel // maxKernel) + 2, (kernel % maxKernel) + 2] |
| 639 | p = [ |
| 640 | (padding // (maxPadding * 4)) % maxPadding, |
| 641 | (padding // (maxPadding * 2)) % maxPadding, |
| 642 | (padding // (maxPadding * 1)) % maxPadding, |
| 643 | padding % maxPadding, |
| 644 | ] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 645 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 646 | arg_list.append( |
| 647 | ( |
| 648 | "st{}{}_kern{}{}_pad{}{}{}{}".format( |
| 649 | s[0], s[1], k[0], k[1], p[0], p[1], p[2], p[3] |
| 650 | ), |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 651 | [s, p, k], |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 652 | ) |
| 653 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 654 | return arg_list |
| 655 | |
| 656 | @staticmethod |
| 657 | def agCast(testGen, opName, shapeList, inDtype): |
| 658 | arg_list = [] |
| 659 | |
| 660 | # Enumerate the output types here |
| 661 | if inDtype == DType.INT8: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 662 | dtypeList = [DType.BOOL, DType.INT16, DType.INT32, DType.FLOAT] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 663 | elif inDtype == DType.INT16: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 664 | dtypeList = [DType.BOOL, DType.INT8, DType.INT32, DType.FLOAT] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 665 | elif inDtype == DType.INT32: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 666 | dtypeList = [DType.BOOL, DType.INT8, DType.INT16, DType.FLOAT] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 667 | elif inDtype == DType.BOOL: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 668 | dtypeList = [DType.INT8, DType.INT16, DType.INT32] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 669 | elif inDtype == DType.FLOAT: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 670 | dtypeList = [DType.INT8, DType.INT16, DType.INT32] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 671 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 672 | raise Exception("Unexpected input dtype: {}".format(inDtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 673 | |
| 674 | for dtype in dtypeList: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 675 | arg_list.append(("out{}".format(DTypeNames[dtype]), [dtype])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 676 | |
| 677 | return arg_list |
| 678 | |
| 679 | @staticmethod |
| 680 | def agRescale(testGen, opName, shapeList, inDtype): |
| 681 | arg_list = [] |
| 682 | |
| 683 | # Enumerate the output types here |
Matthew Haddon | cac4ee9 | 2021-07-22 14:30:53 +0100 | [diff] [blame] | 684 | for dtype in [DType.UINT8, DType.INT8, DType.INT16, DType.INT32]: |
| 685 | if inDtype == DType.UINT8 and dtype != DType.INT8: |
| 686 | # The only output dtype for UINT8 is INT8, skip all other combinations |
| 687 | continue |
| 688 | if inDtype != DType.INT8 and dtype == DType.UINT8: |
| 689 | # The only input dtype for UINT8 is INT8, skip all other combinations |
| 690 | continue |
| 691 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 692 | for scale32 in [False, True]: |
| 693 | for double_round in [False, True]: |
| 694 | for per_channel in [False, True]: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 695 | |
| 696 | if inDtype == DType.INT48 and scale32: |
| 697 | # Illegal condition. Must be scale32=False |
| 698 | continue |
Matthew Haddon | cac4ee9 | 2021-07-22 14:30:53 +0100 | [diff] [blame] | 699 | if double_round and not scale32: |
| 700 | # Illegal condition. ERROR_IF(!scale32 && double_round) |
| 701 | continue |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 702 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 703 | arg_list.append( |
| 704 | ( |
| 705 | "out{}_sc{}_dr{}_pc{}".format( |
| 706 | DTypeNames[dtype], |
| 707 | int(scale32), |
| 708 | int(double_round), |
| 709 | int(per_channel), |
| 710 | ), |
| 711 | [dtype, scale32, double_round, per_channel], |
| 712 | ) |
| 713 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 714 | |
| 715 | return arg_list |
| 716 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 717 | @staticmethod |
| 718 | def agMul(testGen, opName, shapeList, dtype): |
| 719 | arg_list = [] |
| 720 | |
| 721 | if dtype is DType.INT32: |
| 722 | for p in range(testGen.args.num_rand_permutations): |
| 723 | |
| 724 | shift = testGen.randInt(0, 32) |
| 725 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 726 | arg_list.append(("perm{}_shift{}".format(p, shift), [shift])) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 727 | else: |
Matthew Haddon | 43e3719 | 2021-07-09 14:13:02 +0100 | [diff] [blame] | 728 | arg_list.append(("perm0_shift0", [0])) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 729 | |
| 730 | return arg_list |
| 731 | |
| 732 | @staticmethod |
| 733 | def agArithmeticRightShift(testGen, opName, shapeList, dtype): |
| 734 | arg_list = [] |
| 735 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 736 | arg_list.append(("roundTrue", [True])) |
| 737 | arg_list.append(("roundFalse", [False])) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 738 | |
| 739 | return arg_list |
| 740 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 741 | # Helper function for reshape. Gets some factors of a larger number. |
| 742 | @staticmethod |
| 743 | def getFactors(val, start=1): |
| 744 | factors = [] |
| 745 | |
Matthew Haddon | 2ad047d | 2021-06-22 16:55:23 +0100 | [diff] [blame] | 746 | for i in range(start, int(np.sqrt(val)) + 1): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 747 | if (val % i) == 0: |
| 748 | factors.append(i) |
| 749 | |
| 750 | return factors |
| 751 | |
| 752 | @staticmethod |
| 753 | def agReshape(testGen, opName, shapeList, dtype): |
| 754 | arg_list = [] |
| 755 | |
| 756 | origShape = shapeList[0] |
| 757 | |
| 758 | totalElements = 1 |
| 759 | for s in origShape: |
| 760 | totalElements *= s |
| 761 | |
| 762 | # This code is NOT fast. Fortunately, the numbers are fairly small. |
| 763 | factors = TosaArgGen.getFactors(totalElements) |
| 764 | |
| 765 | for p in range(testGen.args.num_rand_permutations): |
Matthew Haddon | 5fc4e68 | 2021-07-07 11:28:29 +0100 | [diff] [blame] | 766 | newRank = testGen.randInt(1, 7) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 767 | if len(factors) < newRank: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 768 | continue |
| 769 | |
Matthew Haddon | 2ad047d | 2021-06-22 16:55:23 +0100 | [diff] [blame] | 770 | found = True |
| 771 | # escape_counter breaks while loop if it continues on for too long |
| 772 | escape_counter = 0 |
| 773 | while found: |
| 774 | newShape = [] |
| 775 | # Generate newShape ensuring it isn't a duplicate |
| 776 | remainingElements = totalElements |
| 777 | shuffledFactors = testGen.rng.permutation(factors) |
Matthew Haddon | 5fc4e68 | 2021-07-07 11:28:29 +0100 | [diff] [blame] | 778 | for i in range(1, newRank): |
Matthew Haddon | 2ad047d | 2021-06-22 16:55:23 +0100 | [diff] [blame] | 779 | # pick rank-1 factors |
| 780 | newShape.append(shuffledFactors[0]) |
| 781 | remainingElements = remainingElements // shuffledFactors[0] |
| 782 | shuffledFactors = testGen.rng.permutation( |
| 783 | TosaArgGen.getFactors(remainingElements) |
| 784 | ) |
| 785 | newShape.append(remainingElements) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 786 | |
Matthew Haddon | 2ad047d | 2021-06-22 16:55:23 +0100 | [diff] [blame] | 787 | # Toss in a -1 sometimes |
| 788 | minusOne = testGen.randInt(0, newRank * 4) |
| 789 | if minusOne < newRank: |
| 790 | newShape[minusOne] = -1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 791 | |
Matthew Haddon | 2ad047d | 2021-06-22 16:55:23 +0100 | [diff] [blame] | 792 | # Check for duplicates |
| 793 | found = False |
| 794 | for name, other_shape in arg_list: |
| 795 | if other_shape[0] == newShape: |
| 796 | found = True |
| 797 | break |
| 798 | |
| 799 | escape_counter += 1 |
| 800 | if escape_counter >= 100: |
| 801 | break |
| 802 | |
| 803 | if not found: |
| 804 | arg_list.append(("perm{}_rank{}".format(p, newRank), [newShape])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 805 | |
| 806 | return arg_list |
| 807 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 808 | @staticmethod |
| 809 | def agTranspose(testGen, opName, shapeList, dtype): |
| 810 | arg_list = [] |
| 811 | |
| 812 | ifm_shape = shapeList[0] |
| 813 | |
Jeremy Johnson | a618557 | 2021-06-21 15:55:35 +0100 | [diff] [blame] | 814 | # Get all permutations |
| 815 | permutations = [p for p in itertools.permutations(range(len(ifm_shape)))] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 816 | |
Jeremy Johnson | a618557 | 2021-06-21 15:55:35 +0100 | [diff] [blame] | 817 | # Limit to possible permutations from shape dimension or argument setting |
| 818 | limit = min(len(permutations), testGen.args.num_rand_permutations) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 819 | |
Jeremy Johnson | a618557 | 2021-06-21 15:55:35 +0100 | [diff] [blame] | 820 | # Get random permutation generator that uses all permutations |
| 821 | random_permutations = testGen.rng.permutation(permutations) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 822 | |
Jeremy Johnson | a618557 | 2021-06-21 15:55:35 +0100 | [diff] [blame] | 823 | # Create list of required amount of permutations |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 824 | arg_list = [ |
| 825 | ("perm{}".format(p), [random_permutations[p].tolist()]) |
| 826 | for p in range(limit) |
| 827 | ] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 828 | return arg_list |
| 829 | |
| 830 | @staticmethod |
| 831 | def agSlice(testGen, opName, shapeList, dtype): |
| 832 | arg_list = [] |
| 833 | |
| 834 | ifm_shape = shapeList[0] |
| 835 | rank = len(ifm_shape) |
| 836 | |
| 837 | for p in range(testGen.args.num_rand_permutations): |
| 838 | begin = [] |
| 839 | size = [] |
| 840 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 841 | valid = True |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 842 | |
| 843 | for i in range(rank): |
| 844 | if ifm_shape[i] > 1: |
| 845 | begin.append(testGen.randInt(0, ifm_shape[i])) |
| 846 | size.append(testGen.randInt(0, ifm_shape[i] - begin[i])) |
| 847 | |
| 848 | # Invalid slice size? |
| 849 | if size[i] == 0: |
| 850 | valid = False |
| 851 | else: |
| 852 | begin.append(0) |
| 853 | size.append(1) |
| 854 | |
| 855 | if valid: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 856 | arg_list.append(("perm{}".format(p), [begin, size])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 857 | return arg_list |
| 858 | |
| 859 | @staticmethod |
| 860 | def agTile(testGen, opName, shapeList, dtype): |
| 861 | arg_list = [] |
| 862 | |
| 863 | ifm_shape = shapeList[0] |
| 864 | rank = len(ifm_shape) |
| 865 | |
| 866 | for p in range(testGen.args.num_rand_permutations): |
| 867 | |
| 868 | # Pick a few random, but small multiple values |
| 869 | # because otherwise this has a tendency to generate |
| 870 | # enormous tensors |
| 871 | multiples = [] |
| 872 | for i in range(rank): |
Matthew Haddon | 82ad4d6 | 2021-08-20 15:02:39 +0100 | [diff] [blame] | 873 | if ifm_shape[i] > 1000: |
| 874 | # Multiple of 1 if ifm_shape dimension is large to reduce tensor size |
| 875 | multiples.append(1) |
| 876 | elif max(ifm_shape) > 1000: |
| 877 | multiples.append(2) |
| 878 | else: |
| 879 | multiples.append(testGen.randInt(1, 4)) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 880 | arg_list.append(("perm{}".format(p), [multiples])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 881 | |
| 882 | return arg_list |
| 883 | |
| 884 | @staticmethod |
| 885 | def agResize(testGen, opName, shapeList, dtype): |
| 886 | arg_list = [] |
| 887 | |
| 888 | ifm_shape = shapeList[0] |
| 889 | |
| 890 | for m in [ResizeMode.NEAREST, ResizeMode.BILINEAR]: |
| 891 | |
| 892 | # Exclude illegal {mode, type} configurations. Pick legal output types |
| 893 | if m == ResizeMode.NEAREST and dtype == DType.INT8: |
Les Bell | 33d837e | 2021-08-10 08:34:43 +0100 | [diff] [blame] | 894 | outputDTypeList = [DType.INT8] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 895 | elif m == ResizeMode.NEAREST and dtype == DType.INT16: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 896 | outputDTypeList = [DType.INT16] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 897 | elif m == ResizeMode.BILINEAR and dtype == DType.INT8: |
Les Bell | 33d837e | 2021-08-10 08:34:43 +0100 | [diff] [blame] | 898 | outputDTypeList = [DType.INT32] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 899 | elif m == ResizeMode.BILINEAR and dtype == DType.INT16: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 900 | outputDTypeList = [DType.INT48] |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 901 | elif dtype == DType.FLOAT: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 902 | outputDTypeList = [DType.FLOAT] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 903 | else: |
| 904 | continue |
| 905 | |
| 906 | for outputDType in outputDTypeList: |
| 907 | for perm in range(testGen.args.num_rand_permutations): |
| 908 | |
| 909 | # Randomly generate legal output dimensions and shift |
| 910 | # and then compute the stride and offset based on them |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 911 | output_dims = [testGen.randInt(1), testGen.randInt(1)] |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 912 | in_center_h = (ifm_shape[1] - 1) / 2.0 |
| 913 | in_center_w = (ifm_shape[2] - 1) / 2.0 |
| 914 | out_center_h = (output_dims[0] - 1) / 2.0 |
| 915 | out_center_w = (output_dims[1] - 1) / 2.0 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 916 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 917 | fp_stride_y = float(ifm_shape[1]) / float(output_dims[0]) |
| 918 | fp_stride_x = float(ifm_shape[2]) / float(output_dims[1]) |
| 919 | fp_offset_y = in_center_h - fp_stride_y * out_center_h |
| 920 | fp_offset_x = in_center_w - fp_stride_x * out_center_w |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 921 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 922 | if outputDType == DType.FLOAT: |
| 923 | shift = 0 |
| 924 | stride = [0, 0] |
| 925 | offset = [0, 0] |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 926 | stride_fp = [fp_stride_y, fp_stride_x] |
| 927 | offset_fp = [fp_offset_y, fp_offset_x] |
| 928 | arg_list.append( |
| 929 | ( |
| 930 | "mode{}_odim{}x{}_out{}_st{:.2f}x{:.2f}_off{:.2f}x{:.2f}".format( |
Les Bell | 33d837e | 2021-08-10 08:34:43 +0100 | [diff] [blame] | 931 | "N" if m == ResizeMode.NEAREST else "B", |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 932 | output_dims[0], |
| 933 | output_dims[1], |
| 934 | testGen.typeStr(outputDType), |
| 935 | stride_fp[0], |
| 936 | stride_fp[1], |
| 937 | offset_fp[0], |
| 938 | offset_fp[1], |
| 939 | ), |
| 940 | [ |
| 941 | m, |
| 942 | stride, |
| 943 | offset, |
| 944 | shift, |
| 945 | stride_fp, |
| 946 | offset_fp, |
| 947 | output_dims, |
| 948 | dtype, |
| 949 | outputDType, |
| 950 | ], |
| 951 | ) |
| 952 | ) |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 953 | else: |
| 954 | shift = 11 |
| 955 | unit = float(1 << shift) |
| 956 | stride_y = int(round(fp_stride_y * unit)) |
| 957 | stride_x = int(round(fp_stride_x * unit)) |
| 958 | offset_y = int(round(fp_offset_y * unit)) |
| 959 | offset_x = int(round(fp_offset_x * unit)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 960 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 961 | while ( |
| 962 | stride_y >= 32768 |
| 963 | or stride_x >= 32768 |
| 964 | or offset_y >= 32768 |
| 965 | or offset_x >= 32768 |
| 966 | or offset_y < -32768 |
| 967 | or offset_x < -32768 |
| 968 | ): |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 969 | shift = shift - 1 |
| 970 | unit = float(1 << shift) |
| 971 | stride_y = int(round(fp_stride_y * unit)) |
| 972 | stride_x = int(round(fp_stride_x * unit)) |
| 973 | offset_y = int(round(fp_offset_y * unit)) |
| 974 | offset_x = int(round(fp_offset_x * unit)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 975 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 976 | stride = [stride_y, stride_x] |
| 977 | offset = [offset_y, offset_x] |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 978 | |
| 979 | stride_fp = [0.0, 0.0] |
| 980 | offset_fp = [0.0, 0.0] |
| 981 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 982 | arg_list.append( |
| 983 | ( |
| 984 | "mode{}_shift{}_odim{}x{}_out{}_st{}x{}_off{}x{}".format( |
Les Bell | 33d837e | 2021-08-10 08:34:43 +0100 | [diff] [blame] | 985 | "N" if m == ResizeMode.NEAREST else "B", |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 986 | shift, |
| 987 | output_dims[0], |
| 988 | output_dims[1], |
| 989 | testGen.typeStr(outputDType), |
| 990 | stride[0], |
| 991 | stride[1], |
| 992 | offset[0], |
| 993 | offset[1], |
| 994 | ), |
| 995 | [ |
| 996 | m, |
| 997 | stride, |
| 998 | offset, |
| 999 | shift, |
| 1000 | stride_fp, |
| 1001 | offset_fp, |
| 1002 | output_dims, |
| 1003 | dtype, |
| 1004 | outputDType, |
| 1005 | ], |
| 1006 | ) |
| 1007 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1008 | |
| 1009 | return arg_list |
| 1010 | |
| 1011 | def agCondIf(testGen, opName, shapeList, dtype): |
| 1012 | # CondIf generates the condition values here. |
| 1013 | # Convert to tensors in the build function, along with the |
| 1014 | # then and else blocks |
| 1015 | arg_list = [] |
| 1016 | |
| 1017 | for c in [False, True]: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1018 | arg_list.append(("cond{}".format(int(c)), [c])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1019 | |
| 1020 | return arg_list |
| 1021 | |
| 1022 | def agWhileLoop(testGen, opName, shapeList, dtype): |
| 1023 | # While loop: 0 iterations, 1, more than 1 |
| 1024 | arg_list = [] |
| 1025 | |
| 1026 | for iter in [0, 1, 4]: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1027 | arg_list.append(("iter{}".format(iter), [iter])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1028 | |
| 1029 | return arg_list |
| 1030 | |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 1031 | class TosaInvalidValidator: |
| 1032 | |
| 1033 | @staticmethod |
| 1034 | def ivWrongDataTypeOrModeResize(**kwargs): |
| 1035 | input_dtype = kwargs["input_dtype"] |
| 1036 | args = kwargs["args"] |
| 1037 | mode = args[0] |
| 1038 | stride = args[1] |
| 1039 | stride_fp = args[4] |
| 1040 | output_dtype = args[8] |
| 1041 | |
| 1042 | if mode == ResizeMode.BILINEAR: |
| 1043 | # Invalid output data type / Invalid input datatype |
| 1044 | return ( |
| 1045 | not (input_dtype == DType.INT8 and output_dtype == DType.INT32) or |
| 1046 | not (input_dtype == DType.INT16 and output_dtype == DType.INT48) or |
| 1047 | not (input_dtype == DType.FLOAT and output_dtype == DType.FLOAT) or |
| 1048 | (input_dtype not in [DType.INT8, DType.INT32, DType.FLOAT]) |
| 1049 | ) |
| 1050 | elif mode == ResizeMode.NEAREST: |
| 1051 | # Invalid output data type / Invalid input datatype |
| 1052 | return ( |
| 1053 | (input_dtype != output_dtype) or |
| 1054 | (input_dtype not in [DType.INT8, DType.INT32, DType.FLOAT]) |
| 1055 | ) |
| 1056 | else: |
| 1057 | # Invalid resize mode |
| 1058 | return True |
| 1059 | |
| 1060 | @staticmethod |
| 1061 | def ivBadStride(**kwargs): |
| 1062 | input_dtype = kwargs["input_dtype"] |
| 1063 | args = kwargs["args"] |
| 1064 | stride_x = args[1][0] |
| 1065 | stride_y = args[1][1] |
| 1066 | stride_fp_x = args[4][0] |
| 1067 | stride_fp_y = args[4][1] |
| 1068 | |
| 1069 | if input_dtype == DType.FLOAT: |
| 1070 | if stride_fp_x <= 0 or stride_fp_y <= 0: |
| 1071 | # Negative or zero stride |
| 1072 | return True |
| 1073 | else: |
| 1074 | if stride_x <= 0 or stride_y <= 0: |
| 1075 | # Negative or zero stride |
| 1076 | return True |
| 1077 | return False |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | @staticmethod |
| 1083 | def ivHeightWidthSmallerZero(**kwargs): |
| 1084 | opName = kwargs['opName'] |
| 1085 | |
| 1086 | inputShapes = kwargs['shapeList'] |
| 1087 | input = inputShapes[0] |
| 1088 | if not opName.endswith("pool2d"): |
| 1089 | filter = inputShapes[1] |
| 1090 | |
| 1091 | args = kwargs['args'] |
| 1092 | strides = args[0] |
| 1093 | padding = args[1] |
| 1094 | dilations = args[2] |
| 1095 | if opName.endswith("pool2d"): |
| 1096 | kernel = args[2] |
| 1097 | |
| 1098 | if opName.startswith('conv2d'): |
| 1099 | h = ( |
| 1100 | input[1] |
| 1101 | - filter[1] |
| 1102 | - (filter[1] - 1) * (dilations[0] - 1) |
| 1103 | + padding[0] |
| 1104 | + padding[1] |
| 1105 | ) // strides[0] + 1 |
| 1106 | |
| 1107 | w = ( |
| 1108 | input[2] |
| 1109 | - filter[2] |
| 1110 | - (filter[2] - 1) * (dilations[1] - 1) |
| 1111 | + padding[2] |
| 1112 | + padding[3] |
| 1113 | ) // strides[1] + 1 |
| 1114 | elif opName.startswith("depthwise_conv2d"): |
| 1115 | h = ( |
| 1116 | input[1] |
| 1117 | - filter[0] |
| 1118 | - (filter[0] - 1) * (dilations[0] - 1) |
| 1119 | + padding[0] |
| 1120 | + padding[1] |
| 1121 | ) // strides[0] + 1 |
| 1122 | |
| 1123 | w = ( |
| 1124 | input[2] |
| 1125 | - filter[1] |
| 1126 | - (filter[1] - 1) * (dilations[1] - 1) |
| 1127 | + padding[2] |
| 1128 | + padding[3] |
| 1129 | ) // strides[1] + 1 |
| 1130 | elif opName.endswith("pool2d"): |
| 1131 | h = (input[1] + padding[0] + padding[1] + strides[0] - kernel[0]) // strides[0] |
| 1132 | w = (input[2] + padding[2] + padding[3] + strides[1] - kernel[1]) // strides[1] |
| 1133 | else: |
| 1134 | assert False, "Unrecognized Op" |
| 1135 | |
| 1136 | if h <= 0 or w <= 0: |
| 1137 | # Invalid parameter combination |
| 1138 | return True |
| 1139 | return False |
| 1140 | |
| 1141 | @staticmethod |
| 1142 | def ivNonPositiveOutputShape(**kwargs): |
| 1143 | args = kwargs['args'] |
| 1144 | output_shape = args[3] |
| 1145 | if output_shape[1] <= 0 or output_shape[2] <= 0: |
| 1146 | # Negative output shape |
| 1147 | return True |
| 1148 | return False |
| 1149 | |
| 1150 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1151 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1152 | class TosaTestGen: |
Jeremy Johnson | 97eb75f | 2021-07-08 11:58:02 +0100 | [diff] [blame] | 1153 | # Maximum rank of tensor supported by test generator. |
| 1154 | TOSA_TENSOR_MAX_RANK = 6 |
| 1155 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1156 | def __init__(self, args): |
| 1157 | self.args = args |
| 1158 | self.basePath = args.output_dir |
| 1159 | self.random_seed = args.random_seed |
| 1160 | self.ser = None |
| 1161 | self.rng = np.random.default_rng(self.random_seed) |
| 1162 | self.createDynamicOpLists() |
| 1163 | self.initOpListDefaults() |
| 1164 | self.quantGen = TosaQuantGen() |
| 1165 | # Force makeShape to do a specific starting shape |
| 1166 | self.targetted_shape = None |
| 1167 | |
| 1168 | def createSerializer(self, opName, testPath): |
| 1169 | self.testPath = os.path.join(opName, testPath) |
| 1170 | |
| 1171 | fullPath = os.path.join(self.basePath, self.testPath) |
| 1172 | os.makedirs(fullPath, exist_ok=True) |
| 1173 | self.ser = ts.TosaSerializer(fullPath) |
| 1174 | |
| 1175 | def getSerializer(self): |
| 1176 | return self.ser |
| 1177 | |
| 1178 | def serialize(self, testName): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1179 | with open( |
| 1180 | os.path.join(self.basePath, self.testPath, "{}.tosa".format(testName)), "wb" |
| 1181 | ) as fd: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1182 | fd.write(self.ser.serialize()) |
| 1183 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1184 | with open(os.path.join(self.basePath, self.testPath, "desc.json"), "w") as fd: |
| 1185 | fd.write(self.ser.writeJson("{}.tosa".format(testName))) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1186 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1187 | def resetRNG(self, seed=None): |
| 1188 | if seed == None: |
| 1189 | seed = self.random_seed + 1 |
| 1190 | self.rng = np.random.default_rng(seed) |
| 1191 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1192 | def getRandTensor(self, shape, dtype): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1193 | if dtype == DType.BOOL: |
| 1194 | np_dt = np.bool |
| 1195 | return np.bool_(self.rng.choice(a=[False, True], size=shape)) |
Kevin Cheng | a901740 | 2021-07-28 17:19:23 -0700 | [diff] [blame] | 1196 | # TOSA specific INT4 weight range from -7 to 7 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1197 | elif dtype == DType.INT4: |
Kevin Cheng | a901740 | 2021-07-28 17:19:23 -0700 | [diff] [blame] | 1198 | return np.int32(self.rng.integers(low=-7, high=8, size=shape)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1199 | elif dtype == DType.INT8: |
Jeremy Johnson | 18e2666 | 2021-07-22 16:15:29 +0100 | [diff] [blame] | 1200 | return np.int32(self.rng.integers(low=-128, high=128, size=shape)) |
| 1201 | elif dtype == DType.UINT8: |
| 1202 | return np.int32(self.rng.integers(low=0, high=256, size=shape)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1203 | elif dtype == DType.INT16: |
| 1204 | return np.int32(self.rng.integers(low=-32768, high=32768, size=shape)) |
| 1205 | elif dtype == DType.INT32: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1206 | return np.int32( |
| 1207 | self.rng.integers(low=-(1 << 31), high=(1 << 31), size=shape) |
| 1208 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1209 | elif dtype == DType.INT48: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1210 | return np.int64( |
| 1211 | self.rng.integers(low=-(1 << 47), high=(1 << 47), size=shape) |
| 1212 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1213 | elif dtype == DType.FLOAT: |
Jeremy Johnson | 18e2666 | 2021-07-22 16:15:29 +0100 | [diff] [blame] | 1214 | return np.float32(self.rng.random(size=shape)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1215 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1216 | raise Exception("Unrecognized Dtype: {}".format(dtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1217 | |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1218 | def buildPlaceholderTensors(self, shape_list, dtype_list): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1219 | placeholders = [] |
| 1220 | |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1221 | assert len(shape_list) == len(dtype_list) |
| 1222 | |
| 1223 | for idx, shape in enumerate(shape_list): |
| 1224 | arr = self.getRandTensor(shape, dtype_list[idx]) |
| 1225 | placeholders.append(self.ser.addPlaceholder(shape, dtype_list[idx], arr)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1226 | |
| 1227 | return placeholders |
| 1228 | |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1229 | def buildConstTensors(self, shape_list, dtype_list): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1230 | consts = [] |
| 1231 | |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1232 | assert len(shape_list) == len(dtype_list) |
| 1233 | |
| 1234 | for idx, shape in enumerate(shape_list): |
| 1235 | arr = self.getRandTensor(shape, dtype_list[idx]) |
| 1236 | consts.append(self.ser.addConst(shape, dtype_list[idx], arr)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1237 | |
| 1238 | return consts |
| 1239 | |
| 1240 | def makeShape(self, rank): |
| 1241 | if self.targetted_shape: |
| 1242 | return np.int32(self.targetted_shape) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1243 | return np.int32( |
| 1244 | self.rng.integers( |
| 1245 | low=self.args.tensor_shape_range[0], |
| 1246 | high=self.args.tensor_shape_range[1], |
| 1247 | size=rank, |
| 1248 | ) |
| 1249 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1250 | |
| 1251 | def setTargetShape(self, shape): |
| 1252 | self.targetted_shape = shape |
| 1253 | |
| 1254 | def randInt(self, low=0, high=256): |
| 1255 | return np.int32(self.rng.integers(low=low, high=high, size=1))[0] |
| 1256 | |
| 1257 | def getRandNumberDType(self, dtype): |
| 1258 | if dtype == DType.FLOAT: |
| 1259 | return self.rng.random() |
| 1260 | elif dtype == DType.BOOL: |
| 1261 | return self.rng.choice([False, True]) |
Kevin Cheng | a901740 | 2021-07-28 17:19:23 -0700 | [diff] [blame] | 1262 | # TOSA specific INT4 weight range from -7 to 7 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1263 | elif dtype == DType.INT4: |
Kevin Cheng | a901740 | 2021-07-28 17:19:23 -0700 | [diff] [blame] | 1264 | low, high = (-7, 8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1265 | elif dtype == DType.INT8: |
Jeremy Johnson | 18e2666 | 2021-07-22 16:15:29 +0100 | [diff] [blame] | 1266 | low, high = (-128, 128) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1267 | elif dtype == DType.INT16: |
| 1268 | low, high = (-32768, 32768) |
| 1269 | elif dtype == DType.INT32: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1270 | low, high = (-(1 << 31), (1 << 31)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1271 | elif dtype == DType.INT48: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1272 | low, high = (-(1 << 47), (1 << 47)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1273 | # Special size |
| 1274 | return np.int64(self.rng.integers(low, high, size=1))[0] |
| 1275 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1276 | raise Exception("Unknown dtype: {}".format(dtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1277 | |
| 1278 | return np.int32(self.rng.integers(low, high, size=1))[0] |
| 1279 | |
| 1280 | def shapeStr(self, shape): |
| 1281 | |
| 1282 | sStr = [] |
| 1283 | # Convert to strings |
| 1284 | for i in shape: |
| 1285 | sStr.append(str(i)) |
| 1286 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1287 | return "x".join(sStr) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1288 | |
| 1289 | def typeStr(self, t): |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1290 | if isinstance(t, list): |
| 1291 | assert len(t) >= 2 |
| 1292 | return "{}x{}".format(self.typeStr(t[0]), self.typeStr(t[1])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1293 | else: |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1294 | if t == DType.BOOL: |
| 1295 | return "b" |
| 1296 | elif t == DType.INT4: |
| 1297 | return "i4" |
| 1298 | elif t == DType.INT8: |
| 1299 | return "i8" |
| 1300 | elif t == DType.UINT8: |
| 1301 | return "u8" |
| 1302 | elif t == DType.INT16: |
| 1303 | return "i16" |
| 1304 | elif t == DType.INT32: |
| 1305 | return "i32" |
| 1306 | elif t == DType.INT48: |
| 1307 | return "i48" |
| 1308 | elif t == DType.FLOAT: |
| 1309 | return "float" |
| 1310 | else: |
| 1311 | raise Exception("Unknown dtype, cannot convert to string: {}".format(t)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1312 | |
| 1313 | def typeWidth(self, t): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1314 | """ Get the datatype width for integer types""" |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 1315 | if t == DType.INT4: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1316 | return 4 |
| 1317 | elif t == DType.INT8: |
| 1318 | return 8 |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 1319 | elif t == DType.UINT8: |
| 1320 | return 8 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1321 | elif t == DType.INT16: |
| 1322 | return 16 |
| 1323 | elif t == DType.INT32: |
| 1324 | return 32 |
| 1325 | elif t == DType.INT48: |
| 1326 | return 48 |
| 1327 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1328 | raise Exception("Unknown dtype, cannot convert to string: {}".format(t)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1329 | |
| 1330 | # Argument generators |
| 1331 | # Returns a list of tuples (stringDescriptor, [build_fcn_arg_list]) |
| 1332 | # Where the string descriptor is used to generate the test name and |
| 1333 | # The build_fcn_arg_list is expanded and passed to the operator test |
| 1334 | # build function |
| 1335 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1336 | def build_unary(self, op, a, qinfo=None): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1337 | result_tens = OutputShaper.unaryOp(self.ser, a) |
| 1338 | self.ser.addOperator(op, [a.name], [result_tens.name], None, qinfo) |
| 1339 | return result_tens |
| 1340 | |
| 1341 | def build_binary_broadcast(self, op, a, b): |
| 1342 | result_tens = OutputShaper.binaryBroadcastOp(self.ser, a, b) |
| 1343 | self.ser.addOperator(op, [a.name, b.name], [result_tens.name]) |
| 1344 | return result_tens |
| 1345 | |
| 1346 | def build_binary_nonbroadcast(self, op, a, b): |
| 1347 | result_tens = OutputShaper.binaryNonBroadcastOp(self.ser, a, b) |
| 1348 | self.ser.addOperator(op, [a.name, b.name], [result_tens.name]) |
| 1349 | return result_tens |
| 1350 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 1351 | def build_arithmetic_right_shift(self, op, a, b, round): |
| 1352 | result_tens = OutputShaper.binaryBroadcastOp(self.ser, a, b) |
| 1353 | |
| 1354 | attr = ts.TosaSerializerAttribute() |
| 1355 | attr.ArithmeticRightShiftAttribute(round) |
| 1356 | |
| 1357 | self.ser.addOperator(op, [a.name, b.name], [result_tens.name], attr) |
| 1358 | return result_tens |
| 1359 | |
| 1360 | def build_mul(self, op, a, b, shift): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1361 | result_tens = OutputShaper.binaryBroadcastOp(self.ser, a, b) |
| 1362 | |
| 1363 | # Special for multiply: |
| 1364 | # Force the result to INT32 for INT types |
| 1365 | if a.dtype != DType.FLOAT: |
| 1366 | result_tens.setDtype(DType.INT32) |
| 1367 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 1368 | attr = ts.TosaSerializerAttribute() |
| 1369 | attr.MulAttribute(shift) |
| 1370 | |
| 1371 | self.ser.addOperator(op, [a.name, b.name], [result_tens.name], attr) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1372 | return result_tens |
| 1373 | |
| 1374 | def build_table(self, op, a): |
Jeremy Johnson | f54d8a2 | 2021-07-20 16:01:06 +0100 | [diff] [blame] | 1375 | # Constant size depending on type, random values |
| 1376 | if a.dtype == DType.INT16: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1377 | table_dtype = DType.INT16 |
Jeremy Johnson | f54d8a2 | 2021-07-20 16:01:06 +0100 | [diff] [blame] | 1378 | table_arr = self.getRandTensor([513], table_dtype) |
| 1379 | else: |
| 1380 | assert a.dtype == DType.INT8 |
| 1381 | table_dtype = DType.INT8 |
| 1382 | table_arr = self.getRandTensor([256], table_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1383 | |
Jeremy Johnson | f54d8a2 | 2021-07-20 16:01:06 +0100 | [diff] [blame] | 1384 | table_tens = self.ser.addConst(table_arr.shape, table_dtype, table_arr) |
| 1385 | result_tens = OutputShaper.tableOp(self.ser, a, table_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1386 | self.ser.addOperator(op, [a.name, table_tens.name], [result_tens.name], None) |
| 1387 | |
| 1388 | return result_tens |
| 1389 | |
| 1390 | def build_select(self, op, cond, a, b): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1391 | result_tens = OutputShaper.selectOp(self.ser, cond, a, b) |
| 1392 | self.ser.addOperator(op, [cond.name, a.name, b.name], [result_tens.name]) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1393 | return result_tens |
| 1394 | |
| 1395 | def build_comparison(self, op, a, b): |
| 1396 | result_tens = OutputShaper.binaryComparisonOp(self.ser, a, b) |
| 1397 | self.ser.addOperator(op, [a.name, b.name], [result_tens.name]) |
| 1398 | return result_tens |
| 1399 | |
| 1400 | def build_argmax(self, op, a, axis): |
| 1401 | result_tens = OutputShaper.argmaxOp(self.ser, a, axis) |
| 1402 | |
| 1403 | attr = ts.TosaSerializerAttribute() |
| 1404 | attr.AxisAttribute(axis) |
| 1405 | |
| 1406 | self.ser.addOperator(op, [a.name], [result_tens.name], attr) |
| 1407 | return result_tens |
| 1408 | |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 1409 | def build_pool2d(self, op, input, stride, pad, kernel, qinfo=None): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1410 | result_tens = OutputShaper.pool2dOp(self.ser, input, kernel, stride, pad) |
| 1411 | |
| 1412 | attr = ts.TosaSerializerAttribute() |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1413 | attr.PoolAttribute(kernel, stride, pad) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1414 | |
| 1415 | self.ser.addOperator(op, [input.name], [result_tens.name], attr, qinfo) |
| 1416 | return result_tens |
| 1417 | |
| 1418 | def build_conv2d(self, op, ifm, filter, bias, strides, padding, dilations, qinfo): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1419 | assert len(padding) == 4 |
| 1420 | result_tens = OutputShaper.conv2dOp( |
| 1421 | self.ser, ifm, filter, strides, padding, dilations |
| 1422 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1423 | |
| 1424 | attr = ts.TosaSerializerAttribute() |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1425 | attr.ConvAttribute(padding, strides, dilations) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1426 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1427 | self.ser.addOperator( |
| 1428 | op, [ifm.name, filter.name, bias.name], [result_tens.name], attr, qinfo |
| 1429 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1430 | return result_tens |
| 1431 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 1432 | def build_conv3d(self, op, ifm, filter, bias, strides, padding, dilations, qinfo): |
| 1433 | assert len(padding) == 6 |
| 1434 | result_tens = OutputShaper.conv3dOp( |
| 1435 | self.ser, ifm, filter, strides, padding, dilations |
| 1436 | ) |
| 1437 | |
| 1438 | attr = ts.TosaSerializerAttribute() |
| 1439 | attr.ConvAttribute(padding, strides, dilations) |
| 1440 | |
| 1441 | self.ser.addOperator( |
| 1442 | op, [ifm.name, filter.name, bias.name], [result_tens.name], attr, qinfo |
| 1443 | ) |
| 1444 | return result_tens |
| 1445 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1446 | def build_transpose_conv2d( |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1447 | self, op, ifm, filter, bias, stride, outpad, dilation, output_shape, qinfo |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1448 | ): |
| 1449 | assert len(outpad) == 2 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1450 | result_tens = OutputShaper.transposeConv2DOp(self.ser, ifm, output_shape) |
| 1451 | |
| 1452 | attr = ts.TosaSerializerAttribute() |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1453 | attr.TransposeConvAttribute(outpad, stride, dilation, output_shape) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1454 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1455 | self.ser.addOperator( |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1456 | op, [ifm.name, filter.name, bias.name], [result_tens.name], attr, qinfo |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1457 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1458 | return result_tens |
| 1459 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1460 | def build_depthwise_conv2d( |
| 1461 | self, op, ifm, filter, bias, strides, padding, dilations, qinfo |
| 1462 | ): |
| 1463 | result_tens = OutputShaper.depthwiseConv2dOp( |
| 1464 | self.ser, ifm, filter, strides, padding, dilations |
| 1465 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1466 | |
| 1467 | attr = ts.TosaSerializerAttribute() |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1468 | attr.ConvAttribute(padding, strides, dilations) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1469 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1470 | self.ser.addOperator( |
| 1471 | op, [ifm.name, filter.name, bias.name], [result_tens.name], attr, qinfo |
| 1472 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1473 | return result_tens |
| 1474 | |
| 1475 | def build_fully_connected(self, op, ifm, filter, bias, qinfo): |
| 1476 | result_tens = OutputShaper.fullyConnectedOp(self.ser, ifm, filter) |
| 1477 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1478 | self.ser.addOperator( |
| 1479 | op, [ifm.name, filter.name, bias.name], [result_tens.name], None, qinfo |
| 1480 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1481 | return result_tens |
| 1482 | |
| 1483 | def build_matmul(self, op, a, b, qinfo): |
| 1484 | result_tens = OutputShaper.matmulOp(self.ser, a, b) |
| 1485 | self.ser.addOperator(op, [a.name, b.name], [result_tens.name], None, qinfo) |
| 1486 | return result_tens |
| 1487 | |
| 1488 | def build_reduce(self, op, a, axis): |
| 1489 | result_tens = OutputShaper.reduceOp(self.ser, a, axis) |
| 1490 | |
| 1491 | attr = ts.TosaSerializerAttribute() |
| 1492 | attr.AxisAttribute(axis) |
| 1493 | |
| 1494 | self.ser.addOperator(op, [a.name], result_tens.name, attr) |
| 1495 | return result_tens |
| 1496 | |
| 1497 | def build_clamp(self, op, a): |
| 1498 | result_tens = OutputShaper.unaryOp(self.ser, a) |
| 1499 | |
| 1500 | attr = ts.TosaSerializerAttribute() |
Jeremy Johnson | 18e2666 | 2021-07-22 16:15:29 +0100 | [diff] [blame] | 1501 | v = [self.getRandNumberDType(a.dtype), self.getRandNumberDType(a.dtype)] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1502 | |
| 1503 | if a.dtype == DType.FLOAT: |
| 1504 | attr.ClampAttribute(0, 0, min(v), max(v)) |
| 1505 | else: |
| 1506 | attr.ClampAttribute(min(v), max(v), 0, 0) |
| 1507 | |
| 1508 | self.ser.addOperator(op, [a.name], [result_tens.name], attr) |
| 1509 | return result_tens |
| 1510 | |
| 1511 | def build_leaky_relu(self, op, a): |
| 1512 | result_tens = OutputShaper.unaryOp(self.ser, a) |
| 1513 | attr = ts.TosaSerializerAttribute() |
| 1514 | |
| 1515 | attr.LeakyReluAttribute(self.getRandNumberDType(DType.FLOAT)) |
| 1516 | |
| 1517 | self.ser.addOperator(op, [a.name], [result_tens.name], attr) |
| 1518 | return result_tens |
| 1519 | |
| 1520 | # Needs an additional type/input |
| 1521 | def build_prelu(self, op, a): |
| 1522 | result_tens = OutputShaper.unaryOp(self.ser, a) |
| 1523 | |
| 1524 | self.ser.addOperator(op, [a.name], [result_tens.name]) |
| 1525 | return result_tens |
| 1526 | |
| 1527 | def build_relun(self, op, a): |
| 1528 | result_tens = OutputShaper.unaryOp(self.ser, a) |
| 1529 | |
| 1530 | attr = ts.TosaSerializerAttribute() |
| 1531 | |
| 1532 | if a.dtype == DType.FLOAT: |
| 1533 | attr.ReluNAttribute(0, self.getRandNumberDType(a.dtype)) |
| 1534 | else: |
| 1535 | attr.ReluNAttribute(self.getRandNumberDType(a.dtype), 0) |
| 1536 | |
| 1537 | self.ser.addOperator(op, [a.name], [result_tens.name], attr) |
| 1538 | return result_tens |
| 1539 | |
| 1540 | def build_sigmoid(self, op, a): |
| 1541 | result_tens = OutputShaper.unaryOp(self.ser, a) |
| 1542 | self.ser.addOperator(op, [a.name], [result_tens.name]) |
| 1543 | return result_tens |
| 1544 | |
| 1545 | def build_tanh(self, op, a): |
| 1546 | result_tens = OutputShaper.unaryOp(self.ser, a) |
| 1547 | self.ser.addOperator(op, [a.name], [result_tens.name]) |
| 1548 | return result_tens |
| 1549 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 1550 | def build_concat(self, op, *a): |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1551 | assert type(a[-1]) == int |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 1552 | |
| 1553 | # To store variable length list of input tensors we need to store axis along with it |
| 1554 | axis = a[-1] |
| 1555 | a = a[:-1] |
| 1556 | |
| 1557 | result_tens = OutputShaper.concatOp(self.ser, axis, *a) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1558 | |
| 1559 | attr = ts.TosaSerializerAttribute() |
| 1560 | attr.AxisAttribute(axis) |
| 1561 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 1562 | input_tensor_names = [] |
| 1563 | for tensor in a: |
| 1564 | input_tensor_names.append(tensor.name) |
| 1565 | |
| 1566 | self.ser.addOperator(op, input_tensor_names, [result_tens.name], attr) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1567 | |
| 1568 | def build_pad(self, op, a, padding, qinfo): |
| 1569 | result_tens = OutputShaper.padOp(self.ser, a, padding) |
| 1570 | |
| 1571 | # Need to turn the padding array into a TOSA tensor here. |
| 1572 | # This is one of the few tensor operands that does not get |
| 1573 | # randomly generated |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1574 | padding_tens = self.ser.addConst(padding.shape, DType.INT32, padding) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1575 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1576 | self.ser.addOperator( |
| 1577 | op, [a.name, padding_tens.name], [result_tens.name], None, qinfo |
| 1578 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1579 | |
| 1580 | def build_reshape(self, op, a, newShape): |
| 1581 | result_tens = OutputShaper.reshapeOp(self.ser, a, newShape) |
| 1582 | |
| 1583 | attr = ts.TosaSerializerAttribute() |
| 1584 | attr.ReshapeAttribute(newShape) |
| 1585 | |
| 1586 | self.ser.addOperator(op, [a.name], [result_tens.name], attr) |
| 1587 | return result_tens |
| 1588 | |
| 1589 | def build_reverse(self, op, a, axis): |
| 1590 | result_tens = OutputShaper.unaryOp(self.ser, a) |
| 1591 | |
| 1592 | attr = ts.TosaSerializerAttribute() |
| 1593 | attr.AxisAttribute(axis) |
| 1594 | |
| 1595 | self.ser.addOperator(op, [a.name], [result_tens.name], attr) |
| 1596 | return result_tens |
| 1597 | |
| 1598 | def build_transpose(self, op, a, perms): |
| 1599 | result_tens = OutputShaper.transposeOp(self.ser, a, perms) |
| 1600 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1601 | perms_tens = self.ser.addConst([len(perms)], DType.INT32, np.int32(perms)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1602 | |
| 1603 | self.ser.addOperator(op, [a.name, perms_tens.name], [result_tens.name]) |
| 1604 | return result_tens |
| 1605 | |
| 1606 | def build_slice(self, op, a, begin, size): |
| 1607 | result_tens = OutputShaper.sliceOp(self.ser, a, begin, size) |
| 1608 | |
| 1609 | attr = ts.TosaSerializerAttribute() |
| 1610 | attr.SliceAttribute(begin, size) |
| 1611 | |
| 1612 | self.ser.addOperator(op, [a.name], [result_tens.name], attr) |
| 1613 | return result_tens |
| 1614 | |
| 1615 | def build_tile(self, op, a, multiples): |
| 1616 | result_tens = OutputShaper.tileOp(self.ser, a, multiples) |
| 1617 | |
| 1618 | attr = ts.TosaSerializerAttribute() |
| 1619 | attr.TileAttribute(multiples) |
| 1620 | |
| 1621 | self.ser.addOperator(op, [a.name], [result_tens.name], attr) |
| 1622 | return result_tens |
| 1623 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 1624 | def build_gather(self, op, values): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1625 | |
| 1626 | # Create a new indicies tensor |
| 1627 | # here with data that doesn't exceed the dimensions of the values tensor |
| 1628 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1629 | K = values.shape[1] # K |
| 1630 | W = self.randInt( |
| 1631 | self.args.tensor_shape_range[0], self.args.tensor_shape_range[1] |
| 1632 | ) # W |
| 1633 | indicies_arr = np.int32( |
| 1634 | self.rng.integers(low=0, high=K, size=[values.shape[0], W]) |
| 1635 | ) # (N, W) |
| 1636 | indicies = self.ser.addConst(indicies_arr.shape, DType.INT32, indicies_arr) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1637 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 1638 | result_tens = OutputShaper.gatherOp(self.ser, values, indicies) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1639 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 1640 | self.ser.addOperator(op, [values.name, indicies.name], [result_tens.name]) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1641 | |
| 1642 | return result_tens |
| 1643 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 1644 | def build_scatter(self, op, values_in, input): |
| 1645 | |
| 1646 | # Create a new indicies tensor |
| 1647 | # here with data that doesn't exceed the dimensions of the values_in tensor |
| 1648 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1649 | K = values_in.shape[1] # K |
| 1650 | W = input.shape[1] # W |
| 1651 | indicies_arr = np.int32( |
| 1652 | self.rng.integers(low=0, high=K, size=[values_in.shape[0], W]) |
| 1653 | ) # (N, W) |
| 1654 | indicies = self.ser.addConst(indicies_arr.shape, DType.INT32, indicies_arr) |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 1655 | |
| 1656 | result_tens = OutputShaper.scatterOp(self.ser, values_in, indicies, input) |
| 1657 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1658 | self.ser.addOperator( |
| 1659 | op, [values_in.name, indicies.name, input.name], [result_tens.name] |
| 1660 | ) |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 1661 | |
| 1662 | return result_tens |
| 1663 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1664 | def build_resize( |
| 1665 | self, |
| 1666 | op, |
| 1667 | input, |
| 1668 | mode, |
| 1669 | stride, |
| 1670 | offset, |
| 1671 | shift, |
| 1672 | stride_fp, |
| 1673 | offset_fp, |
| 1674 | output_dims, |
| 1675 | input_dtype, |
| 1676 | output_dtype, |
| 1677 | ): |
| 1678 | result_tens = OutputShaper.resizeOp( |
| 1679 | self.ser, |
| 1680 | input, |
| 1681 | mode, |
| 1682 | stride, |
| 1683 | offset, |
| 1684 | shift, |
| 1685 | stride_fp, |
| 1686 | offset_fp, |
| 1687 | output_dims, |
| 1688 | input_dtype, |
| 1689 | output_dtype, |
| 1690 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1691 | |
| 1692 | attr = ts.TosaSerializerAttribute() |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 1693 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1694 | attr.ResizeAttribute( |
| 1695 | output_dims, stride, offset, shift, stride_fp, offset_fp, mode |
| 1696 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1697 | |
| 1698 | self.ser.addOperator(op, [input.name], [result_tens.name], attr) |
| 1699 | return result_tens |
| 1700 | |
| 1701 | def build_identityn(self, op, val, val2): |
| 1702 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1703 | result_tens = OutputShaper.unaryOp(self.ser, val) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1704 | result_tens2 = OutputShaper.unaryOp(self.ser, val2) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1705 | self.ser.addOperator( |
| 1706 | op, [val.name, val2.name], [result_tens.name, result_tens2.name] |
| 1707 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1708 | return result_tens |
| 1709 | |
| 1710 | def build_placeholder(self, op, val): |
| 1711 | # Add an identity op to avoid warning in the reference model |
| 1712 | return self.build_unary(Op.IDENTITY, val) |
| 1713 | |
| 1714 | # Type Conversion |
| 1715 | def build_cast(self, op, val, out_dtype): |
| 1716 | result_tens = OutputShaper.typeConversionOp(self.ser, val, out_dtype) |
| 1717 | self.ser.addOperator(op, [val.name], [result_tens.name]) |
| 1718 | return result_tens |
| 1719 | |
| 1720 | def build_rescale(self, op, val, out_dtype, scale32, double_round, per_channel): |
| 1721 | result_tens = OutputShaper.typeConversionOp(self.ser, val, out_dtype) |
| 1722 | |
| 1723 | if per_channel: |
| 1724 | nc = val.shape[-1] |
| 1725 | else: |
| 1726 | nc = 1 |
| 1727 | |
| 1728 | in_type_width = self.typeWidth(val.dtype) |
| 1729 | out_type_width = self.typeWidth(out_dtype) |
| 1730 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 1731 | if val.dtype == DType.INT8: |
Matthew Haddon | cac4ee9 | 2021-07-22 14:30:53 +0100 | [diff] [blame] | 1732 | input_zp = self.randInt(-128, 128) |
| 1733 | in_type_width = in_type_width + 1 |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1734 | elif val.dtype == DType.UINT8: |
Matthew Haddon | cac4ee9 | 2021-07-22 14:30:53 +0100 | [diff] [blame] | 1735 | input_zp = self.randInt(0, 256) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1736 | in_type_width = in_type_width + 1 |
| 1737 | else: |
| 1738 | input_zp = 0 |
| 1739 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 1740 | if out_dtype == DType.INT8: |
Matthew Haddon | cac4ee9 | 2021-07-22 14:30:53 +0100 | [diff] [blame] | 1741 | output_zp = self.randInt(-128, 128) |
| 1742 | out_type_width = out_type_width + 1 |
| 1743 | elif out_dtype == DType.UINT8: |
| 1744 | output_zp = self.randInt(0, 256) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1745 | out_type_width = out_type_width + 1 |
| 1746 | else: |
| 1747 | output_zp = 0 |
| 1748 | |
| 1749 | # Calculate scale based on: |
| 1750 | # scale = a *(2^output_width)/(2^input_width)) |
| 1751 | |
| 1752 | a = np.float32(self.rng.random(size=[nc])) |
| 1753 | scale_arr = a * np.float32((1 << out_type_width) / (1 << in_type_width)) |
| 1754 | |
| 1755 | if scale32: |
| 1756 | pass |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 1757 | # Cap the scaling at 2^31 - 1 for scale32 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1758 | scale_arr = np.clip(scale_arr, 1.0 / (1 << 31), (1 << 31) - 1) |
| 1759 | else: |
| 1760 | # Cap the scaling at 2^15 - 1 for scale16 |
| 1761 | scale_arr = np.clip(scale_arr, 1.0 / (1 << 31), 32767.0) |
| 1762 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1763 | # print('{} {} -> {}'.format(out_type_width, in_type_width, scale_arr)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1764 | |
| 1765 | multiplier_arr = np.int32(np.zeros(shape=[nc])) |
| 1766 | shift_arr = np.int32(np.zeros(shape=[nc])) |
| 1767 | |
| 1768 | for i in range(nc): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1769 | multiplier_arr[i], shift_arr[i] = TosaQuantGen.computeMultiplierAndShift( |
| 1770 | scale_arr[i], scale32 |
| 1771 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1772 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1773 | # print('multiplier {} shift {} inzp {} outzp {}'.format(multiplier_arr, shift_arr, input_zp, output_zp)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1774 | |
| 1775 | attr = ts.TosaSerializerAttribute() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1776 | attr.RescaleAttribute( |
| 1777 | input_zp, |
| 1778 | output_zp, |
| 1779 | multiplier_arr, |
| 1780 | shift_arr, |
| 1781 | scale32, |
| 1782 | double_round, |
| 1783 | per_channel, |
| 1784 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1785 | |
| 1786 | self.ser.addOperator(op, [val.name], [result_tens.name], attr) |
| 1787 | return result_tens |
| 1788 | |
| 1789 | def build_cond_if_const(self, op, then_tens, else_tens, cond): |
| 1790 | # For cond_if with constants, we're supplied with then/else tensors that we ignore |
| 1791 | # (except for the generated shap) and the condition. Build Then/Else blocks |
| 1792 | # and fill them with const nodes for the body. |
| 1793 | |
| 1794 | # Condition tensor |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1795 | cond_tens = self.ser.addConst([], DType.BOOL, [cond]) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1796 | |
| 1797 | # Make then/else tensors |
| 1798 | out_shape = then_tens.shape |
Jeremy Johnson | 18e2666 | 2021-07-22 16:15:29 +0100 | [diff] [blame] | 1799 | then_arr = np.int32(self.rng.integers(0, 256, size=out_shape)) |
| 1800 | else_arr = np.int32(self.rng.integers(0, 256, size=out_shape)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1801 | |
| 1802 | # And the result tensor based on any of the outputs |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1803 | result_tens = self.ser.addOutput(out_shape, DType.INT32) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1804 | |
| 1805 | # Create the attribute with the names of the then/else blocks |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1806 | then_block = "THEN_BLOCK" |
| 1807 | else_block = "ELSE_BLOCK" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1808 | attr = ts.TosaSerializerAttribute() |
| 1809 | attr.CondIfAttribute(then_block, else_block) |
| 1810 | |
| 1811 | # Finally, build the op and the two blocks |
| 1812 | self.ser.addOperator(op, [cond_tens.name], [result_tens.name], attr) |
| 1813 | |
| 1814 | self.ser.startBasicBlock(then_block) |
| 1815 | # Build the actual then/else tensors inside their blocks |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1816 | then_tens = self.ser.addConst(out_shape, DType.INT32, then_arr) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1817 | self.ser.addOutputTensor(then_tens) |
| 1818 | |
| 1819 | self.ser.startBasicBlock(else_block) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1820 | else_tens = self.ser.addConst(out_shape, DType.INT32, else_arr) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1821 | self.ser.addOutputTensor(else_tens) |
| 1822 | |
| 1823 | return result_tens |
| 1824 | |
| 1825 | def build_cond_if_binary(self, op, a, b, cond): |
| 1826 | # For cond_if with a binary op in the then/else blocks, take a and b and |
| 1827 | # alternately add or subtract them based on the condition |
| 1828 | |
| 1829 | # Condition tensor |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1830 | cond_tens = self.ser.addConst([], DType.BOOL, [cond]) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1831 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1832 | result_tens = self.ser.addOutput(a.shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1833 | self.ser.currBasicBlock.addOutput(result_tens.name) |
| 1834 | |
| 1835 | # Create the attribute with the names of the then/else blocks |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1836 | then_block = "THEN_BLOCK" |
| 1837 | else_block = "ELSE_BLOCK" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1838 | attr = ts.TosaSerializerAttribute() |
| 1839 | attr.CondIfAttribute(then_block, else_block) |
| 1840 | |
| 1841 | # Finally, build the op and the two blocks |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1842 | self.ser.addOperator( |
| 1843 | op, [cond_tens.name, a.name, b.name], [result_tens.name], attr |
| 1844 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1845 | |
| 1846 | self.ser.startBasicBlock(then_block) |
| 1847 | self.ser.addInputTensor(a) |
| 1848 | self.ser.addInputTensor(b) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1849 | then_tens = self.ser.addOutput(a.shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1850 | self.ser.addOperator(Op.ADD, [a.name, b.name], [then_tens.name]) |
| 1851 | |
| 1852 | self.ser.startBasicBlock(else_block) |
| 1853 | self.ser.addInputTensor(a) |
| 1854 | self.ser.addInputTensor(b) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1855 | else_tens = self.ser.addOutput(a.shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1856 | self.ser.addOperator(Op.SUB, [a.name, b.name], [else_tens.name]) |
| 1857 | |
| 1858 | return result_tens |
| 1859 | |
| 1860 | def build_while_loop(self, op, a, iter_val): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1861 | iter = self.ser.addPlaceholder([], DType.INT32, [np.int32(iter_val)]) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1862 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1863 | cond_block = "COND_BLOCK" |
| 1864 | body_block = "BODY_BLOCK" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1865 | |
| 1866 | attr = ts.TosaSerializerAttribute() |
| 1867 | attr.WhileLoopAttribute(cond_block, body_block) |
| 1868 | |
| 1869 | # Accumulator tensor |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1870 | # acc = self.ser.addOutput(a.shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1871 | acc_init_val = np.int32(np.zeros(a.shape)) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1872 | acc = self.ser.addPlaceholder(a.shape, a.dtype, acc_init_val) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1873 | |
| 1874 | # Intermediate/output tensors for everything going through the loop |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1875 | iter_out = self.ser.addIntermediate(iter.shape, iter.dtype) |
| 1876 | a_out = self.ser.addIntermediate(a.shape, a.dtype) |
| 1877 | acc_out = self.ser.addIntermediate(acc.shape, acc.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1878 | |
| 1879 | # While_loop operator |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1880 | self.ser.addOperator( |
| 1881 | op, |
| 1882 | [iter.name, a.name, acc.name], |
| 1883 | [iter_out.name, a_out.name, acc_out.name], |
| 1884 | attr, |
| 1885 | ) |
Kevin Cheng | b227ae5 | 2021-09-02 13:43:17 -0700 | [diff] [blame] | 1886 | self.ser.addOutputTensor(acc_out) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1887 | |
| 1888 | # COND block (input: iter, output: cond_tens ) |
| 1889 | self.ser.startBasicBlock(cond_block) |
| 1890 | self.ser.addInputTensor(iter) |
| 1891 | self.ser.addInputTensor(a) |
| 1892 | self.ser.addInputTensor(acc) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1893 | zero_tens = self.ser.addConst([], DType.INT32, [np.int32(0)]) |
| 1894 | cond_tens = self.ser.addOutput([], DType.BOOL) |
| 1895 | self.ser.addOperator(Op.GREATER, [iter.name, zero_tens.name], [cond_tens.name]) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1896 | |
| 1897 | # BODY block (input: a, acc, iter, output: a, acc, iter) |
| 1898 | # Note that local intermediate tensors need to be declared here for the outputs |
| 1899 | self.ser.startBasicBlock(body_block) |
| 1900 | self.ser.addInputTensor(iter) |
| 1901 | self.ser.addInputTensor(a) |
| 1902 | self.ser.addInputTensor(acc) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1903 | one_tens = self.ser.addConst([], DType.INT32, [np.int32(1)]) |
| 1904 | iter_body_out = self.ser.addIntermediate(iter.shape, iter.dtype) |
| 1905 | acc_body_out = self.ser.addIntermediate(acc.shape, acc.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1906 | self.ser.addOperator(Op.ADD, [a.name, acc.name], [acc_body_out.name]) |
| 1907 | self.ser.addOperator(Op.SUB, [iter.name, one_tens.name], [iter_body_out.name]) |
| 1908 | self.ser.addOutputTensor(iter_body_out) |
| 1909 | self.ser.addOutputTensor(a) |
| 1910 | self.ser.addOutputTensor(acc_body_out) |
| 1911 | |
| 1912 | return acc_out |
| 1913 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1914 | def genOpTestList( |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1915 | self, opName, shapeFilter=[None], rankFilter=None, dtypeFilter=None, testType='positive' |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1916 | ): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1917 | |
| 1918 | try: |
| 1919 | op = self.TOSA_OP_LIST[opName] |
| 1920 | except KeyError as e: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1921 | raise Exception("Cannot find op with name {}".format(opName)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1922 | |
| 1923 | # Initialize a new random number generator |
| 1924 | self.rng = np.random.default_rng(self.random_seed) |
| 1925 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1926 | build_fcn, tgen_fcn, agen_fcn = op["build_fcn"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1927 | |
| 1928 | # Generate the lists of arguments |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 1929 | rmin, rmax = op["rank"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1930 | |
Jeremy Johnson | 97eb75f | 2021-07-08 11:58:02 +0100 | [diff] [blame] | 1931 | # Create a default testing rank range, 1-4 inclusive to keep test sizes reasonably small. |
| 1932 | default_test_rank_range = range(1, 5) |
| 1933 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1934 | # Test list consists of a tuple of: |
| 1935 | # (opName, testNameStr, dtype, shapeList, argumentsList) |
| 1936 | testList = [] |
| 1937 | |
| 1938 | if not shapeFilter: |
| 1939 | shapeFilter = [None] |
| 1940 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1941 | # Positive test loop |
| 1942 | if testType in ['positive', 'both']: |
| 1943 | for r in range(rmin, rmax + 1): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1944 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1945 | # Filter out the rank? |
| 1946 | if rankFilter is not None and r not in rankFilter: |
| 1947 | continue |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 1948 | if opName.startswith("conv3d"): |
| 1949 | assert r == 5, "conv3d test must have input rank == 5" |
| 1950 | elif ( |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1951 | rankFilter is None |
| 1952 | and shapeFilter[0] is None |
| 1953 | and r not in default_test_rank_range |
| 1954 | ): |
| 1955 | continue |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1956 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1957 | for t in op["types"]: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1958 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1959 | # Filter tests based on dtype? |
| 1960 | if dtypeFilter is not None: |
| 1961 | if not ( |
| 1962 | t in dtypeFilter |
| 1963 | or (isinstance(t, list) and t[0] in dtypeFilter) |
| 1964 | ): |
| 1965 | continue |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1966 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1967 | # Create the placeholder and const tensors |
| 1968 | for shape in shapeFilter: |
| 1969 | # A None shape chooses a random shape of a given rank |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1970 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1971 | # Filter out by rank |
| 1972 | if shape is not None and len(shape) != r: |
| 1973 | continue |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1974 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1975 | self.setTargetShape(shape) |
| 1976 | shapeList = tgen_fcn(self, op, r) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1977 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1978 | shapeStr = self.shapeStr(shapeList[0]) |
| 1979 | typeStr = self.typeStr(t) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1980 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1981 | # Argument lists consists of tuples of the (str, []) string representation and the build function argument list |
| 1982 | argList = [] |
| 1983 | if agen_fcn: |
| 1984 | argList = agen_fcn(self, opName, shapeList, t) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1985 | else: |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1986 | argList = [("", [])] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1987 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 1988 | for argStr, args in argList: |
| 1989 | if argStr: |
| 1990 | testStr = "{}_{}_{}_{}".format( |
| 1991 | opName, shapeStr, typeStr, argStr |
| 1992 | ) |
| 1993 | else: |
| 1994 | testStr = "{}_{}_{}".format(opName, shapeStr, typeStr) |
| 1995 | |
| 1996 | testList.append((opName, testStr, t, shapeList, args)) |
| 1997 | |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 1998 | # Remove tests which are expected to fail but don't correlate to a ERROR_IF statement |
| 1999 | if "invalid_test_validators" in op: |
| 2000 | invalid_test_validators = op["invalid_test_validators"] |
| 2001 | clean_testList = [] |
| 2002 | for test in testList: |
| 2003 | for validator_fcn in invalid_test_validators: |
| 2004 | remove_test = False |
| 2005 | if validator_fcn(opName=test[0], input_dtype=test[2], shapeList=test[3], args=test[4]): |
| 2006 | remove_test = True |
| 2007 | if not remove_test: |
| 2008 | clean_testList.append(test) |
| 2009 | testList = clean_testList |
| 2010 | |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 2011 | # Reset RNG so both positive and negative tests are reproducible |
| 2012 | self.resetRNG() |
| 2013 | # Negative test loop |
| 2014 | if testType in ['negative', 'both']: |
| 2015 | print("Negative tests unsupported") |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2016 | |
| 2017 | return testList |
| 2018 | |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2019 | def serializeTest(self, opName, testStr, dtype_or_dtypeList, shapeList, testArgs): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2020 | try: |
| 2021 | op = self.TOSA_OP_LIST[opName] |
| 2022 | except KeyError as e: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2023 | raise Exception("Cannot find op with name {}".format(opName)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2024 | |
| 2025 | # Create a serializer |
| 2026 | self.createSerializer(opName, testStr) |
| 2027 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2028 | build_fcn, tgen_fcn, agen_fcn = op["build_fcn"] |
| 2029 | pCount, cCount = op["operands"] |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2030 | num_operands = pCount + cCount |
| 2031 | |
| 2032 | if isinstance(dtype_or_dtypeList, list): |
| 2033 | dtypeList = dtype_or_dtypeList |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 2034 | elif op["op"] == Op.CONCAT: |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 2035 | dtypeList = [dtype_or_dtypeList] * len(shapeList) |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2036 | else: |
| 2037 | dtypeList = [dtype_or_dtypeList] * (num_operands) |
| 2038 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 2039 | if op["op"] != Op.CONCAT: |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 2040 | assert ( |
| 2041 | len(shapeList) == num_operands |
| 2042 | ), "shapeList length {} must match number of operands {}".format( |
| 2043 | len(shapeList), num_operands |
| 2044 | ) |
| 2045 | assert ( |
| 2046 | len(dtypeList) == num_operands |
| 2047 | ), "dtypeList length {} must match number of operands {}".format( |
| 2048 | len(dtypeList), num_operands |
| 2049 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2050 | |
| 2051 | try: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2052 | qgen = op["qgen"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2053 | except KeyError: |
| 2054 | qgen = None |
| 2055 | |
| 2056 | # Build the random tensor operands and the test |
| 2057 | tens = [] |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 2058 | |
Jeremy Johnson | ef509a4 | 2021-09-07 13:59:47 +0100 | [diff] [blame] | 2059 | if (op["op"] == Op.ADD or op["op"] == Op.SUB) and dtypeList[0] == DType.INT32: |
| 2060 | # Make sure the operation does not cause value saturation - where |
| 2061 | # the number wraps due to limited number of bits to store the answer |
| 2062 | assert ( |
| 2063 | pCount == 2 and cCount == 0 |
| 2064 | ), "Op.ADD / Op.SUB must have 2 placeholders, 0 consts" |
| 2065 | |
| 2066 | placeholders = [] |
| 2067 | add = (op["op"] == Op.ADD) |
| 2068 | a_arr = self.getRandTensor(shapeList[0], dtypeList[0]) |
| 2069 | b_arr = self.getRandTensor(shapeList[1], dtypeList[1]) |
| 2070 | if add: |
| 2071 | res_arr = np.add(a_arr, b_arr, dtype=np.int64) |
| 2072 | else: |
| 2073 | res_arr = np.subtract(a_arr, b_arr, dtype=np.int64) |
| 2074 | |
| 2075 | # Work out the saturation limits |
| 2076 | max_i32 = (1 << 31)-1 |
| 2077 | min_i32 = -(1 << 31) |
| 2078 | max_arr = np.full(shapeList[1], max_i32) |
| 2079 | min_arr = np.full(shapeList[1], min_i32) |
| 2080 | |
| 2081 | # Find how much values exceed the maximum/minimums |
| 2082 | sat_max_arr = np.maximum(res_arr - max_arr, 0) |
| 2083 | sat_min_arr = np.minimum(res_arr - min_arr, 0) |
| 2084 | |
| 2085 | if not add: |
| 2086 | # Swap saturation values and negate values as we need to perform opposite operations |
| 2087 | sat_max_arr, sat_min_arr = -sat_min_arr, -sat_max_arr |
| 2088 | |
| 2089 | # Create new array of unsaturated values by clipping values as needed |
| 2090 | b_unsat_arr = b_arr |
| 2091 | if (sat_max_arr != 0).any(): |
| 2092 | # Clip values that cause saturation |
| 2093 | b_unsat_arr = np.subtract(b_unsat_arr, sat_max_arr, dtype=np.int32) |
| 2094 | # Reduce axes in unsaturated tensor to match original tensor |
| 2095 | for axis, dim in enumerate(b_arr.shape): |
| 2096 | if dim != b_unsat_arr.shape[axis]: |
| 2097 | assert ( dim == 1 ), "Op.ADD / SUB dimension must be 1 or matching to be broadcastable" |
| 2098 | b_unsat_arr = np.amin(b_unsat_arr, axis=axis, keepdims=True) |
| 2099 | |
| 2100 | if (sat_min_arr != 0).any(): |
| 2101 | # Clip values that cause saturation |
| 2102 | b_unsat_arr = np.subtract(b_unsat_arr, sat_min_arr, dtype=np.int32) |
| 2103 | # Reduce axes in unsaturated tensor to match original tensor |
| 2104 | for axis, dim in enumerate(b_arr.shape): |
| 2105 | if dim != b_unsat_arr.shape[axis]: |
| 2106 | assert ( dim == 1 ), "Op.ADD / SUB dimension must be 1 or matching to be broadcastable" |
| 2107 | b_unsat_arr = np.amax(b_unsat_arr, axis=axis, keepdims=True) |
| 2108 | |
| 2109 | placeholders.append( |
| 2110 | self.ser.addPlaceholder(shapeList[0], dtypeList[0], a_arr) |
| 2111 | ) |
| 2112 | placeholders.append( |
| 2113 | self.ser.addPlaceholder(shapeList[1], dtypeList[1], b_unsat_arr) |
| 2114 | ) |
| 2115 | |
| 2116 | tens.extend(placeholders) |
| 2117 | elif op["op"] == Op.ARITHMETIC_RIGHT_SHIFT: |
| 2118 | # Force value of operand[1] to be within [0, num_bits] |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2119 | assert ( |
| 2120 | pCount == 2 and cCount == 0 |
| 2121 | ), "Op.ArithmeticRightShift must have 2 placeholders, 0 consts" |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 2122 | |
| 2123 | placeholders = [] |
| 2124 | for idx, shape in enumerate(shapeList[:]): |
| 2125 | if idx == 1: |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2126 | if dtypeList[idx] == DType.INT8: |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 2127 | arr = np.int32(self.rng.integers(low=0, high=8, size=shape)) |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2128 | elif dtypeList[idx] == DType.INT16: |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 2129 | arr = np.int32(self.rng.integers(low=0, high=16, size=shape)) |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2130 | elif dtypeList[idx] == DType.INT32: |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 2131 | arr = np.int32(self.rng.integers(low=0, high=32, size=shape)) |
| 2132 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2133 | raise Exception("OpArithmeticRightShift: invalid input dtype") |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 2134 | else: |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 2135 | arr = self.getRandTensor(shape, dtypeList[idx]) |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2136 | placeholders.append(self.ser.addPlaceholder(shape, dtypeList[idx], arr)) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 2137 | |
| 2138 | tens.extend(placeholders) |
Matthew Haddon | a44ac5e | 2021-07-27 16:31:16 +0100 | [diff] [blame] | 2139 | elif op["op"] == Op.SELECT: |
| 2140 | # Set datatype of condition tensor to boolean |
| 2141 | dtypeList[0] = DType.BOOL |
| 2142 | tens.extend( |
| 2143 | self.buildPlaceholderTensors(shapeList[0:pCount], dtypeList[0:pCount]) |
| 2144 | ) |
| 2145 | tens.extend(self.buildConstTensors(shapeList[pCount:], dtypeList[pCount:])) |
Matthew Haddon | 459443c | 2021-08-23 16:43:13 +0100 | [diff] [blame] | 2146 | elif op["op"] == Op.INTDIV: |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 2147 | assert ( |
| 2148 | pCount == 2 and cCount == 0 |
Matthew Haddon | 459443c | 2021-08-23 16:43:13 +0100 | [diff] [blame] | 2149 | ), "Op.INTDIV must have 2 placeholders, 0 consts" |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 2150 | |
| 2151 | placeholders = [] |
| 2152 | |
Matthew Haddon | 459443c | 2021-08-23 16:43:13 +0100 | [diff] [blame] | 2153 | # Two invalid cases for Op.INTDIV: |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 2154 | # 1. divisor == 0 |
Kevin Cheng | 47315e1 | 2021-05-13 17:41:28 -0700 | [diff] [blame] | 2155 | # 2. dividend == -(1<<31) and divisor == -1 |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 2156 | while True: |
| 2157 | dividend_arr = self.getRandTensor(shapeList[0], dtypeList[0]) |
| 2158 | divisor_arr = self.getRandTensor(shapeList[1], dtypeList[1]) |
| 2159 | |
| 2160 | if (divisor_arr == 0).any(): |
| 2161 | continue |
| 2162 | |
Kevin Cheng | 47315e1 | 2021-05-13 17:41:28 -0700 | [diff] [blame] | 2163 | if (dividend_arr == -(2 ** 31)).any() and (divisor_arr == -1).any(): |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 2164 | continue |
| 2165 | |
| 2166 | break |
| 2167 | |
| 2168 | placeholders.append( |
| 2169 | self.ser.addPlaceholder(shapeList[0], dtypeList[0], dividend_arr) |
| 2170 | ) |
| 2171 | placeholders.append( |
| 2172 | self.ser.addPlaceholder(shapeList[1], dtypeList[1], divisor_arr) |
| 2173 | ) |
| 2174 | |
| 2175 | tens.extend(placeholders) |
| 2176 | elif op["op"] == Op.MUL: |
| 2177 | assert ( |
| 2178 | pCount == 2 and cCount == 0 |
| 2179 | ), "Op.MUL must have 2 placeholders, 0 consts" |
| 2180 | |
| 2181 | if dtypeList[0] == DType.FLOAT: |
| 2182 | tens.extend(self.buildPlaceholderTensors(shapeList[:], dtypeList[:])) |
| 2183 | else: |
| 2184 | placeholders = [] |
| 2185 | |
| 2186 | # Make sure multiply result in int32 range |
| 2187 | shift = testArgs[0] |
| 2188 | if dtypeList[0] == DType.INT8: |
| 2189 | num_bits = 8 |
| 2190 | elif dtypeList[0] == DType.INT16: |
| 2191 | num_bits = 16 |
| 2192 | elif dtypeList[0] == DType.INT32: |
| 2193 | num_bits = 32 |
| 2194 | else: |
| 2195 | raise Exception("OpMul: invalid input dtype") |
| 2196 | |
| 2197 | for idx, shape in enumerate(shapeList[:]): |
| 2198 | low = -(2 ** (num_bits - 1)) |
| 2199 | high = (2 ** (num_bits - 1)) - 1 |
| 2200 | |
| 2201 | a_arr = np.int32( |
| 2202 | self.rng.integers(low=low, high=high, size=shapeList[0]) |
| 2203 | ) |
| 2204 | b_arr = np.int32( |
| 2205 | self.rng.integers(low=low, high=high, size=shapeList[1]) |
| 2206 | ) |
| 2207 | |
| 2208 | i = 0 |
| 2209 | while True: |
| 2210 | |
| 2211 | a_arr_64 = a_arr.astype(np.int64) |
| 2212 | b_arr_64 = b_arr.astype(np.int64) |
| 2213 | |
| 2214 | if shift > 0: |
| 2215 | rounding = 1 << (shift - 1) |
| 2216 | result_arr = ((a_arr_64 * b_arr_64) + rounding) >> shift |
| 2217 | else: |
| 2218 | result_arr = a_arr_64 * b_arr_64 |
| 2219 | |
| 2220 | if (result_arr > -(2 ** 31)).all() and ( |
| 2221 | result_arr <= ((2 ** 31) - 1) |
| 2222 | ).all(): |
| 2223 | break |
| 2224 | |
| 2225 | i = i + 1 |
| 2226 | a_arr = a_arr // 2 |
| 2227 | b_arr = b_arr // 2 |
| 2228 | |
| 2229 | placeholders.append( |
| 2230 | self.ser.addPlaceholder(shapeList[0], dtypeList[0], a_arr) |
| 2231 | ) |
| 2232 | placeholders.append( |
| 2233 | self.ser.addPlaceholder(shapeList[1], dtypeList[1], b_arr) |
| 2234 | ) |
| 2235 | |
| 2236 | tens.extend(placeholders) |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 2237 | elif op["op"] == Op.CONCAT: |
| 2238 | count = len(shapeList) - self.args.num_const_inputs_concat |
| 2239 | if count < 1: |
| 2240 | count = 1 |
| 2241 | if self.args.num_const_inputs_concat == 0: |
| 2242 | count = len(shapeList) |
| 2243 | |
| 2244 | shapeList = TosaTensorGen.tgConcatConstInput(self, shapeList, testArgs[0]) |
| 2245 | tens.extend( |
| 2246 | self.buildPlaceholderTensors(shapeList[0:count], dtypeList[0:count]) |
| 2247 | ) |
| 2248 | tens.extend(self.buildConstTensors(shapeList[count:], dtypeList[count:])) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 2249 | else: |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2250 | tens.extend( |
| 2251 | self.buildPlaceholderTensors(shapeList[0:pCount], dtypeList[0:pCount]) |
| 2252 | ) |
| 2253 | tens.extend(self.buildConstTensors(shapeList[pCount:], dtypeList[pCount:])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2254 | |
| 2255 | if qgen is not None: |
Les Bell | 30e4680 | 2021-07-23 09:43:31 +0100 | [diff] [blame] | 2256 | qinfo = qgen(self, op, dtype_or_dtypeList) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2257 | else: |
| 2258 | qinfo = None |
| 2259 | |
| 2260 | try: |
| 2261 | if qinfo is not None: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2262 | resultName = build_fcn(self, op["op"], *tens, *testArgs, qinfo) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2263 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2264 | resultName = build_fcn(self, op["op"], *tens, *testArgs) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2265 | except TypeError as e: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2266 | print( |
| 2267 | "build_fcn: {}\nTensors: {}\nArgs: {}\n".format( |
| 2268 | build_fcn, tens, testArgs |
| 2269 | ) |
| 2270 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2271 | raise e |
| 2272 | |
| 2273 | # Save the serialized test |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2274 | self.serialize("test") |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2275 | |
| 2276 | def createDynamicOpLists(self): |
| 2277 | |
| 2278 | # Dynamically create op lists for convolutions with a list of kernel sizes |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2279 | KERNELS_2D = [[1, 1], [2, 2], [3, 3], [5, 5], [3, 1], [1, 3]] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2280 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2281 | for k in KERNELS_2D: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2282 | testName = "conv2d_{}x{}".format(k[0], k[1]) |
| 2283 | self.TOSA_OP_LIST[testName] = self.TOSA_OP_LIST["conv2d_TEMPLATE"].copy() |
| 2284 | self.TOSA_OP_LIST[testName]["filter"] = k |
| 2285 | self.TOSA_OP_LIST[testName]["template"] = False |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2286 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2287 | testName = "depthwise_conv2d_{}x{}".format(k[0], k[1]) |
| 2288 | self.TOSA_OP_LIST[testName] = self.TOSA_OP_LIST[ |
| 2289 | "depthwise_conv2d_TEMPLATE" |
| 2290 | ].copy() |
| 2291 | self.TOSA_OP_LIST[testName]["filter"] = k |
| 2292 | self.TOSA_OP_LIST[testName]["template"] = False |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2293 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2294 | testName = "transpose_conv2d_{}x{}".format(k[0], k[1]) |
| 2295 | self.TOSA_OP_LIST[testName] = self.TOSA_OP_LIST[ |
| 2296 | "transpose_conv2d_TEMPLATE" |
| 2297 | ].copy() |
| 2298 | self.TOSA_OP_LIST[testName]["filter"] = k |
| 2299 | self.TOSA_OP_LIST[testName]["template"] = False |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2300 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2301 | KERNELS_3D = [[1, 1, 1], [2, 1, 1], [1, 2, 1], [1, 1, 2]] |
| 2302 | for k in KERNELS_3D: |
| 2303 | testName = "conv3d_{}x{}x{}".format(k[0], k[1], k[2]) |
| 2304 | self.TOSA_OP_LIST[testName] = self.TOSA_OP_LIST["conv3d_TEMPLATE"].copy() |
| 2305 | self.TOSA_OP_LIST[testName]["filter"] = k |
| 2306 | self.TOSA_OP_LIST[testName]["template"] = False |
| 2307 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2308 | # Delete any templates after having created any dynamic ops |
| 2309 | # This is a two-pass operation because it's bad practice to delete |
| 2310 | # keys from dictionaries while iterating |
| 2311 | keyList = [] |
| 2312 | for k in self.TOSA_OP_LIST: |
| 2313 | try: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2314 | if self.TOSA_OP_LIST[k]["template"] == True: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2315 | keyList.append(k) |
| 2316 | continue |
| 2317 | except KeyError: |
| 2318 | pass |
| 2319 | |
| 2320 | for k in keyList: |
| 2321 | del self.TOSA_OP_LIST[k] |
| 2322 | |
| 2323 | def initOpListDefaults(self): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2324 | """Fill in default fields for ops if they aren't already specified. |
| 2325 | Look for missing required fields (datastructure linting).""" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2326 | for op in self.TOSA_OP_LIST: |
| 2327 | |
| 2328 | # Required fields |
| 2329 | try: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2330 | pl, c = self.TOSA_OP_LIST[op]["operands"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2331 | except (KeyError, ValueError, TypeError): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2332 | raise Exception( |
| 2333 | "Op {} is missing a valid operand tuple in TOSA_OP_LIST".format(op) |
| 2334 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2335 | |
| 2336 | try: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2337 | fcn, tgen, arggen = self.TOSA_OP_LIST[op]["build_fcn"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2338 | except (KeyError, ValueError, TypeError): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2339 | raise Exception( |
| 2340 | "Op {} is missing a valid build_fcn tuple in TOSA_OP_LIST".format( |
| 2341 | op |
| 2342 | ) |
| 2343 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2344 | |
| 2345 | try: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2346 | types = self.TOSA_OP_LIST[op]["types"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2347 | except KeyError as e: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2348 | raise Exception( |
| 2349 | "Op {} is missing a valid type list in TOSA_OP_LIST".format(op) |
| 2350 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2351 | |
| 2352 | try: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2353 | opcode = self.TOSA_OP_LIST[op]["op"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2354 | except KeyError as e: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2355 | raise Exception( |
| 2356 | "Op {} is missing the Op field in TOSA_OP_LIST".format(op) |
| 2357 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2358 | |
| 2359 | # Put in default rank range, if missing |
| 2360 | try: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2361 | rank = self.TOSA_OP_LIST[op]["rank"] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2362 | except KeyError: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2363 | self.TOSA_OP_LIST[op]["rank"] = self.DEFAULT_RANK_RANGE |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2364 | |
| 2365 | # Tensor operator list |
| 2366 | # 'op': op name |
| 2367 | # 'operands': tuple of (placeholder, const) operands |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 2368 | # 'rank': optional, restricts rank to tuple inclusive of (min, max), |
| 2369 | # if not specified, defaults to (1, 4) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2370 | # 'build_fcn': tuple of the function to (build_operator(), TensorGen function, ArgGen enum) |
| 2371 | # 'types': array of datatypes to be tested |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2372 | TYPE_FP = [DType.FLOAT] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2373 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2374 | TYPE_INT = [DType.INT8, DType.INT16, DType.INT32] # Excludes INT4 |
| 2375 | TYPE_INT_FP = [DType.INT8, DType.INT16, DType.INT32, DType.FLOAT] # Excludes INT4 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2376 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2377 | TYPE_BOOL = [DType.BOOL] |
| 2378 | TYPE_FI32 = [DType.FLOAT, DType.INT32] |
| 2379 | TYPE_FIB = [DType.FLOAT, DType.INT8, DType.INT16, DType.INT32, DType.BOOL] |
| 2380 | TYPE_FI16 = [DType.FLOAT, DType.INT16] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2381 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2382 | TYPE_NARROW_INT_FP = [DType.INT8, DType.INT16, DType.FLOAT] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2383 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2384 | TYPE_CONV = [ |
Kevin Cheng | a901740 | 2021-07-28 17:19:23 -0700 | [diff] [blame] | 2385 | [DType.INT8, DType.INT4, DType.INT32], |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2386 | [DType.INT8, DType.INT8, DType.INT32], |
| 2387 | [DType.INT16, DType.INT8, DType.INT48], |
| 2388 | DType.FLOAT, |
| 2389 | ] |
| 2390 | |
Jeremy Johnson | 97eb75f | 2021-07-08 11:58:02 +0100 | [diff] [blame] | 2391 | DEFAULT_RANK_RANGE = (1, TOSA_TENSOR_MAX_RANK) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2392 | |
| 2393 | TOSA_OP_LIST = { |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2394 | # Tensor operators |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2395 | "argmax": { |
| 2396 | "op": Op.ARGMAX, |
| 2397 | "operands": (1, 0), |
| 2398 | "build_fcn": (build_argmax, TosaTensorGen.tgBasic, TosaArgGen.agAxis), |
| 2399 | "types": TYPE_NARROW_INT_FP, |
| 2400 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2401 | "avg_pool2d": { |
| 2402 | "op": Op.AVG_POOL2D, |
| 2403 | "operands": (1, 0), |
| 2404 | "rank": (4, 4), |
| 2405 | "build_fcn": (build_pool2d, TosaTensorGen.tgNHWC, TosaArgGen.agPooling), |
| 2406 | "qgen": TosaQuantGen.qgUnary, |
| 2407 | "types": TYPE_NARROW_INT_FP, |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 2408 | "invalid_test_validators": (TosaInvalidValidator.ivHeightWidthSmallerZero,) |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2409 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2410 | # Templated operator. Filled in by createDynamicOpLists |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2411 | "conv2d_TEMPLATE": { |
| 2412 | "op": Op.CONV2D, |
| 2413 | "operands": (1, 2), |
| 2414 | "rank": (4, 4), |
| 2415 | "build_fcn": (build_conv2d, TosaTensorGen.tgConv2D, TosaArgGen.agConv2D), |
| 2416 | "qgen": TosaQuantGen.qgConv, |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2417 | "types": TYPE_CONV, |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 2418 | "invalid_test_validators": (TosaInvalidValidator.ivHeightWidthSmallerZero,), |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2419 | "template": True, |
| 2420 | }, |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2421 | # Templated operator. Filled in by createDynamicOpLists |
| 2422 | "conv3d_TEMPLATE": { |
| 2423 | "op": Op.CONV3D, |
| 2424 | "operands": (1, 2), |
| 2425 | "rank": (5, 5), |
| 2426 | "build_fcn": (build_conv3d, TosaTensorGen.tgConv3D, TosaArgGen.agConv3D), |
| 2427 | "qgen": TosaQuantGen.qgConv, |
| 2428 | "types": TYPE_CONV, |
| 2429 | "template": True, |
| 2430 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2431 | # Templated operator. Filled in by createDynamicOpLists |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2432 | "depthwise_conv2d_TEMPLATE": { |
| 2433 | "op": Op.DEPTHWISE_CONV2D, |
| 2434 | "operands": (1, 2), |
| 2435 | "filter": [1, 1], |
| 2436 | "rank": (4, 4), |
| 2437 | "build_fcn": ( |
| 2438 | build_depthwise_conv2d, |
| 2439 | TosaTensorGen.tgDepthwiseConv2D, |
| 2440 | TosaArgGen.agConv2D, |
| 2441 | ), |
| 2442 | "qgen": TosaQuantGen.qgConv, |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2443 | "types": TYPE_CONV, |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 2444 | "invalid_test_validators": (TosaInvalidValidator.ivHeightWidthSmallerZero,), |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2445 | "template": True, |
| 2446 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2447 | "fully_connected": { |
| 2448 | "op": Op.FULLY_CONNECTED, |
| 2449 | "operands": (1, 2), |
| 2450 | "rank": (2, 2), |
| 2451 | "build_fcn": (build_fully_connected, TosaTensorGen.tgFullyConnected, None), |
| 2452 | "qgen": TosaQuantGen.qgConv, |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2453 | "types": TYPE_CONV, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2454 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2455 | "matmul": { |
| 2456 | "op": Op.MATMUL, |
| 2457 | "operands": (2, 0), |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 2458 | "rank": (3, 3), |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2459 | "build_fcn": (build_matmul, TosaTensorGen.tgMatmul, None), |
| 2460 | "qgen": TosaQuantGen.qgMatmul, |
| 2461 | "types": TYPE_NARROW_INT_FP, |
| 2462 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2463 | "max_pool2d": { |
| 2464 | "op": Op.MAX_POOL2D, |
| 2465 | "operands": (1, 0), |
| 2466 | "rank": (4, 4), |
| 2467 | "build_fcn": (build_pool2d, TosaTensorGen.tgNHWC, TosaArgGen.agPooling), |
| 2468 | "types": TYPE_NARROW_INT_FP, |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 2469 | "invalid_test_validators": (TosaInvalidValidator.ivHeightWidthSmallerZero,) |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2470 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2471 | # Templated operator. Filled in by createDynamicOpLists |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2472 | "transpose_conv2d_TEMPLATE": { |
| 2473 | "op": Op.TRANSPOSE_CONV2D, |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 2474 | "operands": (1, 2), |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2475 | "rank": (4, 4), |
| 2476 | "build_fcn": ( |
| 2477 | build_transpose_conv2d, |
| 2478 | TosaTensorGen.tgTransposeConv2D, |
| 2479 | TosaArgGen.agTransposeConv2D, |
| 2480 | ), |
| 2481 | "qgen": TosaQuantGen.qgConv, |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2482 | "types": TYPE_CONV, |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 2483 | "invalid_test_validators": (TosaInvalidValidator.ivNonPositiveOutputShape,), |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2484 | "template": True, |
| 2485 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2486 | # Activation functions |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2487 | "clamp": { |
| 2488 | "op": Op.CLAMP, |
| 2489 | "operands": (1, 0), |
| 2490 | "build_fcn": (build_clamp, TosaTensorGen.tgBasic, None), |
| 2491 | "types": TYPE_NARROW_INT_FP, |
| 2492 | }, |
| 2493 | "relun": { |
| 2494 | "op": Op.RELUN, |
| 2495 | "operands": (1, 0), |
| 2496 | "build_fcn": (build_relun, TosaTensorGen.tgBasic, None), |
| 2497 | "types": TYPE_FI32, |
| 2498 | }, |
| 2499 | "sigmoid": { |
| 2500 | "op": Op.SIGMOID, |
| 2501 | "operands": (1, 0), |
| 2502 | "build_fcn": (build_sigmoid, TosaTensorGen.tgBasic, None), |
| 2503 | "types": TYPE_FP, |
| 2504 | }, |
| 2505 | "tanh": { |
| 2506 | "op": Op.TANH, |
| 2507 | "operands": (1, 0), |
| 2508 | "build_fcn": (build_tanh, TosaTensorGen.tgBasic, None), |
| 2509 | "types": TYPE_FP, |
| 2510 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2511 | # Elementwise Binary Operators |
| 2512 | "add": { |
| 2513 | "op": Op.ADD, |
| 2514 | "operands": (2, 0), |
| 2515 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2516 | "types": TYPE_FI32, |
| 2517 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2518 | "arithmetic_right_shift": { |
| 2519 | "op": Op.ARITHMETIC_RIGHT_SHIFT, |
| 2520 | "operands": (2, 0), |
| 2521 | "build_fcn": ( |
| 2522 | build_arithmetic_right_shift, |
| 2523 | TosaTensorGen.tgBroadcastFuzz, |
| 2524 | TosaArgGen.agArithmeticRightShift, |
| 2525 | ), |
| 2526 | "types": TYPE_INT, |
| 2527 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2528 | "bitwise_and": { |
| 2529 | "op": Op.BITWISE_AND, |
| 2530 | "operands": (2, 0), |
| 2531 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2532 | "types": TYPE_INT, |
| 2533 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2534 | "bitwise_or": { |
| 2535 | "op": Op.BITWISE_OR, |
| 2536 | "operands": (2, 0), |
| 2537 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2538 | "types": TYPE_INT, |
| 2539 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2540 | "bitwise_xor": { |
| 2541 | "op": Op.BITWISE_XOR, |
| 2542 | "operands": (2, 0), |
| 2543 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2544 | "types": TYPE_INT, |
| 2545 | }, |
Matthew Haddon | 459443c | 2021-08-23 16:43:13 +0100 | [diff] [blame] | 2546 | "intdiv": { |
| 2547 | "op": Op.INTDIV, |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 2548 | "operands": (2, 0), |
| 2549 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2550 | "types": [DType.INT32], |
| 2551 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2552 | "logical_and": { |
| 2553 | "op": Op.LOGICAL_AND, |
| 2554 | "operands": (2, 0), |
| 2555 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2556 | "types": TYPE_BOOL, |
| 2557 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2558 | "logical_left_shift": { |
| 2559 | "op": Op.LOGICAL_LEFT_SHIFT, |
| 2560 | "operands": (2, 0), |
| 2561 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2562 | "types": TYPE_INT, |
| 2563 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2564 | "logical_right_shift": { |
| 2565 | "op": Op.LOGICAL_RIGHT_SHIFT, |
| 2566 | "operands": (2, 0), |
| 2567 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2568 | "types": TYPE_INT, |
| 2569 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2570 | "logical_or": { |
| 2571 | "op": Op.LOGICAL_OR, |
| 2572 | "operands": (2, 0), |
| 2573 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2574 | "types": TYPE_BOOL, |
| 2575 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2576 | "logical_xor": { |
| 2577 | "op": Op.LOGICAL_XOR, |
| 2578 | "operands": (2, 0), |
| 2579 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2580 | "types": TYPE_BOOL, |
| 2581 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2582 | "maximum": { |
| 2583 | "op": Op.MAXIMUM, |
| 2584 | "operands": (2, 0), |
| 2585 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2586 | "types": TYPE_FI32, |
| 2587 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2588 | "minimum": { |
| 2589 | "op": Op.MINIMUM, |
| 2590 | "operands": (2, 0), |
| 2591 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2592 | "types": TYPE_FI32, |
| 2593 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2594 | "mul": { |
| 2595 | "op": Op.MUL, |
| 2596 | "operands": (2, 0), |
| 2597 | "build_fcn": (build_mul, TosaTensorGen.tgBroadcastFuzz, TosaArgGen.agMul), |
| 2598 | "types": TYPE_INT_FP, |
| 2599 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2600 | "pow": { |
| 2601 | "op": Op.POW, |
| 2602 | "operands": (2, 0), |
| 2603 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBasic, None), |
| 2604 | "types": TYPE_FP, |
| 2605 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2606 | "sub": { |
| 2607 | "op": Op.SUB, |
| 2608 | "operands": (2, 0), |
| 2609 | "build_fcn": (build_binary_broadcast, TosaTensorGen.tgBroadcastFuzz, None), |
| 2610 | "types": TYPE_FI32, |
| 2611 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2612 | "table": { |
| 2613 | "op": Op.TABLE, |
| 2614 | # Use the automatic generation functions to create the input array |
| 2615 | # but create the table tensor in the build function, as it may be |
| 2616 | # a different type from the input |
| 2617 | "operands": (1, 0), |
| 2618 | "build_fcn": (build_table, TosaTensorGen.tgBasic, None), |
Jeremy Johnson | f54d8a2 | 2021-07-20 16:01:06 +0100 | [diff] [blame] | 2619 | "types": [DType.INT8, DType.INT16], |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2620 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2621 | # Elementwise Unary operators |
| 2622 | "abs": { |
| 2623 | "op": Op.ABS, |
| 2624 | "operands": (1, 0), |
| 2625 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2626 | "types": TYPE_FI32, |
| 2627 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2628 | "bitwise_not": { |
| 2629 | "op": Op.BITWISE_NOT, |
| 2630 | "operands": (1, 0), |
| 2631 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2632 | "types": TYPE_INT, |
| 2633 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2634 | "ceil": { |
| 2635 | "op": Op.CEIL, |
| 2636 | "operands": (1, 0), |
| 2637 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2638 | "types": TYPE_FP, |
| 2639 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2640 | "clz": { |
| 2641 | "op": Op.CLZ, |
| 2642 | "operands": (1, 0), |
| 2643 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2644 | "types": [DType.INT32], |
| 2645 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2646 | "exp": { |
| 2647 | "op": Op.EXP, |
| 2648 | "operands": (1, 0), |
| 2649 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2650 | "types": TYPE_FP, |
| 2651 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2652 | "floor": { |
| 2653 | "op": Op.FLOOR, |
| 2654 | "operands": (1, 0), |
| 2655 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2656 | "types": TYPE_FP, |
| 2657 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2658 | "log": { |
| 2659 | "op": Op.LOG, |
| 2660 | "operands": (1, 0), |
| 2661 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2662 | "types": TYPE_FP, |
| 2663 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2664 | "logical_not": { |
| 2665 | "op": Op.LOGICAL_NOT, |
| 2666 | "operands": (1, 0), |
| 2667 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2668 | "types": TYPE_BOOL, |
| 2669 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2670 | "negate": { |
| 2671 | "op": Op.NEGATE, |
| 2672 | "operands": (1, 0), |
| 2673 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2674 | "qgen": TosaQuantGen.qgUnary, |
| 2675 | "types": TYPE_INT_FP, |
| 2676 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2677 | "reciprocal": { |
| 2678 | "op": Op.RECIPROCAL, |
| 2679 | "operands": (1, 0), |
| 2680 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2681 | "types": TYPE_FP, |
| 2682 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2683 | "rsqrt": { |
| 2684 | "op": Op.RSQRT, |
| 2685 | "operands": (1, 0), |
| 2686 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2687 | "types": TYPE_FP, |
| 2688 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2689 | # Elementwise Ternary operators |
| 2690 | "select": { |
| 2691 | "op": Op.SELECT, |
| 2692 | "operands": (3, 0), |
| 2693 | "build_fcn": (build_select, TosaTensorGen.tgBroadcastFuzz, None), |
| 2694 | "types": TYPE_FIB, |
| 2695 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2696 | # Comparison operators |
| 2697 | "equal": { |
| 2698 | "op": Op.EQUAL, |
| 2699 | "operands": (2, 0), |
| 2700 | "build_fcn": (build_comparison, TosaTensorGen.tgBroadcastFuzz, None), |
| 2701 | "types": TYPE_FI32, |
| 2702 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2703 | "greater_equal": { |
| 2704 | "op": Op.GREATER_EQUAL, |
| 2705 | "operands": (2, 0), |
| 2706 | "build_fcn": (build_comparison, TosaTensorGen.tgBroadcastFuzz, None), |
| 2707 | "types": TYPE_FI32, |
| 2708 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2709 | "greater": { |
| 2710 | "op": Op.GREATER, |
| 2711 | "operands": (2, 0), |
| 2712 | "build_fcn": (build_comparison, TosaTensorGen.tgBroadcastFuzz, None), |
| 2713 | "types": TYPE_FI32, |
| 2714 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2715 | # Reduction operators |
| 2716 | "reduce_all": { |
| 2717 | "op": Op.REDUCE_ALL, |
| 2718 | "operands": (1, 0), |
| 2719 | "build_fcn": (build_reduce, TosaTensorGen.tgBasic, TosaArgGen.agAxis), |
| 2720 | "types": TYPE_BOOL, |
| 2721 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2722 | "reduce_any": { |
| 2723 | "op": Op.REDUCE_ANY, |
| 2724 | "operands": (1, 0), |
| 2725 | "build_fcn": (build_reduce, TosaTensorGen.tgBasic, TosaArgGen.agAxis), |
| 2726 | "types": TYPE_BOOL, |
| 2727 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2728 | "reduce_max": { |
| 2729 | "op": Op.REDUCE_MAX, |
| 2730 | "operands": (1, 0), |
| 2731 | "build_fcn": (build_reduce, TosaTensorGen.tgBasic, TosaArgGen.agAxis), |
| 2732 | "types": TYPE_INT_FP, |
| 2733 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2734 | "reduce_min": { |
| 2735 | "op": Op.REDUCE_MAX, |
| 2736 | "operands": (1, 0), |
| 2737 | "build_fcn": (build_reduce, TosaTensorGen.tgBasic, TosaArgGen.agAxis), |
| 2738 | "types": TYPE_INT_FP, |
| 2739 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2740 | "reduce_product": { |
| 2741 | "op": Op.REDUCE_PRODUCT, |
| 2742 | "operands": (1, 0), |
| 2743 | "build_fcn": (build_reduce, TosaTensorGen.tgBasic, TosaArgGen.agAxis), |
| 2744 | "types": TYPE_FP, |
| 2745 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2746 | "reduce_sum": { |
| 2747 | "op": Op.REDUCE_SUM, |
| 2748 | "operands": (1, 0), |
| 2749 | "build_fcn": (build_reduce, TosaTensorGen.tgBasic, TosaArgGen.agAxis), |
| 2750 | "types": TYPE_FI32, |
| 2751 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2752 | # Data layout operators |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2753 | "concat": { |
| 2754 | "op": Op.CONCAT, |
| 2755 | "operands": (2, 0), |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 2756 | "build_fcn": (build_concat, TosaTensorGen.tgConcat, TosaArgGen.agAxis), |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2757 | "types": TYPE_FIB, |
| 2758 | }, |
| 2759 | "pad": { |
| 2760 | "op": Op.PAD, |
| 2761 | "operands": (1, 0), |
| 2762 | "build_fcn": (build_pad, TosaTensorGen.tgBasic, TosaArgGen.agPad), |
| 2763 | "qgen": TosaQuantGen.qgPad, |
| 2764 | "types": TYPE_FIB, |
| 2765 | }, |
| 2766 | "reshape": { |
| 2767 | "op": Op.RESHAPE, |
| 2768 | "operands": (1, 0), |
| 2769 | "build_fcn": (build_reshape, TosaTensorGen.tgBasic, TosaArgGen.agReshape), |
| 2770 | "types": TYPE_FIB, |
| 2771 | }, |
| 2772 | "reverse": { |
| 2773 | "op": Op.REVERSE, |
| 2774 | "operands": (1, 0), |
| 2775 | "build_fcn": (build_reverse, TosaTensorGen.tgBasic, TosaArgGen.agAxis), |
| 2776 | "types": TYPE_FIB, |
| 2777 | }, |
| 2778 | "slice": { |
| 2779 | "op": Op.SLICE, |
| 2780 | "operands": (1, 0), |
| 2781 | "build_fcn": (build_slice, TosaTensorGen.tgBasic, TosaArgGen.agSlice), |
| 2782 | "types": TYPE_FIB, |
| 2783 | }, |
| 2784 | "tile": { |
| 2785 | "op": Op.TILE, |
| 2786 | "operands": (1, 0), |
| 2787 | "build_fcn": (build_tile, TosaTensorGen.tgBasic, TosaArgGen.agTile), |
| 2788 | "types": TYPE_FIB, |
| 2789 | }, |
| 2790 | "transpose": { |
| 2791 | "op": Op.TRANSPOSE, |
| 2792 | "operands": (1, 0), |
Jeremy Johnson | a618557 | 2021-06-21 15:55:35 +0100 | [diff] [blame] | 2793 | "rank": (1, 4), |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2794 | "build_fcn": ( |
| 2795 | build_transpose, |
| 2796 | TosaTensorGen.tgBasic, |
| 2797 | TosaArgGen.agTranspose, |
| 2798 | ), |
| 2799 | "types": TYPE_FIB, |
| 2800 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2801 | # Data nodes |
| 2802 | "const": { |
| 2803 | "op": Op.CONST, |
| 2804 | "operands": (1, 0), |
| 2805 | "build_fcn": (build_placeholder, TosaTensorGen.tgBasic, None), |
| 2806 | "types": TYPE_FIB, |
| 2807 | }, |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2808 | "identity": { |
| 2809 | "op": Op.IDENTITY, |
| 2810 | "operands": (1, 0), |
| 2811 | "build_fcn": (build_unary, TosaTensorGen.tgBasic, None), |
| 2812 | "types": TYPE_FIB, |
| 2813 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2814 | # Scatter/Gather |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2815 | "gather": { |
| 2816 | "op": Op.GATHER, |
| 2817 | # Only specify 'values' tensor here. 'indices' is generated in op building stage |
| 2818 | "operands": (1, 0), |
| 2819 | "rank": (3, 3), |
| 2820 | "build_fcn": (build_gather, TosaTensorGen.tgBasic, None), |
| 2821 | "types": TYPE_INT_FP, |
| 2822 | }, |
| 2823 | "scatter": { |
| 2824 | "op": Op.SCATTER, |
| 2825 | # Only specify 'values_in' tensor here. |
| 2826 | #'indices' and 'input' are generated in op building stage |
| 2827 | "operands": (2, 0), |
| 2828 | "rank": (3, 3), |
| 2829 | "build_fcn": (build_scatter, TosaTensorGen.tgScatter, None), |
| 2830 | "types": TYPE_INT_FP, |
| 2831 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2832 | # Image operations |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2833 | "resize": { |
| 2834 | "op": Op.RESIZE, |
| 2835 | "operands": (1, 0), |
| 2836 | "rank": (4, 4), |
| 2837 | "build_fcn": (build_resize, TosaTensorGen.tgNHWC, TosaArgGen.agResize), |
| 2838 | "types": [DType.INT8, DType.INT16, DType.FLOAT], |
Matthew Haddon | b724efc | 2021-08-25 16:40:29 +0100 | [diff] [blame] | 2839 | "invalid_test_validators": (TosaInvalidValidator.ivWrongDataTypeOrModeResize, TosaInvalidValidator.ivBadStride) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2840 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2841 | # Type conversion |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2842 | "cast": { |
| 2843 | "op": Op.CAST, |
| 2844 | "operands": (1, 0), |
| 2845 | "build_fcn": (build_cast, TosaTensorGen.tgBasic, TosaArgGen.agCast), |
| 2846 | "types": [DType.FLOAT, DType.INT8, DType.INT16, DType.INT32, DType.BOOL], |
| 2847 | }, |
| 2848 | "rescale": { |
| 2849 | "op": Op.RESCALE, |
| 2850 | "operands": (1, 0), |
| 2851 | "build_fcn": (build_rescale, TosaTensorGen.tgBasic, TosaArgGen.agRescale), |
Matthew Haddon | cac4ee9 | 2021-07-22 14:30:53 +0100 | [diff] [blame] | 2852 | "types": [DType.UINT8, DType.INT8, DType.INT16, DType.INT32, DType.INT48], |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2853 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2854 | # Custom |
| 2855 | # Not implemented. |
Jared Smolens | 573ecd4 | 2021-03-04 15:24:10 -0800 | [diff] [blame] | 2856 | # Control flow operators |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2857 | # Two varients of cond_if, one that generates one of two constant tensors (no |
| 2858 | # inputs to the basic blocks, one output) and another that either adds or subtracts two tensors |
| 2859 | # (two inputs to the basic blocks, one output) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2860 | "cond_if_const": { |
| 2861 | "op": Op.COND_IF, |
| 2862 | "operands": (0, 2), |
| 2863 | "build_fcn": ( |
| 2864 | build_cond_if_const, |
| 2865 | TosaTensorGen.tgBasic, |
| 2866 | TosaArgGen.agCondIf, |
| 2867 | ), |
| 2868 | "types": [DType.BOOL], |
| 2869 | }, |
| 2870 | "cond_if_binary": { |
| 2871 | "op": Op.COND_IF, |
| 2872 | "operands": (2, 0), |
| 2873 | "build_fcn": ( |
| 2874 | build_cond_if_binary, |
| 2875 | TosaTensorGen.tgBasic, |
| 2876 | TosaArgGen.agCondIf, |
| 2877 | ), |
| 2878 | "types": TYPE_FI32, |
| 2879 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2880 | # while_loop |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2881 | "while_loop": { |
| 2882 | "op": Op.WHILE_LOOP, |
| 2883 | "operands": (0, 1), |
| 2884 | "build_fcn": ( |
| 2885 | build_while_loop, |
| 2886 | TosaTensorGen.tgBasic, |
| 2887 | TosaArgGen.agWhileLoop, |
| 2888 | ), |
| 2889 | "types": [DType.INT32], |
| 2890 | }, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2891 | } |
| 2892 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2893 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2894 | class OutputShaper: |
| 2895 | # Methods in this class compute the expected output shape and datatype |
| 2896 | # for common classes of operations |
| 2897 | def __init__(self): |
| 2898 | pass |
| 2899 | |
| 2900 | # These methods return arguments that can be used for |
| 2901 | # creating a new output tensor |
| 2902 | @staticmethod |
| 2903 | def binaryBroadcastOp(ser, a, b): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2904 | assert len(a.shape) == len(b.shape) |
| 2905 | assert a.dtype == b.dtype |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2906 | |
| 2907 | shape = [] |
| 2908 | for i in range(len(a.shape)): |
| 2909 | if a.shape[i] == 1: |
| 2910 | shape.append(b.shape[i]) |
| 2911 | else: |
| 2912 | shape.append(a.shape[i]) |
| 2913 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2914 | return ser.addOutput(shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2915 | |
| 2916 | @staticmethod |
| 2917 | def binaryNonBroadcastOp(ser, a, b): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2918 | assert len(a.shape) == len(b.shape) |
| 2919 | assert a.dtype == b.dtype |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2920 | |
| 2921 | shape = [] |
| 2922 | for i in range(len(a.shape)): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2923 | assert a.shape[i] == b.shape[i] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2924 | shape.append(a.shape[i]) |
| 2925 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2926 | return ser.addOutput(shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2927 | |
| 2928 | @staticmethod |
| 2929 | def unaryOp(ser, a): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2930 | return ser.addOutput(a.shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2931 | |
| 2932 | @staticmethod |
| 2933 | def selectOp(ser, cond, a, b): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2934 | assert len(a.shape) == len(b.shape) and len(a.shape) == len(cond.shape) |
| 2935 | assert a.dtype == b.dtype |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2936 | |
| 2937 | shape = [] |
| 2938 | for i in range(len(a.shape)): |
| 2939 | shape.append(max(cond.shape[i], a.shape[i], b.shape[i])) |
| 2940 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2941 | return ser.addOutput(shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2942 | |
| 2943 | @staticmethod |
| 2944 | def binaryComparisonOp(ser, a, b): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2945 | assert len(a.shape) == len(b.shape) |
| 2946 | assert a.dtype == b.dtype |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2947 | |
| 2948 | # Do broadcast |
| 2949 | shape = [] |
| 2950 | for i in range(len(a.shape)): |
| 2951 | if a.shape[i] == 1: |
| 2952 | shape.append(b.shape[i]) |
| 2953 | else: |
| 2954 | shape.append(a.shape[i]) |
| 2955 | |
| 2956 | # Force the output type to bool |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2957 | return ser.addOutput(shape, DType.BOOL) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2958 | |
| 2959 | @staticmethod |
| 2960 | def reduceOp(ser, a, axis): |
| 2961 | |
| 2962 | shape = a.shape.copy() |
| 2963 | |
| 2964 | shape[axis] = 1 |
| 2965 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2966 | return ser.addOutput(shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2967 | |
| 2968 | @staticmethod |
| 2969 | def argmaxOp(ser, a, axis): |
| 2970 | shape = a.shape.copy() |
| 2971 | del shape[axis] |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2972 | return ser.addOutput(shape, DType.INT32) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2973 | |
| 2974 | @staticmethod |
| 2975 | def conv2dOp(ser, ifm, filter, strides, padding, dilations): |
| 2976 | |
| 2977 | # IFM: NHWC |
| 2978 | # Filter: OHWI |
| 2979 | # OFM: NHWC |
| 2980 | |
| 2981 | if len(padding) == 2: |
| 2982 | # Expand padding to 4 parameters in the case of transpose_conv2d |
| 2983 | # From H,W to T,B,L,R |
| 2984 | padding = [padding[0], padding[0], padding[1], padding[1]] |
| 2985 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2986 | h = ( |
| 2987 | ifm.shape[1] |
| 2988 | - filter.shape[1] |
| 2989 | - (filter.shape[1] - 1) * (dilations[0] - 1) |
| 2990 | + padding[0] |
| 2991 | + padding[1] |
| 2992 | ) // strides[0] + 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2993 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 2994 | w = ( |
| 2995 | ifm.shape[2] |
| 2996 | - filter.shape[2] |
| 2997 | - (filter.shape[2] - 1) * (dilations[1] - 1) |
| 2998 | + padding[2] |
| 2999 | + padding[3] |
| 3000 | ) // strides[1] + 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3001 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3002 | ofm_shape = [ifm.shape[0], h, w, filter.shape[0]] |
| 3003 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 3004 | if ifm.dtype == DType.INT8: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3005 | out_dtype = DType.INT32 |
| 3006 | elif ifm.dtype == DType.INT16: |
| 3007 | out_dtype = DType.INT48 |
| 3008 | elif ifm.dtype == DType.FLOAT: |
| 3009 | out_dtype = DType.FLOAT |
| 3010 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3011 | raise Exception("Unsupported input dtype: {}".format(ifm.dtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3012 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3013 | return ser.addOutput(ofm_shape, out_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3014 | |
| 3015 | @staticmethod |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 3016 | def conv3dOp(ser, ifm, filter, strides, padding, dilations): |
| 3017 | |
| 3018 | # IFM: NDHWC |
| 3019 | # Filter: ODHWI |
| 3020 | # OFM: NDHWC |
| 3021 | |
| 3022 | d = ( |
| 3023 | ifm.shape[1] |
| 3024 | - filter.shape[1] |
| 3025 | - (filter.shape[1] - 1) * (dilations[0] - 1) |
| 3026 | + padding[0] |
| 3027 | + padding[1] |
| 3028 | ) // strides[0] + 1 |
| 3029 | |
| 3030 | h = ( |
| 3031 | ifm.shape[2] |
| 3032 | - filter.shape[2] |
| 3033 | - (filter.shape[2] - 1) * (dilations[1] - 1) |
| 3034 | + padding[2] |
| 3035 | + padding[3] |
| 3036 | ) // strides[1] + 1 |
| 3037 | |
| 3038 | w = ( |
| 3039 | ifm.shape[3] |
| 3040 | - filter.shape[3] |
| 3041 | - (filter.shape[3] - 1) * (dilations[2] - 1) |
| 3042 | + padding[4] |
| 3043 | + padding[5] |
| 3044 | ) // strides[2] + 1 |
| 3045 | |
| 3046 | ofm_shape = [ifm.shape[0], d, h, w, filter.shape[0]] |
| 3047 | |
| 3048 | if ifm.dtype == DType.INT8: |
| 3049 | out_dtype = DType.INT32 |
| 3050 | elif ifm.dtype == DType.INT16: |
| 3051 | out_dtype = DType.INT48 |
| 3052 | elif ifm.dtype == DType.FLOAT: |
| 3053 | out_dtype = DType.FLOAT |
| 3054 | else: |
| 3055 | raise Exception("Unsupported input dtype: {}".format(ifm.dtype)) |
| 3056 | |
| 3057 | return ser.addOutput(ofm_shape, out_dtype) |
| 3058 | |
| 3059 | @staticmethod |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3060 | def depthwiseConv2dOp(ser, ifm, filter, strides, padding, dilations): |
| 3061 | # IFM: NHWC |
| 3062 | # Filter: HWCM |
| 3063 | # OFM: NHW C*M |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3064 | h = ( |
| 3065 | ifm.shape[1] |
| 3066 | - filter.shape[0] |
| 3067 | - (filter.shape[0] - 1) * (dilations[0] - 1) |
| 3068 | + padding[0] |
| 3069 | + padding[1] |
| 3070 | ) // strides[0] + 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3071 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3072 | w = ( |
| 3073 | ifm.shape[2] |
| 3074 | - filter.shape[1] |
| 3075 | - (filter.shape[1] - 1) * (dilations[1] - 1) |
| 3076 | + padding[2] |
| 3077 | + padding[3] |
| 3078 | ) // strides[1] + 1 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3079 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3080 | ofm_shape = [ifm.shape[0], h, w, filter.shape[2] * filter.shape[3]] |
| 3081 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 3082 | if ifm.dtype == DType.INT8: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3083 | out_dtype = DType.INT32 |
| 3084 | elif ifm.dtype == DType.INT16: |
| 3085 | out_dtype = DType.INT48 |
| 3086 | elif ifm.dtype == DType.FLOAT: |
| 3087 | out_dtype = DType.FLOAT |
| 3088 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3089 | raise Exception("Unsupported input dtype: {}".format(ifm.dtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3090 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3091 | return ser.addOutput(ofm_shape, out_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3092 | |
| 3093 | @staticmethod |
| 3094 | def pool2dOp(ser, ifm, kernel, stride, pad): |
| 3095 | # input: NHWC |
| 3096 | h = (ifm.shape[1] + pad[0] + pad[1] + stride[0] - kernel[0]) // stride[0] |
| 3097 | w = (ifm.shape[2] + pad[2] + pad[3] + stride[1] - kernel[1]) // stride[1] |
| 3098 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3099 | ofm_shape = [ifm.shape[0], h, w, ifm.shape[3]] |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3100 | return ser.addOutput(ofm_shape, ifm.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3101 | |
| 3102 | @staticmethod |
| 3103 | def fullyConnectedOp(ser, input, filter): |
| 3104 | # input: N, IC |
| 3105 | # filter: OC, IC |
| 3106 | # output: N, OC |
| 3107 | |
| 3108 | output_shape = [input.shape[0], filter.shape[0]] |
| 3109 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 3110 | if input.dtype == DType.INT8: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3111 | out_dtype = DType.INT32 |
| 3112 | elif input.dtype == DType.INT16: |
| 3113 | out_dtype = DType.INT48 |
| 3114 | elif input.dtype == DType.FLOAT: |
| 3115 | out_dtype = DType.FLOAT |
| 3116 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3117 | raise Exception("Unsupported input dtype: {}".format(input.dtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3118 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3119 | return ser.addOutput(output_shape, out_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3120 | |
| 3121 | @staticmethod |
| 3122 | def matmulOp(ser, a, b): |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 3123 | # a: N, H, C |
| 3124 | # b: N, C, W |
| 3125 | # out: N, H, W |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3126 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 3127 | output_shape = [a.shape[0], a.shape[1], b.shape[2]] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3128 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 3129 | if a.dtype == DType.INT8: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3130 | out_dtype = DType.INT32 |
| 3131 | elif a.dtype == DType.INT16: |
| 3132 | out_dtype = DType.INT48 |
| 3133 | elif a.dtype == DType.FLOAT: |
| 3134 | out_dtype = DType.FLOAT |
| 3135 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3136 | raise Exception("UNsupported input dtype for matmul: {}".format(a.dtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3137 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3138 | return ser.addOutput(output_shape, out_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3139 | |
| 3140 | @staticmethod |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 3141 | def concatOp(ser, axis, *a): |
| 3142 | input1 = a[0] |
| 3143 | remaining_inputs = a[1:] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3144 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 3145 | output_shape = input1.shape.copy() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3146 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 3147 | output_shape[axis] = input1.shape[axis] |
| 3148 | |
| 3149 | for tensor in remaining_inputs: |
| 3150 | output_shape[axis] += tensor.shape[axis] |
| 3151 | |
| 3152 | return ser.addOutput(output_shape, input1.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3153 | |
| 3154 | @staticmethod |
| 3155 | def padOp(ser, a, padding): |
| 3156 | |
| 3157 | output_shape = a.shape.copy() |
| 3158 | |
| 3159 | for i in range(len(output_shape)): |
| 3160 | output_shape[i] = padding[i][0] + padding[i][1] + output_shape[i] |
| 3161 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3162 | return ser.addOutput(output_shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3163 | |
| 3164 | @staticmethod |
| 3165 | def reshapeOp(ser, a, shape): |
| 3166 | output_shape = shape.copy() |
| 3167 | |
| 3168 | totalElements = 1 |
| 3169 | for i in a.shape: |
| 3170 | totalElements *= i |
| 3171 | |
| 3172 | # If there are any -1 elements, figure out what that dimension must be |
| 3173 | totalOutputElements = 1 |
| 3174 | for i in output_shape: |
| 3175 | if i != -1: |
| 3176 | totalOutputElements *= i |
| 3177 | |
| 3178 | # And fill it in |
| 3179 | for i in range(len(output_shape)): |
| 3180 | if output_shape[i] == -1: |
| 3181 | output_shape[i] = totalElements // totalOutputElements |
| 3182 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3183 | return ser.addOutput(output_shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3184 | |
| 3185 | @staticmethod |
| 3186 | def sliceOp(ser, a, begin, size): |
| 3187 | |
| 3188 | output_shape = size.copy() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3189 | return ser.addOutput(output_shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3190 | |
| 3191 | @staticmethod |
| 3192 | def tileOp(ser, a, multiples): |
| 3193 | |
| 3194 | output_shape = a.shape.copy() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3195 | assert len(multiples) == len(output_shape) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3196 | |
| 3197 | for i in range(len(output_shape)): |
| 3198 | output_shape[i] = a.shape[i] * multiples[i] |
| 3199 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3200 | return ser.addOutput(output_shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3201 | |
| 3202 | @staticmethod |
| 3203 | def transposeOp(ser, a, perms): |
| 3204 | output_shape = a.shape.copy() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3205 | assert len(perms) == len(output_shape) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3206 | |
| 3207 | for i in range(len(output_shape)): |
| 3208 | output_shape[i] = a.shape[perms[i]] |
| 3209 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3210 | return ser.addOutput(output_shape, a.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3211 | |
| 3212 | @staticmethod |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 3213 | def gatherOp(ser, values, indices): |
| 3214 | assert len(values.shape) == 3 |
| 3215 | assert len(indices.shape) == 2 |
| 3216 | assert values.shape[0] == indices.shape[0] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3217 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 3218 | output_shape = [values.shape[0], indices.shape[1], values.shape[2]] |
| 3219 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3220 | return ser.addOutput(output_shape, values.dtype) |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 3221 | |
| 3222 | @staticmethod |
| 3223 | def scatterOp(ser, values_in, indices, input): |
| 3224 | assert len(values_in.shape) == 3 |
| 3225 | assert len(indices.shape) == 2 |
| 3226 | assert len(input.shape) == 3 |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3227 | assert values_in.shape[0] == indices.shape[0] # N |
| 3228 | assert input.shape[1] == indices.shape[1] # W |
| 3229 | assert values_in.shape[2] == input.shape[2] # C |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 3230 | |
| 3231 | output_shape = values_in.shape |
| 3232 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3233 | return ser.addOutput(output_shape, values_in.dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3234 | |
| 3235 | @staticmethod |
Jeremy Johnson | f54d8a2 | 2021-07-20 16:01:06 +0100 | [diff] [blame] | 3236 | def tableOp(ser, input, table_dtype): |
| 3237 | # Same shape as the input, but dtype dependent on table dtype |
| 3238 | assert table_dtype == DType.INT16 or table_dtype == DType.INT8 |
| 3239 | output_dtype = DType.INT32 if table_dtype == DType.INT16 else DType.INT8 |
| 3240 | return ser.addOutput(input.shape, output_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3241 | |
| 3242 | @staticmethod |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3243 | def resizeOp( |
| 3244 | ser, |
| 3245 | input, |
| 3246 | mode, |
| 3247 | stride, |
| 3248 | offset, |
| 3249 | shift, |
| 3250 | stride_fp, |
| 3251 | offset_fp, |
| 3252 | output_dims, |
| 3253 | input_dtype, |
| 3254 | output_dtype, |
| 3255 | ): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3256 | |
| 3257 | output_dims = [input.shape[0], output_dims[0], output_dims[1], input.shape[3]] |
| 3258 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3259 | return ser.addOutput(output_dims, output_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3260 | |
| 3261 | @staticmethod |
| 3262 | def typeConversionOp(ser, val, out_dtype): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3263 | return ser.addOutput(val.shape, out_dtype) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3264 | |
| 3265 | @staticmethod |
| 3266 | def transposeConv2DOp(ser, ifm, output_shape): |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 3267 | if ifm.dtype == DType.INT8: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3268 | out_dtype = DType.INT32 |
| 3269 | elif ifm.dtype == DType.INT16: |
| 3270 | out_dtype = DType.INT48 |
| 3271 | elif ifm.dtype == DType.FLOAT: |
| 3272 | out_dtype = DType.FLOAT |
| 3273 | else: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3274 | raise Exception("Unsupported input dtype: {}".format(ifm.dtype)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3275 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 3276 | return ser.addOutput(output_shape, out_dtype) |