-
Notifications
You must be signed in to change notification settings - Fork 375
Centralize 'trtexec' subprocess runs in ONNX into a single function #1268
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
kevalmorabia97 marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,6 @@ | |
| DEFAULT_NUM_INFERENCE_PER_RUN, | ||
| SHA_256_HASH_LENGTH, | ||
| TRT_MODE_FLAGS, | ||
| TRTEXEC_PATH, | ||
| WARMUP_TIME_MS, | ||
| TRTMode, | ||
| ) | ||
|
|
@@ -41,25 +40,29 @@ | |
| ) | ||
|
|
||
|
|
||
| # TODO: Get rid of this function or get approval for `# nosec` usage if we want to include this | ||
| # as a non-compiled python file in the release. | ||
| def _run_command(cmd: list[str], cwd: Path | None = None) -> tuple[int, bytes]: | ||
| """Util function to execute a command. | ||
| def _run_trtexec_streamed(args: list[str], cwd: Path | None = None) -> tuple[int, bytes]: | ||
|
kevalmorabia97 marked this conversation as resolved.
|
||
| """Run a 'trtexec' command via subprocess, streaming stdout/stderr to a temp file. | ||
|
|
||
| This util will not direct stdout and stderr to console if the cmd succeeds. | ||
| The 'trtexec' binary is hardcoded as the executable; only its arguments may be supplied | ||
| by the caller. This restricts the function to trtexec invocations. | ||
|
|
||
| Output handling: stdout and stderr are captured to a temp file and returned as bytes. | ||
| On failure (non-zero returncode), the captured output is also logged at ERROR level; | ||
| on success, this function emits nothing to the console. | ||
|
|
||
| Args: | ||
| cmd: the command line list | ||
| cwd: current working directory | ||
| args: Arguments to pass to trtexec (without the 'trtexec' command itself). | ||
| cwd: Optional working directory for the subprocess. | ||
|
|
||
| Returns: | ||
| return code: 0 means successful, otherwise means failed | ||
| log_string: the stdout and stderr output as a string | ||
|
|
||
| A tuple of (returncode, output) where output is the combined stdout/stderr bytes. | ||
| """ | ||
| cmd = ["trtexec", *args] | ||
| logging.info(" ".join(cmd)) | ||
| with NamedTemporaryFile("w+b") as log: | ||
| p = subprocess.Popen(cmd, stdout=log, stderr=log, cwd=str(cwd) if cwd else None) # nosec | ||
| p = subprocess.Popen( # nosec B603 - cmd[0] is hardcoded "trtexec" | ||
| cmd, stdout=log, stderr=log, cwd=str(cwd) if cwd else None | ||
| ) | ||
| p.wait() | ||
|
Comment on lines
+43
to
66
Contributor
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 streamed wrapper still bypasses Bandit and can hang indefinitely.
Possible direction-def _run_trtexec_streamed(args: list[str], cwd: Path | None = None) -> tuple[int, bytes]:
+def _run_trtexec_streamed(
+ args: list[str],
+ cwd: Path | None = None,
+ timeout: float = 300.0,
+) -> tuple[int, bytes]:
@@
- p = subprocess.Popen( # nosec B603 - cmd[0] is hardcoded "trtexec"
- cmd, stdout=log, stderr=log, cwd=str(cwd) if cwd else None
- )
- p.wait()
+ p = subprocess.Popen(cmd, stdout=log, stderr=log, cwd=str(cwd) if cwd else None)
+ try:
+ p.wait(timeout=timeout)
+ except subprocess.TimeoutExpired:
+ p.kill()
+ p.wait()
+ raiseAs per coding guidelines, "Bandit security checks must pass without exceptions. 🤖 Prompt for AI Agents |
||
| log.seek(0) | ||
| output = log.read() | ||
|
|
@@ -181,7 +184,7 @@ def _build_command( | |
| calib_cache_path: Path | None = None, | ||
| timing_cache_path: Path | None = None, | ||
| ) -> list[str]: | ||
| cmd = [TRTEXEC_PATH, f"--onnx={onnx_path}"] | ||
| cmd = [f"--onnx={onnx_path}"] | ||
| cmd.extend(TRT_MODE_FLAGS[trt_mode]) | ||
|
|
||
| if trt_mode == TRTMode.INT8 and calib_cache and calib_cache_path: | ||
|
|
@@ -235,7 +238,7 @@ def _setup_files_and_paths( | |
| cmd = _build_command(onnx_path, engine_path, calib_cache_path, timing_cache_path) | ||
|
|
||
| try: | ||
| ret_code, out = _run_command(cmd) | ||
| ret_code, out = _run_trtexec_streamed(cmd) | ||
| if ret_code != 0: | ||
| return None, out | ||
|
|
||
|
|
@@ -284,7 +287,7 @@ def profile_engine( | |
| """ | ||
|
|
||
| def _build_command(engine_path: Path, profile_path: Path, layer_info_path: Path) -> list[str]: | ||
| cmd = [TRTEXEC_PATH, f"--loadEngine={engine_path}"] | ||
| cmd = [f"--loadEngine={engine_path}"] | ||
| cmd += _get_profiling_params(profiling_runs) | ||
|
|
||
| if enable_layerwise_profiling: | ||
|
|
@@ -320,7 +323,7 @@ def _setup_files_and_paths(tmp_dir_path: Path, engine_hash: str) -> tuple[Path, | |
| cmd = _build_command(engine_path, profile_path, layer_info_path) | ||
|
|
||
| try: | ||
| ret_code, out = _run_command(cmd) | ||
| ret_code, out = _run_trtexec_streamed(cmd) | ||
| if ret_code != 0: | ||
| return None, out | ||
|
|
||
|
|
||
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.
Preserve the caller-supplied
trtexec_pathwhen delegating here.TrtExecBenchmarkstill accepts and documentstrtexec_path, but_run_trtexec()hardcodes"trtexec", so this call now ignores custom binary paths entirely. That breaks existing callers using a non-PATH install and makes theFileNotFoundErrorbranch misleading.Possible direction
Also applies to: 270-270
🤖 Prompt for AI Agents