| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | """ |
| 7 | Describe the naming rules for micro-kernels |
| 8 | """ |
| 9 | from naming.grammar import Doc |
| 10 | from naming.grammar import Expr |
| 11 | from naming.grammar import Grammar |
| 12 | from naming.grammar import NaturalInt |
| 13 | from naming.grammar import OneOf |
| 14 | from naming.grammar import OneOrMore |
| 15 | from naming.grammar import OperandType |
| 16 | from naming.grammar import Optional |
| 17 | from naming.grammar import ParseResult |
| 18 | from naming.grammar import Seq |
| 19 | |
| 20 | |
| 21 | grammar = Grammar() |
| 22 | |
| 23 | |
| 24 | @grammar.rule( |
| 25 | title="Quantization mode", |
| 26 | description="Describes whether quantized data uses symmetric or asymmetric quantization.", |
| 27 | ) |
| 28 | def quantization() -> Expr: |
| 29 | return OneOf( |
| 30 | Doc("qa", description="Asymmetrical quantization"), |
| 31 | Doc("qs", description="Symmetrical quantization"), |
| 32 | ) |
| 33 | |
| 34 | |
| 35 | @grammar.rule( |
| 36 | title="Quantization axis or block shape", |
| 37 | description="Describes how quantization parameters are associated with data dimensions or blocks.", |
| 38 | ) |
| 39 | def quantization_axis() -> Expr: |
| 40 | return OneOf( |
| 41 | Doc("cx", description="Per channel quantized"), |
| 42 | Doc("dx", description="Per dimension quantized"), |
| 43 | Doc( |
| 44 | "d32", |
| 45 | description="Per-dimension block quantization, with block length multiple of 32", |
| 46 | ), |
| 47 | Doc( |
| 48 | "c32", |
| 49 | description="Per block quantization, with block length multiple of 32", |
| 50 | ), |
| 51 | ) |
| 52 | |
| 53 | |
| 54 | @grammar.rule( |
| 55 | title="Dimension size", |
| 56 | description=( |
| 57 | "Describes a dimension size, which can be a positive integer, a " |
| 58 | "vector-length multiple using `vl`, a vector-scale multiple using " |
| 59 | "`vs`, or a parametric `mr` or `nr` value." |
| 60 | ), |
| 61 | ) |
| 62 | def size() -> Expr: |
| 63 | return OneOf( |
| 64 | Doc(NaturalInt(), description="Constant value"), |
| 65 | Doc( |
| 66 | Seq(NaturalInt(), "vl"), |
| 67 | description=( |
| 68 | "Accumulator vector length multiple. Assuming 32-bit accumulation " |
| 69 | "on a 512-bit configuration, `4vl` means 64 elements" |
| 70 | ), |
| 71 | ), |
| 72 | Doc( |
| 73 | Seq(NaturalInt(), "vs"), |
| 74 | description=( |
| 75 | "Vector scale multiplier, assuming a vector length of 128 bits. " |
| 76 | "For a 512-bit configuration, `4vs` means 16 elements" |
| 77 | ), |
| 78 | ), |
| 79 | Doc("mr", description="Parametric size, given as argument to micro-kernel"), |
| 80 | Doc("nr", description="Parametric size, given as argument to micro-kernel"), |
| 81 | ) |
| 82 | |
| 83 | |
| 84 | @grammar.rule( |
| 85 | title="Primary tile size", |
| 86 | description="Describes the primary row-by-column tile computed by a micro-kernel.", |
| 87 | ) |
| 88 | def tile_size() -> Expr: |
| 89 | return Seq(size, "x", size) |
| 90 | |
| 91 | |
| 92 | @grammar.rule( |
| 93 | title="Depthwise output block size", |
| 94 | description=( |
| 95 | "Describes the output block shape produced by a depthwise convolution " |
| 96 | "micro-kernel. The `c` column size means all channels." |
| 97 | ), |
| 98 | ) |
| 99 | def dwconv_output_block_size() -> Expr: |
| 100 | # `c` refers to full channel output |
| 101 | return Seq(NaturalInt(), "x", OneOf(NaturalInt(), "c")) |
| 102 | |
| 103 | |
| 104 | @grammar.rule( |
| 105 | title="Packed data order", |
| 106 | description="Describes the ordering of values inside a packed buffer.", |
| 107 | ) |
| 108 | def pack_order() -> Expr: |
| 109 | return OneOf( |
| 110 | Doc("s1s0", description="Packing order of data is sequential"), |
| 111 | Doc( |
| 112 | "s4s0", |
| 113 | description="Packing order of data is interleaved with nibble distance of 4", |
| 114 | ), |
| 115 | Doc("s16s0", description="Packing order of data is interleaved"), |
| 116 | ) |
| 117 | |
| 118 | |
| 119 | @grammar.rule( |
| 120 | title="Packing layout", |
| 121 | description="Describes the dimensions used when data is packed into a buffer.", |
| 122 | ) |
| 123 | def packing_layout() -> Expr: |
| 124 | return Seq( |
| 125 | "p", |
| 126 | Doc(size, description="Width component of a packed buffer layout"), |
| 127 | "x", |
| 128 | Doc(size, description="Height component of a packed buffer layout"), |
| 129 | ) |
| 130 | |
| 131 | |
| 132 | @grammar.rule( |
| 133 | title="Packed scale type", |
| 134 | description="Describes the operand type used for scale values stored in a packed buffer.", |
| 135 | ) |
| 136 | def scale_type() -> Expr: |
| 137 | return Seq("s", OperandType()) |
| 138 | |
| 139 | |
| 140 | @grammar.rule( |
| 141 | title="Packed bias type", |
| 142 | description="Describes the operand type used for bias values stored in a packed buffer.", |
| 143 | ) |
| 144 | def bias_type() -> Expr: |
| 145 | return Seq("b", OperandType()) |
| 146 | |
| 147 | |
| 148 | @grammar.rule( |
| 149 | title="Packed buffer layout description", |
| 150 | description="Describes the packed-buffer suffix of a full buffer descriptor.", |
| 151 | ) |
| 152 | def pack_description() -> Expr: |
| 153 | return Seq( |
| 154 | packing_layout, |
| 155 | Optional(pack_order), |
| 156 | Optional(scale_type), |
| 157 | Optional(bias_type), |
| 158 | ) |
| 159 | |
| 160 | |
| 161 | @grammar.rule( |
| 162 | title="Buffer descriptor", |
| 163 | description=( |
| 164 | "Describes an input or output buffer in a full micro-kernel name. " |
| 165 | "Packed buffers include their concrete packed layout." |
| 166 | ), |
| 167 | ) |
| 168 | def buffer() -> Expr: |
| 169 | return Seq( |
| 170 | Doc(Optional(quantization), description="Quantization indication"), |
| 171 | Doc(OperandType(), description="The main data-type stored in buffer"), |
| 172 | Doc( |
| 173 | Optional(quantization_axis), |
| 174 | description="If quantized, indicates quantization granularity", |
| 175 | ), |
| 176 | Doc( |
| 177 | Optional(pack_description), |
| 178 | description="If packed, indicates packing properties", |
| 179 | ), |
| 180 | ) |
| 181 | |
| 182 | |
| 183 | @grammar.rule( |
| 184 | title="Directory buffer descriptor", |
| 185 | description=( |
| 186 | "Describes an input or output buffer in a micro-kernel directory name. " |
| 187 | "Directory names use abbreviated descriptors and omit concrete packed " |
| 188 | "layout details." |
| 189 | ), |
| 190 | ) |
| 191 | def simplified_buffer() -> Expr: |
| 192 | return Seq( |
| 193 | Optional(quantization), |
| 194 | OperandType(), |
| 195 | Optional(quantization_axis), |
| 196 | Optional("p"), |
| 197 | ) |
| 198 | |
| 199 | |
| 200 | @grammar.rule( |
| 201 | title="Fused operation descriptor", |
| 202 | description="Describes the primary matmul-family operation and any encoded fused operation.", |
| 203 | ) |
| 204 | def matmul_fused_ops() -> Expr: |
| 205 | return OneOf( |
| 206 | Seq(Optional("i"), "matmul", Optional("_clamp")), |
| 207 | "lhs_pack", |
| 208 | "rhs_pack_kxn", |
| 209 | "rhs_pack_nxk", |
| 210 | ) |
| 211 | |
| 212 | |
| 213 | @grammar.rule( |
| 214 | title="Convolution filter size", |
| 215 | description="Describes the height-by-width filter shape used by a depthwise convolution micro-kernel.", |
| 216 | ) |
| 217 | def filter_size() -> Expr: |
| 218 | return Seq(NaturalInt(), "x", NaturalInt()) |
| 219 | |
| 220 | |
| 221 | @grammar.rule( |
| 222 | title="Convolution stride", |
| 223 | description="Describes the stride used by a depthwise convolution micro-kernel.", |
| 224 | ) |
| 225 | def dw_stride() -> Expr: |
| 226 | return Seq("s", NaturalInt()) |
| 227 | |
| 228 | |
| 229 | @grammar.rule( |
| 230 | title="Primary instruction family", |
| 231 | description="Describes the predominant SIMD instruction family used by the implementation.", |
| 232 | ) |
| 233 | def instruction() -> Expr: |
| 234 | return OneOf("dot", "i8mm", "mla", "mmla", "mopa", "sdot") |
| 235 | |
| 236 | |
| 237 | @grammar.rule( |
| 238 | title="Primary feature", |
| 239 | description="Describes the predominant `FEAT_<feature>` used by the implementation.", |
| 240 | ) |
| 241 | def feature() -> Expr: |
| 242 | return OneOf("i8mm", "dotprod") |
| 243 | |
| 244 | |
| 245 | @grammar.rule( |
| 246 | title="SIMD engine", |
| 247 | description="Describes the SIMD engine targeted by the implementation.", |
| 248 | ) |
| 249 | def tech() -> Expr: |
| 250 | return OneOf("neon", "sve", "sve2", "sve2p1", "sme", "sme2", "sme2p1") |
| 251 | |
| 252 | |
| 253 | @grammar.rule( |
| 254 | title="Target microarchitecture", |
| 255 | description="Describes a target microarchitecture for which a micro-kernel is optimized.", |
| 256 | ) |
| 257 | def uarch() -> Expr: |
| 258 | return OneOf("cortexa55") |
| 259 | |
| 260 | |
| 261 | @grammar.rule( |
| 262 | title="Depthwise micro-kernel name", |
| 263 | description="Describes names for depthwise convolution and depthwise RHS packing micro-kernels.", |
| 264 | ) |
| 265 | def dwconv_ukernel_name() -> Expr: |
| 266 | return Seq( |
| 267 | "kai_", |
| 268 | OneOf( |
| 269 | Seq( |
| 270 | "dwconv_clamp", |
| 271 | OneOrMore(Seq("_", buffer)), |
| 272 | Seq("_", filter_size), |
| 273 | Seq("_", dw_stride), |
| 274 | Seq("_", dwconv_output_block_size), |
| 275 | Seq( |
| 276 | "_", |
| 277 | tech, |
| 278 | ), |
| 279 | Optional(Seq("_", instruction)), |
| 280 | ), |
| 281 | Seq( |
| 282 | "rhs_dwconv_pack", |
| 283 | OneOrMore(Seq("_", buffer)), |
| 284 | Seq( |
| 285 | "_", |
| 286 | tech, |
| 287 | ), |
| 288 | ), |
| 289 | ), |
| 290 | ) |
| 291 | |
| 292 | |
| 293 | @grammar.rule( |
| 294 | title="Matmul micro-kernel name", |
| 295 | description="Describes names for matmul-family compute and packing micro-kernels.", |
| 296 | ) |
| 297 | def matmul_ukernel_name() -> Expr: |
| 298 | return Seq( |
| 299 | "kai_", |
| 300 | matmul_fused_ops, |
| 301 | Doc(Seq("_", buffer), description="Destination buffer"), |
| 302 | Doc(OneOrMore(Seq("_", buffer)), description="Input buffer(s)"), |
| 303 | Doc(Optional(Seq("_", tile_size)), description="Tile size"), |
| 304 | Doc(Optional(Seq("_", tech)), description="SIMD engine"), |
| 305 | Doc(Optional(Seq("_", feature)), description="Primary feature"), |
| 306 | Doc(Optional(Seq("_", instruction)), description="Primary instruction"), |
| 307 | Doc(Optional(Seq("_", uarch)), description="Target micro-architecture"), |
| 308 | ) |
| 309 | |
| 310 | |
| 311 | @grammar.rule( |
| 312 | title="Micro-kernel name", |
| 313 | description="Describes every micro-kernel name accepted by the naming rules.", |
| 314 | ) |
| 315 | def kernel_name() -> Expr: |
| 316 | return OneOf(matmul_ukernel_name, dwconv_ukernel_name) |
| 317 | |
| 318 | |
| 319 | @grammar.rule( |
| 320 | title="Micro-kernel directory name", |
| 321 | description="Describes every micro-kernel directory name accepted by the naming rules.", |
| 322 | ) |
| 323 | def directory_name() -> Expr: |
| 324 | return OneOf( |
| 325 | "pack", |
| 326 | Seq( |
| 327 | matmul_fused_ops, |
| 328 | "_", |
| 329 | simplified_buffer, |
| 330 | OneOrMore(Seq("_", simplified_buffer)), |
| 331 | ), |
| 332 | Seq( |
| 333 | "dwconv", |
| 334 | "_", |
| 335 | simplified_buffer, |
| 336 | OneOrMore(Seq("_", simplified_buffer)), |
| 337 | ), |
| 338 | ) |
| 339 | |
| 340 | |
| 341 | def parse_kernel_name(name: str) -> ParseResult: |
| 342 | """Parse the kernel name against the naming rules.""" |
| 343 | return kernel_name.parse_full(name) |
| 344 | |
| 345 | |
| 346 | def parse_directory_name(name: str) -> ParseResult: |
| 347 | """Parse the directory name against the naming rules.""" |
| 348 | return directory_name.parse_full(name) |