Repository navigation
Description
Bug Description
With #4978 (not merged yet), a plain int8 constant (for example an int8 buffer) is stored as int32, because TensorRT accepts int8 constants only in front of a dequantize layer (or a plugin). Before that change, these graphs failed to build. Now they build, but the math runs in int32, so a result that overflows int8 does not wrap around the way it does in PyTorch.
- If the int8 result is returned directly, the output cast wraps it back and the answer is right.
- If the result feeds more math first, for example a cast to float, the answer is wrong and there is no error.
Add is only one example. Multiply, subtract, where and cat with an int8 constant also build now, and an overflowing multiply also differs from PyTorch (100 * 100 gives 10000 instead of 16).
To Reproduce
import torch
import torch_tensorrt
class M(torch.nn.Module):
def __init__(self):
super().__init__()
self.register_buffer("b", torch.tensor([100, -100, 120, 5], dtype=torch.int8))
def forward(self, x):
return (self.b + x.to(torch.int8)).to(torch.float32)
m = M().cuda().eval()
x = torch.tensor([100.0, -100.0, 120.0, 5.0], device="cuda")
trt = torch_tensorrt.dynamo.compile(torch.export.export(m, (x,)), arg_inputs=[x], min_block_size=1)
print("eager ", m(x).tolist())
print("tensorrt", trt(x).tolist())With #4978:
eager [-56.0, 56.0, -16.0, 10.0]
tensorrt [200.0, -200.0, 240.0, 10.0]
Without #4978 the build fails with Quantized constant (/add_lhs) is only allowed before DQ or PLUGIN_V2 or kPLUGIN_V3 node.
Expected behavior
The result matches PyTorch. Two possible ways:
- Wrap the int32 result back into the int8 range with integer math, the way
wrap_to_uint8does for uint8. (A plain cast back to int8 was tried in fix(conversion): batch norm dtypes and stats, adaptive max pooling, FP8 zero amax and float32 quantize scales, int8 constants, torchao dequantize dtype, autocast refit, list outputs and dtypes, square and pad-slice lowering, float32 constant folding #4978 and made the TensorRT 11.4 builder run for more than 20 minutes on a larger model, so it was taken out.) - Keep these nodes in PyTorch, the same way uint8 is handled today.
The warning that #4978 logs for this case should also fire only when the result can actually differ. Today it also fires when the int8 result is returned directly, where the answer is right.
Environment
- Torch-TensorRT Version: 2.16.0.dev with fix(conversion): batch norm dtypes and stats, adaptive max pooling, FP8 zero amax and float32 quantize scales, int8 constants, torchao dequantize dtype, autocast refit, list outputs and dtypes, square and pad-slice lowering, float32 constant folding #4978 applied
- PyTorch Version: 2.15.0+cu132
- TensorRT Version: 11.4.0.106
- CPU Architecture: x86_64
- OS: Linux
- Python version: 3.12
- CUDA version: 13.2
- GPU models and configuration: 1x H100
Activity