Elffuss BraidHub

kikocis/kleidiai public

main · 6.4 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6
7#include "test/reference/binary_elementwise.hpp"
8
9#include <algorithm>
10#include <cstddef>
11#include <cstdint>
12
13#include "kai/kai_common.h"
14#include "test/common/buffer.hpp"
15#include "test/common/data_type.hpp"
16#include "test/common/float16.hpp"
17#include "test/common/int4.hpp"
18#include "test/common/memory.hpp"
19
20namespace kai::test {
21
22namespace {
23
24/// Binary element-wise operator.
25enum class BinaryElementwiseOperator : uint32_t {
26 ADD, ///< Addition.
27 SUB, ///< Subtraction.
28 MUL, ///< Multiplication.
29 DIV, ///< Division.
30};
31
32/// Scalar binary element-wise function.
33///
34/// @tparam op Binary element-wise operator to perform.
35/// @tparam T Data type.
36///
37/// @param[in] lhs LHS operand.
38/// @param[in] rhs RHS operand.
39///
40/// @return The result of the operation.
41template <const BinaryElementwiseOperator op, typename T>
42T scalar_binary_elementwise(T lhs, T rhs) {
43 if constexpr (op == BinaryElementwiseOperator::ADD) {
44 return lhs + rhs;
45 } else if constexpr (op == BinaryElementwiseOperator::SUB) {
46 return lhs - rhs;
47 } else if constexpr (op == BinaryElementwiseOperator::MUL) {
48 return lhs * rhs;
49 } else if constexpr (op == BinaryElementwiseOperator::DIV) {
50 return lhs / rhs;
51 } else {
52 KAI_ERROR("Unsupported binary element-wise operator!");
53 }
54}
55
56/// Binary element-wise function.
57///
58/// @tparam op Binary element-wise operator to perform.
59/// @tparam T Data type.
60///
61/// @param[in] lhs LHS data buffer.
62/// @param[in] rhs RHS data buffer.
63/// @param[in] lhs_height LHS height.
64/// @param[in] lhs_width LHS width.
65/// @param[in] rhs_height RHS height.
66/// @param[in] rhs_width RHS width.
67///
68/// @return The result data buffer.
69template <const BinaryElementwiseOperator op, typename T>
70Buffer binary_elementwise_any_op_type(
71 const void* lhs, const void* rhs, size_t lhs_height, size_t lhs_width, size_t rhs_height, size_t rhs_width) {
72 const auto height = std::max(lhs_height, rhs_height);
73 const auto width = std::max(lhs_width, rhs_width);
74
75 KAI_ASSUME_ALWAYS(width * size_in_bits<T> % 8 == 0);
76 Buffer dst(height * width * size_in_bits<T> / 8);
77
78 for (size_t y = 0; y < height; ++y) {
79 for (size_t x = 0; x < width; ++x) {
80 const auto lhs_y = lhs_height > 1 ? y : 0;
81 const auto lhs_x = lhs_width > 1 ? x : 0;
82 const auto lhs_value = read_array<T>(lhs, lhs_y * lhs_width + lhs_x);
83
84 const auto rhs_y = rhs_height > 1 ? y : 0;
85 const auto rhs_x = rhs_width > 1 ? x : 0;
86 const auto rhs_value = read_array<T>(rhs, rhs_y * rhs_width + rhs_x);
87
88 const auto dst_value = scalar_binary_elementwise<op, T>(lhs_value, rhs_value);
89 write_array<T>(dst.data(), y * width + x, dst_value);
90 }
91 }
92
93 return dst;
94}
95
96template <const BinaryElementwiseOperator op>
97Buffer binary_elementwise_any_type(
98 const void* lhs, DataType lhs_dt, size_t lhs_height, size_t lhs_width, //
99 const void* rhs, DataType rhs_dt, size_t rhs_height, size_t rhs_width) {
100 KAI_ASSUME_ALWAYS(lhs_dt == rhs_dt);
101 KAI_ASSUME_ALWAYS(lhs_height == 1 || rhs_height == 1 || lhs_height == rhs_height);
102 KAI_ASSUME_ALWAYS(lhs_width == 1 || rhs_width == 1 || lhs_width == rhs_width);
103
104 switch (lhs_dt) {
105 case DataType::FP32:
106 return binary_elementwise_any_op_type<op, float>(lhs, rhs, lhs_height, lhs_width, rhs_height, rhs_width);
107
108 case DataType::FP16:
109 return binary_elementwise_any_op_type<op, Float16>(lhs, rhs, lhs_height, lhs_width, rhs_height, rhs_width);
110
111 case DataType::I32:
112 return binary_elementwise_any_op_type<op, int32_t>(lhs, rhs, lhs_height, lhs_width, rhs_height, rhs_width);
113
114 case DataType::QSU4:
115 return binary_elementwise_any_op_type<op, UInt4>(lhs, rhs, lhs_height, lhs_width, rhs_height, rhs_width);
116
117 default:
118 KAI_ERROR("Unsupported data type!");
119 }
120}
121
122} // namespace
123
124Buffer add(
125 const void* lhs, DataType lhs_dt, size_t lhs_height, size_t lhs_width, //
126 const void* rhs, DataType rhs_dt, size_t rhs_height, size_t rhs_width) {
127 return binary_elementwise_any_type<BinaryElementwiseOperator::ADD>(
128 lhs, lhs_dt, lhs_height, lhs_width, rhs, rhs_dt, rhs_height, rhs_width);
129}
130
131Buffer sub(
132 const void* lhs, DataType lhs_dt, size_t lhs_height, size_t lhs_width, //
133 const void* rhs, DataType rhs_dt, size_t rhs_height, size_t rhs_width) {
134 return binary_elementwise_any_type<BinaryElementwiseOperator::SUB>(
135 lhs, lhs_dt, lhs_height, lhs_width, rhs, rhs_dt, rhs_height, rhs_width);
136}
137
138template <typename T>
139Buffer sub(
140 const void* lhs, size_t lhs_height, size_t lhs_width, //
141 const void* rhs, size_t rhs_height, size_t rhs_width) {
142 return binary_elementwise_any_op_type<BinaryElementwiseOperator::SUB, T>(
143 lhs, rhs, lhs_height, lhs_width, rhs_height, rhs_width);
144}
145
146template Buffer sub<int32_t>(
147 const void* lhs, size_t lhs_height, size_t lhs_width, //
148 const void* rhs, size_t rhs_height, size_t rhs_width);
149
150Buffer mul(
151 const void* lhs, DataType lhs_dt, size_t lhs_height, size_t lhs_width, //
152 const void* rhs, DataType rhs_dt, size_t rhs_height, size_t rhs_width) {
153 return binary_elementwise_any_type<BinaryElementwiseOperator::MUL>(
154 lhs, lhs_dt, lhs_height, lhs_width, rhs, rhs_dt, rhs_height, rhs_width);
155}
156
157template <typename T>
158Buffer mul(
159 const void* lhs, size_t lhs_height, size_t lhs_width, //
160 const void* rhs, size_t rhs_height, size_t rhs_width) {
161 return binary_elementwise_any_op_type<BinaryElementwiseOperator::MUL, T>(
162 lhs, rhs, lhs_height, lhs_width, rhs_height, rhs_width);
163}
164
165template Buffer mul<float>(
166 const void* lhs, size_t lhs_height, size_t lhs_width, //
167 const void* rhs, size_t rhs_height, size_t rhs_width);
168
169template Buffer mul<int32_t>(
170 const void* lhs, size_t lhs_height, size_t lhs_width, //
171 const void* rhs, size_t rhs_height, size_t rhs_width);
172
173Buffer div(
174 const void* lhs, DataType lhs_dt, size_t lhs_height, size_t lhs_width, //
175 const void* rhs, DataType rhs_dt, size_t rhs_height, size_t rhs_width) {
176 return binary_elementwise_any_type<BinaryElementwiseOperator::DIV>(
177 lhs, lhs_dt, lhs_height, lhs_width, rhs, rhs_dt, rhs_height, rhs_width);
178}
179
180} // namespace kai::test