Elffuss BraidHub

kikocis/kleidiai public

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