Introduce new Kleidiai kernels with baseline and sqnr calibrated workloads
#9
by ArtificialRay7579 - opened
This view is limited to 50 files because it contains too many changes. See the raw diff here.
- README.md +4 -4
- TASKS.md +24 -10
- definitions/conv2d/conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2.json +77 -0
- definitions/gemm/gemm_bf16_n512_k512.json +40 -10
- definitions/gemm/gemm_f16_qai4c32_sme2_n256_k512.json +77 -0
- definitions/gemm/gemm_f16_qs4cx_sme2_n256_k512.json +65 -0
- definitions/gemm/gemm_f16_qs8cx_sme2_n256_k512.json +61 -0
- definitions/gemm/gemm_f16act_qs4c32_sme2_n256_k512.json +64 -0
- definitions/gemm/gemm_f32_bf16_n512_k512.json +54 -0
- definitions/gemm/gemm_i32_u8_sme2_n256_k512.json +48 -0
- definitions/gemm/gemm_qai4c32_n256_k512.json +77 -0
- definitions/gemm/gemm_qai4c32_sme2_n256_k512.json +77 -0
- definitions/gemm/gemm_qai8_qsi8cx_sme2_n256_k512.json +76 -0
- definitions/gemm/gemm_qai8dxp_qs4c32_sme2_n256_k512.json +70 -0
- definitions/gemm/gemm_qs4c32_sme2_n256_k512.json +64 -0
- definitions/gemm/gemm_qs4cx_n512_k512.json +65 -0
- definitions/gemm/gemm_qs8cx_n512_k512.json +61 -0
- definitions/gemm/gemm_qs8cx_sme2_n256_k512.json +61 -0
- definitions/gemm/gemm_qsu2cx_n256_k512.json +65 -0
- definitions/gemm/gemm_qsu2cx_sme2_n256_k512.json +65 -0
- definitions/gemm/gemm_u8_f32_sme2_n256_k512.json +64 -0
- expected_sets_sve.json +19 -1
- solutions/kleidiai/baseline-kleidiai-arm/conv2d/conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2.json +76 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f16_qai4c32_sme2_n256_k512.json +68 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f16_qs4cx_sme2_n256_k512.json +68 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f16_qs8cx_sme2_n256_k512.json +68 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f16act_qs4c32_sme2_n256_k512.json +68 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f32_bf16_n512_k512.json +0 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_i32_u8_sme2_n256_k512.json +0 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qai4c32_n256_k512.json +75 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qai4c32_sme2_n256_k512.json +68 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qai8_qsi8cx_sme2_n256_k512.json +76 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qai8dxp_qs4c32_sme2_n256_k512.json +68 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qs4c32_sme2_n256_k512.json +64 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qs4cx_n512_k512.json +67 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qs8cx_n512_k512.json +71 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qs8cx_sme2_n256_k512.json +68 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qsu2cx_n256_k512.json +0 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qsu2cx_sme2_n256_k512.json +68 -0
- solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_u8_f32_sme2_n256_k512.json +0 -0
- solutions/kleidiai/reference-scalar/conv2d/conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_f16_qai4c32_sme2_n256_k512.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_f16_qs4cx_sme2_n256_k512.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_f16_qs8cx_sme2_n256_k512.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_f16act_qs4c32_sme2_n256_k512.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_f32_bf16_n512_k512.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_i32_u8_sme2_n256_k512.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_qai4c32_n256_k512.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_qai4c32_sme2_n256_k512.json +36 -0
- solutions/kleidiai/reference-scalar/gemm/gemm_qai8_qsi8cx_sme2_n256_k512.json +36 -0
README.md
CHANGED
|
@@ -35,12 +35,12 @@ kernel is checked against the reference and timed against the baseline.
|
|
| 35 |
| ncnn | 23 | conv2d, conv2d_depthwise, pooling | `baseline-ncnn-arm` |
|
| 36 |
| llama.cpp | 26 | gemm, moe, mha, gqa, mla_decode, mla_prefill, rms_norm | `baseline-llamacpp-arm` |
|
| 37 |
| SIMD Loops | 47 | one loop per definition | `baseline-sve`, `baseline-sve2`, `baseline-sme2` |
|
| 38 |
-
| KleidiAI |
|
| 39 |
| e2e | 18 | gemm on q4_K / q5_K / q6_K weights of Llama-3.1-8B and Qwen3.5-4B | `baseline-llamacpp-arm` |
|
| 40 |
-
| **Total** | **
|
| 41 |
|
| 42 |
-
- `expected_sets_sve.json`: the
|
| 43 |
-
llama.cpp, 26 SIMD Loops,
|
| 44 |
- `subset_sve.json` / `subset_sme.json`: a 32-definition subset for the SVE
|
| 45 |
and SME2 tiers; they differ only in the KleidiAI GEMM.
|
| 46 |
|
|
|
|
| 35 |
| ncnn | 23 | conv2d, conv2d_depthwise, pooling | `baseline-ncnn-arm` |
|
| 36 |
| llama.cpp | 26 | gemm, moe, mha, gqa, mla_decode, mla_prefill, rms_norm | `baseline-llamacpp-arm` |
|
| 37 |
| SIMD Loops | 47 | one loop per definition | `baseline-sve`, `baseline-sve2`, `baseline-sme2` |
|
| 38 |
+
| KleidiAI | 28 | gemm, conv2d, conv2d_depthwise | `baseline-kleidiai-arm` |
|
| 39 |
| e2e | 18 | gemm on q4_K / q5_K / q6_K weights of Llama-3.1-8B and Qwen3.5-4B | `baseline-llamacpp-arm` |
|
| 40 |
+
| **Total** | **142** | | |
|
| 41 |
|
| 42 |
+
- `expected_sets_sve.json`: the 103 evaluated definitions (23 ncnn, 26
|
| 43 |
+
llama.cpp, 26 SIMD Loops, 28 KleidiAI).
|
| 44 |
- `subset_sve.json` / `subset_sme.json`: a 32-definition subset for the SVE
|
| 45 |
and SME2 tiers; they differ only in the KleidiAI GEMM.
|
| 46 |
|
TASKS.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# Tasks and baselines
|
| 2 |
|
| 3 |
-
Every definition in this dataset, with the expert baseline it is scored against on each hardware tier: whether it runs there, and whether it is specialized for that tier. Generated 2026-10-02 from the dataset, the recorded runs and a check of each baseline's code; the SME2 column was updated on 2026-10-20 from a check of every SIMD Loops baseline on M4, `loop_216` on 2026-10-29 after a tolerance fix in the code repository (PRs #111 and #115), and `loop_032` on 2026-10-29 (`baseline-sme2` removed).
|
| 4 |
|
| 5 |
| Mark | Meaning |
|
| 6 |
|---|---|
|
|
@@ -14,7 +14,7 @@ Tiers: **SVE** = Graviton3 (c7g), **SVE2** = Graviton4 (c8g), **SME2** = Apple M
|
|
| 14 |
|
| 15 |
How each source's baseline is chosen and marked is explained under *Notes on the baselines* at the end.
|
| 16 |
|
| 17 |
-
**Eval set** = the
|
| 18 |
|
| 19 |
## Summary
|
| 20 |
|
|
@@ -23,9 +23,9 @@ How each source's baseline is chosen and marked is explained under *Notes on the
|
|
| 23 |
| ncnn | 23 | 0 / 23 / 0 / 0 / 0 | 0 / 23 / 0 / 0 / 0 | 0 / 23 / 0 / 0 / 0 |
|
| 24 |
| llama.cpp | 26 | 0 / 26 / 0 / 0 / 0 | 0 / 26 / 0 / 0 / 0 | 0 / 26 / 0 / 0 / 0 |
|
| 25 |
| SIMD Loops | 47 | 27 / 0 / 0 / 3 / 17 | 11 / 18 / 0 / 7 / 11 | 7 / 16 / 0 / 8 / 16 |
|
| 26 |
-
| KleidiAI |
|
| 27 |
| e2e (llama.cpp GEMMs) | 18 | 0 / 18 / 0 / 0 / 0 | 0 / 18 / 0 / 0 / 0 | 0 / 18 / 0 / 0 / 0 |
|
| 28 |
-
| **All** | **
|
| 29 |
|
| 30 |
## ncnn (23)
|
| 31 |
|
|
@@ -140,9 +140,7 @@ Loops without the Eval set mark are in the dataset but not in the main results.
|
|
| 140 |
| `loop_221` | | 10 | | | — | — | ★ |
|
| 141 |
| `loop_223` | | 10 | | | — | — | ★ |
|
| 142 |
|
| 143 |
-
## KleidiAI (
|
| 144 |
-
|
| 145 |
-
`gemm_qs4c32_n256_k512` needs 256-bit SVE, which Graviton4 (128-bit) does not have.
|
| 146 |
|
| 147 |
| Definition | Op | Workloads | Eval set | Subset | SVE | SVE2 | SME2 |
|
| 148 |
|---|---|---|---|---|---|---|---|
|
|
@@ -150,12 +148,30 @@ Loops without the Eval set mark are in the dataset but not in the main results.
|
|
| 150 |
| `conv2d_depthwise_fp32_nhwc_kh3_kw3_s1_p1` | conv2d_depthwise | 20 | ✓ | | — | — | ★ |
|
| 151 |
| `conv2d_f16_nhwc_kh3_kw3_valid_c32_c64` | conv2d | 20 | ✓ | | — | — | ★ |
|
| 152 |
| `conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64` | conv2d | 16 | ✓ | | ★ | ✓ | — |
|
|
|
|
| 153 |
| `gemm_bf16_n512_k512` | gemm | 15 | ✓ | | — | — | ★ |
|
| 154 |
| `gemm_f16_n512_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
| `gemm_fp32_n512_k512` | gemm | 13 | ✓ | ✓ | ★ | ✓ | — |
|
| 156 |
| `gemm_fp32_sme2_n512_k512` | gemm | 13 | ✓ | ✓ | — | — | ★ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
| `gemm_qs4c32_n256_k512` | gemm | 13 | ✓ | | ★ | — | — |
|
|
|
|
|
|
|
| 158 |
| `gemm_qs4cx_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
## e2e (llama.cpp GEMMs) (18)
|
| 161 |
|
|
@@ -208,7 +224,5 @@ These definitions load only with the e2e branch for now (code repo issue #94).
|
|
| 208 |
- **ncnn** → `baseline-ncnn-arm`, the upstream ncnn kernel, the same on every tier. ncnn is built with its SVE options off, so this is NEON code everywhere: ✓, never ★.
|
| 209 |
- **llama.cpp and e2e** → `baseline-llamacpp-arm`, the ggml CPU kernel built natively on each machine. Which kernels reach ggml's SVE code has not been checked, so these are ✓; ggml's KleidiAI path is off. ggml picks its kernel by op, data type and CPU features, not by shape, so a cell with no recorded run is ✓ when the same op and data type ran on that machine (8 llama.cpp definitions on Graviton4). e2e has recorded runs only on Graviton4 and is ✓ on Graviton3 and M4 for the same reason (q4_K ran on both in the llama.cpp set; q5_K and q6_K have not).
|
| 210 |
- **SIMD Loops** → one hand-written Arm baseline per tier. `baseline-sve2` uses SVE2-only instructions in some loops (★) and is SVE code in the rest (✓). Of the 31 `baseline-sme2`, 24 are SVE code run in streaming mode (`__arm_locally_streaming`, tag `sme2-kernel:streaming-sve`) and 7 use ZA (tag `sme2-kernel:za`: the matrix loops 216–221 and 223). Streaming SVE counts as ✓, not ★, because it uses nothing SME adds (ZA, outer products, SME2 multi-vector instructions); the ZA ones are ★. All 32 were run on M4 on 2026-10-20 (3 passes on every workload, code at main bc8c53d): `loop_032` cannot run there, `loop_216` failed 2 workloads at the default tolerance and passes since its tolerance fix (code repository PRs #111 and #115), and the other 30 pass every workload; the 2x check against the auto-vectorized starting point flags 8 of them, which are ✗ (see the notes below). `loop_032` has no SME2 code upstream, and under `-mcpu=apple-m4` its source falls back to NEON, which streaming mode rejects on M4, so its `baseline-sme2` was removed (—).
|
| 211 |
-
- **KleidiAI** → `baseline-kleidiai-arm`: each kernel uses either an SVE micro-kernel (
|
| 212 |
-
|
| 213 |
-
The SVE2 column of SIMD Loops (including the notes on ✗) and of the KleidiAI SVE kernels comes from running each baseline directly on a Graviton4 (c8g) on 2026-10-02. On 2026-10-03 the SIMD Loops and KleidiAI baselines were also checked on Graviton3 and Graviton4 against the inclusion rule that a baseline be at most 2x slower than the scalar starting point auto-vectorized for that machine; every failure is in the notes on ✗.
|
| 214 |
|
|
|
|
| 1 |
# Tasks and baselines
|
| 2 |
|
| 3 |
+
Every definition in this dataset, with the expert baseline it is scored against on each hardware tier: whether it runs there, and whether it is specialized for that tier. Generated 2026-10-02 from the dataset, the recorded runs and a check of each baseline's code; the SME2 column was updated on 2026-10-20 from a check of every SIMD Loops baseline on M4, `loop_216` on 2026-10-29 after a tolerance fix in the code repository (PRs #111 and #115), and `loop_032` on 2026-10-29 (`baseline-sme2` removed). The KleidiAI section was extended on 2026-10-31 with 18 definitions (5 for the Graviton tiers, 13 for M4).
|
| 4 |
|
| 5 |
| Mark | Meaning |
|
| 6 |
|---|---|
|
|
|
|
| 14 |
|
| 15 |
How each source's baseline is chosen and marked is explained under *Notes on the baselines* at the end.
|
| 16 |
|
| 17 |
+
**Eval set** = the 103 evaluated kernels listed in `expected_sets_sve.json` (23 ncnn, 26 llama.cpp, 26 SIMD Loops, 28 KleidiAI; the 20 SME2-only KleidiAI kernels are evaluated on M4). **Subset** = the 32-kernel subset; `subset_sve.json` and `subset_sme.json` differ only in the KleidiAI GEMM (`gemm_fp32_n512_k512` vs `gemm_fp32_sme2_n512_k512`), so 33 rows carry the mark.
|
| 18 |
|
| 19 |
## Summary
|
| 20 |
|
|
|
|
| 23 |
| ncnn | 23 | 0 / 23 / 0 / 0 / 0 | 0 / 23 / 0 / 0 / 0 | 0 / 23 / 0 / 0 / 0 |
|
| 24 |
| llama.cpp | 26 | 0 / 26 / 0 / 0 / 0 | 0 / 26 / 0 / 0 / 0 | 0 / 26 / 0 / 0 / 0 |
|
| 25 |
| SIMD Loops | 47 | 27 / 0 / 0 / 3 / 17 | 11 / 18 / 0 / 7 / 11 | 7 / 16 / 0 / 8 / 16 |
|
| 26 |
+
| KleidiAI | 28 | 3 / 5 / 0 / 0 / 20 | 0 / 7 / 0 / 0 / 21 | 20 / 0 / 0 / 0 / 8 |
|
| 27 |
| e2e (llama.cpp GEMMs) | 18 | 0 / 18 / 0 / 0 / 0 | 0 / 18 / 0 / 0 / 0 | 0 / 18 / 0 / 0 / 0 |
|
| 28 |
+
| **All** | **142** | 30 / 72 / 0 / 3 / 37 | 11 / 92 / 0 / 7 / 32 | 27 / 83 / 0 / 8 / 24 |
|
| 29 |
|
| 30 |
## ncnn (23)
|
| 31 |
|
|
|
|
| 140 |
| `loop_221` | | 10 | | | — | — | ★ |
|
| 141 |
| `loop_223` | | 10 | | | — | — | ★ |
|
| 142 |
|
| 143 |
+
## KleidiAI (28)
|
|
|
|
|
|
|
| 144 |
|
| 145 |
| Definition | Op | Workloads | Eval set | Subset | SVE | SVE2 | SME2 |
|
| 146 |
|---|---|---|---|---|---|---|---|
|
|
|
|
| 148 |
| `conv2d_depthwise_fp32_nhwc_kh3_kw3_s1_p1` | conv2d_depthwise | 20 | ✓ | | — | — | ★ |
|
| 149 |
| `conv2d_f16_nhwc_kh3_kw3_valid_c32_c64` | conv2d | 20 | ✓ | | — | — | ★ |
|
| 150 |
| `conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64` | conv2d | 16 | ✓ | | ★ | ✓ | — |
|
| 151 |
+
| `conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2` | conv2d | 16 | ✓ | | — | — | ★ |
|
| 152 |
| `gemm_bf16_n512_k512` | gemm | 15 | ✓ | | — | — | ★ |
|
| 153 |
| `gemm_f16_n512_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 154 |
+
| `gemm_f16_qai4c32_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 155 |
+
| `gemm_f16_qs4cx_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 156 |
+
| `gemm_f16_qs8cx_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 157 |
+
| `gemm_f16act_qs4c32_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 158 |
+
| `gemm_f32_bf16_n512_k512` | gemm | 13 | ✓ | | ✓ | ✓ | — |
|
| 159 |
| `gemm_fp32_n512_k512` | gemm | 13 | ✓ | ✓ | ★ | ✓ | — |
|
| 160 |
| `gemm_fp32_sme2_n512_k512` | gemm | 13 | ✓ | ✓ | — | — | ★ |
|
| 161 |
+
| `gemm_i32_u8_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 162 |
+
| `gemm_qai4c32_n256_k512` | gemm | 13 | ✓ | | ✓ | ✓ | — |
|
| 163 |
+
| `gemm_qai4c32_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 164 |
+
| `gemm_qai8_qsi8cx_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 165 |
+
| `gemm_qai8dxp_qs4c32_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 166 |
| `gemm_qs4c32_n256_k512` | gemm | 13 | ✓ | | ★ | — | — |
|
| 167 |
+
| `gemm_qs4c32_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 168 |
+
| `gemm_qs4cx_n512_k512` | gemm | 13 | ✓ | | ✓ | ✓ | — |
|
| 169 |
| `gemm_qs4cx_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 170 |
+
| `gemm_qs8cx_n512_k512` | gemm | 13 | ✓ | | ✓ | ✓ | — |
|
| 171 |
+
| `gemm_qs8cx_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 172 |
+
| `gemm_qsu2cx_n256_k512` | gemm | 13 | ✓ | | ✓ | ✓ | — |
|
| 173 |
+
| `gemm_qsu2cx_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 174 |
+
| `gemm_u8_f32_sme2_n256_k512` | gemm | 13 | ✓ | | — | — | ★ |
|
| 175 |
|
| 176 |
## e2e (llama.cpp GEMMs) (18)
|
| 177 |
|
|
|
|
| 224 |
- **ncnn** → `baseline-ncnn-arm`, the upstream ncnn kernel, the same on every tier. ncnn is built with its SVE options off, so this is NEON code everywhere: ✓, never ★.
|
| 225 |
- **llama.cpp and e2e** → `baseline-llamacpp-arm`, the ggml CPU kernel built natively on each machine. Which kernels reach ggml's SVE code has not been checked, so these are ✓; ggml's KleidiAI path is off. ggml picks its kernel by op, data type and CPU features, not by shape, so a cell with no recorded run is ✓ when the same op and data type ran on that machine (8 llama.cpp definitions on Graviton4). e2e has recorded runs only on Graviton4 and is ✓ on Graviton3 and M4 for the same reason (q4_K ran on both in the llama.cpp set; q5_K and q6_K have not).
|
| 226 |
- **SIMD Loops** → one hand-written Arm baseline per tier. `baseline-sve2` uses SVE2-only instructions in some loops (★) and is SVE code in the rest (✓). Of the 31 `baseline-sme2`, 24 are SVE code run in streaming mode (`__arm_locally_streaming`, tag `sme2-kernel:streaming-sve`) and 7 use ZA (tag `sme2-kernel:za`: the matrix loops 216–221 and 223). Streaming SVE counts as ✓, not ★, because it uses nothing SME adds (ZA, outer products, SME2 multi-vector instructions); the ZA ones are ★. All 32 were run on M4 on 2026-10-20 (3 passes on every workload, code at main bc8c53d): `loop_032` cannot run there, `loop_216` failed 2 workloads at the default tolerance and passes since its tolerance fix (code repository PRs #111 and #115), and the other 30 pass every workload; the 2x check against the auto-vectorized starting point flags 8 of them, which are ✗ (see the notes below). `loop_032` has no SME2 code upstream, and under `-mcpu=apple-m4` its source falls back to NEON, which streaming mode rejects on M4, so its `baseline-sme2` was removed (—).
|
| 227 |
+
- **KleidiAI** → `baseline-kleidiai-arm`: each kernel uses either an SVE micro-kernel (`gemm_fp32`, `gemm_qs4c32`, `conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64`), a NEON dot-product / i8mm / bf16 one (the 5 quantized and bf16 GEMMs added on 2026-10-31: ✓ on Graviton3 and Graviton4, not specialized for either), or an SME2 one (M4), not a mix. The 13 SME2 definitions added on that date run the SME2 `mopa` micro-kernels (★); `gemm_i32_u8` and `gemm_u8_f32` use KleidiAI's newer `kai_matmul_uker` API.
|
|
|
|
|
|
|
| 228 |
|
definitions/conv2d/conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2",
|
| 3 |
+
"op_type": "conv2d",
|
| 4 |
+
"description": "'Same' conv2d (Kh=Kw=3, stride=1, pad=1, so H_out=H/W_out=W) in NHWC input / HWIO weight layout (channels-last) \u2014 distinct from this repo's other conv2d definitions (NCHW-style, via ncnn's Mat convention). Chosen deliberately to match Arm KleidiAI's imatmul indirection-buffer convention directly (no transpose needed in the baseline binding, unlike gemm_fp32_n512_k512). SME2-tier counterpart of conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64 (same operator and ABI; baseline is KleidiAI's SME2 imatmul kernel kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa instead of the SVE one).",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai"
|
| 8 |
+
],
|
| 9 |
+
"axes": {
|
| 10 |
+
"N": {
|
| 11 |
+
"type": "var"
|
| 12 |
+
},
|
| 13 |
+
"H": {
|
| 14 |
+
"type": "var"
|
| 15 |
+
},
|
| 16 |
+
"W": {
|
| 17 |
+
"type": "var"
|
| 18 |
+
},
|
| 19 |
+
"C_in": {
|
| 20 |
+
"type": "const",
|
| 21 |
+
"value": 32
|
| 22 |
+
},
|
| 23 |
+
"C_out": {
|
| 24 |
+
"type": "const",
|
| 25 |
+
"value": 64
|
| 26 |
+
},
|
| 27 |
+
"Kh": {
|
| 28 |
+
"type": "const",
|
| 29 |
+
"value": 3
|
| 30 |
+
},
|
| 31 |
+
"Kw": {
|
| 32 |
+
"type": "const",
|
| 33 |
+
"value": 3
|
| 34 |
+
},
|
| 35 |
+
"pad": {
|
| 36 |
+
"type": "const",
|
| 37 |
+
"value": 1
|
| 38 |
+
},
|
| 39 |
+
"stride": {
|
| 40 |
+
"type": "const",
|
| 41 |
+
"value": 1
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"inputs": {
|
| 45 |
+
"input": {
|
| 46 |
+
"shape": [
|
| 47 |
+
"N",
|
| 48 |
+
"H",
|
| 49 |
+
"W",
|
| 50 |
+
"C_in"
|
| 51 |
+
],
|
| 52 |
+
"dtype": "float32"
|
| 53 |
+
},
|
| 54 |
+
"weight": {
|
| 55 |
+
"shape": [
|
| 56 |
+
"Kh",
|
| 57 |
+
"Kw",
|
| 58 |
+
"C_in",
|
| 59 |
+
"C_out"
|
| 60 |
+
],
|
| 61 |
+
"dtype": "float32"
|
| 62 |
+
}
|
| 63 |
+
},
|
| 64 |
+
"outputs": {
|
| 65 |
+
"output": {
|
| 66 |
+
"shape": [
|
| 67 |
+
"N",
|
| 68 |
+
"H",
|
| 69 |
+
"W",
|
| 70 |
+
"C_out"
|
| 71 |
+
],
|
| 72 |
+
"dtype": "float32"
|
| 73 |
+
}
|
| 74 |
+
},
|
| 75 |
+
"constraints": [],
|
| 76 |
+
"reference": "import numpy as np\ndef run(input, weight):\n N, H, W, C_in = input.shape\n Kh, Kw, _, C_out = weight.shape\n pad = 1\n padded = np.pad(input, ((0, 0), (pad, pad), (pad, pad), (0, 0)))\n out = np.zeros((N, H, W, C_out), dtype=np.float32)\n for n in range(N):\n for oh in range(H):\n for ow in range(W):\n patch = padded[n, oh:oh + Kh, ow:ow + Kw, :]\n out[n, oh, ow, :] = np.tensordot(patch, weight, axes=([0, 1, 2], [0, 1, 2]))\n return out\n"
|
| 77 |
+
}
|
definitions/gemm/gemm_bf16_n512_k512.json
CHANGED
|
@@ -1,24 +1,54 @@
|
|
| 1 |
{
|
| 2 |
"name": "gemm_bf16_n512_k512",
|
| 3 |
"op_type": "gemm",
|
| 4 |
-
"description": "GEMM+bias, N=512 K=512, compute-precision-only bf16: A[M,K]/B[K,N]/bias[N] are fp32 wire format (the actual KleidiAI packers kai_lhs_pack_bf16p2vlx2_f32_sme2 / kai_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme both take raw f32 input and truncate to bf16 internally as part of packing -- there is no bf16 data anywhere upstream, this is a lossy compute-precision optimization applied on the fly, analogous to gemm_qs4c32_n256_k512's dynamic LHS quantization, NOT a pre-quantized-weight contract like gemm_q4_k_m_*). B is [K,N] ('kxn' layout, matching the RHS packer's native input layout directly, no transpose needed) -- unlike this repo's other plain gemm definitions, which use [N,K]. Output is float32. Baseline: kai_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa.
|
| 5 |
"tags": [
|
| 6 |
"status:active",
|
| 7 |
-
"baseline-solution:kleidiai"
|
| 8 |
-
"correctness:sqnr"
|
| 9 |
],
|
| 10 |
"axes": {
|
| 11 |
-
"M": {
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
},
|
| 15 |
"inputs": {
|
| 16 |
-
"A": {
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
},
|
| 20 |
"outputs": {
|
| 21 |
-
"C": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
},
|
| 23 |
"constraints": [],
|
| 24 |
"reference": "import numpy as np\n\n\ndef run(A, B, bias):\n return (A.astype(np.float32) @ B.astype(np.float32) + bias.astype(np.float32)).astype(np.float32)\n"
|
|
|
|
| 1 |
{
|
| 2 |
"name": "gemm_bf16_n512_k512",
|
| 3 |
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=512 K=512, compute-precision-only bf16: A[M,K]/B[K,N]/bias[N] are fp32 wire format (the actual KleidiAI packers kai_lhs_pack_bf16p2vlx2_f32_sme2 / kai_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme both take raw f32 input and truncate to bf16 internally as part of packing -- there is no bf16 data anywhere upstream, this is a lossy compute-precision optimization applied on the fly, analogous to gemm_qs4c32_n256_k512's dynamic LHS quantization, NOT a pre-quantized-weight contract like gemm_q4_k_m_*). B is [K,N] ('kxn' layout, matching the RHS packer's native input layout directly, no transpose needed) -- unlike this repo's other plain gemm definitions, which use [N,K]. Output is float32. Baseline: kai_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa. Judged by element tolerance with a matched-ratio bar sized to bf16 (7 mantissa bits) truncation -- no dequantization is involved, so not SQNR. M varies per workload.",
|
| 5 |
"tags": [
|
| 6 |
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai"
|
|
|
|
| 8 |
],
|
| 9 |
"axes": {
|
| 10 |
+
"M": {
|
| 11 |
+
"type": "var"
|
| 12 |
+
},
|
| 13 |
+
"N": {
|
| 14 |
+
"type": "const",
|
| 15 |
+
"value": 512
|
| 16 |
+
},
|
| 17 |
+
"K": {
|
| 18 |
+
"type": "const",
|
| 19 |
+
"value": 512
|
| 20 |
+
}
|
| 21 |
},
|
| 22 |
"inputs": {
|
| 23 |
+
"A": {
|
| 24 |
+
"shape": [
|
| 25 |
+
"M",
|
| 26 |
+
"K"
|
| 27 |
+
],
|
| 28 |
+
"dtype": "float32"
|
| 29 |
+
},
|
| 30 |
+
"B": {
|
| 31 |
+
"shape": [
|
| 32 |
+
"K",
|
| 33 |
+
"N"
|
| 34 |
+
],
|
| 35 |
+
"dtype": "float32"
|
| 36 |
+
},
|
| 37 |
+
"bias": {
|
| 38 |
+
"shape": [
|
| 39 |
+
"N"
|
| 40 |
+
],
|
| 41 |
+
"dtype": "float32"
|
| 42 |
+
}
|
| 43 |
},
|
| 44 |
"outputs": {
|
| 45 |
+
"C": {
|
| 46 |
+
"shape": [
|
| 47 |
+
"M",
|
| 48 |
+
"N"
|
| 49 |
+
],
|
| 50 |
+
"dtype": "float32"
|
| 51 |
+
}
|
| 52 |
},
|
| 53 |
"constraints": [],
|
| 54 |
"reference": "import numpy as np\n\n\ndef run(A, B, bias):\n return (A.astype(np.float32) @ B.astype(np.float32) + bias.astype(np.float32)).astype(np.float32)\n"
|
definitions/gemm/gemm_f16_qai4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_f16_qai4c32_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=256 K=512, fp16 activations x block-wise asymmetric int4 weights, fp16 kernel family (SME2): A float16 [M,K] (raw patterns), B_q4 uint8 [N,K/2] adjacent-pair nibbles (byte j: k=2j low, k=2j+1 high), per-block (length 32) fp32 scale B_scales and fp32 additive offset B_zeros [N,K/32] already in the real domain (= -zero_point*scale), fp32 bias [N]; dequant via value=(nibble-8)*scale+zero. The kernel's native output is fp16; the raw ABI allocates float32 outputs, so the binding widens it (exact), and fp16 rounding is part of what the baseline-relative SQNR gate absorbs. M varies per workload. Baseline: kai_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_half": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 256
|
| 25 |
+
},
|
| 26 |
+
"K_blocks": {
|
| 27 |
+
"type": "const",
|
| 28 |
+
"value": 16
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"inputs": {
|
| 32 |
+
"A": {
|
| 33 |
+
"shape": [
|
| 34 |
+
"M",
|
| 35 |
+
"K"
|
| 36 |
+
],
|
| 37 |
+
"dtype": "float16"
|
| 38 |
+
},
|
| 39 |
+
"B_q4": {
|
| 40 |
+
"shape": [
|
| 41 |
+
"N",
|
| 42 |
+
"K_half"
|
| 43 |
+
],
|
| 44 |
+
"dtype": "uint8"
|
| 45 |
+
},
|
| 46 |
+
"B_scales": {
|
| 47 |
+
"shape": [
|
| 48 |
+
"N",
|
| 49 |
+
"K_blocks"
|
| 50 |
+
],
|
| 51 |
+
"dtype": "float32"
|
| 52 |
+
},
|
| 53 |
+
"B_zeros": {
|
| 54 |
+
"shape": [
|
| 55 |
+
"N",
|
| 56 |
+
"K_blocks"
|
| 57 |
+
],
|
| 58 |
+
"dtype": "float32"
|
| 59 |
+
},
|
| 60 |
+
"bias": {
|
| 61 |
+
"shape": [
|
| 62 |
+
"N"
|
| 63 |
+
],
|
| 64 |
+
"dtype": "float32"
|
| 65 |
+
}
|
| 66 |
+
},
|
| 67 |
+
"outputs": {
|
| 68 |
+
"C": {
|
| 69 |
+
"shape": [
|
| 70 |
+
"M",
|
| 71 |
+
"N"
|
| 72 |
+
],
|
| 73 |
+
"dtype": "float32"
|
| 74 |
+
}
|
| 75 |
+
},
|
| 76 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q4, B_scales, B_zeros, bias):\n n, k_half = B_q4.shape\n deq = np.empty((n, k_half * 2), dtype=np.float32)\n deq[:, 0::2] = (B_q4 & 0x0F).astype(np.float32) - 8.0\n deq[:, 1::2] = (B_q4 >> 4).astype(np.float32) - 8.0\n s = np.repeat(B_scales.astype(np.float32), 32, axis=1)\n z = np.repeat(B_zeros.astype(np.float32), 32, axis=1)\n B = deq * s + z\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 77 |
+
}
|
definitions/gemm/gemm_f16_qs4cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_f16_qs4cx_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=256 K=512, fp16 activations x per-channel int4 weights, fp16 kernel family (SME2): A float16 [M,K] (raw patterns), B_q4 uint8 [N,K/2] adjacent-pair nibbles (byte j: k=2j low, k=2j+1 high), zero_point=8, one fp32 scale per output channel, fp32 bias [N]; dequant via value=(nibble-8)*scale. The kernel's native output is fp16; the raw ABI allocates float32 outputs, so the binding widens it (exact), and fp16 rounding is part of what the baseline-relative SQNR gate absorbs. M varies per workload. Baseline: kai_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_half": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 256
|
| 25 |
+
}
|
| 26 |
+
},
|
| 27 |
+
"inputs": {
|
| 28 |
+
"A": {
|
| 29 |
+
"shape": [
|
| 30 |
+
"M",
|
| 31 |
+
"K"
|
| 32 |
+
],
|
| 33 |
+
"dtype": "float16"
|
| 34 |
+
},
|
| 35 |
+
"B_q4": {
|
| 36 |
+
"shape": [
|
| 37 |
+
"N",
|
| 38 |
+
"K_half"
|
| 39 |
+
],
|
| 40 |
+
"dtype": "uint8"
|
| 41 |
+
},
|
| 42 |
+
"B_scales": {
|
| 43 |
+
"shape": [
|
| 44 |
+
"N"
|
| 45 |
+
],
|
| 46 |
+
"dtype": "float32"
|
| 47 |
+
},
|
| 48 |
+
"bias": {
|
| 49 |
+
"shape": [
|
| 50 |
+
"N"
|
| 51 |
+
],
|
| 52 |
+
"dtype": "float32"
|
| 53 |
+
}
|
| 54 |
+
},
|
| 55 |
+
"outputs": {
|
| 56 |
+
"C": {
|
| 57 |
+
"shape": [
|
| 58 |
+
"M",
|
| 59 |
+
"N"
|
| 60 |
+
],
|
| 61 |
+
"dtype": "float32"
|
| 62 |
+
}
|
| 63 |
+
},
|
| 64 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q4, B_scales, bias):\n n, k_half = B_q4.shape\n deq = np.empty((n, k_half * 2), dtype=np.float32)\n deq[:, 0::2] = (B_q4 & 0x0F).astype(np.float32) - 8.0\n deq[:, 1::2] = (B_q4 >> 4).astype(np.float32) - 8.0\n B = deq * B_scales.astype(np.float32)[:, None]\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 65 |
+
}
|
definitions/gemm/gemm_f16_qs8cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_f16_qs8cx_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=256 K=512, fp16 activations x per-channel int8 weights, fp16 kernel family (SME2): A float16 [M,K] (raw patterns), B_q8 int8 [N,K] + one fp32 scale per output channel (value=q*scale, no zero point), fp32 bias [N]. The kernel's native output is fp16; the raw ABI allocates float32 outputs, so the binding widens it (exact), and fp16 rounding is part of what the baseline-relative SQNR gate absorbs. M varies per workload. Baseline: kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa (LHS via kai_lhs_quant_pack_qai8dxp_f16_neon).",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
}
|
| 22 |
+
},
|
| 23 |
+
"inputs": {
|
| 24 |
+
"A": {
|
| 25 |
+
"shape": [
|
| 26 |
+
"M",
|
| 27 |
+
"K"
|
| 28 |
+
],
|
| 29 |
+
"dtype": "float16"
|
| 30 |
+
},
|
| 31 |
+
"B_q8": {
|
| 32 |
+
"shape": [
|
| 33 |
+
"N",
|
| 34 |
+
"K"
|
| 35 |
+
],
|
| 36 |
+
"dtype": "int8"
|
| 37 |
+
},
|
| 38 |
+
"B_scales": {
|
| 39 |
+
"shape": [
|
| 40 |
+
"N"
|
| 41 |
+
],
|
| 42 |
+
"dtype": "float32"
|
| 43 |
+
},
|
| 44 |
+
"bias": {
|
| 45 |
+
"shape": [
|
| 46 |
+
"N"
|
| 47 |
+
],
|
| 48 |
+
"dtype": "float32"
|
| 49 |
+
}
|
| 50 |
+
},
|
| 51 |
+
"outputs": {
|
| 52 |
+
"C": {
|
| 53 |
+
"shape": [
|
| 54 |
+
"M",
|
| 55 |
+
"N"
|
| 56 |
+
],
|
| 57 |
+
"dtype": "float32"
|
| 58 |
+
}
|
| 59 |
+
},
|
| 60 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q8, B_scales, bias):\n B = B_q8.astype(np.float32) * B_scales.astype(np.float32)[:, None]\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 61 |
+
}
|
definitions/gemm/gemm_f16act_qs4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_f16act_qs4c32_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM, N=256 K=512, fp16 activations x block-wise int4 weights (SME2): A is float16 [M,K] (raw 16-bit patterns per the raw ABI), B_q4 uint8 [N,K/2] and f16 per-block scales exactly as gemm_qs4c32_sme2_n256_k512 (block length 32, zero_point=8, split-half nibbles); no bias; f32 output. The mixed fp16-activation / int4-weight case. M varies per workload. Baseline: kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa (needs SME2 and FP16).",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_half": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 256
|
| 25 |
+
},
|
| 26 |
+
"K_blocks": {
|
| 27 |
+
"type": "const",
|
| 28 |
+
"value": 16
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"inputs": {
|
| 32 |
+
"A": {
|
| 33 |
+
"shape": [
|
| 34 |
+
"M",
|
| 35 |
+
"K"
|
| 36 |
+
],
|
| 37 |
+
"dtype": "float16"
|
| 38 |
+
},
|
| 39 |
+
"B_q4": {
|
| 40 |
+
"shape": [
|
| 41 |
+
"N",
|
| 42 |
+
"K_half"
|
| 43 |
+
],
|
| 44 |
+
"dtype": "uint8"
|
| 45 |
+
},
|
| 46 |
+
"B_scales": {
|
| 47 |
+
"shape": [
|
| 48 |
+
"N",
|
| 49 |
+
"K_blocks"
|
| 50 |
+
],
|
| 51 |
+
"dtype": "float16"
|
| 52 |
+
}
|
| 53 |
+
},
|
| 54 |
+
"outputs": {
|
| 55 |
+
"C": {
|
| 56 |
+
"shape": [
|
| 57 |
+
"M",
|
| 58 |
+
"N"
|
| 59 |
+
],
|
| 60 |
+
"dtype": "float32"
|
| 61 |
+
}
|
| 62 |
+
},
|
| 63 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q4, B_scales):\n n, k_half = B_q4.shape\n bl = 32\n nb = k_half // (bl // 2)\n lo = (B_q4 & 0x0F).astype(np.float32) - 8.0\n hi = (B_q4 >> 4).astype(np.float32) - 8.0\n deq = np.concatenate([lo.reshape(n, nb, bl // 2), hi.reshape(n, nb, bl // 2)], axis=-1).reshape(n, nb * bl)\n B = deq * np.repeat(B_scales.astype(np.float32), bl, axis=-1)\n return (A.astype(np.float32) @ B.T).astype(np.float32)\n"
|
| 64 |
+
}
|
definitions/gemm/gemm_f32_bf16_n512_k512.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_f32_bf16_n512_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=512 K=512, compute-precision-only bf16 on NEON bfmmla: A[M,K]/B[K,N]/bias[N] are fp32 wire format; the baseline packers (kai_lhs_quant_pack_bf16p8x4_f32_neon, kai_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon) truncate to bf16 internally, as in gemm_bf16_n512_k512 (the SME2 variant). M varies per workload. Baseline: kai_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai"
|
| 8 |
+
],
|
| 9 |
+
"axes": {
|
| 10 |
+
"M": {
|
| 11 |
+
"type": "var"
|
| 12 |
+
},
|
| 13 |
+
"N": {
|
| 14 |
+
"type": "const",
|
| 15 |
+
"value": 512
|
| 16 |
+
},
|
| 17 |
+
"K": {
|
| 18 |
+
"type": "const",
|
| 19 |
+
"value": 512
|
| 20 |
+
}
|
| 21 |
+
},
|
| 22 |
+
"inputs": {
|
| 23 |
+
"A": {
|
| 24 |
+
"shape": [
|
| 25 |
+
"M",
|
| 26 |
+
"K"
|
| 27 |
+
],
|
| 28 |
+
"dtype": "float32"
|
| 29 |
+
},
|
| 30 |
+
"B": {
|
| 31 |
+
"shape": [
|
| 32 |
+
"K",
|
| 33 |
+
"N"
|
| 34 |
+
],
|
| 35 |
+
"dtype": "float32"
|
| 36 |
+
},
|
| 37 |
+
"bias": {
|
| 38 |
+
"shape": [
|
| 39 |
+
"N"
|
| 40 |
+
],
|
| 41 |
+
"dtype": "float32"
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"outputs": {
|
| 45 |
+
"C": {
|
| 46 |
+
"shape": [
|
| 47 |
+
"M",
|
| 48 |
+
"N"
|
| 49 |
+
],
|
| 50 |
+
"dtype": "float32"
|
| 51 |
+
}
|
| 52 |
+
},
|
| 53 |
+
"reference": "import numpy as np\n\n\ndef run(A, B, bias):\n return (A.astype(np.float32) @ B.astype(np.float32) + bias.astype(np.float32)).astype(np.float32)\n"
|
| 54 |
+
}
|
definitions/gemm/gemm_i32_u8_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_i32_u8_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "Integer GEMM, N=256 K=512: C[m,n] = sum_k A[m,k]*B[n,k] with A uint8 [M,K] and B uint8 [N,K], exact int32 accumulation, no scales or bias. The kernel's native output is int32; this repo's raw ABI always allocates float32 outputs, so the binding converts int32 to float32 (round-to-nearest, identical in reference and baseline; products up to 512*255*255 exceed 2^24 so large values are rounded). Exact match is expected, no SQNR. M varies per workload. Baseline: kai_matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa (SME2).",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai"
|
| 8 |
+
],
|
| 9 |
+
"axes": {
|
| 10 |
+
"M": {
|
| 11 |
+
"type": "var"
|
| 12 |
+
},
|
| 13 |
+
"N": {
|
| 14 |
+
"type": "const",
|
| 15 |
+
"value": 256
|
| 16 |
+
},
|
| 17 |
+
"K": {
|
| 18 |
+
"type": "const",
|
| 19 |
+
"value": 512
|
| 20 |
+
}
|
| 21 |
+
},
|
| 22 |
+
"inputs": {
|
| 23 |
+
"A": {
|
| 24 |
+
"shape": [
|
| 25 |
+
"M",
|
| 26 |
+
"K"
|
| 27 |
+
],
|
| 28 |
+
"dtype": "uint8"
|
| 29 |
+
},
|
| 30 |
+
"B": {
|
| 31 |
+
"shape": [
|
| 32 |
+
"N",
|
| 33 |
+
"K"
|
| 34 |
+
],
|
| 35 |
+
"dtype": "uint8"
|
| 36 |
+
}
|
| 37 |
+
},
|
| 38 |
+
"outputs": {
|
| 39 |
+
"C": {
|
| 40 |
+
"shape": [
|
| 41 |
+
"M",
|
| 42 |
+
"N"
|
| 43 |
+
],
|
| 44 |
+
"dtype": "float32"
|
| 45 |
+
}
|
| 46 |
+
},
|
| 47 |
+
"reference": "import numpy as np\n\n\ndef run(A, B):\n return (A.astype(np.int64) @ B.astype(np.int64).T).astype(np.int32).astype(np.float32)\n"
|
| 48 |
+
}
|
definitions/gemm/gemm_qai4c32_n256_k512.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qai4c32_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM, N=256 K=512, weight-only-quantized ASYMMETRIC int4 with block length 32 (NEON i8mm): A is fp32, B ships as qai4c32 -- uint8 [N,K/2] with adjacent-pair nibbles (byte j holds k=2j in the low nibble, k=2j+1 in the high nibble), plus per-block fp32 scale [N,K/32] and per-block fp32 additive offset B_zeros [N,K/32] already in the real domain (= -zero_point*scale, the form KleidiAI's packer takes; unlike gemm_qs4c32_* which has no offset); dequant via value=(nibble-8)*scale+zero. M varies per workload. Baseline: kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_half": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 256
|
| 25 |
+
},
|
| 26 |
+
"K_blocks": {
|
| 27 |
+
"type": "const",
|
| 28 |
+
"value": 16
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"inputs": {
|
| 32 |
+
"A": {
|
| 33 |
+
"shape": [
|
| 34 |
+
"M",
|
| 35 |
+
"K"
|
| 36 |
+
],
|
| 37 |
+
"dtype": "float32"
|
| 38 |
+
},
|
| 39 |
+
"B_q4": {
|
| 40 |
+
"shape": [
|
| 41 |
+
"N",
|
| 42 |
+
"K_half"
|
| 43 |
+
],
|
| 44 |
+
"dtype": "uint8"
|
| 45 |
+
},
|
| 46 |
+
"B_scales": {
|
| 47 |
+
"shape": [
|
| 48 |
+
"N",
|
| 49 |
+
"K_blocks"
|
| 50 |
+
],
|
| 51 |
+
"dtype": "float32"
|
| 52 |
+
},
|
| 53 |
+
"B_zeros": {
|
| 54 |
+
"shape": [
|
| 55 |
+
"N",
|
| 56 |
+
"K_blocks"
|
| 57 |
+
],
|
| 58 |
+
"dtype": "float32"
|
| 59 |
+
},
|
| 60 |
+
"bias": {
|
| 61 |
+
"shape": [
|
| 62 |
+
"N"
|
| 63 |
+
],
|
| 64 |
+
"dtype": "float32"
|
| 65 |
+
}
|
| 66 |
+
},
|
| 67 |
+
"outputs": {
|
| 68 |
+
"C": {
|
| 69 |
+
"shape": [
|
| 70 |
+
"M",
|
| 71 |
+
"N"
|
| 72 |
+
],
|
| 73 |
+
"dtype": "float32"
|
| 74 |
+
}
|
| 75 |
+
},
|
| 76 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q4, B_scales, B_zeros, bias):\n n, k_half = B_q4.shape\n deq = np.empty((n, k_half * 2), dtype=np.float32)\n deq[:, 0::2] = (B_q4 & 0x0F).astype(np.float32) - 8.0\n deq[:, 1::2] = (B_q4 >> 4).astype(np.float32) - 8.0\n s = np.repeat(B_scales.astype(np.float32), 32, axis=1)\n z = np.repeat(B_zeros.astype(np.float32), 32, axis=1)\n B = deq * s + z\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 77 |
+
}
|
definitions/gemm/gemm_qai4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qai4c32_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM, N=256 K=512, weight-only-quantized ASYMMETRIC int4 with block length 32 (SME2): A is fp32, B ships as qai4c32 -- uint8 [N,K/2] with adjacent-pair nibbles (byte j holds k=2j in the low nibble, k=2j+1 in the high nibble), plus per-block fp32 scale [N,K/32] and per-block fp32 additive offset B_zeros [N,K/32] already in the real domain (= -zero_point*scale, the form KleidiAI's packer takes; unlike gemm_qs4c32_* which has no offset); dequant via value=(nibble-8)*scale+zero. M varies per workload. Baseline: kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_half": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 256
|
| 25 |
+
},
|
| 26 |
+
"K_blocks": {
|
| 27 |
+
"type": "const",
|
| 28 |
+
"value": 16
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"inputs": {
|
| 32 |
+
"A": {
|
| 33 |
+
"shape": [
|
| 34 |
+
"M",
|
| 35 |
+
"K"
|
| 36 |
+
],
|
| 37 |
+
"dtype": "float32"
|
| 38 |
+
},
|
| 39 |
+
"B_q4": {
|
| 40 |
+
"shape": [
|
| 41 |
+
"N",
|
| 42 |
+
"K_half"
|
| 43 |
+
],
|
| 44 |
+
"dtype": "uint8"
|
| 45 |
+
},
|
| 46 |
+
"B_scales": {
|
| 47 |
+
"shape": [
|
| 48 |
+
"N",
|
| 49 |
+
"K_blocks"
|
| 50 |
+
],
|
| 51 |
+
"dtype": "float32"
|
| 52 |
+
},
|
| 53 |
+
"B_zeros": {
|
| 54 |
+
"shape": [
|
| 55 |
+
"N",
|
| 56 |
+
"K_blocks"
|
| 57 |
+
],
|
| 58 |
+
"dtype": "float32"
|
| 59 |
+
},
|
| 60 |
+
"bias": {
|
| 61 |
+
"shape": [
|
| 62 |
+
"N"
|
| 63 |
+
],
|
| 64 |
+
"dtype": "float32"
|
| 65 |
+
}
|
| 66 |
+
},
|
| 67 |
+
"outputs": {
|
| 68 |
+
"C": {
|
| 69 |
+
"shape": [
|
| 70 |
+
"M",
|
| 71 |
+
"N"
|
| 72 |
+
],
|
| 73 |
+
"dtype": "float32"
|
| 74 |
+
}
|
| 75 |
+
},
|
| 76 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q4, B_scales, B_zeros, bias):\n n, k_half = B_q4.shape\n deq = np.empty((n, k_half * 2), dtype=np.float32)\n deq[:, 0::2] = (B_q4 & 0x0F).astype(np.float32) - 8.0\n deq[:, 1::2] = (B_q4 >> 4).astype(np.float32) - 8.0\n s = np.repeat(B_scales.astype(np.float32), 32, axis=1)\n z = np.repeat(B_zeros.astype(np.float32), 32, axis=1)\n B = deq * s + z\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 77 |
+
}
|
definitions/gemm/gemm_qai8_qsi8cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qai8_qsi8cx_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "Requantized int8 GEMM, N=256 K=512: A int8 [M,K] with per-tensor scale A_scale (symmetric, zero point fixed at 0); B int8 [N,K] with per-channel scale B_scales (symmetric); fp32 bias [N]; int32 accumulation A@B^T, scaled by A_scale*B_scales[n], plus bias, divided by C_scale, rounded to nearest even and clamped to [-128,127] (output zero point fixed at 0). A_scale and C_scale are 1-element fp32 tensors (axis S=1) because the raw ABI passes only tensors, not scalars. Scales must be positive. The kernel's native output is int8; the raw ABI allocates float32 outputs so the binding converts int8 to float32 (exact). M varies per workload. Baseline: kai_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa (SME2).",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai"
|
| 8 |
+
],
|
| 9 |
+
"axes": {
|
| 10 |
+
"M": {
|
| 11 |
+
"type": "var"
|
| 12 |
+
},
|
| 13 |
+
"N": {
|
| 14 |
+
"type": "const",
|
| 15 |
+
"value": 256
|
| 16 |
+
},
|
| 17 |
+
"K": {
|
| 18 |
+
"type": "const",
|
| 19 |
+
"value": 512
|
| 20 |
+
},
|
| 21 |
+
"S": {
|
| 22 |
+
"type": "const",
|
| 23 |
+
"value": 1
|
| 24 |
+
}
|
| 25 |
+
},
|
| 26 |
+
"inputs": {
|
| 27 |
+
"A": {
|
| 28 |
+
"shape": [
|
| 29 |
+
"M",
|
| 30 |
+
"K"
|
| 31 |
+
],
|
| 32 |
+
"dtype": "int8"
|
| 33 |
+
},
|
| 34 |
+
"A_scale": {
|
| 35 |
+
"shape": [
|
| 36 |
+
"S"
|
| 37 |
+
],
|
| 38 |
+
"dtype": "float32"
|
| 39 |
+
},
|
| 40 |
+
"B_q8": {
|
| 41 |
+
"shape": [
|
| 42 |
+
"N",
|
| 43 |
+
"K"
|
| 44 |
+
],
|
| 45 |
+
"dtype": "int8"
|
| 46 |
+
},
|
| 47 |
+
"B_scales": {
|
| 48 |
+
"shape": [
|
| 49 |
+
"N"
|
| 50 |
+
],
|
| 51 |
+
"dtype": "float32"
|
| 52 |
+
},
|
| 53 |
+
"bias": {
|
| 54 |
+
"shape": [
|
| 55 |
+
"N"
|
| 56 |
+
],
|
| 57 |
+
"dtype": "float32"
|
| 58 |
+
},
|
| 59 |
+
"C_scale": {
|
| 60 |
+
"shape": [
|
| 61 |
+
"S"
|
| 62 |
+
],
|
| 63 |
+
"dtype": "float32"
|
| 64 |
+
}
|
| 65 |
+
},
|
| 66 |
+
"outputs": {
|
| 67 |
+
"C": {
|
| 68 |
+
"shape": [
|
| 69 |
+
"M",
|
| 70 |
+
"N"
|
| 71 |
+
],
|
| 72 |
+
"dtype": "float32"
|
| 73 |
+
}
|
| 74 |
+
},
|
| 75 |
+
"reference": "import numpy as np\n\n\ndef run(A, A_scale, B_q8, B_scales, bias, C_scale):\n acc = A.astype(np.int32) @ B_q8.astype(np.int32).T\n real = acc.astype(np.float32) * (A_scale[0] * B_scales.astype(np.float32))[None, :] + bias.astype(np.float32)\n q = np.rint(real / C_scale[0])\n return np.clip(q, -128, 127).astype(np.float32)\n"
|
| 76 |
+
}
|
definitions/gemm/gemm_qai8dxp_qs4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qai8dxp_qs4c32_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=256 K=512, weight-only-quantized block-wise int4 with dynamically int8-quantized activations (SME2): A and bias are fp32, B_q4 is uint8 [N,K/2] with adjacent-pair nibbles (byte j holds k=2j in the low nibble and k=2j+1 in the high nibble), zero_point=8, one bfloat16 scale per block of 32 along K (the only scale type this kernel family's RHS packer accepts) ([N,K/32]); dequant via value=(nibble-8)*scale. Unlike gemm_qs4c32_sme2_n256_k512 (per-32-block symmetric int8 LHS, split-half nibbles, no bias), the kernel quantizes the LHS per ROW with an asymmetric zero point (qai8dxp). Judged by SQNR against the baseline. M varies per workload. Baseline: kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_half": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 256
|
| 25 |
+
},
|
| 26 |
+
"K_blocks": {
|
| 27 |
+
"type": "const",
|
| 28 |
+
"value": 16
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"inputs": {
|
| 32 |
+
"A": {
|
| 33 |
+
"shape": [
|
| 34 |
+
"M",
|
| 35 |
+
"K"
|
| 36 |
+
],
|
| 37 |
+
"dtype": "float32"
|
| 38 |
+
},
|
| 39 |
+
"B_q4": {
|
| 40 |
+
"shape": [
|
| 41 |
+
"N",
|
| 42 |
+
"K_half"
|
| 43 |
+
],
|
| 44 |
+
"dtype": "uint8"
|
| 45 |
+
},
|
| 46 |
+
"B_scales": {
|
| 47 |
+
"shape": [
|
| 48 |
+
"N",
|
| 49 |
+
"K_blocks"
|
| 50 |
+
],
|
| 51 |
+
"dtype": "bfloat16"
|
| 52 |
+
},
|
| 53 |
+
"bias": {
|
| 54 |
+
"shape": [
|
| 55 |
+
"N"
|
| 56 |
+
],
|
| 57 |
+
"dtype": "float32"
|
| 58 |
+
}
|
| 59 |
+
},
|
| 60 |
+
"outputs": {
|
| 61 |
+
"C": {
|
| 62 |
+
"shape": [
|
| 63 |
+
"M",
|
| 64 |
+
"N"
|
| 65 |
+
],
|
| 66 |
+
"dtype": "float32"
|
| 67 |
+
}
|
| 68 |
+
},
|
| 69 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q4, B_scales, bias):\n n, k_half = B_q4.shape\n deq = np.empty((n, k_half * 2), dtype=np.float32)\n deq[:, 0::2] = (B_q4 & 0x0F).astype(np.float32) - 8.0\n deq[:, 1::2] = (B_q4 >> 4).astype(np.float32) - 8.0\n s = np.repeat(B_scales.astype(np.float32), 32, axis=1)\n B = deq * s\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 70 |
+
}
|
definitions/gemm/gemm_qs4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qs4c32_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM, N=256 K=512, weight-only-quantized block-wise int4 (SME2): same wire format as gemm_qs4c32_n256_k512 -- A fp32, B_q4 uint8 [N,K/2] (qs4c32: block length 32, zero_point=8, byte i of each 16-byte block holds element i in the low nibble and element i+16 in the high nibble), per-block f16 scale; dequant via value=(nibble-8)*scale; no bias. SME2 counterpart of the Graviton3 SVE definition. M varies per workload. Baseline: kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_half": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 256
|
| 25 |
+
},
|
| 26 |
+
"K_blocks": {
|
| 27 |
+
"type": "const",
|
| 28 |
+
"value": 16
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"inputs": {
|
| 32 |
+
"A": {
|
| 33 |
+
"shape": [
|
| 34 |
+
"M",
|
| 35 |
+
"K"
|
| 36 |
+
],
|
| 37 |
+
"dtype": "float32"
|
| 38 |
+
},
|
| 39 |
+
"B_q4": {
|
| 40 |
+
"shape": [
|
| 41 |
+
"N",
|
| 42 |
+
"K_half"
|
| 43 |
+
],
|
| 44 |
+
"dtype": "uint8"
|
| 45 |
+
},
|
| 46 |
+
"B_scales": {
|
| 47 |
+
"shape": [
|
| 48 |
+
"N",
|
| 49 |
+
"K_blocks"
|
| 50 |
+
],
|
| 51 |
+
"dtype": "float16"
|
| 52 |
+
}
|
| 53 |
+
},
|
| 54 |
+
"outputs": {
|
| 55 |
+
"C": {
|
| 56 |
+
"shape": [
|
| 57 |
+
"M",
|
| 58 |
+
"N"
|
| 59 |
+
],
|
| 60 |
+
"dtype": "float32"
|
| 61 |
+
}
|
| 62 |
+
},
|
| 63 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q4, B_scales):\n n, k_half = B_q4.shape\n bl = 32\n nb = k_half // (bl // 2)\n lo = (B_q4 & 0x0F).astype(np.float32) - 8.0\n hi = (B_q4 >> 4).astype(np.float32) - 8.0\n deq = np.concatenate([lo.reshape(n, nb, bl // 2), hi.reshape(n, nb, bl // 2)], axis=-1).reshape(n, nb * bl)\n B = deq * np.repeat(B_scales.astype(np.float32), bl, axis=-1)\n return (A.astype(np.float32) @ B.T).astype(np.float32)\n"
|
| 64 |
+
}
|
definitions/gemm/gemm_qs4cx_n512_k512.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qs4cx_n512_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=512 K=512, weight-only-quantized per-channel int4 (NEON i8mm): same wire format as gemm_qs4cx_sme2_n256_k512 -- A and bias fp32, B as qs4cx uint8 [N,K/2] with adjacent-pair nibbles (byte j holds k=2j low, k=2j+1 high), zero_point=8, one fp32 scale per output channel; dequant via value=(nibble-8)*scale. M varies per workload. Baseline: kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 512
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_half": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 256
|
| 25 |
+
}
|
| 26 |
+
},
|
| 27 |
+
"inputs": {
|
| 28 |
+
"A": {
|
| 29 |
+
"shape": [
|
| 30 |
+
"M",
|
| 31 |
+
"K"
|
| 32 |
+
],
|
| 33 |
+
"dtype": "float32"
|
| 34 |
+
},
|
| 35 |
+
"B_q4": {
|
| 36 |
+
"shape": [
|
| 37 |
+
"N",
|
| 38 |
+
"K_half"
|
| 39 |
+
],
|
| 40 |
+
"dtype": "uint8"
|
| 41 |
+
},
|
| 42 |
+
"B_scales": {
|
| 43 |
+
"shape": [
|
| 44 |
+
"N"
|
| 45 |
+
],
|
| 46 |
+
"dtype": "float32"
|
| 47 |
+
},
|
| 48 |
+
"bias": {
|
| 49 |
+
"shape": [
|
| 50 |
+
"N"
|
| 51 |
+
],
|
| 52 |
+
"dtype": "float32"
|
| 53 |
+
}
|
| 54 |
+
},
|
| 55 |
+
"outputs": {
|
| 56 |
+
"C": {
|
| 57 |
+
"shape": [
|
| 58 |
+
"M",
|
| 59 |
+
"N"
|
| 60 |
+
],
|
| 61 |
+
"dtype": "float32"
|
| 62 |
+
}
|
| 63 |
+
},
|
| 64 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q4, B_scales, bias):\n n, k_half = B_q4.shape\n deq = np.empty((n, k_half * 2), dtype=np.float32)\n deq[:, 0::2] = (B_q4 & 0x0F).astype(np.float32) - 8.0\n deq[:, 1::2] = (B_q4 >> 4).astype(np.float32) - 8.0\n B = deq * B_scales.astype(np.float32)[:, None]\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 65 |
+
}
|
definitions/gemm/gemm_qs8cx_n512_k512.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qs8cx_n512_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=512 K=512, weight-only-quantized per-channel int8 (NEON i8mm): A and bias are fp32, B ships pre-quantized as qsi8cx -- int8 [N,K] (row-major, one row per output channel) plus one fp32 scale per output channel; dequant via value=q*scale, no zero point. LHS is dynamically quantized per row by the baseline (qai8dxp, asymmetric). M varies per workload. Baseline: kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 512
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
}
|
| 22 |
+
},
|
| 23 |
+
"inputs": {
|
| 24 |
+
"A": {
|
| 25 |
+
"shape": [
|
| 26 |
+
"M",
|
| 27 |
+
"K"
|
| 28 |
+
],
|
| 29 |
+
"dtype": "float32"
|
| 30 |
+
},
|
| 31 |
+
"B_q8": {
|
| 32 |
+
"shape": [
|
| 33 |
+
"N",
|
| 34 |
+
"K"
|
| 35 |
+
],
|
| 36 |
+
"dtype": "int8"
|
| 37 |
+
},
|
| 38 |
+
"B_scales": {
|
| 39 |
+
"shape": [
|
| 40 |
+
"N"
|
| 41 |
+
],
|
| 42 |
+
"dtype": "float32"
|
| 43 |
+
},
|
| 44 |
+
"bias": {
|
| 45 |
+
"shape": [
|
| 46 |
+
"N"
|
| 47 |
+
],
|
| 48 |
+
"dtype": "float32"
|
| 49 |
+
}
|
| 50 |
+
},
|
| 51 |
+
"outputs": {
|
| 52 |
+
"C": {
|
| 53 |
+
"shape": [
|
| 54 |
+
"M",
|
| 55 |
+
"N"
|
| 56 |
+
],
|
| 57 |
+
"dtype": "float32"
|
| 58 |
+
}
|
| 59 |
+
},
|
| 60 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q8, B_scales, bias):\n B = B_q8.astype(np.float32) * B_scales.astype(np.float32)[:, None]\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 61 |
+
}
|
definitions/gemm/gemm_qs8cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qs8cx_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=256 K=512, weight-only-quantized per-channel int8 (SME2): A and bias are fp32, B ships pre-quantized as qsi8cx -- int8 [N,K] (row-major, one row per output channel) plus one fp32 scale per output channel; dequant via value=q*scale, no zero point. LHS is dynamically quantized per row by the baseline (qai8dxp, asymmetric). M varies per workload. Baseline: kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
}
|
| 22 |
+
},
|
| 23 |
+
"inputs": {
|
| 24 |
+
"A": {
|
| 25 |
+
"shape": [
|
| 26 |
+
"M",
|
| 27 |
+
"K"
|
| 28 |
+
],
|
| 29 |
+
"dtype": "float32"
|
| 30 |
+
},
|
| 31 |
+
"B_q8": {
|
| 32 |
+
"shape": [
|
| 33 |
+
"N",
|
| 34 |
+
"K"
|
| 35 |
+
],
|
| 36 |
+
"dtype": "int8"
|
| 37 |
+
},
|
| 38 |
+
"B_scales": {
|
| 39 |
+
"shape": [
|
| 40 |
+
"N"
|
| 41 |
+
],
|
| 42 |
+
"dtype": "float32"
|
| 43 |
+
},
|
| 44 |
+
"bias": {
|
| 45 |
+
"shape": [
|
| 46 |
+
"N"
|
| 47 |
+
],
|
| 48 |
+
"dtype": "float32"
|
| 49 |
+
}
|
| 50 |
+
},
|
| 51 |
+
"outputs": {
|
| 52 |
+
"C": {
|
| 53 |
+
"shape": [
|
| 54 |
+
"M",
|
| 55 |
+
"N"
|
| 56 |
+
],
|
| 57 |
+
"dtype": "float32"
|
| 58 |
+
}
|
| 59 |
+
},
|
| 60 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q8, B_scales, bias):\n B = B_q8.astype(np.float32) * B_scales.astype(np.float32)[:, None]\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 61 |
+
}
|
definitions/gemm/gemm_qsu2cx_n256_k512.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qsu2cx_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=256 K=512, weight-only-quantized per-channel 2-bit (NEON dotprod): A and bias are fp32, B ships pre-quantized as qsu2cx -- uint8 [N,K/4], four 2-bit values per byte (byte j holds k=4j in bits 0-1, k=4j+1 in bits 2-3, k=4j+2 in bits 4-5, k=4j+3 in bits 6-7), zero_point=2, one fp32 scale per output channel; dequant via value=(q-2)*scale. K is a multiple of 32. M varies per workload. Baseline: kai_matmul_clamp_f32_qai8dxp4x4_qsu2cxp4x4bf32sf32_8x4_neon_dotprod.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_q": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 128
|
| 25 |
+
}
|
| 26 |
+
},
|
| 27 |
+
"inputs": {
|
| 28 |
+
"A": {
|
| 29 |
+
"shape": [
|
| 30 |
+
"M",
|
| 31 |
+
"K"
|
| 32 |
+
],
|
| 33 |
+
"dtype": "float32"
|
| 34 |
+
},
|
| 35 |
+
"B_q2": {
|
| 36 |
+
"shape": [
|
| 37 |
+
"N",
|
| 38 |
+
"K_q"
|
| 39 |
+
],
|
| 40 |
+
"dtype": "uint8"
|
| 41 |
+
},
|
| 42 |
+
"B_scales": {
|
| 43 |
+
"shape": [
|
| 44 |
+
"N"
|
| 45 |
+
],
|
| 46 |
+
"dtype": "float32"
|
| 47 |
+
},
|
| 48 |
+
"bias": {
|
| 49 |
+
"shape": [
|
| 50 |
+
"N"
|
| 51 |
+
],
|
| 52 |
+
"dtype": "float32"
|
| 53 |
+
}
|
| 54 |
+
},
|
| 55 |
+
"outputs": {
|
| 56 |
+
"C": {
|
| 57 |
+
"shape": [
|
| 58 |
+
"M",
|
| 59 |
+
"N"
|
| 60 |
+
],
|
| 61 |
+
"dtype": "float32"
|
| 62 |
+
}
|
| 63 |
+
},
|
| 64 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q2, B_scales, bias):\n n, k_q = B_q2.shape\n deq = np.empty((n, k_q * 4), dtype=np.float32)\n for j in range(4):\n deq[:, j::4] = ((B_q2 >> (2 * j)) & 0x03).astype(np.float32) - 2.0\n B = deq * B_scales.astype(np.float32)[:, None]\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 65 |
+
}
|
definitions/gemm/gemm_qsu2cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_qsu2cx_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "GEMM+bias, N=256 K=512, weight-only-quantized per-channel 2-bit (SME2): A and bias are fp32, B ships pre-quantized as qsu2cx -- uint8 [N,K/4], four 2-bit values per byte (byte j holds k=4j in bits 0-1, k=4j+1 in bits 2-3, k=4j+2 in bits 4-5, k=4j+3 in bits 6-7), zero_point=2, one fp32 scale per output channel; dequant via value=(q-2)*scale. K is a multiple of 32. M varies per workload. Baseline: kai_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa.",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai",
|
| 8 |
+
"correctness:sqnr"
|
| 9 |
+
],
|
| 10 |
+
"axes": {
|
| 11 |
+
"M": {
|
| 12 |
+
"type": "var"
|
| 13 |
+
},
|
| 14 |
+
"N": {
|
| 15 |
+
"type": "const",
|
| 16 |
+
"value": 256
|
| 17 |
+
},
|
| 18 |
+
"K": {
|
| 19 |
+
"type": "const",
|
| 20 |
+
"value": 512
|
| 21 |
+
},
|
| 22 |
+
"K_q": {
|
| 23 |
+
"type": "const",
|
| 24 |
+
"value": 128
|
| 25 |
+
}
|
| 26 |
+
},
|
| 27 |
+
"inputs": {
|
| 28 |
+
"A": {
|
| 29 |
+
"shape": [
|
| 30 |
+
"M",
|
| 31 |
+
"K"
|
| 32 |
+
],
|
| 33 |
+
"dtype": "float32"
|
| 34 |
+
},
|
| 35 |
+
"B_q2": {
|
| 36 |
+
"shape": [
|
| 37 |
+
"N",
|
| 38 |
+
"K_q"
|
| 39 |
+
],
|
| 40 |
+
"dtype": "uint8"
|
| 41 |
+
},
|
| 42 |
+
"B_scales": {
|
| 43 |
+
"shape": [
|
| 44 |
+
"N"
|
| 45 |
+
],
|
| 46 |
+
"dtype": "float32"
|
| 47 |
+
},
|
| 48 |
+
"bias": {
|
| 49 |
+
"shape": [
|
| 50 |
+
"N"
|
| 51 |
+
],
|
| 52 |
+
"dtype": "float32"
|
| 53 |
+
}
|
| 54 |
+
},
|
| 55 |
+
"outputs": {
|
| 56 |
+
"C": {
|
| 57 |
+
"shape": [
|
| 58 |
+
"M",
|
| 59 |
+
"N"
|
| 60 |
+
],
|
| 61 |
+
"dtype": "float32"
|
| 62 |
+
}
|
| 63 |
+
},
|
| 64 |
+
"reference": "import numpy as np\n\n\ndef run(A, B_q2, B_scales, bias):\n n, k_q = B_q2.shape\n deq = np.empty((n, k_q * 4), dtype=np.float32)\n for j in range(4):\n deq[:, j::4] = ((B_q2 >> (2 * j)) & 0x03).astype(np.float32) - 2.0\n B = deq * B_scales.astype(np.float32)[:, None]\n return (A.astype(np.float32) @ B.T + bias.astype(np.float32)).astype(np.float32)\n"
|
| 65 |
+
}
|
definitions/gemm/gemm_u8_f32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "gemm_u8_f32_sme2_n256_k512",
|
| 3 |
+
"op_type": "gemm",
|
| 4 |
+
"description": "Quantized-input GEMM, N=256 K=512: C[m,n] = float(sum_k A[m,k]*B[n,k]) * C_scale[0] + bias[n] with A uint8 [M,K], B uint8 [N,K], exact int32 accumulation then converted to float32, scaled by one global fp32 scale and offset by a per-column fp32 bias. C_scale is a one-element tensor (axis S=1) because the raw ABI passes no scalars. Elementwise tolerance, no SQNR (output is already float32 and the arithmetic is deterministic). M varies per workload. Baseline: kai_matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa (SME2, new-style kai_matmul_uker API).",
|
| 5 |
+
"tags": [
|
| 6 |
+
"status:active",
|
| 7 |
+
"baseline-solution:kleidiai"
|
| 8 |
+
],
|
| 9 |
+
"axes": {
|
| 10 |
+
"M": {
|
| 11 |
+
"type": "var"
|
| 12 |
+
},
|
| 13 |
+
"N": {
|
| 14 |
+
"type": "const",
|
| 15 |
+
"value": 256
|
| 16 |
+
},
|
| 17 |
+
"K": {
|
| 18 |
+
"type": "const",
|
| 19 |
+
"value": 512
|
| 20 |
+
},
|
| 21 |
+
"S": {
|
| 22 |
+
"type": "const",
|
| 23 |
+
"value": 1
|
| 24 |
+
}
|
| 25 |
+
},
|
| 26 |
+
"inputs": {
|
| 27 |
+
"A": {
|
| 28 |
+
"shape": [
|
| 29 |
+
"M",
|
| 30 |
+
"K"
|
| 31 |
+
],
|
| 32 |
+
"dtype": "uint8"
|
| 33 |
+
},
|
| 34 |
+
"B": {
|
| 35 |
+
"shape": [
|
| 36 |
+
"N",
|
| 37 |
+
"K"
|
| 38 |
+
],
|
| 39 |
+
"dtype": "uint8"
|
| 40 |
+
},
|
| 41 |
+
"C_scale": {
|
| 42 |
+
"shape": [
|
| 43 |
+
"S"
|
| 44 |
+
],
|
| 45 |
+
"dtype": "float32"
|
| 46 |
+
},
|
| 47 |
+
"bias": {
|
| 48 |
+
"shape": [
|
| 49 |
+
"N"
|
| 50 |
+
],
|
| 51 |
+
"dtype": "float32"
|
| 52 |
+
}
|
| 53 |
+
},
|
| 54 |
+
"outputs": {
|
| 55 |
+
"C": {
|
| 56 |
+
"shape": [
|
| 57 |
+
"M",
|
| 58 |
+
"N"
|
| 59 |
+
],
|
| 60 |
+
"dtype": "float32"
|
| 61 |
+
}
|
| 62 |
+
},
|
| 63 |
+
"reference": "import numpy as np\n\n\ndef run(A, B, C_scale, bias):\n acc = (A.astype(np.int64) @ B.astype(np.int64).T).astype(np.int32).astype(np.float32)\n return (acc * C_scale.astype(np.float32)[0] + bias.astype(np.float32)).astype(np.float32)\n"
|
| 64 |
+
}
|
expected_sets_sve.json
CHANGED
|
@@ -57,12 +57,30 @@
|
|
| 57 |
"conv2d_depthwise_fp32_nhwc_kh3_kw3_s1_p1",
|
| 58 |
"conv2d_f16_nhwc_kh3_kw3_valid_c32_c64",
|
| 59 |
"conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64",
|
|
|
|
| 60 |
"gemm_bf16_n512_k512",
|
| 61 |
"gemm_f16_n512_k512",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
"gemm_fp32_n512_k512",
|
| 63 |
"gemm_fp32_sme2_n512_k512",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
"gemm_qs4c32_n256_k512",
|
| 65 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
],
|
| 67 |
"simd-loop": [
|
| 68 |
"loop_001",
|
|
|
|
| 57 |
"conv2d_depthwise_fp32_nhwc_kh3_kw3_s1_p1",
|
| 58 |
"conv2d_f16_nhwc_kh3_kw3_valid_c32_c64",
|
| 59 |
"conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64",
|
| 60 |
+
"conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2",
|
| 61 |
"gemm_bf16_n512_k512",
|
| 62 |
"gemm_f16_n512_k512",
|
| 63 |
+
"gemm_f16_qai4c32_sme2_n256_k512",
|
| 64 |
+
"gemm_f16_qs4cx_sme2_n256_k512",
|
| 65 |
+
"gemm_f16_qs8cx_sme2_n256_k512",
|
| 66 |
+
"gemm_f16act_qs4c32_sme2_n256_k512",
|
| 67 |
+
"gemm_f32_bf16_n512_k512",
|
| 68 |
"gemm_fp32_n512_k512",
|
| 69 |
"gemm_fp32_sme2_n512_k512",
|
| 70 |
+
"gemm_i32_u8_sme2_n256_k512",
|
| 71 |
+
"gemm_qai4c32_n256_k512",
|
| 72 |
+
"gemm_qai4c32_sme2_n256_k512",
|
| 73 |
+
"gemm_qai8_qsi8cx_sme2_n256_k512",
|
| 74 |
+
"gemm_qai8dxp_qs4c32_sme2_n256_k512",
|
| 75 |
"gemm_qs4c32_n256_k512",
|
| 76 |
+
"gemm_qs4c32_sme2_n256_k512",
|
| 77 |
+
"gemm_qs4cx_n512_k512",
|
| 78 |
+
"gemm_qs4cx_sme2_n256_k512",
|
| 79 |
+
"gemm_qs8cx_n512_k512",
|
| 80 |
+
"gemm_qs8cx_sme2_n256_k512",
|
| 81 |
+
"gemm_qsu2cx_n256_k512",
|
| 82 |
+
"gemm_qsu2cx_sme2_n256_k512",
|
| 83 |
+
"gemm_u8_f32_sme2_n256_k512"
|
| 84 |
],
|
| 85 |
"simd-loop": [
|
| 86 |
"loop_001",
|
solutions/kleidiai/baseline-kleidiai-arm/conv2d/conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2",
|
| 3 |
+
"definition": "conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2: KleidiAI kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa with its x32p2vlx1 LHS/RHS imatmul packers.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_conv2d",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa.h",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# kai_lhs_imatmul_pack_x32p2vlx1_x32p_sme to pack the LHS matrix.\n/// -# kai_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme to pack the RHS matrix.\n\n/// Gets m step value.\n///\n/// The starting row index must be divisible by `m_step`.\n///\n/// @return The m step value.\nsize_t kai_get_m_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(void);\n\n/// Gets n step value.\n///\n/// The starting column index must be divisible by `n_step`.\n///\n/// @return The n step value.\nsize_t kai_get_n_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(void);\n\n/// Gets the offset in bytes to the data element in the packed LHS matrix buffer.\n///\n/// @param[in] m_idx Row index in the unpacked LHS matrix. Must be a multiple of `m_step`.\n/// @param[in] k_chunk_count Number of LHS column splits.\n/// @param[in] k_chunk_length Length of a LHS column split.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_lhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t m_idx, size_t k_chunk_count, size_t k_chunk_length);\n\n/// Gets the offset in bytes to the data element in the packed RHS matrix buffer.\n///\n/// @param[in] n_idx Column index in the unpacked RHS matrix. Must be a multiple of `n_step`.\n/// @param[in] k_chunk_count Number of LHS column splits.\n/// @param[in] k_chunk_length Length of a LHS column split.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_rhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t n_idx, size_t k_chunk_count, size_t k_chunk_length);\n\n/// Gets the offset in bytes to the data element in the destination matrix buffer.\n///\n/// @param[in] m_idx Row index. Must be a multiple of `m_step`.\n/// @param[in] n_idx Column index. Must be a multiple of `n_step`.\n/// @param[in] dst_stride_row Row stride in bytes.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_dst_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride_row);\n\n/// Gets the size in bytes of the destination matrix buffer.\n///\n/// @param[in] m Number of rows.\n/// @param[in] n Number of columns.\n///\n/// @return The size in bytes of the destination matrix buffer.\nsize_t kai_get_dst_size_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(size_t m, size_t n);\n\n/// Runs the matrix multiplication microkernel followed by a clamp operation.\n///\n/// The pointer of each buffers (packed LHS, packed RHS and output) needs to be added with offset\n/// calculated using the following functions:\n///\n/// * Packed LHS: @ref kai_get_lhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa.\n/// * Packed RHS: @ref kai_get_rhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa.\n/// * Output: @ref kai_get_dst_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa.\n///\n/// @param[in] m Number of output rows to be computed.\n/// @param[in] n Number of output columns to be computed.\n/// @param[in] k_chunk_count Number of LHS column splits.\n/// @param[in] k_chunk_length Length of a LHS column split.\n/// @param[in] lhs_packed Packed LHS matrix buffer.\n/// @param[in] rhs_packed Packed RHS matrix buffer.\n/// @param[out] dst Output matrix buffer.\n/// @param[in] dst_stride_row Row stride in bytes of the output matrix.\n/// @param[in] clamp_min Minimum value to clamp the final result.\n/// @param[in] clamp_max Maximum value to clamp the final result.\n///\n/// @note Clamp minimum and maximum values are cast internally to the destination type before clamping the computed\n/// values.\n///\nvoid kai_run_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length, const void* lhs_packed, const void* rhs_packed,\n void* dst, size_t dst_stride_row, float clamp_min, float clamp_max);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa.c",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n#include \"kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\ntypedef struct {\n const void* A;\n const void* B;\n void* C;\n uint64_t ldcb;\n uint64_t M;\n uint64_t N;\n uint64_t K;\n float min;\n float max;\n void* accumulator_buffer;\n uint64_t flags;\n} KernelArgs;\n\nstatic const size_t kai_mr = 2;\nstatic const size_t kai_nr = 2;\nstatic const size_t kai_kr = 1;\n\nvoid kai_kernel_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(KernelArgs* args);\n\n// Returns a constant value specific to this kernel that's relative to vector length\nstatic size_t kai_get_kernel_vec_length_constant(void) {\n const size_t kernel_vec_length_constant = kai_get_sme_vector_length_u32() / kai_kr;\n return kernel_vec_length_constant;\n}\n\nsize_t kai_get_m_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(void) {\n return kai_mr * kai_get_kernel_vec_length_constant();\n}\n\nsize_t kai_get_n_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(void) {\n return kai_nr * kai_get_kernel_vec_length_constant();\n}\n\nsize_t kai_get_lhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t m_idx, size_t k_chunk_count, size_t k_chunk_length) {\n KAI_ASSUME(m_idx % kai_get_m_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa() == 0);\n return m_idx * k_chunk_count * kai_roundup(k_chunk_length, kai_kr) * sizeof(float);\n}\n\nstatic size_t kai_get_rhs_packed_stride_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t k_chunk_count, size_t k_chunk_length) {\n return kai_get_n_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa() *\n (sizeof(float) + k_chunk_count * kai_roundup(k_chunk_length, kai_kr) * sizeof(float));\n}\n\nsize_t kai_get_rhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t n_idx, size_t k_chunk_count, size_t k_chunk_length) {\n KAI_ASSUME(n_idx % kai_get_n_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa() == 0);\n const size_t block_idx = n_idx / kai_get_n_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa();\n return block_idx *\n kai_get_rhs_packed_stride_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n k_chunk_count, k_chunk_length);\n}\n\nsize_t kai_get_dst_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride_row) {\n KAI_ASSUME(m_idx % kai_get_m_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa() == 0);\n KAI_ASSUME(n_idx % kai_get_n_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa() == 0);\n\n return m_idx * dst_stride_row + n_idx * sizeof(float);\n}\n\nsize_t kai_get_dst_size_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(size_t m, size_t n) {\n return m * n * sizeof(float);\n}\n\nvoid kai_run_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length, const void* lhs_packed, const void* rhs_packed,\n void* dst, size_t dst_stride_row, float clamp_min, float clamp_max) {\n KernelArgs args;\n\n args.A = lhs_packed;\n args.B = rhs_packed;\n args.C = dst;\n args.ldcb = dst_stride_row;\n args.M = m;\n args.N = n;\n args.K = k_chunk_count * kai_roundup(k_chunk_length, kai_kr);\n args.min = clamp_min;\n args.max = clamp_max;\n args.accumulator_buffer = NULL;\n args.flags = 0;\n\n kai_commit_za();\n\n kai_kernel_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa_asm.S",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa)\n stp x20, x21, [sp, -144]!\n stp x22, x23, [sp, 16]\n stp x24, x25, [sp, 32]\n stp x26, x27, [sp, 48]\n str x28, [sp, 64]\n stp d8, d9, [sp, 72]\n stp d10, d11, [sp, 88]\n stp d12, d13, [sp, 104]\n stp d14, d15, [sp, 120]\n KAI_ASM_INST(0xd503477f) // SMSTART ZA\n mov x15, #0x0\n ptrue p0.b\n KAI_ASM_INST(0x25207811) // ptrue pn9.b\n ldr x14, [x0, #0x30]\n ldr w13, [x0, #0x20]\n mov x11, #0x0\n ldr w10, [x0, #0x28]\n ldr x9, [x0, #0x0]\nKAI_ASM_LABEL(label_1) // M loop\n ldr x28, [x0, #0x8]\nKAI_ASM_LABEL(label_2) // N loop\n fmov z22.s, #1.0\n KAI_ASM_INST(0xa040478c) // ld1w { z12.s-z13.s }, pn9.b/Z, [x28]\n KAI_ASM_INST(0xc00800ff) // zero { zad0, zad1, zad2, zad3, zad4, zad5, zad6, zad7 }\n mov x27, x9\n KAI_ASM_INST(0x25aa4570) // whilelt pn8.s, x11, x10, VLx2\n addvl x28, x28, #2\n KAI_ASM_INST(0x808c02c0) // fmopa za0.s, p0/M, p0/M, z22.s, z12.s\n KAI_ASM_INST(0x808d02c1) // fmopa za1.s, p0/M, p0/M, z22.s, z13.s\n KAI_ASM_INST(0x808c02c2) // fmopa za2.s, p0/M, p0/M, z22.s, z12.s\n KAI_ASM_INST(0x808d02c3) // fmopa za3.s, p0/M, p0/M, z22.s, z13.s\n lsr x21, x14, #0x2\n and x20, x14, #0x3\n cbz x21, label_6\n subs x21, x21, #0x1\n KAI_ASM_INST(0xa040c764) // ld1w { z4.s-z7.s }, pn9.b/Z, [x27]\n KAI_ASM_INST(0xa041c768) // ld1w { z8.s-z11.s }, pn9.b/Z, [x27, #0x4, MUL VL]\n addvl x27, x27, #8\n KAI_ASM_INST(0xa040c794) // ld1w { z20.s-z23.s }, pn9.b/Z, [x28]\n KAI_ASM_INST(0xa041c78c) // ld1w { z12.s-z15.s }, pn9.b/Z, [x28, #0x4, MUL VL]\n addvl x28, x28, #8\n ble label_5\nKAI_ASM_LABEL(label_4) // K loop\n KAI_ASM_INST(0x80940080) // fmopa za0.s, p0/M, p0/M, z4.s, z20.s\n subs x21, x21, #0x1\n KAI_ASM_INST(0x80950081) // fmopa za1.s, p0/M, p0/M, z4.s, z21.s\n KAI_ASM_INST(0x809400a2) // fmopa za2.s, p0/M, p0/M, z5.s, z20.s\n KAI_ASM_INST(0x809500a3) // fmopa za3.s, p0/M, p0/M, z5.s, z21.s\n KAI_ASM_INST(0x809600c0) // fmopa za0.s, p0/M, p0/M, z6.s, z22.s\n KAI_ASM_INST(0x809700c1) // fmopa za1.s, p0/M, p0/M, z6.s, z23.s\n KAI_ASM_INST(0x809600e2) // fmopa za2.s, p0/M, p0/M, z7.s, z22.s\n KAI_ASM_INST(0x809700e3) // fmopa za3.s, p0/M, p0/M, z7.s, z23.s\n KAI_ASM_INST(0xa040c764) // ld1w { z4.s-z7.s }, pn9.b/Z, [x27]\n KAI_ASM_INST(0x808c0100) // fmopa za0.s, p0/M, p0/M, z8.s, z12.s\n KAI_ASM_INST(0xa040c794) // ld1w { z20.s-z23.s }, pn9.b/Z, [x28]\n KAI_ASM_INST(0x808d0101) // fmopa za1.s, p0/M, p0/M, z8.s, z13.s\n KAI_ASM_INST(0x808c0122) // fmopa za2.s, p0/M, p0/M, z9.s, z12.s\n KAI_ASM_INST(0x808d0123) // fmopa za3.s, p0/M, p0/M, z9.s, z13.s\n KAI_ASM_INST(0x808e0140) // fmopa za0.s, p0/M, p0/M, z10.s, z14.s\n KAI_ASM_INST(0x808f0141) // fmopa za1.s, p0/M, p0/M, z10.s, z15.s\n KAI_ASM_INST(0x808e0162) // fmopa za2.s, p0/M, p0/M, z11.s, z14.s\n KAI_ASM_INST(0x808f0163) // fmopa za3.s, p0/M, p0/M, z11.s, z15.s\n KAI_ASM_INST(0xa041c768) // ld1w { z8.s-z11.s }, pn9.b/Z, [x27, #0x4, MUL VL]\n addvl x27, x27, #8\n KAI_ASM_INST(0xa041c78c) // ld1w { z12.s-z15.s }, pn9.b/Z, [x28, #0x4, MUL VL]\n addvl x28, x28, #8\n bgt label_4\nKAI_ASM_LABEL(label_5) // K loop tail\n KAI_ASM_INST(0x80940080) // fmopa za0.s, p0/M, p0/M, z4.s, z20.s\n KAI_ASM_INST(0x80950081) // fmopa za1.s, p0/M, p0/M, z4.s, z21.s\n KAI_ASM_INST(0x809400a2) // fmopa za2.s, p0/M, p0/M, z5.s, z20.s\n KAI_ASM_INST(0x809500a3) // fmopa za3.s, p0/M, p0/M, z5.s, z21.s\n KAI_ASM_INST(0x809600c0) // fmopa za0.s, p0/M, p0/M, z6.s, z22.s\n KAI_ASM_INST(0x809700c1) // fmopa za1.s, p0/M, p0/M, z6.s, z23.s\n KAI_ASM_INST(0x809600e2) // fmopa za2.s, p0/M, p0/M, z7.s, z22.s\n KAI_ASM_INST(0x809700e3) // fmopa za3.s, p0/M, p0/M, z7.s, z23.s\n KAI_ASM_INST(0x808c0100) // fmopa za0.s, p0/M, p0/M, z8.s, z12.s\n KAI_ASM_INST(0x808d0101) // fmopa za1.s, p0/M, p0/M, z8.s, z13.s\n KAI_ASM_INST(0x808c0122) // fmopa za2.s, p0/M, p0/M, z9.s, z12.s\n KAI_ASM_INST(0x808d0123) // fmopa za3.s, p0/M, p0/M, z9.s, z13.s\n KAI_ASM_INST(0x808e0140) // fmopa za0.s, p0/M, p0/M, z10.s, z14.s\n KAI_ASM_INST(0x808f0141) // fmopa za1.s, p0/M, p0/M, z10.s, z15.s\n KAI_ASM_INST(0x808e0162) // fmopa za2.s, p0/M, p0/M, z11.s, z14.s\n KAI_ASM_INST(0x808f0163) // fmopa za3.s, p0/M, p0/M, z11.s, z15.s\nKAI_ASM_LABEL(label_6) // K oddments\n cbz x20, label_8\nKAI_ASM_LABEL(label_7) // K oddments: Loop\n KAI_ASM_INST(0xa040477c) // ld1w { z28.s-z29.s }, pn9.b/Z, [x27]\n subs x20, x20, #0x1\n addvl x27, x27, #2\n KAI_ASM_INST(0xa1404787) // ld1w { z7.s, z15.s }, pn9.b/Z, [x28]\n addvl x28, x28, #2\n KAI_ASM_INST(0x80870380) // fmopa za0.s, p0/M, p0/M, z28.s, z7.s\n KAI_ASM_INST(0x808f0381) // fmopa za1.s, p0/M, p0/M, z28.s, z15.s\n KAI_ASM_INST(0x808703a2) // fmopa za2.s, p0/M, p0/M, z29.s, z7.s\n KAI_ASM_INST(0x808f03a3) // fmopa za3.s, p0/M, p0/M, z29.s, z15.s\n bgt label_7\nKAI_ASM_LABEL(label_8) // K oddments: End\n ldr x26, [x0, #0x10]\n sub x25, x13, x15\n cntw x24\n KAI_ASM_INST(0x854ec01a) // ld1rw { z26.s }, p0/Z, [x0, #56]\n ldr x23, [x0, #0x18]\n cmp x25, x24\n KAI_ASM_INST(0x854fc018) // ld1rw { z24.s }, p0/Z, [x0, #60]\n mov x12, #0x0\n csel x22, x25, x24, LT\n add x26, x26, x11, LSL #2 // C += n\n lsr x21, x22, #0x2\n madd x26, x15, x23, x26 // C += m * ldc\n and x20, x22, #0x3\n cbz x21, label_11\nKAI_ASM_LABEL(label_10) // Store to output array: Accumulator row 0 loop\n KAI_ASM_INST(0xc0860404) // mova { z4.s-z7.s }, za0h.s[x12]\n KAI_ASM_INST(0xc086042c) // mova { z12.s-z15.s }, za1h.s[x12]\n KAI_ASM_INST(0xc1b8cb44) // fclamp { z4.s-z7.s }, z26.s, z24.s\n KAI_ASM_INST(0xc1b8cb4c) // fclamp { z12.s-z15.s }, z26.s, z24.s\n add x12, x12, #0x4\n cmp x12, x21, LSL #2\n KAI_ASM_INST(0xa1604344) // st1w { z4.s, z12.s }, p8, [x26]\n add x26, x26, x23\n KAI_ASM_INST(0xa1604345) // st1w { z5.s, z13.s }, p8, [x26]\n add x26, x26, x23\n KAI_ASM_INST(0xa1604346) // st1w { z6.s, z14.s }, p8, [x26]\n add x26, x26, x23\n KAI_ASM_INST(0xa1604347) // st1w { z7.s, z15.s }, p8, [x26]\n add x26, x26, x23\n blt label_10\nKAI_ASM_LABEL(label_11) // Store to output array: Accumulator row 0 oddments\n cbz x20, label_12\n KAI_ASM_INST(0xc0860400) // mova { z0.s-z3.s }, za0h.s[x12]\n KAI_ASM_INST(0xc0860428) // mova { z8.s-z11.s }, za1h.s[x12]\n subs x20, x20, #0x1\n KAI_ASM_INST(0xc1b8cb40) // fclamp { z0.s-z3.s }, z26.s, z24.s\n KAI_ASM_INST(0xc1b8cb48) // fclamp { z8.s-z11.s }, z26.s, z24.s\n KAI_ASM_INST(0xa1604340) // st1w { z0.s, z8.s }, p8, [x26]\n add x26, x26, x23\n beq label_12\n subs x20, x20, #0x1\n KAI_ASM_INST(0xa1604341) // st1w { z1.s, z9.s }, p8, [x26]\n add x26, x26, x23\n beq label_12\n KAI_ASM_INST(0xa1604342) // st1w { z2.s, z10.s }, p8, [x26]\n add x26, x26, x23\nKAI_ASM_LABEL(label_12) // Store to output array: Accumulator row 0 oddments: End\n subs x25, x25, x22\n beq label_16\n cmp x25, x24\n mov x12, #0x0\n csel x20, x25, x24, LT\n lsr x21, x20, #0x2\n and x20, x20, #0x3\n cbz x21, label_14\nKAI_ASM_LABEL(label_13) // Store to output array: Accumulator row 1 loop\n KAI_ASM_INST(0xc0860454) // mova { z20.s-z23.s }, za2h.s[x12]\n KAI_ASM_INST(0xc086047c) // mova { z28.s-z31.s }, za3h.s[x12]\n KAI_ASM_INST(0xc1b8cb54) // fclamp { z20.s-z23.s }, z26.s, z24.s\n KAI_ASM_INST(0xc1b8cb5c) // fclamp { z28.s-z31.s }, z26.s, z24.s\n add x12, x12, #0x4\n cmp x12, x21, LSL #2\n KAI_ASM_INST(0xa1604354) // st1w { z20.s, z28.s }, p8, [x26]\n add x26, x26, x23\n KAI_ASM_INST(0xa1604355) // st1w { z21.s, z29.s }, p8, [x26]\n add x26, x26, x23\n KAI_ASM_INST(0xa1604356) // st1w { z22.s, z30.s }, p8, [x26]\n add x26, x26, x23\n KAI_ASM_INST(0xa1604357) // st1w { z23.s, z31.s }, p8, [x26]\n add x26, x26, x23\n blt label_13\nKAI_ASM_LABEL(label_14) // Store to output array: Accumulator row 1 oddments\n cbz x20, label_15\n KAI_ASM_INST(0xc0860444) // mova { z4.s-z7.s }, za2h.s[x12]\n KAI_ASM_INST(0xc086046c) // mova { z12.s-z15.s }, za3h.s[x12]\n subs x20, x20, #0x1\n KAI_ASM_INST(0xc1b8cb44) // fclamp { z4.s-z7.s }, z26.s, z24.s\n KAI_ASM_INST(0xc1b8cb4c) // fclamp { z12.s-z15.s }, z26.s, z24.s\n KAI_ASM_INST(0xa1604344) // st1w { z4.s, z12.s }, p8, [x26]\n add x26, x26, x23\n beq label_15\n subs x20, x20, #0x1\n KAI_ASM_INST(0xa1604345) // st1w { z5.s, z13.s }, p8, [x26]\n add x26, x26, x23\n beq label_15\n KAI_ASM_INST(0xa1604346) // st1w { z6.s, z14.s }, p8, [x26]\nKAI_ASM_LABEL(label_15) // Store to output array: Accumulator row 1 oddments: End\nKAI_ASM_LABEL(label_16) // Store to output array: End\n incw x11, ALL, MUL #2\n cmp x11, x10\n blt label_2\n incw x15, ALL, MUL #2\n mov x11, #0x0\n cmp x15, x13\n mov x9, x27\n blt label_1\n KAI_ASM_INST(0xd503467f) // SMSTOP\n ldp x22, x23, [sp, 16]\n ldp x24, x25, [sp, 32]\n ldp x26, x27, [sp, 48]\n ldr x28, [sp, 64]\n ldp d8, d9, [sp, 72]\n ldp d10, d11, [sp, 88]\n ldp d12, d13, [sp, 104]\n ldp d14, d15, [sp, 120]\n ldp x20, x21, [sp], 144\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_imatmul_pack_x32p2vlx1_x32p_sme.h",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Gets m step value.\n///\n/// The starting row index must be divisible by `m_step`.\n///\n/// @return The m step value.\nsize_t kai_get_m_step_lhs_imatmul_pack_x32p2vlx1_x32p_sme(void);\n\n/// Gets the offset in bytes to the data element in the packed LHS buffer.\n///\n/// @param[in] m_idx Row index in the unpacked LHS matrix.\n/// @param[in] k_chunk_count Number of LHS column splits.\n/// @param[in] k_chunk_length Length of a LHS column split.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_lhs_packed_offset_lhs_imatmul_pack_x32p2vlx1_x32p_sme(\n size_t m_idx, size_t k_chunk_count, size_t k_chunk_length);\n\n/// Gets the size in bytes of the packed LHS buffer.\n///\n/// @param[in] m Number of rows in the unpacked LHS matrix.\n/// @param[in] k_chunk_count Number of LHS column splits.\n/// @param[in] k_chunk_length Length of a LHS column split.\n///\n/// @return The size in bytes of the packed LHS buffer.\nsize_t kai_get_lhs_packed_size_lhs_imatmul_pack_x32p2vlx1_x32p_sme(\n size_t m, size_t k_chunk_count, size_t k_chunk_length);\n\n/// Pack the LHS matrix for use with indirect matrix multiplication\n///\n/// @param[in] m Number of rows of the unpacked LHS matrix.\n/// @param[in] k_chunk_count Number of LHS column splits.\n/// @param[in] k_chunk_length Length of a LHS column split.\n/// @param[in] lhs_ptrs Pointer to an array of input pointers consisting of\n/// `roundup(m, m_step) * k_chunk_count` pointers. The pointers are\n/// grouped in blocks of `m_step` rows and `k_chunk_count` columns.\n/// Within each block, the pointers are stored in column-major order.\n/// @param[in] lhs_ptr_offset Offset to add to each pointer of the @ref lhs_ptrs\n/// array, excluding zero pointers.\n/// @param[in] pad_ptr Pointer to chunk used for padding. @ref lhs_ptr_offset is\n/// not applied to this pointer when used in @ref lhs_ptrs. This can\n/// be NULL if there is no padding used @ref lhs_ptrs\n/// @param[out] lhs_packed Packed LHS matrix.\nvoid kai_run_lhs_imatmul_pack_x32p2vlx1_x32p_sme(\n size_t m, size_t k_chunk_count, size_t k_chunk_length, const void* const* lhs_ptrs, size_t lhs_ptr_offset,\n const void* pad_ptr, void* lhs_packed);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_imatmul_pack_x32p2vlx1_x32p_sme.c",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n#include \"kai_lhs_imatmul_pack_x32p2vlx1_x32p_sme.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nenum {\n MR = 2,\n KR = 1,\n MAX_M_STEP = MR * (KAI_SME_VEC_LENGTH_MAX_BYTES / sizeof(float)) / KR,\n};\n\nvoid kai_kernel_lhs_imatmul_pack_x32p2vlx1_x32p_sme(size_t height, size_t width, const void* in, void* out);\n\nstatic size_t kai_get_mr_lhs_imatmul_pack_x32p2vlx1_x32p_sme(void) {\n return MR * kai_get_sme_vector_length_u32() / KR;\n}\n\nsize_t kai_get_m_step_lhs_imatmul_pack_x32p2vlx1_x32p_sme(void) {\n return kai_get_mr_lhs_imatmul_pack_x32p2vlx1_x32p_sme();\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_imatmul_pack_x32p2vlx1_x32p_sme(\n size_t m_idx, size_t k_chunk_count, size_t k_chunk_length) {\n KAI_ASSUME(m_idx % kai_get_m_step_lhs_imatmul_pack_x32p2vlx1_x32p_sme() == 0);\n\n return m_idx * k_chunk_count * kai_roundup(k_chunk_length, KR) * sizeof(float);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_imatmul_pack_x32p2vlx1_x32p_sme(\n size_t m, size_t k_chunk_count, size_t k_chunk_length) {\n const size_t m_end = kai_roundup(m, kai_get_mr_lhs_imatmul_pack_x32p2vlx1_x32p_sme());\n return kai_get_lhs_packed_offset_lhs_imatmul_pack_x32p2vlx1_x32p_sme(m_end, k_chunk_count, k_chunk_length);\n}\n\nvoid kai_run_lhs_imatmul_pack_x32p2vlx1_x32p_sme(\n size_t m, size_t k_chunk_count, size_t k_chunk_length, const void* const* lhs_ptrs, size_t lhs_ptr_offset,\n const void* pad_ptr, void* lhs_packed) {\n KAI_ASSUME(lhs_ptrs != NULL);\n KAI_ASSUME(lhs_packed != NULL);\n\n const size_t m_step = kai_get_mr_lhs_imatmul_pack_x32p2vlx1_x32p_sme();\n const size_t table_size = kai_roundup(m, m_step) * k_chunk_count;\n const size_t width = k_chunk_length;\n\n KAI_ASSERT(m_step <= MAX_M_STEP);\n const uint8_t* in[MAX_M_STEP];\n\n uint8_t* out_base = lhs_packed;\n\n kai_commit_za();\n\n for (size_t i_m = 0; i_m < m; i_m += m_step) {\n for (size_t i_k_chunk = 0; i_k_chunk < k_chunk_count; i_k_chunk += 1) {\n const size_t height = KAI_MIN(m - i_m, m_step);\n void* out = out_base;\n out_base += m_step * kai_roundup(k_chunk_length, KR) * sizeof(float);\n for (size_t y = 0; y < height; y += 1) {\n const size_t lhs_ptr_idx = i_m * k_chunk_count + i_k_chunk * m_step + y;\n KAI_ASSERT(lhs_ptr_idx < table_size);\n in[y] = lhs_ptrs[lhs_ptr_idx];\n if (in[y] != pad_ptr) {\n uintptr_t in_ptr = (uintptr_t)in[y] + lhs_ptr_offset;\n in[y] = (const uint8_t*)in_ptr; // NOLINT(performance-no-int-to-ptr)\n }\n }\n\n // NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion)\n kai_kernel_lhs_imatmul_pack_x32p2vlx1_x32p_sme(height, width, in, out);\n }\n }\n}\n\n#endif // Architectural features check.\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_lhs_imatmul_pack_x32p2vlx1_x32p_sme_asm.S",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(lhs_imatmul_pack_x32p2vlx1_x32p_sme)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_lhs_imatmul_pack_x32p2vlx1_x32p_sme)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_lhs_imatmul_pack_x32p2vlx1_x32p_sme)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_lhs_imatmul_pack_x32p2vlx1_x32p_sme)\n stp x20, x21, [sp, -144]!\n stp x22, x23, [sp, 16]\n stp x24, x25, [sp, 32]\n stp x26, x27, [sp, 48]\n str x28, [sp, 64]\n stp d8, d9, [sp, 72]\n stp d10, d11, [sp, 88]\n stp d12, d13, [sp, 104]\n stp d14, d15, [sp, 120]\n KAI_ASM_INST(0xd503477f) // SMSTART ZA\n mov x22, #0x0\n mov x21, x1\n cntw x16\n incw x21\n mov x20, x1\n sub x21, x21, #0x1\n sub x15, x16, #0x1\n udiv x21, x21, x16 // n_passes = ceildiv(width, VL<T>)\n ands x15, x20, x15\n sub x20, x21, #0x1\n sub x14, x16, #0x2\n mov x11, #0x0\n mov x10, x2\n add x9, x2, x16, LSL #3\n cntw x28, ALL, MUL #2\n ldr x27, [x10, #0x0]\n cntw x26, ALL, MUL #3\n lsr x20, x20, #0x1 // n_loops = (n_passes - 1) / 2\n ldr x25, [x9, #0x0]\n and x24, x21, #0x1 // odd_tail = bool(n_passes & 0x1)\n csel x15, x15, x16, NE\n ldr x23, [x10, #0x8]\n ptrue p12.s\n whilelt p11.s, XZR, x0\n ldr x21, [x9, #0x8]\n whilelt p10.s, x16, x0\n mov x22, x22\n whilelt p9.s, x11, x1\n whilelt p8.s, x11, x1\n add x10, x10, #0x10\n add x9, x9, #0x10\n mov x12, #0x0\n cbz x14, label_2\nKAI_ASM_LABEL(label_1) // K loop: Charge: Loop\n KAI_ASM_INST(0x25306163) // psel p3.s, p8.s/Z, p11.s[w12]\n KAI_ASM_INST(0x25306142) // psel p2.s, p8.s/Z, p10.s[w12]\n KAI_ASM_INST(0x25706161) // psel p1.s, p8.s/Z, p11.s[w12, #1]\n KAI_ASM_INST(0x25706140) // psel p0.s, p8.s/Z, p10.s[w12, #1]\n KAI_ASM_INST(0xe0960f60) // ld1w { za0h.s[x12] }, p3/Z, [x27, x22, LSL #2]\n ldr x27, [x10, #0x0]\n KAI_ASM_INST(0xe0960b24) // ld1w { za1h.s[x12] }, p2/Z, [x25, x22, LSL #2]\n ldr x25, [x9, #0x0]\n KAI_ASM_INST(0xe09606e1) // ld1w { za0h.s[x12, #1] }, p1/Z, [x23, x22, LSL #2]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe09602a5) // ld1w { za1h.s[x12, #1] }, p0/Z, [x21, x22, LSL #2]\n add x12, x12, #0x2\n ldr x21, [x9, #0x8]\n add x9, x9, #0x10\n cmp x12, x14\n blt label_1\nKAI_ASM_LABEL(label_2) // K loop: Charge: End\n KAI_ASM_INST(0x25306163) // psel p3.s, p8.s/Z, p11.s[w12]\n KAI_ASM_INST(0x25306142) // psel p2.s, p8.s/Z, p10.s[w12]\n KAI_ASM_INST(0x25706161) // psel p1.s, p8.s/Z, p11.s[w12, #1]\n KAI_ASM_INST(0x25706140) // psel p0.s, p8.s/Z, p10.s[w12, #1]\n mov x10, x2\n add x9, x2, x16, LSL #3\n KAI_ASM_INST(0xe0960f60) // ld1w { za0h.s[x12] }, p3/Z, [x27, x22, LSL #2]\n ldr x27, [x10, #0x0]\n incw x11\n KAI_ASM_INST(0xe0960b24) // ld1w { za1h.s[x12] }, p2/Z, [x25, x22, LSL #2]\n ldr x25, [x9, #0x0]\n KAI_ASM_INST(0xe09606e1) // ld1w { za0h.s[x12, #1] }, p1/Z, [x23, x22, LSL #2]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe09602a5) // ld1w { za1h.s[x12, #1] }, p0/Z, [x21, x22, LSL #2]\n ldr x21, [x9, #0x8]\n add x9, x9, #0x10\n incw x22\n cbz x20, label_8\n mov x20, x20\nKAI_ASM_LABEL(label_3) // K loop: Main loop\n whilelt p8.s, x11, x1\n mov x13, #0x0\n cbz x14, label_5\nKAI_ASM_LABEL(label_4) // K loop: Main loop: First: Loop\n KAI_ASM_INST(0x25316160) // psel p0.s, p8.s/Z, p11.s[w13]\n KAI_ASM_INST(0x25316142) // psel p2.s, p8.s/Z, p10.s[w13]\n KAI_ASM_INST(0x25716161) // psel p1.s, p8.s/Z, p11.s[w13, #1]\n KAI_ASM_INST(0x25716143) // psel p3.s, p8.s/Z, p10.s[w13, #1]\n KAI_ASM_INST(0xe0962368) // ld1w { za2h.s[x13] }, p0/Z, [x27, x22, LSL #2]\n KAI_ASM_INST(0x25317120) // psel p0.s, p12.s/Z, p9.s[w13]\n ldr x27, [x10, #0x0]\n KAI_ASM_INST(0xe0962b2c) // ld1w { za3h.s[x13] }, p2/Z, [x25, x22, LSL #2]\n KAI_ASM_INST(0x25317122) // psel p2.s, p12.s/Z, p9.s[w13]\n ldr x25, [x9, #0x0]\n KAI_ASM_INST(0xe09626e9) // ld1w { za2h.s[x13, #1] }, p1/Z, [x23, x22, LSL #2]\n KAI_ASM_INST(0x25717121) // psel p1.s, p12.s/Z, p9.s[w13, #1]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe0962ead) // ld1w { za3h.s[x13, #1] }, p3/Z, [x21, x22, LSL #2]\n ldr x21, [x9, #0x8]\n KAI_ASM_INST(0xe0bfa060) // st1w { za0v.s[x13] }, p0/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0x25717120) // psel p0.s, p12.s/Z, p9.s[w13, #1]\n KAI_ASM_INST(0xe0b0a864) // st1w { za1v.s[x13] }, p2/Z, [x3, x16, LSL #2]\n add x9, x9, #0x10\n KAI_ASM_INST(0xe0bca461) // st1w { za0v.s[x13, #1] }, p1/Z, [x3, x28, LSL #2]\n KAI_ASM_INST(0xe0baa065) // st1w { za1v.s[x13, #1] }, p0/Z, [x3, x26, LSL #2]\n add x13, x13, #0x2\n addvl x3, x3, #4\n cmp x13, x14\n blt label_4\nKAI_ASM_LABEL(label_5) // K loop: Main loop: First: Tail\n KAI_ASM_INST(0x25316160) // psel p0.s, p8.s/Z, p11.s[w13]\n KAI_ASM_INST(0x25316142) // psel p2.s, p8.s/Z, p10.s[w13]\n KAI_ASM_INST(0x25716161) // psel p1.s, p8.s/Z, p11.s[w13, #1]\n KAI_ASM_INST(0x25716143) // psel p3.s, p8.s/Z, p10.s[w13, #1]\n mov x10, x2\n add x9, x2, x16, LSL #3\n KAI_ASM_INST(0xe0962368) // ld1w { za2h.s[x13] }, p0/Z, [x27, x22, LSL #2]\n KAI_ASM_INST(0x25317120) // psel p0.s, p12.s/Z, p9.s[w13]\n ldr x27, [x10, #0x0]\n mov x12, #0x0\n KAI_ASM_INST(0xe0962b2c) // ld1w { za3h.s[x13] }, p2/Z, [x25, x22, LSL #2]\n KAI_ASM_INST(0x25317122) // psel p2.s, p12.s/Z, p9.s[w13]\n ldr x25, [x9, #0x0]\n KAI_ASM_INST(0xe09626e9) // ld1w { za2h.s[x13, #1] }, p1/Z, [x23, x22, LSL #2]\n KAI_ASM_INST(0x25717121) // psel p1.s, p12.s/Z, p9.s[w13, #1]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe0962ead) // ld1w { za3h.s[x13, #1] }, p3/Z, [x21, x22, LSL #2]\n ldr x21, [x9, #0x8]\n KAI_ASM_INST(0xe0bfa060) // st1w { za0v.s[x13] }, p0/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0x25717120) // psel p0.s, p12.s/Z, p9.s[w13, #1]\n KAI_ASM_INST(0xe0b0a864) // st1w { za1v.s[x13] }, p2/Z, [x3, x16, LSL #2]\n whilelt p9.s, x11, x1\n incw x11\n KAI_ASM_INST(0xe0bca461) // st1w { za0v.s[x13, #1] }, p1/Z, [x3, x28, LSL #2]\n add x9, x9, #0x10\n incw x22\n KAI_ASM_INST(0xe0baa065) // st1w { za1v.s[x13, #1] }, p0/Z, [x3, x26, LSL #2]\n addvl x3, x3, #4\n whilelt p8.s, x11, x1\n cbz x14, label_7\nKAI_ASM_LABEL(label_6) // K loop: Main loop: Second: Loop\n KAI_ASM_INST(0x25306160) // psel p0.s, p8.s/Z, p11.s[w12]\n KAI_ASM_INST(0x25306142) // psel p2.s, p8.s/Z, p10.s[w12]\n KAI_ASM_INST(0x25706161) // psel p1.s, p8.s/Z, p11.s[w12, #1]\n KAI_ASM_INST(0x25706143) // psel p3.s, p8.s/Z, p10.s[w12, #1]\n KAI_ASM_INST(0xe0960360) // ld1w { za0h.s[x12] }, p0/Z, [x27, x22, LSL #2]\n KAI_ASM_INST(0x25307120) // psel p0.s, p12.s/Z, p9.s[w12]\n ldr x27, [x10, #0x0]\n KAI_ASM_INST(0xe0960b24) // ld1w { za1h.s[x12] }, p2/Z, [x25, x22, LSL #2]\n KAI_ASM_INST(0x25307122) // psel p2.s, p12.s/Z, p9.s[w12]\n ldr x25, [x9, #0x0]\n KAI_ASM_INST(0xe09606e1) // ld1w { za0h.s[x12, #1] }, p1/Z, [x23, x22, LSL #2]\n KAI_ASM_INST(0x25707121) // psel p1.s, p12.s/Z, p9.s[w12, #1]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe0960ea5) // ld1w { za1h.s[x12, #1] }, p3/Z, [x21, x22, LSL #2]\n ldr x21, [x9, #0x8]\n KAI_ASM_INST(0xe0bf8068) // st1w { za2v.s[x12] }, p0/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0x25707120) // psel p0.s, p12.s/Z, p9.s[w12, #1]\n KAI_ASM_INST(0xe0b0886c) // st1w { za3v.s[x12] }, p2/Z, [x3, x16, LSL #2]\n add x9, x9, #0x10\n KAI_ASM_INST(0xe0bc8469) // st1w { za2v.s[x12, #1] }, p1/Z, [x3, x28, LSL #2]\n KAI_ASM_INST(0xe0ba806d) // st1w { za3v.s[x12, #1] }, p0/Z, [x3, x26, LSL #2]\n add x12, x12, #0x2\n addvl x3, x3, #4\n cmp x12, x14\n blt label_6\nKAI_ASM_LABEL(label_7) // K loop: Main loop: Second: Tail\n KAI_ASM_INST(0x25306160) // psel p0.s, p8.s/Z, p11.s[w12]\n KAI_ASM_INST(0x25306142) // psel p2.s, p8.s/Z, p10.s[w12]\n KAI_ASM_INST(0x25706161) // psel p1.s, p8.s/Z, p11.s[w12, #1]\n KAI_ASM_INST(0x25706143) // psel p3.s, p8.s/Z, p10.s[w12, #1]\n mov x10, x2\n add x9, x2, x16, LSL #3\n KAI_ASM_INST(0xe0960360) // ld1w { za0h.s[x12] }, p0/Z, [x27, x22, LSL #2]\n KAI_ASM_INST(0x25307120) // psel p0.s, p12.s/Z, p9.s[w12]\n ldr x27, [x10, #0x0]\n KAI_ASM_INST(0xe0960b24) // ld1w { za1h.s[x12] }, p2/Z, [x25, x22, LSL #2]\n KAI_ASM_INST(0x25307122) // psel p2.s, p12.s/Z, p9.s[w12]\n ldr x25, [x9, #0x0]\n KAI_ASM_INST(0xe09606e1) // ld1w { za0h.s[x12, #1] }, p1/Z, [x23, x22, LSL #2]\n KAI_ASM_INST(0x25707121) // psel p1.s, p12.s/Z, p9.s[w12, #1]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe0960ea5) // ld1w { za1h.s[x12, #1] }, p3/Z, [x21, x22, LSL #2]\n ldr x21, [x9, #0x8]\n KAI_ASM_INST(0xe0bf8068) // st1w { za2v.s[x12] }, p0/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0x25707120) // psel p0.s, p12.s/Z, p9.s[w12, #1]\n KAI_ASM_INST(0xe0b0886c) // st1w { za3v.s[x12] }, p2/Z, [x3, x16, LSL #2]\n whilelt p9.s, x11, x1\n subs x20, x20, #0x1\n KAI_ASM_INST(0xe0bc8469) // st1w { za2v.s[x12, #1] }, p1/Z, [x3, x28, LSL #2]\n add x9, x9, #0x10\n incw x11\n KAI_ASM_INST(0xe0ba806d) // st1w { za3v.s[x12, #1] }, p0/Z, [x3, x26, LSL #2]\n addvl x3, x3, #4\n incw x22\n bgt label_3\nKAI_ASM_LABEL(label_8) // K loop: Tails\n cbnz x24, label_11\n mov x10, x2\n whilelt p8.s, x11, x1\n mov x12, #0x0\nKAI_ASM_LABEL(label_9) // K loop: Tails: Even: First\n KAI_ASM_INST(0x25307123) // psel p3.s, p12.s/Z, p9.s[w12]\n KAI_ASM_INST(0x25307122) // psel p2.s, p12.s/Z, p9.s[w12]\n KAI_ASM_INST(0x25306161) // psel p1.s, p8.s/Z, p11.s[w12]\n KAI_ASM_INST(0x25306140) // psel p0.s, p8.s/Z, p10.s[w12]\n KAI_ASM_INST(0xe0bf8c60) // st1w { za0v.s[x12] }, p3/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0xe0b08864) // st1w { za1v.s[x12] }, p2/Z, [x3, x16, LSL #2]\n addvl x3, x3, #2\n ldr x21, [x10, #0x0]\n ldr x20, [x10, x16, LSL #0x3]\n add x10, x10, #0x8\n KAI_ASM_INST(0xe09606a8) // ld1w { za2h.s[x12] }, p1/Z, [x21, x22, LSL #2]\n KAI_ASM_INST(0xe096028c) // ld1w { za3h.s[x12] }, p0/Z, [x20, x22, LSL #2]\n add x12, x12, #0x1\n cmp x12, x16\n blt label_9\n whilelt p9.s, x11, x1\n whilelt p8.s, x11, x1\n mov x12, #0x0\nKAI_ASM_LABEL(label_10) // K loop: Tails: Even: Second\n KAI_ASM_INST(0x25307121) // psel p1.s, p12.s/Z, p9.s[w12]\n KAI_ASM_INST(0x25307120) // psel p0.s, p12.s/Z, p9.s[w12]\n KAI_ASM_INST(0xe0bf8468) // st1w { za2v.s[x12] }, p1/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0xe0b0806c) // st1w { za3v.s[x12] }, p0/Z, [x3, x16, LSL #2]\n add x12, x12, #0x1\n addvl x3, x3, #2\n cmp x12, x15\n blt label_10\n whilelt p8.s, x11, x1\n b label_13\nKAI_ASM_LABEL(label_11) // K loop: Tails: Odd\n mov x12, #0x0\nKAI_ASM_LABEL(label_12) // K loop: Tails: Odd: Loop\n KAI_ASM_INST(0x25307121) // psel p1.s, p12.s/Z, p9.s[w12]\n KAI_ASM_INST(0x25307120) // psel p0.s, p12.s/Z, p9.s[w12]\n KAI_ASM_INST(0xe0bf8460) // st1w { za0v.s[x12] }, p1/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0xe0b08064) // st1w { za1v.s[x12] }, p0/Z, [x3, x16, LSL #2]\n add x12, x12, #0x1\n addvl x3, x3, #2\n cmp x12, x15\n blt label_12\nKAI_ASM_LABEL(label_13) // K loop: End\n KAI_ASM_INST(0xd503467f) // SMSTOP\n ldp x22, x23, [sp, 16]\n ldp x24, x25, [sp, 32]\n ldp x26, x27, [sp, 48]\n ldr x28, [sp, 64]\n ldp d8, d9, [sp, 72]\n ldp d10, d11, [sp, 88]\n ldp d12, d13, [sp, 104]\n ldp d14, d15, [sp, 120]\n ldp x20, x21, [sp], 144\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_lhs_imatmul_pack_x32p2vlx1_x32p_sme)\n\n KAI_ASM_END\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Gets n step value.\n///\n/// The starting column index must be divisible by `n_step`.\n///\n/// @return The n step value.\nsize_t kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(void);\n\n/// Gets the offset in bytes to the data element in the RHS matrix buffer.\n///\n/// @param[in] n_idx Column index. Must be divisible by `n_step`\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_rhs_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(size_t n_idx);\n\n/// Gets the offset in bytes to the data element in the bias buffer.\n///\n/// @param[in] n_idx Column index.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_bias_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(size_t n_idx);\n\n/// Gets row stride in bytes of the packed RHS matrix.\n///\n/// @param[in] k_chunk_count Number of chunks.\n/// @param[in] k_chunk_length Number of rows in each chunk.\n///\n/// @return Row stride in bytes.\nsize_t kai_get_rhs_packed_stride_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n size_t k_chunk_count, size_t k_chunk_length);\n\n/// Gets the offset in bytes to the data element in the packed RHS buffer.\n///\n/// @param[in] n_idx Column index. Must be divisible by `n_step`\n/// @param[in] k_chunk_count Number of chunks.\n/// @param[in] k_chunk_length Number of rows in each chunk.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n size_t n_idx, size_t k_chunk_count, size_t k_chunk_length);\n\n/// Gets the size in bytes of the packed RHS buffer.\n///\n/// @param[in] n Number of columns.\n/// @param[in] k_chunk_count Number of chunks.\n/// @param[in] k_chunk_length Number of rows in each chunk.\n///\n/// @return The size in bytes of the packed RHS buffer.\nsize_t kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n size_t n, size_t k_chunk_count, size_t k_chunk_length);\n\n/// Runs the RHS packing function for matrix multiplication.\n///\n/// The pointer of each buffer (RHS, bias and packed RHS) needs to be added with offset\n/// calculated using the following functions:\n///\n/// * RHS: @ref kai_get_rhs_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme.\n/// * Bias: @ref kai_get_bias_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme.\n/// * Output: @ref kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme.\n///\n/// @param[in] n Number of columns of the output matrix.\n/// @param[in] k_chunk_count Number of chunks.\n/// @param[in] k_chunk_length Number of rows in each chunk.\n/// @param[in] rhs_stride_row Row stride in bytes of the RHS matrix.\n/// @param[in] rhs RHS matrix data buffer.\n/// @param[in] bias Bias matrix data buffer.\n/// @param[out] rhs_packed Packed RHS matrix.\nvoid kai_run_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n size_t n, size_t k_chunk_count, size_t k_chunk_length, size_t rhs_stride_row, const void* rhs, const void* bias,\n void* rhs_packed);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "kai_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme.c",
|
| 65 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n\n#include \"kai_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nenum {\n NR = 2,\n KR = 1,\n};\n\ntypedef struct {\n const void* bias_ptr;\n size_t width;\n size_t height;\n size_t k_chunk_count;\n size_t in_stride;\n size_t out_stride;\n const void* in;\n void* out;\n} KernelArgs;\n\nstatic const size_t kai_num_bytes_input = sizeof(uint32_t);\nstatic const size_t kai_num_bytes_output = sizeof(uint32_t);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\nvoid kai_kernel_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(const KernelArgs* args_ptr);\n\nsize_t kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(void) {\n return NR * kai_get_sme_vector_length_u32() / KR;\n}\n\nsize_t kai_get_rhs_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(size_t n_idx) {\n KAI_ASSUME(n_idx % kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme() == 0);\n\n return n_idx * kai_num_bytes_input;\n}\n\nsize_t kai_get_bias_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(size_t n_idx) {\n return n_idx * kai_num_bytes_bias;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n size_t k_chunk_count, size_t k_chunk_length) {\n return kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme() *\n (kai_num_bytes_bias + k_chunk_count * kai_roundup(k_chunk_length, KR) * kai_num_bytes_output);\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n size_t n_idx, size_t k_chunk_count, size_t k_chunk_length) {\n KAI_ASSUME(n_idx % kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme() == 0);\n\n const size_t block_idx = n_idx / kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme();\n return block_idx *\n kai_get_rhs_packed_stride_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(k_chunk_count, k_chunk_length);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n size_t n, size_t k_chunk_count, size_t k_chunk_length) {\n const size_t n_nr_blocks = kai_roundup(n, kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme());\n return kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n n_nr_blocks, k_chunk_count, k_chunk_length);\n}\n\nvoid kai_run_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n size_t n, size_t k_chunk_count, size_t k_chunk_length, size_t rhs_stride_row, const void* rhs, const void* bias,\n void* rhs_packed) {\n KAI_ASSUME(rhs != NULL);\n KAI_ASSUME(bias != NULL);\n KAI_ASSUME(rhs_packed != NULL);\n\n KernelArgs args;\n args.bias_ptr = bias;\n args.height = k_chunk_length;\n args.width = n;\n args.in = rhs;\n args.out = rhs_packed;\n args.k_chunk_count = k_chunk_count;\n args.in_stride = rhs_stride_row;\n args.out_stride =\n kai_get_rhs_packed_stride_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(k_chunk_count, k_chunk_length);\n\n kai_commit_za();\n\n kai_kernel_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"path": "kai_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme_asm.S",
|
| 69 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme)\n stp x20, x21, [sp, -144]!\n stp x22, x23, [sp, 16]\n stp x24, x25, [sp, 32]\n stp x26, x27, [sp, 48]\n str x28, [sp, 64]\n stp d8, d9, [sp, 72]\n stp d10, d11, [sp, 88]\n stp d12, d13, [sp, 104]\n stp d14, d15, [sp, 120]\n KAI_ASM_INST(0xd503477f) // SMSTART ZA\n ldr x14, [x0, #0x8]\n ptrue p2.b\n ldr x13, [x0, #0x38]\n ldr x24, [x0, #0x0]\n ldr x12, [x0, #0x10]\n mov x23, x14\n ldr x22, [x0, #0x18]\n mov x21, x13\n ldr x11, [x0, #0x20]\n ldr x10, [x0, #0x28]\n ldr x9, [x0, #0x30]\nKAI_ASM_LABEL(label_1) // Bias: Full loop\n mov x20, x23\n decw x23, ALL, MUL #2\n whilelt p1.s, XZR, x20\n decw x20\n whilelt p0.s, XZR, x20\n cmp x23, #0x0\n ld1w { z17.s }, p1/Z, [x24]\n ld1w { z16.s }, p0/Z, [x24, #1, MUL VL]\n incb x24, ALL, MUL #2\n st1w { z17.s }, p2, [x21]\n st1w { z16.s }, p2, [x21, #1, MUL VL]\n add x21, x21, x10\n bgt label_1\n incb x13, ALL, MUL #2\n mov x28, x22\nKAI_ASM_LABEL(label_2) // Chunk Loop\n mov x27, x12\n cmp x27, #0x4\n blt label_6\nKAI_ASM_LABEL(label_3) // Main row loop: Head\n mov x26, x9\n mov x25, x13\n add x24, x26, x11\n sub x27, x27, #0x4\n add x23, x24, x11\n mov x22, x14\n add x21, x23, x11\n add x9, x21, x11\nKAI_ASM_LABEL(label_4) // Main row loop: Column loop\n mov x20, x22\n decw x22, ALL, MUL #2\n whilelt p1.s, XZR, x20\n decw x20\n whilelt p0.s, XZR, x20\n cmp x22, #0x0\n ld1w { z23.s }, p1/Z, [x26]\n ld1w { z22.s }, p0/Z, [x26, #1, MUL VL]\n addvl x26, x26, #2\n ld1w { z21.s }, p1/Z, [x24]\n ld1w { z20.s }, p0/Z, [x24, #1, MUL VL]\n addvl x24, x24, #2\n ld1w { z19.s }, p1/Z, [x23]\n ld1w { z18.s }, p0/Z, [x23, #1, MUL VL]\n addvl x23, x23, #2\n ld1w { z17.s }, p1/Z, [x21]\n ld1w { z16.s }, p0/Z, [x21, #1, MUL VL]\n addvl x21, x21, #2\n st1w { z23.s }, p2, [x25]\n st1w { z22.s }, p2, [x25, #1, MUL VL]\n st1w { z21.s }, p2, [x25, #2, MUL VL]\n st1w { z20.s }, p2, [x25, #3, MUL VL]\n st1w { z19.s }, p2, [x25, #4, MUL VL]\n st1w { z18.s }, p2, [x25, #5, MUL VL]\n st1w { z17.s }, p2, [x25, #6, MUL VL]\n st1w { z16.s }, p2, [x25, #7, MUL VL]\n add x25, x25, x10\n bgt label_4\n cmp x27, #0x4\n addvl x13, x13, #8\n bge label_3\n cbz x27, label_10\nKAI_ASM_LABEL(label_6) // Main loop skip\nKAI_ASM_LABEL(label_7) // Tail row loop: Head\n mov x26, x9\n cntw x22, ALL, MUL #8\n add x9, x26, x11\n mov x25, x13\n sub x27, x27, #0x1\n mov x21, x14\nKAI_ASM_LABEL(label_8) // Tail row loop: Column loop\n mov x20, x21\n decw x21, ALL, MUL #2\n whilelt p1.s, XZR, x20\n decw x20\n whilelt p0.s, XZR, x20\n cmp x21, #0x0\n ld1w { z17.s }, p1/Z, [x26]\n ld1w { z16.s }, p0/Z, [x26, #1, MUL VL]\n add x26, x26, x22\n st1w { z17.s }, p2, [x25]\n st1w { z16.s }, p2, [x25, #1, MUL VL]\n add x25, x25, x10\n bgt label_8\n cmp x27, #0x1\n addvl x13, x13, #2\n bge label_7\nKAI_ASM_LABEL(label_10) // Done\n sub x28, x28, #0x1\n cbnz x28, label_2\n KAI_ASM_INST(0xd503467f) // SMSTOP\n ldp x22, x23, [sp, 16]\n ldp x24, x25, [sp, 32]\n ldp x26, x27, [sp, 48]\n ldr x28, [sp, 64]\n ldp d8, d9, [sp, 72]\n ldp d10, d11, [sp, 88]\n ldp d12, d13, [sp, 104]\n ldp d14, d15, [sp, 120]\n ldp x20, x21, [sp], 144\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme)\n\n KAI_ASM_END\n"
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"path": "binding.cpp",
|
| 73 |
+
"content": "// Binding for Arm KleidiAI's imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla\n// (pure SVE indirect-GEMM conv2d, clamp-fused) against\n// conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64's raw ABI:\n// armbench_entry_conv2d(input*, output*, weight*, N, H, W)\n// computing a 'same' conv2d (Kh=Kw=3, stride=1, pad=1) in NHWC/HWIO layout.\n// The N images are folded into the GEMM M dimension (M = N*H*W): the indirection\n// table just points each output pixel at its own image, so one imatmul pass covers\n// the whole batch.\n//\n// weight arrives already in KleidiAI's expected \"kxn\" RHS layout \u2014 HWIO\n// [Kh,Kw,C_in,C_out] flattens exactly to [K=Kh*Kw*C_in, N=C_out] row-major,\n// so no transpose is needed here (unlike gemm_fp32_n512_k512's [N,K] B).\n// input feeds the indirection buffer directly \u2014 this kernel variant has an\n// UNPACKED f32 LHS (no separate LHS-pack step, unlike KleidiAI's own SME2\n// conv2d example, which packs both LHS and RHS).\n//\n// Two things the .h docs alone don't make clear, confirmed empirically\n// (crashed otherwise) and against KleidiAI's own test suite\n// (test/tests/imatmul_test.cpp + test/reference/reorder.cpp):\n// 1. This kernel does NOT process an arbitrary M span in one call \u2014 the\n// caller must loop, one call per m_step-sized (or smaller, for the\n// final remainder) chunk, each starting at a multiple of m_step.\n// 2. The indirection buffer is NOT a flat [m_idx][chunk_idx] array. It's\n// block-reordered by m_step: M is padded up to a multiple of m_step,\n// and within each m_step-row block the layout is chunk-major/row-minor\n// (all m_step rows for chunk 0, then all m_step rows for chunk 1, \u2026) \u2014\n// i.e. test/reference/reorder.cpp's reorder_block<const void*>(flat,\n// M, k_chunk_count, m_step, 1). Each block is passed to its own\n// kai_run_imatmul_clamp_... call at the matching m_start offset.\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa.h\"\n#include \"kai_lhs_imatmul_pack_x32p2vlx1_x32p_sme.h\"\n#include \"kai_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme.h\"\n\nnamespace {\nconstexpr int C_in = 32;\nconstexpr int C_out = 64;\nconstexpr int Kh = 3;\nconstexpr int Kw = 3;\nconstexpr int PAD = 1;\n\n// True if `data` (size bytes) differs from the cached snapshot, and updates\n// the snapshot to match when it does. Content-based, not pointer-based:\n// bench.cli evaluates many workloads against one compiled .so, and a later,\n// genuinely different workload's input buffer can land at the exact address\n// an earlier (now-freed) workload's buffer used -- a pointer-identity cache\n// would then wrongly treat different data as unchanged and reuse a stale\n// pack. See github.com/ArtificialRay/CPU-Kernel-Baseline/issues/80. (Only\n// needed for buffers that bake in actual VALUES at cache-build time, like\n// the RHS weight pack below -- the indirection buffer further down stores\n// raw addresses, dereferenced fresh on every run call, so pointer+H/W\n// identity is sufficient for it; see its own comment.)\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale =\n snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n} // namespace\n\nextern \"C\" int armbench_entry_conv2d(const float* input, float* output, const float* weight, int N, int H, int W)\n{\n const size_t HW = (size_t)H * (size_t)W;\n const size_t M = (size_t)N * HW; // batch folded into the GEMM M dimension\n const size_t k_chunk_count = (size_t)(Kh * Kw);\n const size_t k_chunk_length = (size_t)C_in;\n const size_t m_step = kai_get_m_step_lhs_imatmul_pack_x32p2vlx1_x32p_sme();\n const size_t num_blocks = (M + m_step - 1) / m_step;\n\n // Zero-filled padding chunk: any indirection entry that would read\n // outside the (pad=1) input bounds points here instead. Pure function of\n // compile-time C_in -- build once ever, never invalidated.\n static float* pad_chunk = nullptr;\n if (pad_chunk == nullptr) {\n pad_chunk = static_cast<float*>(std::malloc(sizeof(float) * (size_t)C_in));\n if (pad_chunk == nullptr) return -1;\n std::memset(pad_chunk, 0, sizeof(float) * (size_t)C_in);\n }\n\n // Block-reordered indirection buffer \u2014 see this file's top comment.\n // num_blocks * m_step rows are allocated (M padded up to a multiple of\n // m_step); unused rows in the final block are left as pad_chunk\n // (never actually read, since each call passes the true m_this count).\n // Cached across calls that reuse the same input pointer AND N/H/W. Unlike\n // the RHS weight cache below, pointer identity is safe here (no\n // content-comparison needed): this buffer stores raw ADDRESSES\n // (input + offset), never input's VALUES, and the matmul run call\n // dereferences those addresses fresh on every timed call -- so reusing\n // a table built for the same (pointer, H, W) is byte-identical to\n // rebuilding it, regardless of what data currently lives at that\n // address (github.com/ArtificialRay/CPU-Kernel-Baseline/issues/80).\n static const float* cached_input = nullptr;\n static int cached_N = -1, cached_H = -1, cached_W = -1;\n static const void** indirection = nullptr;\n if (input != cached_input || N != cached_N || H != cached_H || W != cached_W) {\n std::free(indirection);\n const size_t total_slots = num_blocks * m_step * k_chunk_count;\n indirection = static_cast<const void**>(std::malloc(sizeof(void*) * total_slots));\n if (indirection == nullptr) { cached_input = nullptr; return -1; }\n for (size_t block = 0; block < num_blocks; ++block) {\n const size_t m_block_start = block * m_step;\n for (size_t chunk_idx = 0; chunk_idx < k_chunk_count; ++chunk_idx) {\n const int kh = (int)(chunk_idx / (size_t)Kw);\n const int kw = (int)(chunk_idx % (size_t)Kw);\n for (size_t row = 0; row < m_step; ++row) {\n const size_t dst_idx = block * m_step * k_chunk_count + chunk_idx * m_step + row;\n const size_t m_idx = m_block_start + row;\n if (m_idx >= M) {\n indirection[dst_idx] = pad_chunk;\n continue;\n }\n const size_t n_idx = m_idx / HW;\n const size_t hw_idx = m_idx % HW;\n const int oh = (int)(hw_idx / (size_t)W);\n const int ow = (int)(hw_idx % (size_t)W);\n const int ih = oh - PAD + kh;\n const int iw = ow - PAD + kw;\n if (ih >= 0 && ih < H && iw >= 0 && iw < W) {\n indirection[dst_idx] =\n input + ((n_idx * (size_t)H + (size_t)ih) * (size_t)W + (size_t)iw) * (size_t)C_in;\n } else {\n indirection[dst_idx] = pad_chunk;\n }\n }\n }\n }\n cached_input = input;\n cached_N = N;\n cached_H = H;\n cached_W = W;\n }\n\n // RHS (weight) is already [K=Kh*Kw*C_in, N=C_out] row-major (HWIO) \u2014\n // pack directly, no transpose. Bias required non-null by this packer\n // (same requirement as the plain rhs_pack_kxn used for gemm); zero =\n // no bias term in this definition (this is a fixed, compile-time-only\n // constant buffer, not user data, so it needs no cache key of its own).\n // Cached across calls whose weight content matches the last pack.\n static uint8_t* weight_snapshot = nullptr;\n static size_t weight_snapshot_size = 0;\n static void* rhs_packed = nullptr;\n const size_t weight_bytes = sizeof(float) * (size_t)C_out * k_chunk_count * k_chunk_length;\n if (refresh_cache(weight_snapshot, weight_snapshot_size, weight, weight_bytes)) {\n float* zero_bias = static_cast<float*>(std::malloc(sizeof(float) * (size_t)C_out));\n if (zero_bias == nullptr) return -1;\n std::memset(zero_bias, 0, sizeof(float) * (size_t)C_out);\n\n const size_t packed_size = kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n (size_t)C_out, k_chunk_count, k_chunk_length);\n std::free(rhs_packed);\n rhs_packed = std::malloc(packed_size);\n if (rhs_packed == nullptr) {\n std::free(zero_bias);\n return -1;\n }\n\n kai_run_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme(\n (size_t)C_out, k_chunk_count, k_chunk_length,\n /*rhs_stride_row=*/(size_t)C_out * sizeof(float),\n /*rhs=*/weight, /*bias=*/zero_bias, /*rhs_packed=*/rhs_packed);\n\n std::free(zero_bias);\n }\n\n // LHS: the pack READS input's values through the indirection table, so it is redone on EVERY call\n // into a grow-only scratch buffer (activation-side work, like a real inference).\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size =\n kai_get_lhs_packed_size_lhs_imatmul_pack_x32p2vlx1_x32p_sme(M, k_chunk_count, k_chunk_length);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_imatmul_pack_x32p2vlx1_x32p_sme(\n M, k_chunk_count, k_chunk_length, indirection, /*lhs_ptr_offset=*/0, pad_chunk, lhs_packed);\n }\n\n // SME2 mopa takes the full M in one call (no per-m_step loop, unlike the SVE kernel).\n kai_run_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(\n M, (size_t)C_out, k_chunk_count, k_chunk_length, lhs_packed, rhs_packed, output,\n (size_t)C_out * sizeof(float),\n -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n\n return 0;\n}\n"
|
| 74 |
+
}
|
| 75 |
+
]
|
| 76 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f16_qai4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_f16_qai4c32_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_f16_qai4c32_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_f16_qai4c32_sme2_n256_k512: KleidiAI kai_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.c",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\ntypedef struct {\n float* dst; // 0\n const void* lhs_packed; // 0x8\n const void* rhs_packed; // 0x10\n size_t dst_stride_row; // 0x18\n size_t lhs_packed_stride; // 0x20\n size_t rhs_packed_stride; // 0x28\n size_t bias; // 0x30\n size_t m; // 0x38\n size_t n; // 0x40\n size_t k; // 0x48\n size_t bl; // 0x50\n const int32_t* lut; // 0x58\n float min; // 0x60\n float max; // 0x64\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(KernelArgs* args_ptr);\n// Compute args\nstatic const size_t kai_m_step = 1; // Multiple of vector length\nstatic const size_t kai_n_step = 4; // Multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // Multiple of vector length\nstatic const size_t kai_nr = 4; // Multiple of vector length\nstatic const size_t kai_kr = 8;\nstatic const size_t kai_sr = 2;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 4;\nstatic const size_t kai_num_bytes_sum_lhs = 4;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_num_bytes_recip_qvalue_rhs = 2;\nstatic const size_t kai_num_bytes_multiplier_rhs = 4;\nstatic const size_t kai_num_bytes_offset_rhs = 4;\n\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 2;\n// Extra args\nstatic const size_t kai_num_bytes_bias = 4;\nstatic const size_t kai_bl = 32;\n\n// Look-up table used for int4->int8 convert\nstatic const int32_t lut[16] = {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};\n\ninline static size_t kai_get_num_bytes_per_block_lhs(size_t bl) {\n return (bl * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs + kai_num_bytes_sum_lhs;\n}\n\ninline static size_t kai_get_num_bytes_per_block_rhs(size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n size_t num_bytes_per_block_rhs =\n (bl / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs + kai_num_bytes_offset_rhs;\n return num_bytes_per_block_rhs;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t bl) {\n const size_t mr = kai_get_mr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n return mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block_lhs(bl);\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t bl) {\n KAI_ASSUME(bl % kai_bl == 0);\n KAI_ASSUME((k % kai_bl) == 0);\n\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl);\n const size_t nr = kai_get_nr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n size_t rhs_packed_stride = nr * (num_bytes_per_block * num_blocks_per_row);\n // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias\n rhs_packed_stride += nr * kai_num_bytes_bias;\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_n_step_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_mr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_nr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_kr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t k, size_t bl) {\n const size_t mr = kai_get_mr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n KAI_ASSUME((m_idx % mr) == 0);\n\n return (m_idx / mr) * kai_get_lhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t n_idx, size_t k, size_t bl) {\n KAI_ASSUME((k % bl) == 0);\n const size_t nr = kai_get_nr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n KAI_ASSUME((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n const size_t m_step = kai_get_m_step_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t n_step = kai_get_n_step_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n KAI_ASSUME((m_idx % m_step) == 0);\n KAI_ASSUME((n_idx % n_step) == 0);\n\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n void* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSUME(dst_stride_col == sizeof(uint16_t));\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl) == 0);\n\n if (m == 0) {\n return;\n }\n\n const size_t nr = kai_get_nr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n KernelArgs args;\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.dst_stride_row = dst_stride_row;\n args.lhs_packed_stride = kai_get_lhs_packed_stride(k, bl);\n args.rhs_packed_stride = kai_get_rhs_packed_stride(k, bl);\n args.bias = args.rhs_packed_stride - nr * kai_num_bytes_bias;\n args.m = m;\n args.n = n;\n args.k = k;\n args.bl = bl;\n args.lut = lut;\n args.min = scalar_min;\n args.max = scalar_max;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa_asm.S",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\n stp x19, x20, [sp, -128 ]!\n stp x21, x22, [sp, 16]\n stp x23, x24, [sp, 32]\n stp x25, x26, [sp, 48]\n stp d8, d9, [sp, 64]\n stp d10, d11, [sp, 80]\n stp d12, d13, [sp, 96]\n stp d14, d15, [sp, 112]\n KAI_ASM_INST(0xd503477f) // smstart\n cntw x14\n ptrue p0.b, all\n KAI_ASM_INST(0x25a07810) // ptrue pn8.s\n cntw x5 // mr\n lsl x5, x5, #2\n whilelt p4.b, xzr, x5\n ldr x6, [x0, #0x58] // lut\n KAI_ASM_INST(0xe11f80c0) // ldr zt0, [x6]\n ldr x19, [x0, #0x10] // rhs_packed\n KAI_ASM_INST(0x8558c009) // ld1rw z9.s, p0/z, [x0, #0x60] // min\n KAI_ASM_INST(0x8559c00a) // ld1rw z10.s, p0/z, [x0, #0x64] //max\n fmov z11.h, #0.0\n ldr\tx4, [x0, #0x50] // bl\n ldr\tx21, [x0, #0x18] // stride\n ldr\tx20, [x0] // dst\n mov x8, #0\n ldr x13, [x0, #0x40] // n\n ldr x23, [x0, #0x48] // k\n KAI_ASM_INST(0x256d6511) //whilelt pn9.h, x8, x13, VLx4\n b.eq label_9 // b.none label_9\nKAI_ASM_LABEL(label_1) // N loop\n ldr\tx9, [x0, #0x38] // m\n ldr\tx22, [x0, #0x8] // lhs_packed\n mov x24, x20\nKAI_ASM_LABEL(label_2) // M Loop\n mov x26, x19\n mov x3, x22\n cmp x9, x14\n csel x15, x9, x14, lo\n lsl x15, x15, #2\n ldr\tx10, [x0, #0x48] // k\n cmp x10, #0\n b.eq label_8\nKAI_ASM_LABEL(label_3) // K Loop\n KAI_ASM_INST(0xc00800ff) // zero {za}\n mov x11, x4\nKAI_ASM_LABEL(label_4) // BL Loop\n KAI_ASM_INST(0xa0404342) //ld1w {z2.s - z3.s}, pn8/z, [x26]\n addvl x26, x26, #2\n ld1h {z8.h}, p0/z, [x3]\n addvl x3, x3, #1\n KAI_ASM_INST(0xc08a4044) // luti4 {z4.b - z5.b}, zt0, z2[0]\n KAI_ASM_INST(0xc08a4066) // luti4 {z6.b - z7.b}, zt0, z3[0]\n KAI_ASM_INST(0xa0840100) // smopa za0.s, p0/m, p0/m, z8.b, z4.b\n KAI_ASM_INST(0xa0850101) // smopa za1.s, p0/m, p0/m, z8.b, z5.b\n KAI_ASM_INST(0xa0860102) // smopa za2.s, p0/m, p0/m, z8.b, z6.b\n KAI_ASM_INST(0xa0870103) // smopa za3.s, p0/m, p0/m, z8.b, z7.b\n subs x11, x11, #4\n b.gt label_4\n mov w12, #0\n mov x25, x24\n ld1b {z17.b}, p4/z, [x3] // lhs sum\n ld1b {z16.b}, p4/z, [x3, #1, mul vl] // lhs scale\n addvl x3, x3, #2\n KAI_ASM_INST(0xa040c354) // ld1w { z20.s - z23.s }, pn8/z, [x26] // rhs zp\n KAI_ASM_INST(0xa041c340) // ld1w { z0.s - z3.s }, pn8/z, [x26, #4, mul vl ] // rhs scale\n addvl x26, x26, #8\n pfalse p3.b\nKAI_ASM_LABEL(label_5)\n pnext p3.s, p0, p3.s\n clastb z19.s, p3, z19.s, z16.s\n clastb z18.s, p3, z18.s, z17.s\n KAI_ASM_INST(0xc006041c) // mova {z28.b-z31.b}, za0h.b[w12, 0:3]\n add w12, w12, #4\n fmul z4.s, z0.s, z19.s\n fmul z5.s, z1.s, z19.s\n fmul z6.s, z2.s, z19.s\n fmul z7.s, z3.s, z19.s\n KAI_ASM_INST(0xc132e39c) // scvtf {z28.s-z31.s}, {z28.s-z31.s}\n cmp x10, x23\n b.ne label_6\n fmul z24.s, z20.s, z18.s\n fmul z25.s, z21.s, z18.s\n fmul z26.s, z22.s, z18.s\n fmul z27.s, z23.s, z18.s\n fmla z24.s, p0/m, z4.s, z28.s\n fmla z25.s, p0/m, z5.s, z29.s\n fmla z26.s, p0/m, z6.s, z30.s\n fmla z27.s, p0/m, z7.s, z31.s\n b label_7\nKAI_ASM_LABEL(label_6)\n KAI_ASM_INST(0xa040272c) // ld1h {z12.h - z13.h}, pn9/z, [x25]\n zip1 z24.h, z12.h, z11.h\n zip2 z25.h, z12.h, z11.h\n zip1 z26.h, z13.h, z11.h\n zip2 z27.h, z13.h, z11.h\n fcvt z24.s, p0/m, z24.h\n fcvt z25.s, p0/m, z25.h\n fcvt z26.s, p0/m, z26.h\n fcvt z27.s, p0/m, z27.h\n fmla z24.s, p0/m, z20.s, z18.s\n fmla z25.s, p0/m, z21.s, z18.s\n fmla z26.s, p0/m, z22.s, z18.s\n fmla z27.s, p0/m, z23.s, z18.s\n fmla z24.s, p0/m, z4.s, z28.s\n fmla z25.s, p0/m, z5.s, z29.s\n fmla z26.s, p0/m, z6.s, z30.s\n fmla z27.s, p0/m, z7.s, z31.s\nKAI_ASM_LABEL(label_7)\n KAI_ASM_INST(0xc120e31c) // fcvt z28.h, {z24.s- z25.s}\n KAI_ASM_INST(0xc120e35d) // fcvt z29.h, {z26.s- z27.s}\n KAI_ASM_INST(0xa060273c) // st1h { z28.h - z29.h }, pn9, [x25]\n add x25, x25, x21\n cmp x12, x15\n blt label_5\n subs x10, x10, x4\n b.gt label_3\nKAI_ASM_LABEL(label_8)\n ldr\tx5, [x0, #0x30]\n add x5, x5, x19\n KAI_ASM_INST(0xa040c0ac) // ld1w {z12.s - z15.s}, pn8/z, [x5] \\n \"\n mov x12, 0\nKAI_ASM_LABEL(label_10)\n KAI_ASM_INST(0xa0402700) // ld1h {z0.h - z1.h}, pn9/z, [x24]\n zip1 z24.h, z0.h, z11.h\n zip2 z25.h, z0.h, z11.h\n zip1 z26.h, z1.h, z11.h\n zip2 z27.h, z1.h, z11.h\n fcvt z24.s, p0/m, z24.h\n fcvt z25.s, p0/m, z25.h\n fcvt z26.s, p0/m, z26.h\n fcvt z27.s, p0/m, z27.h\n fadd z24.s, p0/m, z24.s, z12.s\n fadd z25.s, p0/m, z25.s, z13.s\n fadd z26.s, p0/m, z26.s, z14.s\n fadd z27.s, p0/m, z27.s, z15.s\n KAI_ASM_INST(0xc1aac938) // fclamp { z24.s - z27.s }, z9.s, z10.s\n KAI_ASM_INST(0xc120e31c) // fcvt z28.h, {z24.s- z25.s}\n KAI_ASM_INST(0xc120e35d) // fcvt z29.h, {z26.s- z27.s}\n KAI_ASM_INST(0xa060271c) // st1h { z28.h - z29.h }, pn9, [x24]\n add x24, x24, x21\n add x12, x12, #4\n cmp x12, x15\n blt label_10\n ldr\tx5, [x0, #0x20]\n add x22, x22, x5\n mov x24, x25\n decw x9, all\n cmp x9, #0\n b.gt label_2\n incb x20, all, mul #2\n ldr\tx5, [x0, #0x28]\n add x19, x19, x5\n incb x8, all\n KAI_ASM_INST(0x256d6511) // whilelt pn9.h, x8, x13, VLx4\n b.mi label_1 // b.first label_1\nKAI_ASM_LABEL(label_9)\n KAI_ASM_INST(0xd503467f) // smstop\n ldp d14, d15, [sp, 112]\n ldp d12, d13, [sp, 96]\n ldp d10, d11, [sp, 80]\n ldp d8, d9, [sp, 64]\n ldp x25, x26, [sp, 48]\n ldp x23, x24, [sp, 32]\n ldp x21, x22, [sp, 16]\n ldp x19, x20, [sp],128\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.h",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_run_lhs_quant_pack_qsi8d32pscalef32_f16_neon to dynamically quantize and pack the LHS matrix in a single\n/// step.\n/// -# @ref kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon or @ref\n/// kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s0s1_f32_f32_f32_neon pack the RHS NxK matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-block (multiple of 32) quantization (qsi8d32)\n/// values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of m_step.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Asymmetric Signed 4-bit with per-block (multiple of 32) quantization (qai4c32)\n/// values.\n///\n/// @param[in] n_idx Col index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t n_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of m_step.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of n_step.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride); //\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n); //\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Symmetric Signed 8-bit with per-block (multiple of 32) quantization (qsi8d32) and packed\n/// RHS matrix: Quantized Asymmetric Signed 4-bit with per-block (multiple of 32) quantization (qai4c32) and packed.\n/// Output tile: (rows x cols) = 1 VL x 4 VL (Vector Length)\n///\n/// Instruction used: SME2 (MOPA)\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. Block length. It must be a multiple of 32.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the\n/// top of this file.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the\n/// top of this file.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes.\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n void* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n}\n#endif // __cplusplus\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_quant_pack_qsi8d32pscalef32_f16_neon.h",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qsi8d32pscalef32_f16_neon(size_t mr);\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qsi8d32pscalef32_f16_neon(size_t m_idx, size_t lhs_stride);\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized symmetric per-block (qsi8d32) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl The block length.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32pscalef32_f16_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f16_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be\n/// a multiple of 32.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qsi8d32pscalef32_f16_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs,\n size_t lhs_stride, void* lhs_packed);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_quant_pack_qsi8d32pscalef32_f16_neon.c",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) || !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)\n#error This file must be compiled for AArch64 and FEAT_FP16.\n#else // Architectural features check.\n\n#include \"kai_lhs_quant_pack_qsi8d32pscalef32_f16_neon.h\"\n\n#include <arm_neon.h>\n#include <float.h>\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n#define FLT16_MAX 65504.0F\n#define FLT16_MIN (-65504.0F)\nstatic const size_t kai_num_bytes_sum = sizeof(float);\nstatic const size_t kai_num_bytes_multiplier = sizeof(float);\nstatic const size_t kai_bl_multiple_of = 32;\n\ninline static size_t kai_get_num_bytes_per_block(size_t bl) {\n return bl * sizeof(int8_t) + kai_num_bytes_multiplier + kai_num_bytes_sum;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSERT((k % bl) == 0);\n return k / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t bl) {\n KAI_UNUSED(kr);\n\n return mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block(bl);\n}\nsize_t kai_get_m_step_lhs_quant_pack_qsi8d32pscalef32_f16_neon(size_t mr) {\n return mr;\n}\nsize_t kai_get_lhs_offset_lhs_quant_pack_qsi8d32pscalef32_f16_neon(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32pscalef32_f16_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_UNUSED(sr);\n return (m_idx / mr) * kai_get_lhs_packed_stride(k, mr, kr, bl);\n}\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f16_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n KAI_UNUSED(sr);\n\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return (num_rows * kai_get_lhs_packed_stride(k, mr, kr, bl));\n}\nvoid kai_run_lhs_quant_pack_qsi8d32pscalef32_f16_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs,\n size_t lhs_stride, void* lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSUME((bl % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n\n if (m == 0) {\n return;\n }\n const size_t lhs_packed_stride = kai_get_lhs_packed_stride(k, mr, kr, bl);\n const size_t num_rows = m;\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block(bl);\n const size_t mr_block_size = mr * num_bytes_per_block;\n\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {\n const float16_t* row_src_ptr = (const float16_t*)((const uint8_t*)lhs + (row_idx + m_idx_start) * lhs_stride);\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n for (size_t b = 0; b < num_blocks_per_row; ++b) {\n const float16_t* src_ptr = row_src_ptr + b * bl;\n int8_t* dst_ptr = (int8_t*)lhs_packed + dst_x * k_block_len * sizeof(int8_t) + b * mr_block_size;\n int8_t* param_ptr = (int8_t*)lhs_packed + b * mr_block_size + bl * mr + dst_x * kai_num_bytes_sum;\n // Find absmax for each block\n float16_t absmax = (float16_t)(-FLT16_MAX);\n int32_t k_idx = 0;\n float16x8_t vabsmax = vdupq_n_f16(-FLT16_MAX);\n for (; k_idx < ((int32_t)bl); k_idx += 8) {\n const float16x8_t src = vabsq_f16(vld1q_f16(src_ptr + (size_t)k_idx));\n vabsmax = vmaxq_f16(vabsmax, src);\n }\n // Get the absmax\n absmax = vmaxvq_f16(vabsmax);\n // Maximum/minimum int8 values\n const float qmax = (float)INT8_MAX;\n const float scale0 = absmax == 0.0F ? 0.0F : qmax / absmax;\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n int32_t qsum = 0;\n // Quantize the blocks\n for (k_idx = 0; k_idx < (int32_t)bl; k_idx += k_block_len) {\n for (size_t k_block_idx = 0; k_block_idx < (size_t)k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)k_idx + k_block_idx, k - 1);\n\n const float16_t src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n qsum += v0_s32;\n\n *(dst_ptr) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n *((float*)(param_ptr)) = ((float)qsum) * recip_scale0;\n param_ptr += mr * kai_num_bytes_sum;\n *((float*)(param_ptr)) = recip_scale0;\n }\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((int8_t*)lhs_packed + lhs_packed_stride);\n }\n }\n}\n#endif // Architectural features check.\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.c",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n#include \"kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.h\"\n\n#include <arm_neon.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_offset_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\nstatic const size_t kai_bl_multiple_of = 32;\nstatic const size_t kai_nr_multiple_of = 4;\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_get_num_bytes_per_block(size_t bl) {\n return (bl / 2) + kai_num_bytes_multiplier_rhs + kai_num_bytes_offset_rhs;\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kr) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block(bl);\n return nr * (num_bytes_per_block * num_blocks_per_row + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((n_idx % nr) == 0);\n KAI_UNUSED(kr);\n return (n_idx / nr) * kai_get_rhs_packed_stride(k, nr, kr, bl);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_UNUSED(kr);\n const size_t num_rows = kai_roundup(n, nr) / nr;\n return num_rows * kai_get_rhs_packed_stride(k, nr, kr, bl);\n}\n\nvoid kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t bl, const uint8_t* rhs,\n const void* zero, const void* bias, const void* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qai4c32p_params* params) {\n KAI_ASSUME(num_groups == 1);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n KAI_ASSUME((nr % kai_nr_multiple_of) == 0);\n KAI_ASSUME(extra_bytes == 0);\n\n KAI_ASSUME(sr == 2);\n KAI_ASSUME(kr / sr == 4);\n KAI_ASSUME(rhs != NULL);\n KAI_ASSUME(zero != NULL);\n KAI_ASSUME(scale != NULL);\n KAI_ASSUME(rhs_packed != NULL);\n KAI_ASSUME(params != NULL);\n KAI_ASSUME(params->rhs_zero_point == 8);\n KAI_ASSUME(params->lhs_zero_point == 1);\n\n // Note: The input matrix (rhs) is expected with:\n // \"k\" columns and \"n\" rows (NxK)\n\n const size_t block_length = kr / sr;\n const size_t num_blocks_per_row = k / bl;\n const size_t rhs_stride = k / 2;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride(k, nr, kr, bl);\n\n const size_t dst_packed_block_size = kai_get_num_bytes_per_block(bl) * nr;\n const size_t dst_block_data_size = bl / 2;\n const size_t dst_num_rows = kai_roundup(n, nr) / nr;\n const size_t dst_bias_offset = num_blocks_per_row * dst_packed_block_size;\n const size_t k_block_length_in_bytes = (block_length * sizeof(uint8_t)) / 2;\n\n for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; ++dst_row_idx) {\n uint8_t* dst_row = (uint8_t*)rhs_packed + dst_row_idx * rhs_packed_stride;\n float* dst_row_bias = (float*)(dst_row + dst_bias_offset);\n size_t row_idx = dst_row_idx * nr;\n size_t rows_left = n - row_idx;\n for (size_t block_idx = 0; block_idx < num_blocks_per_row; block_idx++) {\n uint8_t* block_dst_row = dst_row + block_idx * dst_packed_block_size;\n float* block_dst_zp = (float*)(block_dst_row + nr * dst_block_data_size);\n float* block_dst_scale = block_dst_zp + nr;\n size_t k_idx = block_idx * bl;\n for (size_t dst_byte_idx = 0; dst_byte_idx < dst_block_data_size; dst_byte_idx += 8) {\n for (size_t nr_idx = 0; nr_idx <= nr - 4; nr_idx += 4) {\n const size_t n0_idx = KAI_MIN(dst_row_idx * nr + nr_idx, n - 1);\n const size_t n1_idx = KAI_MIN(n0_idx + 1, n - 1);\n const size_t n2_idx = KAI_MIN(n0_idx + 2, n - 1);\n const size_t n3_idx = KAI_MIN(n0_idx + 3, n - 1);\n const uint8_t* src_addr_byte = rhs + (k_idx / 2) + dst_byte_idx;\n\n const uint8x8_t vec0_u8 = vld1_u8(src_addr_byte + n0_idx * rhs_stride);\n const uint8x8_t vec1_u8 = vld1_u8(src_addr_byte + n1_idx * rhs_stride);\n const uint8x8_t vec2_u8 = vld1_u8(src_addr_byte + n2_idx * rhs_stride);\n const uint8x8_t vec3_u8 = vld1_u8(src_addr_byte + n3_idx * rhs_stride);\n\n const uint16x4_t vec0_u16 = vreinterpret_u16_u8(vec0_u8);\n const uint16x4_t vec1_u16 = vreinterpret_u16_u8(vec1_u8);\n const uint16x4_t vec2_u16 = vreinterpret_u16_u8(vec2_u8);\n const uint16x4_t vec3_u16 = vreinterpret_u16_u8(vec3_u8);\n\n const uint16x4_t vec01_lo_u16 = vzip1_u16(vec0_u16, vec1_u16);\n const uint16x4_t vec01_hi_u16 = vzip2_u16(vec0_u16, vec1_u16);\n const uint16x4_t vec23_lo_u16 = vzip1_u16(vec2_u16, vec3_u16);\n const uint16x4_t vec23_hi_u16 = vzip2_u16(vec2_u16, vec3_u16);\n\n const uint32x2_t vec01_lo_u32 = vreinterpret_u32_u16(vec01_lo_u16);\n const uint32x2_t vec01_hi_u32 = vreinterpret_u32_u16(vec01_hi_u16);\n const uint32x2_t vec23_lo_u32 = vreinterpret_u32_u16(vec23_lo_u16);\n const uint32x2_t vec23_hi_u32 = vreinterpret_u32_u16(vec23_hi_u16);\n\n const uint32x2_t vin0_u32 = vzip1_u32(vec01_lo_u32, vec23_lo_u32);\n const uint32x2_t vin1_u32 = vzip2_u32(vec01_lo_u32, vec23_lo_u32);\n const uint32x2_t vin2_u32 = vzip1_u32(vec01_hi_u32, vec23_hi_u32);\n const uint32x2_t vin3_u32 = vzip2_u32(vec01_hi_u32, vec23_hi_u32);\n\n const uint8x8_t vin0_u8 = vreinterpret_u8_u32(vin0_u32);\n const uint8x8_t vin1_u8 = vreinterpret_u8_u32(vin1_u32);\n const uint8x8_t vin2_u8 = vreinterpret_u8_u32(vin2_u32);\n const uint8x8_t vin3_u8 = vreinterpret_u8_u32(vin3_u32);\n\n uint8_t* dst_row_offset = block_dst_row + nr_idx * k_block_length_in_bytes;\n vst1_u8(dst_row_offset, vin0_u8);\n vst1_u8(dst_row_offset + nr * k_block_length_in_bytes, vin1_u8);\n vst1_u8(dst_row_offset + 2 * (nr * k_block_length_in_bytes), vin2_u8);\n vst1_u8(dst_row_offset + 3 * (nr * k_block_length_in_bytes), vin3_u8);\n }\n block_dst_row += nr * sizeof(uint8x8_t);\n }\n\n // Adjust the zero points and scales\n for (size_t i = 0; i < nr; ++i) {\n const size_t src_row_idx = KAI_MIN(row_idx + i, n - 1);\n const size_t src_idx = src_row_idx * num_blocks_per_row + block_idx;\n\n block_dst_scale[i] = ((const float*)scale)[src_idx];\n block_dst_zp[i] = ((const float*)zero)[src_idx];\n }\n }\n // Set the bias\n if (bias == NULL) {\n memset(dst_row_bias, 0, nr * kai_num_bytes_bias);\n } else {\n if (rows_left >= nr) {\n memcpy(dst_row_bias, &((const float*)bias)[row_idx], nr * kai_num_bytes_bias);\n } else {\n // Fill remaining values\n memcpy(dst_row_bias, &((const float*)bias)[row_idx], rows_left * kai_num_bytes_bias);\n // Set leftover to 0\n memset(&dst_row_bias[rows_left], 0, (nr - rows_left) * kai_num_bytes_bias);\n }\n }\n }\n}\n#endif\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef kai_rhs_pack_nxk_qai4c32p_params\n#define kai_rhs_pack_nxk_qai4c32p_params kai_rhs_pack_qs4cxs1s0_param\n#endif\n\n/// Gets the offset in bytes for the RHS matrix (not packed), which holds\n/// the int4 values in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 K values are stored in one byte. These values are stored in blocks\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n_idx, //\n size_t rhs_stride); //\n\n/// Gets the offset in bytes for the packed RHS matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K)\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n_idx, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl //\n);\n\n/// Gets the size in bytes for the quantized and packed RHS matrix.\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple\n/// of 32.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl //\n);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 values are stored in one byte.\n/// nrx4 => this function can take in generic nr values but the input is expected to have a block depth of 4\n/// Block depth is calculated as kr / sr. The values of these parameters are defined in the matmul ukernel.\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of columns of the output matrix (N).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// @param[in] nr The number of N rows to interleave on the same output row.\n/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] bl The block length, which defines the number of\n/// K values stored in a single block. It must be a multiple of 32.\n/// @param[in] rhs The RHS matrix containing the 4-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).\n/// @param[in] zero The zero point.\n/// @param[in] bias The biases.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n const uint8_t* rhs, //\n const void* zero, //\n const void* bias, //\n const void* scale, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_nxk_qai4c32p_params* params);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "binding.cpp",
|
| 65 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa against gemm_f16_qai4c32_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n#include <arm_neon.h>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_quant_pack_qsi8d32pscalef32_f16_neon.h\"\n#include \"kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr size_t BL = 32;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n// Widen n raw fp16 patterns to fp32 (exact).\nvoid widen_f16(const uint16_t* src, float* dst, size_t n) {\n size_t i = 0;\n for (; i + 4 <= n; i += 4) vst1q_f32(dst + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(src + i))));\n for (; i < n; ++i) dst[i] = vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(src[i]))), 0);\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n // The SME2 mopa kernel takes any M in a single call, so there is no GEMV dispatch here.\n const size_t mr = kai_get_mr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f16_neon((size_t)M, (size_t)K, BL, mr, kr, sr);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_quant_pack_qsi8d32pscalef32_f16_neon((size_t)M, (size_t)K, BL, mr, kr, sr, 0, A, (size_t)K * sizeof(uint16_t), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q4_snap = nullptr;\n static size_t B_q4_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* B_zeros_snap = nullptr;\n static size_t B_zeros_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q4_snap, B_q4_snap_size, B_q4, (size_t)N*K/2)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N*(K/BL))\n | refresh_cache(B_zeros_snap, B_zeros_snap_size, B_zeros, sizeof(float)*N*(K/BL))\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon((size_t)N, (size_t)K, nr, kr, BL);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_qs4cxs1s0_param params;\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 8;\n kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(1, (size_t)N, (size_t)K, nr, kr, sr, BL, B_q4, B_zeros, bias, B_scales, rhs_packed, 0, ¶ms);\n }\n\n static uint16_t* tmp16 = nullptr;\n static size_t tmp16_cap = 0;\n if ((size_t)M * N > tmp16_cap) {\n std::free(tmp16);\n tmp16 = static_cast<uint16_t*>(std::malloc(sizeof(uint16_t) * (size_t)M * N));\n tmp16_cap = (size_t)M * N;\n if (tmp16 == nullptr) return -1;\n }\n kai_run_matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa((size_t)M, (size_t)N, (size_t)K, BL, lhs_packed, rhs_packed, tmp16, (size_t)N * sizeof(uint16_t), sizeof(uint16_t), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n // fp16 kernel result widened to the raw ABI's fp32 output (exact).\n widen_f16(tmp16, output, (size_t)M * N);\n return 0;\n}\n"
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f16_qs4cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_f16_qs4cx_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_f16_qs4cx_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_f16_qs4cx_sme2_n256_k512: KleidiAI kai_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa_asm.S",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa)\n stp x19, x20, [sp, -96]!\n stp d8, d9, [sp, 16]\n stp d10, d11, [sp, 32]\n stp d12, d13, [sp, 48]\n stp d14, d15, [sp, 64]\n\n KAI_ASM_INST(0xd503477f) // smstart\n ldr x19, [x0, #0x00] // dst\n ldr x20, [x0, #0x08] // lhs_packed\n ldr x7, [x0, #0x50] // lut\n KAI_ASM_INST(0xe11f80e0) // ldr zt0, [x7]\n cntw x7\n ptrue p2.b\n KAI_ASM_INST(0x8556c81a) // ld1rw { z26.s }, p2/z, [x0, #0x58] // clamp_min\n KAI_ASM_INST(0x8557c81c) // ld1rw { z28.s }, p2/z, [x0, #0x5c] // clamp_max\n ldr x12, [x0, #0x20] // m\n mov z27.d, z26.d\n KAI_ASM_INST(0xc120e35e) // fcvt z30.h, {z26.s-z27.s}\n mov z29.d, z28.d\n KAI_ASM_INST(0xc120e39f) // fcvt z31.h, {z28.s-z29.s}\n\n KAI_ASM_INST(0x25ac17e0) // whilelt p0.s, xzr, x12\nKAI_ASM_LABEL(label_1)\n ldr x8, [x0, #0x10] // rhs_packed\n mov x9, x19\n ldr x13, [x0, #0x28] // n\n cmp x7, x12\n csel x16, x7, x12, lt\n lsl x16, x16, #2\n\n KAI_ASM_INST(0x256d47f0) // whilelt pn8.h, xzr, x13, vlx2\nKAI_ASM_LABEL(label_2)\n mov x10, x20\n mov x11, x8\n mov x17, x9\n KAI_ASM_INST(0x25ad67f1) // whilelt pn9.s, xzr, x13, vlx4\n KAI_ASM_INST(0x256d67f3) // whilelt pn11.h, xzr, x13, vlx4\n\n KAI_ASM_INST(0xc00800ff) // zero {za}\n ldr x14, [x0, #0x40] // m_blk\n add x14, x10, x14\nKAI_ASM_LABEL(label_3)\n KAI_ASM_INST(0xa540a144) // ld1w { z4.s }, p0/z, [x10]\n addvl x10, x10, #1\n KAI_ASM_INST(0xa0402160) // ld1h { z0.h-z1.h }, pn8/z, [x11]\n addvl x11, x11, #2\n KAI_ASM_INST(0xc08a4008) // luti4 { z8.b - z9.b }, zt0, z0[0]\n KAI_ASM_INST(0xc08a402a) // luti4 { z10.b - z11.b }, zt0, z1[0]\n KAI_ASM_INST(0xa0884880) // smopa za0.s, p2/m, p2/m, z4.b, z8.b\n KAI_ASM_INST(0xa0894881) // smopa za1.s, p2/m, p2/m, z4.b, z9.b\n KAI_ASM_INST(0xa08a4882) // smopa za2.s, p2/m, p2/m, z4.b, z10.b\n KAI_ASM_INST(0xa08b4883) // smopa za3.s, p2/m, p2/m, z4.b, z11.b\n cmp x10, x14\n b.lt label_3\n\n // Load RHS row sum, scale factors, and bias.\n KAI_ASM_INST(0xa040c560) // ld1w { z0.s-z3.s }, pn9/z, [x11]\n KAI_ASM_INST(0xa041c564) // ld1w { z4.s-z7.s }, pn9/z, [x11, #4, mul vl]\n KAI_ASM_INST(0xa042c568) // ld1w { z8.s-z11.s }, pn9/z, [x11, #8, mul vl]\n addvl x11, x11, #12\n\n mov x14, #0\n addvl x15, x10, #1\nKAI_ASM_LABEL(label_4)\n KAI_ASM_INST(0x8540c950) // ld1rw { z16.s }, p2/z, [x10]\n KAI_ASM_INST(0x8540c9f1) // ld1rw { z17.s }, p2/z, [x15]\n add x10, x10, #4\n add x15, x15, #4\n\n fmul z20.s, z17.s, z4.s\n fmul z21.s, z17.s, z5.s\n fmul z22.s, z17.s, z6.s\n fmul z23.s, z17.s, z7.s\n\n KAI_ASM_INST(0xc006440c) // mova { z12.b-z15.b }, za0h.b[w14, 0:3]\n\n mla z12.s, p2/m, z16.s, z0.s\n mla z13.s, p2/m, z16.s, z1.s\n mla z14.s, p2/m, z16.s, z2.s\n mla z15.s, p2/m, z16.s, z3.s\n\n KAI_ASM_INST(0xc132e18c) // scvtf { z12.s-z15.s }, { z12.s-z15.s }\n\n fmul z24.s, z12.s, z20.s\n fmul z25.s, z13.s, z21.s\n fmul z26.s, z14.s, z22.s\n fmul z27.s, z15.s, z23.s\n\n fadd z24.s, p2/m, z24.s, z8.s\n fadd z25.s, p2/m, z25.s, z9.s\n fadd z26.s, p2/m, z26.s, z10.s\n fadd z27.s, p2/m, z27.s, z11.s\n\n KAI_ASM_INST(0xc120e31c) // fcvt z28.h, { z24.s-z25.s }\n KAI_ASM_INST(0xc120e35d) // fcvt z29.h, { z26.s-z27.s }\n KAI_ASM_INST(0xc17fc3dc) // fclamp { z28.h-z29.h }, z30.h, z31.h\n KAI_ASM_INST(0xa0602e3c) // st1h { z28.h-z29.h }, pn11, [x17]\n\n ldr x5, [x0, #0x18] // dst_stride_row\n add x17, x17, x5\n add x14, x14, #4\n cmp x14, x16\n b.lt label_4\n\n ldr x14, [x0, #0x38] // rhs_stride\n add x8, x8, x14\n addvl x9, x9, #2\n decb x13, all\n KAI_ASM_INST(0x256d47f0) // whilelt pn8.h, xzr, x13, vlx2\n b.mi label_2\n\n ldr x14, [x0, #0x30] // lhs_stride\n add x20, x20, x14\n ldr x14, [x0, #0x48] // dst_inc\n add x19, x19, x14\n sub x12, x12, x7\n whilelt p0.s, xzr, x12\n b.mi label_1\n\n KAI_ASM_INST(0xd503467f) // smstop\n\n ldp d14, d15, [sp, 64]\n ldp d12, d13, [sp, 48]\n ldp d10, d11, [sp, 32]\n ldp d8, d9, [sp, 16]\n ldp x19, x20, [sp], 96\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa.c",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural feature check\n\n#include \"kai_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#define KAI_LUT_NENTRIES 64\n\n/// Lut to be indexed by i4 resulting in its value in i8 (i.e. -2 = 1110 -> 1111 1110).\nstatic const int8_t lut[KAI_LUT_NENTRIES] = {\n // clang-format off\n 0, 0, 0, 0,\n 1, 0, 0, 0,\n 2, 0, 0, 0,\n 3, 0, 0, 0,\n 4, 0, 0, 0,\n 5, 0, 0, 0,\n 6, 0, 0, 0,\n 7, 0, 0, 0,\n -8, 0, 0, 0,\n -7, 0, 0, 0,\n -6, 0, 0, 0,\n -5, 0, 0, 0,\n -4, 0, 0, 0,\n -3, 0, 0, 0,\n -2, 0, 0, 0,\n -1, 0, 0, 0\n // clang-format on\n};\n\ntypedef struct {\n void* dst; // 0x00\n const void* lhs_packed; // 0x08\n const void* rhs_packed; // 0x10\n size_t dst_stride_row; // 0x18\n size_t m; // 0x20\n size_t n; // 0x28\n size_t lhs_stride; // 0x30\n size_t rhs_stride; // 0x38\n size_t m_blk; // 0x40\n size_t dst_inc; // 0x48\n const int8_t* lut; // 0x50\n float clamp_min; // 0x58\n float clamp_max; // 0x5c\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(KernelArgs* args_ptr);\n\n// Compute args\nstatic const size_t kai_m_step = 1; // multiple of vector length\nstatic const size_t kai_n_step = 4; // multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // multiple of vector length\nstatic const size_t kai_nr = 4; // multiple of vector length\nstatic const size_t kai_kr = 4;\nstatic const size_t kai_sr = 1;\n// LHS format args\nstatic const size_t kai_num_bytes_qvalue_lhs = sizeof(int8_t);\nstatic const size_t kai_num_bytes_multiplier_lhs = sizeof(float);\nstatic const size_t kai_num_bytes_offset_lhs = sizeof(int32_t);\n// RHS format args\nstatic const size_t kai_num_bytes_recip_qvalue_rhs = 2;\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_bias_rhs = sizeof(float);\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = sizeof(uint16_t);\n// Extra args\nstatic const size_t kai_k_multiple_of = 32;\n\nstatic size_t kai_k_roundedup(size_t k) {\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nstatic size_t kai_get_lhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % kai_k_multiple_of) == 0);\n\n return kai_get_mr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa() *\n (k_internal * kai_num_bytes_qvalue_lhs + kai_num_bytes_multiplier_lhs + kai_num_bytes_offset_lhs);\n}\n\nstatic size_t kai_get_rhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % kai_k_multiple_of) == 0);\n\n return kai_get_nr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa() *\n ((k_internal / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs +\n kai_num_bytes_bias_rhs);\n}\n\nsize_t kai_get_m_step_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_n_step_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_mr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_nr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_kr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(size_t m_idx, size_t k) {\n KAI_ASSERT((m_idx % kai_get_m_step_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa()) == 0);\n\n const size_t mr = kai_get_mr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa();\n\n return (m_idx / mr) * kai_get_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(size_t n_idx, size_t k) {\n KAI_ASSERT((n_idx % kai_get_n_step_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa()) == 0);\n\n const size_t nr = kai_get_nr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa();\n\n return (n_idx / nr) * kai_get_rhs_packed_stride(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSERT((m_idx % kai_get_m_step_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa()) == 0);\n KAI_ASSERT((n_idx % kai_get_n_step_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa()) == 0);\n\n return n_idx * kai_num_bytes_dst_value + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(\n size_t m, size_t n, size_t k, const void* restrict lhs_packed, const void* restrict rhs_packed,\n void* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, size_t dst_stride_col, float scalar_min, float scalar_max) {\n KAI_ASSERT(dst_stride_col == sizeof(uint16_t));\n KAI_ASSERT(n > 0);\n KAI_ASSERT(m > 0);\n\n const size_t mr = kai_get_mr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa();\n\n KernelArgs args;\n const size_t k_internal = kai_k_roundedup(k);\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.dst_stride_row = dst_stride_row;\n args.m = m;\n args.n = n;\n args.lhs_stride = kai_get_lhs_packed_stride(k);\n args.rhs_stride = kai_get_rhs_packed_stride(k);\n args.m_blk = mr * k_internal;\n args.dst_inc = mr * dst_stride_row;\n args.lut = lut;\n args.clamp_min = scalar_min;\n args.clamp_max = scalar_max;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(&args);\n}\n\n#endif // Architectural feature check\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa.h",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_lhs_quant_pack_qai8dxp_f16_neon to dynamically quantize and pack the LHS matrix in a single step.\n/// -# @ref kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon to pack the RHS matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix.\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices.\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices.\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qai8dxp) values.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(size_t m_idx, size_t k);\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed 4-bit quantized symmetric per-channel (qsi4cxp) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(size_t n_idx, size_t k);\n\n/// Gets the offset in bytes for the DST matrix.\n///\n/// @param[in] m_idx Row index in the DST matrix.\n/// @param[in] n_idx Column index in the DST matrix.\n/// @param[in] dst_stride The number of bytes in each row of the DST matrix.\n///\n/// @return the destination (DST) offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride);\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(size_t m, size_t n);\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Signed 8-bit quantized asymmetric per-row (qai8dxp) and packed.\n/// RHS matrix: Signed 4-bit quantized symmetric per-channel (qsi4cxp) and packed.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] lhs_packed The LHS packed matrix.\n/// @param[in] rhs_packed The RHS packed matrix.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(uint16_t).\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa(\n size_t m, size_t n, size_t k, const void* lhs_packed, const void* rhs_packed, void* dst, size_t dst_stride_row,\n size_t dst_stride_col, float scalar_min, float scalar_max);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f16_neon.c",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) || !defined(__ARM_FEATURE_FP16_SCALAR_ARITHMETIC) || \\\n !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)\n#error This file must be compiled for AArch64, FEAT_FP16.\n#else // Architectural features check.\n\n#include \"kai_lhs_quant_pack_qai8dxp_f16_neon.h\"\n\n#include <arm_fp16.h>\n#include <arm_neon.h>\n#include <float.h>\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n#define FLT16_MAX 65504.0\n#define FLT16_MIN (-65504.0F)\n\nstatic const size_t kai_num_bytes_per_multiplier = sizeof(float);\nstatic const size_t kai_num_bytes_per_offset = sizeof(int32_t);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return mr * (k_internal * sizeof(int8_t) + kai_num_bytes_per_multiplier + kai_num_bytes_per_offset);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f16_neon(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f16_neon(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f16_neon(\n size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) {\n // It always points to the beginning of the row\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f16_neon(size_t m, size_t k, size_t mr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return num_rows * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nvoid kai_run_lhs_quant_pack_qai8dxp_f16_neon(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* restrict lhs,\n size_t lhs_stride, void* restrict lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSUME((kr / sr == 8) || (kr / sr == 4));\n\n if (m == 0) {\n return;\n }\n\n const size_t num_rows = m;\n\n float16_t const* src_ptr = (float16_t const*)lhs;\n\n const size_t dst_stride = kai_lhs_packed_stride(k, mr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n const int32_t num_blocks_k = (int32_t)(k / k_block_len);\n const int32_t num_blocks_k_internal = (int32_t)(k_internal / k_block_len);\n const size_t lhs_row_length = lhs_stride / sizeof(float16_t);\n\n const float16x8_t vmax = vdupq_n_f16((float16_t)FLT16_MIN);\n const float16x8_t vmin = vdupq_n_f16((float16_t)FLT16_MAX);\n\n // As we load 8-element vectors, limit vectorized loop to avoid reading out-of-bounds\n const int32_t blocks_lim_k = num_blocks_k - (8 / k_block_len);\n\n size_t row_idx = 0;\n\n // Improved performance with 4x loop unrolling where packing parameters allow\n if (mr == 4) {\n for (; row_idx + 3 < m; row_idx += 4) {\n // Find min/max for each channel\n int32_t k_idx = 0;\n float16x8_t vmax0 = vmax;\n float16x8_t vmin0 = vmin;\n float16x8_t vmax1 = vmax;\n float16x8_t vmin1 = vmin;\n float16x8_t vmax2 = vmax;\n float16x8_t vmin2 = vmin;\n float16x8_t vmax3 = vmax;\n float16x8_t vmin3 = vmin;\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float16x8_t src0 = vld1q_f16(src_ptr + k_idx);\n const float16x8_t src1 = vld1q_f16(src_ptr + k_idx + lhs_row_length);\n const float16x8_t src2 = vld1q_f16(src_ptr + k_idx + (2 * lhs_row_length));\n const float16x8_t src3 = vld1q_f16(src_ptr + k_idx + (3 * lhs_row_length));\n\n vmax0 = vmaxq_f16(src0, vmax0);\n vmax1 = vmaxq_f16(src1, vmax1);\n vmax2 = vmaxq_f16(src2, vmax2);\n vmax3 = vmaxq_f16(src3, vmax3);\n vmin0 = vminq_f16(src0, vmin0);\n vmin1 = vminq_f16(src1, vmin1);\n vmin2 = vminq_f16(src2, vmin2);\n vmin3 = vminq_f16(src3, vmin3);\n }\n\n float16_t max0 = vmaxvq_f16(vmax0);\n float16_t min0 = vminvq_f16(vmin0);\n float16_t max1 = vmaxvq_f16(vmax1);\n float16_t min1 = vminvq_f16(vmin1);\n float16_t max2 = vmaxvq_f16(vmax2);\n float16_t min2 = vminvq_f16(vmin2);\n float16_t max3 = vmaxvq_f16(vmax3);\n float16_t min3 = vminvq_f16(vmin3);\n // Process leftover elements with a scalar loop.\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float16_t src0 = *(src_ptr + (size_t)k_idx);\n max0 = vmaxh_f16(src0, max0);\n min0 = vminh_f16(src0, min0);\n const float16_t src1 = *(src_ptr + (size_t)k_idx + lhs_row_length);\n max1 = vmaxh_f16(src1, max1);\n min1 = vminh_f16(src1, min1);\n const float16_t src2 = *(src_ptr + (size_t)k_idx + (2 * lhs_row_length));\n max2 = vmaxh_f16(src2, max2);\n min2 = vminh_f16(src2, min2);\n const float16_t src3 = *(src_ptr + (size_t)k_idx + (3 * lhs_row_length));\n max3 = vmaxh_f16(src3, max3);\n min3 = vminh_f16(src3, min3);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n const float rmin1 = fminf(0.0F, min1);\n const float rmax1 = fmaxf(0.0F, max1);\n const float scale1 = rmin1 == rmax1 ? 1.F : (qmax - qmin) / (rmax1 - rmin1);\n const float rmin2 = fminf(0.0F, min2);\n const float rmax2 = fmaxf(0.0F, max2);\n const float scale2 = rmin2 == rmax2 ? 1.F : (qmax - qmin) / (rmax2 - rmin2);\n const float rmin3 = fminf(0.0F, min3);\n const float rmax3 = fmaxf(0.0F, max3);\n const float scale3 = rmin3 == rmax3 ? 1.F : (qmax - qmin) / (rmax3 - rmin3);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n const float recip_scale1 = scale1 ? 1.0F / scale1 : 0.0F;\n const float recip_scale2 = scale2 ? 1.0F / scale2 : 0.0F;\n const float recip_scale3 = scale3 ? 1.0F / scale3 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n const float descaled_min1 = rmin1 * scale1;\n const float descaled_max1 = rmax1 * scale1;\n const float descaled_min2 = rmin2 * scale2;\n const float descaled_max2 = rmax2 * scale2;\n const float descaled_min3 = rmin3 * scale3;\n const float descaled_max3 = rmax3 * scale3;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n const float zero_point_from_min_error1 = qmin + descaled_min1;\n const float zero_point_from_max_error1 = qmax + descaled_max1;\n const float zero_point_from_min_error2 = qmin + descaled_min2;\n const float zero_point_from_max_error2 = qmax + descaled_max2;\n const float zero_point_from_min_error3 = qmin + descaled_min3;\n const float zero_point_from_max_error3 = qmax + descaled_max3;\n\n float zero_point0 = (zero_point_from_min_error0 + zero_point_from_max_error0 > 0) ? qmin - descaled_min0\n : qmax - descaled_max0;\n float zero_point1 = (zero_point_from_min_error1 + zero_point_from_max_error1 > 0) ? qmin - descaled_min1\n : qmax - descaled_max1;\n float zero_point2 = (zero_point_from_min_error2 + zero_point_from_max_error2 > 0) ? qmin - descaled_min2\n : qmax - descaled_max2;\n float zero_point3 = (zero_point_from_min_error3 + zero_point_from_max_error3 > 0) ? qmin - descaled_min3\n : qmax - descaled_max3;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n zero_point1 = fmaxf(zero_point1, qmin);\n zero_point1 = fminf(zero_point1, qmax);\n zero_point2 = fmaxf(zero_point2, qmin);\n zero_point2 = fminf(zero_point2, qmax);\n zero_point3 = fmaxf(zero_point3, qmin);\n zero_point3 = fminf(zero_point3, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n const int32_t nudged_zero_point1 = (int32_t)rintf(zero_point1);\n const int32_t nudged_zero_point2 = (int32_t)rintf(zero_point2);\n const int32_t nudged_zero_point3 = (int32_t)rintf(zero_point3);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len);\n\n // Quantize the channels\n int32_t block_idx = 0;\n const int32_t block_incr = 8 / k_block_len;\n\n for (; block_idx <= blocks_lim_k; block_idx += block_incr) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float16x8_t src0 = vld1q_f16(src_ptr + k_idx_start);\n const float16x8_t src1 = vld1q_f16(src_ptr + k_idx_start + lhs_row_length);\n const float16x8_t src2 = vld1q_f16(src_ptr + k_idx_start + (2 * lhs_row_length));\n const float16x8_t src3 = vld1q_f16(src_ptr + k_idx_start + (3 * lhs_row_length));\n\n // Scale the values.\n const int32x4_t v0_0_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src0)), scale0));\n const int32x4_t v0_1_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_high_f32_f16(src0), scale0));\n const int32x4_t v1_0_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src1)), scale1));\n const int32x4_t v1_1_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_high_f32_f16(src1), scale1));\n const int32x4_t v2_0_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src2)), scale2));\n const int32x4_t v2_1_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_high_f32_f16(src2), scale2));\n const int32x4_t v3_0_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src3)), scale3));\n const int32x4_t v3_1_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_high_f32_f16(src3), scale3));\n\n const int16x4_t v0_0_s16 = vqmovn_s32(v0_0_s32);\n const int16x4_t v0_1_s16 = vqmovn_s32(v0_1_s32);\n const int16x4_t v1_0_s16 = vqmovn_s32(v1_0_s32);\n const int16x4_t v1_1_s16 = vqmovn_s32(v1_1_s32);\n const int16x4_t v2_0_s16 = vqmovn_s32(v2_0_s32);\n const int16x4_t v2_1_s16 = vqmovn_s32(v2_1_s32);\n const int16x4_t v3_0_s16 = vqmovn_s32(v3_0_s32);\n const int16x4_t v3_1_s16 = vqmovn_s32(v3_1_s32);\n\n int16x8_t v0_s16;\n int16x8_t v1_s16;\n int16x8_t v2_s16;\n int16x8_t v3_s16;\n if (k_block_len == 8) {\n v0_s16 = vcombine_s16(v0_0_s16, v0_1_s16);\n v1_s16 = vcombine_s16(v1_0_s16, v1_1_s16);\n v2_s16 = vcombine_s16(v2_0_s16, v2_1_s16);\n v3_s16 = vcombine_s16(v3_0_s16, v3_1_s16);\n } else { // k_block_len == 4\n v0_s16 = vcombine_s16(v0_0_s16, v1_0_s16);\n v1_s16 = vcombine_s16(v2_0_s16, v3_0_s16);\n v2_s16 = vcombine_s16(v0_1_s16, v1_1_s16);\n v3_s16 = vcombine_s16(v2_1_s16, v3_1_s16);\n }\n\n // Add zero points.\n const int16x8_t vnzp0 = vdupq_n_s16((int16_t)nudged_zero_point0);\n const int16x8_t vnzp1 = vdupq_n_s16((int16_t)nudged_zero_point1);\n const int16x8_t vnzp2 = vdupq_n_s16((int16_t)nudged_zero_point2);\n const int16x8_t vnzp3 = vdupq_n_s16((int16_t)nudged_zero_point3);\n\n v0_s16 = vaddq_s16(v0_s16, vnzp0);\n v0_s16 = vmaxq_s16(v0_s16, vdupq_n_s16(INT8_MIN));\n v0_s16 = vminq_s16(v0_s16, vdupq_n_s16(INT8_MAX));\n v1_s16 = vaddq_s16(v1_s16, vnzp1);\n v1_s16 = vmaxq_s16(v1_s16, vdupq_n_s16(INT8_MIN));\n v1_s16 = vminq_s16(v1_s16, vdupq_n_s16(INT8_MAX));\n v2_s16 = vaddq_s16(v2_s16, vnzp2);\n v2_s16 = vmaxq_s16(v2_s16, vdupq_n_s16(INT8_MIN));\n v2_s16 = vminq_s16(v2_s16, vdupq_n_s16(INT8_MAX));\n v3_s16 = vaddq_s16(v3_s16, vnzp3);\n v3_s16 = vmaxq_s16(v3_s16, vdupq_n_s16(INT8_MIN));\n v3_s16 = vminq_s16(v3_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(v0_s16);\n int8x8_t v1_s8 = vqmovn_s16(v1_s16);\n int8x8_t v2_s8 = vqmovn_s16(v2_s16);\n int8x8_t v3_s8 = vqmovn_s16(v3_s16);\n\n vst1_s8((int8_t*)(dst_ptr), v0_s8);\n vst1_s8((int8_t*)(dst_ptr + sizeof(int8x8_t)), v1_s8);\n vst1_s8((int8_t*)(dst_ptr + 2 * sizeof(int8x8_t)), v2_s8);\n vst1_s8((int8_t*)(dst_ptr + 3 * sizeof(int8x8_t)), v3_s8);\n dst_ptr += block_incr * mr * k_block_len * sizeof(int8_t);\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index.\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0 = (float)(*(src_ptr + k_idx_start));\n const float src1 = (float)(*(src_ptr + k_idx_start + lhs_row_length));\n const float src2 = (float)(*(src_ptr + k_idx_start + (2 * lhs_row_length)));\n const float src3 = (float)(*(src_ptr + k_idx_start + (3 * lhs_row_length)));\n\n // Scale the value.\n int32_t d0_s32 = (int32_t)(roundf(src0 * scale0));\n int32_t d1_s32 = (int32_t)(roundf(src1 * scale1));\n int32_t d2_s32 = (int32_t)(roundf(src2 * scale2));\n int32_t d3_s32 = (int32_t)(roundf(src3 * scale3));\n\n d0_s32 = d0_s32 + nudged_zero_point0;\n d0_s32 = KAI_MAX(d0_s32, INT8_MIN);\n d0_s32 = KAI_MIN(d0_s32, INT8_MAX);\n\n d1_s32 = d1_s32 + nudged_zero_point1;\n d1_s32 = KAI_MAX(d1_s32, INT8_MIN);\n d1_s32 = KAI_MIN(d1_s32, INT8_MAX);\n\n d2_s32 = d2_s32 + nudged_zero_point2;\n d2_s32 = KAI_MAX(d2_s32, INT8_MIN);\n d2_s32 = KAI_MIN(d2_s32, INT8_MAX);\n\n d3_s32 = d3_s32 + nudged_zero_point3;\n d3_s32 = KAI_MAX(d3_s32, INT8_MIN);\n d3_s32 = KAI_MIN(d3_s32, INT8_MAX);\n\n *(int8_t*)dst_ptr = (int8_t)d0_s32;\n *(int8_t*)(dst_ptr + k_block_len * sizeof(int8_t)) = (int8_t)d1_s32;\n *(int8_t*)(dst_ptr + 2 * (k_block_len * sizeof(int8_t))) = (int8_t)d2_s32;\n *(int8_t*)(dst_ptr + 3 * (k_block_len * sizeof(int8_t))) = (int8_t)d3_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n uint8_t* dst_base = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr = dst_base + dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row.\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n *((int32_t*)(dst_ptr + kai_num_bytes_per_offset)) = -nudged_zero_point1;\n *((int32_t*)(dst_ptr + 2 * kai_num_bytes_per_offset)) = -nudged_zero_point2;\n *((int32_t*)(dst_ptr + 3 * kai_num_bytes_per_offset)) = -nudged_zero_point3;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier.\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params.\n *((float*)(dst_ptr)) = recip_scale0;\n *((float*)(dst_ptr + kai_num_bytes_per_multiplier)) = recip_scale1;\n *((float*)(dst_ptr + 2 * kai_num_bytes_per_multiplier)) = recip_scale2;\n *((float*)(dst_ptr + 3 * kai_num_bytes_per_multiplier)) = recip_scale3;\n\n // Update src_ptr. Note: now lhs contains fp16 values (2 bytes each).\n src_ptr += (4 * lhs_row_length);\n\n // Move to the next row as we have interleaved all Mr rows.\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n\n for (; row_idx < num_rows; ++row_idx) {\n // Find min/max for each channel\n int32_t k_idx = 0;\n float16x8_t vmax0 = vmax;\n float16x8_t vmin0 = vmin;\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float16x8_t src0_0 = vld1q_f16(src_ptr + (size_t)k_idx);\n vmax0 = vmaxq_f16(vmax0, src0_0);\n vmin0 = vminq_f16(vmin0, src0_0);\n }\n // Get the max/min\n float16_t max0 = vmaxvq_f16(vmax0);\n float16_t min0 = vminvq_f16(vmin0);\n\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float16_t src0 = *(src_ptr + (size_t)k_idx);\n max0 = vmaxh_f16(src0, max0);\n min0 = vminh_f16(src0, min0);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n\n float zero_point0 =\n zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len * sizeof(int8_t));\n\n // Quantize the channels\n int32_t block_idx = 0;\n if (k_block_len == 8) {\n for (; block_idx < blocks_lim_k; ++block_idx) {\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float16x8_t src_0 = vld1q_f16(src_ptr + k_idx_start);\n\n // Scale the values\n const float32x4_t v0_f32 = vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src_0)), scale0);\n const float32x4_t v1_f32 = vmulq_n_f32(vcvt_high_f32_f16(src_0), scale0);\n const int32x4_t v0_s32 = vcvtnq_s32_f32(v0_f32);\n const int32x4_t v1_s32 = vcvtnq_s32_f32(v1_f32);\n\n const int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n const int16x4_t v1_s16 = vqmovn_s32(v1_s32);\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x8_t vnzp_s16 = vdupq_n_s16(nzp_s16);\n v_s16 = vaddq_s16(v_s16, vnzp_s16);\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v_s8 = vqmovn_s16(v_s16);\n vst1_s8((int8_t*)(dst_ptr), v_s8);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n } else if (k_block_len == 4) {\n for (; block_idx < blocks_lim_k; ++block_idx) {\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float16x4_t src_0 = vld1_f16(src_ptr + k_idx_start);\n\n // Scale the values\n const float32x4_t v0_f32 = vmulq_n_f32(vcvt_f32_f16(src_0), scale0);\n const int32x4_t v0_s32 = vcvtnq_s32_f32(v0_f32);\n\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x4_t vnzp_s16 = vdup_n_s16(nzp_s16);\n v0_s16 = vadd_s16(v0_s16, vnzp_s16);\n v0_s16 = vmax_s16(v0_s16, vdup_n_s16(INT8_MIN));\n v0_s16 = vmin_s16(v0_s16, vdup_n_s16(INT8_MAX));\n\n int8x8_t v_s8 = vqmovn_s16(vcombine_s16(v0_s16, vdup_n_s16(0)));\n\n *((int8_t*)(dst_ptr)) = vget_lane_s8(v_s8, 0);\n *((int8_t*)(dst_ptr + 1)) = vget_lane_s8(v_s8, 1);\n *((int8_t*)(dst_ptr + 2)) = vget_lane_s8(v_s8, 2);\n *((int8_t*)(dst_ptr + 3)) = vget_lane_s8(v_s8, 3);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0 = (float)(*(src_ptr + k_idx_start));\n\n // Scale the values\n int32_t d0_s32 = (int32_t)(roundf(src0 * scale0));\n\n d0_s32 = d0_s32 + nudged_zero_point0;\n d0_s32 = KAI_MAX(d0_s32, INT8_MIN);\n d0_s32 = KAI_MIN(d0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)d0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n dst_ptr = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr += dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params\n *((float*)(dst_ptr)) = recip_scale0;\n\n src_ptr += lhs_row_length;\n\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n}\n\n#endif // Architectural features check.\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f16_neon.h",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <arm_fp16.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f16_neon(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f16_neon(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qa8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f16_neon(\n size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f16_neon(size_t m, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS of the vector-by-matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qai8dxp_f16_neon(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs, size_t lhs_stride,\n void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon.c",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n\n#include \"kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon.h\"\n\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nsize_t kai_get_n_step_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(size_t nr) {\n return nr;\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n // multiple of 2 because 2 elements in a byte\n KAI_ASSERT((k_internal % 2) == 0);\n\n return nr * ((k_internal / 2) + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_ASSERT((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(k, nr, kr, sr);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(k, nr, kr, sr);\n}\n\nvoid kai_run_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, const uint8_t* rhs, const float* bias,\n const float* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon_params* params) {\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % kr) == 0);\n KAI_ASSERT(num_groups == 1);\n KAI_ASSERT(extra_bytes == 0);\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSERT(rhs != NULL);\n KAI_ASSERT(scale != NULL);\n KAI_ASSERT(rhs_packed != NULL);\n KAI_ASSERT(params != NULL);\n KAI_ASSERT(params->lhs_zero_point == 1);\n KAI_ASSERT(params->rhs_zero_point == 0 || params->rhs_zero_point == 8);\n\n // Note: The input matrix (rhs) is expected with:\n // \"k\" columns and \"n\" rows (NxK)\n\n const int32_t rhs_zero_point = params->rhs_zero_point;\n const size_t rhs_stride = kai_roundup(k, 2) / 2;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(k, nr, kr, sr);\n const size_t dst_nr_block_size = nr * kr * sizeof(uint8_t) / 2;\n\n // Iterate over n src rows in blocks of nr rows\n for (size_t row_idx = 0; row_idx < n; row_idx += nr) {\n int8_t* const dst_row = (int8_t*)rhs_packed + ((row_idx / nr) * rhs_packed_stride);\n\n int32_t* const sums = (int32_t*)(dst_row + (nr * (k_internal / 2)));\n float* const scaling_factors = (float*)((uint8_t*)sums + (nr * kai_num_bytes_sum_rhs));\n // Update destination row pointer\n float* const biases = (float*)((uint8_t*)scaling_factors + (nr * kai_num_bytes_multiplier_rhs));\n\n // initialize sums to 0\n memset(sums, 0, nr * kai_num_bytes_sum_rhs);\n\n // Copy the scaling factors and bias\n size_t rows_left = n - row_idx;\n // Saving scales.\n if (rows_left >= nr) {\n memcpy(scaling_factors, &scale[row_idx], nr * kai_num_bytes_multiplier_rhs);\n } else {\n // Fill remaining values\n memcpy(scaling_factors, &scale[row_idx], rows_left * kai_num_bytes_multiplier_rhs);\n // Set leftover to 0\n memset(&scaling_factors[rows_left], 0, (nr - rows_left) * kai_num_bytes_multiplier_rhs);\n }\n if (bias == NULL) {\n // Set bias to 0\n memset(biases, 0, nr * kai_num_bytes_bias);\n } else {\n if (rows_left >= nr) {\n memcpy(biases, &bias[row_idx], nr * kai_num_bytes_bias);\n } else {\n // Fill remaining values\n memcpy(biases, &bias[row_idx], rows_left * kai_num_bytes_bias);\n // Set leftover to 0\n memset(&biases[rows_left], 0, (nr - rows_left) * kai_num_bytes_bias);\n }\n }\n // Iterate over rows in the nr row block\n for (size_t nr_block_idx = 0; nr_block_idx < nr; ++nr_block_idx) {\n const uint8_t* const src_row = rhs + ((row_idx + nr_block_idx) * rhs_stride);\n // Go to the first kr block for this row in the nr block\n int8_t* dst_kr_block = dst_row + (nr_block_idx * kr / 2);\n\n int32_t sum = 0;\n\n // Iterate over k src columns in blocks of kr columns\n if (rhs_zero_point == 8) {\n for (size_t col_idx = 0; col_idx < k_internal; col_idx += kr) {\n // Iterate over columns in the kr block\n // Kr checked to be multiple of 2 (because 2 values per byte)\n for (size_t kr_block_idx = 0; kr_block_idx < kr; kr_block_idx += 2) {\n // We pad dst with 0s if the rounded k or n values have been exceeded\n if (row_idx + nr_block_idx >= n || col_idx + kr_block_idx >= k) {\n dst_kr_block[kr_block_idx / 2] = 0;\n continue;\n }\n\n // Load the 2 u4 values from source\n const uint8_t dst_byte = src_row[(col_idx + kr_block_idx) / 2];\n\n // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n // extract i8 values from the 2 u4 values\n const int8_t first_value = (dst_byte & 0xF) - rhs_zero_point;\n const int8_t second_value =\n col_idx + kr_block_idx + 1 >= k ? 0 : (dst_byte >> 4) - rhs_zero_point;\n\n // Add the i4 value to the row sum\n sum += (int32_t)first_value + (int32_t)second_value;\n\n // Truncate i8 to i4 and write to dst\n const uint8_t hi = second_value & 0x0F;\n const uint8_t lo = first_value & 0x0F;\n dst_kr_block[kr_block_idx / 2] = (hi << 4) | lo;\n // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n }\n\n // Go to the next kr block for this row in the nr rows\n dst_kr_block += dst_nr_block_size;\n }\n } else {\n for (size_t col_idx = 0; col_idx < k_internal; col_idx += kr) {\n // Iterate over columns in the kr block\n // Kr checked to be multiple of 2 (because 2 values per byte)\n for (size_t kr_block_idx = 0; kr_block_idx < kr; kr_block_idx += 2) {\n // We pad dst with 0s if the rounded k or n values have been\n // exceeded\n if (row_idx + nr_block_idx >= n || col_idx + kr_block_idx >= k) {\n dst_kr_block[kr_block_idx / 2] = 0;\n continue;\n }\n\n // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n // Load the 2 u4 values from source\n const int8_t dst_byte = src_row[(col_idx + kr_block_idx) / 2];\n\n // extract i8 values from the 2 u4 values, shift first value\n // back and forth to get the sign right.\n const int8_t first_value = kai_ext_sign_i8_i4(dst_byte & 0xF);\n const int8_t second_value =\n col_idx + kr_block_idx + 1 >= k ? 0 : kai_ext_sign_i8_i4((dst_byte >> 4) & 0xF);\n\n // Add the i4 value to the row sum\n sum += (int32_t)first_value + (int32_t)second_value;\n\n // Truncate i8 to i4 and write to dst\n const uint8_t hi = second_value & 0x0F;\n const uint8_t lo = first_value & 0x0F;\n dst_kr_block[kr_block_idx / 2] = (hi << 4) | lo;\n // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n }\n\n // Go to the next kr block for this row in the nr rows\n dst_kr_block += dst_nr_block_size;\n }\n }\n\n // save sum\n sums[nr_block_idx] = sum;\n }\n }\n}\n#endif // Architectural features check.\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon_params\n#define kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon_params kai_rhs_pack_qs4cxs1s0_param\n#endif\n\n/// Get the n step value.\n///\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n///\n/// @return the n step value\nsize_t kai_get_n_step_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(size_t nr);\n\n/// Gets the offset in bytes for the RHS matrix (not packed)\n///\n/// @note The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 values are stored in one byte. The lower order part of the byte (low) holds\n/// the first nibble (K-index + 0). The higher order of the byte holds the second nibble (K-index + 1).\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(size_t n_idx, size_t rhs_stride);\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed 4-bit quantized symmetric per-channel (qsi4cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K)\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// @brief Gets the size in bytes for the packed RHS matrix\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 values are stored in one byte. The lower order part of the byte (low) holds\n/// the first nibble (K-index + 0). The higher order of the byte holds the second nibble (K-index + 1).\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of columns of the output matrix (N).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K). It must be an even value.\n/// @param[in] nr The number of N columns to interleave on the same output output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// @param[in] rhs The RHS matrix containing the 4-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).\n/// @param[in] bias The biases. The bias is set to 0.f if this argument is NULL.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, const uint8_t* rhs, const float* bias,\n const float* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon_params* params);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "binding.cpp",
|
| 65 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa against gemm_f16_qs4cx_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* bias, int M)\n#include <arm_neon.h>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_quant_pack_qai8dxp_f16_neon.h\"\n#include \"kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n// Widen n raw fp16 patterns to fp32 (exact).\nvoid widen_f16(const uint16_t* src, float* dst, size_t n) {\n size_t i = 0;\n for (; i + 4 <= n; i += 4) vst1q_f32(dst + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(src + i))));\n for (; i < n; ++i) dst[i] = vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(src[i]))), 0);\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* bias, int M)\n{\n // The SME2 mopa kernel takes any M in a single call, so there is no GEMV dispatch here.\n const size_t mr = kai_get_mr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f16_neon((size_t)M, (size_t)K, mr, kr, sr);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_quant_pack_qai8dxp_f16_neon((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(uint16_t), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q4_snap = nullptr;\n static size_t B_q4_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q4_snap, B_q4_snap_size, B_q4, (size_t)N*K/2)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N)\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon((size_t)N, (size_t)K, nr, kr, sr);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_qs4cxs1s0_param params;\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 8;\n kai_run_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon(1, (size_t)N, (size_t)K, nr, kr, sr, B_q4, bias, B_scales, rhs_packed, 0, ¶ms);\n }\n\n static uint16_t* tmp16 = nullptr;\n static size_t tmp16_cap = 0;\n if ((size_t)M * N > tmp16_cap) {\n std::free(tmp16);\n tmp16 = static_cast<uint16_t*>(std::malloc(sizeof(uint16_t) * (size_t)M * N));\n tmp16_cap = (size_t)M * N;\n if (tmp16 == nullptr) return -1;\n }\n kai_run_matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa((size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, tmp16, (size_t)N * sizeof(uint16_t), sizeof(uint16_t), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n // fp16 kernel result widened to the raw ABI's fp32 output (exact).\n widen_f16(tmp16, output, (size_t)M * N);\n return 0;\n}\n"
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f16_qs8cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_f16_qs8cx_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_f16_qs8cx_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_f16_qs8cx_sme2_n256_k512: KleidiAI kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.h",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_lhs_quant_pack_qai8dxp_f32 to dynamically quantize and pack the LHS matrix in a single step.\n/// -# @ref kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon to pack the RHS NxK matrix.\n/// -# @ref kai_rhs_pack_kxn_qsi8cxp_qsi8cx_neon to pack the RHS KxN matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t m_idx, size_t k);\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-channel quantization (qsi8cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t n_idx, size_t k);\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix.\n/// @param[in] n_idx Column index in the DST matrix.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the destination(DST) offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride);\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n);\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) and packed\n/// RHS matrix: Quantized Symmetric Signed 8-bit with per-channel quantization (qsi8cx) and packed.\n/// Output tile: (rows x cols) = 1vl x 4vl\n///\n/// Features used: sme2\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the\n/// top of this file.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the\n/// top of this file.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(uint16_t).\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n void* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.c",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error \"This file must be compiled for AArch64, FEAT_SVE2\"\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\ntypedef struct {\n void* dst; // 0\n const void* lhs_packed; // 0x8\n const void* rhs_packed; // 0x10\n size_t dst_stride_row; // 0x18\n size_t m; // 0x20\n size_t n; // 0x28\n size_t lhs_stride; // 0x30\n size_t rhs_stride; // 0x38\n size_t rhs_row_bytes; // 0x40\n size_t m_blk; // 0x48\n size_t dst_inc; // 0x50\n float clamp_min; // 0x58\n float clamp_max; // 0x5c\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(KernelArgs* args_ptr);\n\n// Compute args\nstatic const size_t kai_m_step = 1; // multiple of vector length\nstatic const size_t kai_n_step = 4; // multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // multiple of vector length\nstatic const size_t kai_nr = 4; // multiple of vector length\nstatic const size_t kai_kr = 4;\nstatic const size_t kai_sr = 1;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 4;\nstatic const size_t kai_num_bytes_zp_lhs = 4;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_num_bytes_qvalue_rhs = 1;\nstatic const size_t kai_num_bytes_multiplier_rhs = 4;\nstatic const size_t kai_num_bytes_rsum_rhs = 4;\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 2;\n// Extra args\nstatic const size_t kai_num_bytes_bias = 4;\nstatic const size_t kai_k_multiple_of = 32;\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n KAI_ASSERT((k_internal % kai_k_multiple_of) == 0);\n const size_t mr = kai_get_mr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n size_t lhs_packed_stride = mr * ((k_internal * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs);\n // Since the LHS matrix is asymmetric with per-row quantization, we must include the\n // the number of bytes to hold the zero point value\n lhs_packed_stride += mr * kai_num_bytes_zp_lhs;\n\n return lhs_packed_stride;\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n KAI_ASSERT((k_internal % kai_k_multiple_of) == 0);\n const size_t nr = kai_get_nr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n size_t rhs_packed_stride = nr * (k_internal * kai_num_bytes_qvalue_rhs);\n rhs_packed_stride += nr * kai_num_bytes_multiplier_rhs;\n // Since the LHS matrix is quantized asymmetric with per-row quantization, we also include\n // the number of bytes for the reduction sum\n rhs_packed_stride += nr * kai_num_bytes_rsum_rhs;\n // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias\n rhs_packed_stride += nr * kai_num_bytes_bias;\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u8() / kai_kr;\n}\n\nsize_t kai_get_n_step_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u8() / kai_kr;\n}\n\nsize_t kai_get_mr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u8() / kai_kr;\n}\n\nsize_t kai_get_nr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u8() / kai_kr;\n}\n\nsize_t kai_get_kr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t m_idx, size_t k) {\n KAI_ASSERT((m_idx % kai_get_mr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n\n const size_t mr = kai_get_mr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n\n return (m_idx / mr) * kai_get_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t n_idx, size_t k) {\n KAI_ASSERT((n_idx % kai_get_nr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n\n const size_t nr = kai_get_nr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n\n return (n_idx / nr) * kai_get_rhs_packed_stride(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSERT((m_idx % kai_get_m_step_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n KAI_ASSERT((n_idx % kai_get_n_step_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n\n return ((n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride);\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n) {\n return (m * n * kai_num_bytes_dst_value);\n}\n\nvoid kai_run_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n void* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSERT(dst_stride_col == sizeof(uint16_t));\n KAI_ASSERT(n > 0);\n KAI_ASSERT(m > 0);\n\n const size_t mr = kai_get_mr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n\n KernelArgs args;\n const size_t k_internal = kai_k_roundedup(k);\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.dst_stride_row = dst_stride_row;\n args.m = m;\n args.n = n;\n args.lhs_stride = kai_get_lhs_packed_stride(k);\n args.rhs_stride = kai_get_rhs_packed_stride(k);\n args.rhs_row_bytes = nr * k_internal;\n args.m_blk = mr * k_internal;\n args.dst_inc = mr * dst_stride_row;\n args.clamp_min = scalar_min;\n args.clamp_max = scalar_max;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(&args);\n}\n\n#endif // Architectural feature check\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa_asm.S",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\n stp x19, x20, [sp, -96]!\n stp x24, x25, [sp, 16]\n stp d8, d9, [sp, 32]\n stp d10, d11, [sp, 48]\n stp d12, d13, [sp, 64]\n stp d14, d15, [sp, 80]\n KAI_ASM_INST(0xd503477f)\t// \tsmstart\n ldr\tx19, [x0] // dst\n ldr\tx20, [x0, #0x8] // lhs_packed\n cntw\tx7 // mr\n ptrue\tp2.b\n KAI_ASM_INST(0x8556c81e) // ld1rw { z30.s }, p2/z, [x0, #0x58] // clamp_min\n KAI_ASM_INST(0x8557c81f) // ld1rw { z31.s }, p2/z, [x0, #0x5c] // clamp_max\n ldr\tx12, [x0, #0x20] // m\n KAI_ASM_INST(0x25ac17e0)\t// \twhilelt\tp0.s, xzr, x12\nKAI_ASM_LABEL(label_1) // Row Loop\n ldr\tx8, [x0, #0x10] // rhs_packed\n mov\tx9, x19\n ldr\tx13, [x0, #0x28] // n\n cmp\tx7, x12\n csel\tx16, x7, x12, lt\n lsl\tx16, x16, #2\n ldr\tx24, [x0, #0x40] // rhs_row_bytes\n add\tx24, x24, x8\n mov\tx11, x8\n KAI_ASM_INST(0x25784570)\t// \twhilelt\tpn8.h, x11, x24, vlx2\n addvl\tx11, x8, #0x2\n KAI_ASM_INST(0x25784572)\t// \twhilelt\tpn10.h, x11, x24, vlx2\nKAI_ASM_LABEL(label_2) // Column Loop\n mov\tx10, x20\n mov\tx11, x8\n mov\tx17, x9\n KAI_ASM_INST(0x25ad67f1)\t// \twhilelt\tpn9.s, xzr, x13, vlx4\n KAI_ASM_INST(0x256d67f3) // whilelt\tpn11.h, xzr, x13, vlx4\n KAI_ASM_INST(0xc00800ff)\t// \tzero\t{za}\n ldr\tx24, [x0, #0x48] // m_blk\n add\tx14, x10, x24\nKAI_ASM_LABEL(label_3) // Block Loop\n ld1w\t{ z4.s }, p0/z, [x10]\n addvl\tx10, x10, #0x1\n KAI_ASM_INST(0xa0402168)\t// \tld1h\t{ z8.h, z9.h }, pn8/z, [x11]\n KAI_ASM_INST(0xa0884880)\t// \tsmopa\tza0.s, p2/m, p2/m, z4.b, z8.b\n KAI_ASM_INST(0xa0894881)\t// \tsmopa\tza1.s, p2/m, p2/m, z4.b, z9.b\n KAI_ASM_INST(0xa041296a)\t// \tld1h\t{ z10.h, z11.h }, pn10/z, [x11, #0x2, mul vl]\n KAI_ASM_INST(0xa08a4882)\t// \tsmopa\tza2.s, p2/m, p2/m, z4.b, z10.b\n KAI_ASM_INST(0xa08b4883)\t// \tsmopa\tza3.s, p2/m, p2/m, z4.b, z11.b\n addvl\tx11, x11, #0x4\n cmp\tx10, x14\n b.lt\tlabel_3\n KAI_ASM_INST(0xa040c560)\t// \tld1w\t{ z0.s - z3.s }, pn9/z, [x11]\n KAI_ASM_INST(0xa041c564)\t// \tld1w\t{ z4.s - z7.s }, pn9/z, [x11, #0x4, mul vl]\n KAI_ASM_INST(0xa042c568)\t// \tld1w\t{ z8.s - z11.s }, pn9/z, [x11, #0x8, mul vl]\n addvl\tx11, x11, #0xc\n mov\tx14, #0x0 // =0\n addvl\tx15, x10, #0x1\nKAI_ASM_LABEL(label_4)\n KAI_ASM_INST(0x8540c950) // ld1rw { z16.s }, p2/z, [x10]\n KAI_ASM_INST(0x8540c9f1) // ld1rw { z17.s }, p2/z, [x15]\n add\tx10, x10, #0x4\n add\tx15, x15, #0x4\n KAI_ASM_INST(0xc006440c)\t// \tmov\t{ z12.b - z15.b }, za0h.b[w14, 0x0:0x3]\n fmul\tz20.s, z17.s, z4.s\n fmul\tz21.s, z17.s, z5.s\n fmul\tz22.s, z17.s, z6.s\n fmul\tz23.s, z17.s, z7.s\n mla\t z12.s, p2/m, z16.s, z0.s\n mla\t z13.s, p2/m, z16.s, z1.s\n mla\t z14.s, p2/m, z16.s, z2.s\n mla\t z15.s, p2/m, z16.s, z3.s\n KAI_ASM_INST(0xc132e18c)\t// \tscvtf\t{ z12.s - z15.s }, { z12.s - z15.s }\n fmul\tz24.s, z20.s, z12.s\n fmul\tz25.s, z21.s, z13.s\n fmul\tz26.s, z22.s, z14.s\n fmul\tz27.s, z23.s, z15.s\n fadd\tz24.s, p2/m, z24.s, z8.s\n fadd\tz25.s, p2/m, z25.s, z9.s\n fadd\tz26.s, p2/m, z26.s, z10.s\n fadd\tz27.s, p2/m, z27.s, z11.s\n KAI_ASM_INST(0xc1bfcbd8) // fclamp\t{ z24.s - z27.s }, z30.s, z31.s\n KAI_ASM_INST(0xc120e31c) // fcvt z28.h, { z24.s - z25.s}\n KAI_ASM_INST(0xc120e35d) // fcvt z29.h, { z26.s - z27.s}\n KAI_ASM_INST(0xa0602e3c) // st1h\t{ z28.h - z29.h }, pn11, [x17]\n ldr\tx24, [x0, #0x18] // dst_stride_row\n add\tx17, x17, x24\n add\tx14, x14, #0x4\n cmp\tx14, x16\n b.lt label_4\n ldr\tx24, [x0, #0x38] // rhs_stride\n add\tx8, x8, x24\n addvl\tx9, x9, #0x2\n ldr\tx24, [x0, #0x40] // rhs_row_bytes\n add\tx24, x24, x8\n mov\tx11, x8\n KAI_ASM_INST(0x25784570)\t// \twhilelt\tpn8.h, x11, x24, vlx2\n addvl\tx11, x8, #0x2\n KAI_ASM_INST(0x25784572)\t// \twhilelt\tpn10.h, x11, x24, vlx2\n decb x13, all\n subs xzr, xzr, x13 // cmp xzr, x13\n b.mi\tlabel_2\n ldr\tx24, [x0, #0x30]\n add\tx20, x20, x24\n ldr\tx24, [x0, #0x50]\n add\tx19, x19, x24\n cntw\tx24\n sub\tx12, x12, x24\n whilelt\tp0.s, xzr, x12\n b.mi\tlabel_1\n KAI_ASM_INST(0xd503467f)\t// \tsmstop\n ldp d14, d15, [sp, 80]\n ldp d12, d13, [sp, 64]\n ldp d10, d11, [sp, 48]\n ldp d8, d9, [sp, 32]\n ldp x24, x25, [sp, 16]\n ldp x19, x20, [sp], 96\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f16_neon.c",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) || !defined(__ARM_FEATURE_FP16_SCALAR_ARITHMETIC) || \\\n !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)\n#error This file must be compiled for AArch64, FEAT_FP16.\n#else // Architectural features check.\n\n#include \"kai_lhs_quant_pack_qai8dxp_f16_neon.h\"\n\n#include <arm_fp16.h>\n#include <arm_neon.h>\n#include <float.h>\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n#define FLT16_MAX 65504.0\n#define FLT16_MIN (-65504.0F)\n\nstatic const size_t kai_num_bytes_per_multiplier = sizeof(float);\nstatic const size_t kai_num_bytes_per_offset = sizeof(int32_t);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return mr * (k_internal * sizeof(int8_t) + kai_num_bytes_per_multiplier + kai_num_bytes_per_offset);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f16_neon(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f16_neon(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f16_neon(\n size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) {\n // It always points to the beginning of the row\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f16_neon(size_t m, size_t k, size_t mr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return num_rows * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nvoid kai_run_lhs_quant_pack_qai8dxp_f16_neon(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* restrict lhs,\n size_t lhs_stride, void* restrict lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSUME((kr / sr == 8) || (kr / sr == 4));\n\n if (m == 0) {\n return;\n }\n\n const size_t num_rows = m;\n\n float16_t const* src_ptr = (float16_t const*)lhs;\n\n const size_t dst_stride = kai_lhs_packed_stride(k, mr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n const int32_t num_blocks_k = (int32_t)(k / k_block_len);\n const int32_t num_blocks_k_internal = (int32_t)(k_internal / k_block_len);\n const size_t lhs_row_length = lhs_stride / sizeof(float16_t);\n\n const float16x8_t vmax = vdupq_n_f16((float16_t)FLT16_MIN);\n const float16x8_t vmin = vdupq_n_f16((float16_t)FLT16_MAX);\n\n // As we load 8-element vectors, limit vectorized loop to avoid reading out-of-bounds\n const int32_t blocks_lim_k = num_blocks_k - (8 / k_block_len);\n\n size_t row_idx = 0;\n\n // Improved performance with 4x loop unrolling where packing parameters allow\n if (mr == 4) {\n for (; row_idx + 3 < m; row_idx += 4) {\n // Find min/max for each channel\n int32_t k_idx = 0;\n float16x8_t vmax0 = vmax;\n float16x8_t vmin0 = vmin;\n float16x8_t vmax1 = vmax;\n float16x8_t vmin1 = vmin;\n float16x8_t vmax2 = vmax;\n float16x8_t vmin2 = vmin;\n float16x8_t vmax3 = vmax;\n float16x8_t vmin3 = vmin;\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float16x8_t src0 = vld1q_f16(src_ptr + k_idx);\n const float16x8_t src1 = vld1q_f16(src_ptr + k_idx + lhs_row_length);\n const float16x8_t src2 = vld1q_f16(src_ptr + k_idx + (2 * lhs_row_length));\n const float16x8_t src3 = vld1q_f16(src_ptr + k_idx + (3 * lhs_row_length));\n\n vmax0 = vmaxq_f16(src0, vmax0);\n vmax1 = vmaxq_f16(src1, vmax1);\n vmax2 = vmaxq_f16(src2, vmax2);\n vmax3 = vmaxq_f16(src3, vmax3);\n vmin0 = vminq_f16(src0, vmin0);\n vmin1 = vminq_f16(src1, vmin1);\n vmin2 = vminq_f16(src2, vmin2);\n vmin3 = vminq_f16(src3, vmin3);\n }\n\n float16_t max0 = vmaxvq_f16(vmax0);\n float16_t min0 = vminvq_f16(vmin0);\n float16_t max1 = vmaxvq_f16(vmax1);\n float16_t min1 = vminvq_f16(vmin1);\n float16_t max2 = vmaxvq_f16(vmax2);\n float16_t min2 = vminvq_f16(vmin2);\n float16_t max3 = vmaxvq_f16(vmax3);\n float16_t min3 = vminvq_f16(vmin3);\n // Process leftover elements with a scalar loop.\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float16_t src0 = *(src_ptr + (size_t)k_idx);\n max0 = vmaxh_f16(src0, max0);\n min0 = vminh_f16(src0, min0);\n const float16_t src1 = *(src_ptr + (size_t)k_idx + lhs_row_length);\n max1 = vmaxh_f16(src1, max1);\n min1 = vminh_f16(src1, min1);\n const float16_t src2 = *(src_ptr + (size_t)k_idx + (2 * lhs_row_length));\n max2 = vmaxh_f16(src2, max2);\n min2 = vminh_f16(src2, min2);\n const float16_t src3 = *(src_ptr + (size_t)k_idx + (3 * lhs_row_length));\n max3 = vmaxh_f16(src3, max3);\n min3 = vminh_f16(src3, min3);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n const float rmin1 = fminf(0.0F, min1);\n const float rmax1 = fmaxf(0.0F, max1);\n const float scale1 = rmin1 == rmax1 ? 1.F : (qmax - qmin) / (rmax1 - rmin1);\n const float rmin2 = fminf(0.0F, min2);\n const float rmax2 = fmaxf(0.0F, max2);\n const float scale2 = rmin2 == rmax2 ? 1.F : (qmax - qmin) / (rmax2 - rmin2);\n const float rmin3 = fminf(0.0F, min3);\n const float rmax3 = fmaxf(0.0F, max3);\n const float scale3 = rmin3 == rmax3 ? 1.F : (qmax - qmin) / (rmax3 - rmin3);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n const float recip_scale1 = scale1 ? 1.0F / scale1 : 0.0F;\n const float recip_scale2 = scale2 ? 1.0F / scale2 : 0.0F;\n const float recip_scale3 = scale3 ? 1.0F / scale3 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n const float descaled_min1 = rmin1 * scale1;\n const float descaled_max1 = rmax1 * scale1;\n const float descaled_min2 = rmin2 * scale2;\n const float descaled_max2 = rmax2 * scale2;\n const float descaled_min3 = rmin3 * scale3;\n const float descaled_max3 = rmax3 * scale3;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n const float zero_point_from_min_error1 = qmin + descaled_min1;\n const float zero_point_from_max_error1 = qmax + descaled_max1;\n const float zero_point_from_min_error2 = qmin + descaled_min2;\n const float zero_point_from_max_error2 = qmax + descaled_max2;\n const float zero_point_from_min_error3 = qmin + descaled_min3;\n const float zero_point_from_max_error3 = qmax + descaled_max3;\n\n float zero_point0 = (zero_point_from_min_error0 + zero_point_from_max_error0 > 0) ? qmin - descaled_min0\n : qmax - descaled_max0;\n float zero_point1 = (zero_point_from_min_error1 + zero_point_from_max_error1 > 0) ? qmin - descaled_min1\n : qmax - descaled_max1;\n float zero_point2 = (zero_point_from_min_error2 + zero_point_from_max_error2 > 0) ? qmin - descaled_min2\n : qmax - descaled_max2;\n float zero_point3 = (zero_point_from_min_error3 + zero_point_from_max_error3 > 0) ? qmin - descaled_min3\n : qmax - descaled_max3;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n zero_point1 = fmaxf(zero_point1, qmin);\n zero_point1 = fminf(zero_point1, qmax);\n zero_point2 = fmaxf(zero_point2, qmin);\n zero_point2 = fminf(zero_point2, qmax);\n zero_point3 = fmaxf(zero_point3, qmin);\n zero_point3 = fminf(zero_point3, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n const int32_t nudged_zero_point1 = (int32_t)rintf(zero_point1);\n const int32_t nudged_zero_point2 = (int32_t)rintf(zero_point2);\n const int32_t nudged_zero_point3 = (int32_t)rintf(zero_point3);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len);\n\n // Quantize the channels\n int32_t block_idx = 0;\n const int32_t block_incr = 8 / k_block_len;\n\n for (; block_idx <= blocks_lim_k; block_idx += block_incr) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float16x8_t src0 = vld1q_f16(src_ptr + k_idx_start);\n const float16x8_t src1 = vld1q_f16(src_ptr + k_idx_start + lhs_row_length);\n const float16x8_t src2 = vld1q_f16(src_ptr + k_idx_start + (2 * lhs_row_length));\n const float16x8_t src3 = vld1q_f16(src_ptr + k_idx_start + (3 * lhs_row_length));\n\n // Scale the values.\n const int32x4_t v0_0_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src0)), scale0));\n const int32x4_t v0_1_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_high_f32_f16(src0), scale0));\n const int32x4_t v1_0_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src1)), scale1));\n const int32x4_t v1_1_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_high_f32_f16(src1), scale1));\n const int32x4_t v2_0_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src2)), scale2));\n const int32x4_t v2_1_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_high_f32_f16(src2), scale2));\n const int32x4_t v3_0_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src3)), scale3));\n const int32x4_t v3_1_s32 = vcvtq_s32_f32(vmulq_n_f32(vcvt_high_f32_f16(src3), scale3));\n\n const int16x4_t v0_0_s16 = vqmovn_s32(v0_0_s32);\n const int16x4_t v0_1_s16 = vqmovn_s32(v0_1_s32);\n const int16x4_t v1_0_s16 = vqmovn_s32(v1_0_s32);\n const int16x4_t v1_1_s16 = vqmovn_s32(v1_1_s32);\n const int16x4_t v2_0_s16 = vqmovn_s32(v2_0_s32);\n const int16x4_t v2_1_s16 = vqmovn_s32(v2_1_s32);\n const int16x4_t v3_0_s16 = vqmovn_s32(v3_0_s32);\n const int16x4_t v3_1_s16 = vqmovn_s32(v3_1_s32);\n\n int16x8_t v0_s16;\n int16x8_t v1_s16;\n int16x8_t v2_s16;\n int16x8_t v3_s16;\n if (k_block_len == 8) {\n v0_s16 = vcombine_s16(v0_0_s16, v0_1_s16);\n v1_s16 = vcombine_s16(v1_0_s16, v1_1_s16);\n v2_s16 = vcombine_s16(v2_0_s16, v2_1_s16);\n v3_s16 = vcombine_s16(v3_0_s16, v3_1_s16);\n } else { // k_block_len == 4\n v0_s16 = vcombine_s16(v0_0_s16, v1_0_s16);\n v1_s16 = vcombine_s16(v2_0_s16, v3_0_s16);\n v2_s16 = vcombine_s16(v0_1_s16, v1_1_s16);\n v3_s16 = vcombine_s16(v2_1_s16, v3_1_s16);\n }\n\n // Add zero points.\n const int16x8_t vnzp0 = vdupq_n_s16((int16_t)nudged_zero_point0);\n const int16x8_t vnzp1 = vdupq_n_s16((int16_t)nudged_zero_point1);\n const int16x8_t vnzp2 = vdupq_n_s16((int16_t)nudged_zero_point2);\n const int16x8_t vnzp3 = vdupq_n_s16((int16_t)nudged_zero_point3);\n\n v0_s16 = vaddq_s16(v0_s16, vnzp0);\n v0_s16 = vmaxq_s16(v0_s16, vdupq_n_s16(INT8_MIN));\n v0_s16 = vminq_s16(v0_s16, vdupq_n_s16(INT8_MAX));\n v1_s16 = vaddq_s16(v1_s16, vnzp1);\n v1_s16 = vmaxq_s16(v1_s16, vdupq_n_s16(INT8_MIN));\n v1_s16 = vminq_s16(v1_s16, vdupq_n_s16(INT8_MAX));\n v2_s16 = vaddq_s16(v2_s16, vnzp2);\n v2_s16 = vmaxq_s16(v2_s16, vdupq_n_s16(INT8_MIN));\n v2_s16 = vminq_s16(v2_s16, vdupq_n_s16(INT8_MAX));\n v3_s16 = vaddq_s16(v3_s16, vnzp3);\n v3_s16 = vmaxq_s16(v3_s16, vdupq_n_s16(INT8_MIN));\n v3_s16 = vminq_s16(v3_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(v0_s16);\n int8x8_t v1_s8 = vqmovn_s16(v1_s16);\n int8x8_t v2_s8 = vqmovn_s16(v2_s16);\n int8x8_t v3_s8 = vqmovn_s16(v3_s16);\n\n vst1_s8((int8_t*)(dst_ptr), v0_s8);\n vst1_s8((int8_t*)(dst_ptr + sizeof(int8x8_t)), v1_s8);\n vst1_s8((int8_t*)(dst_ptr + 2 * sizeof(int8x8_t)), v2_s8);\n vst1_s8((int8_t*)(dst_ptr + 3 * sizeof(int8x8_t)), v3_s8);\n dst_ptr += block_incr * mr * k_block_len * sizeof(int8_t);\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index.\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0 = (float)(*(src_ptr + k_idx_start));\n const float src1 = (float)(*(src_ptr + k_idx_start + lhs_row_length));\n const float src2 = (float)(*(src_ptr + k_idx_start + (2 * lhs_row_length)));\n const float src3 = (float)(*(src_ptr + k_idx_start + (3 * lhs_row_length)));\n\n // Scale the value.\n int32_t d0_s32 = (int32_t)(roundf(src0 * scale0));\n int32_t d1_s32 = (int32_t)(roundf(src1 * scale1));\n int32_t d2_s32 = (int32_t)(roundf(src2 * scale2));\n int32_t d3_s32 = (int32_t)(roundf(src3 * scale3));\n\n d0_s32 = d0_s32 + nudged_zero_point0;\n d0_s32 = KAI_MAX(d0_s32, INT8_MIN);\n d0_s32 = KAI_MIN(d0_s32, INT8_MAX);\n\n d1_s32 = d1_s32 + nudged_zero_point1;\n d1_s32 = KAI_MAX(d1_s32, INT8_MIN);\n d1_s32 = KAI_MIN(d1_s32, INT8_MAX);\n\n d2_s32 = d2_s32 + nudged_zero_point2;\n d2_s32 = KAI_MAX(d2_s32, INT8_MIN);\n d2_s32 = KAI_MIN(d2_s32, INT8_MAX);\n\n d3_s32 = d3_s32 + nudged_zero_point3;\n d3_s32 = KAI_MAX(d3_s32, INT8_MIN);\n d3_s32 = KAI_MIN(d3_s32, INT8_MAX);\n\n *(int8_t*)dst_ptr = (int8_t)d0_s32;\n *(int8_t*)(dst_ptr + k_block_len * sizeof(int8_t)) = (int8_t)d1_s32;\n *(int8_t*)(dst_ptr + 2 * (k_block_len * sizeof(int8_t))) = (int8_t)d2_s32;\n *(int8_t*)(dst_ptr + 3 * (k_block_len * sizeof(int8_t))) = (int8_t)d3_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n uint8_t* dst_base = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr = dst_base + dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row.\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n *((int32_t*)(dst_ptr + kai_num_bytes_per_offset)) = -nudged_zero_point1;\n *((int32_t*)(dst_ptr + 2 * kai_num_bytes_per_offset)) = -nudged_zero_point2;\n *((int32_t*)(dst_ptr + 3 * kai_num_bytes_per_offset)) = -nudged_zero_point3;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier.\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params.\n *((float*)(dst_ptr)) = recip_scale0;\n *((float*)(dst_ptr + kai_num_bytes_per_multiplier)) = recip_scale1;\n *((float*)(dst_ptr + 2 * kai_num_bytes_per_multiplier)) = recip_scale2;\n *((float*)(dst_ptr + 3 * kai_num_bytes_per_multiplier)) = recip_scale3;\n\n // Update src_ptr. Note: now lhs contains fp16 values (2 bytes each).\n src_ptr += (4 * lhs_row_length);\n\n // Move to the next row as we have interleaved all Mr rows.\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n\n for (; row_idx < num_rows; ++row_idx) {\n // Find min/max for each channel\n int32_t k_idx = 0;\n float16x8_t vmax0 = vmax;\n float16x8_t vmin0 = vmin;\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float16x8_t src0_0 = vld1q_f16(src_ptr + (size_t)k_idx);\n vmax0 = vmaxq_f16(vmax0, src0_0);\n vmin0 = vminq_f16(vmin0, src0_0);\n }\n // Get the max/min\n float16_t max0 = vmaxvq_f16(vmax0);\n float16_t min0 = vminvq_f16(vmin0);\n\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float16_t src0 = *(src_ptr + (size_t)k_idx);\n max0 = vmaxh_f16(src0, max0);\n min0 = vminh_f16(src0, min0);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n\n float zero_point0 =\n zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len * sizeof(int8_t));\n\n // Quantize the channels\n int32_t block_idx = 0;\n if (k_block_len == 8) {\n for (; block_idx < blocks_lim_k; ++block_idx) {\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float16x8_t src_0 = vld1q_f16(src_ptr + k_idx_start);\n\n // Scale the values\n const float32x4_t v0_f32 = vmulq_n_f32(vcvt_f32_f16(vget_low_f16(src_0)), scale0);\n const float32x4_t v1_f32 = vmulq_n_f32(vcvt_high_f32_f16(src_0), scale0);\n const int32x4_t v0_s32 = vcvtnq_s32_f32(v0_f32);\n const int32x4_t v1_s32 = vcvtnq_s32_f32(v1_f32);\n\n const int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n const int16x4_t v1_s16 = vqmovn_s32(v1_s32);\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x8_t vnzp_s16 = vdupq_n_s16(nzp_s16);\n v_s16 = vaddq_s16(v_s16, vnzp_s16);\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v_s8 = vqmovn_s16(v_s16);\n vst1_s8((int8_t*)(dst_ptr), v_s8);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n } else if (k_block_len == 4) {\n for (; block_idx < blocks_lim_k; ++block_idx) {\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float16x4_t src_0 = vld1_f16(src_ptr + k_idx_start);\n\n // Scale the values\n const float32x4_t v0_f32 = vmulq_n_f32(vcvt_f32_f16(src_0), scale0);\n const int32x4_t v0_s32 = vcvtnq_s32_f32(v0_f32);\n\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x4_t vnzp_s16 = vdup_n_s16(nzp_s16);\n v0_s16 = vadd_s16(v0_s16, vnzp_s16);\n v0_s16 = vmax_s16(v0_s16, vdup_n_s16(INT8_MIN));\n v0_s16 = vmin_s16(v0_s16, vdup_n_s16(INT8_MAX));\n\n int8x8_t v_s8 = vqmovn_s16(vcombine_s16(v0_s16, vdup_n_s16(0)));\n\n *((int8_t*)(dst_ptr)) = vget_lane_s8(v_s8, 0);\n *((int8_t*)(dst_ptr + 1)) = vget_lane_s8(v_s8, 1);\n *((int8_t*)(dst_ptr + 2)) = vget_lane_s8(v_s8, 2);\n *((int8_t*)(dst_ptr + 3)) = vget_lane_s8(v_s8, 3);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0 = (float)(*(src_ptr + k_idx_start));\n\n // Scale the values\n int32_t d0_s32 = (int32_t)(roundf(src0 * scale0));\n\n d0_s32 = d0_s32 + nudged_zero_point0;\n d0_s32 = KAI_MAX(d0_s32, INT8_MIN);\n d0_s32 = KAI_MIN(d0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)d0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n dst_ptr = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr += dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params\n *((float*)(dst_ptr)) = recip_scale0;\n\n src_ptr += lhs_row_length;\n\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n}\n\n#endif // Architectural features check.\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f16_neon.h",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <arm_fp16.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f16_neon(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f16_neon(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qa8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f16_neon(\n size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f16_neon(size_t m, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS of the vector-by-matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qai8dxp_f16_neon(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs, size_t lhs_stride,\n void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.c",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nsize_t kai_get_n_step_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t nr) {\n return nr;\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n const size_t k_internal = kai_k_roundedup(k);\n\n return nr * (k_internal + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_ASSERT((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n, size_t k, size_t nr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n}\n\nvoid kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n const int8_t* rhs, //\n const float* bias, //\n const float* scale, //\n void* rhs_packed, //\n size_t extra_bytes, const struct kai_rhs_pack_qsi8cx_params* params) {\n KAI_ASSERT(num_groups == 1);\n KAI_ASSERT(extra_bytes == 0);\n KAI_ASSERT(sr == 1);\n KAI_ASSERT(rhs != NULL);\n KAI_ASSERT(scale != NULL);\n KAI_ASSERT(rhs_packed != NULL);\n KAI_ASSERT(params != NULL);\n\n const int32_t lhs_zero_point = params->lhs_zero_point;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const size_t dst_num_rows = kai_roundup(n, nr) / nr;\n const size_t dst_num_bytes_per_row = nr * k_internal;\n const size_t rhs_stride = k;\n\n for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; ++dst_row_idx) {\n uint8_t* dst_row = (uint8_t*)rhs_packed + dst_row_idx * rhs_packed_stride;\n\n int32_t* sums = (int32_t*)(dst_row + nr * k_internal);\n\n // Initialize to zero the RHS reduction sums\n memset(sums, 0, nr * sizeof(int32_t));\n\n for (size_t dst_offset = 0; dst_offset < dst_num_bytes_per_row; dst_offset += kr) {\n const size_t block_idx = dst_offset / kr;\n const size_t nr_idx = block_idx % nr;\n const size_t super_block_idx = block_idx / nr;\n\n const size_t k0_idx = super_block_idx * kr;\n const size_t n0_idx = dst_row_idx * nr + nr_idx;\n\n // Clamp the index to avoid out-of-bound reads\n const size_t n0_valid_idx = KAI_MIN(n0_idx, n - 1);\n\n const size_t src_offset = n0_valid_idx * rhs_stride;\n\n int32_t partial_sum = 0;\n\n // Get the partial reduction sum\n for (size_t i = 0; i < kr; i++) {\n const size_t k0_valid_idx = k0_idx + i;\n int8_t v = 0;\n if (k0_valid_idx < k) {\n v = rhs[src_offset + k0_valid_idx];\n }\n ((int8_t*)dst_row)[i] = v;\n partial_sum += v;\n }\n\n sums[nr_idx] += partial_sum * lhs_zero_point;\n\n dst_row += kr;\n }\n\n // Adjust the reduction sums\n for (size_t i = 0; i < nr; ++i) {\n dst_row += sizeof(int32_t);\n }\n\n // Adjust the scales\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n *((float*)(dst_row)) = scale[src_row_idx];\n dst_row += sizeof(float);\n }\n\n // Set the bias\n if (bias == NULL) {\n memset(dst_row, 0, nr * kai_num_bytes_bias);\n } else {\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n ((float*)dst_row)[i] = bias[src_row_idx];\n }\n }\n }\n}\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Get the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n///\n/// @return the n step value\nsize_t kai_get_n_step_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t nr);\n\n/// Gets the offset in bytes for the RHS matrix (not packed).\n///\n/// @note The int8 values are stored in a N x K matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n_idx, size_t rhs_stride);\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Gets the offset in bytes for the packed RHS matrix, which contains the packed 8-bit quantized symmetric per-channel\n/// (qs8cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// @brief Gets the size in bytes for the packed RHS matrix\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int8 values are stored in a N x K matrix.\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of rows.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K). It must be an even value.\n/// @param[in] nr The number of N rows to interleave on the same output output row.\n/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n/// @param[in] rhs The RHS matrix containing the 8-bit values.\n/// Size in bytes is expected to be equal to or greater than n * k * sizeof(uint8_t).\n/// @param[in] bias The biases.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n const int8_t* rhs, //\n const float* bias, //\n const float* scale, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_qsi8cx_params* params);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "binding.cpp",
|
| 65 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa against gemm_f16_qs8cx_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const uint16_t* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M)\n#include <arm_neon.h>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_quant_pack_qai8dxp_f16_neon.h\"\n#include \"kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n// Widen n raw fp16 patterns to fp32 (exact).\nvoid widen_f16(const uint16_t* src, float* dst, size_t n) {\n size_t i = 0;\n for (; i + 4 <= n; i += 4) vst1q_f32(dst + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(src + i))));\n for (; i < n; ++i) dst[i] = vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(src[i]))), 0);\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M)\n{\n // The SME2 mopa kernel takes any M in a single call, so there is no GEMV dispatch here.\n const size_t mr = kai_get_mr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f16_neon((size_t)M, (size_t)K, mr, kr, sr);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_quant_pack_qai8dxp_f16_neon((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(uint16_t), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q8_snap = nullptr;\n static size_t B_q8_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q8_snap, B_q8_snap_size, B_q8, (size_t)N*K)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N)\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon((size_t)N, (size_t)K, nr, kr, sr);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_qsi8cx_params params;\n params.lhs_zero_point = 1;\n params.scale_multiplier = 1.0f;\n kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(1, (size_t)N, (size_t)K, nr, kr, sr, B_q8, bias, B_scales, rhs_packed, 0, ¶ms);\n }\n\n static uint16_t* tmp16 = nullptr;\n static size_t tmp16_cap = 0;\n if ((size_t)M * N > tmp16_cap) {\n std::free(tmp16);\n tmp16 = static_cast<uint16_t*>(std::malloc(sizeof(uint16_t) * (size_t)M * N));\n tmp16_cap = (size_t)M * N;\n if (tmp16 == nullptr) return -1;\n }\n kai_run_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa((size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, tmp16, (size_t)N * sizeof(uint16_t), sizeof(uint16_t), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n // fp16 kernel result widened to the raw ABI's fp32 output (exact).\n widen_f16(tmp16, output, (size_t)M * N);\n return 0;\n}\n"
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f16act_qs4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_f16act_qs4c32_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_f16act_qs4c32_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_f16act_qs4c32_sme2_n256_k512: KleidiAI kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa.c",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2) || !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)) && \\\n !defined(_M_ARM64)\n#error This file must be compiled for AArch64 and FEAT_SVE2.\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\ntypedef struct {\n float* dst; // 0\n const void* lhs_packed; // 0x8\n const void* rhs_packed; // 0x10\n const void* rhs_scales; // 0x18\n size_t dst_stride_row; // 0x20\n size_t lhs_packed_stride; // 0x28\n size_t rhs_packed_stride; // 0x30\n size_t m; // 0x38\n size_t n; // 0x40\n size_t k; // 0x48\n size_t bl; // 0x50\n const uint16_t* lut; // 0x58\n float min; // 0x60\n float max; // 0x64\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(KernelArgs* args_ptr);\n\n// Compute args\nstatic const size_t kai_m_step = 1; // Multiple of vector length\nstatic const size_t kai_n_step = 4; // Multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // Multiple of vector length\nstatic const size_t kai_nr = 4; // Multiple of vector length\nstatic const size_t kai_kr = 4;\nstatic const size_t kai_sr = 2;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 2;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_recip_num_bytes_qvalue_rhs = 2;\nstatic const size_t kai_num_bytes_multiplier_rhs = 2;\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_bl = 32;\n\n// Look-up table used for int4->int8 convert\nstatic const uint16_t lut[] = {\n 0xc800, // = -8.0\n 0x0000, // = 0.0\n 0xc700, // = -7.0\n 0x0000, // = 0.0\n 0xc600, // = -6.0\n 0x0000, // = 0.0\n 0xc500, // = -5.0\n 0x0000, // = 0.0\n 0xc400, // = -4.0\n 0x0000, // = 0.0\n 0xc200, // = -3.0\n 0x0000, // = 0.0\n 0xc000, // = -2.0\n 0x0000, // = 0.0\n 0xbc00, // = -1.0\n 0x0000, // = 0.0\n 0x0000, // = 0.0\n 0x0000, // = 0.0\n 0x3c00, // = 1.0\n 0x0000, // = 0.0\n 0x4000, // = 2.0\n 0x0000, // = 0.0\n 0x4200, // = 3.0\n 0x0000, // = 0.0\n 0x4400, // = 4.0\n 0x0000, // = 0.0\n 0x4500, // = 5.0\n 0x0000, // = 0.0\n 0x4600, // = 6.0\n 0x0000, // = 0.0\n 0x4700, // = 7.0\n 0x0000, // = 0.0\n};\n\n#define KAI_LUT_NENTRIES (sizeof(lut) / sizeof(lut[0]))\ninline static size_t kai_get_num_bytes_per_block_lhs(size_t bl) {\n return (bl * kai_num_bytes_qvalue_lhs);\n}\n\ninline static size_t kai_get_num_bytes_per_block_rhs(size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n size_t num_bytes_per_block_rhs = (bl / kai_recip_num_bytes_qvalue_rhs) + kai_num_bytes_multiplier_rhs;\n return num_bytes_per_block_rhs;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n KAI_ASSUME((k % kai_bl) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n return k / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t bl) {\n const size_t mr = kai_get_mr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n return mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block_lhs(bl);\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n KAI_ASSUME((k % kai_bl) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl);\n const size_t nr = kai_get_nr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n\n size_t rhs_packed_stride = nr * (num_bytes_per_block * num_blocks_per_row);\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t k, size_t bl) {\n const size_t mr = kai_get_mr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n KAI_ASSUME((m_idx % mr) == 0);\n\n return (m_idx / mr) * kai_get_lhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t n_idx, size_t k, size_t bl) {\n const size_t nr = kai_get_nr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n\n KAI_ASSUME((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n const size_t m_step = kai_get_m_step_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n const size_t n_step = kai_get_n_step_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n KAI_ASSUME((m_idx % m_step) == 0);\n KAI_ASSUME((n_idx % n_step) == 0);\n\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSUME((bl % kai_bl) == 0);\n\n KAI_UNUSED(dst_stride_col);\n\n const size_t num_blocks = kai_get_num_blocks_per_row(k, bl);\n\n const size_t nr = kai_get_nr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride(k, bl);\n\n KernelArgs args;\n const uint8_t* rhs_packed_p = rhs_packed;\n const uint8_t* rhs_scales = rhs_packed_p + rhs_packed_stride - (nr * num_blocks) * kai_num_bytes_multiplier_rhs;\n\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.rhs_scales = rhs_scales;\n args.dst_stride_row = dst_stride_row;\n args.lhs_packed_stride = kai_get_lhs_packed_stride(k, bl);\n args.rhs_packed_stride = rhs_packed_stride;\n args.m = m;\n args.n = n;\n args.k = k;\n args.bl = bl;\n\n args.lut = lut;\n args.min = scalar_min;\n args.max = scalar_max;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa_asm.S",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n KAI_ASM_CODE(matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa)\n KAI_ASM_ALIGN\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa)\n stp x19, x20, [sp, -128 ]!\n stp x21, x22, [sp, 16]\n stp x23, x24, [sp, 32]\n stp d8, d9, [sp, 48]\n stp d10, d11, [sp, 64]\n stp d12, d13, [sp, 80]\n stp d14, d15, [sp, 96]\n KAI_ASM_INST(0xd503477f) // smstart\n cntw x14\n ptrue p2.b, all\n ldr x19, [x0, #0x58] // lut\n KAI_ASM_INST(0xe11f8260) //ldr zt0, [x19]\n mov x8, #0\n ldr x16, [x0, #0x10] // rhs_packed\n ldr x17, [x0, #0x18] // rhs_scales\n ldr x6, [x0, #0x40] // N\n KAI_ASM_INST(0x8558c81c) // ld1rw z28.s, p2/z, [x0, #0x60] // min\n KAI_ASM_INST(0x8559c81d) // ld1rw z29.s, p2/z, [x0, #0x64] // max\n\n KAI_ASM_INST(0x25664510) // whilelt pn8.h, x8, x6, VLx2\n KAI_ASM_INST(0x25a66511) // whilelt pn9.s, x8, x6, VLx4\n ldr x4, [x0] // dst\n b.eq label_9 // b.none label_9\nKAI_ASM_LABEL(label_1) // N loop\n ldr x9, [x0, #0x38] // M\n KAI_ASM_INST(0x25261501) // whilelt p1.b, x8, x6\n ldr x22, [x0, #0x8] // lhs_packed\n mov x24, x4 // dst\nKAI_ASM_LABEL(label_2) // M loop\n KAI_ASM_INST(0xc00800ff) // zero {za}\n KAI_ASM_INST(0x25a917e0)// whilelt p0.s, xzr, x9\n mov x23, x22\n mov x20, #0\n mov x21, #0\n ldr x10, [x0, #0x48] // K\n cmp x10, #0\n b.eq label_8\nKAI_ASM_LABEL(label_3) // K loop\n lsl x5, x21, 1\n KAI_ASM_INST(0xa0052220) // ld1h {z0.h - z1.h}, pn8/z, [x17, x5, lsl #1]\n KAI_ASM_INST(0xc160d018) // zip {z24.h - z25.h}, z0.h, z0.h\n KAI_ASM_INST(0xc161d03a) // zip {z26.h - z27.h}, z1.h, z1.h\n ldr x11, [x0, #0x50] //bl\nKAI_ASM_LABEL(label_4) // Block loop\n lsl x5, x20, 2\n KAI_ASM_INST(0xa425c604) // ld2b {z4.b, z5.b}, p1/z, [x16, x5]\n inch x20, all\n KAI_ASM_INST(0xa540a2e6)// ld1w {z6.s}, p0/z, [x23]\n KAI_ASM_INST(0xc08a9088) // luti4 {z8.h - z11.h}, zt0, z4[0]\n KAI_ASM_INST(0x65580908) // fmul z8.h, z8.h, z24.h\n KAI_ASM_INST(0x65590929) // fmul z9.h, z9.h, z25.h\n KAI_ASM_INST(0x655a094a) // fmul z10.h, z10.h, z26.h\n KAI_ASM_INST(0x655b096b) // fmul z11.h, z11.h, z27.h\n KAI_ASM_INST(0x81a848c0) // fmopa za0.s, p2/m, p2/m, z6.h, z8.h\n KAI_ASM_INST(0x81a948c1) // fmopa za1.s, p2/m, p2/m, z6.h, z9.h\n KAI_ASM_INST(0x81aa48c2) // fmopa za2.s, p2/m, p2/m, z6.h, z10.h\n KAI_ASM_INST(0x81ab48c3) // fmopa za3.s, p2/m, p2/m, z6.h, z11.h\n KAI_ASM_INST(0xa54e42e7) // ld1w {z7.s}, p0/z, [x23, x14, lsl 2]\n add x23, x23, x14, lsl 3\n KAI_ASM_INST(0xc08a90ac) // luti4 {z12.h - z15.h}, zt0, z5[0]\n KAI_ASM_INST(0x6558098c) // fmul z12.h, z12.h, z24.h\n KAI_ASM_INST(0x655909ad) // fmul z13.h, z13.h, z25.h\n KAI_ASM_INST(0x655a09ce) // fmul z14.h, z14.h, z26.h\n KAI_ASM_INST(0x655b09ef) // fmul z15.h, z15.h, z27.h\n KAI_ASM_INST(0x81ac48e0) // fmopa za0.s, p2/m, p2/m, z7.h, z12.h\n KAI_ASM_INST(0x81ad48e1) // fmopa za1.s, p2/m, p2/m, z7.h, z13.h\n KAI_ASM_INST(0x81ae48e2) // fmopa za2.s, p2/m, p2/m, z7.h, z14.h\n KAI_ASM_INST(0x81af48e3) // fmopa za3.s, p2/m, p2/m, z7.h, z15.h\n subs x11, x11, #4\n b.gt label_4\n ldr x5, [x0, #0x50] // bl\n subs x10, x10, x5\n inch x21, all\n b.gt label_3\nKAI_ASM_LABEL(label_8)\n cmp x9, x14\n csel x15, x9, x14, lo\n lsl x15, x15, #2\n mov w12, #0\nKAI_ASM_LABEL(label_5) // store tiles\n KAI_ASM_INST(0xc0060400) // mova {z0.b - z3.b}, za0h.b[w12, 0:3]\n KAI_ASM_INST(0xc1bdcb80) // fclamp { z0.s - z3.s}, z28.s, z29.s\n KAI_ASM_INST(0xa060c700) // st1w {z0.s - z3.s}, pn9, [x24]\n ldr x5, [x0, #0x20] // dst_stride_row\n add x24, x24, x5\n add w12, w12, #4\n cmp w12, w15\n blt label_5\n ldr x5, [x0, #0x28] // offset_stride_l\n add x22, x22, x5\n decw x9, all\n cmp x9, #0\n b.gt label_2\n incb x4, all, mul #4 // dst\n ldr x5, [x0, #0x30] // rhs_packed_stride\n add x16, x16, x5\n add x17, x17, x5\n incb x8, all\n KAI_ASM_INST(0x25664510) // whilelt pn8.h, x8, x6, VLx2\n KAI_ASM_INST(0x25a66511) // whilelt pn9.s, x8, x6, VLx4\n b.mi label_1 // b.first label_1\nKAI_ASM_LABEL(label_9)\n KAI_ASM_INST(0xd503467f) // smstop\n ldp d14, d15, [sp, 96]\n ldp d12, d13, [sp, 80]\n ldp d10, d11, [sp, 64]\n ldp d8, d9, [sp, 48]\n ldp x23, x24, [sp, 32]\n ldp x21, x22, [sp, 16]\n ldp x19, x20, [sp],128\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa.h",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_lhs_pack_f16pmrx2_f32_neon to dynamically quantize and pack the LHS matrix in a single step.\n/// -# @ref kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon to pack the RHS NxK matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed fp16 values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of m_step.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t m_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Symmetric Signed 4-bit with per-block (32) quantization (qsi4c32) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t n_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of m_step.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of n_step.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride); //\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n); //\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Packed fp16 (f16pmrx2)\n/// RHS matrix: Quantized Symmetric Signed 4-bit with per-block (32) quantization (qsi4c32) and packed.\n/// Output tile: (rows x cols) = 1 VL x 4 VL (Vector Length)\n///\n/// Instruction used: SME2 (MOPA)\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] bl Block length. It must be a multiple of 32.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel dependencies list at the top of this file reports\n/// the available LHS packing functions for this micro-kernel.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel dependencies list at the top of this file reports\n/// the available RHS packing functions for this micro-kernel.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. Unused.\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n}\n#endif // __cplusplus\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_pack_f16pmrx2_f32_neon.h",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n/// Gets m step value.\n///\n/// The starting row index must be divisible by `m_step`.\n///\n/// @param[in] mr Number of rows to be interleaved.\n///\n/// @return The m step value.\nsize_t kai_get_m_step_lhs_pack_f16pmrx2_f32_neon(size_t mr);\n\n/// Gets the offset in bytes to the data element in the LHS buffer.\n///\n/// @param[in] m_idx Row index.\n/// @param[in] lhs_stride Row stride in bytes.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_lhs_offset_lhs_pack_f16pmrx2_f32_neon(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes to the data element in the packed LHS buffer.\n///\n/// @param[in] m_idx Row index in the unpacked LHS matrix.\n/// @param[in] k Number of columns in the unpacked LHS matrix.\n/// @param[in] mr Number of rows to be interleaved.\n/// @param[in] kr Number of columns to be interleaved.\n/// @param[in] sr Number of kr splits.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_lhs_packed_offset_lhs_pack_f16pmrx2_f32_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes of the packed LHS buffer.\n///\n/// @param[in] m Number of rows in the unpacked LHS matrix.\n/// @param[in] k Number of columns in the unpacked LHS matrix.\n/// @param[in] mr Number of rows to be interleaved.\n/// @param[in] kr Number of columns to be interleaved.\n/// @param[in] sr Number of kr splits.\n///\n/// @return The size in bytes of the packed LHS buffer.\nsize_t kai_get_lhs_packed_size_lhs_pack_f16pmrx2_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n\n/// Runs the LHS packing function for matrix multiplication.\n///\n/// The pointer of each buffers (LHS and packed LHS) needs to be added with offset\n/// calculated using the following functions:\n///\n/// * LHS: @ref kai_get_lhs_offset_lhs_pack_f16pmrx2_f32_neon.\n/// * Packed LHS: @ref kai_get_lhs_packed_offset_lhs_pack_f16pmrx2_f32_neon.\n///\n/// @param[in] m Number of rows of the unpacked LHS matrix.\n/// @param[in] k Common dimension between the LHS and RHS matrix.\n/// @param[in] bl The block length. unused\n/// @param[in] mr Block size in M dimension.\n/// @param[in] kr Block size in K dimension. It must be 4.\n/// @param[in] sr Number of kr splits. It must be 2\n/// @param[in] m_idx_start Unused.\n/// @param[in] lhs LHS matrix data buffer.\n/// @param[in] lhs_stride Row stride in bytes of the LHS matrix. Currently unused.\n/// @param[out] lhs_packed Packed LHS matrix.\nvoid kai_run_lhs_pack_f16pmrx2_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs,\n size_t lhs_stride, void* lhs_packed);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_pack_f16pmrx2_f32_neon.c",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n// mrx2 => this function can take in generic mr values but the input is expected to have a block depth of 2\n// Block depth is calculated as kr / sr. The values of these parameters are defined in the matmul ukernel.\n\n#if ((!defined(__aarch64__) || !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)) && !defined(_M_ARM64))\n#error This file must be compiled for AArch64\n#else // Architectural features check.\n\n#include \"kai_lhs_pack_f16pmrx2_f32_neon.h\"\n\n#include <arm_neon.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\ninline static size_t kai_num_bytes_per_block(size_t bl) {\n return bl * sizeof(uint16_t);\n}\n\ninline static size_t kai_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSERT((k % bl) == 0);\n return k / bl;\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t bl) {\n KAI_UNUSED(kr);\n\n return mr * kai_num_blocks_per_row(k, bl) * kai_num_bytes_per_block(bl);\n}\n\nsize_t kai_get_m_step_lhs_pack_f16pmrx2_f32_neon(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_pack_f16pmrx2_f32_neon(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_pack_f16pmrx2_f32_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((m_idx % mr) == 0);\n\n KAI_UNUSED(sr);\n\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, bl);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_pack_f16pmrx2_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n KAI_UNUSED(sr);\n\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return (num_rows * kai_lhs_packed_stride(k, mr, kr, bl));\n}\nvoid kai_run_lhs_pack_f16pmrx2_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs,\n size_t lhs_stride, void* lhs_packed) {\n KAI_ASSUME(sr == 2);\n\n KAI_UNUSED(bl);\n KAI_UNUSED(m_idx_start);\n KAI_UNUSED(lhs_stride);\n KAI_UNUSED(kr);\n\n // Pointer on a source matrix of the batch\n const float* lhs_ptr = lhs;\n\n // Pointer on a destination tranposed matrix of the batch\n uint16_t* lhs_packed_ptr = lhs_packed;\n\n for (uint64_t dst_row_idx = 0; dst_row_idx < m; dst_row_idx += mr) {\n // If rows left is more than mr, then calculate over mr as normal otherwise calculate over leftover rows\n uint64_t rows_in_mr = ((m - dst_row_idx) >= mr) ? mr : (m - dst_row_idx);\n for (uint64_t i = 0; (i < (rows_in_mr - (rows_in_mr % 4))); i += 4) {\n for (uint64_t j = 0; j < k; j += 16) {\n float32x4x4_t aa1 = vld4q_f32((lhs_ptr + i * k + j));\n float32x4x4_t aa2 = vld4q_f32((lhs_ptr + (i + 1) * k + j));\n float32x4x4_t aa3 = vld4q_f32((lhs_ptr + (i + 2) * k + j));\n float32x4x4_t aa4 = vld4q_f32((lhs_ptr + (i + 3) * k + j));\n\n float16x4_t a_1 = vcvt_f16_f32(aa1.val[0]);\n float16x4_t a_2 = vcvt_f16_f32(aa1.val[1]);\n float16x4_t a_3 = vcvt_f16_f32(aa2.val[0]);\n float16x4_t a_4 = vcvt_f16_f32(aa2.val[1]);\n float16x4x4_t a = {.val = {a_1, a_2, a_3, a_4}};\n a_1 = vcvt_f16_f32(aa1.val[2]);\n a_2 = vcvt_f16_f32(aa1.val[3]);\n a_3 = vcvt_f16_f32(aa2.val[2]);\n a_4 = vcvt_f16_f32(aa2.val[3]);\n float16x4x4_t a2 = {.val = {a_1, a_2, a_3, a_4}};\n a_1 = vcvt_f16_f32(aa3.val[0]);\n a_2 = vcvt_f16_f32(aa3.val[1]);\n a_3 = vcvt_f16_f32(aa4.val[0]);\n a_4 = vcvt_f16_f32(aa4.val[1]);\n float16x4x4_t a3 = {.val = {a_1, a_2, a_3, a_4}};\n a_1 = vcvt_f16_f32(aa3.val[2]);\n a_2 = vcvt_f16_f32(aa3.val[3]);\n a_3 = vcvt_f16_f32(aa4.val[2]);\n a_4 = vcvt_f16_f32(aa4.val[3]);\n float16x4x4_t a4 = {.val = {a_1, a_2, a_3, a_4}};\n\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + (j / 2) * mr * 2), a, 0);\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + (j / 2) * mr * 2 + 4), a3, 0);\n\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 1) * mr * 2), a2, 0);\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 1) * mr * 2 + 4), a4, 0);\n\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 2) * mr * 2), a, 1);\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 2) * mr * 2 + 4), a3, 1);\n\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 3) * mr * 2), a2, 1);\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 3) * mr * 2 + 4), a4, 1);\n\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 4) * mr * 2), a, 2);\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 4) * mr * 2 + 4), a3, 2);\n\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 5) * mr * 2), a2, 2);\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 5) * mr * 2 + 4), a4, 2);\n\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 6) * mr * 2), a, 3);\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 6) * mr * 2 + 4), a3, 3);\n\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 7) * mr * 2), a2, 3);\n vst4_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 7) * mr * 2 + 4), a4, 3);\n }\n }\n for (uint64_t i = (rows_in_mr - (rows_in_mr % 4)); i < rows_in_mr; i++) {\n for (uint64_t j = 0; j < k; j += 16) {\n float32x4x4_t aa = vld4q_f32((lhs_ptr + i * k + j));\n\n float16x4_t a_1 = vcvt_f16_f32(aa.val[0]);\n float16x4_t a_2 = vcvt_f16_f32(aa.val[1]);\n float16x4_t a_3 = vcvt_f16_f32(aa.val[2]);\n float16x4_t a_4 = vcvt_f16_f32(aa.val[3]);\n\n float16x4x2_t p0 = {.val = {a_1, a_2}};\n float16x4x2_t p1 = {.val = {a_3, a_4}};\n\n vst2_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 0) * mr * 2), p0, 0);\n vst2_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 1) * mr * 2), p1, 0);\n\n vst2_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 2) * mr * 2), p0, 1);\n vst2_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 3) * mr * 2), p1, 1);\n\n vst2_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 4) * mr * 2), p0, 2);\n vst2_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 5) * mr * 2), p1, 2);\n\n vst2_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 6) * mr * 2), p0, 3);\n vst2_lane_f16((void*)(lhs_packed_ptr + i * 2 + ((j / 2) + 7) * mr * 2), p1, 3);\n }\n }\n lhs_ptr += mr * k;\n lhs_packed_ptr += mr * k;\n }\n}\n#endif // Architectural features check.\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.c",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n\n#include \"kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.h\"\n\n#include <arm_neon.h>\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_multiplier = sizeof(uint16_t);\nstatic const size_t kai_max_bl = KAI_SME_VEC_LENGTH_MAX_BYTES;\nstatic const size_t kai_nr_tile = 4;\nstatic const size_t kai_bl_multiple_of = 32;\n\n#if defined(_MSC_VER)\n#define KAI_ALIGNED_AS(N) __declspec(align(N))\n#elif defined(__GNUC__) || defined(__clang__)\n#define KAI_ALIGNED_AS(N) __attribute__((aligned(N)))\n#else\n#define KAI_ALIGNED_AS(N)\n#endif\n\nstatic inline void convert_chunk32_s1s0_s16s0(uint8_t* dst_lo, uint8_t* dst_hi, const uint8_t* src_ptr) {\n // Two sequential 128-bit loads followed by unzip have better throughput than a structured LD2.\n const uint8x16_t v0 = vld1q_u8(src_ptr);\n const uint8x16_t v1 = vld1q_u8(src_ptr + 16);\n\n const uint8x16_t even_bytes = vuzp1q_u8(v0, v1);\n const uint8x16_t odd_bytes = vuzp2q_u8(v0, v1);\n\n const uint8x16_t lo_pairs = vsliq_n_u8(even_bytes, odd_bytes, 4);\n const uint8x16_t hi_pairs = vsriq_n_u8(odd_bytes, even_bytes, 4);\n\n vst1q_u8(dst_lo, lo_pairs);\n vst1q_u8(dst_hi, hi_pairs);\n}\n\nstatic inline void convert_tail16_s1s0_s16s0(uint8_t* dst_lo, uint8_t* dst_hi, const uint8_t* src_ptr) {\n const uint8x16_t v0 = vld1q_u8(src_ptr);\n const uint8x16_t even_dup = vuzp1q_u8(v0, v0);\n const uint8x16_t odd_dup = vuzp2q_u8(v0, v0);\n\n const uint8x16_t lo_pairs_dup = vsliq_n_u8(even_dup, odd_dup, 4);\n const uint8x16_t hi_pairs_dup = vsriq_n_u8(odd_dup, even_dup, 4);\n const uint8x16_t packed = vcombine_u8(vget_low_u8(lo_pairs_dup), vget_low_u8(hi_pairs_dup));\n\n KAI_ALIGNED_AS(16) uint8_t tmp[16];\n vst1q_u8(tmp, packed);\n\n for (size_t i = 0; i < 8; ++i) {\n dst_lo[i] = tmp[i];\n dst_hi[i] = tmp[8 + i];\n }\n}\n\nstatic inline void convert_s1s0_s16s0_scalar(uint16_t* dst_blk, const uint8_t* src_blk, size_t bl) {\n const size_t half = bl / 2;\n const size_t split = bl / 4;\n\n for (size_t bl4_idx = 0; bl4_idx < bl / 4; bl4_idx++) {\n const size_t byte_idx0 = bl4_idx * 2;\n const size_t byte_idx1 = byte_idx0 + 1;\n uint8_t packed0 = 0;\n uint8_t packed1 = 0;\n\n if (byte_idx0 < split) {\n // First half of byte0: pack low nibbles into sequential order.\n const size_t k0 = byte_idx0 * 2;\n packed0 = (src_blk[k0] & 0xF) | (src_blk[k0 + 1] << 4);\n } else {\n // Second half of byte0: pack high nibbles into sequential order.\n const size_t k0 = byte_idx0 * 2 - half;\n packed0 = (src_blk[k0] >> 4) | (src_blk[k0 + 1] & 0xF0);\n }\n // Repeated for byte1\n if (byte_idx1 < split) {\n const size_t k1 = byte_idx1 * 2;\n packed1 = (src_blk[k1] & 0xF) | (src_blk[k1 + 1] << 4);\n } else {\n const size_t k1 = byte_idx1 * 2 - half;\n packed1 = (src_blk[k1] >> 4) | (src_blk[k1 + 1] & 0xF0);\n }\n dst_blk[bl4_idx] = (uint16_t)packed0 | ((uint16_t)packed1 << 8);\n }\n}\n\nstatic inline void convert_s1s0_s16s0_neon(uint16_t* dst_blk, const uint8_t* src_blk, size_t bl) {\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n\n const size_t half = bl / 2;\n const size_t split = bl / 4;\n\n uint8_t* dst_bytes = (uint8_t*)dst_blk;\n uint8_t* dst_low = dst_bytes;\n uint8_t* dst_high = dst_bytes + split;\n\n // Special-case bl == 32 (half == 16) so that the whole block is\n // handled with a single 128-bit load and store.\n if (half == 16) {\n const uint8x16_t v0 = vld1q_u8(src_blk);\n const uint8x16_t even_dup = vuzp1q_u8(v0, v0);\n const uint8x16_t odd_dup = vuzp2q_u8(v0, v0);\n\n const uint8x16_t lo_pairs_dup = vsliq_n_u8(even_dup, odd_dup, 4);\n const uint8x16_t hi_pairs_dup = vsriq_n_u8(odd_dup, even_dup, 4);\n\n const uint8x16_t packed = vcombine_u8(vget_low_u8(lo_pairs_dup), vget_low_u8(hi_pairs_dup));\n vst1q_u8(dst_bytes, packed);\n return;\n }\n\n const uint8_t* src_ptr = src_blk;\n uint8_t* dst_low_ptr = dst_low;\n uint8_t* dst_high_ptr = dst_high;\n\n size_t remaining = half;\n\n // Process two 32-byte chunks per iteration when possible to reduce loop\n // overhead while keeping all loads and stores 128-bit wide.\n for (; remaining >= 64; remaining -= 64) {\n convert_chunk32_s1s0_s16s0(dst_low_ptr, dst_high_ptr, src_ptr);\n convert_chunk32_s1s0_s16s0(dst_low_ptr + 16, dst_high_ptr + 16, src_ptr + 32);\n\n src_ptr += 64;\n dst_low_ptr += 32;\n dst_high_ptr += 32;\n }\n for (; remaining >= 32; remaining -= 32) {\n convert_chunk32_s1s0_s16s0(dst_low_ptr, dst_high_ptr, src_ptr);\n\n src_ptr += 32;\n dst_low_ptr += 16;\n dst_high_ptr += 16;\n }\n\n // Handle the remaining 16 bytes (if any) using a single 128-bit load\n // and store to a temporary buffer, followed by scalar writes.\n if (remaining != 0) {\n convert_tail16_s1s0_s16s0(dst_low_ptr, dst_high_ptr, src_ptr);\n }\n}\n\nstatic inline void convert_s1s0_s16s0_neon_rows4(\n uint16_t* dst0, uint16_t* dst1, uint16_t* dst2, uint16_t* dst3, const uint8_t* src0, const uint8_t* src1,\n const uint8_t* src2, const uint8_t* src3, size_t bl) {\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n\n const size_t half = bl / 2;\n const size_t split = bl / 4;\n\n uint8_t* dst0_bytes = (uint8_t*)dst0;\n uint8_t* dst1_bytes = (uint8_t*)dst1;\n uint8_t* dst2_bytes = (uint8_t*)dst2;\n uint8_t* dst3_bytes = (uint8_t*)dst3;\n\n uint8_t* dst0_low = dst0_bytes;\n uint8_t* dst1_low = dst1_bytes;\n uint8_t* dst2_low = dst2_bytes;\n uint8_t* dst3_low = dst3_bytes;\n\n uint8_t* dst0_high = dst0_bytes + split;\n uint8_t* dst1_high = dst1_bytes + split;\n uint8_t* dst2_high = dst2_bytes + split;\n uint8_t* dst3_high = dst3_bytes + split;\n\n // bl == 32 (half == 16) is common and can be handled without a loop.\n if (half == 16) {\n const uint8x16_t r0 = vld1q_u8(src0);\n const uint8x16_t r1 = vld1q_u8(src1);\n const uint8x16_t r2 = vld1q_u8(src2);\n const uint8x16_t r3 = vld1q_u8(src3);\n\n const uint8x16_t r0_even = vuzp1q_u8(r0, r0);\n const uint8x16_t r0_odd = vuzp2q_u8(r0, r0);\n const uint8x16_t r1_even = vuzp1q_u8(r1, r1);\n const uint8x16_t r1_odd = vuzp2q_u8(r1, r1);\n const uint8x16_t r2_even = vuzp1q_u8(r2, r2);\n const uint8x16_t r2_odd = vuzp2q_u8(r2, r2);\n const uint8x16_t r3_even = vuzp1q_u8(r3, r3);\n const uint8x16_t r3_odd = vuzp2q_u8(r3, r3);\n\n const uint8x16_t r0_lo = vsliq_n_u8(r0_even, r0_odd, 4);\n const uint8x16_t r0_hi = vsriq_n_u8(r0_odd, r0_even, 4);\n const uint8x16_t r1_lo = vsliq_n_u8(r1_even, r1_odd, 4);\n const uint8x16_t r1_hi = vsriq_n_u8(r1_odd, r1_even, 4);\n const uint8x16_t r2_lo = vsliq_n_u8(r2_even, r2_odd, 4);\n const uint8x16_t r2_hi = vsriq_n_u8(r2_odd, r2_even, 4);\n const uint8x16_t r3_lo = vsliq_n_u8(r3_even, r3_odd, 4);\n const uint8x16_t r3_hi = vsriq_n_u8(r3_odd, r3_even, 4);\n\n const uint8x16_t r0_packed = vcombine_u8(vget_low_u8(r0_lo), vget_low_u8(r0_hi));\n const uint8x16_t r1_packed = vcombine_u8(vget_low_u8(r1_lo), vget_low_u8(r1_hi));\n const uint8x16_t r2_packed = vcombine_u8(vget_low_u8(r2_lo), vget_low_u8(r2_hi));\n const uint8x16_t r3_packed = vcombine_u8(vget_low_u8(r3_lo), vget_low_u8(r3_hi));\n\n vst1q_u8(dst0_bytes, r0_packed);\n vst1q_u8(dst1_bytes, r1_packed);\n vst1q_u8(dst2_bytes, r2_packed);\n vst1q_u8(dst3_bytes, r3_packed);\n return;\n }\n\n const uint8_t* src0_ptr = src0;\n const uint8_t* src1_ptr = src1;\n const uint8_t* src2_ptr = src2;\n const uint8_t* src3_ptr = src3;\n\n size_t remaining = half;\n\n for (; remaining >= 64; remaining -= 64) {\n convert_chunk32_s1s0_s16s0(dst0_low, dst0_high, src0_ptr);\n convert_chunk32_s1s0_s16s0(dst0_low + 16, dst0_high + 16, src0_ptr + 32);\n convert_chunk32_s1s0_s16s0(dst1_low, dst1_high, src1_ptr);\n convert_chunk32_s1s0_s16s0(dst1_low + 16, dst1_high + 16, src1_ptr + 32);\n convert_chunk32_s1s0_s16s0(dst2_low, dst2_high, src2_ptr);\n convert_chunk32_s1s0_s16s0(dst2_low + 16, dst2_high + 16, src2_ptr + 32);\n convert_chunk32_s1s0_s16s0(dst3_low, dst3_high, src3_ptr);\n convert_chunk32_s1s0_s16s0(dst3_low + 16, dst3_high + 16, src3_ptr + 32);\n\n src0_ptr += 64;\n src1_ptr += 64;\n src2_ptr += 64;\n src3_ptr += 64;\n\n dst0_low += 32;\n dst1_low += 32;\n dst2_low += 32;\n dst3_low += 32;\n\n dst0_high += 32;\n dst1_high += 32;\n dst2_high += 32;\n dst3_high += 32;\n }\n for (; remaining >= 32; remaining -= 32) {\n convert_chunk32_s1s0_s16s0(dst0_low, dst0_high, src0_ptr);\n convert_chunk32_s1s0_s16s0(dst1_low, dst1_high, src1_ptr);\n convert_chunk32_s1s0_s16s0(dst2_low, dst2_high, src2_ptr);\n convert_chunk32_s1s0_s16s0(dst3_low, dst3_high, src3_ptr);\n\n src0_ptr += 32;\n src1_ptr += 32;\n src2_ptr += 32;\n src3_ptr += 32;\n\n dst0_low += 16;\n dst1_low += 16;\n dst2_low += 16;\n dst3_low += 16;\n\n dst0_high += 16;\n dst1_high += 16;\n dst2_high += 16;\n dst3_high += 16;\n }\n\n if (remaining != 0) {\n convert_tail16_s1s0_s16s0(dst0_low, dst0_high, src0_ptr);\n convert_tail16_s1s0_s16s0(dst1_low, dst1_high, src1_ptr);\n convert_tail16_s1s0_s16s0(dst2_low, dst2_high, src2_ptr);\n convert_tail16_s1s0_s16s0(dst3_low, dst3_high, src3_ptr);\n }\n}\n\nstatic inline void interleave_rows4_u16(\n uint16_t* dst_row_base, size_t nr, const uint16_t* row0, const uint16_t* row1, const uint16_t* row2,\n const uint16_t* row3, size_t bl4) {\n uint16_t* dst_ptr = dst_row_base;\n\n const uint16_t* row0_ptr = row0;\n const uint16_t* row1_ptr = row1;\n const uint16_t* row2_ptr = row2;\n const uint16_t* row3_ptr = row3;\n\n size_t remaining = bl4;\n\n // Store two columns per iteration to reduce branch and address-generation\n // overhead while keeping all loads sequential.\n for (; remaining >= 2; remaining -= 2) {\n const uint64_t packed0 = (uint64_t)row0_ptr[0] | ((uint64_t)row1_ptr[0] << 16) | ((uint64_t)row2_ptr[0] << 32) |\n ((uint64_t)row3_ptr[0] << 48);\n const uint64_t packed1 = (uint64_t)row0_ptr[1] | ((uint64_t)row1_ptr[1] << 16) | ((uint64_t)row2_ptr[1] << 32) |\n ((uint64_t)row3_ptr[1] << 48);\n\n *((uint64_t*)dst_ptr) = packed0;\n *((uint64_t*)(dst_ptr + nr)) = packed1;\n\n row0_ptr += 2;\n row1_ptr += 2;\n row2_ptr += 2;\n row3_ptr += 2;\n dst_ptr += 2 * nr;\n }\n\n if (remaining != 0) {\n const uint64_t packed = (uint64_t)row0_ptr[0] | ((uint64_t)row1_ptr[0] << 16) | ((uint64_t)row2_ptr[0] << 32) |\n ((uint64_t)row3_ptr[0] << 48);\n *((uint64_t*)dst_ptr) = packed;\n }\n}\n\nstatic inline void store_row_strided_u16(\n uint16_t* dst_block_base, size_t row_idx, size_t nr, const uint16_t* src_vals, size_t bl4, size_t nr1, size_t nr2,\n size_t nr3, size_t dst_step) {\n const uint16_t* src_ptr = src_vals;\n uint16_t* dst_ptr = dst_block_base + row_idx;\n\n size_t remaining_bl4 = bl4;\n for (; remaining_bl4 >= 4; remaining_bl4 -= 4) {\n dst_ptr[0] = src_ptr[0];\n dst_ptr[nr1] = src_ptr[1];\n dst_ptr[nr2] = src_ptr[2];\n dst_ptr[nr3] = src_ptr[3];\n src_ptr += 4;\n dst_ptr += dst_step;\n }\n for (; remaining_bl4 != 0; --remaining_bl4) {\n *dst_ptr = *src_ptr;\n ++src_ptr;\n dst_ptr += nr;\n }\n}\n\ninline static size_t kai_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_num_bytes_per_block(size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n return (bl / 2) + kai_num_bytes_multiplier;\n}\n\ninline static size_t kai_rhs_stride(size_t k, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n const size_t num_blocks_per_row = kai_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_num_bytes_per_block(bl);\n\n return num_bytes_per_block * num_blocks_per_row;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n const size_t num_blocks_per_row = kai_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_num_bytes_per_block(bl);\n\n return nr * (num_bytes_per_block * num_blocks_per_row);\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((n_idx % nr) == 0);\n\n // The scales are stored after all the nr packed quantized values\n return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(k, nr, kr, bl);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(k, nr, kr, bl);\n}\n\nvoid kai_run_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t bl, const uint8_t* rhs,\n const float* bias, void* rhs_packed, size_t extra_bytes, const struct kai_rhs_pack_qs4cxs1s0_param* params) {\n KAI_ASSUME(num_groups == 1);\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME(bl <= kai_max_bl);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME(bias == NULL);\n KAI_ASSUME(extra_bytes == 0);\n\n KAI_ASSUME(kr == 4);\n KAI_ASSUME(sr == 2);\n KAI_ASSUME(kr >= 1 && kr <= 16);\n KAI_ASSUME(rhs != NULL);\n KAI_ASSUME(rhs_packed != NULL);\n KAI_ASSUME(params != NULL);\n KAI_ASSUME(params->rhs_zero_point == 8);\n KAI_ASSUME(params->lhs_zero_point == 1);\n\n // Note: The input matrix (rhs) is expected with:\n // \"k\" columns and \"n\" rows (NxK)\n\n const size_t num_blocks = k / bl;\n const size_t rhs_stride = kai_rhs_stride(k, bl);\n const size_t rhs_packed_stride =\n kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(k, nr, kr, bl);\n const size_t num_bytes_per_block = kai_num_bytes_per_block(bl);\n const size_t bl4 = bl / 4;\n const size_t block_stride_u16 = bl4 * nr;\n const size_t scales_offset = rhs_packed_stride - (nr * num_blocks * kai_num_bytes_multiplier);\n\n uint8_t* rhs_packed_ptr = rhs_packed;\n\n if (kr == 4 && sr == 2 && (nr % kai_nr_tile) == 0 && (bl % kai_bl_multiple_of) == 0) {\n // Reuse scratch buffers across all rows and blocks. Four buffers allow\n // row-block interleaving with wide stores across the contiguous row\n // dimension.\n KAI_ALIGNED_AS(16) uint16_t blk_s1s0_rows_0[KAI_SME_VEC_LENGTH_MAX_BYTES / 4] = {0};\n KAI_ALIGNED_AS(16) uint16_t blk_s1s0_rows_1[KAI_SME_VEC_LENGTH_MAX_BYTES / 4] = {0};\n KAI_ALIGNED_AS(16) uint16_t blk_s1s0_rows_2[KAI_SME_VEC_LENGTH_MAX_BYTES / 4] = {0};\n KAI_ALIGNED_AS(16) uint16_t blk_s1s0_rows_3[KAI_SME_VEC_LENGTH_MAX_BYTES / 4] = {0};\n\n const size_t nr1 = nr;\n const size_t nr2 = nr1 + nr;\n const size_t nr3 = nr2 + nr;\n const size_t dst_step = nr3 + nr;\n\n for (size_t group_start = 0; group_start < n; group_start += nr, rhs_packed_ptr += rhs_packed_stride) {\n const size_t rows_in_group = KAI_MIN(n - group_start, nr);\n const uint8_t* rhs_group_base = rhs + group_start * rhs_stride;\n\n uint16_t* group_base_u16 = (uint16_t*)rhs_packed_ptr;\n uint16_t* rhs_packed_scales = (uint16_t*)(rhs_packed_ptr + scales_offset);\n\n const uint8_t* src_block_base = rhs_group_base;\n uint16_t* dst_block_base = group_base_u16;\n uint16_t* scales_block = rhs_packed_scales;\n\n for (size_t block_idx = 0; block_idx < num_blocks; ++block_idx) {\n size_t row_idx = 0;\n const uint8_t* src_row_block = src_block_base;\n\n for (; row_idx + kai_nr_tile <= rows_in_group;\n row_idx += kai_nr_tile, src_row_block += kai_nr_tile * rhs_stride) {\n const uint8_t* restrict src_row0 = src_row_block;\n const uint8_t* restrict src_row1 = src_row0 + rhs_stride;\n const uint8_t* restrict src_row2 = src_row1 + rhs_stride;\n const uint8_t* restrict src_row3 = src_row2 + rhs_stride;\n\n const uint16_t scale0 = *((const uint16_t*)src_row0);\n const uint16_t scale1 = *((const uint16_t*)src_row1);\n const uint16_t scale2 = *((const uint16_t*)src_row2);\n const uint16_t scale3 = *((const uint16_t*)src_row3);\n\n const uint64_t packed_scales = (uint64_t)scale0 | ((uint64_t)scale1 << 16) |\n ((uint64_t)scale2 << 32) | ((uint64_t)scale3 << 48);\n *((uint64_t*)(scales_block + row_idx)) = packed_scales;\n\n convert_s1s0_s16s0_neon_rows4(\n blk_s1s0_rows_0, blk_s1s0_rows_1, blk_s1s0_rows_2, blk_s1s0_rows_3,\n src_row0 + kai_num_bytes_multiplier, src_row1 + kai_num_bytes_multiplier,\n src_row2 + kai_num_bytes_multiplier, src_row3 + kai_num_bytes_multiplier, bl);\n\n interleave_rows4_u16(\n dst_block_base + row_idx, nr, blk_s1s0_rows_0, blk_s1s0_rows_1, blk_s1s0_rows_2,\n blk_s1s0_rows_3, bl4);\n }\n\n for (; row_idx < rows_in_group; ++row_idx, src_row_block += rhs_stride) {\n const uint16_t* blk_scale_ptr = (const uint16_t*)src_row_block;\n const uint8_t* blk_s16s0 = src_row_block + kai_num_bytes_multiplier;\n\n scales_block[row_idx] = *blk_scale_ptr;\n\n convert_s1s0_s16s0_neon(blk_s1s0_rows_0, blk_s16s0, bl);\n store_row_strided_u16(dst_block_base, row_idx, nr, blk_s1s0_rows_0, bl4, nr1, nr2, nr3, dst_step);\n }\n\n src_block_base += num_bytes_per_block;\n dst_block_base += block_stride_u16;\n scales_block += nr;\n }\n }\n return;\n }\n\n for (uint64_t n_idx = 0; n_idx < n; n_idx++) {\n uint16_t* rhs_packed_scales =\n (uint16_t*)(rhs_packed_ptr + rhs_packed_stride - (nr * num_blocks * kai_num_bytes_multiplier));\n\n for (size_t block_idx = 0; block_idx < num_blocks; block_idx++) {\n uint16_t blk_s1s0[KAI_SME_VEC_LENGTH_MAX_BYTES / 4];\n\n const uint16_t* blk_scale_ptr =\n (const uint16_t*)(rhs + (block_idx * num_bytes_per_block) + n_idx * rhs_stride);\n const uint8_t* blk_s16s0 = (const uint8_t*)blk_scale_ptr + kai_num_bytes_multiplier;\n\n convert_s1s0_s16s0_scalar(blk_s1s0, blk_s16s0, bl);\n\n for (size_t bl4_idx = 0; bl4_idx < bl / 4; bl4_idx++) {\n // Uint16 holds 4 int4 values\n ((uint16_t*)rhs_packed_ptr)[(block_idx * bl / 4 + bl4_idx) * nr + (n_idx % nr)] = blk_s1s0[bl4_idx];\n }\n\n // Num. block (rows) x Nr (cols)\n rhs_packed_scales[(n_idx % nr) + block_idx * nr] = *blk_scale_ptr;\n }\n\n if (((n_idx + 1) % nr) == 0) {\n rhs_packed_ptr += rhs_packed_stride;\n }\n }\n}\n#endif // Architectural features check.\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the offset in bytes for the RHS matrix (not packed), which holds\n/// the int4 values in a N x K matrix, where N is number of rows and K is the number of columns.\n///\n/// Two int4 K values are stored in one byte. These values are stored in blocks, where each block\n/// has it own scale factor. The scale factor is expected to be a f16 value and stored at the beginning of each block.\n/// The first byte in the block holds the K-index + 0 and K-index + 16 values.\n/// The K-index + 0 value is stored in the lower order part of the byte (low nibble) while\n/// the K-index + 16 value is stored in the higher order part (high nibble).\n/// For example, if the block length is 32, the values are stored in the following order:\n/// |byte(s16, s0),byte(s17, s1),byte(s18, s2),...,byte(s31, s15),float16(scale)|\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n_idx, //\n size_t rhs_stride); //\n\n/// Gets the offset in bytes for the packed RHS matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K)\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n_idx, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl); //\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl);\n\n/// Gets the size in bytes for the quantized and packed RHS matrix.\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple\n/// of 32.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl); //\n\n/// Runs the RHS packing micro-kernel.\n///\n/// The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 values are stored in one byte. The lower order part of the byte (low) holds\n/// the first nibble (K-index + 0). The higher order of the byte holds the second nibble (K-index + 1).\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of columns of the output matrix (N).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] bl The block length, which defines the number of\n/// K values stored in a single block. It must be a multiple of 32.\n/// @param[in] rhs The RHS matrix containing the 4-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).\n/// @param[in] bias The biases.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n const uint8_t* rhs, //\n const float* bias, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_qs4cxs1s0_param* params); //\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "binding.cpp",
|
| 65 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa against gemm_f16act_qs4c32_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, int M)\n#include <arm_neon.h>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_pack_f16pmrx2_f32_neon.h\"\n#include \"kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr size_t BL = 32;\n// Interleave B_q4 [N, K/2] and the raw f16 B_scales [N, K/32] into the per-block [scale f16][16 data bytes]\n// layout kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon expects.\nvoid interleave_qs4c32(const uint8_t* b_q4, const uint16_t* b_scales, uint8_t* dst) {\n const size_t nb = K / BL, data = BL / 2;\n for (size_t n = 0; n < (size_t)N; ++n)\n for (size_t b = 0; b < nb; ++b) {\n std::memcpy(dst, &b_scales[n * nb + b], sizeof(uint16_t));\n dst += sizeof(uint16_t);\n std::memcpy(dst, b_q4 + (n * nb + b) * data, data);\n dst += data;\n }\n}\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n// Widen n raw fp16 patterns to fp32 (exact).\nvoid widen_f16(const uint16_t* src, float* dst, size_t n) {\n size_t i = 0;\n for (; i + 4 <= n; i += 4) vst1q_f32(dst + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(src + i))));\n for (; i < n; ++i) dst[i] = vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(src[i]))), 0);\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, int M)\n{\n // The SME2 mopa kernel takes any M in a single call, so there is no GEMV dispatch here.\n const size_t mr = kai_get_mr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n // The kernel's LHS packer reads fp32 (it narrows to fp16 itself), so the fp16 activation is widened first (timed).\n static float* a32 = nullptr;\n static size_t a32_cap = 0;\n if ((size_t)M * K > a32_cap) {\n std::free(a32);\n a32 = static_cast<float*>(std::malloc(sizeof(float) * (size_t)M * K));\n a32_cap = (size_t)M * K;\n if (a32 == nullptr) return -1;\n }\n widen_f16(A, a32, (size_t)M * K);\n const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_pack_f16pmrx2_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_pack_f16pmrx2_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr, 0, a32, (size_t)K * sizeof(float), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q4_snap = nullptr;\n static size_t B_q4_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q4_snap, B_q4_snap_size, B_q4, (size_t)N*K/2)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(uint16_t)*N*(K/BL));\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon((size_t)N, (size_t)K, nr, kr, BL);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n const size_t nb = K / BL;\n uint8_t* inter = static_cast<uint8_t*>(std::malloc((size_t)N * nb * (BL / 2 + sizeof(uint16_t))));\n if (inter == nullptr) return -1;\n interleave_qs4c32(B_q4, B_scales, inter);\n struct kai_rhs_pack_qs4cxs1s0_param params;\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 8;\n kai_run_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(1, (size_t)N, (size_t)K, nr, kr, sr, BL, inter, nullptr, rhs_packed, 0, ¶ms);\n std::free(inter);\n }\n\n kai_run_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa((size_t)M, (size_t)N, (size_t)K, BL, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n return 0;\n}\n"
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_f32_bf16_n512_k512.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_i32_u8_sme2_n256_k512.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qai4c32_n256_k512.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qai4c32_n256_k512",
|
| 3 |
+
"definition": "gemm_qai4c32_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qai4c32_n256_k512: KleidiAI kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"graviton4",
|
| 11 |
+
"aarch64-sve2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"dotprod",
|
| 17 |
+
"i8mm"
|
| 18 |
+
],
|
| 19 |
+
"compile_flags": [
|
| 20 |
+
"-O3",
|
| 21 |
+
"-march=armv8.2-a+dotprod+i8mm"
|
| 22 |
+
],
|
| 23 |
+
"link_flags": []
|
| 24 |
+
},
|
| 25 |
+
"sources": [
|
| 26 |
+
{
|
| 27 |
+
"path": "kai/kai_common.h",
|
| 28 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm.h",
|
| 32 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon to dynamically quantize and pack the LHS matrix in a single\n/// step.\n/// -# @ref kai_run_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon to pack the RHS NxK matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-block (multiple of 32) quantization (qsi8d32)\n/// values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of m_step.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t m_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Asymmetric Signed 4-bit with per-block (multiple of 32) quantization (qai4c32)\n/// values.\n///\n/// @param[in] n_idx Col index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t n_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of m_step.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of n_step.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride); //\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t m, //\n size_t n); //\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Symmetric Signed 8-bit with per-block (multiple of 32) quantization (qsi8d32) and packed.\n/// RHS matrix: Quantized Asymmetric Signed 4-bit with per-block (multiple of 32) quantization (qai4c32) and packed.\n/// Output tile: (rows x cols) = m_step x n_step.\n///\n/// Note: Please refer to the get functions for m_step and n_step for the exact values.\n///\n/// Features used: i8mm\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. Block length. It must be a multiple of 32.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the\n/// top of this file.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the\n/// top of this file.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes.\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n}\n#endif // __cplusplus\n"
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm.c",
|
| 36 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if !defined(__aarch64__) && !defined(__ARM_FEATURE_MATMUL_INT8) && !defined(_M_ARM64)\n#error \"I8mm extension required to compile this micro-kernel\"\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm.h\"\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\ntypedef struct {\n float* dst;\n const void* lhs_packed;\n const void* rhs_packed;\n const float* clamp_vals;\n size_t dst_stride_row;\n size_t m;\n size_t n;\n size_t num_blocks;\n size_t num_subblocks;\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(KernelArgs* args_ptr);\n\n// Compute args\nstatic const size_t kai_m_step = 8;\nstatic const size_t kai_n_step = 4;\n// Packing args\nstatic const size_t kai_mr = 4;\nstatic const size_t kai_nr = 4;\nstatic const size_t kai_kr = 16;\nstatic const size_t kai_sr = 2;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 4;\nstatic const size_t kai_num_bytes_sum_lhs = 4;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_num_bytes_recip_qvalue_rhs = 2;\nstatic const size_t kai_num_bytes_multiplier_rhs = 4;\nstatic const size_t kai_num_bytes_offset_rhs = 4;\n\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_num_bytes_bias = 4;\nstatic const size_t kai_bl = 32;\n\ninline static size_t kai_get_num_bytes_per_block_lhs(size_t bl) {\n return (bl * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs + kai_num_bytes_sum_lhs;\n}\n\ninline static size_t kai_get_num_bytes_per_block_rhs(size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n size_t num_bytes_per_block_rhs =\n (bl / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs + kai_num_bytes_offset_rhs;\n return num_bytes_per_block_rhs;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t bl) {\n return kai_mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block_lhs(bl);\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl);\n\n size_t rhs_packed_stride = kai_nr * (num_bytes_per_block * num_blocks_per_row);\n // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias\n rhs_packed_stride += kai_nr * kai_num_bytes_bias;\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void) {\n return kai_m_step;\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void) {\n return kai_n_step;\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void) {\n return kai_mr;\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void) {\n return kai_nr;\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t m_idx, size_t k, size_t bl) {\n KAI_ASSUME((m_idx % kai_mr) == 0);\n\n return (m_idx / kai_mr) * kai_get_lhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t n_idx, size_t k, size_t bl) {\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((n_idx % kai_nr) == 0);\n\n return (n_idx / kai_nr) * kai_get_rhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSUME((m_idx % kai_m_step) == 0);\n KAI_ASSUME((n_idx % kai_n_step) == 0);\n\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSUME(dst_stride_col == sizeof(float));\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl) == 0);\n\n if (m == 0) {\n return;\n }\n const size_t num_subblocks = bl / kai_bl;\n const size_t num_blocks = kai_get_num_blocks_per_row(k, bl);\n const float clamp_vals[2] = {scalar_min, scalar_max};\n\n KernelArgs args;\n\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.clamp_vals = clamp_vals;\n args.dst_stride_row = dst_stride_row;\n args.m = m;\n args.n = n;\n args.num_blocks = num_blocks;\n args.num_subblocks = num_subblocks;\n\n kai_kernel_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm_asm.S",
|
| 40 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm)\n stp x20, x21, [sp, -144]!\n stp x22, x23, [sp, 16]\n stp x24, x25, [sp, 32]\n stp x26, x27, [sp, 48]\n str x28, [sp, 64]\n stp d10, d11, [sp, 72]\n stp d12, d13, [sp, 88]\n stp d14, d15, [sp, 104]\n stp d8, d9, [sp, 120]\n mov x21, #0x20\n movi v12.16b, #0xf0\n mov x6, #0x80\n ldr x20, [x0, #0x28]\n ldr x7, [x0, #0x40]\n ldr x8, [x0, #0x38]\n ldr x17, [x0, #0x8]\n ldr x16, [x0, #0x10]\n ldr x15, [x0, #0x30]\n mov x14, x20\n madd x6, x7, x6, x21\n ldr x13, [x0, #0x0]\n ldr x12, [x0, #0x20]\n ldr x11, [x0, #0x18]\n cmp x14, #0x8\n mul x6, x8, x6\n blt label_11\nKAI_ASM_LABEL(label_1) // Row loop\n mov x10, x16\n mov x9, x15\n add x28, x13, x12, LSL #3\nKAI_ASM_LABEL(label_2) // Column loop\n mov x23, x17\n movi v13.16b, #0x0\n movi v18.16b, #0x0\n mov x22, x8\n movi v29.16b, #0x0\n movi v14.16b, #0x0\n movi v15.16b, #0x0\n movi v11.16b, #0x0\n movi v4.16b, #0x0\n movi v6.16b, #0x0\n add x21, x23, x6\nKAI_ASM_LABEL(label_3) // Block loop\n movi v25.4s, #0x0\n movi v7.4s, #0x0\n mov x20, x7\n movi v10.4s, #0x0\n movi v17.4s, #0x0\n movi v3.4s, #0x0\n movi v8.4s, #0x0\n movi v9.4s, #0x0\n movi v27.4s, #0x0\nKAI_ASM_LABEL(label_4) // Sub block loop\n ldr q20, [x10, #0x0]\n ldr q23, [x10, #0x10]\n subs x20, x20, #0x1\n ldr q31, [x23, #0x0]\n ldr q22, [x23, #0x10]\n ldr q19, [x21, #0x0]\n ldr q28, [x21, #0x10]\n ldr q5, [x10, #0x20]\n ldr q16, [x10, #0x30]\n shl v26.16b, v20.16b, #0x4\n shl v0.16b, v23.16b, #0x4\n ldr q1, [x23, #0x20]\n ldr q21, [x23, #0x30]\n and v20.16b, v20.16b, v12.16b\n and v23.16b, v23.16b, v12.16b\n ldr q30, [x21, #0x20]\n ldr q24, [x21, #0x30]\n add x10, x10, #0x40\n ldr q2, [x23, #0x40]\n KAI_ASM_INST(0x4e9aa7f9) // smmla v25.4s, v31.16b, v26.16b\n KAI_ASM_INST(0x4e80a7e7) // smmla v7.4s, v31.16b, v0.16b\n ldr q31, [x23, #0x50]\n KAI_ASM_INST(0x4e9aa6ca) // smmla v10.4s, v22.16b, v26.16b\n KAI_ASM_INST(0x4e80a6d1) // smmla v17.4s, v22.16b, v0.16b\n ldr q22, [x21, #0x40]\n KAI_ASM_INST(0x4e9aa663) // smmla v3.4s, v19.16b, v26.16b\n KAI_ASM_INST(0x4e80a668) // smmla v8.4s, v19.16b, v0.16b\n ldr q19, [x21, #0x50]\n KAI_ASM_INST(0x4e9aa789) // smmla v9.4s, v28.16b, v26.16b\n ldr q26, [x23, #0x60]\n KAI_ASM_INST(0x4e80a79b) // smmla v27.4s, v28.16b, v0.16b\n ldr q0, [x23, #0x70]\n shl v28.16b, v5.16b, #0x4\n and v5.16b, v5.16b, v12.16b\n add x23, x23, #0x80\n KAI_ASM_INST(0x4e9ca439) // smmla v25.4s, v1.16b, v28.16b\n KAI_ASM_INST(0x4e9ca6aa) // smmla v10.4s, v21.16b, v28.16b\n KAI_ASM_INST(0x4e9ca7c3) // smmla v3.4s, v30.16b, v28.16b\n KAI_ASM_INST(0x4e9ca709) // smmla v9.4s, v24.16b, v28.16b\n ldr q28, [x21, #0x60]\n KAI_ASM_INST(0x4e94a459) // smmla v25.4s, v2.16b, v20.16b\n KAI_ASM_INST(0x4e94a7ea) // smmla v10.4s, v31.16b, v20.16b\n KAI_ASM_INST(0x4e94a6c3) // smmla v3.4s, v22.16b, v20.16b\n KAI_ASM_INST(0x4e94a669) // smmla v9.4s, v19.16b, v20.16b\n ldr q20, [x21, #0x70]\n add x21, x21, #0x80\n KAI_ASM_INST(0x4e85a759) // smmla v25.4s, v26.16b, v5.16b\n KAI_ASM_INST(0x4e85a40a) // smmla v10.4s, v0.16b, v5.16b\n KAI_ASM_INST(0x4e85a783) // smmla v3.4s, v28.16b, v5.16b\n KAI_ASM_INST(0x4e85a689) // smmla v9.4s, v20.16b, v5.16b\n shl v5.16b, v16.16b, #0x4\n and v16.16b, v16.16b, v12.16b\n KAI_ASM_INST(0x4e85a427) // smmla v7.4s, v1.16b, v5.16b\n KAI_ASM_INST(0x4e85a6b1) // smmla v17.4s, v21.16b, v5.16b\n KAI_ASM_INST(0x4e85a7c8) // smmla v8.4s, v30.16b, v5.16b\n KAI_ASM_INST(0x4e85a71b) // smmla v27.4s, v24.16b, v5.16b\n KAI_ASM_INST(0x4e97a447) // smmla v7.4s, v2.16b, v23.16b\n KAI_ASM_INST(0x4e97a7f1) // smmla v17.4s, v31.16b, v23.16b\n KAI_ASM_INST(0x4e97a6c8) // smmla v8.4s, v22.16b, v23.16b\n KAI_ASM_INST(0x4e97a67b) // smmla v27.4s, v19.16b, v23.16b\n KAI_ASM_INST(0x4e90a747) // smmla v7.4s, v26.16b, v16.16b\n KAI_ASM_INST(0x4e90a411) // smmla v17.4s, v0.16b, v16.16b\n KAI_ASM_INST(0x4e90a788) // smmla v8.4s, v28.16b, v16.16b\n KAI_ASM_INST(0x4e90a69b) // smmla v27.4s, v20.16b, v16.16b\n bgt label_4\n ldr q30, [x10, #0x0]\n ld1 { v31.4s }, [x23]\n add x23, x23, #0x10\n uzp1 v23.2d, v25.2d, v7.2d\n ldr q26, [x10, #0x10]\n ldr q16, [x23, #0x0]\n uzp2 v24.2d, v25.2d, v7.2d\n uzp1 v21.2d, v10.2d, v17.2d\n uzp2 v20.2d, v10.2d, v17.2d\n add x10, x10, #0x20\n add x23, x23, #0x10\n fmla v13.4s, v30.4s, v31.s[0]\n fmla v18.4s, v30.4s, v31.s[1]\n fmla v29.4s, v30.4s, v31.s[2]\n scvtf v23.4s, v23.4s\n fmla v14.4s, v30.4s, v31.s[3]\n fmul v19.4s, v26.4s, v16.s[0]\n fmul v1.4s, v26.4s, v16.s[1]\n scvtf v24.4s, v24.4s\n fmul v22.4s, v26.4s, v16.s[2]\n scvtf v21.4s, v21.4s\n fmul v16.4s, v26.4s, v16.s[3]\n scvtf v20.4s, v20.4s\n fmla v13.4s, v23.4s, v19.4s\n fmla v18.4s, v24.4s, v1.4s\n fmla v29.4s, v21.4s, v22.4s\n fmla v14.4s, v20.4s, v16.4s\n ld1 { v7.4s }, [x21]\n add x21, x21, #0x10\n uzp1 v23.2d, v3.2d, v8.2d\n uzp2 v22.2d, v3.2d, v8.2d\n ldr q16, [x21, #0x0]\n uzp1 v21.2d, v9.2d, v27.2d\n uzp2 v20.2d, v9.2d, v27.2d\n add x21, x21, #0x10\n fmla v15.4s, v30.4s, v7.s[0]\n fmla v11.4s, v30.4s, v7.s[1]\n fmla v4.4s, v30.4s, v7.s[2]\n fmla v6.4s, v30.4s, v7.s[3]\n scvtf v23.4s, v23.4s\n fmul v19.4s, v26.4s, v16.s[0]\n fmul v8.4s, v26.4s, v16.s[1]\n scvtf v22.4s, v22.4s\n fmul v30.4s, v26.4s, v16.s[2]\n scvtf v21.4s, v21.4s\n fmul v16.4s, v26.4s, v16.s[3]\n scvtf v20.4s, v20.4s\n fmla v15.4s, v23.4s, v19.4s\n fmla v11.4s, v22.4s, v8.4s\n fmla v4.4s, v21.4s, v30.4s\n fmla v6.4s, v20.4s, v16.4s\n subs x22, x22, #0x1\n bgt label_3\n ldr q21, [x10, #0x0]\n ld1r { v7.4s }, [x11]\n add x20, x11, #0x4\n cmp x9, #0x4\n ld1r { v16.4s }, [x20]\n add x10, x10, #0x10\n fadd v13.4s, v13.4s, v21.4s\n fadd v18.4s, v18.4s, v21.4s\n fadd v29.4s, v29.4s, v21.4s\n fadd v14.4s, v14.4s, v21.4s\n fadd v15.4s, v15.4s, v21.4s\n fadd v11.4s, v11.4s, v21.4s\n fadd v4.4s, v4.4s, v21.4s\n fadd v6.4s, v6.4s, v21.4s\n fmax v13.4s, v13.4s, v7.4s\n fmax v18.4s, v18.4s, v7.4s\n fmax v29.4s, v29.4s, v7.4s\n fmax v14.4s, v14.4s, v7.4s\n fmax v15.4s, v15.4s, v7.4s\n fmax v11.4s, v11.4s, v7.4s\n fmax v4.4s, v4.4s, v7.4s\n fmax v6.4s, v6.4s, v7.4s\n fmin v13.4s, v13.4s, v16.4s\n fmin v18.4s, v18.4s, v16.4s\n fmin v29.4s, v29.4s, v16.4s\n fmin v14.4s, v14.4s, v16.4s\n fmin v15.4s, v15.4s, v16.4s\n fmin v11.4s, v11.4s, v16.4s\n fmin v4.4s, v4.4s, v16.4s\n fmin v6.4s, v6.4s, v16.4s\n blt label_7\n mov x20, x13\n str q13, [x20, #0x0]\n add x20, x20, x12\n str q18, [x20, #0x0]\n add x20, x20, x12\n str q29, [x20, #0x0]\n add x20, x20, x12\n str q14, [x20, #0x0]\n add x20, x20, x12\n str q15, [x20, #0x0]\n add x20, x20, x12\n str q11, [x20, #0x0]\n add x20, x20, x12\n str q4, [x20, #0x0]\n add x20, x20, x12\n str q6, [x20, #0x0]\n b label_10\nKAI_ASM_LABEL(label_7) // Partial output\n mov x27, x13\n add x26, x27, x12, LSL #2\n add x25, x26, x12, LSL #1\n add x24, x26, x12\n add x23, x25, x12\n add x22, x27, x12, LSL #1\n add x21, x27, x12\n add x20, x22, x12\n tbz x9, #1, label_8\n st1 { v6.d }[0], [x23], #0x8\n st1 { v4.d }[0], [x25], #0x8\n st1 { v11.d }[0], [x24], #0x8\n st1 { v15.d }[0], [x26], #0x8\n st1 { v14.d }[0], [x20], #0x8\n st1 { v29.d }[0], [x22], #0x8\n st1 { v18.d }[0], [x21], #0x8\n st1 { v13.d }[0], [x27], #0x8\n tbz x9, #0, label_9\n st1 { v6.s }[2], [x23]\n st1 { v4.s }[2], [x25]\n st1 { v11.s }[2], [x24]\n st1 { v15.s }[2], [x26]\n st1 { v14.s }[2], [x20]\n st1 { v29.s }[2], [x22]\n st1 { v18.s }[2], [x21]\n st1 { v13.s }[2], [x27]\n b label_9\nKAI_ASM_LABEL(label_8) // Output block 0: partial_1_0\n st1 { v6.s }[0], [x23]\n st1 { v4.s }[0], [x25]\n st1 { v11.s }[0], [x24]\n st1 { v15.s }[0], [x26]\n st1 { v14.s }[0], [x20]\n st1 { v29.s }[0], [x22]\n st1 { v18.s }[0], [x21]\n st1 { v13.s }[0], [x27]\nKAI_ASM_LABEL(label_9) // Output block 0: Done\nKAI_ASM_LABEL(label_10) // Output stage exit\n subs x9, x9, #0x4\n add x13, x13, #0x10\n bgt label_2\n mov x20, #0x2\n sub x14, x14, #0x8\n cmp x14, #0x8\n mov x13, x28\n madd x17, x20, x6, x17\n bge label_1\nKAI_ASM_LABEL(label_11) // Row loop skip\n cbz x14, label_21\nKAI_ASM_LABEL(label_12) // Row tail: Row loop\n mov x26, x16\n mov x25, x15\n add x24, x13, x12, LSL #2\nKAI_ASM_LABEL(label_13) // Row tail: Column loop\n movi v13.16b, #0x0\n movi v18.16b, #0x0\n mov x23, x17\n mov x21, x8\n movi v29.16b, #0x0\n movi v14.16b, #0x0\nKAI_ASM_LABEL(label_14) // Row tail: Block loop\n movi v25.4s, #0x0\n movi v7.4s, #0x0\n mov x20, x7\n movi v10.4s, #0x0\n movi v17.4s, #0x0\nKAI_ASM_LABEL(label_15) // Row tail: Sub block loop\n ldr q0, [x26, #0x0]\n ldr q31, [x26, #0x10]\n subs x20, x20, #0x1\n ldr q30, [x23, #0x0]\n ldr q28, [x23, #0x10]\n ldr q27, [x26, #0x20]\n ldr q26, [x26, #0x30]\n add x26, x26, #0x40\n ldr q6, [x23, #0x20]\n ldr q24, [x23, #0x30]\n shl v23.16b, v0.16b, #0x4\n shl v22.16b, v31.16b, #0x4\n ldr q21, [x23, #0x40]\n ldr q20, [x23, #0x50]\n and v0.16b, v0.16b, v12.16b\n and v31.16b, v31.16b, v12.16b\n ldr q19, [x23, #0x60]\n ldr q2, [x23, #0x70]\n shl v11.16b, v27.16b, #0x4\n shl v16.16b, v26.16b, #0x4\n KAI_ASM_INST(0x4e97a7d9) // smmla v25.4s, v30.16b, v23.16b\n KAI_ASM_INST(0x4e96a7c7) // smmla v7.4s, v30.16b, v22.16b\n and v27.16b, v27.16b, v12.16b\n add x23, x23, #0x80\n KAI_ASM_INST(0x4e97a78a) // smmla v10.4s, v28.16b, v23.16b\n KAI_ASM_INST(0x4e96a791) // smmla v17.4s, v28.16b, v22.16b\n and v26.16b, v26.16b, v12.16b\n KAI_ASM_INST(0x4e8ba4d9) // smmla v25.4s, v6.16b, v11.16b\n KAI_ASM_INST(0x4e90a4c7) // smmla v7.4s, v6.16b, v16.16b\n KAI_ASM_INST(0x4e8ba70a) // smmla v10.4s, v24.16b, v11.16b\n KAI_ASM_INST(0x4e90a711) // smmla v17.4s, v24.16b, v16.16b\n KAI_ASM_INST(0x4e80a6b9) // smmla v25.4s, v21.16b, v0.16b\n KAI_ASM_INST(0x4e9fa6a7) // smmla v7.4s, v21.16b, v31.16b\n KAI_ASM_INST(0x4e80a68a) // smmla v10.4s, v20.16b, v0.16b\n KAI_ASM_INST(0x4e9fa691) // smmla v17.4s, v20.16b, v31.16b\n KAI_ASM_INST(0x4e9ba679) // smmla v25.4s, v19.16b, v27.16b\n KAI_ASM_INST(0x4e9aa667) // smmla v7.4s, v19.16b, v26.16b\n KAI_ASM_INST(0x4e9ba44a) // smmla v10.4s, v2.16b, v27.16b\n KAI_ASM_INST(0x4e9aa451) // smmla v17.4s, v2.16b, v26.16b\n bgt label_15\n ldr q6, [x26, #0x0]\n ld1 { v3.4s }, [x23]\n add x23, x23, #0x10\n uzp1 v24.2d, v25.2d, v7.2d\n ldr q23, [x26, #0x10]\n ldr q16, [x23, #0x0]\n uzp2 v22.2d, v25.2d, v7.2d\n uzp1 v21.2d, v10.2d, v17.2d\n uzp2 v20.2d, v10.2d, v17.2d\n add x26, x26, #0x20\n add x23, x23, #0x10\n fmla v13.4s, v6.4s, v3.s[0]\n fmla v18.4s, v6.4s, v3.s[1]\n fmla v29.4s, v6.4s, v3.s[2]\n scvtf v24.4s, v24.4s\n fmla v14.4s, v6.4s, v3.s[3]\n fmul v19.4s, v23.4s, v16.s[0]\n fmul v31.4s, v23.4s, v16.s[1]\n scvtf v22.4s, v22.4s\n fmul v17.4s, v23.4s, v16.s[2]\n scvtf v21.4s, v21.4s\n fmul v16.4s, v23.4s, v16.s[3]\n scvtf v20.4s, v20.4s\n fmla v13.4s, v24.4s, v19.4s\n fmla v18.4s, v22.4s, v31.4s\n fmla v29.4s, v21.4s, v17.4s\n fmla v14.4s, v20.4s, v16.4s\n subs x21, x21, #0x1\n bgt label_14\n ldr q20, [x26, #0x0]\n ld1r { v17.4s }, [x11]\n add x20, x11, #0x4\n cmp x25, #0x4\n ld1r { v16.4s }, [x20]\n add x26, x26, #0x10\n fadd v13.4s, v13.4s, v20.4s\n fadd v18.4s, v18.4s, v20.4s\n fadd v29.4s, v29.4s, v20.4s\n fadd v14.4s, v14.4s, v20.4s\n fmax v13.4s, v13.4s, v17.4s\n fmax v18.4s, v18.4s, v17.4s\n fmax v29.4s, v29.4s, v17.4s\n fmax v14.4s, v14.4s, v17.4s\n fmin v13.4s, v13.4s, v16.4s\n fmin v18.4s, v18.4s, v16.4s\n fmin v29.4s, v29.4s, v16.4s\n fmin v14.4s, v14.4s, v16.4s\n blt label_17\n mov x20, x13\n cmp x14, #0x1\n str q13, [x20, #0x0]\n add x20, x20, x12\n ble label_20\n cmp x14, #0x2\n str q18, [x20, #0x0]\n add x20, x20, x12\n ble label_20\n cmp x14, #0x3\n str q29, [x20, #0x0]\n add x20, x20, x12\n ble label_20\n str q14, [x20, #0x0]\n b label_20\nKAI_ASM_LABEL(label_17) // Row tail: Partial output\n mov x23, x13\n cmp x14, #0x1\n add x22, x23, x12\n csel x22, x22, x23, GT\n cmp x14, #0x2\n add x21, x23, x12, LSL #1\n csel x21, x21, x22, GT\n cmp x14, #0x3\n add x20, x21, x12\n csel x20, x20, x21, GT\n tbz x25, #1, label_18\n st1 { v14.d }[0], [x20], #0x8\n st1 { v29.d }[0], [x21], #0x8\n st1 { v18.d }[0], [x22], #0x8\n st1 { v13.d }[0], [x23], #0x8\n tbz x25, #0, label_19\n st1 { v14.s }[2], [x20]\n st1 { v29.s }[2], [x21]\n st1 { v18.s }[2], [x22]\n st1 { v13.s }[2], [x23]\n b label_19\nKAI_ASM_LABEL(label_18) // Row tail: Output block 0: partial_1_0\n st1 { v14.s }[0], [x20]\n st1 { v29.s }[0], [x21]\n st1 { v18.s }[0], [x22]\n st1 { v13.s }[0], [x23]\nKAI_ASM_LABEL(label_19) // Row tail: Output block 0: Done\nKAI_ASM_LABEL(label_20) // Row tail: Output stage exit\n subs x25, x25, #0x4\n add x13, x13, #0x10\n bgt label_13\n subs x14, x14, #0x4\n add x17, x17, x6\n mov x13, x24\n bgt label_12\nKAI_ASM_LABEL(label_21) // Row tail: Row loop skip\n ldp x22, x23, [sp, 16]\n ldp x24, x25, [sp, 32]\n ldp x26, x27, [sp, 48]\n ldr x28, [sp, 64]\n ldp d10, d11, [sp, 72]\n ldp d12, d13, [sp, 88]\n ldp d14, d15, [sp, 104]\n ldp d8, d9, [sp, 120]\n ldp x20, x21, [sp], 144\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm)\n\n KAI_ASM_END\n"
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod.h",
|
| 44 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon to dynamically quantize and pack the LHS matrix in a single\n/// step.\n/// -# @ref kai_run_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon to pack the RHS NxK matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-block (multiple of 32) quantization (qsi8d32)\n/// values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be 1.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t m_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Asymmetric Signed 4-bit with per-block (multiple of 32) quantization (qai4c32)\n/// values.\n///\n/// @param[in] n_idx Col index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t n_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be 1.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of n_step.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride); //\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t m, //\n size_t n); //\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Symmetric Signed 8-bit with per-block (multiple of 32) quantization (qsi8d32) and packed.\n/// RHS matrix: Quantized Asymmetric Signed 4-bit with per-block (multiple of 32) quantization (qai4c32) and packed.\n/// Output tile: (rows x cols) = m_step x n_step.\n///\n/// Note: Please refer to the get functions for m_step and n_step for the exact values.\n///\n/// Features used: dotprod\n///\n/// @param[in] m The number of output rows written. It must be 1.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. Block length. It must be a multiple of 32.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the\n/// top of this file.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the\n/// top of this file.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes.\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n}\n#endif // __cplusplus\n"
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod.c",
|
| 48 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if !defined(__aarch64__) && !defined(__ARM_FEATURE_DOTPROD) && !defined(_M_ARM64)\n#error \"Dotprod extension required to compile this micro-kernel\"\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod.h\"\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\ntypedef struct {\n float* dst;\n const void* lhs_packed;\n const void* rhs_packed;\n const float* clamp_vals;\n size_t dst_stride_row;\n size_t m;\n size_t n;\n size_t num_blocks;\n size_t num_subblocks;\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(KernelArgs* args_ptr);\n\n// Compute args\nstatic const size_t kai_m_step = 1;\nstatic const size_t kai_n_step = 4;\n// Packing args\nstatic const size_t kai_mr = 1;\nstatic const size_t kai_nr = 4;\nstatic const size_t kai_kr = 16;\nstatic const size_t kai_sr = 2;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 4;\nstatic const size_t kai_num_bytes_sum_lhs = 4;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_num_bytes_recip_qvalue_rhs = 2;\nstatic const size_t kai_num_bytes_multiplier_rhs = 4;\nstatic const size_t kai_num_bytes_offset_rhs = 4;\n\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_num_bytes_bias = 4;\nstatic const size_t kai_bl = 32;\n\ninline static size_t kai_get_num_bytes_per_block_lhs(size_t bl) {\n return (bl * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs + kai_num_bytes_sum_lhs;\n}\n\ninline static size_t kai_get_num_bytes_per_block_rhs(size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n size_t num_bytes_per_block_rhs =\n (bl / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs + kai_num_bytes_offset_rhs;\n return num_bytes_per_block_rhs;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t bl) {\n return kai_mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block_lhs(bl);\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl);\n\n size_t rhs_packed_stride = kai_nr * (num_bytes_per_block * num_blocks_per_row);\n // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias\n rhs_packed_stride += kai_nr * kai_num_bytes_bias;\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void) {\n return kai_m_step;\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void) {\n return kai_n_step;\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void) {\n return kai_mr;\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void) {\n return kai_nr;\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t m_idx, size_t k, size_t bl) {\n KAI_ASSUME((m_idx % kai_mr) == 0);\n\n return (m_idx / kai_mr) * kai_get_lhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t n_idx, size_t k, size_t bl) {\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((n_idx % kai_nr) == 0);\n\n return (n_idx / kai_nr) * kai_get_rhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSUME((m_idx % kai_m_step) == 0);\n KAI_ASSUME((n_idx % kai_n_step) == 0);\n\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSUME(dst_stride_col == sizeof(float));\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl) == 0);\n KAI_ASSUME(m == 1);\n\n if (m == 0) {\n return;\n }\n const size_t num_subblocks = bl / kai_bl;\n const size_t num_blocks = kai_get_num_blocks_per_row(k, bl);\n const float clamp_vals[2] = {scalar_min, scalar_max};\n\n KernelArgs args;\n\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.clamp_vals = clamp_vals;\n args.dst_stride_row = dst_stride_row;\n args.m = m;\n args.n = n;\n args.num_blocks = num_blocks;\n args.num_subblocks = num_subblocks;\n\n kai_kernel_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod_asm.S",
|
| 52 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod)\n stp x20, x21, [sp, -80]!\n stp x22, x23, [sp, 16]\n stp x24, x25, [sp, 32]\n stp x26, x27, [sp, 48]\n str x28, [sp, 64]\n mov x21, #0x8\n movi v31.16b, #0xf0\n mov x15, #0x20\n ldr x14, [x0, #0x40]\n ldr x13, [x0, #0x38]\n ldr x20, [x0, #0x28]\n ldr x12, [x0, #0x8]\n ldr x11, [x0, #0x10]\n ldr x10, [x0, #0x30]\n madd x15, x14, x15, x21\n ldr x9, [x0, #0x0]\n ldr x28, [x0, #0x20]\n ldr x27, [x0, #0x18]\n mov x26, x20\n mul x15, x13, x15\nKAI_ASM_LABEL(label_1) // Row loop\n mov x25, x11\n mov x24, x10\n add x23, x9, x28\nKAI_ASM_LABEL(label_2) // Column loop\n mov x22, x12\n movi v30.16b, #0x0\n mov x21, x13\nKAI_ASM_LABEL(label_3) // Block loop\n movi v29.4s, #0x0\n movi v28.4s, #0x0\n mov x20, x14\nKAI_ASM_LABEL(label_4) // Sub block loop\n ldr q27, [x25, #0x0]\n ldr q26, [x25, #0x10]\n subs x20, x20, #0x1\n ld1r { v25.2d }, [x22], #0x8\n ldr q24, [x25, #0x20]\n ldr q23, [x25, #0x30]\n add x25, x25, #0x40\n ld1r { v22.2d }, [x22], #0x8\n ld1r { v21.2d }, [x22], #0x8\n shl v20.16b, v27.16b, #0x4\n shl v19.16b, v26.16b, #0x4\n ld1r { v18.2d }, [x22], #0x8\n shl v17.16b, v24.16b, #0x4\n and v27.16b, v27.16b, v31.16b\n shl v16.16b, v23.16b, #0x4\n and v26.16b, v26.16b, v31.16b\n KAI_ASM_INST(0x4e99969d) // sdot v29.4s, v20.16b, v25.16b\n KAI_ASM_INST(0x4e99967c) // sdot v28.4s, v19.16b, v25.16b\n and v24.16b, v24.16b, v31.16b\n and v23.16b, v23.16b, v31.16b\n KAI_ASM_INST(0x4e96963d) // sdot v29.4s, v17.16b, v22.16b\n KAI_ASM_INST(0x4e96961c) // sdot v28.4s, v16.16b, v22.16b\n KAI_ASM_INST(0x4e95977d) // sdot v29.4s, v27.16b, v21.16b\n KAI_ASM_INST(0x4e95975c) // sdot v28.4s, v26.16b, v21.16b\n KAI_ASM_INST(0x4e92971d) // sdot v29.4s, v24.16b, v18.16b\n KAI_ASM_INST(0x4e9296fc) // sdot v28.4s, v23.16b, v18.16b\n bgt label_4\n ldr q19, [x25, #0x0]\n ld1r { v18.4s }, [x22]\n add x22, x22, #0x4\n addp v29.4s, v29.4s, v28.4s\n ld1r { v17.4s }, [x22]\n ldr q16, [x25, #0x10]\n sub x21, x21, #0x1\n add x22, x22, #0x4\n add x25, x25, #0x20\n fmla v30.4s, v19.4s, v18.s[0]\n scvtf v29.4s, v29.4s\n fmul v16.4s, v16.4s, v17.4s\n fmla v30.4s, v29.4s, v16.4s\n cbnz x21, label_3\n ldr q18, [x25, #0x0]\n ld1r { v17.4s }, [x27]\n add x20, x27, #0x4\n cmp x24, #0x4\n ld1r { v16.4s }, [x20]\n add x25, x25, #0x10\n fadd v30.4s, v30.4s, v18.4s\n fmax v30.4s, v30.4s, v17.4s\n fmin v30.4s, v30.4s, v16.4s\n blt label_5\n str q30, [x9, #0x0]\n b label_8\nKAI_ASM_LABEL(label_5) // Partial output\n mov x20, x9\n tbz x24, #1, label_6\n st1 { v30.d }[0], [x20], #0x8\n tbz x24, #0, label_7\n st1 { v30.s }[2], [x20]\n b label_7\nKAI_ASM_LABEL(label_6) // Output block 0: partial_1_0\n st1 { v30.s }[0], [x20]\nKAI_ASM_LABEL(label_7) // Output block 0: Done\nKAI_ASM_LABEL(label_8) // Stores done\n subs x24, x24, #0x4\n add x9, x9, #0x10\n bgt label_2\n subs x26, x26, #0x1\n add x12, x12, x15\n mov x9, x23\n bgt label_1\n ldp x22, x23, [sp, 16]\n ldp x24, x25, [sp, 32]\n ldp x26, x27, [sp, 48]\n ldr x28, [sp, 64]\n ldp x20, x21, [sp], 80\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod)\n\n KAI_ASM_END\n"
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"path": "kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.h",
|
| 56 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qsi8d32pscalef32_f32_neon(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized symmetric per-block (qsi8d32) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of mr.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl The block length.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be\n/// a multiple of 32.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs,\n size_t lhs_stride, void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"path": "kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.c",
|
| 60 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n\n#include \"kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.h\"\n\n#include <arm_neon.h>\n#include <float.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_sum = sizeof(float);\nstatic const size_t kai_num_bytes_multiplier = sizeof(float);\nstatic const size_t kai_bl_multiple_of = 32;\n\ninline static size_t kai_get_num_bytes_per_block(size_t bl) {\n return bl * sizeof(int8_t) + kai_num_bytes_multiplier + kai_num_bytes_sum;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSERT((k % bl) == 0);\n return k / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t bl) {\n KAI_UNUSED(kr);\n\n return mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block(bl);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qsi8d32pscalef32_f32_neon(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((m_idx % mr) == 0);\n\n KAI_UNUSED(sr);\n return (m_idx / mr) * kai_get_lhs_packed_stride(k, mr, kr, bl);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n KAI_UNUSED(sr);\n\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return (num_rows * kai_get_lhs_packed_stride(k, mr, kr, bl));\n}\nvoid kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs,\n size_t lhs_stride, void* lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSUME((bl % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n KAI_ASSERT(((kr / sr) == 8) || ((kr / sr) == 4));\n\n if (m == 0) {\n return;\n }\n const size_t lhs_packed_stride = kai_get_lhs_packed_stride(k, mr, kr, bl);\n const size_t num_rows = m;\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block(bl);\n const size_t mr_block_size = mr * num_bytes_per_block;\n\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {\n const float* row_src_ptr = (const float*)((const uint8_t*)lhs + (row_idx + m_idx_start) * lhs_stride);\n const size_t dst_idx = ((row_idx + m_idx_start) % mr);\n\n for (size_t blk_idx = 0; blk_idx < num_blocks_per_row; ++blk_idx) {\n const float* src_ptr = row_src_ptr + blk_idx * bl;\n int8_t* dst_ptr = (int8_t*)lhs_packed + dst_idx * k_block_len * sizeof(int8_t) + blk_idx * mr_block_size;\n int8_t* param_ptr = (int8_t*)lhs_packed + blk_idx * mr_block_size + bl * mr + dst_idx * kai_num_bytes_sum;\n\n // Find absmax for each block\n float absmax = -FLT_MAX;\n int32_t k_idx = 0;\n float32x4_t vabsmax = vdupq_n_f32(-FLT_MAX);\n for (; k_idx < ((int32_t)bl); k_idx += 8) {\n const float32x4_t src0_0 = vld1q_f32(src_ptr + 0 + (size_t)k_idx);\n const float32x4_t src0_1 = vld1q_f32(src_ptr + 4 + (size_t)k_idx);\n // Calculate the max\n vabsmax = vmaxq_f32(vabsq_f32(src0_0), vmaxq_f32(vabsmax, vabsq_f32(src0_1)));\n }\n // Get the absmax\n absmax = vmaxvq_f32(vabsmax);\n\n // Maximum/minimum int8 values\n const float qmax = (float)INT8_MAX;\n\n // Get the scale and reciprocal to quantize\n const float scale0 = absmax == 0.0F ? 0.0F : qmax / absmax;\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n int32_t qsum = 0;\n // Quantize the blocks\n for (k_idx = 0; k_idx <= (int32_t)bl - k_block_len; k_idx += k_block_len) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)k_idx, k - 1);\n if (k_block_len == 8) {\n const float32x4_t vsrc_0 = vld1q_f32(src_ptr + k_idx_start);\n const float32x4_t vsrc_1 = vld1q_f32(src_ptr + k_idx_start + 4);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(vsrc_0, scale0);\n float32x4_t v1_f32 = vmulq_n_f32(vsrc_1, scale0);\n\n int16x4_t v0_s16 = vqmovn_s32(vcvtnq_s32_f32(v0_f32));\n int16x4_t v1_s16 = vqmovn_s32(vcvtnq_s32_f32(v1_f32));\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n // Update the sum\n qsum += vaddvq_s16(v_s16);\n\n int8x8_t v0_s8 = vqmovn_s16(v_s16);\n vst1_s8(dst_ptr, v0_s8);\n dst_ptr += 8 * sizeof(int8_t);\n } else if (k_block_len == 4) {\n const float32x2_t vsrc_0 = vld1_f32(src_ptr + k_idx_start);\n const float32x2_t vsrc_1 = vld1_f32(src_ptr + k_idx_start + 2);\n\n // Scale the values\n float32x2_t v0_f32 = vmul_n_f32(vsrc_0, scale0);\n float32x2_t v1_f32 = vmul_n_f32(vsrc_1, scale0);\n\n int32x2_t v0_s32 = vcvtn_s32_f32(v0_f32);\n int32x2_t v1_s32 = vcvtn_s32_f32(v1_f32);\n int16x4_t v_s16 = vqmovn_s32(vcombine_s32(v0_s32, v1_s32));\n\n v_s16 = vmax_s16(v_s16, vdup_n_s16(INT8_MIN));\n v_s16 = vmin_s16(v_s16, vdup_n_s16(INT8_MAX));\n\n // Update the sum\n qsum += vaddv_s16(v_s16);\n\n dst_ptr[0] = vqmovnh_s16(vget_lane_s16(v_s16, 0));\n dst_ptr[1] = vqmovnh_s16(vget_lane_s16(v_s16, 1));\n dst_ptr[2] = vqmovnh_s16(vget_lane_s16(v_s16, 2));\n dst_ptr[3] = vqmovnh_s16(vget_lane_s16(v_s16, 3));\n dst_ptr += 4 * sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n *((float*)(param_ptr)) = ((float)qsum) * recip_scale0;\n param_ptr += mr * kai_num_bytes_sum;\n *((float*)(param_ptr)) = recip_scale0;\n }\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((int8_t*)lhs_packed + lhs_packed_stride);\n }\n }\n}\n#endif // Architectural features check.\n"
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"path": "kai_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon.h",
|
| 64 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef kai_rhs_pack_nxk_qai4c32p_params\n#define kai_rhs_pack_nxk_qai4c32p_params kai_rhs_pack_qs4cxs1s0_param\n#endif\n\n/// Gets the offset in bytes for the RHS matrix (not packed), which holds\n/// the int4 values in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 K values are stored in one byte. These values are stored in blocks\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(\n size_t n_idx, //\n size_t rhs_stride); //\n\n/// Gets the offset in bytes for the packed RHS matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K)\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(\n size_t n_idx, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl //\n);\n\n/// Gets the size in bytes for the quantized and packed RHS matrix.\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple\n/// of 32.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl //\n);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 values are stored in one byte.\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of columns of the output matrix (N).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// @param[in] nr The number of N rows to interleave on the same output row.\n/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] bl The block length, which defines the number of\n/// K values stored in a single block. It must be a multiple of 32.\n/// @param[in] rhs The RHS matrix containing the 4-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).\n/// @param[in] zero The zero point.\n/// @param[in] bias The biases.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n const uint8_t* rhs, //\n const void* zero, //\n const void* bias, //\n const void* scale, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_nxk_qai4c32p_params* params);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"path": "kai_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon.c",
|
| 68 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n#include \"kai_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon.h\"\n\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_offset_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\nstatic const size_t kai_bl_multiple_of = 32;\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_get_num_bytes_per_block(size_t bl) {\n return (bl / 2) + kai_num_bytes_multiplier_rhs + kai_num_bytes_offset_rhs;\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kr) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block(bl);\n return nr * (num_bytes_per_block * num_blocks_per_row + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((n_idx % nr) == 0);\n KAI_UNUSED(kr);\n return (n_idx / nr) * kai_get_rhs_packed_stride(k, nr, kr, bl);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_UNUSED(kr);\n const size_t num_rows = kai_roundup(n, nr) / nr;\n return num_rows * kai_get_rhs_packed_stride(k, nr, kr, bl);\n}\n\nvoid kai_run_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t bl, const uint8_t* rhs,\n const void* zero, const void* bias, const void* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qai4c32p_params* params) {\n KAI_ASSUME(num_groups == 1);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME(extra_bytes == 0);\n\n KAI_ASSUME(sr == 2);\n KAI_ASSUME(kr >= 1 && kr <= 16);\n KAI_ASSUME(rhs != NULL);\n KAI_ASSUME(zero != NULL);\n KAI_ASSUME(rhs_packed != NULL);\n KAI_ASSUME(params != NULL);\n KAI_ASSUME(params->rhs_zero_point == 8);\n KAI_ASSUME(params->lhs_zero_point == 1);\n\n // Note: The input matrix (rhs) is expected with:\n // \"k\" columns and \"n\" rows (NxK)\n\n const size_t num_blocks_per_row = k / bl;\n const size_t rhs_stride = k;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride(k, nr, kr, bl);\n\n const size_t dst_packed_block_size = kai_get_num_bytes_per_block(bl) * nr;\n const size_t dst_block_data_size = (bl / 2) * nr;\n const size_t dst_num_rows = kai_roundup(n, nr) / nr;\n const size_t dst_bias_offset = num_blocks_per_row * dst_packed_block_size;\n const size_t k_block_length_in_bytes = kr / sr;\n const size_t k_interleaved_v = 16U;\n\n const size_t rhs_zero_point = params->rhs_zero_point;\n\n for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; ++dst_row_idx) {\n uint8_t* dst_row = (uint8_t*)rhs_packed + dst_row_idx * rhs_packed_stride;\n float* dst_row_bias = (float*)(dst_row + dst_bias_offset);\n\n for (size_t block_idx = 0; block_idx < num_blocks_per_row; block_idx++) {\n uint8_t* block_dst_row = dst_row + block_idx * dst_packed_block_size;\n float* block_dst_zp = (float*)(block_dst_row + dst_block_data_size);\n float* block_dst_scale = block_dst_zp + nr;\n\n for (size_t block_byte_idx = 0; block_byte_idx < dst_block_data_size; ++block_byte_idx) {\n const size_t dst_byte_idx = block_byte_idx;\n const size_t k_block_idx = dst_byte_idx / k_block_length_in_bytes;\n const size_t k_block_byte_idx = dst_byte_idx % k_block_length_in_bytes;\n const size_t super_k_block_idx = k_block_idx / nr;\n const size_t nr_idx = k_block_idx % nr;\n\n const size_t k_adjustment =\n ((k_block_byte_idx + super_k_block_idx * k_block_length_in_bytes) / k_interleaved_v) *\n k_interleaved_v;\n const size_t k0_idx = k_block_byte_idx + super_k_block_idx * k_block_length_in_bytes + k_adjustment;\n const size_t k1_idx = k0_idx + k_interleaved_v;\n const size_t n0_idx = dst_row_idx * nr + nr_idx;\n\n // Clamp the index to avoid out-of-bound reads\n const size_t n0_valid_idx = KAI_MIN(n0_idx, n - 1);\n\n const size_t src_addr_byte0 = (k0_idx + n0_valid_idx * rhs_stride + block_idx * bl) / 2;\n const size_t src_addr_byte1 = (k1_idx + n0_valid_idx * rhs_stride + block_idx * bl) / 2;\n\n uint8_t byte0 = rhs_zero_point | rhs_zero_point << 4;\n uint8_t byte1 = rhs_zero_point | rhs_zero_point << 4;\n\n if (k0_idx < k) {\n byte0 = rhs[src_addr_byte0];\n }\n if (k1_idx < k) {\n byte1 = rhs[src_addr_byte1];\n }\n\n const size_t shift_right_x0 = (k0_idx % 2 == 0) ? 4 : 0;\n const size_t shift_right_x1 = (k1_idx % 2 == 0) ? 4 : 0;\n\n const uint8_t src_x0_lo = (byte0 >> shift_right_x0) & 0x0F;\n const uint8_t src_x0_hi = (byte1 >> shift_right_x1) & 0x0F;\n\n const uint8_t dst_qs0 = src_x0_lo | (src_x0_hi << 4);\n\n *block_dst_row = dst_qs0 ^ 0x88;\n block_dst_row += sizeof(uint8_t);\n }\n\n // Adjust the zero points and scales\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n\n const float* block_zero = (const float*)zero + num_blocks_per_row * src_row_idx;\n const float* block_scale = (const float*)scale + num_blocks_per_row * src_row_idx;\n\n *block_dst_zp = block_zero[block_idx];\n *block_dst_scale = block_scale[block_idx] * 0.0625F;\n\n block_dst_zp++;\n block_dst_scale++;\n }\n }\n // Set the bias\n if (bias == NULL) {\n memset(dst_row_bias, 0, nr * kai_num_bytes_bias);\n } else {\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n\n dst_row_bias[i] = *((const float*)bias + src_row_idx);\n }\n }\n }\n}\n#endif\n"
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"path": "binding.cpp",
|
| 72 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm against gemm_qai4c32_n256_k512's raw ABI:\n// armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm.h\"\n#include \"kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod.h\"\n#include \"kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.h\"\n#include \"kai_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr size_t BL = 32;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n // M == 1 runs the dedicated GEMV ukernel, as a KleidiAI user would pick it. Both variants\n // share one packed-RHS layout (same nr/kr/sr), so a single cached weight pack serves both.\n const bool gemv = (M == 1);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm();\n const size_t kr = kai_get_kr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm();\n const size_t sr = kai_get_sr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm();\n if (kai_get_nr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod() != nr || kai_get_kr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod() != kr ||\n kai_get_sr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod() != sr) return -2;\n const size_t mr = gemv ? kai_get_mr_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod() : kai_get_mr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = gemv ? (kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr)) : (kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr));\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n if (gemv) {\n kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n } else {\n kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n }\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q4_snap = nullptr;\n static size_t B_q4_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* B_zeros_snap = nullptr;\n static size_t B_zeros_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q4_snap, B_q4_snap_size, B_q4, (size_t)N*K/2)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N*(K/BL))\n | refresh_cache(B_zeros_snap, B_zeros_snap_size, B_zeros, sizeof(float)*N*(K/BL))\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon((size_t)N, (size_t)K, nr, kr, BL);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n // This packer takes the s0s1 nibble order (high nibble = first element); the definition stores the low nibble first.\n const size_t q4_bytes = (size_t)N * K / 2;\n uint8_t* B_q4_s0s1 = static_cast<uint8_t*>(std::malloc(q4_bytes));\n if (B_q4_s0s1 == nullptr) return -1;\n for (size_t i = 0; i < q4_bytes; ++i) B_q4_s0s1[i] = (uint8_t)((B_q4[i] << 4) | (B_q4[i] >> 4));\n struct kai_rhs_pack_qs4cxs1s0_param params;\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 8;\n kai_run_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(1, (size_t)N, (size_t)K, nr, kr, sr, BL, B_q4_s0s1, B_zeros, bias, B_scales, rhs_packed, 0, ¶ms);\n std::free(B_q4_s0s1);\n }\n\n if (gemv) {\n kai_run_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod((size_t)M, (size_t)N, (size_t)K, BL, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n } else {\n kai_run_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm((size_t)M, (size_t)N, (size_t)K, BL, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n }\n return 0;\n}\n"
|
| 73 |
+
}
|
| 74 |
+
]
|
| 75 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qai4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qai4c32_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_qai4c32_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qai4c32_sme2_n256_k512: KleidiAI kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.h",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon to dynamically quantize and pack the LHS matrix in a single\n/// step.\n/// -# @ref kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon or @ref\n/// kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s0s1_f32_f32_f32_neon pack the RHS NxK matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-block (32) quantization (qsi8d32) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of m_step.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Asymmetric Signed 4-bit with per-block (multiple of 32) quantization (qai4c32)\n/// values.\n///\n/// @param[in] n_idx Col index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t n_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of m_step.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of n_step.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride); //\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n); //\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Symmetric Signed 8-bit with per-block (32) quantization (qsi8d32) and packed\n/// RHS matrix: Quantized Asymmetric Signed 4-bit with per-block (32) quantization (qai4c32) and packed.\n/// Output tile: (rows x cols) = 1 VL x 4 VL (Vector Length)\n///\n/// Instruction used: SME2 (MOPA)\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// It must be a multiple of the block length (bl).\n/// @param[in] bl Block length. Block length. It must be a multiple of 32.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the\n/// top of this file.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the\n/// top of this file.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes.\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n}\n#endif // __cplusplus\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa_asm.S",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n KAI_ASM_CODE(matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\n KAI_ASM_ALIGN\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\n stp x19, x20, [sp, -128 ]!\n stp x21, x22, [sp, 16]\n stp x23, x24, [sp, 32]\n stp x25, x26, [sp, 48]\n stp d8, d9, [sp, 64]\n stp d10, d11, [sp, 80]\n stp d12, d13, [sp, 96]\n stp d14, d15, [sp, 112]\n KAI_ASM_INST(0xd503477f) // smstart\n cntw x14\n ptrue p0.b, all\n KAI_ASM_INST(0x25a07810) // ptrue pn8.s\n cntw x5 //mr\n lsl x5, x5, #2\n whilelt p4.b, xzr, x5\n ldr x6, [x0, #0x58] // lut\n KAI_ASM_INST(0xe11f80c0) // ldr zt0, [x6]\n ldr x19, [x0, #0x10] // rhs_packed\n KAI_ASM_INST(0x8558c009) // ld1rw z9.s, p0/z, [x0, #0x60]\n KAI_ASM_INST(0x8559c00a) // ld1rw z10.s, p0/z, [x0, #0x64]\n ldr x4, [x0, #0x50] // bl\n ldr x21, [x0, #0x18] // dst_stride_row\n ldr x20, [x0] // dst\n mov x8, #0\n ldr x13, [x0, #0x40] // n\n ldr x23, [x0, #0x48] // k\n KAI_ASM_INST(0x25ad6511) // whilelt pn9.s, x8, x13, VLx4\n b.eq label_9 // b.none label_9\nKAI_ASM_LABEL(label_1) // N Loop\n ldr x9, [x0, #0x38] // m\n ldr x22, [x0, #0x8] // lhs_packed\n mov x24, x20\nKAI_ASM_LABEL(label_2) // M Loop\n mov x26, x19\n mov x3, x22\n cmp x9, x14\n csel x15, x9, x14, lo\n lsl x15, x15, #2\n ldr x10, [x0, #0x48] // k\n cmp x10, #0\n b.eq label_8\nKAI_ASM_LABEL(label_3) // K Loop\n KAI_ASM_INST(0xc00800ff) // zero {za}\n mov x11, x4\nKAI_ASM_LABEL(label_4) // Block Loop\n KAI_ASM_INST(0xa0404342) //ld1w {z2.s - z3.s}, pn8/z, [x26]\n addvl x26, x26, #2\n ld1h {z8.h}, p0/z, [x3]\n addvl x3, x3, #1\n KAI_ASM_INST(0xc08a4044) // luti4 {z4.b - z5.b}, zt0, z2[0]\n KAI_ASM_INST(0xc08a4066) // luti4 {z6.b - z7.b}, zt0, z3[0]\n KAI_ASM_INST(0xa0840100) // smopa za0.s, p0/m, p0/m, z8.b, z4.b\n KAI_ASM_INST(0xa0850101) // smopa za1.s, p0/m, p0/m, z8.b, z5.b\n KAI_ASM_INST(0xa0860102) // smopa za2.s, p0/m, p0/m, z8.b, z6.b\n KAI_ASM_INST(0xa0870103) // smopa za3.s, p0/m, p0/m, z8.b, z7.b\n subs x11, x11, #4\n b.gt label_4\n mov w12, #0\n mov x25, x24\n ld1b {z17.b}, p4/z, [x3] // lhs sum\n ld1b {z16.b}, p4/z, [x3, #1, mul vl] // lhs scale\n addvl x3, x3, #2\n KAI_ASM_INST(0xa040c354) // ld1w { z20.s - z23.s }, pn8/z, [x26] // rhs zp\n KAI_ASM_INST(0xa041c340) // ld1w { z0.s - z3.s }, pn8/z, [x26, #4, mul vl ] // rhs scale\n addvl x26, x26, #8\n pfalse p3.b\nKAI_ASM_LABEL(label_5)\n pnext p3.s, p0, p3.s\n clastb z19.s, p3, z19.s, z16.s\n clastb z18.s, p3, z18.s, z17.s\n KAI_ASM_INST(0xc006041c) // mova {z28.b-z31.b}, za0h.b[w12, 0:3]\n add w12, w12, #4\n fmul z4.s, z0.s, z19.s\n fmul z5.s, z1.s, z19.s\n fmul z6.s, z2.s, z19.s\n fmul z7.s, z3.s, z19.s\n KAI_ASM_INST(0xc132e39c) // scvtf {z28.s-z31.s}, {z28.s-z31.s}\n cmp x10, x23\n b.ne label_6\n fmul z24.s, z20.s, z18.s\n fmul z25.s, z21.s, z18.s\n fmul z26.s, z22.s, z18.s\n fmul z27.s, z23.s, z18.s\n fmla z24.s, p0/m, z4.s, z28.s\n fmla z25.s, p0/m, z5.s, z29.s\n fmla z26.s, p0/m, z6.s, z30.s\n fmla z27.s, p0/m, z7.s, z31.s\n b label_7\nKAI_ASM_LABEL(label_6)\n KAI_ASM_INST(0xa040c738) // ld1w {z24.s-z27.s}, pn9/z, [x25]\n fmla z24.s, p0/m, z20.s, z18.s\n fmla z25.s, p0/m, z21.s, z18.s\n fmla z26.s, p0/m, z22.s, z18.s\n fmla z27.s, p0/m, z23.s, z18.s\n fmla z24.s, p0/m, z4.s, z28.s\n fmla z25.s, p0/m, z5.s, z29.s\n fmla z26.s, p0/m, z6.s, z30.s\n fmla z27.s, p0/m, z7.s, z31.s\nKAI_ASM_LABEL(label_7)\n KAI_ASM_INST(0xa060c738) // st1w {z24.s-z27.s}, pn9, [x25]\n add x25, x25, x21\n cmp x12, x15\n blt label_5\n subs x10, x10, x4\n b.gt label_3\nKAI_ASM_LABEL(label_8)\n ldr x5, [x0,0x30]\n add x5, x5, x19\n KAI_ASM_INST(0xa040c0ac) // ld1w {z12.s - z15.s}, pn8/z, [x5]\n mov x5, x24\n mov x12, 0\nKAI_ASM_LABEL(label_10) // Bias loop\n KAI_ASM_INST(0xa040c718) // ld1w {z24.s-z27.s}, pn9/z, [x24]\n fadd z24.s, p0/m, z24.s, z12.s\n fadd z25.s, p0/m, z25.s, z13.s\n fadd z26.s, p0/m, z26.s, z14.s\n fadd z27.s, p0/m, z27.s, z15.s\n KAI_ASM_INST(0xc1aac938) // fclamp { z24.s - z27.s }, z9.s, z10.s\n KAI_ASM_INST(0xa060c718) // st1w {z24.s-z27.s}, pn9, [x24]\n add x24, x24, x21\n add x12, x12, #4\n cmp x12, x15\n blt label_10\n mov x24, x5\n ldr\tx5, [x0, #0x20]\n add x22, x22, x5\n mov x24, x25\n decw x9, all\n cmp x9, #0\n b.gt label_2\n incb x20, all, mul #4\n ldr x5, [x0, #0x28] // rhs_packed_stride\n add x19, x19, x5\n incb x8, all\n KAI_ASM_INST(0x25ad6511) // whilelt pn9.s, x8, x13, VLx4\n b.mi label_1 // b.first label_1\nKAI_ASM_LABEL(label_9)\n KAI_ASM_INST(0xd503467f) // smstop\n ldp d14, d15, [sp, 112]\n ldp d12, d13, [sp, 96]\n ldp d10, d11, [sp, 80]\n ldp d8, d9, [sp, 64]\n ldp x25, x26, [sp, 48]\n ldp x23, x24, [sp, 32]\n ldp x21, x22, [sp, 16]\n ldp x19, x20, [sp],128\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.c",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\ntypedef struct {\n float* dst; // 0\n const void* lhs_packed; // 0x8\n const void* rhs_packed; // 0x10\n size_t dst_stride_row; // 0x18\n size_t lhs_packed_stride; // 0x20\n size_t rhs_packed_stride; // 0x28\n size_t bias; // 0x30\n size_t m; // 0x38\n size_t n; // 0x40\n size_t k; // 0x48\n size_t bl; // 0x50\n const int32_t* lut; // 0x58\n float min; // 0x60\n float max; // 0x64\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(KernelArgs* args_ptr);\n// Compute args\nstatic const size_t kai_m_step = 1; // Multiple of vector length\nstatic const size_t kai_n_step = 4; // Multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // Multiple of vector length\nstatic const size_t kai_nr = 4; // Multiple of vector length\nstatic const size_t kai_kr = 8;\nstatic const size_t kai_sr = 2;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 4;\nstatic const size_t kai_num_bytes_sum_lhs = 4;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_num_bytes_recip_qvalue_rhs = 2;\nstatic const size_t kai_num_bytes_multiplier_rhs = 4;\nstatic const size_t kai_num_bytes_offset_rhs = 4;\n\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_num_bytes_bias = 4;\nstatic const size_t kai_bl = 32;\n\n// Look-up table used for int4->int8 convert\nstatic const int32_t lut[16] = {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};\n\ninline static size_t kai_get_num_bytes_per_block_lhs(size_t bl) {\n return (bl * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs + kai_num_bytes_sum_lhs;\n}\n\ninline static size_t kai_get_num_bytes_per_block_rhs(size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n size_t num_bytes_per_block_rhs =\n (bl / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs + kai_num_bytes_offset_rhs;\n return num_bytes_per_block_rhs;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t bl) {\n const size_t mr = kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n return mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block_lhs(bl);\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t bl) {\n KAI_ASSUME(bl % kai_bl == 0);\n KAI_ASSUME((k % kai_bl) == 0);\n\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n size_t rhs_packed_stride = nr * (num_bytes_per_block * num_blocks_per_row);\n // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias\n rhs_packed_stride += nr * kai_num_bytes_bias;\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t k, size_t bl) {\n const size_t mr = kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n KAI_ASSUME((m_idx % mr) == 0);\n\n return (m_idx / mr) * kai_get_lhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t n_idx, size_t k, size_t bl) {\n KAI_ASSUME((k % bl) == 0);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n KAI_ASSUME((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n const size_t m_step = kai_get_m_step_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t n_step = kai_get_n_step_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n KAI_ASSUME((m_idx % m_step) == 0);\n KAI_ASSUME((n_idx % n_step) == 0);\n\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSUME(dst_stride_col == sizeof(float));\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl) == 0);\n\n if (m == 0) {\n return;\n }\n\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n KernelArgs args;\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.dst_stride_row = dst_stride_row;\n args.lhs_packed_stride = kai_get_lhs_packed_stride(k, bl);\n args.rhs_packed_stride = kai_get_rhs_packed_stride(k, bl);\n args.bias = args.rhs_packed_stride - nr * kai_num_bytes_bias;\n args.m = m;\n args.n = n;\n args.k = k;\n args.bl = bl;\n args.lut = lut;\n args.min = scalar_min;\n args.max = scalar_max;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.c",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n\n#include \"kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.h\"\n\n#include <arm_neon.h>\n#include <float.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_sum = sizeof(float);\nstatic const size_t kai_num_bytes_multiplier = sizeof(float);\nstatic const size_t kai_bl_multiple_of = 32;\n\ninline static size_t kai_get_num_bytes_per_block(size_t bl) {\n return bl * sizeof(int8_t) + kai_num_bytes_multiplier + kai_num_bytes_sum;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSERT((k % bl) == 0);\n return k / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t bl) {\n KAI_UNUSED(kr);\n\n return mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block(bl);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qsi8d32pscalef32_f32_neon(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((m_idx % mr) == 0);\n\n KAI_UNUSED(sr);\n return (m_idx / mr) * kai_get_lhs_packed_stride(k, mr, kr, bl);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n KAI_UNUSED(sr);\n\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return (num_rows * kai_get_lhs_packed_stride(k, mr, kr, bl));\n}\nvoid kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs,\n size_t lhs_stride, void* lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSUME((bl % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n KAI_ASSERT(((kr / sr) == 8) || ((kr / sr) == 4));\n\n if (m == 0) {\n return;\n }\n const size_t lhs_packed_stride = kai_get_lhs_packed_stride(k, mr, kr, bl);\n const size_t num_rows = m;\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block(bl);\n const size_t mr_block_size = mr * num_bytes_per_block;\n\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {\n const float* row_src_ptr = (const float*)((const uint8_t*)lhs + (row_idx + m_idx_start) * lhs_stride);\n const size_t dst_idx = ((row_idx + m_idx_start) % mr);\n\n for (size_t blk_idx = 0; blk_idx < num_blocks_per_row; ++blk_idx) {\n const float* src_ptr = row_src_ptr + blk_idx * bl;\n int8_t* dst_ptr = (int8_t*)lhs_packed + dst_idx * k_block_len * sizeof(int8_t) + blk_idx * mr_block_size;\n int8_t* param_ptr = (int8_t*)lhs_packed + blk_idx * mr_block_size + bl * mr + dst_idx * kai_num_bytes_sum;\n\n // Find absmax for each block\n float absmax = -FLT_MAX;\n int32_t k_idx = 0;\n float32x4_t vabsmax = vdupq_n_f32(-FLT_MAX);\n for (; k_idx < ((int32_t)bl); k_idx += 8) {\n const float32x4_t src0_0 = vld1q_f32(src_ptr + 0 + (size_t)k_idx);\n const float32x4_t src0_1 = vld1q_f32(src_ptr + 4 + (size_t)k_idx);\n // Calculate the max\n vabsmax = vmaxq_f32(vabsq_f32(src0_0), vmaxq_f32(vabsmax, vabsq_f32(src0_1)));\n }\n // Get the absmax\n absmax = vmaxvq_f32(vabsmax);\n\n // Maximum/minimum int8 values\n const float qmax = (float)INT8_MAX;\n\n // Get the scale and reciprocal to quantize\n const float scale0 = absmax == 0.0F ? 0.0F : qmax / absmax;\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n int32_t qsum = 0;\n // Quantize the blocks\n for (k_idx = 0; k_idx <= (int32_t)bl - k_block_len; k_idx += k_block_len) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)k_idx, k - 1);\n if (k_block_len == 8) {\n const float32x4_t vsrc_0 = vld1q_f32(src_ptr + k_idx_start);\n const float32x4_t vsrc_1 = vld1q_f32(src_ptr + k_idx_start + 4);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(vsrc_0, scale0);\n float32x4_t v1_f32 = vmulq_n_f32(vsrc_1, scale0);\n\n int16x4_t v0_s16 = vqmovn_s32(vcvtnq_s32_f32(v0_f32));\n int16x4_t v1_s16 = vqmovn_s32(vcvtnq_s32_f32(v1_f32));\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n // Update the sum\n qsum += vaddvq_s16(v_s16);\n\n int8x8_t v0_s8 = vqmovn_s16(v_s16);\n vst1_s8(dst_ptr, v0_s8);\n dst_ptr += 8 * sizeof(int8_t);\n } else if (k_block_len == 4) {\n const float32x2_t vsrc_0 = vld1_f32(src_ptr + k_idx_start);\n const float32x2_t vsrc_1 = vld1_f32(src_ptr + k_idx_start + 2);\n\n // Scale the values\n float32x2_t v0_f32 = vmul_n_f32(vsrc_0, scale0);\n float32x2_t v1_f32 = vmul_n_f32(vsrc_1, scale0);\n\n int32x2_t v0_s32 = vcvtn_s32_f32(v0_f32);\n int32x2_t v1_s32 = vcvtn_s32_f32(v1_f32);\n int16x4_t v_s16 = vqmovn_s32(vcombine_s32(v0_s32, v1_s32));\n\n v_s16 = vmax_s16(v_s16, vdup_n_s16(INT8_MIN));\n v_s16 = vmin_s16(v_s16, vdup_n_s16(INT8_MAX));\n\n // Update the sum\n qsum += vaddv_s16(v_s16);\n\n dst_ptr[0] = vqmovnh_s16(vget_lane_s16(v_s16, 0));\n dst_ptr[1] = vqmovnh_s16(vget_lane_s16(v_s16, 1));\n dst_ptr[2] = vqmovnh_s16(vget_lane_s16(v_s16, 2));\n dst_ptr[3] = vqmovnh_s16(vget_lane_s16(v_s16, 3));\n dst_ptr += 4 * sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n *((float*)(param_ptr)) = ((float)qsum) * recip_scale0;\n param_ptr += mr * kai_num_bytes_sum;\n *((float*)(param_ptr)) = recip_scale0;\n }\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((int8_t*)lhs_packed + lhs_packed_stride);\n }\n }\n}\n#endif // Architectural features check.\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.h",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qsi8d32pscalef32_f32_neon(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized symmetric per-block (qsi8d32) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of mr.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl The block length.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be\n/// a multiple of 32.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs,\n size_t lhs_stride, void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.c",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n#include \"kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.h\"\n\n#include <arm_neon.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_offset_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\nstatic const size_t kai_bl_multiple_of = 32;\nstatic const size_t kai_nr_multiple_of = 4;\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_get_num_bytes_per_block(size_t bl) {\n return (bl / 2) + kai_num_bytes_multiplier_rhs + kai_num_bytes_offset_rhs;\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kr) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block(bl);\n return nr * (num_bytes_per_block * num_blocks_per_row + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((n_idx % nr) == 0);\n KAI_UNUSED(kr);\n return (n_idx / nr) * kai_get_rhs_packed_stride(k, nr, kr, bl);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_UNUSED(kr);\n const size_t num_rows = kai_roundup(n, nr) / nr;\n return num_rows * kai_get_rhs_packed_stride(k, nr, kr, bl);\n}\n\nvoid kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t bl, const uint8_t* rhs,\n const void* zero, const void* bias, const void* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qai4c32p_params* params) {\n KAI_ASSUME(num_groups == 1);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n KAI_ASSUME((nr % kai_nr_multiple_of) == 0);\n KAI_ASSUME(extra_bytes == 0);\n\n KAI_ASSUME(sr == 2);\n KAI_ASSUME(kr / sr == 4);\n KAI_ASSUME(rhs != NULL);\n KAI_ASSUME(zero != NULL);\n KAI_ASSUME(scale != NULL);\n KAI_ASSUME(rhs_packed != NULL);\n KAI_ASSUME(params != NULL);\n KAI_ASSUME(params->rhs_zero_point == 8);\n KAI_ASSUME(params->lhs_zero_point == 1);\n\n // Note: The input matrix (rhs) is expected with:\n // \"k\" columns and \"n\" rows (NxK)\n\n const size_t block_length = kr / sr;\n const size_t num_blocks_per_row = k / bl;\n const size_t rhs_stride = k / 2;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride(k, nr, kr, bl);\n\n const size_t dst_packed_block_size = kai_get_num_bytes_per_block(bl) * nr;\n const size_t dst_block_data_size = bl / 2;\n const size_t dst_num_rows = kai_roundup(n, nr) / nr;\n const size_t dst_bias_offset = num_blocks_per_row * dst_packed_block_size;\n const size_t k_block_length_in_bytes = (block_length * sizeof(uint8_t)) / 2;\n\n for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; ++dst_row_idx) {\n uint8_t* dst_row = (uint8_t*)rhs_packed + dst_row_idx * rhs_packed_stride;\n float* dst_row_bias = (float*)(dst_row + dst_bias_offset);\n size_t row_idx = dst_row_idx * nr;\n size_t rows_left = n - row_idx;\n for (size_t block_idx = 0; block_idx < num_blocks_per_row; block_idx++) {\n uint8_t* block_dst_row = dst_row + block_idx * dst_packed_block_size;\n float* block_dst_zp = (float*)(block_dst_row + nr * dst_block_data_size);\n float* block_dst_scale = block_dst_zp + nr;\n size_t k_idx = block_idx * bl;\n for (size_t dst_byte_idx = 0; dst_byte_idx < dst_block_data_size; dst_byte_idx += 8) {\n for (size_t nr_idx = 0; nr_idx <= nr - 4; nr_idx += 4) {\n const size_t n0_idx = KAI_MIN(dst_row_idx * nr + nr_idx, n - 1);\n const size_t n1_idx = KAI_MIN(n0_idx + 1, n - 1);\n const size_t n2_idx = KAI_MIN(n0_idx + 2, n - 1);\n const size_t n3_idx = KAI_MIN(n0_idx + 3, n - 1);\n const uint8_t* src_addr_byte = rhs + (k_idx / 2) + dst_byte_idx;\n\n const uint8x8_t vec0_u8 = vld1_u8(src_addr_byte + n0_idx * rhs_stride);\n const uint8x8_t vec1_u8 = vld1_u8(src_addr_byte + n1_idx * rhs_stride);\n const uint8x8_t vec2_u8 = vld1_u8(src_addr_byte + n2_idx * rhs_stride);\n const uint8x8_t vec3_u8 = vld1_u8(src_addr_byte + n3_idx * rhs_stride);\n\n const uint16x4_t vec0_u16 = vreinterpret_u16_u8(vec0_u8);\n const uint16x4_t vec1_u16 = vreinterpret_u16_u8(vec1_u8);\n const uint16x4_t vec2_u16 = vreinterpret_u16_u8(vec2_u8);\n const uint16x4_t vec3_u16 = vreinterpret_u16_u8(vec3_u8);\n\n const uint16x4_t vec01_lo_u16 = vzip1_u16(vec0_u16, vec1_u16);\n const uint16x4_t vec01_hi_u16 = vzip2_u16(vec0_u16, vec1_u16);\n const uint16x4_t vec23_lo_u16 = vzip1_u16(vec2_u16, vec3_u16);\n const uint16x4_t vec23_hi_u16 = vzip2_u16(vec2_u16, vec3_u16);\n\n const uint32x2_t vec01_lo_u32 = vreinterpret_u32_u16(vec01_lo_u16);\n const uint32x2_t vec01_hi_u32 = vreinterpret_u32_u16(vec01_hi_u16);\n const uint32x2_t vec23_lo_u32 = vreinterpret_u32_u16(vec23_lo_u16);\n const uint32x2_t vec23_hi_u32 = vreinterpret_u32_u16(vec23_hi_u16);\n\n const uint32x2_t vin0_u32 = vzip1_u32(vec01_lo_u32, vec23_lo_u32);\n const uint32x2_t vin1_u32 = vzip2_u32(vec01_lo_u32, vec23_lo_u32);\n const uint32x2_t vin2_u32 = vzip1_u32(vec01_hi_u32, vec23_hi_u32);\n const uint32x2_t vin3_u32 = vzip2_u32(vec01_hi_u32, vec23_hi_u32);\n\n const uint8x8_t vin0_u8 = vreinterpret_u8_u32(vin0_u32);\n const uint8x8_t vin1_u8 = vreinterpret_u8_u32(vin1_u32);\n const uint8x8_t vin2_u8 = vreinterpret_u8_u32(vin2_u32);\n const uint8x8_t vin3_u8 = vreinterpret_u8_u32(vin3_u32);\n\n uint8_t* dst_row_offset = block_dst_row + nr_idx * k_block_length_in_bytes;\n vst1_u8(dst_row_offset, vin0_u8);\n vst1_u8(dst_row_offset + nr * k_block_length_in_bytes, vin1_u8);\n vst1_u8(dst_row_offset + 2 * (nr * k_block_length_in_bytes), vin2_u8);\n vst1_u8(dst_row_offset + 3 * (nr * k_block_length_in_bytes), vin3_u8);\n }\n block_dst_row += nr * sizeof(uint8x8_t);\n }\n\n // Adjust the zero points and scales\n for (size_t i = 0; i < nr; ++i) {\n const size_t src_row_idx = KAI_MIN(row_idx + i, n - 1);\n const size_t src_idx = src_row_idx * num_blocks_per_row + block_idx;\n\n block_dst_scale[i] = ((const float*)scale)[src_idx];\n block_dst_zp[i] = ((const float*)zero)[src_idx];\n }\n }\n // Set the bias\n if (bias == NULL) {\n memset(dst_row_bias, 0, nr * kai_num_bytes_bias);\n } else {\n if (rows_left >= nr) {\n memcpy(dst_row_bias, &((const float*)bias)[row_idx], nr * kai_num_bytes_bias);\n } else {\n // Fill remaining values\n memcpy(dst_row_bias, &((const float*)bias)[row_idx], rows_left * kai_num_bytes_bias);\n // Set leftover to 0\n memset(&dst_row_bias[rows_left], 0, (nr - rows_left) * kai_num_bytes_bias);\n }\n }\n }\n}\n#endif\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef kai_rhs_pack_nxk_qai4c32p_params\n#define kai_rhs_pack_nxk_qai4c32p_params kai_rhs_pack_qs4cxs1s0_param\n#endif\n\n/// Gets the offset in bytes for the RHS matrix (not packed), which holds\n/// the int4 values in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 K values are stored in one byte. These values are stored in blocks\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n_idx, //\n size_t rhs_stride); //\n\n/// Gets the offset in bytes for the packed RHS matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K)\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n_idx, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl //\n);\n\n/// Gets the size in bytes for the quantized and packed RHS matrix.\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple\n/// of 32.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl //\n);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 values are stored in one byte.\n/// nrx4 => this function can take in generic nr values but the input is expected to have a block depth of 4\n/// Block depth is calculated as kr / sr. The values of these parameters are defined in the matmul ukernel.\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of columns of the output matrix (N).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// @param[in] nr The number of N rows to interleave on the same output row.\n/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] bl The block length, which defines the number of\n/// K values stored in a single block. It must be a multiple of 32.\n/// @param[in] rhs The RHS matrix containing the 4-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).\n/// @param[in] zero The zero point.\n/// @param[in] bias The biases.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n const uint8_t* rhs, //\n const void* zero, //\n const void* bias, //\n const void* scale, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_nxk_qai4c32p_params* params);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "binding.cpp",
|
| 65 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa against gemm_qai4c32_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n#include <arm_neon.h>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.h\"\n#include \"kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr size_t BL = 32;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n// Widen n raw fp16 patterns to fp32 (exact).\nvoid widen_f16(const uint16_t* src, float* dst, size_t n) {\n size_t i = 0;\n for (; i + 4 <= n; i += 4) vst1q_f32(dst + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(src + i))));\n for (; i < n; ++i) dst[i] = vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(src[i]))), 0);\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n // The SME2 mopa kernel takes any M in a single call, so there is no GEMV dispatch here.\n const size_t mr = kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q4_snap = nullptr;\n static size_t B_q4_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* B_zeros_snap = nullptr;\n static size_t B_zeros_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q4_snap, B_q4_snap_size, B_q4, (size_t)N*K/2)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N*(K/BL))\n | refresh_cache(B_zeros_snap, B_zeros_snap_size, B_zeros, sizeof(float)*N*(K/BL))\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon((size_t)N, (size_t)K, nr, kr, BL);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_qs4cxs1s0_param params;\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 8;\n kai_run_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon(1, (size_t)N, (size_t)K, nr, kr, sr, BL, B_q4, B_zeros, bias, B_scales, rhs_packed, 0, ¶ms);\n }\n\n kai_run_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa((size_t)M, (size_t)N, (size_t)K, BL, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n return 0;\n}\n"
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qai8_qsi8cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qai8_qsi8cx_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_qai8_qsi8cx_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qai8_qsi8cx_sme2_n256_k512: KleidiAI kai_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa with its x8p2vlx4 LHS and kxn qsi8cxp RHS packers; scales and bias folded into the kernel's static requantization parameters.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa.h",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# kai_lhs_pack_x8p2vlx4_x8_sme to pack the LHS matrix.\n/// -# kai_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme to pack the RHS matrix.\n\n/// Gets m step value.\n///\n/// The starting row index must be divisible by `m_step`.\n///\n/// @return The m step value.\nsize_t kai_get_m_step_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void);\n\n/// Gets n step value.\n///\n/// The starting column index must be divisible by `n_step`.\n///\n/// @return The n step value.\nsize_t kai_get_n_step_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void);\n\n/// Gets mr value.\n///\n/// This is the packing parameter which must be used to pack the LHS matrix.\n///\n/// @return The mr value.\nsize_t kai_get_mr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void);\n\n/// Gets nr value.\n///\n/// This is the packing parameter which must be used to pack the RHS matrix.\n///\n/// @return The nr value.\nsize_t kai_get_nr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void);\n\n/// Gets kr value.\n///\n/// This is the packing parameter which must be used to pack the LHS and RHS matrix.\n///\n/// @return The kr value.\nsize_t kai_get_kr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void);\n\n/// Gets sr value.\n///\n/// This is the packing parameter which must be used to pack the LHS and RHS matrix.\n///\n/// @return The sr value.\nsize_t kai_get_sr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void);\n\n/// Gets the offset in bytes to the data element in the packed LHS matrix buffer.\n///\n/// @param[in] m_idx Row index in the unpacked LHS matrix. Must be a multiple of `m_step`.\n/// @param[in] k Number of columns in the unpacked LHS matrix.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_lhs_packed_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(size_t m_idx, size_t k);\n\n/// Gets the offset in bytes to the data element in the packed RHS matrix buffer.\n///\n/// @param[in] n_idx Column index in the unpacked RHS matrix. Must be a multiple of `n_step`.\n/// @param[in] k Number of columns in the unpacked LHS matrix.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_rhs_packed_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(size_t n_idx, size_t k);\n\n/// Gets the offset in bytes to the data element in the destination matrix buffer.\n///\n/// @param[in] m_idx Row index. Must be a multiple of `m_step`.\n/// @param[in] n_idx Column index. Must be a multiple of `n_step`.\n/// @param[in] dst_stride_row Row stride in bytes.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_dst_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride_row);\n\n/// Gets the size in bytes of the destination matrix buffer.\n///\n/// @param[in] m Number of rows.\n/// @param[in] n Number of columns.\n///\n/// @return The size in bytes of the destination matrix buffer.\nsize_t kai_get_dst_size_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(size_t m, size_t n);\n\n/// Runs the matrix multiplication microkernel followed by a clamp operation.\n///\n/// The pointer of each buffers (packed LHS, packed RHS and output) needs to be added with offset\n/// calculated using the following functions:\n///\n/// * Packed LHS: @ref kai_get_lhs_packed_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa.\n/// * Packed RHS: @ref kai_get_rhs_packed_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa.\n/// * Output: @ref kai_get_dst_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa.\n///\n/// @param[in] m Number of output rows to be computed.\n/// @param[in] n Number of output columns to be computed.\n/// @param[in] k Number of columns in the unpacked LHS matrix.\n/// @param[in] lhs_packed Packed LHS matrix buffer.\n/// @param[in] rhs_packed Packed RHS matrix buffer.\n/// @param[out] dst Output matrix buffer.\n/// @param[in] dst_stride_row Row stride in bytes of the output matrix.\n/// @param[in] dst_stride_col Column stride in bytes of the output matrix. Unused\n/// @param[in] params Requantization and clamp parameters.\nvoid kai_run_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(\n size_t m, size_t n, size_t k, const void* lhs_packed, const void* rhs_packed, void* dst, size_t dst_stride_row,\n size_t dst_stride_col, const struct kai_matmul_requantize32_params* params);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa.c",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n#include \"kai_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\ntypedef struct {\n const void* A;\n const void* B;\n void* C;\n uint64_t ldcb;\n uint64_t M;\n uint64_t N;\n uint64_t K;\n int32_t min;\n int32_t max;\n int32_t result_zero_point;\n void* accumulator_buffer;\n uint64_t flags;\n} KernelArgs;\n\nstatic const size_t kai_mr = 2;\nstatic const size_t kai_nr = 2;\nstatic const size_t kai_kr = 4;\nstatic const size_t kai_sr = 1;\n\nvoid kai_kernel_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(KernelArgs* args);\n\n// Returns a constant value specific to this kernel that's relative to vector length\nstatic size_t kai_get_kernel_vec_length_constant(void) {\n const size_t kernel_vec_length_constant = kai_get_sme_vector_length_u8() / kai_kr;\n return kernel_vec_length_constant;\n}\n\nsize_t kai_get_m_step_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void) {\n return kai_mr * kai_get_kernel_vec_length_constant();\n}\n\nsize_t kai_get_n_step_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void) {\n return kai_nr * kai_get_kernel_vec_length_constant();\n}\n\nsize_t kai_get_mr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void) {\n return kai_mr * kai_get_kernel_vec_length_constant();\n}\n\nsize_t kai_get_nr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void) {\n return kai_nr * kai_get_kernel_vec_length_constant();\n}\n\nsize_t kai_get_kr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(size_t m_idx, size_t k) {\n KAI_ASSUME(m_idx % kai_get_mr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa() == 0);\n return m_idx * kai_roundup(k, kai_kr) * sizeof(int8_t);\n}\n\nstatic size_t kai_get_rhs_packed_stride_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(size_t k) {\n return kai_get_nr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa() *\n (sizeof(int32_t) + kai_roundup(k, kai_kr) * sizeof(int8_t) + sizeof(float));\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(size_t n_idx, size_t k) {\n KAI_ASSUME(n_idx % kai_get_nr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa() == 0);\n const size_t block_idx = n_idx / kai_get_nr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa();\n return block_idx * kai_get_rhs_packed_stride_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride_row) {\n KAI_ASSUME(m_idx % kai_get_m_step_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa() == 0);\n KAI_ASSUME(n_idx % kai_get_n_step_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa() == 0);\n\n return m_idx * dst_stride_row + n_idx * sizeof(int8_t);\n}\n\nsize_t kai_get_dst_size_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(size_t m, size_t n) {\n return m * n * sizeof(int8_t);\n}\n\nvoid kai_run_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(\n size_t m, size_t n, size_t k, const void* lhs_packed, const void* rhs_packed, void* dst, size_t dst_stride_row,\n size_t dst_stride_col, const struct kai_matmul_requantize32_params* params) {\n KAI_UNUSED(dst_stride_col);\n KernelArgs args;\n\n args.A = lhs_packed;\n args.B = rhs_packed;\n args.C = dst;\n args.ldcb = dst_stride_row;\n args.M = m;\n args.N = n;\n args.K = k;\n args.min = params->min_value;\n args.max = params->max_value;\n args.result_zero_point = params->output_zero_point;\n args.accumulator_buffer = NULL;\n args.flags = 0;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa_asm.S",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa)\n stp x20, x21, [sp, -144]!\n stp x22, x23, [sp, 16]\n stp x24, x25, [sp, 32]\n stp x26, x27, [sp, 48]\n str x28, [sp, 64]\n stp d8, d9, [sp, 72]\n stp d10, d11, [sp, 88]\n stp d12, d13, [sp, 104]\n stp d14, d15, [sp, 120]\n KAI_ASM_INST(0xd503477f) // SMSTART ZA\n mov x15, #0x0\n ldr x14, [x0, #0x30]\n ptrue p1.b\n KAI_ASM_INST(0x25207810) // ptrue pn8.b\n ldr w13, [x0, #0x20]\n mov x11, #0x0\n ldr w10, [x0, #0x28]\n add x14, x14, #0x3\n ldr x9, [x0, #0x0]\n lsr x14, x14, #0x2\nKAI_ASM_LABEL(label_1) // M loop\n ldr x28, [x0, #0x8]\nKAI_ASM_LABEL(label_2) // N loop\n KAI_ASM_INST(0xa0404382) // ld1w { z2.s-z3.s }, pn8.b/Z, [x28]\n KAI_ASM_INST(0xc00800ff) // zero { zad0, zad1, zad2, zad3, zad4, zad5, zad6, zad7 }\n mov x27, x9\n addvl x28, x28, #2\n KAI_ASM_INST(0xc0902440) // addha za0.s, p1/M, p1/M, z2.s\n KAI_ASM_INST(0xc0902461) // addha za1.s, p1/M, p1/M, z3.s\n KAI_ASM_INST(0xc0902442) // addha za2.s, p1/M, p1/M, z2.s\n KAI_ASM_INST(0xc0902463) // addha za3.s, p1/M, p1/M, z3.s\n lsr x21, x14, #0x2\n and x20, x14, #0x3\n cbz x21, label_6\n subs x21, x21, #0x1\n KAI_ASM_INST(0xa1408362) // ld1b { z2.b, z6.b, z10.b, z14.b }, pn8.b/Z, [x27]\n KAI_ASM_INST(0xa1418360) // ld1b { z0.b, z4.b, z8.b, z12.b }, pn8.b/Z, [x27, #0x4, MUL VL]\n addvl x27, x27, #8\n KAI_ASM_INST(0xa0408390) // ld1b { z16.b-z19.b }, pn8.b/Z, [x28]\n KAI_ASM_INST(0xa041839c) // ld1b { z28.b-z31.b }, pn8.b/Z, [x28, #0x4, MUL VL]\n addvl x28, x28, #8\n ble label_5\nKAI_ASM_LABEL(label_4) // K loop\n KAI_ASM_INST(0xa0902440) // smopa za0.s, p1/M, p1/M, z2.b, z16.b\n subs x21, x21, #0x1\n KAI_ASM_INST(0xa0912441) // smopa za1.s, p1/M, p1/M, z2.b, z17.b\n KAI_ASM_INST(0xa09024c2) // smopa za2.s, p1/M, p1/M, z6.b, z16.b\n KAI_ASM_INST(0xa09124c3) // smopa za3.s, p1/M, p1/M, z6.b, z17.b\n KAI_ASM_INST(0xa0922540) // smopa za0.s, p1/M, p1/M, z10.b, z18.b\n KAI_ASM_INST(0xa0932541) // smopa za1.s, p1/M, p1/M, z10.b, z19.b\n KAI_ASM_INST(0xa09225c2) // smopa za2.s, p1/M, p1/M, z14.b, z18.b\n KAI_ASM_INST(0xa09325c3) // smopa za3.s, p1/M, p1/M, z14.b, z19.b\n KAI_ASM_INST(0xa1408362) // ld1b { z2.b, z6.b, z10.b, z14.b }, pn8.b/Z, [x27]\n KAI_ASM_INST(0xa09c2400) // smopa za0.s, p1/M, p1/M, z0.b, z28.b\n KAI_ASM_INST(0xa0408390) // ld1b { z16.b-z19.b }, pn8.b/Z, [x28]\n KAI_ASM_INST(0xa09d2401) // smopa za1.s, p1/M, p1/M, z0.b, z29.b\n KAI_ASM_INST(0xa09c2482) // smopa za2.s, p1/M, p1/M, z4.b, z28.b\n KAI_ASM_INST(0xa09d2483) // smopa za3.s, p1/M, p1/M, z4.b, z29.b\n KAI_ASM_INST(0xa09e2500) // smopa za0.s, p1/M, p1/M, z8.b, z30.b\n KAI_ASM_INST(0xa09f2501) // smopa za1.s, p1/M, p1/M, z8.b, z31.b\n KAI_ASM_INST(0xa09e2582) // smopa za2.s, p1/M, p1/M, z12.b, z30.b\n KAI_ASM_INST(0xa09f2583) // smopa za3.s, p1/M, p1/M, z12.b, z31.b\n KAI_ASM_INST(0xa1418360) // ld1b { z0.b, z4.b, z8.b, z12.b }, pn8.b/Z, [x27, #0x4, MUL VL]\n addvl x27, x27, #8\n KAI_ASM_INST(0xa041839c) // ld1b { z28.b-z31.b }, pn8.b/Z, [x28, #0x4, MUL VL]\n addvl x28, x28, #8\n bgt label_4\nKAI_ASM_LABEL(label_5) // K loop tail\n KAI_ASM_INST(0xa0902440) // smopa za0.s, p1/M, p1/M, z2.b, z16.b\n KAI_ASM_INST(0xa0912441) // smopa za1.s, p1/M, p1/M, z2.b, z17.b\n KAI_ASM_INST(0xa09024c2) // smopa za2.s, p1/M, p1/M, z6.b, z16.b\n KAI_ASM_INST(0xa09124c3) // smopa za3.s, p1/M, p1/M, z6.b, z17.b\n KAI_ASM_INST(0xa0922540) // smopa za0.s, p1/M, p1/M, z10.b, z18.b\n KAI_ASM_INST(0xa0932541) // smopa za1.s, p1/M, p1/M, z10.b, z19.b\n KAI_ASM_INST(0xa09225c2) // smopa za2.s, p1/M, p1/M, z14.b, z18.b\n KAI_ASM_INST(0xa09325c3) // smopa za3.s, p1/M, p1/M, z14.b, z19.b\n KAI_ASM_INST(0xa09c2400) // smopa za0.s, p1/M, p1/M, z0.b, z28.b\n KAI_ASM_INST(0xa09d2401) // smopa za1.s, p1/M, p1/M, z0.b, z29.b\n KAI_ASM_INST(0xa09c2482) // smopa za2.s, p1/M, p1/M, z4.b, z28.b\n KAI_ASM_INST(0xa09d2483) // smopa za3.s, p1/M, p1/M, z4.b, z29.b\n KAI_ASM_INST(0xa09e2500) // smopa za0.s, p1/M, p1/M, z8.b, z30.b\n KAI_ASM_INST(0xa09f2501) // smopa za1.s, p1/M, p1/M, z8.b, z31.b\n KAI_ASM_INST(0xa09e2582) // smopa za2.s, p1/M, p1/M, z12.b, z30.b\n KAI_ASM_INST(0xa09f2583) // smopa za3.s, p1/M, p1/M, z12.b, z31.b\nKAI_ASM_LABEL(label_6) // K oddments\n cbz x20, label_8\nKAI_ASM_LABEL(label_7) // K oddments: Loop\n KAI_ASM_INST(0xa0400370) // ld1b { z16.b-z17.b }, pn8.b/Z, [x27]\n subs x20, x20, #0x1\n addvl x27, x27, #2\n KAI_ASM_INST(0xa1400385) // ld1b { z5.b, z13.b }, pn8.b/Z, [x28]\n addvl x28, x28, #2\n KAI_ASM_INST(0xa0852600) // smopa za0.s, p1/M, p1/M, z16.b, z5.b\n KAI_ASM_INST(0xa08d2601) // smopa za1.s, p1/M, p1/M, z16.b, z13.b\n KAI_ASM_INST(0xa0852622) // smopa za2.s, p1/M, p1/M, z17.b, z5.b\n KAI_ASM_INST(0xa08d2623) // smopa za3.s, p1/M, p1/M, z17.b, z13.b\n bgt label_7\nKAI_ASM_LABEL(label_8) // K oddments: End\n ldr x26, [x0, #0x10]\n sub x25, x13, x15\n cntw x24\n KAI_ASM_INST(0x854ec41a) // ld1rw { z26.s }, p1/Z, [x0, #56]\n ldr x23, [x0, #0x18]\n whilelt p0.h, x11, x10\n cmp x25, x24\n KAI_ASM_INST(0x854fc417) // ld1rw { z23.s }, p1/Z, [x0, #60]\n csel x22, x25, x24, LT\n KAI_ASM_INST(0x8550c400) // ld1rw { z0.s }, p1/Z, [x0, #64]\n mov x12, #0x0\n add x26, x26, x11 // C += n\n lsr x21, x22, #0x2\n KAI_ASM_INST(0xa0404382) // ld1w { z2.s-z3.s }, pn8.b/Z, [x28]\n madd x26, x15, x23, x26 // C += m * ldc\n addvl x28, x28, #2\n and x20, x22, #0x3\n cbz x21, label_11\nKAI_ASM_LABEL(label_10) // Store to output array: Accumulator row 0 loop\n KAI_ASM_INST(0xc0860408) // mova { z8.s-z11.s }, za0h.s[x12]\n KAI_ASM_INST(0xc0860430) // mova { z16.s-z19.s }, za1h.s[x12]\n KAI_ASM_INST(0xc132e108) // scvtf { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc132e210) // scvtf { z16.s-z19.s }, { z16.s-z19.s }\n fmul z8.s, z8.s, z2.s\n fmul z9.s, z9.s, z2.s\n add x12, x12, #0x4\n fmul z10.s, z10.s, z2.s\n fmul z11.s, z11.s, z2.s\n cmp x12, x21, LSL #2\n fmul z16.s, z16.s, z3.s\n fmul z17.s, z17.s, z3.s\n fmul z18.s, z18.s, z3.s\n fmul z19.s, z19.s, z3.s\n KAI_ASM_INST(0xc1b8e108) // frintn { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc131e108) // fcvtzs { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc1b8e210) // frintn { z16.s-z19.s }, { z16.s-z19.s }\n KAI_ASM_INST(0xc1a0ab08) // add { z8.s-z11.s }, { z8.s-z11.s }, z0.s\n KAI_ASM_INST(0xc131e210) // fcvtzs { z16.s-z19.s }, { z16.s-z19.s }\n KAI_ASM_INST(0xc1a0ab10) // add { z16.s-z19.s }, { z16.s-z19.s }, z0.s\n KAI_ASM_INST(0xc1b7cf48) // sclamp { z8.s-z11.s }, z26.s, z23.s\n KAI_ASM_INST(0xc1b7cf50) // sclamp { z16.s-z19.s }, z26.s, z23.s\n uzp1 z5.h, z8.h, z16.h\n uzp1 z14.h, z9.h, z17.h\n uzp1 z17.h, z10.h, z18.h\n uzp1 z16.h, z11.h, z19.h\n st1b { z5.h }, p0, [x26]\n add x26, x26, x23\n st1b { z14.h }, p0, [x26]\n add x26, x26, x23\n st1b { z17.h }, p0, [x26]\n add x26, x26, x23\n st1b { z16.h }, p0, [x26]\n add x26, x26, x23\n blt label_10\nKAI_ASM_LABEL(label_11) // Store to output array: Accumulator row 0 oddments\n cbz x20, label_12\n KAI_ASM_INST(0xc0860408) // mova { z8.s-z11.s }, za0h.s[x12]\n KAI_ASM_INST(0xc086042c) // mova { z12.s-z15.s }, za1h.s[x12]\n KAI_ASM_INST(0xc132e108) // scvtf { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc132e18c) // scvtf { z12.s-z15.s }, { z12.s-z15.s }\n fmul z8.s, z8.s, z2.s\n fmul z9.s, z9.s, z2.s\n subs x20, x20, #0x1\n fmul z10.s, z10.s, z2.s\n fmul z11.s, z11.s, z2.s\n fmul z12.s, z12.s, z3.s\n fmul z13.s, z13.s, z3.s\n fmul z14.s, z14.s, z3.s\n fmul z15.s, z15.s, z3.s\n KAI_ASM_INST(0xc1b8e108) // frintn { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc131e108) // fcvtzs { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc1b8e18c) // frintn { z12.s-z15.s }, { z12.s-z15.s }\n KAI_ASM_INST(0xc1a0ab08) // add { z8.s-z11.s }, { z8.s-z11.s }, z0.s\n KAI_ASM_INST(0xc131e18c) // fcvtzs { z12.s-z15.s }, { z12.s-z15.s }\n KAI_ASM_INST(0xc1a0ab0c) // add { z12.s-z15.s }, { z12.s-z15.s }, z0.s\n KAI_ASM_INST(0xc1b7cf48) // sclamp { z8.s-z11.s }, z26.s, z23.s\n KAI_ASM_INST(0xc1b7cf4c) // sclamp { z12.s-z15.s }, z26.s, z23.s\n uzp1 z16.h, z8.h, z12.h\n st1b { z16.h }, p0, [x26]\n add x26, x26, x23\n beq label_12\n subs x20, x20, #0x1\n uzp1 z16.h, z9.h, z13.h\n st1b { z16.h }, p0, [x26]\n add x26, x26, x23\n beq label_12\n uzp1 z16.h, z10.h, z14.h\n st1b { z16.h }, p0, [x26]\n add x26, x26, x23\nKAI_ASM_LABEL(label_12) // Store to output array: Accumulator row 0 oddments: End\n subs x25, x25, x22\n beq label_16\n cmp x25, x24\n mov x12, #0x0\n csel x20, x25, x24, LT\n lsr x21, x20, #0x2\n and x20, x20, #0x3\n cbz x21, label_14\nKAI_ASM_LABEL(label_13) // Store to output array: Accumulator row 1 loop\n KAI_ASM_INST(0xc0860448) // mova { z8.s-z11.s }, za2h.s[x12]\n KAI_ASM_INST(0xc0860470) // mova { z16.s-z19.s }, za3h.s[x12]\n KAI_ASM_INST(0xc132e108) // scvtf { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc132e210) // scvtf { z16.s-z19.s }, { z16.s-z19.s }\n fmul z8.s, z8.s, z2.s\n fmul z9.s, z9.s, z2.s\n add x12, x12, #0x4\n fmul z10.s, z10.s, z2.s\n fmul z11.s, z11.s, z2.s\n cmp x12, x21, LSL #2\n fmul z16.s, z16.s, z3.s\n fmul z17.s, z17.s, z3.s\n fmul z18.s, z18.s, z3.s\n fmul z19.s, z19.s, z3.s\n KAI_ASM_INST(0xc1b8e108) // frintn { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc131e108) // fcvtzs { z8.s-z11.s }, { z8.s-z11.s }\n KAI_ASM_INST(0xc1b8e210) // frintn { z16.s-z19.s }, { z16.s-z19.s }\n KAI_ASM_INST(0xc1a0ab08) // add { z8.s-z11.s }, { z8.s-z11.s }, z0.s\n KAI_ASM_INST(0xc131e210) // fcvtzs { z16.s-z19.s }, { z16.s-z19.s }\n KAI_ASM_INST(0xc1a0ab10) // add { z16.s-z19.s }, { z16.s-z19.s }, z0.s\n KAI_ASM_INST(0xc1b7cf48) // sclamp { z8.s-z11.s }, z26.s, z23.s\n KAI_ASM_INST(0xc1b7cf50) // sclamp { z16.s-z19.s }, z26.s, z23.s\n uzp1 z21.h, z8.h, z16.h\n uzp1 z20.h, z9.h, z17.h\n uzp1 z17.h, z10.h, z18.h\n uzp1 z16.h, z11.h, z19.h\n st1b { z21.h }, p0, [x26]\n add x26, x26, x23\n st1b { z20.h }, p0, [x26]\n add x26, x26, x23\n st1b { z17.h }, p0, [x26]\n add x26, x26, x23\n st1b { z16.h }, p0, [x26]\n add x26, x26, x23\n blt label_13\nKAI_ASM_LABEL(label_14) // Store to output array: Accumulator row 1 oddments\n cbz x20, label_15\n KAI_ASM_INST(0xc086044c) // mova { z12.s-z15.s }, za2h.s[x12]\n KAI_ASM_INST(0xc0860464) // mova { z4.s-z7.s }, za3h.s[x12]\n KAI_ASM_INST(0xc132e18c) // scvtf { z12.s-z15.s }, { z12.s-z15.s }\n KAI_ASM_INST(0xc132e084) // scvtf { z4.s-z7.s }, { z4.s-z7.s }\n fmul z12.s, z12.s, z2.s\n fmul z13.s, z13.s, z2.s\n subs x20, x20, #0x1\n fmul z14.s, z14.s, z2.s\n fmul z15.s, z15.s, z2.s\n fmul z4.s, z4.s, z3.s\n fmul z5.s, z5.s, z3.s\n fmul z6.s, z6.s, z3.s\n fmul z7.s, z7.s, z3.s\n KAI_ASM_INST(0xc1b8e18c) // frintn { z12.s-z15.s }, { z12.s-z15.s }\n KAI_ASM_INST(0xc131e18c) // fcvtzs { z12.s-z15.s }, { z12.s-z15.s }\n KAI_ASM_INST(0xc1b8e084) // frintn { z4.s-z7.s }, { z4.s-z7.s }\n KAI_ASM_INST(0xc1a0ab0c) // add { z12.s-z15.s }, { z12.s-z15.s }, z0.s\n KAI_ASM_INST(0xc131e084) // fcvtzs { z4.s-z7.s }, { z4.s-z7.s }\n KAI_ASM_INST(0xc1a0ab04) // add { z4.s-z7.s }, { z4.s-z7.s }, z0.s\n KAI_ASM_INST(0xc1b7cf4c) // sclamp { z12.s-z15.s }, z26.s, z23.s\n KAI_ASM_INST(0xc1b7cf44) // sclamp { z4.s-z7.s }, z26.s, z23.s\n uzp1 z16.h, z12.h, z4.h\n st1b { z16.h }, p0, [x26]\n add x26, x26, x23\n beq label_15\n subs x20, x20, #0x1\n uzp1 z16.h, z13.h, z5.h\n st1b { z16.h }, p0, [x26]\n add x26, x26, x23\n beq label_15\n uzp1 z16.h, z14.h, z6.h\n st1b { z16.h }, p0, [x26]\nKAI_ASM_LABEL(label_15) // Store to output array: Accumulator row 1 oddments: End\nKAI_ASM_LABEL(label_16) // Store to output array: End\n incw x11, ALL, MUL #2\n cmp x11, x10\n blt label_2\n incw x15, ALL, MUL #2\n mov x11, #0x0\n cmp x15, x13\n mov x9, x27\n blt label_1\n KAI_ASM_INST(0xd503467f) // SMSTOP\n ldp x22, x23, [sp, 16]\n ldp x24, x25, [sp, 32]\n ldp x26, x27, [sp, 48]\n ldr x28, [sp, 64]\n ldp d8, d9, [sp, 72]\n ldp d10, d11, [sp, 88]\n ldp d12, d13, [sp, 104]\n ldp d14, d15, [sp, 120]\n ldp x20, x21, [sp], 144\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_pack_x8p2vlx4_x8_sme.h",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Gets m step value.\n///\n/// The starting row index must be divisible by `m_step`.\n///\n/// @param[in] mr Number of rows to be interleaved.\n///\n/// @return The m step value.\nsize_t kai_get_m_step_lhs_pack_x8p2vlx4_x8_sme(size_t mr);\n\n/// Gets the offset in bytes to the data element in the LHS buffer.\n///\n/// @param[in] m_idx Row index.\n/// @param[in] lhs_stride_row Row stride in bytes.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_lhs_offset_lhs_pack_x8p2vlx4_x8_sme(size_t m_idx, size_t lhs_stride_row);\n\n/// Gets the offset in bytes to the data element in the packed LHS buffer.\n///\n/// @param[in] m_idx Row index in the unpacked LHS matrix.\n/// @param[in] k Number of columns in the unpacked LHS matrix.\n/// @param[in] mr Number of rows to be interleaved.\n/// @param[in] kr Unused. Must be 4.\n/// @param[in] sr Unused. Must be 1.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_lhs_packed_offset_lhs_pack_x8p2vlx4_x8_sme(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes of the packed LHS buffer.\n///\n/// @param[in] m Number of rows in the unpacked LHS matrix.\n/// @param[in] k Number of columns in the unpacked LHS matrix.\n/// @param[in] mr Number of rows to be interleaved.\n/// @param[in] kr Unused. Must be 4.\n/// @param[in] sr Unused. Must be 1.\n///\n/// @return The size in bytes of the packed LHS buffer.\nsize_t kai_get_lhs_packed_size_lhs_pack_x8p2vlx4_x8_sme(size_t m, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Runs the LHS packing function for matrix multiplication.\n///\n/// The pointer of each buffers (LHS and packed LHS) needs to be added with offset\n/// calculated using the following functions:\n///\n/// * LHS: @ref kai_get_lhs_offset_lhs_pack_x8p2vlx4_x8_sme.\n/// * Packed LHS: @ref kai_get_lhs_packed_offset_lhs_pack_x8p2vlx4_x8_sme.\n///\n/// @param[in] m Number of rows of the unpacked LHS matrix.\n/// @param[in] k Number of columns in the unpacked LHS matrix.\n/// @param[in] mr Block size in M dimension. It must be kai_get_m_step_lhs_pack_x8p2vlx4_x8_sme().\n/// @param[in] kr Block size in K dimension. It must be 4.\n/// @param[in] sr Number of kr splits. It must be 1.\n/// @param[in] m_idx_start Unused. Must be 0.\n/// @param[in] lhs LHS matrix data buffer.\n/// @param[in] lhs_stride_row Row stride in bytes of the LHS matrix.\n/// @param[out] lhs_packed Packed LHS matrix.\nvoid kai_run_lhs_pack_x8p2vlx4_x8_sme(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs, size_t lhs_stride_row,\n void* lhs_packed);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_pack_x8p2vlx4_x8_sme.c",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n#include \"kai_lhs_pack_x8p2vlx4_x8_sme.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nenum {\n MR = 2,\n KR = 4,\n MAX_M_STEP = MR * (KAI_SME_VEC_LENGTH_MAX_BYTES / sizeof(int8_t)) / KR,\n SR = 1,\n};\n\nvoid kai_kernel_lhs_pack_x8p2vlx4_x8_sme(size_t height, size_t width, const void* in, void* out);\n\nstatic size_t kai_get_mr_lhs_pack_x8p2vlx4_x8_sme(void) {\n return MR * kai_get_sme_vector_length_u8() / KR;\n}\n\nsize_t kai_get_m_step_lhs_pack_x8p2vlx4_x8_sme(size_t mr) {\n KAI_ASSUME(mr == kai_get_mr_lhs_pack_x8p2vlx4_x8_sme());\n KAI_UNUSED(mr);\n return kai_get_mr_lhs_pack_x8p2vlx4_x8_sme();\n}\n\nsize_t kai_get_lhs_offset_lhs_pack_x8p2vlx4_x8_sme(size_t m_idx, size_t lhs_stride_row) {\n KAI_ASSUME(m_idx % kai_get_mr_lhs_pack_x8p2vlx4_x8_sme() == 0);\n\n return m_idx * lhs_stride_row;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_pack_x8p2vlx4_x8_sme(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME(m_idx % kai_get_m_step_lhs_pack_x8p2vlx4_x8_sme(mr) == 0);\n KAI_ASSUME(mr == kai_get_mr_lhs_pack_x8p2vlx4_x8_sme());\n KAI_ASSUME(kr == KR);\n KAI_ASSUME(sr == SR);\n\n KAI_UNUSED(mr);\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n return m_idx * kai_roundup(k, KR) * sizeof(int8_t);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_pack_x8p2vlx4_x8_sme(size_t m, size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME(mr == kai_get_mr_lhs_pack_x8p2vlx4_x8_sme());\n KAI_ASSUME(kr == KR);\n KAI_ASSUME(sr == SR);\n\n KAI_UNUSED(mr);\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n return kai_roundup(m, kai_get_mr_lhs_pack_x8p2vlx4_x8_sme()) * kai_roundup(k, KR) * sizeof(int8_t);\n}\n\nvoid kai_run_lhs_pack_x8p2vlx4_x8_sme(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs, size_t lhs_stride_row,\n void* lhs_packed) {\n KAI_ASSUME(mr == kai_get_mr_lhs_pack_x8p2vlx4_x8_sme());\n KAI_ASSUME(kr == KR);\n KAI_ASSUME(sr == SR);\n KAI_ASSUME(m_idx_start == 0);\n KAI_ASSUME(lhs != NULL);\n KAI_ASSUME(lhs_packed != NULL);\n\n const size_t m_step = kai_get_mr_lhs_pack_x8p2vlx4_x8_sme();\n const size_t width = k;\n\n KAI_ASSERT(m_step <= MAX_M_STEP);\n const uint8_t* in[MAX_M_STEP];\n\n uint8_t* out_base = lhs_packed;\n const uint8_t* lhs_ptr = lhs;\n\n kai_commit_za();\n\n for (size_t i_m = 0; i_m < m; i_m += m_step) {\n const size_t height = KAI_MIN(m - i_m, m_step);\n void* out = out_base;\n out_base += m_step * kai_roundup(k, KR) * sizeof(int8_t);\n\n for (size_t y = 0; y < height; y++) {\n in[y] = lhs_ptr + (i_m + y) * lhs_stride_row;\n }\n\n kai_kernel_lhs_pack_x8p2vlx4_x8_sme(\n height, width, in, out); // NOLINT(bugprone-multi-level-implicit-pointer-conversion)\n }\n}\n\n#endif // Architectural features check.\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_lhs_pack_x8p2vlx4_x8_sme_asm.S",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(lhs_pack_x8p2vlx4_x8_sme)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_lhs_pack_x8p2vlx4_x8_sme)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_lhs_pack_x8p2vlx4_x8_sme)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_lhs_pack_x8p2vlx4_x8_sme)\n stp x20, x21, [sp, -144]!\n stp x22, x23, [sp, 16]\n stp x24, x25, [sp, 32]\n stp x26, x27, [sp, 48]\n str x28, [sp, 64]\n stp d8, d9, [sp, 72]\n stp d10, d11, [sp, 88]\n stp d12, d13, [sp, 104]\n stp d14, d15, [sp, 120]\n KAI_ASM_INST(0xd503477f) // SMSTART ZA\n mov x12, #0x0\n cntb x21\n mov x23, x1\n incb x23\n mov x20, x1\n sub x8, x21, #0x1\n cntw x17\n sub x23, x23, #0x1\n ands x8, x20, x8\n udiv x23, x23, x21 // n_passes = ceildiv(width, VL<T>)\n csel x8, x8, x21, NE\n lsl x22, x0, #0x1 // height * 2\n lsl x21, x17, #0x1\n sub x20, x23, #0x1\n add x8, x8, #0x3\n sub x16, x17, #0x2\n whilelt p9.b, XZR, x22\n whilelt p8.b, x21, x22\n mov x11, #0x0\n mov x10, x2\n add x9, x2, x17, LSL #3\n cntw x28, ALL, MUL #2\n cntw x27, ALL, MUL #3\n ldr x26, [x10, #0x0]\n lsr x20, x20, #0x1 // n_loops = (n_passes - 1) / 2\n and x25, x23, #0x1 // odd_tail = bool(n_passes & 0x1)\n ldr x24, [x9, #0x0]\n lsr x8, x8, #0x2\n ptrue p11.s\n ldr x23, [x10, #0x8]\n zip1 p10.b, p9.b, p8.b\n mov x22, x12\n ldr x21, [x9, #0x8]\n whilelt p9.b, x11, x1\n whilelt p8.b, x11, x1\n add x10, x10, #0x10\n add x9, x9, #0x10\n mov x12, #0x0\n cbz x16, label_2\nKAI_ASM_LABEL(label_1) // K loop: Charge: Loop\n KAI_ASM_INST(0x25246143) // psel p3.b, p8.b/Z, p10.b[w12]\n KAI_ASM_INST(0x252c6142) // psel p2.b, p8.b/Z, p10.b[w12, #1]\n KAI_ASM_INST(0x25646141) // psel p1.b, p8.b/Z, p10.b[w12, #4]\n KAI_ASM_INST(0x256c6140) // psel p0.b, p8.b/Z, p10.b[w12, #5]\n KAI_ASM_INST(0xe0160f40) // ld1b { za0h.b[x12] }, p3/Z, [x26, x22]\n ldr x26, [x10, #0x0]\n KAI_ASM_INST(0xe0160b01) // ld1b { za0h.b[x12, #1] }, p2/Z, [x24, x22]\n ldr x24, [x9, #0x0]\n KAI_ASM_INST(0xe01606e4) // ld1b { za0h.b[x12, #4] }, p1/Z, [x23, x22]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe01602a5) // ld1b { za0h.b[x12, #5] }, p0/Z, [x21, x22]\n add x12, x12, #0x8\n ldr x21, [x9, #0x8]\n add x9, x9, #0x10\n cmp x12, x16, LSL #2\n blt label_1\nKAI_ASM_LABEL(label_2) // K loop: Charge: End\n KAI_ASM_INST(0x25246143) // psel p3.b, p8.b/Z, p10.b[w12]\n KAI_ASM_INST(0x252c6142) // psel p2.b, p8.b/Z, p10.b[w12, #1]\n KAI_ASM_INST(0x25646141) // psel p1.b, p8.b/Z, p10.b[w12, #4]\n KAI_ASM_INST(0x256c6140) // psel p0.b, p8.b/Z, p10.b[w12, #5]\n mov x10, x2\n add x9, x2, x17, LSL #3\n KAI_ASM_INST(0xe0160f40) // ld1b { za0h.b[x12] }, p3/Z, [x26, x22]\n ldr x26, [x10, #0x0]\n incb x11\n KAI_ASM_INST(0xe0160b01) // ld1b { za0h.b[x12, #1] }, p2/Z, [x24, x22]\n ldr x24, [x9, #0x0]\n KAI_ASM_INST(0xe01606e4) // ld1b { za0h.b[x12, #4] }, p1/Z, [x23, x22]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe01602a5) // ld1b { za0h.b[x12, #5] }, p0/Z, [x21, x22]\n ldr x21, [x9, #0x8]\n add x9, x9, #0x10\n incb x22\n cbz x20, label_8\n mov x20, x20\nKAI_ASM_LABEL(label_3) // K loop: Main loop\n whilelt p8.b, x11, x1\n mov x15, #0x0\n mov x14, #0x0\n cbz x16, label_5\nKAI_ASM_LABEL(label_4) // K loop: Main loop: First: Loop\n KAI_ASM_INST(0x25376143) // psel p3.b, p8.b/Z, p10.b[w15, #2]\n KAI_ASM_INST(0x253f6142) // psel p2.b, p8.b/Z, p10.b[w15, #3]\n KAI_ASM_INST(0x25776141) // psel p1.b, p8.b/Z, p10.b[w15, #6]\n KAI_ASM_INST(0x257f6140) // psel p0.b, p8.b/Z, p10.b[w15, #7]\n KAI_ASM_INST(0xe0166f42) // ld1b { za0h.b[x15, #2] }, p3/Z, [x26, x22]\n KAI_ASM_INST(0x25266d23) // psel p3.b, p11.b/Z, p9.b[w14]\n ldr x26, [x10, #0x0]\n KAI_ASM_INST(0xe0166b03) // ld1b { za0h.b[x15, #3] }, p2/Z, [x24, x22]\n KAI_ASM_INST(0x25266d22) // psel p2.b, p11.b/Z, p9.b[w14]\n ldr x24, [x9, #0x0]\n KAI_ASM_INST(0xe01666e6) // ld1b { za0h.b[x15, #6] }, p1/Z, [x23, x22]\n KAI_ASM_INST(0x252e6d21) // psel p1.b, p11.b/Z, p9.b[w14, #1]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe01662a7) // ld1b { za0h.b[x15, #7] }, p0/Z, [x21, x22]\n ldr x21, [x9, #0x8]\n KAI_ASM_INST(0x252e6d20) // psel p0.b, p11.b/Z, p9.b[w14, #1]\n add x9, x9, #0x10\n KAI_ASM_INST(0xe0bfcc60) // st1w { za0v.s[x14] }, p3/Z, [x3, XZR, LSL #2]\n add x15, x15, #0x8\n KAI_ASM_INST(0xe0b1c864) // st1w { za1v.s[x14] }, p2/Z, [x3, x17, LSL #2]\n KAI_ASM_INST(0xe0bcc461) // st1w { za0v.s[x14, #1] }, p1/Z, [x3, x28, LSL #2]\n KAI_ASM_INST(0xe0bbc065) // st1w { za1v.s[x14, #1] }, p0/Z, [x3, x27, LSL #2]\n add x14, x14, #0x2\n addvl x3, x3, #4\n cmp x14, x16\n blt label_4\nKAI_ASM_LABEL(label_5) // K loop: Main loop: First: Tail\n KAI_ASM_INST(0x25376143) // psel p3.b, p8.b/Z, p10.b[w15, #2]\n KAI_ASM_INST(0x253f6142) // psel p2.b, p8.b/Z, p10.b[w15, #3]\n KAI_ASM_INST(0x25776141) // psel p1.b, p8.b/Z, p10.b[w15, #6]\n KAI_ASM_INST(0x257f6140) // psel p0.b, p8.b/Z, p10.b[w15, #7]\n mov x10, x2\n add x9, x2, x17, LSL #3\n KAI_ASM_INST(0xe0166f42) // ld1b { za0h.b[x15, #2] }, p3/Z, [x26, x22]\n KAI_ASM_INST(0x25266d23) // psel p3.b, p11.b/Z, p9.b[w14]\n ldr x26, [x10, #0x0]\n mov x13, #0x0\n KAI_ASM_INST(0xe0166b03) // ld1b { za0h.b[x15, #3] }, p2/Z, [x24, x22]\n KAI_ASM_INST(0x25266d22) // psel p2.b, p11.b/Z, p9.b[w14]\n ldr x24, [x9, #0x0]\n mov x12, #0x0\n KAI_ASM_INST(0xe01666e6) // ld1b { za0h.b[x15, #6] }, p1/Z, [x23, x22]\n KAI_ASM_INST(0x252e6d21) // psel p1.b, p11.b/Z, p9.b[w14, #1]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe01662a7) // ld1b { za0h.b[x15, #7] }, p0/Z, [x21, x22]\n ldr x21, [x9, #0x8]\n KAI_ASM_INST(0x252e6d20) // psel p0.b, p11.b/Z, p9.b[w14, #1]\n whilelt p9.b, x11, x1\n KAI_ASM_INST(0xe0bfcc60) // st1w { za0v.s[x14] }, p3/Z, [x3, XZR, LSL #2]\n incb x11\n add x9, x9, #0x10\n KAI_ASM_INST(0xe0b1c864) // st1w { za1v.s[x14] }, p2/Z, [x3, x17, LSL #2]\n incb x22\n whilelt p8.b, x11, x1\n KAI_ASM_INST(0xe0bcc461) // st1w { za0v.s[x14, #1] }, p1/Z, [x3, x28, LSL #2]\n KAI_ASM_INST(0xe0bbc065) // st1w { za1v.s[x14, #1] }, p0/Z, [x3, x27, LSL #2]\n addvl x3, x3, #4\n cbz x16, label_7\nKAI_ASM_LABEL(label_6) // K loop: Main loop: Second: Loop\n KAI_ASM_INST(0x25256143) // psel p3.b, p8.b/Z, p10.b[w13]\n KAI_ASM_INST(0x252d6142) // psel p2.b, p8.b/Z, p10.b[w13, #1]\n KAI_ASM_INST(0x25656141) // psel p1.b, p8.b/Z, p10.b[w13, #4]\n KAI_ASM_INST(0x256d6140) // psel p0.b, p8.b/Z, p10.b[w13, #5]\n KAI_ASM_INST(0xe0162f40) // ld1b { za0h.b[x13] }, p3/Z, [x26, x22]\n KAI_ASM_INST(0x25246d23) // psel p3.b, p11.b/Z, p9.b[w12]\n ldr x26, [x10, #0x0]\n KAI_ASM_INST(0xe0162b01) // ld1b { za0h.b[x13, #1] }, p2/Z, [x24, x22]\n KAI_ASM_INST(0x25246d22) // psel p2.b, p11.b/Z, p9.b[w12]\n ldr x24, [x9, #0x0]\n KAI_ASM_INST(0xe01626e4) // ld1b { za0h.b[x13, #4] }, p1/Z, [x23, x22]\n KAI_ASM_INST(0x252c6d21) // psel p1.b, p11.b/Z, p9.b[w12, #1]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe01622a5) // ld1b { za0h.b[x13, #5] }, p0/Z, [x21, x22]\n ldr x21, [x9, #0x8]\n KAI_ASM_INST(0x252c6d20) // psel p0.b, p11.b/Z, p9.b[w12, #1]\n add x9, x9, #0x10\n KAI_ASM_INST(0xe0bf8c68) // st1w { za2v.s[x12] }, p3/Z, [x3, XZR, LSL #2]\n add x13, x13, #0x8\n KAI_ASM_INST(0xe0b1886c) // st1w { za3v.s[x12] }, p2/Z, [x3, x17, LSL #2]\n KAI_ASM_INST(0xe0bc8469) // st1w { za2v.s[x12, #1] }, p1/Z, [x3, x28, LSL #2]\n KAI_ASM_INST(0xe0bb806d) // st1w { za3v.s[x12, #1] }, p0/Z, [x3, x27, LSL #2]\n add x12, x12, #0x2\n addvl x3, x3, #4\n cmp x12, x16\n blt label_6\nKAI_ASM_LABEL(label_7) // K loop: Main loop: Second: Tail\n KAI_ASM_INST(0x25256143) // psel p3.b, p8.b/Z, p10.b[w13]\n KAI_ASM_INST(0x252d6142) // psel p2.b, p8.b/Z, p10.b[w13, #1]\n KAI_ASM_INST(0x25656141) // psel p1.b, p8.b/Z, p10.b[w13, #4]\n KAI_ASM_INST(0x256d6140) // psel p0.b, p8.b/Z, p10.b[w13, #5]\n mov x10, x2\n add x9, x2, x17, LSL #3\n KAI_ASM_INST(0xe0162f40) // ld1b { za0h.b[x13] }, p3/Z, [x26, x22]\n KAI_ASM_INST(0x25246d23) // psel p3.b, p11.b/Z, p9.b[w12]\n ldr x26, [x10, #0x0]\n KAI_ASM_INST(0xe0162b01) // ld1b { za0h.b[x13, #1] }, p2/Z, [x24, x22]\n KAI_ASM_INST(0x25246d22) // psel p2.b, p11.b/Z, p9.b[w12]\n ldr x24, [x9, #0x0]\n KAI_ASM_INST(0xe01626e4) // ld1b { za0h.b[x13, #4] }, p1/Z, [x23, x22]\n KAI_ASM_INST(0x252c6d21) // psel p1.b, p11.b/Z, p9.b[w12, #1]\n ldr x23, [x10, #0x8]\n add x10, x10, #0x10\n KAI_ASM_INST(0xe01622a5) // ld1b { za0h.b[x13, #5] }, p0/Z, [x21, x22]\n ldr x21, [x9, #0x8]\n KAI_ASM_INST(0x252c6d20) // psel p0.b, p11.b/Z, p9.b[w12, #1]\n whilelt p9.b, x11, x1\n KAI_ASM_INST(0xe0bf8c68) // st1w { za2v.s[x12] }, p3/Z, [x3, XZR, LSL #2]\n subs x20, x20, #0x1\n add x9, x9, #0x10\n KAI_ASM_INST(0xe0b1886c) // st1w { za3v.s[x12] }, p2/Z, [x3, x17, LSL #2]\n incb x11\n incb x22\n KAI_ASM_INST(0xe0bc8469) // st1w { za2v.s[x12, #1] }, p1/Z, [x3, x28, LSL #2]\n KAI_ASM_INST(0xe0bb806d) // st1w { za3v.s[x12, #1] }, p0/Z, [x3, x27, LSL #2]\n addvl x3, x3, #4\n bgt label_3\nKAI_ASM_LABEL(label_8) // K loop: Tails\n cbnz x25, label_11\n mov x10, x2\n whilelt p8.b, x11, x1\n mov x13, #0x0\n mov x12, #0x0\nKAI_ASM_LABEL(label_9) // K loop: Tails: Even: First\n KAI_ASM_INST(0x25306d23) // psel p3.s, p11.s/Z, p9.s[w12]\n KAI_ASM_INST(0x25306d22) // psel p2.s, p11.s/Z, p9.s[w12]\n KAI_ASM_INST(0x25356141) // psel p1.b, p8.b/Z, p10.b[w13, #2]\n KAI_ASM_INST(0x253d6140) // psel p0.b, p8.b/Z, p10.b[w13, #3]\n KAI_ASM_INST(0xe0bf8c60) // st1w { za0v.s[x12] }, p3/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0xe0b18864) // st1w { za1v.s[x12] }, p2/Z, [x3, x17, LSL #2]\n add x12, x12, #0x1\n addvl x3, x3, #2\n ldr x21, [x10, #0x0]\n cmp x12, x17\n ldr x20, [x10, x17, LSL #0x3]\n add x10, x10, #0x8\n KAI_ASM_INST(0xe01626a2) // ld1b { za0h.b[x13, #2] }, p1/Z, [x21, x22]\n KAI_ASM_INST(0xe0162283) // ld1b { za0h.b[x13, #3] }, p0/Z, [x20, x22]\n add x13, x13, #0x4\n blt label_9\n whilelt p9.b, x11, x1\n whilelt p8.b, x11, x1\n mov x20, #0x0\n mov x12, #0x0\nKAI_ASM_LABEL(label_10) // K loop: Tails: Even: Second\n KAI_ASM_INST(0x25306d21) // psel p1.s, p11.s/Z, p9.s[w12]\n KAI_ASM_INST(0x25306d20) // psel p0.s, p11.s/Z, p9.s[w12]\n add x20, x20, #0x4\n KAI_ASM_INST(0xe0bf8468) // st1w { za2v.s[x12] }, p1/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0xe0b1806c) // st1w { za3v.s[x12] }, p0/Z, [x3, x17, LSL #2]\n add x12, x12, #0x1\n addvl x3, x3, #2\n cmp x12, x8\n blt label_10\n whilelt p8.b, x11, x1\n b label_13\nKAI_ASM_LABEL(label_11) // K loop: Tails: Odd\n mov x12, #0x0\nKAI_ASM_LABEL(label_12) // K loop: Tails: Odd: Loop\n KAI_ASM_INST(0x25306d21) // psel p1.s, p11.s/Z, p9.s[w12]\n KAI_ASM_INST(0x25306d20) // psel p0.s, p11.s/Z, p9.s[w12]\n KAI_ASM_INST(0xe0bf8460) // st1w { za0v.s[x12] }, p1/Z, [x3, XZR, LSL #2]\n KAI_ASM_INST(0xe0b18064) // st1w { za1v.s[x12] }, p0/Z, [x3, x17, LSL #2]\n add x12, x12, #0x1\n addvl x3, x3, #2\n cmp x12, x8\n blt label_12\nKAI_ASM_LABEL(label_13) // K loop: End\n KAI_ASM_INST(0xd503467f) // SMSTOP\n ldp x22, x23, [sp, 16]\n ldp x24, x25, [sp, 32]\n ldp x26, x27, [sp, 48]\n ldr x28, [sp, 64]\n ldp d8, d9, [sp, 72]\n ldp d10, d11, [sp, 88]\n ldp d12, d13, [sp, 104]\n ldp d14, d15, [sp, 120]\n ldp x20, x21, [sp], 144\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_lhs_pack_x8p2vlx4_x8_sme)\n\n KAI_ASM_END\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Gets n step value.\n///\n/// The starting column index must be divisible by `n_step`.\n///\n/// @return The n step value.\nsize_t kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(void);\n\n/// Gets the offset in bytes to the data element in the RHS matrix buffer.\n///\n/// @param[in] n_idx Column index. Must be divisible by `n_step`\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_rhs_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n_idx);\n\n/// Gets the offset in bytes to the data element in the bias buffer.\n///\n/// @param[in] n_idx Column index.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_bias_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n_idx);\n\n/// Gets row stride in bytes of the packed RHS matrix.\n///\n/// @param[in] k Number of rows.\n///\n/// @return Row stride in bytes.\nsize_t kai_get_rhs_packed_stride_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t k);\n\n/// Gets the offset in bytes to the data element in the scale buffer.\n///\n/// @param[in] n_idx Column index.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_scale_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n_idx);\n\n/// Gets the offset in bytes to the data element in the packed RHS buffer.\n///\n/// @param[in] n_idx Column index. Must be divisible by `n_step`\n/// @param[in] k Number of rows.\n///\n/// @return The offset in bytes to the data element.\nsize_t kai_get_rhs_packed_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n_idx, size_t k);\n\n/// Gets the size in bytes of the packed RHS buffer.\n///\n/// @param[in] n Number of columns.\n/// @param[in] k Number of rows.\n///\n/// @return The size in bytes of the packed RHS buffer.\nsize_t kai_get_rhs_packed_size_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n, size_t k);\n\n/// Runs the RHS packing function for matrix multiplication.\n///\n/// The pointer of each buffer (RHS, bias and packed RHS) needs to be added with offset\n/// calculated using the following functions:\n///\n/// * RHS: @ref kai_get_rhs_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme.\n/// * Bias: @ref kai_get_bias_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme.\n/// * Scale: @ref kai_get_scale_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme.\n/// * Output: @ref kai_get_rhs_packed_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme.\n///\n/// @param[in] num_groups Number of groups. It must be 1.\n/// @param[in] n Number of columns of the output matrix.\n/// @param[in] k Number of rows.\n/// @param[in] nr Block size in N dimension. It must be `get_n_step`\n/// @param[in] kr Block size in K dimension. It must be 4.\n/// @param[in] sr Number of kr splits. It must be 1.\n/// @param[in] rhs_stride_row Row stride in bytes of the RHS matrix.\n/// @param[in] rhs RHS matrix data buffer.\n/// @param[in] bias Bias matrix data buffer.\n/// @param[in] scale Scale data buffer.\n/// @param[out] rhs_packed Packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix. It must be 0.\n/// @param[in] params Extra quantization packing parameters.\nvoid kai_run_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t rhs_stride_row, const void* rhs,\n const void* bias, const void* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_qsi8cx_params* params);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "kai_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme.c",
|
| 65 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n\n#include \"kai_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nenum {\n NR = 2,\n KR = 4,\n MAX_N_STEP = NR * ((KAI_SME_VEC_LENGTH_MAX_BYTES / sizeof(uint8_t)) / KR),\n};\n\ntypedef struct {\n const void* bias_ptr;\n const void* scale_ptr;\n int32_t input_zero_point;\n float scale_multiplier;\n size_t width;\n size_t height;\n size_t in_stride;\n size_t out_stride;\n const void* in;\n void* out;\n const void* pad_row;\n} KernelArgs;\n\nstatic const size_t kai_num_bytes_input = sizeof(uint8_t);\nstatic const size_t kai_num_bytes_output = sizeof(uint8_t);\nstatic const size_t kai_num_bytes_bias = sizeof(int32_t);\nstatic const size_t kai_num_bytes_scale = sizeof(float);\n\nvoid kai_kernel_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(const KernelArgs* args_ptr);\n\nsize_t kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(void) {\n return NR * kai_get_sme_vector_length_u8() / KR;\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n_idx) {\n KAI_ASSUME(n_idx % kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme() == 0);\n\n return n_idx * kai_num_bytes_input;\n}\n\nsize_t kai_get_bias_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n_idx) {\n return n_idx * kai_num_bytes_bias;\n}\n\nsize_t kai_get_scale_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n_idx) {\n return n_idx * kai_num_bytes_scale;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t k) {\n return kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme() *\n (kai_num_bytes_bias + kai_roundup(k, KR) * kai_num_bytes_output + kai_num_bytes_scale);\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n_idx, size_t k) {\n KAI_ASSUME(n_idx % kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme() == 0);\n\n const size_t block_idx = n_idx / kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme();\n return block_idx * kai_get_rhs_packed_stride_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(k);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(size_t n, size_t k) {\n const size_t n_nr_blocks = kai_roundup(n, kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme());\n return kai_get_rhs_packed_offset_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(n_nr_blocks, k);\n}\n\nvoid kai_run_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t rhs_stride_row, const void* rhs,\n const void* bias, const void* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_qsi8cx_params* params) {\n KAI_ASSUME(num_groups == 1);\n KAI_ASSUME(nr == kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme());\n KAI_ASSUME(kr == KR);\n KAI_ASSUME(sr == 1);\n KAI_ASSUME(rhs != NULL);\n KAI_ASSUME(bias != NULL);\n KAI_ASSUME(scale != NULL);\n KAI_ASSUME(rhs_packed != NULL);\n KAI_ASSUME(extra_bytes == 0);\n KAI_ASSUME(params != NULL);\n\n KAI_ASSERT(kai_get_n_step_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme() <= MAX_N_STEP);\n static const uint8_t pad_row[MAX_N_STEP] = {0};\n\n KernelArgs args;\n args.bias_ptr = bias;\n args.scale_ptr = scale;\n args.height = k;\n args.width = n;\n args.in = rhs;\n args.out = rhs_packed;\n args.in_stride = rhs_stride_row;\n args.out_stride = kai_get_rhs_packed_stride_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(args.height);\n args.input_zero_point = params->lhs_zero_point;\n args.scale_multiplier = params->scale_multiplier;\n args.pad_row = pad_row;\n\n kai_commit_za();\n\n kai_kernel_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"path": "kai_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme_asm.S",
|
| 69 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme)\n stp x20, x21, [sp, -144]!\n stp x22, x23, [sp, 16]\n stp x24, x25, [sp, 32]\n stp x26, x27, [sp, 48]\n str x28, [sp, 64]\n stp d8, d9, [sp, 72]\n stp d10, d11, [sp, 88]\n stp d12, d13, [sp, 104]\n stp d14, d15, [sp, 120]\n KAI_ASM_INST(0xd503477f) // SMSTART ZA\n ldr x4, [x0, #0x20]\n ptrue p2.b\n ldr x5, [x0, #0x40]\n ldr x6, [x0, #0x0]\n mov x7, x4\n ldr x8, [x0, #0x8]\n cmp x7, #0x8\n ldr w17, [x0, #0x10]\n mov x16, x5\n ldr w15, [x0, #0x14]\n incb x5, ALL, MUL #2\n ldr x14, [x0, #0x18]\n ldr x13, [x0, #0x28]\n ldr x12, [x0, #0x30]\n ldr x11, [x0, #0x38]\n ldr x10, [x0, #0x48]\n blt label_4\nKAI_ASM_LABEL(label_1) // Main row loop: Head\n mov x9, x11\n mov x28, x5\n add x27, x9, x13\n sub x7, x7, #0x8\n add x26, x27, x13\n mov x24, x14\n add x25, x26, x13\n add x23, x25, x13\n add x22, x23, x13\n add x21, x22, x13\n add x20, x21, x13\n add x11, x20, x13\nKAI_ASM_LABEL(label_2) // Main row loop: Column loop\n whilelt p0.b, XZR, x24\n decw x24, ALL, MUL #2\n ld1b { z18.b }, p0/Z, [x9]\n cmp x24, #0x0\n incd x9, ALL, MUL #4\n ld1b { z22.b }, p0/Z, [x27]\n incd x27, ALL, MUL #4\n ld1b { z17.b }, p0/Z, [x26]\n incd x26, ALL, MUL #4\n ld1b { z16.b }, p0/Z, [x25]\n incd x25, ALL, MUL #4\n ld1b { z20.b }, p0/Z, [x23]\n incd x23, ALL, MUL #4\n ld1b { z19.b }, p0/Z, [x22]\n zip1 z21.b, z18.b, z17.b\n incd x22, ALL, MUL #4\n ld1b { z18.b }, p0/Z, [x21]\n zip1 z17.b, z22.b, z16.b\n incd x21, ALL, MUL #4\n ld1b { z16.b }, p0/Z, [x20]\n incd x20, ALL, MUL #4\n zip1 z20.b, z20.b, z18.b\n zip1 z16.b, z19.b, z16.b\n zip1 z19.b, z21.b, z17.b\n zip2 z18.b, z21.b, z17.b\n zip1 z17.b, z20.b, z16.b\n zip2 z16.b, z20.b, z16.b\n st1b { z19.b }, p2, [x28]\n st1b { z18.b }, p2, [x28, #1, MUL VL]\n st1b { z17.b }, p2, [x28, #2, MUL VL]\n st1b { z16.b }, p2, [x28, #3, MUL VL]\n add x28, x28, x12\n bgt label_2\n cmp x7, #0x8\n addvl x5, x5, #4\n bge label_1\n cbz x7, label_8\nKAI_ASM_LABEL(label_4) // Main loop skip\nKAI_ASM_LABEL(label_5) // Tail row loop: Head\n mov x9, x11\n cmp x7, #0x3\n add x27, x9, x13\n cntw x24, ALL, MUL #2\n add x26, x27, x13\n csel x23, x24, XZR, GT\n add x25, x26, x13\n csel x22, x24, XZR, GE\n add x11, x25, x13\n mov x28, x5\n csel x11, x11, x25, GT\n csel x25, x25, x10, GT\n csel x11, x11, x26, GE\n csel x26, x26, x10, GE\n cmp x7, #0x1\n sub x7, x7, #0x4\n csel x11, x11, x27, GT\n csel x27, x27, x10, GT\n csel x21, x24, XZR, GT\n mov x20, x14\nKAI_ASM_LABEL(label_6) // Tail row loop: Column loop\n whilelt p0.b, XZR, x20\n decw x20, ALL, MUL #2\n ld1b { z18.b }, p0/Z, [x9]\n cmp x20, #0x0\n add x9, x9, x24\n ld1b { z19.b }, p0/Z, [x27]\n add x27, x27, x21\n ld1b { z17.b }, p0/Z, [x26]\n add x26, x26, x22\n ld1b { z16.b }, p0/Z, [x25]\n add x25, x25, x23\n zip1 z18.b, z18.b, z17.b\n zip1 z16.b, z19.b, z16.b\n zip1 z17.b, z18.b, z16.b\n zip2 z16.b, z18.b, z16.b\n st1b { z17.b }, p2, [x28]\n st1b { z16.b }, p2, [x28, #1, MUL VL]\n add x28, x28, x12\n bgt label_6\n cmp x7, #0x1\n addvl x5, x5, #2\n bge label_5\nKAI_ASM_LABEL(label_8) // Done\n mov x22, x5\n mov x21, x14\n dup z18.s, w15\n cbz x8, label_10\nKAI_ASM_LABEL(label_9) // Scale: Full loop\n mov x20, x21\n decw x21, ALL, MUL #2\n whilelt p1.s, XZR, x20\n decw x20\n whilelt p0.s, XZR, x20\n ld1w { z17.s }, p1/Z, [x8]\n cmp x21, #0x0\n ld1w { z16.s }, p0/Z, [x8, #1, MUL VL]\n incb x8, ALL, MUL #2\n fmul z17.s, z17.s, z18.s\n fmul z16.s, z16.s, z18.s\n st1w { z17.s }, p2, [x22]\n st1w { z16.s }, p2, [x22, #1, MUL VL]\n add x22, x22, x12\n bgt label_9\nKAI_ASM_LABEL(label_10) // Scale: Done\n cbz x14, label_13\n cbz x4, label_13\n dup z21.s, w17\n add x25, x4, #0x3\n cntw x24, ALL, MUL #2\n mov z20.b, #0x1\n lsr x25, x25, #0x2\n mov x23, x14\n addvl x22, x16, #2\n neg z21.s, p2/M, z21.s\nKAI_ASM_LABEL(label_11) // Bias: N loop\n mov x21, x22\n mov x20, x25\n mov z19.s, #0x0\n mov z18.s, #0x0\nKAI_ASM_LABEL(label_12) // Bias: K loop\n ld1b { z17.b }, p2/Z, [x21]\n subs x20, x20, #0x1\n ld1b { z16.b }, p2/Z, [x21, #1, MUL VL]\n addvl x21, x21, #2\n sdot z19.s, z17.b, z20.b\n sdot z18.s, z16.b, z20.b\n bgt label_12\n mov x20, x23\n add x22, x22, x12\n whilelt p1.s, XZR, x20\n decw x20\n whilelt p0.s, XZR, x20\n ld1w { z17.s }, p1/Z, [x6]\n subs x23, x23, x24\n ld1w { z16.s }, p0/Z, [x6, #1, MUL VL]\n addvl x6, x6, #2\n mla z17.s, p2/M, z19.s, z21.s\n mla z16.s, p2/M, z18.s, z21.s\n st1w { z17.s }, p2, [x16]\n st1w { z16.s }, p2, [x16, #1, MUL VL]\n add x16, x16, x12\n bgt label_11\nKAI_ASM_LABEL(label_13) // Bias: Done\n KAI_ASM_INST(0xd503467f) // SMSTOP\n ldp x22, x23, [sp, 16]\n ldp x24, x25, [sp, 32]\n ldp x26, x27, [sp, 48]\n ldr x28, [sp, 64]\n ldp d8, d9, [sp, 72]\n ldp d10, d11, [sp, 88]\n ldp d12, d13, [sp, 104]\n ldp d14, d15, [sp, 120]\n ldp x20, x21, [sp], 144\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme)\n\n KAI_ASM_END\n"
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"path": "binding.cpp",
|
| 73 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa against gemm_qai8_qsi8cx_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const int8_t* A, float* output, const float* A_scale, const int8_t* B_q8, const float* B_scales, const float* bias, const float* C_scale, int M)\n//\n// Statically-quantized int8 GEMM. The kernel computes, per output element,\n// dst = clamp(round_to_nearest_even((acc_i32 + bias_i32[n]) * scale[n] * scale_multiplier) + out_zp, min, max)\n// so the definition's float quantities are folded into the kernel's parameters:\n// scale[n] = B_scales[n]\n// scale_multiplier = A_scale[0] / C_scale[0]\n// bias_i32[n] = rint(bias[n] / (A_scale[0] * B_scales[n])) (bias expressed in accumulator units)\n// out_zp = 0, lhs_zp = 0, clamp = [-128, 127]\n// bias is therefore quantized to the accumulator grid; the definition's tolerance (abs_tol 1 LSB of the int8\n// output) covers that rounding and round-half-even ties.\n// The kernel's weight-side packer only has a kxn variant, so B_q8 [N,K] is transposed to [K,N] once per weight.\n#include <arm_neon.h>\n#include <cmath>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa.h\"\n#include \"kai_lhs_pack_x8p2vlx4_x8_sme.h\"\n#include \"kai_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const int8_t* A, float* output, const float* A_scale, const int8_t* B_q8, const float* B_scales, const float* bias, const float* C_scale, int M)\n{\n const size_t mr = kai_get_mr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa();\n\n // LHS: the int8 activation is (re)packed on EVERY call into a grow-only scratch buffer.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t sz = kai_get_lhs_packed_size_lhs_pack_x8p2vlx4_x8_sme((size_t)M, (size_t)K, mr, kr, sr);\n if (sz > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(sz);\n lhs_cap = sz;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_pack_x8p2vlx4_x8_sme((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K, lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged. A_scale and C_scale only\n // enter through scale_multiplier, so they are part of the cache key too.\n static uint8_t* B_snap = nullptr; static size_t B_snap_size = 0;\n static uint8_t* Bs_snap = nullptr; static size_t Bs_snap_size = 0;\n static uint8_t* bias_snap = nullptr; static size_t bias_snap_size = 0;\n static uint8_t* As_snap = nullptr; static size_t As_snap_size = 0;\n static uint8_t* Cs_snap = nullptr; static size_t Cs_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_snap, B_snap_size, B_q8, (size_t)N * K)\n | refresh_cache(Bs_snap, Bs_snap_size, B_scales, sizeof(float) * N)\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float) * N)\n | refresh_cache(As_snap, As_snap_size, A_scale, sizeof(float))\n | refresh_cache(Cs_snap, Cs_snap_size, C_scale, sizeof(float));\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_size = kai_get_rhs_packed_size_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme((size_t)N, (size_t)K);\n rhs_packed = std::malloc(rhs_size);\n int8_t* b_kxn = static_cast<int8_t*>(std::malloc((size_t)N * K));\n int32_t* bias_i32 = static_cast<int32_t*>(std::malloc(sizeof(int32_t) * N));\n if (rhs_packed == nullptr || b_kxn == nullptr || bias_i32 == nullptr) {\n std::free(b_kxn);\n std::free(bias_i32);\n return -1;\n }\n for (int n = 0; n < N; ++n) {\n for (int k = 0; k < K; ++k) b_kxn[(size_t)k * N + n] = B_q8[(size_t)n * K + k];\n bias_i32[n] = (int32_t)std::nearbyintf(bias[n] / (A_scale[0] * B_scales[n]));\n }\n struct kai_rhs_pack_qsi8cx_params params;\n params.lhs_zero_point = 0;\n params.scale_multiplier = A_scale[0] / C_scale[0];\n kai_run_rhs_pack_kxn_qsi8cxp2vlx4sb_qs8cx_f32_i32_sme(\n 1, (size_t)N, (size_t)K, nr, kr, sr, (size_t)N, b_kxn, bias_i32, B_scales, rhs_packed, 0, ¶ms);\n std::free(b_kxn);\n std::free(bias_i32);\n }\n\n // int8 result into a grow-only scratch, then widened to the raw ABI's float32 output (exact).\n static int8_t* out8 = nullptr;\n static size_t out8_cap = 0;\n const size_t total = (size_t)M * N;\n if (total > out8_cap) {\n std::free(out8);\n out8 = static_cast<int8_t*>(std::malloc(total));\n out8_cap = total;\n if (out8 == nullptr) return -1;\n }\n struct kai_matmul_requantize32_params rq;\n rq.min_value = -128;\n rq.max_value = 127;\n rq.output_zero_point = 0;\n kai_run_matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa(\n (size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, out8, (size_t)N * sizeof(int8_t), sizeof(int8_t), &rq);\n\n size_t i = 0;\n for (; i + 8 <= total; i += 8) {\n const int16x8_t w = vmovl_s8(vld1_s8(out8 + i));\n vst1q_f32(output + i, vcvtq_f32_s32(vmovl_s16(vget_low_s16(w))));\n vst1q_f32(output + i + 4, vcvtq_f32_s32(vmovl_s16(vget_high_s16(w))));\n }\n for (; i < total; ++i) output[i] = (float)out8[i];\n return 0;\n}\n"
|
| 74 |
+
}
|
| 75 |
+
]
|
| 76 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qai8dxp_qs4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qai8dxp_qs4c32_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_qai8dxp_qs4c32_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qai8dxp_qs4c32_sme2_n256_k512: KleidiAI kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa with its qai8dxp LHS and qsi4c32p RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.h",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# kai_lhs_quant_pack_qai8dxp_f32 to dynamically quantize and pack the LHS matrix\n/// -# kai_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon and kai_rhs_pack_kxn_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon\n/// to pack the RHS matrix\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix with\n/// the @ref kai_lhs_quant_pack_qai8dxp_f32 micro-kernel\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix with\n/// the @ref kai_run_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon micro-kernel\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the RHS matrix with\n/// the @ref kai_run_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon micro-kernel\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the RHS matrix with\n/// the @ref kai_run_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon micro-kernel\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qai8dxp) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(size_t m_idx, size_t k);\n\n/**\n * Gets the offset in bytes for the packed RHS matrix,\n * which contains the packed 4-bit quantized symmetric per-block (32) values (qsi4c32p).\n *\n * @param[in] n_idx Row index in the RHS matrix (not packed).\n * @param[in] k The common dimension between the LHS and RHS matrix (K).\n * @param[in] bl Block length along K. It must be a multiple of 32, and K must be a multiple of bl.\n *\n * @return the offset in bytes to the packed RHS matrix\n */\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t n_idx, size_t k, size_t bl);\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix.\n/// @param[in] n_idx Column index in the DST matrix.\n/// @param[in] dst_stride The number of bytes in each row of the DST matrix\n///\n/// @return the destination(DST) offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride);\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n);\n\n///\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Signed 8-bit quantized asymmetric per-row (qai8dxp) and packed\n/// RHS matrix: Signed 4-bit quantized symmetric per-block (32) (qsi4c32p) and packed.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] bl Block length along K. It must be a multiple of 32, and K must be a multiple of bl.\n/// @param[in] lhs_packed The LHS packed matrix.\n/// When the activation are dynamically quantized, you can obtain this matrix\n/// by calling the @ref kai_lhs_quant_pack_qai8dxp_f32 micro-kernel which performs\n/// both the dynamic quantization to 8-bit and activation packing in a single step.\n/// @param[in] rhs_packed The RHS packed matrix, which is obtained by calling @ref\n/// kai_run_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float).\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, size_t n, size_t k, size_t bl, const void* lhs_packed, const void* rhs_packed, float* dst,\n size_t dst_stride_row, size_t dst_stride_col, float scalar_min, float scalar_max);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.c",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#define KAI_LUT_NENTRIES 64\n\n/// Lut to be indexed by i4 resulting in its value in i8 (i.e. -2 = 1110 -> 1111 1110).\nstatic const int8_t lut[KAI_LUT_NENTRIES] = {\n // clang-format off\n 0, 0, 0, 0,\n 1, 0, 0, 0,\n 2, 0, 0, 0,\n 3, 0, 0, 0,\n 4, 0, 0, 0,\n 5, 0, 0, 0,\n 6, 0, 0, 0,\n 7, 0, 0, 0,\n -8, 0, 0, 0,\n -7, 0, 0, 0,\n -6, 0, 0, 0,\n -5, 0, 0, 0,\n -4, 0, 0, 0,\n -3, 0, 0, 0,\n -2, 0, 0, 0,\n -1, 0, 0, 0\n // clang-format on\n};\n\ntypedef struct {\n float* dst; // 0 (0x00)\n const void* lhs_packed; // 8 (0x08)\n const void* rhs_packed; // 16 (0x10)\n size_t dst_stride_row; // 24 (0x18)\n size_t lhs_stride; // 32 (0x20)\n size_t rhs_stride; // 40 (0x28)\n size_t m; // 48 (0x30)\n size_t n; // 56 (0x38)\n size_t k; // 64 (0x40)\n size_t bl; // 72 (0x48)\n const int8_t* lut; // 80 (0x50)\n float scalar_max; // 88 (0x58)\n float scalar_min; // 92 (0x5C)\n} KernelArgs;\n\nextern void kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(KernelArgs* args_ptr);\n\n// Compute args\nstatic const size_t kai_m_step = 1; // multiple of vector length\nstatic const size_t kai_n_step = 4; // multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // multiple of vector length\nstatic const size_t kai_nr = 4; // multiple of vector length\nstatic const size_t kai_kr = 8;\nstatic const size_t kai_sr = 2;\n// LHS format args\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = sizeof(float);\nstatic const size_t kai_num_bytes_offset_lhs = sizeof(int32_t);\n// RHS format args\nstatic const size_t kai_num_bytes_recip_qvalue_rhs = 2;\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(uint16_t);\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias_rhs = sizeof(float);\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_k_multiple_of = 32;\nstatic const size_t kai_bl = 32;\n\nstatic size_t kai_k_roundedup(const size_t k) {\n // Round up k to be a multiple of 32.\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nstatic size_t kai_get_num_bytes_per_block_rhs(const size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n const size_t num_bytes_per_block_rhs = (bl / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs;\n return num_bytes_per_block_rhs;\n}\n\nstatic size_t kai_get_num_blocks_per_row(const size_t k, const size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n return kai_roundup(k, bl) / bl;\n}\n\nstatic size_t kai_get_lhs_packed_stride(const size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n return mr * (k_internal * kai_num_bytes_qvalue_lhs + kai_num_bytes_multiplier_lhs + kai_num_bytes_offset_lhs);\n}\n\nstatic size_t kai_get_rhs_packed_stride(const size_t k, const size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n KAI_ASSUME((k % kai_bl) == 0);\n\n const size_t k_internal = kai_k_roundedup(k);\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k_internal, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n size_t rhs_packed_stride = nr * (num_blocks_per_row * num_bytes_per_block);\n rhs_packed_stride += nr * kai_num_bytes_sum_rhs; // per-column rsum\n rhs_packed_stride += nr * kai_num_bytes_bias_rhs; // per-column bias\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(size_t m_idx, size_t k) {\n KAI_ASSUME((m_idx % kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa()) == 0);\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n return (m_idx / mr) * kai_get_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n const size_t n_idx, const size_t k, const size_t bl) {\n KAI_ASSUME((n_idx % kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa()) == 0);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n return (n_idx / nr) * kai_get_rhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n const size_t m_idx, const size_t n_idx, const size_t dst_stride) {\n KAI_ASSUME((m_idx % kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa()) == 0);\n KAI_ASSUME((n_idx % kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa()) == 0);\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(const size_t m, const size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n const size_t m, //\n const size_t n, //\n const size_t k, //\n const size_t bl, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n const size_t dst_stride_row, //\n const size_t dst_stride_col, //\n const float scalar_min, //\n const float scalar_max) {\n KAI_ASSUME(dst_stride_col == sizeof(float));\n KAI_ASSUME(m > 0);\n KAI_ASSUME(n > 0);\n KAI_ASSUME(k > 0);\n KAI_ASSUME((bl % kai_k_multiple_of) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n KernelArgs args;\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.dst_stride_row = dst_stride_row;\n args.m = m;\n args.n = n;\n args.k = kai_k_roundedup(k);\n args.bl = bl;\n args.lhs_stride = kai_get_lhs_packed_stride(k);\n args.rhs_stride = kai_get_rhs_packed_stride(k, bl);\n args.scalar_max = scalar_max;\n args.scalar_min = scalar_min;\n args.lut = lut;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(&args);\n}\n\n#endif // Architectural feature check\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa_asm.S",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa)\n\n // -----------------------------------------------------------------------------\n // Args (x0 = args_ptr)\n // 0 (0x00)\n // 8 (0x08)\n // 16 (0x10)\n // 24 (0x18)\n // 32 (0x20)\n // 40 (0x28)\n // 48 (0x30)\n // 56 (0x38)\n // 64 (0x40)\n // 72 (0x48)\n // 80 (0x50)\n // 88 (0x58)\n // 92 (0x5C)\n // -----------------------------------------------------------------------------\n // GPR usage\n // x0/x16 args_ptr\n // x19 dst base\n // x20 lhs_packed\n // x8/x11 rhs_packed\n // x6 k\n // x7 lut_ptr (\u2192 zt0)\n // x12 m loop counter\n // x13 n loop counter\n // x14/x15 row loop index / bound\n // x9/x17 dst working ptrs\n // x21\u2013x22 store pointers\n // x25 k-loop counter\n // x5 stride / bl loads\n // x23\u2013x28 saved / scratch regs\n // -----------------------------------------------------------------------------\n // Z / ZA usage\n // zt0 lut descriptor\n // z0\u2013z1 rhs nibble inputs\n // z4 lhs bytes\n // z8\u2013z11 rhs expanded bytes\n // za0\u2013za3 accumulators\n // z12\u2013z15 ZA extracts\n // z18\u2013z19 scalar_min / scalar_max\n // z24\u2013z27 rhs scales or temps\n // z28\u2013z31 post-accum results\n // -----------------------------------------------------------------------------\n // Predicates\n // p0.s m active\n // p2.b all true\n // pn8.h n active (vlx2)\n // pn9.s n active (vlx4)\n // -----------------------------------------------------------------------------\n // Loop structure\n // m_loop: M tiles (cntw VL)\n // n_loop: N tiles (vlx4)\n // k_loop: K in blocks of 'bl'\n // zero ZA\n // ld1w LHS, ld1h RHS, luti4 expand\n // smopa accumulate\n // load RHS scale (lsl#16)\n // extract ZA \u2192 f32\n // scale, bias, clamp, store\n // -----------------------------------------------------------------------------\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa)\n stp x19, x20, [sp, #-144]!\n stp x21, x22, [sp, #16]\n stp x23, x24, [sp, #32]\n stp x25, x26, [sp, #112]\n stp x27, x28, [sp, #128]\n stp d8, d9, [sp, #48]\n stp d10, d11, [sp, #64]\n stp d12, d13, [sp, #80]\n stp d14, d15, [sp, #96]\n\n mov x16, x0 // args_ptr\n\n KAI_ASM_INST(0xd503477f) // smstart\n mov x16, x0 // save args_ptr\n ldr x19, [x16, #0x00] // dst\n ldr x6, [x16, #0x40] // k\n ldr x20, [x16, #0x08] // lhs_packed\n ldr x7, [x16, #0x50] // lut\n KAI_ASM_INST(0xe11f80e0) // ldr zt0, [x7]\n cntw x7 // m_step = VL\n ptrue p2.b\n KAI_ASM_INST(0x8557ca12) // ld1rw {z18.s}, p2/z, [x16, #0x5C] - scalar_min\n KAI_ASM_INST(0x8556ca13) // ld1rw {z19.s}, p2/z, [x16, #0x58] - scalar_max\n\n ldr x12, [x16, #0x30] // M loop count\n KAI_ASM_INST(0x25ac17e0) // whilelt p0.s, xzr, x12\nKAI_ASM_LABEL(m_loop)\n ldr x8, [x16, #0x10] // rhs_packed\n mov x9, x19 // working dst\n ldr x13, [x16, #0x38] // N loop count\n cmp x7, x12 // m_step vs M loop count\n csel x15, x7, x12, lt //\n lsl x15, x15, #2 // rows in this M block * 4 ( loop bound for row)\n\n KAI_ASM_INST(0x256d47f0) // whilelt pn8.h, xzr, x13, vlx2\nKAI_ASM_LABEL(n_loop)\n mov x10, x20 // lhs_ptr\n mov x11, x8 // rhs_ptr\n mov x17, x9 // dst_ptr\n KAI_ASM_INST(0x25ad67f1) // whilelt pn9.s, xzr, x13, vlx4\n mov x25, x6 // k-loop counter\n\nKAI_ASM_LABEL(k_loop)\n KAI_ASM_INST(0xc00800ff) // zero {za}\n ldr x5, [x16, #0x48] // bl\nKAI_ASM_LABEL(k_block_loop)\n KAI_ASM_INST(0xa540a144) // ld1w { z4.s }, p0/z, [x10]\n addvl x10, x10, #1\n KAI_ASM_INST(0xa0402160) // ld1h { z0.h-z1.h }, pn8/z, [x11]\n addvl x11, x11, #2\n KAI_ASM_INST(0xc08a4008) // luti4 { z8.b - z9.b }, zt0, z0[0]\n KAI_ASM_INST(0xc08a402a) // luti4 { z10.b - z11.b }, zt0, z1[0]\n KAI_ASM_INST(0xa0884880) // smopa za0.s, p2/m, p2/m, z4.b, z8.b\n KAI_ASM_INST(0xa0894881) // smopa za1.s, p2/m, p2/m, z4.b, z9.b\n KAI_ASM_INST(0xa08a4882) // smopa za2.s, p2/m, p2/m, z4.b, z10.b\n KAI_ASM_INST(0xa08b4883) // smopa za3.s, p2/m, p2/m, z4.b, z11.b\n subs x5, x5, #4\n b.gt k_block_loop\n\n // Load per-output-vector RHS fixed-point scales (s16), then lsl #16 \u2192 Q15 in s32 lanes\n // Used to scale float accumulators after scvtf.\n ld1h z24.s, p2/z, [x11]\n ld1h z25.s, p2/z, [x11, #1, MUL VL]\n ld1h z26.s, p2/z, [x11, #2, MUL VL]\n ld1h z27.s, p2/z, [x11, #3, MUL VL]\n addvl x11, x11, #2 // advance rhs_ptr past 4 halfword vectors (scales)\n\n\n mov x14, #0 // store loop index\n mov x22, x17 // dst store pointer\n lsl z24.s, z24.s, #16\n lsl z25.s, z25.s, #16\n lsl z26.s, z26.s, #16\n lsl z27.s, z27.s, #16\n\nKAI_ASM_LABEL(accum_loop)\n // Extract 4 output vectors from ZA row w14 (low half), map to N columns 0..3\n // of this tile\n KAI_ASM_INST(0xc006440c) // mova { z12.b-z15.b }, za0h.b[w14, 0:3]\n add x14, x14, #4\n KAI_ASM_INST(0xc132e18c) // scvtf { z12.s-z15.s }, { z12.s-z15.s }\n\n // First K tile? (x25==x6 only on the first tile). If yes: write = accum * scale.\n // Else: C += accum * scale (FMLA into previous partial result).\n cmp x25, x6\n b.ne accum_fallback\n\n fmul z28.s, z12.s, z24.s\n fmul z29.s, z13.s, z25.s\n fmul z30.s, z14.s, z26.s\n fmul z31.s, z15.s, z27.s\n b post_accum\n\nKAI_ASM_LABEL(accum_fallback)\n KAI_ASM_INST(0xa040c6dc) // ld1w { z28.s - z31.s }, pn9/z, [x22]\n fmla z28.s, p2/m, z12.s, z24.s\n fmla z29.s, p2/m, z13.s, z25.s\n fmla z30.s, p2/m, z14.s, z26.s\n fmla z31.s, p2/m, z15.s, z27.s\n\nKAI_ASM_LABEL(post_accum)\n KAI_ASM_INST(0xa060c6dc) // st1w { z28.s - z31.s }, pn9, [x22]\n ldr x5, [x16, #0x18] // dst_stride_row\n add x22, x22, x5\n cmp x14, x15 // M tile loop: x15 = rows*4, x14 += 4 per row\n b.lt accum_loop\n ldr x5, [x16, #0x48] // bl\n subs x25, x25, x5\n b.gt k_loop\n\n // RHS row sums and bias\n KAI_ASM_INST(0xa040c560) // ld1w { z0.s-z3.s }, pn9/z, [x11]\n KAI_ASM_INST(0xa041c568) // ld1w { z8.s-z11.s }, pn9/z, [x11, #4, mul vl]\n addvl x11, x11, #8\n\n mov x14, #0\n addvl x21, x10, #1\n\nKAI_ASM_LABEL(postprocess_loop)\n // Load LHS row offset and scale factor\n KAI_ASM_INST(0x8540c950) // ld1rw {z16.s}, p2/z, [x10]\n KAI_ASM_INST(0x8540cab1) // ld1rw {z17.s}, p2/z, [x21]\n add x10, x10, #4\n add x21, x21, #4\n scvtf z16.s, p2/m, z16.s\n\n KAI_ASM_INST(0xa040c63c) // ld1w { z28.s - z31.s }, pn9/z, [x17]\n // facc + offset * row sum\n fmla z28.s, p2/m, z16.s, z0.s\n fmla z29.s, p2/m, z16.s, z1.s\n fmla z30.s, p2/m, z16.s, z2.s\n fmla z31.s, p2/m, z16.s, z3.s\n\n // Apply LHS scale factor\n fmul z24.s, z28.s, z17.s\n fmul z25.s, z29.s, z17.s\n fmul z26.s, z30.s, z17.s\n fmul z27.s, z31.s, z17.s\n\n // Add bias\n fadd z24.s, z24.s, z8.s\n fadd z25.s, z25.s, z9.s\n fadd z26.s, z26.s, z10.s\n fadd z27.s, z27.s, z11.s\n\n // Clamp and store\n KAI_ASM_INST(0xc1b3ca58) // fclamp { z24.s-z27.s }, z18.s, z19.s\n KAI_ASM_INST(0xa060c638) // st1w { z24.s-z27.s }, pn9, [x17]\n\n ldr x5, [x16, #0x18] // dst_stride_row\n add x17, x17, x5\n add x14, x14, #4\n cmp x14, x15\n b.lt postprocess_loop\n\n // N loop tail\n ldr x5, [x16, #0x28] // rhs_stride\n add x8, x8, x5\n addvl x9, x9, #4\n decb x13, all\n KAI_ASM_INST(0x256d47f0) // whilelt pn8.h, xzr, x13, vlx2\n b.mi n_loop\n\n // M loop tail\n ldr x5, [x16, #0x20] // lhs_stride\n add x20, x20, x5\n ldr x5, [x16, #0x18] // dst_stride_row\n mul x5, x5, x7\n add x19, x19, x5\n sub x12, x12, x7\n whilelt p0.s, xzr, x12\n b.mi m_loop\n\n KAI_ASM_INST(0xd503467f) // smstop\n\n ldp d14, d15, [sp, #96]\n ldp d12, d13, [sp, #80]\n ldp d10, d11, [sp, #64]\n ldp d8, d9, [sp, #48]\n ldp x27, x28, [sp, #128]\n ldp x25, x26, [sp, #112]\n ldp x23, x24, [sp, #32]\n ldp x21, x22, [sp, #16]\n ldp x19, x20, [sp], #144\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.h",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qa8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS of the vector-by-matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs, size_t lhs_stride,\n void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.c",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n\n#if defined(__aarch64__)\n#include <arm_neon.h>\n#endif\n#include <float.h>\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_per_multiplier = sizeof(float);\nstatic const size_t kai_num_bytes_per_offset = sizeof(int32_t);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return mr * (k_internal * sizeof(int8_t) + kai_num_bytes_per_multiplier + kai_num_bytes_per_offset);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) {\n // It always points to the beginning of the row\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return num_rows * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* restrict lhs,\n size_t lhs_stride, void* restrict lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n\n if (m == 0) {\n return;\n }\n\n const size_t num_rows = m;\n\n const float* src_ptr = lhs;\n\n const size_t dst_stride = kai_lhs_packed_stride(k, mr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n const int32_t num_blocks_k = (int32_t)(k / k_block_len);\n const int32_t num_blocks_k_internal = (int32_t)(k_internal / k_block_len);\n\n for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {\n float max0 = -FLT_MAX;\n float min0 = FLT_MAX;\n\n // Find min/max for each channel\n int32_t k_idx = 0;\n\n#if defined(__aarch64__)\n float32x4_t vmax0 = vdupq_n_f32(-FLT_MAX);\n float32x4_t vmin0 = vdupq_n_f32(FLT_MAX);\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float32x4_t src0_0 = vld1q_f32(src_ptr + 0 + (size_t)k_idx);\n const float32x4_t src0_1 = vld1q_f32(src_ptr + 4 + (size_t)k_idx);\n\n // Calculate the max\n vmax0 = vmaxq_f32(src0_0, vmax0);\n vmax0 = vmaxq_f32(vmax0, src0_1);\n\n // Calculate the min\n vmin0 = vminq_f32(src0_0, vmin0);\n vmin0 = vminq_f32(vmin0, src0_1);\n }\n // Get the max/min\n max0 = vmaxvq_f32(vmax0);\n min0 = vminvq_f32(vmin0);\n#endif\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float src0_0 = *(src_ptr + (size_t)k_idx);\n max0 = fmaxf(src0_0, max0);\n min0 = fminf(src0_0, min0);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n\n float zero_point0 =\n zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len * sizeof(int8_t));\n\n // Quantize the channels\n int32_t block_idx = 0;\n\n#if defined(__aarch64__)\n if (k_block_len == 8) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n const float32x4_t src_1 = vld1q_f32(src_ptr + k_idx_start + 4);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n float32x4_t v1_f32 = vmulq_n_f32(src_1, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int32x4_t v1_s32 = vcvtaq_s32_f32(v1_f32);\n\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n int16x4_t v1_s16 = vqmovn_s32(v1_s32);\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x8_t vnzp_s16 = vdupq_n_s16(nzp_s16);\n v_s16 = vaddq_s16(v_s16, vnzp_s16);\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(v_s16);\n vst1_s8((int8_t*)(dst_ptr), v0_s8);\n dst_ptr += 8 * sizeof(int8_t);\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n } else if (k_block_len == 4) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x4_t vnzp_s16 = vdup_n_s16(nzp_s16);\n int16x4_t v_s16 = vadd_s16(v0_s16, vnzp_s16);\n v_s16 = vmax_s16(v_s16, vdup_n_s16(INT8_MIN));\n v_s16 = vmin_s16(v_s16, vdup_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(vcombine_s16(v_s16, vdup_n_s16(0)));\n\n *((int8_t*)(dst_ptr)) = vget_lane_s8(v0_s8, 0);\n *((int8_t*)(dst_ptr + 1)) = vget_lane_s8(v0_s8, 1);\n *((int8_t*)(dst_ptr + 2)) = vget_lane_s8(v0_s8, 2);\n *((int8_t*)(dst_ptr + 3)) = vget_lane_s8(v0_s8, 3);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n } else\n#endif\n {\n for (; block_idx < num_blocks_k; ++block_idx) {\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n const int32_t k_idx_start = (block_idx * k_block_len) + k_block_idx;\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n dst_ptr = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr += dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params\n *((float*)(dst_ptr)) = recip_scale0;\n\n src_ptr += (lhs_stride / sizeof(float));\n\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n}\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon.h",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Get the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n///\n/// @return the n step value\nsize_t kai_get_n_step_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(size_t nr);\n\n/// Gets the offset in bytes for the RHS matrix (not packed), which holds\n/// the int4 values in a N x K matrix, where N is number of rows and K is the number of columns.\n///\n/// Two int4 values are stored in one byte.\n/// The lower order part of the byte (low) holds the first nibble (K-index + 0).\n/// The higher order of the byte holds the second nibble (K-index + 1).\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n size_t n_idx, //\n size_t rhs_stride);\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n/// @param[in] scale_dt Block scale data type\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n enum kai_datatype scale_dt);\n\n/// Gets the offset in bytes for the packed RHS matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n/// @param[in] scale_dt Block scale data type\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n size_t n_idx, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n enum kai_datatype scale_dt);\n\n/// Gets the size in bytes for the quantized and packed RHS matrix.\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple\n/// of 32.\n/// @param[in] scale_dt Block scale data type\n///\n/// @return the packed RHS matrix size in bytes\n// clang-format off\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n enum kai_datatype scale_dt);\n// clang-format on\n\n/// Runs the RHS packing micro-kernel.\n///\n/// The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 values are stored in one byte. The lower order part of the byte (low) holds\n/// the first nibble (K-index + 0). The higher order of the byte holds the second nibble (K-index + 1).\n///\n/// @param[in] num_groups The number of groups. It must be 1. Currently unused.\n/// @param[in] n The number of rows in the RHS matrix (not packed).\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel. It must be a multiple of 4.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] bl The block length, which defines the number of\n/// K values stored in a single block. It must be a multiple of 32.\n/// @param[in] rhs The RHS matrix containing the 4-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).\n/// @param[in] rhs_stride The number of bytes per row in bytes of the RHS matrix\n/// @param[in] bias The biases.\n/// @param[in] scale The per-block quantization scales.\n/// The scale data type must be provided with the params object.\n/// Supported scale data types are FP32, FP16 and BF16.\n/// @param[in] scale_stride The number of bytes per row in bytes of the scale matrix\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix. Currently unused.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n const uint8_t* rhs, //\n size_t rhs_stride, //\n const float* bias, //\n const void* scale, //\n size_t scale_stride, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params* params);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon.c",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n// nrx4 => this function can take in generic nr values but the input is expected to have a block depth of 4.\n// Block depth is calculated as kr / sr. The values of these parameters are defined in the matmul ukernel.\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n\n#include \"kai_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon.h\"\n\n#include <arm_neon.h>\n#include <stddef.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\nstatic const size_t kai_nr_multiple_of = 4;\nstatic const size_t kai_bl_multiple_of = 32;\n\nstatic size_t kai_get_num_blocks_per_row(const size_t k, const size_t bl) {\n KAI_ASSERT((bl % kai_bl_multiple_of) == 0);\n return kai_roundup(k, bl) / bl;\n}\n\nstatic size_t kai_get_num_bytes_per_block(const size_t bl, const size_t num_bytes_multiplier_rhs) {\n KAI_ASSERT((bl % kai_bl_multiple_of) == 0);\n return (bl / 2) + num_bytes_multiplier_rhs;\n}\n\nstatic size_t kai_get_rhs_packed_offset_end_of_all_blocks(\n // clang-format off\n const size_t k,\n const size_t nr,\n const size_t kr,\n const size_t bl,\n const size_t num_bytes_multiplier_rhs) {\n // clang-format on\n KAI_ASSERT((bl % kr) == 0);\n KAI_ASSERT((nr % kai_nr_multiple_of) == 0);\n KAI_ASSERT((bl % kai_bl_multiple_of) == 0);\n\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block(bl, num_bytes_multiplier_rhs);\n\n return (nr * num_bytes_per_block * num_blocks_per_row);\n}\n\nsize_t kai_get_n_step_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(const size_t nr) {\n return nr;\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(const size_t n_idx, const size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n // clang-format off\n const size_t k,\n const size_t nr,\n const size_t kr,\n const size_t sr,\n const size_t bl,\n const enum kai_datatype scale_dt) {\n // clang-format on\n KAI_ASSERT((k % bl) == 0);\n KAI_ASSERT((bl % kr) == 0);\n KAI_ASSERT((nr % kai_nr_multiple_of) == 0);\n KAI_ASSERT((bl % kai_bl_multiple_of) == 0);\n KAI_ASSERT(scale_dt == kai_dt_bf16);\n\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t num_bytes_multiplier_rhs = kai_get_datatype_size_in_bytes(scale_dt);\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block(bl, num_bytes_multiplier_rhs);\n\n return nr * ((num_bytes_per_block * num_blocks_per_row) + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\n// clang-format off\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n const size_t n_idx,\n const size_t k,\n const size_t nr,\n const size_t kr,\n const size_t sr,\n const size_t bl,\n const enum kai_datatype scale_dt) {\n // clang-format on\n KAI_ASSERT((n_idx % nr) == 0);\n KAI_ASSERT((k % bl) == 0);\n KAI_ASSERT((bl % kr) == 0);\n KAI_ASSERT((nr % kai_nr_multiple_of) == 0);\n KAI_ASSERT((bl % kai_bl_multiple_of) == 0);\n KAI_ASSERT(scale_dt == kai_dt_bf16);\n\n return (n_idx / nr) *\n kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(k, nr, kr, sr, bl, scale_dt);\n}\n\n// clang-format off\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n const size_t n, //\n const size_t k, //\n const size_t nr, //\n const size_t kr, //\n const size_t sr, //\n const size_t bl, //\n const enum kai_datatype scale_dt) {\n // clang-format on\n KAI_ASSERT((k % bl) == 0);\n KAI_ASSERT((bl % kr) == 0);\n KAI_ASSERT((nr % kai_nr_multiple_of) == 0);\n KAI_ASSERT((bl % kai_bl_multiple_of) == 0);\n KAI_ASSERT(scale_dt == kai_dt_bf16);\n\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows *\n kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(k, nr, kr, sr, bl, scale_dt);\n}\n\nvoid kai_run_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n // clang-format off\n const size_t num_groups,\n const size_t n,\n const size_t k,\n const size_t nr,\n const size_t kr,\n const size_t sr,\n const size_t bl,\n const uint8_t* rhs,\n const size_t rhs_stride,\n const float* bias,\n const void* scale,\n const size_t scale_stride,\n void* rhs_packed,\n const size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params* params) {\n // clang-format on\n KAI_UNUSED(num_groups);\n KAI_UNUSED(extra_bytes);\n KAI_ASSERT(rhs != NULL);\n KAI_ASSERT(scale != NULL);\n KAI_ASSERT(rhs_packed != NULL);\n KAI_ASSERT(params != NULL);\n KAI_ASSERT(params->rhs_zero_point == 8);\n KAI_ASSERT(params->lhs_zero_point == 1);\n\n KAI_ASSERT((k % bl) == 0);\n KAI_ASSERT((bl % kr) == 0);\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSERT((nr % kai_nr_multiple_of) == 0);\n KAI_ASSERT((bl % kai_bl_multiple_of) == 0);\n KAI_ASSERT(params->scale_dt == kai_dt_bf16);\n\n // Note: The input matrix (rhs) is expected with:\n // \"k\" columns and \"n\" rows (NxK)\n const size_t block_length = kr / sr;\n KAI_ASSERT(block_length == 4);\n const enum kai_datatype scale_dt = params->scale_dt;\n const size_t num_bytes_multiplier_rhs = kai_get_datatype_size_in_bytes(scale_dt);\n const size_t rhs_packed_offset_end_of_all_blocks =\n kai_get_rhs_packed_offset_end_of_all_blocks(k, nr, kr, bl, num_bytes_multiplier_rhs);\n const size_t num_qblocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block_k = bl / 2;\n const size_t dst_num_rows = kai_roundup(n, nr);\n const size_t block_length_in_bytes = block_length / 2;\n\n const int8x16_t rhs_zero_point = vdupq_n_s8(8);\n const uint8x16_t low_mask = vdupq_n_u8(0x0F);\n const size_t num_bytes_processed = 16;\n\n uint8_t* dst_row = (uint8_t*)rhs_packed;\n\n for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; dst_row_idx += nr) {\n float* sums = (float*)(dst_row + rhs_packed_offset_end_of_all_blocks);\n\n // Initialize the RHS reduction sums to zero\n memset(sums, 0, nr * kai_num_bytes_sum_rhs);\n\n // Iterate over the quantized blocks\n for (size_t dst_qblock_idx = 0; dst_qblock_idx < num_qblocks_per_row; ++dst_qblock_idx) {\n // Store the scales after packing all K values in the block\n uint8_t* rhs_packed_scale = dst_row + num_bytes_per_block_k * nr;\n const uint8_t* scale_ptr = (const uint8_t*)scale + dst_qblock_idx * num_bytes_multiplier_rhs;\n\n for (size_t i = 0; i < nr; ++i) {\n const size_t src_row_idx = KAI_MIN(dst_row_idx + i, n - 1);\n const void* src_scales_ptr = scale_ptr + src_row_idx * scale_stride;\n void* dst_scales_ptr = rhs_packed_scale + i * num_bytes_multiplier_rhs;\n\n memcpy(\n dst_scales_ptr, //\n src_scales_ptr, //\n num_bytes_multiplier_rhs); //\n }\n\n size_t k0_idx_i = dst_qblock_idx * bl;\n\n for (size_t dst_byte_idx = 0; dst_byte_idx < num_bytes_per_block_k; dst_byte_idx += num_bytes_processed) {\n for (size_t nr_idx = 0; nr_idx < nr; nr_idx += 4) {\n // Clamp the indices to avoid out-of-bound reads\n const size_t n0_idx = KAI_MIN(dst_row_idx + nr_idx, n - 1);\n const size_t n1_idx = KAI_MIN(n0_idx + 1, n - 1);\n const size_t n2_idx = KAI_MIN(n0_idx + 2, n - 1);\n const size_t n3_idx = KAI_MIN(n0_idx + 3, n - 1);\n\n // Load scales\n const float d0 = kai_cast_f32_bf16(((uint16_t*)rhs_packed_scale)[nr_idx + 0]);\n const float d1 = kai_cast_f32_bf16(((uint16_t*)rhs_packed_scale)[nr_idx + 1]);\n const float d2 = kai_cast_f32_bf16(((uint16_t*)rhs_packed_scale)[nr_idx + 2]);\n const float d3 = kai_cast_f32_bf16(((uint16_t*)rhs_packed_scale)[nr_idx + 3]);\n\n // Initialize partial sum\n int32_t partial_sum0 = 0;\n int32_t partial_sum1 = 0;\n int32_t partial_sum2 = 0;\n int32_t partial_sum3 = 0;\n\n const uint8_t* src_block_base = rhs + ((k0_idx_i / 2) + dst_byte_idx);\n const uint8x16_t vsrc0_0 = vld1q_u8(src_block_base + n0_idx * rhs_stride);\n const uint8x16_t vsrc1_0 = vld1q_u8(src_block_base + n1_idx * rhs_stride);\n const uint8x16_t vsrc2_0 = vld1q_u8(src_block_base + n2_idx * rhs_stride);\n const uint8x16_t vsrc3_0 = vld1q_u8(src_block_base + n3_idx * rhs_stride);\n\n // Get the lower and higher nibble and apply zero-points\n const int8x16_t vsrc0_0_lo =\n vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vsrc0_0, low_mask)), rhs_zero_point);\n const int8x16_t vsrc0_0_hi = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(vsrc0_0, 4)), rhs_zero_point);\n const int8x16_t vsrc1_0_lo =\n vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vsrc1_0, low_mask)), rhs_zero_point);\n const int8x16_t vsrc1_0_hi = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(vsrc1_0, 4)), rhs_zero_point);\n const int8x16_t vsrc2_0_lo =\n vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vsrc2_0, low_mask)), rhs_zero_point);\n const int8x16_t vsrc2_0_hi = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(vsrc2_0, 4)), rhs_zero_point);\n const int8x16_t vsrc3_0_lo =\n vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vsrc3_0, low_mask)), rhs_zero_point);\n const int8x16_t vsrc3_0_hi = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(vsrc3_0, 4)), rhs_zero_point);\n\n // Calculate and store row sums\n partial_sum0 += vaddlvq_s16(vaddl_s8(\n vadd_s8(vget_low_s8(vsrc0_0_lo), vget_high_s8(vsrc0_0_lo)),\n vadd_s8(vget_low_s8(vsrc0_0_hi), vget_high_s8(vsrc0_0_hi))));\n partial_sum1 += vaddlvq_s16(vaddl_s8(\n vadd_s8(vget_low_s8(vsrc1_0_lo), vget_high_s8(vsrc1_0_lo)),\n vadd_s8(vget_low_s8(vsrc1_0_hi), vget_high_s8(vsrc1_0_hi))));\n partial_sum2 += vaddlvq_s16(vaddl_s8(\n vadd_s8(vget_low_s8(vsrc2_0_lo), vget_high_s8(vsrc2_0_lo)),\n vadd_s8(vget_low_s8(vsrc2_0_hi), vget_high_s8(vsrc2_0_hi))));\n partial_sum3 += vaddlvq_s16(vaddl_s8(\n vadd_s8(vget_low_s8(vsrc3_0_lo), vget_high_s8(vsrc3_0_lo)),\n vadd_s8(vget_low_s8(vsrc3_0_hi), vget_high_s8(vsrc3_0_hi))));\n\n // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n sums[nr_idx + 0] += (float)partial_sum0 * d0;\n sums[nr_idx + 1] += (float)partial_sum1 * d1;\n sums[nr_idx + 2] += (float)partial_sum2 * d2;\n sums[nr_idx + 3] += (float)partial_sum3 * d3;\n // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n\n const uint8x16_t vdst_u8_0 = vorrq_u8(\n vandq_u8(vreinterpretq_u8_s8(vsrc0_0_lo), low_mask),\n vshlq_n_u8(vreinterpretq_u8_s8(vsrc0_0_hi), 4));\n const uint8x16_t vdst_u8_1 = vorrq_u8(\n vandq_u8(vreinterpretq_u8_s8(vsrc1_0_lo), low_mask),\n vshlq_n_u8(vreinterpretq_u8_s8(vsrc1_0_hi), 4));\n const uint8x16_t vdst_u8_2 = vorrq_u8(\n vandq_u8(vreinterpretq_u8_s8(vsrc2_0_lo), low_mask),\n vshlq_n_u8(vreinterpretq_u8_s8(vsrc2_0_hi), 4));\n const uint8x16_t vdst_u8_3 = vorrq_u8(\n vandq_u8(vreinterpretq_u8_s8(vsrc3_0_lo), low_mask),\n vshlq_n_u8(vreinterpretq_u8_s8(vsrc3_0_hi), 4));\n\n // Reorder to interleave nr rows\n const uint16x8_t vdst_u16_0 = vreinterpretq_u16_u8(vdst_u8_0);\n const uint16x8_t vdst_u16_1 = vreinterpretq_u16_u8(vdst_u8_1);\n const uint16x8_t vdst_u16_2 = vreinterpretq_u16_u8(vdst_u8_2);\n const uint16x8_t vdst_u16_3 = vreinterpretq_u16_u8(vdst_u8_3);\n\n const uint32x4_t vdst_u32_0 = vreinterpretq_u32_u16(vzip1q_u16(vdst_u16_0, vdst_u16_1));\n const uint32x4_t vdst_u32_1 = vreinterpretq_u32_u16(vzip1q_u16(vdst_u16_2, vdst_u16_3));\n const uint32x4_t vdst_u32_2 = vreinterpretq_u32_u16(vzip2q_u16(vdst_u16_0, vdst_u16_1));\n const uint32x4_t vdst_u32_3 = vreinterpretq_u32_u16(vzip2q_u16(vdst_u16_2, vdst_u16_3));\n\n const uint32x4_t vdst0_0 = vzip1q_u32(vdst_u32_0, vdst_u32_1);\n const uint32x4_t vdst1_0 = vzip2q_u32(vdst_u32_0, vdst_u32_1);\n const uint32x4_t vdst2_0 = vzip1q_u32(vdst_u32_2, vdst_u32_3);\n const uint32x4_t vdst3_0 = vzip2q_u32(vdst_u32_2, vdst_u32_3);\n\n // Store packed values\n vst1_u32((uint32_t*)dst_row, vget_low_u32(vdst0_0));\n vst1_u32((uint32_t*)(dst_row + nr * block_length_in_bytes), vget_high_u32(vdst0_0));\n vst1_u32((uint32_t*)(dst_row + (2 * nr * block_length_in_bytes)), vget_low_u32(vdst1_0));\n\n vst1_u32((uint32_t*)(dst_row + (3 * nr * block_length_in_bytes)), vget_high_u32(vdst1_0));\n vst1_u32((uint32_t*)(dst_row + (4 * nr * block_length_in_bytes)), vget_low_u32(vdst2_0));\n vst1_u32((uint32_t*)(dst_row + (5 * nr * block_length_in_bytes)), vget_high_u32(vdst2_0));\n vst1_u32((uint32_t*)(dst_row + (6 * nr * block_length_in_bytes)), vget_low_u32(vdst3_0));\n vst1_u32((uint32_t*)(dst_row + (7 * nr * block_length_in_bytes)), vget_high_u32(vdst3_0));\n\n dst_row += (4 * block_length_in_bytes);\n }\n // Skip to end of qblock\n dst_row += 7 * nr * block_length_in_bytes;\n }\n\n // Move the pointer after scales\n dst_row += num_bytes_multiplier_rhs * nr;\n }\n\n // Move the pointer after the row sum\n dst_row += kai_num_bytes_sum_rhs * nr;\n\n // Set the bias\n if (bias == NULL) {\n memset(dst_row, 0, nr * kai_num_bytes_bias);\n } else {\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx + i, n - 1);\n ((float*)dst_row)[i] = bias[src_row_idx];\n }\n }\n\n // Move the pointer after the row sum\n dst_row += kai_num_bytes_bias * nr;\n }\n}\n#endif // Architectural features check.\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "binding.cpp",
|
| 65 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa against gemm_qai8dxp_qs4c32_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, const float* bias, int M)\n// C = A @ dequant(B_q4, B_scales)^T + bias, activations dynamically quantized per row to asymmetric int8.\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n#include \"kai_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr size_t BL = 32;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, const float* bias, int M)\n{\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n // LHS: (re)quantized and packed on EVERY call into a grow-only scratch buffer (activation-side work).\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t sz = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr);\n if (sz > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(sz);\n lhs_cap = sz;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_quant_pack_qai8dxp_f32(\n (size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_snap = nullptr; static size_t B_snap_size = 0;\n static uint8_t* Bs_snap = nullptr; static size_t Bs_snap_size = 0;\n static uint8_t* bias_snap = nullptr; static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_snap, B_snap_size, B_q4, (size_t)N * K / 2)\n | refresh_cache(Bs_snap, Bs_snap_size, B_scales, sizeof(uint16_t) * N * (K / BL))\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float) * N);\n if (stale) {\n std::free(rhs_packed);\n const size_t sz = kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon((size_t)N, (size_t)K, nr, kr, sr, BL, kai_dt_bf16);\n rhs_packed = std::malloc(sz);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params params;\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 8;\n params.scale_dt = kai_dt_bf16;\n kai_run_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(\n 1, (size_t)N, (size_t)K, nr, kr, sr, BL, B_q4, /*rhs_stride=*/(size_t)K / 2, bias, B_scales,\n /*scale_stride=*/sizeof(uint16_t) * (K / BL), rhs_packed, 0, ¶ms);\n }\n\n kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n (size_t)M, (size_t)N, (size_t)K, BL, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float),\n -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n return 0;\n}\n"
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qs4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qs4c32_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_qs4c32_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qs4c32_sme2_n256_k512: KleidiAI kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.c",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) || !(defined(__ARM_FEATURE_SVE2) || defined(__ARM_FEATURE_SME2))\n#error This file must be compiled for AArch64, FEAT_SVE2 or FEAT_SME2.\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.h\"\n\n#include <float.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n// Compute args\nstatic const size_t kai_m_step = 1; // Multiple of vector length\nstatic const size_t kai_n_step = 4; // Multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // Multiple of vector length\nstatic const size_t kai_nr = 4; // Multiple of vector length\nstatic const size_t kai_kr = 4;\nstatic const size_t kai_sr = 2;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 2;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_recip_num_bytes_qvalue_rhs = 2;\nstatic const size_t kai_num_bytes_multiplier_rhs = 2;\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_bl = 32;\n\n// Look-up table used for int4->int8 convert\nstatic const int32_t lut[16] = {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};\n\ninline static size_t kai_get_num_bytes_per_block_lhs(size_t bl) {\n return (bl * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs;\n}\n\ninline static size_t kai_get_num_bytes_per_block_rhs(size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n size_t num_bytes_per_block_rhs = (bl / kai_recip_num_bytes_qvalue_rhs) + kai_num_bytes_multiplier_rhs;\n return num_bytes_per_block_rhs;\n}\n\ninline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n return k / bl;\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k, size_t bl) {\n const size_t mr = kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n return mr * kai_get_num_blocks_per_row(k, bl) * kai_get_num_bytes_per_block_lhs(bl);\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k, size_t bl) {\n KAI_ASSUME((bl % kai_bl) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n size_t rhs_packed_stride = nr * (num_bytes_per_block * num_blocks_per_row);\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t k, size_t bl) {\n const size_t mr = kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n KAI_ASSUME((m_idx % mr) == 0);\n\n return (m_idx / mr) * kai_get_lhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t n_idx, size_t k, size_t bl) {\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n KAI_ASSUME((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride(k, bl);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n const size_t m_step = kai_get_m_step_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t n_step = kai_get_n_step_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n KAI_ASSUME((m_idx % m_step) == 0);\n KAI_ASSUME((n_idx % n_step) == 0);\n\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSUME(dst_stride_col == sizeof(float));\n KAI_ASSUME((bl % kai_bl) == 0);\n\n if (m == 0) {\n return;\n }\n\n typedef struct {\n size_t lhs_packed_stride;\n size_t rhs_packed_stride;\n size_t mr;\n size_t bl;\n float scalar_min;\n float scalar_max;\n uint32_t is_clamp_valid;\n } KernelArgs;\n\n KernelArgs ka;\n\n const size_t num_blocks = kai_get_num_blocks_per_row(k, bl);\n\n const size_t mr = kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n\n ka.mr = mr;\n ka.lhs_packed_stride = kai_get_lhs_packed_stride(k, bl);\n ka.rhs_packed_stride = kai_get_rhs_packed_stride(k, bl);\n ka.bl = bl;\n ka.scalar_min = scalar_min;\n ka.scalar_max = scalar_max;\n\n const uint16_t* lhs_scales = (const uint16_t*)((const int8_t*)lhs_packed + ka.lhs_packed_stride -\n (mr * num_blocks) * kai_num_bytes_multiplier_lhs);\n const uint16_t* rhs_scales = (const uint16_t*)((const uint8_t*)rhs_packed + ka.rhs_packed_stride -\n (nr * num_blocks) * kai_num_bytes_multiplier_rhs);\n\n ka.is_clamp_valid = ((scalar_min > -FLT_MAX) && (scalar_max < FLT_MAX)) ? 1 : 0;\n\n kai_commit_za();\n\n __asm__ volatile(\n // Switch to streaming mode with ZA enabling\n \" .inst 0xd503477f // smstart \\n\"\n\n // Constants\n // - SVLs\n \" cntw x14 \\n\"\n // - ptrue\n \" ptrue p0.b, all \\n\"\n \" .inst 0x25a07810 // ptrue pn8.s \\n\"\n\n // Predicate for loading fp16 scaling factors\n \" ldr x5, [%x[args_ptr], %[offset_mr]]\\n\"\n \" lsl x5, x5, #1 \\n\"\n \" whilelt p4.b, xzr, x5 \\n\"\n\n // Initialize ZT0 (Lookup table)\n \" mov x6, %[lut]\\n\"\n \" .inst 0xe11f80c0 // ldr zt0, [x6] \\n\"\n\n // Initialize the RHS packes and scale pointers\n \" mov x16, %[rhs_packed] \\n\"\n \" mov x17, %[rhs_scales] \\n\"\n\n // Load clamp min/max from KernelArgs\n \" ld1rw z15.s, p0/z, [%x[args_ptr], %[offset_min]] \\n\" // min\n \" ld1rw z17.s, p0/z, [%x[args_ptr], %[offset_max]] \\n\" // max\n\n // Iterate over n (x8)\n // e.g. for(n_idx = 0; n_idx < n; n_idx+=n_step)\n \" mov x8, #0 \\n\"\n \" mov x0, %[N] \\n\"\n \" .inst 0x25a06511 // whilelt pn9.s, x8, x0, VLx4 \\n\"\n\n \" b.none 9f // .LOOP_N_END%= \\n\"\n\n \" 1: // .LOOP_N_START%=: \\n\"\n\n // Iterate over m (x9)\n // e.g. for(n_idx = 0; n_idx < n; n_idx+=n_step)\n \" mov x9, %[M] \\n\"\n\n // Initialize the LHS packed and scale pointers\n \" mov x22, %[lhs_packed] \\n\"\n \" mov x23, %[lhs_scales] \\n\"\n\n // Initialize the DST pointer\n \" mov x24, %[dst] \\n\"\n\n \" 2: // .LOOP_M_START%=: \\n\"\n\n // Address offset for the left and right quantized values\n \" mov x20, #0 \\n\"\n \" mov x21, #0 \\n\"\n\n // Number of output rows to store -> min(SVLh, loop M index)\n \" cmp x9, x14 \\n\"\n \" csel x15, x9, x14, lo \\n\"\n \" lsl x15, x15, #2 \\n\"\n\n // Iterate over all K values\n // e.g. for(k_idx = 0; k_idx < k; k_idx += bl)\n \" mov x10, %[K] \\n\"\n\n // Skip processing if K=0\n \" cmp x10, #0 \\n\"\n \" b.eq 8f // .LOOP_K_END%= \\n\"\n\n \" 3: // .LOOP_K_START%=: \\n\"\n\n // Zeroing of ZA accumulator\n \" .inst 0xc00800ff // zero {za} \\n\"\n\n // Load the fp16 scaling factors for the right matrix block\n \" .inst 0xa0154220 // ld1w {z0.s - z1.s}, pn8/z, [x17], x21, lsl #2] \\n\"\n \" .inst 0xc161d000 // zip {z0.h - z1.h}, z0.h, z1.h \\n\"\n\n // Iterate over all values in the block\n // k_blk_idx = bl\n // e.g. while(k_blk_idx > 0) {... k_blk_idx -= 4}\n \" ldr x5, [%x[args_ptr], %[bl]] \\n\"\n \" mov x11, x5\\n\"\n\n \" 4: // .LOOP_BL_START%=: \\n\"\n\n // Load right matrix row\n \" .inst 0xa0144202 // ld1w {z2.s - z3.s}, pn8/z, [x16], x20, lsl #2] \\n\"\n\n // Load left matrix column\n \" ld1h {z8.h}, p0/z, [x22, x20, lsl #1] \\n\"\n \" inch x20, all \\n\"\n\n // Convert Int4 -> Int8\n \" .inst 0xc08a4044 // luti4 {z4.b - z5.b}, zt0, z2[0] \\n\"\n \" .inst 0xc08a4066 // luti4 {z6.b - z7.b}, zt0, z3[0] \\n\"\n\n // Outer-products\n \" .inst 0xa0840100 // smopa za0.s, p0/m, p0/m, z8.b, z4.b \\n\"\n \" .inst 0xa0850101 // smopa za1.s, p0/m, p0/m, z8.b, z5.b \\n\"\n \" .inst 0xa0860102 // smopa za2.s, p0/m, p0/m, z8.b, z6.b \\n\"\n \" .inst 0xa0870103 // smopa za3.s, p0/m, p0/m, z8.b, z7.b \\n\"\n\n // Decrement the block loop index\n \" subs x11, x11, #4 \\n\"\n\n \" b.gt 4b // .LOOP_BL_START%= \\n\"\n\n // === End of the block loop ===\n\n // Store loop index\n \" mov w12, #0 \\n\"\n\n // Copy destination pointer for store loop\n \" mov x25, x24 \\n\"\n\n // Load the fp16 scaling factors for the left matrix block\n \" ld1b {z16.b}, p4/z, [x23, x21] \\n\"\n \" inch x21, all \\n\"\n\n // Predicate for the selection of a scaling among the vector\n \" pfalse p3.b \\n\"\n\n \" 5: // .LOOP_ZA%=: \\n\"\n\n // Select and replicate scaling factor for the right block\n \" pnext p3.h, p0, p3.h \\n\"\n \" clastb z19.h, p3, z19.h, z16.h \\n\"\n\n // Get data from za\n \" .inst 0xc006041c // mova {z28.b-z31.b}, za0h.b[w12, 0:3] \\n\"\n \" add w12, w12, #4 \\n\"\n\n // Convert from int32 to fp32\n \" .inst 0xc132e39c // scvtf {z28.s-z31.s}, {z28.s-z31.s} \\n\"\n\n // Multiply left and right scaling factors\n \" movprfx z8, z18 \\n\"\n \" fmlalb z8.s, z19.h, z0.h \\n\"\n \" movprfx z9, z18 \\n\"\n \" fmlalb z9.s, z19.h, z1.h \\n\"\n \" movprfx z10, z18 \\n\"\n \" fmlalt z10.s, z19.h, z0.h \\n\"\n \" movprfx z11, z18 \\n\"\n \" fmlalt z11.s, z19.h, z1.h \\n\"\n\n \" cmp x10, %[K] \\n\"\n \" b.ne 6f // .ACCUMULATE%= \\n\"\n\n // Applying combined scaling factors to processed block\n \" fmul z24.s, z8.s, z28.s \\n\"\n \" fmul z25.s, z9.s, z29.s \\n\"\n \" fmul z26.s, z10.s, z30.s \\n\"\n \" fmul z27.s, z11.s, z31.s \\n\"\n\n \"b 7f // .STORE%= \\n\"\n\n \" 6: // .ACCUMULATE%=: \\n\"\n // Load intermediate result\n \" .inst 0xa040c738 // ld1w {z24.s-z27.s}, pn9/z, [x25] \\n\"\n\n // Multiply the intermediate results by LHS_SCALE x RHS_SCALE\n // and store in the main floating-point accumulator\n \" fmla z24.s, p0/m, z8.s, z28.s \\n\"\n \" fmla z25.s, p0/m, z9.s, z29.s \\n\"\n \" fmla z26.s, p0/m, z10.s, z30.s \\n\"\n \" fmla z27.s, p0/m, z11.s, z31.s \\n\"\n\n \"7: // .STORE%=: \\n\"\n // Store the results into memory\n \" .inst 0xa060c738 // st1w {z24.s-z27.s}, pn9, [x25] \\n\"\n \" add x25, x25, %[stride] \\n\"\n\n \" cmp x12, x15 \\n\"\n \" blt 5b // .LOOP_ZA%= \\n\"\n\n // Decrement K loop index by bl\n \" ldr x5, [%x[args_ptr], %[bl]] \\n\"\n \" subs x10, x10, x5 \\n\"\n\n \" b.gt 3b // .LOOP_K_START%= \\n\"\n\n \" 8: // .LOOP_K_END%=: \\n\"\n\n // === End of the K loop ===\n \" ldr x5, [%x[args_ptr], %[is_clamp_valid]]\\n\"\n \" cbz x5, 11f \\n\"\n \" mov x5, x24 \\n\"\n \" mov x12, 0 \\n\"\n\n // Clamp loop\n \" 10: // .LOOP_CLAMP%=: \\n\"\n \" .inst 0xa040c718 // ld1w {z24.s-z27.s}, pn9/z, [x24] \\n\"\n // Apply clamp\n \" .inst 0xc1b1c9f8 // fclamp { z24.s - z27.s }, z15.s, z17.s \\n\"\n // Store results into memory\n \" .inst 0xa060c718 // st1w {z24.s-z27.s}, pn9, [x24] \\n\"\n\n \" add x24, x24, %[stride] \\n\"\n \" add x12, x12, #4 \\n\"\n \" cmp x12, x15 \\n\"\n\n \" blt 10b // .LOOP_CLAMP%=: \\n\"\n\n \" mov x24, x5 \\n\"\n \" 11: // .LOOP_CLAMP_END%=: \\n\"\n \" ldr x5, [%x[args_ptr], %[offset_stride_l]] \\n\"\n\n // Increment pointer to the quantized values of the right matrix\n \" add x22, x22, x5\\n\"\n\n // Increment pointer to the scaling factors of the right matrix\n \" add x23, x23, x5 \\n\"\n\n // Update destination pointer\n \" mov x24, x25 \\n\"\n\n // Decrement M loop index\n \" decw x9, all \\n\"\n\n \" cmp x9, #0 \\n\"\n \" b.gt 2b // .LOOP_M_START%= \\n\"\n\n // === End of M loop ===\n\n // Increment output pointer\n \" incb %[dst], all, mul #4 \\n\"\n\n \" ldr x5, [%x[args_ptr], %[offset_stride_r]]\\n\"\n\n \" add x16, x16, x5 \\n\"\n \" add x17, x17, x5 \\n\"\n\n // Increment N loop index\n \" incb x8, all \\n\"\n\n \" .inst 0x25a06511 // whilelt pn9.s, x8, %[N], VLx4 \\n\"\n\n \" b.first 1b // .LOOP_N_START%= \\n\"\n\n \" 9: // .LOOP_N_END%=: \\n\"\n\n // === End of N loop ===\n\n // Exit streaming mode\n \" .inst 0xd503467f // smstop \\n\"\n : [dst] \"+r\"(dst), [rhs_packed] \"+r\"(rhs_packed), [rhs_scales] \"+r\"(rhs_scales)\n : [M] \"r\"(m), [N] \"r\"(n), [K] \"r\"(k), [lhs_packed] \"r\"(lhs_packed), [lhs_scales] \"r\"(lhs_scales),\n [stride] \"r\"(dst_stride_row), [lut] \"r\"(lut), [args_ptr] \"r\"(&ka),\n [offset_stride_l] \"I\"(offsetof(KernelArgs, lhs_packed_stride)),\n [offset_stride_r] \"I\"(offsetof(KernelArgs, rhs_packed_stride)), [offset_mr] \"I\"(offsetof(KernelArgs, mr)),\n [bl] \"I\"(offsetof(KernelArgs, bl)), [is_clamp_valid] \"I\"(offsetof(KernelArgs, is_clamp_valid)),\n [offset_min] \"I\"(offsetof(KernelArgs, scalar_min)), [offset_max] \"I\"(offsetof(KernelArgs, scalar_max))\n : \"p0\", \"p1\", \"p3\", \"p4\", \"p5\", \"p6\", \"p7\", \"p8\", \"p9\", \"p10\", \"p11\", \"p12\", \"p13\", \"p14\", \"p15\", \"z0\", \"z1\",\n \"z2\", \"z3\", \"z4\", \"z5\", \"z6\", \"z7\", \"z8\", \"z9\", \"z10\", \"z11\", \"z12\", \"z13\", \"z14\", \"z15\", \"z16\", \"z17\", \"z18\",\n \"z19\", \"z20\", \"z21\", \"z22\", \"z23\", \"z24\", \"z25\", \"z26\", \"z27\", \"z28\", \"z29\", \"z30\", \"z31\", \"x0\", \"x5\", \"x6\",\n \"x8\", \"x9\", \"x10\", \"x11\", \"x12\", \"x14\", \"x15\", \"x16\", \"x17\", \"x20\", \"x21\", \"x22\", \"x23\", \"x24\", \"x25\",\n \"memory\", \"cc\");\n}\n\n#endif // Architectural features check.\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.h",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_lhs_quant_pack_qsi8d32p_f32_neon to dynamically quantize and pack the LHS matrix in a single step.\n/// -# @ref kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon to pack the RHS NxK matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-block (32) quantization (qsi8d32) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of m_step.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Symmetric Signed 4-bit with per-block (32) quantization (qsi4c32) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// @param[in] bl Block length. It must be a multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t n_idx, //\n size_t k, //\n size_t bl); //\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of m_step.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of n_step.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride); //\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n); //\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Symmetric Signed 8-bit with per-block (32) quantization (qsi8d32) and packed\n/// RHS matrix: Quantized Symmetric Signed 4-bit with per-block (32) quantization (qsi4c32) and packed.\n/// Output tile: (rows x cols) = 1 VL x 4 VL (Vector Length)\n///\n/// Instruction used: SME2 (MOPA)\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] bl Block length. It must be a multiple of 32.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel dependencies list at the top of this file reports\n/// the available\n/// LHS packing functions for this micro-kernel.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel dependencies list at the top of this file reports\n/// the available\n/// RHS packing functions for this micro-kernel.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes.\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n size_t bl, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n}\n#endif // __cplusplus\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_lhs_quant_pack_qsi8d32p_f32_neon.h",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qsi8d32p_f32_neon(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qsi8d32p_f32_neon(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized symmetric per-block (qsi8d32) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of mr.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl The block length.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32p_f32_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32p_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be\n/// a multiple of 32.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qsi8d32p_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs,\n size_t lhs_stride, void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_quant_pack_qsi8d32p_f32_neon.c",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n\n#include \"kai_lhs_quant_pack_qsi8d32p_f32_neon.h\"\n\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_multiplier = sizeof(uint16_t);\n\ninline static size_t kai_num_bytes_per_block(size_t bl) {\n return bl * sizeof(int8_t) + kai_num_bytes_multiplier;\n}\n\ninline static size_t kai_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSERT((k % bl) == 0);\n return k / bl;\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t bl) {\n KAI_UNUSED(kr);\n\n return mr * kai_num_blocks_per_row(k, bl) * kai_num_bytes_per_block(bl);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qsi8d32p_f32_neon(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qsi8d32p_f32_neon(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32p_f32_neon(\n size_t m_idx, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((m_idx % mr) == 0);\n\n KAI_UNUSED(sr);\n KAI_UNUSED(kr);\n\n // The scales are stored after all the mr packed quantized values\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, bl);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32p_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr) {\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n KAI_UNUSED(sr);\n KAI_UNUSED(kr);\n\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return (num_rows * kai_lhs_packed_stride(k, mr, kr, bl));\n}\n\nvoid kai_run_lhs_quant_pack_qsi8d32p_f32_neon(\n size_t m, size_t k, size_t bl, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs,\n size_t lhs_stride, void* lhs_packed) {\n KAI_ASSUME((bl % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME(kr == 4);\n KAI_ASSUME((bl % 32) == 0);\n KAI_UNUSED(sr);\n KAI_UNUSED(m_idx_start);\n KAI_UNUSED(lhs_stride);\n\n if (m == 0) {\n return;\n }\n\n const size_t num_blocks = kai_num_blocks_per_row(k, bl);\n const size_t lhs_packed_stride = kai_lhs_packed_stride(k, mr, kr, bl);\n\n const float* lhs_ptr = lhs;\n int8_t* lhs_packed_start_ptr = lhs_packed;\n\n for (size_t m_idx = 0; m_idx < m; m_idx++) {\n int8_t* lhs_packed_ptr = lhs_packed_start_ptr;\n uint16_t* lhs_packed_scales =\n (uint16_t*)(lhs_packed_ptr + lhs_packed_stride - ((mr * num_blocks) * kai_num_bytes_multiplier));\n\n lhs_packed_ptr += (m_idx % mr) * kr;\n lhs_packed_scales += (m_idx % mr);\n\n for (size_t block_idx = 0; block_idx < num_blocks; block_idx++) {\n // Maximum absolute value of the block elements\n float amax = 0.0F;\n\n for (size_t bl_idx = 0; bl_idx < bl; bl_idx++) {\n amax = KAI_MAX(amax, fabsf(lhs_ptr[bl_idx]));\n }\n\n const float sf = amax / ((1 << 7) - 1);\n\n const float sf_inv = sf ? 1.0F / sf : 0.0F;\n\n for (size_t bl_idx = 0; bl_idx < bl; bl_idx += kr) {\n for (size_t kr_idx = 0; kr_idx < kr; ++kr_idx) {\n int32_t v0_s32 = (int32_t)(roundf(lhs_ptr[kr_idx] * sf_inv));\n lhs_packed_ptr[kr_idx] = (int8_t)v0_s32;\n }\n lhs_ptr += kr;\n lhs_packed_ptr += mr * kr;\n }\n\n // Num_blocks (rows) x Mr (cols)\n lhs_packed_scales[0] = kai_cast_f16_f32(sf);\n\n lhs_packed_scales += mr;\n }\n if (((m_idx + 1) % mr) == 0) {\n lhs_packed_start_ptr += lhs_packed_stride;\n }\n }\n}\n#endif // Architectural features check.\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.c",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n\n#include \"kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.h\"\n\n#include <arm_neon.h>\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_multiplier = sizeof(uint16_t);\nstatic const size_t kai_max_bl = KAI_SME_VEC_LENGTH_MAX_BYTES;\nstatic const size_t kai_nr_tile = 4;\nstatic const size_t kai_bl_multiple_of = 32;\n\n#if defined(_MSC_VER)\n#define KAI_ALIGNED_AS(N) __declspec(align(N))\n#elif defined(__GNUC__) || defined(__clang__)\n#define KAI_ALIGNED_AS(N) __attribute__((aligned(N)))\n#else\n#define KAI_ALIGNED_AS(N)\n#endif\n\nstatic inline void convert_chunk32_s1s0_s16s0(uint8_t* dst_lo, uint8_t* dst_hi, const uint8_t* src_ptr) {\n // Two sequential 128-bit loads followed by unzip have better throughput than a structured LD2.\n const uint8x16_t v0 = vld1q_u8(src_ptr);\n const uint8x16_t v1 = vld1q_u8(src_ptr + 16);\n\n const uint8x16_t even_bytes = vuzp1q_u8(v0, v1);\n const uint8x16_t odd_bytes = vuzp2q_u8(v0, v1);\n\n const uint8x16_t lo_pairs = vsliq_n_u8(even_bytes, odd_bytes, 4);\n const uint8x16_t hi_pairs = vsriq_n_u8(odd_bytes, even_bytes, 4);\n\n vst1q_u8(dst_lo, lo_pairs);\n vst1q_u8(dst_hi, hi_pairs);\n}\n\nstatic inline void convert_tail16_s1s0_s16s0(uint8_t* dst_lo, uint8_t* dst_hi, const uint8_t* src_ptr) {\n const uint8x16_t v0 = vld1q_u8(src_ptr);\n const uint8x16_t even_dup = vuzp1q_u8(v0, v0);\n const uint8x16_t odd_dup = vuzp2q_u8(v0, v0);\n\n const uint8x16_t lo_pairs_dup = vsliq_n_u8(even_dup, odd_dup, 4);\n const uint8x16_t hi_pairs_dup = vsriq_n_u8(odd_dup, even_dup, 4);\n const uint8x16_t packed = vcombine_u8(vget_low_u8(lo_pairs_dup), vget_low_u8(hi_pairs_dup));\n\n KAI_ALIGNED_AS(16) uint8_t tmp[16];\n vst1q_u8(tmp, packed);\n\n for (size_t i = 0; i < 8; ++i) {\n dst_lo[i] = tmp[i];\n dst_hi[i] = tmp[8 + i];\n }\n}\n\nstatic inline void convert_s1s0_s16s0_scalar(uint16_t* dst_blk, const uint8_t* src_blk, size_t bl) {\n const size_t half = bl / 2;\n const size_t split = bl / 4;\n\n for (size_t bl4_idx = 0; bl4_idx < bl / 4; bl4_idx++) {\n const size_t byte_idx0 = bl4_idx * 2;\n const size_t byte_idx1 = byte_idx0 + 1;\n uint8_t packed0 = 0;\n uint8_t packed1 = 0;\n\n if (byte_idx0 < split) {\n // First half of byte0: pack low nibbles into sequential order.\n const size_t k0 = byte_idx0 * 2;\n packed0 = (src_blk[k0] & 0xF) | (src_blk[k0 + 1] << 4);\n } else {\n // Second half of byte0: pack high nibbles into sequential order.\n const size_t k0 = byte_idx0 * 2 - half;\n packed0 = (src_blk[k0] >> 4) | (src_blk[k0 + 1] & 0xF0);\n }\n // Repeated for byte1\n if (byte_idx1 < split) {\n const size_t k1 = byte_idx1 * 2;\n packed1 = (src_blk[k1] & 0xF) | (src_blk[k1 + 1] << 4);\n } else {\n const size_t k1 = byte_idx1 * 2 - half;\n packed1 = (src_blk[k1] >> 4) | (src_blk[k1 + 1] & 0xF0);\n }\n dst_blk[bl4_idx] = (uint16_t)packed0 | ((uint16_t)packed1 << 8);\n }\n}\n\nstatic inline void convert_s1s0_s16s0_neon(uint16_t* dst_blk, const uint8_t* src_blk, size_t bl) {\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n\n const size_t half = bl / 2;\n const size_t split = bl / 4;\n\n uint8_t* dst_bytes = (uint8_t*)dst_blk;\n uint8_t* dst_low = dst_bytes;\n uint8_t* dst_high = dst_bytes + split;\n\n // Special-case bl == 32 (half == 16) so that the whole block is\n // handled with a single 128-bit load and store.\n if (half == 16) {\n const uint8x16_t v0 = vld1q_u8(src_blk);\n const uint8x16_t even_dup = vuzp1q_u8(v0, v0);\n const uint8x16_t odd_dup = vuzp2q_u8(v0, v0);\n\n const uint8x16_t lo_pairs_dup = vsliq_n_u8(even_dup, odd_dup, 4);\n const uint8x16_t hi_pairs_dup = vsriq_n_u8(odd_dup, even_dup, 4);\n\n const uint8x16_t packed = vcombine_u8(vget_low_u8(lo_pairs_dup), vget_low_u8(hi_pairs_dup));\n vst1q_u8(dst_bytes, packed);\n return;\n }\n\n const uint8_t* src_ptr = src_blk;\n uint8_t* dst_low_ptr = dst_low;\n uint8_t* dst_high_ptr = dst_high;\n\n size_t remaining = half;\n\n // Process two 32-byte chunks per iteration when possible to reduce loop\n // overhead while keeping all loads and stores 128-bit wide.\n for (; remaining >= 64; remaining -= 64) {\n convert_chunk32_s1s0_s16s0(dst_low_ptr, dst_high_ptr, src_ptr);\n convert_chunk32_s1s0_s16s0(dst_low_ptr + 16, dst_high_ptr + 16, src_ptr + 32);\n\n src_ptr += 64;\n dst_low_ptr += 32;\n dst_high_ptr += 32;\n }\n for (; remaining >= 32; remaining -= 32) {\n convert_chunk32_s1s0_s16s0(dst_low_ptr, dst_high_ptr, src_ptr);\n\n src_ptr += 32;\n dst_low_ptr += 16;\n dst_high_ptr += 16;\n }\n\n // Handle the remaining 16 bytes (if any) using a single 128-bit load\n // and store to a temporary buffer, followed by scalar writes.\n if (remaining != 0) {\n convert_tail16_s1s0_s16s0(dst_low_ptr, dst_high_ptr, src_ptr);\n }\n}\n\nstatic inline void convert_s1s0_s16s0_neon_rows4(\n uint16_t* dst0, uint16_t* dst1, uint16_t* dst2, uint16_t* dst3, const uint8_t* src0, const uint8_t* src1,\n const uint8_t* src2, const uint8_t* src3, size_t bl) {\n KAI_ASSUME((bl % kai_bl_multiple_of) == 0);\n\n const size_t half = bl / 2;\n const size_t split = bl / 4;\n\n uint8_t* dst0_bytes = (uint8_t*)dst0;\n uint8_t* dst1_bytes = (uint8_t*)dst1;\n uint8_t* dst2_bytes = (uint8_t*)dst2;\n uint8_t* dst3_bytes = (uint8_t*)dst3;\n\n uint8_t* dst0_low = dst0_bytes;\n uint8_t* dst1_low = dst1_bytes;\n uint8_t* dst2_low = dst2_bytes;\n uint8_t* dst3_low = dst3_bytes;\n\n uint8_t* dst0_high = dst0_bytes + split;\n uint8_t* dst1_high = dst1_bytes + split;\n uint8_t* dst2_high = dst2_bytes + split;\n uint8_t* dst3_high = dst3_bytes + split;\n\n // bl == 32 (half == 16) is common and can be handled without a loop.\n if (half == 16) {\n const uint8x16_t r0 = vld1q_u8(src0);\n const uint8x16_t r1 = vld1q_u8(src1);\n const uint8x16_t r2 = vld1q_u8(src2);\n const uint8x16_t r3 = vld1q_u8(src3);\n\n const uint8x16_t r0_even = vuzp1q_u8(r0, r0);\n const uint8x16_t r0_odd = vuzp2q_u8(r0, r0);\n const uint8x16_t r1_even = vuzp1q_u8(r1, r1);\n const uint8x16_t r1_odd = vuzp2q_u8(r1, r1);\n const uint8x16_t r2_even = vuzp1q_u8(r2, r2);\n const uint8x16_t r2_odd = vuzp2q_u8(r2, r2);\n const uint8x16_t r3_even = vuzp1q_u8(r3, r3);\n const uint8x16_t r3_odd = vuzp2q_u8(r3, r3);\n\n const uint8x16_t r0_lo = vsliq_n_u8(r0_even, r0_odd, 4);\n const uint8x16_t r0_hi = vsriq_n_u8(r0_odd, r0_even, 4);\n const uint8x16_t r1_lo = vsliq_n_u8(r1_even, r1_odd, 4);\n const uint8x16_t r1_hi = vsriq_n_u8(r1_odd, r1_even, 4);\n const uint8x16_t r2_lo = vsliq_n_u8(r2_even, r2_odd, 4);\n const uint8x16_t r2_hi = vsriq_n_u8(r2_odd, r2_even, 4);\n const uint8x16_t r3_lo = vsliq_n_u8(r3_even, r3_odd, 4);\n const uint8x16_t r3_hi = vsriq_n_u8(r3_odd, r3_even, 4);\n\n const uint8x16_t r0_packed = vcombine_u8(vget_low_u8(r0_lo), vget_low_u8(r0_hi));\n const uint8x16_t r1_packed = vcombine_u8(vget_low_u8(r1_lo), vget_low_u8(r1_hi));\n const uint8x16_t r2_packed = vcombine_u8(vget_low_u8(r2_lo), vget_low_u8(r2_hi));\n const uint8x16_t r3_packed = vcombine_u8(vget_low_u8(r3_lo), vget_low_u8(r3_hi));\n\n vst1q_u8(dst0_bytes, r0_packed);\n vst1q_u8(dst1_bytes, r1_packed);\n vst1q_u8(dst2_bytes, r2_packed);\n vst1q_u8(dst3_bytes, r3_packed);\n return;\n }\n\n const uint8_t* src0_ptr = src0;\n const uint8_t* src1_ptr = src1;\n const uint8_t* src2_ptr = src2;\n const uint8_t* src3_ptr = src3;\n\n size_t remaining = half;\n\n for (; remaining >= 64; remaining -= 64) {\n convert_chunk32_s1s0_s16s0(dst0_low, dst0_high, src0_ptr);\n convert_chunk32_s1s0_s16s0(dst0_low + 16, dst0_high + 16, src0_ptr + 32);\n convert_chunk32_s1s0_s16s0(dst1_low, dst1_high, src1_ptr);\n convert_chunk32_s1s0_s16s0(dst1_low + 16, dst1_high + 16, src1_ptr + 32);\n convert_chunk32_s1s0_s16s0(dst2_low, dst2_high, src2_ptr);\n convert_chunk32_s1s0_s16s0(dst2_low + 16, dst2_high + 16, src2_ptr + 32);\n convert_chunk32_s1s0_s16s0(dst3_low, dst3_high, src3_ptr);\n convert_chunk32_s1s0_s16s0(dst3_low + 16, dst3_high + 16, src3_ptr + 32);\n\n src0_ptr += 64;\n src1_ptr += 64;\n src2_ptr += 64;\n src3_ptr += 64;\n\n dst0_low += 32;\n dst1_low += 32;\n dst2_low += 32;\n dst3_low += 32;\n\n dst0_high += 32;\n dst1_high += 32;\n dst2_high += 32;\n dst3_high += 32;\n }\n for (; remaining >= 32; remaining -= 32) {\n convert_chunk32_s1s0_s16s0(dst0_low, dst0_high, src0_ptr);\n convert_chunk32_s1s0_s16s0(dst1_low, dst1_high, src1_ptr);\n convert_chunk32_s1s0_s16s0(dst2_low, dst2_high, src2_ptr);\n convert_chunk32_s1s0_s16s0(dst3_low, dst3_high, src3_ptr);\n\n src0_ptr += 32;\n src1_ptr += 32;\n src2_ptr += 32;\n src3_ptr += 32;\n\n dst0_low += 16;\n dst1_low += 16;\n dst2_low += 16;\n dst3_low += 16;\n\n dst0_high += 16;\n dst1_high += 16;\n dst2_high += 16;\n dst3_high += 16;\n }\n\n if (remaining != 0) {\n convert_tail16_s1s0_s16s0(dst0_low, dst0_high, src0_ptr);\n convert_tail16_s1s0_s16s0(dst1_low, dst1_high, src1_ptr);\n convert_tail16_s1s0_s16s0(dst2_low, dst2_high, src2_ptr);\n convert_tail16_s1s0_s16s0(dst3_low, dst3_high, src3_ptr);\n }\n}\n\nstatic inline void interleave_rows4_u16(\n uint16_t* dst_row_base, size_t nr, const uint16_t* row0, const uint16_t* row1, const uint16_t* row2,\n const uint16_t* row3, size_t bl4) {\n uint16_t* dst_ptr = dst_row_base;\n\n const uint16_t* row0_ptr = row0;\n const uint16_t* row1_ptr = row1;\n const uint16_t* row2_ptr = row2;\n const uint16_t* row3_ptr = row3;\n\n size_t remaining = bl4;\n\n // Store two columns per iteration to reduce branch and address-generation\n // overhead while keeping all loads sequential.\n for (; remaining >= 2; remaining -= 2) {\n const uint64_t packed0 = (uint64_t)row0_ptr[0] | ((uint64_t)row1_ptr[0] << 16) | ((uint64_t)row2_ptr[0] << 32) |\n ((uint64_t)row3_ptr[0] << 48);\n const uint64_t packed1 = (uint64_t)row0_ptr[1] | ((uint64_t)row1_ptr[1] << 16) | ((uint64_t)row2_ptr[1] << 32) |\n ((uint64_t)row3_ptr[1] << 48);\n\n *((uint64_t*)dst_ptr) = packed0;\n *((uint64_t*)(dst_ptr + nr)) = packed1;\n\n row0_ptr += 2;\n row1_ptr += 2;\n row2_ptr += 2;\n row3_ptr += 2;\n dst_ptr += 2 * nr;\n }\n\n if (remaining != 0) {\n const uint64_t packed = (uint64_t)row0_ptr[0] | ((uint64_t)row1_ptr[0] << 16) | ((uint64_t)row2_ptr[0] << 32) |\n ((uint64_t)row3_ptr[0] << 48);\n *((uint64_t*)dst_ptr) = packed;\n }\n}\n\nstatic inline void store_row_strided_u16(\n uint16_t* dst_block_base, size_t row_idx, size_t nr, const uint16_t* src_vals, size_t bl4, size_t nr1, size_t nr2,\n size_t nr3, size_t dst_step) {\n const uint16_t* src_ptr = src_vals;\n uint16_t* dst_ptr = dst_block_base + row_idx;\n\n size_t remaining_bl4 = bl4;\n for (; remaining_bl4 >= 4; remaining_bl4 -= 4) {\n dst_ptr[0] = src_ptr[0];\n dst_ptr[nr1] = src_ptr[1];\n dst_ptr[nr2] = src_ptr[2];\n dst_ptr[nr3] = src_ptr[3];\n src_ptr += 4;\n dst_ptr += dst_step;\n }\n for (; remaining_bl4 != 0; --remaining_bl4) {\n *dst_ptr = *src_ptr;\n ++src_ptr;\n dst_ptr += nr;\n }\n}\n\ninline static size_t kai_num_blocks_per_row(size_t k, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n return kai_roundup(k, bl) / bl;\n}\n\ninline static size_t kai_num_bytes_per_block(size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n return (bl / 2) + kai_num_bytes_multiplier;\n}\n\ninline static size_t kai_rhs_stride(size_t k, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n const size_t num_blocks_per_row = kai_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_num_bytes_per_block(bl);\n\n return num_bytes_per_block * num_blocks_per_row;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n const size_t num_blocks_per_row = kai_num_blocks_per_row(k, bl);\n const size_t num_bytes_per_block = kai_num_bytes_per_block(bl);\n\n return nr * (num_bytes_per_block * num_blocks_per_row);\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME((n_idx % nr) == 0);\n\n // The scales are stored after all the nr packed quantized values\n return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(k, nr, kr, bl);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t bl) {\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(k, nr, kr, bl);\n}\n\nvoid kai_run_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t bl, const uint8_t* rhs,\n const float* bias, void* rhs_packed, size_t extra_bytes, const struct kai_rhs_pack_qs4cxs1s0_param* params) {\n KAI_ASSUME(num_groups == 1);\n KAI_ASSUME((bl % 32) == 0);\n KAI_ASSUME(bl <= kai_max_bl);\n KAI_ASSUME((k % 2) == 0);\n KAI_ASSUME((k % kr) == 0);\n KAI_ASSUME((k % bl) == 0);\n KAI_ASSUME(bias == NULL);\n KAI_ASSUME(extra_bytes == 0);\n\n KAI_ASSUME(kr == 4);\n KAI_ASSUME(sr == 2);\n KAI_ASSUME(kr >= 1 && kr <= 16);\n KAI_ASSUME(rhs != NULL);\n KAI_ASSUME(rhs_packed != NULL);\n KAI_ASSUME(params != NULL);\n KAI_ASSUME(params->rhs_zero_point == 8);\n KAI_ASSUME(params->lhs_zero_point == 1);\n\n // Note: The input matrix (rhs) is expected with:\n // \"k\" columns and \"n\" rows (NxK)\n\n const size_t num_blocks = k / bl;\n const size_t rhs_stride = kai_rhs_stride(k, bl);\n const size_t rhs_packed_stride =\n kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(k, nr, kr, bl);\n const size_t num_bytes_per_block = kai_num_bytes_per_block(bl);\n const size_t bl4 = bl / 4;\n const size_t block_stride_u16 = bl4 * nr;\n const size_t scales_offset = rhs_packed_stride - (nr * num_blocks * kai_num_bytes_multiplier);\n\n uint8_t* rhs_packed_ptr = rhs_packed;\n\n if (kr == 4 && sr == 2 && (nr % kai_nr_tile) == 0 && (bl % kai_bl_multiple_of) == 0) {\n // Reuse scratch buffers across all rows and blocks. Four buffers allow\n // row-block interleaving with wide stores across the contiguous row\n // dimension.\n KAI_ALIGNED_AS(16) uint16_t blk_s1s0_rows_0[KAI_SME_VEC_LENGTH_MAX_BYTES / 4] = {0};\n KAI_ALIGNED_AS(16) uint16_t blk_s1s0_rows_1[KAI_SME_VEC_LENGTH_MAX_BYTES / 4] = {0};\n KAI_ALIGNED_AS(16) uint16_t blk_s1s0_rows_2[KAI_SME_VEC_LENGTH_MAX_BYTES / 4] = {0};\n KAI_ALIGNED_AS(16) uint16_t blk_s1s0_rows_3[KAI_SME_VEC_LENGTH_MAX_BYTES / 4] = {0};\n\n const size_t nr1 = nr;\n const size_t nr2 = nr1 + nr;\n const size_t nr3 = nr2 + nr;\n const size_t dst_step = nr3 + nr;\n\n for (size_t group_start = 0; group_start < n; group_start += nr, rhs_packed_ptr += rhs_packed_stride) {\n const size_t rows_in_group = KAI_MIN(n - group_start, nr);\n const uint8_t* rhs_group_base = rhs + group_start * rhs_stride;\n\n uint16_t* group_base_u16 = (uint16_t*)rhs_packed_ptr;\n uint16_t* rhs_packed_scales = (uint16_t*)(rhs_packed_ptr + scales_offset);\n\n const uint8_t* src_block_base = rhs_group_base;\n uint16_t* dst_block_base = group_base_u16;\n uint16_t* scales_block = rhs_packed_scales;\n\n for (size_t block_idx = 0; block_idx < num_blocks; ++block_idx) {\n size_t row_idx = 0;\n const uint8_t* src_row_block = src_block_base;\n\n for (; row_idx + kai_nr_tile <= rows_in_group;\n row_idx += kai_nr_tile, src_row_block += kai_nr_tile * rhs_stride) {\n const uint8_t* restrict src_row0 = src_row_block;\n const uint8_t* restrict src_row1 = src_row0 + rhs_stride;\n const uint8_t* restrict src_row2 = src_row1 + rhs_stride;\n const uint8_t* restrict src_row3 = src_row2 + rhs_stride;\n\n const uint16_t scale0 = *((const uint16_t*)src_row0);\n const uint16_t scale1 = *((const uint16_t*)src_row1);\n const uint16_t scale2 = *((const uint16_t*)src_row2);\n const uint16_t scale3 = *((const uint16_t*)src_row3);\n\n const uint64_t packed_scales = (uint64_t)scale0 | ((uint64_t)scale1 << 16) |\n ((uint64_t)scale2 << 32) | ((uint64_t)scale3 << 48);\n *((uint64_t*)(scales_block + row_idx)) = packed_scales;\n\n convert_s1s0_s16s0_neon_rows4(\n blk_s1s0_rows_0, blk_s1s0_rows_1, blk_s1s0_rows_2, blk_s1s0_rows_3,\n src_row0 + kai_num_bytes_multiplier, src_row1 + kai_num_bytes_multiplier,\n src_row2 + kai_num_bytes_multiplier, src_row3 + kai_num_bytes_multiplier, bl);\n\n interleave_rows4_u16(\n dst_block_base + row_idx, nr, blk_s1s0_rows_0, blk_s1s0_rows_1, blk_s1s0_rows_2,\n blk_s1s0_rows_3, bl4);\n }\n\n for (; row_idx < rows_in_group; ++row_idx, src_row_block += rhs_stride) {\n const uint16_t* blk_scale_ptr = (const uint16_t*)src_row_block;\n const uint8_t* blk_s16s0 = src_row_block + kai_num_bytes_multiplier;\n\n scales_block[row_idx] = *blk_scale_ptr;\n\n convert_s1s0_s16s0_neon(blk_s1s0_rows_0, blk_s16s0, bl);\n store_row_strided_u16(dst_block_base, row_idx, nr, blk_s1s0_rows_0, bl4, nr1, nr2, nr3, dst_step);\n }\n\n src_block_base += num_bytes_per_block;\n dst_block_base += block_stride_u16;\n scales_block += nr;\n }\n }\n return;\n }\n\n for (uint64_t n_idx = 0; n_idx < n; n_idx++) {\n uint16_t* rhs_packed_scales =\n (uint16_t*)(rhs_packed_ptr + rhs_packed_stride - (nr * num_blocks * kai_num_bytes_multiplier));\n\n for (size_t block_idx = 0; block_idx < num_blocks; block_idx++) {\n uint16_t blk_s1s0[KAI_SME_VEC_LENGTH_MAX_BYTES / 4];\n\n const uint16_t* blk_scale_ptr =\n (const uint16_t*)(rhs + (block_idx * num_bytes_per_block) + n_idx * rhs_stride);\n const uint8_t* blk_s16s0 = (const uint8_t*)blk_scale_ptr + kai_num_bytes_multiplier;\n\n convert_s1s0_s16s0_scalar(blk_s1s0, blk_s16s0, bl);\n\n for (size_t bl4_idx = 0; bl4_idx < bl / 4; bl4_idx++) {\n // Uint16 holds 4 int4 values\n ((uint16_t*)rhs_packed_ptr)[(block_idx * bl / 4 + bl4_idx) * nr + (n_idx % nr)] = blk_s1s0[bl4_idx];\n }\n\n // Num. block (rows) x Nr (cols)\n rhs_packed_scales[(n_idx % nr) + block_idx * nr] = *blk_scale_ptr;\n }\n\n if (((n_idx + 1) % nr) == 0) {\n rhs_packed_ptr += rhs_packed_stride;\n }\n }\n}\n#endif // Architectural features check.\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.h",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the offset in bytes for the RHS matrix (not packed), which holds\n/// the int4 values in a N x K matrix, where N is number of rows and K is the number of columns.\n///\n/// Two int4 K values are stored in one byte. These values are stored in blocks, where each block\n/// has it own scale factor. The scale factor is expected to be a f16 value and stored at the beginning of each block.\n/// The first byte in the block holds the K-index + 0 and K-index + 16 values.\n/// The K-index + 0 value is stored in the lower order part of the byte (low nibble) while\n/// the K-index + 16 value is stored in the higher order part (high nibble).\n/// For example, if the block length is 32, the values are stored in the following order:\n/// |byte(s16, s0),byte(s17, s1),byte(s18, s2),...,byte(s31, s15),float16(scale)|\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n_idx, //\n size_t rhs_stride); //\n\n/// Gets the offset in bytes for the packed RHS matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K)\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n_idx, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl); //\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a\n/// multiple of 32.\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl);\n\n/// Gets the size in bytes for the quantized and packed RHS matrix.\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple\n/// of 32.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t bl); //\n\n/// Runs the RHS packing micro-kernel.\n///\n/// The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Two int4 values are stored in one byte. The lower order part of the byte (low) holds\n/// the first nibble (K-index + 0). The higher order of the byte holds the second nibble (K-index + 1).\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of columns of the output matrix (N).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] bl The block length, which defines the number of\n/// K values stored in a single block. It must be a multiple of 32.\n/// @param[in] rhs The RHS matrix containing the 4-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).\n/// @param[in] bias The biases.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n size_t bl, //\n const uint8_t* rhs, //\n const float* bias, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_qs4cxs1s0_param* params); //\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "binding.cpp",
|
| 61 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa against gemm_qs4c32_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, int M)\n#include <arm_neon.h>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_quant_pack_qsi8d32p_f32_neon.h\"\n#include \"kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr size_t BL = 32;\n// Interleave B_q4 [N, K/2] and the raw f16 B_scales [N, K/32] into the per-block [scale f16][16 data bytes]\n// layout kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon expects.\nvoid interleave_qs4c32(const uint8_t* b_q4, const uint16_t* b_scales, uint8_t* dst) {\n const size_t nb = K / BL, data = BL / 2;\n for (size_t n = 0; n < (size_t)N; ++n)\n for (size_t b = 0; b < nb; ++b) {\n std::memcpy(dst, &b_scales[n * nb + b], sizeof(uint16_t));\n dst += sizeof(uint16_t);\n std::memcpy(dst, b_q4 + (n * nb + b) * data, data);\n dst += data;\n }\n}\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n// Widen n raw fp16 patterns to fp32 (exact).\nvoid widen_f16(const uint16_t* src, float* dst, size_t n) {\n size_t i = 0;\n for (; i + 4 <= n; i += 4) vst1q_f32(dst + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(src + i))));\n for (; i < n; ++i) dst[i] = vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(src[i]))), 0);\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, int M)\n{\n // The SME2 mopa kernel takes any M in a single call, so there is no GEMV dispatch here.\n const size_t mr = kai_get_mr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32p_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_quant_pack_qsi8d32p_f32_neon((size_t)M, (size_t)K, BL, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q4_snap = nullptr;\n static size_t B_q4_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q4_snap, B_q4_snap_size, B_q4, (size_t)N*K/2)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(uint16_t)*N*(K/BL));\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon((size_t)N, (size_t)K, nr, kr, BL);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n const size_t nb = K / BL;\n uint8_t* inter = static_cast<uint8_t*>(std::malloc((size_t)N * nb * (BL / 2 + sizeof(uint16_t))));\n if (inter == nullptr) return -1;\n interleave_qs4c32(B_q4, B_scales, inter);\n struct kai_rhs_pack_qs4cxs1s0_param params;\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 8;\n kai_run_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon(1, (size_t)N, (size_t)K, nr, kr, sr, BL, inter, nullptr, rhs_packed, 0, ¶ms);\n std::free(inter);\n }\n\n kai_run_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa((size_t)M, (size_t)N, (size_t)K, BL, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n return 0;\n}\n"
|
| 62 |
+
}
|
| 63 |
+
]
|
| 64 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qs4cx_n512_k512.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qs4cx_n512_k512",
|
| 3 |
+
"definition": "gemm_qs4cx_n512_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qs4cx_n512_k512: KleidiAI kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"graviton4",
|
| 11 |
+
"aarch64-sve2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"dotprod",
|
| 17 |
+
"i8mm"
|
| 18 |
+
],
|
| 19 |
+
"compile_flags": [
|
| 20 |
+
"-O3",
|
| 21 |
+
"-march=armv8.2-a+dotprod+i8mm"
|
| 22 |
+
],
|
| 23 |
+
"link_flags": []
|
| 24 |
+
},
|
| 25 |
+
"sources": [
|
| 26 |
+
{
|
| 27 |
+
"path": "kai/kai_common.h",
|
| 28 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"path": "kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm.h",
|
| 32 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Micro-kernel dependencies\n///\n/// -# kai_lhs_quant_pack_qai8dxp_f32 to dynamically quantize and pack the LHS matrix\n/// -# kai_rhs_pack_kxn_qsi4cxp_qs4cxs1s0 OR kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0 to pack the RHS matrix\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix with\n/// the @ref kai_lhs_quant_pack_qai8dxp_f32 micro-kernel\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix with\n/// the @ref kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0 micro-kernel\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void);\n\n/// Gets the kr value, which must be used to pack the RHS matrix with\n/// the @ref kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0 micro-kernel\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void);\n\n/// Gets the sr value, which must be used to pack the RHS matrix with\n/// the @ref kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0 micro-kernel\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qa8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of 4.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(size_t m_idx, size_t k);\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed 4-bit quantized symmetric per-channel (qsu4cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of 8.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(size_t n_idx, size_t k);\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of 4.\n/// @param[in] n_idx Column index in the DST matrix. It must be a multiple of 8.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(\n size_t m_idx, size_t n_idx, size_t dst_stride);\n\n/// Gets the size in bytes for the destination matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(size_t m, size_t n);\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Signed 8-bit quantized asymmetric per-row (qai8dx) and packed\n/// RHS matrix: Signed 4-bit quantized symmetric per-channel (qsu4cx) and packed.\n/// Output tile: (rows x cols) = 4 x 8\n/// Accumulation performed in a single for loop: 32\n/// Instruction used: i8mm\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS.\n/// @param[in] lhs_packed The LHS matrix packed.\n/// When the activation are dynamically quantized, you can obtain this matrix\n/// by calling the @ref kai_lhs_quant_pack_qai8dxp_f32 micro-kernel which performs\n/// both the dynamic quantization to 8-bit and activation packing in a single step.\n/// @param[in] rhs_packed The RHS matrix packed, which is obtained by calling @ref\n/// kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0\n/// @param[out] dst Result of the vector-by-matrix\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. For now, it must be sizeof(float)\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(\n size_t m, size_t n, size_t k, const void* lhs_packed, const void* rhs_packed, float* dst, size_t dst_stride_row,\n size_t dst_stride_col, float scalar_min, float scalar_max);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"path": "kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm.c",
|
| 36 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n// Do not flag up inline assembly blocks\n#pragma GCC diagnostic ignored \"-Woverlength-strings\"\n\n#if !defined(__ARM_FEATURE_MATMUL_INT8)\n#error \"I8mm extension required to compile this micro-kernel\"\n#else\n#include \"kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm.h\"\n\n#include <arm_neon.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_m_step = 4;\nstatic const size_t kai_n_step = 8;\nstatic const size_t kai_mr = 4;\nstatic const size_t kai_nr = 8;\nstatic const size_t kai_kr = 16;\nstatic const size_t kai_sr = 2;\nstatic const size_t kai_num_bytes_multiplier_lhs = sizeof(float);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_offset_lhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Since we pack a float and int32 value at the end of the row,\n // we must make sure that k is a multiple of 4 for alignment\n size_t kr_sr_roundedup4 = kai_roundup(kai_kr * kai_sr, 4);\n return kai_roundup(k, kr_sr_roundedup4);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return kai_mr * (k_internal * sizeof(int8_t) + kai_num_bytes_multiplier_lhs + kai_num_bytes_offset_lhs);\n}\n\ninline static size_t kai_rhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return kai_nr * ((k_internal / 2) + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void) {\n return kai_m_step;\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void) {\n return kai_n_step;\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void) {\n return kai_mr;\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void) {\n return kai_nr;\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(size_t m_idx, size_t k) {\n KAI_ASSERT((m_idx % kai_mr) == 0);\n\n return (m_idx / kai_mr) * kai_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(size_t n_idx, size_t k) {\n KAI_ASSERT((n_idx % kai_nr) == 0);\n\n return (n_idx / kai_nr) * kai_rhs_packed_stride(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSERT((m_idx % kai_m_step) == 0);\n KAI_ASSERT((n_idx % kai_n_step) == 0);\n\n return (n_idx * sizeof(float)) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(size_t m, size_t n) {\n return m * n * sizeof(float);\n}\n\nvoid kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm(\n size_t m, size_t n, size_t k, const void* restrict lhs_packed, const void* restrict rhs_packed,\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, size_t dst_stride_col, float scalar_min, float scalar_max) {\n KAI_ASSERT(dst_stride_col == sizeof(float));\n\n if (m == 0) {\n return;\n }\n\n const size_t k_internal = kai_k_roundedup(k);\n\n size_t num_blocks = k_internal / 32;\n\n float clamp_vals[2] = {scalar_min, scalar_max};\n\n __asm__ __volatile__(\n \"mov x28, #0x80\\n\"\n \"mov x20, #0x20\\n\"\n \"movi v12.16b, #0xf0\\n\"\n \"mov x27, %x[m]\\n\"\n \"madd x28, %x[num_blocks], x28, x20\\n\"\n \"cbz x27, 11f\\n\"\n \"1:\" // Row loop\n \"mov x26, %x[rhs_packed]\\n\"\n \"mov x25, %x[n]\\n\"\n \"add x24, %x[dst], %x[dst_stride_row], LSL #2\\n\"\n \"2:\" // Column loop\n \"mov x21, %x[lhs_packed]\\n\"\n \"movi v11.4s, #0x0\\n\"\n \"movi v10.4s, #0x0\\n\"\n \"mov x20, %x[num_blocks]\\n\"\n \"movi v9.4s, #0x0\\n\"\n \"movi v8.4s, #0x0\\n\"\n \"movi v7.4s, #0x0\\n\"\n \"movi v6.4s, #0x0\\n\"\n \"movi v5.4s, #0x0\\n\"\n \"movi v4.4s, #0x0\\n\"\n \"3:\" // Sub block loop\n \"ldr q3, [x26, #0x0]\\n\"\n \"ldr q2, [x26, #0x10]\\n\"\n \"subs x20, x20, #0x1\\n\"\n \"ldr q1, [x26, #0x20]\\n\"\n \"ldr q0, [x26, #0x30]\\n\"\n \"ldr q31, [x21, #0x0]\\n\"\n \"ldr q30, [x21, #0x10]\\n\"\n \"ldr q29, [x26, #0x40]\\n\"\n \"ldr q28, [x26, #0x50]\\n\"\n \"shl v19.16b, v3.16b, #0x4\\n\"\n \"shl v18.16b, v2.16b, #0x4\\n\"\n \"ldr q27, [x26, #0x60]\\n\"\n \"ldr q26, [x26, #0x70]\\n\"\n \"shl v17.16b, v1.16b, #0x4\\n\"\n \"shl v16.16b, v0.16b, #0x4\\n\"\n \"ldr q25, [x21, #0x20]\\n\"\n \"ldr q24, [x21, #0x30]\\n\"\n \"and v3.16b, v3.16b, v12.16b\\n\"\n \"and v2.16b, v2.16b, v12.16b\\n\"\n \"ldr q23, [x21, #0x40]\\n\"\n \"ldr q22, [x21, #0x50]\\n\"\n \".inst 0x4e93a7eb // smmla v11.4s, v31.16b, v19.16b\\n\"\n \".inst 0x4e92a7e9 // smmla v9.4s, v31.16b, v18.16b\\n\"\n \"ldr q21, [x21, #0x60]\\n\"\n \"ldr q20, [x21, #0x70]\\n\"\n \".inst 0x4e91a7ea // smmla v10.4s, v31.16b, v17.16b\\n\"\n \".inst 0x4e90a7e8 // smmla v8.4s, v31.16b, v16.16b\\n\"\n \".inst 0x4e93a7c7 // smmla v7.4s, v30.16b, v19.16b\\n\"\n \".inst 0x4e92a7c5 // smmla v5.4s, v30.16b, v18.16b\\n\"\n \"shl v19.16b, v29.16b, #0x4\\n\"\n \"add x26, x26, #0x80\\n\"\n \".inst 0x4e91a7c6 // smmla v6.4s, v30.16b, v17.16b\\n\"\n \".inst 0x4e90a7c4 // smmla v4.4s, v30.16b, v16.16b\\n\"\n \"shl v18.16b, v28.16b, #0x4\\n\"\n \"add x21, x21, #0x80\\n\"\n \"shl v17.16b, v27.16b, #0x4\\n\"\n \"shl v16.16b, v26.16b, #0x4\\n\"\n \".inst 0x4e93a72b // smmla v11.4s, v25.16b, v19.16b\\n\"\n \"and v1.16b, v1.16b, v12.16b\\n\"\n \"and v0.16b, v0.16b, v12.16b\\n\"\n \".inst 0x4e92a729 // smmla v9.4s, v25.16b, v18.16b\\n\"\n \".inst 0x4e93a707 // smmla v7.4s, v24.16b, v19.16b\\n\"\n \".inst 0x4e92a705 // smmla v5.4s, v24.16b, v18.16b\\n\"\n \"and v29.16b, v29.16b, v12.16b\\n\"\n \".inst 0x4e91a72a // smmla v10.4s, v25.16b, v17.16b\\n\"\n \".inst 0x4e90a728 // smmla v8.4s, v25.16b, v16.16b\\n\"\n \"and v28.16b, v28.16b, v12.16b\\n\"\n \".inst 0x4e91a706 // smmla v6.4s, v24.16b, v17.16b\\n\"\n \".inst 0x4e90a704 // smmla v4.4s, v24.16b, v16.16b\\n\"\n \"and v27.16b, v27.16b, v12.16b\\n\"\n \".inst 0x4e83a6eb // smmla v11.4s, v23.16b, v3.16b\\n\"\n \".inst 0x4e82a6e9 // smmla v9.4s, v23.16b, v2.16b\\n\"\n \"and v26.16b, v26.16b, v12.16b\\n\"\n \".inst 0x4e83a6c7 // smmla v7.4s, v22.16b, v3.16b\\n\"\n \".inst 0x4e82a6c5 // smmla v5.4s, v22.16b, v2.16b\\n\"\n \".inst 0x4e81a6ea // smmla v10.4s, v23.16b, v1.16b\\n\"\n \".inst 0x4e80a6e8 // smmla v8.4s, v23.16b, v0.16b\\n\"\n \".inst 0x4e81a6c6 // smmla v6.4s, v22.16b, v1.16b\\n\"\n \".inst 0x4e80a6c4 // smmla v4.4s, v22.16b, v0.16b\\n\"\n \".inst 0x4e9da6ab // smmla v11.4s, v21.16b, v29.16b\\n\"\n \".inst 0x4e9ca6a9 // smmla v9.4s, v21.16b, v28.16b\\n\"\n \".inst 0x4e9da687 // smmla v7.4s, v20.16b, v29.16b\\n\"\n \".inst 0x4e9ca685 // smmla v5.4s, v20.16b, v28.16b\\n\"\n \".inst 0x4e9ba6aa // smmla v10.4s, v21.16b, v27.16b\\n\"\n \".inst 0x4e9aa6a8 // smmla v8.4s, v21.16b, v26.16b\\n\"\n \".inst 0x4e9ba686 // smmla v6.4s, v20.16b, v27.16b\\n\"\n \".inst 0x4e9aa684 // smmla v4.4s, v20.16b, v26.16b\\n\"\n \"bgt 3b\\n\"\n \"ldr q20, [x26, #0x0]\\n\"\n \"ldr q19, [x26, #0x10]\\n\"\n \"uzp1 v0.2d, v11.2d, v9.2d\\n\"\n \"uzp2 v31.2d, v11.2d, v9.2d\\n\"\n \"ld1 { v18.4s }, [x21]\\n\"\n \"ldr q17, [x26, #0x20]\\n\"\n \"uzp1 v30.2d, v10.2d, v8.2d\\n\"\n \"uzp2 v29.2d, v10.2d, v8.2d\\n\"\n \"ldr q28, [x26, #0x30]\\n\"\n \"uzp1 v27.2d, v7.2d, v5.2d\\n\"\n \"uzp2 v26.2d, v7.2d, v5.2d\\n\"\n \"add x21, x21, #0x10\\n\"\n \"ldr q16, [x21, #0x0]\\n\"\n \"uzp1 v25.2d, v6.2d, v4.2d\\n\"\n \"uzp2 v24.2d, v6.2d, v4.2d\\n\"\n \"add x26, x26, #0x40\\n\"\n \"mla v0.4s, v20.4s, v18.s[0]\\n\"\n \"mla v30.4s, v19.4s, v18.s[0]\\n\"\n \"mla v31.4s, v20.4s, v18.s[1]\\n\"\n \"mla v29.4s, v19.4s, v18.s[1]\\n\"\n \"mla v27.4s, v20.4s, v18.s[2]\\n\"\n \"mla v25.4s, v19.4s, v18.s[2]\\n\"\n \"fmul v23.4s, v17.4s, v16.s[0]\\n\"\n \"mla v26.4s, v20.4s, v18.s[3]\\n\"\n \"mla v24.4s, v19.4s, v18.s[3]\\n\"\n \"fmul v22.4s, v28.4s, v16.s[0]\\n\"\n \"scvtf v0.4s, v0.4s\\n\"\n \"scvtf v30.4s, v30.4s\\n\"\n \"fmul v21.4s, v17.4s, v16.s[1]\\n\"\n \"scvtf v31.4s, v31.4s\\n\"\n \"fmul v20.4s, v28.4s, v16.s[1]\\n\"\n \"scvtf v29.4s, v29.4s\\n\"\n \"fmul v19.4s, v17.4s, v16.s[2]\\n\"\n \"scvtf v27.4s, v27.4s\\n\"\n \"fmul v18.4s, v28.4s, v16.s[2]\\n\"\n \"scvtf v25.4s, v25.4s\\n\"\n \"fmul v17.4s, v17.4s, v16.s[3]\\n\"\n \"scvtf v26.4s, v26.4s\\n\"\n \"fmul v16.4s, v28.4s, v16.s[3]\\n\"\n \"scvtf v24.4s, v24.4s\\n\"\n \"fmul v11.4s, v0.4s, v23.4s\\n\"\n \"fmul v10.4s, v30.4s, v22.4s\\n\"\n \"fmul v9.4s, v31.4s, v21.4s\\n\"\n \"fmul v8.4s, v29.4s, v20.4s\\n\"\n \"fmul v7.4s, v27.4s, v19.4s\\n\"\n \"fmul v6.4s, v25.4s, v18.4s\\n\"\n \"fmul v5.4s, v26.4s, v17.4s\\n\"\n \"fmul v4.4s, v24.4s, v16.4s\\n\"\n \"ldr q19, [x26, #0x0]\\n\"\n \"ldr q18, [x26, #0x10]\\n\"\n \"add x20, %x[clamp_vals], #0x4\\n\"\n \"cmp x25, #0x8\\n\"\n \"ld1r { v17.4s }, [%x[clamp_vals]]\\n\"\n \"ld1r { v16.4s }, [x20]\\n\"\n \"add x26, x26, #0x20\\n\"\n \"fadd v11.4s, v11.4s, v19.4s\\n\"\n \"fadd v10.4s, v10.4s, v18.4s\\n\"\n \"fadd v9.4s, v9.4s, v19.4s\\n\"\n \"fadd v8.4s, v8.4s, v18.4s\\n\"\n \"fadd v7.4s, v7.4s, v19.4s\\n\"\n \"fadd v6.4s, v6.4s, v18.4s\\n\"\n \"fadd v5.4s, v5.4s, v19.4s\\n\"\n \"fadd v4.4s, v4.4s, v18.4s\\n\"\n \"fmax v11.4s, v11.4s, v17.4s\\n\"\n \"fmax v10.4s, v10.4s, v17.4s\\n\"\n \"fmax v9.4s, v9.4s, v17.4s\\n\"\n \"fmax v8.4s, v8.4s, v17.4s\\n\"\n \"fmax v7.4s, v7.4s, v17.4s\\n\"\n \"fmax v6.4s, v6.4s, v17.4s\\n\"\n \"fmax v5.4s, v5.4s, v17.4s\\n\"\n \"fmax v4.4s, v4.4s, v17.4s\\n\"\n \"fmin v11.4s, v11.4s, v16.4s\\n\"\n \"fmin v10.4s, v10.4s, v16.4s\\n\"\n \"fmin v9.4s, v9.4s, v16.4s\\n\"\n \"fmin v8.4s, v8.4s, v16.4s\\n\"\n \"fmin v7.4s, v7.4s, v16.4s\\n\"\n \"fmin v6.4s, v6.4s, v16.4s\\n\"\n \"fmin v5.4s, v5.4s, v16.4s\\n\"\n \"fmin v4.4s, v4.4s, v16.4s\\n\"\n \"blt 5f\\n\"\n \"mov x20, %x[dst]\\n\"\n \"cmp x27, #0x1\\n\"\n \"str q11, [x20, #0x0]\\n\"\n \"str q10, [x20, #0x10]\\n\"\n \"add x20, x20, %x[dst_stride_row]\\n\"\n \"ble 10f\\n\"\n \"cmp x27, #0x2\\n\"\n \"str q9, [x20, #0x0]\\n\"\n \"str q8, [x20, #0x10]\\n\"\n \"add x20, x20, %x[dst_stride_row]\\n\"\n \"ble 10f\\n\"\n \"cmp x27, #0x3\\n\"\n \"str q7, [x20, #0x0]\\n\"\n \"str q6, [x20, #0x10]\\n\"\n \"add x20, x20, %x[dst_stride_row]\\n\"\n \"ble 10f\\n\"\n \"str q5, [x20, #0x0]\\n\"\n \"str q4, [x20, #0x10]\\n\"\n \"b 10f\\n\"\n \"5:\" // Partial output\n \"mov x23, %x[dst]\\n\"\n \"cmp x27, #0x1\\n\"\n \"add x22, x23, %x[dst_stride_row]\\n\"\n \"csel x22, x22, x23, GT\\n\"\n \"cmp x27, #0x2\\n\"\n \"add x21, x23, %x[dst_stride_row], LSL #1\\n\"\n \"csel x21, x21, x22, GT\\n\"\n \"cmp x27, #0x3\\n\"\n \"add x20, x21, %x[dst_stride_row]\\n\"\n \"csel x20, x20, x21, GT\\n\"\n \"tbz x25, #2, 7f\\n\"\n \"st1 { v5.4s }, [x20], #0x10\\n\"\n \"st1 { v7.4s }, [x21], #0x10\\n\"\n \"st1 { v9.4s }, [x22], #0x10\\n\"\n \"st1 { v11.4s }, [x23], #0x10\\n\"\n \"tbz x25, #1, 6f\\n\"\n \"st1 { v4.d }[0], [x20], #0x8\\n\"\n \"st1 { v6.d }[0], [x21], #0x8\\n\"\n \"st1 { v8.d }[0], [x22], #0x8\\n\"\n \"st1 { v10.d }[0], [x23], #0x8\\n\"\n \"tbz x25, #0, 9f\\n\"\n \"st1 { v4.s }[2], [x20]\\n\"\n \"st1 { v6.s }[2], [x21]\\n\"\n \"st1 { v8.s }[2], [x22]\\n\"\n \"st1 { v10.s }[2], [x23]\\n\"\n \"b 9f\\n\"\n \"6:\" // Output block 0: partial_1_4\n \"tbz x25, #0, 9f\\n\"\n \"st1 { v4.s }[0], [x20]\\n\"\n \"st1 { v6.s }[0], [x21]\\n\"\n \"st1 { v8.s }[0], [x22]\\n\"\n \"st1 { v10.s }[0], [x23]\\n\"\n \"b 9f\\n\"\n \"7:\" // Output block 0: partial_2_0\n \"tbz x25, #1, 8f\\n\"\n \"st1 { v5.d }[0], [x20], #0x8\\n\"\n \"st1 { v7.d }[0], [x21], #0x8\\n\"\n \"st1 { v9.d }[0], [x22], #0x8\\n\"\n \"st1 { v11.d }[0], [x23], #0x8\\n\"\n \"tbz x25, #0, 9f\\n\"\n \"st1 { v5.s }[2], [x20]\\n\"\n \"st1 { v7.s }[2], [x21]\\n\"\n \"st1 { v9.s }[2], [x22]\\n\"\n \"st1 { v11.s }[2], [x23]\\n\"\n \"b 9f\\n\"\n \"8:\" // Output block 0: partial_1_0\n \"st1 { v5.s }[0], [x20]\\n\"\n \"st1 { v7.s }[0], [x21]\\n\"\n \"st1 { v9.s }[0], [x22]\\n\"\n \"st1 { v11.s }[0], [x23]\\n\"\n \"9:\" // Output block 0: Done\n \"10:\" // Output stage exit\n \"subs x25, x25, #0x8\\n\"\n \"add %x[dst], %x[dst], #0x20\\n\"\n \"bgt 2b\\n\"\n \"subs x27, x27, #0x4\\n\"\n \"add %x[lhs_packed], %x[lhs_packed], x28\\n\"\n \"mov %x[dst], x24\\n\"\n \"bgt 1b\\n\"\n \"11:\" // Row loop skip\n : [dst] \"+&r\"(dst), [lhs_packed] \"+&r\"(lhs_packed)\n : [clamp_vals] \"r\"(clamp_vals), [dst_stride_row] \"r\"(dst_stride_row), [m] \"r\"(m), [n] \"r\"(n),\n [num_blocks] \"r\"(num_blocks), [rhs_packed] \"r\"(rhs_packed)\n : \"cc\", \"memory\", \"v0\", \"v1\", \"v2\", \"v3\", \"v4\", \"v5\", \"v6\", \"v7\", \"v8\", \"v9\", \"v10\", \"v11\", \"v12\", \"v16\", \"v17\",\n \"v18\", \"v19\", \"v20\", \"v21\", \"v22\", \"v23\", \"v24\", \"v25\", \"v26\", \"v27\", \"v28\", \"v29\", \"v30\", \"v31\", \"x20\",\n \"x21\", \"x22\", \"x23\", \"x24\", \"x25\", \"x26\", \"x27\", \"x28\");\n}\n#endif // Architectural feature check\n"
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod.h",
|
| 40 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# kai_lhs_quant_pack_qai8dxp_f32 to dynamically quantize and pack the LHS matrix\n/// -# kai_rhs_pack_kxn_qsi4cxp_qs4cxs1s0 OR kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0 to pack the RHS matrix\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix with\n/// the @ref kai_rhs_pack_kxn_qsi4cxp_qs4cxs1s0 micro-kernel\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void);\n\n/// Gets the kr value, which must be used to pack the RHS matrix with\n/// the @ref kai_rhs_pack_kxn_qsi4cxp_qs4cxs1s0 micro-kernel\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void);\n\n/// Gets the sr value, which must be used to pack the RHS matrix with\n/// the @ref kai_rhs_pack_kxn_qsi4cxp_qs4cxs1s0 micro-kernel\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Signed 8-bit quantized asymmetric per-row (qai8dxp) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of 1\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(size_t m_idx, size_t k);\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Signed 4-bit quantized symmetric per-channel (qsi4cxp) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of 8.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(\n size_t n_idx, //\n size_t k);\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of 1.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of 8.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride);\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(size_t m, size_t n);\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Signed 8-bit quantized asymmetric per-row (qai8dxp) and packed\n/// RHS matrix: Signed 4-bit quantized symmetric per-channel (qsi4cxp) and packed.\n/// Output tile: (rows x cols) = 1 x 8\n/// Accumulation performed in a single for loop: 32\n/// Extension used: dotprod\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] lhs_packed The LHS packed matrix.\n/// When the activation are dynamically quantized, you can obtain this matrix\n/// by calling the @ref kai_lhs_quant_pack_qai8dxp_f32 micro-kernel which performs\n/// both the dynamic quantization to 8-bit and activation packing in a single step.\n/// @param[in] rhs_packed The RHS packed matrix, which is obtained by calling @ref\n/// kai_run_rhs_pack_kxn_qsi4cxp_qs4cxs1s0\n/// OR kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float).\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(\n size_t m, size_t n, size_t k,\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod.c",
|
| 44 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if !defined(__ARM_FEATURE_DOTPROD)\n#error \"Dotprod extension required to compile this micro-kernel\"\n#else // Architectural features check.\n#include \"kai_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod.h\"\n\n#include <arm_neon.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_m_step = 1;\nstatic const size_t kai_n_step = 8;\nstatic const size_t kai_mr = 1;\nstatic const size_t kai_nr = 8;\nstatic const size_t kai_kr = 16;\nstatic const size_t kai_sr = 2;\nstatic const size_t kai_num_bytes_multiplier_lhs = sizeof(float);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_offset_lhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Since we pack a float and int32 value at the end of the row,\n // we must make sure that k is a multiple of 4 for alignment\n size_t kr_sr_roundedup4 = kai_roundup(kai_kr * kai_sr, 4);\n return kai_roundup(k, kr_sr_roundedup4);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return kai_mr * (k_internal * sizeof(int8_t) + kai_num_bytes_multiplier_lhs + kai_num_bytes_offset_lhs);\n}\n\ninline static size_t kai_rhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return kai_nr * ((k_internal / 2) + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void) {\n return kai_m_step;\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void) {\n return kai_n_step;\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void) {\n return kai_mr;\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void) {\n return kai_nr;\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(size_t m_idx, size_t k) {\n KAI_ASSERT((m_idx % kai_mr) == 0);\n\n return (m_idx / kai_mr) * kai_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(size_t n_idx, size_t k) {\n KAI_ASSERT((n_idx % kai_nr) == 0);\n\n return (n_idx / kai_nr) * kai_rhs_packed_stride(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSERT((m_idx % kai_m_step) == 0);\n KAI_ASSERT((n_idx % kai_n_step) == 0);\n\n return (n_idx * sizeof(float)) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(size_t m, size_t n) {\n return m * n * sizeof(float);\n}\n\nvoid kai_run_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod(\n size_t m, size_t n, size_t k, const void* restrict lhs_packed, const void* restrict rhs_packed,\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, size_t dst_stride_col, float scalar_min, float scalar_max) {\n KAI_ASSERT(dst_stride_col == sizeof(float));\n\n if (m == 0) {\n return;\n }\n\n const size_t k_internal = kai_k_roundedup(k);\n\n size_t num_blocks = k_internal / 32;\n\n float clamp_vals[2] = {scalar_min, scalar_max};\n __asm__ __volatile__(\n \"mov x26, #0x20\\n\"\n \"mov x20, #0x8\\n\"\n \"movi v5.16b, #0xf0\\n\"\n \"mov x25, %x[m]\\n\"\n \"madd x26, %x[num_blocks], x26, x20\\n\"\n \"1:\" // Row loop\n \"mov x24, %x[rhs_packed]\\n\"\n \"mov x23, %x[n]\\n\"\n \"add x22, %x[dst], %x[dst_stride_row]\\n\"\n \"2:\" // Column loop\n \"mov x21, %x[lhs_packed]\\n\"\n \"movi v4.4s, #0x0\\n\"\n \"movi v3.4s, #0x0\\n\"\n \"mov x20, %x[num_blocks]\\n\"\n \"movi v2.4s, #0x0\\n\"\n \"movi v1.4s, #0x0\\n\"\n \"3:\" // Sub block loop\n \"ldr q0, [x24, #0x0]\\n\"\n \"ldr q31, [x24, #0x10]\\n\"\n \"subs x20, x20, #0x1\\n\"\n \"ldr q30, [x24, #0x20]\\n\"\n \"ldr q29, [x24, #0x30]\\n\"\n \"ld1r { v28.2d }, [x21], #0x8\\n\"\n \"ldr q27, [x24, #0x40]\\n\"\n \"ldr q26, [x24, #0x50]\\n\"\n \"ldr q25, [x24, #0x60]\\n\"\n \"shl v24.16b, v0.16b, #0x4\\n\"\n \"shl v18.16b, v31.16b, #0x4\\n\"\n \"ldr q23, [x24, #0x70]\\n\"\n \"shl v17.16b, v30.16b, #0x4\\n\"\n \"shl v16.16b, v29.16b, #0x4\\n\"\n \"add x24, x24, #0x80\\n\"\n \"ld1r { v22.2d }, [x21], #0x8\\n\"\n \"shl v21.16b, v27.16b, #0x4\\n\"\n \"and v0.16b, v0.16b, v5.16b\\n\"\n \"ld1r { v20.2d }, [x21], #0x8\\n\"\n \"ld1r { v19.2d }, [x21], #0x8\\n\"\n \".inst 0x4e9c9704 // sdot v4.4s, v24.16b, v28.16b\\n\"\n \".inst 0x4e9c9643 // sdot v3.4s, v18.16b, v28.16b\\n\"\n \"shl v18.16b, v26.16b, #0x4\\n\"\n \".inst 0x4e9c9622 // sdot v2.4s, v17.16b, v28.16b\\n\"\n \".inst 0x4e9c9601 // sdot v1.4s, v16.16b, v28.16b\\n\"\n \"shl v17.16b, v25.16b, #0x4\\n\"\n \"shl v16.16b, v23.16b, #0x4\\n\"\n \"and v31.16b, v31.16b, v5.16b\\n\"\n \"and v30.16b, v30.16b, v5.16b\\n\"\n \"and v29.16b, v29.16b, v5.16b\\n\"\n \".inst 0x4e9696a4 // sdot v4.4s, v21.16b, v22.16b\\n\"\n \".inst 0x4e969643 // sdot v3.4s, v18.16b, v22.16b\\n\"\n \"and v27.16b, v27.16b, v5.16b\\n\"\n \".inst 0x4e969622 // sdot v2.4s, v17.16b, v22.16b\\n\"\n \".inst 0x4e969601 // sdot v1.4s, v16.16b, v22.16b\\n\"\n \"and v26.16b, v26.16b, v5.16b\\n\"\n \"and v25.16b, v25.16b, v5.16b\\n\"\n \"and v23.16b, v23.16b, v5.16b\\n\"\n \".inst 0x4e949404 // sdot v4.4s, v0.16b, v20.16b\\n\"\n \".inst 0x4e9497e3 // sdot v3.4s, v31.16b, v20.16b\\n\"\n \".inst 0x4e9497c2 // sdot v2.4s, v30.16b, v20.16b\\n\"\n \".inst 0x4e9497a1 // sdot v1.4s, v29.16b, v20.16b\\n\"\n \".inst 0x4e939764 // sdot v4.4s, v27.16b, v19.16b\\n\"\n \".inst 0x4e939743 // sdot v3.4s, v26.16b, v19.16b\\n\"\n \".inst 0x4e939722 // sdot v2.4s, v25.16b, v19.16b\\n\"\n \".inst 0x4e9396e1 // sdot v1.4s, v23.16b, v19.16b\\n\"\n \"bgt 3b\\n\"\n \"ldr q25, [x24, #0x0]\\n\"\n \"ldr q24, [x24, #0x10]\\n\"\n \"addp v4.4s, v4.4s, v3.4s\\n\"\n \"addp v2.4s, v2.4s, v1.4s\\n\"\n \"ld1r { v23.4s }, [x21]\\n\"\n \"ldr q22, [x24, #0x20]\\n\"\n \"add x21, x21, #0x4\\n\"\n \"add x20, %x[clamp_vals], #0x4\\n\"\n \"ld1r { v17.4s }, [x21]\\n\"\n \"ldr q16, [x24, #0x30]\\n\"\n \"cmp x23, #0x8\\n\"\n \"ldr q21, [x24, #0x40]\\n\"\n \"ldr q20, [x24, #0x50]\\n\"\n \"add x24, x24, #0x60\\n\"\n \"ld1r { v19.4s }, [%x[clamp_vals]]\\n\"\n \"ld1r { v18.4s }, [x20]\\n\"\n \"mla v4.4s, v25.4s, v23.s[0]\\n\"\n \"mla v2.4s, v24.4s, v23.s[0]\\n\"\n \"fmul v22.4s, v22.4s, v17.4s\\n\"\n \"fmul v16.4s, v16.4s, v17.4s\\n\"\n \"scvtf v4.4s, v4.4s\\n\"\n \"scvtf v2.4s, v2.4s\\n\"\n \"fmul v17.4s, v4.4s, v22.4s\\n\"\n \"fmul v16.4s, v2.4s, v16.4s\\n\"\n \"fadd v17.4s, v17.4s, v21.4s\\n\"\n \"fadd v16.4s, v16.4s, v20.4s\\n\"\n \"fmax v17.4s, v17.4s, v19.4s\\n\"\n \"fmax v16.4s, v16.4s, v19.4s\\n\"\n \"fmin v17.4s, v17.4s, v18.4s\\n\"\n \"fmin v16.4s, v16.4s, v18.4s\\n\"\n \"blt 4f\\n\"\n \"str q17, [%x[dst], #0x0]\\n\"\n \"str q16, [%x[dst], #0x10]\\n\"\n \"b 9f\\n\"\n \"4:\" // Partial output\n \"mov x20, %x[dst]\\n\"\n \"tbz x23, #2, 6f\\n\"\n \"st1 { v17.4s }, [x20], #0x10\\n\"\n \"tbz x23, #1, 5f\\n\"\n \"st1 { v16.d }[0], [x20], #0x8\\n\"\n \"tbz x23, #0, 8f\\n\"\n \"st1 { v16.s }[2], [x20]\\n\"\n \"b 8f\\n\"\n \"5:\" // Output block 0: partial_1_4\n \"tbz x23, #0, 8f\\n\"\n \"st1 { v16.s }[0], [x20]\\n\"\n \"b 8f\\n\"\n \"6:\" // Output block 0: partial_2_0\n \"tbz x23, #1, 7f\\n\"\n \"st1 { v17.d }[0], [x20], #0x8\\n\"\n \"tbz x23, #0, 8f\\n\"\n \"st1 { v17.s }[2], [x20]\\n\"\n \"b 8f\\n\"\n \"7:\" // Output block 0: partial_1_0\n \"st1 { v17.s }[0], [x20]\\n\"\n \"8:\" // Output block 0: Done\n \"9:\" // Stores done\n \"subs x23, x23, #0x8\\n\"\n \"add %x[dst], %x[dst], #0x20\\n\"\n \"bgt 2b\\n\"\n \"subs x25, x25, #0x1\\n\"\n \"add %x[lhs_packed], %x[lhs_packed], x26\\n\"\n \"mov %x[dst], x22\\n\"\n \"bgt 1b\\n\"\n : [dst] \"+&r\"(dst), [lhs_packed] \"+&r\"(lhs_packed)\n : [clamp_vals] \"r\"(clamp_vals), [dst_stride_row] \"r\"(dst_stride_row), [m] \"r\"(m), [n] \"r\"(n),\n [num_blocks] \"r\"(num_blocks), [rhs_packed] \"r\"(rhs_packed)\n : \"cc\", \"memory\", \"v0\", \"v1\", \"v2\", \"v3\", \"v4\", \"v5\", \"v16\", \"v17\", \"v18\", \"v19\", \"v20\", \"v21\", \"v22\", \"v23\",\n \"v24\", \"v25\", \"v26\", \"v27\", \"v28\", \"v29\", \"v30\", \"v31\", \"x20\", \"x21\", \"x22\", \"x23\", \"x24\", \"x25\", \"x26\");\n}\n\n#endif // Architectural features check.\n"
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.h",
|
| 48 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qa8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS of the vector-by-matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs, size_t lhs_stride,\n void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.c",
|
| 52 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n\n#if defined(__aarch64__)\n#include <arm_neon.h>\n#endif\n#include <float.h>\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_per_multiplier = sizeof(float);\nstatic const size_t kai_num_bytes_per_offset = sizeof(int32_t);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return mr * (k_internal * sizeof(int8_t) + kai_num_bytes_per_multiplier + kai_num_bytes_per_offset);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) {\n // It always points to the beginning of the row\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return num_rows * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* restrict lhs,\n size_t lhs_stride, void* restrict lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n\n if (m == 0) {\n return;\n }\n\n const size_t num_rows = m;\n\n const float* src_ptr = lhs;\n\n const size_t dst_stride = kai_lhs_packed_stride(k, mr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n const int32_t num_blocks_k = (int32_t)(k / k_block_len);\n const int32_t num_blocks_k_internal = (int32_t)(k_internal / k_block_len);\n\n for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {\n float max0 = -FLT_MAX;\n float min0 = FLT_MAX;\n\n // Find min/max for each channel\n int32_t k_idx = 0;\n\n#if defined(__aarch64__)\n float32x4_t vmax0 = vdupq_n_f32(-FLT_MAX);\n float32x4_t vmin0 = vdupq_n_f32(FLT_MAX);\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float32x4_t src0_0 = vld1q_f32(src_ptr + 0 + (size_t)k_idx);\n const float32x4_t src0_1 = vld1q_f32(src_ptr + 4 + (size_t)k_idx);\n\n // Calculate the max\n vmax0 = vmaxq_f32(src0_0, vmax0);\n vmax0 = vmaxq_f32(vmax0, src0_1);\n\n // Calculate the min\n vmin0 = vminq_f32(src0_0, vmin0);\n vmin0 = vminq_f32(vmin0, src0_1);\n }\n // Get the max/min\n max0 = vmaxvq_f32(vmax0);\n min0 = vminvq_f32(vmin0);\n#endif\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float src0_0 = *(src_ptr + (size_t)k_idx);\n max0 = fmaxf(src0_0, max0);\n min0 = fminf(src0_0, min0);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n\n float zero_point0 =\n zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len * sizeof(int8_t));\n\n // Quantize the channels\n int32_t block_idx = 0;\n\n#if defined(__aarch64__)\n if (k_block_len == 8) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n const float32x4_t src_1 = vld1q_f32(src_ptr + k_idx_start + 4);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n float32x4_t v1_f32 = vmulq_n_f32(src_1, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int32x4_t v1_s32 = vcvtaq_s32_f32(v1_f32);\n\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n int16x4_t v1_s16 = vqmovn_s32(v1_s32);\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x8_t vnzp_s16 = vdupq_n_s16(nzp_s16);\n v_s16 = vaddq_s16(v_s16, vnzp_s16);\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(v_s16);\n vst1_s8((int8_t*)(dst_ptr), v0_s8);\n dst_ptr += 8 * sizeof(int8_t);\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n } else if (k_block_len == 4) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x4_t vnzp_s16 = vdup_n_s16(nzp_s16);\n int16x4_t v_s16 = vadd_s16(v0_s16, vnzp_s16);\n v_s16 = vmax_s16(v_s16, vdup_n_s16(INT8_MIN));\n v_s16 = vmin_s16(v_s16, vdup_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(vcombine_s16(v_s16, vdup_n_s16(0)));\n\n *((int8_t*)(dst_ptr)) = vget_lane_s8(v0_s8, 0);\n *((int8_t*)(dst_ptr + 1)) = vget_lane_s8(v0_s8, 1);\n *((int8_t*)(dst_ptr + 2)) = vget_lane_s8(v0_s8, 2);\n *((int8_t*)(dst_ptr + 3)) = vget_lane_s8(v0_s8, 3);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n } else\n#endif\n {\n for (; block_idx < num_blocks_k; ++block_idx) {\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n const int32_t k_idx_start = (block_idx * k_block_len) + k_block_idx;\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n dst_ptr = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr += dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params\n *((float*)(dst_ptr)) = recip_scale0;\n\n src_ptr += (lhs_stride / sizeof(float));\n\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n}\n"
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"path": "kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0.h",
|
| 56 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0_params kai_rhs_pack_qs4cxs1s0_param\n\n/// Get the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n///\n/// @return the n step value\nsize_t kai_get_n_step_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t nr);\n\n/// Gets the offset in bytes for the RHS matrix (not packed).\n///\n/// @note The int4 values are stored in a N x K matrix. Two int4 values are stored in one byte.\n/// The lower order part of the byte (low) holds the first nibble (K-index + 0).\n/// The higher order of the byte holds the second nibble (K-index + 1).\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t n_idx, size_t rhs_stride);\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Gets the offset in bytes for the packed RHS matrix, which contains the packed 4-bit quantized symmetric per-channel\n/// (qsu4cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// @brief Gets the size in bytes for the packed RHS matrix\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t n, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int4 values are stored in a N x K matrix. Two int4 values are stored in one byte.\n/// The lower order part of the byte (low) holds the first nibble (K-index + 0).\n/// The higher order of the byte holds the second nibble (K-index + 1).\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of rows.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K). It must be an even value.\n/// @param[in] nr The number of N rows to interleave on the same output output row.\n/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] rhs The RHS matrix containing the 4-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).\n/// @param[in] bias The biases.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n const uint8_t* rhs, //\n const float* bias, //\n const float* scale, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0_params* params);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"path": "kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0.c",
|
| 60 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nsize_t kai_get_n_step_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t nr) {\n return nr;\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return nr * ((k_internal / 2) + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_ASSERT((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(k, nr, kr, sr);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t n, size_t k, size_t nr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(k, nr, kr, sr);\n}\n\nvoid kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, const uint8_t* rhs, const float* bias,\n const float* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0_params* params) {\n KAI_ASSERT(num_groups == 1);\n KAI_ASSERT(extra_bytes == 0);\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSERT(rhs != NULL);\n KAI_ASSERT(scale != NULL);\n KAI_ASSERT(rhs_packed != NULL);\n KAI_ASSERT(params != NULL);\n KAI_ASSERT(params->lhs_zero_point == 1);\n KAI_ASSERT(params->rhs_zero_point == 0 || params->rhs_zero_point == 8);\n\n const uint8_t rhs_zero_point = params->rhs_zero_point;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(k, nr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const size_t dst_num_rows = kai_roundup(n, nr) / nr;\n const size_t dst_num_bytes_per_row = nr * (kai_k_roundedup(k) / 2);\n const size_t block_length_in_bytes = kr / sr;\n const size_t k_interleaved_v = 16U;\n const size_t rhs_stride = kai_roundup(k, 2) / 2;\n\n for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; ++dst_row_idx) {\n uint8_t* dst_row = (uint8_t*)rhs_packed + dst_row_idx * rhs_packed_stride;\n\n int32_t* sums = (int32_t*)(dst_row + nr * (k_internal / 2));\n\n // Initialize to zero the RHS reduction sums\n memset(sums, 0, nr * sizeof(int32_t));\n\n for (size_t dst_byte_idx = 0; dst_byte_idx < dst_num_bytes_per_row; ++dst_byte_idx) {\n const size_t block_idx = dst_byte_idx / block_length_in_bytes;\n const size_t block_byte_idx = dst_byte_idx % block_length_in_bytes;\n const size_t super_block_idx = block_idx / nr;\n const size_t nr_idx = block_idx % nr;\n\n const size_t k_adjustment =\n ((block_byte_idx + super_block_idx * block_length_in_bytes) / k_interleaved_v) * k_interleaved_v;\n const size_t k0_idx = block_byte_idx + super_block_idx * block_length_in_bytes + k_adjustment;\n const size_t k1_idx = k0_idx + k_interleaved_v;\n const size_t n0_idx = dst_row_idx * nr + nr_idx;\n\n // Clamp the index to avoid out-of-bound reads\n const size_t n0_valid_idx = KAI_MIN(n0_idx, n - 1);\n\n const size_t src_addr_byte0 = (k0_idx / 2) + n0_valid_idx * rhs_stride;\n const size_t src_addr_byte1 = (k1_idx / 2) + n0_valid_idx * rhs_stride;\n\n const size_t shift_right_x0 = (k0_idx % 2) * 4;\n const size_t shift_right_x1 = (k1_idx % 2) * 4;\n\n if (rhs_zero_point == 8) {\n uint8_t byte0 = rhs_zero_point | rhs_zero_point << 4;\n uint8_t byte1 = rhs_zero_point | rhs_zero_point << 4;\n\n if (k0_idx < k) {\n byte0 = rhs[src_addr_byte0];\n }\n\n if (k1_idx < k) {\n byte1 = rhs[src_addr_byte1];\n }\n\n // The following operations where we extract the values from the bytes\n // can be also written in the following and less efficient manner:\n /*\n uint8_t src_x0_lo = 0;\n uint8_t src_x0_hi = 0;\n\n if ((k0_idx % 2) == 0) {\n src_x0_lo = (byte0 & 0x0F);\n } else {\n src_x0_lo = (byte0 >> 4);\n }\n\n if ((k1_idx % 2) == 0) {\n src_x0_hi = (byte1 & 0x0F);\n } else {\n src_x0_hi = (byte1 >> 4);\n }\n */\n const uint8_t src_x0_lo = (byte0 >> shift_right_x0) & 0x0F;\n const uint8_t src_x0_hi = (byte1 >> shift_right_x1) & 0x0F;\n\n sums[nr_idx] += (int32_t)src_x0_lo + (int32_t)src_x0_hi - 2 * (int32_t)rhs_zero_point;\n\n const uint8_t dst_qs0 = src_x0_lo | (src_x0_hi << 4);\n\n *dst_row = dst_qs0 ^ 0x88;\n dst_row += sizeof(uint8_t);\n } else {\n int8_t byte0 = 0;\n int8_t byte1 = 0;\n\n // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n if (k0_idx < k) {\n byte0 = rhs[src_addr_byte0];\n }\n\n if (k1_idx < k) {\n byte1 = rhs[src_addr_byte1];\n }\n\n // The logic behind the following operations where we extract the\n // values from the bytes is same as unsigned\n\n int8_t src_x0_lo = (byte0 >> shift_right_x0) & 0x0F;\n int8_t src_x0_hi = (byte1 >> shift_right_x1) & 0x0F;\n\n const int8_t dst_qs0 = src_x0_lo | (src_x0_hi << 4);\n // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n\n *(int8_t*)dst_row = dst_qs0;\n dst_row += sizeof(int8_t);\n\n src_x0_lo = kai_ext_sign_i8_i4(src_x0_lo);\n src_x0_hi = kai_ext_sign_i8_i4(src_x0_hi);\n sums[nr_idx] += (int32_t)src_x0_lo + (int32_t)src_x0_hi;\n }\n }\n\n // Adjust the reduction sums\n for (size_t i = 0; i < nr; ++i) {\n sums[i] = sums[i] * 16;\n dst_row += sizeof(int32_t);\n }\n\n // Adjust the scales\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n *((float*)(dst_row)) = scale[src_row_idx] * 0.0625F;\n dst_row += sizeof(float);\n }\n\n // Set the bias\n if (bias == NULL) {\n memset(dst_row, 0, nr * kai_num_bytes_bias);\n } else {\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n ((float*)dst_row)[i] = bias[src_row_idx];\n }\n }\n }\n}\n"
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"path": "binding.cpp",
|
| 64 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm against gemm_qs4cx_n512_k512's raw ABI:\n// armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* bias, int M)\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm.h\"\n#include \"kai_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod.h\"\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n#include \"kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0.h\"\n\nnamespace {\nconstexpr int N = 512;\nconstexpr int K = 512;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* bias, int M)\n{\n // M == 1 runs the dedicated GEMV ukernel, as a KleidiAI user would pick it. Both variants\n // share one packed-RHS layout (same nr/kr/sr), so a single cached weight pack serves both.\n const bool gemv = (M == 1);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm();\n const size_t kr = kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm();\n const size_t sr = kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm();\n if (kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod() != nr || kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod() != kr ||\n kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod() != sr) return -2;\n const size_t mr = gemv ? kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod() : kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = gemv ? (kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr)) : (kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr));\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n if (gemv) {\n kai_run_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n } else {\n kai_run_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n }\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q4_snap = nullptr;\n static size_t B_q4_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q4_snap, B_q4_snap_size, B_q4, (size_t)N*K/2)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N)\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi4cxp_qs4cxs1s0((size_t)N, (size_t)K, nr, kr, sr);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_qs4cxs1s0_param params;\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 8;\n kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(1, (size_t)N, (size_t)K, nr, kr, sr, B_q4, bias, B_scales, rhs_packed, 0, ¶ms);\n }\n\n if (gemv) {\n kai_run_matmul_clamp_f32_qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod((size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n } else {\n kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm((size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n }\n return 0;\n}\n"
|
| 65 |
+
}
|
| 66 |
+
]
|
| 67 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qs8cx_n512_k512.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qs8cx_n512_k512",
|
| 3 |
+
"definition": "gemm_qs8cx_n512_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qs8cx_n512_k512: KleidiAI kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"graviton4",
|
| 11 |
+
"aarch64-sve2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"dotprod",
|
| 17 |
+
"i8mm"
|
| 18 |
+
],
|
| 19 |
+
"compile_flags": [
|
| 20 |
+
"-O3",
|
| 21 |
+
"-march=armv8.2-a+dotprod+i8mm"
|
| 22 |
+
],
|
| 23 |
+
"link_flags": []
|
| 24 |
+
},
|
| 25 |
+
"sources": [
|
| 26 |
+
{
|
| 27 |
+
"path": "kai/kai_common.h",
|
| 28 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"path": "kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.h",
|
| 32 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_lhs_quant_pack_qai8dxp_f32 to dynamically quantize and pack the LHS matrix in a single step.\n/// -# @ref kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon to pack the RHS NxK matrix.\n/// -# @ref kai_rhs_pack_kxn_qsi8cxp_qsi8cx_neon to pack the RHS KxN matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of 16\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(\n size_t m_idx, //\n size_t k); //\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-channel quantization (qsi8cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of 4.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(\n size_t n_idx, //\n size_t k); //\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of 16.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of 4.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride); //\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(\n size_t m, //\n size_t n); //\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) and packed\n/// RHS matrix: Quantized Symmetric Signed 8-bit with per-channel quantization (qsi8cx) and packed.\n/// Output tile: (rows x cols) = 16 x 4\n///\n/// Features used: i8mm\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the\n/// top of this file.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the\n/// top of this file.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes.\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(\n size_t m, //\n size_t n, //\n size_t k, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n}\n#endif // __cplusplus\n"
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"path": "kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.c",
|
| 36 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_MATMUL_INT8)) && !defined(_M_ARM64)\n#error \"I8mm extension required to compile this micro-kernel\"\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\ntypedef struct {\n float* dst;\n const void* lhs_packed;\n const void* rhs_packed;\n const float* clamp_vals;\n size_t dst_stride_row;\n size_t m;\n size_t n;\n size_t num_blocks;\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(KernelArgs* args);\n\n// Compute args\nstatic const size_t kai_m_step = 16;\nstatic const size_t kai_n_step = 4;\n// Packing args\nstatic const size_t kai_mr = 4;\nstatic const size_t kai_nr = 4;\nstatic const size_t kai_kr = 8;\nstatic const size_t kai_sr = 1;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 4;\nstatic const size_t kai_num_bytes_zp_lhs = 4;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_num_bytes_qvalue_rhs = 1;\nstatic const size_t kai_num_bytes_multiplier_rhs = 4;\nstatic const size_t kai_num_bytes_rsum_rhs = 4;\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_num_bytes_bias = 4;\nstatic const size_t kai_k_multiple_of = 32;\n\ninline static size_t kai_k_roundedup(size_t k) {\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n size_t lhs_packed_stride = kai_mr * ((k_internal * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs);\n // Since the LHS matrix is asymmetric with per-row quantization, we must include the\n // the number of bytes to hold the zero point value\n lhs_packed_stride += kai_mr * kai_num_bytes_zp_lhs;\n\n return lhs_packed_stride;\n}\n\ninline static size_t kai_rhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n size_t rhs_packed_stride = kai_nr * (k_internal * kai_num_bytes_qvalue_rhs);\n rhs_packed_stride += kai_nr * kai_num_bytes_multiplier_rhs;\n // Since the LHS matrix is quantized asymmetric with per-row quantization, we also include\n // the number of bytes for the reduction sum\n rhs_packed_stride += kai_nr * kai_num_bytes_rsum_rhs;\n // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias\n rhs_packed_stride += kai_nr * kai_num_bytes_bias;\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void) {\n return kai_m_step;\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void) {\n return kai_n_step;\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void) {\n return kai_mr;\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void) {\n return kai_nr;\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(size_t m_idx, size_t k) {\n KAI_ASSUME((m_idx % kai_mr) == 0);\n\n return (m_idx / kai_mr) * kai_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(size_t n_idx, size_t k) {\n KAI_ASSUME((n_idx % kai_nr) == 0);\n\n return (n_idx / kai_nr) * kai_rhs_packed_stride(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSUME((m_idx % kai_m_step) == 0);\n KAI_ASSUME((n_idx % kai_n_step) == 0);\n\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(\n size_t m, //\n size_t n, //\n size_t k, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSUME(dst_stride_col == sizeof(float));\n\n if (m == 0) {\n return;\n }\n\n const size_t k_internal = kai_k_roundedup(k);\n const size_t num_blocks = k_internal / kai_k_multiple_of;\n const float clamp_vals[2] = {scalar_min, scalar_max};\n\n KernelArgs args;\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.clamp_vals = clamp_vals;\n args.dst_stride_row = dst_stride_row;\n args.m = m;\n args.n = n;\n args.num_blocks = num_blocks;\n\n kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm(&args);\n}\n\n#endif // Architectural features check.\n"
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"path": "kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm_asm.S",
|
| 40 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm)\n // Save callee-saved registers x19-x28 and d8-d15\n stp x19, x20, [sp, -176]!\n stp x21, x22, [sp, 16]\n stp x23, x24, [sp, 32]\n stp x25, x26, [sp, 48]\n stp x27, x28, [sp, 64]\n stp d8, d9, [sp, 80]\n stp d10, d11, [sp, 96]\n stp d12, d13, [sp, 112]\n stp d14, d15, [sp, 128]\n\n // Load arguments from struct KernelArgs (passed in x0)\n ldr x19, [x0, #0] // running_dst = dst\n ldr x15, [x0, #8] // running_lhs_packed = lhs_packed\n ldr x17, [x0, #24] // clamp_vals\n ldr x28, [x0, #32] // dst_stride_row\n ldr x13, [x0, #40] // m\n ldr x14, [x0, #56] // num_blocks\n\n mov x12, #0x80\n mov x20, #0x20\n cmp x13, #0x10\n madd x12, x14, x12, x20\n blt label_14\n\nKAI_ASM_LABEL(label_1) // Row loop\n ldr x11, [x0, #16] // Reset rhs_packed\n ldr x10, [x0, #48] // Reset n\n add x9, x19, x28, LSL #4\n\nKAI_ASM_LABEL(label_2) // Column loop\n mov x27, x15\n movi v31.4s, #0x0\n movi v30.4s, #0x0\n mov x23, x14 // num_blocks\n movi v29.4s, #0x0\n movi v28.4s, #0x0\n movi v27.4s, #0x0\n movi v26.4s, #0x0\n add x22, x27, x12\n add x21, x22, x12\n add x20, x21, x12\n movi v25.4s, #0x0\n movi v24.4s, #0x0\n movi v23.4s, #0x0\n movi v22.4s, #0x0\n movi v21.4s, #0x0\n movi v20.4s, #0x0\n movi v19.4s, #0x0\n movi v18.4s, #0x0\n movi v17.4s, #0x0\n movi v16.4s, #0x0\n\nKAI_ASM_LABEL(label_3) // Sub block loop\n ldr q2, [x11, #0x0]\n ldr q1, [x11, #0x10]\n subs x23, x23, #0x1\n ldr q5, [x27, #0x0]\n ldr q9, [x27, #0x10]\n ldr q8, [x22, #0x0]\n ldr q7, [x22, #0x10]\n ldr q4, [x21, #0x0]\n ldr q14, [x21, #0x10]\n ldr q3, [x20, #0x0]\n ldr q0, [x20, #0x10]\n KAI_ASM_INST(0x4e82a4bf) // smmla v31.4s, v5.16b, v2.16b\n KAI_ASM_INST(0x4e81a4be) // smmla v30.4s, v5.16b, v1.16b\n ldr q6, [x11, #0x20]\n ldr q5, [x11, #0x30]\n KAI_ASM_INST(0x4e82a53d) // smmla v29.4s, v9.16b, v2.16b\n KAI_ASM_INST(0x4e81a53c) // smmla v28.4s, v9.16b, v1.16b\n ldr q13, [x27, #0x20]\n ldr q12, [x27, #0x30]\n KAI_ASM_INST(0x4e82a51b) // smmla v27.4s, v8.16b, v2.16b\n KAI_ASM_INST(0x4e81a51a) // smmla v26.4s, v8.16b, v1.16b\n ldr q11, [x22, #0x20]\n ldr q10, [x22, #0x30]\n KAI_ASM_INST(0x4e82a4f9) // smmla v25.4s, v7.16b, v2.16b\n KAI_ASM_INST(0x4e81a4f8) // smmla v24.4s, v7.16b, v1.16b\n ldr q9, [x21, #0x20]\n ldr q8, [x21, #0x30]\n KAI_ASM_INST(0x4e82a497) // smmla v23.4s, v4.16b, v2.16b\n KAI_ASM_INST(0x4e81a496) // smmla v22.4s, v4.16b, v1.16b\n ldr q7, [x20, #0x20]\n ldr q4, [x20, #0x30]\n KAI_ASM_INST(0x4e82a5d5) // smmla v21.4s, v14.16b, v2.16b\n KAI_ASM_INST(0x4e81a5d4) // smmla v20.4s, v14.16b, v1.16b\n ldr q15, [x11, #0x40]\n ldr q14, [x11, #0x50]\n KAI_ASM_INST(0x4e82a473) // smmla v19.4s, v3.16b, v2.16b\n KAI_ASM_INST(0x4e81a472) // smmla v18.4s, v3.16b, v1.16b\n ldr q3, [x27, #0x40]\n KAI_ASM_INST(0x4e82a411) // smmla v17.4s, v0.16b, v2.16b\n ldr q2, [x27, #0x50]\n KAI_ASM_INST(0x4e81a410) // smmla v16.4s, v0.16b, v1.16b\n ldr q1, [x22, #0x40]\n ldr q0, [x22, #0x50]\n KAI_ASM_INST(0x4e86a5bf) // smmla v31.4s, v13.16b, v6.16b\n KAI_ASM_INST(0x4e85a5be) // smmla v30.4s, v13.16b, v5.16b\n ldr q13, [x21, #0x40]\n KAI_ASM_INST(0x4e86a59d) // smmla v29.4s, v12.16b, v6.16b\n KAI_ASM_INST(0x4e85a59c) // smmla v28.4s, v12.16b, v5.16b\n ldr q12, [x21, #0x50]\n KAI_ASM_INST(0x4e86a57b) // smmla v27.4s, v11.16b, v6.16b\n KAI_ASM_INST(0x4e85a57a) // smmla v26.4s, v11.16b, v5.16b\n ldr q11, [x20, #0x40]\n KAI_ASM_INST(0x4e86a559) // smmla v25.4s, v10.16b, v6.16b\n KAI_ASM_INST(0x4e85a558) // smmla v24.4s, v10.16b, v5.16b\n ldr q10, [x20, #0x50]\n KAI_ASM_INST(0x4e86a537) // smmla v23.4s, v9.16b, v6.16b\n KAI_ASM_INST(0x4e85a536) // smmla v22.4s, v9.16b, v5.16b\n ldr q9, [x11, #0x60]\n KAI_ASM_INST(0x4e86a515) // smmla v21.4s, v8.16b, v6.16b\n KAI_ASM_INST(0x4e85a514) // smmla v20.4s, v8.16b, v5.16b\n ldr q8, [x11, #0x70]\n add x11, x11, #0x80\n KAI_ASM_INST(0x4e86a4f3) // smmla v19.4s, v7.16b, v6.16b\n KAI_ASM_INST(0x4e85a4f2) // smmla v18.4s, v7.16b, v5.16b\n ldr q7, [x27, #0x60]\n KAI_ASM_INST(0x4e86a491) // smmla v17.4s, v4.16b, v6.16b\n ldr q6, [x27, #0x70]\n KAI_ASM_INST(0x4e85a490) // smmla v16.4s, v4.16b, v5.16b\n ldr q5, [x22, #0x60]\n ldr q4, [x22, #0x70]\n KAI_ASM_INST(0x4e8fa47f) // smmla v31.4s, v3.16b, v15.16b\n KAI_ASM_INST(0x4e8ea47e) // smmla v30.4s, v3.16b, v14.16b\n ldr q3, [x21, #0x60]\n KAI_ASM_INST(0x4e8fa45d) // smmla v29.4s, v2.16b, v15.16b\n KAI_ASM_INST(0x4e8ea45c) // smmla v28.4s, v2.16b, v14.16b\n ldr q2, [x21, #0x70]\n add x27, x27, #0x80\n KAI_ASM_INST(0x4e8fa43b) // smmla v27.4s, v1.16b, v15.16b\n KAI_ASM_INST(0x4e8ea43a) // smmla v26.4s, v1.16b, v14.16b\n ldr q1, [x20, #0x60]\n add x22, x22, #0x80\n KAI_ASM_INST(0x4e8fa419) // smmla v25.4s, v0.16b, v15.16b\n KAI_ASM_INST(0x4e8ea418) // smmla v24.4s, v0.16b, v14.16b\n ldr q0, [x20, #0x70]\n add x21, x21, #0x80\n KAI_ASM_INST(0x4e8fa5b7) // smmla v23.4s, v13.16b, v15.16b\n KAI_ASM_INST(0x4e8ea5b6) // smmla v22.4s, v13.16b, v14.16b\n add x20, x20, #0x80\n KAI_ASM_INST(0x4e8fa595) // smmla v21.4s, v12.16b, v15.16b\n KAI_ASM_INST(0x4e8ea594) // smmla v20.4s, v12.16b, v14.16b\n KAI_ASM_INST(0x4e8fa573) // smmla v19.4s, v11.16b, v15.16b\n KAI_ASM_INST(0x4e8ea572) // smmla v18.4s, v11.16b, v14.16b\n KAI_ASM_INST(0x4e8fa551) // smmla v17.4s, v10.16b, v15.16b\n KAI_ASM_INST(0x4e8ea550) // smmla v16.4s, v10.16b, v14.16b\n KAI_ASM_INST(0x4e89a4ff) // smmla v31.4s, v7.16b, v9.16b\n KAI_ASM_INST(0x4e88a4fe) // smmla v30.4s, v7.16b, v8.16b\n KAI_ASM_INST(0x4e89a4dd) // smmla v29.4s, v6.16b, v9.16b\n KAI_ASM_INST(0x4e88a4dc) // smmla v28.4s, v6.16b, v8.16b\n KAI_ASM_INST(0x4e89a4bb) // smmla v27.4s, v5.16b, v9.16b\n KAI_ASM_INST(0x4e88a4ba) // smmla v26.4s, v5.16b, v8.16b\n KAI_ASM_INST(0x4e89a499) // smmla v25.4s, v4.16b, v9.16b\n KAI_ASM_INST(0x4e88a498) // smmla v24.4s, v4.16b, v8.16b\n KAI_ASM_INST(0x4e89a477) // smmla v23.4s, v3.16b, v9.16b\n KAI_ASM_INST(0x4e88a476) // smmla v22.4s, v3.16b, v8.16b\n KAI_ASM_INST(0x4e89a455) // smmla v21.4s, v2.16b, v9.16b\n KAI_ASM_INST(0x4e88a454) // smmla v20.4s, v2.16b, v8.16b\n KAI_ASM_INST(0x4e89a433) // smmla v19.4s, v1.16b, v9.16b\n KAI_ASM_INST(0x4e88a432) // smmla v18.4s, v1.16b, v8.16b\n KAI_ASM_INST(0x4e89a411) // smmla v17.4s, v0.16b, v9.16b\n KAI_ASM_INST(0x4e88a410) // smmla v16.4s, v0.16b, v8.16b\n bgt label_3\n\n ldr q7, [x11, #0x0]\n ld1 { v4.4s }, [x27]\n uzp1 v3.2d, v31.2d, v30.2d\n uzp2 v2.2d, v31.2d, v30.2d\n ldr q6, [x11, #0x10]\n uzp1 v1.2d, v29.2d, v28.2d\n uzp2 v0.2d, v29.2d, v28.2d\n add x27, x27, #0x10\n ldr q28, [x27, #0x0]\n add x11, x11, #0x20\n mla v3.4s, v7.4s, v4.s[0]\n mla v2.4s, v7.4s, v4.s[1]\n mla v1.4s, v7.4s, v4.s[2]\n mla v0.4s, v7.4s, v4.s[3]\n fmul v31.4s, v6.4s, v28.s[0]\n fmul v30.4s, v6.4s, v28.s[1]\n fmul v29.4s, v6.4s, v28.s[2]\n fmul v28.4s, v6.4s, v28.s[3]\n scvtf v3.4s, v3.4s\n scvtf v2.4s, v2.4s\n scvtf v1.4s, v1.4s\n scvtf v0.4s, v0.4s\n fmul v31.4s, v3.4s, v31.4s\n fmul v30.4s, v2.4s, v30.4s\n fmul v29.4s, v1.4s, v29.4s\n fmul v28.4s, v0.4s, v28.4s\n ld1 { v5.4s }, [x22]\n uzp1 v4.2d, v27.2d, v26.2d\n uzp2 v3.2d, v27.2d, v26.2d\n add x22, x22, #0x10\n ldr q2, [x22, #0x0]\n uzp1 v1.2d, v25.2d, v24.2d\n uzp2 v0.2d, v25.2d, v24.2d\n mla v4.4s, v7.4s, v5.s[0]\n mla v3.4s, v7.4s, v5.s[1]\n mla v1.4s, v7.4s, v5.s[2]\n mla v0.4s, v7.4s, v5.s[3]\n fmul v27.4s, v6.4s, v2.s[0]\n fmul v26.4s, v6.4s, v2.s[1]\n fmul v25.4s, v6.4s, v2.s[2]\n scvtf v4.4s, v4.4s\n fmul v24.4s, v6.4s, v2.s[3]\n scvtf v3.4s, v3.4s\n scvtf v1.4s, v1.4s\n scvtf v0.4s, v0.4s\n fmul v27.4s, v4.4s, v27.4s\n fmul v26.4s, v3.4s, v26.4s\n fmul v25.4s, v1.4s, v25.4s\n fmul v24.4s, v0.4s, v24.4s\n ld1 { v5.4s }, [x21]\n uzp1 v4.2d, v23.2d, v22.2d\n uzp2 v3.2d, v23.2d, v22.2d\n add x21, x21, #0x10\n ldr q2, [x21, #0x0]\n uzp1 v1.2d, v21.2d, v20.2d\n uzp2 v0.2d, v21.2d, v20.2d\n mla v4.4s, v7.4s, v5.s[0]\n mla v3.4s, v7.4s, v5.s[1]\n mla v1.4s, v7.4s, v5.s[2]\n mla v0.4s, v7.4s, v5.s[3]\n fmul v23.4s, v6.4s, v2.s[0]\n fmul v22.4s, v6.4s, v2.s[1]\n fmul v21.4s, v6.4s, v2.s[2]\n scvtf v4.4s, v4.4s\n fmul v20.4s, v6.4s, v2.s[3]\n scvtf v3.4s, v3.4s\n scvtf v1.4s, v1.4s\n scvtf v0.4s, v0.4s\n fmul v23.4s, v4.4s, v23.4s\n fmul v22.4s, v3.4s, v22.4s\n fmul v21.4s, v1.4s, v21.4s\n fmul v20.4s, v0.4s, v20.4s\n ld1 { v5.4s }, [x20]\n uzp1 v4.2d, v19.2d, v18.2d\n uzp2 v3.2d, v19.2d, v18.2d\n add x20, x20, #0x10\n ldr q2, [x20, #0x0]\n uzp1 v1.2d, v17.2d, v16.2d\n uzp2 v0.2d, v17.2d, v16.2d\n mla v4.4s, v7.4s, v5.s[0]\n mla v3.4s, v7.4s, v5.s[1]\n mla v1.4s, v7.4s, v5.s[2]\n mla v0.4s, v7.4s, v5.s[3]\n fmul v19.4s, v6.4s, v2.s[0]\n fmul v18.4s, v6.4s, v2.s[1]\n fmul v17.4s, v6.4s, v2.s[2]\n scvtf v4.4s, v4.4s\n fmul v16.4s, v6.4s, v2.s[3]\n scvtf v3.4s, v3.4s\n scvtf v1.4s, v1.4s\n scvtf v0.4s, v0.4s\n fmul v19.4s, v4.4s, v19.4s\n fmul v18.4s, v3.4s, v18.4s\n fmul v17.4s, v1.4s, v17.4s\n fmul v16.4s, v0.4s, v16.4s\n ldr q2, [x11, #0x0]\n ld1r { v1.4s }, [x17]\n add x20, x17, #0x4\n cmp x10, #0x4\n ld1r { v0.4s }, [x20]\n add x11, x11, #0x10\n fadd v31.4s, v31.4s, v2.4s\n fadd v30.4s, v30.4s, v2.4s\n fadd v29.4s, v29.4s, v2.4s\n fadd v28.4s, v28.4s, v2.4s\n fadd v27.4s, v27.4s, v2.4s\n fadd v26.4s, v26.4s, v2.4s\n fadd v25.4s, v25.4s, v2.4s\n fadd v24.4s, v24.4s, v2.4s\n fadd v23.4s, v23.4s, v2.4s\n fadd v22.4s, v22.4s, v2.4s\n fadd v21.4s, v21.4s, v2.4s\n fadd v20.4s, v20.4s, v2.4s\n fadd v19.4s, v19.4s, v2.4s\n fadd v18.4s, v18.4s, v2.4s\n fadd v17.4s, v17.4s, v2.4s\n fadd v16.4s, v16.4s, v2.4s\n fmax v31.4s, v31.4s, v1.4s\n fmax v30.4s, v30.4s, v1.4s\n fmax v29.4s, v29.4s, v1.4s\n fmax v28.4s, v28.4s, v1.4s\n fmax v27.4s, v27.4s, v1.4s\n fmax v26.4s, v26.4s, v1.4s\n fmax v25.4s, v25.4s, v1.4s\n fmax v24.4s, v24.4s, v1.4s\n fmax v23.4s, v23.4s, v1.4s\n fmax v22.4s, v22.4s, v1.4s\n fmax v21.4s, v21.4s, v1.4s\n fmax v20.4s, v20.4s, v1.4s\n fmax v19.4s, v19.4s, v1.4s\n fmax v18.4s, v18.4s, v1.4s\n fmax v17.4s, v17.4s, v1.4s\n fmax v16.4s, v16.4s, v1.4s\n fmin v31.4s, v31.4s, v0.4s\n fmin v30.4s, v30.4s, v0.4s\n fmin v29.4s, v29.4s, v0.4s\n fmin v28.4s, v28.4s, v0.4s\n fmin v27.4s, v27.4s, v0.4s\n fmin v26.4s, v26.4s, v0.4s\n fmin v25.4s, v25.4s, v0.4s\n fmin v24.4s, v24.4s, v0.4s\n fmin v23.4s, v23.4s, v0.4s\n fmin v22.4s, v22.4s, v0.4s\n fmin v21.4s, v21.4s, v0.4s\n fmin v20.4s, v20.4s, v0.4s\n fmin v19.4s, v19.4s, v0.4s\n fmin v18.4s, v18.4s, v0.4s\n fmin v17.4s, v17.4s, v0.4s\n fmin v16.4s, v16.4s, v0.4s\n blt label_8\n mov x20, x19\n str q31, [x20, #0x0]\n add x20, x20, x28\n str q30, [x20, #0x0]\n add x20, x20, x28\n str q29, [x20, #0x0]\n add x20, x20, x28\n str q28, [x20, #0x0]\n add x20, x20, x28\n str q27, [x20, #0x0]\n add x20, x20, x28\n str q26, [x20, #0x0]\n add x20, x20, x28\n str q25, [x20, #0x0]\n add x20, x20, x28\n str q24, [x20, #0x0]\n add x20, x20, x28\n str q23, [x20, #0x0]\n add x20, x20, x28\n str q22, [x20, #0x0]\n add x20, x20, x28\n str q21, [x20, #0x0]\n add x20, x20, x28\n str q20, [x20, #0x0]\n add x20, x20, x28\n str q19, [x20, #0x0]\n add x20, x20, x28\n str q18, [x20, #0x0]\n add x20, x20, x28\n str q17, [x20, #0x0]\n add x20, x20, x28\n str q16, [x20, #0x0]\n b label_13\n\nKAI_ASM_LABEL(label_8) // Partial output\n mov x16, x19\n add x26, x16, x28, LSL #2\n add x25, x26, x28, LSL #1\n add x24, x26, x28\n add x23, x25, x28\n add x22, x16, x28, LSL #1\n add x21, x16, x28\n add x20, x22, x28\n add x27, x23, x28\n tbz x10, #1, label_9\n st1 { v24.d }[0], [x23], #0x8\n st1 { v25.d }[0], [x25], #0x8\n st1 { v26.d }[0], [x24], #0x8\n st1 { v27.d }[0], [x26], #0x8\n st1 { v28.d }[0], [x20], #0x8\n st1 { v29.d }[0], [x22], #0x8\n st1 { v30.d }[0], [x21], #0x8\n st1 { v31.d }[0], [x16], #0x8\n tbz x10, #0, label_10\n st1 { v24.s }[2], [x23]\n st1 { v25.s }[2], [x25]\n st1 { v26.s }[2], [x24]\n st1 { v27.s }[2], [x26]\n st1 { v28.s }[2], [x20]\n st1 { v29.s }[2], [x22]\n st1 { v30.s }[2], [x21]\n st1 { v31.s }[2], [x16]\n b label_10\n\nKAI_ASM_LABEL(label_9) // Output block 0: partial_1_0\n st1 { v24.s }[0], [x23]\n st1 { v25.s }[0], [x25]\n st1 { v26.s }[0], [x24]\n st1 { v27.s }[0], [x26]\n st1 { v28.s }[0], [x20]\n st1 { v29.s }[0], [x22]\n st1 { v30.s }[0], [x21]\n st1 { v31.s }[0], [x16]\n\nKAI_ASM_LABEL(label_10) // Output block 0: Done\n add x26, x27, x28, LSL #2\n add x25, x27, x28, LSL #1\n add x24, x26, x28, LSL #1\n add x23, x27, x28\n add x22, x25, x28\n add x21, x26, x28\n add x20, x24, x28\n tbz x10, #1, label_11\n st1 { v16.d }[0], [x20], #0x8\n st1 { v17.d }[0], [x24], #0x8\n st1 { v18.d }[0], [x21], #0x8\n st1 { v19.d }[0], [x26], #0x8\n st1 { v20.d }[0], [x22], #0x8\n st1 { v21.d }[0], [x25], #0x8\n st1 { v22.d }[0], [x23], #0x8\n st1 { v23.d }[0], [x27], #0x8\n tbz x10, #0, label_12\n st1 { v16.s }[2], [x20]\n st1 { v17.s }[2], [x24]\n st1 { v18.s }[2], [x21]\n st1 { v19.s }[2], [x26]\n st1 { v20.s }[2], [x22]\n st1 { v21.s }[2], [x25]\n st1 { v22.s }[2], [x23]\n st1 { v23.s }[2], [x27]\n b label_12\n\nKAI_ASM_LABEL(label_11) // Output block 1: partial_1_0\n st1 { v16.s }[0], [x20]\n st1 { v17.s }[0], [x24]\n st1 { v18.s }[0], [x21]\n st1 { v19.s }[0], [x26]\n st1 { v20.s }[0], [x22]\n st1 { v21.s }[0], [x25]\n st1 { v22.s }[0], [x23]\n st1 { v23.s }[0], [x27]\n\nKAI_ASM_LABEL(label_12) // Output block 1: Done\n\nKAI_ASM_LABEL(label_13) // Output stage exit\n subs x10, x10, #0x4\n add x19, x19, #0x10\n bgt label_2\n mov x20, #0x4\n sub x13, x13, #0x10\n cmp x13, #0x10\n mov x19, x9\n madd x15, x20, x12, x15\n bge label_1\n\nKAI_ASM_LABEL(label_14) // Row loop skip\n cbz x13, label_23\n\nKAI_ASM_LABEL(label_15) // Row tail: Row loop\n ldr x26, [x0, #16] // Reset rhs_packed\n ldr x25, [x0, #48] // Reset n\n add x24, x19, x28, LSL #2\n\nKAI_ASM_LABEL(label_16) // Row tail: Column loop\n mov x27, x15\n movi v31.4s, #0x0\n movi v30.4s, #0x0\n mov x20, x14 // num_blocks\n movi v29.4s, #0x0\n movi v28.4s, #0x0\n\nKAI_ASM_LABEL(label_17) // Row tail: Sub block loop\n ldr q19, [x26, #0x0]\n ldr q18, [x26, #0x10]\n subs x20, x20, #0x1\n ldr q17, [x27, #0x0]\n ldr q16, [x27, #0x10]\n ldr q27, [x26, #0x20]\n ldr q26, [x26, #0x30]\n ldr q25, [x27, #0x20]\n ldr q24, [x27, #0x30]\n ldr q23, [x26, #0x40]\n ldr q22, [x26, #0x50]\n KAI_ASM_INST(0x4e93a63f) // smmla v31.4s, v17.16b, v19.16b\n KAI_ASM_INST(0x4e92a63e) // smmla v30.4s, v17.16b, v18.16b\n ldr q21, [x27, #0x40]\n ldr q20, [x27, #0x50]\n KAI_ASM_INST(0x4e93a61d) // smmla v29.4s, v16.16b, v19.16b\n KAI_ASM_INST(0x4e92a61c) // smmla v28.4s, v16.16b, v18.16b\n ldr q19, [x26, #0x60]\n ldr q18, [x26, #0x70]\n add x26, x26, #0x80\n ldr q17, [x27, #0x60]\n ldr q16, [x27, #0x70]\n add x27, x27, #0x80\n KAI_ASM_INST(0x4e9ba73f) // smmla v31.4s, v25.16b, v27.16b\n KAI_ASM_INST(0x4e9aa73e) // smmla v30.4s, v25.16b, v26.16b\n KAI_ASM_INST(0x4e9ba71d) // smmla v29.4s, v24.16b, v27.16b\n KAI_ASM_INST(0x4e9aa71c) // smmla v28.4s, v24.16b, v26.16b\n KAI_ASM_INST(0x4e97a6bf) // smmla v31.4s, v21.16b, v23.16b\n KAI_ASM_INST(0x4e96a6be) // smmla v30.4s, v21.16b, v22.16b\n KAI_ASM_INST(0x4e97a69d) // smmla v29.4s, v20.16b, v23.16b\n KAI_ASM_INST(0x4e96a69c) // smmla v28.4s, v20.16b, v22.16b\n KAI_ASM_INST(0x4e93a63f) // smmla v31.4s, v17.16b, v19.16b\n KAI_ASM_INST(0x4e92a63e) // smmla v30.4s, v17.16b, v18.16b\n KAI_ASM_INST(0x4e93a61d) // smmla v29.4s, v16.16b, v19.16b\n KAI_ASM_INST(0x4e92a61c) // smmla v28.4s, v16.16b, v18.16b\n bgt label_17\n\n ldr q18, [x26, #0x0]\n ld1 { v17.4s }, [x27]\n uzp1 v24.2d, v31.2d, v30.2d\n uzp2 v23.2d, v31.2d, v30.2d\n ldr q22, [x26, #0x10]\n uzp1 v21.2d, v29.2d, v28.2d\n uzp2 v20.2d, v29.2d, v28.2d\n add x27, x27, #0x10\n ldr q16, [x27, #0x0]\n add x26, x26, #0x20\n mla v24.4s, v18.4s, v17.s[0]\n mla v23.4s, v18.4s, v17.s[1]\n mla v21.4s, v18.4s, v17.s[2]\n mla v20.4s, v18.4s, v17.s[3]\n fmul v19.4s, v22.4s, v16.s[0]\n fmul v18.4s, v22.4s, v16.s[1]\n fmul v17.4s, v22.4s, v16.s[2]\n fmul v16.4s, v22.4s, v16.s[3]\n scvtf v24.4s, v24.4s\n scvtf v23.4s, v23.4s\n scvtf v21.4s, v21.4s\n scvtf v20.4s, v20.4s\n fmul v31.4s, v24.4s, v19.4s\n fmul v30.4s, v23.4s, v18.4s\n fmul v29.4s, v21.4s, v17.4s\n fmul v28.4s, v20.4s, v16.4s\n ldr q18, [x26, #0x0]\n ld1r { v17.4s }, [x17]\n add x20, x17, #0x4\n cmp x25, #0x4\n ld1r { v16.4s }, [x20]\n add x26, x26, #0x10\n fadd v31.4s, v31.4s, v18.4s\n fadd v30.4s, v30.4s, v18.4s\n fadd v29.4s, v29.4s, v18.4s\n fadd v28.4s, v28.4s, v18.4s\n fmax v31.4s, v31.4s, v17.4s\n fmax v30.4s, v30.4s, v17.4s\n fmax v29.4s, v29.4s, v17.4s\n fmax v28.4s, v28.4s, v17.4s\n fmin v31.4s, v31.4s, v16.4s\n fmin v30.4s, v30.4s, v16.4s\n fmin v29.4s, v29.4s, v16.4s\n fmin v28.4s, v28.4s, v16.4s\n blt label_19\n mov x20, x19\n cmp x13, #0x1\n str q31, [x20, #0x0]\n add x20, x20, x28\n ble label_22\n cmp x13, #0x2\n str q30, [x20, #0x0]\n add x20, x20, x28\n ble label_22\n cmp x13, #0x3\n str q29, [x20, #0x0]\n add x20, x20, x28\n ble label_22\n str q28, [x20, #0x0]\n b label_22\n\nKAI_ASM_LABEL(label_19) // Row tail: Partial output\n add x16, sp, #144\n mov x23, x19\n cmp x13, #0x1\n add x22, x23, x28\n csel x22, x22, x16, GT\n cmp x13, #0x2\n add x21, x23, x28, LSL #1\n csel x21, x21, x16, GT\n cmp x13, #0x3\n add x20, x21, x28\n csel x20, x20, x16, GT\n tbz x25, #1, label_20\n st1 { v28.d }[0], [x20], #0x8\n st1 { v29.d }[0], [x21], #0x8\n st1 { v30.d }[0], [x22], #0x8\n st1 { v31.d }[0], [x23], #0x8\n tbz x25, #0, label_21\n st1 { v28.s }[2], [x20]\n st1 { v29.s }[2], [x21]\n st1 { v30.s }[2], [x22]\n st1 { v31.s }[2], [x23]\n b label_21\n\nKAI_ASM_LABEL(label_20) // Row tail: Output block 0: partial_1_0\n st1 { v28.s }[0], [x20]\n st1 { v29.s }[0], [x21]\n st1 { v30.s }[0], [x22]\n st1 { v31.s }[0], [x23]\n\nKAI_ASM_LABEL(label_21) // Row tail: Output block 0: Done\n\nKAI_ASM_LABEL(label_22) // Row tail: Output stage exit\n subs x25, x25, #0x4\n add x19, x19, #0x10\n bgt label_16\n subs x13, x13, #0x4\n add x15, x15, x12\n mov x19, x24\n bgt label_15\n\nKAI_ASM_LABEL(label_23) // Row tail: Row loop skip\n\n // Restore callee-saved registers x19-x28 and d8-d15\n ldp d14, d15, [sp, 128]\n ldp d12, d13, [sp, 112]\n ldp d10, d11, [sp, 96]\n ldp d8, d9, [sp, 80]\n ldp x27, x28, [sp, 64]\n ldp x25, x26, [sp, 48]\n ldp x23, x24, [sp, 32]\n ldp x21, x22, [sp, 16]\n ldp x19, x20, [sp], 176\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm)\n\n KAI_ASM_END\n"
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod.h",
|
| 44 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_lhs_quant_pack_qai8dxp_f32 to dynamically quantize and pack the LHS matrix in a single step.\n/// -# @ref kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon to pack the RHS NxK matrix.\n/// -# @ref kai_rhs_pack_kxn_qsi8cxp_qsi8cx_neon to pack the RHS KxN matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be 1.\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(\n size_t m_idx, //\n size_t k); //\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-channel quantization (qsi8cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of 4.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(\n size_t n_idx, //\n size_t k); //\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix. It must be 1.\n/// @param[in] n_idx Column index in the DST matrix. It must be multiple of 4.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the DST offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(\n size_t m_idx, //\n size_t n_idx, //\n size_t dst_stride); //\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(\n size_t m, //\n size_t n); //\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) and packed\n/// RHS matrix: Quantized Symmetric Signed 8-bit with per-channel quantization (qsi8cx) and packed.\n/// Output tile: (rows x cols) = 1 x 4\n///\n/// Features used: dotprod\n///\n/// @param[in] m The number of output rows written. It must be 1.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the\n/// top of this file.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the\n/// top of this file.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes.\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(\n size_t m, //\n size_t n, //\n size_t k, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n}\n#endif // __cplusplus\n"
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod.c",
|
| 48 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if !defined(__aarch64__) && !defined(__ARM_FEATURE_DOTPROD)\n#error \"Dotprod extension required to compile this micro-kernel\"\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n// Compute args\nstatic const size_t kai_m_step = 1;\nstatic const size_t kai_n_step = 4;\n// Packing args\nstatic const size_t kai_mr = 1;\nstatic const size_t kai_nr = 4;\nstatic const size_t kai_kr = 8;\nstatic const size_t kai_sr = 1;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 4;\nstatic const size_t kai_num_bytes_zp_lhs = 4;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_num_bytes_qvalue_rhs = 1;\nstatic const size_t kai_num_bytes_multiplier_rhs = 4;\nstatic const size_t kai_num_bytes_rsum_rhs = 4;\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_num_bytes_bias = 4;\nstatic const size_t kai_k_multiple_of = 32;\n\ninline static size_t kai_k_roundedup(size_t k) {\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n size_t lhs_packed_stride = kai_mr * ((k_internal * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs);\n // Since the LHS matrix is asymmetric with per-row quantization, we must include the\n // the number of bytes to hold the zero point value\n lhs_packed_stride += kai_mr * kai_num_bytes_zp_lhs;\n\n return lhs_packed_stride;\n}\n\ninline static size_t kai_rhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n size_t rhs_packed_stride = kai_nr * (k_internal * kai_num_bytes_qvalue_rhs);\n rhs_packed_stride += kai_nr * kai_num_bytes_multiplier_rhs;\n // Since the LHS matrix is quantized asymmetric with per-row quantization, we also include\n // the number of bytes for the reduction sum\n rhs_packed_stride += kai_nr * kai_num_bytes_rsum_rhs;\n // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias\n rhs_packed_stride += kai_nr * kai_num_bytes_bias;\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void) {\n return kai_m_step;\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void) {\n return kai_n_step;\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void) {\n return kai_mr;\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void) {\n return kai_nr;\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(size_t m_idx, size_t k) {\n KAI_ASSUME((m_idx % kai_mr) == 0);\n\n return (m_idx / kai_mr) * kai_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(size_t n_idx, size_t k) {\n KAI_ASSUME((n_idx % kai_nr) == 0);\n\n return (n_idx / kai_nr) * kai_rhs_packed_stride(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSUME((m_idx % kai_m_step) == 0);\n KAI_ASSUME((n_idx % kai_n_step) == 0);\n\n return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride;\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod(\n size_t m, //\n size_t n, //\n size_t k, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSUME(dst_stride_col == sizeof(float));\n\n if (m == 0) {\n return;\n }\n\n const size_t k_internal = kai_k_roundedup(k);\n const size_t num_blocks = k_internal / kai_k_multiple_of;\n const float clamp_vals[2] = {scalar_min, scalar_max};\n\n __asm__ __volatile__(\n \"mov x26, #0x20\\n\"\n \"mov x20, #0x8\\n\"\n \"mov x25, %x[m]\\n\"\n \"madd x26, %x[num_blocks], x26, x20\\n\"\n \"1:\" // Row loop\n \"mov x24, %x[rhs_packed]\\n\"\n \"mov x23, %x[n]\\n\"\n \"add x22, %x[dst], %x[dst_stride_row]\\n\"\n \"2:\" // Column loop\n \"mov x21, %x[lhs_packed]\\n\"\n \"movi v27.4s, #0x0\\n\"\n \"movi v26.4s, #0x0\\n\"\n \"mov x20, %x[num_blocks]\\n\"\n \"3:\" // Sub block loop\n \"ldr q25, [x24, #0x0]\\n\"\n \"ldr q17, [x24, #0x10]\\n\"\n \"subs x20, x20, #0x1\\n\"\n \"ld1r { v16.2d }, [x21], #0x8\\n\"\n \"ldr q24, [x24, #0x20]\\n\"\n \"ldr q23, [x24, #0x30]\\n\"\n \"ldr q22, [x24, #0x40]\\n\"\n \"ld1r { v21.2d }, [x21], #0x8\\n\"\n \"ldr q20, [x24, #0x50]\\n\"\n \"ld1r { v19.2d }, [x21], #0x8\\n\"\n \"ldr q18, [x24, #0x60]\\n\"\n \".inst 0x4e90973b // sdot v27.4s, v25.16b, v16.16b\\n\"\n \".inst 0x4e90963a // sdot v26.4s, v17.16b, v16.16b\\n\"\n \"ldr q17, [x24, #0x70]\\n\"\n \"add x24, x24, #0x80\\n\"\n \"ld1r { v16.2d }, [x21], #0x8\\n\"\n \".inst 0x4e95971b // sdot v27.4s, v24.16b, v21.16b\\n\"\n \".inst 0x4e9596fa // sdot v26.4s, v23.16b, v21.16b\\n\"\n \".inst 0x4e9396db // sdot v27.4s, v22.16b, v19.16b\\n\"\n \".inst 0x4e93969a // sdot v26.4s, v20.16b, v19.16b\\n\"\n \".inst 0x4e90965b // sdot v27.4s, v18.16b, v16.16b\\n\"\n \".inst 0x4e90963a // sdot v26.4s, v17.16b, v16.16b\\n\"\n \"bgt 3b\\n\"\n \"ldr q22, [x24, #0x0]\\n\"\n \"ld1r { v21.4s }, [x21]\\n\"\n \"addp v27.4s, v27.4s, v26.4s\\n\"\n \"add x21, x21, #0x4\\n\"\n \"ld1r { v20.4s }, [x21]\\n\"\n \"ldr q16, [x24, #0x10]\\n\"\n \"add x20, %x[clamp_vals], #0x4\\n\"\n \"cmp x23, #0x4\\n\"\n \"ldr q19, [x24, #0x20]\\n\"\n \"ld1r { v18.4s }, [%x[clamp_vals]]\\n\"\n \"add x24, x24, #0x30\\n\"\n \"ld1r { v17.4s }, [x20]\\n\"\n \"mla v27.4s, v22.4s, v21.s[0]\\n\"\n \"fmul v16.4s, v16.4s, v20.4s\\n\"\n \"scvtf v27.4s, v27.4s\\n\"\n \"fmul v16.4s, v27.4s, v16.4s\\n\"\n \"fadd v16.4s, v16.4s, v19.4s\\n\"\n \"fmax v16.4s, v16.4s, v18.4s\\n\"\n \"fmin v16.4s, v16.4s, v17.4s\\n\"\n \"blt 4f\\n\"\n \"str q16, [%x[dst], #0x0]\\n\"\n \"b 7f\\n\"\n \"4:\" // Partial output\n \"mov x20, %x[dst]\\n\"\n \"tbz x23, #1, 5f\\n\"\n \"st1 { v16.d }[0], [x20], #0x8\\n\"\n \"tbz x23, #0, 6f\\n\"\n \"st1 { v16.s }[2], [x20]\\n\"\n \"b 6f\\n\"\n \"5:\" // Output block 0: partial_1_0\n \"st1 { v16.s }[0], [x20]\\n\"\n \"6:\" // Output block 0: Done\n \"7:\" // Stores done\n \"subs x23, x23, #0x4\\n\"\n \"add %x[dst], %x[dst], #0x10\\n\"\n \"bgt 2b\\n\"\n \"subs x25, x25, #0x1\\n\"\n \"add %x[lhs_packed], %x[lhs_packed], x26\\n\"\n \"mov %x[dst], x22\\n\"\n \"bgt 1b\\n\"\n : [dst] \"+&r\"(dst), [lhs_packed] \"+&r\"(lhs_packed)\n : [clamp_vals] \"r\"(clamp_vals), [dst_stride_row] \"r\"(dst_stride_row), [m] \"r\"(m), [n] \"r\"(n),\n [num_blocks] \"r\"(num_blocks), [rhs_packed] \"r\"(rhs_packed)\n : \"cc\", \"memory\", \"v16\", \"v17\", \"v18\", \"v19\", \"v20\", \"v21\", \"v22\", \"v23\", \"v24\", \"v25\", \"v26\", \"v27\", \"x20\",\n \"x21\", \"x22\", \"x23\", \"x24\", \"x25\", \"x26\");\n}\n\n#endif // Architectural features check.\n"
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.h",
|
| 52 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qa8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS of the vector-by-matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs, size_t lhs_stride,\n void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.c",
|
| 56 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n\n#if defined(__aarch64__)\n#include <arm_neon.h>\n#endif\n#include <float.h>\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_per_multiplier = sizeof(float);\nstatic const size_t kai_num_bytes_per_offset = sizeof(int32_t);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return mr * (k_internal * sizeof(int8_t) + kai_num_bytes_per_multiplier + kai_num_bytes_per_offset);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) {\n // It always points to the beginning of the row\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return num_rows * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* restrict lhs,\n size_t lhs_stride, void* restrict lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n\n if (m == 0) {\n return;\n }\n\n const size_t num_rows = m;\n\n const float* src_ptr = lhs;\n\n const size_t dst_stride = kai_lhs_packed_stride(k, mr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n const int32_t num_blocks_k = (int32_t)(k / k_block_len);\n const int32_t num_blocks_k_internal = (int32_t)(k_internal / k_block_len);\n\n for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {\n float max0 = -FLT_MAX;\n float min0 = FLT_MAX;\n\n // Find min/max for each channel\n int32_t k_idx = 0;\n\n#if defined(__aarch64__)\n float32x4_t vmax0 = vdupq_n_f32(-FLT_MAX);\n float32x4_t vmin0 = vdupq_n_f32(FLT_MAX);\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float32x4_t src0_0 = vld1q_f32(src_ptr + 0 + (size_t)k_idx);\n const float32x4_t src0_1 = vld1q_f32(src_ptr + 4 + (size_t)k_idx);\n\n // Calculate the max\n vmax0 = vmaxq_f32(src0_0, vmax0);\n vmax0 = vmaxq_f32(vmax0, src0_1);\n\n // Calculate the min\n vmin0 = vminq_f32(src0_0, vmin0);\n vmin0 = vminq_f32(vmin0, src0_1);\n }\n // Get the max/min\n max0 = vmaxvq_f32(vmax0);\n min0 = vminvq_f32(vmin0);\n#endif\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float src0_0 = *(src_ptr + (size_t)k_idx);\n max0 = fmaxf(src0_0, max0);\n min0 = fminf(src0_0, min0);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n\n float zero_point0 =\n zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len * sizeof(int8_t));\n\n // Quantize the channels\n int32_t block_idx = 0;\n\n#if defined(__aarch64__)\n if (k_block_len == 8) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n const float32x4_t src_1 = vld1q_f32(src_ptr + k_idx_start + 4);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n float32x4_t v1_f32 = vmulq_n_f32(src_1, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int32x4_t v1_s32 = vcvtaq_s32_f32(v1_f32);\n\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n int16x4_t v1_s16 = vqmovn_s32(v1_s32);\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x8_t vnzp_s16 = vdupq_n_s16(nzp_s16);\n v_s16 = vaddq_s16(v_s16, vnzp_s16);\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(v_s16);\n vst1_s8((int8_t*)(dst_ptr), v0_s8);\n dst_ptr += 8 * sizeof(int8_t);\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n } else if (k_block_len == 4) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x4_t vnzp_s16 = vdup_n_s16(nzp_s16);\n int16x4_t v_s16 = vadd_s16(v0_s16, vnzp_s16);\n v_s16 = vmax_s16(v_s16, vdup_n_s16(INT8_MIN));\n v_s16 = vmin_s16(v_s16, vdup_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(vcombine_s16(v_s16, vdup_n_s16(0)));\n\n *((int8_t*)(dst_ptr)) = vget_lane_s8(v0_s8, 0);\n *((int8_t*)(dst_ptr + 1)) = vget_lane_s8(v0_s8, 1);\n *((int8_t*)(dst_ptr + 2)) = vget_lane_s8(v0_s8, 2);\n *((int8_t*)(dst_ptr + 3)) = vget_lane_s8(v0_s8, 3);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n } else\n#endif\n {\n for (; block_idx < num_blocks_k; ++block_idx) {\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n const int32_t k_idx_start = (block_idx * k_block_len) + k_block_idx;\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n dst_ptr = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr += dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params\n *((float*)(dst_ptr)) = recip_scale0;\n\n src_ptr += (lhs_stride / sizeof(float));\n\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n}\n"
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"path": "kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h",
|
| 60 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Get the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n///\n/// @return the n step value\nsize_t kai_get_n_step_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t nr);\n\n/// Gets the offset in bytes for the RHS matrix (not packed).\n///\n/// @note The int8 values are stored in a N x K matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n_idx, size_t rhs_stride);\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Gets the offset in bytes for the packed RHS matrix, which contains the packed 8-bit quantized symmetric per-channel\n/// (qs8cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// @brief Gets the size in bytes for the packed RHS matrix\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int8 values are stored in a N x K matrix.\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of rows.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K). It must be an even value.\n/// @param[in] nr The number of N rows to interleave on the same output output row.\n/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n/// @param[in] rhs The RHS matrix containing the 8-bit values.\n/// Size in bytes is expected to be equal to or greater than n * k * sizeof(uint8_t).\n/// @param[in] bias The biases.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n const int8_t* rhs, //\n const float* bias, //\n const float* scale, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_qsi8cx_params* params);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"path": "kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.c",
|
| 64 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nsize_t kai_get_n_step_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t nr) {\n return nr;\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n const size_t k_internal = kai_k_roundedup(k);\n\n return nr * (k_internal + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_ASSERT((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n, size_t k, size_t nr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n}\n\nvoid kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n const int8_t* rhs, //\n const float* bias, //\n const float* scale, //\n void* rhs_packed, //\n size_t extra_bytes, const struct kai_rhs_pack_qsi8cx_params* params) {\n KAI_ASSERT(num_groups == 1);\n KAI_ASSERT(extra_bytes == 0);\n KAI_ASSERT(sr == 1);\n KAI_ASSERT(rhs != NULL);\n KAI_ASSERT(scale != NULL);\n KAI_ASSERT(rhs_packed != NULL);\n KAI_ASSERT(params != NULL);\n\n const int32_t lhs_zero_point = params->lhs_zero_point;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const size_t dst_num_rows = kai_roundup(n, nr) / nr;\n const size_t dst_num_bytes_per_row = nr * k_internal;\n const size_t rhs_stride = k;\n\n for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; ++dst_row_idx) {\n uint8_t* dst_row = (uint8_t*)rhs_packed + dst_row_idx * rhs_packed_stride;\n\n int32_t* sums = (int32_t*)(dst_row + nr * k_internal);\n\n // Initialize to zero the RHS reduction sums\n memset(sums, 0, nr * sizeof(int32_t));\n\n for (size_t dst_offset = 0; dst_offset < dst_num_bytes_per_row; dst_offset += kr) {\n const size_t block_idx = dst_offset / kr;\n const size_t nr_idx = block_idx % nr;\n const size_t super_block_idx = block_idx / nr;\n\n const size_t k0_idx = super_block_idx * kr;\n const size_t n0_idx = dst_row_idx * nr + nr_idx;\n\n // Clamp the index to avoid out-of-bound reads\n const size_t n0_valid_idx = KAI_MIN(n0_idx, n - 1);\n\n const size_t src_offset = n0_valid_idx * rhs_stride;\n\n int32_t partial_sum = 0;\n\n // Get the partial reduction sum\n for (size_t i = 0; i < kr; i++) {\n const size_t k0_valid_idx = k0_idx + i;\n int8_t v = 0;\n if (k0_valid_idx < k) {\n v = rhs[src_offset + k0_valid_idx];\n }\n ((int8_t*)dst_row)[i] = v;\n partial_sum += v;\n }\n\n sums[nr_idx] += partial_sum * lhs_zero_point;\n\n dst_row += kr;\n }\n\n // Adjust the reduction sums\n for (size_t i = 0; i < nr; ++i) {\n dst_row += sizeof(int32_t);\n }\n\n // Adjust the scales\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n *((float*)(dst_row)) = scale[src_row_idx];\n dst_row += sizeof(float);\n }\n\n // Set the bias\n if (bias == NULL) {\n memset(dst_row, 0, nr * kai_num_bytes_bias);\n } else {\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n ((float*)dst_row)[i] = bias[src_row_idx];\n }\n }\n }\n}\n"
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"path": "binding.cpp",
|
| 68 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm against gemm_qs8cx_n512_k512's raw ABI:\n// armbench_entry_gemm(const float* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M)\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.h\"\n#include \"kai_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod.h\"\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n#include \"kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h\"\n\nnamespace {\nconstexpr int N = 512;\nconstexpr int K = 512;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M)\n{\n // M == 1 runs the dedicated GEMV ukernel, as a KleidiAI user would pick it. Both variants\n // share one packed-RHS layout (same nr/kr/sr), so a single cached weight pack serves both.\n const bool gemv = (M == 1);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm();\n const size_t kr = kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm();\n const size_t sr = kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm();\n if (kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod() != nr || kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod() != kr ||\n kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod() != sr) return -2;\n const size_t mr = gemv ? kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod() : kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = gemv ? (kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr)) : (kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr));\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n if (gemv) {\n kai_run_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n } else {\n kai_run_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n }\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q8_snap = nullptr;\n static size_t B_q8_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q8_snap, B_q8_snap_size, B_q8, (size_t)N*K)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N)\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon((size_t)N, (size_t)K, nr, kr, sr);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_qsi8cx_params params;\n params.lhs_zero_point = 1;\n params.scale_multiplier = 1.0f;\n kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(1, (size_t)N, (size_t)K, nr, kr, sr, B_q8, bias, B_scales, rhs_packed, 0, ¶ms);\n }\n\n if (gemv) {\n kai_run_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod((size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n } else {\n kai_run_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm((size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n }\n return 0;\n}\n"
|
| 69 |
+
}
|
| 70 |
+
]
|
| 71 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qs8cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qs8cx_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_qs8cx_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qs8cx_sme2_n256_k512: KleidiAI kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa_asm.S",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\n\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\n stp x19, x20, [sp, -96]!\n stp x24, x25, [sp, 16]\n stp d8, d9, [sp, 32]\n stp d10, d11, [sp, 48]\n stp d12, d13, [sp, 64]\n stp d14, d15, [sp, 80]\n KAI_ASM_INST(0xd503477f)\t// \tsmstart\n ldr\tx19, [x0] // dst\n ldr\tx20, [x0, #0x8] // lhs_packed\n cntw\tx7\n ptrue\tp2.b\n KAI_ASM_INST(0x8556c81e) // ld1rw { z30.s }, p2/z, [x0, #0x58] // clamp_min\n KAI_ASM_INST(0x8557c81f) // ld1rw { z31.s }, p2/z, [x0, #0x5c] // clamp_max\n ldr\tx12, [x0, #0x20] // m\n KAI_ASM_INST(0x25ac17e0)\t// \twhilelt\tp0.s, xzr, x12\nKAI_ASM_LABEL(label_1) // Row Loop\n ldr\tx8, [x0, #0x10] // rhs_packed\n mov\tx9, x19\n ldr\tx13, [x0, #0x28] // n\n cmp\tx7, x12\n csel\tx16, x7, x12, lt\n lsl\tx16, x16, #2\n ldr\tx24, [x0, #0x40] // rhs_row_bytes\n add\tx24, x24, x8\n mov\tx11, x8\n KAI_ASM_INST(0x25784570)\t// \twhilelt\tpn8.h, x11, x24, vlx2\n addvl\tx11, x8, #0x2\n KAI_ASM_INST(0x25784572)\t// \twhilelt\tpn10.h, x11, x24, vlx2\nKAI_ASM_LABEL(label_2) // Column Loop\n mov\tx10, x20\n mov\tx11, x8\n mov\tx17, x9\n KAI_ASM_INST(0x25ad67f1)\t// \twhilelt\tpn9.s, xzr, x13, vlx4\n KAI_ASM_INST(0xc00800ff)\t// \tzero\t{za}\n ldr\tx24, [x0, #0x48] // m_blk\n add\tx14, x10, x24\nKAI_ASM_LABEL(label_3) // Block Loop\n ld1w\t{ z4.s }, p0/z, [x10]\n addvl\tx10, x10, #0x1\n KAI_ASM_INST(0xa0402168)\t// \tld1h\t{ z8.h, z9.h }, pn8/z, [x11]\n KAI_ASM_INST(0xa0884880)\t// \tsmopa\tza0.s, p2/m, p2/m, z4.b, z8.b\n KAI_ASM_INST(0xa0894881)\t// \tsmopa\tza1.s, p2/m, p2/m, z4.b, z9.b\n KAI_ASM_INST(0xa041296a)\t// \tld1h\t{ z10.h, z11.h }, pn10/z, [x11, #0x2, mul vl]\n KAI_ASM_INST(0xa08a4882)\t// \tsmopa\tza2.s, p2/m, p2/m, z4.b, z10.b\n KAI_ASM_INST(0xa08b4883)\t// \tsmopa\tza3.s, p2/m, p2/m, z4.b, z11.b\n addvl\tx11, x11, #0x4\n cmp\tx10, x14\n b.lt\tlabel_3\n KAI_ASM_INST(0xa040c560)\t// \tld1w\t{ z0.s - z3.s }, pn9/z, [x11]\n KAI_ASM_INST(0xa041c564)\t// \tld1w\t{ z4.s - z7.s }, pn9/z, [x11, #0x4, mul vl]\n KAI_ASM_INST(0xa042c568)\t// \tld1w\t{ z8.s - z11.s }, pn9/z, [x11, #0x8, mul vl]\n addvl\tx11, x11, #0xc\n mov\tx14, #0x0 // =0\n addvl\tx15, x10, #0x1\nKAI_ASM_LABEL(label_4)\n KAI_ASM_INST(0x8540c950) // ld1rw { z16.s }, p2/z, [x10]\n KAI_ASM_INST(0x8540c9f1) // ld1rw { z17.s }, p2/z, [x15]\n add\tx10, x10, #0x4\n add\tx15, x15, #0x4\n KAI_ASM_INST(0xc006440c)\t// \tmov\t{ z12.b - z15.b }, za0h.b[w14, 0x0:0x3]\n fmul\tz20.s, z17.s, z4.s\n fmul\tz21.s, z17.s, z5.s\n fmul\tz22.s, z17.s, z6.s\n fmul\tz23.s, z17.s, z7.s\n mla\t z12.s, p2/m, z16.s, z0.s\n mla\t z13.s, p2/m, z16.s, z1.s\n mla\t z14.s, p2/m, z16.s, z2.s\n mla\t z15.s, p2/m, z16.s, z3.s\n KAI_ASM_INST(0xc132e18c)\t// \tscvtf\t{ z12.s - z15.s }, { z12.s - z15.s }\n fmul\tz24.s, z20.s, z12.s\n fmul\tz25.s, z21.s, z13.s\n fmul\tz26.s, z22.s, z14.s\n fmul\tz27.s, z23.s, z15.s\n fadd\tz24.s, p2/m, z24.s, z8.s\n fadd\tz25.s, p2/m, z25.s, z9.s\n fadd\tz26.s, p2/m, z26.s, z10.s\n fadd\tz27.s, p2/m, z27.s, z11.s\n KAI_ASM_INST(0xc1bfcbd8) // fclamp\t{ z24.s - z27.s }, z30.s, z31.s\n KAI_ASM_INST(0xa060c638) // \tst1w\t{ z24.s - z27.s }, pn9, [x17]\n ldr\tx24, [x0, #0x18] // dst_stride_row\n add\tx17, x17, x24\n add\tx14, x14, #0x4\n cmp\tx14, x16\n b.lt label_4\n ldr\tx24, [x0, #0x38] // rhs_stride\n add\tx8, x8, x24\n addvl\tx9, x9, #0x4\n ldr\tx24, [x0, #0x40] // rhs_row_bytes\n add\tx24, x24, x8\n mov\tx11, x8\n KAI_ASM_INST(0x25784570)\t// \twhilelt\tpn8.h, x11, x24, vlx2\n addvl\tx11, x8, #0x2\n KAI_ASM_INST(0x25784572)\t// \twhilelt\tpn10.h, x11, x24, vlx2\n cntw\tx24, ALL, MUL #0x4\n sub\tx13, x13, x24\n subs xzr, xzr, x13 // cmp xzr, x13\n b.mi\tlabel_2\n ldr\tx24, [x0, #0x30]\n add\tx20, x20, x24\n ldr\tx24, [x0, #0x50]\n add\tx19, x19, x24\n cntw\tx24\n sub\tx12, x12, x24\n whilelt\tp0.s, xzr, x12\n b.mi\tlabel_1\n KAI_ASM_INST(0xd503467f)\t// \tsmstop\n ldp d14, d15, [sp, 80]\n ldp d12, d13, [sp, 64]\n ldp d10, d11, [sp, 48]\n ldp d8, d9, [sp, 32]\n ldp x24, x25, [sp, 16]\n ldp x19, x20, [sp], 96\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.c",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error \"This file must be compiled for AArch64, FEAT_SVE2\"\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.h\"\n\n#include <stddef.h>\n\n#include \"kai/kai_common.h\"\n\ntypedef struct {\n float* dst; // 0\n const void* lhs_packed; // 0x8\n const void* rhs_packed; // 0x10\n size_t dst_stride_row; // 0x18\n size_t m; // 0x20\n size_t n; // 0x28\n size_t lhs_stride; // 0x30\n size_t rhs_stride; // 0x38\n size_t rhs_row_bytes; // 0x40\n size_t m_blk; // 0x48\n size_t dst_inc; // 0x50\n float clamp_min; // 0x58\n float clamp_max; // 0x5c\n} KernelArgs;\n\nvoid kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(KernelArgs* args_ptr);\n\n// Compute args\nstatic const size_t kai_m_step = 1; // multiple of vector length\nstatic const size_t kai_n_step = 4; // multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // multiple of vector length\nstatic const size_t kai_nr = 4; // multiple of vector length\nstatic const size_t kai_kr = 4;\nstatic const size_t kai_sr = 1;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = 1;\nstatic const size_t kai_num_bytes_multiplier_lhs = 4;\nstatic const size_t kai_num_bytes_zp_lhs = 4;\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is\n// asymmetric))\nstatic const size_t kai_num_bytes_qvalue_rhs = 1;\nstatic const size_t kai_num_bytes_multiplier_rhs = 4;\nstatic const size_t kai_num_bytes_rsum_rhs = 4;\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = 4;\n// Extra args\nstatic const size_t kai_num_bytes_bias = 4;\nstatic const size_t kai_k_multiple_of = 32;\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_get_lhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n KAI_ASSERT((k_internal % kai_k_multiple_of) == 0);\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n size_t lhs_packed_stride = mr * ((k_internal * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs);\n // Since the LHS matrix is asymmetric with per-row quantization, we must include the\n // the number of bytes to hold the zero point value\n lhs_packed_stride += mr * kai_num_bytes_zp_lhs;\n\n return lhs_packed_stride;\n}\n\ninline static size_t kai_get_rhs_packed_stride(size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n KAI_ASSERT((k_internal % kai_k_multiple_of) == 0);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n size_t rhs_packed_stride = nr * (k_internal * kai_num_bytes_qvalue_rhs);\n rhs_packed_stride += nr * kai_num_bytes_multiplier_rhs;\n // Since the LHS matrix is quantized asymmetric with per-row quantization, we also include\n // the number of bytes for the reduction sum\n rhs_packed_stride += nr * kai_num_bytes_rsum_rhs;\n // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias\n rhs_packed_stride += nr * kai_num_bytes_bias;\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u8() / kai_kr;\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u8() / kai_kr;\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u8() / kai_kr;\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u8() / kai_kr;\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t m_idx, size_t k) {\n KAI_ASSERT((m_idx % kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n\n return (m_idx / mr) * kai_get_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t n_idx, size_t k) {\n KAI_ASSERT((n_idx % kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n\n return (n_idx / nr) * kai_get_rhs_packed_stride(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSERT((m_idx % kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n KAI_ASSERT((n_idx % kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n\n return ((n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride);\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n) {\n return (m * n * kai_num_bytes_dst_value);\n}\n\nvoid kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n const void* restrict lhs_packed, //\n const void* restrict rhs_packed, //\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max) {\n KAI_ASSERT(dst_stride_col == sizeof(float));\n KAI_ASSERT(n > 0);\n KAI_ASSERT(m > 0);\n\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n\n KernelArgs args;\n const size_t k_internal = kai_k_roundedup(k);\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.dst_stride_row = dst_stride_row;\n args.m = m;\n args.n = n;\n args.lhs_stride = kai_get_lhs_packed_stride(k);\n args.rhs_stride = kai_get_rhs_packed_stride(k);\n args.rhs_row_bytes = nr * k_internal;\n args.m_blk = mr * k_internal;\n args.dst_inc = mr * dst_stride_row;\n args.clamp_min = scalar_min;\n args.clamp_max = scalar_max;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(&args);\n}\n\n#endif // Architectural feature check\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.h",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# @ref kai_lhs_quant_pack_qai8dxp_f32 to dynamically quantize and pack the LHS matrix in a single step.\n/// -# @ref kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon to pack the RHS NxK matrix.\n/// -# @ref kai_rhs_pack_kxn_qsi8cxp_qsi8cx_neon to pack the RHS KxN matrix.\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix.\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the LHS and RHS matrices\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t m_idx, size_t k);\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed Quantized Symmetric Signed 8-bit with per-channel quantization (qsi8cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t n_idx, size_t k);\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix.\n/// @param[in] n_idx Column index in the DST matrix.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the destination(DST) offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride);\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n);\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) and packed\n/// RHS matrix: Quantized Symmetric Signed 8-bit with per-channel quantization (qsi8cx) and packed.\n/// Output tile: (rows x cols) = 1vl x 4vl\n///\n/// Features used: sme2\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix.\n/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the\n/// top of this file.\n/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the\n/// top of this file.\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float).\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\nvoid kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m, //\n size_t n, //\n size_t k, //\n const void* lhs_packed, //\n const void* rhs_packed, //\n float* dst, //\n size_t dst_stride_row, //\n size_t dst_stride_col, //\n float scalar_min, //\n float scalar_max); //\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.h",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qa8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS of the vector-by-matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs, size_t lhs_stride,\n void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.c",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n\n#if defined(__aarch64__)\n#include <arm_neon.h>\n#endif\n#include <float.h>\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_per_multiplier = sizeof(float);\nstatic const size_t kai_num_bytes_per_offset = sizeof(int32_t);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return mr * (k_internal * sizeof(int8_t) + kai_num_bytes_per_multiplier + kai_num_bytes_per_offset);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) {\n // It always points to the beginning of the row\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return num_rows * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* restrict lhs,\n size_t lhs_stride, void* restrict lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n\n if (m == 0) {\n return;\n }\n\n const size_t num_rows = m;\n\n const float* src_ptr = lhs;\n\n const size_t dst_stride = kai_lhs_packed_stride(k, mr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n const int32_t num_blocks_k = (int32_t)(k / k_block_len);\n const int32_t num_blocks_k_internal = (int32_t)(k_internal / k_block_len);\n\n for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {\n float max0 = -FLT_MAX;\n float min0 = FLT_MAX;\n\n // Find min/max for each channel\n int32_t k_idx = 0;\n\n#if defined(__aarch64__)\n float32x4_t vmax0 = vdupq_n_f32(-FLT_MAX);\n float32x4_t vmin0 = vdupq_n_f32(FLT_MAX);\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float32x4_t src0_0 = vld1q_f32(src_ptr + 0 + (size_t)k_idx);\n const float32x4_t src0_1 = vld1q_f32(src_ptr + 4 + (size_t)k_idx);\n\n // Calculate the max\n vmax0 = vmaxq_f32(src0_0, vmax0);\n vmax0 = vmaxq_f32(vmax0, src0_1);\n\n // Calculate the min\n vmin0 = vminq_f32(src0_0, vmin0);\n vmin0 = vminq_f32(vmin0, src0_1);\n }\n // Get the max/min\n max0 = vmaxvq_f32(vmax0);\n min0 = vminvq_f32(vmin0);\n#endif\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float src0_0 = *(src_ptr + (size_t)k_idx);\n max0 = fmaxf(src0_0, max0);\n min0 = fminf(src0_0, min0);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n\n float zero_point0 =\n zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len * sizeof(int8_t));\n\n // Quantize the channels\n int32_t block_idx = 0;\n\n#if defined(__aarch64__)\n if (k_block_len == 8) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n const float32x4_t src_1 = vld1q_f32(src_ptr + k_idx_start + 4);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n float32x4_t v1_f32 = vmulq_n_f32(src_1, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int32x4_t v1_s32 = vcvtaq_s32_f32(v1_f32);\n\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n int16x4_t v1_s16 = vqmovn_s32(v1_s32);\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x8_t vnzp_s16 = vdupq_n_s16(nzp_s16);\n v_s16 = vaddq_s16(v_s16, vnzp_s16);\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(v_s16);\n vst1_s8((int8_t*)(dst_ptr), v0_s8);\n dst_ptr += 8 * sizeof(int8_t);\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n } else if (k_block_len == 4) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x4_t vnzp_s16 = vdup_n_s16(nzp_s16);\n int16x4_t v_s16 = vadd_s16(v0_s16, vnzp_s16);\n v_s16 = vmax_s16(v_s16, vdup_n_s16(INT8_MIN));\n v_s16 = vmin_s16(v_s16, vdup_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(vcombine_s16(v_s16, vdup_n_s16(0)));\n\n *((int8_t*)(dst_ptr)) = vget_lane_s8(v0_s8, 0);\n *((int8_t*)(dst_ptr + 1)) = vget_lane_s8(v0_s8, 1);\n *((int8_t*)(dst_ptr + 2)) = vget_lane_s8(v0_s8, 2);\n *((int8_t*)(dst_ptr + 3)) = vget_lane_s8(v0_s8, 3);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n } else\n#endif\n {\n for (; block_idx < num_blocks_k; ++block_idx) {\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n const int32_t k_idx_start = (block_idx * k_block_len) + k_block_idx;\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n dst_ptr = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr += dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params\n *((float*)(dst_ptr)) = recip_scale0;\n\n src_ptr += (lhs_stride / sizeof(float));\n\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n}\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.c",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nsize_t kai_get_n_step_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t nr) {\n return nr;\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n const size_t k_internal = kai_k_roundedup(k);\n\n return nr * (k_internal + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_ASSERT((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n, size_t k, size_t nr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n}\n\nvoid kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n const int8_t* rhs, //\n const float* bias, //\n const float* scale, //\n void* rhs_packed, //\n size_t extra_bytes, const struct kai_rhs_pack_qsi8cx_params* params) {\n KAI_ASSERT(num_groups == 1);\n KAI_ASSERT(extra_bytes == 0);\n KAI_ASSERT(sr == 1);\n KAI_ASSERT(rhs != NULL);\n KAI_ASSERT(scale != NULL);\n KAI_ASSERT(rhs_packed != NULL);\n KAI_ASSERT(params != NULL);\n\n const int32_t lhs_zero_point = params->lhs_zero_point;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(k, nr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const size_t dst_num_rows = kai_roundup(n, nr) / nr;\n const size_t dst_num_bytes_per_row = nr * k_internal;\n const size_t rhs_stride = k;\n\n for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; ++dst_row_idx) {\n uint8_t* dst_row = (uint8_t*)rhs_packed + dst_row_idx * rhs_packed_stride;\n\n int32_t* sums = (int32_t*)(dst_row + nr * k_internal);\n\n // Initialize to zero the RHS reduction sums\n memset(sums, 0, nr * sizeof(int32_t));\n\n for (size_t dst_offset = 0; dst_offset < dst_num_bytes_per_row; dst_offset += kr) {\n const size_t block_idx = dst_offset / kr;\n const size_t nr_idx = block_idx % nr;\n const size_t super_block_idx = block_idx / nr;\n\n const size_t k0_idx = super_block_idx * kr;\n const size_t n0_idx = dst_row_idx * nr + nr_idx;\n\n // Clamp the index to avoid out-of-bound reads\n const size_t n0_valid_idx = KAI_MIN(n0_idx, n - 1);\n\n const size_t src_offset = n0_valid_idx * rhs_stride;\n\n int32_t partial_sum = 0;\n\n // Get the partial reduction sum\n for (size_t i = 0; i < kr; i++) {\n const size_t k0_valid_idx = k0_idx + i;\n int8_t v = 0;\n if (k0_valid_idx < k) {\n v = rhs[src_offset + k0_valid_idx];\n }\n ((int8_t*)dst_row)[i] = v;\n partial_sum += v;\n }\n\n sums[nr_idx] += partial_sum * lhs_zero_point;\n\n dst_row += kr;\n }\n\n // Adjust the reduction sums\n for (size_t i = 0; i < nr; ++i) {\n dst_row += sizeof(int32_t);\n }\n\n // Adjust the scales\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n *((float*)(dst_row)) = scale[src_row_idx];\n dst_row += sizeof(float);\n }\n\n // Set the bias\n if (bias == NULL) {\n memset(dst_row, 0, nr * kai_num_bytes_bias);\n } else {\n for (size_t i = 0; i < nr; ++i) {\n // Clamp the row index to avoid out-of-bound reads\n const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1);\n ((float*)dst_row)[i] = bias[src_row_idx];\n }\n }\n }\n}\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Get the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n///\n/// @return the n step value\nsize_t kai_get_n_step_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t nr);\n\n/// Gets the offset in bytes for the RHS matrix (not packed).\n///\n/// @note The int8 values are stored in a N x K matrix.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n_idx, size_t rhs_stride);\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Gets the offset in bytes for the packed RHS matrix, which contains the packed 8-bit quantized symmetric per-channel\n/// (qs8cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// @brief Gets the size in bytes for the packed RHS matrix\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(size_t n, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int8 values are stored in a N x K matrix.\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of rows.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K). It must be an even value.\n/// @param[in] nr The number of N rows to interleave on the same output output row.\n/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It must be 1.\n/// @param[in] rhs The RHS matrix containing the 8-bit values.\n/// Size in bytes is expected to be equal to or greater than n * k * sizeof(uint8_t).\n/// @param[in] bias The biases.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\nvoid kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(\n size_t num_groups, //\n size_t n, //\n size_t k, //\n size_t nr, //\n size_t kr, //\n size_t sr, //\n const int8_t* rhs, //\n const float* bias, //\n const float* scale, //\n void* rhs_packed, //\n size_t extra_bytes, //\n const struct kai_rhs_pack_qsi8cx_params* params);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "binding.cpp",
|
| 65 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa against gemm_qs8cx_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const float* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M)\n#include <arm_neon.h>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n#include \"kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n// Widen n raw fp16 patterns to fp32 (exact).\nvoid widen_f16(const uint16_t* src, float* dst, size_t n) {\n size_t i = 0;\n for (; i + 4 <= n; i += 4) vst1q_f32(dst + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(src + i))));\n for (; i < n; ++i) dst[i] = vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(src[i]))), 0);\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M)\n{\n // The SME2 mopa kernel takes any M in a single call, so there is no GEMV dispatch here.\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q8_snap = nullptr;\n static size_t B_q8_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q8_snap, B_q8_snap_size, B_q8, (size_t)N*K)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N)\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon((size_t)N, (size_t)K, nr, kr, sr);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_qsi8cx_params params;\n params.lhs_zero_point = 1;\n params.scale_multiplier = 1.0f;\n kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(1, (size_t)N, (size_t)K, nr, kr, sr, B_q8, bias, B_scales, rhs_packed, 0, ¶ms);\n }\n\n kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa((size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());\n return 0;\n}\n"
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qsu2cx_n256_k512.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_qsu2cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "baseline-kleidiai-arm_gemm_qsu2cx_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_qsu2cx_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "baseline-kleidiai-arm",
|
| 6 |
+
"description": "baseline-kleidiai-arm baseline for gemm_qsu2cx_sme2_n256_k512: KleidiAI kai_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa with its own LHS/RHS packers; binding.cpp bakes N/K and caches the weight pack.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "binding.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [
|
| 16 |
+
"sme2"
|
| 17 |
+
],
|
| 18 |
+
"compile_flags": [
|
| 19 |
+
"-O2",
|
| 20 |
+
"-march=armv8.2-a+sve+sve2",
|
| 21 |
+
"-fno-tree-vectorize",
|
| 22 |
+
"-fno-tree-slp-vectorize"
|
| 23 |
+
],
|
| 24 |
+
"link_flags": []
|
| 25 |
+
},
|
| 26 |
+
"sources": [
|
| 27 |
+
{
|
| 28 |
+
"path": "kai/kai_common.h",
|
| 29 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#if defined(__ARM_NEON)\n#include <arm_neon.h>\n#endif // defined(__ARM_NEON)\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n//\n// * cppcoreguidelines-avoid-do-while: do-while is necessary for macros.\n// * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected.\n// * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting.\n\n#ifndef KLEIDIAI_ERROR_TRAP\n#define KLEIDIAI_ERROR_TRAP 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0\n#endif\n\n#ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KLEIDIAI_HAS_BUILTIN_ASSUME0 0\n#endif\n\n#if KLEIDIAI_ERROR_TRAP\n#define KAI_ABORT() __builtin_trap()\n#else\n#define KAI_ABORT() abort()\n#endif\n\n#if KLEIDIAI_HAS_BUILTIN_UNREACHABLE\n#define KAI_UNREACHABLE() __builtin_unreachable()\n#elif KLEIDIAI_HAS_BUILTIN_ASSUME0\n#define KAI_UNREACHABLE() __assume(0);\n#else\n#define KAI_UNREACHABLE()\n#endif\n\n#ifdef NDEBUG\n#define KAI_ERROR(msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) \\\n do { \\\n KAI_UNUSED(msg); \\\n if (!(cond)) { \\\n KAI_UNREACHABLE(); \\\n } \\\n } while (0)\n#else\n#define KAI_ERROR(msg) \\\n do { \\\n fflush(stdout); \\\n fprintf(stderr, \"%s:%d %s\", __FILE__, __LINE__, msg); \\\n KAI_ABORT(); \\\n } while (0)\n\n#define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg)\n#endif // NDEBUG\n\n#define KAI_ASSERT_ALWAYS_MSG(cond, msg) \\\n do { \\\n if (!(cond)) { \\\n KAI_ERROR(msg); \\\n } \\\n } while (0)\n\n// NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c)\n\n/// KAI_ASSERT* is used for logic sanity checking in the program\n/// flow. Checks are optimized away in release builds same as\n/// `assert`\n#define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond)\n#define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond)\n#define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg)\n#define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond \" |-> \" #cond)\n\n/// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23.\n/// So KAI_ASSUME should be used directly on the function parameters, rather than inside\n/// function logic.\n#define KAI_ASSUME_MSG KAI_ASSERT_MSG\n#define KAI_ASSUME KAI_ASSERT\n#define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG\n#define KAI_ASSUME_IF KAI_ASSERT_IF\n\n/// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG`\n#define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG\n#define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS\n#define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG\n#define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF\n\n/// Indicate that result of `x` is unused\n#define KAI_UNUSED(x) (void)(x)\n\n/// Return minimum or maximum of `a` and `b`\n#define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b))\n#define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b))\n\n/// KleidiAI shared constants\nenum {\n /// Largest supported SME vector length in bytes\n KAI_SME_VEC_LENGTH_MAX_BYTES = 256,\n\n /// Size of one vscale unit, in bytes\n KAI_VSCALE_UNIT_BYTES = 16,\n\n /// Maximum possible VSCALE\n KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES,\n};\n\n/// Gets the version of the project in the Major.Minor.Patch semantic versioning format.\n///\n/// @return Project version as a string literal.\ninline const char* kai_get_version(void) {\n return \"1.31.0\";\n}\n\n/// KleidiAI data types\n/// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type)\nenum kai_datatype {\n kai_dt_unknown = 0x0000,\n kai_dt_f32 = 0x0411,\n kai_dt_f16 = 0x0212,\n kai_dt_bf16 = 0x0213,\n kai_dt_int32 = 0x0421,\n kai_dt_int16 = 0x0222,\n kai_dt_int8 = 0x0124,\n kai_dt_uint32 = 0x0431,\n kai_dt_uint16 = 0x0232,\n kai_dt_uint8 = 0x0134,\n kai_dt_bool = 0x0441\n};\n\n/// Gets number of bytes for a given data type\n/// @param[in] dt KleidiAI data type\n///\n/// @return the numbers of bytes for the data type\ninline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) {\n return (size_t)(dt >> 8);\n}\n\n/// Converts a scalar f16 value to f32\n/// @param[in] f16 The f16 value\n///\n/// @return the f32 value\n#if defined(__ARM_NEON)\ninline static float kai_cast_f32_f16(uint16_t f16) {\n float16_t f32 = 0;\n memcpy(&f32, &f16, sizeof(uint16_t));\n return (float)f32;\n}\n#endif\n\n/// Converts a scalar bf16 value to f32\n/// @param[in] bf16 The f16 value\n///\n/// @return the f32 value\ninline static float kai_cast_f32_bf16(uint16_t bf16) {\n const uint32_t i32 = (bf16 << 16);\n float f32 = 0;\n memcpy(&f32, &i32, sizeof(i32));\n return f32;\n}\n\n/// Converts a f32 value to bf16\n/// @param[in] f32 The f32 value\n///\n/// @return the bf16 value\ninline static uint16_t kai_cast_bf16_f32(float f32) {\n uint16_t bf16 = 0;\n#ifdef __ARM_FEATURE_BF16\n __asm__ __volatile__(\"bfcvt %h[output], %s[input]\" : [output] \"=w\"(bf16) : [input] \"w\"(f32));\n#else\n const uint32_t* i32 = (uint32_t*)(&f32);\n bf16 = (*i32 >> 16);\n#endif\n return bf16;\n}\n\n/// Converts a scalar f32 value to f16\n/// @param[in] f32 The f32 value\n///\n/// @return the f16 value\n#if defined(__ARM_NEON)\ninline static uint16_t kai_cast_f16_f32(float f32) {\n uint16_t f16 = 0;\n float16_t tmp = (float16_t)f32;\n memcpy(&f16, &tmp, sizeof(uint16_t));\n return f16;\n}\n#endif\n\n/// Divide a value, `a`, with a value `b` and round up the result\ninline static size_t kai_div_ceil(size_t a, size_t b) {\n return (a + b - 1) / b;\n}\n\n/// Round up a value, `a` to a multiple of `b`\ninline static size_t kai_roundup(size_t a, size_t b) {\n return kai_div_ceil(a, b) * b;\n}\n\n#if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n/// Gets the SME vector length for 8-bit elements.\nuint64_t kai_get_sme_vector_length_u8(void);\n\n/// Gets the SME vector length for 16-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u16(void) {\n return kai_get_sme_vector_length_u8() / 2;\n}\n\n/// Gets the SME vector length for 32-bit elements.\ninline static uint64_t kai_get_sme_vector_length_u32(void) {\n return kai_get_sme_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SME\ninline static uint64_t kai_get_sme_vscale(void) {\n return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Commit ZA to lazy save buffer\nvoid kai_commit_za(void);\n#endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64)\n\n/// Gets the SVE vector length for 8-bit elements.\nuint64_t kai_get_sve_vector_length_u8(void);\n\n/// Gets the SVE vector length for 16-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u16(void) {\n return kai_get_sve_vector_length_u8() / 2;\n}\n\n/// Gets the SVE vector length for 32-bit elements.\ninline static uint64_t kai_get_sve_vector_length_u32(void) {\n return kai_get_sve_vector_length_u8() / 4;\n}\n\n/// Gets the vscale scale factor for SVE\ninline static uint64_t kai_get_sve_vscale(void) {\n return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES;\n}\n\n/// Extends the sign bit of int 4-bit value (stored in int8_t variable)\n/// @param[in] value The 4-bit int value\n///\n/// @return the int8_t value with sign extended\ninline static int8_t kai_ext_sign_i8_i4(int8_t value) {\n // Make sure value holds correct int4 value\n KAI_ASSUME(value <= 0xF);\n\n return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n}\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization)\nstruct kai_rhs_pack_qsi8cx_params {\n int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales.\n};\n\n/// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0\n/// nibble ordering)\nstruct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0)\nstruct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params {\n int8_t lhs_zero_point;\n uint8_t rhs_zero_point;\n enum kai_datatype scale_dt;\n};\n\n/// Parameter struct for RHS matrix packing\nstruct kai_rhs_pack_qs4cxs1s0_param {\n int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point\n uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point\n};\n\n/// Requantization and clamp parameters for GEMM/GEMV output stage.\nstruct kai_matmul_requantize32_params {\n int32_t min_value; ///< Minimum output value.\n int32_t max_value; ///< Maximum output value.\n int32_t output_zero_point; ///< Output quantization zero point.\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kai/kai_common_sme_asm.S",
|
| 33 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_FUNCTION_USE(name) name\n\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #define KAI_ASM_FUNCTION_USE(name) _##name\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #define KAI_ASM_FUNCTION_USE(name) name\n #endif\n\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n\n KAI_ASM_CODE(kai_common)\n KAI_ASM_ALIGN\n\n KAI_ASM_GLOBAL(kai_get_sme_vector_length_u8)\n KAI_ASM_GLOBAL(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_commit_za)\nKAI_ASM_FUNCTION_LABEL(kai_commit_za)\n#if defined(__ARM_FEATURE_SME)\n stp fp, lr, [sp, #-16]!\n mov fp, sp\n mrs x9, TPIDR2_EL0\n cbz x9, leave\n bl KAI_ASM_FUNCTION_USE(__arm_tpidr2_save)\n msr TPIDR2_EL0, xzr\nleave:\n ldp fp, lr, [sp], #16\n#endif\n ret\n KAI_ASM_FUNCTION_END(kai_commit_za)\n\nKAI_ASM_FUNCTION_TYPE(kai_get_sme_vector_length_u8)\nKAI_ASM_FUNCTION_LABEL(kai_get_sme_vector_length_u8)\n KAI_ASM_INST(0x04bf5820) // rdsvl x0, #1\n ret\n KAI_ASM_FUNCTION_END(kai_get_sme_vector_length_u8)\n\n KAI_ASM_END\n"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa.h",
|
| 37 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif // __cplusplus\n\n/// Micro-kernel dependencies\n///\n/// -# kai_lhs_quant_pack_qai8dxp_f32 to dynamically quantize and pack the LHS matrix\n/// -# kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon to pack the RHS matrix\n\n/// --------------------------------------------------\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @return the m step value\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the n step value.\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @return the n step\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the mr value, which must be used to pack the LHS matrix with\n/// the @ref kai_lhs_quant_pack_qai8dxp_f32 micro-kernel\n///\n/// @return the mr value\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the nr value, which must be used to pack the RHS matrix with\n/// the @ref kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon micro-kernel\n///\n/// @return the nr value\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the kr value, which must be used to pack the RHS matrix with\n/// the @ref kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon micro-kernel\n///\n/// @return the kr value\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the sr value, which must be used to pack the RHS matrix with\n/// the @ref kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon micro-kernel\n///\n/// @return the sr value\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qai8dxp) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(size_t m_idx, size_t k);\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed 2-bit quantized symmetric per-channel (qsu2cxp) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K).\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(size_t n_idx, size_t k);\n\n/// Gets the offset in bytes for the DST matrix\n///\n/// @param[in] m_idx Row index in the DST matrix.\n/// @param[in] n_idx Column index in the DST matrix.\n/// @param[in] dst_stride The number of bytes in in each row of the DST matrix\n///\n/// @return the destination(DST) offset in bytes\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride);\n\n/// Gets the size in bytes for the destination (DST) matrix.\n///\n/// @param[in] m Number of rows in the destination (DST) matrix.\n/// @param[in] n Number of columns in the destination (DST) matrix.\n///\n/// @return the destination (DST) matrix size in bytes\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n);\n\n/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation.\n///\n/// LHS matrix: Signed 8-bit quantized asymmetric per-row (qai8dxp) and packed\n/// RHS matrix: Signed 2-bit quantized symmetric per-channel (qsu2cxp) and packed.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] n The number of output columns written.\n/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix. It must be a\n/// a multiple of 32.\n/// @param[in] lhs_packed The LHS packed matrix.\n/// When the activation are dynamically quantized, you can obtain this matrix\n/// by calling the @ref kai_lhs_quant_pack_qai8dxp_f32 micro-kernel which performs\n/// both the dynamic quantization to 8-bit and activation packing in a single step.\n/// @param[in] rhs_packed The RHS packed matrix, which is obtained by calling @ref\n/// kai_run_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon\n/// @param[out] dst The DST matrix.\n/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix.\n/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float).\n/// @param[in] scalar_min Min value used to clamp the final result.\n/// @param[in] scalar_max Max value used to clamp the final result.\n/// @param[in] lut Look up table for mapping 2-bit to 8-bit values.\nvoid kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m, size_t n, size_t k, const void* lhs_packed, const void* rhs_packed, float* dst, size_t dst_stride_row,\n size_t dst_stride_col, float scalar_min, float scalar_max, const int32_t* lut);\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif // __cplusplus\n"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa.c",
|
| 41 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n\n#if (!defined(__aarch64__) || !defined(__ARM_FEATURE_SVE2)) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64, FEAT_SVE2.\n#else // Architectural features check.\n\n#include \"kai_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa.h\"\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#define KAI_LUT_NENTRIES 4\n\n/// Look-up table used for int2 -> int8 conversion\nstatic const int32_t lut_i8_i2[KAI_LUT_NENTRIES] = {-2, -1, 0, 1};\n\ntypedef struct {\n float* dst; // 0 (0x00)\n const void* lhs_packed; // 8 (0x08)\n const void* rhs_packed; // 16 (0x10)\n size_t dst_stride_row; // 24 (0x18)\n size_t lhs_stride; // 32 (0x20)\n size_t rhs_stride; // 40 (0x28)\n size_t m; // 48 (0x30)\n size_t n; // 56 (0x38)\n size_t k; // 64 (0x40)\n const int32_t* lut; // 72 (0x48)\n float scalar_max; // 80 (0x50)\n float scalar_min; // 84 (0x54)\n} KernelArgs;\n\nextern void kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(KernelArgs* args_ptr);\n\n// Compute args\nstatic const size_t kai_m_step = 1; // multiple of vector length\nstatic const size_t kai_n_step = 4; // multiple of vector length\n// Packing args\nstatic const size_t kai_mr = 1; // multiple of vector length\nstatic const size_t kai_nr = 4; // multiple of vector length\nstatic const size_t kai_kr = 4;\nstatic const size_t kai_sr = 1;\n// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_qvalue_lhs = sizeof(int8_t);\nstatic const size_t kai_num_bytes_multiplier_lhs = sizeof(float);\nstatic const size_t kai_num_bytes_offset_lhs = sizeof(int32_t);\n// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric))\nstatic const size_t kai_num_bytes_recip_qvalue_rhs = 4; // 4 2-bit quantized int values in a byte\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_bias_rhs = sizeof(float);\n// DST format args\nstatic const size_t kai_num_bytes_dst_value = sizeof(float);\n// Extra args\nstatic const size_t kai_k_multiple_of = 32;\n\nstatic size_t kai_k_roundedup(const size_t k) {\n // Round up k to be a multiple of 32.\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nstatic size_t kai_get_lhs_packed_stride(const size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n KAI_ASSUME((k_internal % kai_k_multiple_of) == 0);\n\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa();\n return mr * (k_internal * kai_num_bytes_qvalue_lhs + kai_num_bytes_multiplier_lhs + kai_num_bytes_offset_lhs);\n}\n\nstatic size_t kai_get_rhs_packed_stride(const size_t k) {\n const size_t k_internal = kai_k_roundedup(k);\n KAI_ASSUME((k_internal % kai_k_multiple_of) == 0);\n\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa();\n\n size_t rhs_packed_stride = nr *\n ((k_internal / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs +\n kai_num_bytes_bias_rhs);\n\n return rhs_packed_stride;\n}\n\nsize_t kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_m_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_n_step * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_mr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_nr * kai_get_sme_vector_length_u32();\n}\n\nsize_t kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_kr;\n}\n\nsize_t kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(void) {\n return kai_sr;\n}\n\nsize_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(size_t m_idx, size_t k) {\n KAI_ASSUME((m_idx % kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa();\n\n return (m_idx / mr) * kai_get_lhs_packed_stride(k);\n}\n\nsize_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(size_t n_idx, size_t k) {\n KAI_ASSUME((n_idx % kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa();\n\n return (n_idx / nr) * kai_get_rhs_packed_stride(k);\n}\n\nsize_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m_idx, size_t n_idx, size_t dst_stride) {\n KAI_ASSUME((m_idx % kai_get_m_step_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n KAI_ASSUME((n_idx % kai_get_n_step_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa()) == 0);\n\n return ((n_idx * kai_num_bytes_dst_value) + (m_idx * dst_stride));\n}\n\nsize_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(size_t m, size_t n) {\n return m * n * kai_num_bytes_dst_value;\n}\n\nvoid kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(\n size_t m, size_t n, size_t k, const void* restrict lhs_packed, const void* restrict rhs_packed,\n float* restrict dst, // NOLINT(readability-non-const-parameter)\n size_t dst_stride_row, size_t dst_stride_col, float scalar_min, float scalar_max, const int32_t* lut) {\n KAI_ASSUME(dst_stride_col == sizeof(float));\n KAI_ASSUME(m > 0);\n KAI_ASSUME(n > 0);\n KAI_ASSUME(k > 0);\n KAI_ASSUME(k % kai_k_multiple_of == 0);\n\n KernelArgs args;\n args.dst = dst;\n args.lhs_packed = lhs_packed;\n args.rhs_packed = rhs_packed;\n args.dst_stride_row = dst_stride_row;\n args.m = m;\n args.n = n;\n args.k = kai_k_roundedup(k);\n args.lhs_stride = kai_get_lhs_packed_stride(k);\n args.rhs_stride = kai_get_rhs_packed_stride(k);\n args.scalar_max = scalar_max;\n args.scalar_min = scalar_min;\n args.lut = lut != NULL ? lut : lut_i8_i2;\n\n kai_commit_za();\n\n kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa(&args);\n}\n\n#endif // Architectural feature check\n"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"path": "kai_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa_asm.S",
|
| 45 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if defined(_MSC_VER)\n #define KAI_ASM_GLOBAL(name) GLOBAL name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) name PROC\n #define KAI_ASM_FUNCTION_END(name) ENDP\n #define KAI_ASM_CODE(name) AREA name, CODE, READONLY\n #define KAI_ASM_ALIGN\n #define KAI_ASM_LABEL(name) name\n #define KAI_ASM_INST(hex) DCD hex\n #define KAI_ASM_END END\n#else\n #if defined(__APPLE__)\n #define KAI_ASM_GLOBAL(name) .globl _##name\n #define KAI_ASM_FUNCTION_TYPE(name)\n #define KAI_ASM_FUNCTION_LABEL(name) _##name:\n #define KAI_ASM_FUNCTION_END(name)\n #else\n #define KAI_ASM_GLOBAL(name) .global name\n #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function\n #define KAI_ASM_FUNCTION_LABEL(name) name:\n #define KAI_ASM_FUNCTION_END(name) .size name, .-name\n #endif\n #define KAI_ASM_CODE(name) .text\n #define KAI_ASM_ALIGN .p2align 4,,11\n #define KAI_ASM_LABEL(name) name:\n #define KAI_ASM_INST(hex) .inst hex\n #define KAI_ASM_END\n#endif\n KAI_ASM_CODE(matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa)\n KAI_ASM_ALIGN\n KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa)\nKAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa)\n stp x19, x20, [sp, -112]!\n stp x21, x22, [sp, 16]\n stp x23, x24, [sp, 32]\n stp d8, d9, [sp, 48]\n stp d10, d11, [sp, 64]\n stp d12, d13, [sp, 80]\n stp d14, d15, [sp, 96]\n KAI_ASM_INST(0xd503477f) // smstart\n ldr x19, [x0, #0x0] // dst\n ldr x20, [x0, #0x8] // lhs\n ldr x7, [x0, #0x48] // lut\n KAI_ASM_INST(0xe11f80e0) // ldr zt0, [x7]\n cntw x7 // mr\n ldr x5, [x0, 0x40] // k\n mul x5, x5, x7\n cntw x6, ALL, MUL #4 // nr\n ptrue p2.b\n KAI_ASM_INST(0x8555c81e) // ld1rw { z30.s }, p2/Z, [x0, 0x54] // min\n KAI_ASM_INST(0x8554c81f) // ld1rw { z31.s }, p2/Z, [x0, 0x50] // max\n ldr x23, [x0, 0x18] // dst_stride_row\n ldr x12, [x0, 0x30] // m\n KAI_ASM_INST(0x25ac17e0) // whilelt p0.s, xzr, x12\n b.eq M_Loop_End // b.none M_Loop_End\nKAI_ASM_LABEL(M_Loop_Start) // M Loop\n ldr x8, [x0, 0x10] // rhs\n mov x9, x19\n ldr x13, [x0, 0x38] // n\n cmp x7, x12\n csel x21, x7, x12, lt\n lsl x21, x21, #2\n whilelt p3.b, xzr, x13\nKAI_ASM_LABEL(N_Loop) // N Loop\n mov x10, x20\n mov x11, x8\n mov x22, x9\n KAI_ASM_INST(0x25ad67f1) // whilelt pn9.s, xzr, x13, vlx4\n KAI_ASM_INST(0xc00800ff) // zero {za}\n add x14, x10,x5\nKAI_ASM_LABEL(K_Loop) // K Loop\n KAI_ASM_INST(0xa540a144) // ld1w { z4.s }, p0/z, [x10]\n addvl x10, x10, #1\n ld1b { z0.b }, p3/z, [x11]\n addvl x11, x11, #1\n KAI_ASM_INST(0xc08c8008) // luti2 { z8.b - z11.b }, zt0, z0[0]\n KAI_ASM_INST(0xa0884880) // smopa za0.s, p2/m, p2/m, z4.b, z8.b\n KAI_ASM_INST(0xa0894881) // smopa za1.s, p2/m, p2/m, z4.b, z9.b\n KAI_ASM_INST(0xa08a4882) // smopa za2.s, p2/m, p2/m, z4.b, z10.b\n KAI_ASM_INST(0xa08b4883) // smopa za3.s, p2/m, p2/m, z4.b, z11.b\n cmp x10, x14\n b.lt K_Loop\n // Load RHS row sum, scale factors & bias\n KAI_ASM_INST(0xa040c560) // ld1w { z0.s - z3.s }, pn9/z, [x11]\n KAI_ASM_INST(0xa041c564) // ld1w { z4.s - z7.s }, pn9/z, [x11, #4, mul vl]\n KAI_ASM_INST(0xa042c568) // ld1w { z8.s - z11.s }, pn9/z, [x11, #8, mul vl]\n KAI_ASM_INST(0x042b518b) // addvl x11, x11, #12\n mov x14, #0\n addvl x15, x10, #1\nKAI_ASM_LABEL(ZA_Loop) // Store Loop\n // Load LHS zero-points & scale factors\n KAI_ASM_INST(0x8540c950) // ld1rw { z16.s }, p2/z, [x10]\n KAI_ASM_INST(0x8540c9f1) // ld1rw { z17.s }, p2/z, [x15]\n // Load inner accumulated value\n KAI_ASM_INST(0xc006440c) // mova { z12.b - z15.b }, za0h.b[w14,3]\n add x10, x10, #4\n add x15, x15, #4\n // LHS scaling factor * RHS scaling factor\n fmul z20.s, z17.s, z4.s\n fmul z21.s, z17.s, z5.s\n fmul z22.s, z17.s, z6.s\n fmul z23.s, z17.s, z7.s\n // Zero-points x Row-sum + iacc\n mla\t z12.s, p2/m, z16.s, z0.s\n mla\t z13.s, p2/m, z16.s, z1.s\n mla\t z14.s, p2/m, z16.s, z2.s\n mla\t z15.s, p2/m, z16.s, z3.s\n KAI_ASM_INST(0xc132e18c) // scvtf { z12.s - z15.s }, { z12.s - z15.s }\n // Apply combined scale factors\n fmul z24.s, z12.s, z20.s\n fmul z25.s, z13.s, z21.s\n fmul z26.s, z14.s, z22.s\n fmul z27.s, z15.s, z23.s\n // Add the bias\n fadd z24.s, p2/m, z24.s, z8.s\n fadd z25.s, p2/m, z25.s, z9.s\n fadd z26.s, p2/m, z26.s, z10.s\n fadd z27.s, p2/m, z27.s, z11.s\n // Clamp and store\n KAI_ASM_INST(0xc1bfcbd8) // fclamp { z24.s - z27.s }, z30.s, z31.s\n KAI_ASM_INST(0xa060c6d8) // st1w { z24.s - z27.s }, pn9, [x22]\n add x22, x22, x23\n add x14, x14, #4\n cmp x14, x21\n b.lt ZA_Loop\n ldr x24, [x0, 0x28] // rhs_stride\n add x8, x8,x24\n KAI_ASM_INST(0x04295089) // addvl x9, x9, #4\n sub x13, x13, x6\n whilelt p3.b, xzr, x13\n b.mi N_Loop\n ldr x24, [x0, 0x20] // lhs_stride\n add x20, x20, x24\n ldr x24, [x0, 0x18] // dst_stride_row\n mul x24, x7, x24\n add x19, x19,x24\n sub x12, x12, x7\n whilelt p0.s, xzr, x12\n b.mi M_Loop_Start\nKAI_ASM_LABEL(M_Loop_End)\n KAI_ASM_INST(0xd503467f) // smstop\n ldp d14, d15, [sp, 96]\n ldp d12, d13, [sp, 80]\n ldp d10, d11, [sp, 64]\n ldp d8, d9, [sp, 48]\n ldp x23, x24, [sp, 32]\n ldp x21, x22, [sp, 16]\n ldp x19, x20, [sp], 112\n ret\n KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa)\n\n KAI_ASM_END\n"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.h",
|
| 49 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/// Gets the m step value.\n/// The micro-kernel can process any M values. However, the starting M index to\n/// be processed must be a multiple of m step.\n///\n/// @param[in] mr The number of M rows to interleave on the same output row.\n///\n/// @return the m step value\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr);\n\n/// Gets the offset in bytes for the LHS matrix (not packed)\n///\n/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed)\n///\n/// @return the offset in bytes to the LHS matrix\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride);\n\n/// Gets the offset in bytes for the packed LHS matrix,\n/// which contains the packed 8-bit quantized asymmetric per-row (qa8dx) values.\n///\n/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel.\n///\n/// @param[in] m_idx Row index in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed LHS matrix\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Gets the size in bytes for the quantized and packed LHS matrix\n///\n/// @param[in] m Total number of rows in the LHS matrix (not packed).\n/// @param[in] k Total number of columns in the LHS matrix (not packed).\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed LHS matrix size in bytes\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to quantize and pack the LHS matrix.\n///\n/// @param[in] m The number of output rows written.\n/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8.\n/// @param[in] mr The number of M rows to interleave on the same output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// However, kr must be multiple of sr.\n/// @param[in] m_idx_start The starting M index.\n/// @param[in] lhs LHS of the vector-by-matrix.\n/// @param[in] lhs_stride Stride in bytes between two rows of LHS.\n/// @param[out] lhs_packed The quantized and packed LHS matrix.\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs, size_t lhs_stride,\n void* lhs_packed);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "kai_lhs_quant_pack_qai8dxp_f32.c",
|
| 53 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n\n#if defined(__aarch64__)\n#include <arm_neon.h>\n#endif\n#include <float.h>\n#include <math.h>\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\nstatic const size_t kai_num_bytes_per_multiplier = sizeof(float);\nstatic const size_t kai_num_bytes_per_offset = sizeof(int32_t);\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n size_t kai_k_multiple_of = 32;\n return kai_roundup(k, kai_k_multiple_of);\n}\n\ninline static size_t kai_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % 2) == 0);\n\n return mr * (k_internal * sizeof(int8_t) + kai_num_bytes_per_multiplier + kai_num_bytes_per_offset);\n}\n\nsize_t kai_get_m_step_lhs_quant_pack_qai8dxp_f32(size_t mr) {\n return mr;\n}\n\nsize_t kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t lhs_stride) {\n return m_idx * lhs_stride;\n}\n\nsize_t kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) {\n // It always points to the beginning of the row\n return (m_idx / mr) * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nsize_t kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(size_t m, size_t k, size_t mr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(m, mr) / mr;\n\n return num_rows * kai_lhs_packed_stride(k, mr, kr, sr);\n}\n\nvoid kai_run_lhs_quant_pack_qai8dxp_f32(\n size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* restrict lhs,\n size_t lhs_stride, void* restrict lhs_packed) {\n KAI_ASSERT((kr % sr) == 0);\n\n if (m == 0) {\n return;\n }\n\n const size_t num_rows = m;\n\n const float* src_ptr = lhs;\n\n const size_t dst_stride = kai_lhs_packed_stride(k, mr, kr, sr);\n const size_t k_internal = kai_k_roundedup(k);\n const int32_t k_block_len = (int32_t)(kr / sr);\n\n const int32_t num_blocks_k = (int32_t)(k / k_block_len);\n const int32_t num_blocks_k_internal = (int32_t)(k_internal / k_block_len);\n\n for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {\n float max0 = -FLT_MAX;\n float min0 = FLT_MAX;\n\n // Find min/max for each channel\n int32_t k_idx = 0;\n\n#if defined(__aarch64__)\n float32x4_t vmax0 = vdupq_n_f32(-FLT_MAX);\n float32x4_t vmin0 = vdupq_n_f32(FLT_MAX);\n\n for (; k_idx <= ((int32_t)k - 8); k_idx += 8) {\n const float32x4_t src0_0 = vld1q_f32(src_ptr + 0 + (size_t)k_idx);\n const float32x4_t src0_1 = vld1q_f32(src_ptr + 4 + (size_t)k_idx);\n\n // Calculate the max\n vmax0 = vmaxq_f32(src0_0, vmax0);\n vmax0 = vmaxq_f32(vmax0, src0_1);\n\n // Calculate the min\n vmin0 = vminq_f32(src0_0, vmin0);\n vmin0 = vminq_f32(vmin0, src0_1);\n }\n // Get the max/min\n max0 = vmaxvq_f32(vmax0);\n min0 = vminvq_f32(vmin0);\n#endif\n for (; k_idx < (int32_t)k; ++k_idx) {\n const float src0_0 = *(src_ptr + (size_t)k_idx);\n max0 = fmaxf(src0_0, max0);\n min0 = fminf(src0_0, min0);\n }\n\n // Maximum/minimum int8 values\n const float qmin = (float)INT8_MIN;\n const float qmax = (float)INT8_MAX;\n\n const float rmin0 = fminf(0.0F, min0);\n const float rmax0 = fmaxf(0.0F, max0);\n const float scale0 = rmin0 == rmax0 ? 1.F : (qmax - qmin) / (rmax0 - rmin0);\n\n // Reciprocal to quantize\n const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F;\n\n const float descaled_min0 = rmin0 * scale0;\n const float descaled_max0 = rmax0 * scale0;\n\n const float zero_point_from_min_error0 = qmin + descaled_min0;\n const float zero_point_from_max_error0 = qmax + descaled_max0;\n\n float zero_point0 =\n zero_point_from_min_error0 + zero_point_from_max_error0 > 0 ? qmin - descaled_min0 : qmax - descaled_max0;\n\n zero_point0 = fmaxf(zero_point0, qmin);\n zero_point0 = fminf(zero_point0, qmax);\n\n // Round to nearest integer\n const int32_t nudged_zero_point0 = (int32_t)rintf(zero_point0);\n\n const size_t dst_x = ((row_idx + m_idx_start) % mr);\n\n uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len * sizeof(int8_t));\n\n // Quantize the channels\n int32_t block_idx = 0;\n\n#if defined(__aarch64__)\n if (k_block_len == 8) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n const float32x4_t src_1 = vld1q_f32(src_ptr + k_idx_start + 4);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n float32x4_t v1_f32 = vmulq_n_f32(src_1, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int32x4_t v1_s32 = vcvtaq_s32_f32(v1_f32);\n\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n int16x4_t v1_s16 = vqmovn_s32(v1_s32);\n int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x8_t vnzp_s16 = vdupq_n_s16(nzp_s16);\n v_s16 = vaddq_s16(v_s16, vnzp_s16);\n v_s16 = vmaxq_s16(v_s16, vdupq_n_s16(INT8_MIN));\n v_s16 = vminq_s16(v_s16, vdupq_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(v_s16);\n vst1_s8((int8_t*)(dst_ptr), v0_s8);\n dst_ptr += 8 * sizeof(int8_t);\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n } else if (k_block_len == 4) {\n for (; block_idx < num_blocks_k; ++block_idx) {\n // Clamp at the last valid k-index\n const int32_t k_idx_start = block_idx * k_block_len;\n\n const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start);\n\n // Scale the values\n float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0);\n int32x4_t v0_s32 = vcvtaq_s32_f32(v0_f32);\n int16x4_t v0_s16 = vqmovn_s32(v0_s32);\n\n // Add zero points\n int16_t nzp_s16 = (int16_t)nudged_zero_point0;\n int16x4_t vnzp_s16 = vdup_n_s16(nzp_s16);\n int16x4_t v_s16 = vadd_s16(v0_s16, vnzp_s16);\n v_s16 = vmax_s16(v_s16, vdup_n_s16(INT8_MIN));\n v_s16 = vmin_s16(v_s16, vdup_n_s16(INT8_MAX));\n\n int8x8_t v0_s8 = vqmovn_s16(vcombine_s16(v_s16, vdup_n_s16(0)));\n\n *((int8_t*)(dst_ptr)) = vget_lane_s8(v0_s8, 0);\n *((int8_t*)(dst_ptr + 1)) = vget_lane_s8(v0_s8, 1);\n *((int8_t*)(dst_ptr + 2)) = vget_lane_s8(v0_s8, 2);\n *((int8_t*)(dst_ptr + 3)) = vget_lane_s8(v0_s8, 3);\n\n dst_ptr += mr * k_block_len * sizeof(int8_t);\n }\n } else\n#endif\n {\n for (; block_idx < num_blocks_k; ++block_idx) {\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n const int32_t k_idx_start = (block_idx * k_block_len) + k_block_idx;\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n }\n\n for (; block_idx < num_blocks_k_internal; ++block_idx) {\n // left over k\n for (int32_t k_block_idx = 0; k_block_idx < k_block_len; ++k_block_idx) {\n // Clamp at the last valid k-index\n const size_t k_idx_start = KAI_MIN((size_t)((block_idx * k_block_len) + k_block_idx), k - 1);\n\n const float src0_0 = *(src_ptr + k_idx_start);\n\n // Scale the values\n int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0));\n\n v0_s32 = v0_s32 + nudged_zero_point0;\n v0_s32 = KAI_MAX(v0_s32, INT8_MIN);\n v0_s32 = KAI_MIN(v0_s32, INT8_MAX);\n\n *((int8_t*)(dst_ptr)) = (int8_t)v0_s32;\n dst_ptr += sizeof(int8_t);\n }\n dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t);\n }\n\n dst_ptr = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t));\n\n dst_ptr += dst_x * kai_num_bytes_per_offset;\n\n // LHS offset at the beginning of the row\n *((int32_t*)(dst_ptr)) = -nudged_zero_point0;\n\n // Assuming the same sizeof() for kai_num_bytes_per_offset and kai_num_bytes_per_multiplier\n KAI_ASSERT(kai_num_bytes_per_offset == kai_num_bytes_per_multiplier);\n\n dst_ptr += mr * kai_num_bytes_per_offset;\n\n // Store the scale quantization params\n *((float*)(dst_ptr)) = recip_scale0;\n\n src_ptr += (lhs_stride / sizeof(float));\n\n // Move to the next row if we have interleaved all Mr rows\n if ((((row_idx + 1) + m_idx_start) % mr) == 0) {\n lhs_packed = (void*)((uint8_t*)lhs_packed + dst_stride);\n }\n }\n}\n"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"path": "kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon.c",
|
| 57 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#if !defined(__aarch64__) && !defined(_M_ARM64)\n#error This file must be compiled for AArch64.\n#else // Architectural features check.\n\n#include \"kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon.h\"\n\n#include <arm_neon.h>\n#include <stdint.h>\n#include <string.h>\n\n#include \"kai/kai_common.h\"\n\n#define KAI_LUT_NENTRIES 4\n\nstatic const size_t kai_num_bytes_sum_rhs = sizeof(int32_t);\nstatic const size_t kai_num_bytes_multiplier_rhs = sizeof(float);\nstatic const size_t kai_num_bytes_bias = sizeof(float);\n\n/// Look-up table used for int2 -> int8 conversion\nstatic const int32_t lut_i8_i2[KAI_LUT_NENTRIES] = {-2, -1, 0, 1};\nstatic const size_t kai_k_multiple_of = 32;\n\ninline static size_t kai_k_roundedup(size_t k) {\n // Round up k to be a multiple of 32.\n return kai_roundup(k, kai_k_multiple_of);\n}\n\nsize_t kai_get_n_step_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(size_t nr) {\n return nr;\n}\n\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(size_t n_idx, size_t rhs_stride) {\n return n_idx * rhs_stride;\n}\n\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_UNUSED(kr);\n KAI_UNUSED(sr);\n\n const size_t k_internal = kai_k_roundedup(k);\n\n // multiple of 4 because 4 2-bit quantized int elements in a byte\n KAI_ASSERT((k_internal % 4) == 0);\n\n return nr * ((k_internal / 4) + kai_num_bytes_multiplier_rhs + kai_num_bytes_sum_rhs + kai_num_bytes_bias);\n}\n\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr) {\n KAI_ASSERT((n_idx % nr) == 0);\n\n return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(k, nr, kr, sr);\n}\n\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t sr) {\n const size_t num_rows = kai_roundup(n, nr) / nr;\n\n return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(k, nr, kr, sr);\n}\n\nvoid kai_run_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, const uint8_t* rhs, const float* bias,\n const float* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon_params* params, const int32_t* lut_arg) {\n const size_t k_internal = kai_k_roundedup(k);\n\n KAI_ASSERT((k_internal % kr) == 0);\n KAI_ASSERT(num_groups == 1);\n KAI_ASSERT(extra_bytes == 0);\n KAI_ASSERT((kr % sr) == 0);\n KAI_ASSERT(rhs != NULL);\n KAI_ASSERT(scale != NULL);\n KAI_ASSERT(rhs_packed != NULL);\n KAI_ASSERT(params != NULL);\n KAI_ASSERT(params->lhs_zero_point == 1);\n KAI_ASSERT(params->rhs_zero_point == 2);\n KAI_ASSUME(k % kai_k_multiple_of == 0);\n KAI_ASSUME((kr / sr) == 4);\n\n // Note: The input matrix (rhs) is expected with:\n // \"k\" columns and \"n\" rows (NxK)\n\n const size_t rhs_stride = kai_roundup(k, 4) / 4;\n const size_t rhs_packed_stride = kai_get_rhs_packed_stride_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(k, nr, kr, sr);\n const int32_t* lut = lut_arg != NULL ? lut_arg : lut_i8_i2;\n const size_t num_bytes_processed = 8;\n\n // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n const int8_t lut_s8[8] = {lut[0], lut[1], lut[2], lut[3], 0, 0, 0, 0};\n // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)\n\n const int8x8_t vlut_s8 = vld1_s8(lut_s8);\n const uint8x8_t mask_2b = vdup_n_u8(0x03);\n // Iterate over n src rows in blocks of nr rows\n for (size_t row_idx = 0; row_idx < n; row_idx += nr) {\n int8_t* dst_row = (int8_t*)rhs_packed + ((row_idx / nr) * rhs_packed_stride);\n\n int32_t* const sums = (int32_t*)(dst_row + (nr * (k_internal / 4)));\n float* const scaling_factors = (float*)((uint8_t*)sums + (nr * kai_num_bytes_sum_rhs));\n float* const biases = (float*)((uint8_t*)scaling_factors + (nr * kai_num_bytes_multiplier_rhs));\n\n // initialize sums to 0\n memset(sums, 0, nr * kai_num_bytes_sum_rhs);\n\n // Copy the scaling factors and bias\n size_t rows_left = n - row_idx;\n // Saving scales.\n if (rows_left >= nr) {\n memcpy(scaling_factors, &scale[row_idx], nr * kai_num_bytes_multiplier_rhs);\n } else {\n // Fill remaining values\n memcpy(scaling_factors, &scale[row_idx], rows_left * kai_num_bytes_multiplier_rhs);\n // Set leftover to 0\n memset(&scaling_factors[rows_left], 0, (nr - rows_left) * kai_num_bytes_multiplier_rhs);\n }\n if (bias == NULL) {\n // Set bias to 0\n memset(biases, 0, nr * kai_num_bytes_bias);\n } else {\n if (rows_left >= nr) {\n memcpy(biases, &bias[row_idx], nr * kai_num_bytes_bias);\n } else {\n // Fill remaining values\n memcpy(biases, &bias[row_idx], rows_left * kai_num_bytes_bias);\n // Set leftover to 0\n memset(&biases[rows_left], 0, (nr - rows_left) * kai_num_bytes_bias);\n }\n }\n // Iterate over rows in the nr row block\n for (size_t k_byte_idx = 0; k_byte_idx < rhs_stride; k_byte_idx += num_bytes_processed) {\n for (size_t nr_block_idx = 0; nr_block_idx < nr; nr_block_idx += num_bytes_processed) {\n // Clamp the indices to avoid out-of-bound reads\n const size_t n0_idx = KAI_MIN(row_idx + nr_block_idx, n - 1);\n const size_t n1_idx = KAI_MIN(n0_idx + 1, n - 1);\n const size_t n2_idx = KAI_MIN(n0_idx + 2, n - 1);\n const size_t n3_idx = KAI_MIN(n0_idx + 3, n - 1);\n const size_t n4_idx = KAI_MIN(n0_idx + 4, n - 1);\n const size_t n5_idx = KAI_MIN(n0_idx + 5, n - 1);\n const size_t n6_idx = KAI_MIN(n0_idx + 6, n - 1);\n const size_t n7_idx = KAI_MIN(n0_idx + 7, n - 1);\n\n // Initialize partial sum\n int32_t partial_sum0 = 0;\n int32_t partial_sum1 = 0;\n int32_t partial_sum2 = 0;\n int32_t partial_sum3 = 0;\n int32_t partial_sum4 = 0;\n int32_t partial_sum5 = 0;\n int32_t partial_sum6 = 0;\n int32_t partial_sum7 = 0;\n\n const uint8_t* src_base = rhs + k_byte_idx;\n const uint8x8_t vsrc0_0 = vld1_u8(src_base + n0_idx * rhs_stride);\n const uint8x8_t vsrc1_0 = vld1_u8(src_base + n1_idx * rhs_stride);\n const uint8x8_t vsrc2_0 = vld1_u8(src_base + n2_idx * rhs_stride);\n const uint8x8_t vsrc3_0 = vld1_u8(src_base + n3_idx * rhs_stride);\n const uint8x8_t vsrc4_0 = vld1_u8(src_base + n4_idx * rhs_stride);\n const uint8x8_t vsrc5_0 = vld1_u8(src_base + n5_idx * rhs_stride);\n const uint8x8_t vsrc6_0 = vld1_u8(src_base + n6_idx * rhs_stride);\n const uint8x8_t vsrc7_0 = vld1_u8(src_base + n7_idx * rhs_stride);\n\n // Extract 2-bit values from the byte.\n const int8x8_t vsrc0_0_s0 = vreinterpret_s8_u8(vand_u8(vsrc0_0, mask_2b));\n const int8x8_t vsrc0_0_s1 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc0_0, 2), mask_2b));\n const int8x8_t vsrc0_0_s2 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc0_0, 4), mask_2b));\n const int8x8_t vsrc0_0_s3 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc0_0, 6), mask_2b));\n\n const int8x8_t vsrc0_0_s0_s8 = vtbl1_s8(vlut_s8, vsrc0_0_s0);\n const int8x8_t vsrc0_0_s1_s8 = vtbl1_s8(vlut_s8, vsrc0_0_s1);\n const int8x8_t vsrc0_0_s2_s8 = vtbl1_s8(vlut_s8, vsrc0_0_s2);\n const int8x8_t vsrc0_0_s3_s8 = vtbl1_s8(vlut_s8, vsrc0_0_s3);\n\n // Extract 2-bit values from the byte.\n const int8x8_t vsrc1_0_s0 = vreinterpret_s8_u8(vand_u8(vsrc1_0, mask_2b));\n const int8x8_t vsrc1_0_s1 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc1_0, 2), mask_2b));\n const int8x8_t vsrc1_0_s2 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc1_0, 4), mask_2b));\n const int8x8_t vsrc1_0_s3 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc1_0, 6), mask_2b));\n\n const int8x8_t vsrc1_0_s0_s8 = vtbl1_s8(vlut_s8, vsrc1_0_s0);\n const int8x8_t vsrc1_0_s1_s8 = vtbl1_s8(vlut_s8, vsrc1_0_s1);\n const int8x8_t vsrc1_0_s2_s8 = vtbl1_s8(vlut_s8, vsrc1_0_s2);\n const int8x8_t vsrc1_0_s3_s8 = vtbl1_s8(vlut_s8, vsrc1_0_s3);\n\n // Extract 2-bit values from the byte.\n const int8x8_t vsrc2_0_s0 = vreinterpret_s8_u8(vand_u8(vsrc2_0, mask_2b));\n const int8x8_t vsrc2_0_s1 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc2_0, 2), mask_2b));\n const int8x8_t vsrc2_0_s2 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc2_0, 4), mask_2b));\n const int8x8_t vsrc2_0_s3 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc2_0, 6), mask_2b));\n\n const int8x8_t vsrc2_0_s0_s8 = vtbl1_s8(vlut_s8, vsrc2_0_s0);\n const int8x8_t vsrc2_0_s1_s8 = vtbl1_s8(vlut_s8, vsrc2_0_s1);\n const int8x8_t vsrc2_0_s2_s8 = vtbl1_s8(vlut_s8, vsrc2_0_s2);\n const int8x8_t vsrc2_0_s3_s8 = vtbl1_s8(vlut_s8, vsrc2_0_s3);\n\n // Extract 2-bit values from the byte.\n const int8x8_t vsrc3_0_s0 = vreinterpret_s8_u8(vand_u8(vsrc3_0, mask_2b));\n const int8x8_t vsrc3_0_s1 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc3_0, 2), mask_2b));\n const int8x8_t vsrc3_0_s2 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc3_0, 4), mask_2b));\n const int8x8_t vsrc3_0_s3 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc3_0, 6), mask_2b));\n\n const int8x8_t vsrc3_0_s0_s8 = vtbl1_s8(vlut_s8, vsrc3_0_s0);\n const int8x8_t vsrc3_0_s1_s8 = vtbl1_s8(vlut_s8, vsrc3_0_s1);\n const int8x8_t vsrc3_0_s2_s8 = vtbl1_s8(vlut_s8, vsrc3_0_s2);\n const int8x8_t vsrc3_0_s3_s8 = vtbl1_s8(vlut_s8, vsrc3_0_s3);\n\n // Extract 2-bit values from the byte.\n const int8x8_t vsrc4_0_s0 = vreinterpret_s8_u8(vand_u8(vsrc4_0, mask_2b));\n const int8x8_t vsrc4_0_s1 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc4_0, 2), mask_2b));\n const int8x8_t vsrc4_0_s2 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc4_0, 4), mask_2b));\n const int8x8_t vsrc4_0_s3 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc4_0, 6), mask_2b));\n\n const int8x8_t vsrc4_0_s0_s8 = vtbl1_s8(vlut_s8, vsrc4_0_s0);\n const int8x8_t vsrc4_0_s1_s8 = vtbl1_s8(vlut_s8, vsrc4_0_s1);\n const int8x8_t vsrc4_0_s2_s8 = vtbl1_s8(vlut_s8, vsrc4_0_s2);\n const int8x8_t vsrc4_0_s3_s8 = vtbl1_s8(vlut_s8, vsrc4_0_s3);\n\n // Extract 2-bit values from the byte.\n const int8x8_t vsrc5_0_s0 = vreinterpret_s8_u8(vand_u8(vsrc5_0, mask_2b));\n const int8x8_t vsrc5_0_s1 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc5_0, 2), mask_2b));\n const int8x8_t vsrc5_0_s2 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc5_0, 4), mask_2b));\n const int8x8_t vsrc5_0_s3 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc5_0, 6), mask_2b));\n\n const int8x8_t vsrc5_0_s0_s8 = vtbl1_s8(vlut_s8, vsrc5_0_s0);\n const int8x8_t vsrc5_0_s1_s8 = vtbl1_s8(vlut_s8, vsrc5_0_s1);\n const int8x8_t vsrc5_0_s2_s8 = vtbl1_s8(vlut_s8, vsrc5_0_s2);\n const int8x8_t vsrc5_0_s3_s8 = vtbl1_s8(vlut_s8, vsrc5_0_s3);\n\n // Extract 2-bit values from the byte.\n const int8x8_t vsrc6_0_s0 = vreinterpret_s8_u8(vand_u8(vsrc6_0, mask_2b));\n const int8x8_t vsrc6_0_s1 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc6_0, 2), mask_2b));\n const int8x8_t vsrc6_0_s2 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc6_0, 4), mask_2b));\n const int8x8_t vsrc6_0_s3 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc6_0, 6), mask_2b));\n\n const int8x8_t vsrc6_0_s0_s8 = vtbl1_s8(vlut_s8, vsrc6_0_s0);\n const int8x8_t vsrc6_0_s1_s8 = vtbl1_s8(vlut_s8, vsrc6_0_s1);\n const int8x8_t vsrc6_0_s2_s8 = vtbl1_s8(vlut_s8, vsrc6_0_s2);\n const int8x8_t vsrc6_0_s3_s8 = vtbl1_s8(vlut_s8, vsrc6_0_s3);\n\n // Extract 2-bit values from the byte.\n const int8x8_t vsrc7_0_s0 = vreinterpret_s8_u8(vand_u8(vsrc7_0, mask_2b));\n const int8x8_t vsrc7_0_s1 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc7_0, 2), mask_2b));\n const int8x8_t vsrc7_0_s2 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc7_0, 4), mask_2b));\n const int8x8_t vsrc7_0_s3 = vreinterpret_s8_u8(vand_u8(vshr_n_u8(vsrc7_0, 6), mask_2b));\n\n const int8x8_t vsrc7_0_s0_s8 = vtbl1_s8(vlut_s8, vsrc7_0_s0);\n const int8x8_t vsrc7_0_s1_s8 = vtbl1_s8(vlut_s8, vsrc7_0_s1);\n const int8x8_t vsrc7_0_s2_s8 = vtbl1_s8(vlut_s8, vsrc7_0_s2);\n const int8x8_t vsrc7_0_s3_s8 = vtbl1_s8(vlut_s8, vsrc7_0_s3);\n\n // Calculate the partial sum\n partial_sum0 +=\n vaddlvq_s16(vaddl_s8(vadd_s8(vsrc0_0_s0_s8, vsrc0_0_s1_s8), vadd_s8(vsrc0_0_s2_s8, vsrc0_0_s3_s8)));\n partial_sum1 +=\n vaddlvq_s16(vaddl_s8(vadd_s8(vsrc1_0_s0_s8, vsrc1_0_s1_s8), vadd_s8(vsrc1_0_s2_s8, vsrc1_0_s3_s8)));\n partial_sum2 +=\n vaddlvq_s16(vaddl_s8(vadd_s8(vsrc2_0_s0_s8, vsrc2_0_s1_s8), vadd_s8(vsrc2_0_s2_s8, vsrc2_0_s3_s8)));\n partial_sum3 +=\n vaddlvq_s16(vaddl_s8(vadd_s8(vsrc3_0_s0_s8, vsrc3_0_s1_s8), vadd_s8(vsrc3_0_s2_s8, vsrc3_0_s3_s8)));\n partial_sum4 +=\n vaddlvq_s16(vaddl_s8(vadd_s8(vsrc4_0_s0_s8, vsrc4_0_s1_s8), vadd_s8(vsrc4_0_s2_s8, vsrc4_0_s3_s8)));\n partial_sum5 +=\n vaddlvq_s16(vaddl_s8(vadd_s8(vsrc5_0_s0_s8, vsrc5_0_s1_s8), vadd_s8(vsrc5_0_s2_s8, vsrc5_0_s3_s8)));\n partial_sum6 +=\n vaddlvq_s16(vaddl_s8(vadd_s8(vsrc6_0_s0_s8, vsrc6_0_s1_s8), vadd_s8(vsrc6_0_s2_s8, vsrc6_0_s3_s8)));\n partial_sum7 +=\n vaddlvq_s16(vaddl_s8(vadd_s8(vsrc7_0_s0_s8, vsrc7_0_s1_s8), vadd_s8(vsrc7_0_s2_s8, vsrc7_0_s3_s8)));\n\n // Update the sum\n sums[nr_block_idx + 0] += partial_sum0;\n sums[nr_block_idx + 1] += partial_sum1;\n sums[nr_block_idx + 2] += partial_sum2;\n sums[nr_block_idx + 3] += partial_sum3;\n sums[nr_block_idx + 4] += partial_sum4;\n sums[nr_block_idx + 5] += partial_sum5;\n sums[nr_block_idx + 6] += partial_sum6;\n sums[nr_block_idx + 7] += partial_sum7;\n\n // Rearrange to get the dst blocksn\n const uint16x4_t vdst_u16_0 = vreinterpret_u16_u8(vzip1_u8(vsrc0_0, vsrc1_0)); // 00 11 22 33\n const uint16x4_t vdst_u16_1 = vreinterpret_u16_u8(vzip2_u8(vsrc0_0, vsrc1_0)); // 44 55 66 77\n const uint16x4_t vdst_u16_2 = vreinterpret_u16_u8(vzip1_u8(vsrc2_0, vsrc3_0)); // 00 11 22 33\n const uint16x4_t vdst_u16_3 = vreinterpret_u16_u8(vzip2_u8(vsrc2_0, vsrc3_0)); // 44 55 66 77\n const uint16x4_t vdst_u16_4 = vreinterpret_u16_u8(vzip1_u8(vsrc4_0, vsrc5_0)); // 00 11 22 33\n const uint16x4_t vdst_u16_5 = vreinterpret_u16_u8(vzip2_u8(vsrc4_0, vsrc5_0)); // 44 55 66 77\n const uint16x4_t vdst_u16_6 = vreinterpret_u16_u8(vzip1_u8(vsrc6_0, vsrc7_0)); // 00 11 22 33\n const uint16x4_t vdst_u16_7 = vreinterpret_u16_u8(vzip2_u8(vsrc6_0, vsrc7_0)); // 44 55 66 77\n\n const uint32x2_t vdst_u32_0 = vreinterpret_u32_u16(vzip1_u16(vdst_u16_0, vdst_u16_2)); // 0000 1111\n const uint32x2_t vdst_u32_1 = vreinterpret_u32_u16(vzip1_u16(vdst_u16_4, vdst_u16_6)); // 0000 1111\n const uint32x2_t vdst_u32_2 = vreinterpret_u32_u16(vzip2_u16(vdst_u16_0, vdst_u16_2)); // 2222 3333\n const uint32x2_t vdst_u32_3 = vreinterpret_u32_u16(vzip2_u16(vdst_u16_4, vdst_u16_6)); // 2222 3333\n const uint32x2_t vdst_u32_4 = vreinterpret_u32_u16(vzip1_u16(vdst_u16_1, vdst_u16_3)); // 4444 5555\n const uint32x2_t vdst_u32_5 = vreinterpret_u32_u16(vzip1_u16(vdst_u16_5, vdst_u16_7)); // 4444 5555\n const uint32x2_t vdst_u32_6 = vreinterpret_u32_u16(vzip2_u16(vdst_u16_1, vdst_u16_3)); // 6666 7777\n const uint32x2_t vdst_u32_7 = vreinterpret_u32_u16(vzip2_u16(vdst_u16_5, vdst_u16_7)); // 6666 7777\n\n const uint32x2_t vdst_0 = vzip1_u32(vdst_u32_0, vdst_u32_1); // 00000000\n const uint32x2_t vdst_1 = vzip2_u32(vdst_u32_0, vdst_u32_1); // 11111111\n const uint32x2_t vdst_2 = vzip1_u32(vdst_u32_2, vdst_u32_3); // 22222222\n const uint32x2_t vdst_3 = vzip2_u32(vdst_u32_2, vdst_u32_3); // 33333333\n const uint32x2_t vdst_4 = vzip1_u32(vdst_u32_4, vdst_u32_5); // 44444444\n const uint32x2_t vdst_5 = vzip2_u32(vdst_u32_4, vdst_u32_5); // 55555555\n const uint32x2_t vdst_6 = vzip1_u32(vdst_u32_6, vdst_u32_7); // 66666666\n const uint32x2_t vdst_7 = vzip2_u32(vdst_u32_6, vdst_u32_7); // 77777777\n\n // Store the packed values\n vst1_u32((uint32_t*)dst_row, vdst_0);\n vst1_u32((uint32_t*)(dst_row + nr), vdst_1);\n vst1_u32((uint32_t*)(dst_row + nr * 2), vdst_2);\n vst1_u32((uint32_t*)(dst_row + nr * 3), vdst_3);\n vst1_u32((uint32_t*)(dst_row + nr * 4), vdst_4);\n vst1_u32((uint32_t*)(dst_row + nr * 5), vdst_5);\n vst1_u32((uint32_t*)(dst_row + nr * 6), vdst_6);\n vst1_u32((uint32_t*)(dst_row + nr * 7), vdst_7);\n\n dst_row += 8 * sizeof(uint8_t);\n }\n dst_row += 7 * nr * sizeof(uint8_t);\n }\n }\n}\n#endif // Architectural features check.\n"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"path": "kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon.h",
|
| 61 |
+
"content": "//\n// SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>\n//\n// SPDX-License-Identifier: Apache-2.0\n//\n#pragma once\n\n#include <stddef.h>\n#include <stdint.h>\n\n#include \"kai/kai_common.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon_params\n#define kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon_params kai_rhs_pack_qs4cxs1s0_param\n#endif\n\n/// Get the n step value.\n///\n/// The micro-kernel can process any N values. However, the starting N index to\n/// be processed must be a multiple of n step.\n///\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n///\n/// @return the n step value\nsize_t kai_get_n_step_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(size_t nr);\n\n/// Gets the offset in bytes for the RHS matrix (not packed)\n///\n/// @note The int2 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Four int2 values are stored in one byte as s3s2s1s0. The lower order part of the byte (low) holds\n/// the first 2-bit (K-index + 0), then the higher order of the byte holds the second 2-bit (K-index + 1)\n/// and so on..\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)\n///\n/// @return the offset in bytes to the RHS matrix (not packed)\nsize_t kai_get_rhs_offset_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(size_t n_idx, size_t rhs_stride);\n\n/// Get the row stride in bytes to the packed RHS matrix\n///\n/// @param[in] k In the RHS matrix (not packed), K is the number of columns.\n/// @param[in] nr The number of columns written by the matmul micro-kernel.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the stride in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Gets the offset in bytes for the packed RHS matrix,\n/// which contains the packed 2-bit quantized symmetric per-channel (qsi2cx) values.\n///\n/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.\n/// @param[in] k The common dimension between the LHS and RHS matrix (K)\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the offset in bytes to the packed RHS matrix\nsize_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(\n size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// @brief Gets the size in bytes for the packed RHS matrix\n///\n/// @param[in] n The number of rows in the RHS matrix (not packed)\n/// @param[in] k The number of columns in the RHS matrix (not packed).\n/// @param[in] nr The number of columns written by the matmul micro-kernel\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n///\n/// @return the packed RHS matrix size in bytes\nsize_t kai_get_rhs_packed_size_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(\n size_t n, size_t k, size_t nr, size_t kr, size_t sr);\n\n/// Run the micro-kernel to pack the RHS matrix.\n///\n/// @note The int2 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.\n/// Four int2 values are stored in one byte as s3s2s1s0. The lower order part of the byte (low) holds\n/// the first 2-bit (K-index + 0), then the higher order of the byte holds the second 2-bit (K-index + 1)\n/// and so on..\n///\n/// @param[in] num_groups The number of groups. It must be 1.\n/// @param[in] n The number of columns of the output matrix (N).\n/// @param[in] k The common dimension between the LHS and RHS matrix (K). It must be a a multiple of 32.\n/// @param[in] nr The number of N columns to interleave on the same output output row.\n/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.\n/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.\n/// @param[in] rhs The RHS matrix containing the 2-bit values.\n/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 4).\n/// @param[in] bias The biases. The bias is set to 0.f if this argument is NULL.\n/// @param[in] scale The scale for each output channel.\n/// @param[out] rhs_packed The packed RHS matrix.\n/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.\n/// @param[in] params Parameters for the micro-kernel.\n/// @param[in] lut_arg Look up table for mapping 2-bit to 8-bit values.\nvoid kai_run_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(\n size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, const uint8_t* rhs, const float* bias,\n const float* scale, void* rhs_packed, size_t extra_bytes,\n const struct kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon_params* params, const int32_t* lut_arg);\n\n#ifdef __cplusplus\n}\n#endif\n"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"path": "binding.cpp",
|
| 65 |
+
"content": "// Binding for Arm KleidiAI kai_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa against gemm_qsu2cx_sme2_n256_k512's raw ABI:\n// armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q2, const float* B_scales, const float* bias, int M)\n#include <arm_neon.h>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n\n#include \"kai/kai_common.h\"\n#include \"kai_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa.h\"\n#include \"kai_lhs_quant_pack_qai8dxp_f32.h\"\n#include \"kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon.h\"\n\nnamespace {\nconstexpr int N = 256;\nconstexpr int K = 512;\n\n// True if `data` differs from the cached snapshot (content-based, not pointer-based: a later\n// workload's buffer can reuse a freed buffer's address; see issue #80), updating the snapshot.\nbool refresh_cache(uint8_t*& snapshot, size_t& snapshot_size, const void* data, size_t size) {\n const bool stale = snapshot == nullptr || snapshot_size != size || std::memcmp(snapshot, data, size) != 0;\n if (stale) {\n if (snapshot_size != size) {\n std::free(snapshot);\n snapshot = static_cast<uint8_t*>(std::malloc(size));\n snapshot_size = size;\n }\n std::memcpy(snapshot, data, size);\n }\n return stale;\n}\n// Widen n raw fp16 patterns to fp32 (exact).\nvoid widen_f16(const uint16_t* src, float* dst, size_t n) {\n size_t i = 0;\n for (; i + 4 <= n; i += 4) vst1q_f32(dst + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(src + i))));\n for (; i < n; ++i) dst[i] = vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(src[i]))), 0);\n}\n} // namespace\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q2, const float* B_scales, const float* bias, int M)\n{\n // The SME2 mopa kernel takes any M in a single call, so there is no GEMV dispatch here.\n const size_t mr = kai_get_mr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t nr = kai_get_nr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t kr = kai_get_kr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa();\n const size_t sr = kai_get_sr_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa();\n (void)sr;\n\n // LHS: the activation is (re)quantized/packed on EVERY call into a grow-only scratch buffer,\n // so the baseline is timed doing the same per-input work a real inference does.\n static void* lhs_packed = nullptr;\n static size_t lhs_cap = 0;\n {\n const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr);\n if (lhs_packed_size > lhs_cap) {\n std::free(lhs_packed);\n lhs_packed = std::malloc(lhs_packed_size);\n lhs_cap = lhs_packed_size;\n if (lhs_packed == nullptr) return -1;\n }\n kai_run_lhs_quant_pack_qai8dxp_f32((size_t)M, (size_t)K, mr, kr, sr, 0, A, (size_t)K * sizeof(float), lhs_packed);\n }\n\n // RHS: weight-derived pack, cached while every weight-side input is unchanged.\n static uint8_t* B_q2_snap = nullptr;\n static size_t B_q2_snap_size = 0;\n static uint8_t* B_scales_snap = nullptr;\n static size_t B_scales_snap_size = 0;\n static uint8_t* bias_snap = nullptr;\n static size_t bias_snap_size = 0;\n static void* rhs_packed = nullptr;\n const bool stale = false\n | refresh_cache(B_q2_snap, B_q2_snap_size, B_q2, (size_t)N*K/4)\n | refresh_cache(B_scales_snap, B_scales_snap_size, B_scales, sizeof(float)*N)\n | refresh_cache(bias_snap, bias_snap_size, bias, sizeof(float)*N);\n if (stale) {\n std::free(rhs_packed);\n const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon((size_t)N, (size_t)K, nr, kr, sr);\n rhs_packed = std::malloc(rhs_packed_size);\n if (rhs_packed == nullptr) return -1;\n struct kai_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon_params params {};\n params.lhs_zero_point = 1;\n params.rhs_zero_point = 2;\n kai_run_rhs_pack_nxk_qsu2cxp4vlx4_qsu2cx_neon(1, (size_t)N, (size_t)K, nr, kr, sr, B_q2, bias, B_scales, rhs_packed, 0, ¶ms, nullptr);\n }\n\n kai_run_matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa((size_t)M, (size_t)N, (size_t)K, lhs_packed, rhs_packed, output, (size_t)N * sizeof(float), sizeof(float), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), nullptr);\n return 0;\n}\n"
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}
|
solutions/kleidiai/baseline-kleidiai-arm/gemm/gemm_u8_f32_sme2_n256_k512.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
solutions/kleidiai/reference-scalar/conv2d/conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2",
|
| 3 |
+
"definition": "conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer 'same' conv2d (NHWC/HWIO) for conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64. Constexpr-baked dims; armbench_entry_conv2d calls inner_conv2d. Ground-truth correctness baseline / nanobot starting point.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "conv2d.cpp::armbench_entry_conv2d",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "conv2d.h",
|
| 25 |
+
"content": "#pragma once\n\n// Per-definition constants for conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2.\n// NHWC input / HWIO weight (channels-last) \u2014 'same' conv; N, H, W are runtime dims.\nnamespace conv2d_def {\nconstexpr int C_in = 32;\nconstexpr int C_out = 64;\nconstexpr int Kh = 3;\nconstexpr int Kw = 3;\nconstexpr int pad = 1;\n} // namespace conv2d_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// N, H, W are the var dims. input: (N, H, W, C_in); weight: (Kh, Kw, C_in, C_out); output: (N, H, W, C_out).\nvoid inner_conv2d(const float* input, float* output, const float* weight, int N, int H, int W);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "conv2d.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_conv2d (no derived dims needed).\n// ABI: armbench_entry_conv2d(input*, output*, weight*, N, H, W)\n#include \"conv2d.h\"\nusing namespace conv2d_def;\n\nextern \"C\" int armbench_entry_conv2d(const float* input, float* output, const float* weight, int N, int H, int W)\n{\n inner_conv2d(input, output, weight, N, H, W);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar conv2d ('same', NHWC/HWIO).\n// LLM target: replace this file with an optimised inner_conv2d.\n// All per-definition constants live in conv2d_def:: (conv2d.h).\n#include \"conv2d.h\"\nusing namespace conv2d_def;\n\nextern \"C\" void inner_conv2d(const float* input, float* output, const float* weight, int N, int H, int W)\n{\n for (int n = 0; n < N; ++n) {\n const float* in_img = input + (long)n * H * W * C_in;\n float* out_img = output + (long)n * H * W * C_out;\n for (int oh = 0; oh < H; ++oh) {\n for (int ow = 0; ow < W; ++ow) {\n float* out_px = out_img + ((long)oh * W + ow) * C_out;\n for (int oc = 0; oc < C_out; ++oc) out_px[oc] = 0.0f;\n for (int kh = 0; kh < Kh; ++kh) {\n int ih = oh - pad + kh;\n if (ih < 0 || ih >= H) continue;\n for (int kw = 0; kw < Kw; ++kw) {\n int iw = ow - pad + kw;\n if (iw < 0 || iw >= W) continue;\n const float* in_px = in_img + ((long)ih * W + iw) * C_in;\n const float* w_row = weight + (((long)kh * Kw + kw) * C_in) * C_out;\n for (int ic = 0; ic < C_in; ++ic) {\n float a = in_px[ic];\n const float* w_ic = w_row + (long)ic * C_out;\n for (int oc = 0; oc < C_out; ++oc) {\n out_px[oc] += a * w_ic[oc];\n }\n }\n }\n }\n }\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_f16_qai4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_f16_qai4c32_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_f16_qai4c32_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_f16_qai4c32_sme2_n256_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_f16_qai4c32_sme2_n256_k512.\n// A f16 bit patterns, B as gemm_qai4c32_sme2, bias f32.\nnamespace gemm_def {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr int K_half = 256;\nconstexpr int K_blocks = 16;\n} // namespace gemm_def\n\n// IEEE half -> float, bit-level so it needs no fp16 compiler support.\nstatic inline float f16_to_f32(uint16_t h) {\n const uint32_t sign = (uint32_t)(h & 0x8000u) << 16;\n uint32_t exp = (h >> 10) & 0x1Fu, man = h & 0x3FFu, bits;\n if (exp == 0) {\n if (man == 0) bits = sign;\n else { exp = 127 - 15 + 1; while (!(man & 0x400u)) { man <<= 1; --exp; } man &= 0x3FFu; bits = sign | (exp << 23) | (man << 13); }\n } else if (exp == 31) bits = sign | 0x7F800000u | (man << 13);\n else bits = sign | ((exp + 112) << 23) | (man << 13);\n union { uint32_t u; float f; } c; c.u = bits; return c.f;\n}\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, B_zeros, bias, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_f16_qai4c32_sme2_n256_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n for (int m = 0; m < M; ++m) {\n float* out_row = output + (long)m * N;\n const uint16_t* a_row = A + (long)m * K;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b = B_q4 + (long)n * K_half;\n float sum = bias[n];\n for (int blk = 0; blk < K_blocks; ++blk) {\n const float scale = B_scales[(long)n * K_blocks + blk];\n const float zero = B_zeros[(long)n * K_blocks + blk];\n for (int j = blk * 16; j < blk * 16 + 16; ++j) {\n const uint8_t byte = b[j];\n sum += f16_to_f32(a_row[2 * j]) * (((float)(byte & 0x0F) - 8.0f) * scale + zero);\n sum += f16_to_f32(a_row[2 * j + 1]) * (((float)(byte >> 4) - 8.0f) * scale + zero);\n }\n }\n out_row[n] = sum;\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_f16_qs4cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_f16_qs4cx_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_f16_qs4cx_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_f16_qs4cx_sme2_n256_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_f16_qs4cx_sme2_n256_k512.\n// A f16 bit patterns, B_q4[N,K_half] adjacent-pair nibbles zero_point 8, B_scales[N] f32, bias[N] f32.\nnamespace gemm_def {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr int K_half = 256;\n} // namespace gemm_def\n\n// IEEE half -> float, bit-level so it needs no fp16 compiler support.\nstatic inline float f16_to_f32(uint16_t h) {\n const uint32_t sign = (uint32_t)(h & 0x8000u) << 16;\n uint32_t exp = (h >> 10) & 0x1Fu, man = h & 0x3FFu, bits;\n if (exp == 0) {\n if (man == 0) bits = sign;\n else { exp = 127 - 15 + 1; while (!(man & 0x400u)) { man <<= 1; --exp; } man &= 0x3FFu; bits = sign | (exp << 23) | (man << 13); }\n } else if (exp == 31) bits = sign | 0x7F800000u | (man << 13);\n else bits = sign | ((exp + 112) << 23) | (man << 13);\n union { uint32_t u; float f; } c; c.u = bits; return c.f;\n}\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* bias, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* bias, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, bias, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_f16_qs4cx_sme2_n256_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* bias, int M)\n{\n for (int m = 0; m < M; ++m) {\n float* out_row = output + (long)m * N;\n const uint16_t* a_row = A + (long)m * K;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b = B_q4 + (long)n * K_half;\n const float scale = B_scales[n];\n float sum = bias[n];\n for (int j = 0; j < K_half; ++j) {\n const uint8_t byte = b[j];\n sum += f16_to_f32(a_row[2 * j]) * ((float)((int)(byte & 0x0F) - 8) * scale);\n sum += f16_to_f32(a_row[2 * j + 1]) * ((float)((int)(byte >> 4) - 8) * scale);\n }\n out_row[n] = sum;\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_f16_qs8cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_f16_qs8cx_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_f16_qs8cx_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_f16_qs8cx_sme2_n256_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_f16_qs8cx_sme2_n256_k512.\n// A f16 bit patterns, B_q8[N,K] int8, B_scales[N] f32, bias[N] f32.\nnamespace gemm_def {\nconstexpr int N = 256;\nconstexpr int K = 512;\n} // namespace gemm_def\n\n// IEEE half -> float, bit-level so it needs no fp16 compiler support.\nstatic inline float f16_to_f32(uint16_t h) {\n const uint32_t sign = (uint32_t)(h & 0x8000u) << 16;\n uint32_t exp = (h >> 10) & 0x1Fu, man = h & 0x3FFu, bits;\n if (exp == 0) {\n if (man == 0) bits = sign;\n else { exp = 127 - 15 + 1; while (!(man & 0x400u)) { man <<= 1; --exp; } man &= 0x3FFu; bits = sign | (exp << 23) | (man << 13); }\n } else if (exp == 31) bits = sign | 0x7F800000u | (man << 13);\n else bits = sign | ((exp + 112) << 23) | (man << 13);\n union { uint32_t u; float f; } c; c.u = bits; return c.f;\n}\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const uint16_t* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M)\n{\n inner_gemm(A, output, B_q8, B_scales, bias, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_f16_qs8cx_sme2_n256_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const int8_t* B_q8, const float* B_scales, const float* bias, int M)\n{\n for (int m = 0; m < M; ++m) {\n float* out_row = output + (long)m * N;\n const uint16_t* a_row = A + (long)m * K;\n for (int n = 0; n < N; ++n) {\n const int8_t* b = B_q8 + (long)n * K;\n const float scale = B_scales[n];\n float sum = bias[n];\n for (int k = 0; k < K; ++k) sum += f16_to_f32(a_row[k]) * ((float)b[k] * scale);\n out_row[n] = sum;\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_f16act_qs4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_f16act_qs4c32_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_f16act_qs4c32_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_f16act_qs4c32_sme2_n256_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_f16act_qs4c32_sme2_n256_k512.\n// A f16 bit patterns, B_q4 as gemm_qs4c32_sme2, B_scales f16 bit patterns.\nnamespace gemm_def {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr int K_half = 256;\nconstexpr int K_blocks = 16;\n} // namespace gemm_def\n\n// IEEE half -> float, bit-level so it needs no fp16 compiler support.\nstatic inline float f16_to_f32(uint16_t h) {\n const uint32_t sign = (uint32_t)(h & 0x8000u) << 16;\n uint32_t exp = (h >> 10) & 0x1Fu, man = h & 0x3FFu, bits;\n if (exp == 0) {\n if (man == 0) bits = sign;\n else { exp = 127 - 15 + 1; while (!(man & 0x400u)) { man <<= 1; --exp; } man &= 0x3FFu; bits = sign | (exp << 23) | (man << 13); }\n } else if (exp == 31) bits = sign | 0x7F800000u | (man << 13);\n else bits = sign | ((exp + 112) << 23) | (man << 13);\n union { uint32_t u; float f; } c; c.u = bits; return c.f;\n}\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_f16act_qs4c32_sme2_n256_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4, const uint16_t* B_scales, int M)\n{\n for (int m = 0; m < M; ++m) {\n float* out_row = output + (long)m * N;\n const uint16_t* a_row = A + (long)m * K;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b = B_q4 + (long)n * K_half;\n float sum = 0.0f;\n for (int blk = 0; blk < K_blocks; ++blk) {\n const float scale = f16_to_f32(B_scales[(long)n * K_blocks + blk]);\n for (int i = 0; i < 16; ++i) {\n const uint8_t byte = b[blk * 16 + i];\n sum += f16_to_f32(a_row[blk * 32 + i]) * ((float)((int)(byte & 0x0F) - 8) * scale);\n sum += f16_to_f32(a_row[blk * 32 + 16 + i]) * ((float)((int)(byte >> 4) - 8) * scale);\n }\n }\n out_row[n] = sum;\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_f32_bf16_n512_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_f32_bf16_n512_k512",
|
| 3 |
+
"definition": "gemm_f32_bf16_n512_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_f32_bf16_n512_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"graviton4",
|
| 11 |
+
"aarch64-sve2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_f32_bf16_n512_k512.\n// A[M,K] f32, B[K,N] f32 (kxn), bias[N] f32; plain f32 compute (the baseline truncates to bf16 internally).\nnamespace gemm_def {\nconstexpr int N = 512;\nconstexpr int K = 512;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const float* A, float* output, const float* B, const float* bias, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const float* B, const float* bias, int M)\n{\n inner_gemm(A, output, B, bias, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_f32_bf16_n512_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const float* A, float* output, const float* B, const float* bias, int M)\n{\n for (int m = 0; m < M; ++m) {\n const float* a_row = A + (long)m * K;\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) out_row[n] = bias[n];\n for (int k = 0; k < K; ++k) {\n const float a = a_row[k];\n const float* b_row = B + (long)k * N;\n for (int n = 0; n < N; ++n) out_row[n] += a * b_row[n];\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_i32_u8_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_i32_u8_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_i32_u8_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_i32_u8_sme2_n256_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_i32_u8_sme2_n256_k512.\n// A[M,K] uint8, B[N,K] uint8; exact int32 accumulation, converted to float32 at the end.\nnamespace gemm_def {\nconstexpr int N = 256;\nconstexpr int K = 512;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const uint8_t* A, float* output, const uint8_t* B, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint8_t* A, float* output, const uint8_t* B, int M)\n{\n inner_gemm(A, output, B, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_i32_u8_sme2_n256_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const uint8_t* A, float* output, const uint8_t* B, int M)\n{\n for (int m = 0; m < M; ++m) {\n float* out_row = output + (long)m * N;\n const uint8_t* a_row = A + (long)m * K;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b = B + (long)n * K;\n int64_t sum = 0;\n for (int k = 0; k < K; ++k) sum += (int64_t)a_row[k] * (int64_t)b[k];\n out_row[n] = (float)(int32_t)sum;\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_qai4c32_n256_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_qai4c32_n256_k512",
|
| 3 |
+
"definition": "gemm_qai4c32_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_qai4c32_n256_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"graviton4",
|
| 11 |
+
"aarch64-sve2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_qai4c32_n256_k512.\n// A[M,K] f32, B_q4[N,K_half] uint8 (adjacent-pair nibbles, block length 32), B_scales/B_zeros[N,K_blocks] f32, value=(nibble-8)*scale+zero, bias[N] f32.\nnamespace gemm_def {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr int K_half = 256;\nconstexpr int K_blocks = 16;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, B_zeros, bias, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_qai4c32_n256_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n for (int m = 0; m < M; ++m) {\n const float* a_row = A + (long)m * K;\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b = B_q4 + (long)n * K_half;\n float sum = bias[n];\n for (int blk = 0; blk < K_blocks; ++blk) {\n const float scale = B_scales[(long)n * K_blocks + blk];\n const float zero = B_zeros[(long)n * K_blocks + blk];\n for (int j = blk * 16; j < blk * 16 + 16; ++j) {\n const uint8_t byte = b[j];\n sum += a_row[2 * j] * (((float)(byte & 0x0F) - 8.0f) * scale + zero);\n sum += a_row[2 * j + 1] * (((float)(byte >> 4) - 8.0f) * scale + zero);\n }\n }\n out_row[n] = sum;\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_qai4c32_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_qai4c32_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_qai4c32_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_qai4c32_sme2_n256_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_qai4c32_sme2_n256_k512.\n// A f32, B_q4[N,K_half] uint8 adjacent-pair nibbles, B_scales/B_zeros[N,K_blocks] f32, value=(nibble-8)*scale+zero, bias f32.\nnamespace gemm_def {\nconstexpr int N = 256;\nconstexpr int K = 512;\nconstexpr int K_half = 256;\nconstexpr int K_blocks = 16;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, B_zeros, bias, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_qai4c32_sme2_n256_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const float* A, float* output, const uint8_t* B_q4, const float* B_scales, const float* B_zeros, const float* bias, int M)\n{\n for (int m = 0; m < M; ++m) {\n float* out_row = output + (long)m * N;\n const float* a_row = A + (long)m * K;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b = B_q4 + (long)n * K_half;\n float sum = bias[n];\n for (int blk = 0; blk < K_blocks; ++blk) {\n const float scale = B_scales[(long)n * K_blocks + blk];\n const float zero = B_zeros[(long)n * K_blocks + blk];\n for (int j = blk * 16; j < blk * 16 + 16; ++j) {\n const uint8_t byte = b[j];\n sum += a_row[2 * j] * (((float)(byte & 0x0F) - 8.0f) * scale + zero);\n sum += a_row[2 * j + 1] * (((float)(byte >> 4) - 8.0f) * scale + zero);\n }\n }\n out_row[n] = sum;\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/kleidiai/reference-scalar/gemm/gemm_qai8_qsi8cx_sme2_n256_k512.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_gemm_qai8_qsi8cx_sme2_n256_k512",
|
| 3 |
+
"definition": "gemm_qai8_qsi8cx_sme2_n256_k512",
|
| 4 |
+
"dataset": "kleidiai",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer gemm for gemm_qai8_qsi8cx_sme2_n256_k512. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"apple-m4",
|
| 11 |
+
"aarch64-sme2"
|
| 12 |
+
],
|
| 13 |
+
"entry_point": "gemm.cpp::armbench_entry_gemm",
|
| 14 |
+
"dependencies": [],
|
| 15 |
+
"isa_features": [],
|
| 16 |
+
"compile_flags": [
|
| 17 |
+
"-O2",
|
| 18 |
+
"-std=c++14"
|
| 19 |
+
],
|
| 20 |
+
"link_flags": []
|
| 21 |
+
},
|
| 22 |
+
"sources": [
|
| 23 |
+
{
|
| 24 |
+
"path": "gemm.h",
|
| 25 |
+
"content": "#pragma once\n\n#include <cstdint>\n\n// Per-definition constants for gemm_qai8_qsi8cx_sme2_n256_k512.\n// A[M,K] int8 (scale A_scale[0], zero point 0), B_q8[N,K] int8 (per-channel B_scales), fp32 bias, output\n// scale C_scale[0] (zero point 0). int8 result is returned as float32.\nnamespace gemm_def {\nconstexpr int N = 256;\nconstexpr int K = 512;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid inner_gemm(const int8_t* A, float* output, const float* A_scale, const int8_t* B_q8, const float* B_scales, const float* bias, const float* C_scale, int M);\n#ifdef __cplusplus\n}\n#endif\n"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "gemm.cpp",
|
| 29 |
+
"content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const int8_t* A, float* output, const float* A_scale, const int8_t* B_q8, const float* B_scales, const float* bias, const float* C_scale, int M)\n{\n inner_gemm(A, output, A_scale, B_q8, B_scales, bias, C_scale, M);\n return 0;\n}\n"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"path": "kernel.cpp",
|
| 33 |
+
"content": "// Reference-scalar gemm for gemm_qai8_qsi8cx_sme2_n256_k512.\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include <cmath>\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" void inner_gemm(const int8_t* A, float* output, const float* A_scale, const int8_t* B_q8, const float* B_scales, const float* bias, const float* C_scale, int M)\n{\n for (int m = 0; m < M; ++m) {\n float* out_row = output + (long)m * N;\n const int8_t* a_row = A + (long)m * K;\n for (int n = 0; n < N; ++n) {\n const int8_t* b = B_q8 + (long)n * K;\n int32_t acc = 0;\n for (int k = 0; k < K; ++k) acc += (int32_t)a_row[k] * (int32_t)b[k];\n const float real = (float)acc * (A_scale[0] * B_scales[n]) + bias[n];\n float q = std::nearbyintf(real / C_scale[0]);\n if (q > 127.0f) q = 127.0f;\n if (q < -128.0f) q = -128.0f;\n out_row[n] = q;\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|