Debian Codegraph @

examples/matmul_clamp_f32_qai8dxp_qsi4cxp/matmul_clamp_f32_qai8dxp_qsi4cxp.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 <cmath>
12 #include <cstdint>
13 #include <cstring>
14 #include <iostream>
15 #include <string>
16
17 // Include micro-kernel variants
18 #include "kai_lhs_quant_pack_qai8dxp_f32.h"
19 #include "kai_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod.h"
20 #include "kai_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod.h"
21 #include "kai_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod.h"
22 #include "kai_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod.h"
23 #include "kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod.h"
24 #include "kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm.h"
25 #include "kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm.h"
26 #include "kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm.h"
27 #include "kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm.h"
28 #include "kai_matmul_clamp_f32_qai8dxp_qsi4cxp_interface.h"
29 #include "kai_rhs_pack_kxn_qsi4cxp_qs4cxs1s0.h"
30 #include "kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0.h"
31
32 #define INT4_MIN (-8)
33 #define INT4_MAX (7)
34
35 enum class rhs_format {
36 nxk,
37 kxn,
38 };
39
40 struct mnk {
41 size_t m = 0;
42 size_t n = 0;
43 size_t k = 0;
44 };
45
46 mnk matmul_shapes[] = {{1, 33, 32}, {13, 33, 32}, {37, 75, 17}, {16, 32, 64}, {7, 17, 33}, {15, 31, 45}};
47
48 // Micro-kernel interface
49 struct kai_matmul_ukernel_f32_qa8dxp_qs4cxp {
50 kai_matmul_clamp_f32_qai8dxp_qsi4cxp_ukernel ukernel;
51 std::string name = {};
52 };
53
54 kai_matmul_ukernel_f32_qa8dxp_qs4cxp ukernel_variants[] = {
55 {kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
56 kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
57 kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
58 kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
59 kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
60 kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
61 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
62 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
63 kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
64 kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
65 kai_run_matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod,
66 "matmul_clamp_f32_qai8dxp1x8_qsi4cxp4x8_1x4x32_neon_dotprod"},
67 {kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
68 kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
69 kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
70 kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
71 kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
72 kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
73 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
74 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
75 kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
76 kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
77 kai_run_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod,
78 "matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod"},
79 {kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
80 kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
81 kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
82 kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
83 kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
84 kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
85 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
86 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
87 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
88 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
89 kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm,
90 "matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm"},
91 {kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
92 kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
93 kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
94 kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
95 kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
96 kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
97 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
98 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
99 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
100 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
101 kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm,
102 "matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm"},
103 {kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
104 kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
105 kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
106 kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
107 kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
108 kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
109 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
110 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
111 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
112 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
113 kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm,
114 "matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm"},
115 {kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
116 kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
117 kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
118 kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
119 kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
120 kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
121 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
122 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
123 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
124 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
125 kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
126 "matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm"},
127 {kai_get_m_step_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
128 kai_get_n_step_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
129 kai_get_mr_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
130 kai_get_nr_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
131 kai_get_kr_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
132 kai_get_sr_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
133 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
134 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
135 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
136 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
137 kai_run_matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod,
138 "matmul_clamp_f32_qai8dxp4x4_qsi4cxp8x4_8x8x32_neon_dotprod"},
139 {kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
140 kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
141 kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
142 kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
143 kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
144 kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
145 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
146 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
147 kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
148 kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
149 kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod,
150 "matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x4_16x4x32_neon_dotprod"},
151 {kai_get_m_step_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
152 kai_get_n_step_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
153 kai_get_mr_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
154 kai_get_nr_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
155 kai_get_kr_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
156 kai_get_sr_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
157 kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
158 kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
159 kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
160 kai_get_dst_size_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
161 kai_run_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod,
162 "matmul_clamp_f32_qai8dxp1x4_qsi4cxp4x4_1x4_neon_dotprod"}
163
164 };
165
166 // Number of micro-kernel variants stored in the array
167 const size_t num_ukernel_variants = sizeof(ukernel_variants) / sizeof(ukernel_variants[0]);
168
169 static size_t roundup(size_t a, size_t b) {
170 return ((a + b - 1) / b) * b;
171 }
172
173 static void fill_uniform_random(size_t num_rows, size_t num_cols, float* dst, size_t seed) {
174 std::srand(seed);
175
176 // Fill the array with random values between -1 and 1
177 for (int i = 0; i < num_rows * num_cols; i++) {
178 dst[i] = (float)((double)std::rand() / RAND_MAX) * 2 - 1;
179 }
180 }
181
182 static void ref_quant_qa8dx_f32(size_t m, size_t k, const float* lhs_f32, int8_t* lhs_qa8dx) {
183 const size_t dst_stride = (k * sizeof(int8_t) + sizeof(float) + sizeof(int32_t));
184
185 const size_t lhs_qa8dx_stride = k;
186
187 for (size_t m_idx = 0; m_idx < m; ++m_idx) {
188 const float* src_ptr = lhs_f32 + m_idx * lhs_qa8dx_stride;
189
190 float max0 = -FLT_MAX;
191 float min0 = FLT_MAX;
192
193 // Find min/max for each channel
194 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
195 const float src0_0 = src_ptr[k_idx];
196
197 max0 = std::max(src0_0, max0);
198 min0 = std::min(src0_0, min0);
199 }
200
201 // Maximum/minimum int8 values
202 const float qmin = (float)INT8_MIN;
203 const float qmax = (float)INT8_MAX;
204
205 const float rmin0 = std::min(0.0f, min0);
206 const float rmax0 = std::max(0.0f, max0);
207
208 const float scale0 = rmin0 == rmax0 ? 1.f : (qmax - qmin) / (rmax0 - rmin0);
209
210 // Reciprocal to quantize
211 const float recip_scale0 = scale0 ? 1.0f / scale0 : 0.0f;
212
213 const float descaled_min0 = rmin0 * scale0;
214 const float descaled_max0 = rmax0 * scale0;
215
216 const float zero_point_from_min_error0 = qmin + descaled_min0;
217 const float zero_point_from_max_error0 = qmax + descaled_max0;
218
219 float zero_point0 =
220 zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;
221
222 zero_point0 = std::max(zero_point0, qmin);
223 zero_point0 = std::min(zero_point0, qmax);
224
225 // Round to nearest integer
226 const int32_t nudged_zero_point0 = lrintf(zero_point0);
227
228 int8_t* dst_ptr = (int8_t*)lhs_qa8dx + m_idx * dst_stride;
229
230 // LHS offset at the beginning of the row
231 *((float*)(dst_ptr)) = recip_scale0;
232 dst_ptr += sizeof(float);
233 *((int32_t*)(dst_ptr)) = -nudged_zero_point0;
234 dst_ptr += sizeof(int32_t);
235
236 // Quantize the channels
237 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
238 const float src0_0 = src_ptr[k_idx];
239
240 // Scale the values
241 int32_t v0_s32 = (int32_t)(round(src0_0 * scale0));
242
243 v0_s32 = v0_s32 + nudged_zero_point0;
244 v0_s32 = std::max(v0_s32, INT8_MIN);
245 v0_s32 = std::min(v0_s32, INT8_MAX);
246 dst_ptr[0] = (int8_t)v0_s32;
247 dst_ptr += sizeof(int8_t);
248 }
249 }
250 };
251
252 static void quant_nxk_qs4cx_f32(size_t n, size_t k, const float* rhs_f32, uint8_t* rhs_qs4cx, float* rhs_scales_f32) {
253 const size_t rhs_qs4cx_stride = (roundup(k, 2) / 2);
254
255 // Make sure the output is filled with zeros
256 std::memset(rhs_qs4cx, 0, n * rhs_qs4cx_stride);
257
258 for (size_t n_idx = 0; n_idx < n; ++n_idx) {
259 const float* src_ptr = rhs_f32 + n_idx * k;
260
261 float max0 = -FLT_MAX;
262 float min0 = FLT_MAX;
263
264 // Find min/max for each channel
265 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
266 const float src0_0 = src_ptr[k_idx];
267
268 max0 = std::max(src0_0, max0);
269 min0 = std::min(src0_0, min0);
270 }
271
272 // Maximum/minimum int8 values
273 const float qmin = (float)INT4_MIN;
274 const float qmax = (float)INT4_MAX;
275
276 const float rmin0 = std::min(0.0f, min0);
277 const float rmax0 = std::max(0.0f, max0);
278
279 const float scale0 = rmin0 == rmax0 ? 1.f : (qmax - qmin) / (rmax0 - rmin0);
280
281 // Reciprocal to quantize
282 const float recip_scale0 = scale0 ? 1.0f / scale0 : 0.0f;
283
284 // Quantize the channels
285 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
286 const float src0_0 = src_ptr[k_idx];
287
288 // Scale the values
289 int32_t v0_s32 = (int32_t)(round(src0_0 * scale0));
290
291 // Maximum/minimum int4 values
292 v0_s32 = std::max(v0_s32, INT4_MIN);
293 v0_s32 = std::min(v0_s32, INT4_MAX);
294
295 const uint8_t v0_u8 = (uint8_t)(v0_s32 + 8);
296
297 const size_t dst_addr = (k_idx / 2) + n_idx * rhs_qs4cx_stride;
298 uint8_t rhs_v0 = rhs_qs4cx[dst_addr];
299
300 if ((k_idx % 2) == 0) {
301 rhs_v0 |= v0_u8;
302 } else {
303 rhs_v0 |= (v0_u8 << 4);
304 }
305 rhs_qs4cx[dst_addr] = rhs_v0;
306 }
307
308 rhs_scales_f32[n_idx] = recip_scale0;
309 }
310 };
311
312 static void quant_kxn_qs4cx_f32(size_t n, size_t k, const float* rhs_f32, uint8_t* rhs_qs4cx, float* rhs_scales_f32) {
313 const size_t rhs_qs4cx_stride = (roundup(n, 2) / 2);
314
315 // Make sure the output is filled with zeros
316 std::memset(rhs_qs4cx, 0, k * rhs_qs4cx_stride);
317
318 for (size_t n_idx = 0; n_idx < n; ++n_idx) {
319 const float* src_ptr = rhs_f32 + n_idx * k;
320
321 float max0 = -FLT_MAX;
322 float min0 = FLT_MAX;
323
324 // Find min/max for each channel
325 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
326 const float src0_0 = src_ptr[k_idx];
327
328 max0 = std::max(src0_0, max0);
329 min0 = std::min(src0_0, min0);
330 }
331
332 // Maximum/minimum int8 values
333 const float qmin = (float)INT4_MIN;
334 const float qmax = (float)INT4_MAX;
335
336 const float rmin0 = std::min(0.0f, min0);
337 const float rmax0 = std::max(0.0f, max0);
338
339 const float scale0 = rmin0 == rmax0 ? 1.f : (qmax - qmin) / (rmax0 - rmin0);
340
341 // Reciprocal to quantize
342 const float recip_scale0 = scale0 ? 1.0f / scale0 : 0.0f;
343
344 // Quantize the channels
345 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
346 const float src0_0 = src_ptr[k_idx];
347
348 // Scale the values
349 int32_t v0_s32 = (int32_t)(round(src0_0 * scale0));
350
351 // Maximum/minimum int4 values
352 v0_s32 = std::max(v0_s32, INT4_MIN);
353 v0_s32 = std::min(v0_s32, INT4_MAX);
354
355 const uint8_t v0_u8 = (uint8_t)(v0_s32 + 8);
356
357 const size_t dst_addr = (n_idx / 2) + k_idx * rhs_qs4cx_stride;
358 uint8_t rhs_v0 = rhs_qs4cx[dst_addr];
359
360 if ((n_idx % 2) == 0) {
361 rhs_v0 |= v0_u8;
362 } else {
363 rhs_v0 |= (v0_u8 << 4);
364 }
365 rhs_qs4cx[dst_addr] = rhs_v0;
366 }
367
368 rhs_scales_f32[n_idx] = recip_scale0;
369 }
370 };
371
372 static void quant_qs4cx_f32(
373 size_t n, size_t k, rhs_format format, const float* rhs_f32, uint8_t* rhs_qs4cx, float* rhs_scales_f32) {
374 if (rhs_format::nxk == format) {
375 quant_nxk_qs4cx_f32(n, k, rhs_f32, rhs_qs4cx, rhs_scales_f32);
376 } else {
377 quant_kxn_qs4cx_f32(n, k, rhs_f32, rhs_qs4cx, rhs_scales_f32);
378 }
379 };
380
381 static void ref_matmul_mxn_mxk_nxk_f32_qa8dx_qs4cx(
382 size_t m, size_t n, size_t k, const int8_t* lhs_qa8dx, const uint8_t* rhs_qs4cx, const float* rhs_scales_f32,
383 float* dst_f32, float scalar_min, float scalar_max) {
384 const size_t lhs_stride = k * sizeof(int8_t) + sizeof(float) + sizeof(int32_t);
385
386 const size_t rhs_qs4cx_stride = (roundup(k, 2) / 2);
387
388 for (size_t m_idx = 0; m_idx < m; ++m_idx) {
389 const int8_t* lhs_ptr_start = lhs_qa8dx + m_idx * lhs_stride;
390
391 for (size_t n_idx = 0; n_idx < n; ++n_idx) {
392 // Main f32 accumulator
393 int32_t iacc = 0;
394
395 const int8_t* lhs_ptr = lhs_ptr_start;
396 const uint8_t* rhs_ptr = rhs_qs4cx + n_idx * rhs_qs4cx_stride;
397
398 // Get the LHS quantization parameters stored at the
399 // beginning of each row
400 const float lhs_scale = *(const float*)lhs_ptr;
401 lhs_ptr += sizeof(float);
402
403 const int32_t lhs_offset = *(const int32_t*)lhs_ptr;
404 lhs_ptr += sizeof(int32_t);
405
406 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
407 // Get the LHS values
408 const int32_t lhs_v0 = (int32_t)lhs_ptr[0];
409
410 // Get the RHS values
411 const uint8_t rhs_byte = rhs_ptr[0];
412
413 // Unpack the RHS values
414 int32_t rhs_v0 = 0;
415 if ((k_idx % 2) == 0) {
416 rhs_v0 = (((int32_t)(rhs_byte & 0x0F)) - 8);
417 } else {
418 rhs_v0 = (((int32_t)(rhs_byte >> 4)) - 8);
419 }
420
421 iacc += lhs_v0 * rhs_v0;
422 iacc += lhs_offset * rhs_v0;
423
424 lhs_ptr += 1;
425
426 // Increment only when k_idx is not a multiple of 2
427 rhs_ptr += k_idx % 2;
428 }
429
430 // Get the RHS scale
431 const float rhs_scale = rhs_scales_f32[n_idx];
432
433 float main_acc = iacc * rhs_scale;
434
435 main_acc = main_acc * lhs_scale;
436
437 // Clamp (min-max) operation
438 main_acc = std::max(main_acc, scalar_min);
439 main_acc = std::min(main_acc, scalar_max);
440
441 dst_f32[0] = main_acc;
442 dst_f32 += 1;
443 }
444 }
445 };
446
447 static void ref_matmul_mxn_mxk_kxn_f32_qa8dx_qs4cx(
448 size_t m, size_t n, size_t k, const int8_t* lhs_qa8dx, const uint8_t* rhs_qs4cx, const float* rhs_scales_f32,
449 float* dst_f32, float scalar_min, float scalar_max) {
450 const size_t lhs_stride = k * sizeof(int8_t) + sizeof(float) + sizeof(int32_t);
451
452 const size_t rhs_qs4cx_stride = (roundup(n, 2) / 2);
453
454 for (size_t m_idx = 0; m_idx < m; ++m_idx) {
455 const int8_t* lhs_ptr_start = lhs_qa8dx + m_idx * lhs_stride;
456
457 for (size_t n_idx = 0; n_idx < n; ++n_idx) {
458 // Main f32 accumulator
459 int32_t iacc = 0;
460
461 const int8_t* lhs_ptr = lhs_ptr_start;
462 const uint8_t* rhs_ptr = rhs_qs4cx + (n_idx / 2);
463
464 // Get the LHS quantization parameters stored at the
465 // beginning of each row
466 const float lhs_scale = *(const float*)lhs_ptr;
467 lhs_ptr += sizeof(float);
468
469 const int32_t lhs_offset = *(const int32_t*)lhs_ptr;
470 lhs_ptr += sizeof(int32_t);
471
472 for (size_t k_idx = 0; k_idx < k; ++k_idx) {
473 // Get the LHS values
474 const int32_t lhs_v0 = (int32_t)lhs_ptr[0];
475
476 // Get the RHS values
477 const uint8_t rhs_byte = rhs_ptr[0];
478
479 // Unpack the RHS values
480 int32_t rhs_v0 = 0;
481 if ((n_idx % 2) == 0) {
482 rhs_v0 = (((int32_t)(rhs_byte & 0x0F)) - 8);
483 } else {
484 rhs_v0 = (((int32_t)(rhs_byte >> 4)) - 8);
485 }
486
487 iacc += lhs_v0 * rhs_v0;
488 iacc += lhs_offset * rhs_v0;
489
490 lhs_ptr += 1;
491
492 // Increment only when k_idx is not a multiple of 2
493 rhs_ptr += rhs_qs4cx_stride;
494 }
495
496 // Get the RHS scale
497 const float rhs_scale = rhs_scales_f32[n_idx];
498
499 float main_acc = iacc * rhs_scale;
500
501 main_acc = main_acc * lhs_scale;
502
503 // Clamp (min-max) operation
504 main_acc = std::max(main_acc, scalar_min);
505 main_acc = std::min(main_acc, scalar_max);
506
507 dst_f32[0] = main_acc;
508 dst_f32 += 1;
509 }
510 }
511 };
512
513 static void ref_matmul_f32_qa8dx_qs4cx(
514 size_t m, size_t n, size_t k, rhs_format format, const int8_t* lhs_qa8dx, const uint8_t* rhs_qs4cx,
515 const float* rhs_scales_f32, float* dst_f32, float scalar_min, float scalar_max) {
516 const size_t lhs_stride = k * sizeof(int8_t) + sizeof(float) + sizeof(int32_t);
517
518 if (rhs_format::nxk == format) {
519 ref_matmul_mxn_mxk_nxk_f32_qa8dx_qs4cx(
520 m, n, k, lhs_qa8dx, rhs_qs4cx, rhs_scales_f32, dst_f32, scalar_min, scalar_max);
521 } else {
522 ref_matmul_mxn_mxk_kxn_f32_qa8dx_qs4cx(
523 m, n, k, lhs_qa8dx, rhs_qs4cx, rhs_scales_f32, dst_f32, scalar_min, scalar_max);
524 }
525 };
526
527 static bool is_output_correct(size_t num_rows, size_t num_cols, float tolerance, const float* ref, const float* act) {
528 bool is_valid = true;
529
530 for (size_t i = 0; i < num_rows * num_cols; ++i) {
531 if (std::fabs(ref[i] - act[i]) > tolerance) {
532 const size_t x = i % num_cols;
533 const size_t y = i / num_cols;
534 printf("ERROR![%ld][%ld]: ref=%.5f vs. act=%.5f\n", y, x, ref[i], act[i]);
535 is_valid = false;
536 return is_valid;
537 }
538 }
539 return is_valid;
540 }
541
542 int main(int argc, char** argv) {
543 const size_t seed_lhs = 4568;
544 const size_t seed_rhs = seed_lhs + 4;
545 const size_t num_shapes = std::size(matmul_shapes);
546
547 std::cout << "------------" << std::endl;
548 for (size_t test_idx = 0; test_idx < num_shapes; ++test_idx) {
549 size_t m = matmul_shapes[test_idx].m;
550 size_t n = matmul_shapes[test_idx].n;
551 size_t k = matmul_shapes[test_idx].k;
552
553 std::cout << "\nTEST[" << m << ", " << n << "," << k << "]"
554 << "\n";
555 // Iterate over the RHS format (NxK or KxN)
556 for (const rhs_format& format : {rhs_format::nxk, rhs_format::kxn}) {
557 std::cout << "Testing RHS format = " << (format == rhs_format::nxk ? "N x K" : "K x N") << std::endl;
558
559 const size_t lhs_native_size_f32 = m * k * sizeof(float);
560 const size_t rhs_native_size_f32 = n * k * sizeof(float);
561 const size_t rhs_native_size_qs4cx = format == rhs_format::nxk ? n * (roundup(k, 2) / 2) * sizeof(uint8_t)
562 : k * (roundup(n, 2) / 2) * sizeof(uint8_t);
563 const size_t rhs_scales_size_f32 = n * sizeof(float);
564
565 // Allocate the memory
566 uint8_t* lhs_native_mtx_f32 = new uint8_t[lhs_native_size_f32];
567 uint8_t* rhs_native_mtx_f32 = new uint8_t[rhs_native_size_f32];
568 uint8_t* rhs_native_mtx_qs4cx = new uint8_t[rhs_native_size_qs4cx];
569 uint8_t* rhs_scales_f32 = new uint8_t[rhs_scales_size_f32];
570
571 fill_uniform_random(m, k, (float*)lhs_native_mtx_f32, seed_lhs);
572 fill_uniform_random(n, k, (float*)rhs_native_mtx_f32, seed_rhs);
573
574 quant_qs4cx_f32(
575 n, k, format, (const float*)rhs_native_mtx_f32, (uint8_t*)rhs_native_mtx_qs4cx, (float*)rhs_scales_f32);
576
577 delete[] rhs_native_mtx_f32;
578
579 //----------- REFERENCE IMPLEMENTATION
580 //------------------------------------
581 //------------------------------------
582 // Memory sizes for the reference implementation
583 // After dynamically quantized the LHS matrix, we have the scale and offset for each
584 // row. The scale (f32) and offset (int32) are stored at the beginning of each row
585 const size_t lhs_ref_size_qa8dx = m * (k + sizeof(int32_t) + sizeof(float));
586 const size_t dst_ref_size_f32 = m * n * sizeof(float);
587
588 uint8_t* lhs_ref_mtx_qa8dx = new uint8_t[lhs_ref_size_qa8dx];
589 uint8_t* dst_ref_mtx_f32 = new uint8_t[dst_ref_size_f32];
590
591 ref_quant_qa8dx_f32(m, k, (const float*)lhs_native_mtx_f32, (int8_t*)lhs_ref_mtx_qa8dx);
592
593 ref_matmul_f32_qa8dx_qs4cx(
594 m, n, k, format, (const int8_t*)lhs_ref_mtx_qa8dx, (const uint8_t*)rhs_native_mtx_qs4cx,
595 (const float*)rhs_scales_f32, (float*)dst_ref_mtx_f32, -FLT_MAX, FLT_MAX);
596
597 // Remove the unnecessary buffer
598 delete[] lhs_ref_mtx_qa8dx;
599
600 //----------- END REFERENCE IMPLEMENTATION
601 //------------------------------------
602 //------------------------------------
603
604 //----------- MICRO-KERNELS TESTS
605 //------------------------------------
606 //------------------------------------
607 for (size_t idx_variant = 0; idx_variant < num_ukernel_variants; ++idx_variant) {
608 std::cout << "Testing " << ukernel_variants[idx_variant].name << std::endl;
609
610 // Get the packing parameters
611 const size_t mr = ukernel_variants[idx_variant].ukernel.get_mr();
612 const size_t nr = ukernel_variants[idx_variant].ukernel.get_nr();
613 const size_t kr = ukernel_variants[idx_variant].ukernel.get_kr();
614 const size_t sr = ukernel_variants[idx_variant].ukernel.get_sr();
615
616 // Get the size in bytes for the packed matrices
617 const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(m, k, mr, kr, sr);
618 size_t rhs_packed_size = 0;
619
620 if (format == rhs_format::nxk) {
621 rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(n, k, nr, kr, sr);
622 } else {
623 rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_kxn_qsi4cxp_qs4cxs1s0(n, k, nr, kr, sr);
624 }
625
626 const size_t dst_size = ukernel_variants[idx_variant].ukernel.get_dst_size(m, n);
627
628 // Allocate the matrices
629 uint8_t* lhs_packed_mtx_qa8dx = new uint8_t[lhs_packed_size];
630 uint8_t* rhs_packed_mtx_qs4cx = new uint8_t[rhs_packed_size];
631 uint8_t* dst_act_mtx_f32 = new uint8_t[dst_size];
632
633 // If the RHS matrix contains constant values, the packing can be performed
634 // only once
635 if (format == rhs_format::nxk) {
636 struct kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0_params nxk_params;
637
638 nxk_params.lhs_zero_point = 1;
639 nxk_params.rhs_zero_point = 8;
640 // RHS packing
641 kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(
642 1, n, k, nr, kr, sr, // Packing arguments
643 (const uint8_t*)(rhs_native_mtx_qs4cx), // RHS
644 NULL, // Bias
645 (const float*)(rhs_scales_f32), // Scale
646 rhs_packed_mtx_qs4cx, // RHS packed
647 0, &nxk_params);
648 } else {
649 struct kai_rhs_pack_kxn_qsi4cxp_qs4cxs1s0_params kxn_params;
650 kxn_params.lhs_zero_point = 1;
651 kxn_params.rhs_zero_point = 8;
652 // RHS packing
653 kai_run_rhs_pack_kxn_qsi4cxp_qs4cxs1s0(
654 1, n, k, nr, kr, sr, // Packing arguments
655 (const uint8_t*)(rhs_native_mtx_qs4cx), // RHS
656 NULL, // Bias
657 (const float*)(rhs_scales_f32), // Scale
658 rhs_packed_mtx_qs4cx, // RHS packed
659 0, &kxn_params);
660 }
661
662 // LHS packing
663 kai_run_lhs_quant_pack_qai8dxp_f32(
664 m, k, mr, kr, sr, 0, // Packing arguments
665 (const float*)lhs_native_mtx_f32, // LHS
666 k * sizeof(float), // LHS stride
667 lhs_packed_mtx_qa8dx); // LHS packed
668
669 // Matmul
670 {
671 const size_t dst_stride = n * sizeof(float);
672 const size_t lhs_offset = ukernel_variants[idx_variant].ukernel.get_lhs_packed_offset(0, k);
673 const size_t rhs_offset = ukernel_variants[idx_variant].ukernel.get_rhs_packed_offset(0, k);
674 const size_t dst_offset = ukernel_variants[idx_variant].ukernel.get_dst_offset(0, 0, dst_stride);
675
676 const void* lhs_ptr = (const void*)((const char*)lhs_packed_mtx_qa8dx + lhs_offset);
677 const void* rhs_ptr = (const void*)((const char*)rhs_packed_mtx_qs4cx + rhs_offset);
678 float* dst_ptr = (float*)((uint8_t*)dst_act_mtx_f32 + dst_offset);
679
680 ukernel_variants[idx_variant].ukernel.run_matmul(
681 m, n, k, // Dimensions
682 lhs_ptr, // LHS packed
683 rhs_ptr, // RHS packed
684 dst_ptr, // DST
685 dst_stride, // DST stride (row)
686 sizeof(float), // DST stride (col)
687 -FLT_MAX, FLT_MAX // Min and max for the clamp operation
688 );
689 }
690
691 const bool is_valid =
692 is_output_correct(m, n, 0.0001f, (const float*)dst_ref_mtx_f32, (const float*)dst_act_mtx_f32);
693
694 if (is_valid) {
695 printf("TEST[%ld] = PASSED\n", idx_variant);
696 } else {
697 printf("TEST[%ld] = FAILED\n", idx_variant);
698 }
699
700 delete[] lhs_packed_mtx_qa8dx;
701 delete[] rhs_packed_mtx_qs4cx;
702 delete[] dst_act_mtx_f32;
703 }
704 delete[] lhs_native_mtx_f32;
705 delete[] rhs_native_mtx_qs4cx;
706 delete[] rhs_scales_f32;
707 delete[] dst_ref_mtx_f32;
708 }
709 }
710 }
711
712 //----------- END MICRO-KERNELS TESTS
713 //------------------------------------
714 //------------------------------------
715
716 #endif // Architectural feature check