|
| 1 | + |
| 2 | +# TorchServe inference with torch.compile of densenet161 model |
| 3 | + |
| 4 | +This example shows how to take eager model of `densenet161`, 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 | +``` |
| 22 | +echo "pt2 : {backend: inductor, mode: reduce-overhead}" > model-config.yaml |
| 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 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 | +``` |
| 55 | + |
| 56 | +### Performance improvement from using `torch.compile` |
| 57 | + |
| 58 | +To measure the handler `preprocess`, `inference`, `postprocess` times, run the following |
| 59 | + |
| 60 | +#### Measure inference time with PyTorch eager |
| 61 | + |
| 62 | +``` |
| 63 | +echo "handler:" > model-config.yaml && \ |
| 64 | +echo " profile: true" >> model-config.yaml |
| 65 | +``` |
| 66 | + |
| 67 | +Once the `yaml` file is updated, create the model-archive, start TorchServe and run inference using the steps shown above. |
| 68 | +After a few iterations of warmup, we see the following |
| 69 | + |
| 70 | +``` |
| 71 | +2024-02-03T00:54:31,136 [INFO ] W-9000-densenet161_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - result=[METRICS]ts_handler_preprocess.Milliseconds:6.118656158447266|#ModelName:densenet161,Level:Model|#type:GAUGE|#hostname:ip-172-31-11-40,1706921671,c02b3170-c8fc-4396-857d-6c6266bf94a9, pattern=[METRICS] |
| 72 | +2024-02-03T00:54:31,155 [INFO ] W-9000-densenet161_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - result=[METRICS]ts_handler_inference.Milliseconds:18.77564811706543|#ModelName:densenet161,Level:Model|#type:GAUGE|#hostname:ip-172-31-11-40,1706921671,c02b3170-c8fc-4396-857d-6c6266bf94a9, pattern=[METRICS] |
| 73 | +2024-02-03T00:54:31,155 [INFO ] W-9000-densenet161_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - result=[METRICS]ts_handler_postprocess.Milliseconds:0.16630400717258453|#ModelName:densenet161,Level:Model|#type:GAUGE|#hostname:ip-172-31-11-40,1706921671,c02b3170-c8fc-4396-857d-6c6266bf94a9, pattern=[METRICS] |
| 74 | +``` |
| 75 | + |
| 76 | +#### Measure inference time with `torch.compile` |
| 77 | + |
| 78 | +``` |
| 79 | +echo "pt2: {backend: inductor, mode: reduce-overhead}" > model-config.yaml && \ |
| 80 | +echo "handler:" >> model-config.yaml && \ |
| 81 | +echo " profile: true" >> model-config.yaml |
| 82 | +``` |
| 83 | + |
| 84 | +Once the `yaml` file is updated, create the model-archive, start TorchServe and run inference using the steps shown above. |
| 85 | +`torch.compile` needs a few inferences to warmup. Once warmed up, we see the following |
| 86 | +``` |
| 87 | +2024-02-03T00:56:14,808 [INFO ] W-9000-densenet161_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - result=[METRICS]ts_handler_preprocess.Milliseconds:5.9771199226379395|#ModelName:densenet161,Level:Model|#type:GAUGE|#hostname:ip-172-31-11-40,1706921774,d38601be-6312-46b4-b455-0322150509e5, pattern=[METRICS] |
| 88 | +2024-02-03T00:56:14,814 [INFO ] W-9000-densenet161_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - result=[METRICS]ts_handler_inference.Milliseconds:5.8818559646606445|#ModelName:densenet161,Level:Model|#type:GAUGE|#hostname:ip-172-31-11-40,1706921774,d38601be-6312-46b4-b455-0322150509e5, pattern=[METRICS] |
| 89 | +2024-02-03T00:56:14,814 [INFO ] W-9000-densenet161_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - result=[METRICS]ts_handler_postprocess.Milliseconds:0.19392000138759613|#ModelName:densenet161,Level:Model|#type:GAUGE|#hostname:ip-172-31-11-40,1706921774,d38601be-6312-46b4-b455-0322150509e5, pattern=[METRICS] |
| 90 | +``` |
| 91 | + |
| 92 | +### Conclusion |
| 93 | + |
| 94 | +`torch.compile` reduces the inference time from 18ms to 5ms |
0 commit comments