aboutsummaryrefslogtreecommitdiff
path: root/tests/test_tosa_checker.py
blob: 52378ee1e603a7dddd0092bee78df9eaa4feaa27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import tensorflow as tf
import tempfile
import tosa_checker


@pytest.fixture(scope="module")
def build_tosa_non_compat_model():
    num_boxes = 6
    max_output_size = 5
    iou_threshold = 0.5
    score_threshold = 0.1

    def non_max_suppression(x):
        boxes = x[0]
        scores = x[1]
        output = tf.image.non_max_suppression_with_scores(
            boxes[0],
            scores[0],
            max_output_size=max_output_size,
            iou_threshold=iou_threshold,
            score_threshold=score_threshold,
            soft_nms_sigma=1.0,
        )
        return output

    boxes_in = tf.keras.layers.Input(
        shape=(num_boxes, 4), batch_size=1, dtype=tf.float32, name="boxes"
    )
    scores_in = tf.keras.layers.Input(
        shape=(num_boxes), batch_size=1, dtype=tf.float32, name="scores"
    )
    outputs = tf.keras.layers.Lambda(non_max_suppression, name="nms")(
        [boxes_in, scores_in]
    )
    model = tf.keras.models.Model(inputs=[boxes_in, scores_in], outputs=outputs)

    return model


@pytest.fixture(scope="module")
def build_tosa_non_compat_model_custom_op():
    @tf.function(
        experimental_implements='name: "exp_log" \
                                attr { \
                                    key: "tfl_fusable_op" \
                                    value { b: true } \
                                }'
    )
    def exp_log(x):
        x = tf.math.exp(x)
        x = tf.math.log(x)

        return x

    input = tf.keras.layers.Input(shape=(16,), name="input")
    x = tf.keras.layers.Lambda(exp_log, name="exp_log")(input)
    x = tf.keras.layers.Dense(8, activation="relu", name="dense")(x)
    model = tf.keras.models.Model(inputs=[input], outputs=x)

    return model


@pytest.fixture(scope="module")
def build_tosa_compat_model():
    input = tf.keras.layers.Input(shape=(16,), name="input")
    x = tf.keras.layers.Dense(8, activation="relu", name="dense")(input)
    model = tf.keras.models.Model(inputs=[input], outputs=x)
    return model


def create_tflite(model):
    converter = tf.lite.TFLiteConverter.from_keras_model(model)
    tflite_model = converter.convert()
    return tflite_model


@pytest.fixture(scope="module")
def non_compat_file(build_tosa_non_compat_model):
    tflite_model = create_tflite(build_tosa_non_compat_model)
    with tempfile.TemporaryDirectory() as tmp_dir:
        file = os.path.join(tmp_dir, "test.tflite")
        open(file, "wb").write(tflite_model)
        yield file


@pytest.fixture(scope="module")
def non_compat_file_custom_op(build_tosa_non_compat_model_custom_op):
    tflite_model = create_tflite(build_tosa_non_compat_model_custom_op)
    with tempfile.TemporaryDirectory() as tmp_dir:
        file = os.path.join(tmp_dir, "test.tflite")
        open(file, "wb").write(tflite_model)
        yield file


@pytest.fixture(scope="module")
def compat_file(build_tosa_compat_model):
    tflite_model = create_tflite(build_tosa_compat_model)
    with tempfile.TemporaryDirectory() as tmp_dir:
        file = os.path.join(tmp_dir, "test.tflite")
        open(file, "wb").write(tflite_model)
        yield file


