Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / tests / nnapi / specs / skip / V1_2 / dequantize_v1_2.mod.py
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
18 def test(name, input0, output0, input0_data, output0_data):
19   model = Model().Operation("DEQUANTIZE", input0).To(output0)
20   example = Example({
21       input0: input0_data,
22       output0: output0_data,
23   },
24                     model=model,
25                     name=name).AddVariations("relaxed", "float16")
26
27
28 test(
29     name="1d_quant8_asymm",
30     input0=Input("input0", "TENSOR_QUANT8_ASYMM", "{10}, 0.5, 127"),
31     output0=Output("output0", "TENSOR_FLOAT32", "{10}"),
32     input0_data=[0, 1, 2, 3, 4, 251, 252, 253, 254, 255],
33     output0_data=[-63.5, -63, -62.5, -62, -61.5, 62, 62.5, 63, 63.5, 64],
34 )
35
36 test(
37     name="2d_quant8_asymm",
38     input0=Input("input0", "TENSOR_QUANT8_ASYMM", "{2, 5}, 0.5, 127"),
39     output0=Output("output0", "TENSOR_FLOAT32", "{2, 5}"),
40     input0_data=[0, 1, 2, 3, 4, 251, 252, 253, 254, 255],
41     output0_data=[-63.5, -63, -62.5, -62, -61.5, 62, 62.5, 63, 63.5, 64],
42 )
43
44 test(
45     name="3d_quant8_symm",
46     input0=Input("input0", "TENSOR_QUANT8_SYMM", "{2, 2, 2}, 0.5, 0"),
47     output0=Output("output0", "TENSOR_FLOAT32", "{2, 2, 2}"),
48     input0_data=[-128, -127, -126, -125, 124, 125, 126, 127],
49     output0_data=[-64, -63.5, -63, -62.5, 62, 62.5, 63, 63.5],
50 )
51
52 test(
53     name="4d_quant8_symm",
54     input0=Input("input0", "TENSOR_QUANT8_SYMM", "{2, 1, 2, 2}, 0.5, 0"),
55     output0=Output("output0", "TENSOR_FLOAT32", "{2, 1, 2, 2}"),
56     input0_data=[-128, -127, -126, -125, 124, 125, 126, 127],
57     output0_data=[-64, -63.5, -63, -62.5, 62, 62.5, 63, 63.5],
58 )
59
60 test(
61     name="3d_per_channel_first_dim",
62     input0=Input(
63         "input0", ("TENSOR_QUANT8_SYMM_PER_CHANNEL", [2, 3, 4], 0, 0),
64         extraParams=SymmPerChannelQuantParams(channelDim=0, scales=[2., 0.5])),
65     output0=Output("output0", "TENSOR_FLOAT32", "{2, 3, 4}"),
66     input0_data=[
67         -128, -127, -126, -125, -124, -123, -122, -121, -120, -119, -118, -117,
68         116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127
69     ],
70     output0_data=[
71         -256, -254, -252, -250, -248, -246, -244, -242, -240, -238, -236, -234,
72         58., 58.5, 59., 59.5, 60., 60.5, 61., 61.5, 62., 62.5, 63., 63.5
73     ],
74 )
75
76 test(
77     name="3d_per_channel_second_dim",
78     input0=Input(
79         "input0", ("TENSOR_QUANT8_SYMM_PER_CHANNEL", [2, 3, 4], 0, 0),
80         extraParams=SymmPerChannelQuantParams(
81             channelDim=1, scales=[2., 1., 0.5])),
82     output0=Output("output0", "TENSOR_FLOAT32", "{2, 3, 4}"),
83     input0_data=[
84         -128, -127, -126, -125, -124, -123, -122, -121, -120, -119, -118, -117,
85         116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127
86     ],
87     output0_data=[
88         -256., -254., -252., -250., -124., -123., -122., -121., -60., -59.5,
89         -59., -58.5, 232., 234., 236., 238., 120., 121., 122., 123., 62., 62.5,
90         63., 63.5
91     ],
92 )
93
94 # DEQUANTIZE from TENSOR_QUANT8_ASYMM to TENSOR_FLOAT32 is introduced in V1_0.
95 Example.SetVersion("V1_0", "dequantize_v1_2_1d_quant8_asymm", "dequantize_v1_2_2d_quant8_asymm")
96
97 # FLOAT16
98 model = Model()
99 i1 = Input("op1",  "TENSOR_QUANT8_ASYMM", "{1, 2, 2, 1}, 1.f, 0")
100 i2 = Output("op2", "TENSOR_FLOAT16", "{1, 2, 2, 1}")
101 model = model.Operation("DEQUANTIZE", i1).To(i2)
102
103 # Example 1. Input in operand 0,
104 input0 = {i1: # input 0
105           [0, 32, 128, 255]}
106
107 output0 = {i2: # output 0
108            [0.0, 32.0, 128.0, 255.0]}
109
110 # Instantiate an example
111 Example((input0, output0))
112
113
114 # Zero-sized input
115
116 # Use BOX_WITH_NMS_LIMIT op to generate a zero-sized internal tensor for box cooridnates.
117 p1 = Parameter("scores", "TENSOR_QUANT8_ASYMM", "{1, 2}, 0.1f, 128", [137, 129]) # scores
118 p2 = Parameter("roi", "TENSOR_QUANT16_ASYMM", "{1, 8}, 0.125f, 0", [8, 8, 80, 80, 0, 0, 80, 80]) # roi
119 o1 = Output("scoresOut", "TENSOR_QUANT8_ASYMM", "{0}, 0.1f, 128") # scores out
120 o2 = Output("classesOut", "TENSOR_INT32", "{0}") # classes out
121 tmp1 = Internal("roiOut", "TENSOR_QUANT16_ASYMM", "{0, 4}, 0.125f, 0") # roi out
122 tmp2 = Internal("batchSplitOut", "TENSOR_INT32", "{0}") # batch split out
123 model = Model("zero_sized").Operation("BOX_WITH_NMS_LIMIT", p1, p2, [0], 0.3, -1, 0, 0.4, 1.0, 0.3).To(o1, tmp1, o2, tmp2)
124
125 # Use ROI_ALIGN op to convert into zero-sized feature map.
126 layout = BoolScalar("layout", False) # NHWC
127 i1 = Input("in", "TENSOR_QUANT8_ASYMM", "{1, 1, 1, 1}, 0.1f, 128")
128 zero_sized = Internal("featureMap", "TENSOR_QUANT8_ASYMM", "{0, 2, 2, 1}, 0.1f, 128")
129 model = model.Operation("ROI_ALIGN", i1, tmp1, tmp2, 2, 2, 2.0, 2.0, 4, 4, layout).To(zero_sized)
130
131 # DEQUANTIZE op with numBatches = 0.
132 o3 = Output("out", "TENSOR_FLOAT32", "{0, 2, 2, 1}") # out
133 model = model.Operation("DEQUANTIZE", zero_sized).To(o3)
134
135 float16 = DataTypeConverter().Identify({o3: ("TENSOR_FLOAT16",)})
136
137 # Create test case with dummy values.
138 Example({
139     i1: [1],
140     o1: [0],
141     o2: [0],
142     o3: [0],
143 }).AddVariations("relaxed", float16)