From ad58d8196800c670acb98716b27f89e163c8ebf3 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Thu, 27 Apr 2023 14:21:37 -0500 Subject: [PATCH 1/2] [Relay] Handle pad value coming from Tensor instead of scalar The PadCompute function would pass empty index to obtain the pad value. This caused a crash when the pad value was given in a tensor with the following message: Check failed: shape.size() == indices.size() (1 vs. 0) : Tensor dimension mismatch in read ndim = 1, indices.size=0 --- src/relay/op/nn/pad.cc | 2 +- .../unittest/test_pad_value_in_array.py | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/python/unittest/test_pad_value_in_array.py diff --git a/src/relay/op/nn/pad.cc b/src/relay/op/nn/pad.cc index 365873d2fd51..8cfb369901ad 100644 --- a/src/relay/op/nn/pad.cc +++ b/src/relay/op/nn/pad.cc @@ -177,7 +177,7 @@ Array PadCompute(const Attrs& attrs, const Array& inputs pad_after.push_back(pad_width[i][1]); } te::Tensor cast_pad_value = topi::cast(inputs[1], inputs[0]->dtype); - const PrimExpr& pad_value = cast_pad_value(Array()); + const PrimExpr& pad_value = cast_pad_value(Array(inputs[1]->shape.size(), 0)); return Array{topi::pad(inputs[0], pad_before, pad_after, pad_value, "T_pad", topi::kElementWise, param->pad_mode)}; } diff --git a/tests/python/unittest/test_pad_value_in_array.py b/tests/python/unittest/test_pad_value_in_array.py new file mode 100644 index 000000000000..7d1889d8a728 --- /dev/null +++ b/tests/python/unittest/test_pad_value_in_array.py @@ -0,0 +1,45 @@ +# 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 numpy as np + +import tvm +import tvm.testing +from tvm import relay + +# This used to crash + + +def test_pad_value_in_array(): + A = relay.var("A", shape=(32, 32), dtype="int8") + + p0 = relay.Constant(tvm.nd.array(np.array([2], dtype="int8"))) + p1 = relay.nn.pad(A, pad_value=p0, pad_width=((1, 1), (1, 1))) + + func = relay.Function(relay.analysis.free_vars(p1), p1) + mod = tvm.IRModule.from_expr(func) + + target = "llvm" + lib = relay.build( + mod, + tvm.target.Target(target, host=target), + runtime=relay.backend.Runtime("cpp"), + executor=relay.backend.Executor("aot", {"unpacked-api": False, "interface-api": "packed"}), + ) + + +if __name__ == "__main__": + tvm.testing.main() From cb3fb1dbf0ac1ecf1683e6ce07ad10ceb37a3ee7 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Fri, 28 Apr 2023 04:40:22 -0700 Subject: [PATCH 2/2] Move test to tests/python/relay/test_op_level2.py --- tests/python/relay/test_op_level2.py | 19 ++++++++ .../unittest/test_pad_value_in_array.py | 45 ------------------- 2 files changed, 19 insertions(+), 45 deletions(-) delete mode 100644 tests/python/unittest/test_pad_value_in_array.py diff --git a/tests/python/relay/test_op_level2.py b/tests/python/relay/test_op_level2.py index 434b4fa0a0bc..0a0ae561ab73 100644 --- a/tests/python/relay/test_op_level2.py +++ b/tests/python/relay/test_op_level2.py @@ -1444,6 +1444,25 @@ def _test_run(dtype): _test_run("int32") +def test_pad_value_in_array(): + A = relay.var("A", shape=(32, 32), dtype="int8") + + # Extract pad value from an array + p0 = relay.Constant(tvm.nd.array(np.array([2], dtype="int8"))) + p1 = relay.nn.pad(A, pad_value=p0, pad_width=((1, 1), (1, 1))) + + func = relay.Function(relay.analysis.free_vars(p1), p1) + mod = tvm.IRModule.from_expr(func) + + target = "llvm" + lib = relay.build( + mod, + tvm.target.Target(target, host=target), + runtime=relay.backend.Runtime("cpp"), + executor=relay.backend.Executor("aot", {"unpacked-api": False, "interface-api": "packed"}), + ) + + @tvm.testing.uses_gpu @pytest.mark.parametrize("dtype", ["float32", "float16"]) def test_lrn(executor_kind, dtype): diff --git a/tests/python/unittest/test_pad_value_in_array.py b/tests/python/unittest/test_pad_value_in_array.py deleted file mode 100644 index 7d1889d8a728..000000000000 --- a/tests/python/unittest/test_pad_value_in_array.py +++ /dev/null @@ -1,45 +0,0 @@ -# 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 numpy as np - -import tvm -import tvm.testing -from tvm import relay - -# This used to crash - - -def test_pad_value_in_array(): - A = relay.var("A", shape=(32, 32), dtype="int8") - - p0 = relay.Constant(tvm.nd.array(np.array([2], dtype="int8"))) - p1 = relay.nn.pad(A, pad_value=p0, pad_width=((1, 1), (1, 1))) - - func = relay.Function(relay.analysis.free_vars(p1), p1) - mod = tvm.IRModule.from_expr(func) - - target = "llvm" - lib = relay.build( - mod, - tvm.target.Target(target, host=target), - runtime=relay.backend.Runtime("cpp"), - executor=relay.backend.Executor("aot", {"unpacked-api": False, "interface-api": "packed"}), - ) - - -if __name__ == "__main__": - tvm.testing.main()