Skip to content

fix(dynamo): honor Input.profiles on the whole-graph path and document how to set opt for exported programs - #5085

Draft
shoumikhin wants to merge 4 commits into
pytorch:mainfrom
shoumikhin:opt-shape-exported-program
Draft

shoumikhin wants to merge 4 commits into
pytorch:mainfrom
shoumikhin:opt-shape-exported-program

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What is wrong today

TensorRT tunes an engine for one size in each dynamic range, the opt size. For a dynamic ExportedProgram:

  1. With require_full_compilation=True, the whole-graph path (taken when every operator converts) ignores Input.profiles, so optimization_profile(model, 1) has nothing to select. In 2.14 the same call built one profile per entry.
  2. Profiles are matched to the graph inputs by position. With kwargs in another order than at export, a tuple input before a profiled one, or a module that updates a buffer, an input can get another input's profile.
  3. A plain Input(min_shape, opt_shape, max_shape) does not set opt for an exported program. Opt comes from the export example. This is undocumented (🐛 [Bug] Input(opt_shape=...) is ignored when compiling an ExportedProgram, so decode steps run prefill-sized kernels #5076).
  4. With Dim(min=1), compile warns that the engine min will be 2, but it is 1.

What this change does

  • Both paths build one engine profile per profiles entry, matched to the inputs through the export input spec.
  • Behavior change on the whole-graph path for inputs with more than one profile. In 2.15 it built one profile covering every entry, so any size in the union ran unpinned. Now, as on the partitioned path, a call outside profile 0 must pick a profile with optimization_profile, a size that no profile covers cannot run, and different profile counts raise.
  • When a plain Input.opt_shape differs from the export example in a dynamic dimension no profile covers, and TensorRT gets part of the graph, compile logs one warning naming both ways to set opt.
  • The wrong min warning becomes a debug message.
  • The dynamic shapes guide explains both ways to set opt (export with an example of that size, or pass a single profile):
rows = torch.export.Dim("rows", min=1, max=1024)
ep = torch.export.export(model, (torch.randn(512, 64).cuda(),), dynamic_shapes=({0: rows},))
spec = torch_tensorrt.Input(
    profiles=[{"min_shape": (1, 64), "opt_shape": (1, 64), "max_shape": (1024, 64)}]
)
trt_gm = torch_tensorrt.dynamo.compile(ep, inputs=[spec])  # tuned for 1 row
  • save_dynamic_shapes_example.py now exports at its opt size.

Tests

  • New: whole-graph engines with one and two profiles (read back, run, match eager), mismatched counts raise, profiles follow reordered kwargs and tuple inputs on both paths, and kwargs next to an updated buffer, the opt warning cases, a short opt_shape.
  • Ran the dynamic shape, multi-profile, partitioning and dryrun tests and the guide snippet on Linux x86_64 (A100, TensorRT 11.4).

Models without profiles build the same engines.

@meta-cla meta-cla Bot added the cla signed label Nov 20, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 20, 2026
@github-actions
github-actions Bot requested a review from zewenli98 November 20, 2026 05:36
@shoumikhin
shoumikhin force-pushed the opt-shape-exported-program branch from f9f6fdb to fb0fc92 Compare November 20, 2026 11:52
@narendasan
narendasan requested a review from apbose November 20, 2026 17:54
@narendasan narendasan added this to the v2.16.0 milestone Nov 20, 2026
@shoumikhin
shoumikhin force-pushed the opt-shape-exported-program branch 2 times, most recently from f118f99 to 1f76ef6 Compare November 20, 2026 21:51
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Nov 20, 2026
@shoumikhin
shoumikhin force-pushed the opt-shape-exported-program branch 4 times, most recently from f012726 to ca7e49c Compare November 21, 2026 14:21
@github-actions github-actions Bot added the component: conversion Issues re: Conversion stage label Nov 21, 2026
@shoumikhin
shoumikhin force-pushed the opt-shape-exported-program branch 2 times, most recently from e689f7e to 3c8b2c1 Compare November 21, 2026 21:17
@shoumikhin shoumikhin changed the title fix(dynamo): use Input.opt_shape for the optimization profile of exported programs fix(dynamo): honor Input.profiles on the whole-graph path and document how to set opt for exported programs Nov 21, 2026
@shoumikhin
shoumikhin force-pushed the opt-shape-exported-program branch 2 times, most recently from b64999f to 89a1a5c Compare November 22, 2026 00:39
@shoumikhin
shoumikhin force-pushed the opt-shape-exported-program branch 2 times, most recently from 78e64a0 to dd94db4 Compare November 23, 2026 19:56
…t how to set opt for exported programs

With require_full_compilation=True and every operator supported, compile
converts the whole graph as one engine and skips partitioning. That path
ignored Input.profiles. It built one profile, tuned for the export
example, that covered every entry, so optimization_profile(model, 1) had
no profile to select. The partitioned path builds one profile per entry,
and before the whole-graph path existed (2.14) this case took it too.

Build the profiles once, before the path is chosen, and use them on both
paths: each engine gets one profile per entry, and different profile
counts raise. This changes the whole-graph path for inputs with more than
one profile. A call outside profile 0 must now pick a profile with
optimization_profile, a size that no profile covers cannot run, and inputs
with different profile counts no longer compile. That is how the
partitioned path already works.

Match the Inputs to the graph inputs through the export input spec, as
_build_user_symbol_bounds already does, instead of by position. Without a
spec, for example after a mutated buffer becomes an engine input, match
kwargs by name and take the args in order, then any kwarg that names no
graph input, as export can rename an argument (X becomes x). Before,
kwargs passed in a different order than at export, a tuple input before a
profiled one, or a mutated buffer next to profiled kwargs gave an input
the profile of another input.

For an ExportedProgram, the engine opt comes from the export example, and
a plain Input(min_shape, opt_shape, max_shape) does not change it. Warn
once per compile when opt_shape differs from the example in a dynamic
dimension of the export that no profile covers, and document the two ways
to set opt: export with an example of that size, or pass the input as a
single entry in Input(profiles=[...]).

The 0/1 specialization message said the engine min would be 2, but the
engine keeps min 1. Make it a debug message that says so.

Signed-off-by: Anthony Shoumikhin <[email protected]>
The warning that a plain Input.opt_shape does not set the engine opt was
logged before compile_module knew whether TensorRT would get any part of
the graph, so a compile that converts nothing still warned about an
engine. _build_user_symbol_bounds now returns the dims it would warn
about, and compile_module warns on the whole-graph path, and on the
partitioned path only when a TensorRT part exists. The input checks in
_build_user_symbol_bounds still run where they did.

The unit tests that exported at their opt_shape only to avoid the old
warning go back to their original examples, and the guide says when the
warning is logged.

Signed-off-by: Anthony Shoumikhin <[email protected]>
The 0/1 specialization test passed whether or not the branch ran, so
check its debug record.

Signed-off-by: Anthony Shoumikhin <[email protected]>
save_dynamic_shapes_example.py exported its first model at batch 2 with
opt_shape batch 8, so the engine was tuned for batch 2. Export it at
batch 8.

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin
shoumikhin force-pushed the opt-shape-exported-program branch from dd94db4 to 89ee0d5 Compare November 23, 2026 20:35
@shoumikhin
shoumikhin marked this pull request as draft November 23, 2026 21:54
@apbose

apbose commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

The explicit-profile propagation fix looks good. For a follow-up API cleanup, could we make the intent explicit? When compiling an ExportedProgram, opt_shape=None would use the export example, while an explicitly supplied opt_shape would override it.


Input(opt_shape=None, ...): derive the optimization shape from the export example.
Input(opt_shape=(...), ...): honor the supplied optimization shape.

For Input.profiles, we could also accept a named OptimizationProfile(min_shape, opt_shape, max_shape) alongside dictionaries? For example:

Input(profiles=[
    OptimizationProfile(
        min_shape=(1, 64),
        opt_shape=(1, 64),
        max_shape=(512, 64),
    )
])

This would make profiles clearer to construct and distinguish an omitted optimization shape from an explicit override. Let me know what you think?

@shoumikhin

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I agree with both points.

  1. opt_shape=None versus an explicit opt_shape: yes. For an ExportedProgram, None would mean "use the export example", and an explicit value would always win.
  2. A named OptimizationProfile(min_shape, opt_shape, max_shape): yes, accepted next to the current dictionaries, so existing code keeps working.

Both change the public Input API, so I would keep them in a separate PR and leave this one focused on the profile fix.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants