Skip to content

Commit 780ca83

Browse files
committed
fmt
1 parent fd30685 commit 780ca83

File tree

8 files changed

+94
-16
lines changed

8 files changed

+94
-16
lines changed

cpp/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ find_package(folly REQUIRED)
3030
find_package(fmt REQUIRED)
3131
find_package(gflags REQUIRED)
3232
find_package(Torch REQUIRED)
33-
find_package(yaml-cpp REQUIRED NO_CMAKE_PATH)
33+
3434
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
3535

3636
include_directories(${TORCH_INCLUDE_DIRS})

cpp/test/resources/examples/aot_inductor/resnet_handler/MAR-INF/MANIFEST.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"model": {
55
"modelName": "resnetcppaot",
66
"handler": "libresnet_handler:ResnetCppHandler",
7-
"modelVersion": "1.0"
7+
"modelVersion": "1.0",
8+
"configFile": "model-config.yaml"
89
},
910
"archiverVersion": "0.9.0"
1011
}

cpp/test/resources/examples/aot_inductor/resnet_handler/config.json

-3
This file was deleted.
+72-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
1-
torch-model-archiver --model-name resnetcppaot --version 1.0 --handler ../../../../cpp/_build/test/resources/examples/aot_inductor/resnet/resnet_handler/libresnet_handler:ResnetCppHandler --runtime LSP --extra-files config.json,index_to_name.json,resnet50_pt2.so --archive-format no-archive
1+
This example uses AOTInductor to compile the Resnet50 into an so file which is then executed using libtorch.
2+
The handler C++ source code for this examples can be found [here](src).
3+
4+
### Setup
5+
1. Follow the instructions in [README.md](../../../../cpp/README.md) to build the TorchServe C++ backend.
6+
7+
```
8+
cd serve/cpp
9+
./builld.sh
10+
```
11+
12+
The build script will create the necessary artifact for this example.
13+
To recreate these by hand you can follow the prepare_test_files function of the [build.sh](../../../../cpp/build.sh) script.
14+
We will need the handler .so file as well as the resne50_pt2.so file containing the model and weights.
15+
16+
2. Create a [model-config.yaml](model-config.yaml)
17+
18+
```yaml
19+
minWorkers: 1
20+
maxWorkers: 1
21+
batchSize: 2
22+
23+
handler:
24+
model_so_path: "resnet50_pt2.so"
25+
mapping: "index_to_name.json"
26+
```
27+
28+
### Generate MAR file
29+
30+
Now lets generate the mar file
31+
32+
```bash
33+
torch-model-archiver --model-name resnetcppaot --version 1.0 --handler ../../../../cpp/_build/test/resources/examples/aot_inductor/resnet_handler/libresnet_handler:ResnetCppHandler --runtime LSP --extra-files index_to_name.json,../../../../cpp/_build/test/resources/examples/aot_inductor/resnet_handler/resnet50_pt2.so --config-file model-config.yaml --archive-format no-archive
34+
```
35+
36+
Create model store directory and move the mar file
37+
38+
```
39+
mkdir model_store
40+
mv resnetcppaot model_store/
41+
```
42+
43+
### Inference
44+
45+
Start torchserve using the following command
46+
47+
```
48+
torchserve --ncs --model-store model_store/
49+
```
50+
51+
Register the model using the following command
52+
53+
```
54+
curl -v -X POST "http://localhost:8081/models?initial_workers=1&url=resnetcppaot&batch_size=2&max_batch_delay=5000"
55+
56+
{
57+
"status": "Model \"resnetcppaot\" Version: 1.0 registered with 1 initial workers"
58+
}
59+
```
60+
61+
Infer the model using the following command
62+
63+
```
64+
curl http://localhost:8080/predictions/resnetcppaot -T ../../../../cpp/test/resources/examples/aot_inductor/resnet_handler/0_png.pt
65+
{
66+
"lens_cap": 0.0022578993812203407,
67+
"lynx": 0.0032067005522549152,
68+
"Egyptian_cat": 0.046274684369564056,
69+
"tiger_cat": 0.13740436732769012,
70+
"tabby": 0.2724998891353607
71+
}
72+
```

examples/cpp/aot_inductor/resnet/config.json

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
minWorkers: 1
2+
maxWorkers: 1
3+
batchSize: 2
4+
5+
handler:
6+
model_so_path: "resnet50_pt2.so"
7+
mapping: "index_to_name.json"

examples/cpp/aot_inductor/resnet/src/resnet_handler.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ ResnetCppHandler::LoadModel(
3737
try {
3838
auto device = GetTorchDevice(load_model_request);
3939

40+
const std::string modelConfigYamlFilePath =
41+
fmt::format("{}/{}", load_model_request->model_dir, "model-config.yaml");
42+
model_config_yaml_ = std::make_unique<YAML::Node>(YAML::LoadFile(modelConfigYamlFilePath));
43+
4044
const std::string mapFilePath =
41-
fmt::format("{}/{}", load_model_request->model_dir, "index_to_name.json");
45+
fmt::format("{}/{}", load_model_request->model_dir,
46+
(*model_config_yaml_)["handler"]["mapping"].as<std::string>());
4247
mapping_json_ = LoadJsonFile(mapFilePath);
4348

44-
const std::string configFilePath =
45-
fmt::format("{}/{}", load_model_request->model_dir, "config.json");
46-
config_json_ = LoadJsonFile(configFilePath);
47-
48-
std::string model_so_path = fmt::format("{}/{}", load_model_request->model_dir, GetJsonValue(config_json_, "model_so_path").asString());
49+
std::string model_so_path =
50+
fmt::format("{}/{}", load_model_request->model_dir,
51+
(*model_config_yaml_)["handler"]["model_so_path"].as<std::string>());
52+
mapping_json_ = LoadJsonFile(mapFilePath);
4953
c10::InferenceMode mode;
5054

5155
if (device->is_cuda()) {

examples/cpp/aot_inductor/resnet/src/resnet_handler.hh

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <torch/torch.h>
99
#include <torch/csrc/inductor/aoti_model_container_runner.h>
1010
#include <torch/csrc/inductor/aoti_model_container_runner_cuda.h>
11+
#include <yaml-cpp/yaml.h>
1112

1213
#include "src/backends/handler/base_handler.hh"
1314

@@ -48,7 +49,7 @@ private:
4849
const folly::dynamic& GetJsonValue(std::unique_ptr<folly::dynamic>& json, const std::string& key);
4950
std::string MapClassToLabel(const torch::Tensor& classes, const torch::Tensor& probs);
5051

51-
std::unique_ptr<folly::dynamic> config_json_;
5252
std::unique_ptr<folly::dynamic> mapping_json_;
53+
std::unique_ptr<YAML::Node> model_config_yaml_;
5354
};
5455
} // namespac

0 commit comments

Comments
 (0)