-
Notifications
You must be signed in to change notification settings - Fork 534
fix(autotune): pre-check remote board connectivity before benchmark #2078
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,8 +31,10 @@ | |
| import os | ||
| import re | ||
| import shutil | ||
| import socket | ||
| import tempfile | ||
| import time | ||
| import urllib.parse | ||
| from abc import ABC, abstractmethod | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
@@ -41,6 +43,7 @@ | |
| import torch | ||
|
|
||
| from modelopt.onnx.logging_config import logger | ||
| from modelopt.onnx.quantization.autotune.common import RemoteConnectionError | ||
| from modelopt.onnx.quantization.ort_utils import _check_for_trtexec, _run_trtexec | ||
|
|
||
| TRT_AVAILABLE = importlib.util.find_spec("tensorrt") is not None | ||
|
|
@@ -50,6 +53,71 @@ | |
| TORCH_CUDA_AVAILABLE = torch.cuda.is_available() | ||
|
|
||
|
|
||
| _DEFAULT_PORTS = {"ssh": 22, "http": 80, "https": 443} | ||
|
|
||
|
|
||
| def _check_remote_connectivity(trtexec_args: list[str], retries: int = 3) -> None: | ||
| """Test TCP connectivity to the remote autotuning board before running trtexec. | ||
|
|
||
| Scans trtexec_args for --remoteAutoTuningConfig, parses the URI to extract | ||
| hostname and port, then attempts a TCP connection with a 5-second timeout. | ||
| Retries up to `retries` times before raising an error. | ||
|
|
||
| Args: | ||
| trtexec_args: List of trtexec command-line arguments. | ||
| retries: Number of connection attempts before giving up (default: 3). | ||
|
|
||
| Raises: | ||
| RemoteConnectionError: If the remote board is unreachable after all retries. | ||
| """ | ||
| config_value = None | ||
| for i, arg in enumerate(trtexec_args): | ||
| if arg.startswith("--remoteAutoTuningConfig="): | ||
| config_value = arg.split("=", 1)[1] | ||
| break | ||
| elif arg == "--remoteAutoTuningConfig" and i + 1 < len(trtexec_args): | ||
| config_value = trtexec_args[i + 1] | ||
| break | ||
|
|
||
| if config_value is None: | ||
| return | ||
|
|
||
| parsed = urllib.parse.urlparse(config_value) | ||
| hostname = parsed.hostname | ||
| if not hostname: | ||
| return | ||
|
|
||
| port = parsed.port | ||
| if port is None: | ||
| port = _DEFAULT_PORTS.get(parsed.scheme, 22) | ||
|
Comment on lines
+85
to
+92
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. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Normalize and validate the remote URI. The documented CLI command reaches this code with quotes in the Based on supplied workflow and documentation context, 🤖 Prompt for AI Agents |
||
|
|
||
| last_error = _try_connect(hostname, port, retries) | ||
| if last_error is not None: | ||
| raise RemoteConnectionError( | ||
| f"Cannot reach remote autotuning board at {hostname}:{port} after {retries} attempts - " | ||
| f"{last_error}. Exiting to avoid marking schemes as errors in state file." | ||
| ) from last_error | ||
|
|
||
|
|
||
| def _try_connect(hostname: str, port: int, retries: int) -> Exception | None: | ||
| """Attempt TCP connection with retries. Returns None on success, last error on failure.""" | ||
| last_error = None | ||
| for attempt in range(1, retries + 1): | ||
| try: | ||
| conn = socket.create_connection((hostname, port), timeout=5) | ||
| conn.close() | ||
| return None | ||
| except (TimeoutError, OSError) as e: # noqa: PERF203 | ||
| last_error = e | ||
| if attempt < retries: | ||
| logger.warning( | ||
| f"Remote board connection attempt {attempt}/{retries} failed " | ||
| f"({hostname}:{port}): {e}. Retrying..." | ||
| ) | ||
| time.sleep(2) | ||
| return last_error | ||
|
|
||
|
|
||
| def _validate_shape_range(min_shape: list, opt_shape: list, max_shape: list) -> None: | ||
| """Raise ValueError if shape lengths differ or if min <= opt <= max fails at any dimension.""" | ||
| if len(min_shape) != len(opt_shape) or len(opt_shape) != len(max_shape): | ||
|
|
@@ -159,6 +227,7 @@ def __init__( | |
| timing_runs: int = 10, | ||
| plugin_libraries: list[str] | None = None, | ||
| trtexec_args: list[str] | None = None, | ||
| remote_connection_retries: int = 3, | ||
| ): | ||
| """Initialize the trtexec benchmark. | ||
|
|
||
|
|
@@ -170,8 +239,12 @@ def __init__( | |
| trtexec_args: Additional command-line arguments to pass to trtexec. | ||
| These are appended after the standard arguments. | ||
| Example: ['--fp16', '--workspace=4096', '--verbose'] | ||
| remote_connection_retries: Number of TCP connection attempts to the remote | ||
| board before giving up (default: 3). Only used when | ||
| --remoteAutoTuningConfig is present in trtexec_args. | ||
| """ | ||
| super().__init__(timing_cache_file, warmup_runs, timing_runs, plugin_libraries) | ||
| self._remote_connection_retries = remote_connection_retries | ||
| self.trtexec_args = trtexec_args if trtexec_args is not None else [] | ||
| self.temp_dir = tempfile.mkdtemp(prefix="trtexec_benchmark_") | ||
| self.engine_path = os.path.join(self.temp_dir, "engine.trt") | ||
|
|
@@ -253,6 +326,8 @@ def run( | |
| if not os.path.exists(self.timing_cache_file): | ||
| self.logger.debug(f"Will create timing cache: {self.timing_cache_file}") | ||
|
|
||
| _check_remote_connectivity(self._base_cmd, retries=self._remote_connection_retries) | ||
|
|
||
|
Comment on lines
+329
to
+330
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. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Save state for baseline and final connectivity failures. Line 329 runs for baseline, per-scheme, and final measurements. The supplied Based on supplied workflow context, the current state-save handler surrounds only per-scheme measurements. 🤖 Prompt for AI Agents |
||
| try: | ||
| model_path = path_or_bytes | ||
| if isinstance(model_path, bytes): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.