Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

torch.aten.avg_pool2d to linalg #643

Closed
AmosLewis opened this issue Apr 19, 2024 · 2 comments
Closed

torch.aten.avg_pool2d to linalg #643

AmosLewis opened this issue Apr 19, 2024 · 2 comments
Assignees

Comments

@AmosLewis
Copy link
Contributor

AmosLewis commented Apr 19, 2024

Find this failed in Inception_v4_vaiq_int8 model support nod-ai/SHARK-TestSuite#190

Inception_v4_vaiq_int8.default.onnx.torch.mlir:1053:12: error: failed to legalize operation 'torch.aten.avg_pool2d' that was explicitly marked illegal
    %816 = torch.aten.avg_pool2d %656, %813, %815, %814, %false_224, %false_225, %none_226 : !torch.vtensor<[32,384,25,25],f32>, !torch.list<int>, !torch.list<int>, !torch.list<int>, !torch.bool, !torch.bool, !torch.none -> !torch.vtensor<[32,384,25,25],f32>

%446 = torch.operator "onnx.AveragePool"(%403) {torch.onnx.ceil_mode = 0 : si64, torch.onnx.kernel_shape = [3 : si64, 3 : si64], torch.onnx.pads = [1 : si64, 1 : si64, 1 : si64, 1 : si64], torch.onnx.strides = [1 : si64, 1 : si64]} : (!torch.vtensor<[32,384,25,25],f32>) -> !torch.vtensor<[32,384,25,25],f32>

Previouse related patch:
torch-to-linalg
[MLIR][TORCH] Add E2E support for aten.avg_pool2d op
[Stablehlo]Add support for AvgPool1dOp
[RFC] general support for Adaptive Pooling Ops
onnx-to-torch:
[MLIR][ONNX] Add OnnxToTorch support for AveragePool op

count_include_pad=True/False explaned

@AmosLewis
Copy link
Contributor Author

AmosLewis commented Apr 22, 2024

Tried to add an new e2e tests for this case:

class AvgPool2dFloatStaticModule(torch.nn.Module):

    def __init__(self):
        super().__init__()
        self.ap2d = torch.nn.AvgPool2d(kernel_size=[3, 3],
                                       stride=[1, 1],
                                       padding=[1, 1, 1, 1],
                                       ceil_mode=False,
                                       count_include_pad=False,
                                       divisor_override=None)

    @export
    @annotate_args([
        None,
        ([32, 384, 25, 25], torch.float32, True),
    ])
    def forward(self, x):
        return self.ap2d(x)


@register_test_case(module_factory=lambda: AvgPool2dFloatStaticModule())
def AvgPool2dFloatStaticModule_basic(module, tu: TestUtils):
    module.forward(tu.rand(32, 384, 25, 25, low=-1))

Run:
python -m e2e_testing.main --config=linalg --filter AvgPool2dFloatStaticModule -v
Got:

TORCH_VERSION_FOR_COMPARISON = 2.4.0.dev20240416
FAIL - "AvgPool2dFloatStaticModule_basic"

Unexpected outcome summary: (linalg)

****** Failed tests - 1 tests
    FAIL - "AvgPool2dFloatStaticModule_basic"
        Compilation error: Traceback (most recent call last):
          File "/home/chi/src/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/framework.py", line 295, in compile_and_run_test
            golden_trace = generate_golden_trace(test)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "/home/chi/src/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/framework.py", line 289, in generate_golden_trace
            test.program_invoker(tracer, TestUtils())
          File "/home/chi/src/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/test_suite/pooling.py", line 867, in AvgPool2dFloatStaticModule_basic
            module.forward(tu.rand(32, 384, 25, 25, low=-1))
          File "/home/chi/src/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/framework.py", line 269, in __call__
            output = self.__wrapped__(*args, **kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "/home/chi/src/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir/torch_mlir_e2e_test/test_suite/pooling.py", line 862, in forward
            return self.ap2d(x)
                   ^^^^^^^^^^^^
          File "/home/chi/src/torch-mlir/mlir_venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
            return self._call_impl(*args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "/home/chi/src/torch-mlir/mlir_venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
            return forward_call(*args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "/home/chi/src/torch-mlir/mlir_venv/lib/python3.11/site-packages/torch/nn/modules/pooling.py", line 641, in forward
            return F.avg_pool2d(input, self.kernel_size, self.stride,
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        RuntimeError: avg_pool2d: padding must either be a single int, or a tuple of two ints


Summary:
    Failed: 1

AmosLewis added a commit to llvm/torch-mlir that referenced this issue Jun 12, 2024
Issues was found here nod-ai/SHARK-ModelDev#643
    - [ONNX] Fix padding attributes for onnx.AveragePool
    - [Linalg] Add countIncludePad false support for AtenAvgPool1/2dOp
    - [Linalg] Add an avg_pool2d countIncludePad False e2e tests
    - [Linalg] Fix conflict with AtenAvgPool3dOp
    - [Linalg] Fix e2e crash with AtenAvgPool1dOp
    - [Linalg] Add dynamic dim support for AtenAvgPool2dOp
    - [Linalg] Fix AvgPool2dDivisorOverrideModule crash
@AmosLewis
Copy link
Contributor Author

llvm/torch-mlir#3428

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant