Elffuss BraidHub

kikocis/kleidiai public

main · 3.0 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/reference/matmul.hpp"
8
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <type_traits>
13
14#include "test/common/assert.hpp"
15#include "test/common/buffer.hpp"
16#include "test/common/data_type.hpp"
17#include "test/common/float16.hpp"
18#include "test/common/memory.hpp"
19#include "test/common/round.hpp"
20#include "test/common/span.hpp"
21#include "test/nextgen/functions/fused_mul_add.hpp"
22
23namespace kai::test {
24
25namespace {
26
27template <typename Lhs, typename Rhs, typename Acc>
28[[nodiscard]] Buffer matmul_nt_t(
29 size_t shape_m, size_t shape_n, size_t shape_k, Span<const std::byte> lhs, Span<const std::byte> rhs) {
30 Buffer dst(shape_m * round_up_division(shape_n * size_in_bits<Acc>, 8), 0);
31
32 for (size_t row = 0; row < shape_m; ++row) {
33 for (size_t col = 0; col < shape_n; ++col) {
34 Acc acc = static_cast<Acc>(0);
35
36 for (size_t depth = 0; depth < shape_k; ++depth) {
37 const Acc lhs_value = static_cast<Acc>(read_2d<Lhs>(lhs, shape_k, row, depth));
38 const Acc rhs_value = static_cast<Acc>(read_2d<Rhs>(rhs, shape_k, col, depth));
39
40 if constexpr (std::is_floating_point_v<Acc>) {
41 acc = fused_mul_add<Acc>(lhs_value, rhs_value, acc);
42 } else {
43 acc += lhs_value * rhs_value;
44 }
45 }
46
47 write_2d<Acc>(dst.view(), shape_n, row, col, acc);
48 }
49 }
50
51 return dst;
52}
53
54struct MatMulEntry {
55 DataType lhs_dtype;
56 DataType rhs_dtype;
57 DataType acc_dtype;
58 MatMulFn fn;
59};
60
61// Binds a runtime DataType value to its matching C++ storage type.
62template <DataType DType, typename Type>
63struct TypedData {
64 static constexpr DataType dtype = DType;
65 using type = Type;
66};
67
68using FP32 = TypedData<DataType::FP32, float>;
69using FP16 = TypedData<DataType::FP16, Float16>;
70using U8 = TypedData<DataType::U8, uint8_t>;
71using I32 = TypedData<DataType::I32, int32_t>;
72
73// Creates one dispatch table entry mapping a DataType combination to its reference implementation.
74template <typename Lhs, typename Rhs, typename Acc>
75[[nodiscard]] constexpr MatMulEntry make_entry() {
76 return {
77 Lhs::dtype,
78 Rhs::dtype,
79 Acc::dtype,
80 &matmul_nt_t<typename Lhs::type, typename Rhs::type, typename Acc::type>,
81 };
82}
83
84} // namespace
85
86MatMulFn make_matmul_nt_t(DataType lhs_dtype, DataType rhs_dtype, DataType acc_dtype) {
87 static constexpr std::array entries = {
88 make_entry<FP32, FP32, FP32>(),
89 make_entry<FP16, FP16, FP32>(),
90 make_entry<U8, U8, I32>(),
91 };
92
93 for (const auto& [e_lhs, e_rhs, e_acc, fn] : entries) {
94 if (lhs_dtype == e_lhs && rhs_dtype == e_rhs && acc_dtype == e_acc) {
95 return fn;
96 }
97 }
98
99 KAI_TEST_ERROR("Matmul data type combination is not implemented.");
100}
101
102} // namespace kai::test