From 7acfa1e2f038a1226cc87476f7c253e84558ba85 Mon Sep 17 00:00:00 2001 From: diegofiori Date: Sun, 22 Jan 2023 16:08:05 +0100 Subject: [PATCH 1/7] Fix wrongly sorted import --- .../accelerate/speedster/speedster/api/tests/test_tensorflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/accelerate/speedster/speedster/api/tests/test_tensorflow.py b/apps/accelerate/speedster/speedster/api/tests/test_tensorflow.py index ce039660..714f9b96 100644 --- a/apps/accelerate/speedster/speedster/api/tests/test_tensorflow.py +++ b/apps/accelerate/speedster/speedster/api/tests/test_tensorflow.py @@ -1,6 +1,6 @@ -import cpuinfo from tempfile import TemporaryDirectory +import cpuinfo import pytest import tensorflow as tf from keras.applications import ResNet50 From cb782a86f17691feaa3065a173314eaa732436ee Mon Sep 17 00:00:00 2001 From: diegofiori Date: Sun, 22 Jan 2023 16:13:13 +0100 Subject: [PATCH 2/7] Add save_model function --- nebullvm/operations/inference_learners/utils.py | 0 nebullvm/operations/optimizations/utils.py | 6 ++++++ 2 files changed, 6 insertions(+) create mode 100644 nebullvm/operations/inference_learners/utils.py diff --git a/nebullvm/operations/inference_learners/utils.py b/nebullvm/operations/inference_learners/utils.py new file mode 100644 index 00000000..e69de29b diff --git a/nebullvm/operations/optimizations/utils.py b/nebullvm/operations/optimizations/utils.py index bfa8d8de..c8db649c 100644 --- a/nebullvm/operations/optimizations/utils.py +++ b/nebullvm/operations/optimizations/utils.py @@ -3,6 +3,7 @@ from nebullvm.operations.inference_learners.base import LearnerMetadata + def map_compilers_and_compressors(ignore_list: List, enum_class: Callable): if ignore_list is None: ignore_list = [] @@ -10,5 +11,10 @@ def map_compilers_and_compressors(ignore_list: List, enum_class: Callable): ignore_list = [enum_class(element) for element in ignore_list] return ignore_list + def load_model(path: Union[Path, str]): return LearnerMetadata.read(path).load_model(path) + + +def save_model(model, path: Union[Path, str]): + model.save(path) From b466df506660998341fe1e58e339cda1677da7be Mon Sep 17 00:00:00 2001 From: diegofiori Date: Sun, 22 Jan 2023 16:19:01 +0100 Subject: [PATCH 3/7] Move save and load functions from optimizations to inference_learners section --- .../operations/inference_learners/tensorflow.py | 3 ++- nebullvm/operations/inference_learners/utils.py | 12 ++++++++++++ .../optimizations/tests/test_deepsparse.py | 2 +- .../tests/test_intel_neural_compressor.py | 2 +- .../optimizations/tests/test_onnxruntime.py | 2 +- .../operations/optimizations/tests/test_openvino.py | 3 ++- .../optimizations/tests/test_tensor_rt.py | 2 +- .../optimizations/tests/test_tensorflow.py | 3 ++- .../optimizations/tests/test_torchscript.py | 2 +- nebullvm/operations/optimizations/tests/test_tvm.py | 2 +- nebullvm/operations/optimizations/utils.py | 13 +------------ 11 files changed, 25 insertions(+), 21 deletions(-) diff --git a/nebullvm/operations/inference_learners/tensorflow.py b/nebullvm/operations/inference_learners/tensorflow.py index 29b4e858..a0792c18 100644 --- a/nebullvm/operations/inference_learners/tensorflow.py +++ b/nebullvm/operations/inference_learners/tensorflow.py @@ -2,6 +2,7 @@ from pathlib import Path from typing import Tuple, Union, Dict, Type +import nebullvm.operations.inference_learners.utils from nebullvm.config import TENSORFLOW_BACKEND_FILENAMES from nebullvm.operations.inference_learners.base import ( TensorflowBaseInferenceLearner, @@ -42,7 +43,7 @@ def load(cls, path: Union[Path, str], **kwargs): metadata = LearnerMetadata.read(path) network_parameters = ModelParams(**metadata.network_parameters) input_tfms = metadata.input_tfms - model = tf.keras.models.load_model( + model = nebullvm.operations.inference_learners.utils.load_model( path / TENSORFLOW_BACKEND_FILENAMES["tf_model"] ) device = Device(metadata.device) diff --git a/nebullvm/operations/inference_learners/utils.py b/nebullvm/operations/inference_learners/utils.py index e69de29b..f4cb9b21 100644 --- a/nebullvm/operations/inference_learners/utils.py +++ b/nebullvm/operations/inference_learners/utils.py @@ -0,0 +1,12 @@ +from pathlib import Path +from typing import Union + +from nebullvm.operations.inference_learners.base import LearnerMetadata + + +def load_model(path: Union[Path, str]): + return LearnerMetadata.read(path).load_model(path) + + +def save_model(model, path: Union[Path, str]): + model.save(path) diff --git a/nebullvm/operations/optimizations/tests/test_deepsparse.py b/nebullvm/operations/optimizations/tests/test_deepsparse.py index c85e1887..09bfe843 100644 --- a/nebullvm/operations/optimizations/tests/test_deepsparse.py +++ b/nebullvm/operations/optimizations/tests/test_deepsparse.py @@ -18,7 +18,7 @@ deepsparse_is_available, ) from nebullvm.operations.optimizations.tests.utils import initialize_model -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.tools.base import DeepLearningFramework, Device, ModelCompiler device = Device.CPU diff --git a/nebullvm/operations/optimizations/tests/test_intel_neural_compressor.py b/nebullvm/operations/optimizations/tests/test_intel_neural_compressor.py index 150dc76d..85e83614 100644 --- a/nebullvm/operations/optimizations/tests/test_intel_neural_compressor.py +++ b/nebullvm/operations/optimizations/tests/test_intel_neural_compressor.py @@ -18,7 +18,7 @@ initialize_model, check_model_validity, ) -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.tools.base import ( DeepLearningFramework, QuantizationType, diff --git a/nebullvm/operations/optimizations/tests/test_onnxruntime.py b/nebullvm/operations/optimizations/tests/test_onnxruntime.py index a1117d9d..86a548fe 100644 --- a/nebullvm/operations/optimizations/tests/test_onnxruntime.py +++ b/nebullvm/operations/optimizations/tests/test_onnxruntime.py @@ -17,7 +17,7 @@ initialize_model, check_model_validity, ) -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.tools.base import ( DeepLearningFramework, QuantizationType, diff --git a/nebullvm/operations/optimizations/tests/test_openvino.py b/nebullvm/operations/optimizations/tests/test_openvino.py index 596048d4..a2dbb8c8 100644 --- a/nebullvm/operations/optimizations/tests/test_openvino.py +++ b/nebullvm/operations/optimizations/tests/test_openvino.py @@ -18,7 +18,7 @@ initialize_model, check_model_validity, ) -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.tools.base import ( Device, DeepLearningFramework, @@ -26,6 +26,7 @@ ModelCompiler, ) + @pytest.mark.parametrize( ( "output_library", diff --git a/nebullvm/operations/optimizations/tests/test_tensor_rt.py b/nebullvm/operations/optimizations/tests/test_tensor_rt.py index e6bfb51c..0701565a 100644 --- a/nebullvm/operations/optimizations/tests/test_tensor_rt.py +++ b/nebullvm/operations/optimizations/tests/test_tensor_rt.py @@ -20,7 +20,7 @@ initialize_model, check_model_validity, ) -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.tools.base import ( DeepLearningFramework, QuantizationType, diff --git a/nebullvm/operations/optimizations/tests/test_tensorflow.py b/nebullvm/operations/optimizations/tests/test_tensorflow.py index 276fdd16..9d325cd0 100644 --- a/nebullvm/operations/optimizations/tests/test_tensorflow.py +++ b/nebullvm/operations/optimizations/tests/test_tensorflow.py @@ -17,7 +17,7 @@ initialize_model, check_model_validity, ) -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.tools.base import ( DeepLearningFramework, QuantizationType, @@ -26,6 +26,7 @@ ) from nebullvm.tools.utils import gpu_is_available + @pytest.mark.parametrize( ( "output_library", diff --git a/nebullvm/operations/optimizations/tests/test_torchscript.py b/nebullvm/operations/optimizations/tests/test_torchscript.py index 8dd67dea..41b420a5 100644 --- a/nebullvm/operations/optimizations/tests/test_torchscript.py +++ b/nebullvm/operations/optimizations/tests/test_torchscript.py @@ -16,7 +16,7 @@ initialize_model, check_model_validity, ) -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.tools.base import ( DeepLearningFramework, QuantizationType, diff --git a/nebullvm/operations/optimizations/tests/test_tvm.py b/nebullvm/operations/optimizations/tests/test_tvm.py index 26da2f85..7234666e 100644 --- a/nebullvm/operations/optimizations/tests/test_tvm.py +++ b/nebullvm/operations/optimizations/tests/test_tvm.py @@ -19,7 +19,7 @@ initialize_model, check_model_validity, ) -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.tools.base import ( DeepLearningFramework, QuantizationType, diff --git a/nebullvm/operations/optimizations/utils.py b/nebullvm/operations/optimizations/utils.py index c8db649c..56086063 100644 --- a/nebullvm/operations/optimizations/utils.py +++ b/nebullvm/operations/optimizations/utils.py @@ -1,7 +1,4 @@ -from pathlib import Path -from typing import Callable, List, Union - -from nebullvm.operations.inference_learners.base import LearnerMetadata +from typing import Callable, List def map_compilers_and_compressors(ignore_list: List, enum_class: Callable): @@ -10,11 +7,3 @@ def map_compilers_and_compressors(ignore_list: List, enum_class: Callable): else: ignore_list = [enum_class(element) for element in ignore_list] return ignore_list - - -def load_model(path: Union[Path, str]): - return LearnerMetadata.read(path).load_model(path) - - -def save_model(model, path: Union[Path, str]): - model.save(path) From fa6b41108e39738777d6ca59d2e9fe2854df6649 Mon Sep 17 00:00:00 2001 From: diegofiori Date: Sun, 22 Jan 2023 16:24:13 +0100 Subject: [PATCH 4/7] Add load and save function to speedster --- apps/accelerate/speedster/speedster/__init__.py | 4 ++++ .../speedster/api/tests/test_huggingface.py | 5 ++--- .../speedster/speedster/api/tests/test_onnx.py | 13 ++++++++----- .../speedster/speedster/api/tests/test_pytorch.py | 3 +-- .../speedster/api/tests/test_tensorflow.py | 3 +-- apps/accelerate/speedster/speedster/utils.py | 4 +--- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/apps/accelerate/speedster/speedster/__init__.py b/apps/accelerate/speedster/speedster/__init__.py index 776936c0..479e08e6 100644 --- a/apps/accelerate/speedster/speedster/__init__.py +++ b/apps/accelerate/speedster/speedster/__init__.py @@ -1 +1,5 @@ from speedster.api.functions import optimize_model # noqa: F401 +from nebullvm.operations.inference_learners.utils import ( # noqa: F401 + load_model, + save_model, +) diff --git a/apps/accelerate/speedster/speedster/api/tests/test_huggingface.py b/apps/accelerate/speedster/speedster/api/tests/test_huggingface.py index 3826091b..c70d97b5 100644 --- a/apps/accelerate/speedster/speedster/api/tests/test_huggingface.py +++ b/apps/accelerate/speedster/speedster/api/tests/test_huggingface.py @@ -4,12 +4,11 @@ from nebullvm.operations.inference_learners.huggingface import ( HuggingFaceInferenceLearner, ) -from nebullvm.operations.optimizations.utils import load_model from nebullvm.optional_modules.tensorflow import tensorflow as tf from nebullvm.optional_modules.torch import torch from transformers import AlbertModel, TFAlbertModel, AlbertTokenizer -from speedster import optimize_model +from speedster import optimize_model, load_model def test_torch_huggingface_ort_input_text(): @@ -46,7 +45,7 @@ def test_torch_huggingface_ort_input_text(): ), ) - #save and load + # save and load with TemporaryDirectory() as tmp_dir: optimized_model.save(tmp_dir) loaded_model = load_model(tmp_dir) diff --git a/apps/accelerate/speedster/speedster/api/tests/test_onnx.py b/apps/accelerate/speedster/speedster/api/tests/test_onnx.py index e453c642..ca2b9b6c 100644 --- a/apps/accelerate/speedster/speedster/api/tests/test_onnx.py +++ b/apps/accelerate/speedster/speedster/api/tests/test_onnx.py @@ -18,10 +18,9 @@ NumpyApacheTVMInferenceLearner, ) from nebullvm.operations.optimizations.compilers.utils import tvm_is_available -from nebullvm.operations.optimizations.utils import load_model from torchvision import models -from speedster import optimize_model +from speedster import optimize_model, load_model from speedster.api.tests.utils import torch_to_onnx @@ -57,16 +56,20 @@ def test_onnx_ort(): assert isinstance(loaded_model.get_size(), int) # Try the optimized model - device = torch.device("cuda" if torch.cuda.is_available() else "cpu") + device = torch.device( + "cuda" if torch.cuda.is_available() else "cpu" + ) x = torch.randn(1, 3, 256, 256, requires_grad=False) model.eval() res_original = model(x.to(device)) res_optimized = optimized_model(x.numpy())[0] assert ( - abs((res_original.detach().cpu().numpy() - res_optimized)).max() + abs( + (res_original.detach().cpu().numpy() - res_optimized) + ).max() < 1e-2 - ) + ) def test_onnx_ort_quant(): diff --git a/apps/accelerate/speedster/speedster/api/tests/test_pytorch.py b/apps/accelerate/speedster/speedster/api/tests/test_pytorch.py index 24914164..ede5d95e 100644 --- a/apps/accelerate/speedster/speedster/api/tests/test_pytorch.py +++ b/apps/accelerate/speedster/speedster/api/tests/test_pytorch.py @@ -28,9 +28,8 @@ tvm_is_available, bladedisc_is_available, ) -from nebullvm.operations.optimizations.utils import load_model -from speedster import optimize_model +from speedster import optimize_model, load_model def test_torch_ort(): diff --git a/apps/accelerate/speedster/speedster/api/tests/test_tensorflow.py b/apps/accelerate/speedster/speedster/api/tests/test_tensorflow.py index 714f9b96..2d327fc9 100644 --- a/apps/accelerate/speedster/speedster/api/tests/test_tensorflow.py +++ b/apps/accelerate/speedster/speedster/api/tests/test_tensorflow.py @@ -22,10 +22,9 @@ TensorflowApacheTVMInferenceLearner, ) from nebullvm.operations.optimizations.compilers.utils import tvm_is_available -from nebullvm.operations.optimizations.utils import load_model from nebullvm.tools.utils import gpu_is_available -from speedster import optimize_model +from speedster import optimize_model, load_model # Limit tensorflow gpu memory usage gpus = tf.config.list_physical_devices("GPU") diff --git a/apps/accelerate/speedster/speedster/utils.py b/apps/accelerate/speedster/speedster/utils.py index b13ccdc6..9cd835fb 100644 --- a/apps/accelerate/speedster/speedster/utils.py +++ b/apps/accelerate/speedster/speedster/utils.py @@ -8,7 +8,7 @@ import numpy as np import psutil -from nebullvm.operations.optimizations.utils import load_model +from nebullvm.operations.inference_learners.utils import load_model from nebullvm.optional_modules.torch import Module, torch from nebullvm.optional_modules.utils import ( torch_is_available, @@ -95,5 +95,3 @@ def get_size(self): core_size = self.core.get_size() head_size = np.sum([p.element_size() for p in self.head.parameters()]) return core_size + head_size - - From 1ab42c492065ee5e3131a46051bcc8b03274e887 Mon Sep 17 00:00:00 2001 From: diegofiori Date: Sun, 22 Jan 2023 16:47:15 +0100 Subject: [PATCH 5/7] Update notebooks with save and load functions --- ...ing_Face_PyTorch_BERT_with_Speedster.ipynb | 8 +-- ...ce_PyTorch_DistilBERT_with_Speedster.ipynb | 8 +-- ...ing_Face_PyTorch_GPT2_with_Speedster.ipynb | 8 +-- ..._Face_TensorFlow_BERT_with_Speedster.ipynb | 8 +-- ...elerate_ONNX_ResNet50_with_Speedster.ipynb | 8 +-- ...rate_PyTorch_ResNet50_with_Speedster.ipynb | 8 +-- ...lerate_PyTorch_YOLOv5_with_Speedster.ipynb | 8 +-- ...lerate_PyTorch_YOLOv8_with_Speedster.ipynb | 6 +- ...rate_fast_ai_Resnet34_with_Speedster.ipynb | 6 +- ...e_Tensorflow_ResNet50_with_Speedster.ipynb | 58 +------------------ 10 files changed, 30 insertions(+), 96 deletions(-) diff --git a/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_BERT_with_Speedster.ipynb b/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_BERT_with_Speedster.ipynb index 2f1793fa..b657f8ac 100644 --- a/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_BERT_with_Speedster.ipynb +++ b/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_BERT_with_Speedster.ipynb @@ -222,7 +222,7 @@ }, "outputs": [], "source": [ - "from speedster import optimize_model" + "from speedster import optimize_model, save_model, load_model" ] }, { @@ -572,7 +572,7 @@ "metadata": {}, "outputs": [], "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ] }, { @@ -591,9 +591,7 @@ "metadata": {}, "outputs": [], "source": [ - "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", - "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" + "optimized_model = load_model(\"model_save_path\")" ] }, { diff --git a/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_DistilBERT_with_Speedster.ipynb b/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_DistilBERT_with_Speedster.ipynb index 3aca0bd1..cbb8bbf3 100644 --- a/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_DistilBERT_with_Speedster.ipynb +++ b/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_DistilBERT_with_Speedster.ipynb @@ -222,7 +222,7 @@ }, "outputs": [], "source": [ - "from speedster import optimize_model" + "from speedster import optimize_model, save_model, load_model" ] }, { @@ -572,7 +572,7 @@ "metadata": {}, "outputs": [], "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ] }, { @@ -591,9 +591,7 @@ "metadata": {}, "outputs": [], "source": [ - "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", - "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" + "optimized_model = load_model(\"model_save_path\")" ] }, { diff --git a/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_GPT2_with_Speedster.ipynb b/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_GPT2_with_Speedster.ipynb index 1589c399..f108c405 100644 --- a/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_GPT2_with_Speedster.ipynb +++ b/notebooks/speedster/huggingface/Accelerate_Hugging_Face_PyTorch_GPT2_with_Speedster.ipynb @@ -231,7 +231,7 @@ }, "outputs": [], "source": [ - "from speedster import optimize_model" + "from speedster import optimize_model, save_model, load_model" ] }, { @@ -545,7 +545,7 @@ "metadata": {}, "outputs": [], "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ] }, { @@ -564,9 +564,7 @@ "metadata": {}, "outputs": [], "source": [ - "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", - "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" + "optimized_model = load_model(\"model_save_path\")" ] }, { diff --git a/notebooks/speedster/huggingface/Accelerate_Hugging_Face_TensorFlow_BERT_with_Speedster.ipynb b/notebooks/speedster/huggingface/Accelerate_Hugging_Face_TensorFlow_BERT_with_Speedster.ipynb index 02d8d55d..6a1ce115 100644 --- a/notebooks/speedster/huggingface/Accelerate_Hugging_Face_TensorFlow_BERT_with_Speedster.ipynb +++ b/notebooks/speedster/huggingface/Accelerate_Hugging_Face_TensorFlow_BERT_with_Speedster.ipynb @@ -217,7 +217,7 @@ }, "outputs": [], "source": [ - "from speedster import optimize_model" + "from speedster import optimize_model, save_model, load_model" ] }, { @@ -560,7 +560,7 @@ "metadata": {}, "outputs": [], "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ] }, { @@ -579,9 +579,7 @@ "metadata": {}, "outputs": [], "source": [ - "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", - "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" + "optimized_model = load_model(\"model_save_path\")" ] }, { diff --git a/notebooks/speedster/onnx/Accelerate_ONNX_ResNet50_with_Speedster.ipynb b/notebooks/speedster/onnx/Accelerate_ONNX_ResNet50_with_Speedster.ipynb index 817000b8..cc7a45a7 100644 --- a/notebooks/speedster/onnx/Accelerate_ONNX_ResNet50_with_Speedster.ipynb +++ b/notebooks/speedster/onnx/Accelerate_ONNX_ResNet50_with_Speedster.ipynb @@ -171,7 +171,7 @@ "outputs": [], "source": [ "import numpy as np\n", - "from speedster import optimize_model\n", + "from speedster import optimize_model, save_model, load_model\n", "\n", "# Load a resnet as example\n", "model = \"resnet50-v1-12.onnx\"\n", @@ -540,7 +540,7 @@ "metadata": {}, "outputs": [], "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ] }, { @@ -559,9 +559,7 @@ "metadata": {}, "outputs": [], "source": [ - "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", - "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" + "optimized_model = load_model(\"model_save_path\")" ] }, { diff --git a/notebooks/speedster/pytorch/Accelerate_PyTorch_ResNet50_with_Speedster.ipynb b/notebooks/speedster/pytorch/Accelerate_PyTorch_ResNet50_with_Speedster.ipynb index 50d3b7ac..b4a2aa08 100644 --- a/notebooks/speedster/pytorch/Accelerate_PyTorch_ResNet50_with_Speedster.ipynb +++ b/notebooks/speedster/pytorch/Accelerate_PyTorch_ResNet50_with_Speedster.ipynb @@ -141,7 +141,7 @@ "source": [ "import torch\n", "import torchvision.models as models\n", - "from speedster import optimize_model\n", + "from speedster import optimize_model, save_model, load_model\n", "\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "\n", @@ -464,7 +464,7 @@ "metadata": {}, "outputs": [], "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ] }, { @@ -483,9 +483,7 @@ "metadata": {}, "outputs": [], "source": [ - "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", - "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" + "optimized_model = load_model(\"model_save_path\")" ] }, { diff --git a/notebooks/speedster/pytorch/Accelerate_PyTorch_YOLOv5_with_Speedster.ipynb b/notebooks/speedster/pytorch/Accelerate_PyTorch_YOLOv5_with_Speedster.ipynb index bcd0f6fa..2c48755c 100644 --- a/notebooks/speedster/pytorch/Accelerate_PyTorch_YOLOv5_with_Speedster.ipynb +++ b/notebooks/speedster/pytorch/Accelerate_PyTorch_YOLOv5_with_Speedster.ipynb @@ -231,7 +231,7 @@ }, "outputs": [], "source": [ - "from speedster import optimize_model" + "from speedster import optimize_model, save_model, load_model" ] }, { @@ -565,7 +565,7 @@ "metadata": {}, "outputs": [], "source": [ - "model_optimized.save(\"model_save_path\")" + "save_model(model_optimized, \"model_save_path\")" ] }, { @@ -584,9 +584,7 @@ "metadata": {}, "outputs": [], "source": [ - "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", - "\n", - "model_optimized = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")\n", + "model_optimized = load_model(\"model_save_path\")\n", "final_core = OptimizedYolo(model_optimized, last_layer)\n", "model.model.model = final_core" ] diff --git a/notebooks/speedster/pytorch/Accelerate_PyTorch_YOLOv8_with_Speedster.ipynb b/notebooks/speedster/pytorch/Accelerate_PyTorch_YOLOv8_with_Speedster.ipynb index 8daf127f..436519ea 100644 --- a/notebooks/speedster/pytorch/Accelerate_PyTorch_YOLOv8_with_Speedster.ipynb +++ b/notebooks/speedster/pytorch/Accelerate_PyTorch_YOLOv8_with_Speedster.ipynb @@ -228,7 +228,7 @@ "metadata": {}, "outputs": [], "source": [ - "from speedster import optimize_model\n", + "from speedster import optimize_model, save_model, load_model\n", "from ultralytics import YOLO\n", "\n", "yolo = YOLO('yolov8s.pt')\n", @@ -279,7 +279,7 @@ "metadata": {}, "outputs": [], "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ] }, { @@ -300,7 +300,7 @@ "source": [ "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")\n", + "optimized_model = load_model(\"model_save_path\")\n", "optimized_wrapper = OptimizedYOLO(optimized_model)" ] }, diff --git a/notebooks/speedster/pytorch/Accelerate_fast_ai_Resnet34_with_Speedster.ipynb b/notebooks/speedster/pytorch/Accelerate_fast_ai_Resnet34_with_Speedster.ipynb index 1e0267ff..8b7e2060 100644 --- a/notebooks/speedster/pytorch/Accelerate_fast_ai_Resnet34_with_Speedster.ipynb +++ b/notebooks/speedster/pytorch/Accelerate_fast_ai_Resnet34_with_Speedster.ipynb @@ -324,7 +324,7 @@ "source": [ "import torch\n", "import torchvision.models as models\n", - "from speedster import optimize_model" + "from speedster import optimize_model, save_model, load_model" ] }, { @@ -506,7 +506,7 @@ "metadata": {}, "outputs": [], "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ] }, { @@ -527,7 +527,7 @@ "source": [ "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" + "optimized_model = load_model(\"model_save_path\")" ] }, { diff --git a/notebooks/speedster/tensorflow/Accelerate_Tensorflow_ResNet50_with_Speedster.ipynb b/notebooks/speedster/tensorflow/Accelerate_Tensorflow_ResNet50_with_Speedster.ipynb index 98579137..3ce124c7 100644 --- a/notebooks/speedster/tensorflow/Accelerate_Tensorflow_ResNet50_with_Speedster.ipynb +++ b/notebooks/speedster/tensorflow/Accelerate_Tensorflow_ResNet50_with_Speedster.ipynb @@ -161,7 +161,7 @@ "# If you encountered any error, run the cell again\n", "import tensorflow as tf\n", "from tensorflow.keras.applications.resnet50 import ResNet50\n", - "from speedster import optimize_model\n", + "from speedster import optimize_model, save_model, load_model\n", "\n", "# Load a resnet as example\n", "model = ResNet50()\n", @@ -450,7 +450,7 @@ { "cell_type": "code", "source": [ - "optimized_model.save(\"model_save_path\")" + "save_model(optimized_model, \"model_save_path\")" ], "metadata": { "id": "3M565P-zzaFB" @@ -473,7 +473,7 @@ "source": [ "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" + "optimized_model = load_model(\"model_save_path\")" ], "metadata": { "id": "zOQ88SY_zg-A" @@ -481,58 +481,6 @@ "execution_count": null, "outputs": [] }, - { - "cell_type": "markdown", - "source": [ - "## Save and reload the optimized model" - ], - "metadata": { - "id": "GzDQNhMO1Cxn" - } - }, - { - "cell_type": "markdown", - "source": [ - "We can easily save to disk the optimized model with the following line:" - ], - "metadata": { - "id": "JGqHm6FC1Cxo" - } - }, - { - "cell_type": "code", - "source": [ - "optimized_model.save(\"model_save_path\")" - ], - "metadata": { - "id": "EovNfO451Cxo" - }, - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "source": [ - "We can then load again the model:\n", - "\n" - ], - "metadata": { - "id": "hU0lv_121Cxo" - } - }, - { - "cell_type": "code", - "source": [ - "from nebullvm.operations.inference_learners.base import LearnerMetadata\n", - "\n", - "optimized_model = LearnerMetadata.read(\"model_save_path\").load_model(\"model_save_path\")" - ], - "metadata": { - "id": "xQBMMK5b1Cxo" - }, - "execution_count": null, - "outputs": [] - }, { "attachments": {}, "cell_type": "markdown", From 0a408c4ceb030bdc25597b77a52361e49b8bbafd Mon Sep 17 00:00:00 2001 From: diegofiori Date: Sun, 22 Jan 2023 18:09:52 +0100 Subject: [PATCH 6/7] Fix wrong replacement --- nebullvm/operations/inference_learners/tensorflow.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nebullvm/operations/inference_learners/tensorflow.py b/nebullvm/operations/inference_learners/tensorflow.py index a0792c18..29b4e858 100644 --- a/nebullvm/operations/inference_learners/tensorflow.py +++ b/nebullvm/operations/inference_learners/tensorflow.py @@ -2,7 +2,6 @@ from pathlib import Path from typing import Tuple, Union, Dict, Type -import nebullvm.operations.inference_learners.utils from nebullvm.config import TENSORFLOW_BACKEND_FILENAMES from nebullvm.operations.inference_learners.base import ( TensorflowBaseInferenceLearner, @@ -43,7 +42,7 @@ def load(cls, path: Union[Path, str], **kwargs): metadata = LearnerMetadata.read(path) network_parameters = ModelParams(**metadata.network_parameters) input_tfms = metadata.input_tfms - model = nebullvm.operations.inference_learners.utils.load_model( + model = tf.keras.models.load_model( path / TENSORFLOW_BACKEND_FILENAMES["tf_model"] ) device = Device(metadata.device) From 011a732d9195d392a5c7a98990abf35dfd47c935 Mon Sep 17 00:00:00 2001 From: diegofiori Date: Mon, 23 Jan 2023 23:14:38 +0100 Subject: [PATCH 7/7] Update release version --- apps/accelerate/speedster/setup.py | 4 ++-- apps/accelerate/speedster/speedster/root_op.py | 2 +- nebullvm/config.py | 2 +- setup.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/accelerate/speedster/setup.py b/apps/accelerate/speedster/setup.py index f35b02e8..440722be 100644 --- a/apps/accelerate/speedster/setup.py +++ b/apps/accelerate/speedster/setup.py @@ -3,7 +3,7 @@ REQUIREMENTS = [ - "nebullvm>=0.7.3", + "nebullvm>=0.8.0", "tabulate>=0.8.0", ] @@ -12,7 +12,7 @@ setup( name="speedster", - version="0.1.3", + version="0.2.0", packages=find_packages(), install_requires=REQUIREMENTS, long_description=long_description, diff --git a/apps/accelerate/speedster/speedster/root_op.py b/apps/accelerate/speedster/speedster/root_op.py index 3e99fb53..4af552bf 100644 --- a/apps/accelerate/speedster/speedster/root_op.py +++ b/apps/accelerate/speedster/speedster/root_op.py @@ -71,7 +71,7 @@ SPEEDSTER_FEEDBACK_COLLECTOR = FeedbackCollector( url="https://nebuly.cloud/v1/store_speedster_results", disable_telemetry_environ_var="SPEEDSTER_DISABLE_TELEMETRY", - app_version="0.1.3", + app_version="0.2.0", ) diff --git a/nebullvm/config.py b/nebullvm/config.py index 753f9682..33c0af52 100644 --- a/nebullvm/config.py +++ b/nebullvm/config.py @@ -1,7 +1,7 @@ from nebullvm.optional_modules.torch import torch -VERSION = "0.7.3" +VERSION = "0.8.0" LEARNER_METADATA_FILENAME = "metadata.json" ONNX_OPSET_VERSION = 13 NEBULLVM_DEBUG_FILE = "nebullvm_debug.json" diff --git a/setup.py b/setup.py index cb19863c..80247692 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ setup( name="nebullvm", - version="0.7.3", + version="0.8.0", packages=find_packages(), install_requires=REQUIREMENTS, long_description=long_description,