Jeremy Johnson | 4f93130 | 2024-01-04 17:05:24 +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 | #include "generate.h" |
Jeremy Johnson | 4f93130 | 2024-01-04 17:05:24 +0000 | [diff] [blame] | 15 | #include "half.hpp" |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 16 | |
| 17 | #include <doctest.h> |
| 18 | |
| 19 | #include <array> |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 20 | #include <sstream> |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 24 | namespace |
| 25 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 26 | void update_json_template(std::string& str, const std::string& find, const std::string& change) |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 27 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 28 | // Update the 'str' by looking for instances of 'find' and replacing them with 'change' |
| 29 | auto pos = str.find(find); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 30 | while (pos != std::string::npos) |
| 31 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 32 | str.replace(pos, find.length(), change); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 33 | pos = str.find(find); |
| 34 | } |
| 35 | } |
| 36 | |
Jeremy Johnson | 4f93130 | 2024-01-04 17:05:24 +0000 | [diff] [blame] | 37 | template <typename T> |
| 38 | void check_value(bool match, T result, T expected, uint32_t idx) |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 39 | { |
| 40 | std::stringstream msg; |
| 41 | msg << "index: " << idx << " expected: " << std::hex << expected << " got: " << result; |
| 42 | if (match) |
| 43 | { |
| 44 | REQUIRE_MESSAGE(expected == result, msg.str()); |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | REQUIRE_MESSAGE(expected != result, msg.str()); |
| 49 | } |
| 50 | } |
| 51 | |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 52 | template <typename T> |
| 53 | void check_output(const std::vector<T>& results, const std::vector<uint32_t>& expected) |
| 54 | { |
| 55 | for (size_t idx = 0; idx < expected.size(); ++idx) |
| 56 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 57 | check_value(true, *(uint32_t*)&results[idx], expected[idx], idx); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 61 | template <typename T> |
Jeremy Johnson | 4f93130 | 2024-01-04 17:05:24 +0000 | [diff] [blame] | 62 | void check_output(const std::vector<T>& results, const std::vector<uint16_t>& expected) |
| 63 | { |
| 64 | for (size_t idx = 0; idx < expected.size(); ++idx) |
| 65 | { |
| 66 | check_value(true, *(uint16_t*)&results[idx], expected[idx], idx); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | template <typename T> |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 71 | void check_output(const std::vector<T>& results, const std::vector<T>& expected) |
| 72 | { |
| 73 | for (size_t idx = 0; idx < expected.size(); ++idx) |
| 74 | { |
| 75 | check_value(true, *(uint32_t*)&results[idx], *(uint32_t*)&expected[idx], idx); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | template <typename T> |
| 80 | void check_not_output(const std::vector<T>& results, const std::vector<T>& expected) |
| 81 | { |
| 82 | for (size_t idx = 0; idx < expected.size(); ++idx) |
| 83 | { |
| 84 | check_value(false, *(uint32_t*)&results[idx], *(uint32_t*)&expected[idx], idx); |
| 85 | } |
| 86 | } |
| 87 | |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 88 | } // namespace |
| 89 | |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 90 | TEST_SUITE_BEGIN("generate"); |
| 91 | |
| 92 | TEST_CASE("negative - api") |
| 93 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 94 | std::string templateJsonCfg = R"({ |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 95 | "tensors" : { |
| 96 | "in1" : { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 97 | "generator": "_GENERATOR_", |
| 98 | "data_type": "_TYPE_", |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 99 | "input_type": "VARIABLE", |
| 100 | "shape" : [ 4, 8, 8 ], |
| 101 | "input_pos": 0, |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 102 | "op" : "_OP_", |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 103 | "dot_product_info": { |
| 104 | "s": 0, |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 105 | "ks": 8, |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 106 | "acc_type": "_TYPE_" |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | })"; |
| 111 | |
| 112 | const std::string tosaName = "in1"; |
| 113 | const size_t tosaElements = 4 * 8 * 8; |
| 114 | const size_t tosaSize = tosaElements * 4; |
| 115 | |
| 116 | SUBCASE("missing input") |
| 117 | { |
| 118 | REQUIRE_FALSE(tgd_generate_data(NULL, NULL, NULL, 0)); |
| 119 | } |
| 120 | SUBCASE("invalid json") |
| 121 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 122 | std::string invalidJsonCfg = R"({ |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 123 | "tensors" : { |
| 124 | "in1" : { |
| 125 | "generator": DOT_PRODUCT, |
| 126 | }, |
| 127 | } |
| 128 | })"; |
| 129 | |
| 130 | std::vector<float> buffer(tosaElements); |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 131 | REQUIRE_FALSE(tgd_generate_data(invalidJsonCfg.c_str(), tosaName.c_str(), (void*)buffer.data(), tosaSize)); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 132 | } |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 133 | SUBCASE("unknown generator") |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 134 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 135 | std::string jsonCfg = templateJsonCfg; |
| 136 | update_json_template(jsonCfg, "_GENERATOR_", "SOLAR"); |
| 137 | update_json_template(jsonCfg, "_TYPE_", "FP32"); |
| 138 | update_json_template(jsonCfg, "_OP_", "MATMUL"); |
| 139 | std::vector<float> buffer(tosaElements); |
| 140 | REQUIRE_FALSE(tgd_generate_data(jsonCfg.c_str(), tosaName.c_str(), (void*)buffer.data(), tosaSize)); |
| 141 | } |
| 142 | SUBCASE("unknown op") |
| 143 | { |
| 144 | std::string jsonCfg = templateJsonCfg; |
| 145 | update_json_template(jsonCfg, "_GENERATOR_", "DOT_PRODUCT"); |
| 146 | update_json_template(jsonCfg, "_TYPE_", "FP32"); |
| 147 | update_json_template(jsonCfg, "_OP_", "GREEN"); |
| 148 | |
| 149 | std::vector<float> buffer(tosaElements); |
| 150 | REQUIRE_FALSE(tgd_generate_data(jsonCfg.c_str(), tosaName.c_str(), (void*)buffer.data(), tosaSize)); |
| 151 | } |
| 152 | SUBCASE("unknown type") |
| 153 | { |
| 154 | std::string jsonCfg = templateJsonCfg; |
| 155 | update_json_template(jsonCfg, "_GENERATOR_", "DOT_PRODUCT"); |
| 156 | update_json_template(jsonCfg, "_TYPE_", "WATT"); |
| 157 | update_json_template(jsonCfg, "_OP_", "MATMUL"); |
| 158 | |
| 159 | std::vector<float> buffer(tosaElements); |
| 160 | REQUIRE_FALSE(tgd_generate_data(jsonCfg.c_str(), tosaName.c_str(), (void*)buffer.data(), tosaSize)); |
| 161 | } |
| 162 | SUBCASE("mismatching name") |
| 163 | { |
| 164 | std::string jsonCfg = templateJsonCfg; |
| 165 | update_json_template(jsonCfg, "_GENERATOR_", "DOT_PRODUCT"); |
| 166 | update_json_template(jsonCfg, "_TYPE_", "FP32"); |
| 167 | update_json_template(jsonCfg, "_OP_", "MATMUL"); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 168 | std::string invalidName = "notFound1"; |
| 169 | |
| 170 | std::vector<float> buffer(tosaElements); |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 171 | REQUIRE_FALSE(tgd_generate_data(jsonCfg.c_str(), invalidName.c_str(), (void*)buffer.data(), tosaSize)); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 172 | } |
| 173 | SUBCASE("mismatching size") |
| 174 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 175 | std::string jsonCfg = templateJsonCfg; |
| 176 | update_json_template(jsonCfg, "_GENERATOR_", "DOT_PRODUCT"); |
| 177 | update_json_template(jsonCfg, "_TYPE_", "FP32"); |
| 178 | update_json_template(jsonCfg, "_OP_", "MATMUL"); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 179 | size_t smallElements = 4 * 8 * 7; |
| 180 | size_t smallSize = smallElements * 4; |
| 181 | |
| 182 | std::vector<float> buffer(smallElements); |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 183 | REQUIRE_FALSE(tgd_generate_data(jsonCfg.c_str(), tosaName.c_str(), (void*)buffer.data(), smallSize)); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 187 | void matmul_test_FP32(const std::string tosaName[2], |
| 188 | const size_t tosaElements[2], |
| 189 | const std::string templateJsonCfg, |
| 190 | const std::string setStr, |
| 191 | int32_t param, |
| 192 | const std::vector<uint32_t> expected) |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 193 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 194 | std::string jsonCfg = templateJsonCfg; |
| 195 | update_json_template(jsonCfg, "_SET_", setStr); |
| 196 | std::vector<float> buffer(tosaElements[param]); |
| 197 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName[param].c_str(), (void*)buffer.data(), tosaElements[param] * 4)); |
| 198 | check_output<float>(buffer, expected); |
| 199 | } |
| 200 | |
| 201 | TEST_CASE("positive - FP32 matmul dot product (first 3 values)") |
| 202 | { |
| 203 | std::string templateJsonCfg = R"({ |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 204 | "tensors" : { |
| 205 | "in1" : { |
| 206 | "generator": "DOT_PRODUCT", |
| 207 | "data_type": "FP32", |
| 208 | "input_type": "VARIABLE", |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 209 | "shape" : [ 4, 8, 2 ], |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 210 | "input_pos": 0, |
| 211 | "op" : "MATMUL", |
| 212 | "dot_product_info": { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 213 | "s": _SET_, |
| 214 | "ks": 2, |
| 215 | "acc_type": "FP32" |
| 216 | } |
| 217 | }, |
| 218 | "in2" : { |
| 219 | "generator": "DOT_PRODUCT", |
| 220 | "data_type": "FP32", |
| 221 | "input_type": "VARIABLE", |
| 222 | "shape" : [ 4, 2, 5 ], |
| 223 | "input_pos": 1, |
| 224 | "op" : "MATMUL", |
| 225 | "dot_product_info": { |
| 226 | "s": _SET_, |
| 227 | "ks": 2, |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 228 | "acc_type": "FP32" |
| 229 | } |
| 230 | } |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 231 | |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 232 | } |
| 233 | })"; |
| 234 | |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 235 | const std::string tosaName[2] = { "in1", "in2" }; |
| 236 | const size_t tosaElements[2] = { (4 * 8 * 2), (4 * 2 * 5) }; |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 237 | |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 238 | SUBCASE("matmul, set 0, param 0") |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 239 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 240 | std::vector<uint32_t> expected = { 0xbf665aa4, 0xbf736bd3, 0x0 }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 241 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", 0, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 242 | } |
| 243 | SUBCASE("matmul, set 0, param 1") |
| 244 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 245 | std::vector<uint32_t> expected = { 0x0, 0x0, 0x3f34f2dd }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 246 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", 1, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 247 | } |
| 248 | SUBCASE("matmul, set 1, param 0") |
| 249 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 250 | std::vector<uint32_t> expected = { 0x5e97f1b0, 0x5ea6a18e, 0x5eb811af }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 251 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", 0, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 252 | } |
| 253 | SUBCASE("matmul, set 1, param 1") |
| 254 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 255 | std::vector<uint32_t> expected = { 0x5f128bb1, 0x5ef54579, 0x5ebd65b8 }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 256 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", 1, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 257 | } |
| 258 | SUBCASE("matmul, set 2, param 0") |
| 259 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 260 | std::vector<uint32_t> expected = { 0x3f800000, 0x3e66ed53, 0x3f800000 }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 261 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", 0, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 262 | } |
| 263 | SUBCASE("matmul, set 2, param 1") |
| 264 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 265 | std::vector<uint32_t> expected = { 0x3f800000, 0x3f800000, 0x3f800000 }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 266 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", 1, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 267 | } |
| 268 | SUBCASE("matmul, set 3, param 0") |
| 269 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 270 | // NOTE: Python test script produced 0xbf256686 - so off by 1 |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 271 | std::vector<uint32_t> expected = { 0x41800000, 0xbf256685, 0x41800000 }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 272 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", 0, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 273 | } |
| 274 | SUBCASE("matmul, set 3, param 1") |
| 275 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 276 | std::vector<uint32_t> expected = { 0x41800000, 0x41800000, 0x41800000 }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 277 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", 1, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 278 | } |
| 279 | SUBCASE("matmul, set 4, param 0") |
| 280 | { |
Jeremy Johnson | 0601f80 | 2023-11-08 16:28:09 +0000 | [diff] [blame] | 281 | std::vector<uint32_t> expected = { 0x0, 0xbf000000, 0x5f14e80c }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 282 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", 0, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 283 | } |
| 284 | SUBCASE("matmul, set 4, param 1") |
| 285 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 286 | std::vector<uint32_t> expected = { 0x5d5d0db2, 0xdf2c82a8, 0x0 }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 287 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", 1, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 288 | } |
| 289 | SUBCASE("matmul, set 5, param 0") |
| 290 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 291 | std::vector<uint32_t> expected = { 0x5df6c4b3, 0x5e6b4088, 0x5ed0fe71 }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 292 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", 0, expected); |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 293 | } |
| 294 | SUBCASE("matmul, set 5, param 1") |
| 295 | { |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 296 | std::vector<uint32_t> expected = { 0xde086d85, 0x5e630878, 0x5eba5c7b }; |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 297 | matmul_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", 1, expected); |
Jeremy Johnson | b20b0c9 | 2023-10-04 14:17:55 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
Jeremy Johnson | d1a08ce | 2023-10-18 17:22:21 +0100 | [diff] [blame] | 300 | |
| 301 | void conv2d_test_FP32(const std::string tosaName[3], |
| 302 | const size_t tosaElements[3], |
| 303 | const std::string templateJsonCfg, |
| 304 | const std::string setStr, |
| 305 | int32_t param, |
| 306 | const std::vector<uint32_t> lastExpected) |
| 307 | { |
| 308 | std::string jsonCfg = templateJsonCfg; |
| 309 | update_json_template(jsonCfg, "_SET_", setStr); |
| 310 | |
| 311 | std::vector<float> buffer(tosaElements[param]); |
| 312 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName[param].c_str(), (void*)buffer.data(), tosaElements[param] * 4)); |
| 313 | std::vector<float> last_three(buffer.end() - std::min<int>(3, buffer.size()), buffer.end()); |
| 314 | check_output<float>(last_three, lastExpected); |
| 315 | } |
| 316 | |
| 317 | TEST_CASE("positive - FP32 conv2d dot product (last 3 values)") |
| 318 | { |
| 319 | std::string templateJsonCfg = R"({ |
| 320 | "tensors" : { |
| 321 | "input" : { |
| 322 | "generator": "DOT_PRODUCT", |
| 323 | "data_type": "FP32", |
| 324 | "input_type": "VARIABLE", |
| 325 | "shape" : [ 1, 8, 2, 4 ], |
| 326 | "input_pos": 0, |
| 327 | "op" : "CONV2D", |
| 328 | "dot_product_info": { |
| 329 | "s": _SET_, |
| 330 | "ks": 16, |
| 331 | "acc_type": "FP32", |
| 332 | "kernel": [2, 2] |
| 333 | } |
| 334 | }, |
| 335 | "weight" : { |
| 336 | "generator": "DOT_PRODUCT", |
| 337 | "data_type": "FP32", |
| 338 | "input_type": "CONSTANT", |
| 339 | "shape" : [ 2, 2, 2, 4 ], |
| 340 | "input_pos": 1, |
| 341 | "op" : "CONV2D", |
| 342 | "dot_product_info": { |
| 343 | "s": _SET_, |
| 344 | "ks": 16, |
| 345 | "acc_type": "FP32" |
| 346 | } |
| 347 | }, |
| 348 | "bias" : { |
| 349 | "generator": "DOT_PRODUCT", |
| 350 | "data_type": "FP32", |
| 351 | "input_type": "CONSTANT", |
| 352 | "shape" : [ 2 ], |
| 353 | "input_pos": 2, |
| 354 | "op" : "CONV2D", |
| 355 | "dot_product_info": { |
| 356 | "s": _SET_, |
| 357 | "ks": 16, |
| 358 | "acc_type": "FP32" |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | } |
| 363 | })"; |
| 364 | |
| 365 | const std::string tosaName[3] = { "input", "weight", "bias" }; |
| 366 | const size_t tosaElements[3] = { (1 * 8 * 2 * 4), (2 * 2 * 2 * 4), 2 }; |
| 367 | |
| 368 | SUBCASE("conv2d, set 0, param 0") |
| 369 | { |
| 370 | std::vector<uint32_t> lastExpected = { 0x0, 0xbf28bfda, 0xbe99cd47 }; |
| 371 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", 0, lastExpected); |
| 372 | } |
| 373 | SUBCASE("conv2d, set 0, param 1") |
| 374 | { |
| 375 | std::vector<uint32_t> lastExpected = { 0x0, 0x3f648dfd, 0xbd4cb21c }; |
| 376 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", 1, lastExpected); |
| 377 | } |
| 378 | SUBCASE("conv2d, set 0, param 2") |
| 379 | { |
| 380 | std::vector<uint32_t> lastExpected = { 0x0, 0x0 }; |
| 381 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", 2, lastExpected); |
| 382 | } |
| 383 | SUBCASE("conv2d, set 1, param 0") |
| 384 | { |
| 385 | std::vector<uint32_t> lastExpected = { 0x5e6f0400, 0x5e2f78e5, 0x5e62318d }; |
| 386 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", 0, lastExpected); |
| 387 | } |
| 388 | SUBCASE("conv2d, set 1, param 1") |
| 389 | { |
| 390 | // NOTE: Python test script produced 0x5e6960b0 - so off by 1 |
| 391 | std::vector<uint32_t> lastExpected = { 0x5e6960af, 0x5e6d0ca9, 0x5e0b8561 }; |
| 392 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", 1, lastExpected); |
| 393 | } |
| 394 | SUBCASE("conv2d, set 1, param 2") |
| 395 | { |
| 396 | // NOTE: Python test script produced 0x7cf260d0, 0x7d355432 - so off by 1 |
| 397 | std::vector<uint32_t> lastExpected = { 0x7cf260d1, 0x7d355431 }; |
| 398 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", 2, lastExpected); |
| 399 | } |
| 400 | SUBCASE("conv2d, set 2, param 0") |
| 401 | { |
| 402 | std::vector<uint32_t> lastExpected = { 0x3e7da8e9, 0x3df76a57, 0xbe338212 }; |
| 403 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", 0, lastExpected); |
| 404 | } |
| 405 | SUBCASE("conv2d, set 2, param 1") |
| 406 | { |
| 407 | std::vector<uint32_t> lastExpected = { 0x3daabbc5, 0xbe2f8909, 0xbdb806ec }; |
| 408 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", 1, lastExpected); |
| 409 | } |
| 410 | SUBCASE("conv2d, set 2, param 2") |
| 411 | { |
| 412 | std::vector<uint32_t> lastExpected = { 0x0, 0x0 }; |
| 413 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", 2, lastExpected); |
| 414 | } |
| 415 | SUBCASE("conv2d, set 3, param 0") |
| 416 | { |
| 417 | std::vector<uint32_t> lastExpected = { 0xbee77fe5, 0x402141c5, 0xbda1b2ed }; |
| 418 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", 0, lastExpected); |
| 419 | } |
| 420 | SUBCASE("conv2d, set 3, param 1") |
| 421 | { |
| 422 | // NOTE: Python test script produced 0xbe9947ac - so off by 1 |
| 423 | std::vector<uint32_t> lastExpected = { 0x3f91e619, 0x3e9ac66b, 0xbe9947ad }; |
| 424 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", 1, lastExpected); |
| 425 | } |
| 426 | SUBCASE("conv2d, set 3, param 2") |
| 427 | { |
| 428 | std::vector<uint32_t> lastExpected = { 0x0, 0x0 }; |
| 429 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", 2, lastExpected); |
| 430 | } |
| 431 | SUBCASE("conv2d, set 4, param 0") |
| 432 | { |
| 433 | std::vector<uint32_t> lastExpected = { 0xdd7e8575, 0x0, 0xde569ff3 }; |
| 434 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", 0, lastExpected); |
| 435 | } |
| 436 | SUBCASE("conv2d, set 4, param 1") |
| 437 | { |
| 438 | std::vector<uint32_t> lastExpected = { 0x5e2d6921, 0x5e13a014, 0x0 }; |
| 439 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", 1, lastExpected); |
| 440 | } |
| 441 | SUBCASE("conv2d, set 4, param 2") |
| 442 | { |
| 443 | std::vector<uint32_t> lastExpected = { 0x0, 0x0 }; |
| 444 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", 2, lastExpected); |
| 445 | } |
| 446 | SUBCASE("conv2d, set 5, param 0") |
| 447 | { |
| 448 | std::vector<uint32_t> lastExpected = { 0x5e719fb9, 0x5e6b329c, 0xdd7617d4 }; |
| 449 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", 0, lastExpected); |
| 450 | } |
| 451 | SUBCASE("conv2d, set 5, param 1") |
| 452 | { |
| 453 | std::vector<uint32_t> lastExpected = { 0xde42f57a, 0x5dd68799, 0xde2ddfcb }; |
| 454 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", 1, lastExpected); |
| 455 | } |
| 456 | SUBCASE("conv2d, set 5, param 2") |
| 457 | { |
| 458 | std::vector<uint32_t> lastExpected = { 0x0, 0x0 }; |
| 459 | conv2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", 2, lastExpected); |
| 460 | } |
| 461 | } |
Jeremy Johnson | a8420ad | 2023-12-07 16:35:28 +0000 | [diff] [blame] | 462 | TEST_CASE("positive - FP32 pseudo random") |
Jeremy Johnson | d41feb7 | 2023-10-12 16:03:15 +0100 | [diff] [blame] | 463 | { |
| 464 | std::string templateJsonCfg = R"({ |
| 465 | "tensors" : { |
| 466 | "input0" : { |
| 467 | "generator": "PSEUDO_RANDOM", |
| 468 | "data_type": "FP32", |
| 469 | "input_type": "VARIABLE", |
| 470 | "shape" : [ 12, 3 ], |
| 471 | "input_pos": 0, |
| 472 | "op" : "PAD", |
| 473 | "pseudo_random_info": { |
| 474 | "rng_seed": _SEED0_ |
| 475 | } |
| 476 | }, |
| 477 | "input1" : { |
| 478 | "generator": "PSEUDO_RANDOM", |
| 479 | "data_type": "FP32", |
| 480 | "input_type": "VARIABLE", |
| 481 | "shape" : [ 1, 3 ], |
| 482 | "input_pos": 1, |
| 483 | "op" : "PAD", |
| 484 | "pseudo_random_info": { |
| 485 | "rng_seed": _SEED1_ |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | } |
| 490 | })"; |
| 491 | |
| 492 | const std::string tosaNameP0 = "input0"; |
| 493 | const size_t tosaElementsP0 = 12 * 3; |
| 494 | const std::string tosaNameP1 = "input1"; |
| 495 | const size_t tosaElementsP1 = 1 * 3; |
| 496 | |
| 497 | SUBCASE("pad - same rng") |
| 498 | { |
| 499 | std::string jsonCfg = templateJsonCfg; |
| 500 | update_json_template(jsonCfg, "_SEED0_", "0"); |
| 501 | update_json_template(jsonCfg, "_SEED1_", "0"); |
| 502 | |
| 503 | std::vector<float> bufferP0(tosaElementsP0); |
| 504 | std::vector<float> bufferP1(tosaElementsP1); |
| 505 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaNameP0.c_str(), (void*)bufferP0.data(), tosaElementsP0 * 4)); |
| 506 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaNameP1.c_str(), (void*)bufferP1.data(), tosaElementsP1 * 4)); |
| 507 | check_output<float>(bufferP0, bufferP1); |
| 508 | } |
| 509 | |
| 510 | SUBCASE("pad - different rng") |
| 511 | { |
| 512 | std::string jsonCfg = templateJsonCfg; |
| 513 | update_json_template(jsonCfg, "_SEED0_", "0"); |
| 514 | update_json_template(jsonCfg, "_SEED1_", "1000"); |
| 515 | |
| 516 | std::vector<float> bufferP0(tosaElementsP0); |
| 517 | std::vector<float> bufferP1(tosaElementsP1); |
| 518 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaNameP0.c_str(), (void*)bufferP0.data(), tosaElementsP0 * 4)); |
| 519 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaNameP1.c_str(), (void*)bufferP1.data(), tosaElementsP1 * 4)); |
| 520 | check_not_output<float>(bufferP0, bufferP1); |
| 521 | } |
| 522 | } |
Jeremy Johnson | aee62af | 2023-11-02 17:16:25 +0000 | [diff] [blame] | 523 | |
Jeremy Johnson | bfc5303 | 2023-11-01 11:29:56 +0000 | [diff] [blame] | 524 | void reduce_sum_test_FP32(const std::string tosaName, |
| 525 | const size_t tosaElements, |
| 526 | const std::string templateJsonCfg, |
| 527 | const std::string setStr, |
| 528 | const std::vector<uint32_t> expected) |
| 529 | { |
| 530 | std::string jsonCfg = templateJsonCfg; |
| 531 | update_json_template(jsonCfg, "_SET_", setStr); |
| 532 | |
| 533 | std::vector<float> buffer(tosaElements); |
| 534 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName.c_str(), (void*)buffer.data(), tosaElements * 4)); |
| 535 | // Choose different generator values to test at positions 6, 7 & 8 |
| 536 | std::vector<float> mid_three(buffer.begin() + 6, buffer.begin() + 9); |
| 537 | check_output<float>(mid_three, expected); |
| 538 | } |
| 539 | |
| 540 | TEST_CASE("positive - FP32 reduce_sum dot product (values 6,7 & 8)") |
| 541 | { |
| 542 | std::string templateJsonCfg = R"({ |
| 543 | "tensors" : { |
| 544 | "input" : { |
| 545 | "generator": "DOT_PRODUCT", |
| 546 | "data_type": "FP32", |
| 547 | "input_type": "VARIABLE", |
| 548 | "shape" : [ 5, 3, 7 ], |
| 549 | "input_pos": 0, |
| 550 | "op" : "REDUCE_SUM", |
| 551 | "dot_product_info": { |
| 552 | "s": _SET_, |
| 553 | "ks": 3, |
| 554 | "acc_type": "FP32", |
| 555 | "axis": 1 |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | })"; |
| 560 | |
| 561 | const std::string tosaName = "input"; |
| 562 | const size_t tosaElements = 5 * 3 * 7; |
| 563 | |
| 564 | SUBCASE("reduce_sum, set 0, param 0") |
| 565 | { |
| 566 | std::vector<uint32_t> expected = { 0x3df2e612, 0x3f59255f, 0x0 }; |
| 567 | reduce_sum_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", expected); |
| 568 | } |
| 569 | SUBCASE("reduce_sum, set 1, param 0") |
| 570 | { |
| 571 | std::vector<uint32_t> expected = { 0x5edaa175, 0x5edb84c1, 0x5ea3c765 }; |
| 572 | reduce_sum_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", expected); |
| 573 | } |
| 574 | SUBCASE("reduce_sum, set 2, param 0") |
| 575 | { |
| 576 | std::vector<uint32_t> expected = { 0x3f800000, 0x3e73f143, 0x3f12cef8 }; |
| 577 | reduce_sum_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", expected); |
| 578 | } |
| 579 | SUBCASE("reduce_sum, set 3, param 0") |
| 580 | { |
| 581 | std::vector<uint32_t> expected = { 0x41800000, 0xbe9f659e, 0xbfaca78c }; |
| 582 | reduce_sum_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", expected); |
| 583 | } |
| 584 | SUBCASE("reduce_sum, set 4, param 0") |
| 585 | { |
Jeremy Johnson | 0601f80 | 2023-11-08 16:28:09 +0000 | [diff] [blame] | 586 | std::vector<uint32_t> expected = { 0x5e1e6f12, 0x3f000000, 0xbf000000 }; |
Jeremy Johnson | bfc5303 | 2023-11-01 11:29:56 +0000 | [diff] [blame] | 587 | reduce_sum_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", expected); |
| 588 | } |
| 589 | SUBCASE("reduce_sum, set 5, param 0") |
| 590 | { |
| 591 | std::vector<uint32_t> expected = { 0x5d2790c5, 0xdec3dadc, 0xdea1486e }; |
| 592 | reduce_sum_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", expected); |
| 593 | } |
| 594 | } |
Jeremy Johnson | aee62af | 2023-11-02 17:16:25 +0000 | [diff] [blame] | 595 | |
| 596 | void fully_connected_test_FP32(const std::string tosaName[3], |
| 597 | const size_t tosaElements[3], |
| 598 | const std::string templateJsonCfg, |
| 599 | const std::string setStr, |
| 600 | int32_t param, |
| 601 | const std::vector<uint32_t> lastExpected) |
| 602 | { |
| 603 | std::string jsonCfg = templateJsonCfg; |
| 604 | update_json_template(jsonCfg, "_SET_", setStr); |
| 605 | |
| 606 | std::vector<float> buffer(tosaElements[param]); |
| 607 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName[param].c_str(), (void*)buffer.data(), tosaElements[param] * 4)); |
| 608 | if (param != 2) |
| 609 | { |
| 610 | // Get values at positions -8, -7 and -6 from the end |
| 611 | std::vector<float> last_three_ish(buffer.end() - 8, buffer.end() - 5); |
| 612 | check_output<float>(last_three_ish, lastExpected); |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | // Use last three as this buffer is too small |
| 617 | std::vector<float> last_three(buffer.end() - std::min<int>(3, buffer.size()), buffer.end()); |
| 618 | check_output<float>(last_three, lastExpected); |
| 619 | } |
| 620 | } |
| 621 | TEST_CASE("positive - FP32 fully_connected dot product (values -8, -7 & -6 from the end)") |
| 622 | { |
| 623 | std::string templateJsonCfg = R"({ |
| 624 | "tensors" : { |
| 625 | "input" : { |
| 626 | "generator": "DOT_PRODUCT", |
| 627 | "data_type": "FP32", |
| 628 | "input_type": "VARIABLE", |
| 629 | "shape" : [ 6, 9 ], |
| 630 | "input_pos": 0, |
| 631 | "op" : "FULLY_CONNECTED", |
| 632 | "dot_product_info": { |
| 633 | "s": _SET_, |
| 634 | "ks": 9, |
| 635 | "acc_type": "FP32" |
| 636 | } |
| 637 | }, |
| 638 | "weight" : { |
| 639 | "generator": "DOT_PRODUCT", |
| 640 | "data_type": "FP32", |
| 641 | "input_type": "CONSTANT", |
| 642 | "shape" : [ 4, 9 ], |
| 643 | "input_pos": 1, |
| 644 | "op" : "FULLY_CONNECTED", |
| 645 | "dot_product_info": { |
| 646 | "s": _SET_, |
| 647 | "ks": 9, |
| 648 | "acc_type": "FP32" |
| 649 | } |
| 650 | }, |
| 651 | "bias" : { |
| 652 | "generator": "DOT_PRODUCT", |
| 653 | "data_type": "FP32", |
| 654 | "input_type": "CONSTANT", |
| 655 | "shape" : [ 4 ], |
| 656 | "input_pos": 2, |
| 657 | "op" : "FULLY_CONNECTED", |
| 658 | "dot_product_info": { |
| 659 | "s": _SET_, |
| 660 | "ks": 9, |
| 661 | "acc_type": "FP32" |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | } |
| 666 | })"; |
| 667 | |
| 668 | const std::string tosaName[3] = { "input", "weight", "bias" }; |
| 669 | const size_t tosaElements[3] = { (6 * 9), (4 * 9), 4 }; |
| 670 | |
| 671 | SUBCASE("fully_connected, set 0, param 0") |
| 672 | { |
| 673 | std::vector<uint32_t> lastExpected = { 0x3f13876f, 0x0, 0x0 }; |
| 674 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", 0, lastExpected); |
| 675 | } |
| 676 | SUBCASE("fully_connected, set 0, param 1") |
| 677 | { |
| 678 | std::vector<uint32_t> lastExpected = { 0x0, 0x0, 0x3f648dfd }; |
| 679 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", 1, lastExpected); |
| 680 | } |
| 681 | SUBCASE("fully_connected, set 0, param 2") |
| 682 | { |
| 683 | std::vector<uint32_t> lastExpected = { 0x0, 0x0, 0x0 }; |
| 684 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", 2, lastExpected); |
| 685 | } |
| 686 | SUBCASE("fully_connected, set 1, param 0") |
| 687 | { |
| 688 | // NOTE: Python test script produced 0x5e6cc8d7 - so off by 1 |
| 689 | std::vector<uint32_t> lastExpected = { 0x5e531bbf, 0x5e6cc8d8, 0x5e4e2539 }; |
| 690 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", 0, lastExpected); |
| 691 | } |
| 692 | SUBCASE("fully_connected, set 1, param 1") |
| 693 | { |
| 694 | std::vector<uint32_t> lastExpected = { 0x5e9870df, 0x5e9824c5, 0x5e9a898f }; |
| 695 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", 1, lastExpected); |
| 696 | } |
| 697 | SUBCASE("fully_connected, set 1, param 2") |
| 698 | { |
| 699 | // NOTE: Python test script produced 0x7dc95352 - so off by 1 |
| 700 | std::vector<uint32_t> lastExpected = { 0x7d9a212a, 0x7dc95351, 0x7db7c1f2 }; |
| 701 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", 2, lastExpected); |
| 702 | } |
| 703 | SUBCASE("fully_connected, set 2, param 0") |
| 704 | { |
| 705 | std::vector<uint32_t> lastExpected = { 0xbcc1e987, 0xbe68efd7, 0x3db90130 }; |
| 706 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", 0, lastExpected); |
| 707 | } |
| 708 | SUBCASE("fully_connected, set 2, param 1") |
| 709 | { |
| 710 | std::vector<uint32_t> lastExpected = { 0x3e069935, 0x3de3a507, 0xbe6a0c0c }; |
| 711 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", 1, lastExpected); |
| 712 | } |
| 713 | SUBCASE("fully_connected, set 2, param 2") |
| 714 | { |
| 715 | std::vector<uint32_t> lastExpected = { 0x0, 0x0, 0x0 }; |
| 716 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", 2, lastExpected); |
| 717 | } |
| 718 | SUBCASE("fully_connected, set 3, param 0") |
| 719 | { |
| 720 | std::vector<uint32_t> lastExpected = { 0x3e57454e, 0x3b48e294, 0x3e889ece }; |
| 721 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", 0, lastExpected); |
| 722 | } |
| 723 | SUBCASE("fully_connected, set 3, param 1") |
| 724 | { |
| 725 | std::vector<uint32_t> lastExpected = { 0xbd20e608, 0x3f91e619, 0x3e9ac66b }; |
| 726 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", 1, lastExpected); |
| 727 | } |
| 728 | SUBCASE("fully_connected, set 3, param 2") |
| 729 | { |
| 730 | std::vector<uint32_t> lastExpected = { 0x0, 0x0, 0x0 }; |
| 731 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", 2, lastExpected); |
| 732 | } |
| 733 | SUBCASE("fully_connected, set 4, param 0") |
| 734 | { |
| 735 | std::vector<uint32_t> lastExpected = { 0x0, 0x5e29ad6d, 0x5e959eac }; |
| 736 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", 0, lastExpected); |
| 737 | } |
| 738 | SUBCASE("fully_connected, set 4, param 1") |
| 739 | { |
| 740 | std::vector<uint32_t> lastExpected = { 0x0, 0x5e6736d7, 0x5e44d571 }; |
| 741 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", 1, lastExpected); |
| 742 | } |
| 743 | SUBCASE("fully_connected, set 4, param 2") |
| 744 | { |
| 745 | std::vector<uint32_t> lastExpected = { 0x0, 0x0, 0x0 }; |
| 746 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", 2, lastExpected); |
| 747 | } |
| 748 | SUBCASE("fully_connected, set 5, param 0") |
| 749 | { |
| 750 | std::vector<uint32_t> lastExpected = { 0xde8b0d70, 0xdd51465a, 0x5e57c772 }; |
| 751 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", 0, lastExpected); |
| 752 | } |
| 753 | SUBCASE("fully_connected, set 5, param 1") |
| 754 | { |
| 755 | std::vector<uint32_t> lastExpected = { 0xddde72f1, 0xde7e31ff, 0x5e0bdb32 }; |
| 756 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", 1, lastExpected); |
| 757 | } |
| 758 | SUBCASE("fully_connected, set 5, param 2") |
| 759 | { |
| 760 | std::vector<uint32_t> lastExpected = { 0x0, 0x0, 0x0 }; |
| 761 | fully_connected_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", 2, lastExpected); |
| 762 | } |
| 763 | } |
Jeremy Johnson | 0601f80 | 2023-11-08 16:28:09 +0000 | [diff] [blame] | 764 | |
| 765 | void avg_pool2d_test_FP32(const std::string tosaName, |
| 766 | const size_t tosaElements, |
| 767 | const std::string templateJsonCfg, |
| 768 | const std::string setStr, |
| 769 | const std::vector<uint32_t> expected) |
| 770 | { |
| 771 | std::string jsonCfg = templateJsonCfg; |
| 772 | update_json_template(jsonCfg, "_SET_", setStr); |
| 773 | |
| 774 | std::vector<float> buffer(tosaElements); |
| 775 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName.c_str(), (void*)buffer.data(), tosaElements * 4)); |
| 776 | std::vector<float> first_three(buffer.begin(), buffer.begin() + 3); |
| 777 | check_output<float>(first_three, expected); |
| 778 | } |
| 779 | |
| 780 | TEST_CASE("positive - FP32 avg_pool2d dot product (first 3 values)") |
| 781 | { |
| 782 | std::string templateJsonCfg = R"({ |
| 783 | "tensors" : { |
| 784 | "input" : { |
| 785 | "generator": "DOT_PRODUCT", |
| 786 | "data_type": "FP32", |
| 787 | "input_type": "VARIABLE", |
| 788 | "shape" : [ 2, 6, 2, 3 ], |
| 789 | "input_pos": 0, |
| 790 | "op" : "AVG_POOL2D", |
| 791 | "dot_product_info": { |
| 792 | "s": _SET_, |
| 793 | "ks": 3, |
| 794 | "acc_type": "FP32", |
| 795 | "kernel": [3, 1] |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | })"; |
| 800 | |
| 801 | const std::string tosaName = "input"; |
| 802 | const size_t tosaElements = 2 * 6 * 2 * 3; |
| 803 | |
| 804 | SUBCASE("avg_pool2d, set 0, param 0") |
| 805 | { |
| 806 | std::vector<uint32_t> expected = { 0xbf665aa4, 0xbf736bd3, 0x0 }; |
| 807 | avg_pool2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "0", expected); |
| 808 | } |
| 809 | SUBCASE("avg_pool2d, set 1, param 0") |
| 810 | { |
| 811 | // NOTE: Python test script produced 0x5e839663,0x5e9f6894 - so off by 1 |
| 812 | std::vector<uint32_t> expected = { 0x5e839662, 0x5e904e86, 0x5e9f6893 }; |
| 813 | avg_pool2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "1", expected); |
| 814 | } |
| 815 | SUBCASE("avg_pool2d, set 2, param 0") |
| 816 | { |
| 817 | std::vector<uint32_t> expected = { 0x3f800000, 0x3e3c8d18, 0xbe813879 }; |
| 818 | avg_pool2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "2", expected); |
| 819 | } |
| 820 | SUBCASE("avg_pool2d, set 3, param 0") |
| 821 | { |
| 822 | // NOTE: Python test script produced 0xbf256686,0x3e1e8d3b - so off by 1 |
| 823 | std::vector<uint32_t> expected = { 0x41800000, 0xbf256685, 0x3e1e8d3b }; |
| 824 | avg_pool2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "3", expected); |
| 825 | } |
| 826 | SUBCASE("avg_pool2d, set 4, param 0") |
| 827 | { |
| 828 | std::vector<uint32_t> expected = { 0x0, 0xbf000000, 0x5ef329c7 }; |
| 829 | avg_pool2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "4", expected); |
| 830 | } |
| 831 | SUBCASE("avg_pool2d, set 5, param 0") |
| 832 | { |
| 833 | std::vector<uint32_t> expected = { 0x5dd5b529, 0x5e4bbbf9, 0x5eb4fe79 }; |
| 834 | avg_pool2d_test_FP32(tosaName, tosaElements, templateJsonCfg, "5", expected); |
| 835 | } |
| 836 | } |
Jeremy Johnson | a8420ad | 2023-12-07 16:35:28 +0000 | [diff] [blame] | 837 | |
| 838 | TEST_CASE("positive - INT32 pseudo random") |
| 839 | { |
| 840 | std::string templateJsonCfg = R"({ |
| 841 | "tensors" : { |
| 842 | "input0" : { |
| 843 | "generator": "PSEUDO_RANDOM", |
| 844 | "data_type": "INT32", |
| 845 | "input_type": "VARIABLE", |
| 846 | "shape" : [ 2, 12 ], |
| 847 | "input_pos": 0, |
| 848 | "op" : "SCATTER", |
| 849 | "pseudo_random_info": { |
| 850 | "rng_seed": 13, |
| 851 | "range": [ "-5", "5" ] |
| 852 | } |
| 853 | }, |
| 854 | "input1" : { |
| 855 | "generator": "PSEUDO_RANDOM", |
| 856 | "data_type": "INT32", |
| 857 | "input_type": "VARIABLE", |
| 858 | "shape" : [ 2, 10 ], |
| 859 | "input_pos": 1, |
| 860 | "op" : "SCATTER", |
| 861 | "pseudo_random_info": { |
| 862 | "rng_seed": 14, |
| 863 | "range": [ "0", "9" ] |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | } |
| 868 | })"; |
| 869 | |
| 870 | const std::string tosaNameP0 = "input0"; |
| 871 | const size_t tosaElementsP0 = 2 * 12; |
| 872 | const std::string tosaNameP1 = "input1"; |
| 873 | const size_t tosaElementsP1 = 2 * 10; |
| 874 | |
| 875 | SUBCASE("scatter - int32 random") |
| 876 | { |
| 877 | std::string jsonCfg = templateJsonCfg; |
| 878 | |
| 879 | std::vector<int32_t> bufferP0(tosaElementsP0); |
| 880 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaNameP0.c_str(), (void*)bufferP0.data(), tosaElementsP0 * 4)); |
| 881 | for (auto e = bufferP0.begin(); e < bufferP0.end(); ++e) |
| 882 | { |
| 883 | // Check the values are within range |
| 884 | bool withinRange = (*e >= -5 && *e <= 5); |
| 885 | REQUIRE(withinRange); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | SUBCASE("scatter - int32 row shuffle") |
| 890 | { |
| 891 | std::string jsonCfg = templateJsonCfg; |
| 892 | |
| 893 | std::vector<int32_t> bufferP1(tosaElementsP1); |
| 894 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaNameP1.c_str(), (void*)bufferP1.data(), tosaElementsP1 * 4)); |
| 895 | |
| 896 | std::vector<bool> set; |
| 897 | for (int32_t n = 0; n < 2; ++n) |
| 898 | { |
| 899 | set.assign(10, false); |
| 900 | for (int32_t i = 0; i < 10; ++i) |
| 901 | { |
| 902 | auto idx = bufferP1[i]; |
| 903 | // Check that the values in the buffer only occur once |
| 904 | REQUIRE(!set[idx]); |
| 905 | set[idx] = true; |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | } |
Jeremy Johnson | 4f93130 | 2024-01-04 17:05:24 +0000 | [diff] [blame] | 910 | void depthwise_conv2d_test_FP16(const std::string tosaName[3], |
| 911 | const size_t tosaElements[3], |
| 912 | const std::string templateJsonCfg, |
| 913 | const std::string setStr, |
| 914 | int32_t param, |
| 915 | const std::vector<uint16_t> expected) |
| 916 | { |
| 917 | std::string jsonCfg = templateJsonCfg; |
| 918 | update_json_template(jsonCfg, "_SET_", setStr); |
| 919 | |
| 920 | std::vector<half_float::half> buffer(tosaElements[param]); |
| 921 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName[param].c_str(), (void*)buffer.data(), tosaElements[param] * 2)); |
| 922 | check_output<half_float::half>(buffer, expected); |
| 923 | } |
| 924 | |
| 925 | TEST_CASE("positive - FP16 depthwise_conv2d dot product (first 3 values)") |
| 926 | { |
| 927 | std::string templateJsonCfg = R"({ |
| 928 | "tensors" : { |
| 929 | "input" : { |
| 930 | "generator": "DOT_PRODUCT", |
| 931 | "data_type": "FP16", |
| 932 | "input_type": "VARIABLE", |
| 933 | "shape" : [1, 6, 3, 4], |
| 934 | "input_pos": 0, |
| 935 | "op" : "DEPTHWISE_CONV2D", |
| 936 | "dot_product_info": { |
| 937 | "s": _SET_, |
| 938 | "ks": 3, |
| 939 | "acc_type": "FP16", |
| 940 | "kernel": [1, 3] |
| 941 | } |
| 942 | }, |
| 943 | "weight" : { |
| 944 | "generator": "DOT_PRODUCT", |
| 945 | "data_type": "FP16", |
| 946 | "input_type": "CONSTANT", |
| 947 | "shape" : [1, 3, 4, 2], |
| 948 | "input_pos": 1, |
| 949 | "op" : "DEPTHWISE_CONV2D", |
| 950 | "dot_product_info": { |
| 951 | "s": _SET_, |
| 952 | "ks": 3, |
| 953 | "acc_type": "FP16" |
| 954 | } |
| 955 | }, |
| 956 | "bias" : { |
| 957 | "generator": "DOT_PRODUCT", |
| 958 | "data_type": "FP16", |
| 959 | "input_type": "CONSTANT", |
| 960 | "shape" : [ 2 ], |
| 961 | "input_pos": 2, |
| 962 | "op" : "DEPTHWISE_CONV2D", |
| 963 | "dot_product_info": { |
| 964 | "s": _SET_, |
| 965 | "ks": 3, |
| 966 | "acc_type": "FP16" |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | } |
| 971 | })"; |
| 972 | |
| 973 | const std::string tosaName[3] = { "input", "weight", "bias" }; |
| 974 | const size_t tosaElements[3] = { (1 * 6 * 3 * 4), (1 * 3 * 4 * 2), 2 }; |
| 975 | |
| 976 | SUBCASE("depthwise_conv2d, set 0, param 0") |
| 977 | { |
| 978 | std::vector<uint16_t> expected = { 0xbb33, 0xbb9b, 0x0 }; |
| 979 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 0, expected); |
| 980 | } |
| 981 | SUBCASE("depthwise_conv2d, set 0, param 1") |
| 982 | { |
| 983 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x39a8 }; |
| 984 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 1, expected); |
| 985 | } |
| 986 | SUBCASE("depthwise_conv2d, set 0, param 2") |
| 987 | { |
| 988 | std::vector<uint16_t> expected = { 0x0, 0x0 }; |
| 989 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 2, expected); |
| 990 | } |
| 991 | SUBCASE("depthwise_conv2d, set 1, param 0") |
| 992 | { |
| 993 | std::vector<uint16_t> expected = { 0x541c, 0x5482, 0x54fb }; |
| 994 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 0, expected); |
| 995 | } |
| 996 | SUBCASE("depthwise_conv2d, set 1, param 1") |
| 997 | { |
| 998 | std::vector<uint16_t> expected = { 0x57ee, 0x56a2, 0x5520 }; |
| 999 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 1, expected); |
| 1000 | } |
| 1001 | SUBCASE("depthwise_conv2d, set 1, param 2") |
| 1002 | { |
| 1003 | std::vector<uint16_t> expected = { 0x7005, 0x7204 }; |
| 1004 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 2, expected); |
| 1005 | } |
| 1006 | SUBCASE("depthwise_conv2d, set 2, param 0") |
| 1007 | { |
| 1008 | std::vector<uint16_t> expected = { 0x3c00, 0x3c00, 0x3c00 }; |
| 1009 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 0, expected); |
| 1010 | } |
| 1011 | SUBCASE("depthwise_conv2d, set 2, param 1") |
| 1012 | { |
| 1013 | std::vector<uint16_t> expected = { 0x3c00, 0x3c00, 0x3c00 }; |
| 1014 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 1, expected); |
| 1015 | } |
| 1016 | SUBCASE("depthwise_conv2d, set 2, param 2") |
| 1017 | { |
| 1018 | std::vector<uint16_t> expected = { 0x0, 0x0 }; |
| 1019 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 2, expected); |
| 1020 | } |
| 1021 | SUBCASE("depthwise_conv2d, set 3, param 0") |
| 1022 | { |
| 1023 | std::vector<uint16_t> expected = { 0x4c00, 0x4c00, 0x4c00 }; |
| 1024 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 0, expected); |
| 1025 | } |
| 1026 | SUBCASE("depthwise_conv2d, set 3, param 1") |
| 1027 | { |
| 1028 | std::vector<uint16_t> expected = { 0x4c00, 0x4c00, 0x4c00 }; |
| 1029 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 1, expected); |
| 1030 | } |
| 1031 | SUBCASE("depthwise_conv2d, set 3, param 2") |
| 1032 | { |
| 1033 | std::vector<uint16_t> expected = { 0x0, 0x0 }; |
| 1034 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 2, expected); |
| 1035 | } |
| 1036 | SUBCASE("depthwise_conv2d, set 4, param 0") |
| 1037 | { |
| 1038 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x5798 }; |
| 1039 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 0, expected); |
| 1040 | } |
| 1041 | SUBCASE("depthwise_conv2d, set 4, param 1") |
| 1042 | { |
| 1043 | std::vector<uint16_t> expected = { 0x49a3, 0xd866, 0x0 }; |
| 1044 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 1, expected); |
| 1045 | } |
| 1046 | SUBCASE("depthwise_conv2d, set 4, param 2") |
| 1047 | { |
| 1048 | std::vector<uint16_t> expected = { 0x0, 0x0 }; |
| 1049 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 2, expected); |
| 1050 | } |
| 1051 | SUBCASE("depthwise_conv2d, set 5, param 0") |
| 1052 | { |
| 1053 | std::vector<uint16_t> expected = { 0x4ead, 0x525d, 0x55a7 }; |
| 1054 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 0, expected); |
| 1055 | } |
| 1056 | SUBCASE("depthwise_conv2d, set 5, param 1") |
| 1057 | { |
| 1058 | std::vector<uint16_t> expected = { 0xcf61, 0x5224, 0x550b }; |
| 1059 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 1, expected); |
| 1060 | } |
| 1061 | SUBCASE("depthwise_conv2d, set 5, param 2") |
| 1062 | { |
| 1063 | std::vector<uint16_t> expected = { 0x0, 0x0 }; |
| 1064 | depthwise_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 2, expected); |
| 1065 | } |
| 1066 | } |
| 1067 | |
Jeremy Johnson | 95a6710 | 2024-01-10 14:16:39 +0000 | [diff] [blame] | 1068 | void transpose_conv2d_test_FP16(const std::string tosaName[3], |
| 1069 | const size_t tosaElements[3], |
| 1070 | const std::string templateJsonCfg, |
| 1071 | const std::string setStr, |
| 1072 | int32_t param, |
| 1073 | const std::vector<uint16_t> lastExpected) |
| 1074 | { |
| 1075 | std::string jsonCfg = templateJsonCfg; |
| 1076 | update_json_template(jsonCfg, "_SET_", setStr); |
| 1077 | |
| 1078 | std::vector<half_float::half> buffer(tosaElements[param]); |
| 1079 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName[param].c_str(), (void*)buffer.data(), tosaElements[param] * 2)); |
| 1080 | std::vector<half_float::half> last_three(buffer.end() - std::min<int>(3, buffer.size()), buffer.end()); |
| 1081 | check_output<half_float::half>(last_three, lastExpected); |
| 1082 | } |
| 1083 | |
| 1084 | TEST_CASE("positive - FP16 transpose_conv2d dot product (last 3 values)") |
| 1085 | { |
| 1086 | std::string templateJsonCfg = R"({ |
| 1087 | "tensors" : { |
| 1088 | "input" : { |
| 1089 | "generator": "DOT_PRODUCT", |
| 1090 | "data_type": "FP16", |
| 1091 | "input_type": "VARIABLE", |
| 1092 | "shape" : [1, 5, 6, 3], |
| 1093 | "input_pos": 0, |
| 1094 | "op" : "TRANSPOSE_CONV2D", |
| 1095 | "dot_product_info": { |
| 1096 | "s": _SET_, |
| 1097 | "ks": 30, |
| 1098 | "acc_type": "FP16", |
| 1099 | "kernel": [5, 2] |
| 1100 | } |
| 1101 | }, |
| 1102 | "weight" : { |
| 1103 | "generator": "DOT_PRODUCT", |
| 1104 | "data_type": "FP16", |
| 1105 | "input_type": "CONSTANT", |
| 1106 | "shape" : [3, 5, 2, 3], |
| 1107 | "input_pos": 1, |
| 1108 | "op" : "TRANSPOSE_CONV2D", |
| 1109 | "dot_product_info": { |
| 1110 | "s": _SET_, |
| 1111 | "ks": 30, |
| 1112 | "acc_type": "FP16" |
| 1113 | } |
| 1114 | }, |
| 1115 | "bias" : { |
| 1116 | "generator": "DOT_PRODUCT", |
| 1117 | "data_type": "FP16", |
| 1118 | "input_type": "CONSTANT", |
| 1119 | "shape" : [3], |
| 1120 | "input_pos": 2, |
| 1121 | "op" : "TRANSPOSE_CONV2D", |
| 1122 | "dot_product_info": { |
| 1123 | "s": _SET_, |
| 1124 | "ks": 30, |
| 1125 | "acc_type": "FP16" |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | } |
| 1130 | })"; |
| 1131 | |
| 1132 | const std::string tosaName[3] = { "input", "weight", "bias" }; |
| 1133 | const size_t tosaElements[3] = { (1 * 5 * 6 * 3), (3 * 5 * 2 * 3), 3 }; |
| 1134 | |
| 1135 | SUBCASE("transpose_conv2d, set 0, param 0") |
| 1136 | { |
| 1137 | std::vector<uint16_t> expected = { 0x30e3, 0x0, 0x38e7 }; |
| 1138 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 0, expected); |
| 1139 | } |
| 1140 | SUBCASE("transpose_conv2d, set 0, param 1") |
| 1141 | { |
| 1142 | std::vector<uint16_t> expected = { 0x0, 0x312a, 0x0 }; |
| 1143 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 1, expected); |
| 1144 | } |
| 1145 | SUBCASE("transpose_conv2d, set 0, param 2") |
| 1146 | { |
| 1147 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1148 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 2, expected); |
| 1149 | } |
| 1150 | SUBCASE("transpose_conv2d, set 1, param 0") |
| 1151 | { |
| 1152 | std::vector<uint16_t> expected = { 0x4eb3, 0x4fce, 0x4e4e }; |
| 1153 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 0, expected); |
| 1154 | } |
| 1155 | SUBCASE("transpose_conv2d, set 1, param 1") |
| 1156 | { |
| 1157 | std::vector<uint16_t> expected = { 0x4e79, 0x4ed8, 0x502e }; |
| 1158 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 1, expected); |
| 1159 | } |
| 1160 | SUBCASE("transpose_conv2d, set 1, param 2") |
| 1161 | { |
| 1162 | std::vector<uint16_t> expected = { 0x6426, 0x6635, 0x680e }; |
| 1163 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 2, expected); |
| 1164 | } |
| 1165 | SUBCASE("transpose_conv2d, set 2, param 0") |
| 1166 | { |
| 1167 | std::vector<uint16_t> expected = { 0xb05d, 0xaf37, 0xa424 }; |
| 1168 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 0, expected); |
| 1169 | } |
| 1170 | SUBCASE("transpose_conv2d, set 2, param 1") |
| 1171 | { |
| 1172 | std::vector<uint16_t> expected = { 0x3182, 0xacff, 0xaf9a }; |
| 1173 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 1, expected); |
| 1174 | } |
| 1175 | SUBCASE("transpose_conv2d, set 2, param 2") |
| 1176 | { |
| 1177 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1178 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 2, expected); |
| 1179 | } |
| 1180 | SUBCASE("transpose_conv2d, set 3, param 0") |
| 1181 | { |
| 1182 | std::vector<uint16_t> expected = { 0xbc41, 0x1cbe, 0x3009 }; |
| 1183 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 0, expected); |
| 1184 | } |
| 1185 | SUBCASE("transpose_conv2d, set 3, param 1") |
| 1186 | { |
| 1187 | std::vector<uint16_t> expected = { 0x35ac, 0x3b2a, 0x2db4 }; |
| 1188 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 1, expected); |
| 1189 | } |
| 1190 | SUBCASE("transpose_conv2d, set 3, param 2") |
| 1191 | { |
| 1192 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1193 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 2, expected); |
| 1194 | } |
| 1195 | SUBCASE("transpose_conv2d, set 4, param 0") |
| 1196 | { |
| 1197 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x4953 }; |
| 1198 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 0, expected); |
| 1199 | } |
| 1200 | SUBCASE("transpose_conv2d, set 4, param 1") |
| 1201 | { |
| 1202 | std::vector<uint16_t> expected = { 0x4e62, 0xcd17, 0x0 }; |
| 1203 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 1, expected); |
| 1204 | } |
| 1205 | SUBCASE("transpose_conv2d, set 4, param 2") |
| 1206 | { |
| 1207 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1208 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 2, expected); |
| 1209 | } |
| 1210 | SUBCASE("transpose_conv2d, set 5, param 0") |
| 1211 | { |
| 1212 | std::vector<uint16_t> expected = { 0x505a, 0x4ff8, 0xd174 }; |
| 1213 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 0, expected); |
| 1214 | } |
| 1215 | SUBCASE("transpose_conv2d, set 5, param 1") |
| 1216 | { |
| 1217 | std::vector<uint16_t> expected = { 0xd10e, 0xc728, 0xcd2e }; |
| 1218 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 1, expected); |
| 1219 | } |
| 1220 | SUBCASE("transpose_conv2d, set 5, param 2") |
| 1221 | { |
| 1222 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1223 | transpose_conv2d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 2, expected); |
| 1224 | } |
| 1225 | } |
| 1226 | |
evacha01 | 47ab176 | 2024-01-29 13:23:23 +0000 | [diff] [blame^] | 1227 | void conv3d_test_FP16(const std::string tosaName[3], |
| 1228 | const size_t tosaElements[3], |
| 1229 | const std::string templateJsonCfg, |
| 1230 | const std::string setStr, |
| 1231 | int32_t param, |
| 1232 | const std::vector<uint16_t> expected) |
| 1233 | { |
| 1234 | std::string jsonCfg = templateJsonCfg; |
| 1235 | update_json_template(jsonCfg, "_SET_", setStr); |
| 1236 | |
| 1237 | std::vector<half_float::half> buffer(tosaElements[param]); |
| 1238 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName[param].c_str(), (void*)buffer.data(), tosaElements[param] * 2)); |
| 1239 | check_output<half_float::half>(buffer, expected); |
| 1240 | } |
| 1241 | |
| 1242 | TEST_CASE("positive - FP16 conv3d dot product (first 3 values)") |
| 1243 | { |
| 1244 | std::string templateJsonCfg = R"({ |
| 1245 | "tensors" : { |
| 1246 | "input" : { |
| 1247 | "generator": "DOT_PRODUCT", |
| 1248 | "data_type": "FP16", |
| 1249 | "input_type": "VARIABLE", |
| 1250 | "shape" : [1, 3, 2, 2, 3], |
| 1251 | "input_pos": 0, |
| 1252 | "op" : "CONV3D", |
| 1253 | "dot_product_info": { |
| 1254 | "s": _SET_, |
| 1255 | "ks": 27, |
| 1256 | "acc_type": "FP16", |
| 1257 | "kernel": [3, 1, 3] |
| 1258 | } |
| 1259 | }, |
| 1260 | "weight" : { |
| 1261 | "generator": "DOT_PRODUCT", |
| 1262 | "data_type": "FP16", |
| 1263 | "input_type": "CONSTANT", |
| 1264 | "shape" : [4, 3, 1, 3, 3], |
| 1265 | "input_pos": 1, |
| 1266 | "op" : "CONV3D", |
| 1267 | "dot_product_info": { |
| 1268 | "s": _SET_, |
| 1269 | "ks": 27, |
| 1270 | "acc_type": "FP16" |
| 1271 | } |
| 1272 | }, |
| 1273 | "bias" : { |
| 1274 | "generator": "DOT_PRODUCT", |
| 1275 | "data_type": "FP16", |
| 1276 | "input_type": "CONSTANT", |
| 1277 | "shape" : [ 4 ], |
| 1278 | "input_pos": 2, |
| 1279 | "op" : "CONV3D", |
| 1280 | "dot_product_info": { |
| 1281 | "s": _SET_, |
| 1282 | "ks": 27, |
| 1283 | "acc_type": "FP16" |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | } |
| 1288 | })"; |
| 1289 | |
| 1290 | const std::string tosaName[3] = { "input", "weight", "bias" }; |
| 1291 | const size_t tosaElements[3] = { (1 * 3 * 2 * 2 * 3), (4 * 3 * 1 * 3 * 3), 4 }; |
| 1292 | |
| 1293 | SUBCASE("conv3d, set 0, param 0") |
| 1294 | { |
| 1295 | std::vector<uint16_t> expected = { 0xbb33, 0xbb9b, 0x0 }; |
| 1296 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 0, expected); |
| 1297 | } |
| 1298 | SUBCASE("conv3d, set 0, param 1") |
| 1299 | { |
| 1300 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x39a8 }; |
| 1301 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 1, expected); |
| 1302 | } |
| 1303 | SUBCASE("conv3d, set 0, param 2") |
| 1304 | { |
| 1305 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1306 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "0", 2, expected); |
| 1307 | } |
| 1308 | SUBCASE("conv3d, set 1, param 0") |
| 1309 | { |
| 1310 | std::vector<uint16_t> expected = { 0x4e37, 0x4ed1, 0x4f87 }; |
| 1311 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 0, expected); |
| 1312 | } |
| 1313 | SUBCASE("conv3d, set 1, param 1") |
| 1314 | { |
| 1315 | std::vector<uint16_t> expected = { 0x51fe, 0x5104, 0x4fbf }; |
| 1316 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 1, expected); |
| 1317 | } |
| 1318 | SUBCASE("conv3d, set 1, param 2") |
| 1319 | { |
| 1320 | std::vector<uint16_t> expected = { 0x6498, 0x66e0, 0x687d }; |
| 1321 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "1", 2, expected); |
| 1322 | } |
| 1323 | SUBCASE("conv3d, set 2, param 0") |
| 1324 | { |
| 1325 | std::vector<uint16_t> expected = { 0x3c00, 0x2bdb, 0xad62 }; |
| 1326 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 0, expected); |
| 1327 | } |
| 1328 | SUBCASE("conv3d, set 2, param 1") |
| 1329 | { |
| 1330 | std::vector<uint16_t> expected = { 0x3c00, 0x1814, 0x31be }; |
| 1331 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 1, expected); |
| 1332 | } |
| 1333 | SUBCASE("conv3d, set 2, param 2") |
| 1334 | { |
| 1335 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1336 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "2", 2, expected); |
| 1337 | } |
| 1338 | SUBCASE("conv3d, set 3, param 0") |
| 1339 | { |
| 1340 | std::vector<uint16_t> expected = { 0x4c00, 0xb92b, 0x30f4 }; |
| 1341 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 0, expected); |
| 1342 | } |
| 1343 | SUBCASE("conv3d, set 3, param 1") |
| 1344 | { |
| 1345 | std::vector<uint16_t> expected = { 0x4c00, 0x3a2e, 0x3bf5 }; |
| 1346 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 1, expected); |
| 1347 | } |
| 1348 | SUBCASE("conv3d, set 3, param 2") |
| 1349 | { |
| 1350 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1351 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "3", 2, expected); |
| 1352 | } |
| 1353 | SUBCASE("conv3d, set 4, param 0") |
| 1354 | { |
| 1355 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x5110 }; |
| 1356 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 0, expected); |
| 1357 | } |
| 1358 | SUBCASE("conv3d, set 4, param 1") |
| 1359 | { |
| 1360 | std::vector<uint16_t> expected = { 0x4384, 0xd1de, 0x0 }; |
| 1361 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 1, expected); |
| 1362 | } |
| 1363 | SUBCASE("conv3d, set 4, param 2") |
| 1364 | { |
| 1365 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1366 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "4", 2, expected); |
| 1367 | } |
| 1368 | SUBCASE("conv3d, set 5, param 0") |
| 1369 | { |
| 1370 | std::vector<uint16_t> expected = { 0x490c, 0x4ccf, 0x5046 }; |
| 1371 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 0, expected); |
| 1372 | } |
| 1373 | SUBCASE("conv3d, set 5, param 1") |
| 1374 | { |
| 1375 | std::vector<uint16_t> expected = { 0xc994, 0x4ca4, 0x4f9f }; |
| 1376 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 1, expected); |
| 1377 | } |
| 1378 | SUBCASE("conv3d, set 5, param 2") |
| 1379 | { |
| 1380 | std::vector<uint16_t> expected = { 0x0, 0x0, 0x0 }; |
| 1381 | conv3d_test_FP16(tosaName, tosaElements, templateJsonCfg, "5", 2, expected); |
| 1382 | } |
| 1383 | } |
| 1384 | |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 1385 | void fft2d_test_FP32(const std::string tosaName, |
| 1386 | const size_t tosaElements, |
| 1387 | const std::string templateJsonCfg, |
| 1388 | const std::string setStr, |
| 1389 | const std::vector<uint32_t> lastExpected) |
| 1390 | { |
| 1391 | std::string jsonCfg = templateJsonCfg; |
| 1392 | update_json_template(jsonCfg, "_SET_", setStr); |
| 1393 | |
| 1394 | std::vector<float> buffer(tosaElements); |
| 1395 | REQUIRE(tgd_generate_data(jsonCfg.c_str(), tosaName.c_str(), (void*)buffer.data(), tosaElements * 4)); |
| 1396 | // Get values at positions -8, -7 and -6 from the end |
| 1397 | std::vector<float> last_three_ish(buffer.end() - 8, buffer.end() - 5); |
| 1398 | |
| 1399 | check_output<float>(last_three_ish, lastExpected); |
| 1400 | } |
| 1401 | |
| 1402 | TEST_CASE("positive - FP32 fft2d dot product (values -8, -7 & -6 from the end)") |
| 1403 | { |
| 1404 | std::string templateJsonCfg = R"({ |
| 1405 | "tensors" : { |
| 1406 | "real" : { |
| 1407 | "generator": "DOT_PRODUCT", |
| 1408 | "data_type": "FP32", |
| 1409 | "input_type": "VARIABLE", |
| 1410 | "shape" : [ 8, 2, 4 ], |
| 1411 | "input_pos": 0, |
| 1412 | "op" : "FFT2D", |
| 1413 | "dot_product_info": { |
| 1414 | "s": _SET_, |
| 1415 | "ks": 16, |
| 1416 | "acc_type": "FP32" |
| 1417 | } |
| 1418 | }, |
| 1419 | "imag" : { |
| 1420 | "generator": "DOT_PRODUCT", |
| 1421 | "data_type": "FP32", |
| 1422 | "input_type": "VARIABLE", |
| 1423 | "shape" : [ 8, 2, 4 ], |
| 1424 | "input_pos": 1, |
| 1425 | "op" : "FFT2D", |
| 1426 | "dot_product_info": { |
| 1427 | "s": _SET_, |
| 1428 | "ks": 16, |
| 1429 | "acc_type": "FP32" |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | })"; |
| 1434 | |
| 1435 | const std::string tosaNameReal = "real"; |
| 1436 | const std::string tosaNameImag = "imag"; |
| 1437 | const size_t tosaElements = 8 * 2 * 4; |
| 1438 | |
| 1439 | SUBCASE("fft2d, set 0, real") |
| 1440 | { |
| 1441 | std::vector<uint32_t> expected = { 0x0, 0x0, 0x3ee06867 }; |
| 1442 | fft2d_test_FP32(tosaNameReal, tosaElements, templateJsonCfg, "0", expected); |
| 1443 | } |
| 1444 | SUBCASE("fft2d, set 0, imag") |
| 1445 | { |
| 1446 | std::vector<uint32_t> expected = { 0x3e6d1d36, 0x0, 0x0 }; |
| 1447 | fft2d_test_FP32(tosaNameImag, tosaElements, templateJsonCfg, "0", expected); |
| 1448 | } |
| 1449 | SUBCASE("fft2d, set 1, real") |
| 1450 | { |
| 1451 | // NOTE: Python test script produced 0x5e7219eb - so off by 1 |
| 1452 | std::vector<uint32_t> expected = { 0x5e18358e, 0x5e7219ec, 0x5e2beab2 }; |
| 1453 | fft2d_test_FP32(tosaNameReal, tosaElements, templateJsonCfg, "1", expected); |
| 1454 | } |
| 1455 | SUBCASE("fft2d, set 1, imag") |
| 1456 | { |
| 1457 | std::vector<uint32_t> expected = { 0x5e71fbcc, 0x5e1bd27a, 0x5e46c84a }; |
| 1458 | fft2d_test_FP32(tosaNameImag, tosaElements, templateJsonCfg, "1", expected); |
| 1459 | } |
| 1460 | SUBCASE("fft2d, set 2, real") |
| 1461 | { |
| 1462 | std::vector<uint32_t> expected = { 0x3f800000, 0x3d704bae, 0x3e4443a6 }; |
| 1463 | fft2d_test_FP32(tosaNameReal, tosaElements, templateJsonCfg, "2", expected); |
| 1464 | } |
| 1465 | SUBCASE("fft2d, set 2, imag") |
| 1466 | { |
| 1467 | std::vector<uint32_t> expected = { 0x3f800000, 0x3dacbd02, 0xbe26be6a }; |
| 1468 | fft2d_test_FP32(tosaNameImag, tosaElements, templateJsonCfg, "2", expected); |
| 1469 | } |
| 1470 | SUBCASE("fft2d, set 3, real") |
| 1471 | { |
| 1472 | // NOTE: Python test script produced 0x3de257cf, 0x3f144b53 - so off by 1 |
| 1473 | std::vector<uint32_t> expected = { 0x41800000, 0x3de257ce, 0x3f144b54 }; |
| 1474 | fft2d_test_FP32(tosaNameReal, tosaElements, templateJsonCfg, "3", expected); |
| 1475 | } |
| 1476 | SUBCASE("fft2d, set 3, imag") |
| 1477 | { |
| 1478 | std::vector<uint32_t> expected = { 0x41800000, 0x3f86492c, 0xbf5bd4b3 }; |
| 1479 | fft2d_test_FP32(tosaNameImag, tosaElements, templateJsonCfg, "3", expected); |
| 1480 | } |
| 1481 | SUBCASE("fft2d, set 4, real") |
| 1482 | { |
| 1483 | std::vector<uint32_t> expected = { 0x0, 0x5d8c6475, 0x0 }; |
| 1484 | fft2d_test_FP32(tosaNameReal, tosaElements, templateJsonCfg, "4", expected); |
| 1485 | } |
| 1486 | SUBCASE("fft2d, set 4, imag") |
| 1487 | { |
| 1488 | std::vector<uint32_t> expected = { 0xdca65b4f, 0x5c98b5d2, 0xdd14ddd8 }; |
| 1489 | fft2d_test_FP32(tosaNameImag, tosaElements, templateJsonCfg, "4", expected); |
| 1490 | } |
| 1491 | SUBCASE("fft2d, set 5, real") |
| 1492 | { |
| 1493 | std::vector<uint32_t> expected = { 0x5cb9bbd4, 0x5d8c0c21, 0x5daa1928 }; |
| 1494 | fft2d_test_FP32(tosaNameReal, tosaElements, templateJsonCfg, "5", expected); |
| 1495 | } |
| 1496 | SUBCASE("fft2d, set 5, imag") |
| 1497 | { |
| 1498 | std::vector<uint32_t> expected = { 0x5e708eb3, 0x5e2c1a78, 0x5ddbbc3f }; |
| 1499 | fft2d_test_FP32(tosaNameImag, tosaElements, templateJsonCfg, "5", expected); |
| 1500 | } |
| 1501 | } |
| 1502 | |
Jeremy Johnson | 59b307d | 2023-10-04 14:17:26 +0100 | [diff] [blame] | 1503 | TEST_SUITE_END(); // generate |