• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import itertools
18import collections
19
20Operand = collections.namedtuple(
21    "Operand", ["name", "as_input", "as_output", "data", "supports_relaxation"])
22
23operands = [
24    Operand(
25        name="float16",
26        as_input=Input("input0", "TENSOR_FLOAT16", "{2, 3}"),
27        as_output=Output("output0", "TENSOR_FLOAT16", "{2, 3}"),
28        data=[1, 2, 3, 4, 5, 6],
29        supports_relaxation=False),
30    Operand(
31        name="float32",
32        as_input=Input("input0", "TENSOR_FLOAT32", "{2, 3}"),
33        as_output=Output("output0", "TENSOR_FLOAT32", "{2, 3}"),
34        data=[1, 2, 3, 4, 5, 6],
35        supports_relaxation=True),
36    Operand(
37        name="int32",
38        as_input=Input("input0", "TENSOR_INT32", "{2, 3}"),
39        as_output=Output("output0", "TENSOR_INT32", "{2, 3}"),
40        data=[1, 2, 3, 4, 5, 6],
41        supports_relaxation=False),
42    Operand(
43        name="quant8",
44        as_input=Input("input0", "TENSOR_QUANT8_ASYMM", "{2, 3}, 4.0, 100"),
45        as_output=Output("output0", "TENSOR_QUANT8_ASYMM", "{2, 3}, 4.0, 100"),
46        data=[1, 2, 3, 4, 5, 6],
47        supports_relaxation=False),
48]
49
50for operand1, operand2 in itertools.product(operands, operands):
51  input0 = operand1.as_input
52  output0 = operand2.as_output
53
54  model = Model().Operation("CAST", input0).To(output0)
55
56  example = Example({
57      input0: operand1.data,
58      output0: operand2.data,
59  }, model=model, name='{}_to_{}'.format(operand1.name, operand2.name))
60
61  if operand1.supports_relaxation or operand2.supports_relaxation:
62    example.AddRelaxed()
63
64
65# Test overflow and underflow.
66operands = [
67    Operand(
68        name="float16",
69        as_input=Input("input0", "TENSOR_FLOAT16", "{2}"),
70        as_output=None,
71        data=[-1, 256],
72        supports_relaxation=False),
73    Operand(
74        name="float32",
75        as_input=Input("input0", "TENSOR_FLOAT32", "{2}"),
76        as_output=None,
77        data=[-1, 256],
78        supports_relaxation=True),
79    Operand(
80        name="int32",
81        as_input=Input("input0", "TENSOR_INT32", "{2}"),
82        as_output=None,
83        data=[-1, 256],
84        supports_relaxation=False),
85]
86
87for operand1 in operands:
88  input0 = operand1.as_input
89  output0 = Output("output0", "TENSOR_QUANT8_ASYMM", "{2}, 4.0, 100")
90
91  model = Model().Operation("CAST", input0).To(output0)
92
93  example = Example({
94      input0: operand1.data,
95      output0: [0, 255],
96  }, model=model, name='{}_to_quant8_overflow'.format(operand1.name))
97
98  if operand1.supports_relaxation:
99    example.AddRelaxed()
100