Skip to content

Commit

Permalink
Merge pull request #159 from nebuly-ai/add_save_function
Browse files Browse the repository at this point in the history
Speedster: Add save function and edit notebooks
  • Loading branch information
diegofiori authored Jan 23, 2023
2 parents 63a31fe + 011a732 commit 20ef9d7
Show file tree
Hide file tree
Showing 29 changed files with 73 additions and 128 deletions.
4 changes: 2 additions & 2 deletions apps/accelerate/speedster/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


REQUIREMENTS = [
"nebullvm>=0.7.3",
"nebullvm>=0.8.0",
"tabulate>=0.8.0",
]

Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions apps/accelerate/speedster/speedster/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions apps/accelerate/speedster/speedster/api/tests/test_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cpuinfo
from tempfile import TemporaryDirectory

import cpuinfo
import pytest
import tensorflow as tf
from keras.applications import ResNet50
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion apps/accelerate/speedster/speedster/root_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)


Expand Down
2 changes: 1 addition & 1 deletion nebullvm/config.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
12 changes: 12 additions & 0 deletions nebullvm/operations/inference_learners/utils.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion nebullvm/operations/optimizations/tests/test_deepsparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion nebullvm/operations/optimizations/tests/test_openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Device,
DeepLearningFramework,
Expand Down
2 changes: 1 addition & 1 deletion nebullvm/operations/optimizations/tests/test_tensor_rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion nebullvm/operations/optimizations/tests/test_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion nebullvm/operations/optimizations/tests/test_tvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions nebullvm/operations/optimizations/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from pathlib import Path
from typing import Callable, List, Union
from typing import Callable, List

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 = []
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)
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
},
"outputs": [],
"source": [
"from speedster import optimize_model"
"from speedster import optimize_model, save_model, load_model"
]
},
{
Expand Down Expand Up @@ -572,7 +572,7 @@
"metadata": {},
"outputs": [],
"source": [
"optimized_model.save(\"model_save_path\")"
"save_model(optimized_model, \"model_save_path\")"
]
},
{
Expand All @@ -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\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
},
"outputs": [],
"source": [
"from speedster import optimize_model"
"from speedster import optimize_model, save_model, load_model"
]
},
{
Expand Down Expand Up @@ -572,7 +572,7 @@
"metadata": {},
"outputs": [],
"source": [
"optimized_model.save(\"model_save_path\")"
"save_model(optimized_model, \"model_save_path\")"
]
},
{
Expand All @@ -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\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
},
"outputs": [],
"source": [
"from speedster import optimize_model"
"from speedster import optimize_model, save_model, load_model"
]
},
{
Expand Down Expand Up @@ -545,7 +545,7 @@
"metadata": {},
"outputs": [],
"source": [
"optimized_model.save(\"model_save_path\")"
"save_model(optimized_model, \"model_save_path\")"
]
},
{
Expand All @@ -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\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
},
"outputs": [],
"source": [
"from speedster import optimize_model"
"from speedster import optimize_model, save_model, load_model"
]
},
{
Expand Down Expand Up @@ -560,7 +560,7 @@
"metadata": {},
"outputs": [],
"source": [
"optimized_model.save(\"model_save_path\")"
"save_model(optimized_model, \"model_save_path\")"
]
},
{
Expand All @@ -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\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -540,7 +540,7 @@
"metadata": {},
"outputs": [],
"source": [
"optimized_model.save(\"model_save_path\")"
"save_model(optimized_model, \"model_save_path\")"
]
},
{
Expand All @@ -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\")"
]
},
{
Expand Down
Loading

0 comments on commit 20ef9d7

Please sign in to comment.