| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <array> |
| 11 | #include <cstddef> |
| 12 | #include <iterator> |
| 13 | #include <random> |
| 14 | #include <string> |
| 15 | #include <unordered_map> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "test/common/assert.hpp" |
| 20 | #include "test/common/matrix_portion.hpp" |
| 21 | #include "test/common/seed.hpp" |
| 22 | #include "test/common/span.hpp" |
| 23 | #include "test/nextgen/common/random.hpp" |
| 24 | #include "test/nextgen/common/test_config.hpp" |
| 25 | #include "test/nextgen/common/test_registry.hpp" |
| 26 | #include "test/nextgen/operators/matmul/matmul_bias_mode.hpp" |
| 27 | #include "test/nextgen/operators/matmul/matmul_operator.hpp" |
| 28 | #include "test/nextgen/operators/matmul/matmul_tb.hpp" |
| 29 | |
| 30 | namespace kai::test { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | struct MatMulDistribution { |
| 35 | Rng m_rng{seed_stream("MatMulNext::setup")()}; |
| 36 | std::uniform_int_distribution<size_t> m_shape_dist{1, 150}; |
| 37 | std::uniform_real_distribution<float> m_probability_dist{0.0F, 1.0F}; |
| 38 | std::uniform_real_distribution<float> m_dist_70_to_100{0.7F, 1.0F}; |
| 39 | std::uniform_real_distribution<float> m_dist_10_to_70{0.1F, 0.7F}; |
| 40 | }; |
| 41 | |
| 42 | struct MatMulFixtureParams { |
| 43 | size_t iteration_no; |
| 44 | |
| 45 | size_t shape_m; |
| 46 | size_t shape_n; |
| 47 | size_t shape_k; |
| 48 | MatMulBiasModeSet bias_formats; |
| 49 | std::optional<float> clamp_keep_ratio; |
| 50 | |
| 51 | const MatMulOperator* op; |
| 52 | |
| 53 | /// Builds a descriptive name for these fixture parameters. |
| 54 | /// |
| 55 | /// @return A unique name string used for test registration and caching. |
| 56 | [[nodiscard]] std::string name() const { |
| 57 | const std::string clamp_keep_ratio_name = |
| 58 | clamp_keep_ratio.has_value() ? std::to_string(clamp_keep_ratio.value()) : "noclamp"; |
| 59 | |
| 60 | return std::string(op->name) + // |
| 61 | ",m=" + std::to_string(shape_m) + // |
| 62 | ",n=" + std::to_string(shape_n) + // |
| 63 | ",k=" + std::to_string(shape_k) + // |
| 64 | ",bias=" + matmul_bias_format_set_name(bias_formats) + // |
| 65 | ",clamp_keep_ratio=" + clamp_keep_ratio_name + // |
| 66 | ",iteration=" + std::to_string(iteration_no); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | struct MatMulTestParams { |
| 71 | MatrixPortion portion; |
| 72 | |
| 73 | /// Builds a descriptive name for the selected matrix portion. |
| 74 | /// |
| 75 | /// @return A name string encoding the portion location and size. |
| 76 | [[nodiscard]] std::string name() const { |
| 77 | return "start_m=" + std::to_string(portion.start_row()) + ",size_m=" + std::to_string(portion.height()) + |
| 78 | ",start_n=" + std::to_string(portion.start_col()) + ",size_n=" + std::to_string(portion.width()); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | class MatMulFixture : public testing::Test { |
| 83 | public: |
| 84 | /// Creates a fixture bound to a specific parameter set. |
| 85 | /// |
| 86 | /// @param[in] params The fixture parameters used for this instance. |
| 87 | explicit MatMulFixture(const MatMulFixtureParams& params) : m_fixture_params(params) { |
| 88 | } |
| 89 | |
| 90 | protected: |
| 91 | /// Returns the parameters for this fixture instance. |
| 92 | /// |
| 93 | /// @return A constant reference to the fixture parameters. |
| 94 | [[nodiscard]] const MatMulFixtureParams& fixture_params() const { |
| 95 | return m_fixture_params; |
| 96 | } |
| 97 | |
| 98 | /// Returns the cached test bench for this fixture, creating it if needed. |
| 99 | /// |
| 100 | /// The test bench is keyed by the fixture name, and test data is generated |
| 101 | /// once per unique fixture to keep results deterministic. |
| 102 | /// |
| 103 | /// @return A reference to the cached test bench. |
| 104 | [[nodiscard]] MatMulTb& test_bench() { |
| 105 | const std::string name = m_fixture_params.name(); |
| 106 | |
| 107 | // If the test has already been created, uses it. |
| 108 | const auto it = test_benches.find(name); |
| 109 | |
| 110 | if (it != test_benches.end()) { |
| 111 | return it->second; |
| 112 | } |
| 113 | |
| 114 | // Creates a new test if it hasn't been created. |
| 115 | MatMulTb test( |
| 116 | m_fixture_params.shape_m, m_fixture_params.shape_n, m_fixture_params.shape_k, m_fixture_params.bias_formats, |
| 117 | m_fixture_params.clamp_keep_ratio, m_fixture_params.op); |
| 118 | |
| 119 | const std::string seed_key = "MatMulNext::testbench:" + name; |
| 120 | auto& feed = seed_stream(seed_key); |
| 121 | Rng rng(feed()); |
| 122 | |
| 123 | test.generate_test_data(rng); |
| 124 | |
| 125 | return test_benches[name] = std::move(test); |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | static std::unordered_map<std::string, MatMulTb> test_benches; |
| 130 | |
| 131 | MatMulFixtureParams m_fixture_params; |
| 132 | }; |
| 133 | |
| 134 | std::unordered_map<std::string, MatMulTb> MatMulFixture::test_benches; |
| 135 | |
| 136 | class MatMulPackLhsTest : public MatMulFixture { |
| 137 | public: |
| 138 | /// Creates an LHS packing test instance. |
| 139 | /// |
| 140 | /// @param[in] fixture_params The fixture parameters shared across tests. |
| 141 | /// @param[in] test_params The parameters describing the matrix portion. |
| 142 | explicit MatMulPackLhsTest(const MatMulFixtureParams& fixture_params, const MatMulTestParams& test_params) : |
| 143 | MatMulFixture(fixture_params), m_test_params(test_params) { |
| 144 | } |
| 145 | |
| 146 | /// Runs the LHS packing validation for the selected portion. |
| 147 | void TestBody() override { |
| 148 | MatMulTb& test = test_bench(); |
| 149 | const MatMulFixtureParams& params = fixture_params(); |
| 150 | const MatrixPortion& portion = m_test_params.portion; |
| 151 | |
| 152 | const auto [step_m, step_k] = test.lhs_packing_steps(); |
| 153 | const Rect rect = portion.compute_portion(params.shape_m, params.shape_k, step_m, step_k); |
| 154 | |
| 155 | const size_t start_m = rect.start_row(); |
| 156 | const size_t start_k = rect.start_col(); |
| 157 | const size_t size_m = rect.height(); |
| 158 | const size_t size_k = rect.width(); |
| 159 | |
| 160 | test.test_lhs_packing(start_m, start_k, size_m, size_k); |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | MatMulTestParams m_test_params; |
| 165 | }; |
| 166 | |
| 167 | class MatMulPackRhsTest : public MatMulFixture { |
| 168 | public: |
| 169 | /// Creates an RHS packing test instance. |
| 170 | /// |
| 171 | /// @param[in] fixture_params The fixture parameters shared across tests. |
| 172 | /// @param[in] test_params The parameters describing the matrix portion. |
| 173 | explicit MatMulPackRhsTest(const MatMulFixtureParams& fixture_params, const MatMulTestParams& test_params) : |
| 174 | MatMulFixture(fixture_params), m_test_params(test_params) { |
| 175 | } |
| 176 | |
| 177 | /// Runs the RHS packing validation for the selected portion. |
| 178 | void TestBody() override { |
| 179 | MatMulTb& test = test_bench(); |
| 180 | const MatMulFixtureParams& params = fixture_params(); |
| 181 | const MatrixPortion& portion = m_test_params.portion; |
| 182 | |
| 183 | const auto [step_n, step_k] = test.rhs_packing_steps(); |
| 184 | const Rect rect = portion.compute_portion(params.shape_n, params.shape_k, step_n, step_k); |
| 185 | |
| 186 | const size_t start_n = rect.start_row(); |
| 187 | const size_t start_k = rect.start_col(); |
| 188 | const size_t size_n = rect.height(); |
| 189 | const size_t size_k = rect.width(); |
| 190 | |
| 191 | test.test_rhs_packing(start_n, start_k, size_n, size_k); |
| 192 | } |
| 193 | |
| 194 | private: |
| 195 | MatMulTestParams m_test_params; |
| 196 | }; |
| 197 | |
| 198 | class MatMulMatMulTest : public MatMulFixture { |
| 199 | public: |
| 200 | /// Creates a matrix multiplication test instance. |
| 201 | /// |
| 202 | /// @param[in] fixture_params The fixture parameters shared across tests. |
| 203 | /// @param[in] test_params The parameters describing the matrix portion. |
| 204 | explicit MatMulMatMulTest(const MatMulFixtureParams& fixture_params, const MatMulTestParams& test_params) : |
| 205 | MatMulFixture(fixture_params), m_test_params(test_params) { |
| 206 | } |
| 207 | |
| 208 | /// Runs the matrix multiplication validation for the selected portion. |
| 209 | void TestBody() override { |
| 210 | MatMulTb& test = test_bench(); |
| 211 | const MatMulFixtureParams& params = fixture_params(); |
| 212 | const MatrixPortion& portion = m_test_params.portion; |
| 213 | |
| 214 | const auto [step_m, step_n] = test.matmul_steps(); |
| 215 | const Rect rect = portion.compute_portion(params.shape_m, params.shape_n, step_m, step_n); |
| 216 | |
| 217 | const size_t start_m = rect.start_row(); |
| 218 | const size_t start_n = rect.start_col(); |
| 219 | const size_t size_m = rect.height(); |
| 220 | const size_t size_n = rect.width(); |
| 221 | |
| 222 | test.test_matmul(start_m, start_n, size_m, size_n); |
| 223 | } |
| 224 | |
| 225 | private: |
| 226 | MatMulTestParams m_test_params; |
| 227 | }; |
| 228 | |
| 229 | struct BiasSelector { |
| 230 | public: |
| 231 | /// Builds a bias-format selector for a given operator. |
| 232 | /// |
| 233 | /// @param[in] op The operator providing supported bias format sets. |
| 234 | explicit BiasSelector(const MatMulOperator& op) { |
| 235 | // Separates with-bias format sets from the list of supported bias format sets. |
| 236 | // |
| 237 | // Reason: no-bias and with-bias cases are chosen by a distribution, |
| 238 | // and if the with-bias case is chosen, each with-bias format set will be |
| 239 | // chosen by another distribution. |
| 240 | has_no_bias_format_set = std::any_of( |
| 241 | op.supported_bias_mode_sets.begin(), op.supported_bias_mode_sets.end(), |
| 242 | [](MatMulBiasModeSet bias_formats) { return bias_formats.is_empty(); }); |
| 243 | |
| 244 | std::copy_if( |
| 245 | op.supported_bias_mode_sets.begin(), op.supported_bias_mode_sets.end(), |
| 246 | std::back_inserter(with_bias_format_sets), matmul_bias_format_set_has_bias_data); |
| 247 | has_with_bias_format_set = !with_bias_format_sets.empty(); |
| 248 | if (has_with_bias_format_set) { |
| 249 | with_bias_dist = std::uniform_int_distribution<size_t>(0, with_bias_format_sets.size() - 1); |
| 250 | } |
| 251 | |
| 252 | KAI_TEST_ASSERT_MSG( |
| 253 | has_no_bias_format_set || has_with_bias_format_set, "At least one bias format set is needed!"); |
| 254 | } |
| 255 | |
| 256 | /// Picks a bias format set based on the configured distributions. |
| 257 | /// |
| 258 | /// @param[in,out] dist_ctx The shared distribution context. |
| 259 | /// |
| 260 | /// @return The selected bias format set. |
| 261 | MatMulBiasModeSet pick(MatMulDistribution& dist_ctx) { |
| 262 | // Bias format set: |
| 263 | // * 70% of tests have bias. |
| 264 | // * 30% of tests have no bias. |
| 265 | // |
| 266 | // If there is no no-bias format set, a with-bias format set is always chosen. |
| 267 | // If a with-bias format set is chosen, each format set (except no bias) |
| 268 | // will be chosen uniformly. |
| 269 | const float bias_prob = dist_ctx.m_probability_dist(dist_ctx.m_rng); |
| 270 | const bool with_bias = has_with_bias_format_set && (!has_no_bias_format_set || bias_prob < 0.7F); |
| 271 | |
| 272 | if (with_bias) { |
| 273 | const size_t bias_formats_idx = with_bias_dist(dist_ctx.m_rng); |
| 274 | return with_bias_format_sets.at(bias_formats_idx); |
| 275 | } |
| 276 | |
| 277 | return MatMulBiasModeSet{}; |
| 278 | } |
| 279 | |
| 280 | private: |
| 281 | bool has_no_bias_format_set = false; |
| 282 | bool has_with_bias_format_set = false; |
| 283 | std::vector<MatMulBiasModeSet> with_bias_format_sets; |
| 284 | std::uniform_int_distribution<size_t> with_bias_dist; |
| 285 | }; |
| 286 | |
| 287 | /// Returns the output matrix portions used in tests. |
| 288 | /// |
| 289 | /// @return An array containing full and corner portions. |
| 290 | std::array<MatrixPortion, 3> make_output_portions() { |
| 291 | return { |
| 292 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 293 | MatrixPortion(0, 0, 0.25, 0.25), // Top-left corner. |
| 294 | MatrixPortion(0.75, 0.75, 1, 1) // Bottom-right corner. |
| 295 | }; |
| 296 | } |
| 297 | |
| 298 | /// Picks a clamp keep ratio for the operator clamp mode. |
| 299 | /// |
| 300 | /// @param[in] op The operator to test. |
| 301 | /// @param[in,out] dist_ctx The shared distribution context. |
| 302 | /// |
| 303 | /// @return A clamp keep ratio if clamping arguments should be provided. |
| 304 | std::optional<float> pick_clamp_keep_ratio(const MatMulOperator& op, MatMulDistribution& dist_ctx) { |
| 305 | if (op.clamp_mode == MatMulClampMode::UNSUPPORTED) { |
| 306 | return std::nullopt; |
| 307 | } |
| 308 | |
| 309 | KAI_TEST_ASSERT(op.clamp_mode == MatMulClampMode::OPTIONAL || op.clamp_mode == MatMulClampMode::REQUIRED); |
| 310 | |
| 311 | // Clamp keep ratio: |
| 312 | // * 50% of tests have no clamping. |
| 313 | // * 10% of tests keep the full output range. |
| 314 | // * 20% of tests keep between 70% to 100% of the output range. |
| 315 | // * 20% of tests keep between 10% to 70% of the output range. |
| 316 | const float clamp_prob = dist_ctx.m_probability_dist(dist_ctx.m_rng); |
| 317 | |
| 318 | const bool no_clamp = clamp_prob < 0.5F; |
| 319 | const bool clamp_100 = clamp_prob >= 0.5F && clamp_prob < 0.6F; |
| 320 | const bool clamp_70_to_100 = clamp_prob >= 0.6F && clamp_prob < 0.8F; |
| 321 | const bool clamp_10_to_70 = clamp_prob >= 0.8F; |
| 322 | |
| 323 | if (no_clamp) { |
| 324 | return std::nullopt; |
| 325 | } |
| 326 | |
| 327 | if (clamp_100) { |
| 328 | return 1.0F; |
| 329 | } |
| 330 | |
| 331 | if (clamp_70_to_100) { |
| 332 | return dist_ctx.m_dist_70_to_100(dist_ctx.m_rng); |
| 333 | } |
| 334 | |
| 335 | KAI_TEST_ASSERT(clamp_10_to_70); |
| 336 | return dist_ctx.m_dist_10_to_70(dist_ctx.m_rng); |
| 337 | } |
| 338 | |
| 339 | /// Selects a valid fixture configuration for an operator and portion. |
| 340 | /// |
| 341 | /// @param[in] shape_no The shape iteration index. |
| 342 | /// @param[in] op The operator to test. |
| 343 | /// @param[in] portion The output matrix portion to validate. |
| 344 | /// @param[in,out] dist_ctx The shared distribution context. |
| 345 | /// @param[in,out] bias_selector The bias selection helper. |
| 346 | /// |
| 347 | /// @return A populated fixture parameter set. |
| 348 | MatMulFixtureParams pick_fixture( |
| 349 | size_t shape_no, // |
| 350 | const MatMulOperator& op, // |
| 351 | const MatrixPortion& portion, // |
| 352 | MatMulDistribution& dist_ctx, // |
| 353 | BiasSelector& bias_selector) { |
| 354 | size_t shape_m = 0; |
| 355 | size_t shape_n = 0; |
| 356 | size_t shape_k = 0; |
| 357 | |
| 358 | static constexpr uint32_t max_attempts = 10'000; |
| 359 | uint32_t attempts = 0; |
| 360 | while (true) { |
| 361 | KAI_TEST_ASSERT_MSG(attempts <= max_attempts, "Unable to find matching shape after _many_ tries"); |
| 362 | ++attempts; |
| 363 | shape_m = dist_ctx.m_shape_dist(dist_ctx.m_rng); |
| 364 | shape_n = dist_ctx.m_shape_dist(dist_ctx.m_rng); |
| 365 | shape_k = dist_ctx.m_shape_dist(dist_ctx.m_rng); |
| 366 | |
| 367 | if (op.is_shape_suitable(shape_m, shape_n, shape_k, portion)) { |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | const MatMulBiasModeSet bias_formats = bias_selector.pick(dist_ctx); |
| 373 | const std::optional<float> clamp_keep_ratio = pick_clamp_keep_ratio(op, dist_ctx); |
| 374 | |
| 375 | return { |
| 376 | shape_no, shape_m, shape_n, shape_k, bias_formats, clamp_keep_ratio, &op, |
| 377 | }; |
| 378 | } |
| 379 | |
| 380 | /// Registers pack and matmul tests for a given operator and portion. |
| 381 | /// |
| 382 | /// @param[in] test_suite_name The test suite name to register with. |
| 383 | /// @param[in] op The operator under test. |
| 384 | /// @param[in] fixture_params The fixture parameter set. |
| 385 | /// @param[in] portion The output matrix portion for this test instance. |
| 386 | void register_operator_test( |
| 387 | const std::string& test_suite_name, // |
| 388 | const MatMulOperator& op, // |
| 389 | const MatMulFixtureParams& fixture_params, // |
| 390 | const MatrixPortion& portion) { |
| 391 | const MatMulTestParams test_params{ |
| 392 | portion, |
| 393 | }; |
| 394 | |
| 395 | const std::string desc = fixture_params.name() + "," + test_params.name(); |
| 396 | |
| 397 | if (op.pack_lhs.has_value()) { |
| 398 | KAI_REGISTER_TEST( |
| 399 | MatMulFixture, MatMulPackLhsTest, test_suite_name.c_str(), ("PackLhs/" + desc).c_str(), fixture_params, |
| 400 | test_params); |
| 401 | } |
| 402 | |
| 403 | if (op.pack_rhs.has_value()) { |
| 404 | KAI_REGISTER_TEST( |
| 405 | MatMulFixture, MatMulPackRhsTest, test_suite_name.c_str(), ("PackRhs/" + desc).c_str(), fixture_params, |
| 406 | test_params); |
| 407 | } |
| 408 | |
| 409 | if (op.matmul.has_value()) { |
| 410 | KAI_REGISTER_TEST( |
| 411 | MatMulFixture, MatMulMatMulTest, test_suite_name.c_str(), ("MatMul/" + desc).c_str(), fixture_params, |
| 412 | test_params); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /// Registers all MatMulNext tests with the test registry. |
| 417 | const auto matmul_tests_setup = TestRegistry::register_setup([]() { |
| 418 | const size_t num_shapes_per_op = TestConfig::Get().num_shapes(); |
| 419 | const auto output_portions = make_output_portions(); |
| 420 | |
| 421 | /* NOTE: that `dist_cxt` must only be constructed once here, as |
| 422 | * it contains the per test suite seed stream */ |
| 423 | const Span<const MatMulOperator> available_operators = get_available_matmul_operators(); |
| 424 | MatMulDistribution dist_ctx; |
| 425 | |
| 426 | for (const MatMulOperator& op : available_operators) { |
| 427 | if (!op.is_cpu_supported()) { |
| 428 | continue; |
| 429 | } |
| 430 | |
| 431 | const std::string test_suite_name = "MatMulNext"; |
| 432 | BiasSelector bias_selector(op); |
| 433 | |
| 434 | for (size_t shape_no = 0; shape_no < num_shapes_per_op; ++shape_no) { |
| 435 | for (const MatrixPortion& portion : output_portions) { |
| 436 | const MatMulFixtureParams fixture = pick_fixture(shape_no, op, portion, dist_ctx, bias_selector); |
| 437 | register_operator_test(test_suite_name, op, fixture, portion); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | }); |
| 442 | |
| 443 | } // namespace |
| 444 | |
| 445 | } // namespace kai::test |