Debian Codegraph @

examples/matmul_clamp_f32_qai8dxp_qsi4c32p/matmul_clamp_f32_qai8dxp_qsi4c32p.cpp

raw symbols SCIP
1 //
2 // SPDX-FileCopyrightText: Copyright 2024 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)
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 <cstring>
15 #include <iostream>
16 #include <string>
17
18 // Include micro-kernel variants
19 #include "kai_lhs_quant_pack_qai8dxp_f32.h"
20 #include "kai_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod.h"
21 #include "kai_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod.h"
22 #include "kai_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod.h"
23 #include "kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm.h"
24 #include "kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm.h"
25 #include "kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm.h"
26 #include "kai_matmul_clamp_f32_qai8dxp_qsi4c32p_interface.h"
27 #include "kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0.h"
28 #include "kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0.h"
29
30 #define INT4_MIN (-8)
31 #define INT4_MAX (7)
32
33 enum class rhs_format {
34 nxk,
35 kxn,
36 };
37 struct mnk {
38 size_t m = 0;
39 size_t n = 0;
40 size_t k = 0;
41 size_t bl = 0;
42 };
43 mnk matmul_shapes[] = {{1, 33, 32, 32}, {13, 33, 32, 32}, {37, 75, 256, 64}, {16, 32, 64, 32}, {8, 32, 64, 64}};
44 // Micro-kernel interface
45 struct kai_matmul_ukernel_f32_qa8dxp_qs4c32p {
46 kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel ukernel;
47 std::string name = {};
48 };
49
50 kai_matmul_ukernel_f32_qa8dxp_qs4c32p ukernel_variants[] = {
51 {{kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
52 kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
53 kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
54 kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
55 kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
56 kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
57 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
58 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
59 kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
60 kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod,
61 kai_run_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod},
62 "matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod"},
63 {{kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
64 kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
65 kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
66 kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
67 kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
68 kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
69 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
70 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
71 kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
72 kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod,
73 kai_run_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod},
74 "matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod"},
75 {{kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
76 kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
77 kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
78 kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
79 kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
80 kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
81 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
82 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
83 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
84 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm,
85 kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm},
86 "matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm"},
87 {{kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
88 kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
89 kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
90 kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
91 kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
92 kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
93 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
94 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
95 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
96 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm,
97 kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm},
98 "matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm"},
99 {{kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
100 kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
101 kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
102 kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
103 kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
104 kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
105 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
106 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
107 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
108 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm,
109 kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm},
110 "matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm"},
111 {{kai_get_m_step_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
112 kai_get_n_step_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
113 kai_get_mr_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
114 kai_get_nr_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
115 kai_get_kr_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
116 kai_get_sr_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
117 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
118 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
119 kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
120 kai_get_dst_size_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod,
121 kai_run_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod},
122 "matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod"},
123 };
124
125 // Number of micro-kernel variants stored in the array
126 const size_t num_ukernel_variants = sizeof(ukernel_variants) / sizeof(ukernel_variants[0]);
127
128 static size_t roundup(size_t a, size_t b) {
129 return ((a + b - 1) / b) * b;
130 }
131
132 static inline size_t get_num_blocks_per_row(size_t k, size_t bl) {
133 return roundup(k, bl) / bl;
134 }
135
136 static inline size_t get_rhs_native_stride(size_t x) {
137 return roundup(x, 2) / 2;
138 }
139
140 static inline size_t get_rhs_scale_stride(size_t k, size_t bl) {
141 const size_t num_blocks_per_row = get_num_blocks_per_row(k, bl);
142 return num_blocks_per_row * sizeof(uint16_t);
143 }
144
145 static void fill_uniform_random(size_t num_rows, size_t num_cols, float* dst, size_t seed) {
146 std::srand(seed);
147
148 // Fill the array with random values between -1 and 1
149 for (size_t i = 0; i < num_rows * num_cols; i++) {
150 dst[i] = (float)((double)std::rand() / RAND_MAX) * 2 - 1;
151 }
152 }
153
154 static void quant_nxk_qs4c32_f32(
155 size_t n, size_t k, size_t bl, const float* rhs_f32, uint8_t* rhs_qs4c32, uint16_t* rhs_scales_bf16) {
156 const size_t num_blocks_row = get_num_blocks_per_row(k, bl);
157 const size_t rhs_qs4c32_stride = get_rhs_native_stride(k);
158
159 // Make sure the output is filled with zeros
160 std::memset(rhs_qs4c32, 0, n * rhs_qs4c32_stride);
161
162 for (size_t row_idx = 0; row_idx < n; ++row_idx) {
163 const float* src_ptr = rhs_f32 + row_idx * k;
164
165 for (size_t block_idx = 0; block_idx < num_blocks_row; ++block_idx) {
166 float amax = 0.0f;
167 float max = 0.0f;
168
169 for (size_t b = 0; b < bl; ++b) {
170 const size_t k_idx = block_idx * bl + b;
171
172 if (k_idx >= k) {
173 break;
174 }
175
176 const float src0_0 = src_ptr[k_idx];
177 const float asrc0_0 = fabsf(src0_0);
178
179 if (amax < asrc0_0) {
180 amax = asrc0_0;
181 max = src0_0;
182 }
183 }
184
185 const float scale = max / -8.0;
186 const float recip_scale = scale ? 1.0f / scale : 0.0f;
187
188 // Store the scale in the dedicated buffer
189 *rhs_scales_bf16 = kai_cast_bf16_f32(scale);
190
191 rhs_scales_bf16 += 1;
192
193 for (size_t i = 0; i < bl; ++i) {
194 const size_t k_idx = block_idx * bl + i;
195
196 if (k_idx >= k) {
197 break;
198 }
199
200 const float src0_0 = src_ptr[k_idx];
201
202 // Scale the values
203 int32_t v0_s32 = (int32_t)(round(src0_0 * recip_scale));
204
205 // Maximum/minimum int4 values
206 v0_s32 = std::max(v0_s32, INT4_MIN);
207 v0_s32 = std::min(v0_s32, INT4_MAX);
208
209 const uint8_t v0_u8 = (uint8_t)(v0_s32 + 8);
210
211 const size_t dst_addr = (k_idx / 2) + row_idx * rhs_qs4c32_stride;
212 uint8_t rhs_v0 = rhs_qs4c32[dst_addr];
213
214 if ((k_idx % 2) == 0) {
215 rhs_v0 = v0_u8;
216 } else {
217 rhs_v0 |= (v0_u8 << 4);
218 }
219
220 rhs_qs4c32[dst_addr] = rhs_v0;
221 }
222 }
223 }
224 }
225
226 static void quant_kxn_qs4c32_f32(
227 size_t n, size_t k, size_t bl, const float* rhs_f32, uint8_t* rhs_qs4c32, uint16_t* rhs_scales_bf16) {
228 const size_t num_blocks_row = get_num_blocks_per_row(k, bl);
229 const size_t rhs_qs4c32_stride = get_rhs_native_stride(n);
230
231 // Make sure the output is filled with zeros
232 std::memset(rhs_qs4c32, 0, k * rhs_qs4c32_stride);
233
234 for (size_t row_idx = 0; row_idx < n; ++row_idx) {
235 const float* src_ptr = rhs_f32 + row_idx * k;
236
237 for (size_t block_idx = 0; block_idx < num_blocks_row; ++block_idx) {
238 float amax = 0.0f;
239 float max = 0.0f;
240
241 for (size_t b = 0; b < bl; ++b) {
242 const size_t k_idx = block_idx * bl + b;
243
244 if (k_idx >= k) {
245 break;
246 }
247
248 const float src0_0 = src_ptr[k_idx];
249 const float asrc0_0 = fabsf(src0_0);
250
251 if (amax < asrc0_0) {
252 amax = asrc0_0;
253 max = src0_0;
254 }
255 }
256
257 const float scale = max / -8.0;
258 const float recip_scale = scale ? 1.0f / scale : 0.0f;
259
260 // Store the scale in the dedicated buffer
261 *rhs_scales_bf16 = kai_cast_bf16_f32(scale);
262
263 rhs_scales_bf16 += 1;
264
265 for (size_t i = 0; i < bl; ++i) {
266 const size_t k_idx = block_idx * bl + i;
267
268 if (k_idx >= k) {
269 break;
270 }
271
272 const float src0_0 = src_ptr[k_idx];
273
274 // Scale the values
275 int32_t v0_s32 = (int32_t)(round(src0_0 * recip_scale));
276
277 // Maximum/minimum int4 values
278 v0_s32 = std::max(v0_s32, INT4_MIN);
279 v0_s32 = std::min(v0_s32, INT4_MAX);
280
281 const uint8_t v0_u8 = (uint8_t)(v0_s32 + 8);
282
283 const size_t dst_addr = (row_idx / 2) + k_idx * rhs_qs4c32_stride;
284 uint8_t rhs_v0 = rhs_qs4c32[dst_addr];
285
286 if ((row_idx % 2) == 0) {
287 rhs_v0 = v0_u8;
288 } else {
289 rhs_v0 |= (v0_u8 << 4);
290 }
291
292 rhs_qs4c32[dst_addr] = rhs_v0;
293 }
294 }
295 }
296 }
297
298 static void quant_qs4cx_f32(
299 size_t n, size_t k, size_t bl, rhs_format format, const float* rhs_f32, uint8_t* rhs_qs4c32,
300 uint16_t* rhs_scales_bf16) {
301 if (rhs_format::nxk == format) {
302 quant_nxk_qs4c32_f32(n, k, bl, rhs_f32, rhs_qs4c32, rhs_scales_bf16);
303 } else {
304 quant_kxn_qs4c32_f32(n, k, bl, rhs_f32, rhs_qs4c32, rhs_scales_bf16);
305 }
306 };
307
308 static void ref_quant_qa8dx_f32(size_t m, size_t k, const float* lhs_f32, int8_t* lhs_qa8dx) {
309 const size_t dst_stride = (k * sizeof(int8_t) + sizeof(float) + sizeof(int32_t));
310
311 for (size_t row_idx = 0; row_idx < m; ++row_idx) {
312 const float* src_ptr = lhs_f32 + row_idx * k;
313
314 float max0 = -FLT_MAX;
315 float min0 = FLT_MAX;
316
317 // Find min/max for each channel
318 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
319 const float src0_0 = src_ptr[k_idx];
320
321 max0 = std::max(src0_0, max0);
322 min0 = std::min(src0_0, min0);
323 }
324
325 // Maximum/minimum int8 values
326 const float qmin = (float)INT8_MIN;
327 const float qmax = (float)INT8_MAX;
328
329 const float rmin0 = std::min(0.0f, min0);
330 const float rmax0 = std::max(0.0f, max0);
331
332 const float scale0 = rmin0 == rmax0 ? 1.f : (qmax - qmin) / (rmax0 - rmin0);
333
334 // Reciprocal to quantize
335 const float recip_scale0 = scale0 ? 1.0f / scale0 : 0.0f;
336
337 const float descaled_min0 = rmin0 * scale0;
338 const float descaled_max0 = rmax0 * scale0;
339
340 const float zero_point_from_min_error0 = qmin + descaled_min0;
341 const float zero_point_from_max_error0 = qmax + descaled_max0;
342
343 float zero_point0 =
344 zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;
345
346 zero_point0 = std::max(zero_point0, qmin);
347 zero_point0 = std::min(zero_point0, qmax);
348
349 // Round to nearest integer
350 const int32_t nudged_zero_point0 = lrintf(zero_point0);
351
352 int8_t* dst_ptr = (int8_t*)lhs_qa8dx + row_idx * dst_stride;
353
354 // LHS offset at the beginning of the row
355 *((float*)(dst_ptr)) = recip_scale0;
356 dst_ptr += sizeof(float);
357 *((int32_t*)(dst_ptr)) = -nudged_zero_point0;
358 dst_ptr += sizeof(int32_t);
359
360 // Quantize the channels
361 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
362 const float src0_0 = src_ptr[k_idx];
363
364 // Scale the values
365 int32_t v0_s32 = (int32_t)(round(src0_0 * scale0));
366
367 v0_s32 = v0_s32 + nudged_zero_point0;
368 v0_s32 = std::max(v0_s32, INT8_MIN);
369 v0_s32 = std::min(v0_s32, INT8_MAX);
370 dst_ptr[0] = (int8_t)v0_s32;
371 dst_ptr += sizeof(int8_t);
372 }
373 }
374 }
375
376 static void ref_matmul_mxn_mxk_nxk_f32_qa8dx_qs4c32(
377 size_t m, size_t n, size_t k, size_t bl, const int8_t* lhs_qa8dx, const uint8_t* rhs_qs4c32,
378 const uint16_t* scale_bf16, float* dst_f32, float scalar_min, float scalar_max) {
379 const size_t num_blocks_row = get_num_blocks_per_row(k, bl);
380
381 const size_t lhs_stride = k + sizeof(float) + sizeof(int32_t);
382 const size_t rhs_stride = get_rhs_native_stride(k);
383
384 for (size_t row_idx = 0; row_idx < m; ++row_idx) {
385 const int8_t* lhs_ptr_start = lhs_qa8dx + row_idx * lhs_stride;
386
387 for (size_t col_idx = 0; col_idx < n; ++col_idx) {
388 // Main f32 accumulator
389 float main_acc = 0.0f;
390
391 const int8_t* lhs_ptr = lhs_ptr_start;
392 const uint8_t* rhs_ptr = rhs_qs4c32 + col_idx * rhs_stride;
393
394 // Get the LHS quantization parameters stored at the
395 // beginning of each row
396 const float lhs_scale = *(const float*)lhs_ptr;
397 lhs_ptr += sizeof(float);
398
399 const int32_t lhs_offset = *(const int32_t*)lhs_ptr;
400 lhs_ptr += sizeof(int32_t);
401
402 for (size_t block_idx = 0; block_idx < num_blocks_row; ++block_idx) {
403 const uint16_t rhs_scale_bf16 = scale_bf16[block_idx + col_idx * num_blocks_row];
404 const float rhs_scale = kai_cast_f32_bf16(rhs_scale_bf16);
405
406 int32_t iacc = 0;
407
408 for (size_t i = 0; i < bl; ++i) {
409 const size_t k_idx = block_idx * bl + i;
410
411 if (k_idx >= k) {
412 break;
413 }
414
415 // Get the LHS values
416 const int32_t lhs_v0 = (int32_t)lhs_ptr[0];
417
418 // Get the RHS values
419 const uint8_t rhs_byte = rhs_ptr[0];
420
421 // Unpack the RHS values
422 int32_t rhs_v0 = 0;
423 if ((k_idx % 2) == 0) {
424 rhs_v0 = (((int32_t)(rhs_byte & 0x0F)) - 8);
425 } else {
426 rhs_v0 = (((int32_t)(rhs_byte >> 4)) - 8);
427 }
428
429 iacc += lhs_v0 * rhs_v0;
430 iacc += lhs_offset * rhs_v0;
431
432 lhs_ptr += 1;
433
434 // Increment only when k_idx is not a multiple of 2
435 rhs_ptr += k_idx % 2;
436 }
437
438 main_acc += iacc * rhs_scale;
439 }
440
441 main_acc = main_acc * lhs_scale;
442
443 // Clamp (min-max) operation
444 main_acc = std::max(main_acc, scalar_min);
445 main_acc = std::min(main_acc, scalar_max);
446
447 dst_f32[0] = main_acc;
448 dst_f32 += 1;
449 }
450 }
451 };
452
453 static void ref_matmul_mxn_mxk_kxn_f32_qa8dx_qs4c32(
454 size_t m, size_t n, size_t k, size_t bl, const int8_t* lhs_qa8dx, const uint8_t* rhs_qs4c32,
455 const uint16_t* scale_bf16, float* dst_f32, float scalar_min, float scalar_max) {
456 const size_t num_blocks_row = get_num_blocks_per_row(k, bl);
457
458 const size_t lhs_stride = k + sizeof(float) + sizeof(int32_t);
459 const size_t rhs_stride = get_rhs_native_stride(n);
460
461 for (size_t row_idx = 0; row_idx < m; ++row_idx) {
462 const int8_t* lhs_ptr_start = lhs_qa8dx + row_idx * lhs_stride;
463
464 for (size_t col_idx = 0; col_idx < n; ++col_idx) {
465 // Main f32 accumulator
466 float main_acc = 0.0f;
467
468 const int8_t* lhs_ptr = lhs_ptr_start;
469 const uint8_t* rhs_ptr = rhs_qs4c32 + (col_idx / 2);
470
471 // Get the LHS quantization parameters stored at the
472 // beginning of each row
473 const float lhs_scale = *(const float*)lhs_ptr;
474 lhs_ptr += sizeof(float);
475
476 const int32_t lhs_offset = *(const int32_t*)lhs_ptr;
477 lhs_ptr += sizeof(int32_t);
478
479 for (size_t block_idx = 0; block_idx < num_blocks_row; ++block_idx) {
480 const uint16_t rhs_scale_bf16 = scale_bf16[block_idx + col_idx * num_blocks_row];
481 const float rhs_scale = kai_cast_f32_bf16(rhs_scale_bf16);
482
483 int32_t iacc = 0;
484
485 for (size_t i = 0; i < bl; ++i) {
486 const size_t k_idx = block_idx * bl + i;
487
488 if (k_idx >= k) {
489 break;
490 }
491
492 // Get the LHS values
493 const int32_t lhs_v0 = (int32_t)lhs_ptr[0];
494
495 // Get the RHS values
496 const uint8_t rhs_byte = rhs_ptr[0];
497
498 // Unpack the RHS values
499 int32_t rhs_v0 = 0;
500 if ((col_idx % 2) == 0) {
501 rhs_v0 = (((int32_t)(rhs_byte & 0x0F)) - 8);
502 } else {
503 rhs_v0 = (((int32_t)(rhs_byte >> 4)) - 8);
504 }
505
506 iacc += lhs_v0 * rhs_v0;
507 iacc += lhs_offset * rhs_v0;
508
509 lhs_ptr += 1;
510 rhs_ptr += rhs_stride;
511 }
512
513 main_acc += iacc * rhs_scale;
514 }
515
516 main_acc = main_acc * lhs_scale;
517
518 // Clamp (min-max) operation
519 main_acc = std::max(main_acc, scalar_min);
520 main_acc = std::min(main_acc, scalar_max);
521
522 dst_f32[0] = main_acc;
523 dst_f32 += 1;
524 }
525 }
526 };
527
528 static void ref_matmul_f32_qa8dx_qs4c32(
529 size_t m, size_t n, size_t k, size_t bl, rhs_format format, const int8_t* lhs_qa8dx, const uint8_t* rhs_qs4c32,
530 const uint16_t* rhs_scales_bf16, float* dst_f32, float scalar_min, float scalar_max) {
531 if (rhs_format::nxk == format) {
532 ref_matmul_mxn_mxk_nxk_f32_qa8dx_qs4c32(
533 m, n, k, bl, lhs_qa8dx, rhs_qs4c32, rhs_scales_bf16, dst_f32, scalar_min, scalar_max);
534 } else {
535 ref_matmul_mxn_mxk_kxn_f32_qa8dx_qs4c32(
536 m, n, k, bl, lhs_qa8dx, rhs_qs4c32, rhs_scales_bf16, dst_f32, scalar_min, scalar_max);
537 }
538 };
539
540 static bool is_output_correct(size_t num_rows, size_t num_cols, float tolerance, const float* ref, const float* act) {
541 bool is_valid = true;
542
543 for (size_t i = 0; i < num_rows * num_cols; ++i) {
544 if (std::fabs(ref[i] - act[i]) > tolerance) {
545 const size_t x = i % num_cols;
546 const size_t y = i / num_cols;
547 printf("ERROR![%ld][%ld]: ref=%.5f vs. act=%.5f\n", y, x, ref[i], act[i]);
548 is_valid = false;
549 }
550 }
551 return is_valid;
552 }
553
554 int main() {
555 const size_t num_shapes = std::size(matmul_shapes);
556
557 const size_t seed_lhs = 4568;
558 const size_t seed_rhs = seed_lhs + 4;
559
560 std::cout << "------------" << std::endl;
561 for (size_t test_idx = 0; test_idx < num_shapes; ++test_idx) {
562 size_t m = matmul_shapes[test_idx].m;
563 size_t n = matmul_shapes[test_idx].n;
564 size_t k = matmul_shapes[test_idx].k;
565 size_t bl = matmul_shapes[test_idx].bl;
566
567 std::cout << "\nTEST[" << m << ", " << n << "," << k << "] with Block Size " << bl << "\n";
568 // Iterate over the RHS format (NxK or KxN)
569 for (const rhs_format& format : {rhs_format::nxk, rhs_format::kxn}) {
570 std::cout << "Testing RHS format = " << (format == rhs_format::nxk ? "N x K" : "K x N") << std::endl;
571
572 const size_t lhs_native_size_f32 = m * k * sizeof(float);
573 const size_t rhs_native_size_f32 = n * k * sizeof(float);
574 const size_t rhs_native_size_qs4c32 =
575 format == rhs_format::nxk ? n * get_rhs_native_stride(k) : k * get_rhs_native_stride(n);
576 const size_t rhs_scales_size_bf16 = n * get_rhs_scale_stride(k, bl);
577
578 // Allocate the memory
579 uint8_t* lhs_native_mtx_f32 = new uint8_t[lhs_native_size_f32];
580 uint8_t* rhs_native_mtx_f32 = new uint8_t[rhs_native_size_f32];
581 uint8_t* rhs_native_mtx_qs4c32 = new uint8_t[rhs_native_size_qs4c32];
582 uint8_t* rhs_scales_mtx_bf16 = new uint8_t[rhs_scales_size_bf16];
583
584 fill_uniform_random(m, k, (float*)lhs_native_mtx_f32, seed_lhs);
585 fill_uniform_random(n, k, (float*)rhs_native_mtx_f32, seed_rhs);
586
587 quant_qs4cx_f32(
588 n, k, bl, // Dimensions
589 format, // Format (NxK or KxN)
590 (const float*)rhs_native_mtx_f32, // RHS (F32)
591 rhs_native_mtx_qs4c32, // RHS (QS4C32)
592 (uint16_t*)rhs_scales_mtx_bf16); // Scales (Bf16)
593
594 delete[] rhs_native_mtx_f32;
595
596 //----------- REFERENCE IMPLEMENTATION
597 //------------------------------------
598 //------------------------------------
599 // Memory sizes for the reference implementation
600 // After dynamically quantized the LHS matrix, we have the scale and offset for each
601 // row. The scale (f32) and offset (int32) are stored at the beginning of each row
602 const size_t lhs_ref_size_qa8dx = m * (k + sizeof(int32_t) + sizeof(float));
603 const size_t dst_ref_size_f32 = m * n * sizeof(float);
604
605 uint8_t* lhs_ref_mtx_qa8dx = new uint8_t[lhs_ref_size_qa8dx];
606 uint8_t* dst_ref_mtx_f32 = new uint8_t[dst_ref_size_f32];
607
608 ref_quant_qa8dx_f32(m, k, (const float*)lhs_native_mtx_f32, (int8_t*)lhs_ref_mtx_qa8dx);
609
610 ref_matmul_f32_qa8dx_qs4c32(
611 m, n, k, // Dimensions
612 bl, // Block length
613 format, // Format (NxK or KxN)
614 (const int8_t*)lhs_ref_mtx_qa8dx, // LHS
615 (const uint8_t*)rhs_native_mtx_qs4c32, // RHS
616 (const uint16_t*)rhs_scales_mtx_bf16, // Scale
617 (float*)dst_ref_mtx_f32, // DST
618 -FLT_MAX, FLT_MAX); // Min and max for the clamp operation
619
620 // Remove the unnecessary buffer
621 delete[] lhs_ref_mtx_qa8dx;
622
623 //----------- END REFERENCE IMPLEMENTATION
624 //------------------------------------
625 //------------------------------------
626
627 //----------- MICRO-KERNELS TESTS
628 //------------------------------------
629 //------------------------------------
630 for (size_t idx_variant = 0; idx_variant < num_ukernel_variants; ++idx_variant) {
631 // Get the packing parameters
632 const size_t mr = ukernel_variants[idx_variant].ukernel.get_mr();
633 const size_t nr = ukernel_variants[idx_variant].ukernel.get_nr();
634 const size_t kr = ukernel_variants[idx_variant].ukernel.get_kr();
635 const size_t sr = ukernel_variants[idx_variant].ukernel.get_sr();
636
637 // Get the size in bytes for the packed matrices
638 const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(m, k, mr, kr, sr);
639 size_t rhs_packed_size = 0;
640
641 if (format == rhs_format::nxk) {
642 rhs_packed_size =
643 kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(n, k, nr, kr, sr, bl, kai_dt_bf16);
644
645 } else {
646 rhs_packed_size =
647 kai_get_rhs_packed_size_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0(n, k, nr, kr, sr, bl, kai_dt_bf16);
648 }
649
650 const size_t dst_size = ukernel_variants[idx_variant].ukernel.get_dst_size(m, n);
651
652 // Allocate the matrices
653 uint8_t* lhs_packed_mtx_qa8dx = new uint8_t[lhs_packed_size];
654 uint8_t* rhs_packed_mtx_qs4c32 = new uint8_t[rhs_packed_size];
655 uint8_t* dst_act_mtx_f32 = new uint8_t[dst_size];
656
657 memset(dst_act_mtx_f32, 0, dst_size);
658
659 // If the RHS matrix contains constant values, the packing can be performed
660 // only once
661 if (format == rhs_format::nxk) {
662 kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params params;
663 params.lhs_zero_point = 1;
664 params.rhs_zero_point = 8;
665 params.scale_dt = kai_dt_bf16;
666
667 kai_run_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(
668 1, n, k, // Dimensions
669 nr, kr, sr, // Packing arguments
670 bl, // Block length
671 (const uint8_t*)(rhs_native_mtx_qs4c32), // RHS
672 get_rhs_native_stride(k), // RHS stride
673 NULL, // Bias
674 rhs_scales_mtx_bf16, // Scale
675 get_rhs_scale_stride(k, bl), // Scale stride
676 rhs_packed_mtx_qs4c32, // RHS packed
677 0, &params);
678
679 } else {
680 kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params params;
681 params.lhs_zero_point = 1;
682 params.rhs_zero_point = 8;
683 params.scale_dt = kai_dt_bf16;
684
685 kai_run_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0(
686 1, n, k, // Dimensions
687 nr, kr, sr, // Packing arguments
688 bl, // Block length
689 (const uint8_t*)(rhs_native_mtx_qs4c32), // RHS
690 get_rhs_native_stride(n), // RHS stride
691 NULL, // Bias
692 rhs_scales_mtx_bf16, // Scale
693 get_rhs_scale_stride(k, bl), // Scale stride
694 rhs_packed_mtx_qs4c32, // RHS packed
695 0, &params);
696 }
697
698 const auto time_s = std::chrono::high_resolution_clock::now();
699
700 // LHS packing
701 kai_run_lhs_quant_pack_qai8dxp_f32(
702 m, k, // Dimensions
703 mr, kr, sr, 0, // Packing arguments
704 (const float*)lhs_native_mtx_f32, // LHS
705 k * sizeof(float), // LHS stride
706 lhs_packed_mtx_qa8dx); // LHS packed
707
708 // Matmul
709 {
710 const size_t dst_stride = n * sizeof(float);
711 const size_t lhs_offset = ukernel_variants[idx_variant].ukernel.get_lhs_packed_offset(0, k);
712 const size_t rhs_offset = ukernel_variants[idx_variant].ukernel.get_rhs_packed_offset(0, k, bl);
713 const size_t dst_offset = ukernel_variants[idx_variant].ukernel.get_dst_offset(0, 0, dst_stride);
714
715 const void* lhs_ptr = (const void*)((const char*)lhs_packed_mtx_qa8dx + lhs_offset);
716 const void* rhs_ptr = (const void*)((const char*)rhs_packed_mtx_qs4c32 + rhs_offset);
717 float* dst_ptr = (float*)((uint8_t*)dst_act_mtx_f32 + dst_offset);
718
719 ukernel_variants[idx_variant].ukernel.run_matmul(
720 m, n, k, // Dimensions
721 bl, // Block length
722 lhs_ptr, // LHS packed
723 rhs_ptr, // RHS packed
724 dst_ptr, // DST
725 dst_stride, // DST stride (row)
726 sizeof(float), // DST stride (col)
727 -FLT_MAX, FLT_MAX // Min and max for the clamp operation
728 );
729 }
730
731 const auto time_e = std::chrono::high_resolution_clock::now();
732
733 const auto elap = std::chrono::duration_cast<std::chrono::microseconds>(time_e - time_s);
734
735 const bool is_valid =
736 is_output_correct(m, n, 0.0001f, (const float*)dst_ref_mtx_f32, (const float*)dst_act_mtx_f32);
737
738 std::cout << "TEST[" << idx_variant << "]: Dynamic quantization + matmul" << std::endl;
739 std::cout << "- ukernel: " << ukernel_variants[idx_variant].name << std::endl;
740 if (is_valid) {
741 std::cout << "- Status: PASSED" << std::endl;
742 std::cout << "- Performance: " << elap.count() << " us" << std::endl;
743 } else {
744 std::cout << "Status: FAILED" << std::endl;
745 }
746 std::cout << "------------" << std::endl;
747 delete[] lhs_packed_mtx_qa8dx;
748 delete[] rhs_packed_mtx_qs4c32;
749 delete[] dst_act_mtx_f32;
750 }
751 delete[] lhs_native_mtx_f32;
752 delete[] rhs_native_mtx_qs4c32;
753 delete[] rhs_scales_mtx_bf16;
754 delete[] dst_ref_mtx_f32;
755 }
756 }
757 }
758
759 //----------- END MICRO-KERNELS TESTS
760 //------------------------------------
761 //------------------------------------
762
763 #endif // Architectural feature check