test/nextgen/reference/binary_elementwise.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/binary_elementwise.hpp" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cstddef> | ||
| 11 | #include <functional> | ||
| 12 | #include <numeric> | ||
| 13 | #include <vector> | ||
| 14 | |||
| 15 | #include "test/common/assert.hpp" | ||
| 16 | #include "test/common/buffer.hpp" | ||
| 17 | #include "test/common/data_type.hpp" | ||
| 18 | #include "test/common/int4.hpp" | ||
| 19 | #include "test/common/memory.hpp" | ||
| 20 | #include "test/common/round.hpp" | ||
| 21 | #include "test/common/span.hpp" | ||
| 22 | |||
| 23 | namespace kai::test { | ||
| 24 | |||
| 25 | namespace { | ||
| 26 | |||
| 27 | template <typename T> | ||
| 28 | 145 | [[nodiscard]] Buffer add( | |
| 29 | size_t lhs_height, size_t lhs_width, Span<const std::byte> lhs_data, size_t rhs_height, size_t rhs_width, | ||
| 30 | Span<const std::byte> rhs_data) { | ||
| 31 | 145 | const size_t dst_height = std::max(lhs_height, rhs_height); | |
| 32 |
3/16✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 145 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 145 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
145 | KAI_TEST_ASSERT(lhs_height == rhs_height || lhs_height == 1 || rhs_height == 1); |
| 33 | |||
| 34 | 145 | const size_t dst_width = std::max(lhs_width, rhs_width); | |
| 35 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
145 | KAI_TEST_ASSERT(lhs_width == rhs_width || lhs_width == 1 || rhs_width == 1); |
| 36 | |||
| 37 | 145 | const size_t lhs_row_size = round_up_division(lhs_width * size_in_bits<T>, 8); | |
| 38 | 145 | const size_t rhs_row_size = round_up_division(rhs_width * size_in_bits<T>, 8); | |
| 39 | 145 | const size_t dst_row_size = round_up_division(dst_width * size_in_bits<T>, 8); | |
| 40 | |||
| 41 | 145 | const size_t dst_size = dst_height * dst_row_size; | |
| 42 | |||
| 43 | 145 | Buffer dst(dst_size, 0); | |
| 44 | |||
| 45 |
2/4✓ Branch 0 taken 145 times.
✓ Branch 1 taken 11497 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
11642 | for (size_t row = 0; row < dst_height; ++row) { |
| 46 |
1/4✓ Branch 0 taken 11497 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
11497 | const Span<const std::byte> lhs_row_data = lhs_data.subspan((row % lhs_height) * lhs_row_size, lhs_row_size); |
| 47 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 11497 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
11497 | const Span<const std::byte> rhs_row_data = rhs_data.subspan((row % rhs_height) * rhs_row_size, rhs_row_size); |
| 48 |
2/8✓ Branch 0 taken 11497 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11497 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
11497 | const Span<std::byte> dst_row_data = Span<std::byte>(dst).subspan(row * dst_row_size, dst_row_size); |
| 49 | |||
| 50 |
2/4✓ Branch 0 taken 889480 times.
✓ Branch 1 taken 11497 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
900977 | for (size_t col = 0; col < dst_width; ++col) { |
| 51 |
1/4✓ Branch 0 taken 889480 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
889480 | const T lhs_value = read_array<T>(lhs_row_data, col % lhs_width); |
| 52 |
1/4✓ Branch 0 taken 889480 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
889480 | const T rhs_value = read_array<T>(rhs_row_data, col % rhs_width); |
| 53 | |||
| 54 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
889480 | const T dst_value = lhs_value + rhs_value; |
| 55 | |||
| 56 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 889480 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
889480 | write_array<T>(dst_row_data, col, dst_value); |
| 57 | 889480 | } | |
| 58 | 11497 | } | |
| 59 | |||
| 60 | 145 | return dst; | |
| 61 | 145 | } | |
| 62 | |||
| 63 | } // namespace | ||
| 64 | |||
| 65 | 145 | BinaryElementwiseFn make_add_2d(DataType dtype) { | |
| 66 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
|
145 | switch (dtype) { |
| 67 | case DataType::FP32: | ||
| 68 | 145 | return add<float>; | |
| 69 | |||
| 70 | case DataType::U4: | ||
| 71 | case DataType::I4: | ||
| 72 | ✗ | return add<Int4>; | |
| 73 | |||
| 74 | default: | ||
| 75 | ✗ | KAI_TEST_ERROR("Not supported."); | |
| 76 | } | ||
| 77 | 145 | } | |
| 78 | |||
| 79 | } // namespace kai::test | ||
| 80 |