class TestTosaCompatibilityTool:
    def test_bad_tflite_file(self):
        make_bad_tfile = os.path.join(tempfile.mkdtemp(), "test.tflite")
        open(make_bad_tfile, "wb").write("bad tflite file".encode("ASCII"))

        with pytest.raises(RuntimeError):
            checker = tosa_checker.TOSAChecker(model_path=make_bad_tfile)

    def test_tosa_non_compat_model(self, non_compat_file):
        checker = tosa_checker.TOSAChecker(model_path=non_compat_file)
        tosa_compatible = checker.is_tosa_compatible()
        assert tosa_compatible == False

        ops = checker._get_tosa_compatibility_for_ops()
        assert type(ops) == list
        assert [[op.name, op.is_tosa_compatible] for op in ops] == [
            ["tfl.pseudo_const", True],
            ["tfl.pseudo_const", True],
            ["tfl.pseudo_const", True],
            ["tfl.strided_slice", True],
            ["tfl.pseudo_const", True],
            ["tfl.pseudo_const", True],
            ["tfl.pseudo_const", True],
            ["tfl.strided_slice", True],
            ["tfl.pseudo_const", True],
            ["tfl.pseudo_const", True],
            ["tfl.pseudo_const", True],
            ["tfl.pseudo_const", True],
            ["tfl.non_max_suppression_v5", False],
        ]

        tosa_ops = checker._get_used_tosa_ops()
        assert type(tosa_ops) == list
        assert [[op.name, op.is_tosa_compatible] for op in tosa_ops] == [
            ["tosa.const", True],
            ["tosa.const", True],
            ["tosa.const", True],
            ["tosa.const", True],
            ["tosa.reshape", True],
            ["tosa.reshape", True],
        ]

    def test_tosa_non_compat_model_with_custom_op(self, non_compat_file_custom_op):
        checker = tosa_checker.TOSAChecker(model_path=non_compat_file_custom_op)
        tosa_compatible = checker.is_tosa_compatible()
        assert tosa_compatible == False

        ops = checker._get_tosa_compatibility_for_ops()
        assert type(ops) == list
        assert [[op.name, op.is_tosa_compatible] for op in ops] == [
            ["tfl.custom", False],
            ["tfl.pseudo_const", True],
            ["tfl.no_value", True],
            ["tfl.fully_connected", True],
        ]

        tosa_ops = checker._get_used_tosa_ops()
        assert type(tosa_ops) == list
        assert [[op.name, op.is_tosa_compatible] for op in tosa_ops] == [
            ["tosa.const", True],
            ["tosa.const", True],
            ["tosa.custom", False],
            ["tosa.fully_connected", True],
            ["tosa.clamp", True],
        ]

    def test_tosa_compat_model(self, compat_file):
        checker = tosa_checker.TOSAChecker(model_path=compat_file)
        tosa_compatible = checker.is_tosa_compatible()
        assert tosa_compatible == True

        ops = checker._get_tosa_compatibility_for_ops()
        assert type(ops) == list
        assert [[op.name, op.is_tosa_compatible] for op in ops] == [
            ["tfl.pseudo_const", True],
            ["tfl.no_value", True],
            ["tfl.fully_connected", True],
        ]

        tosa_ops = checker._get_used_tosa_ops()
        assert type(tosa_ops) == list
        assert [[op.name, op.is_tosa_compatible] for op in tosa_ops] == [
            ["tosa.const", True],
            ["tosa.const", True],
            ["tosa.fully_connected", True],
            ["tosa.clamp", True],
        ]

    def test_tosa_non_compat_model_mlir_representation(self, non_compat_file):
        checker = tosa_checker.TOSAChecker(model_path=non_compat_file)

        tfl_mlir_representation = checker._get_mlir_model_representation(
            elide_large_elements_attrs=True
        )
        # TODO Use regular expression to make the test more robust or parse the MLIR module
        expected_mlir_representation = """\
module attributes {tf_saved_model.semantics, tfl.description = "MLIR Converted.", tfl.schema_version = 3 : i32} {
  func @main(%arg0: tensor<1x6x4xf32> {tf_saved_model.index_path = ["boxes"]}, %arg1: tensor<1x6xf32> {tf_saved_model.index_path = ["scores"]}) -> (tensor<?xf32> {tf_saved_model.index_path = ["nms_1"]}, tensor<?xi32> {tf_saved_model.index_path = ["nms"]}) attributes {tf.entry_function = {inputs = "serving_default_boxes:0,serving_default_scores:0", outputs = "PartitionedCall:1,PartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} {
    %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<3xi32>} : () -> tensor<3xi32>
    %1 = "tfl.pseudo_const"() {value = dense<[1, 6, 4]> : tensor<3xi32>} : () -> tensor<3xi32>
    %2 = "tfl.pseudo_const"() {value = dense<1> : tensor<3xi32>} : () -> tensor<3xi32>
    %3 = "tfl.strided_slice"(%arg0, %0, %1, %2) {begin_mask = 6 : i32, ellipsis_mask = 0 : i32, end_mask = 6 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 1 : i32} : (tensor<1x6x4xf32>, tensor<3xi32>, tensor<3xi32>, tensor<3xi32>) -> tensor<6x4xf32>
    %4 = "tfl.pseudo_const"() {value = dense<0> : tensor<2xi32>} : () -> tensor<2xi32>
    %5 = "tfl.pseudo_const"() {value = dense<[1, 6]> : tensor<2xi32>} : () -> tensor<2xi32>
    %6 = "tfl.pseudo_const"() {value = dense<1> : tensor<2xi32>} : () -> tensor<2xi32>
    %7 = "tfl.strided_slice"(%arg1, %4, %5, %6) {begin_mask = 2 : i32, ellipsis_mask = 0 : i32, end_mask = 2 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 1 : i32} : (tensor<1x6xf32>, tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<6xf32>
    %8 = "tfl.pseudo_const"() {value = dense<5> : tensor<i32>} : () -> tensor<i32>
    %9 = "tfl.pseudo_const"() {value = dense<5.000000e-01> : tensor<f32>} : () -> tensor<f32>
    %10 = "tfl.pseudo_const"() {value = dense<1.000000e-01> : tensor<f32>} : () -> tensor<f32>
    %11 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<f32>} : () -> tensor<f32>
    %selected_indices, %selected_scores, %valid_outputs = "tfl.non_max_suppression_v5"(%3, %7, %8, %9, %10, %11) : (tensor<6x4xf32>, tensor<6xf32>, tensor<i32>, tensor<f32>, tensor<f32>, tensor<f32>) -> (tensor<?xi32>, tensor<?xf32>, tensor<*xi32>)
    return %selected_scores, %selected_indices : tensor<?xf32>, tensor<?xi32>
  }
}
"""
        assert tfl_mlir_representation == expected_mlir_representation

        tosa_mlir_representation = checker._get_mlir_tosa_model_representation(
            elide_large_elements_attrs=True
        )
        expected_tosa_mlir_representation = """\
module attributes {tf_saved_model.semantics, tfl.description = "MLIR Converted.", tfl.schema_version = 3 : i32} {
  func @main(%arg0: tensor<1x6x4xf32> {tf_saved_model.index_path = ["boxes"]}, %arg1: tensor<1x6xf32> {tf_saved_model.index_path = ["scores"]}) -> (tensor<?xf32> {tf_saved_model.index_path = ["nms_1"]}, tensor<?xi32> {tf_saved_model.index_path = ["nms"]}) attributes {tf.entry_function = {inputs = "serving_default_boxes:0,serving_default_scores:0", outputs = "PartitionedCall:1,PartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} {
    %0 = "tosa.const"() {value = dense<5> : tensor<i32>} : () -> tensor<i32>
    %1 = "tosa.const"() {value = dense<5.000000e-01> : tensor<f32>} : () -> tensor<f32>
    %2 = "tosa.const"() {value = dense<1.000000e-01> : tensor<f32>} : () -> tensor<f32>
    %3 = "tosa.const"() {value = dense<1.000000e+00> : tensor<f32>} : () -> tensor<f32>
    %4 = "tosa.reshape"(%arg0) {new_shape = [6, 4]} : (tensor<1x6x4xf32>) -> tensor<6x4xf32>
    %5 = "tosa.reshape"(%arg1) {new_shape = [6]} : (tensor<1x6xf32>) -> tensor<6xf32>
    %selected_indices, %selected_scores, %valid_outputs = "tfl.non_max_suppression_v5"(%4, %5, %0, %1, %2, %3) : (tensor<6x4xf32>, tensor<6xf32>, tensor<i32>, tensor<f32>, tensor<f32>, tensor<f32>) -> (tensor<?xi32>, tensor<?xf32>, tensor<*xi32>)
    return %selected_scores, %selected_indices : tensor<?xf32>, tensor<?xi32>
  }
}
"""
        assert tosa_mlir_representation == expected_tosa_mlir_representation

    def test_tosa_compat_model_mlir_representation(self, compat_file):
        checker = tosa_checker.TOSAChecker(model_path=compat_file)
        tfl_mlir_representation = checker._get_mlir_model_representation(
            elide_large_elements_attrs=True
        )
        expected_mlir_representation = """\
module attributes {tf_saved_model.semantics, tfl.description = "MLIR Converted.", tfl.schema_version = 3 : i32} {
  func @main(%arg0: tensor<?x16xf32> {tf_saved_model.index_path = ["input"]}) -> (tensor<?x8xf32> {tf_saved_model.index_path = ["dense"]}) attributes {tf.entry_function = {inputs = "serving_default_input:0", outputs = "StatefulPartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} {
    %0 = "tfl.pseudo_const"() {value = opaque<"elided_large_const", "0xDEADBEEF"> : tensor<8x16xf32>} : () -> tensor<8x16xf32>
    %1 = "tfl.no_value"() {value} : () -> none
    %2 = "tfl.fully_connected"(%arg0, %0, %1) {asymmetric_quantize_inputs = false, fused_activation_function = "RELU", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<?x16xf32>, tensor<8x16xf32>, none) -> tensor<?x8xf32>
    return %2 : tensor<?x8xf32>
  }
}
"""
        assert tfl_mlir_representation == expected_mlir_representation

        tosa_mlir_representation = checker._get_mlir_tosa_model_representation(
            elide_large_elements_attrs=True
        )
        expected_tosa_mlir_representation = """\
module attributes {tf_saved_model.semantics, tfl.description = "MLIR Converted.", tfl.schema_version = 3 : i32} {
  func @main(%arg0: tensor<?x16xf32> {tf_saved_model.index_path = ["input"]}) -> (tensor<?x8xf32> {tf_saved_model.index_path = ["dense"]}) attributes {tf.entry_function = {inputs = "serving_default_input:0", outputs = "StatefulPartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} {
    %0 = "tosa.const"() {value = opaque<"elided_large_const", "0xDEADBEEF"> : tensor<8x16xf32>} : () -> tensor<8x16xf32>
    %1 = "tosa.const"() {value = dense<0.000000e+00> : tensor<8xf32>} : () -> tensor<8xf32>
    %2 = "tosa.fully_connected"(%arg0, %0, %1) : (tensor<?x16xf32>, tensor<8x16xf32>, tensor<8xf32>) -> tensor<?x8xf32>
    %3 = "tosa.clamp"(%2) {max_fp = 3.40282347E+38 : f32, max_int = 2147483647 : i64, min_fp = 0.000000e+00 : f32, min_int = 0 : i64} : (tensor<?x8xf32>) -> tensor<?x8xf32>
    return %3 : tensor<?x8xf32>
  }
}
"""
        assert tosa_mlir_representation == expected_tosa_mlir_representation