Skip to content

Commit 7652c1e

Browse files
committed
updated torch.compile example
1 parent 3627ee6 commit 7652c1e

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

examples/pt2/README.md

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
## PyTorch 2.x integration
22

3-
PyTorch 2.0 brings more compiler options to PyTorch, for you that should mean better perf either in the form of lower latency or lower memory consumption. Integrating PyTorch 2.0 is fairly trivial but for now the support will be experimental given that most public benchmarks have focused on training instead of inference.
3+
PyTorch 2.0 brings more compiler options to PyTorch, for you that should mean better perf either in the form of lower latency or lower memory consumption.
44

55
We strongly recommend you leverage newer hardware so for GPUs that would be an Ampere architecture. You'll get even more benefits from using server GPU deployments like A10G and A100 vs consumer cards. But you should expect to see some speedups for any Volta or Ampere architecture.
66

77
## Get started
88

99
Install torchserve and ensure that you're using at least `torch>=2.0.0`
1010

11+
To use the latest nightlies, you can run the following commands
1112
```sh
12-
python ts_scripts/install_dependencies.py --cuda=cu118
13-
pip install torchserve torch-model-archiver
13+
python ts_scripts/install_dependencies.py --cuda=cu121 --nightly_torch
14+
pip install torchserve-nightly torch-model-archiver-nightly
1415
```
1516

1617
## torch.compile
@@ -27,13 +28,7 @@ You can also pass a dictionary with compile options if you need more control ove
2728
pt2 : {backend: inductor, mode: reduce-overhead}
2829
```
2930
30-
As an example let's expand our getting started guide with the only difference being passing in the extra `model_config.yaml` file
31-
32-
```
33-
mkdir model_store
34-
torch-model-archiver --model-name densenet161 --version 1.0 --model-file ./serve/examples/image_classifier/densenet_161/model.py --export-path model_store --extra-files ./serve/examples/image_classifier/index_to_name.json --handler image_classifier --config-file model_config.yaml
35-
torchserve --start --ncs --model-store model_store --models densenet161.mar
36-
```
31+
An example of using `torch.compile` can be found [here](./torch_compile/README.md)
3732

3833
The exact same approach works with any other model, what's going on is the below
3934

examples/pt2/torch_compile/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
# TorchServe inference with torch.compile of densnet161 model
3+
4+
This example shows how to take eager model of `densnet161`, configure TorchServe to use `torch.compile` and run inference using `torch.compile`
5+
6+
7+
### Pre-requisites
8+
9+
- `PyTorch >= 2.0`
10+
11+
Change directory to the examples directory
12+
Ex: `cd examples/pt2/torch_compile`
13+
14+
15+
### torch.compile config
16+
17+
`torch.compile` supports a variety of config and the performance you get can vary based on the config. You can find the various options [here](https://pytorch.org/docs/stable/generated/torch.compile.html)
18+
19+
In this example , we use the following config
20+
21+
```yaml
22+
pt2 : {backend: inductor, mode: reduce-overhead}
23+
```
24+
25+
### Create model archive
26+
27+
```
28+
wget https://download.pytorch.org/models/densenet161-8d451a50.pth
29+
mkdir model_store
30+
torch-model-archiver --model-name densenet161 --version 1.0 --model-file ../../image_classifier/densenet_161/model.py --serialized-file densenet161-8d451a50.pth --export-path model_store --extra-files ../../image_classifier/index_to_name.json --handler image_classifier --config-file model_config.yaml -f
31+
```
32+
33+
#### Start TorchServe
34+
```
35+
torchserve --start --ncs --model-store model_store --models densenet161.mar
36+
```
37+
38+
#### Run Inference
39+
40+
```
41+
curl http://127.0.0.1:8080/predictions/densenet161 -T ../../image_classifier/kitten.jpg
42+
```
43+
44+
produces the output
45+
46+
```
47+
{
48+
"tabby": 0.4664836823940277,
49+
"tiger_cat": 0.4645617604255676,
50+
"Egyptian_cat": 0.06619937717914581,
51+
"lynx": 0.0012969186063855886,
52+
"plastic_bag": 0.00022856894065625966
53+
}
54+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pt2 : {backend: inductor, mode: reduce-overhead}

0 commit comments

Comments
 (0)