Skip to content

Add onnx export with verification#6237

Merged
wyli merged 59 commits intoProject-MONAI:devfrom
liqunfu:liqun/export-onnx
Apr 5, 2023
Merged

Add onnx export with verification#6237
wyli merged 59 commits intoProject-MONAI:devfrom
liqunfu:liqun/export-onnx

Conversation

@liqunfu
Copy link
Copy Markdown
Contributor

@liqunfu liqunfu commented Mar 26, 2023

Fixes # Issue#TBD.

Description

A MONAI Bundle can contain models in PyTorch, TorchScript, and ONNX formats.
Utility exists in MONAI to export a model in TorchScript format. Exported TorchScript models can be verified with Torch runtime.
It is helpful (and complete) to provide a same utility to export ONNX models and to validate the models with PyTorch, ONNX(https://github.com/onnx/onnx), and onnxruntime (https://github.com/microsoft/onnxruntime).

With the proposed utility, ONNX model can be added to bundles in model-zoo with calling monai.bundle ckpt_export.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • [x ] New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

./runtests.sh --quick --unittests --disttests
TOTAL 72191 11290 84%

@liqunfu liqunfu marked this pull request as draft March 26, 2023 00:21
@wyli wyli requested review from Nic-Ma, binliunls and ericspod March 27, 2023 10:46
@Nic-Ma Nic-Ma requested a review from yiheng-wang-nv March 27, 2023 14:36
@binliunls
Copy link
Copy Markdown
Contributor

Hi @liqunfu ,
Do you have an ETA for this PR? We are working on this too. If you can have this PR merged this week, we can wait. Otherwise we can start a PR for this and invite you to review it.

Thanks,
Bin

@liqunfu
Copy link
Copy Markdown
Contributor Author

liqunfu commented Mar 29, 2023

Hi @liqunfu , Do you have an ETA for this PR? We are working on this too. If you can have this PR merged this week, we can wait. Otherwise we can start a PR for this and invite you to review it.

Thanks, Bin

I am starting today to update my PR with reviewers' comments and to fix CI failures. I was trying to add more test for models that are used by model-zoo but it is time consuming because I need to get evaluation data some of which also need to wait for access approval. I agree with @ericspod that it can be in another PR.

BTW thanks all for reviewing this PR. I will update it by the end of today.

@liqunfu
Copy link
Copy Markdown
Contributor Author

liqunfu commented Mar 29, 2023

Hi @liqunfu , Do you have an ETA for this PR? We are working on this too. If you can have this PR merged this week, we can wait. Otherwise we can start a PR for this and invite you to review it.
Thanks, Bin

I am starting today to update my PR with reviewers' comments and to fix CI failures. I was trying to add more test for models that are used by model-zoo but it is time consuming because I need to get evaluation data some of which also need to wait for access approval. I agree with @ericspod that it can be in another PR.

BTW thanks all for reviewing this PR. I will update it by the end of today.

@binliunls Please feel free to add to this PR with what you may already have so we can merge sooner.

@liqunfu liqunfu changed the title WIP: add onnx export with verification Add onnx export with verification Mar 29, 2023
@liqunfu liqunfu marked this pull request as ready for review March 29, 2023 19:49
Copy link
Copy Markdown
Contributor

@Nic-Ma Nic-Ma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nic-Ma
Copy link
Copy Markdown
Contributor

Nic-Ma commented Mar 30, 2023

Mark: This PR will be targeting MONAI 1.2 P1 feature as several bundles which can't be supported by torch-trt so far can leverage ONNX path to convert to TensorRT model.

Thanks.

@binliunls
Copy link
Copy Markdown
Contributor

Hi @wyli ,
The windows-latest ci/cd test reports OOM error. Could you please give me some advice on why this happened and how to fix it?

error:

======================================================================
ERROR: test_vnet_shape_5 (tests.test_vnet.TestVNet)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\site-packages\parameterized\parameterized.py", line 620, in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
  File "D:\a\MONAI\MONAI\tests\test_vnet.py", line 69, in test_vnet_shape
    net = VNet(**input_param).to(device)
  File "D:\a\MONAI\MONAI\monai\networks\nets\vnet.py", line 241, in __init__
    self.up_tr256 = UpTransition(spatial_dims, 256, 256, 2, act, dropout_prob=dropout_prob)
  File "D:\a\MONAI\MONAI\monai\networks\nets\vnet.py", line 151, in __init__
    self.ops = _make_nconv(spatial_dims, out_channels, nconvs, act)
  File "D:\a\MONAI\MONAI\monai\networks\nets\vnet.py", line 55, in _make_nconv
    layers.append(LUConv(spatial_dims, nchan, act, bias))
  File "D:\a\MONAI\MONAI\monai\networks\nets\vnet.py", line 36, in __init__
    self.conv_block = Convolution(
  File "D:\a\MONAI\MONAI\monai\networks\blocks\convolutions.py", line 143, in __init__
    conv = conv_type(
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\site-packages\torch\nn\modules\conv.py", line 591, in __init__
    super(Conv3d, self).__init__(
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\site-packages\torch\nn\modules\conv.py", line 137, in __init__
    self.weight = Parameter(torch.empty(
RuntimeError: [enforce fail at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\c10\core\impl\alloc_cpu.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 32768000 bytes.

======================================================================
FAIL: test_cachedataset (tests.test_sampler_dist.DistributedSamplerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\MONAI\MONAI\tests\utils.py", line 539, in _wrapper
    assert results.get(), "Distributed call failed."
AssertionError: Distributed call failed.

Thanks,
Bin

@binliunls binliunls mentioned this pull request Apr 3, 2023
7 tasks
Copy link
Copy Markdown
Contributor

@Nic-Ma Nic-Ma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, put some minor comments inline.
@wyli Do you think we need to add all the optional packages in this function or only selected?
https://github.com/Project-MONAI/MONAI/blob/dev/monai/config/deviceconfig.py#L64

Thanks.

Copy link
Copy Markdown
Contributor

@Nic-Ma Nic-Ma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, looks good to me.
Please fix the failed CI tests on windows, if it's expected error, please add skip_if_windows decorator.

Thanks.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Apr 4, 2023

/build

@wyli wyli enabled auto-merge (squash) April 4, 2023 21:38
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Apr 4, 2023

/build

1 similar comment
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Apr 5, 2023

/build

@wyli wyli merged commit 9b09ed6 into Project-MONAI:dev Apr 5, 2023
wyli added a commit that referenced this pull request Apr 5, 2023
wyli pushed a commit that referenced this pull request Apr 11, 2023
Fixes #6258 .

### Description

Add onnx as an option for converting pytorch models to TensorRT models
through `trt_export` API.
This pr depends on #6237 to add the onnx convert function. 

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: binliu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants