Skip to content

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

Open
apbose wants to merge 9 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 9 commits into
mainfrom
abose/torchTRT_dynamic_user_shape_bounds

Conversation

@apbose

@apbose apbose commented Apr 27, 2026

Copy link
Copy Markdown
Collaborator

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

apbose commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator Author

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

apbose commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

@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

@narendasan narendasan left a comment

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.

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 thread py/torch_tensorrt/dynamo/_compiler.py
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

@apbose apbose force-pushed the abose/torchTRT_dynamic_user_shape_bounds branch from dae27b3 to 69a0857 Compare June 12, 2026 18:29
@apbose

apbose commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator Author

failing tests-
L0 dynamo core tests- RTX Linux- runtime cache failure
L0 dynamo core tests- Linux- same
L0 dynamo converter tests- RTX Linux- cumsum operator
L0 torchscript tests- windows- test_e2e_behavior.py worker crashes, test_get_layer_info Key Bindings missing
Python only runtime tests- wrong bounda

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

# Forwarded to the partitioner to fill Dim.DYNAMIC upper bounds.
# Read-only w.r.t. ShapeEnv so range_constraints survive save/re-export.
user_symbol_bounds = _build_user_symbol_bounds(

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.

instead of trying to handle args, kwargs, why not operate on the flattened pytree inputs? If you really do need to do this work this early then you can always call in_spec to flatten the args, kwargs. Then the order is deterministic and you arent doing name matching

Comment thread py/torch_tensorrt/dynamo/_compiler.py Outdated
"""
placeholders = [n for n in gm.graph.nodes if n.op == "placeholder"]

sample_by_name: dict[str, Input] = {}

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.

Like this code should go away with pytree flattening and you can just zip the placeholders

Comment thread py/torch_tensorrt/dynamo/_compiler.py

# Flatten args+kwargs in pytree order — guaranteed to match placeholder
# order by torch.export, so we can zip directly without name matching.
flat_inputs, _ = pytree.tree_flatten((list(sample_arg_inputs), sample_kwarg_inputs))

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.

This should come from the exported program (i believe its called in_spec)

log only); the ``user_min=1, exp_min=2`` case warns -- it's PyTorch's
0/1 specialization artifact, not a user error.
"""
placeholders = [n for n in gm.graph.nodes if n.op == "placeholder"]

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.

Same thing here, there should be a flat list of inputs to the graph

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.

we would need the placeholders for the faketensor values right?

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