Commit 60c250b
authored
[Fix][Relax] Scope BasePyModule's Python function registry entries to the module (#20417)
`BasePyModule.__del__` calls `vm.builtin.clear_py_func_registry`, which
empties the process-wide registry rather than removing the module's own
entries. Collecting any module therefore drops every registration in the
process, and a later `R.call_py_func` fails with `Python function
'<name>' not found in registry`.
Whether it happens depends on when the collector runs, so it shows up as
an unrelated test failing. The module built in
`tests/python/relax/test_pytorch_integration.py` registers nothing
itself, but its finalizer wipes the names registered by
`tests/python/relax/test_relax_operators.py`:
```python
tvm.get_global_func("vm.builtin.register_py_func")("victim", tvm.runtime.convert(lambda x: x))
bystander = BasePyModule.__new__(BasePyModule) # registers nothing
del bystander
gc.collect()
tvm.get_global_func("vm.builtin.get_py_func")("victim")
# InternalError: Python function 'victim' not found in registry
```
This adds `vm.builtin.unregister_py_func` and records the names each
module registers, so the finalizer removes only its own entries.
Registering a name again transfers ownership to the newer module, which
keeps a stale module from dropping a live one's function.
The per-module cleanup was also unreachable before this change. The
wrapper stored in the registry captured `self`, so the registry held a
strong reference to every module that registered a function and none of
them were ever collected — the finalizer only ever ran for modules that
had registered nothing, which are exactly the ones that then cleared
everyone else's entries. The wrapper now holds a weak reference to its
module, and reports a clear error if it is called after the module is
gone:
```
RuntimeError: Python function 'f' belongs to a BasePyModule that has been destroyed;
keep the module alive while calling into it.
```
`vm.builtin.clear_py_func_registry` is left in place; it simply has no
callers now.1 parent 0db97a5 commit 60c250b
3 files changed
Lines changed: 85 additions & 10 deletions
File tree
- python/tvm/relax
- src/runtime/vm
- tests/python/relax
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
| |||
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
46 | 53 | | |
47 | 54 | | |
48 | 55 | | |
| |||
56 | 63 | | |
57 | 64 | | |
58 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
59 | 70 | | |
60 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
61 | 75 | | |
62 | | - | |
63 | | - | |
64 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
65 | 91 | | |
66 | 92 | | |
67 | 93 | | |
| |||
203 | 229 | | |
204 | 230 | | |
205 | 231 | | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
206 | 237 | | |
207 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
208 | 246 | | |
209 | | - | |
| 247 | + | |
210 | 248 | | |
211 | 249 | | |
212 | | - | |
| 250 | + | |
213 | 251 | | |
214 | | - | |
| 252 | + | |
215 | 253 | | |
216 | 254 | | |
217 | 255 | | |
218 | 256 | | |
219 | 257 | | |
220 | 258 | | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
221 | 262 | | |
222 | 263 | | |
223 | 264 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
472 | 472 | | |
473 | 473 | | |
474 | 474 | | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
475 | 481 | | |
476 | 482 | | |
477 | 483 | | |
| |||
522 | 528 | | |
523 | 529 | | |
524 | 530 | | |
| 531 | + | |
525 | 532 | | |
526 | 533 | | |
527 | 534 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
| |||
478 | 481 | | |
479 | 482 | | |
480 | 483 | | |
481 | | - | |
482 | | - | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
483 | 510 | | |
484 | 511 | | |
485 | 512 | | |
| |||
0 commit comments