Elffuss BraidHub

kikocis/kleidiai public

main · 23.7 KiBRawPermalink
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#include "test/nextgen/operators/matmul/matmul_tb.hpp"
8
9#include <algorithm>
10#include <array>
11#include <cstddef>
12#include <optional>
13#include <string>
14#include <tuple>
15#include <utility>
16#include <vector>
17
18#include "test/common/assert.hpp"
19#include "test/common/buffer.hpp"
20#include "test/common/compare.hpp"
21#include "test/common/data_type.hpp"
22#include "test/nextgen/common/poly.hpp"
23#include "test/nextgen/common/random.hpp"
24#include "test/nextgen/format/fill.hpp"
25#include "test/nextgen/format/format.hpp"
26#include "test/nextgen/format/plain_format.hpp"
27#include "test/nextgen/harness/kernel_wrapper.hpp"
28#include "test/nextgen/operators/matmul/matmul_config.hpp"
29#include "test/nextgen/operators/matmul/matmul_dims.hpp"
30#include "test/nextgen/operators/matmul/matmul_main_args.hpp"
31#include "test/nextgen/operators/matmul/matmul_slots.hpp"
32#include "test/nextgen/quantization/quantizer.hpp"
33#include "test/nextgen/reference/binary_elementwise.hpp"
34#include "test/nextgen/reference/matmul.hpp"
35#include "test/nextgen/reference/reduce.hpp"
36#include "test/nextgen/reference/unary_elementwise.hpp"
37#include "test/reference/cast.hpp"
38#include "test/reference/clamp.hpp"
39#include "test/reference/transpose.hpp"
40
41namespace kai::test {
42
43MatMulTb::MatMulTb(
44 size_t shape_m, size_t shape_n, size_t shape_k, MatMulBiasModeSet bias_modes, std::optional<float> clamp_keep_ratio,
45 const MatMulOperator* op) :
46 m_shape_m(shape_m),
47 m_shape_n(shape_n),
48 m_shape_k(shape_k),
49 m_bias_modes(bias_modes),
50 m_clamp_keep_ratio(clamp_keep_ratio),
51 m_op(op),
52 m_tensors_required() {
53 std::fill(m_tensors_required.begin(), m_tensors_required.end(), false);
54}
55
56void MatMulTb::generate_test_data(Rng& rng) {
57 populate_config();
58 determine_required_tensors();
59
60 // Populates the constant information.
61 if (const std::optional<MatMulKernelPtr>& matmul = m_op->matmul) {
62 (*matmul)->populate_constant_info(m_tensors);
63 }
64
65 if (const std::optional<MatPackKernelPtr>& pack_lhs = m_op->pack_lhs) {
66 (*pack_lhs)->populate_constant_info(m_tensors);
67 }
68
69 if (const std::optional<MatPackKernelPtr>& pack_rhs = m_op->pack_rhs) {
70 (*pack_rhs)->populate_constant_info(m_tensors);
71 }
72
73 // Generates the non-quantized inputs.
74 generate_lhs_data(rng);
75 generate_rhs_data(rng);
76 generate_acc_bias_m_data(rng);
77 generate_acc_bias_n_data(rng);
78 generate_acc_scale_global_data(rng);
79 generate_scale_bias_n_data(rng);
80
81 compute_rhs_t_data(); // The transposed RHS data is always needed for reference packing.
82
83 // Quantizes the input data.
84 if (m_op->lhs_quant.has_value()) {
85 quantize_lhs();
86 }
87
88 if (m_op->rhs_quant.has_value()) {
89 quantize_rhs_t();
90 }
91
92 if (m_op->bias_quant.has_value()) {
93 quantize_bias();
94 }
95
96 // Calculates additional data.
97 if (is_tensor_required(MatMulSlot::LHS_QZP_NEG)) {
98 compute_lhs_qzp_neg();
99 }
100
101 if (is_tensor_required(MatMulSlot::RHS_T_QDATA_SIGN)) {
102 compute_rhs_t_qdata_sign();
103 }
104
105 if (is_tensor_required(MatMulSlot::RHS_T_QDATA_SIGN_SUM)) {
106 compute_rhs_t_qdata_sign_sum();
107 }
108
109 // Generates reference output.
110 if (m_op->pack_lhs.has_value()) {
111 compute_ref_packed_lhs();
112 }
113
114 if (m_op->pack_rhs.has_value()) {
115 compute_ref_packed_rhs();
116 }
117
118 if (m_op->matmul.has_value()) {
119 compute_ref_matmul();
120 }
121}
122
123void MatMulTb::populate_config() {
124 get_tensor(MatMulSlot::CONFIG).set_value(MatMulConfig{m_bias_modes});
125}
126
127void MatMulTb::determine_required_tensors() {
128 auto add_required_tensors = [&](const auto* kernel) {
129 if (kernel != nullptr) {
130 const std::vector<MatMulSlot> run_inputs = kernel->run_inputs(m_tensors);
131 const std::vector<MatMulSlot> ref_inputs = kernel->ref_inputs(m_tensors);
132
133 for (const MatMulSlot id : run_inputs) {
134 set_tensor_required(id);
135 }
136
137 for (const MatMulSlot id : ref_inputs) {
138 set_tensor_required(id);
139 }
140 }
141 };
142
143 if (m_op->matmul.has_value()) {
144 add_required_tensors(m_op->matmul.value().get());
145 }
146
147 if (m_op->pack_lhs.has_value()) {
148 add_required_tensors(m_op->pack_lhs.value().get());
149 }
150
151 if (m_op->pack_rhs.has_value()) {
152 add_required_tensors(m_op->pack_rhs.value().get());
153 }
154}
155
156void MatMulTb::generate_lhs_data(Rng& rng) {
157 const std::array shape{m_shape_m, m_shape_k};
158 const Poly<Format> format(std::in_place_type<PlainFormat>, m_op->lhs_dtype);
159 Tensor& tensor = get_tensor(MatMulSlot::LHS_DATA);
160
161 const auto seed = rng();
162 Rng data_rng(seed);
163 const std::string uid = "fill_random(" + format->uid() + "," + std::to_string(seed) + ",{" +
164 std::to_string(m_shape_m) + "," + std::to_string(m_shape_k) + "})";
165
166 // For deterministic debug inputs call fill_sequential or fill_constant
167 tensor.set_shape(shape).set_format(format).set_data(
168 format->generate(
169 shape,
170 [&](Span<const size_t> gen_shape, DataType dtype, Span<std::byte> output) {
171 fill_random(gen_shape, dtype, output, data_rng);
172 }),
173 uid);
174}
175
176void MatMulTb::generate_rhs_data(Rng& rng) {
177 const std::array shape{m_shape_k, m_shape_n};
178 const Poly<Format> format(std::in_place_type<PlainFormat>, m_op->rhs_dtype);
179 Tensor& tensor = get_tensor(MatMulSlot::RHS_DATA);
180
181 const auto seed = rng();
182 Rng data_rng(seed);
183 const std::string uid = "fill_random(" + format->uid() + "," + std::to_string(seed) + ",{" +
184 std::to_string(m_shape_k) + "," + std::to_string(m_shape_n) + "})";
185
186 // For deterministic debug inputs call fill_sequential or fill_constant
187 tensor.set_shape(shape).set_format(format).set_data(
188 format->generate(
189 shape,
190 [&](Span<const size_t> gen_shape, DataType dtype, Span<std::byte> output) {
191 fill_random(gen_shape, dtype, output, data_rng);
192 }),
193 uid);
194}
195
196void MatMulTb::generate_acc_bias_m_data(Rng& rng) {
197 if (!is_tensor_required(MatMulSlot::ACC_BIAS_M_DATA)) {
198 return;
199 }
200
201 const Poly<Format> format(std::in_place_type<PlainFormat>, m_op->bias_dtype);
202 const auto fill_bias = [&](Span<const size_t> gen_shape, DataType dtype, Span<std::byte> output) {
203 // Limit the bias to something that is very unlikely to overflow
204 fill_random(gen_shape, dtype, output, rng, integer_range_for_dtype(DataType::I8));
205 };
206
207 const std::array shape{m_shape_m};
208 Tensor& tensor = get_tensor(MatMulSlot::ACC_BIAS_M_DATA);
209
210 tensor.set_shape(shape).set_format(format).set_data(format->generate(shape, fill_bias));
211}
212
213void MatMulTb::generate_acc_bias_n_data(Rng& rng) {
214 if (!is_tensor_required(MatMulSlot::ACC_BIAS_N_DATA)) {
215 return;
216 }
217
218 const Poly<Format> format(std::in_place_type<PlainFormat>, m_op->bias_dtype);
219 const auto fill_bias = [&](Span<const size_t> gen_shape, DataType dtype, Span<std::byte> output) {
220 // Limit the bias to something that is very unlikely to overflow
221 fill_random(gen_shape, dtype, output, rng, integer_range_for_dtype(DataType::I8));
222 };
223
224 const std::array shape{m_shape_n};
225 Tensor& tensor = get_tensor(MatMulSlot::ACC_BIAS_N_DATA);
226
227 tensor.set_shape(shape).set_format(format).set_data(format->generate(shape, fill_bias));
228}
229
230void MatMulTb::generate_acc_scale_global_data(Rng& rng) {
231 if (!is_tensor_required(MatMulSlot::ACC_SCALE_GLOBAL_DATA)) {
232 return;
233 }
234
235 const Poly<Format> format(std::in_place_type<PlainFormat>, m_op->dst_dtype);
236 const std::array shape{size_t{1}};
237 Tensor& tensor = get_tensor(MatMulSlot::ACC_SCALE_GLOBAL_DATA);
238
239 tensor.set_shape(shape).set_format(format).set_data(
240 format->generate(shape, [&](Span<const size_t> gen_shape, DataType gen_dtype, Span<std::byte> output) {
241 fill_random_scale(gen_shape, gen_dtype, output, rng);
242 }));
243}
244
245void MatMulTb::generate_scale_bias_n_data(Rng& rng) {
246 if (!is_tensor_required(MatMulSlot::SCALE_BIAS_N_DATA)) {
247 return;
248 }
249
250 const Poly<Format> format(std::in_place_type<PlainFormat>, m_op->dst_dtype);
251 const std::array shape{m_shape_n};
252 Tensor& tensor = get_tensor(MatMulSlot::SCALE_BIAS_N_DATA);
253
254 const auto seed = rng();
255 Rng data_rng(seed);
256 const std::string uid =
257 "fill_random(" + format->uid() + "," + std::to_string(seed) + ",{" + std::to_string(m_shape_n) + "})";
258
259 // For deterministic debug inputs call fill_sequential or fill_constant
260 tensor.set_shape(shape).set_format(format).set_data(
261 format->generate(
262 shape,
263 [&](Span<const size_t> gen_shape, DataType dtype, Span<std::byte> output) {
264 fill_random(gen_shape, dtype, output, data_rng);
265 }),
266 uid);
267}
268
269void MatMulTb::compute_rhs_t_data() {
270 const std::array shape{m_shape_n, m_shape_k};
271 const Poly<Format> format(std::in_place_type<PlainFormat>, m_op->rhs_dtype);
272 Tensor& rhs_t_data = get_tensor(MatMulSlot::RHS_T_DATA);
273 const Tensor& rhs_data = get_tensor(MatMulSlot::RHS_DATA);
274
275 const std::string uid = "transpose(" + std::string(rhs_data.id()) + ")";
276
277 rhs_t_data.set_shape(shape).set_format(format).set_data(
278 transpose(rhs_data.data_ptr(), m_op->rhs_dtype, m_shape_k, m_shape_n), uid);
279}
280
281void MatMulTb::quantize_lhs() {
282 const Quantizer& lhs_quant = *m_op->lhs_quant.value();
283
284 const std::array lhs_shape{m_shape_m, m_shape_k};
285 const Tensor& lhs_data = get_tensor(MatMulSlot::LHS_DATA);
286 Tensor& lhs_qdata = get_tensor(MatMulSlot::LHS_QDATA);
287 Tensor& lhs_qscale = get_tensor(MatMulSlot::LHS_QSCALE);
288 Tensor& lhs_qzp = get_tensor(MatMulSlot::LHS_QZP);
289
290 lhs_quant.dynamic_quantize(m_op->lhs_dtype, lhs_shape, lhs_data.data(), lhs_qdata, lhs_qscale, lhs_qzp);
291
292 const std::string uid = lhs_quant.uid() + "(" + std::string(lhs_data.id()) + ")";
293 lhs_qdata.set_id(uid + ".qdata");
294 lhs_qscale.set_id(uid + ".qscale");
295 lhs_qzp.set_id(uid + ".qzp");
296}
297
298void MatMulTb::quantize_rhs_t() {
299 const Quantizer& rhs_quant = *m_op->rhs_quant.value();
300
301 const std::array rhs_t_shape{m_shape_n, m_shape_k};
302 const Tensor& rhs_t_data = get_tensor(MatMulSlot::RHS_T_DATA);
303 Tensor& rhs_t_qdata = get_tensor(MatMulSlot::RHS_T_QDATA);
304 Tensor& rhs_t_qscale = get_tensor(MatMulSlot::RHS_T_QSCALE);
305 Tensor& rhs_t_qzp = get_tensor(MatMulSlot::RHS_T_QZP);
306
307 rhs_quant.dynamic_quantize(m_op->rhs_dtype, rhs_t_shape, rhs_t_data.data(), rhs_t_qdata, rhs_t_qscale, rhs_t_qzp);
308
309 const std::string uid = rhs_quant.uid() + "(" + std::string(rhs_t_data.id()) + ")";
310 rhs_t_qdata.set_id(uid + ".qdata");
311 rhs_t_qscale.set_id(uid + ".qscale");
312 rhs_t_qzp.set_id(uid + ".qzp");
313}
314
315void MatMulTb::quantize_bias() {
316 KAI_TEST_ERROR("Not supported.");
317}
318
319void MatMulTb::compute_lhs_qzp_neg() {
320 const Tensor& lhs_qzp = get_tensor(MatMulSlot::LHS_QZP);
321 Tensor& lhs_qzp_neg = get_tensor(MatMulSlot::LHS_QZP_NEG);
322
323 const Shape shape = lhs_qzp.shape();
324 const Poly<Format>& format = lhs_qzp.format();
325
326 const std::string uid = "neg(" + std::string(lhs_qzp.id()) + ")";
327
328 const UnaryElementwiseFn fn = make_negate(format->dtype());
329 Buffer data = fn(shape, lhs_qzp.data());
330
331 lhs_qzp_neg.set_shape(shape).set_format(format).set_data(std::move(data), uid);
332}
333
334void MatMulTb::compute_rhs_t_qdata_sign() {
335 const Tensor& rhs_t_qdata = get_tensor(MatMulSlot::RHS_T_QDATA);
336 Tensor& rhs_t_qdata_sign = get_tensor(MatMulSlot::RHS_T_QDATA_SIGN);
337
338 const Shape shape = rhs_t_qdata.shape();
339 const DataType src_dtype = rhs_t_qdata.format()->dtype();
340 DataType signed_dtype = DataType::I4;
341 switch (src_dtype) {
342 case DataType::U4:
343 case DataType::I4:
344 signed_dtype = DataType::I4;
345 break;
346 default:
347 KAI_TEST_ERROR("Not supported.");
348 }
349
350 // Store the signed interpretation with the signed dtype so reducers can use it directly.
351 const Poly<Format> format(std::in_place_type<PlainFormat>, signed_dtype);
352
353 const UnaryElementwiseFn fn = make_change_signedness(src_dtype);
354 Buffer data = fn(shape, rhs_t_qdata.data());
355
356 rhs_t_qdata_sign.set_shape(shape).set_format(format).set_data(std::move(data));
357}
358
359void MatMulTb::compute_rhs_t_qdata_sign_sum() {
360 const Tensor& rhs_t_qdata_sign = get_tensor(MatMulSlot::RHS_T_QDATA_SIGN);
361 Tensor& rhs_t_qdata_sign_sum = get_tensor(MatMulSlot::RHS_T_QDATA_SIGN_SUM);
362
363 const std::array rhs_t_shape = {m_shape_n, m_shape_k};
364 const std::array rhs_t_rowsum_shape = {m_shape_n};
365 const DataType src_dtype = rhs_t_qdata_sign.format()->dtype();
366 const DataType dst_dtype = rhs_t_qdata_sign_sum.format()->dtype();
367
368 const ReduceFn fn = make_reduce_add(src_dtype, dst_dtype);
369 Buffer data = fn(0, rhs_t_shape, rhs_t_qdata_sign.data());
370
371 rhs_t_qdata_sign_sum.set_shape(rhs_t_rowsum_shape).set_data(std::move(data));
372}
373
374void MatMulTb::compute_ref_packed_lhs() {
375 const KernelWrapper<MatShape>& pack_lhs = *m_op->pack_lhs.value();
376 const std::array lhs_shape{m_shape_m, m_shape_k};
377 pack_lhs.compute_reference(lhs_shape, m_tensors);
378}
379
380void MatMulTb::compute_ref_packed_rhs() {
381 const KernelWrapper<MatShape>& pack_rhs = *m_op->pack_rhs.value();
382 const std::array rhs_t_shape{m_shape_n, m_shape_k};
383 pack_rhs.compute_reference(rhs_t_shape, m_tensors);
384}
385
386void MatMulTb::compute_ref_matmul() {
387 const MatMulConfig& config = get_tensor(MatMulSlot::CONFIG).value<MatMulConfig>();
388 const Tensor& lhs_data = get_tensor(MatMulSlot::LHS_DATA);
389 const Tensor& lhs_qdata = get_tensor(MatMulSlot::LHS_QDATA);
390 const Tensor& lhs_qscale = get_tensor(MatMulSlot::LHS_QSCALE);
391 const Tensor& lhs_qzp = get_tensor(MatMulSlot::LHS_QZP);
392 const Tensor& rhs_t_data = get_tensor(MatMulSlot::RHS_T_DATA);
393 const Tensor& rhs_t_qdata = get_tensor(MatMulSlot::RHS_T_QDATA);
394 const Tensor& rhs_t_qscale = get_tensor(MatMulSlot::RHS_T_QSCALE);
395 const Tensor& acc_bias_m_data = get_tensor(MatMulSlot::ACC_BIAS_M_DATA);
396 const Tensor& acc_bias_n_data = get_tensor(MatMulSlot::ACC_BIAS_N_DATA);
397 Tensor& kernel_args = get_tensor(MatMulSlot::MATMUL_ARGS);
398 Tensor& ref_dst_data = get_tensor(MatMulSlot::DST_DATA);
399
400 ref_dst_data.set_shape({m_shape_m, m_shape_n}).set_format(make_poly<PlainFormat>(m_op->dst_dtype));
401
402 Buffer tmp_mm_lhs;
403 Span<const std::byte> mm_lhs_view;
404
405 DataType mm_lhs_dtype = m_op->lhs_dtype;
406 std::string mm_lhs_id;
407
408 Buffer tmp_mm_rhs_t;
409 Span<const std::byte> mm_rhs_t_view;
410
411 DataType mm_rhs_dtype = m_op->rhs_dtype;
412 std::string mm_rhs_id;
413
414 // Prepares the input data for the reference matrix multiplication.
415 // * If the input data is floating-point, converts it to the accumulator type.
416 // * If the input data is quantized, dequantizes it to the accumulator type.
417 if (m_op->lhs_quant.has_value()) {
418 const Quantizer& lhs_quant = *m_op->lhs_quant.value();
419 tmp_mm_lhs = lhs_quant.dequantize(
420 m_op->acc_dtype, {m_shape_m, m_shape_k}, lhs_qdata.data(), lhs_qscale.data(), lhs_qzp.data());
421 mm_lhs_view = tmp_mm_lhs.view();
422 mm_lhs_dtype = m_op->acc_dtype;
423 mm_lhs_id = "dequantize(" + lhs_quant.uid() + "," + std::string(lhs_qdata.id()) + ")";
424 } else {
425 mm_lhs_view = lhs_data.data();
426 mm_lhs_id = std::string(lhs_data.id());
427 }
428
429 if (m_op->rhs_quant.has_value()) {
430 const Quantizer& rhs_quant = *m_op->rhs_quant.value();
431 tmp_mm_rhs_t =
432 rhs_quant.dequantize(m_op->acc_dtype, {m_shape_n, m_shape_k}, rhs_t_qdata.data(), rhs_t_qscale.data(), {});
433 mm_rhs_t_view = tmp_mm_rhs_t.view();
434 mm_rhs_dtype = m_op->acc_dtype;
435 mm_rhs_id = "dequantize(" + rhs_quant.uid() + "," + std::string(rhs_t_qdata.id()) + ")";
436 } else {
437 mm_rhs_t_view = rhs_t_data.data();
438 mm_rhs_id = std::string(rhs_t_data.id());
439 }
440
441 // Runs the reference matrix multiplication.
442 const MatMulFn matmul_fn = make_matmul_nt_t(mm_lhs_dtype, mm_rhs_dtype, m_op->acc_dtype);
443 Buffer ref_dst = matmul_fn(m_shape_m, m_shape_n, m_shape_k, mm_lhs_view, mm_rhs_t_view);
444
445 const BinaryElementwiseFn add_fn = make_add_2d(m_op->acc_dtype);
446
447 if (config.bias_modes.has(MatMulBiasMode::ACCUMULATION_PER_M)) {
448 KAI_TEST_ASSERT_MSG(
449 m_op->bias_dtype == m_op->acc_dtype, "Only support the accumulator and per-M bias type being the same.");
450
451 ref_dst = add_fn(m_shape_m, m_shape_n, ref_dst, m_shape_m, 1, acc_bias_m_data.data());
452 }
453 if (config.bias_modes.has(MatMulBiasMode::ACCUMULATION_PER_N)) {
454 Buffer tmp_bias;
455 Span<const std::byte> bias_view = acc_bias_n_data.data();
456
457 if (m_op->bias_dtype != m_op->acc_dtype) {
458 tmp_bias = cast(acc_bias_n_data.data_ptr(), m_op->bias_dtype, m_op->acc_dtype, 1, m_shape_n);
459 bias_view = tmp_bias.view();
460 }
461
462 ref_dst = add_fn(m_shape_m, m_shape_n, ref_dst, 1, m_shape_n, bias_view);
463 }
464
465 const bool has_acc_scaling_stage = //
466 is_tensor_required(MatMulSlot::ACC_SCALE_GLOBAL_DATA) || //
467 is_tensor_required(MatMulSlot::ACC_SCALE_M_DATA) || //
468 is_tensor_required(MatMulSlot::ACC_SCALE_N_DATA);
469 if (m_op->acc_dtype != m_op->dst_dtype) {
470 ref_dst = cast(ref_dst.data(), m_op->acc_dtype, m_op->dst_dtype, m_shape_m, m_shape_n);
471 }
472
473 if (has_acc_scaling_stage) {
474 const BinaryElementwiseFn multiply_fn = make_multiply_2d(m_op->dst_dtype);
475
476 if (is_tensor_required(MatMulSlot::ACC_SCALE_GLOBAL_DATA)) {
477 ref_dst =
478 multiply_fn(m_shape_m, m_shape_n, ref_dst, 1, 1, get_tensor(MatMulSlot::ACC_SCALE_GLOBAL_DATA).data());
479 }
480
481 if (is_tensor_required(MatMulSlot::ACC_SCALE_M_DATA)) {
482 ref_dst = multiply_fn(
483 m_shape_m, m_shape_n, ref_dst, m_shape_m, 1, get_tensor(MatMulSlot::ACC_SCALE_M_DATA).data());
484 }
485
486 if (is_tensor_required(MatMulSlot::ACC_SCALE_N_DATA)) {
487 ref_dst = multiply_fn(
488 m_shape_m, m_shape_n, ref_dst, 1, m_shape_n, get_tensor(MatMulSlot::ACC_SCALE_N_DATA).data());
489 }
490 }
491
492 const bool has_scale_bias_stage = //
493 is_tensor_required(MatMulSlot::SCALE_BIAS_GLOBAL_DATA) || //
494 is_tensor_required(MatMulSlot::SCALE_BIAS_M_DATA) || //
495 is_tensor_required(MatMulSlot::SCALE_BIAS_N_DATA);
496 if (has_scale_bias_stage) {
497 const BinaryElementwiseFn add_fn = make_add_2d(m_op->dst_dtype);
498
499 if (is_tensor_required(MatMulSlot::SCALE_BIAS_GLOBAL_DATA)) {
500 ref_dst =
501 add_fn(m_shape_m, m_shape_n, ref_dst, 1, 1, get_tensor(MatMulSlot::SCALE_BIAS_GLOBAL_DATA).data());
502 }
503
504 if (is_tensor_required(MatMulSlot::SCALE_BIAS_M_DATA)) {
505 ref_dst =
506 add_fn(m_shape_m, m_shape_n, ref_dst, m_shape_m, 1, get_tensor(MatMulSlot::SCALE_BIAS_M_DATA).data());
507 }
508
509 if (config.bias_modes.has(MatMulBiasMode::SCALE_BIAS_PER_N)) {
510 ref_dst =
511 add_fn(m_shape_m, m_shape_n, ref_dst, 1, m_shape_n, get_tensor(MatMulSlot::SCALE_BIAS_N_DATA).data());
512 }
513 }
514
515 std::optional<MatMulClampArgsF32> clamp_args = std::nullopt;
516
517 if (m_op->clamp_mode != MatMulClampMode::UNSUPPORTED &&
518 (m_clamp_keep_ratio.has_value() || m_op->clamp_mode == MatMulClampMode::REQUIRED)) {
519 const size_t dst_size = m_shape_m * m_shape_n;
520 const auto [clamp_min, clamp_max] =
521 find_clamp_range(m_op->dst_dtype, ref_dst.data(), dst_size, m_clamp_keep_ratio);
522
523 clamp_args = MatMulClampArgsF32{clamp_min, clamp_max};
524
525 if (m_clamp_keep_ratio.has_value()) {
526 ref_dst = clamp(m_op->dst_dtype, ref_dst.data(), dst_size, clamp_min, clamp_max);
527 }
528 }
529 kernel_args.set_value(std::move(clamp_args));
530 std::string uid = "matmul(" + mm_lhs_id + "," + mm_rhs_id + ")";
531 ref_dst_data.set_data(std::move(ref_dst), uid);
532}
533
534std::tuple<size_t, size_t> MatMulTb::lhs_packing_steps() const {
535 const KernelWrapper<MatShape>& pack_lhs = *m_op->pack_lhs.value();
536 const std::vector<size_t> steps = pack_lhs.steps({m_shape_m, m_shape_k}, m_tensors);
537 return {steps.at(as_idx(MatDim::R)), steps.at(as_idx(MatDim::C))};
538}
539
540void MatMulTb::test_lhs_packing(size_t start_m, size_t start_k, size_t size_m, size_t size_k) {
541 const KernelWrapper<MatShape>& pack_lhs = *m_op->pack_lhs.value();
542
543 const std::array full_shape{m_shape_m, m_shape_k};
544 const std::array tile_coords{start_m, start_k};
545 const std::array tile_shape{size_m, size_k};
546
547 pack_lhs.run(full_shape, tile_coords, tile_shape, m_tensors);
548
549 const Tensor& ref_packed_lhs = get_tensor(MatMulSlot::LHS_PACKED);
550 const Tensor& imp_packed_lhs = get_tensor(MatMulSlot::LHS_PACKED_IMP);
551 const Format& format = *ref_packed_lhs.format();
552
553 DefaultMismatchHandler handler(0.0F, 0.0F, 0, 0.0F);
554 const bool ok =
555 format.compare(full_shape, tile_coords, tile_shape, imp_packed_lhs.data(), ref_packed_lhs.data(), handler);
556 KAI_TEST_ASSERT(ok);
557}
558
559std::tuple<size_t, size_t> MatMulTb::rhs_packing_steps() const {
560 const KernelWrapper<MatShape>& pack_rhs = *m_op->pack_rhs.value();
561 const std::vector<size_t> steps = pack_rhs.steps({m_shape_n, m_shape_k}, m_tensors);
562 return {steps.at(as_idx(MatDim::R)), steps.at(as_idx(MatDim::C))};
563}
564
565void MatMulTb::test_rhs_packing(size_t start_n, size_t start_k, size_t size_n, size_t size_k) {
566 const KernelWrapper<MatShape>& pack_rhs = *m_op->pack_rhs.value();
567
568 const std::array full_shape{m_shape_n, m_shape_k};
569 const std::array tile_coords{start_n, start_k};
570 const std::array tile_shape{size_n, size_k};
571
572 pack_rhs.run(full_shape, tile_coords, tile_shape, m_tensors);
573
574 const Tensor& ref_packed_rhs = get_tensor(MatMulSlot::RHS_PACKED);
575 const Tensor& imp_packed_rhs = get_tensor(MatMulSlot::RHS_PACKED_IMP);
576 const Format& format = *ref_packed_rhs.format();
577
578 DefaultMismatchHandler handler(0.0F, 0.0F, 0, 0.0F);
579 const bool ok =
580 format.compare(full_shape, tile_coords, tile_shape, imp_packed_rhs.data(), ref_packed_rhs.data(), handler);
581 KAI_TEST_ASSERT(ok);
582}
583
584std::tuple<size_t, size_t> MatMulTb::matmul_steps() const {
585 const KernelWrapper<MatMulShape>& matmul = *m_op->matmul.value();
586 const std::vector<size_t> steps = matmul.steps({m_shape_m, m_shape_n, m_shape_k}, m_tensors);
587 return {steps.at(as_idx(MatMulDim::M)), steps.at(as_idx(MatMulDim::N))};
588}
589
590void MatMulTb::test_matmul(size_t start_m, size_t start_n, size_t size_m, size_t size_n) {
591 const KernelWrapper<MatMulShape>& matmul = *m_op->matmul.value();
592
593 const std::array matmul_full_shape{m_shape_m, m_shape_n, m_shape_k};
594 const std::array matmul_tile_coords{start_m, start_n, static_cast<size_t>(0)};
595 const std::array matmul_tile_shape{size_m, size_n, m_shape_k};
596
597 const std::array dst_full_shape{m_shape_m, m_shape_n};
598 const std::array dst_tile_coords{start_m, start_n};
599 const std::array dst_tile_shape{size_m, size_n};
600
601 matmul.run(matmul_full_shape, matmul_tile_coords, matmul_tile_shape, m_tensors);
602
603 const Tensor& ref_dst_data = get_tensor(MatMulSlot::DST_DATA);
604 const Tensor& imp_dst_data = get_tensor(MatMulSlot::DST_DATA_IMP);
605 const Format& format = *ref_dst_data.format();
606
607 DefaultMismatchHandler handler(1e-3, 1e-3, 0, 0.0F);
608 const bool ok = format.compare(
609 dst_full_shape, dst_tile_coords, dst_tile_shape, imp_dst_data.data(), ref_dst_data.data(), handler);
610 KAI_TEST_ASSERT(ok);
611}
612
613void MatMulTb::set_tensor_required(MatMulSlot slot) {
614 m_tensors_required.at(as_idx(slot)) = true;
615}
616
617bool MatMulTb::is_tensor_required(MatMulSlot slot) {
618 return m_tensors_required.at(as_idx(slot));
619}
620
621Tensor& MatMulTb::get_tensor(MatMulSlot slot) {
622 return m_tensors.at(as_idx(slot));
623}
624
625} // namespace kai::test