test/nextgen/operators/matmul/matmul_tb.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 2025 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 <tuple> | ||
| 12 | |||
| 13 | #include "test/nextgen/common/random.hpp" | ||
| 14 | #include "test/nextgen/harness/tensor.hpp" | ||
| 15 | #include "test/nextgen/operators/matmul/matmul_bias_mode.hpp" | ||
| 16 | #include "test/nextgen/operators/matmul/matmul_operator.hpp" | ||
| 17 | #include "test/nextgen/operators/matmul/matmul_slots.hpp" | ||
| 18 | |||
| 19 | namespace kai::test { | ||
| 20 | |||
| 21 | /// Matrix multiplication test bench. | ||
| 22 | class MatMulTb { | ||
| 23 | public: | ||
| 24 | /// Default constructor. | ||
| 25 | 400 | MatMulTb() = default; | |
| 26 | |||
| 27 | /// Creates a new matrix multiplication test bench. | ||
| 28 | /// | ||
| 29 | /// @param[in] shape_m The LHS and output height. | ||
| 30 | /// @param[in] shape_n The RHS and output width. | ||
| 31 | /// @param[in] shape_k The LHS width and RHS height. | ||
| 32 | /// @param[in] bias_mode The bias mode. | ||
| 33 | /// @param[in] clamp_ratio The ratio of clamping range and the output range. | ||
| 34 | /// @param[in] op The operator under test. | ||
| 35 | MatMulTb( | ||
| 36 | size_t shape_m, size_t shape_n, size_t shape_k, MatMulBiasMode bias_mode, float clamp_ratio, | ||
| 37 | const MatMulOperator* op); | ||
| 38 | |||
| 39 | /// Generates the test data. | ||
| 40 | /// | ||
| 41 | /// @param[in, out] rng The random number generator. | ||
| 42 | void generate_test_data(Rng& rng); | ||
| 43 | |||
| 44 | /// Determines whether LHS packing test is available. | ||
| 45 | [[nodiscard]] bool has_lhs_packing() const; | ||
| 46 | |||
| 47 | /// Gets the scheduling step for LHS packing kernel. | ||
| 48 | /// | ||
| 49 | /// @return The step in M and K dimensions. | ||
| 50 | [[nodiscard]] std::tuple<size_t, size_t> lhs_packing_steps() const; | ||
| 51 | |||
| 52 | /// Tests the LHS packing kernel. | ||
| 53 | /// | ||
| 54 | /// @param[in] start_m The coordinate of the region under test in M dimension. | ||
| 55 | /// @param[in] start_k The coordinate of the region under test in K dimension. | ||
| 56 | /// @param[in] size_m The size of the region under test in M dimension. | ||
| 57 | /// @param[in] size_k The size of the region under test in K dimension. | ||
| 58 | void test_lhs_packing(size_t start_m, size_t start_k, size_t size_m, size_t size_k); | ||
| 59 | |||
| 60 | /// Determines whether RHS packing test is available. | ||
| 61 | [[nodiscard]] bool has_rhs_packing() const; | ||
| 62 | |||
| 63 | /// Gets the scheduling step for RHS packing kernel. | ||
| 64 | /// | ||
| 65 | /// @return The step in N and K dimensions. | ||
| 66 | [[nodiscard]] std::tuple<size_t, size_t> rhs_packing_steps() const; | ||
| 67 | |||
| 68 | /// Tests the RHS packing kernel. | ||
| 69 | /// | ||
| 70 | /// @param[in] start_n The coordinate of the region under test in N dimension. | ||
| 71 | /// @param[in] start_k The coordinate of the region under test in K dimension. | ||
| 72 | /// @param[in] size_n The size of the region under test in N dimension. | ||
| 73 | /// @param[in] size_k The size of the region under test in K dimension. | ||
| 74 | void test_rhs_packing(size_t start_n, size_t start_k, size_t size_n, size_t size_k); | ||
| 75 | |||
| 76 | /// Gets the scheduling step for matrix mulplication kernel. | ||
| 77 | /// | ||
| 78 | /// @return The step in M and N dimensions. | ||
| 79 | [[nodiscard]] std::tuple<size_t, size_t> matmul_steps() const; | ||
| 80 | |||
| 81 | /// Tests the matrix multiplication kernel. | ||
| 82 | /// | ||
| 83 | /// @param[in] start_m The coordinate of the region under test in M dimension. | ||
| 84 | /// @param[in] start_n The coordinate of the region under test in N dimension. | ||
| 85 | /// @param[in] size_m The size of the region under test in M dimension. | ||
| 86 | /// @param[in] size_n The size of the region under test in N dimension. | ||
| 87 | void test_matmul(size_t start_m, size_t start_n, size_t size_m, size_t size_n); | ||
| 88 | |||
| 89 | private: | ||
| 90 | void populate_config(); ///< Populates the operator configuration. | ||
| 91 | |||
| 92 | /// Determines each tensor whether it is required to run the micro-kernel | ||
| 93 | /// or reference implementation. | ||
| 94 | void determine_required_tensors(); | ||
| 95 | |||
| 96 | void generate_lhs_raw(Rng& rng); ///< Generates the raw LHS data in F32. | ||
| 97 | void generate_rhs_raw(Rng& rng); ///< Generates the raw RHS data in F32. | ||
| 98 | void generate_bias_raw(Rng& rng); ///< Generates the raw bias data in F32. | ||
| 99 | |||
| 100 | void compute_rhs_t_raw(); ///< Computes the raw transposed RHS data. | ||
| 101 | void quantize_lhs(); ///< Quantizes the LHS data. | ||
| 102 | void quantize_rhs_t(); ///< Quantizes the RHS data. | ||
| 103 | void quantize_bias(); ///< Quantizes the bias data. | ||
| 104 | |||
| 105 | void compute_lhs_qzp_neg(); ///< Computes the negative LHS quantization zero-point. | ||
| 106 | |||
| 107 | void compute_rhs_t_qdata_sign(); ///< Computes the quantized RHS data with opposite signedness. | ||
| 108 | void compute_rhs_t_qdata_sign_sum(); ///< Computes the row sum of quantized RHS data with opposite signedness. | ||
| 109 | |||
| 110 | void compute_ref_packed_lhs(); ///< Computes the reference packed LHS. | ||
| 111 | void compute_ref_packed_rhs(); ///< Computes the reference packed RHS. | ||
| 112 | void compute_ref_matmul(); ///< Computes the reference matrix multiplication. | ||
| 113 | |||
| 114 | size_t m_shape_m; | ||
| 115 | size_t m_shape_n; | ||
| 116 | size_t m_shape_k; | ||
| 117 | MatMulBiasMode m_bias_mode; | ||
| 118 | float m_clamp_ratio; | ||
| 119 | |||
| 120 | const MatMulOperator* m_op; | ||
| 121 | std::array<Tensor, NUM_MATMUL_SLOTS> m_tensors; | ||
| 122 | std::array<bool, NUM_MATMUL_SLOTS> m_tensors_required; | ||
| 123 | }; | ||
| 124 | |||
| 125 | } // namespace kai::test | ||
| 126 |