test/nextgen/reference/reduce.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 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/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 | |||
| 21 | namespace kai::test { | ||
| 22 | |||
| 23 | namespace { | ||
| 24 | |||
| 25 | template <typename Op> | ||
| 26 | 200 | [[nodiscard]] Buffer reduce(size_t axis, Span<const size_t> shape, Span<const std::byte> data) { | |
| 27 | using Input = typename Op::InputType; | ||
| 28 | using Output = typename Op::OutputType; | ||
| 29 | |||
| 30 |
1/4✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
200 | KAI_TEST_ASSERT(shape.size() > axis); |
| 31 | |||
| 32 |
1/4✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
200 | KAI_TEST_ASSERT_MSG(shape.size() == 2, "Only 2D data is supported."); |
| 33 |
1/4✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
200 | KAI_TEST_ASSERT_MSG(axis == 0, "Only row reduction is supported."); |
| 34 | |||
| 35 | 200 | const size_t height = shape.at(0); | |
| 36 | 200 | const size_t width = shape.at(1); | |
| 37 | 200 | const size_t src_row_size = round_up_division(width * size_in_bits<Input>, 8); | |
| 38 | |||
| 39 | 200 | const size_t dst_size = round_up_division(height * size_in_bits<Output>, 8); | |
| 40 | 200 | Buffer dst(dst_size, 0); | |
| 41 | |||
| 42 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 15923 times.
|
16123 | for (size_t row = 0; row < height; ++row) { |
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15923 times.
|
15923 | const Span<const std::byte> src_row_data = data.subspan(row * src_row_size, src_row_size); |
| 44 | |||
| 45 |
1/2✓ Branch 0 taken 15923 times.
✗ Branch 1 not taken.
|
15923 | Output acc = Op::init(); |
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 15923 times.
✓ Branch 1 taken 1272221 times.
|
1288144 | for (size_t col = 0; col < width; ++col) { |
| 48 |
1/2✓ Branch 0 taken 1272221 times.
✗ Branch 1 not taken.
|
1272221 | const Input value = read_array<Input>(src_row_data, col); |
| 49 |
1/2✓ Branch 0 taken 1272221 times.
✗ Branch 1 not taken.
|
1272221 | acc = Op::reduce(acc, value); |
| 50 | 1272221 | } | |
| 51 | |||
| 52 |
2/4✓ Branch 0 taken 15923 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15923 times.
✗ Branch 3 not taken.
|
15923 | write_array<Output>(dst, row, acc); |
| 53 | 15923 | } | |
| 54 | |||
| 55 | 200 | return dst; | |
| 56 | 200 | } | |
| 57 | |||
| 58 | template <typename Input, typename Output> | ||
| 59 | struct AddOp { | ||
| 60 | using InputType = Input; | ||
| 61 | using OutputType = Output; | ||
| 62 | |||
| 63 | 15923 | [[nodiscard]] static Output init() { | |
| 64 | 15923 | return {}; | |
| 65 | } | ||
| 66 | |||
| 67 | 1272221 | [[nodiscard]] static Output reduce(Output acc, Input value) { | |
| 68 | 1272221 | return acc + static_cast<Output>(value); | |
| 69 | } | ||
| 70 | }; | ||
| 71 | |||
| 72 | } // namespace | ||
| 73 | |||
| 74 | 200 | ReduceFn make_reduce_add(DataType src_dtype, DataType dst_dtype) { | |
| 75 | 200 | const auto dtypes = std::make_tuple(src_dtype, dst_dtype); | |
| 76 | |||
| 77 |
1/2✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
|
200 | if (dtypes == std::make_tuple(DataType::U4, DataType::I32)) { |
| 78 | 200 | return reduce<AddOp<UInt4, int32_t>>; | |
| 79 | } | ||
| 80 | |||
| 81 | ✗ | KAI_TEST_ERROR("Not implemented."); | |
| 82 | 200 | } | |
| 83 | |||
| 84 | } // namespace kai::test | ||
| 85 |