Commit 522a30d
committed
[mlir python] Port Python core code to nanobind.
Why? https://nanobind.readthedocs.io/en/latest/why.html says it better
than I can, but my primary motivation for this change is to improve MLIR
IR construction time from JAX.
For a complicated Google-internal LLM model in JAX, this change improves the MLIR
lowering time by around 5s (out of around 30s), which is a significant
speedup for simply switching binding frameworks.
To a large extent, this is a mechanical change, for instance changing pybind11::
to nanobind::.
Notes:
* this PR needs wjakob/nanobind#806 to land in
nanobind first. Without that fix, importing the MLIR modules will
fail.
* this PR does not port the in-tree dialect extension modules. They can
be ported in a future PR.
* I removed the py::sibling() annotations from def_static and def_class
in PybindAdapters.h. These ask pybind11 to try to form an overload
with an existing method, but it's not possible to form mixed
pybind11/nanobind overloads this ways and the parent class is now defined in
nanobind. Better solutions may be possible here.
* nanobind does not contain an exact equivalent of pybind11's buffer
protocol support. It was not hard to add a nanobind implementation of
a similar API.
* nanobind is pickier about casting to std::vector<bool>, expecting that
the input is a sequence of bool types, not truthy values. In a couple
of places I added code to support truthy values during casting.
* nanobind distinguishes bytes (nb::bytes) from strings (e.g.,
std::string). This required nb::bytes overloads in a few places.1 parent 5d8eabc commit 522a30d
File tree
18 files changed
+1838
-1576
lines changed- mlir
- include/mlir/Bindings/Python
- lib/Bindings/Python
- python
- test/python/ir
18 files changed
+1838
-1576
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | | - | |
| 67 | + | |
69 | 68 | | |
70 | 69 | | |
71 | 70 | | |
| |||
86 | 85 | | |
87 | 86 | | |
88 | 87 | | |
89 | | - | |
90 | | - | |
| 88 | + | |
91 | 89 | | |
92 | 90 | | |
93 | 91 | | |
| |||
106 | 104 | | |
107 | 105 | | |
108 | 106 | | |
109 | | - | |
110 | | - | |
| 107 | + | |
111 | 108 | | |
112 | 109 | | |
113 | 110 | | |
| |||
117 | 114 | | |
118 | 115 | | |
119 | 116 | | |
120 | | - | |
121 | | - | |
| 117 | + | |
122 | 118 | | |
123 | 119 | | |
124 | 120 | | |
| |||
137 | 133 | | |
138 | 134 | | |
139 | 135 | | |
140 | | - | |
141 | | - | |
| 136 | + | |
142 | 137 | | |
143 | 138 | | |
144 | 139 | | |
| |||
156 | 151 | | |
157 | 152 | | |
158 | 153 | | |
159 | | - | |
160 | | - | |
| 154 | + | |
161 | 155 | | |
162 | 156 | | |
163 | 157 | | |
| |||
181 | 175 | | |
182 | 176 | | |
183 | 177 | | |
184 | | - | |
185 | | - | |
| 178 | + | |
186 | 179 | | |
187 | 180 | | |
188 | 181 | | |
| |||
200 | 193 | | |
201 | 194 | | |
202 | 195 | | |
203 | | - | |
204 | | - | |
| 196 | + | |
205 | 197 | | |
206 | 198 | | |
207 | 199 | | |
| |||
221 | 213 | | |
222 | 214 | | |
223 | 215 | | |
224 | | - | |
225 | | - | |
| 216 | + | |
226 | 217 | | |
227 | 218 | | |
228 | 219 | | |
| |||
242 | 233 | | |
243 | 234 | | |
244 | 235 | | |
245 | | - | |
246 | | - | |
| 236 | + | |
247 | 237 | | |
248 | 238 | | |
249 | 239 | | |
| |||
264 | 254 | | |
265 | 255 | | |
266 | 256 | | |
267 | | - | |
268 | | - | |
| 257 | + | |
269 | 258 | | |
270 | 259 | | |
271 | 260 | | |
| |||
275 | 264 | | |
276 | 265 | | |
277 | 266 | | |
278 | | - | |
279 | | - | |
| 267 | + | |
280 | 268 | | |
281 | 269 | | |
282 | 270 | | |
| |||
296 | 284 | | |
297 | 285 | | |
298 | 286 | | |
299 | | - | |
300 | | - | |
| 287 | + | |
301 | 288 | | |
302 | 289 | | |
303 | 290 | | |
| |||
374 | 361 | | |
375 | 362 | | |
376 | 363 | | |
377 | | - | |
378 | | - | |
379 | | - | |
| 364 | + | |
| 365 | + | |
380 | 366 | | |
381 | 367 | | |
382 | 368 | | |
| |||
387 | 373 | | |
388 | 374 | | |
389 | 375 | | |
390 | | - | |
391 | | - | |
392 | | - | |
| 376 | + | |
| 377 | + | |
393 | 378 | | |
394 | 379 | | |
395 | 380 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | | - | |
| 97 | + | |
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
119 | 119 | | |
120 | | - | |
| 120 | + | |
121 | 121 | | |
122 | | - | |
| 122 | + | |
123 | 123 | | |
124 | | - | |
| 124 | + | |
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| |||
0 commit comments