Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 1 | // 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 | |
| 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" }, |
| 36 | }) |
| 37 | |
| 38 | NLOHMANN_JSON_SERIALIZE_ENUM(Op, |
| 39 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 40 | { Op::Op_UNKNOWN, "UNKNOWN" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 41 | { Op::Op_ABS, "ABS" }, |
Jeremy Johnson | 7bf0cb9 | 2023-10-31 14:37:54 +0000 | [diff] [blame] | 42 | { Op::Op_ADD, "ADD" }, |
Jeremy Johnson | bfc5303 | 2023-11-01 11:29:56 +0000 | [diff] [blame] | 43 | { Op::Op_ARGMAX, "ARGMAX" }, |
Jeremy Johnson | 0601f80 | 2023-11-08 16:28:09 +0000 | [diff] [blame] | 44 | { Op::Op_AVG_POOL2D, "AVG_POOL2D" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 45 | { Op::Op_CEIL, "CEIL" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame^] | 46 | { Op::Op_CLAMP, "CLAMP" }, |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 47 | { Op::Op_CONV2D, "CONV2D" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame^] | 48 | { Op::Op_ERF, "ERF" }, |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 49 | { Op::Op_EXP, "EXP" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 50 | { Op::Op_FLOOR, "FLOOR" }, |
Jeremy Johnson | aee62af | 2023-11-02 17:16:25 +0000 | [diff] [blame] | 51 | { Op::Op_FULLY_CONNECTED, "FULLY_CONNECTED" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 52 | { Op::Op_IDENTITY, "IDENTITY" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame^] | 53 | { Op::Op_LOG, "LOG" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 54 | { Op::Op_MATMUL, "MATMUL" }, |
Jeremy Johnson | 7bf0cb9 | 2023-10-31 14:37:54 +0000 | [diff] [blame] | 55 | { Op::Op_MAXIMUM, "MAXIMUM" }, |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 56 | { Op::Op_MAX_POOL2D, "MAX_POOL2D" }, |
Jeremy Johnson | 7bf0cb9 | 2023-10-31 14:37:54 +0000 | [diff] [blame] | 57 | { Op::Op_MINIMUM, "MINIMUM" }, |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 58 | { Op::Op_MUL, "MUL" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 59 | { Op::Op_NEGATE, "NEGATE" }, |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 60 | { Op::Op_PAD, "PAD" }, |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 61 | { Op::Op_POW, "POW" }, |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 62 | { Op::Op_RECIPROCAL, "RECIPROCAL" }, |
| 63 | { Op::Op_RSQRT, "RSQRT" }, |
Jeremy Johnson | bfc5303 | 2023-11-01 11:29:56 +0000 | [diff] [blame] | 64 | { Op::Op_REDUCE_MAX, "REDUCE_MAX" }, |
| 65 | { Op::Op_REDUCE_MIN, "REDUCE_MIN" }, |
| 66 | { Op::Op_REDUCE_SUM, "REDUCE_SUM" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame^] | 67 | { Op::Op_SIGMOID, "SIGMOID" }, |
Jeremy Johnson | 7bf0cb9 | 2023-10-31 14:37:54 +0000 | [diff] [blame] | 68 | { Op::Op_SUB, "SUB" }, |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame^] | 69 | { Op::Op_TANH, "TANH" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 70 | }) |
| 71 | |
| 72 | } // namespace tosa |
| 73 | |
| 74 | namespace TosaReference |
| 75 | { |
| 76 | |
| 77 | NLOHMANN_JSON_SERIALIZE_ENUM(GeneratorType, |
| 78 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 79 | { GeneratorType::Unknown, "UNKNOWN" }, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 80 | { 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 Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 87 | // NOTE: This assumes it's VARIABLE if the InputType is not recognized |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 88 | NLOHMANN_JSON_SERIALIZE_ENUM(InputType, |
| 89 | { |
| 90 | { InputType::Variable, "VARIABLE" }, |
| 91 | { InputType::Constant, "CONSTANT" }, |
| 92 | }) |
| 93 | |
| 94 | void 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 Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 109 | void from_json(const nlohmann::json& j, PseudoRandomInfo& pseudoRandomInfo) |
| 110 | { |
| 111 | j.at("rng_seed").get_to(pseudoRandomInfo.rngSeed); |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 112 | if (j.contains("range")) |
| 113 | { |
| 114 | j.at("range").get_to(pseudoRandomInfo.range); |
| 115 | } |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 118 | void 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 Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 130 | if (j.contains("pseudo_random_info")) |
| 131 | { |
| 132 | j.at("pseudo_random_info").get_to(cfg.pseudoRandomInfo); |
| 133 | } |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | std::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 Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 144 | { |
| 145 | WARNING("[Generator] Invalid json config."); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 146 | return std::nullopt; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 147 | } |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 148 | if (!jsonCfg.contains("tensors")) |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 149 | { |
| 150 | WARNING("[Generator] Missing tensors in json config."); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 151 | return std::nullopt; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 152 | } |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 153 | const auto& tensors = jsonCfg["tensors"]; |
| 154 | if (!tensors.contains(tensorName)) |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 155 | { |
| 156 | WARNING("[Generator] Missing tensor %s in json config.", tensorName); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 157 | return std::nullopt; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 158 | } |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 159 | const auto& namedTensor = tensors[tensorName]; |
| 160 | return namedTensor.get<GenerateConfig>(); |
| 161 | } |
| 162 | |
| 163 | int64_t numElementsFromShape(const std::vector<int32_t>& shape) |
| 164 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 165 | // Rank 0 shapes have no entries and so this will return 1 |
| 166 | // Other ranked shapes will return the product of their dimensions |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 167 | return std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int64_t>()); |
| 168 | } |
| 169 | |
| 170 | size_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 |