TensorRT outputs -1 when running modulus on certain integer values
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 13.4k
- Forks
- 2.4k
- Avg merge
- 5d 3h
- Merged PRs (30d)
- 2
Description
Description
TensorRT produces incorrect results for the modulus operation (x % 32) on int32 tensors for certain input values. The engine returns -1 instead of the correct result 31 for inputs like 18542495 and 2147483647, while working correctly for other values like 1800000000.
This appears to be a correctness bug in how TensorRT handles the modulus operation on signed int32 values. The issue also persists when using int64 rather than int32.
Error Example:
✗ Test failed for input 18542495
TensorRT output: -1
PyTorch output: 31
Environment
TensorRT Version: 10.7.0.post1
NVIDIA GPU: NVIDIA A10G
NVIDIA Driver Version: 570.172.08
CUDA Version: 12.6
CUDNN Version: 90501
Operating System: Ubuntu 22.04.4 LTS
Python Version (if applicable): 3.10.7
Tensorflow Version (if applicable): N/A
PyTorch Version (if applicable): 2.6.0.post3
Baremetal or Container (if so, version): Baremetal
Relevant Files
Model link: Minimal model included in reproduction script below (simple modulus operation)
Steps To Reproduce
Full Reproducible Script
import tensorrt as trt
import torch
ONNX_FILE = "modulus.onnx"
example_tensor = torch.tensor([1], dtype=torch.int32, device="cuda")
class ExampleModulusModule(torch.nn.Module):
def forward(self, x):
return x % 32
module = ExampleModulusModule()
with torch.no_grad():
torch.onnx.export(
module,
(example_tensor,),
ONNX_FILE,
opset_version=18,
input_names=["input"],
output_names=["output"],
dynamo=True
)
# Create TensorRT logger and builder
trt_logger = trt.Logger(trt.Logger.INFO)
trt_builder = trt.Builder(trt_logger)
trt_network = trt_builder.create_network()
trt_parser = trt.OnnxParser(trt_network, trt_logger)
trt_config = trt_builder.create_builder_config()
# Configure TensorRT settings
trt_config.profiling_verbosity = trt.ProfilingVerbosity.DETAILED
trt_config.hardware_compatibility_level = trt.HardwareCompatibilityLevel.AMPERE_PLUS
# Parse the ONNX file
with open(ONNX_FILE, "rb") as f:
if not trt_parser.parse(f.read()):
for i in range(trt_parser.num_errors):
print(trt_parser.get_error(i))
raise RuntimeError("Failed to parse the ONNX file")
# Build the serialized network
print("Building TensorRT engine...")
engine_bytes = trt_builder.build_serialized_network(trt_network, trt_config)
# Deserialize the engine
runtime = trt.Runtime(trt_logger)
engine = runtime.deserialize_cuda_engine(engine_bytes)
# Create execution context
context = engine.create_execution_context()
# Get tensor names and shapes
input_name = "input"
output_name = "output"
input_shape = engine.get_tensor_shape(input_name)
output_shape = engine.get_tensor_shape(output_name)
print(f"Input shape: {input_shape}")
print(f"Output shape: {output_shape}")
# Run test cases
print("\n" + "=" * 80)
print("Testing TensorRT Engine")
print("=" * 80)
for test_case in [18542495, 1800000000, 2147483647]:
input_tensor = torch.tensor([test_case], dtype=torch.int32, device="cuda").contiguous()
output_tensor = torch.empty(tuple(output_shape), dtype=torch.int32, device="cuda").contiguous()
# Set tensor addresses
context.set_tensor_address(input_name, input_tensor.data_ptr())
context.set_tensor_address(output_name, output_tensor.data_ptr())
# Execute inference
context.execute_async_v3(torch.cuda.current_stream().cuda_stream)
torch.cuda.synchronize()
# Compare with PyTorch result
expected_out = module(input_tensor)
try:
torch.testing.assert_close(output_tensor, expected_out)
print(f"✓ Test passed for input {test_case}: TRT={output_tensor.item()}, PyTorch={expected_out.item()}")
except AssertionError as e:
print(f"✗ Test failed for input {test_case}")
print(f" TensorRT output: {output_tensor.item()}")
print(f" PyTorch output: {expected_out.item()}")
print("=" * 80)
Commands or scripts:
python reproduce_modulus_bug.py
Output:
================================================================================
Testing TensorRT Engine
================================================================================
✗ Test failed for input 18542495
TensorRT output: -1
PyTorch output: 31
✓ Test passed for input 1800000000: TRT=0, PyTorch=0
✗ Test failed for input 2147483647
TensorRT output: -1
PyTorch output: 31
Results Summary:
| Input | Expected (PyTorch) | TensorRT Result | Status |
|---|---|---|---|
| 18542495 | 31 | -1 | ❌ FAIL |
| 1800000000 | 0 | 0 | ✅ PASS |
| 2147483647 | 31 | -1 | ❌ FAIL |
Have you tried the latest release?:
No
Attach the captured .json and .bin files from TensorRT's API Capture tool if you're on an x86_64 Unix system
N/A
Can this model run on other frameworks? For example run ONNX model with ONNXRuntime (polygraphy run <model.onnx> --onnxrt):
It should be possible, this is a trivial model
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running reproduce_modulus_bug.py with the listed TensorRT, CUDA, GPU, and PyTorch environment, then inspect the generated ONNX modulus operation and TensorRT's parsing and engine-building path. Compare the results for the three supplied inputs and for int32 versus int64. Done means TensorRT returns the expected remainder, including 31 for 18542495 and 2147483647.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100