[VitMatte] VitMatte cannot use torch.compile
Author: ShirasawaSama
Pull request: no

### System Info

- `transformers` version: 4.38.1
- Platform: Linux-6.1.0-18-amd64-x86_64-with-glibc2.36
- Python version: 3.11.2
- Huggingface_hub version: 0.20.3
- Safetensors version: 0.4.2
- Accelerate version: 0.27.2
- Accelerate config:    not found
- PyTorch version (GPU?): 2.1.2+cu121 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

### Who can help?

@NielsRogge Thank you very much.

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

My code: https://huggingface.co/docs/transformers/main/model_doc/vitmatte#transformers.VitMatteForImageMatting.forward.example

```diff
from transformers import VitMatteImageProcessor, VitMatteForImageMatting
import torch
from PIL import Image
from huggingface_hub import hf_hub_download

processor = VitMatteImageProcessor.from_pretrained("hustvl/vitmatte-small-composition-1k")
- model = VitMatteForImageMatting.from_pretrained("hustvl/vitmatte-small-composition-1k")
+ model = VitMatteForImageMatting.from_pretrained("hustvl/vitmatte-small-composition-1k", torch_dtype=torch.float16).to('cuda')

+ model = torch.compile(model)

filepath = hf_hub_download(
    repo_id="hf-internal-testing/image-matting-fixtures", filename="image.png", repo_type="dataset"
)
image = Image.open(filepath).convert("RGB")
filepath = hf_hub_download(
    repo_id="hf-internal-testing/image-matting-fixtures", filename="trimap.png", repo_type="dataset"
)
trimap = Image.open(filepath).convert("L")

# prepare image + trimap for the model
- inputs = processor(images=image, trimaps=trimap, return_tensors="pt")
+ inputs = processor(images=image, trimaps=trimap, return_tensors="pt").to('cuda', torch.float16)

with torch.no_grad():
    alphas = model(**inputs).alphas
print(alphas.shape)
```

The I got:

```
torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
LoweringException: AttributeError: 'PermuteView' object has no attribute 'freeze_layout'
  target: aten.convolution.default
  args[0]: TensorBox(
    PermuteView(data=StorageBox(
      ComputedBuffer(name='buf128', layout=FlexibleLayout('cuda', torch.float32, size=[1, 112, 68, 768], stride=[5849088, 52224, 768, 1]), data=Pointwise(
        'cuda',
        torch.float32,
        def inner_fn(index):
            _, i1, i2, i3 = index
            tmp0 = ops.load(buf93, i3 + 768 * i2 + 52224 * i1)
            tmp1 = ops.load(arg80_1, i3)
            tmp2 = ops.load(buf120, i3 + 768 * i2 + 52224 * i1)
            tmp3 = tmp1 + tmp2
            tmp4 = tmp0 + tmp3
            tmp5 = ops.load(arg86_1, i3)
            tmp6 = ops.load(buf127, i3 + 768 * i2 + 52224 * i1)
            tmp7 = tmp5 + tmp6
            tmp8 = tmp4 + tmp7
            return tmp8
        ,
        ranges=[1, 112, 68, 768],
        origin_node=add_79,
        origins={add_79, clone_27, add_75}
      ))
    ), dims=[0, 3, 1, 2])
  )
  args[1]: TensorBox(StorageBox(
    InputBuffer(name='arg87_1', layout=FixedLayout('cuda', torch.float32, size=[384, 768, 1, 1], stride=[768, 1, 1, 1]))
  ))
  args[2]: None
  args[3]: [1, 1]
  args[4]: [0, 0]
  args[5]: [1, 1]
  args[6]: False
  args[7]: [0, 0]
  args[8]: 1
```

