Elffuss BraidHub

kikocis/kleidiai public

main · 5.5 KiBRawPermalink
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"""Render micro-kernel naming documentation."""
7from __future__ import annotations
8
9from collections.abc import Iterable
10
11import naming.rules as naming
12from naming.grammar import Doc
13from naming.grammar import Expr
14from naming.grammar import OneOf
15from naming.grammar import Rule
16from naming.grammar import Seq
17
18
19def _grammar_rule_blocks() -> str:
20 return _rule_definition_blocks(
21 reversed(tuple(naming.grammar.iter_documented_rules()))
22 )
23
24
25def _rule_heading(rule: Rule) -> str:
26 if rule.title:
27 return f"### {rule.title}"
28 return f"### `{rule.name}`"
29
30
31def _sentence(text: str) -> str:
32 if not text or text[-1] in ".!?":
33 return text
34 return f"{text}."
35
36
37def _rule_definition_blocks(rules: Iterable[Rule]) -> str:
38 lines = []
39 for rule in rules:
40 if rule.title:
41 lines.extend((_rule_heading(rule), ""))
42 if rule.description:
43 lines.extend((_sentence(rule.description), ""))
44 lines.extend(
45 (
46 f"**`{rule.name}`** = `{rule.expr.to_grammar()}`",
47 "",
48 )
49 )
50 documented_items = tuple(_documented_items(rule.expr))
51 if documented_items:
52 lines.extend(("where:", ""))
53 lines.extend(
54 f"- **`{grammar}`**: {description}"
55 for grammar, description in documented_items
56 )
57 lines.append("")
58 return "\n".join(lines).rstrip()
59
60
61def _documented_items(expr: Expr) -> Iterable[tuple[str, str]]:
62 if isinstance(expr, OneOf):
63 items = expr.options
64 elif isinstance(expr, Seq):
65 items = expr.items
66 else:
67 return
68
69 for item in items:
70 if isinstance(item, Doc):
71 yield (item.to_grammar(), item.description)
72
73
74_COMMON_NAME_SHAPES = """### Common Name Shapes
75
76- LHS packing micro-kernels are named `kai_lhs_pack_<output>_<input>_<description>`.
77- RHS packing micro-kernels are named `kai_rhs_pack_<orientation>_<output>_<inputs>_<description>`.
78- Matmul compute micro-kernels are named `kai_<operation>_<output>_<LHS input>_<RHS input>_<description>`.
79- Depthwise convolution micro-kernels are named with the depthwise operation, buffers, filter, stride, output block, SIMD engine, and optional instruction.
80
81For matmul-family compute micro-kernels, buffer descriptors appear in the order
82destination, LHS input, then RHS input. The output descriptors of LHS and RHS
83packing micro-kernels match the corresponding packed input descriptors of the
84matmul micro-kernel.
85
86### Packed Buffer Layouts
87
88Packed buffers include a `p<width>x<height>` layout in full micro-kernel names.
89For matmul-family names, the LHS packed width is normally the row blocking
90dimension (`MR`), and the RHS packed width is normally the column blocking
91dimension (`NR`). The packed height is the block depth (`BD`), which is derived
92from the `KR` and `SR` values used by the micro-kernel as `KR / SR`.
93
94Packed buffers can also encode data order, packed scale type, and packed bias
95type. Scale values are encoded as `s<type>` and bias values as `b<type>`.
96Legacy `scalef*` and `biasf*` spellings are not valid in the grammar."""
97
98
99def render_documentation() -> str:
100 """Render the generated micro-kernel naming documentation."""
101 grammar = naming.grammar.to_grammar()
102 grammar_rules = _grammar_rule_blocks()
103
104 return f"""<!--
105 SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
106
107 SPDX-License-Identifier: Apache-2.0
108-->
109
110# The micro-kernel naming scheme
111
112This document describes the KleidiAI micro-kernel naming scheme.
113
114## Naming Structure
115
116The high level view of micro-kernel naming is generated from the documented
117grammar rules:
118
119- Micro-kernel source files use names beginning with `kai_`.
120- Matmul micro-kernels use the `matmul_ukernel_name` grammar.
121- Depthwise convolution micro-kernels describe the operation, buffers, filter, stride, output block, SIMD engine, and optional instruction through the `dwconv_ukernel_name` grammar.
122- Micro-kernel directories use the `directory_name` grammar.
123
124{_COMMON_NAME_SHAPES}
125
126### How to Read the Grammar
127
128- **`rule_name`**: Reference to another grammar rule.
129- **`"text"`**: Literal text that appears in the name.
130- **`a b`**: Sequence: `a` followed by `b`.
131- **`a | b`**: Choice: either `a` or `b`.
132- **`[a]`**: Optional expression.
133- **`a+`**: One or more repetitions of `a`.
134- **`(a b)`**: Grouped expression.
135- **`@natural_int`**: Positive integer literal.
136- **`@pow2_int`**: Power-of-two integer literal.
137- **`@operand_type`**: Data type descriptor.
138
139## Naming Rules
140
141The documented naming rules are listed below.
142
143{grammar_rules}
144
145## Rule Enforcement
146
147The naming scheme is implemented in `tools/naming/rules.py`. Micro-kernel and
148directory names are checked by `tools/check_microkernel_names.py`, and CI uses
149the same checker to enforce the rules.
150
151This document is generated from the naming rules. Regenerate it with
152`tools/check_microkernel_names.py --generate-documentation` after updating the
153naming scheme.
154
155## Known naming issues
156
157Some legacy micro-kernel and directory names do not match the current grammar,
158but are retained to preserve the existing API. The known exceptions are listed
159in `tools/naming/issues.py`. To include them in checker output, run
160`tools/check_microkernel_names.py --report-known-issues`.
161
162## Full naming grammar
163
164The grammar below is generated from the naming rules.
165
166```text
167{grammar}
168```
169"""