Elffuss BraidHub

kikocis/kleidiai public

main · 2.2 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/reduce.hpp"
8
9#include <cstddef>
10#include <cstdint>
11#include <tuple>
12
13#include "test/common/assert.hpp"
14#include "test/common/buffer.hpp"
15#include "test/common/data_type.hpp"
16#include "test/common/int4.hpp"
17#include "test/common/memory.hpp"
18#include "test/common/round.hpp"
19#include "test/common/span.hpp"
20
21namespace kai::test {
22
23namespace {
24
25template <typename Op>
26[[nodiscard]] Buffer reduce(size_t axis, Shape shape, Span<const std::byte> data) {
27 using Input = typename Op::InputType;
28 using Output = typename Op::OutputType;
29
30 KAI_TEST_ASSERT(shape.size() > axis);
31
32 KAI_TEST_ASSERT_MSG(shape.size() == 2, "Only 2D data is supported.");
33 KAI_TEST_ASSERT_MSG(axis == 0, "Only row reduction is supported.");
34
35 const size_t height = shape.at(0);
36 const size_t width = shape.at(1);
37 const size_t src_row_size = round_up_division(width * size_in_bits<Input>, 8);
38
39 const size_t dst_size = round_up_division(height * size_in_bits<Output>, 8);
40 Buffer dst(dst_size, 0);
41
42 for (size_t row = 0; row < height; ++row) {
43 const Span<const std::byte> src_row_data = data.subspan(row * src_row_size, src_row_size);
44
45 Output acc = Op::init();
46
47 for (size_t col = 0; col < width; ++col) {
48 const Input value = read_array<Input>(src_row_data, col);
49 acc = Op::reduce(acc, value);
50 }
51
52 write_array<Output>(dst, row, acc);
53 }
54
55 return dst;
56}
57
58template <typename Input, typename Output>
59struct AddOp {
60 using InputType = Input;
61 using OutputType = Output;
62
63 [[nodiscard]] static Output init() {
64 return {};
65 }
66
67 [[nodiscard]] static Output reduce(Output acc, Input value) {
68 return acc + static_cast<Output>(value);
69 }
70};
71
72} // namespace
73
74ReduceFn make_reduce_add(DataType src_dtype, DataType dst_dtype) {
75 const auto dtypes = std::make_tuple(src_dtype, dst_dtype);
76
77 if (dtypes == std::make_tuple(DataType::I4, DataType::I32)) {
78 return reduce<AddOp<Int4, int32_t>>;
79 }
80
81 KAI_TEST_ERROR("Not implemented.");
82}
83
84} // namespace kai::test