Repository: pytorch
Issue: [Bug] torch.fx.experimental.symbolic_shapes.ConstraintViolationError: torch._dynamo.mark_dynamic(a, 0,min=1,max=8196) min don't work
Labels: triaged,oncall: pt2,module: dynamic shapes

### 🐛 Describe the bug

```
import torch
import torch_tensorrt
from typing import Optional, Sequence,Dict,List
from torch.nn import functional as F
from tzrec.modules.mlp import MLP
from torch import nn

@torch.fx.wrap
def _get_dict(grouped_features_keys: List[str], args:List[torch.Tensor])->Dict[str, torch.Tensor]:
    if len(grouped_features_keys) != len(args):
            raise ValueError(
                "The number of grouped_features_keys must match "
                "the number of arguments."
            )
    grouped_features = {
        key: value for key, value in zip(grouped_features_keys, args)
    }
    return grouped_features

@torch.fx.wrap
def _arange(end: int, device: torch.device) -> torch.Tensor:
    return torch.arange(end, device=device)

class MatMul(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.keys = ["query","sequence","sequence_length"]
        attn_mlp= {'hidden_units': [256, 64], 'dropout_ratio': [], 'activation': 'nn.ReLU', 'use_bn': False}
        self.mlp = MLP(in_features=41 * 4, **attn_mlp)
        self.linear = nn.Linear(self.mlp.hidden_units[-1], 1)

    def forward(self, *args1: List[torch.Tensor]):
        """Forward the module."""
        # use predict to avoid trace error in self._output_to_prediction(y)
        return self.predict(args1)
    
    def predict(self, args: List[torch.Tensor]):
        grouped_features= _get_dict(self.keys, args)
        query = grouped_features["query"]
        sequence = grouped_features["sequence"]
        sequence_length = grouped_features["sequence_length"]
        max_seq_length = sequence.size(1)
        sequence_mask = _arange(
            max_seq_length, device=sequence_length.device
        ).unsqueeze(0) < sequence_length.unsqueeze(1)

       
        queries = query.unsqueeze(1).expand(-1, max_seq_length, -1)

        attn_input = torch.cat(
            [queries, sequence, queries - sequence, queries * sequence], dim=-1
        )
        
        return attn_input
       

model = MatMul().eval().cuda()
a=torch.randn(1, 41).cuda()
b=torch.randn(1, 50,41).cuda()
c=torch.randn(1).cuda()
torch._dynamo.mark_dynamic(a, 0,min=1,max=8196)
torch._dynamo.mark_dynamic(b, 0,min=1,max=8196)
# torch._dynamo.mark_dynamic(b, 1, min=1, max=50)
torch._dynamo.mark_dynamic(c, 0,min=1,max=8196)
inputs = [a, b,c]
print(model(*inputs)[0][0][0])
# seq_len = torch.export.Dim("seq_len", min=1, max=10)
# dynamic_shapes=({2: seq_len}, {2: seq_len})
# Export the model first with custom dynamic shape constraints
from torchrec.fx import symbolic_trace
model = symbolic_trace(model)
print(model.code)
exp_program = torch.export.export(model, (*inputs,))

```

when  i use batch_size=1 will raise error, but when I use. batch_size=2 ,it's ok

