-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description Added alternative class to ONNX->TRT export and wrap TRT engines for inference. It encapsulates filesystem persistence and does not rely on torch-tensortrt for execution. Also can be used to run ONNX with onnxruntime. ### 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: Boris Fomitchev <[email protected]> Signed-off-by: Yiheng Wang <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: YunLiu <[email protected]> Co-authored-by: Yiheng Wang <[email protected]> Co-authored-by: Yiheng Wang <[email protected]> Co-authored-by: binliunls <[email protected]>
- Loading branch information
1 parent
fa1ef8b
commit c9f8d32
Showing
17 changed files
with
1,121 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from monai.config import IgniteInfo | ||
from monai.networks import trt_compile | ||
from monai.utils import min_version, optional_import | ||
|
||
Events, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Events") | ||
if TYPE_CHECKING: | ||
from ignite.engine import Engine | ||
else: | ||
Engine, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Engine") | ||
|
||
|
||
class TrtHandler: | ||
""" | ||
TrtHandler acts as an Ignite handler to apply TRT acceleration to the model. | ||
Usage example:: | ||
handler = TrtHandler(model=model, base_path="/test/checkpoint.pt", args={"precision": "fp16"}) | ||
handler.attach(engine) | ||
engine.run() | ||
""" | ||
|
||
def __init__(self, model, base_path, args=None, submodule=None): | ||
""" | ||
Args: | ||
base_path: TRT path basename. TRT plan(s) saved to "base_path[.submodule].plan" | ||
args: passed to trt_compile(). See trt_compile() for details. | ||
submodule : Hierarchical ids of submodules to convert, e.g. 'image_decoder.decoder' | ||
""" | ||
self.model = model | ||
self.base_path = base_path | ||
self.args = args | ||
self.submodule = submodule | ||
|
||
def attach(self, engine: Engine) -> None: | ||
""" | ||
Args: | ||
engine: Ignite Engine, it can be a trainer, validator or evaluator. | ||
""" | ||
self.logger = engine.logger | ||
engine.add_event_handler(Events.STARTED, self) | ||
|
||
def __call__(self, engine: Engine) -> None: | ||
""" | ||
Args: | ||
engine: Ignite Engine, it can be a trainer, validator or evaluator. | ||
""" | ||
trt_compile(self.model, self.base_path, args=self.args, submodule=self.submodule, logger=self.logger) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.