|
| 1 | +# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES |
| 2 | +# Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +# SPDX-License-Identifier: Apache-2.0 |
| 17 | + |
| 18 | +import os |
| 19 | +import subprocess |
| 20 | + |
| 21 | + |
| 22 | +# Model and engine configuration. |
| 23 | +MESH_FILE_NAME = 'textured_simple.obj' |
| 24 | + |
| 25 | +REFINE_MODEL_NAME = 'dummy_refine_model.onnx' |
| 26 | +REFINE_ENGINE_NAME = 'dummy_refine_trt_engine.plan' |
| 27 | +SCORE_MODEL_NAME = 'dummy_score_model.onnx' |
| 28 | +SCORE_ENGINE_NAME = 'dummy_score_trt_engine.plan' |
| 29 | + |
| 30 | +REFINE_ENGINE_PATH = '/tmp/' + REFINE_ENGINE_NAME |
| 31 | +SCORE_ENGINE_PATH = '/tmp/' + SCORE_ENGINE_NAME |
| 32 | + |
| 33 | + |
| 34 | +def generate_tensorrt_engine(engine_path, model_name, trtexec_args): |
| 35 | + """ |
| 36 | + Generate TensorRT engine file from ONNX model using trtexec. |
| 37 | +
|
| 38 | + Parameters |
| 39 | + ---------- |
| 40 | + engine_path : str |
| 41 | + Path where the engine file will be saved |
| 42 | + model_name : str |
| 43 | + Name of the model for logging purposes |
| 44 | + trtexec_args : list |
| 45 | + List of trtexec command arguments |
| 46 | +
|
| 47 | + """ |
| 48 | + if not os.path.isfile(engine_path): |
| 49 | + print(f'Generating an engine file for the {model_name} model...') |
| 50 | + # Prepend 'trtexec' to the arguments list. |
| 51 | + cmd = ['/usr/src/tensorrt/bin/trtexec'] + trtexec_args |
| 52 | + print('Generating model engine file by command: ', ' '.join(cmd)) |
| 53 | + result = subprocess.run( |
| 54 | + cmd, |
| 55 | + env=os.environ, |
| 56 | + stdout=subprocess.PIPE, |
| 57 | + stderr=subprocess.PIPE |
| 58 | + ) |
| 59 | + if result.returncode != 0: |
| 60 | + raise Exception( |
| 61 | + f'Failed to convert with status: {result.returncode}.\n' |
| 62 | + f'stderr:\n' + result.stderr.decode('utf-8') |
| 63 | + ) |
| 64 | + print(f'{model_name} model engine file generation was finished') |
| 65 | + |
| 66 | + |
| 67 | +def generate_foundationpose_engines(): |
| 68 | + """ |
| 69 | + Generate both Refine and Score TensorRT engine files for FoundationPose tests. |
| 70 | +
|
| 71 | + This function should be called at the beginning of generate_test_description() |
| 72 | + in FoundationPose test files. |
| 73 | + """ |
| 74 | + # Get correct model paths (relative to test directory). |
| 75 | + base_path = os.path.dirname(__file__) |
| 76 | + refine_model_path = os.path.join(base_path, '../../test/models', REFINE_MODEL_NAME) |
| 77 | + score_model_path = os.path.join(base_path, '../../test/models', SCORE_MODEL_NAME) |
| 78 | + |
| 79 | + # Generate Refine engine. |
| 80 | + refine_trtexec_args = [ |
| 81 | + f'--onnx={refine_model_path}', |
| 82 | + f'--saveEngine={REFINE_ENGINE_PATH}', |
| 83 | + '--minShapes=input1:1x160x160x6,input2:1x160x160x6', |
| 84 | + '--optShapes=input1:1x160x160x6,input2:1x160x160x6', |
| 85 | + '--maxShapes=input1:42x160x160x6,input2:42x160x160x6', |
| 86 | + '--fp16', |
| 87 | + '--skipInference', |
| 88 | + ] |
| 89 | + generate_tensorrt_engine(REFINE_ENGINE_PATH, 'Refine', refine_trtexec_args) |
| 90 | + |
| 91 | + # Generate Score engine. |
| 92 | + score_trtexec_args = [ |
| 93 | + f'--onnx={score_model_path}', |
| 94 | + f'--saveEngine={SCORE_ENGINE_PATH}', |
| 95 | + '--fp16', |
| 96 | + '--minShapes=input1:1x160x160x6,input2:1x160x160x6', |
| 97 | + '--optShapes=input1:1x160x160x6,input2:1x160x160x6', |
| 98 | + '--maxShapes=input1:252x160x160x6,input2:252x160x160x6', |
| 99 | + '--skipInference', |
| 100 | + ] |
| 101 | + generate_tensorrt_engine(SCORE_ENGINE_PATH, 'Score', score_trtexec_args) |
| 102 | + |
| 103 | + |
| 104 | +def get_engines(): |
| 105 | + """ |
| 106 | + Get model and engine names and paths for FoundationPose tests. |
| 107 | +
|
| 108 | + Returns |
| 109 | + ------- |
| 110 | + dict |
| 111 | + Dictionary containing model and engine names and paths |
| 112 | +
|
| 113 | + """ |
| 114 | + # Calculate correct model paths (relative to test directory). |
| 115 | + base_path = os.path.dirname(__file__) |
| 116 | + refine_model_path = os.path.join(base_path, '../../test/models', REFINE_MODEL_NAME) |
| 117 | + score_model_path = os.path.join(base_path, '../../test/models', SCORE_MODEL_NAME) |
| 118 | + |
| 119 | + return { |
| 120 | + 'refine_model_name': REFINE_MODEL_NAME, |
| 121 | + 'refine_engine_name': REFINE_ENGINE_NAME, |
| 122 | + 'score_model_name': SCORE_MODEL_NAME, |
| 123 | + 'score_engine_name': SCORE_ENGINE_NAME, |
| 124 | + 'refine_model_path': refine_model_path, |
| 125 | + 'refine_engine_path': REFINE_ENGINE_PATH, |
| 126 | + 'score_model_path': score_model_path, |
| 127 | + 'score_engine_path': SCORE_ENGINE_PATH, |
| 128 | + } |
0 commit comments