Repository: pytorch
Issue: Dynamo unsupported: dynamic padding
Labels: triaged,oncall: pt2,module: dynamic shapes,module: dynamo

Dynamo raises data dependent value error if the pad size is computed by unbacked values (not sure if I used the term correctly), the issue still persists after using `torch._constrain_as_size`. However, if the pad size is computed from a tensor dim size then dynamo works.

The is one of the blocking issue from #122898, where the pad size is likely unbacked. The pad size can be either positive or negative, maybe this is posing some difficulty during graph capturing. The eventual fx graph should look like the one in example 2.

### Example 1: tensor.item() as pad size fails

```python
import torch

class Model(torch.nn.Module):
    def forward(self, x, size, len):
        # Experiment 1:
        """
        torch._dynamo.exc.UserError: Tried to use data-dependent value in the subsequent computation. This can happen when we encounter unbounded dynamic value that is unknown during tracing time.  You will need to explicitly give hint to the compiler. Please take a look at constrain_as_value OR constrain_as_size APIs.  Could not guard on data-dependent expression 24 - u0 < 0 (unhinted: 24 - u0 < 0).  (Size-like symbols: none)

        Potential framework code culprit (scroll up for full backtrace):
          File "/pytorch/torch/_refs/__init__.py", line 2849, in constant_pad_nd
            if pad[pad_idx + 1] < 0:
        """
        return torch.nn.functional.pad(x, (0, len - size.item()))


func = Model()

x = torch.randn(16, 32)
size = torch.tensor(16, dtype=torch.int64)
len = 24

gm = torch._dynamo.export(func, aten_graph=True)(x, size, len)
gm.graph_module.print_readable()
```

### Example 2: tensor.size(0) as pad size works

```python
import torch

class Model(torch.nn.Module):
    def forward(self, x, size, len):
        """
        class GraphModule(torch.nn.Module):
            def forward(self, x, size, len):
                arg0: "f32[s0, s1]"; arg1: "i64[]";

                arg0, arg1, arg2, = fx_pytree.tree_flatten_spec(([x, size, len], {}), self._in_spec)
                arg0_1 = arg0

                # File: /workspace/bowbao/onnxbench/pytorch/torch/nn/functional.py:4539 in pad, code: return torch._C._nn.pad(input, pad, mode, value)
                sym_size_int: "Sym(s0)" = torch.ops.aten.sym_size.int(arg0_1, 0)
                sub: "Sym(24 - s0)" = 24 - sym_size_int;  sym_size_int = None
                constant_pad_nd: "f32[s0, -s0 + s1 + 24]" = torch.ops.aten.constant_pad_nd.default(arg0_1, [0, sub], 0.0);  arg0_1 = sub = None
                return pytree.tree_unflatten([constant_pad_nd], self._out_spec)

        """
        return torch.nn.functional.pad(x, (0, len - x.size(0)))


func = Model()

x = torch.randn(16, 32)
size = torch.tensor(16, dtype=torch.int64)
len = 24

gm = torch._dynamo.export(func, aten_graph=True)(x, size, len)
gm.graph_module.print_readable()
```

### Experiment 3: tensor.item() + torch._constrain_as_size() as pad size fails
```python
import torch

class Model(torch.nn.Module):
    def forward(self, x, size, len):
        """
        torch._dynamo.exc.UserError: Tried to use data-dependent value in the subsequent computation. This can happen when we encounter unbounded dynamic value that is unknown during tracing time.  You will need to explicitly give hint to the compiler. Please take a look at constrain_as_value OR constrain_as_size APIs.  Could not guard on data-dependent expression 24 - u0 < 0 (unhinted: 24 - u0 < 0).  (Size-like symbols: none)

        Potential framework code culprit (scroll up for full backtrace):
          File "/pytorch/torch/_refs/__init__.py", line 2849, in constant_pad_nd
            if pad[pad_idx + 1] < 0:
        """
        size_i = size.item()
        torch._constrain_as_size(size_i)
        pad_size = len - size_i
        return torch.nn.functional.pad(x, (0, pad_size))


func = Model()

x = torch.randn(16, 32)
size = torch.tensor(16, dtype=torch.int64)
len = 24

gm = torch._dynamo.export(func, aten_graph=True)(x, size, len)
gm.graph_module.print_readable()
```

cc @ezyang @msaroufim @bdhirsh @anijain2305 @chauhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4