Jeremy Johnson | bd80196 | 2024-01-03 17:07:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2023-2024, ARM Limited. |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 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 | |
| 21 | namespace tosa |
| 22 | { |
| 23 | |
| 24 | NLOHMANN_JSON_SERIALIZE_ENUM(DType, |
| 25 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 26 | { DType::DType_UNKNOWN, "UNKNOWN" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 27 | { 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 Jeon | 64e4bfe | 2024-01-18 06:31:55 +0000 | [diff] [blame] | 36 | { DType::DType_SHAPE, "SHAPE" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 37 | }) |
| 38 | |
| 39 | NLOHMANN_JSON_SERIALIZE_ENUM(Op, |
| 40 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 41 | { Op::Op_UNKNOWN, "UNKNOWN" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 42 | { Op::Op_ABS, "ABS" }, |
Jeremy Johnson | 7bf0cb9 | 2023-10-31 14:37:54 +0000 | [diff] [blame] | 43 | { Op::Op_ADD, "ADD" }, |
Jeremy Johnson | bfc5303 | 2023-11-01 11:29:56 +0000 | [diff] [blame] | 44 | { Op::Op_ARGMAX, "ARGMAX" }, |
Jeremy Johnson | 0601f80 | 2023-11-08 16:28:09 +0000 | [diff] [blame] | 45 | { Op::Op_AVG_POOL2D, "AVG_POOL2D" }, |
Jeremy Johnson | 708da82 | 2023-11-15 16:25:45 +0000 | [diff] [blame] | 46 | { Op::Op_CAST, "CAST" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 47 | { Op::Op_CEIL, "CEIL" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame] | 48 | { Op::Op_CLAMP, "CLAMP" }, |
Jeremy Johnson | 3eafe66 | 2024-01-10 13:13:35 +0000 | [diff] [blame] | 49 | { Op::Op_CONCAT, "CONCAT" }, |
evacha01 | 9847722 | 2024-01-26 12:25:32 +0000 | [diff] [blame] | 50 | { Op::Op_CONST, "CONST" }, |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 51 | { Op::Op_CONV2D, "CONV2D" }, |
Jeremy Johnson | 4f93130 | 2024-01-04 17:05:24 +0000 | [diff] [blame] | 52 | { Op::Op_DEPTHWISE_CONV2D, "DEPTHWISE_CONV2D" }, |
evacha01 | 47ab176 | 2024-01-29 13:23:23 +0000 | [diff] [blame] | 53 | { Op::Op_CONV3D, "CONV3D" }, |
Jeremy Johnson | a015001 | 2023-11-15 15:52:06 +0000 | [diff] [blame] | 54 | { Op::Op_EQUAL, "EQUAL" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame] | 55 | { Op::Op_ERF, "ERF" }, |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 56 | { Op::Op_EXP, "EXP" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 57 | { Op::Op_FLOOR, "FLOOR" }, |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 58 | { Op::Op_FFT2D, "FFT2D" }, |
Jeremy Johnson | aee62af | 2023-11-02 17:16:25 +0000 | [diff] [blame] | 59 | { Op::Op_FULLY_CONNECTED, "FULLY_CONNECTED" }, |
Jeremy Johnson | a8420ad | 2023-12-07 16:35:28 +0000 | [diff] [blame] | 60 | { Op::Op_GATHER, "GATHER" }, |
Jeremy Johnson | a015001 | 2023-11-15 15:52:06 +0000 | [diff] [blame] | 61 | { Op::Op_GREATER, "GREATER" }, |
| 62 | { Op::Op_GREATER_EQUAL, "GREATER_EQUAL" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 63 | { Op::Op_IDENTITY, "IDENTITY" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame] | 64 | { Op::Op_LOG, "LOG" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 65 | { Op::Op_MATMUL, "MATMUL" }, |
Jeremy Johnson | 7bf0cb9 | 2023-10-31 14:37:54 +0000 | [diff] [blame] | 66 | { Op::Op_MAXIMUM, "MAXIMUM" }, |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 67 | { Op::Op_MAX_POOL2D, "MAX_POOL2D" }, |
Jeremy Johnson | 7bf0cb9 | 2023-10-31 14:37:54 +0000 | [diff] [blame] | 68 | { Op::Op_MINIMUM, "MINIMUM" }, |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 69 | { Op::Op_MUL, "MUL" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 70 | { Op::Op_NEGATE, "NEGATE" }, |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 71 | { Op::Op_PAD, "PAD" }, |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 72 | { Op::Op_POW, "POW" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 73 | { Op::Op_RECIPROCAL, "RECIPROCAL" }, |
Jeremy Johnson | bfc5303 | 2023-11-01 11:29:56 +0000 | [diff] [blame] | 74 | { Op::Op_REDUCE_MAX, "REDUCE_MAX" }, |
| 75 | { Op::Op_REDUCE_MIN, "REDUCE_MIN" }, |
Jeremy Johnson | bd80196 | 2024-01-03 17:07:44 +0000 | [diff] [blame] | 76 | { Op::Op_REDUCE_PRODUCT, "REDUCE_PRODUCT" }, |
Jeremy Johnson | bfc5303 | 2023-11-01 11:29:56 +0000 | [diff] [blame] | 77 | { Op::Op_REDUCE_SUM, "REDUCE_SUM" }, |
Jeremy Johnson | 32d0b5a | 2024-02-01 15:54:07 +0000 | [diff] [blame^] | 78 | { Op::Op_RESHAPE, "RESHAPE" }, |
| 79 | { Op::Op_RESIZE, "RESIZE" }, |
evacha01 | 9847722 | 2024-01-26 12:25:32 +0000 | [diff] [blame] | 80 | { Op::Op_REVERSE, "REVERSE" }, |
Jeremy Johnson | 6f57e6e | 2024-01-30 16:10:50 +0000 | [diff] [blame] | 81 | { Op::Op_RFFT2D, "RFFT2D" }, |
Jeremy Johnson | 32d0b5a | 2024-02-01 15:54:07 +0000 | [diff] [blame^] | 82 | { Op::Op_RSQRT, "RSQRT" }, |
Jeremy Johnson | a8420ad | 2023-12-07 16:35:28 +0000 | [diff] [blame] | 83 | { Op::Op_SCATTER, "SCATTER" }, |
Jeremy Johnson | 7b9abce | 2024-01-10 11:07:29 +0000 | [diff] [blame] | 84 | { Op::Op_SELECT, "SELECT" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame] | 85 | { Op::Op_SIGMOID, "SIGMOID" }, |
evacha01 | 7f7d425 | 2024-01-24 12:08:09 +0000 | [diff] [blame] | 86 | { Op::Op_SLICE, "SLICE" }, |
Jeremy Johnson | 7bf0cb9 | 2023-10-31 14:37:54 +0000 | [diff] [blame] | 87 | { Op::Op_SUB, "SUB" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame] | 88 | { Op::Op_TANH, "TANH" }, |
Jeremy Johnson | 9f5febe | 2024-01-15 15:12:17 +0000 | [diff] [blame] | 89 | { Op::Op_TILE, "TILE" }, |
evacha01 | 9847722 | 2024-01-26 12:25:32 +0000 | [diff] [blame] | 90 | { Op::Op_TRANSPOSE, "TRANSPOSE" }, |
Jeremy Johnson | 95a6710 | 2024-01-10 14:16:39 +0000 | [diff] [blame] | 91 | { Op::Op_TRANSPOSE_CONV2D, "TRANSPOSE_CONV2D" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 92 | }) |
| 93 | |
| 94 | } // namespace tosa |
| 95 | |
| 96 | namespace TosaReference |
| 97 | { |
| 98 | |
| 99 | NLOHMANN_JSON_SERIALIZE_ENUM(GeneratorType, |
| 100 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 101 | { GeneratorType::Unknown, "UNKNOWN" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 102 | { 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 Jeon | 64e4bfe | 2024-01-18 06:31:55 +0000 | [diff] [blame] | 107 | { GeneratorType::FixedData, "FIXED_DATA" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 108 | }) |
| 109 | |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 110 | // NOTE: This assumes it's VARIABLE if the InputType is not recognized |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 111 | NLOHMANN_JSON_SERIALIZE_ENUM(InputType, |
| 112 | { |
| 113 | { InputType::Variable, "VARIABLE" }, |
| 114 | { InputType::Constant, "CONSTANT" }, |
| 115 | }) |
| 116 | |
| 117 | void 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 Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 132 | void from_json(const nlohmann::json& j, PseudoRandomInfo& pseudoRandomInfo) |
| 133 | { |
| 134 | j.at("rng_seed").get_to(pseudoRandomInfo.rngSeed); |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 135 | if (j.contains("range")) |
| 136 | { |
| 137 | j.at("range").get_to(pseudoRandomInfo.range); |
| 138 | } |
Jeremy Johnson | 3047625 | 2023-11-20 16:15:30 +0000 | [diff] [blame] | 139 | if (j.contains("round")) |
| 140 | { |
| 141 | j.at("round").get_to(pseudoRandomInfo.round); |
| 142 | } |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Won Jeon | 64e4bfe | 2024-01-18 06:31:55 +0000 | [diff] [blame] | 145 | void from_json(const nlohmann::json& j, FixedDataInfo& fixedDataInfo) |
| 146 | { |
| 147 | j.at("data").get_to(fixedDataInfo.data); |
| 148 | } |
| 149 | |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 150 | void 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 Johnson | 3047625 | 2023-11-20 16:15:30 +0000 | [diff] [blame] | 158 | |
| 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 Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 165 | if (j.contains("dot_product_info")) |
| 166 | { |
| 167 | j.at("dot_product_info").get_to(cfg.dotProductInfo); |
| 168 | } |
Jeremy Johnson | 3047625 | 2023-11-20 16:15:30 +0000 | [diff] [blame] | 169 | |
| 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 Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 174 | if (j.contains("pseudo_random_info")) |
| 175 | { |
| 176 | j.at("pseudo_random_info").get_to(cfg.pseudoRandomInfo); |
| 177 | } |
Won Jeon | 64e4bfe | 2024-01-18 06:31:55 +0000 | [diff] [blame] | 178 | |
| 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 Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | std::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 Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 195 | { |
| 196 | WARNING("[Generator] Invalid json config."); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 197 | return std::nullopt; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 198 | } |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 199 | if (!jsonCfg.contains("tensors")) |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 200 | { |
| 201 | WARNING("[Generator] Missing tensors in json config."); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 202 | return std::nullopt; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 203 | } |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 204 | const auto& tensors = jsonCfg["tensors"]; |
| 205 | if (!tensors.contains(tensorName)) |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 206 | { |
| 207 | WARNING("[Generator] Missing tensor %s in json config.", tensorName); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 208 | return std::nullopt; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 209 | } |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 210 | const auto& namedTensor = tensors[tensorName]; |
| 211 | return namedTensor.get<GenerateConfig>(); |
| 212 | } |
| 213 | |
| 214 | int64_t numElementsFromShape(const std::vector<int32_t>& shape) |
| 215 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 216 | // Rank 0 shapes have no entries and so this will return 1 |
| 217 | // Other ranked shapes will return the product of their dimensions |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 218 | return std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int64_t>()); |
| 219 | } |
| 220 | |
| 221 | size_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 Jeon | 64e4bfe | 2024-01-18 06:31:55 +0000 | [diff] [blame] | 236 | case DType::DType_SHAPE: |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 237 | return 4; |
| 238 | default: |
| 239 | return 0; |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | } // namespace TosaReference |