| 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 | #pragma once |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cstddef> |
| 11 | #include <optional> |
| 12 | #include <tuple> |
| 13 | |
| 14 | #include "test/nextgen/common/random.hpp" |
| 15 | #include "test/nextgen/harness/tensor.hpp" |
| 16 | #include "test/nextgen/operators/matmul/matmul_bias_mode.hpp" |
| 17 | #include "test/nextgen/operators/matmul/matmul_operator.hpp" |
| 18 | #include "test/nextgen/operators/matmul/matmul_slots.hpp" |
| 19 | |
| 20 | namespace kai::test { |
| 21 | |
| 22 | /// Matrix multiplication test bench. |
| 23 | class MatMulTb { |
| 24 | public: |
| 25 | /// Default constructor. |
| 26 | MatMulTb() = default; |
| 27 | |
| 28 | /// Creates a new matrix multiplication test bench. |
| 29 | /// |
| 30 | /// @param[in] shape_m The LHS and output height. |
| 31 | /// @param[in] shape_n The RHS and output width. |
| 32 | /// @param[in] shape_k The LHS width and RHS height. |
| 33 | /// @param[in] bias_formats The logical bias formats selected for this test. |
| 34 | /// @param[in] clamp_keep_ratio The ratio of output range to keep unclamped. |
| 35 | /// @param[in] op The operator under test. |
| 36 | MatMulTb( |
| 37 | size_t shape_m, size_t shape_n, size_t shape_k, MatMulBiasModeSet bias_formats, |
| 38 | std::optional<float> clamp_keep_ratio, const MatMulOperator* op); |
| 39 | |
| 40 | /// Generates the test data. |
| 41 | /// |
| 42 | /// @param[in, out] rng The random number generator. |
| 43 | void generate_test_data(Rng& rng); |
| 44 | |
| 45 | /// Gets the scheduling step for LHS packing kernel. |
| 46 | /// |
| 47 | /// @return The step in M and K dimensions. |
| 48 | [[nodiscard]] std::tuple<size_t, size_t> lhs_packing_steps() const; |
| 49 | |
| 50 | /// Tests the LHS packing kernel. |
| 51 | /// |
| 52 | /// @param[in] start_m The coordinate of the region under test in M dimension. |
| 53 | /// @param[in] start_k The coordinate of the region under test in K dimension. |
| 54 | /// @param[in] size_m The size of the region under test in M dimension. |
| 55 | /// @param[in] size_k The size of the region under test in K dimension. |
| 56 | void test_lhs_packing(size_t start_m, size_t start_k, size_t size_m, size_t size_k); |
| 57 | |
| 58 | /// Gets the scheduling step for RHS packing kernel. |
| 59 | /// |
| 60 | /// @return The step in N and K dimensions. |
| 61 | [[nodiscard]] std::tuple<size_t, size_t> rhs_packing_steps() const; |
| 62 | |
| 63 | /// Tests the RHS packing kernel. |
| 64 | /// |
| 65 | /// @param[in] start_n The coordinate of the region under test in N dimension. |
| 66 | /// @param[in] start_k The coordinate of the region under test in K dimension. |
| 67 | /// @param[in] size_n The size of the region under test in N dimension. |
| 68 | /// @param[in] size_k The size of the region under test in K dimension. |
| 69 | void test_rhs_packing(size_t start_n, size_t start_k, size_t size_n, size_t size_k); |
| 70 | |
| 71 | /// Gets the scheduling step for matrix mulplication kernel. |
| 72 | /// |
| 73 | /// @return The step in M and N dimensions. |
| 74 | [[nodiscard]] std::tuple<size_t, size_t> matmul_steps() const; |
| 75 | |
| 76 | /// Tests the matrix multiplication kernel. |
| 77 | /// |
| 78 | /// @param[in] start_m The coordinate of the region under test in M dimension. |
| 79 | /// @param[in] start_n The coordinate of the region under test in N dimension. |
| 80 | /// @param[in] size_m The size of the region under test in M dimension. |
| 81 | /// @param[in] size_n The size of the region under test in N dimension. |
| 82 | void test_matmul(size_t start_m, size_t start_n, size_t size_m, size_t size_n); |
| 83 | |
| 84 | private: |
| 85 | void populate_config(); ///< Populates the operator configuration. |
| 86 | |
| 87 | /// Determines each tensor whether it is required to run the micro-kernel |
| 88 | /// or reference implementation. |
| 89 | void determine_required_tensors(); |
| 90 | |
| 91 | void generate_lhs_data(Rng& rng); ///< Generates the LHS data. |
| 92 | void generate_rhs_data(Rng& rng); ///< Generates the RHS data. |
| 93 | void generate_acc_bias_m_data(Rng& rng); ///< Generates the per-M accumulator bias data. |
| 94 | void generate_acc_bias_n_data(Rng& rng); ///< Generates the per-N accumulator bias data. |
| 95 | void generate_acc_scale_global_data(Rng& rng); ///< Generates the global accumulator scale data. |
| 96 | void generate_scale_bias_n_data(Rng& rng); ///< Generates the per-N scaled-accumulator bias data. |
| 97 | |
| 98 | void compute_rhs_t_data(); ///< Computes the transposed RHS data. |
| 99 | void quantize_lhs(); ///< Quantizes the LHS data. |
| 100 | void quantize_rhs_t(); ///< Quantizes the RHS data. |
| 101 | void quantize_bias(); ///< Quantizes the bias data. |
| 102 | |
| 103 | void compute_lhs_qzp_neg(); ///< Computes the negative LHS quantization zero-point. |
| 104 | |
| 105 | void compute_rhs_t_qdata_sign(); ///< Computes the quantized RHS data with opposite signedness. |
| 106 | void compute_rhs_t_qdata_sign_sum(); ///< Computes the row sum of quantized RHS data with opposite signedness. |
| 107 | |
| 108 | void compute_ref_packed_lhs(); ///< Computes the reference packed LHS. |
| 109 | void compute_ref_packed_rhs(); ///< Computes the reference packed RHS. |
| 110 | void compute_ref_matmul(); ///< Computes the reference matrix multiplication. |
| 111 | |
| 112 | void set_tensor_required(MatMulSlot slot); ///< Mark tensor as required |
| 113 | bool is_tensor_required(MatMulSlot slot); ///< Check if tensor is required |
| 114 | Tensor& get_tensor(MatMulSlot slot); ///< retrieve tensor |
| 115 | |
| 116 | size_t m_shape_m; |
| 117 | size_t m_shape_n; |
| 118 | size_t m_shape_k; |
| 119 | MatMulBiasModeSet m_bias_modes; |
| 120 | std::optional<float> m_clamp_keep_ratio; |
| 121 | |
| 122 | const MatMulOperator* m_op; |
| 123 | std::array<Tensor, n_elements<MatMulSlot>()> m_tensors; |
| 124 | std::array<bool, n_elements<MatMulSlot>()> m_tensors_required; |
| 125 | }; |
| 126 | |
| 127 | } // namespace kai::test |