| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include "test/common/compare.hpp" |
| 8 | |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 | #include <cstdlib> |
| 12 | #include <sstream> |
| 13 | #include <tuple> |
| 14 | #include <type_traits> |
| 15 | |
| 16 | #include "kai/kai_common.h" |
| 17 | #include "test/common/bfloat16.hpp" |
| 18 | #include "test/common/data_format.hpp" |
| 19 | #include "test/common/data_type.hpp" |
| 20 | #include "test/common/float16.hpp" |
| 21 | #include "test/common/int4.hpp" |
| 22 | #include "test/common/logging.hpp" |
| 23 | #include "test/common/memory.hpp" |
| 24 | #include "test/common/printer.hpp" |
| 25 | #include "test/common/rect.hpp" |
| 26 | #include "test/common/round.hpp" |
| 27 | |
| 28 | namespace kai::test { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | /// Calculates the absolute and relative errors. |
| 33 | /// |
| 34 | /// @param[in] imp Value under test. |
| 35 | /// @param[in] ref Reference value. |
| 36 | /// |
| 37 | /// @return The absolute error and relative error. |
| 38 | template <typename T> |
| 39 | std::tuple<float, float> calculate_error(T imp, T ref) { |
| 40 | const auto imp_f = static_cast<float>(imp); |
| 41 | const auto ref_f = static_cast<float>(ref); |
| 42 | |
| 43 | const auto abs_error = std::abs(imp_f - ref_f); |
| 44 | const auto rel_error = ref_f != 0 ? abs_error / std::abs(ref_f) : 0.0F; |
| 45 | |
| 46 | return {abs_error, rel_error}; |
| 47 | } |
| 48 | |
| 49 | /// Compares matrices with per-row quantization. |
| 50 | template <typename Data> |
| 51 | bool compare_raw( |
| 52 | const void* imp_data, const void* ref_data, const DataFormat& format, size_t full_height, size_t full_width, |
| 53 | const Rect& rect, MismatchHandler& handler) { |
| 54 | const size_t block_height = format.actual_block_height(full_height); |
| 55 | const size_t block_width = format.actual_block_width(full_width); |
| 56 | const size_t subblock_height = format.actual_subblock_height(full_height); |
| 57 | const size_t subblock_width = format.actual_subblock_width(full_width); |
| 58 | |
| 59 | size_t idx = 0; |
| 60 | |
| 61 | bool block_heading_written = false; |
| 62 | bool subblock_heading_written = false; |
| 63 | bool row_heading_written = false; |
| 64 | std::ostringstream sstream; |
| 65 | for (size_t y_block = 0; y_block < full_height; y_block += block_height) { |
| 66 | for (size_t x_block = 0; x_block < full_width; x_block += block_width) { |
| 67 | for (size_t y_subblock = 0; y_subblock < block_height; y_subblock += subblock_height) { |
| 68 | for (size_t x_subblock = 0; x_subblock < block_width; x_subblock += subblock_width) { |
| 69 | for (size_t y_element = 0; y_element < subblock_height; ++y_element) { |
| 70 | for (size_t x_element = 0; x_element < subblock_width; ++x_element) { |
| 71 | const auto y = y_block + y_subblock + y_element; |
| 72 | const auto x = x_block + x_subblock + x_element; |
| 73 | |
| 74 | const auto in_roi = rect.contains(y, x); |
| 75 | const auto imp_value = read_array<Data>(imp_data, idx); |
| 76 | const auto ref_value = in_roi ? read_array<Data>(ref_data, idx) : static_cast<Data>(0); |
| 77 | |
| 78 | const auto [abs_err, rel_err] = calculate_error(imp_value, ref_value); |
| 79 | |
| 80 | if (abs_err != 0 || rel_err != 0) { |
| 81 | if (!in_roi) { |
| 82 | handler.mark_as_failed(); |
| 83 | } |
| 84 | |
| 85 | const auto notifying = !in_roi || handler.handle_data(abs_err, rel_err); |
| 86 | |
| 87 | if (notifying) { |
| 88 | if (!block_heading_written) { |
| 89 | sstream << "block @ (" << y_block << ", " << x_block << "):\n"; |
| 90 | block_heading_written = true; |
| 91 | } |
| 92 | if (!subblock_heading_written) { |
| 93 | sstream << " sub-block @ (" << y_subblock << ", " << x_subblock << "):\n"; |
| 94 | subblock_heading_written = true; |
| 95 | } |
| 96 | if (!row_heading_written) { |
| 97 | sstream << " row=" << y_element << ": "; |
| 98 | row_heading_written = true; |
| 99 | } |
| 100 | sstream << x_element << ", "; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | ++idx; |
| 105 | } |
| 106 | if (row_heading_written) { |
| 107 | sstream << "\n"; |
| 108 | } |
| 109 | row_heading_written = false; |
| 110 | } |
| 111 | subblock_heading_written = false; |
| 112 | } |
| 113 | } |
| 114 | block_heading_written = false; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const bool success = handler.success(full_height * full_width); |
| 119 | if (!success) { |
| 120 | KAI_LOGE("mismatches:\n", sstream.str()); |
| 121 | } |
| 122 | return success; |
| 123 | } |
| 124 | |
| 125 | /// Compares matrices with per-row bias or per-row quantization. |
| 126 | template <typename Data, typename Scale, typename Offset> |
| 127 | bool compare_per_row( |
| 128 | const void* imp_data, const void* ref_data, const DataFormat& format, size_t full_height, size_t full_width, |
| 129 | const Rect& rect, MismatchHandler& handler) { |
| 130 | static constexpr auto has_scale = !std::is_null_pointer_v<Scale>; |
| 131 | |
| 132 | const auto block_height = format.actual_block_height(full_height); |
| 133 | const auto block_width = format.actual_block_width(full_width); |
| 134 | const auto subblock_height = format.actual_subblock_height(full_height); |
| 135 | const auto subblock_width = format.actual_subblock_width(full_width); |
| 136 | |
| 137 | KAI_ASSUME_ALWAYS(format.scheduler_block_height(full_height) == block_height); |
| 138 | KAI_ASSUME_ALWAYS(format.scheduler_block_width(full_width) == full_width); |
| 139 | KAI_ASSUME_ALWAYS(rect.start_col() == 0); |
| 140 | KAI_ASSUME_ALWAYS(rect.width() == full_width); |
| 141 | |
| 142 | const size_t row_block_zero_points_bytes = block_height * sizeof(Offset); |
| 143 | const size_t row_block_scales_bytes = has_scale ? block_height * sizeof(Scale) : 0; |
| 144 | const size_t row_block_data_bytes = block_height * round_up_multiple(full_width, block_width) * sizeof(Data); |
| 145 | |
| 146 | const auto* imp_ptr = reinterpret_cast<const uint8_t*>(imp_data); |
| 147 | const auto* ref_ptr = reinterpret_cast<const uint8_t*>(ref_data); |
| 148 | |
| 149 | for (size_t y_block = 0; y_block < full_height; y_block += block_height) { |
| 150 | const auto in_roi = y_block >= rect.start_row() && y_block < rect.end_row(); |
| 151 | |
| 152 | // Checks the zero points. |
| 153 | for (size_t i = 0; i < block_height; ++i) { |
| 154 | const auto imp_zero_point = reinterpret_cast<const Offset*>(imp_ptr)[i]; |
| 155 | const Offset ref_zero_point = in_roi ? reinterpret_cast<const Offset*>(ref_ptr)[i] : static_cast<Offset>(0); |
| 156 | const auto [abs_err, rel_err] = calculate_error(imp_zero_point, ref_zero_point); |
| 157 | |
| 158 | if (abs_err != 0 || rel_err != 0) { |
| 159 | handler.mark_as_failed(); |
| 160 | |
| 161 | const auto raw_row = y_block + i; |
| 162 | KAI_LOGE( |
| 163 | "Mismatched zero point ", raw_row, ": actual = ", imp_zero_point, ", expected: ", ref_zero_point); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | imp_ptr += row_block_zero_points_bytes; |
| 168 | ref_ptr += row_block_zero_points_bytes; |
| 169 | |
| 170 | // Checks the data. |
| 171 | for (size_t x_block = 0; x_block < full_width; x_block += block_width) { |
| 172 | for (size_t y_subblock = 0; y_subblock < block_height; y_subblock += subblock_height) { |
| 173 | for (size_t x_subblock = 0; x_subblock < block_width; x_subblock += subblock_width) { |
| 174 | for (size_t y = 0; y < subblock_height; ++y) { |
| 175 | for (size_t x = 0; x < subblock_width; ++x) { |
| 176 | const auto offset = (y_subblock + y) * full_width + x_block + x_subblock + x; |
| 177 | const auto imp_value = read_array<Data>(imp_ptr, offset); |
| 178 | const Data ref_value = in_roi ? read_array<Data>(ref_ptr, offset) : static_cast<Data>(0); |
| 179 | const auto [abs_err, rel_err] = calculate_error(imp_value, ref_value); |
| 180 | |
| 181 | if (abs_err != 0 || rel_err != 0) { |
| 182 | if (!in_roi) { |
| 183 | handler.mark_as_failed(); |
| 184 | } |
| 185 | |
| 186 | const auto notifying = !in_roi || handler.handle_data(abs_err, rel_err); |
| 187 | |
| 188 | if (notifying) { |
| 189 | const auto raw_index = y_block * block_height * block_width + offset; |
| 190 | KAI_LOGE( |
| 191 | "Mismatched data ", raw_index, ": actual = ", imp_value, |
| 192 | ", expected: ", ref_value); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | imp_ptr += row_block_data_bytes; |
| 202 | ref_ptr += row_block_data_bytes; |
| 203 | |
| 204 | // Checks the scales (if exists). |
| 205 | if constexpr (has_scale) { |
| 206 | for (size_t i = 0; i < block_height; ++i) { |
| 207 | const auto imp_scale = reinterpret_cast<const Scale*>(imp_ptr)[i]; |
| 208 | const Scale ref_scale = in_roi ? reinterpret_cast<const Scale*>(ref_ptr)[i] : 0; |
| 209 | const auto [abs_err, rel_err] = calculate_error(imp_scale, ref_scale); |
| 210 | |
| 211 | if (abs_err != 0 || rel_err != 0) { |
| 212 | handler.mark_as_failed(); |
| 213 | |
| 214 | const auto raw_row = y_block + i; |
| 215 | KAI_LOGE( |
| 216 | "Mismatched quantization scale ", raw_row, ": actual = ", imp_scale, ", expected: ", ref_scale); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | imp_ptr += row_block_scales_bytes; |
| 221 | ref_ptr += row_block_scales_bytes; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return handler.success(rect.height() * full_width); |
| 226 | } |
| 227 | |
| 228 | } // namespace |
| 229 | |
| 230 | bool compare( |
| 231 | const void* imp_data, const void* ref_data, const DataFormat& format, size_t full_height, size_t full_width, |
| 232 | const Rect& rect, MismatchHandler& handler) { |
| 233 | const auto data_type = format.data_type(); |
| 234 | const auto scale_dt = format.scale_data_type(); |
| 235 | const auto offset_dt = format.zero_point_data_type(); |
| 236 | |
| 237 | switch (format.pack_format()) { |
| 238 | case DataFormat::PackFormat::NONE: |
| 239 | switch (data_type) { |
| 240 | case DataType::FP32: |
| 241 | return compare_raw<float>(imp_data, ref_data, format, full_height, full_width, rect, handler); |
| 242 | |
| 243 | case DataType::FP16: |
| 244 | return compare_raw<Float16>(imp_data, ref_data, format, full_height, full_width, rect, handler); |
| 245 | |
| 246 | case DataType::BF16: |
| 247 | return compare_raw<BFloat16<>>(imp_data, ref_data, format, full_height, full_width, rect, handler); |
| 248 | |
| 249 | default: |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | break; |
| 254 | |
| 255 | case DataFormat::PackFormat::BIAS_PER_ROW: |
| 256 | if (data_type == DataType::FP16 && offset_dt == DataType::FP16) { |
| 257 | return compare_per_row<Float16, std::nullptr_t, Float16>( |
| 258 | imp_data, ref_data, format, full_height, full_width, rect, handler); |
| 259 | } else if (data_type == DataType::FP32 && offset_dt == DataType::FP32) { |
| 260 | return compare_per_row<float, std::nullptr_t, float>( |
| 261 | imp_data, ref_data, format, full_height, full_width, rect, handler); |
| 262 | } else if (data_type == DataType::BF16 && offset_dt == DataType::FP32) { |
| 263 | return compare_per_row<BFloat16<>, std::nullptr_t, float>( |
| 264 | imp_data, ref_data, format, full_height, full_width, rect, handler); |
| 265 | } |
| 266 | |
| 267 | break; |
| 268 | |
| 269 | case DataFormat::PackFormat::QUANTIZE_PER_ROW: |
| 270 | if (data_type_is_quantized_int8(data_type) && scale_dt == DataType::FP32 && offset_dt == DataType::I32) { |
| 271 | return compare_per_row<int8_t, float, int32_t>( |
| 272 | imp_data, ref_data, format, full_height, full_width, rect, handler); |
| 273 | } else if ( |
| 274 | data_type_is_quantized_int4(data_type) && scale_dt == DataType::FP32 && offset_dt == DataType::I32) { |
| 275 | return compare_per_row<Int4, float, int32_t>( |
| 276 | imp_data, ref_data, format, full_height, full_width, rect, handler); |
| 277 | } |
| 278 | |
| 279 | break; |
| 280 | |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | KAI_ERROR("Unsupported format!"); |
| 286 | } |
| 287 | |
| 288 | // ===================================================================================================================== |
| 289 | |
| 290 | DefaultMismatchHandler::DefaultMismatchHandler( |
| 291 | float abs_error_threshold, float rel_error_threshold, size_t abs_mismatched_threshold, |
| 292 | float rel_mismatched_threshold) : |
| 293 | _abs_error_threshold(abs_error_threshold), |
| 294 | _rel_error_threshold(rel_error_threshold), |
| 295 | _abs_mismatched_threshold(abs_mismatched_threshold), |
| 296 | _rel_mismatched_threshold(rel_mismatched_threshold), |
| 297 | _num_mismatches(0), |
| 298 | _failed(false) { |
| 299 | } |
| 300 | |
| 301 | DefaultMismatchHandler::DefaultMismatchHandler(const DefaultMismatchHandler& rhs) : |
| 302 | _abs_error_threshold(rhs._abs_error_threshold), |
| 303 | _rel_error_threshold(rhs._rel_error_threshold), |
| 304 | _abs_mismatched_threshold(rhs._abs_mismatched_threshold), |
| 305 | _rel_mismatched_threshold(rhs._rel_mismatched_threshold), |
| 306 | _num_mismatches(0), |
| 307 | _failed(false) { |
| 308 | // Cannot copy mismatch handler that is already in use. |
| 309 | KAI_ASSUME_ALWAYS(rhs._num_mismatches == 0); |
| 310 | KAI_ASSUME_ALWAYS(!rhs._failed); |
| 311 | } |
| 312 | |
| 313 | DefaultMismatchHandler& DefaultMismatchHandler::operator=(const DefaultMismatchHandler& rhs) { |
| 314 | if (this != &rhs) { |
| 315 | // Cannot copy mismatch handler that is already in use. |
| 316 | KAI_ASSUME_ALWAYS(rhs._num_mismatches == 0); |
| 317 | KAI_ASSUME_ALWAYS(!rhs._failed); |
| 318 | |
| 319 | _abs_error_threshold = rhs._abs_error_threshold; |
| 320 | _rel_error_threshold = rhs._rel_error_threshold; |
| 321 | _abs_mismatched_threshold = rhs._abs_mismatched_threshold; |
| 322 | _rel_mismatched_threshold = rhs._rel_mismatched_threshold; |
| 323 | } |
| 324 | |
| 325 | return *this; |
| 326 | } |
| 327 | |
| 328 | bool DefaultMismatchHandler::handle_data(float absolute_error, float relative_error) { |
| 329 | const auto mismatched = absolute_error > _abs_error_threshold && relative_error > _rel_error_threshold; |
| 330 | |
| 331 | if (mismatched) { |
| 332 | ++_num_mismatches; |
| 333 | } |
| 334 | |
| 335 | return mismatched; |
| 336 | } |
| 337 | |
| 338 | void DefaultMismatchHandler::mark_as_failed() { |
| 339 | _failed = true; |
| 340 | } |
| 341 | |
| 342 | bool DefaultMismatchHandler::success(size_t num_checks) const { |
| 343 | if (_failed) { |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | const auto mismatched_rate = static_cast<float>(_num_mismatches) / static_cast<float>(num_checks); |
| 348 | return _num_mismatches <= _abs_mismatched_threshold || mismatched_rate <= _rel_mismatched_threshold; |
| 349 | } |
| 350 | |
| 351 | } // namespace kai::test |