1#
2# Copyright (C) 2019 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
17
18def test(name, input0, output0, input0_data, output0_data):
19  model = Model().Operation("HARD_SWISH", input0).To(output0)
20  quant8 = DataTypeConverter().Identify({
21      input0: ["TENSOR_QUANT8_ASYMM", 0.078125, 128],
22      output0: ["TENSOR_QUANT8_ASYMM", 0.078125, 128],
23  })
24  quant8_signed = DataTypeConverter().Identify({
25      input0: ["TENSOR_QUANT8_ASYMM_SIGNED", 0.078125, 0],
26      output0: ["TENSOR_QUANT8_ASYMM_SIGNED", 0.078125, 0],
27  })
28  quant8_overflow = DataTypeConverter().Identify({
29      input0: ["TENSOR_QUANT8_ASYMM", 0.078125, 128],
30      output0: ["TENSOR_QUANT8_ASYMM", 0.03125, 0],
31  })
32  quant8_signed_overflow = DataTypeConverter().Identify({
33      input0: ["TENSOR_QUANT8_ASYMM_SIGNED", 0.078125, 0],
34      output0: ["TENSOR_QUANT8_ASYMM_SIGNED", 0.03125, -128],
35  })
36  example = Example({
37      input0: input0_data,
38      output0: output0_data,
39  },
40                    model=model,
41                    name=name).AddVariations("float16", "relaxed", quant8,
42                                             quant8_signed, quant8_overflow,
43                                             quant8_signed_overflow)
44
45
46test(
47    name="simple",
48    input0=Input("input0", "TENSOR_FLOAT32", "{40}"),
49    output0=Output("output0", "TENSOR_FLOAT32", "{40}"),
50    input0_data=[
51        4.53125, 3.90625, 3.046875, -8.59375, -1.328125, 1.328125, 0.0,
52        -8.515625, -8.984375, -0.234375, 0.859375, 9.84375, -0.15625, -8.515625,
53        8.671875, 4.609375, 9.21875, -1.796875, 1.171875, 9.375, -8.75,
54        2.421875, -8.125, -1.09375, -9.609375, -1.015625, -9.84375, 2.578125,
55        4.921875, -5.078125, 5.0, -0.859375, 1.953125, -6.640625, -7.8125,
56        4.453125, -4.453125, -6.875, 0.78125, 0.859375
57    ],
58    output0_data=[
59        4.53125, 3.90625, 3.046875, 0.0, -0.3700765, 0.9580485, 0.0, 0.0, 0.0,
60        -0.1080322, 0.5527751, 9.84375, -0.074056, 0.0, 8.671875, 4.609375,
61        9.21875, -0.3603109, 0.8148193, 9.375, 0.0, 2.1885173, 0.0, -0.3474935,
62        0.0, -0.3358968, 0.0, 2.3968506, 4.921875, 0.0, 5.0, -0.3065999,
63        1.6123454, 0.0, 0.0, 4.453125, 0.0, 0.0, 0.4923503, 0.5527751
64    ],
65)
66
67test(
68    name="5d_input",
69    input0=Input("input0", "TENSOR_FLOAT32", "{1, 2, 2, 2, 5}"),
70    output0=Output("output0", "TENSOR_FLOAT32", "{1, 2, 2, 2, 5}"),
71    input0_data=[
72        4.53125, 3.90625, 3.046875, -8.59375, -1.328125, 1.328125, 0.0,
73        -8.515625, -8.984375, -0.234375, 0.859375, 9.84375, -0.15625, -8.515625,
74        8.671875, 4.609375, 9.21875, -1.796875, 1.171875, 9.375, -8.75,
75        2.421875, -8.125, -1.09375, -9.609375, -1.015625, -9.84375, 2.578125,
76        4.921875, -5.078125, 5.0, -0.859375, 1.953125, -6.640625, -7.8125,
77        4.453125, -4.453125, -6.875, 0.78125, 0.859375
78    ],
79    output0_data=[
80        4.53125, 3.90625, 3.046875, 0.0, -0.3700765, 0.9580485, 0.0, 0.0, 0.0,
81        -0.1080322, 0.5527751, 9.84375, -0.074056, 0.0, 8.671875, 4.609375,
82        9.21875, -0.3603109, 0.8148193, 9.375, 0.0, 2.1885173, 0.0, -0.3474935,
83        0.0, -0.3358968, 0.0, 2.3968506, 4.921875, 0.0, 5.0, -0.3065999,
84        1.6123454, 0.0, 0.0, 4.453125, 0.0, 0.0, 0.4923503, 0.5527751
85    ],
86)
87