From 482892a28e0b29898b66c6a2c7ca9493a8efae8f Mon Sep 17 00:00:00 2001 From: Yuwen Hu Date: Thu, 15 Sep 2022 15:55:21 +0800 Subject: [PATCH 1/3] Update Trainer to InferenceOptimizer for related examples --- .../inference/pytorch/pytorch_inference_onnx.py | 12 ++++++------ .../inference/pytorch/pytorch_inference_openvino.py | 10 +++++----- .../inference/pytorch/pytorch_quantization.py | 5 +++-- .../inference/pytorch/pytorch_quantization_onnx.py | 8 ++++---- .../pytorch/pytorch_quantization_openvino.py | 8 ++++---- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/python/nano/tutorial/inference/pytorch/pytorch_inference_onnx.py b/python/nano/tutorial/inference/pytorch/pytorch_inference_onnx.py index c86e5ab5c2e..2856ad2f228 100644 --- a/python/nano/tutorial/inference/pytorch/pytorch_inference_onnx.py +++ b/python/nano/tutorial/inference/pytorch/pytorch_inference_onnx.py @@ -20,7 +20,6 @@ # pip install onnx onnxruntime # ``` -import torch import torch from torchvision.models import resnet18 @@ -35,17 +34,18 @@ predictions = y_hat.argmax(dim=1) print(predictions) - # Accelerated Inference Using ONNX Runtime - from bigdl.nano.pytorch import Trainer - ort_model = Trainer.trace(model_ft, - accelerator="onnxruntime", - input_sample=torch.rand(1, 3, 224, 224)) + # Accelerated Inference Using ONNXRuntime + from bigdl.nano.pytorch import InferenceOptimizer + ort_model = InferenceOptimizer.trace(model_ft, + accelerator="onnxruntime", + input_sample=torch.rand(1, 3, 224, 224)) y_hat = ort_model(x) predictions = y_hat.argmax(dim=1) print(predictions) # Save Optimized Model + from bigdl.nano.pytorch import Trainer Trainer.save(ort_model, "./optimized_model") # Load the Optimized Model diff --git a/python/nano/tutorial/inference/pytorch/pytorch_inference_openvino.py b/python/nano/tutorial/inference/pytorch/pytorch_inference_openvino.py index 3655bc70297..233184c593f 100644 --- a/python/nano/tutorial/inference/pytorch/pytorch_inference_openvino.py +++ b/python/nano/tutorial/inference/pytorch/pytorch_inference_openvino.py @@ -20,7 +20,6 @@ # pip install openvino-dev # ``` -import torch import torch from torchvision.models import resnet18 @@ -35,15 +34,16 @@ print(predictions) # Accelerated Inference Using OpenVINO - from bigdl.nano.pytorch import Trainer - ov_model = Trainer.trace(model_ft, - accelerator="openvino", - input_sample=torch.rand(1, 3, 224, 224)) + from bigdl.nano.pytorch import InferenceOptimizer + ov_model = InferenceOptimizer.trace(model_ft, + accelerator="openvino", + input_sample=torch.rand(1, 3, 224, 224)) y_hat = ov_model(x) predictions = y_hat.argmax(dim=1) print(predictions) # Save Optimized Model + from bigdl.nano.pytorch import Trainer Trainer.save(ov_model, "./optimized_model") # Load the Optimized Model diff --git a/python/nano/tutorial/inference/pytorch/pytorch_quantization.py b/python/nano/tutorial/inference/pytorch/pytorch_quantization.py index 5dc7a9e617d..b6ab7c9379e 100644 --- a/python/nano/tutorial/inference/pytorch/pytorch_quantization.py +++ b/python/nano/tutorial/inference/pytorch/pytorch_quantization.py @@ -24,7 +24,6 @@ from torchvision import transforms from torchvision.datasets import OxfordIIITPet from torch.utils.data.dataloader import DataLoader -import torch from torchvision.models import resnet18 from bigdl.nano.pytorch import Trainer from torchmetrics import Accuracy @@ -93,7 +92,9 @@ def finetune_pet_dataset(model_ft): print(predictions) # Static Quantization for PyTorch - q_model = Trainer.quantize(model, calib_dataloader=DataLoader(train_dataset, batch_size=32)) + from bigdl.nano.pytorch import InferenceOptimizer + q_model = InferenceOptimizer.quantize(model, + calib_dataloader=DataLoader(train_dataset, batch_size=32)) # Inference with Quantized Model y_hat = q_model(x) diff --git a/python/nano/tutorial/inference/pytorch/pytorch_quantization_onnx.py b/python/nano/tutorial/inference/pytorch/pytorch_quantization_onnx.py index f86701a4fa6..0428d3510b5 100644 --- a/python/nano/tutorial/inference/pytorch/pytorch_quantization_onnx.py +++ b/python/nano/tutorial/inference/pytorch/pytorch_quantization_onnx.py @@ -25,7 +25,6 @@ from torchvision import transforms from torchvision.datasets import OxfordIIITPet from torch.utils.data.dataloader import DataLoader -import torch from torchvision.models import resnet18 from bigdl.nano.pytorch import Trainer from torchmetrics import Accuracy @@ -94,9 +93,10 @@ def finetune_pet_dataset(model_ft): print(predictions) # Static Quantization for ONNX - q_model = Trainer.quantize(model, - accelerator='onnxruntime', - calib_dataloader=DataLoader(train_dataset, batch_size=32)) + from bigdl.nano.pytorch import InferenceOptimizer + q_model = InferenceOptimizer.quantize(model, + accelerator='onnxruntime', + calib_dataloader=DataLoader(train_dataset, batch_size=32)) # Inference with Quantized Model y_hat = q_model(x) diff --git a/python/nano/tutorial/inference/pytorch/pytorch_quantization_openvino.py b/python/nano/tutorial/inference/pytorch/pytorch_quantization_openvino.py index 1c4b032fcdb..8a2a608d4be 100644 --- a/python/nano/tutorial/inference/pytorch/pytorch_quantization_openvino.py +++ b/python/nano/tutorial/inference/pytorch/pytorch_quantization_openvino.py @@ -24,7 +24,6 @@ from torchvision import transforms from torchvision.datasets import OxfordIIITPet from torch.utils.data.dataloader import DataLoader -import torch from torchvision.models import resnet18 from bigdl.nano.pytorch import Trainer from torchmetrics import Accuracy @@ -93,9 +92,10 @@ def finetune_pet_dataset(model_ft): print(predictions) # Static Quantization for OpenVINO - q_model = Trainer.quantize(model, - accelerator='openvino', - calib_dataloader=DataLoader(train_dataset, batch_size=32)) + from bigdl.nano.pytorch import InferenceOptimizer + q_model = InferenceOptimizer.quantize(model, + accelerator='openvino', + calib_dataloader=DataLoader(train_dataset, batch_size=32)) # Inference with Quantized Model y_hat = q_model(x) From 4a3b933e77b16f63b4d6d882843d7cfe6347c251 Mon Sep 17 00:00:00 2001 From: Yuwen Hu Date: Thu, 15 Sep 2022 15:59:46 +0800 Subject: [PATCH 2/3] Diasble unrelated nano tests --- .github/workflows/nano_howto_guides_tests.yml | 5 +++-- .github/workflows/nano_unit_tests_basic.yml | 5 +++-- .github/workflows/nano_unit_tests_pytorch.yml | 5 +++-- .github/workflows/nano_unit_tests_tensorflow.yml | 5 +++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/nano_howto_guides_tests.yml b/.github/workflows/nano_howto_guides_tests.yml index 0e40a42bff5..217c8d16383 100644 --- a/.github/workflows/nano_howto_guides_tests.yml +++ b/.github/workflows/nano_howto_guides_tests.yml @@ -11,8 +11,9 @@ on: pull_request: branches: [ main ] paths: - - 'python/nano/**' - - '.github/workflows/nano_howto_guides_tests.yml' + #- 'python/nano/**' + #- '.github/workflows/nano_howto_guides_tests.yml' + - 'app/**' # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: diff --git a/.github/workflows/nano_unit_tests_basic.yml b/.github/workflows/nano_unit_tests_basic.yml index a1edf526b13..b98b457371b 100644 --- a/.github/workflows/nano_unit_tests_basic.yml +++ b/.github/workflows/nano_unit_tests_basic.yml @@ -8,8 +8,9 @@ on: pull_request: branches: [ main ] paths: - - 'python/nano/**' - - '.github/workflows/nano_unit_tests_basic.yml' + #- 'python/nano/**' + #- '.github/workflows/nano_unit_tests_basic.yml' + - 'app/**' # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: diff --git a/.github/workflows/nano_unit_tests_pytorch.yml b/.github/workflows/nano_unit_tests_pytorch.yml index 799bc81f7ea..8833747dd48 100644 --- a/.github/workflows/nano_unit_tests_pytorch.yml +++ b/.github/workflows/nano_unit_tests_pytorch.yml @@ -8,8 +8,9 @@ on: pull_request: branches: [ main ] paths: - - 'python/nano/**' - - '.github/workflows/nano_unit_tests_pytorch.yml' + #- 'python/nano/**' + #- '.github/workflows/nano_unit_tests_pytorch.yml' + - 'app/**' # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: diff --git a/.github/workflows/nano_unit_tests_tensorflow.yml b/.github/workflows/nano_unit_tests_tensorflow.yml index b420ad5e8f6..9310c82a6aa 100644 --- a/.github/workflows/nano_unit_tests_tensorflow.yml +++ b/.github/workflows/nano_unit_tests_tensorflow.yml @@ -8,8 +8,9 @@ on: pull_request: branches: [ main ] paths: - - 'python/nano/**' - - '.github/workflows/nano_unit_tests_tensorflow.yml' + #- 'python/nano/**' + #- '.github/workflows/nano_unit_tests_tensorflow.yml' + - 'app/**' # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: From b6cda1c1dc3bffd42795382b21fdfde1fb2816b5 Mon Sep 17 00:00:00 2001 From: Yuwen Hu Date: Thu, 15 Sep 2022 17:05:25 +0800 Subject: [PATCH 3/3] Enable unrelated nano tests again --- .github/workflows/nano_howto_guides_tests.yml | 5 ++--- .github/workflows/nano_unit_tests_basic.yml | 5 ++--- .github/workflows/nano_unit_tests_pytorch.yml | 5 ++--- .github/workflows/nano_unit_tests_tensorflow.yml | 5 ++--- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/.github/workflows/nano_howto_guides_tests.yml b/.github/workflows/nano_howto_guides_tests.yml index 217c8d16383..0e40a42bff5 100644 --- a/.github/workflows/nano_howto_guides_tests.yml +++ b/.github/workflows/nano_howto_guides_tests.yml @@ -11,9 +11,8 @@ on: pull_request: branches: [ main ] paths: - #- 'python/nano/**' - #- '.github/workflows/nano_howto_guides_tests.yml' - - 'app/**' + - 'python/nano/**' + - '.github/workflows/nano_howto_guides_tests.yml' # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: diff --git a/.github/workflows/nano_unit_tests_basic.yml b/.github/workflows/nano_unit_tests_basic.yml index b98b457371b..a1edf526b13 100644 --- a/.github/workflows/nano_unit_tests_basic.yml +++ b/.github/workflows/nano_unit_tests_basic.yml @@ -8,9 +8,8 @@ on: pull_request: branches: [ main ] paths: - #- 'python/nano/**' - #- '.github/workflows/nano_unit_tests_basic.yml' - - 'app/**' + - 'python/nano/**' + - '.github/workflows/nano_unit_tests_basic.yml' # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: diff --git a/.github/workflows/nano_unit_tests_pytorch.yml b/.github/workflows/nano_unit_tests_pytorch.yml index 8833747dd48..799bc81f7ea 100644 --- a/.github/workflows/nano_unit_tests_pytorch.yml +++ b/.github/workflows/nano_unit_tests_pytorch.yml @@ -8,9 +8,8 @@ on: pull_request: branches: [ main ] paths: - #- 'python/nano/**' - #- '.github/workflows/nano_unit_tests_pytorch.yml' - - 'app/**' + - 'python/nano/**' + - '.github/workflows/nano_unit_tests_pytorch.yml' # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: diff --git a/.github/workflows/nano_unit_tests_tensorflow.yml b/.github/workflows/nano_unit_tests_tensorflow.yml index 9310c82a6aa..b420ad5e8f6 100644 --- a/.github/workflows/nano_unit_tests_tensorflow.yml +++ b/.github/workflows/nano_unit_tests_tensorflow.yml @@ -8,9 +8,8 @@ on: pull_request: branches: [ main ] paths: - #- 'python/nano/**' - #- '.github/workflows/nano_unit_tests_tensorflow.yml' - - 'app/**' + - 'python/nano/**' + - '.github/workflows/nano_unit_tests_tensorflow.yml' # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: