From 5af2225fe5b0bcd71838214a756d6e64449909cc Mon Sep 17 00:00:00 2001 From: Ritwik Das Date: Fri, 29 Jan 2021 15:19:38 -0800 Subject: [PATCH 01/10] Support negative pad values --- src/relay/op/nn/pad.cc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/relay/op/nn/pad.cc b/src/relay/op/nn/pad.cc index 5b9988b101eb..5c029a789fb1 100644 --- a/src/relay/op/nn/pad.cc +++ b/src/relay/op/nn/pad.cc @@ -139,13 +139,10 @@ bool PadRel(const Array& types, int num_inputs, const Attrs& attrs, ICHECK(width1 != nullptr); ICHECK(width2 != nullptr); - ICHECK(*width1 >= 0) << "Param width elements should be positive but first pad width at " - << "index " << i << " is " << *width1 << "."; - ICHECK(*width2 >= 0) << "Param width elements should be positive but first pad width at " - << "index " << i << " is " << *width2 << "."; - if (!data->shape[i].as()) { auto padding = tir::make_const(data->shape[i].dtype(), *width1 + *width2); + ICHECK(topi::detail::GetConstInt(data->shape[i] + padding) >= 0) + << "Output shape post padding should be positive but got " << data->shape[i] + padding; oshape.push_back(data->shape[i] + padding); } else { oshape.push_back(data->shape[i]); From 26a2556144b220526ac4ebe53934806bbdee731c Mon Sep 17 00:00:00 2001 From: Ritwik Das Date: Fri, 29 Jan 2021 18:22:16 -0800 Subject: [PATCH 02/10] Update test_op_level2.py --- tests/python/relay/test_op_level2.py | 53 ++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/tests/python/relay/test_op_level2.py b/tests/python/relay/test_op_level2.py index 06bd01b4189a..83858e36c502 100644 --- a/tests/python/relay/test_op_level2.py +++ b/tests/python/relay/test_op_level2.py @@ -1171,7 +1171,7 @@ def test_flatten_infer_type(): @tvm.testing.uses_gpu def test_pad_infer_type(): - # entirely concrete case + # entirely concrete cases n, c, h, w = 1, 2, 3, 4 t = relay.var("t", relay.TensorType((n, c, h, w), "float32")) y = relay.nn.pad(t, ((1, 1), (2, 2), (3, 3), (4, 4))) @@ -1179,6 +1179,13 @@ def test_pad_infer_type(): yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((3, 6, 9, 12), "float32") + n, c, h, w = 4, 6, 3, 5 + t = relay.var("t", relay.TensorType((n, c, h, w), "float32")) + y = relay.nn.pad(t, ((-1, -1), (2, -2), (0, -3), (4, 4)), pad_mode="reflect") + "pad_width=" in y.astext() + yy = run_infer_type(y) + assert yy.checked_type == relay.TensorType((2, 6, 0, 13), "float32") + # some symbolic values n, c, h, w = te.size_var("n"), 2, 3, te.size_var("w") t = relay.var("t", relay.TensorType((n, c, h, w), "float32")) @@ -1186,20 +1193,44 @@ def test_pad_infer_type(): yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n + 2, 6, 9, w + 8), "float32") + n, c, h, w = te.size_var("n"), te.size_var("c"), te.size_var("h"), te.size_var("w") + t = relay.var("t", relay.TensorType((n, c, h, w), "float32")) + y = relay.nn.pad(t, ((-1, -1), (-2, -2), (1, -3), (4, 4))) + yy = run_infer_type(y) + assert yy.checked_type == relay.TensorType((n + (-2), c + (-4), h + (-2), w + 8), "float32") + @tvm.testing.uses_gpu def test_pad_run(): def _test_run(dtype): - dshape = (4, 10, 7, 7) - x = relay.var("x", shape=dshape) - y = relay.nn.pad(x, ((1, 1), (2, 2), (3, 3), (4, 4))) - func = relay.Function([x], y) - data = np.random.uniform(size=dshape).astype(dtype) - ref_res = np.pad(data, ((1, 1), (2, 2), (3, 3), (4, 4)), "constant") - for target, ctx in tvm.testing.enabled_targets(): - intrp1 = relay.create_executor("graph", ctx=ctx, target=target) - op_res1 = intrp1.evaluate(func)(data) - tvm.testing.assert_allclose(op_res1.asnumpy(), ref_res, rtol=1e-5, atol=1e-5) + dshape_list = [(4, 10, 7, 7), (4, 6, 3, 5)] + pad_list = [((1, 1), (2, 2), (3, 3), (4, 4)), ((-1, -1), (2, -2), (0, -3), (4, 4))] + + for dshape, pad in zip(dshape_list, pad_list): + x = relay.var("x", shape=dshape) + y = relay.nn.pad(x, pad) + func = relay.Function([x], y) + data = np.random.uniform(size=dshape).astype(dtype) + mod_pad = [] + mod_data = data + for axis, (pad_x, pad_y) in enumerate(pad): + # if pad_x >= 0 and pad_y >= 0: + # mod_pad.append((pad_x, pad_y)) + indices = range(dshape[axis]) + if pad_x < 0: + indices = indices[abs(pad_x) :] + pad_x = 0 + if pad_y < 0: + indices = indices[:pad_y] + pad_y = 0 + mod_data = np.take(mod_data, indices, axis) + mod_pad.append((pad_x, pad_y)) + + ref_res = np.pad(mod_data, tuple(mod_pad), "constant") + for target, ctx in tvm.testing.enabled_targets(): + intrp1 = relay.create_executor("graph", ctx=ctx, target=target) + op_res1 = intrp1.evaluate(func)(data) + tvm.testing.assert_allclose(op_res1.asnumpy(), ref_res, rtol=1e-5, atol=1e-5) _test_run("float32") _test_run("int32") From ef6e3104756b6f469c9d4bd0c6b364455c3888db Mon Sep 17 00:00:00 2001 From: Ritwik Das Date: Fri, 29 Jan 2021 18:23:25 -0800 Subject: [PATCH 03/10] Update pad.cc --- src/relay/op/nn/pad.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/relay/op/nn/pad.cc b/src/relay/op/nn/pad.cc index 5c029a789fb1..f90127a85bbe 100644 --- a/src/relay/op/nn/pad.cc +++ b/src/relay/op/nn/pad.cc @@ -141,8 +141,6 @@ bool PadRel(const Array& types, int num_inputs, const Attrs& attrs, if (!data->shape[i].as()) { auto padding = tir::make_const(data->shape[i].dtype(), *width1 + *width2); - ICHECK(topi::detail::GetConstInt(data->shape[i] + padding) >= 0) - << "Output shape post padding should be positive but got " << data->shape[i] + padding; oshape.push_back(data->shape[i] + padding); } else { oshape.push_back(data->shape[i]); From 0f602ebeacb723abf9c2c1ba05d8cb78879a721e Mon Sep 17 00:00:00 2001 From: Ritwik Das Date: Fri, 29 Jan 2021 18:28:21 -0800 Subject: [PATCH 04/10] Update test_op_level2.py --- tests/python/relay/test_op_level2.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/python/relay/test_op_level2.py b/tests/python/relay/test_op_level2.py index 83858e36c502..6af201cddf7b 100644 --- a/tests/python/relay/test_op_level2.py +++ b/tests/python/relay/test_op_level2.py @@ -1214,8 +1214,6 @@ def _test_run(dtype): mod_pad = [] mod_data = data for axis, (pad_x, pad_y) in enumerate(pad): - # if pad_x >= 0 and pad_y >= 0: - # mod_pad.append((pad_x, pad_y)) indices = range(dshape[axis]) if pad_x < 0: indices = indices[abs(pad_x) :] From 46411a617a6da5cde0bdc37f6be9b63c73da464f Mon Sep 17 00:00:00 2001 From: Ritwik Das Date: Fri, 29 Jan 2021 20:30:35 -0800 Subject: [PATCH 05/10] PR Comments --- tests/python/relay/test_op_level2.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/python/relay/test_op_level2.py b/tests/python/relay/test_op_level2.py index 6af201cddf7b..141e0ac45012 100644 --- a/tests/python/relay/test_op_level2.py +++ b/tests/python/relay/test_op_level2.py @@ -1175,14 +1175,12 @@ def test_pad_infer_type(): n, c, h, w = 1, 2, 3, 4 t = relay.var("t", relay.TensorType((n, c, h, w), "float32")) y = relay.nn.pad(t, ((1, 1), (2, 2), (3, 3), (4, 4))) - "pad_width=" in y.astext() yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((3, 6, 9, 12), "float32") n, c, h, w = 4, 6, 3, 5 t = relay.var("t", relay.TensorType((n, c, h, w), "float32")) y = relay.nn.pad(t, ((-1, -1), (2, -2), (0, -3), (4, 4)), pad_mode="reflect") - "pad_width=" in y.astext() yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((2, 6, 0, 13), "float32") From ff302709caa1d4275ddb741084bafbfa75d69ab1 Mon Sep 17 00:00:00 2001 From: Ritwik Das Date: Mon, 1 Feb 2021 15:06:48 -0800 Subject: [PATCH 06/10] Update pad.cc --- src/relay/op/nn/pad.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/relay/op/nn/pad.cc b/src/relay/op/nn/pad.cc index f90127a85bbe..c6b987eb42aa 100644 --- a/src/relay/op/nn/pad.cc +++ b/src/relay/op/nn/pad.cc @@ -142,6 +142,10 @@ bool PadRel(const Array& types, int num_inputs, const Attrs& attrs, if (!data->shape[i].as()) { auto padding = tir::make_const(data->shape[i].dtype(), *width1 + *width2); oshape.push_back(data->shape[i] + padding); + if (tir::as_const_int(data->shape[i])) { + ICHECK(topi::detail::GetConstInt(data->shape[i] + padding) >= 0) + << "Output shape post padding should be positive but got " << data->shape[i] + padding; + } } else { oshape.push_back(data->shape[i]); } From 102b040c179f83fce7bde153c23ab5e1ad4f2a07 Mon Sep 17 00:00:00 2001 From: Ritwik Das Date: Tue, 2 Feb 2021 12:54:40 -0800 Subject: [PATCH 07/10] Address PR Comments --- tests/python/relay/test_op_level2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/relay/test_op_level2.py b/tests/python/relay/test_op_level2.py index 141e0ac45012..1a1f451f4c74 100644 --- a/tests/python/relay/test_op_level2.py +++ b/tests/python/relay/test_op_level2.py @@ -1202,7 +1202,7 @@ def test_pad_infer_type(): def test_pad_run(): def _test_run(dtype): dshape_list = [(4, 10, 7, 7), (4, 6, 3, 5)] - pad_list = [((1, 1), (2, 2), (3, 3), (4, 4)), ((-1, -1), (2, -2), (0, -3), (4, 4))] + pad_list = [((1, 1), (2, 2), (3, 3), (4, 4)), ((-1, -1), (2, -2), (0, -2), (4, 4))] for dshape, pad in zip(dshape_list, pad_list): x = relay.var("x", shape=dshape) From b6d8bf16739b5e1d597476e6e84ed8b420607791 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 3 Feb 2021 04:19:32 +0000 Subject: [PATCH 08/10] CI Error From 65a55832b0efb9e8162a14b0e9767661c4e11f71 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 4 Feb 2021 20:37:36 +0000 Subject: [PATCH 09/10] CI Error From 785c534f46661f8d49b3becda89e608f056484ed Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 5 Feb 2021 00:07:51 +0000 Subject: [PATCH 10/10] CI Error