Elffuss BraidHub

kikocis/kleidiai public

main · 10.6 KiBRenderedRawPermalink
1<!--
2 SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
3
4 SPDX-License-Identifier: Apache-2.0
5-->
6
7# The micro-kernel naming scheme
8
9This document describes the KleidiAI micro-kernel naming scheme.
10
11## Naming Structure
12
13The high level view of micro-kernel naming is generated from the documented
14grammar rules:
15
16- Micro-kernel source files use names beginning with `kai_`.
17- Matmul micro-kernels use the `matmul_ukernel_name` grammar.
18- Depthwise convolution micro-kernels describe the operation, buffers, filter, stride, output block, SIMD engine, and optional instruction through the `dwconv_ukernel_name` grammar.
19- Micro-kernel directories use the `directory_name` grammar.
20
21### Common Name Shapes
22
23- LHS packing micro-kernels are named `kai_lhs_pack_<output>_<input>_<description>`.
24- RHS packing micro-kernels are named `kai_rhs_pack_<orientation>_<output>_<inputs>_<description>`.
25- Matmul compute micro-kernels are named `kai_<operation>_<output>_<LHS input>_<RHS input>_<description>`.
26- Depthwise convolution micro-kernels are named with the depthwise operation, buffers, filter, stride, output block, SIMD engine, and optional instruction.
27
28For matmul-family compute micro-kernels, buffer descriptors appear in the order
29destination, LHS input, then RHS input. The output descriptors of LHS and RHS
30packing micro-kernels match the corresponding packed input descriptors of the
31matmul micro-kernel.
32
33### Packed Buffer Layouts
34
35Packed buffers include a `p<width>x<height>` layout in full micro-kernel names.
36For matmul-family names, the LHS packed width is normally the row blocking
37dimension (`MR`), and the RHS packed width is normally the column blocking
38dimension (`NR`). The packed height is the block depth (`BD`), which is derived
39from the `KR` and `SR` values used by the micro-kernel as `KR / SR`.
40
41Packed buffers can also encode data order, packed scale type, and packed bias
42type. Scale values are encoded as `s<type>` and bias values as `b<type>`.
43Legacy `scalef*` and `biasf*` spellings are not valid in the grammar.
44
45### How to Read the Grammar
46
47- **`rule_name`**: Reference to another grammar rule.
48- **`"text"`**: Literal text that appears in the name.
49- **`a b`**: Sequence: `a` followed by `b`.
50- **`a | b`**: Choice: either `a` or `b`.
51- **`[a]`**: Optional expression.
52- **`a+`**: One or more repetitions of `a`.
53- **`(a b)`**: Grouped expression.
54- **`@natural_int`**: Positive integer literal.
55- **`@pow2_int`**: Power-of-two integer literal.
56- **`@operand_type`**: Data type descriptor.
57
58## Naming Rules
59
60The documented naming rules are listed below.
61
62### Micro-kernel directory name
63
64Describes every micro-kernel directory name accepted by the naming rules.
65
66**`directory_name`** = `"pack" | matmul_fused_ops "_" simplified_buffer ("_" simplified_buffer)+ | "dwconv" "_" simplified_buffer ("_" simplified_buffer)+`
67
68### Micro-kernel name
69
70Describes every micro-kernel name accepted by the naming rules.
71
72**`kernel_name`** = `matmul_ukernel_name | dwconv_ukernel_name`
73
74### Matmul micro-kernel name
75
76Describes names for matmul-family compute and packing micro-kernels.
77
78**`matmul_ukernel_name`** = `"kai_" matmul_fused_ops ("_" buffer) ("_" buffer)+ ["_" tile_size] ["_" tech] ["_" feature] ["_" instruction] ["_" uarch]`
79
80where:
81
82- **`"_" buffer`**: Destination buffer
83- **`("_" buffer)+`**: Input buffer(s)
84- **`["_" tile_size]`**: Tile size
85- **`["_" tech]`**: SIMD engine
86- **`["_" feature]`**: Primary feature
87- **`["_" instruction]`**: Primary instruction
88- **`["_" uarch]`**: Target micro-architecture
89
90### Depthwise micro-kernel name
91
92Describes names for depthwise convolution and depthwise RHS packing micro-kernels.
93
94**`dwconv_ukernel_name`** = `"kai_" ("dwconv_clamp" ("_" buffer)+ ("_" filter_size) ("_" dw_stride) ("_" dwconv_output_block_size) ("_" tech) ["_" instruction] | "rhs_dwconv_pack" ("_" buffer)+ ("_" tech))`
95
96### Target microarchitecture
97
98Describes a target microarchitecture for which a micro-kernel is optimized.
99
100**`uarch`** = `"cortexa55"`
101
102### SIMD engine
103
104Describes the SIMD engine targeted by the implementation.
105
106**`tech`** = `"neon" | "sve" | "sve2" | "sve2p1" | "sme" | "sme2" | "sme2p1"`
107
108### Primary feature
109
110Describes the predominant `FEAT_<feature>` used by the implementation.
111
112**`feature`** = `"i8mm" | "dotprod"`
113
114### Primary instruction family
115
116Describes the predominant SIMD instruction family used by the implementation.
117
118**`instruction`** = `"dot" | "i8mm" | "mla" | "mmla" | "mopa" | "sdot"`
119
120### Convolution stride
121
122Describes the stride used by a depthwise convolution micro-kernel.
123
124**`dw_stride`** = `"s" @natural_int`
125
126### Convolution filter size
127
128Describes the height-by-width filter shape used by a depthwise convolution micro-kernel.
129
130**`filter_size`** = `@natural_int "x" @natural_int`
131
132### Fused operation descriptor
133
134Describes the primary matmul-family operation and any encoded fused operation.
135
136**`matmul_fused_ops`** = `["i"] "matmul" ["_clamp"] | "lhs_pack" | "rhs_pack_kxn" | "rhs_pack_nxk"`
137
138### Directory buffer descriptor
139
140Describes an input or output buffer in a micro-kernel directory name. Directory names use abbreviated descriptors and omit concrete packed layout details.
141
142**`simplified_buffer`** = `[quantization] @operand_type [quantization_axis] ["p"]`
143
144### Buffer descriptor
145
146Describes an input or output buffer in a full micro-kernel name. Packed buffers include their concrete packed layout.
147
148**`buffer`** = `[quantization] @operand_type [quantization_axis] [pack_description]`
149
150where:
151
152- **`[quantization]`**: Quantization indication
153- **`@operand_type`**: The main data-type stored in buffer
154- **`[quantization_axis]`**: If quantized, indicates quantization granularity
155- **`[pack_description]`**: If packed, indicates packing properties
156
157### Packed buffer layout description
158
159Describes the packed-buffer suffix of a full buffer descriptor.
160
161**`pack_description`** = `packing_layout [pack_order] [scale_type] [bias_type]`
162
163### Packed bias type
164
165Describes the operand type used for bias values stored in a packed buffer.
166
167**`bias_type`** = `"b" @operand_type`
168
169### Packed scale type
170
171Describes the operand type used for scale values stored in a packed buffer.
172
173**`scale_type`** = `"s" @operand_type`
174
175### Packing layout
176
177Describes the dimensions used when data is packed into a buffer.
178
179**`packing_layout`** = `"p" size "x" size`
180
181where:
182
183- **`size`**: Width component of a packed buffer layout
184- **`size`**: Height component of a packed buffer layout
185
186### Packed data order
187
188Describes the ordering of values inside a packed buffer.
189
190**`pack_order`** = `"s1s0" | "s4s0" | "s16s0"`
191
192where:
193
194- **`"s1s0"`**: Packing order of data is sequential
195- **`"s4s0"`**: Packing order of data is interleaved with nibble distance of 4
196- **`"s16s0"`**: Packing order of data is interleaved
197
198### Depthwise output block size
199
200Describes the output block shape produced by a depthwise convolution micro-kernel. The `c` column size means all channels.
201
202**`dwconv_output_block_size`** = `@natural_int "x" (@natural_int | "c")`
203
204### Primary tile size
205
206Describes the primary row-by-column tile computed by a micro-kernel.
207
208**`tile_size`** = `size "x" size`
209
210### Dimension size
211
212Describes a dimension size, which can be a positive integer, a vector-length multiple using `vl`, a vector-scale multiple using `vs`, or a parametric `mr` or `nr` value.
213
214**`size`** = `@natural_int | @natural_int "vl" | @natural_int "vs" | "mr" | "nr"`
215
216where:
217
218- **`@natural_int`**: Constant value
219- **`@natural_int "vl"`**: Accumulator vector length multiple. Assuming 32-bit accumulation on a 512-bit configuration, `4vl` means 64 elements
220- **`@natural_int "vs"`**: Vector scale multiplier, assuming a vector length of 128 bits. For a 512-bit configuration, `4vs` means 16 elements
221- **`"mr"`**: Parametric size, given as argument to micro-kernel
222- **`"nr"`**: Parametric size, given as argument to micro-kernel
223
224### Quantization axis or block shape
225
226Describes how quantization parameters are associated with data dimensions or blocks.
227
228**`quantization_axis`** = `"cx" | "dx" | "d32" | "c32"`
229
230where:
231
232- **`"cx"`**: Per channel quantized
233- **`"dx"`**: Per dimension quantized
234- **`"d32"`**: Per-dimension block quantization, with block length multiple of 32
235- **`"c32"`**: Per block quantization, with block length multiple of 32
236
237### Quantization mode
238
239Describes whether quantized data uses symmetric or asymmetric quantization.
240
241**`quantization`** = `"qa" | "qs"`
242
243where:
244
245- **`"qa"`**: Asymmetrical quantization
246- **`"qs"`**: Symmetrical quantization
247
248## Rule Enforcement
249
250The naming scheme is implemented in `tools/naming/rules.py`. Micro-kernel and
251directory names are checked by `tools/check_microkernel_names.py`, and CI uses
252the same checker to enforce the rules.
253
254This document is generated from the naming rules. Regenerate it with
255`tools/check_microkernel_names.py --generate-documentation` after updating the
256naming scheme.
257
258## Known naming issues
259
260Some legacy micro-kernel and directory names do not match the current grammar,
261but are retained to preserve the existing API. The known exceptions are listed
262in `tools/naming/issues.py`. To include them in checker output, run
263`tools/check_microkernel_names.py --report-known-issues`.
264
265## Full naming grammar
266
267The grammar below is generated from the naming rules.
268
269```text
270directory_name = "pack" | matmul_fused_ops "_" simplified_buffer ("_" simplified_buffer)+ | "dwconv" "_" simplified_buffer ("_" simplified_buffer)+
271kernel_name = matmul_ukernel_name | dwconv_ukernel_name
272matmul_ukernel_name = "kai_" matmul_fused_ops ("_" buffer) ("_" buffer)+ ["_" tile_size] ["_" tech] ["_" feature] ["_" instruction] ["_" uarch]
273dwconv_ukernel_name = "kai_" ("dwconv_clamp" ("_" buffer)+ ("_" filter_size) ("_" dw_stride) ("_" dwconv_output_block_size) ("_" tech) ["_" instruction] | "rhs_dwconv_pack" ("_" buffer)+ ("_" tech))
274uarch = "cortexa55"
275tech = "neon" | "sve" | "sve2" | "sve2p1" | "sme" | "sme2" | "sme2p1"
276feature = "i8mm" | "dotprod"
277instruction = "dot" | "i8mm" | "mla" | "mmla" | "mopa" | "sdot"
278dw_stride = "s" @natural_int
279filter_size = @natural_int "x" @natural_int
280matmul_fused_ops = ["i"] "matmul" ["_clamp"] | "lhs_pack" | "rhs_pack_kxn" | "rhs_pack_nxk"
281simplified_buffer = [quantization] @operand_type [quantization_axis] ["p"]
282buffer = [quantization] @operand_type [quantization_axis] [pack_description]
283pack_description = packing_layout [pack_order] [scale_type] [bias_type]
284bias_type = "b" @operand_type
285scale_type = "s" @operand_type
286packing_layout = "p" size "x" size
287pack_order = "s1s0" | "s4s0" | "s16s0"
288dwconv_output_block_size = @natural_int "x" (@natural_int | "c")
289tile_size = size "x" size
290size = @natural_int | @natural_int "vl" | @natural_int "vs" | "mr" | "nr"
291quantization_axis = "cx" | "dx" | "d32" | "c32"
292quantization = "qa" | "qs"
293```