-
Notifications
You must be signed in to change notification settings - Fork 688
[Refactor][tools] Add prebuild tools. #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 21 commits
da87a80
2a03ca7
8047c1b
93f2611
d90cd65
4ec8e88
3c8a85f
6f28158
3702378
b782773
bb83390
d70b803
f00fe81
71521eb
8f2e874
2da21a5
4d53c1e
34e49ac
6efe4fe
fee4c41
fa92dae
d616014
31c9b10
ace3b7d
4143279
f2e7b00
8eec447
5a59ab1
a55b214
6e74153
35d27bd
2edc101
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ __pycache__/ | |
|
|
||
| # C extensions | ||
| *.so | ||
| onnx2ncnn | ||
|
|
||
| # Distribution / packaging | ||
| .Python | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| include requirements/*.txt | ||
| include mmdeploy/backend/ncnn/*.so | ||
| include mmdeploy/backend/ncnn/*.dll | ||
| include mmdeploy/backend/ncnn/*.pyd | ||
| include mmdeploy/lib/*.so | ||
| include mmdeploy/lib/*.dll | ||
| include mmdeploy/lib/*.pyd |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
| from mmdeploy.backend.ncnn import is_available as ncnn_available | ||
| from mmdeploy.backend.onnxruntime import is_available as ort_available | ||
| from mmdeploy.backend.openvino import is_available as openvino_available | ||
| from mmdeploy.backend.pplnn import is_available as pplnn_available | ||
| from mmdeploy.backend.sdk import is_available as sdk_available | ||
| from mmdeploy.backend.tensorrt import is_available as trt_available | ||
|
|
||
| __all__ = [] | ||
| if ncnn_available(): | ||
| from .ncnn import NCNNWrapper # noqa: F401,F403 | ||
| __all__.append('NCNNWrapper') | ||
| if ort_available(): | ||
| from .onnxruntime import ORTWrapper # noqa: F401,F403 | ||
| __all__.append('ORTWrapper') | ||
| if trt_available(): | ||
| from .tensorrt import TRTWrapper # noqa: F401,F403 | ||
| __all__.append('TRTWrapper') | ||
| if pplnn_available(): | ||
| from .pplnn import PPLNNWrapper # noqa: F401,F403 | ||
| __all__.append('PPLNNWrapper') | ||
| if openvino_available(): | ||
| from .openvino import OpenVINOWrapper # noqa: F401,F403 | ||
| __all__.append('OpenVINOWrapper') | ||
| if sdk_available(): | ||
| from .sdk import SDKWrapper # noqa: F401,F403 | ||
| __all__.append('SDKWrapper') |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,9 @@ | |
|
|
||
| import onnx | ||
| import tensorrt as trt | ||
| import torch | ||
| from packaging import version | ||
|
|
||
| from mmdeploy.utils import get_root_logger | ||
| from .calib_utils import HDF5Calibrator | ||
| from .init_plugins import load_tensorrt_plugin | ||
|
|
||
|
|
||
|
|
@@ -54,8 +52,17 @@ def create_trt_engine(onnx_model: Union[str, onnx.ModelProto], | |
| >>> device_id=0) | ||
| >>> }) | ||
| """ | ||
|
|
||
| import os | ||
| old_cuda_device = os.environ.get('CUDA_DEVICE', None) | ||
| os.environ['CUDA_DEVICE'] = str(device_id) | ||
| import pycuda.autoinit # noqa:F401 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we may add pycuda to requirements
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I tested on Jetson, I only install successful with
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found that pycuda has been released not so often. Have you tried version of 2021.1? |
||
| if old_cuda_device is not None: | ||
| os.environ['CUDA_DEVICE'] = old_cuda_device | ||
| else: | ||
| os.environ.pop('CUDA_DEVICE') | ||
|
|
||
| load_tensorrt_plugin() | ||
| device = torch.device('cuda:{}'.format(device_id)) | ||
| # create builder and network | ||
| logger = trt.Logger(log_level) | ||
| builder = trt.Builder(logger) | ||
|
|
@@ -96,6 +103,7 @@ def create_trt_engine(onnx_model: Union[str, onnx.ModelProto], | |
| config.set_flag(trt.BuilderFlag.FP16) | ||
|
|
||
| if int8_mode: | ||
| from .calib_utils import HDF5Calibrator | ||
| config.set_flag(trt.BuilderFlag.INT8) | ||
| assert int8_param is not None | ||
| config.int8_calibrator = HDF5Calibrator( | ||
|
|
@@ -110,8 +118,7 @@ def create_trt_engine(onnx_model: Union[str, onnx.ModelProto], | |
| builder.int8_calibrator = config.int8_calibrator | ||
|
|
||
| # create engine | ||
| with torch.cuda.device(device): | ||
| engine = builder.build_engine(network, config) | ||
| engine = builder.build_engine(network, config) | ||
|
|
||
| assert engine is not None, 'Failed to create TensorRT engine' | ||
| return engine | ||
|
|
@@ -145,46 +152,6 @@ def load_trt_engine(path: str) -> trt.ICudaEngine: | |
| return engine | ||
|
|
||
|
|
||
| def torch_dtype_from_trt(dtype: trt.DataType) -> torch.dtype: | ||
| """Convert pytorch dtype to TensorRT dtype. | ||
|
|
||
| Args: | ||
| dtype (str.DataType): The data type in tensorrt. | ||
|
|
||
| Returns: | ||
| torch.dtype: The corresponding data type in torch. | ||
| """ | ||
|
|
||
| if dtype == trt.bool: | ||
| return torch.bool | ||
| elif dtype == trt.int8: | ||
| return torch.int8 | ||
| elif dtype == trt.int32: | ||
| return torch.int32 | ||
| elif dtype == trt.float16: | ||
| return torch.float16 | ||
| elif dtype == trt.float32: | ||
| return torch.float32 | ||
| else: | ||
| raise TypeError(f'{dtype} is not supported by torch') | ||
|
|
||
|
|
||
| def torch_device_from_trt(device: trt.TensorLocation): | ||
| """Convert pytorch device to TensorRT device. | ||
|
|
||
| Args: | ||
| device (trt.TensorLocation): The device in tensorrt. | ||
| Returns: | ||
| torch.device: The corresponding device in torch. | ||
| """ | ||
| if device == trt.TensorLocation.DEVICE: | ||
| return torch.device('cuda') | ||
| elif device == trt.TensorLocation.HOST: | ||
| return torch.device('cpu') | ||
| else: | ||
| return TypeError(f'{device} is not supported by torch') | ||
|
|
||
|
|
||
| def get_trt_log_level() -> trt.Logger.Severity: | ||
| """Get tensorrt log level from root logger. | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.