blob: 8b16e97e3032095c03fba2fc8eb417812c7373b1 [file] [log] [blame]
Jeremy Johnsonbd801962024-01-03 17:07:44 +00001// Copyright (c) 2023-2024, ARM Limited.
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "generate_utils.h"
16
17#include <nlohmann/json.hpp>
18
19#include <algorithm>
20
21namespace tosa
22{
23
24NLOHMANN_JSON_SERIALIZE_ENUM(DType,
25 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010026 { DType::DType_UNKNOWN, "UNKNOWN" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010027 { DType::DType_BOOL, "BOOL" },
28 { DType::DType_INT4, "INT4" },
29 { DType::DType_INT8, "INT8" },
30 { DType::DType_INT16, "INT16" },
31 { DType::DType_INT32, "INT32" },
32 { DType::DType_INT48, "INT48" },
33 { DType::DType_FP16, "FP16" },
34 { DType::DType_BF16, "BF16" },
35 { DType::DType_FP32, "FP32" },
Won Jeon64e4bfe2024-01-18 06:31:55 +000036 { DType::DType_SHAPE, "SHAPE" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010037 })
38
39NLOHMANN_JSON_SERIALIZE_ENUM(Op,
40 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010041 { Op::Op_UNKNOWN, "UNKNOWN" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000042 { Op::Op_ABS, "ABS" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000043 { Op::Op_ADD, "ADD" },
Jeremy Johnsonbfc53032023-11-01 11:29:56 +000044 { Op::Op_ARGMAX, "ARGMAX" },
Jeremy Johnson0601f802023-11-08 16:28:09 +000045 { Op::Op_AVG_POOL2D, "AVG_POOL2D" },
Jeremy Johnson708da822023-11-15 16:25:45 +000046 { Op::Op_CAST, "CAST" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000047 { Op::Op_CEIL, "CEIL" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000048 { Op::Op_CLAMP, "CLAMP" },
Jeremy Johnson3eafe662024-01-10 13:13:35 +000049 { Op::Op_CONCAT, "CONCAT" },
evacha0198477222024-01-26 12:25:32 +000050 { Op::Op_CONST, "CONST" },
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010051 { Op::Op_CONV2D, "CONV2D" },
Jeremy Johnson4f931302024-01-04 17:05:24 +000052 { Op::Op_DEPTHWISE_CONV2D, "DEPTHWISE_CONV2D" },
evacha0147ab1762024-01-29 13:23:23 +000053 { Op::Op_CONV3D, "CONV3D" },
Jeremy Johnsona0150012023-11-15 15:52:06 +000054 { Op::Op_EQUAL, "EQUAL" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000055 { Op::Op_ERF, "ERF" },
Jeremy Johnson9a758382023-11-07 16:27:35 +000056 { Op::Op_EXP, "EXP" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000057 { Op::Op_FLOOR, "FLOOR" },
Jeremy Johnsonc8330812024-01-18 16:57:28 +000058 { Op::Op_FFT2D, "FFT2D" },
Jeremy Johnsonaee62af2023-11-02 17:16:25 +000059 { Op::Op_FULLY_CONNECTED, "FULLY_CONNECTED" },
Jeremy Johnsona8420ad2023-12-07 16:35:28 +000060 { Op::Op_GATHER, "GATHER" },
Jeremy Johnsona0150012023-11-15 15:52:06 +000061 { Op::Op_GREATER, "GREATER" },
62 { Op::Op_GREATER_EQUAL, "GREATER_EQUAL" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000063 { Op::Op_IDENTITY, "IDENTITY" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000064 { Op::Op_LOG, "LOG" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010065 { Op::Op_MATMUL, "MATMUL" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000066 { Op::Op_MAXIMUM, "MAXIMUM" },
Jeremy Johnsond41feb72023-10-12 16:03:15 +010067 { Op::Op_MAX_POOL2D, "MAX_POOL2D" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000068 { Op::Op_MINIMUM, "MINIMUM" },
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010069 { Op::Op_MUL, "MUL" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000070 { Op::Op_NEGATE, "NEGATE" },
Jeremy Johnsond41feb72023-10-12 16:03:15 +010071 { Op::Op_PAD, "PAD" },
Jeremy Johnson9a758382023-11-07 16:27:35 +000072 { Op::Op_POW, "POW" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000073 { Op::Op_RECIPROCAL, "RECIPROCAL" },
Jeremy Johnsonbfc53032023-11-01 11:29:56 +000074 { Op::Op_REDUCE_MAX, "REDUCE_MAX" },
75 { Op::Op_REDUCE_MIN, "REDUCE_MIN" },
Jeremy Johnsonbd801962024-01-03 17:07:44 +000076 { Op::Op_REDUCE_PRODUCT, "REDUCE_PRODUCT" },
Jeremy Johnsonbfc53032023-11-01 11:29:56 +000077 { Op::Op_REDUCE_SUM, "REDUCE_SUM" },
Jeremy Johnson32d0b5a2024-02-01 15:54:07 +000078 { Op::Op_RESHAPE, "RESHAPE" },
79 { Op::Op_RESIZE, "RESIZE" },
evacha0198477222024-01-26 12:25:32 +000080 { Op::Op_REVERSE, "REVERSE" },
Jeremy Johnson6f57e6e2024-01-30 16:10:50 +000081 { Op::Op_RFFT2D, "RFFT2D" },
Jeremy Johnson32d0b5a2024-02-01 15:54:07 +000082 { Op::Op_RSQRT, "RSQRT" },
Jeremy Johnsona8420ad2023-12-07 16:35:28 +000083 { Op::Op_SCATTER, "SCATTER" },
Jeremy Johnson7b9abce2024-01-10 11:07:29 +000084 { Op::Op_SELECT, "SELECT" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000085 { Op::Op_SIGMOID, "SIGMOID" },
evacha017f7d4252024-01-24 12:08:09 +000086 { Op::Op_SLICE, "SLICE" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000087 { Op::Op_SUB, "SUB" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000088 { Op::Op_TANH, "TANH" },
Jeremy Johnson9f5febe2024-01-15 15:12:17 +000089 { Op::Op_TILE, "TILE" },
evacha0198477222024-01-26 12:25:32 +000090 { Op::Op_TRANSPOSE, "TRANSPOSE" },
Jeremy Johnson95a67102024-01-10 14:16:39 +000091 { Op::Op_TRANSPOSE_CONV2D, "TRANSPOSE_CONV2D" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010092 })
93
94} // namespace tosa
95
96namespace TosaReference
97{
98
99NLOHMANN_JSON_SERIALIZE_ENUM(GeneratorType,
100 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100101 { GeneratorType::Unknown, "UNKNOWN" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100102 { GeneratorType::PseudoRandom, "PSEUDO_RANDOM" },
103 { GeneratorType::DotProduct, "DOT_PRODUCT" },
104 { GeneratorType::OpFullRange, "OP_FULL_RANGE" },
105 { GeneratorType::OpBoundary, "OP_BOUNDARY" },
106 { GeneratorType::OpSpecial, "OP_SPECIAL" },
Won Jeon64e4bfe2024-01-18 06:31:55 +0000107 { GeneratorType::FixedData, "FIXED_DATA" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100108 })
109
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100110// NOTE: This assumes it's VARIABLE if the InputType is not recognized
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100111NLOHMANN_JSON_SERIALIZE_ENUM(InputType,
112 {
113 { InputType::Variable, "VARIABLE" },
114 { InputType::Constant, "CONSTANT" },
115 })
116
117void from_json(const nlohmann::json& j, DotProductInfo& dotProductInfo)
118{
119 j.at("s").get_to(dotProductInfo.s);
120 j.at("ks").get_to(dotProductInfo.ks);
121 j.at("acc_type").get_to(dotProductInfo.accType);
122 if (j.contains("kernel"))
123 {
124 j.at("kernel").get_to(dotProductInfo.kernel);
125 }
126 if (j.contains("axis"))
127 {
128 j.at("axis").get_to(dotProductInfo.axis);
129 }
130}
131
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100132void from_json(const nlohmann::json& j, PseudoRandomInfo& pseudoRandomInfo)
133{
134 j.at("rng_seed").get_to(pseudoRandomInfo.rngSeed);
Jeremy Johnsona4d907e2023-10-26 13:53:14 +0100135 if (j.contains("range"))
136 {
137 j.at("range").get_to(pseudoRandomInfo.range);
138 }
Jeremy Johnson30476252023-11-20 16:15:30 +0000139 if (j.contains("round"))
140 {
141 j.at("round").get_to(pseudoRandomInfo.round);
142 }
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100143}
144
Won Jeon64e4bfe2024-01-18 06:31:55 +0000145void from_json(const nlohmann::json& j, FixedDataInfo& fixedDataInfo)
146{
147 j.at("data").get_to(fixedDataInfo.data);
148}
149
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100150void from_json(const nlohmann::json& j, GenerateConfig& cfg)
151{
152 j.at("data_type").get_to(cfg.dataType);
153 j.at("input_type").get_to(cfg.inputType);
154 j.at("shape").get_to(cfg.shape);
155 j.at("input_pos").get_to(cfg.inputPos);
156 j.at("op").get_to(cfg.opType);
157 j.at("generator").get_to(cfg.generatorType);
Jeremy Johnson30476252023-11-20 16:15:30 +0000158
159 // Set up defaults for dotProductInfo
160 cfg.dotProductInfo.s = -1;
161 cfg.dotProductInfo.ks = -1;
162 cfg.dotProductInfo.accType = DType_UNKNOWN;
163 cfg.dotProductInfo.kernel = std::vector<int32_t>();
164 cfg.dotProductInfo.axis = -1;
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100165 if (j.contains("dot_product_info"))
166 {
167 j.at("dot_product_info").get_to(cfg.dotProductInfo);
168 }
Jeremy Johnson30476252023-11-20 16:15:30 +0000169
170 // Set up defaults for pseudoRandomInfo
171 cfg.pseudoRandomInfo.rngSeed = -1;
172 cfg.pseudoRandomInfo.range = std::vector<std::string>();
173 cfg.pseudoRandomInfo.round = false;
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100174 if (j.contains("pseudo_random_info"))
175 {
176 j.at("pseudo_random_info").get_to(cfg.pseudoRandomInfo);
177 }
Won Jeon64e4bfe2024-01-18 06:31:55 +0000178
179 // Set up defaults for fixedDataInfo
180 cfg.fixedDataInfo.data = std::vector<int32_t>();
181 if (j.contains("fixed_data_info"))
182 {
183 j.at("fixed_data_info").get_to(cfg.fixedDataInfo);
184 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100185}
186
187std::optional<GenerateConfig> parseGenerateConfig(const char* json, const char* tensorName)
188{
189 if (!tensorName)
190 return std::nullopt;
191
192 auto jsonCfg = nlohmann::json::parse(json, nullptr, /* allow exceptions */ false);
193
194 if (jsonCfg.is_discarded())
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100195 {
196 WARNING("[Generator] Invalid json config.");
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100197 return std::nullopt;
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100198 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100199 if (!jsonCfg.contains("tensors"))
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100200 {
201 WARNING("[Generator] Missing tensors in json config.");
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100202 return std::nullopt;
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100203 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100204 const auto& tensors = jsonCfg["tensors"];
205 if (!tensors.contains(tensorName))
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100206 {
207 WARNING("[Generator] Missing tensor %s in json config.", tensorName);
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100208 return std::nullopt;
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100209 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100210 const auto& namedTensor = tensors[tensorName];
211 return namedTensor.get<GenerateConfig>();
212}
213
214int64_t numElementsFromShape(const std::vector<int32_t>& shape)
215{
Jeremy Johnson59b307d2023-10-04 14:17:26 +0100216 // Rank 0 shapes have no entries and so this will return 1
217 // Other ranked shapes will return the product of their dimensions
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100218 return std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int64_t>());
219}
220
221size_t elementSizeFromType(DType type)
222{
223 switch (type)
224 {
225 case DType::DType_BOOL:
226 case DType::DType_UINT8:
227 case DType::DType_INT8:
228 return 1;
229 case DType::DType_UINT16:
230 case DType::DType_INT16:
231 case DType::DType_FP16:
232 case DType::DType_BF16:
233 return 2;
234 case DType::DType_INT32:
235 case DType::DType_FP32:
Won Jeon64e4bfe2024-01-18 06:31:55 +0000236 case DType::DType_SHAPE:
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100237 return 4;
238 default:
239 return 0;
240 }
241 return 0;
242}
243} // namespace TosaReference