torch-mlir icon indicating copy to clipboard operation
torch-mlir copied to clipboard

Fails to legalize operation 'torch.aten.convolution' when output_type=OutputType.TOSA

Open KunWillRICH opened this issue 10 months ago • 1 comments

Reproducible code:

import torch
import torch.nn as nn
import torch_mlir
from torch_mlir import TensorPlaceholder, OutputType


class conv1d(nn.Module):
    def __init__(self):
        super(conv1d, self).__init__()
        self.conv1_pre = torch.nn.Conv1d(in_channels=2, out_channels=16, kernel_size=1, stride=1, padding=0, bias=False)

    def forward(self, x):
        x = self.conv1_pre(x)
        return x


def main():
    model = conv1d()
    input = torch.zeros(64, 2, 96) 
    input_empty = torch.empty(64, 2, 96)
    input = input_empty.uniform_(0.01, 0.1)

    model.eval()
    filename = 'layerNorm'

    mlir_model = torch_mlir.compile(
        model, input,
        output_type=OutputType.TOSA,
        use_tracing=True)

    torch.save(model.state_dict(), f'{filename}.pth')


if __name__ == '__main__':
    main()

Error message:

Traceback (most recent call last):
  File "D:\pythonProject11\conv1d.py", line 86, in <module>
    main()
  File "D:\pythonProject11\conv1d.py", line 75, in main
    mlir_model = torch_mlir.compile(
                 ^^^^^^^^^^^^^^^^^^^
  File "D:\pythonProject11\venv\Lib\site-packages\torch_mlir\__init__.py", line 478, in compile
    return _lower_mlir_module(verbose, output_type, mb.module)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\pythonProject11\venv\Lib\site-packages\torch_mlir\__init__.py", line 294, in _lower_mlir_module
    run_pipeline_with_repro_report(
  File "D:\pythonProject11\venv\Lib\site-packages\torch_mlir\compiler_utils.py", line 73, in run_pipeline_with_repro_report
    raise TorchMlirCompilerError(trimmed_message) from None
torch_mlir.compiler_utils.TorchMlirCompilerError: Lowering Torch Backend IR -> TOSA Backend IR failed with the following diagnostics:


python exception: Failure while executing pass pipeline:
error: "__module.conv1_pre/aten::_convolution"("D:\\pythonProject11\\venv\\Lib\\site-packages\\torch\\nn\\modules\\conv.py":306:0): failed to legalize operation 'torch.aten.convolution' that was explicitly marked illegal
note: "__module.conv1_pre/aten::_convolution"("D:\\pythonProject11\\venv\\Lib\\site-packages\\torch\\nn\\modules\\conv.py":306:0): see current operation: %8 = "torch.aten.convolution"(%arg0, %1, %2, %6, %7, %6, %5, %7, %3) : (!torch.vtensor<[64,2,96],f32>, !torch.vtensor<[16,2,1],f32>, !torch.none, !torch.list<int>, !torch.list<int>, !torch.list<int>, !torch.bool, !torch.list<int>, !torch.int) -> !torch.vtensor<[64,16,96],f32>

For Torch-MLIR developers, the error can be reproduced with:
$ torch-mlir-opt -pass-pipeline='builtin.module(torch-backend-to-tosa-backend-pipeline)' C:\Users\L00610~1\AppData\Local\Temp\SpaceFormer.mlir
Add '-mlir-print-ir-after-all -mlir-disable-threading' to get the IR dump for debugging purpose.


Process finished with exit code 1

Torch-MLIR will not report an error when the output type is "output_type=torch_mlir.OutputType.LINALG_ON_TENSORS", but will report an error when "output_type=OutputType.TOSA".

What causes this issue? Can someone help me fix the issue? Thank you!

KunWillRICH avatar Feb 17 '25 11:02 KunWillRICH

Hi, there is no valid conversion from torch to TOSA for 1D convolutions, it's unsupported.

I didn't reproduce your code but this may be the cause of your problem.

Also notice, that conv1d in not a TOSA operator (link).

rYm-A avatar Feb 17 '25 17:02 rYm-A