This repository was archived by the owner on Aug 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
Profile TorchServe Handler (preprocess vs inference vs post-process) #2470
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f376080
Profile TS Handler using ab tool
agunapal e03d411
Added an example
agunapal 10780e2
Added an example
agunapal 7363c0c
Merge branch 'master' into feature/ts_benchmark_profile
agunapal 51d99be
handler class not needed
agunapal 037c7fc
Merge branch 'feature/ts_benchmark_profile' of https://github.com/pyt…
agunapal 7522388
Add model_yaml_config to MockCOntext
agunapal 03bfa52
remove unnecessary config
agunapal 046624e
based on review comments
agunapal 80757f7
Merge branch 'master' into feature/ts_benchmark_profile
agunapal c433c69
Merge branch 'master' into feature/ts_benchmark_profile
agunapal c6a8c0d
Merge branch 'master' into feature/ts_benchmark_profile
agunapal 3132942
Added details on how to enable this
agunapal 6e64ecf
Merge branch 'feature/ts_benchmark_profile' of https://github.com/pyt…
agunapal 7869322
Added details on how to enable this
agunapal ebb132a
Merge branch 'master' into feature/ts_benchmark_profile
agunapal 267af76
Merge branch 'master' into feature/ts_benchmark_profile
agunapal 19829be
Merge branch 'master' into feature/ts_benchmark_profile
agunapal 23a9589
Merge branch 'master' into feature/ts_benchmark_profile
chauhang f124d1f
Merge branch 'master' into feature/ts_benchmark_profile
agunapal 366a283
lint fix
agunapal b19a9f8
lint fix
agunapal 43f5196
Merge branch 'master' into feature/ts_benchmark_profile
agunapal 4c01f35
lint fix
agunapal a531548
Merge branch 'feature/ts_benchmark_profile' of https://github.com/pyt…
agunapal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
# Benchmark ResNet50 and profile the detailed split of PredictionTime | ||
|
||
This example shows how to run the benchmark ab tool on ResNet50 and identify the time spent on preprocess, inference and postprocess | ||
|
||
Change directory to the root of `serve` | ||
Ex: if `serve` is under `/home/ubuntu`, change directory to `/home/ubuntu/serve` | ||
|
||
|
||
## Download the weights | ||
|
||
``` | ||
wget https://download.pytorch.org/models/resnet50-11ad3fa6.pth | ||
``` | ||
|
||
### Create model archive | ||
|
||
To enable profiling of TorchServe Handler, add the following config in model-config.yaml | ||
``` | ||
handler: | ||
profile: true | ||
``` | ||
|
||
``` | ||
torch-model-archiver --model-name resnet-50 --version 1.0 --model-file ./examples/benchmarking/resnet50/model.py --serialized-file resnet50-11ad3fa6.pth --handler image_classifier --extra-files ./examples/image_classifier/index_to_name.json --config-file ./examples/benchmarking/resnet50/model-config.yaml | ||
|
||
mkdir model_store | ||
mv resnet-50.mar model_store/. | ||
``` | ||
|
||
### Install dependencies for benchmark tool | ||
|
||
``` | ||
sudo apt-get update -y | ||
sudo apt-get install -y apache2-utils | ||
pip install -r benchmarks/requirements-ab.txt | ||
``` | ||
|
||
### Run ab tool for benchmarking | ||
|
||
``` | ||
python benchmarks/auto_benchmark.py --input examples/benchmarking/resnet50/benchmark_profile.yaml --skip true | ||
``` | ||
|
||
This generates the report under `/tmp/ts_benchmarking/report.md` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Torchserve version is to be installed. It can be one of the options | ||
# - branch : "master" | ||
# - nightly: "2022.3.16" | ||
# - release: "0.5.3" | ||
# Nightly build will be installed if "ts_version" is not specifiged | ||
#ts_version: | ||
# branch: &ts_version "master" | ||
|
||
# a list of model configure yaml files defined in benchmarks/models_config | ||
# or a list of model configure yaml files with full path | ||
models: | ||
- "/home/ubuntu/serve/examples/benchmarking/resnet50/resnet50.yaml" | ||
|
||
# benchmark on "cpu" or "gpu". | ||
# "cpu" is set if "hardware" is not specified | ||
hardware: &hardware "gpu" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
handler: | ||
profile: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from torchvision.models.resnet import Bottleneck, ResNet | ||
|
||
|
||
class ImageClassifier(ResNet): | ||
def __init__(self): | ||
super(ImageClassifier, self).__init__(Bottleneck, [3, 4, 6, 3]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
resnet50: | ||
eager_mode: | ||
benchmark_engine: "ab" | ||
url: "file:///home/ubuntu/serve/model_store/resnet-50.mar" | ||
workers: | ||
- 4 | ||
batch_delay: 100 | ||
batch_size: | ||
- 1 | ||
- 2 | ||
- 4 | ||
- 8 | ||
- 16 | ||
- 32 | ||
- 64 | ||
requests: 10000 | ||
concurrency: 100 | ||
input: "./examples/image_classifier/kitten.jpg" | ||
handler_profiling: true | ||
exec_env: "local" | ||
processors: | ||
- "cpu" | ||
- "gpus": "all" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Decorator for timing handler methods | ||
|
||
Use this decorator to compute the execution time for your preprocesss, inference and | ||
postprocess methods. | ||
By default this feature is not enabled. | ||
|
||
To enable this, add the following section in your model-config.yaml file | ||
|
||
handler: | ||
profile: true | ||
|
||
An example of running benchmarks with the profiling enabled is in | ||
https://github.com/pytorch/serve/tree/master/examples/benchmarking/resnet50 | ||
|
||
""" | ||
|
||
import time | ||
|
||
import torch | ||
|
||
|
||
def timed(func): | ||
def wrap_func(self, *args, **kwargs): | ||
# Measure time if config specified in model_yaml_config | ||
if ( | ||
"handler" in self.context.model_yaml_config | ||
and "profile" in self.context.model_yaml_config["handler"] | ||
): | ||
if self.context.model_yaml_config["handler"]["profile"]: | ||
# Measure start time | ||
if torch.cuda.is_available(): | ||
start = torch.cuda.Event(enable_timing=True) | ||
end = torch.cuda.Event(enable_timing=True) | ||
start.record() | ||
else: | ||
start = time.time() | ||
|
||
result = func(self, *args, **kwargs) | ||
|
||
# Measure end time | ||
if torch.cuda.is_available(): | ||
end.record() | ||
torch.cuda.synchronize() | ||
else: | ||
end = time.time() | ||
|
||
# Measure time taken to execute the function in miliseconds | ||
if torch.cuda.is_available(): | ||
duration = start.elapsed_time(end) | ||
else: | ||
duration = (end - start) * 1000 | ||
|
||
# Add metrics for profiling | ||
metrics = self.context.metrics | ||
metrics.add_time("ts_handler_" + func.__name__, duration) | ||
else: | ||
# If profile config specified in model_yaml_config is False | ||
result = func(self, *args, **kwargs) | ||
else: | ||
# If no profile config specified in model_yaml_config | ||
result = func(self, *args, **kwargs) | ||
|
||
return result | ||
|
||
return wrap_func |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.