Elffuss BraidHub

kikocis/kleidiai public

main · 3.8 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#include "test/nextgen/reference/compare.hpp"
8
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <functional>
13#include <iostream>
14#include <ostream>
15
16#include "test/common/assert.hpp"
17#include "test/common/data_type.hpp"
18#include "test/common/float16.hpp"
19#include "test/common/int4.hpp"
20#include "test/common/memory.hpp"
21#include "test/common/span.hpp"
22#include "test/common/type_traits.hpp"
23
24namespace kai::test {
25
26namespace {
27
28/// Calculates the absolute and relative errors.
29///
30/// @param[in] imp Value under test.
31/// @param[in] ref Reference value.
32///
33/// @return The absolute error and relative error.
34template <typename T>
35std::tuple<float, float> calculate_error(T imp, T ref) {
36 const auto imp_f = static_cast<float>(imp);
37 const auto ref_f = static_cast<float>(ref);
38
39 const auto abs_error = std::abs(imp_f - ref_f);
40 const auto rel_error = ref_f != 0 ? abs_error / std::abs(ref_f) : 0.0F;
41
42 return {abs_error, rel_error};
43}
44
45template <typename T>
46size_t compare_plain_2d(
47 Shape shape, Span<const size_t> tile_coords, Shape tile_shape, Span<const std::byte> imp_buffer,
48 Span<const std::byte> ref_buffer, const std::function<void(std::ostream& os, Span<const size_t> coords)>& report_fn,
49 MismatchHandler& handler) {
50 const size_t height = shape.at(0);
51 const size_t width = shape.at(1);
52
53 const size_t start_row = tile_coords.at(0);
54 const size_t start_col = tile_coords.at(1);
55
56 const size_t tile_height = tile_shape.at(0);
57 const size_t tile_width = tile_shape.at(1);
58
59 const size_t end_row = start_row + tile_height;
60 const size_t end_col = start_col + tile_width;
61
62 for (size_t row = 0; row < height; ++row) {
63 for (size_t col = 0; col < width; ++col) {
64 const bool in_tile = row >= start_row && row < end_row && col >= start_col && col < end_col;
65 const size_t index = row * width + col;
66
67 const T imp_value = read_array<T>(imp_buffer, index);
68 const T ref_value = in_tile ? read_array<T>(ref_buffer, index) : static_cast<T>(0);
69
70 const auto [abs_err, rel_err] = calculate_error<T>(imp_value, ref_value);
71
72 if (abs_err != 0 || rel_err != 0) {
73 // If the mismatch happens outside the tile, it's an error straightaway
74 // since these are expected to be 0 and the kernel is likely to write out-of-bound.
75 // If the mismatch happens inside the tile, the mismatch handler makes the decision
76 // based on the absolute error and relative error.
77
78 if (!in_tile) {
79 handler.mark_as_failed();
80 }
81
82 const auto notifying = !in_tile || handler.handle_data(abs_err, rel_err);
83
84 if (notifying) {
85 report_fn(std::cerr, std::array{row, col});
86 std::cerr << ": actual = " << displayable(imp_value) << ", expected = " << displayable(ref_value)
87 << "\n";
88 }
89 }
90 }
91 }
92
93 return tile_height * tile_width;
94}
95
96} // namespace
97
98CompareFn make_compare_plain_2d(DataType dtype) {
99 switch (dtype) {
100 case DataType::FP32:
101 return compare_plain_2d<float>;
102
103 case DataType::FP16:
104 return compare_plain_2d<Float16>;
105
106 case DataType::I32:
107 return compare_plain_2d<int32_t>;
108
109 case DataType::I8:
110 return compare_plain_2d<int8_t>;
111
112 case DataType::U8:
113 return compare_plain_2d<uint8_t>;
114
115 case DataType::I4:
116 return compare_plain_2d<Int4>;
117
118 default:
119 KAI_TEST_ERROR("Not implemented.");
120 }
121}
122
123} // namespace kai::test