test/nextgen/format/format.hpp
| 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 | #pragma once | ||
| 8 | |||
| 9 | #include <cstddef> | ||
| 10 | #include <ostream> | ||
| 11 | |||
| 12 | #include "test/common/assert.hpp" | ||
| 13 | #include "test/common/buffer.hpp" | ||
| 14 | #include "test/common/compare.hpp" | ||
| 15 | #include "test/common/data_type.hpp" | ||
| 16 | #include "test/common/span.hpp" | ||
| 17 | #include "test/nextgen/common/random.hpp" | ||
| 18 | |||
| 19 | namespace kai::test { | ||
| 20 | |||
| 21 | /// Data format. | ||
| 22 | /// | ||
| 23 | /// A data format contains the description of how the data is stored in the memory, | ||
| 24 | /// including data type, data reordering rule, multi-component data packing, etc. | ||
| 25 | /// | ||
| 26 | /// Data format does not own data nor has any information about the size of the data | ||
| 27 | /// as well as the underlining meaning of the data (e.g. data, scale, bias, etc.). | ||
| 28 | class Format { | ||
| 29 | public: | ||
| 30 | 2860 | Format() = default; ///< Default constructor. | |
| 31 | 2908 | virtual ~Format() = default; ///< Destructor. | |
| 32 | 3648 | Format(const Format&) = default; ///< Copy constructor. | |
| 33 | Format& operator=(const Format&) = default; ///< Copy assignment. | ||
| 34 | Format(Format&&) = default; ///< Move constructor. | ||
| 35 | Format& operator=(Format&&) = default; ///< Move assignment. | ||
| 36 | |||
| 37 | /// Gets the data type of data format. | ||
| 38 | /// | ||
| 39 | /// Only @ref PlainFormat supports this method. | ||
| 40 | ✗ | [[nodiscard]] virtual DataType dtype() const { | |
| 41 | ✗ | KAI_TEST_ERROR("Not supported."); | |
| 42 | ✗ | } | |
| 43 | |||
| 44 | /// Calculates the offset in bytes to locate data of this format in the memory. | ||
| 45 | /// | ||
| 46 | /// @param[in] shape The size of the multidimensional data. | ||
| 47 | /// @param[in] indices The coordinate to the data element. | ||
| 48 | /// | ||
| 49 | /// @return The offset in bytes. | ||
| 50 | [[nodiscard]] virtual size_t compute_offset(Span<const size_t> shape, Span<const size_t> indices) const = 0; | ||
| 51 | |||
| 52 | /// Calculates the size in bytes of a data buffer of this format with the specified shape. | ||
| 53 | /// | ||
| 54 | /// @param[in] shape The size of the multidimensional data. | ||
| 55 | /// | ||
| 56 | /// @return The size in bytes. | ||
| 57 | [[nodiscard]] virtual size_t compute_size(Span<const size_t> shape) const = 0; | ||
| 58 | |||
| 59 | /// Generates random data with this format. | ||
| 60 | /// | ||
| 61 | /// @param[in] shape The size of the multidimensional data. | ||
| 62 | /// @param[in, out] rng The random number generator. | ||
| 63 | /// | ||
| 64 | /// @return The data buffer. | ||
| 65 | [[nodiscard]] virtual Buffer generate_random(Span<const size_t> shape, Rng& rng) const = 0; | ||
| 66 | |||
| 67 | /// Packs the data with this format. | ||
| 68 | /// | ||
| 69 | /// Depending on the actual format, the list of source data buffers can be different. | ||
| 70 | /// | ||
| 71 | /// @param[in] buffers The list of source data buffers. | ||
| 72 | /// | ||
| 73 | /// @return The packed data buffer. | ||
| 74 | [[nodiscard]] virtual Buffer pack(Span<const size_t> shape, Span<const Span<const std::byte>> buffers) const = 0; | ||
| 75 | |||
| 76 | /// Compares a portion of two data buffers with this format. | ||
| 77 | /// | ||
| 78 | /// The data inside the tile of interests of the two buffers are compared. | ||
| 79 | /// The data in the buffer under test that is outside the tile of intersts must be 0. | ||
| 80 | /// | ||
| 81 | /// @param[in] shape The size of the multidimensional data. | ||
| 82 | /// @param[in] tile_coords The starting coordinate of the tile to be compared. | ||
| 83 | /// @param[in] tile_shape The size of the tile to be compared. | ||
| 84 | /// @param[in] imp_buffer The data buffer under test. | ||
| 85 | /// @param[in] ref_buffer The reference data buffer. | ||
| 86 | /// @param[in] handler The mismatch handler. | ||
| 87 | /// | ||
| 88 | /// @return `true` if the two data buffers are considered matched. | ||
| 89 | [[nodiscard]] virtual bool compare( | ||
| 90 | Span<const size_t> shape, Span<const size_t> tile_coords, Span<const size_t> tile_shape, | ||
| 91 | Span<const std::byte> imp_buffer, Span<const std::byte> ref_buffer, MismatchHandler& handler) const = 0; | ||
| 92 | |||
| 93 | /// Prints the content of the data buffer with this format to the output stream. | ||
| 94 | /// | ||
| 95 | /// @param[in] os The output stream to write to. | ||
| 96 | /// @param[in] shape The size of the multidimensional data. | ||
| 97 | /// @param[in] data The data buffer. | ||
| 98 | virtual void print(std::ostream& os, Span<const size_t> shape, Span<const std::byte> data) const = 0; | ||
| 99 | |||
| 100 | /// Equal operator. | ||
| 101 | [[nodiscard]] virtual bool operator==(const Format& other) const = 0; | ||
| 102 | |||
| 103 | /// Not equal operator. | ||
| 104 | [[nodiscard]] bool operator!=(const Format& other) const { | ||
| 105 | return !(*this == other); | ||
| 106 | } | ||
| 107 | }; | ||
| 108 | |||
| 109 | } // namespace kai::test | ||
| 110 |