blob: 5546269327a4362153e005e8258c3b49e70bf637 [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 Johnsona4d907e2023-10-26 13:53:14 +010046 { Op::Op_CONV2D, "CONV2D" },
Jeremy Johnson9a758382023-11-07 16:27:35 +000047 { Op::Op_EXP, "EXP" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000048 { Op::Op_FLOOR, "FLOOR" },
Jeremy Johnsonaee62af2023-11-02 17:16:25 +000049 { Op::Op_FULLY_CONNECTED, "FULLY_CONNECTED" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000050 { Op::Op_IDENTITY, "IDENTITY" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010051 { Op::Op_MATMUL, "MATMUL" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000052 { Op::Op_MAXIMUM, "MAXIMUM" },
Jeremy Johnsond41feb72023-10-12 16:03:15 +010053 { Op::Op_MAX_POOL2D, "MAX_POOL2D" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000054 { Op::Op_MINIMUM, "MINIMUM" },
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010055 { Op::Op_MUL, "MUL" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000056 { Op::Op_NEGATE, "NEGATE" },
Jeremy Johnsond41feb72023-10-12 16:03:15 +010057 { Op::Op_PAD, "PAD" },
Jeremy Johnson9a758382023-11-07 16:27:35 +000058 { Op::Op_POW, "POW" },
Jeremy Johnson2d70ac42023-11-06 17:46:02 +000059 { Op::Op_RECIPROCAL, "RECIPROCAL" },
60 { Op::Op_RSQRT, "RSQRT" },
Jeremy Johnsonbfc53032023-11-01 11:29:56 +000061 { Op::Op_REDUCE_MAX, "REDUCE_MAX" },
62 { Op::Op_REDUCE_MIN, "REDUCE_MIN" },
63 { Op::Op_REDUCE_SUM, "REDUCE_SUM" },
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +000064 { Op::Op_SUB, "SUB" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010065 })
66
67} // namespace tosa
68
69namespace TosaReference
70{
71
72NLOHMANN_JSON_SERIALIZE_ENUM(GeneratorType,
73 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010074 { GeneratorType::Unknown, "UNKNOWN" },
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010075 { GeneratorType::PseudoRandom, "PSEUDO_RANDOM" },
76 { GeneratorType::DotProduct, "DOT_PRODUCT" },
77 { GeneratorType::OpFullRange, "OP_FULL_RANGE" },
78 { GeneratorType::OpBoundary, "OP_BOUNDARY" },
79 { GeneratorType::OpSpecial, "OP_SPECIAL" },
80 })
81
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010082// NOTE: This assumes it's VARIABLE if the InputType is not recognized
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +010083NLOHMANN_JSON_SERIALIZE_ENUM(InputType,
84 {
85 { InputType::Variable, "VARIABLE" },
86 { InputType::Constant, "CONSTANT" },
87 })
88
89void from_json(const nlohmann::json& j, DotProductInfo& dotProductInfo)
90{
91 j.at("s").get_to(dotProductInfo.s);
92 j.at("ks").get_to(dotProductInfo.ks);
93 j.at("acc_type").get_to(dotProductInfo.accType);
94 if (j.contains("kernel"))
95 {
96 j.at("kernel").get_to(dotProductInfo.kernel);
97 }
98 if (j.contains("axis"))
99 {
100 j.at("axis").get_to(dotProductInfo.axis);
101 }
102}
103
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100104void from_json(const nlohmann::json& j, PseudoRandomInfo& pseudoRandomInfo)
105{
106 j.at("rng_seed").get_to(pseudoRandomInfo.rngSeed);
Jeremy Johnsona4d907e2023-10-26 13:53:14 +0100107 if (j.contains("range"))
108 {
109 j.at("range").get_to(pseudoRandomInfo.range);
110 }
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100111}
112
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100113void from_json(const nlohmann::json& j, GenerateConfig& cfg)
114{
115 j.at("data_type").get_to(cfg.dataType);
116 j.at("input_type").get_to(cfg.inputType);
117 j.at("shape").get_to(cfg.shape);
118 j.at("input_pos").get_to(cfg.inputPos);
119 j.at("op").get_to(cfg.opType);
120 j.at("generator").get_to(cfg.generatorType);
121 if (j.contains("dot_product_info"))
122 {
123 j.at("dot_product_info").get_to(cfg.dotProductInfo);
124 }
Jeremy Johnsond41feb72023-10-12 16:03:15 +0100125 if (j.contains("pseudo_random_info"))
126 {
127 j.at("pseudo_random_info").get_to(cfg.pseudoRandomInfo);
128 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100129}
130
131std::optional<GenerateConfig> parseGenerateConfig(const char* json, const char* tensorName)
132{
133 if (!tensorName)
134 return std::nullopt;
135
136 auto jsonCfg = nlohmann::json::parse(json, nullptr, /* allow exceptions */ false);
137
138 if (jsonCfg.is_discarded())
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100139 {
140 WARNING("[Generator] Invalid json config.");
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100141 return std::nullopt;
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100142 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100143 if (!jsonCfg.contains("tensors"))
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100144 {
145 WARNING("[Generator] Missing tensors in 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 const auto& tensors = jsonCfg["tensors"];
149 if (!tensors.contains(tensorName))
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100150 {
151 WARNING("[Generator] Missing tensor %s in json config.", tensorName);
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100152 return std::nullopt;
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +0100153 }
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100154 const auto& namedTensor = tensors[tensorName];
155 return namedTensor.get<GenerateConfig>();
156}
157
158int64_t numElementsFromShape(const std::vector<int32_t>& shape)
159{
Jeremy Johnson59b307d2023-10-04 14:17:26 +0100160 // Rank 0 shapes have no entries and so this will return 1
161 // Other ranked shapes will return the product of their dimensions
Jeremy Johnsonb20b0c92023-10-04 14:17:55 +0100162 return std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int64_t>());
163}
164
165size_t elementSizeFromType(DType type)
166{
167 switch (type)
168 {
169 case DType::DType_BOOL:
170 case DType::DType_UINT8:
171 case DType::DType_INT8:
172 return 1;
173 case DType::DType_UINT16:
174 case DType::DType_INT16:
175 case DType::DType_FP16:
176 case DType::DType_BF16:
177 return 2;
178 case DType::DType_INT32:
179 case DType::DType_FP32:
180 return 4;
181 default:
182 return 0;
183 }
184 return 0;
185}
186} // namespace TosaReference