| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 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 <cfloat> |
| 10 | #include <cstddef> |
| 11 | #include <cstdint> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "benchmark/cycle_counter.hpp" |
| 15 | #include "kai/kai_common.h" |
| 16 | #include "pack_matmul_interface.hpp" |
| 17 | #include "test/common/cpu_info.hpp" |
| 18 | #include "test/common/data_type.hpp" |
| 19 | |
| 20 | #ifdef __GNUC__ |
| 21 | #pragma GCC diagnostic push |
| 22 | #pragma GCC diagnostic ignored "-Wswitch-default" |
| 23 | #endif // __GNUC__ |
| 24 | |
| 25 | #include <benchmark/benchmark.h> |
| 26 | |
| 27 | #ifdef __GNUC__ |
| 28 | #pragma GCC diagnostic pop |
| 29 | #endif // __GNUC__ |
| 30 | |
| 31 | namespace kai::benchmark { |
| 32 | using Buffer = std::vector<uint8_t>; |
| 33 | |
| 34 | /// Benchmarks LHS packing followed by a matrix multiplication micro-kernel. |
| 35 | inline void kai_benchmark_pack_matmul(::benchmark::State& state, const PackMatMulEntry* entry) { |
| 36 | if (!entry->is_cpu_supported()) { |
| 37 | state.SkipWithMessage("Unsupported CPU feature"); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | const size_t m = state.range(0); |
| 42 | const size_t n = state.range(1); |
| 43 | const size_t k = state.range(2); |
| 44 | size_t bl = 0; |
| 45 | |
| 46 | if (m > 1 && entry->matmul_op == PackMatMulOp::GEMV) { |
| 47 | state.SkipWithMessage("GEMV optimized for m=1 only"); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | if (entry->needs_block_size) { |
| 52 | bl = state.range(3); |
| 53 | if (k % bl != 0) { |
| 54 | state.SkipWithMessage("K must be a multiple of block size"); |
| 55 | return; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | const size_t lhs_stride = data_type_array_size_in_bytes(entry->lhs_type, k); |
| 60 | const size_t dst_stride_row = data_type_array_size_in_bytes(entry->dst_type, n); |
| 61 | const size_t dst_stride_col = data_type_array_size_in_bytes(entry->dst_type, 1); |
| 62 | |
| 63 | const size_t mr = entry->get_mr(); |
| 64 | const size_t kr = entry->get_kr(); |
| 65 | const size_t sr = entry->get_sr(); |
| 66 | |
| 67 | const size_t lhs_offset = entry->get_lhs_offset(0, lhs_stride); |
| 68 | const size_t lhs_packed_offset = entry->get_lhs_packed_offset(0, k, mr, kr, sr); |
| 69 | const size_t matmul_lhs_packed_offset = entry->get_matmul_lhs_packed_offset(0, k); |
| 70 | |
| 71 | if (lhs_packed_offset != matmul_lhs_packed_offset) { |
| 72 | state.SkipWithMessage("LHS packing offset does not match matmul LHS packed offset"); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | const size_t lhs_size = lhs_offset + (m * lhs_stride); |
| 77 | const size_t lhs_packed_size = entry->get_lhs_packed_size(m, k, mr, kr, sr); |
| 78 | |
| 79 | size_t rhs_size = n * k * sizeof(uint64_t); |
| 80 | size_t dst_size = m * dst_stride_row; |
| 81 | if (test::cpu_has_sme() || test::cpu_has_sme2()) { |
| 82 | rhs_size *= kai_get_sme_vector_length_u32(); |
| 83 | dst_size *= kai_get_sme_vector_length_u32(); |
| 84 | } |
| 85 | |
| 86 | const Buffer lhs(lhs_size); |
| 87 | Buffer lhs_packed(lhs_packed_size); |
| 88 | const Buffer rhs(rhs_size); |
| 89 | Buffer dst(dst_size); |
| 90 | |
| 91 | const bool cycle_counter_available = cycle_counter_init(); |
| 92 | uint64_t total_cycles = 0; |
| 93 | uint64_t start_cycles = 0; |
| 94 | bool cycle_measurement_valid = false; |
| 95 | if (cycle_counter_available) { |
| 96 | cycle_counter_start(); |
| 97 | cycle_measurement_valid = cycle_counter_read(start_cycles); |
| 98 | } |
| 99 | |
| 100 | for (auto _ : state) { |
| 101 | entry->run_pack_matmul( |
| 102 | m, n, k, bl, mr, kr, sr, 0, lhs.data() + lhs_offset, lhs_stride, rhs.data(), |
| 103 | lhs_packed.data() + lhs_packed_offset, dst.data(), dst_stride_row, dst_stride_col); |
| 104 | } |
| 105 | |
| 106 | if (cycle_counter_available) { |
| 107 | uint64_t end_cycles = 0; |
| 108 | cycle_measurement_valid = |
| 109 | cycle_measurement_valid && cycle_counter_read(end_cycles) && end_cycles >= start_cycles; |
| 110 | cycle_counter_stop(); |
| 111 | if (cycle_measurement_valid) { |
| 112 | total_cycles += (end_cycles - start_cycles); |
| 113 | } |
| 114 | cycle_counter_shutdown(); |
| 115 | } |
| 116 | |
| 117 | state.counters["cpu_cycles"] = ::benchmark::Counter( |
| 118 | static_cast<double>(total_cycles), ::benchmark::Counter::kAvgIterations, ::benchmark::Counter::OneK::kIs1000); |
| 119 | } |
| 120 | } // namespace kai::benchmark |