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 #include "fuzzing/operation_signatures/OperationSignatureUtils.h"
18 
19 namespace android {
20 namespace nn {
21 namespace fuzzing_test {
22 
softmaxConstructor(TestOperandType dataType,uint32_t rank,RandomOperation * op)23 static void softmaxConstructor(TestOperandType dataType, uint32_t rank, RandomOperation* op) {
24     sameDimensionOpConstructor(dataType, rank, op);
25     // Generate value for "axis" parameter.
26     if (op->inputs.size() > 2) {
27         op->inputs[2]->setScalarValue<int32_t>(getUniform<int32_t>(-rank, rank - 1));
28     }
29 }
30 
DEFINE_OPERATION_SIGNATURE(SOFTMAX_V1_0)31 DEFINE_OPERATION_SIGNATURE(SOFTMAX_V1_0){
32         .opType = TestOperationType::SOFTMAX,
33         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32,
34                                TestOperandType::TENSOR_QUANT8_ASYMM},
35         .supportedRanks = {2, 4},
36         .version = TestHalVersion::V1_0,
37         .inputs = {INPUT_DEFAULT, PARAMETER_FLOAT_RANGE(0.1, 10.0)},
38         .outputs = {OUTPUT_QUANT(1.f / 256, 0)},
39         .constructor = softmaxConstructor};
40 
DEFINE_OPERATION_SIGNATURE(SOFTMAX_V1_2)41 DEFINE_OPERATION_SIGNATURE(SOFTMAX_V1_2){
42         .opType = TestOperationType::SOFTMAX,
43         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32,
44                                TestOperandType::TENSOR_QUANT8_ASYMM},
45         .supportedRanks = {1, 3},
46         .version = TestHalVersion::V1_2,
47         .inputs = {INPUT_DEFAULT, PARAMETER_FLOAT_RANGE(0.1, 10.0)},
48         .outputs = {OUTPUT_QUANT(1.f / 256, 0)},
49         .constructor = softmaxConstructor};
50 
DEFINE_OPERATION_SIGNATURE(SOFTMAX_float16_V1_2)51 DEFINE_OPERATION_SIGNATURE(SOFTMAX_float16_V1_2){
52         .opType = TestOperationType::SOFTMAX,
53         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT16},
54         .supportedRanks = {1, 2, 3, 4},
55         .version = TestHalVersion::V1_2,
56         .inputs = {INPUT_DEFAULT, PARAMETER_FLOAT_RANGE(0.1, 10.0)},
57         .outputs = {OUTPUT_QUANT(1.f / 256, 0)},
58         .constructor = softmaxConstructor};
59 
DEFINE_OPERATION_SIGNATURE(SOFTMAX_axis_V1_2)60 DEFINE_OPERATION_SIGNATURE(SOFTMAX_axis_V1_2){
61         .opType = TestOperationType::SOFTMAX,
62         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32,
63                                TestOperandType::TENSOR_QUANT8_ASYMM,
64                                TestOperandType::TENSOR_FLOAT16},
65         .supportedRanks = {1, 2, 3, 4},
66         .version = TestHalVersion::V1_2,
67         .inputs = {INPUT_DEFAULT, PARAMETER_FLOAT_RANGE(0.1, 10.0),
68                    PARAMETER_NONE(TestOperandType::INT32)},
69         .outputs = {OUTPUT_QUANT(1.f / 256, 0)},
70         .constructor = softmaxConstructor};
71 
DEFINE_OPERATION_SIGNATURE(SOFTMAX_V1_3)72 DEFINE_OPERATION_SIGNATURE(SOFTMAX_V1_3){
73         .opType = TestOperationType::SOFTMAX,
74         .supportedDataTypes = {TestOperandType::TENSOR_QUANT8_ASYMM_SIGNED},
75         .supportedRanks = {1, 2, 3, 4},
76         .version = TestHalVersion::V1_3,
77         .inputs = {INPUT_DEFAULT, PARAMETER_FLOAT_RANGE(0.1, 10.0)},
78         .outputs = {OUTPUT_QUANT(1.f / 256, -128)},
79         .constructor = softmaxConstructor};
80 
DEFINE_OPERATION_SIGNATURE(SOFTMAX_axis_V1_3)81 DEFINE_OPERATION_SIGNATURE(SOFTMAX_axis_V1_3){
82         .opType = TestOperationType::SOFTMAX,
83         .supportedDataTypes = {TestOperandType::TENSOR_QUANT8_ASYMM_SIGNED},
84         .supportedRanks = {1, 2, 3, 4},
85         .version = TestHalVersion::V1_3,
86         .inputs = {INPUT_DEFAULT, PARAMETER_FLOAT_RANGE(0.1, 10.0),
87                    PARAMETER_NONE(TestOperandType::INT32)},
88         .outputs = {OUTPUT_QUANT(1.f / 256, -128)},
89         .constructor = softmaxConstructor};
90 
l2normConstructor(TestOperandType dataType,uint32_t rank,RandomOperation * op)91 static void l2normConstructor(TestOperandType dataType, uint32_t rank, RandomOperation* op) {
92     sameDimensionOpConstructor(dataType, rank, op);
93     // Generate value for "axis" parameter.
94     if (op->inputs.size() > 1) {
95         op->inputs[1]->setScalarValue<int32_t>(getUniform<int32_t>(-rank, rank - 1));
96     }
97     // L2_NORMALIZATION may produce NaN output values with all zero inputs. We should not connect
98     // the output tensor to the input of another operation.
99     op->outputs[0]->doNotConnect = true;
100 }
101 
DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_V1_0)102 DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_V1_0){
103         .opType = TestOperationType::L2_NORMALIZATION,
104         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32},
105         .supportedRanks = {4},
106         .version = TestHalVersion::V1_0,
107         .inputs = {INPUT_DEFAULT},
108         .outputs = {OUTPUT_DEFAULT},
109         .constructor = l2normConstructor};
110 
DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_V1_2)111 DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_V1_2){
112         .opType = TestOperationType::L2_NORMALIZATION,
113         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32},
114         .supportedRanks = {1, 2, 3},
115         .version = TestHalVersion::V1_2,
116         .inputs = {INPUT_DEFAULT},
117         .outputs = {OUTPUT_DEFAULT},
118         .constructor = l2normConstructor};
119 
DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_type_V1_2)120 DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_type_V1_2){
121         .opType = TestOperationType::L2_NORMALIZATION,
122         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT16,
123                                TestOperandType::TENSOR_QUANT8_ASYMM},
124         .supportedRanks = {1, 2, 3, 4},
125         .version = TestHalVersion::V1_2,
126         .inputs = {INPUT_DEFAULT},
127         .outputs = {OUTPUT_QUANT(1.f / 128, 128)},
128         .constructor = l2normConstructor};
129 
DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_axis_V1_2)130 DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_axis_V1_2){
131         .opType = TestOperationType::L2_NORMALIZATION,
132         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32, TestOperandType::TENSOR_FLOAT16,
133                                TestOperandType::TENSOR_QUANT8_ASYMM},
134         .supportedRanks = {1, 2, 3, 4},
135         .version = TestHalVersion::V1_2,
136         .inputs = {INPUT_DEFAULT, PARAMETER_NONE(TestOperandType::INT32)},
137         .outputs = {OUTPUT_QUANT(1.f / 128, 128)},
138         .constructor = l2normConstructor};
139 
DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_V1_3)140 DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_V1_3){
141         .opType = TestOperationType::L2_NORMALIZATION,
142         .supportedDataTypes = {TestOperandType::TENSOR_QUANT8_ASYMM_SIGNED},
143         .supportedRanks = {1, 2, 3, 4},
144         .version = TestHalVersion::V1_3,
145         .inputs = {INPUT_DEFAULT},
146         .outputs = {OUTPUT_QUANT(1.f / 128, 0)},
147         .constructor = l2normConstructor};
148 
DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_axis_V1_3)149 DEFINE_OPERATION_SIGNATURE(L2_NORMALIZATION_axis_V1_3){
150         .opType = TestOperationType::L2_NORMALIZATION,
151         .supportedDataTypes = {TestOperandType::TENSOR_QUANT8_ASYMM_SIGNED},
152         .supportedRanks = {1, 2, 3, 4},
153         .version = TestHalVersion::V1_3,
154         .inputs = {INPUT_DEFAULT, PARAMETER_NONE(TestOperandType::INT32)},
155         .outputs = {OUTPUT_QUANT(1.f / 128, 0)},
156         .constructor = l2normConstructor};
157 
localResponseNormConstructor(TestOperandType dataType,uint32_t rank,RandomOperation * op)158 static void localResponseNormConstructor(TestOperandType dataType, uint32_t rank,
159                                          RandomOperation* op) {
160     sameDimensionOpConstructor(dataType, rank, op);
161     // Generate value for "axis" parameter.
162     if (op->inputs.size() > 5) {
163         op->inputs[5]->setScalarValue<int32_t>(getUniform<int32_t>(-rank, rank - 1));
164     }
165 }
166 
DEFINE_OPERATION_SIGNATURE(LOCAL_RESPONSE_NORMALIZATION_V1_0)167 DEFINE_OPERATION_SIGNATURE(LOCAL_RESPONSE_NORMALIZATION_V1_0){
168         .opType = TestOperationType::LOCAL_RESPONSE_NORMALIZATION,
169         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32},
170         .supportedRanks = {4},
171         .version = TestHalVersion::V1_0,
172         .inputs =
173                 {
174                         INPUT_DEFAULT,
175                         PARAMETER_RANGE(TestOperandType::INT32, 1, 10),
176                         PARAMETER_FLOAT_RANGE(0.0, 10.0),
177                         PARAMETER_FLOAT_RANGE(0.1, 10.0),
178                         PARAMETER_FLOAT_RANGE(0.1, 1.0),
179                 },
180         .outputs = {OUTPUT_DEFAULT},
181         .constructor = localResponseNormConstructor};
182 
DEFINE_OPERATION_SIGNATURE(LOCAL_RESPONSE_NORMALIZATION_V1_2)183 DEFINE_OPERATION_SIGNATURE(LOCAL_RESPONSE_NORMALIZATION_V1_2){
184         .opType = TestOperationType::LOCAL_RESPONSE_NORMALIZATION,
185         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32},
186         .supportedRanks = {1, 2, 3},
187         .version = TestHalVersion::V1_2,
188         .inputs =
189                 {
190                         INPUT_DEFAULT,
191                         PARAMETER_RANGE(TestOperandType::INT32, 1, 10),
192                         PARAMETER_FLOAT_RANGE(0.0, 10.0),
193                         PARAMETER_FLOAT_RANGE(0.1, 10.0),
194                         PARAMETER_FLOAT_RANGE(0.1, 1.0),
195                 },
196         .outputs = {OUTPUT_DEFAULT},
197         .constructor = localResponseNormConstructor};
198 
DEFINE_OPERATION_SIGNATURE(LOCAL_RESPONSE_NORMALIZATION_float16_V1_2)199 DEFINE_OPERATION_SIGNATURE(LOCAL_RESPONSE_NORMALIZATION_float16_V1_2){
200         .opType = TestOperationType::LOCAL_RESPONSE_NORMALIZATION,
201         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT16},
202         .supportedRanks = {1, 2, 3, 4},
203         .version = TestHalVersion::V1_2,
204         .inputs =
205                 {
206                         INPUT_DEFAULT,
207                         PARAMETER_RANGE(TestOperandType::INT32, 1, 10),
208                         PARAMETER_FLOAT_RANGE(0.0, 10.0),
209                         PARAMETER_FLOAT_RANGE(0.1, 10.0),
210                         PARAMETER_FLOAT_RANGE(0.1, 1.0),
211                 },
212         .outputs = {OUTPUT_DEFAULT},
213         .constructor = localResponseNormConstructor};
214 
DEFINE_OPERATION_SIGNATURE(LOCAL_RESPONSE_NORMALIZATION_axis_V1_2)215 DEFINE_OPERATION_SIGNATURE(LOCAL_RESPONSE_NORMALIZATION_axis_V1_2){
216         .opType = TestOperationType::LOCAL_RESPONSE_NORMALIZATION,
217         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32, TestOperandType::TENSOR_FLOAT16},
218         .supportedRanks = {1, 2, 3, 4},
219         .version = TestHalVersion::V1_2,
220         .inputs =
221                 {
222                         INPUT_DEFAULT,
223                         PARAMETER_RANGE(TestOperandType::INT32, 1, 10),
224                         PARAMETER_FLOAT_RANGE(0.0, 10.0),
225                         PARAMETER_FLOAT_RANGE(0.1, 10.0),
226                         PARAMETER_FLOAT_RANGE(0.1, 1.0),
227                         PARAMETER_NONE(TestOperandType::INT32),
228                 },
229         .outputs = {OUTPUT_DEFAULT},
230         .constructor = localResponseNormConstructor};
231 
DEFINE_OPERATION_SIGNATURE(INSTANCE_NORMALIZATION_V1_2)232 DEFINE_OPERATION_SIGNATURE(INSTANCE_NORMALIZATION_V1_2){
233         .opType = TestOperationType::INSTANCE_NORMALIZATION,
234         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32, TestOperandType::TENSOR_FLOAT16},
235         .supportedRanks = {4},
236         .version = TestHalVersion::V1_2,
237         .inputs =
238                 {
239                         INPUT_DEFAULT,
240                         PARAMETER_FLOAT_RANGE(0.1, 10.0),
241                         PARAMETER_FLOAT_RANGE(-10.0, 10.0),
242                         PARAMETER_FLOAT_RANGE(0.01, 1.0),
243                         PARAMETER_CHOICE(TestOperandType::BOOL, true, false),
244                 },
245         .outputs = {OUTPUT_DEFAULT},
246         .constructor = sameShapeOpConstructor};
247 
DEFINE_OPERATION_SIGNATURE(LOG_SOFTMAX_TENSOR_FLOAT32_V1_2)248 DEFINE_OPERATION_SIGNATURE(LOG_SOFTMAX_TENSOR_FLOAT32_V1_2){
249         .opType = TestOperationType::LOG_SOFTMAX,
250         .supportedDataTypes = {TestOperandType::TENSOR_FLOAT32, TestOperandType::TENSOR_FLOAT16},
251         .supportedRanks = {1, 2, 3, 4},
252         .version = TestHalVersion::V1_2,
253         .inputs = {INPUT_DEFAULT, PARAMETER_FLOAT_RANGE(0.1, 10.0),
254                    PARAMETER_NONE(TestOperandType::INT32)},
255         .outputs = {OUTPUT_DEFAULT},
256         .constructor = softmaxConstructor};
257 
258 }  // namespace fuzzing_test
259 }  // namespace nn
260 }  // namespace android
261