error:
```
WARNING:torch_tensorrt.dynamo.conversion.aten_ops_converters:Unable to import quantization op. Please install modelopt library (https://github.com/NVIDIA/TensorRT-Model-Optimizer?tab=readme-ov-file#installation) to add support for compiling quantized models
Initializing pyfg...
I20240903 09:34:33.319730 126517 str_utils.cc:53] AVX supported
I20240903 09:34:33.319756 126517 str_utils.cc:55] FMA supported
I20240903 09:34:33.319765 126517 str_utils.cc:72] AVX-512F supported
I20240903 09:34:33.319772 126517 str_utils.cc:78] AVX-512VL supported
I20240903 09:34:33.319779 126517 str_utils.cc:84] AVX-512BW supported
I20240903 09:34:33.319787 126517 str_utils.cc:90] AVX-512DQ supported
support avx512: true
I20240903 09:34:33.319797 126517 str_utils.cc:194] support avx512: 1
V0903 09:34:35.910000 140056757741376 torch/fx/experimental/symbolic_shapes.py:2529] [0/0] create_env
I0903 09:34:35.991000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3639] [0/0] produce_guards
V0903 09:34:35.991000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][0].size()[0] 1 StrictMinMaxConstraint(warn_only=False, vr=VR[1, 8196])
V0903 09:34:35.991000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][0].size()[1] 41 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][0].stride()[0] 41 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][0].stride()[1] 1 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][0].storage_offset() 0 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][1].size()[0] 1 StrictMinMaxConstraint(warn_only=False, vr=VR[1, 8196])
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][1].size()[1] 50 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][1].size()[2] 41 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][1].stride()[0] 2050 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][1].stride()[1] 41 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][1].stride()[2] 1 None
V0903 09:34:35.992000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][1].storage_offset() 0 None
V0903 09:34:35.993000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][2].size()[0] 1 StrictMinMaxConstraint(warn_only=False, vr=VR[1, 8196])
V0903 09:34:35.993000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][2].stride()[0] 1 None
V0903 09:34:35.993000 140056757741376 torch/fx/experimental/symbolic_shapes.py:3821] [0/0] track_symint L['args1'][2].storage_offset() 0 None
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0] Error while creating guard:
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0] Name: ''
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     Source: shape_env
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     Create Function: SHAPE_ENV
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     Guard Types: None
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     Code List: None
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     Object Weakref: None
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     Guarded Class Weakref: None
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0] Traceback (most recent call last):
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_guards.py", line 260, in create
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     return self.create_fn(builder, self)
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/guards.py", line 1717, in SHAPE_ENV
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     guards = output_graph.shape_env.produce_guards(
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/fx/experimental/symbolic_shapes.py", line 4163, in produce_guards
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]     raise ConstraintViolationError(
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0] torch.fx.experimental.symbolic_shapes.ConstraintViolationError: Constraints violated (L['args1'][0].size()[0], L['args1'][1].size()[0], L['args1'][2].size()[0])! For more information, run with TORCH_LOGS="+dynamic".
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]   - Not all values of L['args1'][0].size()[0] = L['args1'][0].size()[0] in the specified range L['args1'][0].size()[0] <= 8196 are valid because L['args1'][0].size()[0] was inferred to be a constant (1).
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]   - Not all values of L['args1'][1].size()[0] = L['args1'][1].size()[0] in the specified range L['args1'][1].size()[0] <= 8196 are valid because L['args1'][1].size()[0] was inferred to be a constant (1).
E0903 09:34:35.993000 140056757741376 torch/_guards.py:262] [0/0]   - Not all values of L['args1'][2].size()[0] = L['args1'][2].size()[0] in the specified range L['args1'][2].size()[0] <= 8196 are valid because L['args1'][2].size()[0] was inferred to be a constant (1).
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0] Created at:
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 564, in transform
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0]     tracer = InstructionTranslator(
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/symbolic_convert.py", line 2360, in __init__
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0]     output=OutputGraph(
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/output_graph.py", line 313, in __init__
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0]     self.init_ambient_guards()
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/output_graph.py", line 452, in init_ambient_guards
E0903 09:34:35.996000 140056757741376 torch/_guards.py:264] [0/0]     self.guards.add(ShapeEnvSource().make_guard(GuardBuilder.SHAPE_ENV))
```

### Versions

```
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.4
[pip3] optree==0.12.1
[pip3] torch==2.4.0
[pip3] torch_tensorrt==2.4.0
[pip3] torchaudio==2.4.0
[pip3] torchelastic==0.2.2
[pip3] torchmetrics==1.0.3
[pip3] torchrec==0.8.0+cu121
[pip3] torchvision==0.19.0
[pip3] triton==3.0.0
[conda] blas                      1.0                         mkl
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46344
[conda] mkl-service               2.4.0           py311h5eee18b_1
[conda] mkl_fft                   1.3.8           py311h5eee18b_0
[conda] mkl_random                1.2.4           py311hdb19cb5_0
[conda] numpy                     1.26.4          py311h08b1b3b_0
[conda] numpy-base                1.26.4          py311hf175353_0
[conda] optree                    0.12.1                   pypi_0    pypi
[conda] pytorch                   2.4.0           py3.11_cuda12.1_cudnn9.1.0_0    pytorch
[conda] pytorch-cuda              12.1                 ha16c6d3_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torch-tensorrt            2.4.0                    pypi_0    pypi
[conda] torchaudio                2.4.0               py311_cu121    pytorch
[conda] torchelastic              0.2.2                    pypi_0    pypi
[conda] torchmetrics              1.0.3                    pypi_0    pypi
[conda] torchrec                  0.8.0+cu121              pypi_0    pypi
[conda] torchtriton               3.0.0                     py311    pytorch
[conda] torchvision               0.19.0              py311_cu121    pytorch
```

cc @ezyang @chauhang @penguinwu