| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <[email protected]> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | // Example usage for matrix multiplication of two half precision floating-point (FP16) matrices and the accumulation of |
| 8 | // the result into an FP16 destination matrix. |
| 9 | // |
| 10 | // The activations and the weights, stored in the LHS and RHS matrices respectively, are both non-transposed matrices. |
| 11 | // The matrix multiplication computation is performed using floating-point fused multiply-add to accumulator (FMLA) |
| 12 | // vector instructions present in the FEAT_FP16 ArmĀ® architecture feature. |
| 13 | // |
| 14 | #if !defined(__aarch64__) || !defined(__ARM_FEATURE_FP16_SCALAR_ARITHMETIC) || \ |
| 15 | !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
| 16 | #error This file must be compiled for AArch64, FEAT_FP16. |
| 17 | #else |
| 18 | #include <arm_neon.h> |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cfloat> |
| 22 | #include <chrono> |
| 23 | #include <cmath> |
| 24 | #include <cstddef> |
| 25 | #include <iomanip> |
| 26 | #include <iostream> |
| 27 | |
| 28 | // Include micro-kernel variants |
| 29 | #include "kai_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla.h" |
| 30 | #include "kai_matmul_clamp_f16_f16_f16p_interface.h" |
| 31 | #include "kai_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon.h" |
| 32 | |
| 33 | namespace { |
| 34 | /// Micro-kernel interface |
| 35 | constexpr kai_matmul_clamp_f16_f16_f16p_ukernel ukernel{ |
| 36 | kai_get_m_step_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 37 | kai_get_n_step_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 38 | kai_get_nr_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 39 | kai_get_kr_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 40 | kai_get_sr_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 41 | kai_get_lhs_offset_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 42 | kai_get_rhs_packed_offset_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 43 | kai_get_dst_offset_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 44 | kai_get_dst_size_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla, |
| 45 | kai_run_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla}; |
| 46 | |
| 47 | /// Reference implementation of matrix multiplication |
| 48 | void run_matmul_ref( |
| 49 | size_t m, size_t n, size_t k, const float16_t* lhs, const float16_t* rhs, const float16_t* bias, float16_t* dst, |
| 50 | float scalar_min, float scalar_max) { |
| 51 | for (size_t row_idx = 0; row_idx < m; ++row_idx) { |
| 52 | for (size_t col_idx = 0; col_idx < n; ++col_idx) { |
| 53 | float16_t acc = bias[col_idx]; |
| 54 | |
| 55 | for (size_t k_idx = 0; k_idx < k; ++k_idx) { |
| 56 | acc += lhs[row_idx * k + k_idx] * rhs[col_idx + n * k_idx]; |
| 57 | } |
| 58 | acc = std::max(acc, static_cast<float16_t>(scalar_min)); |
| 59 | acc = std::min(acc, static_cast<float16_t>(scalar_max)); |
| 60 | |
| 61 | dst[row_idx * n + col_idx] = acc; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// Fills the matrix with incremental values |
| 67 | void fill_matrix(size_t num_rows, size_t num_cols, float16_t* dst, const float16_t weight) { |
| 68 | for (size_t i = 0; i < num_rows * num_cols; i++) { |
| 69 | dst[i] = float16_t(i * weight); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// Print the matrix |
| 74 | void print_matrix(size_t num_rows, size_t num_cols, const char* name, const float16_t* src) { |
| 75 | std::cout << name << " = [\n"; |
| 76 | for (size_t y = 0; y < num_rows; ++y) { |
| 77 | std::cout << " ["; |
| 78 | for (size_t x = 0; x < num_cols; ++x) { |
| 79 | std::cout << std::setprecision(2) << std::fixed << src[y * num_cols + x] << ", "; |
| 80 | } |
| 81 | std::cout << ("],\n"); |
| 82 | } |
| 83 | std::cout << ("]\n\n"); |
| 84 | } |
| 85 | |
| 86 | /// Verify the micro-kernel output matches the reference implementation |
| 87 | bool is_output_correct( |
| 88 | size_t num_rows, size_t num_cols, const float16_t tolerance, const float16_t* ref, const float16_t* act) { |
| 89 | bool is_valid = true; |
| 90 | |
| 91 | for (size_t i = 0; i < num_rows * num_cols; ++i) { |
| 92 | if (std::fabs(ref[i] - act[i]) > tolerance) { |
| 93 | const size_t x = i % num_cols; |
| 94 | const size_t y = i / num_cols; |
| 95 | |
| 96 | std::cout << std::setprecision(5) << std::fixed << "ERROR![" << y << "][" << x << "]: ref=" << ref[i] |
| 97 | << " vs. act=" << act[i] << "\n"; |
| 98 | |
| 99 | is_valid = false; |
| 100 | } |
| 101 | } |
| 102 | return is_valid; |
| 103 | } |
| 104 | } // namespace |
| 105 | |
| 106 | int main() { |
| 107 | int ret = 0; |
| 108 | // 1x1 Convolution operator in NHWC format. |
| 109 | const size_t nhwc_n = 2; |
| 110 | const size_t nhwc_h = 2; |
| 111 | const size_t nhwc_w = 4; |
| 112 | const size_t nhwc_c_in = 4; // Input channels |
| 113 | const size_t nhwc_c_out = 24; // Output channels |
| 114 | |
| 115 | // Map NHWC of operator to GEMM terminology |
| 116 | const size_t M = nhwc_h * nhwc_w * nhwc_n; // Rows of LHS and DST matrices |
| 117 | const size_t N = nhwc_c_out; // Columns of RHS and DST matrices |
| 118 | const size_t K = nhwc_c_in; // Columns of LHS, rows of RHS matrices |
| 119 | |
| 120 | const size_t lhs_size = M * K; |
| 121 | const size_t rhs_size = N * K; |
| 122 | const size_t bias_size = N; |
| 123 | const size_t dst_size = M * N; |
| 124 | |
| 125 | // Allocate the memory |
| 126 | float16_t* lhs = new float16_t[lhs_size]; |
| 127 | float16_t* rhs = new float16_t[rhs_size]; |
| 128 | float16_t* bias = new float16_t[bias_size]; |
| 129 | |
| 130 | fill_matrix(M, K, lhs, 0.1); |
| 131 | fill_matrix(K, N, rhs, 0.1); |
| 132 | fill_matrix(1, N, bias, 10); |
| 133 | |
| 134 | #ifdef KAI_DEBUG |
| 135 | print_matrix(M, K, "lhs", lhs); |
| 136 | print_matrix(K, N, "rhs", rhs); |
| 137 | print_matrix(1, N, "bias", bias); |
| 138 | #endif // KAI_DEBUG |
| 139 | |
| 140 | //----------- REFERENCE IMPLEMENTATION |
| 141 | //------------------------------------ |
| 142 | //------------------------------------ |
| 143 | float16_t* dst_ref = new float16_t[dst_size]; |
| 144 | |
| 145 | run_matmul_ref( |
| 146 | M, N, K, // Dimensions |
| 147 | lhs, // LHS buffer |
| 148 | rhs, // RHS buffer |
| 149 | bias, // Bias buffer |
| 150 | dst_ref, // DST |
| 151 | -FLT_MAX, FLT_MAX // Min and max for the clamp operation |
| 152 | ); |
| 153 | //----------- END REFERENCE IMPLEMENTATION |
| 154 | //------------------------------------ |
| 155 | //------------------------------------ |
| 156 | |
| 157 | //----------- MICRO-KERNELS TESTS |
| 158 | //------------------------------------ |
| 159 | //------------------------------------ |
| 160 | const size_t nr = ukernel.get_nr(); |
| 161 | const size_t kr = ukernel.get_kr(); |
| 162 | const size_t sr = ukernel.get_sr(); |
| 163 | |
| 164 | // In a single row, we pack nr bias values followed by K rows of nr RHS values |
| 165 | const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon(N, K); |
| 166 | const size_t rhs_packed_cols = nr + K * nr; |
| 167 | const size_t rhs_packed_rows = rhs_packed_size / (rhs_packed_cols * sizeof(float16_t)); |
| 168 | |
| 169 | uint8_t* rhs_packed = new uint8_t[rhs_packed_size]; |
| 170 | |
| 171 | const size_t lhs_stride = K * sizeof(float16_t); |
| 172 | const size_t rhs_stride = N * sizeof(float16_t); |
| 173 | const size_t dst_stride_row = N * sizeof(float16_t); |
| 174 | const size_t dst_stride_col = sizeof(float16_t); |
| 175 | |
| 176 | // Packing only needs to be performed once if the contents of the bias and RHS matrices are expected to be constant. |
| 177 | kai_run_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon( |
| 178 | 1, N, K, nr, kr, sr, // Packing arguments |
| 179 | rhs_stride, // RHS stride |
| 180 | rhs, // RHS |
| 181 | bias, // Bias |
| 182 | NULL, // Scale |
| 183 | rhs_packed, // RHS packed |
| 184 | 0, NULL); |
| 185 | |
| 186 | // The RHS and Bias buffers can be freed after packing, however we reuse them for the reference test below |
| 187 | |
| 188 | #ifdef KAI_DEBUG |
| 189 | print_matrix(rhs_packed_rows, rhs_packed_cols, "rhs_packed", rhs_packed); |
| 190 | #endif // KAI_DEBUG |
| 191 | |
| 192 | float16_t* dst = new float16_t[dst_size]; |
| 193 | |
| 194 | // Framework scheduling params |
| 195 | |
| 196 | // Example alternative values to try. ukernel.get_m_step() * 2 or M; |
| 197 | const size_t m_step = ukernel.get_m_step(); // Scheduling along M |
| 198 | |
| 199 | // Example alternative values to try. n_step = N; |
| 200 | const size_t n_step = ukernel.get_n_step(); // Scheduling along N |
| 201 | |
| 202 | for (size_t i_m_step = 0; i_m_step < M; i_m_step += m_step) { |
| 203 | for (size_t i_n_step = 0; i_n_step < N; i_n_step += n_step) { |
| 204 | // Support functions return offset in bytes |
| 205 | const uint8_t* lhs_ptr = |
| 206 | (const uint8_t*)lhs + (ukernel.get_lhs_packed_offset(i_m_step, K * sizeof(uint16_t))); |
| 207 | const uint8_t* rhs_ptr = (const uint8_t*)rhs_packed + (ukernel.get_rhs_packed_offset(i_n_step, K)); |
| 208 | uint8_t* dst_ptr = (uint8_t*)dst + (ukernel.get_dst_offset(i_m_step, i_n_step, N * sizeof(uint16_t))); |
| 209 | #ifdef KAI_DEBUG |
| 210 | printf("Processing a %zux%zu ouptut block starting at (%zu, %zu)\n", m_step, n_step, i_m_step, i_n_step); |
| 211 | #endif |
| 212 | const size_t actual_m = std::min(M - i_m_step, m_step); |
| 213 | const size_t actual_n = std::min(N - i_n_step, n_step); |
| 214 | |
| 215 | ukernel.run_matmul( |
| 216 | actual_m, actual_n, K, // Dimensions |
| 217 | lhs_ptr, // LHS |
| 218 | lhs_stride, // LHS stride |
| 219 | rhs_ptr, // RHS packed |
| 220 | dst_ptr, // DST |
| 221 | dst_stride_row, // DST stride (row) |
| 222 | dst_stride_col, // DST stride (col) |
| 223 | -FLT_MAX, FLT_MAX // Min and max for the clamp operation |
| 224 | ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #ifdef KAI_DEBUG |
| 229 | print_matrix(M, N, "dst", dst); |
| 230 | #endif // KAI_DEBUG |
| 231 | |
| 232 | const bool is_valid = is_output_correct(M, N, 0.0001, dst_ref, dst); |
| 233 | |
| 234 | std::cout << "TEST[matmul_clamp_f16_f16_f16p]\n"; |
| 235 | std::cout << "- ukernel: matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla\n"; |
| 236 | if (is_valid) { |
| 237 | std::cout << "- Status: PASSED\n"; |
| 238 | } else { |
| 239 | std::cout << "- Status: FAILED\n"; |
| 240 | ret = 1; |
| 241 | } |
| 242 | |
| 243 | //----------- END MICRO-KERNELS TESTS |
| 244 | //------------------------------------ |
| 245 | //------------------------------------ |
| 246 | |
| 247 | delete[] lhs; |
| 248 | delete[] rhs; |
| 249 | delete[] bias; |
| 250 | delete[] rhs_packed; |
| 251 | delete[] dst; |
| 252 | delete[] dst_ref; |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | #endif // Architectural features check. |