🐛 [Bug] Encountered bug when using item() Torch-TensorRT #3355
Open
Description
Bug Description
Traceback (most recent call last):
File "/opt/conda/lib/python3.11/site-packages/sympy/core/expr.py", line 4035, in _mag
mag_first_dig = int(ceil(log10(xpos)))
^^^^^^^^^^^^^^^^^
OverflowError: cannot convert float infinity to integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/larec/tzrec/tests/test_item.py", line 45, in <module>
trt_gm = torch_tensorrt.dynamo.compile(exp_program, [a,b],min_block_size=1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/torch_tensorrt/dynamo/_compiler.py", line 291, in compile
trt_gm = compile_module(
^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/torch_tensorrt/dynamo/_compiler.py", line 480, in compile_module
parse_graph_io(gm, dryrun_tracker)
File "/opt/conda/lib/python3.11/site-packages/torch_tensorrt/dynamo/utils.py", line 423, in parse_graph_io
output_shapes = get_graph_io_attrs(output_nodes, "shape")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/torch_tensorrt/dynamo/utils.py", line 402, in get_graph_io_attrs
graph_io_attrs.append(attr_fn(metadata))
^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/torch_tensorrt/dynamo/utils.py", line 365, in unwrap_tensor_shape
min_max_opt = extract_var_range_info(tensor)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/torch_tensorrt/dynamo/utils.py", line 342, in extract_var_range_info
min_val, max_val, opt_val = int(var_range.lower), int(var_range.upper), int(var_val)
^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/sympy/core/expr.py", line 308, in __int__
r = self.round(2)
^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/sympy/core/expr.py", line 3856, in round
digits_to_decimal = _mag(x) # _mag(12) = 2, _mag(.012) = -1
^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/sympy/core/expr.py", line 4037, in _mag
mag_first_dig = int(ceil(Float(mpf_log(xpos._mpf_, 53))/log(10)))
^^^^^^^^^^
AttributeError: 'Infinity' object has no attribute '_mpf_'
To Reproduce
Steps to reproduce the behavior:
import torch.nn as nn
import torch
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
@torch.fx.wrap
def _int_item(x: torch.Tensor) -> int:
return int(x.item())
@torch.fx.wrap
def _tensor(x :List[int])->torch.Tensor:
return torch.tensor(x)
class Item(nn.Module):
def forward(self, x:torch.Tensor,y:torch.Tensor):
values = []
values.append(x.shape)
values.append(y.shape)
values = _tensor(values)
group_sequence_length = _int_item(torch.max(values))
return group_sequence_length
a=torch.randn(66093).cuda()
b=torch.randn(50).cuda()
model = Item().cuda()
res = model(a,b)
print(res)
batch = torch.export.Dim("batch",min=1,max=1000000)
batch2 = torch.export.Dim("batch2",min=1,max=1000000)
dynamic_shapes={"x":{0:batch},"y": {0:batch2}}
# from torch.fx import symbolic_trace
from torchrec.fx import symbolic_trace
model = symbolic_trace(model)
import torch_tensorrt
exp_program = torch.export.export(model, tuple([a,b]),dynamic_shapes=dynamic_shapes)
trt_gm = torch_tensorrt.dynamo.compile(exp_program, [a,b],min_block_size=1)
# # # Run inference
print(trt_gm.code)
Expected behavior
Environment
Byte Order: Little Endian
CPU(s): 104
On-line CPU(s) list: 0-103
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Platinum 8269CY CPU @ 2.50GHz
CPU family: 6
Model: 85
Thread(s) per core: 2
Core(s) per socket: 26
Socket(s): 2
Stepping: 7
CPU max MHz: 3800.0000
CPU min MHz: 1200.0000
BogoMIPS: 5000.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 1.6 MiB (52 instances)
L1i cache: 1.6 MiB (52 instances)
L2 cache: 52 MiB (52 instances)
L3 cache: 71.5 MiB (2 instances)
NUMA node(s): 1
NUMA node0 CPU(s): 0-103
Vulnerability Itlb multihit: KVM: Mitigation: Split huge pages
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling
Vulnerability Tsx async abort: Mitigation; TSX disabled
Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.3
[pip3] nvidia-cublas-cu12==12.1.3.1
[pip3] nvidia-cuda-runtime-cu12==12.1.105
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] torch==2.5.0+cu121
[pip3] torch_tensorrt==2.5.0
[pip3] torchmetrics==1.0.3
[pip3] torchrec==1.0.0+cu121
[pip3] triton==3.1.0
[conda] numpy 1.26.3 pypi_0 pypi
[conda] nvidia-cublas-cu12 12.1.3.1 pypi_0 pypi
[conda] nvidia-cuda-runtime-cu12 12.1.105 pypi_0 pypi
[conda] nvidia-cudnn-cu12 9.1.0.70 pypi_0 pypi
[conda] nvidia-nccl-cu12 2.21.5 pypi_0 pypi
[conda] torch 2.5.0+cu121 pypi_0 pypi
[conda] torch-tensorrt 2.5.0 pypi_0 pypi
[conda] torchmetrics 1.0.3 pypi_0 pypi
[conda] torchrec 1.0.0+cu121 pypi_0 pypi
[conda] triton 3.1.0 pypi_0 pypi