summaryrefslogtreecommitdiff
path: root/tests/nnapi/specs/V1_2/topk_v2.mod.py
blob: 189b9907dd09c65fbdf80e9573dc2586f9b837b6 (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
#
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import collections

TestCase = collections.namedtuple("TestCase", [
    "inp", "inp_data", "k", "out_values", "out_values_data", "out_indices",
    "out_indices_data"
])

test_cases = [
    TestCase(
        inp=Input("input", "TENSOR_FLOAT32", "{2, 2}"),
        inp_data=[-2.0, 0.2, 0.8, 0.1],
        k=Int32Scalar("k", 2),
        out_values=Output("out_values", "TENSOR_FLOAT32", "{2, 2}"),
        out_values_data=[0.2, -2.0, 0.8, 0.1],
        out_indices=Output("out_indices", "TENSOR_INT32", "{2, 2}"),
        out_indices_data=[1, 0, 0, 1]),
    TestCase(
        inp=Input("input", "TENSOR_FLOAT32", "{2, 3}"),
        inp_data=[-2.0, -3.0, 0.2, 0.8, 0.1, -0.1],
        k=Int32Scalar("k", 2),
        out_values=Output("out_values", "TENSOR_FLOAT32", "{2, 2}"),
        out_values_data=[0.2, -2.0, 0.8, 0.1],
        out_indices=Output("out_indices", "TENSOR_INT32", "{2, 2}"),
        out_indices_data=[2, 0, 0, 1]),
    TestCase(
        inp=Input("input", "TENSOR_FLOAT32", "{2, 4}"),
        inp_data=[-2.0, -3.0, -4.0, 0.2, 0.8, 0.1, -0.1, -0.8],
        k=Int32Scalar("k", 2),
        out_values=Output("out_values", "TENSOR_FLOAT32", "{2, 2}"),
        out_values_data=[0.2, -2.0, 0.8, 0.1],
        out_indices=Output("out_indices", "TENSOR_INT32", "{2, 2}"),
        out_indices_data=[3, 0, 0, 1]),
    TestCase(
        inp=Input("input", "TENSOR_FLOAT32", "{8}"),
        inp_data=[-2.0, -3.0, -4.0, 0.2, 0.8, 0.1, -0.1, -0.8],
        k=Int32Scalar("k", 2),
        out_values=Output("out_values", "TENSOR_FLOAT32", "{2}"),
        out_values_data=[0.8, 0.2],
        out_indices=Output("out_indices", "TENSOR_INT32", "{2}"),
        out_indices_data=[4, 3]),
    TestCase(
        inp=Input("input", "TENSOR_QUANT8_ASYMM", "{2, 3}, 2.0, 128"),
        inp_data=[1, 2, 3, 251, 250, 249],
        k=Int32Scalar("k", 2),
        out_values=Output("out_values", "TENSOR_QUANT8_ASYMM", "{2, 2}, 2.0, 128"),
        out_values_data=[3, 2, 251, 250],
        out_indices=Output("out_indices", "TENSOR_INT32", "{2, 2}"),
        out_indices_data=[2, 1, 0, 1]),
    TestCase(
        inp=Input("input", "TENSOR_INT32", "{2, 3}"),
        inp_data=[1, 2, 3, 10251, 10250, 10249],
        k=Int32Scalar("k", 2),
        out_values=Output("out_values", "TENSOR_INT32", "{2, 2}"),
        out_values_data=[3, 2, 10251, 10250],
        out_indices=Output("out_indices", "TENSOR_INT32", "{2, 2}"),
        out_indices_data=[2, 1, 0, 1]),
]

for test_case in test_cases:
  model = Model().Operation("TOPK_V2", test_case.inp, test_case.k).To(
      test_case.out_values, test_case.out_indices)
  Example({
      test_case.inp: test_case.inp_data,
      test_case.out_values: test_case.out_values_data,
      test_case.out_indices: test_case.out_indices_data
  }, model=model).AddVariations("relaxed", "float16")