aboutsummaryrefslogtreecommitdiff
path: root/verif/frameworks/test_gen_utils.py
blob: 2d8e5d60f9e1988a3e09b082f3a85842843d9d31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright (c) 2020-2022, ARM Limited.
# SPDX-License-Identifier: Apache-2.0
from enum import IntEnum
from enum import unique

import tensorflow as tf


# Get a string name for a given shape
def get_shape_str(shape, dtype):
    shape_name = None
    for dim in shape:
        shape_name = (shape_name + "x" + str(dim)) if shape_name else str(dim)

    if dtype == tf.float32:
        shape_name = shape_name + "_f32"
    elif dtype == tf.float16:
        shape_name = shape_name + "_f16"
    elif dtype == tf.int32:
        shape_name = shape_name + "_i32"
    elif dtype == tf.uint32:
        shape_name = shape_name + "_u32"
    elif dtype == tf.bool:
        shape_name = shape_name + "_bool"
    elif dtype == tf.quint8:
        shape_name = shape_name + "_qu8"
    elif dtype == tf.qint8:
        shape_name = shape_name + "_qi8"
    elif dtype == tf.qint16:
        shape_name = shape_name + "_qi16"
    elif dtype == tf.quint16:
        shape_name = shape_name + "_qu16"
    else:
        raise Exception("Unsupported type: {}".format(dtype))

    return shape_name


@unique
class QuantType(IntEnum):
    UNKNOWN = 0
    ALL_I8 = 1
    ALL_U8 = 2
    ALL_I16 = 3
    # TODO: support QUINT16
    CONV_U8_U8 = 4
    CONV_I8_I8 = 5
    CONV_I8_I4 = 6
    CONV_I16_I8 = 7


def get_tf_dtype(quantized_inference_dtype):
    if quantized_inference_dtype == QuantType.ALL_I8:
        return tf.qint8
    elif quantized_inference_dtype == QuantType.ALL_U8:
        return tf.quint8
    elif quantized_inference_dtype == QuantType.ALL_I16:
        return tf.qint16
    elif quantized_inference_dtype == QuantType.CONV_U8_U8:
        return tf.quint8
    elif quantized_inference_dtype == QuantType.CONV_I8_I8:
        return tf.qint8
    elif quantized_inference_dtype == QuantType.CONV_I8_I4:
        return tf.qint8
    elif quantized_inference_dtype == QuantType.CONV_I16_I8:
        return tf.qint16
    else:
        return None


class TensorScale:
    def __init__(self, _min, _max, _num_bits, _narrow_range):
        self.min = _min
        self.max = _max
        self.num_bits = _num_bits
        self.narrow_range = _narrow_range