| 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/format/plain_format.hpp" |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <cstddef> |
| 11 | #include <cstdint> |
| 12 | #include <numeric> |
| 13 | #include <ostream> |
| 14 | |
| 15 | #include "test/common/assert.hpp" |
| 16 | #include "test/common/buffer.hpp" |
| 17 | #include "test/common/compare.hpp" |
| 18 | #include "test/common/data_type.hpp" |
| 19 | #include "test/common/round.hpp" |
| 20 | #include "test/common/span.hpp" |
| 21 | #include "test/nextgen/format/fill.hpp" |
| 22 | #include "test/nextgen/format/format.hpp" |
| 23 | #include "test/nextgen/reference/compare.hpp" |
| 24 | #include "test/nextgen/reference/print.hpp" |
| 25 | |
| 26 | namespace kai::test { |
| 27 | |
| 28 | size_t PlainFormat::compute_offset(Shape shape, Span<const size_t> indices) const { |
| 29 | KAI_TEST_ASSERT(shape.size() > 0); |
| 30 | KAI_TEST_ASSERT(shape.size() == indices.size()); |
| 31 | |
| 32 | const size_t num_dims = shape.size(); |
| 33 | size_t stride = round_up_division(shape.at(num_dims - 1) * data_type_size_in_bits(m_dtype), 8); |
| 34 | size_t offset = indices.at(num_dims - 1) * data_type_size_in_bits(m_dtype) / 8; |
| 35 | KAI_TEST_ASSERT(indices.at(num_dims - 1) * data_type_size_in_bits(m_dtype) % 8 == 0); |
| 36 | |
| 37 | if (num_dims > 1) { |
| 38 | size_t dim = num_dims - 2; |
| 39 | |
| 40 | while (true) { |
| 41 | offset += indices.at(dim) * stride; |
| 42 | stride *= shape.at(dim); |
| 43 | |
| 44 | if (dim == 0) { |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | --dim; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return offset; |
| 53 | } |
| 54 | |
| 55 | size_t PlainFormat::compute_size(Shape shape) const { |
| 56 | if (shape.empty()) { |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | const size_t row_size = round_up_division(shape.at(shape.size() - 1) * data_type_size_in_bits(m_dtype), 8); |
| 61 | const size_t num_rows = std::accumulate(shape.begin(), shape.end() - 1, size_t{1}, std::multiplies<>()); |
| 62 | const size_t size = row_size * num_rows; |
| 63 | |
| 64 | return size; |
| 65 | } |
| 66 | |
| 67 | Buffer PlainFormat::generate(Shape shape, const GeneratorFn& generator) const { |
| 68 | KAI_TEST_ASSERT_MSG(static_cast<bool>(generator), "Generator function must be provided."); |
| 69 | |
| 70 | const size_t size = compute_size(shape); |
| 71 | if (size == 0) { |
| 72 | return Buffer(); |
| 73 | } |
| 74 | |
| 75 | Buffer buffer(size); |
| 76 | generator(shape, m_dtype, buffer.view()); |
| 77 | return buffer; |
| 78 | } |
| 79 | |
| 80 | Buffer PlainFormat::pack(Shape shape, Span<const Span<const std::byte>> buffers) const { |
| 81 | KAI_TEST_ASSERT_MSG(buffers.size() == 1, "Plain format only has 1 data component."); |
| 82 | |
| 83 | const Span<const std::byte> data = buffers.at(0); |
| 84 | const size_t size = compute_size(shape); |
| 85 | KAI_TEST_ASSERT_MSG(data.size() == size, "The data buffer must have the right size."); |
| 86 | |
| 87 | Buffer packed_buffer(size); |
| 88 | std::copy(data.begin(), data.end(), packed_buffer.data()); |
| 89 | |
| 90 | return packed_buffer; |
| 91 | } |
| 92 | |
| 93 | bool PlainFormat::compare( |
| 94 | Shape shape, Span<const size_t> tile_coords, Shape tile_shape, Span<const std::byte> imp_buffer, |
| 95 | Span<const std::byte> ref_buffer, MismatchHandler& handler) const { |
| 96 | KAI_TEST_ASSERT_MSG(shape.size() == 2, "Only 2D array is supported."); |
| 97 | |
| 98 | const CompareFn compare_fn = make_compare_plain_2d(m_dtype); |
| 99 | const size_t num_checks = compare_fn( |
| 100 | shape, tile_coords, tile_shape, imp_buffer, ref_buffer, |
| 101 | [](std::ostream& os, Span<const size_t> indices) { |
| 102 | os << "Mismatch at row " << indices.at(0) << ", col " << indices.at(1); |
| 103 | }, |
| 104 | handler); |
| 105 | |
| 106 | return handler.success(num_checks); |
| 107 | } |
| 108 | |
| 109 | void PlainFormat::print(std::ostream& os, Shape shape, Span<const std::byte> data) const { |
| 110 | if (shape.empty()) { |
| 111 | os << "None"; |
| 112 | } else { |
| 113 | const PrintFn print_fn = make_print_array(m_dtype); |
| 114 | print_fn(os, shape, data, 0); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | std::string PlainFormat::uid() const { |
| 119 | return data_type_uid(m_dtype); |
| 120 | } |
| 121 | |
| 122 | bool PlainFormat::operator==(const Format& other) const { |
| 123 | const auto* rhs = dynamic_cast<const PlainFormat*>(&other); |
| 124 | return rhs != nullptr && m_dtype == rhs->m_dtype; |
| 125 | } |
| 126 | |
| 127 | } // namespace kai::test |