From 12fe9c14464600dbb3deab64cb8769afe85fd76b Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 14:13:50 +0800 Subject: [PATCH 01/13] reject invalid input_shape --- python/tvm/relay/frontend/keras.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py index 1913d4a2681a..93743ae77e66 100644 --- a/python/tvm/relay/frontend/keras.py +++ b/python/tvm/relay/frontend/keras.py @@ -1429,6 +1429,12 @@ def _check_model_is_tf_keras(): def _convert_input_layer(keras_layer): input_name = keras_layer.name input_shape = shape[input_name] if shape is not None and input_name in shape else None + if len(input_shape) > 1 and any(dim <= 0 for dim in input_shape[1:]): + msg = ( + "Expected input's non-batch dimensions to have positive length, " + f"but the input has a shape of {input_shape}" + ) + raise RuntimeError(msg) etab.set_expr(input_name, new_var(input_name, shape=input_shape)) def _convert_layer(keras_layer, etab, scope=""): From 21fae08d16672024aeb60e3a52ae0abee14d1358 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 14:28:23 +0800 Subject: [PATCH 02/13] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 50a0e9850559..4582ac447936 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -297,6 +297,12 @@ def test_forward_pool(self, keras_mod): y = keras_mod.layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(data) keras_model = keras_mod.models.Model(data, y) verify_keras_frontend(keras_model) + # reject the invalid input shape + data = keras_mod.layers.Input(shape=(0, 3, 6, 4)) + x = keras_mod.layers.GlobalAveragePooling3D()(data) + keras_model = keras_mod.models.Model(data, x) + with pytest.raises(RuntimeError): + verify_model(keras_model) def test_forward_conv1d(self, keras_mod): """test_forward_conv1d""" From 676d51bd52b73aef108e44dabbeb1733146c7c95 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 16:17:49 +0800 Subject: [PATCH 03/13] Update keras.py --- python/tvm/relay/frontend/keras.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py index 93743ae77e66..94315e437ff1 100644 --- a/python/tvm/relay/frontend/keras.py +++ b/python/tvm/relay/frontend/keras.py @@ -1430,11 +1430,11 @@ def _convert_input_layer(keras_layer): input_name = keras_layer.name input_shape = shape[input_name] if shape is not None and input_name in shape else None if len(input_shape) > 1 and any(dim <= 0 for dim in input_shape[1:]): - msg = ( - "Expected input's non-batch dimensions to have positive length, " - f"but the input has a shape of {input_shape}" - ) - raise RuntimeError(msg) + msg = ( + "Expected input's non-batch dimensions to have positive length, " + f"but the input has a shape of {input_shape}" + ) + raise RuntimeError(msg) etab.set_expr(input_name, new_var(input_name, shape=input_shape)) def _convert_layer(keras_layer, etab, scope=""): From 2c3ec7f1257e983130a00528b8677a4a39811f68 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 17:49:58 +0800 Subject: [PATCH 04/13] Update keras.py --- python/tvm/relay/frontend/keras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py index 94315e437ff1..8760f3a667ba 100644 --- a/python/tvm/relay/frontend/keras.py +++ b/python/tvm/relay/frontend/keras.py @@ -1431,7 +1431,7 @@ def _convert_input_layer(keras_layer): input_shape = shape[input_name] if shape is not None and input_name in shape else None if len(input_shape) > 1 and any(dim <= 0 for dim in input_shape[1:]): msg = ( - "Expected input's non-batch dimensions to have positive length, " + "Expected input's non-batch dimensions to have positive length, " f"but the input has a shape of {input_shape}" ) raise RuntimeError(msg) From cc8d9ed83a1275ac15f483a92b97e46f777d2951 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 18:24:35 +0800 Subject: [PATCH 05/13] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 4582ac447936..fb8412afed20 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -302,7 +302,7 @@ def test_forward_pool(self, keras_mod): x = keras_mod.layers.GlobalAveragePooling3D()(data) keras_model = keras_mod.models.Model(data, x) with pytest.raises(RuntimeError): - verify_model(keras_model) + verify_keras_frontend(keras_model) def test_forward_conv1d(self, keras_mod): """test_forward_conv1d""" From bac2588e07f4ff9e282d740499283c1d8146dc15 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 19:24:34 +0800 Subject: [PATCH 06/13] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index fb8412afed20..246ec6659c95 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -32,6 +32,7 @@ from tvm import relay from tvm.contrib import graph_executor import tvm.testing +from tvm.relay.backend import Runtime if tf.executing_eagerly(): GPUS = tf.config.experimental.list_physical_devices("GPU") From 5961b1ec10e7f3fce8ab8204db8af67b9c2a0b71 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 21:07:33 +0800 Subject: [PATCH 07/13] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 246ec6659c95..890004a08433 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -32,7 +32,7 @@ from tvm import relay from tvm.contrib import graph_executor import tvm.testing -from tvm.relay.backend import Runtime +import pytest if tf.executing_eagerly(): GPUS = tf.config.experimental.list_physical_devices("GPU") @@ -48,10 +48,10 @@ def pytest_generate_tests(metafunc): """ - This function generates the list of tests for pytest, based + This function generates the list of tests for , based on scenarios that will change the parameters in which the tests use to run. - https://docs.pytest.org/en/latest/example/parametrize.html + https://docs..org/en/latest/example/parametrize.html """ idlist = [] argvalues = [] @@ -252,7 +252,7 @@ def test_forward_activations_except(self, keras_mod): for act_func in act_funcs: layer = act_func(data) keras_model = keras_mod.models.Model(data, layer) - with pytest.raises(tvm.error.OpAttributeInvalid): + with .raises(tvm.error.OpAttributeInvalid): verify_keras_frontend(keras_model) def test_forward_dense(self, keras_mod): From e13dee012b2e433be473d21184b0700705b3b4a1 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 21:09:56 +0800 Subject: [PATCH 08/13] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 890004a08433..c412502ba810 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -48,10 +48,10 @@ def pytest_generate_tests(metafunc): """ - This function generates the list of tests for , based + This function generates the list of tests for pytest, based on scenarios that will change the parameters in which the tests use to run. - https://docs..org/en/latest/example/parametrize.html + https://docs.pytest.org/en/latest/example/parametrize.html """ idlist = [] argvalues = [] @@ -252,7 +252,7 @@ def test_forward_activations_except(self, keras_mod): for act_func in act_funcs: layer = act_func(data) keras_model = keras_mod.models.Model(data, layer) - with .raises(tvm.error.OpAttributeInvalid): + with pytest.raises(tvm.error.OpAttributeInvalid): verify_keras_frontend(keras_model) def test_forward_dense(self, keras_mod): From ab170eb6451e9851cc77df287759f6d5c5b08ba8 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 21:13:00 +0800 Subject: [PATCH 09/13] Update keras.py --- python/tvm/relay/frontend/keras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py index 8760f3a667ba..c60c7c6b70af 100644 --- a/python/tvm/relay/frontend/keras.py +++ b/python/tvm/relay/frontend/keras.py @@ -1434,7 +1434,7 @@ def _convert_input_layer(keras_layer): "Expected input's non-batch dimensions to have positive length, " f"but the input has a shape of {input_shape}" ) - raise RuntimeError(msg) + raise ValueError(msg) etab.set_expr(input_name, new_var(input_name, shape=input_shape)) def _convert_layer(keras_layer, etab, scope=""): From 843e150c787845afa2c26cddd56fb83e29757096 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 21:13:20 +0800 Subject: [PATCH 10/13] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index c412502ba810..553d27290aef 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -32,7 +32,6 @@ from tvm import relay from tvm.contrib import graph_executor import tvm.testing -import pytest if tf.executing_eagerly(): GPUS = tf.config.experimental.list_physical_devices("GPU") @@ -302,7 +301,7 @@ def test_forward_pool(self, keras_mod): data = keras_mod.layers.Input(shape=(0, 3, 6, 4)) x = keras_mod.layers.GlobalAveragePooling3D()(data) keras_model = keras_mod.models.Model(data, x) - with pytest.raises(RuntimeError): + with pytest.raises(ValueError): verify_keras_frontend(keras_model) def test_forward_conv1d(self, keras_mod): From efcbe008bbe86cdcc61f97df36ace62ffb0fb1cd Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 17 Jul 2023 23:22:00 +0800 Subject: [PATCH 11/13] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 553d27290aef..9cb7dd386715 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -288,6 +288,7 @@ def test_forward_sequential(self, keras_mod): verify_keras_frontend(keras_model) def test_forward_pool(self, keras_mod): + """test_forward_pool""" data = keras_mod.layers.Input(shape=(32, 32, 1)) # maxpool x = keras_mod.layers.MaxPooling2D((3, 3), strides=(1, 1), padding="same")(data) From a6927e4bd55abb31795276a7ad75d4e4cbc20015 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Tue, 18 Jul 2023 10:12:45 +0800 Subject: [PATCH 12/13] Update keras.py --- python/tvm/relay/frontend/keras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py index c60c7c6b70af..6c7853854c53 100644 --- a/python/tvm/relay/frontend/keras.py +++ b/python/tvm/relay/frontend/keras.py @@ -1429,7 +1429,7 @@ def _check_model_is_tf_keras(): def _convert_input_layer(keras_layer): input_name = keras_layer.name input_shape = shape[input_name] if shape is not None and input_name in shape else None - if len(input_shape) > 1 and any(dim <= 0 for dim in input_shape[1:]): + if input_shape and len(input_shape) > 1 and any(dim <= 0 for dim in input_shape[1:]): msg = ( "Expected input's non-batch dimensions to have positive length, " f"but the input has a shape of {input_shape}" From 6fbcd4ad8ad87c30ec5cd8603210a2d6246d0f41 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Tue, 18 Jul 2023 15:27:43 +0800 Subject: [PATCH 13/13] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 9cb7dd386715..b4ebe7b0ecda 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -32,6 +32,7 @@ from tvm import relay from tvm.contrib import graph_executor import tvm.testing +import pytest if tf.executing_eagerly(): GPUS = tf.config.experimental.list_physical_devices("GPU")