blob: d2168c9f1a0d83b16a4f41057f44b7bdf8306b68 [file] [log] [blame]
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +01001// Copyright (c) 2023, ARM Limited.
2//
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" },
36 })
37
38NLOHMANN_JSON_SERIALIZE_ENUM(Op,
39 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010040 { Op::Op_UNKNOWN, "UNKNOWN" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000041 { Op::Op_ABS, "ABS" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000042 { Op::Op_ADD, "ADD" },
Jeremy Johnsonbfc53032023-11-01 11:29:56 +000043 { Op::Op_ARGMAX, "ARGMAX" },
Jeremy Johnson0601f802023-11-08 16:28:09 +000044 { Op::Op_AVG_POOL2D, "AVG_POOL2D" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000045 { Op::Op_CEIL, "CEIL" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000046 { Op::Op_CLAMP, "CLAMP" },
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010047 { Op::Op_CONV2D, "CONV2D" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000048 { Op::Op_ERF, "ERF" },
Jeremy Johnson9a758382023-11-07 16:27:35 +000049 { Op::Op_EXP, "EXP" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000050 { Op::Op_FLOOR, "FLOOR" },
Jeremy Johnsonaee62af2023-11-02 17:16:25 +000051 { Op::Op_FULLY_CONNECTED, "FULLY_CONNECTED" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000052 { Op::Op_IDENTITY, "IDENTITY" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000053 { Op::Op_LOG, "LOG" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010054 { Op::Op_MATMUL, "MATMUL" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000055 { Op::Op_MAXIMUM, "MAXIMUM" },
Jeremy Johnsond41feb72023-10-12 16:03:15 +010056 { Op::Op_MAX_POOL2D, "MAX_POOL2D" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000057 { Op::Op_MINIMUM, "MINIMUM" },
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010058 { Op::Op_MUL, "MUL" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000059 { Op::Op_NEGATE, "NEGATE" },
Jeremy Johnsond41feb72023-10-12 16:03:15 +010060 { Op::Op_PAD, "PAD" },
Jeremy Johnson9a758382023-11-07 16:27:35 +000061 { Op::Op_POW, "POW" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000062 { Op::Op_RECIPROCAL, "RECIPROCAL" },
63 { Op::Op_RSQRT, "RSQRT" },
Jeremy Johnsonbfc53032023-11-01 11:29:56 +000064 { Op::Op_REDUCE_MAX, "REDUCE_MAX" },
65 { Op::Op_REDUCE_MIN, "REDUCE_MIN" },
66 { Op::Op_REDUCE_SUM, "REDUCE_SUM" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000067 { Op::Op_SIGMOID, "SIGMOID" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000068 { Op::Op_SUB, "SUB" },
Jeremy Johnson0bbd8bc2023-11-09 16:56:07 +000069 { Op::Op_TANH, "TANH" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010070 })
71
72} // namespace tosa
73
74namespace TosaReference
75{
76
77NLOHMANN_JSON_SERIALIZE_ENUM(GeneratorType,
78 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010079 { GeneratorType::Unknown, "UNKNOWN" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010080 { GeneratorType::PseudoRandom, "PSEUDO_RANDOM" },
81 { GeneratorType::DotProduct, "DOT_PRODUCT" },
82 { GeneratorType::OpFullRange, "OP_FULL_RANGE" },
83 { GeneratorType::OpBoundary, "OP_BOUNDARY" },
84 { GeneratorType::OpSpecial, "OP_SPECIAL" },
85 })
86
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010087// NOTE: This assumes it's VARIABLE if the InputType is not recognized
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010088NLOHMANN_JSON_SERIALIZE_ENUM(InputType,
89 {
90 { InputType::Variable, "VARIABLE" },
91 { InputType::Constant, "CONSTANT" },
92 })
93
94void from_json(const nlohmann::json& j, DotProductInfo& dotProductInfo)
95{
96 j.at("s").get_to(dotProductInfo.s);
97 j.at("ks").get_to(dotProductInfo.ks);
98 j.at("acc_type").get_to(dotProductInfo.accType);
99 if (j.contains("kernel"))
100 {
101 j.at("kernel").get_to(dotProductInfo.kernel);
102 }
103 if (j.contains("axis"))
104 {
105 j.at("axis").get_to(dotProductInfo.axis);
106 }
107}
108
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100109void from_json(const nlohmann::json& j, PseudoRandomInfo& pseudoRandomInfo)
110{
111 j.at("rng_seed").get_to(pseudoRandomInfo.rngSeed);
Jeremy Johnsona4d907e2023-10-26 13:53:14 +0100112 if (j.contains("range"))
113 {
114 j.at("range").get_to(pseudoRandomInfo.range);
115 }
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100116}
117
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100118void from_json(const nlohmann::json& j, GenerateConfig& cfg)
119{
120 j.at("data_type").get_to(cfg.dataType);
121 j.at("input_type").get_to(cfg.inputType);
122 j.at("shape").get_to(cfg.shape);
123 j.at("input_pos").get_to(cfg.inputPos);
124 j.at("op").get_to(cfg.opType);
125 j.at("generator").get_to(cfg.generatorType);
126 if (j.contains("dot_product_info"))
127 {
128 j.at("dot_product_info").get_to(cfg.dotProductInfo);
129 }
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100130 if (j.contains("pseudo_random_info"))
131 {
132 j.at("pseudo_random_info").get_to(cfg.pseudoRandomInfo);
133 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100134}
135
136std::optional<GenerateConfig> parseGenerateConfig(const char* json, const char* tensorName)
137{
138 if (!tensorName)
139 return std::nullopt;
140
141 auto jsonCfg = nlohmann::json::parse(json, nullptr, /* allow exceptions */ false);
142
143 if (jsonCfg.is_discarded())
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100144 {
145 WARNING("[Generator] Invalid json config.");
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100146 return std::nullopt;
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100147 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100148 if (!jsonCfg.contains("tensors"))
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100149 {
150 WARNING("[Generator] Missing tensors in json config.");
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100151 return std::nullopt;
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100152 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100153 const auto& tensors = jsonCfg["tensors"];
154 if (!tensors.contains(tensorName))
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100155 {
156 WARNING("[Generator] Missing tensor %s in json config.", tensorName);
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100157 return std::nullopt;
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100158 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100159 const auto& namedTensor = tensors[tensorName];
160 return namedTensor.get<GenerateConfig>();
161}
162
163int64_t numElementsFromShape(const std::vector<int32_t>& shape)
164{
Jeremy Johnson59b307d2023-10-04 14:17:26 +0100165 // Rank 0 shapes have no entries and so this will return 1
166 // Other ranked shapes will return the product of their dimensions
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100167 return std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int64_t>());
168}
169
170size_t elementSizeFromType(DType type)
171{
172 switch (type)
173 {
174 case DType::DType_BOOL:
175 case DType::DType_UINT8:
176 case DType::DType_INT8:
177 return 1;
178 case DType::DType_UINT16:
179 case DType::DType_INT16:
180 case DType::DType_FP16:
181 case DType::DType_BF16:
182 return 2;
183 case DType::DType_INT32:
184 case DType::DType_FP32:
185 return 4;
186 default:
187 return 0;
188 }
189 return 0;
190}
191} // namespace TosaReference