Skip to content

Commit 14ca64a

Browse files
committed
Moved 'trtexec' into function
1 parent 01cc557 commit 14ca64a

4 files changed

Lines changed: 32 additions & 27 deletions

File tree

modelopt/onnx/quantization/autotune/benchmark.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ def __init__(
185185
self.latency_pattern = r"\[I\]\s+Latency:.*?median\s*=\s*([\d.]+)\s*ms"
186186

187187
self._base_cmd = [
188-
self.trtexec_path,
189188
f"--avgRuns={self.timing_runs}",
190189
f"--iterations={self.timing_runs}",
191190
f"--warmUp={self.warmup_runs}",

modelopt/onnx/quantization/ort_utils.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@ def _check_lib_in_ld_library_path(ld_library_path, lib_pattern):
4444
return False, None
4545

4646

47-
def _run_trtexec(cmd, timeout=None):
48-
"""Run a 'trtexec' command via subprocess."""
49-
# Ensure that this command is a trtexec run
50-
assert any("trtexec" in c for c in cmd), "Subprocess can only execute 'trtexec' commands"
47+
def _run_trtexec(
48+
args: list[str] | None = None, timeout: float | None = None
49+
) -> subprocess.CompletedProcess:
50+
"""Run a 'trtexec' command via subprocess.
5151
52-
# Run trtexec command
52+
Args:
53+
args: Arguments to pass to trtexec (without the 'trtexec' command itself).
54+
timeout: Optional subprocess timeout in seconds.
55+
56+
Returns:
57+
The completed subprocess result.
58+
"""
59+
cmd = ["trtexec", *(args or [])]
5360
return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout) # nosec B603
5461

5562

@@ -96,7 +103,7 @@ def _parse_version_from_string(version_str: str) -> str | None:
96103
)
97104

98105
try:
99-
result = _run_trtexec([trtexec_path], timeout=5)
106+
result = _run_trtexec(timeout=5)
100107
banner_output = result.stdout + result.stderr
101108
parsed_version = _parse_version_from_string(banner_output)
102109

modelopt/torch/_deploy/_runtime/tensorrt/constants.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
ONE_GIBI_IN_BYTES = 1 << 30
3333

3434
# TensorRT conversion tool names
35-
TRTEXEC = "trtexec"
36-
37-
# trtexec path within docker
38-
TRTEXEC_PATH = "trtexec"
3935
DEFAULT_ARTIFACT_DIR = "modelopt_build/trt_artifacts"
4036

4137
# Default conversion params

modelopt/torch/_deploy/_runtime/tensorrt/engine_builder.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
DEFAULT_NUM_INFERENCE_PER_RUN,
2929
SHA_256_HASH_LENGTH,
3030
TRT_MODE_FLAGS,
31-
TRTEXEC_PATH,
3231
WARMUP_TIME_MS,
3332
TRTMode,
3433
)
@@ -41,25 +40,29 @@
4140
)
4241

4342

44-
# TODO: Get rid of this function or get approval for `# nosec` usage if we want to include this
45-
# as a non-compiled python file in the release.
46-
def _run_command(cmd: list[str], cwd: Path | None = None) -> tuple[int, bytes]:
47-
"""Util function to execute a command.
43+
def _run_trtexec_streamed(args: list[str], cwd: Path | None = None) -> tuple[int, bytes]:
44+
"""Run a 'trtexec' command via subprocess, streaming stdout/stderr to a temp file.
4845
49-
This util will not direct stdout and stderr to console if the cmd succeeds.
46+
The 'trtexec' binary is hardcoded as the executable; only its arguments may be supplied
47+
by the caller. This restricts the function to trtexec invocations.
48+
49+
Output handling: stdout and stderr are captured to a temp file and returned as bytes.
50+
On failure (non-zero returncode), the captured output is also logged at ERROR level;
51+
on success, this function emits nothing to the console.
5052
5153
Args:
52-
cmd: the command line list
53-
cwd: current working directory
54+
args: Arguments to pass to trtexec (without the 'trtexec' command itself).
55+
cwd: Optional working directory for the subprocess.
5456
5557
Returns:
56-
return code: 0 means successful, otherwise means failed
57-
log_string: the stdout and stderr output as a string
58-
58+
A tuple of (returncode, output) where output is the combined stdout/stderr bytes.
5959
"""
60+
cmd = ["trtexec", *args]
6061
logging.info(" ".join(cmd))
6162
with NamedTemporaryFile("w+b") as log:
62-
p = subprocess.Popen(cmd, stdout=log, stderr=log, cwd=str(cwd) if cwd else None) # nosec
63+
p = subprocess.Popen( # nosec B603 - cmd[0] is hardcoded "trtexec"
64+
cmd, stdout=log, stderr=log, cwd=str(cwd) if cwd else None
65+
)
6366
p.wait()
6467
log.seek(0)
6568
output = log.read()
@@ -181,7 +184,7 @@ def _build_command(
181184
calib_cache_path: Path | None = None,
182185
timing_cache_path: Path | None = None,
183186
) -> list[str]:
184-
cmd = [TRTEXEC_PATH, f"--onnx={onnx_path}"]
187+
cmd = [f"--onnx={onnx_path}"]
185188
cmd.extend(TRT_MODE_FLAGS[trt_mode])
186189

187190
if trt_mode == TRTMode.INT8 and calib_cache and calib_cache_path:
@@ -235,7 +238,7 @@ def _setup_files_and_paths(
235238
cmd = _build_command(onnx_path, engine_path, calib_cache_path, timing_cache_path)
236239

237240
try:
238-
ret_code, out = _run_command(cmd)
241+
ret_code, out = _run_trtexec_streamed(cmd)
239242
if ret_code != 0:
240243
return None, out
241244

@@ -284,7 +287,7 @@ def profile_engine(
284287
"""
285288

286289
def _build_command(engine_path: Path, profile_path: Path, layer_info_path: Path) -> list[str]:
287-
cmd = [TRTEXEC_PATH, f"--loadEngine={engine_path}"]
290+
cmd = [f"--loadEngine={engine_path}"]
288291
cmd += _get_profiling_params(profiling_runs)
289292

290293
if enable_layerwise_profiling:
@@ -320,7 +323,7 @@ def _setup_files_and_paths(tmp_dir_path: Path, engine_hash: str) -> tuple[Path,
320323
cmd = _build_command(engine_path, profile_path, layer_info_path)
321324

322325
try:
323-
ret_code, out = _run_command(cmd)
326+
ret_code, out = _run_trtexec_streamed(cmd)
324327
if ret_code != 0:
325328
return None, out
326329

0 commit comments

Comments
 (0)