| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024-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/common/printer.hpp" |
| 8 | |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 | #include <ostream> |
| 12 | #include <string_view> |
| 13 | |
| 14 | #include "kai/kai_common.h" |
| 15 | #include "test/common/bfloat16.hpp" |
| 16 | #include "test/common/data_format.hpp" |
| 17 | #include "test/common/data_type.hpp" |
| 18 | #include "test/common/float16.hpp" |
| 19 | #include "test/common/int2.hpp" |
| 20 | #include "test/common/int4.hpp" |
| 21 | |
| 22 | namespace kai::test { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | inline void print_data(std::ostream& os, const uint8_t* data, size_t len, DataType data_type) { |
| 27 | if (data_type == DataType::QSU4) { |
| 28 | for (size_t i = 0; i < len / 2; ++i) { |
| 29 | const auto [low, high] = UInt4::unpack_u8(data[i]); |
| 30 | os << static_cast<int32_t>(low) << ", " << static_cast<int32_t>(high) << ", "; |
| 31 | } |
| 32 | } else if (data_type == DataType::QSI4 || data_type == DataType::QAI4) { |
| 33 | for (size_t i = 0; i < len / 2; ++i) { |
| 34 | const auto [low, high] = Int4::unpack_u8(data[i]); |
| 35 | os << static_cast<int32_t>(low) << ", " << static_cast<int32_t>(high) << ", "; |
| 36 | } |
| 37 | } else if (data_type == DataType::QSI2) { |
| 38 | for (size_t i = 0; i < len / 4; ++i) { |
| 39 | const auto [v1, v2, v3, v4] = Int2::unpack_u8(data[i]); |
| 40 | os << static_cast<int32_t>(v1) << ", " << static_cast<int32_t>(v2) << ", " << static_cast<int32_t>(v3) |
| 41 | << ", " << static_cast<int32_t>(v4) << ", "; |
| 42 | } |
| 43 | } else { |
| 44 | for (size_t i = 0; i < len; ++i) { |
| 45 | switch (data_type) { |
| 46 | case DataType::FP32: |
| 47 | os << reinterpret_cast<const float*>(data)[i]; |
| 48 | break; |
| 49 | |
| 50 | case DataType::FP16: |
| 51 | os << reinterpret_cast<const Float16*>(data)[i]; |
| 52 | break; |
| 53 | |
| 54 | case DataType::BF16: |
| 55 | os << reinterpret_cast<const BFloat16<>*>(data)[i]; |
| 56 | break; |
| 57 | |
| 58 | case DataType::I32: |
| 59 | os << reinterpret_cast<const int32_t*>(data)[i]; |
| 60 | break; |
| 61 | |
| 62 | case DataType::QAI8: |
| 63 | case DataType::QSI8: |
| 64 | os << static_cast<int32_t>(reinterpret_cast<const int8_t*>(data)[i]); |
| 65 | break; |
| 66 | |
| 67 | default: |
| 68 | KAI_ERROR("Unsupported data type!"); |
| 69 | } |
| 70 | |
| 71 | os << ", "; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void print_matrix_raw(std::ostream& os, const uint8_t* data, const DataFormat& format, size_t height, size_t width) { |
| 77 | const auto data_type = format.data_type(); |
| 78 | const auto esize_bits = data_type_size_in_bits(data_type); |
| 79 | const auto block_height = format.actual_block_height(height); |
| 80 | const auto block_width = format.actual_block_width(width); |
| 81 | const auto subblock_height = format.actual_subblock_height(height); |
| 82 | const auto subblock_width = format.actual_subblock_width(width); |
| 83 | |
| 84 | os << "[\n"; |
| 85 | for (size_t y_block = 0; y_block < height; y_block += block_height) { |
| 86 | if (block_height != height) { |
| 87 | os << " [\n"; |
| 88 | } |
| 89 | |
| 90 | for (size_t x_block = 0; x_block < width; x_block += block_width) { |
| 91 | if (block_width != width) { |
| 92 | os << " [\n"; |
| 93 | } |
| 94 | |
| 95 | for (size_t y_subblock = 0; y_subblock < block_height; y_subblock += subblock_height) { |
| 96 | if (subblock_height != block_height) { |
| 97 | os << " [\n"; |
| 98 | } |
| 99 | |
| 100 | for (size_t x_subblock = 0; x_subblock < block_width; x_subblock += subblock_width) { |
| 101 | if (subblock_width != block_width) { |
| 102 | os << " [\n"; |
| 103 | } |
| 104 | |
| 105 | for (size_t y = 0; y < subblock_height; ++y) { |
| 106 | os << " ["; |
| 107 | print_data(os, data, subblock_width, data_type); |
| 108 | data += subblock_width * esize_bits / 8; |
| 109 | os << "],\n"; |
| 110 | } |
| 111 | |
| 112 | if (subblock_width != block_width) { |
| 113 | os << " ]\n"; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (subblock_height != block_height) { |
| 118 | os << " ]\n"; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (block_width != width) { |
| 123 | os << " ],\n"; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (block_height != height) { |
| 128 | os << " ],\n"; |
| 129 | } |
| 130 | } |
| 131 | os << "]\n"; |
| 132 | } |
| 133 | |
| 134 | void print_matrix_per_row( |
| 135 | std::ostream& os, const uint8_t* data, const DataFormat& format, size_t height, size_t width) { |
| 136 | const auto has_scale = format.pack_format() == DataFormat::PackFormat::QUANTIZE_PER_ROW; |
| 137 | |
| 138 | const auto block_height = format.actual_block_height(height); |
| 139 | |
| 140 | const auto num_blocks = (height + block_height - 1) / block_height; |
| 141 | |
| 142 | KAI_ASSUME_ALWAYS(format.default_size_in_bytes(height, width) % num_blocks == 0); |
| 143 | const auto block_data_bytes = block_height * width * data_type_size_in_bits(format.data_type()) / 8; |
| 144 | const auto block_offsets_bytes = block_height * data_type_size_in_bits(format.zero_point_data_type()) / 8; |
| 145 | const auto block_scales_bytes = has_scale ? block_height * data_type_size_in_bits(format.scale_data_type()) / 8 : 0; |
| 146 | |
| 147 | os << "[\n"; |
| 148 | for (size_t y = 0; y < num_blocks; ++y) { |
| 149 | os << " {\"offsets\": ["; |
| 150 | print_data(os, data, block_height, format.zero_point_data_type()); |
| 151 | os << "], \"data\": ["; |
| 152 | print_data(os, data + block_offsets_bytes, block_height * width, format.data_type()); |
| 153 | |
| 154 | if (has_scale) { |
| 155 | os << "], \"scales\": ["; |
| 156 | print_data(os, data + block_offsets_bytes + block_data_bytes, block_height, format.scale_data_type()); |
| 157 | } |
| 158 | |
| 159 | os << "]},\n"; |
| 160 | |
| 161 | data += block_offsets_bytes + block_data_bytes + block_scales_bytes; |
| 162 | } |
| 163 | os << "]\n"; |
| 164 | } |
| 165 | |
| 166 | } // namespace |
| 167 | |
| 168 | void print_matrix( |
| 169 | std::ostream& os, std::string_view name, const void* data, const DataFormat& format, size_t height, size_t width) { |
| 170 | os << name << " = "; |
| 171 | |
| 172 | switch (format.pack_format()) { |
| 173 | case DataFormat::PackFormat::NONE: |
| 174 | print_matrix_raw(os, reinterpret_cast<const uint8_t*>(data), format, height, width); |
| 175 | break; |
| 176 | |
| 177 | case DataFormat::PackFormat::BIAS_PER_ROW: |
| 178 | case DataFormat::PackFormat::QUANTIZE_PER_ROW: |
| 179 | print_matrix_per_row(os, reinterpret_cast<const uint8_t*>(data), format, height, width); |
| 180 | break; |
| 181 | |
| 182 | default: |
| 183 | KAI_ERROR("Unsupported quantization packing format!"); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } // namespace kai::test |