ophirfrish commented on a change in pull request #9074:
URL: https://github.com/apache/tvm/pull/9074#discussion_r724306595
##########
File path: python/tvm/driver/tvmc/frontends.py
##########
@@ -249,13 +314,12 @@ def suffixes():
return ["pth", "zip"]
def load(self, path, shape_dict=None, **kwargs):
- # pylint: disable=C0415
- import torch
+ tc = import_torch()
Review comment:
Done
##########
File path: python/tvm/driver/tvmc/frontends.py
##########
@@ -88,10 +88,76 @@ def import_keras():
from tensorflow import keras
return tf, keras
+ except ImportError:
+ err = (
+ "Error: Tensorflow is required and was not found.\n"
+ 'Please install it using "pip install tensorflow".'
+ )
+ raise TVMCException(str(err))
finally:
sys.stderr = stderr
+def import_onnx():
+ """Lazy import function for onnx"""
+ try:
+ # pylint: disable=C0415
+ import onnx as _onnx
+ except ImportError:
+ err = (
+ "Error: ONNX is required and was not found.\n"
+ 'Please install it using "pip install onnx".'
+ )
+ raise TVMCException(str(err))
+
+ return _onnx
+
+
+def import_tensorflow():
+ """Lazy import function for tensorflow"""
+ try:
+ # pylint: disable=C0415
+ import tensorflow as tf
+ except ImportError:
+ err = (
+ "Error: Tensorflow is required and was not found.\n"
+ 'Please install it using "pip install tensorflow".'
+ )
+ raise TVMCException(str(err))
+
+ return tf
+
+
+def import_torch():
+ """Lazy import function for torch"""
+ try:
+ # pylint: disable=C0415
+ import torch as tc
+ except ImportError:
+ err = (
+ "Error: Torch is required and was not found.\n"
+ 'Please install it using "pip install torch".'
+ )
+ raise TVMCException(str(err))
+
+ return tc
+
+
+def import_tflite():
+ """Lazy import function for tflite.Model"""
+ try:
+ # pylint: disable=C0415
+ import tflite.Model as model
+ except ImportError:
+ err = (
+ "Error: tflite is required and was not found.\n"
+ 'Please install it using "pip install tflite".'
+ )
+ raise TVMCException(str(err))
Review comment:
Done
##########
File path: tests/python/driver/tvmc/test_frontends.py
##########
@@ -211,3 +212,43 @@ def
test_load_model___wrong_language__to_pytorch(tflite_mobilenet_v1_1_quant):
model_format="pytorch",
shape_dict={"input": [1, 3, 224, 224]},
)
+
+
[email protected]("tvm.driver.tvmc.frontends.import_keras")
+def test_import_keras_friendly_message(import_keras_mock, keras_resnet50):
+ import_keras_mock.side_effect = TVMCException("keras is not installed")
Review comment:
Done, I have changed all unit tests yo use monkeypatch
##########
File path: tests/python/driver/tvmc/test_frontends.py
##########
@@ -211,3 +212,43 @@ def
test_load_model___wrong_language__to_pytorch(tflite_mobilenet_v1_1_quant):
model_format="pytorch",
shape_dict={"input": [1, 3, 224, 224]},
)
+
+
[email protected]("tvm.driver.tvmc.frontends.import_keras")
+def test_import_keras_friendly_message(import_keras_mock, keras_resnet50):
+ import_keras_mock.side_effect = TVMCException("keras is not installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(keras_resnet50, model_format="keras")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_onnx")
+def test_import_onnx_friendly_message(import_onnx_mock, onnx_resnet50):
Review comment:
I have added the suggested comment to all tests
##########
File path: tests/python/driver/tvmc/test_frontends.py
##########
@@ -211,3 +212,43 @@ def
test_load_model___wrong_language__to_pytorch(tflite_mobilenet_v1_1_quant):
model_format="pytorch",
shape_dict={"input": [1, 3, 224, 224]},
)
+
+
[email protected]("tvm.driver.tvmc.frontends.import_keras")
+def test_import_keras_friendly_message(import_keras_mock, keras_resnet50):
+ import_keras_mock.side_effect = TVMCException("keras is not installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(keras_resnet50, model_format="keras")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_onnx")
+def test_import_onnx_friendly_message(import_onnx_mock, onnx_resnet50):
+ import_onnx_mock.side_effect = TVMCException("onnx is not installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(onnx_resnet50, model_format="onnx")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_tensorflow")
+def test_import_tensorflow_friendly_message(import_tensorflow_mock,
pb_mobilenet_v1_1_quant):
+ import_tensorflow_mock.side_effect = TVMCException("tensorflow is not
installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(pb_mobilenet_v1_1_quant,
model_format="pb")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_torch")
+def test_import_torch_friendly_message(import_torch_mock, pytorch_resnet18):
Review comment:
Added the suggested comment
##########
File path: tests/python/driver/tvmc/test_frontends.py
##########
@@ -211,3 +212,43 @@ def
test_load_model___wrong_language__to_pytorch(tflite_mobilenet_v1_1_quant):
model_format="pytorch",
shape_dict={"input": [1, 3, 224, 224]},
)
+
+
[email protected]("tvm.driver.tvmc.frontends.import_keras")
+def test_import_keras_friendly_message(import_keras_mock, keras_resnet50):
+ import_keras_mock.side_effect = TVMCException("keras is not installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(keras_resnet50, model_format="keras")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_onnx")
+def test_import_onnx_friendly_message(import_onnx_mock, onnx_resnet50):
+ import_onnx_mock.side_effect = TVMCException("onnx is not installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(onnx_resnet50, model_format="onnx")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_tensorflow")
+def test_import_tensorflow_friendly_message(import_tensorflow_mock,
pb_mobilenet_v1_1_quant):
Review comment:
Added the suggested comment
##########
File path: tests/python/driver/tvmc/test_frontends.py
##########
@@ -211,3 +212,43 @@ def
test_load_model___wrong_language__to_pytorch(tflite_mobilenet_v1_1_quant):
model_format="pytorch",
shape_dict={"input": [1, 3, 224, 224]},
)
+
+
[email protected]("tvm.driver.tvmc.frontends.import_keras")
+def test_import_keras_friendly_message(import_keras_mock, keras_resnet50):
+ import_keras_mock.side_effect = TVMCException("keras is not installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(keras_resnet50, model_format="keras")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_onnx")
+def test_import_onnx_friendly_message(import_onnx_mock, onnx_resnet50):
+ import_onnx_mock.side_effect = TVMCException("onnx is not installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(onnx_resnet50, model_format="onnx")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_tensorflow")
+def test_import_tensorflow_friendly_message(import_tensorflow_mock,
pb_mobilenet_v1_1_quant):
+ import_tensorflow_mock.side_effect = TVMCException("tensorflow is not
installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(pb_mobilenet_v1_1_quant,
model_format="pb")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_torch")
+def test_import_torch_friendly_message(import_torch_mock, pytorch_resnet18):
+ import_torch_mock.side_effect = TVMCException("torch is not installed")
+
+ with pytest.raises(TVMCException) as e:
+ _ = tvmc.frontends.load_model(pytorch_resnet18, model_format="pytorch")
+
+
[email protected]("tvm.driver.tvmc.frontends.import_tflite")
+def test_import_tflite_friendly_message(import_tflite_mock,
tflite_mobilenet_v1_1_quant):
Review comment:
Added the suggested comment
--
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]