Skip to content

user provided bound for torchtrt compile when export dimension is unb…#4213

Open
apbose wants to merge 8 commits into
mainfrom
abose/torchTRT_dynamic_user_shape_bounds
Open

user provided bound for torchtrt compile when export dimension is unb…#4213
apbose wants to merge 8 commits into
mainfrom
abose/torchTRT_dynamic_user_shape_bounds

Conversation

@apbose
Copy link
Copy Markdown
Collaborator

@apbose apbose commented Apr 27, 2026

This PR validates user bounds against any finite exporter bounds, with three branches:

  • Upper overflow (user_max > exp_max) → ValueError. TRT engine profile follows the exporter; shapes above exp_max would be rejected at runtime anyway.
  • Lower underflow (user_min < exp_min) → ValueError, except the (user_min=1, exp_min=2) case which is just PyTorch's 0/1-specialization artifact (Dim(min=1) silently becomes 2 in ShapeEnv), this particular case emits a warning.
  • Subset (user range strictly inside exporter range) → engine profile is narrowed to the user's bounds (logged at INFO). The user's Input(min_shape, max_shape) is the contract for the TRT profile; a subset of the exporter's valid range is still correct, and a tighter profile lets TRT specialize more aggressively.
  • Dim.DYNAMIC (unbounded exporter upper) → no check, user fills the gap (the intended use).

@meta-cla meta-cla Bot added the cla signed label Apr 27, 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 Apr 27, 2026
@github-actions github-actions Bot requested a review from lanluo-nvidia April 27, 2026 19:27
@apbose apbose force-pushed the abose/torchTRT_dynamic_user_shape_bounds branch from 4d1ff02 to eec2c04 Compare April 27, 2026 23:35
@apbose
Copy link
Copy Markdown
Collaborator Author

apbose commented Apr 30, 2026

CI index put cases failing. Looking at it.

@apbose apbose force-pushed the abose/torchTRT_dynamic_user_shape_bounds branch from 0fe114e to 2905ff6 Compare April 30, 2026 01:05
@apbose apbose requested a review from narendasan May 6, 2026 21:09
@narendasan
Copy link
Copy Markdown
Collaborator

This PR is only for externally exported programs?

@apbose
Copy link
Copy Markdown
Collaborator Author

apbose commented May 6, 2026

@narendasan yeah it addresses (torch_tensorrt.dynamo.compile(exported_program, ...)). The flow for torch_tensorrt.compile(model, inputs=..) should be unaffected, since in that case export bounds and input bounds should be same

@apbose apbose force-pushed the abose/torchTRT_dynamic_user_shape_bounds branch from dee44b1 to 22728b3 Compare May 6, 2026 22:15
Copy link
Copy Markdown
Collaborator

@narendasan narendasan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the user bounds in the narrow case

@apbose apbose force-pushed the abose/torchTRT_dynamic_user_shape_bounds branch from 02d94e5 to 01798b0 Compare May 21, 2026 18:08
@apbose apbose force-pushed the abose/torchTRT_dynamic_user_shape_bounds branch from 01798b0 to fd52275 Compare May 28, 2026 20:56
Comment on lines +953 to +962
if exp_max_unbounded:
# Dim.DYNAMIC: user fills the gap (intended use).
continue
try:
exp_min = int(exp_lower)
exp_max = int(exp_upper)
except (TypeError, ValueError):
continue
if user_min == exp_min and user_max == exp_max:
continue
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what the logic here is?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block runs only when the exported program already bounds the symbol to a finite range (e.g. exported with Dim("batch", min=10, max=20)). The compiled engine's TRT optimization profile follows that exported range, so we compare the user's Input range against it here, at compile time, and handle four cases:

  • exported upper is unbounded (Dim.DYNAMIC) → user's Input fills the gap, nothing to validate (the continue just above);
  • Input range == exported range → nothing to do;
  • Input extends beyond the exported range (user_max > exp_max or user_min < exp_min) → those shapes can never run, so we raise now;
  • Input strictly inside the exported range → safe to honor, we narrow the engine profile to the Input.
    The user_min == exp_min and user_max == exp_max check on this line is just the "exact match → no-op" early-out.

Comment thread py/torch_tensorrt/dynamo/_compiler.py Outdated

if user_max > exp_max:
raise ValueError(
f"{mismatch} Input.max_shape exceeds the exporter's max "
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the exported program right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the exported program. will change the wording

Comment thread py/torch_tensorrt/dynamo/_compiler.py Outdated
if user_max > exp_max:
raise ValueError(
f"{mismatch} Input.max_shape exceeds the exporter's max "
f"({user_max} > {exp_max}); TRT will reject shapes above "
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TRT will reject or pytorch will?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TRT will. [10,20] + Input min_shape=2: shows
IExecutionContext::setInputShape: Error Code 3: API Usage Error
(Set dimension [2,8] for tensor x does not satisfy any optimization profiles.
Valid range for profile 0: [10,8]..[20,8])
through core/runtime/execute_engine.cpp:149.

Comment thread py/torch_tensorrt/dynamo/_compiler.py Outdated
# 1->2 is the 0/1 specialization artifact, not a user error.
if user_min == 1 and exp_min == 2:
logger.warning(
"%s Input.min_shape=1 vs exporter min=2 is the "
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to make sure these error messages are clear to follow from a user perspective

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: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants