Repository: pytorch
Issue: [torch.export] Detect internal constrains
Labels: triaged,oncall: pt2,export-triaged,oncall: export

### 🐛 Describe the bug

I don't know if it is a valid bug report or more a feature request. But is technically possible that dynamo detect the model padding instead of enforcing input guards?

This minimal repro was create to debug the same issue on a different bigger model.

```python
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.export as exp
from torch.export import Dim, ShapesCollection

class MinimalModel(nn.Module):
    def __init__(self):
        super(MinimalModel, self).__init__()
        # A simple convolution layer expecting 1 input channel
        self.conv = nn.Conv2d(1, 8, kernel_size=3, stride=1, padding=1)

    def forward(self, inputs):
        B, C, H, W = inputs.shape
        # Calculate necessary padding to make dimensions multiples of 32
        pad_H = (32 - H % 32) % 32
        pad_W = (32 - W % 32) % 32

        if pad_H > 0 or pad_W > 0:
            pad_top = pad_H // 2
            pad_bottom = pad_H - pad_top
            pad_left = pad_W // 2
            pad_right = pad_W - pad_left
            # Apply reflection padding
            inputs = F.pad(inputs, (pad_left, pad_right, pad_top, pad_bottom), mode='reflect')
        
        # Apply convolution
        outputs = self.conv(inputs)
        return outputs


# Initialize the minimal model
model = MinimalModel().eval()

# Example input (1-channel input with arbitrary size)
inputs = torch.randn(1, 1, 224, 224)  # 1-channel input with height and width of 224

# Device setup
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
inputs = inputs.to(device)

# Define dynamic dimensions for export
height_dim = Dim("height_dim", min=224, max=1024)
width_dim = Dim("width_dim", min=224, max=1024)

# Use ShapesCollection to define dynamic height and width
dynamic_shapes = ShapesCollection()
dynamic_shapes[inputs] = (Dim.STATIC, Dim.STATIC, height_dim, width_dim)

# Attempt export
try:
    exported_model = exp.export(
        model,
        (inputs,),
        dynamic_shapes=dynamic_shapes
    )
    exp.save(exported_model, "exported_model.pt")
    print("Model exported successfully.")
except torch._dynamo.exc.UserError as e:
    print(f"Failed to export: {e}")
```

```python
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0] Error while creating guard:
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0] Name: ''
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     Source: shape_env
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     Create Function: SHAPE_ENV
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     Guard Types: None
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     Code List: None
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     Object Weakref: None
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     Guarded Class Weakref: None
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0] Traceback (most recent call last):
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_guards.py", line 281, in create
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     return self.create_fn(builder, self)
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/guards.py", line 1844, in SHAPE_ENV
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     guards = output_graph.shape_env.produce_guards(
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/fx/experimental/symbolic_shapes.py", line 4194, in produce_guards
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]     raise ConstraintViolationError(
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0] torch.fx.experimental.symbolic_shapes.ConstraintViolationError: Constraints violated (height_dim, width_dim)! For more information, run with TORCH_LOGS="+dynamic".
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]   - Not all values of height_dim = L['inputs'].size()[2] in the specified range 224 <= height_dim <= 1024 satisfy the generated guard Mod(32 - Mod(L['inputs'].size()[2], 32), 32) <= 0.
E0917 20:00:03.648000 545 site-packages/torch/_guards.py:283] [0/0]   - Not all values of width_dim = L['inputs'].size()[3] in the specified range 224 <= width_dim <= 1024 satisfy the generated guard Mod(32 - Mod(L['inputs'].size()[3], 32), 32) <= 0.
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0] Created at:
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 615, in transform
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0]     tracer = InstructionTranslator(
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/symbolic_convert.py", line 2670, in __init__
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0]     output=OutputGraph(
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/output_graph.py", line 317, in __init__
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0]     self.init_ambient_guards()
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0]   File "/opt/conda/lib/python3.11/site-packages/torch/_dynamo/output_graph.py", line 463, in init_ambient_guards
E0917 20:00:03.652000 545 site-packages/torch/_guards.py:285] [0/0]     self.guards.add(ShapeEnvSource().make_guard(GuardBuilder.SHAPE_ENV))
Failed to export: Constraints violated (height_dim, width_dim)! For more information, run with TORCH_LOGS="+dynamic".
  - Not all values of height_dim = L['inputs'].size()[2] in the specified range 224 <= height_dim <= 1024 satisfy the generated guard Mod(32 - Mod(L['inputs'].size()[2], 32), 32) <= 0.
  - Not all values of width_dim = L['inputs'].size()[3] in the specified range 224 <= width_dim <= 1024 satisfy the generated guard Mod(32 - Mod(L['inputs'].size()[3], 32), 32) <= 0.

Suggested fixes:
  _height_dim = Dim('_height_dim', min=7, max=32)
  _width_dim = Dim('_width_dim', min=7, max=32)
  height_dim = 32*_height_dim
  width_dim = 32*_width_dim
```

### Versions

nightly

cc @ezyang @chauhang @penguinwu @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4