Elffuss BraidHub

kikocis/kleidiai public

main · 5.2 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 <string>
11#include <vector>
12
13#include "test/common/assert.hpp"
14#include "test/common/data_type.hpp"
15#include "test/nextgen/common/random.hpp"
16#include "test/nextgen/common/shape.hpp"
17#include "test/nextgen/format/format.hpp"
18
19namespace kai::test {
20
21/// 2D blocked data with optional per-row values.
22///
23/// Example:
24/// Shape: (5, 8)
25/// Block size: (2, 3)
26/// Prefix per-row data 0:
27/// a0 a1 a2 a3 a4
28/// Prefix per-row data 1:
29/// b0 b1 b2 b3 b4
30/// Data:
31/// v00 v01 v02 v03 v04 v05 v06 v07
32/// v10 v11 v12 v13 v14 v15 v16 v17
33/// v20 v21 v22 v23 v24 v25 v26 v27
34/// v30 v31 v32 v33 v34 v35 v36 v37
35/// v40 v41 v42 v43 v44 v45 v46 v47
36/// Postfix per-row data 0:
37/// c0 c1 c2 c3 c4
38/// Postfix per-row data 1:
39/// d0 d1 d2 d3 d4
40///
41/// Combined blocked data with per-row data:
42/// +----+----+-------------+--------------+------------+----+----+
43/// | a0 | b0 | v00 v01 v02 | v03 v04 v05 | v06 v07 ___ | c0 | d0 |
44/// | a1 | b1 | v10 v11 v12 | v13 v14 v15 | v16 v17 ___ | c1 | d1 |
45/// +----+----+-------------+--------------+------------+----+----+
46/// | a2 | b2 | v20 v21 v22 | v23 v24 v25 | v26 v27 ___ | c2 | d2 |
47/// | a3 | b3 | v30 v31 v32 | v33 v34 v35 | v36 v37 ___ | c3 | d3 |
48/// +----+----+-------------+--------------+------------+----+----+
49/// | a4 | b4 | v40 v41 v42 | v43 v44 v45 | v46 v47 ___ | c4 | d4 |
50/// | __ | __ | ___ ___ ___ | ___ ___ ___ | ___ ___ ___ | __ | __ |
51/// +----+----+-------------+--------------+------------+----+----+
52///
53/// Packed data stream:
54/// +-------+-------+-------------------------+-------------------------+-------------------------+-------+-------+
55/// | a0 a1 | b0 b1 | v00 v01 v02 v10 v11 v12 | v03 v04 v05 v13 v14 v15 | v06 v07 0 v16 v17 0 | c0 c1 | d0 d1 |
56/// +-------+-------+-------------------------+-------------------------+-------------------------+-------+-------+
57/// | a2 a3 | b2 b3 | v20 v21 v22 v30 v31 v32 | v23 v24 v25 v33 v34 v35 | v26 v27 0 v36 v37 0 | c2 c3 | d2 d3 |
58/// +-------+-------+-------------------------+-------------------------+-------------------------+-------+-------+
59/// | a4 0 | b4 0 | v40 v41 v42 0 0 0 | v43 v44 v45 0 0 0 | v46 v47 0 0 0 0 | c4 0 | d4 0 |
60/// +-------+-------+-------------------------+-------------------------+-------------------------+-------+-------+
61class Block2dRowFormat : public Format {
62public:
63 /// Creates a 2D blocked data with optional per-row values.
64 ///
65 /// @param[in] block_height The block height.
66 /// @param[in] block_width The block width.
67 /// @param[in] width_align The input data is padded so that the width is multiple of this value
68 /// before the data is packed. This value must be divisible by block width.
69 /// @param[in] pad_right_same Right padding with the last element instead of 0.
70 /// @param[in] dtype The data type.
71 /// @param[in] pre_dtypes The data type of each prefix per-row component.
72 /// @param[in] post_dtypes The data type of each postfix per-row component.
73 Block2dRowFormat(
74 size_t block_height, size_t block_width, size_t width_align, bool pad_right_same, DataType dtype,
75 Span<const DataType> pre_dtypes, Span<const DataType> post_dtypes) :
76 m_block_height(block_height),
77 m_block_width(block_width),
78 m_width_align(width_align),
79 m_pad_right_same(pad_right_same),
80 m_dtype(dtype),
81 m_pre_dtypes(pre_dtypes.begin(), pre_dtypes.end()),
82 m_post_dtypes(post_dtypes.begin(), post_dtypes.end()) {
83 KAI_TEST_ASSERT(width_align % block_width == 0);
84 KAI_TEST_ASSERT(block_height * block_width * data_type_size_in_bits(dtype) % 8 == 0);
85
86 for (const DataType pre_dtype : pre_dtypes) {
87 KAI_TEST_ASSERT(data_type_size_in_bits(pre_dtype) % 8 == 0);
88 }
89
90 for (const DataType post_dtype : post_dtypes) {
91 KAI_TEST_ASSERT(data_type_size_in_bits(post_dtype) % 8 == 0);
92 }
93 }
94
95 [[nodiscard]] std::string uid() const override;
96 [[nodiscard]] size_t compute_offset(Shape shape, Span<const size_t> indices) const override;
97 [[nodiscard]] size_t compute_size(Shape shape) const override;
98 [[nodiscard]] Buffer generate(Shape shape, const GeneratorFn& generator) const override;
99 [[nodiscard]] Buffer pack(Shape shape, Span<const Span<const std::byte>> buffers) const override;
100 [[nodiscard]] bool compare(
101 Shape shape, Span<const size_t> tile_coords, Shape tile_shape, Span<const std::byte> imp_buffer,
102 Span<const std::byte> ref_buffer, MismatchHandler& handler) const override;
103 void print(std::ostream& os, Shape shape, Span<const std::byte> data) const override;
104 [[nodiscard]] bool operator==(const Format& other) const override;
105
106private:
107 size_t m_block_height;
108 size_t m_block_width;
109 size_t m_width_align;
110 bool m_pad_right_same;
111 DataType m_dtype;
112 std::vector<DataType> m_pre_dtypes;
113 std::vector<DataType> m_post_dtypes;
114};
115
116} // namespace kai::test