|
28 | 28 | DEFAULT_NUM_INFERENCE_PER_RUN, |
29 | 29 | SHA_256_HASH_LENGTH, |
30 | 30 | TRT_MODE_FLAGS, |
31 | | - TRTEXEC_PATH, |
32 | 31 | WARMUP_TIME_MS, |
33 | 32 | TRTMode, |
34 | 33 | ) |
|
41 | 40 | ) |
42 | 41 |
|
43 | 42 |
|
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. |
48 | 45 |
|
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. |
50 | 52 |
|
51 | 53 | 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. |
54 | 56 |
|
55 | 57 | 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. |
59 | 59 | """ |
| 60 | + cmd = ["trtexec", *args] |
60 | 61 | logging.info(" ".join(cmd)) |
61 | 62 | 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 | + ) |
63 | 66 | p.wait() |
64 | 67 | log.seek(0) |
65 | 68 | output = log.read() |
@@ -181,7 +184,7 @@ def _build_command( |
181 | 184 | calib_cache_path: Path | None = None, |
182 | 185 | timing_cache_path: Path | None = None, |
183 | 186 | ) -> list[str]: |
184 | | - cmd = [TRTEXEC_PATH, f"--onnx={onnx_path}"] |
| 187 | + cmd = [f"--onnx={onnx_path}"] |
185 | 188 | cmd.extend(TRT_MODE_FLAGS[trt_mode]) |
186 | 189 |
|
187 | 190 | if trt_mode == TRTMode.INT8 and calib_cache and calib_cache_path: |
@@ -235,7 +238,7 @@ def _setup_files_and_paths( |
235 | 238 | cmd = _build_command(onnx_path, engine_path, calib_cache_path, timing_cache_path) |
236 | 239 |
|
237 | 240 | try: |
238 | | - ret_code, out = _run_command(cmd) |
| 241 | + ret_code, out = _run_trtexec_streamed(cmd) |
239 | 242 | if ret_code != 0: |
240 | 243 | return None, out |
241 | 244 |
|
@@ -284,7 +287,7 @@ def profile_engine( |
284 | 287 | """ |
285 | 288 |
|
286 | 289 | 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}"] |
288 | 291 | cmd += _get_profiling_params(profiling_runs) |
289 | 292 |
|
290 | 293 | if enable_layerwise_profiling: |
@@ -320,7 +323,7 @@ def _setup_files_and_paths(tmp_dir_path: Path, engine_hash: str) -> tuple[Path, |
320 | 323 | cmd = _build_command(engine_path, profile_path, layer_info_path) |
321 | 324 |
|
322 | 325 | try: |
323 | | - ret_code, out = _run_command(cmd) |
| 326 | + ret_code, out = _run_trtexec_streamed(cmd) |
324 | 327 | if ret_code != 0: |
325 | 328 | return None, out |
326 | 329 |
|
|
0 commit comments