Skip to content

Commit 26f8621

Browse files
committed
feat: trtexec options dedup
1 parent 82f1e70 commit 26f8621

2 files changed

Lines changed: 67 additions & 10 deletions

File tree

modelopt/onnx/quantization/autotune/benchmark.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,48 @@ def _write_log_file(self, file: Path | str | None, content: str) -> None:
144144
except Exception as e:
145145
self.logger.warning(f"Failed to save logs to {file}: {e}")
146146

147+
_SUBGRAPH_SAFE_FLAGS = frozenset({
148+
"stronglyTyped", "maxTactics", "sparsity", "maxAuxStreams",
149+
"builderOptimizationLevel",
150+
"fp16", "int8", "fp8", "best", "noTF32",
151+
"useCudaGraph", "useSpinWait", "profilingVerbosity", "verbose",
152+
"separateProfileRun", "noDataTransfers", "dumpProfile", "dumpLayerInfo",
153+
"avgRuns", "iterations", "warmUp", "duration",
154+
"saveEngine", "timingCacheFile",
155+
"staticPlugins", "dynamicPlugins",
156+
"noMyelinFusion", "workspace", "memPoolSize",
157+
"safe",
158+
})
159+
160+
161+
def _extract_flag_name(arg: str) -> str:
162+
"""Extract the flag name from a trtexec argument like '--maxTactics=2000'."""
163+
return arg.lstrip("-").split("=", 1)[0]
164+
165+
166+
def _filter_subgraph_safe_args(cmd: list[str]) -> list[str]:
167+
"""Keep only subgraph-safe args from a trtexec command list.
168+
169+
Shape args (--optShapes, --minShapes, --maxShapes) are stripped here because
170+
they are rebuilt per-subgraph by the caller. All other model-specific args
171+
(--loadInputs, --exportOutput, --shapes, etc.) are also excluded.
172+
"""
173+
filtered: list[str] = []
174+
skip_next = False
175+
for c in cmd:
176+
if skip_next:
177+
skip_next = False
178+
continue
179+
if not c.startswith("-"):
180+
filtered.append(c)
181+
continue
182+
flag = _extract_flag_name(c)
183+
if flag in _SUBGRAPH_SAFE_FLAGS:
184+
filtered.append(c)
185+
elif "=" not in c:
186+
skip_next = True
187+
return filtered
188+
147189

148190
def _dedup_trtexec_args(
149191
base_args: list[tuple[str, str | None]], user_args: list[str]
@@ -323,10 +365,11 @@ def run(
323365
cmd = list(self._base_cmd)
324366

325367
if strip_shape_args:
326-
cmd = [
327-
c for c in cmd
328-
if not any(c.startswith(p) for p in ("--optShapes", "--minShapes", "--maxShapes"))
329-
]
368+
before = cmd[:]
369+
cmd = _filter_subgraph_safe_args(cmd)
370+
removed = set(before) - set(cmd)
371+
if removed:
372+
self.logger.debug(f"Subgraph filter removed: {removed}")
330373

331374
cmd.append(f"--onnx={model_path}")
332375

modelopt/onnx/quantization/autotune/subgraph_workflow.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -929,15 +929,29 @@ def subgraph_autotuning_workflow(
929929
skipped += 1
930930
continue
931931

932-
# Build per-subgraph shape args
932+
# Build per-subgraph shape args only if the subgraph has dynamic inputs
933933
subgraph_extra_args = None
934934
if has_dynamic_shapes:
935-
subgraph_extra_args = _build_subgraph_shape_args(
936-
group.input_tensors,
937-
min_tensor_shapes, opt_tensor_shapes, max_tensor_shapes,
935+
sub_model_proto = onnx.load_from_string(sub_bytes)
936+
has_dynamic_inputs = any(
937+
any(
938+
(not dim.dim_value and dim.dim_value != 0)
939+
for dim in inp.type.tensor_type.shape.dim
940+
)
941+
for inp in sub_model_proto.graph.input
942+
if inp.type.HasField("tensor_type")
943+
and inp.type.tensor_type.HasField("shape")
938944
)
939-
if subgraph_extra_args:
940-
logger.debug(f" Subgraph shape args: {subgraph_extra_args}")
945+
if has_dynamic_inputs:
946+
subgraph_extra_args = _build_subgraph_shape_args(
947+
group.input_tensors,
948+
min_tensor_shapes, opt_tensor_shapes, max_tensor_shapes,
949+
)
950+
if subgraph_extra_args:
951+
logger.debug(f" Subgraph shape args: {subgraph_extra_args}")
952+
else:
953+
logger.debug(" Subgraph has static inputs, skipping shape args")
954+
del sub_model_proto
941955

942956
# Generate heuristic schemes
943957
schemes = generate_heuristic_schemes(graph, group)

0 commit comments

Comments
 (0)