apache / tvm

Open deep learning compiler stack for cpu, gpu and specialized accelerators
https://tvm.apache.org/
Apache License 2.0
11.6k stars 3.44k forks source link

[Bug] frontend.from_paddle does not correctly handle paddle.nn.AdaptiveAvgPool2D with data_format="NHWC" #12645

Open cVladu opened 2 years ago

cVladu commented 2 years ago

I tried to convert a paddle network containing an AdaptiveAvgPool2D with data_format="NHWC" into relay IR

Expected behavior

The network is correctly converted

Actual behavior

The frontend.from_paddle ignores the data_format parameter of AdaptiveAvgPool2D

Environment

Operating system: Distributor ID: Ubuntu Description: Ubuntu 18.04.6 LTS Release: 18.04 Codename: bionic

TVM version: 0.10.dev0 (commit 3d41ac3a9) Steps to build the TVM were followed from: https://tvm.apache.org/docs/install/from_source.html -- no change to config.cmake file

Steps to reproduce

def test_adaptive_pool():
    import paddle

    class PaddleNet(paddle.nn.Layer):
        def __init__(self):
            super(PaddleNet, self).__init__()
            self.adaptive_pool = paddle.nn.AdaptiveAvgPool2D(output_size=20, data_format="NHWC")

        @paddle.jit.to_static(input_spec=[paddle.static.InputSpec(shape=(8, 32, 32, 3), dtype=paddle.float32, name="x")])
        def forward(self, x):
            return self.adaptive_pool(x)

    paddle_net = PaddleNet()
    import tempfile
    import os
    with tempfile.TemporaryDirectory() as tmp_d:
        paddle.jit.save(paddle_net, os.path.join(tmp_d, "paddle_model"))
        loaded_net = paddle.jit.load(os.path.join(tmp_d, "paddle_model"))
    mod, params = relay.frontend.from_paddle(loaded_net)
    mod = tvm.relay.transform.InferType()(mod)
    print(mod.astext())
SStarver commented 2 years ago

@cVladu Hi,there! I am doing the similar thing as "convert CNN with data_format="NHWC" into relay IR" Also I have got problems in the convert from "NCHW" onnx CNN into "NHWC" one.

Is there any learning resources or opensource apis to do this or at least make it easier?

Current Situation: hardware driver background got some tutorials on onnx (both operator & python api) got some tutorials on TVM (beginner)

Wish for your reply, Thanks a lot

blackkker commented 2 years ago

I make a fix in #12658 about layout transorm in paddle. However, the data format NHWC is not encouraged in PaddlePaddle framework. More fix will be added to disable layout transorm in paddle maybe.

blackkker commented 2 years ago

@cVladu Hi,there! I am doing the similar thing as "convert CNN with data_format="NHWC" into relay IR" Also I have got problems in the convert from "NCHW" onnx CNN into "NHWC" one.

Is there any learning resources or opensource apis to do this or at least make it easier?

Current Situation: hardware driver background got some tutorials on onnx (both operator & python api) got some tutorials on TVM (beginner)

Wish for your reply, Thanks a lot

Maybe you can refer to Convert Layout Pass. And applying the ConvertLayout pass to your relay module.

SStarver commented 2 years ago

@cVladu Hi,there! I am doing the similar thing as "convert CNN with data_format="NHWC" into relay IR" Also I have got problems in the convert from "NCHW" onnx CNN into "NHWC" one. Is there any learning resources or opensource apis to do this or at least make it easier? Current Situation: hardware driver background got some tutorials on onnx (both operator & python api) got some tutorials on TVM (beginner) Wish for your reply, Thanks a lot

Maybe you can refer to Convert Layout Pass. And applying the ConvertLayout pass to your relay module.

Yes, this is exactly what I'm looking for. thanks a lot