From e8584b1f45724ac26a941f6dd29bd5971dd14113 Mon Sep 17 00:00:00 2001 From: theaperdeng Date: Wed, 7 Sep 2022 22:19:35 +0800 Subject: [PATCH] add some updates --- .../cpu_inference_acceleration.py} | 54 +++++++++++++------ .../example/inference-acceleration/readme.md | 20 +++++++ python/chronos/example/nano_qp/tcn/readme.md | 4 -- 3 files changed, 58 insertions(+), 20 deletions(-) rename python/chronos/example/{nano_qp/tcn/train_test.py => inference-acceleration/cpu_inference_acceleration.py} (60%) create mode 100644 python/chronos/example/inference-acceleration/readme.md delete mode 100644 python/chronos/example/nano_qp/tcn/readme.md diff --git a/python/chronos/example/nano_qp/tcn/train_test.py b/python/chronos/example/inference-acceleration/cpu_inference_acceleration.py similarity index 60% rename from python/chronos/example/nano_qp/tcn/train_test.py rename to python/chronos/example/inference-acceleration/cpu_inference_acceleration.py index 474b7105e7d5..3e065aa2087c 100644 --- a/python/chronos/example/nano_qp/tcn/train_test.py +++ b/python/chronos/example/inference-acceleration/cpu_inference_acceleration.py @@ -1,5 +1,20 @@ +# +# Copyright 2016 The BigDL Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# import torch -from bigdl.nano.pytorch import Trainer +from bigdl.chronos.pytorch import TSTrainer as Trainer from bigdl.chronos.model.tcn import model_creator from bigdl.chronos.metric.forecast_metrics import Evaluator from bigdl.chronos.data.repo_dataset import get_public_dataset @@ -8,11 +23,10 @@ def gen_dataloader(): tsdata_train, tsdata_val,\ tsdata_test = get_public_dataset(name='nyc_taxi', - with_split=True, - val_ratio=0.1, - test_ratio=0.1 - ) - # carry out additional customized preprocessing on the dataset. + with_split=True, + val_ratio=0.1, + test_ratio=0.1) + stand = StandardScaler() for tsdata in [tsdata_train, tsdata_val, tsdata_test]: tsdata.deduplicate()\ @@ -25,18 +39,20 @@ def gen_dataloader(): tsdata_valdataloader = tsdata_val.to_torch_data_loader(batch_size=32, shuffle=False) tsdata_testdataloader = tsdata_test.to_torch_data_loader(batch_size=32, shuffle=False) - return tsdata_traindataloader,\ - tsdata_valdataloader,\ - tsdata_testdataloader + return tsdata_traindataloader, tsdata_valdataloader, tsdata_testdataloader def predict_wraper(model, input_sample): model(input_sample) if __name__ == '__main__': + + # create data loaders for train/valid/test tsdata_traindataloader,\ tsdata_valdataloader,\ tsdata_testdataloader = gen_dataloader() + # create a model + # This could be an arbitrary model, we choose to use a built-in model TCN here config = {'input_feature_num':8, 'output_feature_num':1, 'past_seq_len':48, @@ -51,18 +67,24 @@ def predict_wraper(model, input_sample): loss = torch.nn.MSELoss() optimizer = torch.optim.Adam(lr=0.001, params=model.parameters()) lit_model = Trainer.compile(model, loss, optimizer) - trainer = Trainer(max_epochs=3, val_check_interval=1.0, - accelerator='gpu', - devices=1, - ) + + # train the model + # You may use any method to train the model either on gpu or cpu + trainer = Trainer(max_epochs=3, + accelerator='gpu', + devices=1, + ) trainer.fit(lit_model, tsdata_traindataloader, tsdata_testdataloader) - + + # get an input sample x = None for x, _ in tsdata_traindataloader: break input_sample = x[0].unsqueeze(0) - print("original pytorch latency (ms):", Evaluator.get_latency(predict_wraper, lit_model, input_sample)) - + # speed up the model using Chronos TSTrainer speed_model = Trainer.trace(lit_model, accelerator="onnxruntime", input_sample=input_sample) + + # evaluate the model's latency + print("original pytorch latency (ms):", Evaluator.get_latency(predict_wraper, lit_model, input_sample)) print("onnxruntime latency (ms):", Evaluator.get_latency(predict_wraper, speed_model, input_sample)) diff --git a/python/chronos/example/inference-acceleration/readme.md b/python/chronos/example/inference-acceleration/readme.md new file mode 100644 index 000000000000..2fe7848e46ea --- /dev/null +++ b/python/chronos/example/inference-acceleration/readme.md @@ -0,0 +1,20 @@ +# Accelerate the inference speed of model trained on other platform + +## Introduction +Chronos has many built-in models wrapped in forecasters, detectors and simulators optimized on CPU (especially intel CPU) platform. + +While users may want to use their own model or built-in models trained on another platform (e.g. GPU) but prefer to carry out the inferencing process on CPU platform. Chronos can also help users to accelerate their model for inferencing. + +In this example, we show an example to train the model on GPU and accelerate the model by using onnxruntime on CPU. + +## How to run this example +```bash +python cpu_inference_acceleration.py +``` + +## Sample output +```bash +Epoch 2: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████| 288/288 +original pytorch latency (ms): {'p50': 1.236, 'p90': 1.472, 'p95': 1.612, 'p99': 32.989} +onnxruntime latency (ms): {'p50': 0.124, 'p90': 0.129, 'p95': 0.148, 'p99': 0.363} +``` \ No newline at end of file diff --git a/python/chronos/example/nano_qp/tcn/readme.md b/python/chronos/example/nano_qp/tcn/readme.md deleted file mode 100644 index c24a34ea337a..000000000000 --- a/python/chronos/example/nano_qp/tcn/readme.md +++ /dev/null @@ -1,4 +0,0 @@ -### train -python ./train.py -### test -python ./test.py \ No newline at end of file