Skip to content

Commit

Permalink
add some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TheaperDeng committed Sep 7, 2022
1 parent 4e01877 commit e8584b1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()\
Expand All @@ -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,
Expand All @@ -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))
20 changes: 20 additions & 0 deletions python/chronos/example/inference-acceleration/readme.md
Original file line number Diff line number Diff line change
@@ -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}
```
4 changes: 0 additions & 4 deletions python/chronos/example/nano_qp/tcn/readme.md

This file was deleted.

0 comments on commit e8584b1

Please sign in to comment.