| 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 | #ifndef KLEIDIAI_BENCHMARK_MATMUL_MATMUL_BENCHMARK_LOGIC_HPP |
| 8 | #define KLEIDIAI_BENCHMARK_MATMUL_MATMUL_BENCHMARK_LOGIC_HPP |
| 9 | |
| 10 | #include <cstddef> |
| 11 | #include <cstdint> |
| 12 | #include <functional> |
| 13 | #include <type_traits> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "benchmark/cycle_counter.hpp" |
| 17 | #include "kai/kai_common.h" |
| 18 | #include "matmul_interface.hpp" |
| 19 | #include "matmul_runner.hpp" |
| 20 | |
| 21 | #ifdef __GNUC__ |
| 22 | #pragma GCC diagnostic push |
| 23 | #pragma GCC diagnostic ignored "-Wswitch-default" |
| 24 | #endif // __GNUC__ |
| 25 | |
| 26 | #include <benchmark/benchmark.h> |
| 27 | |
| 28 | #ifdef __GNUC__ |
| 29 | #pragma GCC diagnostic pop |
| 30 | #endif // __GNUC__ |
| 31 | |
| 32 | namespace kai::benchmark { |
| 33 | using Buffer = std::vector<uint8_t>; |
| 34 | using CpuRequirement = std::function<bool()>; |
| 35 | |
| 36 | /// High level description of the matrix multiplication operation. |
| 37 | enum class MatMulOp : uint8_t { |
| 38 | GEMM, |
| 39 | GEMV, |
| 40 | }; |
| 41 | |
| 42 | /// Benchmarks a matrix multiplication micro-kernel. |
| 43 | /// |
| 44 | /// @tparam MatMulInterface Interface of the matrix multiplication micro-kernel. |
| 45 | /// @param state State for the benchmark to use. |
| 46 | /// @param matmul_interface Abstraction containing the micro-kernel to run. |
| 47 | /// @param dst_type Output type of the micro-kernel. Required for the micro-kernel to make certain assumptions |
| 48 | /// internally about the stride of the data. |
| 49 | /// @param matmul_op Type of matrix multiplication operation. |
| 50 | /// @param is_cpu_supported Function that checks the CPU feature requirement to run this benchmark. |
| 51 | template <typename MatMulInterface> |
| 52 | void kai_benchmark_matmul( |
| 53 | ::benchmark::State& state, const MatMulInterface matmul_interface, const DataType dst_type, |
| 54 | const MatMulOp matmul_op, const CpuRequirement& is_cpu_supported) { |
| 55 | if (!is_cpu_supported()) { |
| 56 | state.SkipWithMessage("Unsupported CPU feature"); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | const size_t m = state.range(0); |
| 61 | const size_t n = state.range(1); |
| 62 | const size_t k = state.range(2); |
| 63 | const size_t bl = state.range(3); |
| 64 | |
| 65 | if (m > 1 && matmul_op == MatMulOp::GEMV) { |
| 66 | state.SkipWithMessage("GEMV optimized for m=1 only"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if constexpr ( |
| 71 | std::is_same_v<MatMulInterface, MatMulBlockwiseDynamicQuantInterface> || |
| 72 | std::is_same_v<MatMulInterface, MatMulBlockwiseDynamicQuantGenericDstInterface> || |
| 73 | std::is_same_v<MatMulInterface, MatMulBlockwiseDynamicQuantLutInterface>) { |
| 74 | if (k % bl != 0) { |
| 75 | state.SkipWithMessage("K must be a multiple of block size"); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Create sufficiently large buffers |
| 81 | size_t lhs_size = m * k * sizeof(uint64_t); |
| 82 | size_t rhs_size = n * k * sizeof(uint64_t); |
| 83 | size_t dst_size = m * n * sizeof(uint32_t); |
| 84 | |
| 85 | if (test::cpu_has_sme() || test::cpu_has_sme2()) { |
| 86 | lhs_size *= kai_get_sme_vector_length_u32(); |
| 87 | rhs_size *= kai_get_sme_vector_length_u32(); |
| 88 | dst_size *= kai_get_sme_vector_length_u32(); |
| 89 | } |
| 90 | |
| 91 | const Buffer lhs(lhs_size); |
| 92 | const Buffer rhs(rhs_size); |
| 93 | Buffer dst(dst_size); |
| 94 | |
| 95 | MatMulRunner matmul_runner(matmul_interface, dst_type); |
| 96 | matmul_runner.set_mnk(m, n, k); |
| 97 | matmul_runner.set_bl(bl); |
| 98 | matmul_runner.prepare(); |
| 99 | |
| 100 | const bool cycle_counter_available = cycle_counter_init(); |
| 101 | uint64_t total_cycles = 0; |
| 102 | uint64_t start_cycles = 0; |
| 103 | bool cycle_measurement_valid = false; |
| 104 | if (cycle_counter_available) { |
| 105 | cycle_counter_start(); |
| 106 | cycle_measurement_valid = cycle_counter_read(start_cycles); |
| 107 | } |
| 108 | |
| 109 | for (auto _ : state) { |
| 110 | matmul_runner.run(lhs.data(), rhs.data(), dst.data()); |
| 111 | } |
| 112 | |
| 113 | if (cycle_counter_available) { |
| 114 | uint64_t end_cycles = 0; |
| 115 | cycle_measurement_valid = |
| 116 | cycle_measurement_valid && cycle_counter_read(end_cycles) && end_cycles >= start_cycles; |
| 117 | cycle_counter_stop(); |
| 118 | if (cycle_measurement_valid) { |
| 119 | total_cycles += (end_cycles - start_cycles); |
| 120 | } |
| 121 | cycle_counter_shutdown(); |
| 122 | } |
| 123 | |
| 124 | state.counters["cpu_cycles"] = ::benchmark::Counter( |
| 125 | static_cast<double>(total_cycles), ::benchmark::Counter::kAvgIterations, ::benchmark::Counter::OneK::kIs1000); |
| 126 | } |
| 127 | } // namespace kai::benchmark |
| 128 | |
| 129 | #endif // KLEIDIAI_BENCHMARK_MATMUL_MATMUL_BENCHMARK_LOGIC_HPP |