From 5fb7d13efea2b399c16444ed3e3f49b32258b837 Mon Sep 17 00:00:00 2001 From: Akira Maruoka Date: Wed, 26 May 2021 04:49:05 +0900 Subject: [PATCH 1/6] Fixed access to pad_value --- python/tvm/relay/op/contrib/tensorrt.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/op/contrib/tensorrt.py b/python/tvm/relay/op/contrib/tensorrt.py index cbe6a22f4a4d..03ee9d7d7ceb 100644 --- a/python/tvm/relay/op/contrib/tensorrt.py +++ b/python/tvm/relay/op/contrib/tensorrt.py @@ -723,8 +723,12 @@ def pad_annotate_fn(expr): # pylint: disable=unused-variable if attrs.pad_mode != "constant": logger.info("nn.pad: pad mode is %s but must be constant.", attrs.pad_mode) return False - if float(attrs.pad_value) != 0.0: - logger.info("nn.pad: pad value is %f but must be 0.0.", float(attrs.pad_value)) + if ( + isinstance(args[1], relay.Constant) + and len(args[1].checked_type.shape) == 0 + and args[1].data.numpy().item() != 0.0 + ): + logger.info("nn.pad: pad value is %s but must be 0.0.", args[1]) return False if len(attrs.pad_width) not in [4, 5]: logger.info("nn.pad: can only pad 4D or 5D inputs") From 6e0bbc036669a125776db34be6887c7f46b92cb4 Mon Sep 17 00:00:00 2001 From: Akira Maruoka Date: Wed, 26 May 2021 05:55:39 +0900 Subject: [PATCH 2/6] Added test cases --- tests/python/contrib/test_tensorrt.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/python/contrib/test_tensorrt.py b/tests/python/contrib/test_tensorrt.py index f9912c9674e5..17a9657746dd 100644 --- a/tests/python/contrib/test_tensorrt.py +++ b/tests/python/contrib/test_tensorrt.py @@ -793,17 +793,19 @@ def get_graph(x_shape=(1, 16)): def test_pad(): - def get_graph(x_shape, pad_width): + def get_graph(x_shape, pad_width, pad_value=0.0): x = relay.var("x", shape=(x_shape), dtype="float32") - out = relay.nn.pad(x, pad_width=pad_width) + out = relay.nn.pad(x, pad_width=pad_width, pad_value=pad_value) f = relay.Function([x], out) return f, {"x": x_shape}, [] run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [0, 0], [0, 0]])) run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [1, 1], [1, 1]])) + run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [1, 1], [1, 1]], pad_value=-1.0e30)) run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [0, 1], [2, 0]])) + run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [0, 1], [2, 0]], pad_value=-1.0e30)) run_and_verify_func(get_graph((1, 8, 3, 16, 16), [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]])) - + run_and_verify_func(get_graph((1, 8, 3, 16, 16), [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], pad_value=-1.0e30)) def test_softmax(): def get_graph(x_shape, axis): From d7af28e5d687ae1abf29a3c0e6f925a67a0dbbcf Mon Sep 17 00:00:00 2001 From: Akira Maruoka Date: Wed, 26 May 2021 06:22:43 +0900 Subject: [PATCH 3/6] Added pad_value into PadOpConverter inputs --- src/runtime/contrib/tensorrt/tensorrt_ops.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/contrib/tensorrt/tensorrt_ops.cc b/src/runtime/contrib/tensorrt/tensorrt_ops.cc index 7197172d73db..020ef866692a 100644 --- a/src/runtime/contrib/tensorrt/tensorrt_ops.cc +++ b/src/runtime/contrib/tensorrt/tensorrt_ops.cc @@ -1057,7 +1057,7 @@ class ReshapeOpConverter : public TensorRTOpConverter { class PadOpConverter : public TensorRTOpConverter { public: - PadOpConverter() : TensorRTOpConverter({kTensor}) {} + PadOpConverter() : TensorRTOpConverter({kTensor, kWeight}) {} void Convert(TensorRTOpConverterParams* params) const { auto input = params->inputs.at(0).tensor; From 9cc90d8fa0fef32486cd8fc37645922939856829 Mon Sep 17 00:00:00 2001 From: Akira Maruoka Date: Wed, 26 May 2021 06:27:51 +0900 Subject: [PATCH 4/6] Check pad_value is Relay.Constant by assertion --- python/tvm/relay/op/contrib/tensorrt.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/python/tvm/relay/op/contrib/tensorrt.py b/python/tvm/relay/op/contrib/tensorrt.py index 03ee9d7d7ceb..e97c1e7ebda3 100644 --- a/python/tvm/relay/op/contrib/tensorrt.py +++ b/python/tvm/relay/op/contrib/tensorrt.py @@ -723,11 +723,8 @@ def pad_annotate_fn(expr): # pylint: disable=unused-variable if attrs.pad_mode != "constant": logger.info("nn.pad: pad mode is %s but must be constant.", attrs.pad_mode) return False - if ( - isinstance(args[1], relay.Constant) - and len(args[1].checked_type.shape) == 0 - and args[1].data.numpy().item() != 0.0 - ): + assert isinstance(args[1], relay.Constant) + if len(args[1].checked_type.shape) == 0 and args[1].data.numpy().item() != 0.0: logger.info("nn.pad: pad value is %s but must be 0.0.", args[1]) return False if len(attrs.pad_width) not in [4, 5]: From e50bc434b6dd952f843f71192a11edc923f57748 Mon Sep 17 00:00:00 2001 From: Akira Maruoka Date: Thu, 27 May 2021 15:04:49 +0900 Subject: [PATCH 5/6] Fixed checking if constant pad_value --- python/tvm/relay/op/contrib/tensorrt.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/op/contrib/tensorrt.py b/python/tvm/relay/op/contrib/tensorrt.py index e97c1e7ebda3..ea8467dd2767 100644 --- a/python/tvm/relay/op/contrib/tensorrt.py +++ b/python/tvm/relay/op/contrib/tensorrt.py @@ -723,8 +723,11 @@ def pad_annotate_fn(expr): # pylint: disable=unused-variable if attrs.pad_mode != "constant": logger.info("nn.pad: pad mode is %s but must be constant.", attrs.pad_mode) return False - assert isinstance(args[1], relay.Constant) - if len(args[1].checked_type.shape) == 0 and args[1].data.numpy().item() != 0.0: + if ( + not isinstance(args[1], relay.Constant) + or len(args[1].checked_type.shape) != 0 + or args[1].data.numpy().item() != 0.0 + ): logger.info("nn.pad: pad value is %s but must be 0.0.", args[1]) return False if len(attrs.pad_width) not in [4, 5]: From 827797a30346b28ef28d21d36d0dc92bea1d3627 Mon Sep 17 00:00:00 2001 From: Akira Maruoka Date: Wed, 9 Jun 2021 04:33:13 +0900 Subject: [PATCH 6/6] Formatted --- python/tvm/relay/op/contrib/tensorrt.py | 2 +- tests/python/contrib/test_tensorrt.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/op/contrib/tensorrt.py b/python/tvm/relay/op/contrib/tensorrt.py index ea8467dd2767..bd9051716969 100644 --- a/python/tvm/relay/op/contrib/tensorrt.py +++ b/python/tvm/relay/op/contrib/tensorrt.py @@ -725,7 +725,7 @@ def pad_annotate_fn(expr): # pylint: disable=unused-variable return False if ( not isinstance(args[1], relay.Constant) - or len(args[1].checked_type.shape) != 0 + or len(args[1].checked_type.shape) != 0 or args[1].data.numpy().item() != 0.0 ): logger.info("nn.pad: pad value is %s but must be 0.0.", args[1]) diff --git a/tests/python/contrib/test_tensorrt.py b/tests/python/contrib/test_tensorrt.py index 17a9657746dd..7ff051a4d38b 100644 --- a/tests/python/contrib/test_tensorrt.py +++ b/tests/python/contrib/test_tensorrt.py @@ -801,11 +801,18 @@ def get_graph(x_shape, pad_width, pad_value=0.0): run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [0, 0], [0, 0]])) run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [1, 1], [1, 1]])) - run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [1, 1], [1, 1]], pad_value=-1.0e30)) + run_and_verify_func( + get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [1, 1], [1, 1]], pad_value=-1.0e30) + ) run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [0, 1], [2, 0]])) - run_and_verify_func(get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [0, 1], [2, 0]], pad_value=-1.0e30)) + run_and_verify_func( + get_graph((1, 8, 16, 16), [[0, 0], [0, 0], [0, 1], [2, 0]], pad_value=-1.0e30) + ) run_and_verify_func(get_graph((1, 8, 3, 16, 16), [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]])) - run_and_verify_func(get_graph((1, 8, 3, 16, 16), [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], pad_value=-1.0e30)) + run_and_verify_func( + get_graph((1, 8, 3, 16, 16), [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], pad_value=-1.0e30) + ) + def test_softmax(): def get_graph(x_shape, axis):