Commit 6a5c26e
committed
fix(networks): lazily import onnx so a broken onnx cannot block
`monai/networks/utils.py` and `monai/bundle/scripts.py` imported `onnx`,
`onnx.reference` and `onnxruntime` eagerly at module scope via `optional_import`,
which calls `__import__` immediately. Because `import monai` auto-loads
`monai.networks`, a broken or hanging onnx install (e.g. onnx 1.18 on Windows)
takes down `import monai` with no error message.
Defer these optional imports into the functions that use them
(`convert_to_onnx`, `onnx_export`), matching the existing lazy pattern used for
tensorrt. Importing MONAI no longer imports onnx, and the conversion paths are
otherwise unchanged. Adds a regression test asserting `import monai` and
`import monai.bundle` do not eagerly import onnx/onnxruntime.
Fixes #8455.
Signed-off-by: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com>import monai
1 parent b7d14c8 commit 6a5c26e
3 files changed
Lines changed: 56 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | 60 | | |
62 | 61 | | |
63 | 62 | | |
| |||
1419 | 1418 | | |
1420 | 1419 | | |
1421 | 1420 | | |
| 1421 | + | |
1422 | 1422 | | |
1423 | 1423 | | |
1424 | 1424 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 37 | | |
41 | 38 | | |
42 | 39 | | |
| |||
708 | 705 | | |
709 | 706 | | |
710 | 707 | | |
| 708 | + | |
| 709 | + | |
711 | 710 | | |
712 | 711 | | |
713 | 712 | | |
| |||
777 | 776 | | |
778 | 777 | | |
779 | 778 | | |
| 779 | + | |
780 | 780 | | |
781 | 781 | | |
782 | 782 | | |
783 | 783 | | |
784 | 784 | | |
| 785 | + | |
785 | 786 | | |
786 | 787 | | |
787 | 788 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
0 commit comments