Repository navigation
Add in-tree isaac_deploy_trt and optimize_graph TensorRT rewrite. #16
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
base: main
Are you sure you want to change the base?
Changes from all commits
c15ff43
a914108
560d011
cd4b889
7e5cad3
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 |
|---|---|---|
|
|
@@ -145,6 +145,28 @@ The ``models`` mapping is deterministically ordered. LEAPP guarantees: | |
| use these fields to verify that the model file has not changed since | ||
| export. | ||
|
|
||
| ``opset_version`` | ||
| Present on ONNX backends. The ONNX opset used at export. | ||
|
|
||
| ``optimization`` | ||
| Present on ONNX backends unless | ||
| ``backend_params={"annotate_tensorrt_compatible": False}``. | ||
|
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. lets not have this option. we can centralize all the optimizations in the optimize_graph api |
||
| ``true`` means every ONNX operator in the exported graph is supported | ||
| by the minimal TensorRT catalog: the intersection of every checked-in | ||
| release and platform. If ``validate_tensorrt_build`` was enabled, it | ||
| also means the installed TensorRT package parsed and built the model. | ||
| This does **not** switch the runtime ``backend``; Isaac Deploy / Triton | ||
| still load ``backend: onnx`` unless the deployer maps this flag itself. | ||
| Operators that fail the catalog are omitted from the YAML. They are | ||
| written to ``log.txt`` as ``tensorrt_blockers <model.onnx>: ...``. | ||
| Catalog ``true`` is not a guarantee that TensorRT can build engines | ||
| with dynamic shapes or shape-tensor inputs without optimization profiles. | ||
|
|
||
| ``tensorrt_version`` | ||
| Written next to ``optimization``. ``minimal`` means validation and | ||
| optimize used the intersection of every checked-in TensorRT release. | ||
| A release such as ``10.16`` means that catalog was selected. | ||
|
|
||
| ``pipeline`` | ||
| ------------ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,3 +176,46 @@ Disable session patches | |
| LEAPP patches for the tracing session (for example NumPy and torch | ||
| interop). Use this only to test whether those patches are involved in | ||
| a failure. | ||
|
|
||
| TensorRT catalog and optimize | ||
|
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. this deserves its own docs page. can you move make a new page called something like optimizations? keep it next to debugging. |
||
| ============================== | ||
|
|
||
| TensorRT op-catalog checks, GraphSurgeon optimizations, and DAG merge live in | ||
| the in-tree ``isaac_deploy_trt`` package (also the ``graph-surgery`` extra), | ||
| not in the LEAPP tracing CLI. After ``compile_graph()``, optimize exported | ||
| ONNX in place with ``leapp.optimize_graph(trt_compatible=True)``. That | ||
| updates YAML checksums and ``parameters.optimization``. | ||
|
|
||
| With no ``--tensorrt`` flag, ``check``, ``optimize``, ``optimize-dir``, and | ||
| ``merge-pipeline --optimize`` use the minimal catalog, | ||
| ``tensorrt_catalogs/minimal/tensorrt_layers.json``. An ONNX operator or | ||
| native layer counts as supported only when every checked-in TensorRT release | ||
| and platform marks it supported. ``leapp.optimize_graph(trt_compatible=True)`` | ||
| uses that catalog for unsupported-operator optimizations. UINT8 intermediate | ||
| Casts, Resize ``antialias``, and rank-9 Reshape edits always run. | ||
|
|
||
| ``--tensorrt`` selects one release, for example ``10.16``. ``--platform`` | ||
| (``x86``, ``orin``, ``thor``, or ``dgx``) applies only together with | ||
| ``--tensorrt``. ``--tensorrt 11.2`` with no platform uses the built-in 11.2 | ||
| parser table. ``--catalog`` replaces those flags with one JSON file. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| pip install -e ".[graph-surgery]" | ||
| isaac_deploy_trt check --onnx path/to/model.onnx --strict | ||
| isaac_deploy_trt check --onnx path/to/model.onnx --tensorrt 10.16 --platform orin --strict | ||
| isaac_deploy_trt check --onnx-dir path/to/export_dir --build --strict | ||
| isaac_deploy_trt optimize-dir path/to/export_dir -o path/to/export_dir_trt | ||
| isaac_deploy_trt optimize-dir path/to/export_dir -o path/to/export_dir_trt --tensorrt 10.16 --platform orin | ||
| isaac_deploy_trt merge-pipeline path/to/export.yaml -o pipeline.onnx --optimize | ||
|
|
||
| ``--build`` also runs TensorRT parse/build (requires ``tensorrt``). | ||
| ``--strict`` exits non-zero if catalog or TensorRT checks fail. | ||
|
|
||
| The YAML fields ``parameters.optimization`` and ``parameters.tensorrt_version`` | ||
| record that check. ``tensorrt_version`` is ``minimal`` when no release was | ||
| selected. Operators that fail the catalog are written to ``log.txt`` as | ||
| ``tensorrt_blockers <model.onnx>: ...`` and are not stored in the YAML. | ||
| Set ``backend_params={"annotate_tensorrt_compatible": False}`` to omit the | ||
| flag, or ``{"validate_tensorrt": True, "validate_tensorrt_build": True}`` | ||
| to require a TensorRT parse/build before marking ``true``. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,30 @@ | |
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| import os | ||
| import torch | ||
| from typing import Tuple | ||
| from leapp.backends.export_backend import ExportBackend, prepare_tensors_for_export, SimplifiedONNXProgram | ||
| from leapp.utils.logging import _get_logger | ||
| import os | ||
|
|
||
|
|
||
| def log_tensorrt_blockers(onnx_path: str, result) -> None: | ||
|
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. can you check if this is needed? this should go into the plugin_replace.py |
||
| """Write TensorRT catalog failures to the LEAPP log, not the YAML.""" | ||
| blockers = list(getattr(result, "unsupported_ops", []) or []) | ||
| blockers.extend( | ||
| f"unknown:{name}" for name in (getattr(result, "unknown_ops", []) or []) | ||
| ) | ||
| for error in getattr(result, "errors", []) or []: | ||
| if str(error).startswith("ONNX ops not "): | ||
| continue | ||
| blockers.append(str(error)) | ||
| if not blockers: | ||
| return | ||
| _get_logger().warning( | ||
| f"tensorrt_blockers {os.path.basename(onnx_path)}: {', '.join(blockers)}" | ||
| ) | ||
|
|
||
|
|
||
| import onnx | ||
| from onnx import AttributeProto, TensorProto, helper | ||
| from onnx import numpy_helper | ||
|
|
@@ -20,6 +39,10 @@ class ONNXExportBackend(ExportBackend): | |
| def get_backend_metadata(self): | ||
| metadata = {} | ||
| metadata['opset_version'] = getattr(self, 'opset_version', _constants.ONNX_DEFAULT_OPSET) | ||
| tensorrt_compatible = getattr(self, 'tensorrt_compatible', None) | ||
|
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. similar here. compile graph should not have tensorrt stuff for maximum export success rate and separation of concerns. We are editing the yaml anyway in the tensorrt related files so lets keep this one clean |
||
| if tensorrt_compatible is not None: | ||
| metadata['optimization'] = bool(tensorrt_compatible) | ||
| metadata['tensorrt_version'] = getattr(self, 'tensorrt_version', None) or "minimal" | ||
| return metadata | ||
|
|
||
| def get_backend_model_type(self): | ||
|
|
@@ -253,10 +276,48 @@ def save(self, save_path: str) -> Tuple[str, str, str]: | |
| _get_logger().error( | ||
| f"ONNX model validation warning (model still saved): {e}") | ||
|
|
||
| # Record the YAML ``optimization`` flag (catalog check by default). | ||
| # Set backend_params={"annotate_tensorrt_compatible": False} to omit it. | ||
| # Enable parse/build with {"validate_tensorrt": True} and optional | ||
| # {"validate_tensorrt_build": True, "validate_tensorrt_strict": True}. | ||
| self._annotate_tensorrt_compatible(onnx_path) | ||
|
|
||
| md5sum, sha256sum = self._verify_model_location_and_get_hash(onnx_path) | ||
|
|
||
| return onnx_path, md5sum, sha256sum | ||
|
|
||
| def _annotate_tensorrt_compatible(self, onnx_path: str) -> None: | ||
| annotate = self.backend_params.get('annotate_tensorrt_compatible', True) | ||
| validate = self.backend_params.get('validate_tensorrt', False) | ||
| if not annotate and not validate: | ||
| return | ||
|
|
||
| try: | ||
| from isaac_deploy_trt.validate import ( | ||
| check_onnx_ops_against_tensorrt, | ||
| validate_onnx_for_tensorrt, | ||
| ) | ||
| except ImportError: | ||
| _get_logger().warning( | ||
| "isaac_deploy_trt is not installed; skipping TensorRT " | ||
| "compatibility annotation. pip install -e ./packages/isaac_deploy_trt" | ||
| ) | ||
| return | ||
|
|
||
| if validate: | ||
| result = validate_onnx_for_tensorrt( | ||
| onnx_path, | ||
| build=self.backend_params.get('validate_tensorrt_build', False), | ||
| strict=self.backend_params.get('validate_tensorrt_strict', False), | ||
| ) | ||
| else: | ||
| result = check_onnx_ops_against_tensorrt(onnx_path) | ||
|
|
||
| log_tensorrt_blockers(onnx_path, result) | ||
| if annotate: | ||
| self.tensorrt_compatible = bool(result.ok) | ||
| self.tensorrt_version = result.tensorrt_version or "minimal" | ||
|
|
||
| def compile(self, m: torch.nn.Module = None) -> SimplifiedONNXProgram: | ||
| raise NotImplementedError( | ||
| "TorchExportBackend does not support compilation, please use torch-script or torch-trace instead") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should go into parameters, optimization should be centralized in optimize graph. when building the onnx file we should not be able to replace with tensorrt compatible ops.