Elffuss BraidHub

kikocis/kleidiai public

main · 17.3 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6
7#include <gtest/gtest.h>
8
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <functional>
13#include <limits>
14#include <map>
15#include <string_view>
16#include <tuple>
17#include <utility>
18#include <vector>
19
20#include "kai/kai_common.h"
21#include "test/common/buffer.hpp"
22#include "test/common/compare.hpp"
23#include "test/common/cpu_info.hpp"
24#include "test/common/data_format.hpp"
25#include "test/common/data_type.hpp"
26#include "test/common/matmul_shape_generator.hpp"
27#include "test/common/matmul_test_common.hpp"
28#include "test/common/matrix_portion.hpp"
29#include "test/common/printer.hpp"
30#include "test/common/seed.hpp"
31#include "test/nextgen/common/test_registry.hpp"
32#include "test/reference/clamp.hpp"
33#include "test/reference/fill.hpp"
34#include "test/reference/matmul.hpp"
35#include "test/reference/pack.hpp"
36
37// matmul_clamp_f16_bf16p_bf16p
38#include "kai/ukernels/matmul/matmul_clamp_f16_bf16p_bf16p/kai_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla.h"
39#include "kai/ukernels/matmul/pack/kai_lhs_pack_bf16p8x4_f16_neon.h"
40#include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_bf16p12x4biasf16_f16_neon.h"
41
42namespace kai::test {
43
44/// List of supported matrix multiplication methods.
45namespace {
46
47static const std::array<MatMulMethod, 2>& get_matmul_methods() {
48 static std::array<MatMulMethod, 2> matmul_methods{};
49
50 matmul_methods[0].name = "matmul_nt_nt_f16_bf16p_bf16p_8x12_neon_mla";
51 matmul_methods[0].m0 = 8;
52 matmul_methods[0].n0 = 12;
53 matmul_methods[0].k0 = 4;
54 matmul_methods[0].dst_format = DataFormat(DataType::FP16);
55 matmul_methods[0].lhs_format = DataFormat(DataType::FP16);
56 matmul_methods[0].packed_lhs_format =
57 DataFormat(DataType::BF16, 8, 4, DataFormat::PackFormat::NONE, DataType::FP16, DataType::UNKNOWN, 8, 4);
58 matmul_methods[0].rhs_format = DataFormat(DataType::FP16);
59 matmul_methods[0].packed_rhs_format = DataFormat(
60 DataType::BF16, 12, 4, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP16, DataType::UNKNOWN, 12, 4);
61 matmul_methods[0].bias_format = DataFormat(DataType::FP16);
62 matmul_methods[0].fn_is_supported = cpu_has_bf16;
63 matmul_methods[0].fn_get_mr = kai_get_mr_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
64 matmul_methods[0].fn_get_nr = kai_get_nr_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
65 matmul_methods[0].fn_get_kr = kai_get_kr_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
66 matmul_methods[0].fn_get_sr = kai_get_sr_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
67 matmul_methods[0].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
68 matmul_methods[0].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
69 matmul_methods[0].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
70 matmul_methods[0].fn_get_lhs_offset = kai_get_lhs_offset_lhs_pack_bf16p8x4_f16_neon;
71 matmul_methods[0].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_pack_bf16p8x4_f16_neon;
72 matmul_methods[0].fn_get_packed_lhs_offset =
73 kai_get_lhs_packed_offset_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
74 matmul_methods[0].fn_pack_lhs = kai_run_lhs_pack_bf16p8x4_f16_neon;
75 matmul_methods[0].fn_get_rhs_offset = kai_get_rhs_offset_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
76 matmul_methods[0].fn_get_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
77 matmul_methods[0].fn_get_main_packed_rhs_offset =
78 kai_get_rhs_packed_offset_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
79 matmul_methods[0].fn_pack_rhs = kai_run_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
80 matmul_methods[0].fn_get_bias_offset = kai_get_bias_offset_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
81 matmul_methods[0].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
82 matmul_methods[0].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
83 matmul_methods[0].fn_matmul_f16_bf16p_bf16p = kai_run_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
84
85 matmul_methods[1].name = "matmul_nt_nt_f16_bf16p_bf16p_8x12_neon_mla_opt_bias";
86 matmul_methods[1].m0 = 8;
87 matmul_methods[1].n0 = 12;
88 matmul_methods[1].k0 = 4;
89 matmul_methods[1].dst_format = DataFormat(DataType::FP16);
90 matmul_methods[1].lhs_format = DataFormat(DataType::FP16);
91 matmul_methods[1].packed_lhs_format =
92 DataFormat(DataType::BF16, 8, 4, DataFormat::PackFormat::NONE, DataType::FP16, DataType::UNKNOWN, 8, 4);
93 matmul_methods[1].rhs_format = DataFormat(DataType::FP16);
94 matmul_methods[1].packed_rhs_format = DataFormat(
95 DataType::BF16, 12, 4, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP16, DataType::UNKNOWN, 12, 4);
96 matmul_methods[1].bias_format = DataFormat(DataType::UNKNOWN);
97 matmul_methods[1].fn_is_supported = cpu_has_bf16;
98 matmul_methods[1].fn_get_mr = kai_get_mr_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
99 matmul_methods[1].fn_get_nr = kai_get_nr_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
100 matmul_methods[1].fn_get_kr = kai_get_kr_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
101 matmul_methods[1].fn_get_sr = kai_get_sr_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
102 matmul_methods[1].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
103 matmul_methods[1].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
104 matmul_methods[1].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
105 matmul_methods[1].fn_get_lhs_offset = kai_get_lhs_offset_lhs_pack_bf16p8x4_f16_neon;
106 matmul_methods[1].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_pack_bf16p8x4_f16_neon;
107 matmul_methods[1].fn_get_packed_lhs_offset =
108 kai_get_lhs_packed_offset_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
109 matmul_methods[1].fn_pack_lhs = kai_run_lhs_pack_bf16p8x4_f16_neon;
110 matmul_methods[1].fn_get_rhs_offset = kai_get_rhs_offset_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
111 matmul_methods[1].fn_get_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
112 matmul_methods[1].fn_get_main_packed_rhs_offset =
113 kai_get_rhs_packed_offset_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
114 matmul_methods[1].fn_pack_rhs = kai_run_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
115 matmul_methods[1].fn_get_bias_offset = kai_get_bias_offset_rhs_pack_kxn_bf16p12x4biasf16_f16_neon;
116 matmul_methods[1].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
117 matmul_methods[1].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
118 matmul_methods[1].fn_matmul_f16_bf16p_bf16p = kai_run_matmul_clamp_f16_bf16p8x4_bf16p12x4b_8x12_neon_mmla;
119
120 return matmul_methods;
121}
122
123/// Matrix multiplication test fixture.
124class MatMulTestBf16OutFp16 : public testing::Test {
125public:
126 explicit MatMulTestBf16OutFp16(const MatMulClampTestParams& params) : m_params{params} {
127 }
128 void TestBody() override;
129
130private:
131 MatMulClampTestParams m_params;
132 /// Unique ID: m, n, k
133 using TestDataId = std::tuple<size_t, size_t, size_t, std::optional<float>, std::string_view>;
134
135protected:
136 /// Cached test data that is shared between multiple test case.
137 struct TestData {
138 Buffer lhs{}; ///< LHS operand.
139 Buffer ref_packed_lhs{}; ///< Reference packed LHS.
140 Buffer rhs{}; ///< RHS operand.
141 Buffer rhs_scales{}; ///< RHS per-row quantization scales.
142 Buffer bias{}; ///< Bias.
143 Buffer ref_packed_rhs{}; ///< Reference packed RHS.
144 Buffer ref_dst{}; ///< Reference output.
145 Range<float> clamp_range; ///< Clamp range
146 };
147
148 /// Gets the test data for the current test case.
149 static const TestData& test_data(const MatMulClampTestParams& params) {
150 const auto& [method, info, portion, bias_mode, clamp_keep_ratio] = params;
151 const TestDataId data_id{info.m, info.n, info.k, clamp_keep_ratio, method.name};
152
153 // If the test data is already available, returns it.
154 const auto data_it = _data.find(data_id);
155
156 if (data_it != _data.end()) {
157 return data_it->second;
158 }
159
160 // Generates the test data.
161 const auto has_lhs_pack = method.packed_lhs_format.data_type() != DataType::UNKNOWN;
162 const auto has_rhs_pack = method.packed_rhs_format.data_type() != DataType::UNKNOWN;
163 const auto has_bias = method.bias_format.data_type() != DataType::UNKNOWN;
164
165 // Seed the random generator.
166 const auto key = std::string(method.name) + "_" + std::to_string(info.m) + "x" + std::to_string(info.n) + "x" +
167 std::to_string(info.k) + "_" + (bias_mode == BiasMode::INTERNAL ? "internal" : "provided") + ":" +
168 (clamp_keep_ratio.has_value() ? std::to_string(clamp_keep_ratio.value()) : "noclamp");
169 auto& feed = seed_stream(key);
170
171 const auto lhs_h = info.m;
172 const auto lhs_w = info.k;
173 auto lhs = fill_matrix_random(lhs_h, lhs_w, method.lhs_format, feed());
174 Buffer ref_packed_lhs;
175
176 if (has_lhs_pack) {
177 ref_packed_lhs =
178 pack(method.packed_lhs_format, lhs.data(), nullptr, nullptr, method.lhs_format, lhs_h, lhs_w);
179 }
180
181 const auto rhs_h = info.k;
182 const auto rhs_w = info.n;
183 auto rhs = fill_matrix_random(rhs_h, rhs_w, method.rhs_format, feed());
184
185 Buffer rhs_scales;
186 if (data_type_is_quantized(method.rhs_format.data_type()) &&
187 method.rhs_format.pack_format() == DataFormat::PackFormat::NONE) {
188 rhs_scales = fill_matrix_random(rhs_h, 1, DataFormat(DataType::FP32), feed());
189 }
190
191 const auto bias_h = 1;
192 const auto bias_w = info.n;
193 Buffer bias;
194
195 if (has_bias) {
196 bias = fill_matrix_random(bias_h, bias_w, method.bias_format, feed());
197 }
198
199 Buffer packed_rhs(method.fn_get_packed_rhs_size(rhs_w, rhs_h));
200
201 if (has_rhs_pack) {
202 const auto ref_rhs_row_stride = method.rhs_format.default_row_stride(rhs_w);
203 method.pack_rhs(
204 info.n, info.k, rhs.data(), ref_rhs_row_stride, has_bias ? bias.data() : nullptr, nullptr,
205 packed_rhs.data());
206 }
207
208 KAI_ASSUME_ALWAYS(method.lhs_format.is_raw());
209 KAI_ASSUME_ALWAYS(method.rhs_format.is_raw());
210 KAI_ASSUME_ALWAYS(method.dst_format.is_raw());
211
212 auto ref_dst = matmul(
213 lhs.data(), nullptr, nullptr, method.lhs_format.data_type(), //
214 rhs.data(), rhs_scales.data(), nullptr, method.rhs_format.data_type(), //
215 has_bias ? bias.data() : nullptr, nullptr, nullptr, method.bias_format.data_type(), //
216 method.dst_format.data_type(), //
217 info.m, info.n, info.k, false /* lhs_transposed */, false /* rhs_transposed */);
218
219 const auto [min, max] =
220 find_clamp_range(method.dst_format.data_type(), ref_dst.data(), info.m * info.n, clamp_keep_ratio);
221
222 auto ref_clamped = clamp(DataType::FP16, ref_dst.data(), info.m * info.n, min, max);
223
224 auto& data = _data[data_id] = {};
225 data.lhs = std::move(lhs);
226 data.ref_packed_lhs = std::move(ref_packed_lhs);
227 data.rhs = std::move(rhs);
228 data.rhs_scales = std::move(rhs_scales);
229 data.bias = std::move(bias);
230 data.ref_packed_rhs = std::move(packed_rhs);
231 data.ref_dst = std::move(ref_clamped);
232 data.clamp_range = {min, max};
233
234 return data;
235 }
236
237private:
238 // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
239 static std::map<TestDataId, TestData> _data;
240 // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
241};
242
243// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
244std::map<MatMulTestBf16OutFp16::TestDataId, MatMulTestBf16OutFp16::TestData> MatMulTestBf16OutFp16::_data;
245// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
246
247/// Tests the output.
248void MatMulTestBf16OutFp16::TestBody() {
249 const auto& [method, info, portion, bias_mode, clamp_keep_ratio] = m_params;
250
251 if (method.fn_is_supported && !method.fn_is_supported()) {
252 GTEST_SKIP() << "Unsupported CPU feature";
253 }
254
255 if (!method.has_main_kernel()) {
256 GTEST_SKIP() << "No main kernel available";
257 }
258
259 const auto& data = test_data(m_params);
260
261 const auto m_step = method.fn_get_main_m_step();
262 ASSERT_EQ(m_step, method.m0);
263
264 const auto n_step = method.fn_get_main_n_step();
265 ASSERT_EQ(n_step, method.n0);
266
267 const auto rect = portion.compute_portion(info.m, info.n, method.m0, method.n0);
268
269 if (rect.height() == 0 || rect.width() == 0) {
270 GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")";
271 }
272
273 const size_t lhs_w = info.k;
274 const size_t rhs_w = rect.width();
275 const size_t bias_w = info.n;
276 const size_t dst_w = info.n;
277 const bool has_bias = (data.bias.size() > 0);
278
279 const auto lhs_start_row = rect.start_row();
280 const auto lhs_stride = method.lhs_format.default_row_stride(lhs_w);
281
282 const size_t lhs_packed_size = method.fn_get_packed_lhs_size(info.m, info.k, method.m0, method.k0, 1 /* sr */);
283 Buffer lhs_data(lhs_packed_size);
284
285 uintptr_t lhs_offset = method.fn_get_lhs_offset(lhs_start_row, lhs_stride);
286 uintptr_t lhs_packed_offset = method.fn_get_packed_lhs_offset(lhs_start_row, info.k);
287
288 KAI_UNUSED(lhs_offset);
289 method.fn_pack_lhs(
290 rect.height(), info.k, method.m0, method.k0, 1 /* sr */, 0 /* m_idx_start */, data.lhs.data() + lhs_offset,
291 lhs_stride, lhs_data.data() + lhs_packed_offset);
292
293 const auto rhs_stride = method.rhs_format.default_row_stride(info.n);
294
295 const size_t rhs_packed_size = method.fn_get_packed_rhs_size(info.n, info.k);
296 Buffer rhs_data(rhs_packed_size);
297
298 const auto packed_rhs_start_row = rect.start_col();
299 const auto packed_rhs_start_col = 0;
300
301 uintptr_t rhs_offset = method.fn_get_rhs_offset(rect.start_col());
302 uintptr_t rhs_packed_offset = method.fn_get_main_packed_rhs_offset(packed_rhs_start_row, info.k);
303 const auto ref_rhs_packed_offset =
304 method.packed_rhs_format.default_offset_in_bytes(packed_rhs_start_row, packed_rhs_start_col, info.k);
305
306 ASSERT_EQ(rhs_packed_offset, ref_rhs_packed_offset);
307
308 uintptr_t bias_offset = sizeof(uint16_t) * rect.start_col();
309
310 method.fn_pack_rhs(
311 1, // num_groups
312 rhs_w, info.k, method.n0, method.k0,
313 1, // sr
314 rhs_stride, data.rhs.data() + rhs_offset, has_bias ? data.bias.data() + bias_offset : nullptr,
315 NULL, // Scale
316 rhs_data.data() + rhs_packed_offset, 0, NULL);
317
318 if (has_bias) {
319 const auto ref_bias_offset = method.bias_format.default_offset_in_bytes(0, rect.start_col(), bias_w);
320 ASSERT_EQ(ref_bias_offset, bias_offset);
321 }
322
323 const auto dst_stride = method.dst_format.default_row_stride(dst_w);
324 const auto dst_offset = method.fn_get_dst_offset(rect.start_row(), rect.start_col(), dst_stride);
325 const auto ref_dst_offset = method.dst_format.default_offset_in_bytes(rect.start_row(), rect.start_col(), dst_w);
326 ASSERT_EQ(dst_offset, ref_dst_offset);
327
328 const auto dst_size = method.fn_get_dst_size(info.m, info.n);
329 const auto ref_dst_size = method.dst_format.default_size_in_bytes(info.m, info.n);
330 ASSERT_EQ(dst_size, ref_dst_size);
331
332 Buffer dst(dst_size);
333 method.main_kernel(
334 rect.height(), rect.width(), info.k, lhs_data.data() + lhs_packed_offset, rhs_data.data() + rhs_packed_offset,
335 NULL, dst.data() + dst_offset, lhs_stride, rhs_stride, dst_stride, data.clamp_range.min, data.clamp_range.max);
336
337 DefaultMismatchHandler handler(0, 0.02, 0, 0.05);
338 const auto success = compare(dst.data(), data.ref_dst.data(), method.dst_format, info.m, info.n, rect, handler);
339 ASSERT_TRUE(success);
340}
341
342const auto matmul_tests_setup = kai::test::TestRegistry::register_setup([]() {
343 auto& feed = seed_stream("MatMulTestBf16OutFp16::shapes");
344 MatMulShapeGenerator gen({1, 256}, {1, 256}, {1, 256}, feed);
345 const auto shapes = gen.generate(256);
346 const std::array portions = {
347 MatrixPortion(0, 0, 1, 1), // Full matrix.
348 MatrixPortion(0, 0, 0.25, 0.25), // Top-left corner.
349 MatrixPortion(0.75, 0.75, 1, 1), // Bottom-right corner.
350 MatrixPortion(0.75, 0, 1, 1), // Bottom-left corner.
351 MatrixPortion(0.4, 0.5, 0.6, 0.8), // Middle portion.
352 };
353 const std::array<std::optional<float>, 4> clamp = {std::nullopt, 1.0f, 0.9f, 0.5f};
354 for (const auto& method : get_matmul_methods()) {
355 for (const auto& shape : shapes) {
356 for (const auto& portion : portions) {
357 for (const auto& clamp_keep_ratio : clamp) {
358 MatMulClampTestParams params{method, shape, portion, BiasMode::PROVIDED, clamp_keep_ratio};
359 const std::string name = test_description(method.name, shape, portion, true, clamp_keep_ratio);
360 KAI_REGISTER_TEST(MatMulTestBf16OutFp16, MatMulTestBf16OutFp16, "MatMul", name.c_str(), params);
361 }
362 }
363 }
364 }
365});
366} // namespace
367
368} // namespace kai::test