| 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 | #pragma once |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <cstddef> |
| 11 | #include <optional> |
| 12 | #include <ostream> |
| 13 | #include <string> |
| 14 | #include <string_view> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "test/common/assert.hpp" |
| 19 | #include "test/common/buffer.hpp" |
| 20 | #include "test/common/enum_utils.hpp" |
| 21 | #include "test/nextgen/common/poly.hpp" |
| 22 | #include "test/nextgen/common/shape.hpp" |
| 23 | #include "test/nextgen/format/format.hpp" |
| 24 | #include "test/nextgen/operators/matmul/matmul_slots.hpp" |
| 25 | |
| 26 | namespace kai::test { |
| 27 | |
| 28 | /// Multidimensional data slot. |
| 29 | class Tensor { |
| 30 | public: |
| 31 | /// Gets the size of the multidimensional array. |
| 32 | [[nodiscard]] Shape shape() const { |
| 33 | return m_shape; |
| 34 | } |
| 35 | |
| 36 | /// Sets the shape. |
| 37 | /// |
| 38 | /// A slot cannot change its shape. If the shape is already non-zero, |
| 39 | /// the new shape must be the same as the existing shape |
| 40 | /// to make sure all components in the test environment have the same |
| 41 | /// expectation for this slot. |
| 42 | Tensor& set_shape(Shape shape) { |
| 43 | if (m_shape.empty()) { |
| 44 | KAI_TEST_ASSERT_MSG(m_data.size() == 0, "The slot must be empty when its shape is setup."); |
| 45 | m_shape = std::vector(shape.begin(), shape.end()); |
| 46 | } else { |
| 47 | KAI_TEST_ASSERT_MSG( |
| 48 | std::equal(m_shape.begin(), m_shape.end(), shape.begin(), shape.end()), |
| 49 | "The new shape must be the same as the existing shape."); |
| 50 | } |
| 51 | |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | /// Gets the data format. |
| 56 | [[nodiscard]] const Poly<Format>& format() const { |
| 57 | return m_format.value(); |
| 58 | } |
| 59 | |
| 60 | /// Sets the data format. |
| 61 | /// |
| 62 | /// A slot cannot change its data format. If the data format is already known, |
| 63 | /// the new format must be the same as the existing one |
| 64 | /// to make sure all components in the test environment have the same |
| 65 | /// expectation for this slot. |
| 66 | Tensor& set_format(const Poly<Format>& format) { |
| 67 | if (!m_format.has_value()) { |
| 68 | KAI_TEST_ASSERT_MSG(m_data.size() == 0, "The slot must be empty when its format is setup."); |
| 69 | m_format = format; |
| 70 | } else { |
| 71 | KAI_TEST_ASSERT_MSG( |
| 72 | *m_format.value() == *format, "The new format must be the same as the existing format."); |
| 73 | } |
| 74 | |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | /// Gets the data. |
| 79 | [[nodiscard]] Span<const std::byte> data() const { |
| 80 | return m_data; |
| 81 | } |
| 82 | |
| 83 | /// Gets the data. |
| 84 | [[nodiscard]] Span<std::byte> data() { |
| 85 | return m_data; |
| 86 | } |
| 87 | |
| 88 | /// Gets the data. |
| 89 | [[nodiscard]] const std::byte* data_ptr() const { |
| 90 | return m_data.data(); |
| 91 | } |
| 92 | |
| 93 | /// Gets the data. |
| 94 | [[nodiscard]] std::byte* data_ptr() { |
| 95 | return m_data.data(); |
| 96 | } |
| 97 | |
| 98 | [[nodiscard]] std::string_view id() const { |
| 99 | KAI_TEST_ASSERT_MSG(!m_id.empty(), "Tensor ID must not be empty."); |
| 100 | return m_id; |
| 101 | } |
| 102 | |
| 103 | void set_id(const std::string& id) { |
| 104 | KAI_TEST_ASSERT_MSG(!id.empty(), "Tensor ID must not be empty."); |
| 105 | KAI_TEST_ASSERT_MSG(m_id.empty() || m_id == id, "Tensor ID must not be changed."); |
| 106 | m_id = id; |
| 107 | } |
| 108 | |
| 109 | /// Gets the value in custom format. |
| 110 | template <typename T> |
| 111 | [[nodiscard]] const T& value() const { |
| 112 | KAI_TEST_ASSERT_MSG(!m_format.has_value(), "This method only works on custom data."); |
| 113 | KAI_TEST_ASSERT_MSG(m_data.size() == sizeof(T), "The data size is incorrect."); |
| 114 | |
| 115 | return *reinterpret_cast<const T*>(data_ptr()); |
| 116 | } |
| 117 | |
| 118 | /// Gets the value in custom format. |
| 119 | template <typename T> |
| 120 | [[nodiscard]] T& value() { |
| 121 | KAI_TEST_ASSERT_MSG(!m_format.has_value(), "This method only works on custom data."); |
| 122 | KAI_TEST_ASSERT_MSG(m_data.size() == sizeof(T), "The data size is incorrect."); |
| 123 | |
| 124 | return *reinterpret_cast<T*>(data_ptr()); |
| 125 | } |
| 126 | |
| 127 | /// Sets the value in custom format. |
| 128 | template <typename T> |
| 129 | void set_value(T&& value) { |
| 130 | KAI_TEST_ASSERT_MSG(!m_format.has_value(), "This method only works on custom data."); |
| 131 | if (m_shape.empty()) { |
| 132 | KAI_TEST_ASSERT_MSG( |
| 133 | m_data.size() == 0, "If the shape is not specified, the data cannot be already allocated."); |
| 134 | |
| 135 | set_shape({sizeof(T)}); |
| 136 | allocate(); |
| 137 | } else { |
| 138 | KAI_TEST_ASSERT_MSG(m_shape.size() == 1 && m_shape.at(0) == sizeof(T), "The shape is incorrect."); |
| 139 | KAI_TEST_ASSERT_MSG(m_data.size() == sizeof(T), "The data size is incorrect."); |
| 140 | } |
| 141 | |
| 142 | this->value<T>() = std::forward<T>(value); |
| 143 | } |
| 144 | |
| 145 | /// Allocates and resets the data buffer. |
| 146 | void allocate() { |
| 147 | const size_t size = compute_size(); |
| 148 | m_data = Buffer(size, 0); |
| 149 | } |
| 150 | |
| 151 | /// Sets the data buffer. |
| 152 | /// |
| 153 | /// The new data buffer must have the right size. |
| 154 | void set_data(Buffer&& buffer, std::string id = {}) { |
| 155 | [[maybe_unused]] const size_t size = compute_size(); |
| 156 | KAI_TEST_ASSERT_MSG(buffer.size() == size, "New data buffer must have the right size."); |
| 157 | |
| 158 | if (!id.empty()) { |
| 159 | set_id(id); |
| 160 | } |
| 161 | |
| 162 | m_data = std::move(buffer); |
| 163 | } |
| 164 | |
| 165 | private: |
| 166 | [[nodiscard]] size_t compute_size() const { |
| 167 | if (m_format.has_value()) { |
| 168 | return m_format.value()->compute_size(m_shape); |
| 169 | } |
| 170 | |
| 171 | // If the data format is not available, this slot is used to store custom data |
| 172 | // rather than multidimensional array. The first element of the shape |
| 173 | // is the size of the data being stored. |
| 174 | KAI_TEST_ASSERT_MSG( |
| 175 | m_shape.size() == 1, "Custom data slot must use the shape to store the size of entire data."); |
| 176 | return m_shape.at(0); |
| 177 | } |
| 178 | |
| 179 | std::vector<size_t> m_shape; |
| 180 | std::optional<Poly<Format>> m_format; |
| 181 | Buffer m_data; |
| 182 | std::string m_id; |
| 183 | }; |
| 184 | |
| 185 | /// Helper class for allowing MatMulSlot to be used as index. |
| 186 | class TensorSet : public Span<Tensor> { |
| 187 | public: |
| 188 | using Span<Tensor>::Span; |
| 189 | using Span<Tensor>::at; |
| 190 | |
| 191 | /// Gets the tensor at the specified slot with bounds checking. |
| 192 | /// |
| 193 | /// @param[in] slot The tensor slot. |
| 194 | /// |
| 195 | /// @return The tensor at the requested slot. |
| 196 | [[nodiscard]] constexpr Tensor& at(MatMulSlot slot) const { |
| 197 | return Span<Tensor>::at(as_idx(slot)); |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | /// Helper class for allowing MatMulSlot to be used as index. |
| 202 | class ConstTensorSet : public Span<const Tensor> { |
| 203 | public: |
| 204 | using Span<const Tensor>::Span; |
| 205 | using Span<const Tensor>::at; |
| 206 | |
| 207 | /// Gets the tensor at the specified slot with bounds checking. |
| 208 | /// |
| 209 | /// @param[in] slot The tensor slot. |
| 210 | /// |
| 211 | /// @return The tensor at the requested slot. |
| 212 | [[nodiscard]] constexpr const Tensor& at(MatMulSlot slot) const { |
| 213 | return Span<const Tensor>::at(as_idx(slot)); |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | } // namespace kai::test |