ekalda commented on a change in pull request #9384: URL: https://github.com/apache/tvm/pull/9384#discussion_r739093198
########## File path: tests/python/contrib/test_ethosu/test_replace_pooling.py ########## @@ -0,0 +1,158 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +import pytest + +pytest.importorskip("ethosu.vela") + +import tvm +from tvm import relay +from tvm.relay.testing import run_opt_pass +from tvm.relay.backend.contrib.ethosu.tir import spec +from tvm.relay.backend.contrib.ethosu.tir.compiler import lower_to_tir +from .infra import make_ethosu_pooling, get_pooling_args + + [email protected]( + "ifm_shape, ofm_channels, ifm_layout, ofm_layout", + [ + ((1, 5, 9, 3), 3, "NHWC", "NHWC"), + ((1, 8, 3, 9, 16), 40, "NHCWB16", "NHCWB16"), + ((1, 8, 3, 9, 16), 40, "NHCWB16", "NHWC"), + ((1, 8, 9, 40), 40, "NHWC", "NHCWB16"), + ], +) [email protected]("pooling_type", ["AVG", "MAX"]) [email protected]("activation", ["NONE", "CLIP", "TANH", "SIGMOID"]) Review comment: The activation function in the Relay operator is just a string attribute, so it won't interact anyhow with the ifm/ofm shape and therefore I don't think we get much benefit from testing the different activation functions here... ########## File path: python/tvm/relay/op/contrib/ethosu.py ########## @@ -331,6 +332,133 @@ def qnn_depthwise_conv2d_pattern() -> tvm.relay.dataflow_pattern.DFPattern: return clip_or_req +class MaxPool2DParams: + """ + This class will parse a call to a ethosu.maxpool2d composite function + and extract the parameter information. + """ + + composite_name = "ethosu.maxpool2d" + # The hardware only supports padding upto the numbers as follows + padding_bounds = [127, 127, 128, 128] + + def __init__(self, func_body: Call): + clip = None + if str(func_body.op) == "clip": + clip = func_body + pool_op = clip.args[0] + else: + pool_op = func_body + + attrs = pool_op.attrs + self.ifm = TensorParams(pool_op.args[MaxPoolArgs.ifm.value], attrs.layout) + self.ofm = TensorParams(pool_op, attrs.layout) + self.pool_shape = [int(i) for i in attrs.pool_size] + self.strides = attrs.strides + self.padding = attrs.padding + self.activation = clip + self.pooling_type = "MAX" + + def is_valid(self): + """ + This function checks whether MaxPool2D has compatible attributes with the NPU + """ + tensor_params = [self.ifm, self.ofm] + if not check_valid_dtypes(tensor_params): + return False + if self.ifm.dtype != self.ofm.dtype: + return False + if not check_strides(self.strides): + return False + if not check_batch_size(self.ifm): + return False + if not check_padding(self.padding, self.padding_bounds): + return False + # Check pool size + if ( + len(self.pool_shape) != 2 + or self.pool_shape[1] > 256 + or self.pool_shape[0] * self.pool_shape[1] > 256 * 256 + ): + return False + return True + + +def qnn_maxpool2d_pattern() -> tvm.relay.dataflow_pattern.DFPattern: + """ + This function creates the pattern for nn.max_pool2d with optional fused RELU activation. + """ + pattern = is_op("nn.max_pool2d")(wildcard()) + pattern = pattern.optional(is_op("clip")) + return pattern + + +class AvgPool2DParams: Review comment: Ah yes, you are right :) ########## File path: python/tvm/relay/op/contrib/ethosu.py ########## @@ -331,6 +332,133 @@ def qnn_depthwise_conv2d_pattern() -> tvm.relay.dataflow_pattern.DFPattern: return clip_or_req +class MaxPool2DParams: + """ + This class will parse a call to a ethosu.maxpool2d composite function + and extract the parameter information. + """ + + composite_name = "ethosu.maxpool2d" + # The hardware only supports padding upto the numbers as follows + padding_bounds = [127, 127, 128, 128] + + def __init__(self, func_body: Call): + clip = None + if str(func_body.op) == "clip": + clip = func_body + pool_op = clip.args[0] + else: + pool_op = func_body + + attrs = pool_op.attrs + self.ifm = TensorParams(pool_op.args[MaxPoolArgs.ifm.value], attrs.layout) + self.ofm = TensorParams(pool_op, attrs.layout) + self.pool_shape = [int(i) for i in attrs.pool_size] + self.strides = attrs.strides + self.padding = attrs.padding + self.activation = clip + self.pooling_type = "MAX" + + def is_valid(self): + """ + This function checks whether MaxPool2D has compatible attributes with the NPU + """ + tensor_params = [self.ifm, self.ofm] + if not check_valid_dtypes(tensor_params): + return False + if self.ifm.dtype != self.ofm.dtype: + return False + if not check_strides(self.strides): + return False + if not check_batch_size(self.ifm): + return False + if not check_padding(self.padding, self.padding_bounds): + return False + # Check pool size + if ( + len(self.pool_shape) != 2 + or self.pool_shape[1] > 256 + or self.pool_shape[0] * self.pool_shape[1] > 256 * 256 + ): + return False + return True Review comment: I agree with Nicola that even though we could reduce the code size a bit like that, the current approach is a bit more intuitive. ########## File path: tests/python/contrib/test_ethosu/test_replace_pooling.py ########## @@ -0,0 +1,158 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +import pytest + +pytest.importorskip("ethosu.vela") + +import tvm +from tvm import relay +from tvm.relay.testing import run_opt_pass +from tvm.relay.backend.contrib.ethosu.tir import spec +from tvm.relay.backend.contrib.ethosu.tir.compiler import lower_to_tir +from .infra import make_ethosu_pooling, get_pooling_args + + [email protected]( + "ifm_shape, ofm_channels, ifm_layout, ofm_layout", + [ + ((1, 5, 9, 3), 3, "NHWC", "NHWC"), + ((1, 8, 3, 9, 16), 40, "NHCWB16", "NHCWB16"), + ((1, 8, 3, 9, 16), 40, "NHCWB16", "NHWC"), + ((1, 8, 9, 40), 40, "NHWC", "NHCWB16"), + ], +) [email protected]("pooling_type", ["AVG", "MAX"]) [email protected]("activation", ["NONE", "CLIP", "TANH", "SIGMOID"]) +def test_pooling_single( + ifm_shape, + ofm_channels, + ifm_layout, + ofm_layout, + pooling_type, + activation, +): + pool_shape = (3, 2) + strides = (1, 2) + padding = (1, 1, 1, 0) + ifm = relay.var("ifm", shape=ifm_shape, dtype="int8") + pooling = make_ethosu_pooling( + ifm, + pooling_type, + pool_shape, + ofm_channels, + strides, + padding, + activation, + ifm_layout, + ofm_layout, + ) + func = relay.Function(relay.analysis.free_vars(pooling), pooling) + func = run_opt_pass(func, relay.transform.InferType()) + mod, _ = lower_to_tir(func) + data = [] + + def _visit(stmt): + if isinstance(stmt, tvm.tir.Call): + data.append(get_pooling_args(stmt)) + + tvm.tir.stmt_functor.post_order_visit(mod["main"].body, _visit) + if ifm_layout == "NHWC": + ifm_stride_c = 1 + ifm_stride_w = ifm_shape[3] + ifm_stride_h = ifm_shape[2] * ifm_shape[3] + ofm_height = (ifm_shape[1] - pool_shape[0] + padding[0] + padding[0]) // strides[0] + 1 + ofm_width = (ifm_shape[2] - pool_shape[1] + padding[1] + padding[1]) // strides[1] + 1 + else: + ifm_stride_w = 16 + ifm_stride_c = 16 * ifm_shape[3] + ifm_stride_h = 16 * ifm_shape[2] * ifm_shape[3] + ofm_height = (ifm_shape[1] - pool_shape[0] + padding[0] + padding[0]) // strides[0] + 1 + ofm_width = (ifm_shape[3] - pool_shape[1] + padding[1] + padding[1]) // strides[1] + 1 + + if ofm_layout == "NHWC": + ofm_stride_c = 1 + ofm_stride_w = ofm_channels if ofm_width > 1 else 1 + ofm_stride_h = ofm_channels * ofm_width if ofm_height > 1 else 1 + else: + ofm_stride_w = 16 + ofm_stride_c = 16 * ofm_width + ofm_stride_h = 16 * ofm_width * ((ofm_channels - 1) // 16 + 1) Review comment: It seems like if we wanted to get rid of these if/else statements, we would need a separate test case for each combination of ifm layout and ofm layout, so that would be 4 tests. I agree that there would be quite a bit of code duplication and I don't mind the `if`s much myself, but if others find it clearer to have separate tests, I'd be fine with it :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