<details> <summary>Full callstacks</summary>
```
/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_utils.py:831: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly.  To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
  return self.fget.__get__(instance, owner)()
Traceback (most recent call last):
  File "/home/shirasawa/omokage-gpu/matte.py", line 24, in <module>
    alphas = model(**inputs).alphas
             ^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/eval_frame.py", line 328, in _fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/transformers/models/vitmatte/modeling_vitmatte.py", line 268, in forward
    @add_start_docstrings_to_model_forward(VITMATTE_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/transformers/utils/backbone_utils.py", line 227, in forward_with_filtered_kwargs
    return self(*args, **filtered_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/eval_frame.py", line 490, in catch_errors
    return callback(frame, cache_entry, hooks, frame_state)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 641, in _convert_frame
    result = inner_convert(frame, cache_size, hooks, frame_state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 133, in _fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 389, in _convert_frame_assert
    return _compile(
           ^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 569, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/utils.py", line 189, in time_wrapper
    r = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 491, in compile_inner
    out_code = transform_code_object(code, transform)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/bytecode_transformation.py", line 1028, in transform_code_object
    transformations(instructions, code_options)
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 458, in transform
    tracer.run()
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/symbolic_convert.py", line 2069, in run
    super().run()
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/symbolic_convert.py", line 719, in run
    and self.step()
        ^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/symbolic_convert.py", line 683, in step
    getattr(self, inst.opname)(inst)
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/symbolic_convert.py", line 2157, in RETURN_VALUE
    self.output.compile_subgraph(
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/output_graph.py", line 857, in compile_subgraph
    self.compile_and_call_fx_graph(tx, pass2.graph_output_vars(), root)
  File "/usr/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/output_graph.py", line 957, in compile_and_call_fx_graph
    compiled_fn = self.call_user_compiler(gm)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/utils.py", line 189, in time_wrapper
    r = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/output_graph.py", line 1024, in call_user_compiler
    raise BackendCompilerFailed(self.compiler_fn, e).with_traceback(
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/output_graph.py", line 1009, in call_user_compiler
    compiled_fn = compiler_fn(gm, self.example_inputs())
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/repro/after_dynamo.py", line 117, in debug_wrapper
    compiled_gm = compiler_fn(gm, example_inputs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/__init__.py", line 1568, in __call__
    return compile_fx(model_, inputs_, config_patches=self.config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/compile_fx.py", line 1150, in compile_fx
    return aot_autograd(
           ^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/backends/common.py", line 55, in compiler_fn
    cg = aot_module_simplified(gm, example_inputs, **kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_functorch/aot_autograd.py", line 3891, in aot_module_simplified
    compiled_fn = create_aot_dispatcher_function(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/utils.py", line 189, in time_wrapper
    r = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_functorch/aot_autograd.py", line 3429, in create_aot_dispatcher_function
    compiled_fn = compiler_fn(flat_fn, fake_flat_args, aot_config, fw_metadata=fw_metadata)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_functorch/aot_autograd.py", line 2212, in aot_wrapper_dedupe
    return compiler_fn(flat_fn, leaf_flat_args, aot_config, fw_metadata=fw_metadata)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_functorch/aot_autograd.py", line 2392, in aot_wrapper_synthetic_base
    return compiler_fn(flat_fn, flat_args, aot_config, fw_metadata=fw_metadata)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_functorch/aot_autograd.py", line 1573, in aot_dispatch_base
    compiled_fw = compiler(fw_module, flat_args)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/utils.py", line 189, in time_wrapper
    r = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/compile_fx.py", line 1092, in fw_compiler_base
    return inner_compile(
           ^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/repro/after_aot.py", line 80, in debug_wrapper
    inner_compiled_fn = compiler_fn(gm, example_inputs)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/debug.py", line 228, in inner
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/compile_fx.py", line 54, in newFunction
    return old_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/compile_fx.py", line 341, in compile_fx_inner
    compiled_graph: CompiledFxGraph = fx_codegen_and_compile(
                                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/compile_fx.py", line 550, in fx_codegen_and_compile
    graph.run(*example_inputs)
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_dynamo/utils.py", line 189, in time_wrapper
    r = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/graph.py", line 456, in run
    return super().run(*args)
           ^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/fx/interpreter.py", line 138, in run
    self.env[node] = self.run_node(node)
                     ^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/graph.py", line 708, in run_node
    result = self.call_function(n.target, args, kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/graph.py", line 613, in call_function
    raise LoweringException(e, target, args, kwargs).with_traceback(
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/graph.py", line 610, in call_function
    out = lowerings[target](*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/lowering.py", line 279, in wrapped
    out = decomp_fn(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/kernel/conv.py", line 361, in convolution
    return convert_1x1_conv_to_mm(x, weight, bias)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/kernel/conv.py", line 280, in convert_1x1_conv_to_mm
    x.freeze_layout()
    ^^^^^^^^^^^^^^^
  File "/home/shirasawa/omokage-gpu/venv/lib/python3.11/site-packages/torch/_inductor/ir.py", line 5094, in __getattr__
    fn = getattr(self.data, name)
         ^^^^^^^^^^^^^^^^^^^^^^^^
torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
LoweringException: AttributeError: 'PermuteView' object has no attribute 'freeze_layout'
  target: aten.convolution.default
  args[0]: TensorBox(
    PermuteView(data=StorageBox(
      ComputedBuffer(name='buf128', layout=FlexibleLayout('cuda', torch.float32, size=[1, 112, 68, 768], stride=[5849088, 52224, 768, 1]), data=Pointwise(
        'cuda',
        torch.float32,
        def inner_fn(index):
            _, i1, i2, i3 = index
            tmp0 = ops.load(buf93, i3 + 768 * i2 + 52224 * i1)
            tmp1 = ops.load(arg80_1, i3)
            tmp2 = ops.load(buf120, i3 + 768 * i2 + 52224 * i1)
            tmp3 = tmp1 + tmp2
            tmp4 = tmp0 + tmp3
            tmp5 = ops.load(arg86_1, i3)
            tmp6 = ops.load(buf127, i3 + 768 * i2 + 52224 * i1)
            tmp7 = tmp5 + tmp6
            tmp8 = tmp4 + tmp7
            return tmp8
        ,
        ranges=[1, 112, 68, 768],
        origin_node=add_79,
        origins={add_79, clone_27, add_75}
      ))
    ), dims=[0, 3, 1, 2])
  )
  args[1]: TensorBox(StorageBox(
    InputBuffer(name='arg87_1', layout=FixedLayout('cuda', torch.float32, size=[384, 768, 1, 1], stride=[768, 1, 1, 1]))
  ))
  args[2]: None
  args[3]: [1, 1]
  args[4]: [0, 0]
  args[5]: [1, 1]
  args[6]: False
  args[7]: [0, 0]
  args[8]: 1

Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information


You can suppress this exception and fall back to eager by setting:
    import torch._dynamo
    torch._dynamo.config.suppress_errors = True
```
</details>

### Expected behavior

Can compile correctly and accelerate inference.

Comments:
Hi @ShirasawaSama, thanks for the issue! I'm not encountering the same issue exactly, but it seems to be working when using transformers from `main`, the shapes are printed out. Can you try installing from `main`?

After I restart the computer, it can be compiled. Thank you.

@molbap I have found the reason. If the size of the input image is different from the previous one, an error will occur. 

Is there any solution to this problem? Thank you very much.