Debian Codegraph @

examples/matmul_clamp_f32_qsi8d32p_qsi4c32p/matmul_clamp_f32_qsi8d32p_qsi4c32p.cpp

raw symbols SCIP
1 //
2 // SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <[email protected]>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 #if !defined(__ARM_FEATURE_DOTPROD) && !defined(__ARM_FEATURE_MATMUL_INT8) && !defined(__aarch64__)
7 #error "Dotprod and I8mm extensions required to compile this example"
8 #else
9 #include <cassert>
10 #include <cfloat>
11 #include <chrono>
12 #include <cmath>
13 #include <cstdint>
14 #include <cstdlib>
15 #include <iostream>
16 #include <limits>
17 #include <string>
18 #include <thread>
19 #include <vector>
20
21 // Include micro-kernel variants
22 #include "kai_lhs_quant_pack_qsi8d32p_f32.h"
23 #include "kai_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod.h"
24 #include "kai_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm.h"
25 #include "kai_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm.h"
26 #include "kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_interface.h"
27 #include "kai_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0.h"
28
29 #define INT4_MIN (-8)
30 #define INT4_MAX (7)
31
32 static size_t parse_thread_count_value(const std::string& value) {
33 char* end = nullptr;
34 const unsigned long parsed = std::strtoul(value.c_str(), &end, 10);
35 if (end == value.c_str() || *end != '\0') {
36 std::cerr << "Invalid thread count: '" << value << "'\n";
37 std::exit(EXIT_FAILURE);
38 }
39 if (parsed == 0 || parsed > std::numeric_limits<size_t>::max()) {
40 std::cerr << "Thread count must be in range [1, " << std::numeric_limits<size_t>::max() << "]\n";
41 std::exit(EXIT_FAILURE);
42 }
43 return static_cast<size_t>(parsed);
44 }
45
46 static void print_usage(const char* program_name) {
47 std::cerr << "Usage: " << program_name << " [--threads <count> | --threads=<count>]\n";
48 }
49
50 static size_t parse_thread_count(int argc, char** argv) {
51 size_t thread_count = 1;
52 for (int i = 1; i < argc; ++i) {
53 std::string arg(argv[i]);
54 if (arg == "--help" || arg == "-h") {
55 print_usage(argv[0]);
56 std::exit(EXIT_SUCCESS);
57 }
58
59 if (arg == "--threads" || arg == "-t") {
60 if ((i + 1) >= argc) {
61 std::cerr << "--threads expects a value\n";
62 print_usage(argv[0]);
63 std::exit(EXIT_FAILURE);
64 }
65 thread_count = parse_thread_count_value(argv[++i]);
66 continue;
67 }
68
69 const std::string prefix = "--threads=";
70 if (arg.rfind(prefix, 0) == 0) {
71 thread_count = parse_thread_count_value(arg.substr(prefix.size()));
72 continue;
73 }
74
75 std::cerr << "Unrecognized argument: " << arg << "\n";
76 print_usage(argv[0]);
77 std::exit(EXIT_FAILURE);
78 }
79 return thread_count;
80 }
81
82 // Micro-kernel interface
83 struct kai_matmul_ukernel_f32_qa8d32p_qs4c32p {
84 kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_ukernel ukernel;
85 std::string name = {};
86 };
87
88 kai_matmul_ukernel_f32_qa8d32p_qs4c32p ukernel_variants[] = {
89 {kai_get_m_step_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
90 kai_get_n_step_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
91 kai_get_mr_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
92 kai_get_nr_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
93 kai_get_kr_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
94 kai_get_sr_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
95 kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
96 kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
97 kai_get_dst_offset_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
98 kai_get_dst_size_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
99 kai_run_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
100 "matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod"},
101 {kai_get_m_step_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
102 kai_get_n_step_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
103 kai_get_mr_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
104 kai_get_nr_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
105 kai_get_kr_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
106 kai_get_sr_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
107 kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
108 kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
109 kai_get_dst_offset_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
110 kai_get_dst_size_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
111 kai_run_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
112 "matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm"},
113 {kai_get_m_step_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
114 kai_get_n_step_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
115 kai_get_mr_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
116 kai_get_nr_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
117 kai_get_kr_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
118 kai_get_sr_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
119 kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
120 kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
121 kai_get_dst_offset_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
122 kai_get_dst_size_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
123 kai_run_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm,
124 "matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm"},
125 };
126
127 // Number of micro-kernel variants stored in the array
128 const size_t num_ukernel_variants = sizeof(ukernel_variants) / sizeof(ukernel_variants[0]);
129
130 static void fill_uniform_random(size_t num_rows, size_t num_cols, float* dst, size_t seed) {
131 std::srand(seed);
132
133 // Fill the array with random values between -1 and 1
134 for (int i = 0; i < num_rows * num_cols; i++) {
135 dst[i] = (float)((double)std::rand() / RAND_MAX) * 2 - 1;
136 }
137 }
138
139 static inline size_t num_blocks_per_row(size_t k, size_t bl) {
140 return k / bl;
141 }
142
143 static inline size_t num_bytes_per_block_qs8c32(size_t bl) {
144 return bl + sizeof(int16_t);
145 }
146
147 static inline size_t num_bytes_per_block_qs4c32(size_t bl) {
148 return (bl / 2) + sizeof(int16_t);
149 }
150
151 static void quant_qs4c32_f32(size_t n, size_t k, size_t bl, const float* rhs_f32, uint8_t* rhs_qs4c32) {
152 const size_t num_blocks_row = num_blocks_per_row(k, bl);
153 const size_t num_bytes_block = num_bytes_per_block_qs4c32(bl);
154 const size_t dst_stride = num_blocks_row * num_bytes_block;
155
156 for (size_t row_idx = 0; row_idx < n; ++row_idx) {
157 const float* src_ptr = rhs_f32 + row_idx * k;
158
159 uint8_t* dst_ptr = (uint8_t*)rhs_qs4c32 + row_idx * dst_stride;
160
161 for (size_t block_idx = 0; block_idx < num_blocks_row; ++block_idx) {
162 float amax = 0.0f;
163 float max = 0.0f;
164
165 for (size_t b = 0; b < bl; ++b) {
166 const float src0_0 = src_ptr[block_idx * bl + b];
167 const float asrc0_0 = fabsf(src0_0);
168
169 if (amax < asrc0_0) {
170 amax = asrc0_0;
171 max = src0_0;
172 }
173 }
174
175 const float scale = max / -8.0;
176 const float recip_scale = scale ? 1.0f / scale : 0.0f;
177
178 // Store the scale at the beginning of the block
179 *((uint16_t*)dst_ptr) = kai_cast_f16_f32(scale);
180 dst_ptr += sizeof(uint16_t);
181
182 const size_t block_size = 32;
183 const size_t num_subblocks = bl / 32;
184
185 for (size_t subblock_idx = 0; subblock_idx < num_subblocks; ++subblock_idx) {
186 for (size_t i = 0; i < block_size / 2; ++i) {
187 const size_t src_base_addr = block_idx * bl + i + subblock_idx * block_size;
188 float v0_f32 = src_ptr[src_base_addr];
189 float v1_f32 = src_ptr[src_base_addr + block_size / 2];
190
191 v0_f32 *= recip_scale;
192 v1_f32 *= recip_scale;
193
194 const uint8_t v0_u8 = (uint8_t)std::min((int8_t)15, (int8_t)(v0_f32 + 8.5f));
195 const uint8_t v1_u8 = (uint8_t)std::min((int8_t)15, (int8_t)(v1_f32 + 8.5f));
196
197 const uint8_t rhs_v0 = (v1_u8 << 4) | v0_u8;
198
199 dst_ptr[0] = rhs_v0;
200 dst_ptr += sizeof(uint8_t);
201 }
202 }
203 }
204 }
205 };
206
207 static void ref_quant_qs8d32_f32(size_t n, size_t k, size_t bl, const float* rhs_f32, uint8_t* rhs_qs8c32) {
208 const size_t num_blocks_row = num_blocks_per_row(k, bl);
209 const size_t num_bytes_block = num_bytes_per_block_qs8c32(bl);
210 const size_t dst_stride = num_blocks_row * num_bytes_block;
211
212 for (size_t row_idx = 0; row_idx < n; ++row_idx) {
213 const float* src_ptr = rhs_f32 + row_idx * k;
214
215 int8_t* dst_ptr = (int8_t*)rhs_qs8c32 + row_idx * dst_stride;
216
217 for (size_t block_idx = 0; block_idx < num_blocks_row; ++block_idx) {
218 float amax = 0.0f;
219
220 for (size_t b = 0; b < bl; ++b) {
221 const float src0_0 = src_ptr[block_idx * bl + b];
222 const float asrc0_0 = fabsf(src0_0);
223
224 if (amax < asrc0_0) {
225 amax = asrc0_0;
226 }
227 }
228
229 const float scale = amax / ((1 << 7) - 1);
230 const float recip_scale = scale ? 1.0f / scale : 0.0f;
231
232 // Store the scale at the beginning of the block
233 *((uint16_t*)dst_ptr) = kai_cast_f16_f32(scale);
234 dst_ptr += sizeof(uint16_t);
235
236 const size_t block_size = 32;
237 const size_t num_subblocks = bl / 32;
238
239 for (size_t subblock_idx = 0; subblock_idx < num_subblocks; ++subblock_idx) {
240 for (size_t i = 0; i < block_size; ++i) {
241 const size_t src_base_addr = block_idx * bl + i + subblock_idx * block_size;
242 float v0_f32 = src_ptr[src_base_addr];
243
244 v0_f32 *= recip_scale;
245
246 dst_ptr[0] = roundf(v0_f32);
247 dst_ptr += sizeof(int8_t);
248 }
249 }
250 }
251 }
252 };
253
254 static void ref_matmul_f32_qs8d32_qs4c32(
255 size_t m, size_t n, size_t k, size_t bl, const int8_t* lhs_qa8d32, const uint8_t* rhs_qs4c32, float* dst_f32,
256 float scalar_min, float scalar_max) {
257 const size_t num_blocks_row = num_blocks_per_row(k, bl);
258 const size_t num_bytes_block_qs4c32 = num_bytes_per_block_qs4c32(bl);
259 const size_t num_bytes_block_qs8c32 = num_bytes_per_block_qs8c32(bl);
260
261 const size_t lhs_stride = num_blocks_row * num_bytes_block_qs8c32;
262 const size_t rhs_stride = num_blocks_row * num_bytes_block_qs4c32;
263
264 for (size_t row_idx = 0; row_idx < m; ++row_idx) {
265 const int8_t* lhs_ptr_start = lhs_qa8d32 + row_idx * lhs_stride;
266 for (size_t col_idx = 0; col_idx < n; ++col_idx) {
267 // Main f32 accumulator
268 float main_acc = 0.0f;
269
270 const size_t block_size = 32;
271 const size_t num_subblocks = bl / 32;
272
273 for (size_t block_idx = 0; block_idx < num_blocks_row; ++block_idx) {
274 const int8_t* lhs_ptr = lhs_ptr_start;
275 const uint8_t* rhs_ptr = rhs_qs4c32 + col_idx * rhs_stride;
276
277 lhs_ptr += block_idx * num_bytes_block_qs8c32;
278 rhs_ptr += block_idx * num_bytes_block_qs4c32;
279
280 for (size_t subblock_idx = 0; subblock_idx < num_subblocks; ++subblock_idx) {
281 int32_t temp_acc = 0;
282
283 // Get the LHS/RHS quantization scale stored at the
284 // beginning of each block
285 const float lhs_scale = kai_cast_f32_f16(*(const uint16_t*)lhs_ptr);
286 const float rhs_scale = kai_cast_f32_f16(*(const uint16_t*)rhs_ptr);
287
288 lhs_ptr += sizeof(uint16_t);
289 rhs_ptr += sizeof(uint16_t);
290
291 for (size_t i = 0; i < block_size / 2; ++i) {
292 // Get the LHS values
293 const int32_t lhs_v0 = (int32_t)lhs_ptr[0];
294 const int32_t lhs_v1 = (int32_t)lhs_ptr[block_size / 2];
295
296 // Get the RHS values
297 const uint8_t rhs_byte = rhs_ptr[0];
298
299 // Unpack the RHS values
300 const int32_t rhs_v0 = (((int32_t)(rhs_byte & 0x0F)) - 8);
301 const int32_t rhs_v1 = (((int32_t)(rhs_byte >> 4)) - 8);
302
303 temp_acc += lhs_v0 * rhs_v0;
304 temp_acc += lhs_v1 * rhs_v1;
305
306 lhs_ptr += 1;
307 rhs_ptr += 1;
308 }
309
310 main_acc += temp_acc * lhs_scale * rhs_scale;
311 }
312 }
313
314 main_acc = std::max(main_acc, scalar_min);
315 main_acc = std::min(main_acc, scalar_max);
316
317 dst_f32[0] = main_acc;
318 dst_f32 += 1;
319 }
320 }
321 };
322
323 static bool is_output_correct(size_t num_rows, size_t num_cols, float tolerance, const float* ref, const float* act) {
324 bool is_valid = true;
325
326 for (size_t i = 0; i < num_rows * num_cols; ++i) {
327 if (std::fabs(ref[i] - act[i]) > tolerance) {
328 const size_t x = i % num_cols;
329 const size_t y = i / num_cols;
330 printf("ERROR![%ld][%ld]: ref=%.5f vs. act=%.5f\n", y, x, ref[i], act[i]);
331 is_valid = false;
332 }
333 }
334 return is_valid;
335 }
336
337 int main(int argc, char** argv) {
338 const size_t num_threads = parse_thread_count(argc, argv);
339 std::cout << "Using " << num_threads << " thread(s) for computations." << std::endl;
340
341 const size_t bl = 32; // Block length. It must be 32
342 const size_t m = 71;
343 const size_t n = 63;
344 const size_t k = 128;
345 const size_t seed_lhs = 4568;
346 const size_t seed_rhs = seed_lhs + 4;
347
348 KAI_ASSERT_MSG((k % bl) == 0, "K must be a multiple of block length");
349
350 const size_t num_blocks = k / bl;
351 const size_t num_bytes_per_block_qs4c32 = (bl / 2) + sizeof(int16_t);
352 const size_t num_bytes_per_block_qs8c32 = bl + sizeof(int16_t);
353
354 const size_t lhs_native_size_f32 = m * k * sizeof(float);
355 const size_t rhs_native_size_f32 = n * k * sizeof(float);
356 const size_t rhs_native_size_qs4c32 = n * num_blocks * num_bytes_per_block_qs4c32;
357
358 // Allocate the memory
359 uint8_t* lhs_native_mtx_f32 = new uint8_t[lhs_native_size_f32];
360 uint8_t* rhs_native_mtx_f32 = new uint8_t[rhs_native_size_f32];
361 uint8_t* rhs_native_mtx_qs4c32 = new uint8_t[rhs_native_size_qs4c32];
362
363 fill_uniform_random(m, k, (float*)lhs_native_mtx_f32, seed_lhs);
364 fill_uniform_random(n, k, (float*)rhs_native_mtx_f32, seed_rhs);
365
366 quant_qs4c32_f32(n, k, bl, (const float*)rhs_native_mtx_f32, (uint8_t*)rhs_native_mtx_qs4c32);
367
368 delete[] rhs_native_mtx_f32;
369
370 //----------- REFERENCE IMPLEMENTATION
371 //------------------------------------
372 //------------------------------------
373 // Memory sizes for the reference implementation
374 // After dynamically quantized the LHS matrix, each row is quantized per block of 32 values.
375 // Each block has a scale factor in f16 format.
376 // The scale factor is stored at the beginning of each block
377 const size_t lhs_ref_size_qa8d32 = m * num_blocks * num_bytes_per_block_qs8c32;
378 const size_t dst_ref_size_f32 = m * n * sizeof(float);
379
380 uint8_t* lhs_ref_mtx_qa8d32 = new uint8_t[lhs_ref_size_qa8d32];
381 uint8_t* dst_ref_mtx_f32 = new uint8_t[dst_ref_size_f32];
382
383 ref_quant_qs8d32_f32(m, k, bl, (const float*)lhs_native_mtx_f32, (uint8_t*)lhs_ref_mtx_qa8d32);
384
385 ref_matmul_f32_qs8d32_qs4c32(
386 m, n, k, bl, (const int8_t*)lhs_ref_mtx_qa8d32, (const uint8_t*)rhs_native_mtx_qs4c32, (float*)dst_ref_mtx_f32,
387 -FLT_MAX, FLT_MAX);
388
389 // Remove the unnecessary buffer
390 delete[] lhs_ref_mtx_qa8d32;
391
392 //----------- END REFERENCE IMPLEMENTATION
393 //------------------------------------
394 //------------------------------------
395
396 //----------- MICRO-KERNELS TESTS
397 //------------------------------------
398 //------------------------------------
399 for (size_t idx_variant = 0; idx_variant < num_ukernel_variants; ++idx_variant) {
400 std::cout << "Testing " << ukernel_variants[idx_variant].name << std::endl;
401
402 // Get the packing parameters
403 const size_t mr = ukernel_variants[idx_variant].ukernel.get_mr();
404 const size_t nr = ukernel_variants[idx_variant].ukernel.get_nr();
405 const size_t kr = ukernel_variants[idx_variant].ukernel.get_kr();
406 const size_t sr = ukernel_variants[idx_variant].ukernel.get_sr();
407
408 // Get the size in bytes for the packed matrices
409 const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32p_f32(m, k, bl, mr, kr, sr);
410 const size_t rhs_packed_size =
411 kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0(n, k, nr, kr, bl);
412 const size_t dst_size = ukernel_variants[idx_variant].ukernel.get_dst_size(m, n);
413
414 // Allocate the matrices
415 uint8_t* lhs_packed_mtx_qs8d32 = new uint8_t[lhs_packed_size];
416 uint8_t* rhs_packed_mtx_qs4c32 = new uint8_t[rhs_packed_size];
417 uint8_t* dst_act_mtx_f32 = new uint8_t[dst_size];
418
419 // If the RHS matrix contains constant values, the packing can be performed
420 // only once
421 struct kai_rhs_pack_qs4cxs1s0_param params;
422 params.lhs_zero_point = 1;
423 params.rhs_zero_point = 8;
424
425 // RHS packing. RHS is usually constant and packed only once, e.g. at model loading time.
426 kai_run_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0(
427 1, n, k, // Dimensions
428 nr, kr, sr, // Packing arguments
429 bl, // Block length
430 (const uint8_t*)(rhs_native_mtx_qs4c32), // RHS
431 NULL, // Bias
432 rhs_packed_mtx_qs4c32, // RHS packed
433 0, &params);
434
435 // Worker thread function. Multithreads both LHS packing and matmul execution.
436 auto thread_worker = [&](int thread_index) {
437 // Each thread processes m_to_process number of rows. Note that, due to the format block size, each
438 // thread must process an integer multiple of m_step number of rows.
439 const size_t m_step = ukernel_variants[idx_variant].ukernel.get_m_step();
440 const size_t num_m_per_thread = kai_roundup(m, m_step * num_threads) / num_threads;
441 const size_t m_start = thread_index * num_m_per_thread;
442
443 // For small shapes and m_step > 1, there may not be enough parallelism to put all threads to work
444 if (m_start < m) {
445 size_t m_to_process = num_m_per_thread;
446 if (m_start + m_to_process > m) {
447 m_to_process = m - m_start;
448 }
449
450 // LHS packing
451 const float* src_ptr = (float*)lhs_native_mtx_f32 +
452 kai_get_lhs_offset_lhs_quant_pack_qsi8d32p_f32(m_start, k * sizeof(float)) / sizeof(float);
453 const size_t lhs_packed_offset =
454 ukernel_variants[idx_variant].ukernel.get_lhs_packed_offset(m_start, k, bl);
455 void* lhs_packed_ptr = lhs_packed_mtx_qs8d32 + lhs_packed_offset;
456
457 kai_run_lhs_quant_pack_qsi8d32p_f32(
458 m_to_process, k, bl, // Dimensions
459 mr, kr, sr, 0, // Packing arguments
460 src_ptr, // LHS
461 k * sizeof(float), // LHS stride
462 lhs_packed_ptr); // LHS packed
463
464 // Matmul micro-kernel
465 const size_t dst_stride = n * sizeof(float);
466 const size_t rhs_packed_offset = ukernel_variants[idx_variant].ukernel.get_rhs_packed_offset(0, k, bl);
467 const void* rhs_packed_ptr = rhs_packed_mtx_qs4c32 + rhs_packed_offset;
468 const size_t dst_offset = ukernel_variants[idx_variant].ukernel.get_dst_offset(m_start, 0, dst_stride);
469 float* dst_ptr = (float*)(dst_act_mtx_f32 + dst_offset);
470
471 ukernel_variants[idx_variant].ukernel.run_matmul(
472 m_to_process, n, k, bl, // Dimensions
473 lhs_packed_ptr, // LHS packed
474 rhs_packed_ptr, // RHS packed
475 dst_ptr, // DST
476 dst_stride, // DST stride (row)
477 sizeof(float), // DST stride (col)
478 -FLT_MAX, FLT_MAX // Min and max for the clamp operation
479 );
480 }
481 };
482
483 const auto time_s = std::chrono::high_resolution_clock::now();
484
485 std::vector<std::thread> threads;
486 threads.reserve(num_threads);
487
488 // Create worker threads and execute the operator
489 for (int i = 0; i < num_threads; ++i) {
490 threads.emplace_back(thread_worker, i);
491 }
492
493 // Wait until all threads have finished
494 for (auto& t : threads) {
495 t.join();
496 }
497
498 const auto time_e = std::chrono::high_resolution_clock::now();
499
500 const auto elap = std::chrono::duration_cast<std::chrono::microseconds>(time_e - time_s);
501
502 const bool is_valid =
503 is_output_correct(m, n, 0.0001f, (const float*)dst_ref_mtx_f32, (const float*)dst_act_mtx_f32);
504
505 if (is_valid) {
506 printf("TEST[%ld] = PASSED\n", idx_variant);
507 std::cout << "- Performance: " << elap.count() << " us" << std::endl;
508 } else {
509 printf("TEST[%ld] = FAILED\n", idx_variant);
510 }
511 delete[] lhs_packed_mtx_qs8d32;
512 delete[] rhs_packed_mtx_qs4c32;
513 delete[] dst_act_mtx_f32;
514 }
515 delete[] lhs_native_mtx_f32;
516 delete[] rhs_native_mtx_qs4c32;
517 delete[] dst_ref_mtx_f32;
518 }
519
520 //----------- END MICRO-KERNELS TESTS
521 //------------------------------------
522 //------------------------------------
523
524 #endif // Architectural feature check