|
1 | | -#!/usr/bin/env python3 |
2 | 1 | # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
3 | 2 | # SPDX-License-Identifier: Apache-2.0 |
4 | 3 | # |
|
38 | 37 |
|
39 | 38 | import numpy as np |
40 | 39 |
|
41 | | -# Optional dependencies - gracefully handle missing packages |
42 | 40 | try: |
43 | 41 | import tensorrt as trt |
44 | 42 |
|
@@ -165,11 +163,10 @@ def __init__( |
165 | 163 | super().__init__(timing_cache_file, warmup_runs, timing_runs, plugin_libraries) |
166 | 164 | self.trtexec_path = trtexec_path |
167 | 165 | self.trtexec_args = trtexec_args if trtexec_args is not None else [] |
168 | | - self._temp_dir = tempfile.mkdtemp(prefix="trtexec_benchmark_") |
169 | | - self.engine_dir = self._temp_dir |
170 | | - self.engine_path = os.path.join(self.engine_dir, "engine.trt") |
171 | | - self.temp_model_path = os.path.join(self.engine_dir, "temp_model.onnx") |
172 | | - self.logger.debug(f"Created temporary engine directory: {self.engine_dir}") |
| 166 | + self.temp_dir = tempfile.mkdtemp(prefix="trtexec_benchmark_") |
| 167 | + self.engine_path = os.path.join(self.temp_dir, "engine.trt") |
| 168 | + self.temp_model_path = os.path.join(self.temp_dir, "temp_model.onnx") |
| 169 | + self.logger.debug(f"Created temporary engine directory: {self.temp_dir}") |
173 | 170 | self.logger.debug(f"Temporary model path: {self.temp_model_path}") |
174 | 171 | self.latency_pattern = r"\[I\]\s+Latency:.*?median\s*=\s*([\d.]+)\s*ms" |
175 | 172 |
|
@@ -213,10 +210,10 @@ def __init__( |
213 | 210 |
|
214 | 211 | def __del__(self): |
215 | 212 | """Cleanup temporary directory.""" |
216 | | - if hasattr(self, "_temp_dir"): |
| 213 | + if hasattr(self, "temp_dir"): |
217 | 214 | try: |
218 | | - shutil.rmtree(self._temp_dir, ignore_errors=True) |
219 | | - self.logger.debug(f"Cleaned up temporary directory: {self._temp_dir}") |
| 215 | + shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 216 | + self.logger.debug(f"Cleaned up temporary directory: {self.temp_dir}") |
220 | 217 | except Exception as e: |
221 | 218 | self.logger.warning(f"Failed to cleanup temporary directory: {e}") |
222 | 219 |
|
@@ -344,13 +341,8 @@ def __init__( |
344 | 341 |
|
345 | 342 | self.network_flags = 1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH) |
346 | 343 | self.network_flags |= 1 << int(trt.NetworkDefinitionCreationFlag.STRONGLY_TYPED) |
347 | | - |
348 | | - # Load timing cache from disk or create new one |
349 | 344 | self._timing_cache = None |
350 | 345 | self._load_timing_cache() |
351 | | - |
352 | | - # Storage for user-defined shape configurations |
353 | | - # Format: {input_name: (min_shape, opt_shape, max_shape)} |
354 | 346 | self._shape_configs = {} |
355 | 347 |
|
356 | 348 | def _load_plugin_libraries(self): |
@@ -600,9 +592,8 @@ def run( |
600 | 592 | min_latency = float(np.min(latencies)) |
601 | 593 | max_latency = float(np.max(latencies)) |
602 | 594 |
|
603 | | - self.logger.info("TensorRT Python API benchmark:") |
604 | 595 | self.logger.info( |
605 | | - f" min={min_latency:.3f}ms, max={max_latency:.3f}ms, " |
| 596 | + f"TensorRT Python API benchmark: min={min_latency:.3f}ms, max={max_latency:.3f}ms, " |
606 | 597 | f"mean={mean_latency:.3f}ms, std={std_latency:.3f}ms, median={median_latency:.3f}ms" |
607 | 598 | ) |
608 | 599 |
|
@@ -639,33 +630,21 @@ def run( |
639 | 630 | return float("inf") |
640 | 631 | finally: |
641 | 632 | try: |
| 633 | + [inp["device"].free() for inp in inputs if "device" in inp] |
| 634 | + [out["device"].free() for out in outputs if "device" in out] |
642 | 635 | for inp in inputs: |
643 | | - if "device" in inp: |
644 | | - inp["device"].free() |
645 | 636 | if "host" in inp: |
646 | 637 | del inp["host"] |
647 | 638 | for out in outputs: |
648 | | - if "device" in out: |
649 | | - out["device"].free() |
650 | 639 | if "host" in out: |
651 | 640 | del out["host"] |
652 | 641 | inputs.clear() |
653 | 642 | outputs.clear() |
654 | | - |
655 | | - if context is not None: |
656 | | - del context |
657 | | - if stream is not None: |
658 | | - del stream |
659 | | - if engine is not None: |
660 | | - del engine |
661 | | - if serialized_engine is not None: |
662 | | - del serialized_engine |
663 | | - if parser is not None: |
664 | | - del parser |
665 | | - if network is not None: |
666 | | - del network |
667 | | - if config is not None: |
668 | | - del config |
| 643 | + resources = [context, stream, engine, serialized_engine, parser, network, config] |
| 644 | + for resource in resources: |
| 645 | + if resource is not None: |
| 646 | + del resource |
| 647 | + resources.clear() |
669 | 648 | except Exception as cleanup_error: |
670 | 649 | self.logger.warning(f"Error during cleanup: {cleanup_error}") |
671 | 650 |
|
|
0 commit comments