Commit 7942d33
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 7942d33
File tree
20 files changed
+2165
-1916
lines changed- mlir
- include/mlir/Bindings/Python
- lib/Bindings/Python
- python
- test/python/ir
20 files changed
+2165
-1916
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | | - | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
57 | | - | |
| 56 | + | |
| 57 | + | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
| 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 | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
30 | | - | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
236 | | - | |
237 | | - | |
| 236 | + | |
238 | 237 | | |
239 | 238 | | |
240 | 239 | | |
| |||
255 | 254 | | |
256 | 255 | | |
257 | 256 | | |
258 | | - | |
259 | | - | |
| 257 | + | |
260 | 258 | | |
261 | 259 | | |
262 | 260 | | |
| |||
289 | 287 | | |
290 | 288 | | |
291 | 289 | | |
292 | | - | |
293 | | - | |
| 290 | + | |
294 | 291 | | |
295 | 292 | | |
296 | 293 | | |
| |||
321 | 318 | | |
322 | 319 | | |
323 | 320 | | |
324 | | - | |
325 | | - | |
| 321 | + | |
| 322 | + | |
326 | 323 | | |
327 | 324 | | |
328 | 325 | | |
| |||
340 | 337 | | |
341 | 338 | | |
342 | 339 | | |
343 | | - | |
| 340 | + | |
344 | 341 | | |
345 | 342 | | |
346 | 343 | | |
| |||
382 | 379 | | |
383 | 380 | | |
384 | 381 | | |
385 | | - | |
| 382 | + | |
386 | 383 | | |
387 | 384 | | |
388 | 385 | | |
| |||
396 | 393 | | |
397 | 394 | | |
398 | 395 | | |
399 | | - | |
| 396 | + | |
400 | 397 | | |
401 | 398 | | |
402 | 399 | | |
| |||
405 | 402 | | |
406 | 403 | | |
407 | 404 | | |
408 | | - | |
| 405 | + | |
409 | 406 | | |
410 | 407 | | |
411 | 408 | | |
412 | 409 | | |
413 | 410 | | |
414 | 411 | | |
415 | 412 | | |
416 | | - | |
| 413 | + | |
417 | 414 | | |
418 | 415 | | |
419 | 416 | | |
| |||
445 | 442 | | |
446 | 443 | | |
447 | 444 | | |
448 | | - | |
| 445 | + | |
449 | 446 | | |
450 | 447 | | |
451 | 448 | | |
| |||
491 | 488 | | |
492 | 489 | | |
493 | 490 | | |
494 | | - | |
| 491 | + | |
495 | 492 | | |
496 | 493 | | |
497 | 494 | | |
| |||
523 | 520 | | |
524 | 521 | | |
525 | 522 | | |
526 | | - | |
| 523 | + | |
527 | 524 | | |
528 | 525 | | |
529 | 526 | | |
| |||
573 | 570 | | |
574 | 571 | | |
575 | 572 | | |
576 | | - | |
| 573 | + | |
577 | 574 | | |
578 | 575 | | |
579 | 576 | | |
| |||
601 | 598 | | |
602 | 599 | | |
603 | 600 | | |
604 | | - | |
| 601 | + | |
605 | 602 | | |
606 | 603 | | |
607 | 604 | | |
| |||
629 | 626 | | |
630 | 627 | | |
631 | 628 | | |
632 | | - | |
| 629 | + | |
633 | 630 | | |
634 | 631 | | |
635 | 632 | | |
636 | 633 | | |
637 | | - | |
| 634 | + | |
638 | 635 | | |
639 | 636 | | |
640 | 637 | | |
| |||
646 | 643 | | |
647 | 644 | | |
648 | 645 | | |
649 | | - | |
| 646 | + | |
650 | 647 | | |
651 | 648 | | |
652 | 649 | | |
| |||
665 | 662 | | |
666 | 663 | | |
667 | 664 | | |
668 | | - | |
669 | | - | |
| 665 | + | |
| 666 | + | |
670 | 667 | | |
671 | | - | |
| 668 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
377 | | - | |
378 | | - | |
379 | | - | |
| 377 | + | |
| 378 | + | |
380 | 379 | | |
381 | 380 | | |
382 | 381 | | |
| |||
387 | 386 | | |
388 | 387 | | |
389 | 388 | | |
390 | | - | |
391 | | - | |
392 | | - | |
| 389 | + | |
| 390 | + | |
393 | 391 | | |
394 | 392 | | |
395 | 393 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | | - | |
15 | | - | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 20 | + | |
| 21 | + | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
| |||
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
60 | | - | |
| 59 | + | |
61 | 60 | | |
62 | 61 | | |
63 | 62 | | |
64 | 63 | | |
65 | 64 | | |
66 | | - | |
| 65 | + | |
67 | 66 | | |
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
71 | 70 | | |
72 | 71 | | |
73 | | - | |
| 72 | + | |
74 | 73 | | |
75 | 74 | | |
76 | 75 | | |
77 | 76 | | |
78 | 77 | | |
79 | 78 | | |
80 | | - | |
| 79 | + | |
81 | 80 | | |
82 | 81 | | |
83 | 82 | | |
84 | 83 | | |
85 | 84 | | |
86 | | - | |
| 85 | + | |
87 | 86 | | |
88 | 87 | | |
89 | | - | |
90 | | - | |
| 88 | + | |
| 89 | + | |
91 | 90 | | |
92 | 91 | | |
93 | | - | |
| 92 | + | |
94 | 93 | | |
95 | 94 | | |
96 | 95 | | |
97 | | - | |
| 96 | + | |
98 | 97 | | |
99 | 98 | | |
100 | 99 | | |
101 | 100 | | |
102 | | - | |
103 | | - | |
| 101 | + | |
| 102 | + | |
104 | 103 | | |
105 | 104 | | |
106 | 105 | | |
107 | 106 | | |
108 | | - | |
109 | | - | |
| 107 | + | |
| 108 | + | |
110 | 109 | | |
111 | | - | |
| 110 | + | |
112 | 111 | | |
113 | 112 | | |
114 | 113 | | |
115 | 114 | | |
116 | | - | |
| 115 | + | |
117 | 116 | | |
118 | | - | |
| 117 | + | |
119 | 118 | | |
120 | | - | |
| 119 | + | |
121 | 120 | | |
122 | | - | |
| 121 | + | |
123 | 122 | | |
124 | | - | |
| 123 | + | |
125 | 124 | | |
126 | 125 | | |
127 | 126 | | |
| |||
0 commit comments