Repository navigation
Description
Bug Description
When torch_tensorrt.dynamo.compile gets an ExportedProgram with a dynamic dimension, the engine's optimization profile takes its opt value from the example that torch.export saw. The opt_shape of a torch_tensorrt.Input passed in inputs= is not used. Only min_shape and max_shape are read, to fill or narrow the exporter's range.
This matters for LLM decoding. A typical export uses a prefill-sized example (for example 512 tokens), but most calls at run time are 1-token decode steps. TensorRT then tunes every matmul for 512 rows and runs the same tiles for 1 row.
In code, extract_var_range_info sets opt from the shape environment's value for the symbol (the export example). _build_user_symbol_bounds passes only (min, max) from the user's inputs.
To Reproduce
repro_opt.py below (one bf16 gated MLP, 4096 to 14336 to 4096, sequence length dynamic from 1 to 512):
A export example 512, Input opt 1 : profile (min, opt, max) = ((1, 1, 4096), (1, 512, 4096), (1, 512, 4096)) | 1 token 0.318 ms, 512 tokens 0.947 ms
B export example 2, no Input : profile (min, opt, max) = ((1, 1, 4096), (1, 2, 4096), (1, 512, 4096)) | 1 token 0.246 ms, 512 tokens 1.260 ms
A asks for opt_shape=(1, 1, 4096), but the engine is built with opt 512. A 1-token call is about 30% slower than in B, where the export example itself is 2 tokens.
repro_opt.py
# Input(opt_shape=...) is ignored when compiling an ExportedProgram: the engine's opt comes from the export example.
import time
import torch, tensorrt as trt, torch_tensorrt
class MLP(torch.nn.Module): # one decoder-style gated MLP, bf16
def __init__(self, d=4096, h=14336):
super().__init__()
self.gate = torch.nn.Linear(d, h, bias=False)
self.up = torch.nn.Linear(d, h, bias=False)
self.down = torch.nn.Linear(h, d, bias=False)
def forward(self, x):
return self.down(torch.nn.functional.silu(self.gate(x)) * self.up(x))
torch.manual_seed(0)
m = MLP().cuda().to(torch.bfloat16).eval()
T = torch.export.Dim("T", min=1, max=512)
def build(example_len, inputs=None):
x = torch.randn(1, example_len, 4096, device="cuda", dtype=torch.bfloat16)
ep = torch.export.export(m, (x,), dynamic_shapes=({1: T},))
kw = dict(min_block_size=1, use_explicit_typing=True, use_python_runtime=True)
return torch_tensorrt.dynamo.compile(ep, inputs=inputs or [x], **kw)
# Record the profile shapes Torch-TensorRT hands to TensorRT for the input.
seen = []
_set_shape = trt.IOptimizationProfile.set_shape
def _record(self, name, min, opt, max):
seen.append((tuple(min), tuple(opt), tuple(max)))
return _set_shape(self, name, min, opt, max)
trt.IOptimizationProfile.set_shape = _record
def profile_of(build_fn, *args, **kwargs):
seen.clear()
gm = build_fn(*args, **kwargs)
return gm, seen[0] if seen else "no profile recorded"
def ms_per_call(gm, t):
x = torch.randn(1, t, 4096, device="cuda", dtype=torch.bfloat16)
for _ in range(20): gm(x)
torch.cuda.synchronize(); s = time.perf_counter()
for _ in range(200): gm(x)
torch.cuda.synchronize(); return (time.perf_counter() - s) / 200 * 1e3
print("torch_tensorrt", torch_tensorrt.__version__, "tensorrt", trt.__version__, torch.cuda.get_device_name())
asked = [torch_tensorrt.Input(min_shape=(1, 1, 4096), opt_shape=(1, 1, 4096), max_shape=(1, 512, 4096), dtype=torch.bfloat16)]
a, pa = profile_of(build, 512, inputs=asked)
print("A export example 512, Input opt 1 : profile (min, opt, max) =", pa, "| 1 token %.3f ms, 512 tokens %.3f ms" % (ms_per_call(a, 1), ms_per_call(a, 512)))
b, pb = profile_of(build, 2)
print("B export example 2, no Input : profile (min, opt, max) =", pb, "| 1 token %.3f ms, 512 tokens %.3f ms" % (ms_per_call(b, 1), ms_per_call(b, 512)))Expected behavior
opt_shape from inputs= is used for the optimization profile, the same way min_shape and max_shape are. If that is not wanted for exported programs, a warning that it is ignored would help.
Why it matters
Exporting with a tiny example to get a small opt is not a good workaround: one profile cannot serve both phases, and with opt 2 the 512-token call gets about 30% slower (B above). The profiles=[...] form of Input takes an explicit opt per profile, so the plain opt_shape form working the same way would be the least surprising.
The effect is larger with INT4 weight-only layers. With a 512-token opt, TensorRT dequantizes each INT4 weight to a bf16 copy on every 1-token call and then runs a bf16 matmul. With opt 2 it uses a fused INT4 matmul. On a Jetson Thor, a one-layer slice of a 30B model (hidden size 6656) took 13.4 ms per decode step with opt 512 and 5.4 ms with opt 2.
Environment
- Torch-TensorRT: main at 13658aa (Python files) on the 2.15.0+cu132 package
- TensorRT 11.4.0.106, PyTorch 2.15, CUDA 13.2, Linux x86_64
- GPU: NVIDIA A100 80GB
Activity
fix(dynamo): use Input.opt_shape for the optimization profile of expo…
fix(dynamo): use Input.opt_shape for the optimization profile of expo…
fix(dynamo): use Input.opt_shape for the optimization profile of expo…
fix(dynamo): use Input.opt_shape for the optimization profile of expo…
fix(dynamo): use Input.opt_shape for the optimization profile of expo…
fix(dynamo): use Input.opt_shape for the optimization profile of expo…
fix(dynamo): use Input.opt_shape for the optimization profile of expo…
fix(dynamo): use Input.opt_shape for the optimization profile of expo…