Won Jeon | 64e4bfe | 2024-01-18 06:31:55 +0000 | [diff] [blame] | 1 | # Copyright (c) 2021-2024, ARM Limited. |
Jeremy Johnson | 9a66abb | 2022-04-07 11:29:20 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: Apache-2.0 |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 3 | import struct |
| 4 | import sys |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 5 | from enum import IntEnum |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 6 | |
| 7 | import numpy as np |
Jeremy Johnson | 9a66abb | 2022-04-07 11:29:20 +0100 | [diff] [blame] | 8 | from tosa.DType import DType |
| 9 | |
Jeremy Johnson | a0e03f3 | 2022-06-13 17:48:09 +0100 | [diff] [blame] | 10 | # Maximum dimension size for output and inputs for RESIZE |
| 11 | MAX_RESIZE_DIMENSION = 16384 |
| 12 | |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 13 | # Data type information dictionary |
| 14 | # - str: filename abbreviation |
| 15 | # - width: number of bytes needed for type |
| 16 | # - json: JSON type string |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 17 | DTYPE_ATTRIBUTES = { |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 18 | DType.BOOL: {"str": "b", "width": 1, "json": "BOOL"}, |
| 19 | DType.INT4: {"str": "i4", "width": 4, "json": "INT4"}, |
| 20 | DType.INT8: {"str": "i8", "width": 8, "json": "INT8"}, |
| 21 | DType.UINT8: {"str": "u8", "width": 8, "json": "UINT8"}, |
| 22 | DType.INT16: {"str": "i16", "width": 16, "json": "INT16"}, |
| 23 | DType.UINT16: {"str": "u16", "width": 16, "json": "UINT16"}, |
| 24 | DType.INT32: {"str": "i32", "width": 32, "json": "INT32"}, |
| 25 | DType.INT48: {"str": "i48", "width": 48, "json": "INT48"}, |
| 26 | DType.SHAPE: {"str": "s", "width": 64, "json": "SHAPE"}, |
| 27 | DType.FP16: {"str": "f16", "width": 16, "json": "FP16"}, |
| 28 | DType.BF16: {"str": "bf16", "width": 16, "json": "BF16"}, |
| 29 | DType.FP32: {"str": "f32", "width": 32, "json": "FP32"}, |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 30 | DType.FP8E4M3: {"str": "f8e4m3", "width": 8, "json": "FP8E4M3"}, |
| 31 | DType.FP8E5M2: {"str": "f8e5m2", "width": 8, "json": "FP8E5M2"}, |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Jeremy Johnson | 9a66abb | 2022-04-07 11:29:20 +0100 | [diff] [blame] | 34 | |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 35 | class ComplianceMode(IntEnum): |
| 36 | """Compliance mode types.""" |
| 37 | |
| 38 | EXACT = 0 |
| 39 | DOT_PRODUCT = 1 |
| 40 | ULP = 2 |
| 41 | FP_SPECIAL = 3 |
| 42 | REDUCE_PRODUCT = 4 |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 43 | ABS_ERROR = 5 |
Jeremy Johnson | 32d0b5a | 2024-02-01 15:54:07 +0000 | [diff] [blame] | 44 | RELATIVE = 6 |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 45 | |
| 46 | |
| 47 | class DataGenType(IntEnum): |
| 48 | """Data generator types.""" |
| 49 | |
| 50 | PSEUDO_RANDOM = 0 |
| 51 | DOT_PRODUCT = 1 |
| 52 | OP_BOUNDARY = 2 |
| 53 | OP_FULLSET = 3 |
| 54 | OP_SPECIAL = 4 |
Won Jeon | 64e4bfe | 2024-01-18 06:31:55 +0000 | [diff] [blame] | 55 | FIXED_DATA = 5 |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 56 | |
| 57 | |
Jeremy Johnson | 65ba809 | 2023-10-09 16:31:13 +0100 | [diff] [blame] | 58 | def dtypeIsSupportedByCompliance(dtype): |
| 59 | """Types supported by the new data generation and compliance flow.""" |
Jeremy Johnson | d1a08ce | 2023-10-18 17:22:21 +0100 | [diff] [blame] | 60 | if isinstance(dtype, list) or isinstance(dtype, tuple): |
| 61 | dtype = dtype[0] |
Jeremy Johnson | fc4bde9 | 2024-01-25 12:53:21 +0000 | [diff] [blame] | 62 | return dtype in (DType.FP32, DType.FP16) |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 63 | |
| 64 | |
Jeremy Johnson | d1a08ce | 2023-10-18 17:22:21 +0100 | [diff] [blame] | 65 | def getOpNameFromOpListName(opName): |
| 66 | """Get the op name from a TOSA_OP_LIST name that can have suffixes.""" |
| 67 | for name in ("conv2d", "depthwise_conv2d", "transpose_conv2d", "conv3d"): |
| 68 | if opName.startswith(name): |
| 69 | return name |
| 70 | return opName |
| 71 | |
| 72 | |
Jeremy Johnson | 9a66abb | 2022-04-07 11:29:20 +0100 | [diff] [blame] | 73 | def valueToName(item, value): |
| 74 | """Get the name of an attribute with the given value. |
| 75 | |
| 76 | This convenience function is needed to print meaningful names for |
| 77 | the values of the tosa.Op.Op and tosa.DType.DType classes. |
| 78 | This would not be necessary if they were subclasses of Enum, or |
| 79 | IntEnum, which, sadly, they are not. |
| 80 | |
| 81 | Args: |
| 82 | item: The class, or object, to find the value in |
| 83 | value: The value to find |
| 84 | |
| 85 | Example, to get the name of a DType value: |
| 86 | |
| 87 | name = valueToName(DType, DType.INT8) # returns 'INT8' |
| 88 | name = valueToName(DType, 4) # returns 'INT8' |
| 89 | |
| 90 | Returns: |
| 91 | The name of the first attribute found with a matching value, |
| 92 | |
| 93 | Raises: |
| 94 | ValueError if the value is not found |
| 95 | """ |
| 96 | for attr in dir(item): |
| 97 | if getattr(item, attr) == value: |
| 98 | return attr |
| 99 | raise ValueError(f"value ({value}) not found") |
| 100 | |
| 101 | |
| 102 | def allDTypes(*, excludes=None): |
| 103 | """Get a set of all DType values, optionally excluding some values. |
| 104 | |
| 105 | This convenience function is needed to provide a sequence of DType values. |
| 106 | This would be much easier if DType was a subclass of Enum, or IntEnum, |
| 107 | as we could then iterate over the values directly, instead of using |
| 108 | dir() to find the attributes and then check if they are what we want. |
| 109 | |
| 110 | Args: |
| 111 | excludes: iterable of DTYPE values (e.g. [DType.INT8, DType.BOOL]) |
| 112 | |
| 113 | Returns: |
| 114 | A set of DType values |
| 115 | """ |
| 116 | excludes = () if not excludes else excludes |
| 117 | return { |
| 118 | getattr(DType, t) |
| 119 | for t in dir(DType) |
| 120 | if not callable(getattr(DType, t)) |
| 121 | and not t.startswith("__") |
| 122 | and getattr(DType, t) not in excludes |
| 123 | } |
| 124 | |
| 125 | |
| 126 | def usableDTypes(*, excludes=None): |
| 127 | """Get a set of usable DType values, optionally excluding some values. |
| 128 | |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 129 | Excludes uncommon types (DType.UNKNOWN, DType.UINT16, DType.UINT8) in |
| 130 | addition to the excludes specified by the caller, as the serializer lib |
| 131 | does not support them. |
| 132 | If you wish to include 'UNKNOWN', 'UINT8' or 'UINT16' use allDTypes |
| 133 | instead. |
Jeremy Johnson | 9a66abb | 2022-04-07 11:29:20 +0100 | [diff] [blame] | 134 | |
| 135 | Args: |
| 136 | excludes: iterable of DType values (e.g. [DType.INT8, DType.BOOL]) |
| 137 | |
| 138 | Returns: |
| 139 | A set of DType values |
| 140 | """ |
Jeremy Johnson | 0633c3a | 2023-08-22 16:55:08 +0100 | [diff] [blame] | 141 | omit = {DType.UNKNOWN, DType.UINT8, DType.UINT16, DType.SHAPE} |
Jeremy Johnson | 9a66abb | 2022-04-07 11:29:20 +0100 | [diff] [blame] | 142 | omit.update(excludes if excludes else ()) |
| 143 | return allDTypes(excludes=omit) |
| 144 | |
| 145 | |
| 146 | def product(shape): |
| 147 | value = 1 |
| 148 | for n in shape: |
| 149 | value *= n |
| 150 | return value |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 151 | |
| 152 | |
| 153 | def get_accum_dtype_from_tgTypes(dtypes): |
| 154 | # Get accumulate data-type from the test generator's defined types |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 155 | assert isinstance(dtypes, list) or isinstance(dtypes, tuple) |
| 156 | return dtypes[-1] |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 157 | |
| 158 | |
| 159 | def get_wrong_output_type(op_name, rng, input_dtype): |
| 160 | if op_name == "fully_connected" or op_name == "matmul": |
| 161 | if input_dtype == DType.INT8: |
| 162 | incorrect_types = ( |
| 163 | DType.INT4, |
| 164 | DType.INT8, |
| 165 | DType.INT16, |
| 166 | DType.INT48, |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 167 | DType.FP32, |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 168 | DType.FP16, |
| 169 | ) |
| 170 | elif input_dtype == DType.INT16: |
| 171 | incorrect_types = ( |
| 172 | DType.INT4, |
| 173 | DType.INT8, |
| 174 | DType.INT16, |
| 175 | DType.INT32, |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 176 | DType.FP32, |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 177 | DType.FP16, |
| 178 | ) |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 179 | elif ( |
| 180 | input_dtype == DType.FP32 |
| 181 | or input_dtype == DType.FP16 |
| 182 | or input_dtype == DType.BF16 |
| 183 | ): |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 184 | incorrect_types = ( |
| 185 | DType.INT4, |
| 186 | DType.INT8, |
| 187 | DType.INT16, |
| 188 | DType.INT32, |
| 189 | DType.INT48, |
| 190 | ) |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 191 | elif input_dtype == DType.FP8E4M3 or input_dtype == DType.FP8E5M2: |
| 192 | incorrect_types = ( |
| 193 | DType.INT4, |
| 194 | DType.INT8, |
| 195 | DType.INT16, |
| 196 | DType.INT32, |
| 197 | DType.INT48, |
| 198 | DType.FP32, |
| 199 | DType.BF16, |
| 200 | ) |
Jeremy Johnson | 05c711e | 2022-12-12 18:00:41 +0000 | [diff] [blame] | 201 | else: |
| 202 | # Assume all types but the input type are incorrect |
| 203 | incorrect_types = list(usableDTypes(excludes=(input_dtype,))) |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 204 | return rng.choice(a=incorrect_types) |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 205 | |
| 206 | |
Luke Hutton | a4e48ca | 2023-02-22 11:53:48 +0000 | [diff] [blame] | 207 | def get_rank_mismatch_shape(rng, output_shape): |
| 208 | """ |
| 209 | Extends the rank of the provided output_shape by |
| 210 | an arbitrary amount but ensures the total element |
| 211 | count remains the same. |
| 212 | """ |
| 213 | rank_modifier = rng.choice([1, 2, 3]) |
| 214 | output_shape += [1] * rank_modifier |
| 215 | return output_shape |
| 216 | |
| 217 | |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 218 | def float32_is_valid_bfloat16(f): |
| 219 | """Return True if float value is valid bfloat16.""" |
| 220 | f32_bits = get_float32_bitstring(f) |
| 221 | return f32_bits[16:] == "0" * 16 |
| 222 | |
| 223 | |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 224 | def float32_is_valid_float8(f): |
| 225 | """Return True if float value is valid float8.""" |
| 226 | f32_bits = get_float32_bitstring(f) |
| 227 | return f32_bits[8:] == "0" * 24 |
| 228 | |
| 229 | |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 230 | def get_float32_bitstring(f): |
| 231 | """Return a big-endian string of bits representing a 32 bit float.""" |
| 232 | f32_bits_as_int = struct.unpack(">L", struct.pack(">f", f))[0] |
| 233 | return f"{f32_bits_as_int:032b}" |
| 234 | |
| 235 | |
| 236 | def float32_to_bfloat16(f): |
| 237 | """Turns fp32 value into bfloat16 by flooring. |
| 238 | |
| 239 | Floors the least significant 16 bits of the input |
| 240 | fp32 value and returns this valid bfloat16 representation as fp32. |
| 241 | For simplicity during bit-wrangling, ignores underlying system |
| 242 | endianness and interprets as big-endian. |
| 243 | Returns a bf16-valid float following system's native byte order. |
| 244 | """ |
| 245 | f32_bits = get_float32_bitstring(f) |
| 246 | f32_floored_bits = f32_bits[:16] + "0" * 16 |
| 247 | |
| 248 | # Assume sys.byteorder matches system's underlying float byteorder |
| 249 | fp_bytes = int(f32_floored_bits, 2).to_bytes(4, byteorder=sys.byteorder) |
| 250 | return struct.unpack("@f", fp_bytes)[0] # native byteorder |
| 251 | |
| 252 | |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 253 | def float32_to_fp8e4m3(f): |
| 254 | """Turns fp32 value into fp8e4m3""" |
| 255 | f32_bits = get_float32_bitstring(f) |
| 256 | fp8_bits = f32_bits[0] + f32_bits[1:5] + f32_bits[9:12] + "0" * 24 |
| 257 | fp_bytes = int(fp8_bits, 2).to_bytes(4, byteorder=sys.byteorder) |
| 258 | return struct.unpack("@f", fp_bytes)[0] # native byteorder |
| 259 | |
| 260 | |
| 261 | def float32_to_fp8e5m2(f): |
| 262 | """Turns fp32 value into fp8e5m2""" |
| 263 | f32_bits = get_float32_bitstring(f) |
| 264 | fp8_bits = f32_bits[0] + f32_bits[1:6] + f32_bits[9:11] + "0" * 24 |
| 265 | fp_bytes = int(fp8_bits, 2).to_bytes(4, byteorder=sys.byteorder) |
| 266 | return struct.unpack("@f", fp_bytes)[0] |
| 267 | |
| 268 | |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 269 | vect_f32_to_bf16 = np.vectorize( |
| 270 | float32_to_bfloat16, otypes=(np.float32,) |
| 271 | ) # NumPy vectorize: applies function to vector faster than looping |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 272 | |
| 273 | vect_f32_to_fp8e4m3 = np.vectorize( |
| 274 | float32_to_fp8e4m3, otypes=(np.float32,) |
| 275 | ) # NumPy vectorize: applies function to vector faster than looping |
| 276 | |
| 277 | vect_f32_to_fp8e5m2 = np.vectorize( |
| 278 | float32_to_fp8e5m2, otypes=(np.float32,) |
| 279 | ) # Numpy vectorize: applies function to vector faster than looping |