summaryrefslogtreecommitdiff
path: root/tests/nnapi/specs/V1_2/cast.mod.py
blob: f1d93ce7dd40c021d2b2f68e8e8a8deaa7367c6a (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
#
# 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 itertools
import collections

Operand = collections.namedtuple(
    "Operand", ["name", "as_input", "as_output", "data", "supports_relaxation"])

operands = [
    Operand(
        name="float16",
        as_input=Input("input0", "TENSOR_FLOAT16", "{2, 3}"),
        as_output=Output("output0", "TENSOR_FLOAT16", "{2, 3}"),
        data=[1, 2, 3, 4, 5, 6],
        supports_relaxation=False),
    Operand(
        name="float32",
        as_input=Input("input0", "TENSOR_FLOAT32", "{2, 3}"),
        as_output=Output("output0", "TENSOR_FLOAT32", "{2, 3}"),
        data=[1, 2, 3, 4, 5, 6],
        supports_relaxation=True),
    Operand(
        name="int32",
        as_input=Input("input0", "TENSOR_INT32", "{2, 3}"),
        as_output=Output("output0", "TENSOR_INT32", "{2, 3}"),
        data=[1, 2, 3, 4, 5, 6],
        supports_relaxation=False),
    Operand(
        name="quant8",
        as_input=Input("input0", "TENSOR_QUANT8_ASYMM", "{2, 3}, 4.0, 100"),
        as_output=Output("output0", "TENSOR_QUANT8_ASYMM", "{2, 3}, 4.0, 100"),
        data=[1, 2, 3, 4, 5, 6],
        supports_relaxation=False),
]

for operand1, operand2 in itertools.product(operands, operands):
  input0 = operand1.as_input
  output0 = operand2.as_output

  model = Model().Operation("CAST", input0).To(output0)

  example = Example({
      input0: operand1.data,
      output0: operand2.data,
  }, model=model, name='{}_to_{}'.format(operand1.name, operand2.name))

  if operand1.supports_relaxation or operand2.supports_relaxation:
    example.AddRelaxed()


# Test overflow and underflow.
operands = [
    Operand(
        name="float16",
        as_input=Input("input0", "TENSOR_FLOAT16", "{2}"),
        as_output=None,
        data=[-1, 256],
        supports_relaxation=False),
    Operand(
        name="float32",
        as_input=Input("input0", "TENSOR_FLOAT32", "{2}"),
        as_output=None,
        data=[-1, 256],
        supports_relaxation=True),
    Operand(
        name="int32",
        as_input=Input("input0", "TENSOR_INT32", "{2}"),
        as_output=None,
        data=[-1, 256],
        supports_relaxation=False),
]

for operand1 in operands:
  input0 = operand1.as_input
  output0 = Output("output0", "TENSOR_QUANT8_ASYMM", "{2}, 4.0, 100")

  model = Model().Operation("CAST", input0).To(output0)

  example = Example({
      input0: operand1.data,
      output0: [0, 255],
  }, model=model, name='{}_to_quant8_overflow'.format(operand1.name))

  if operand1.supports_relaxation:
    example.AddRelaxed()