-
Notifications
You must be signed in to change notification settings - Fork 879
/
Copy pathtest_cpp_backend.py
140 lines (103 loc) · 3.63 KB
/
test_cpp_backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import io
import os
import shutil
from pathlib import Path
from unittest.mock import patch
from zipfile import ZIP_STORED, ZipFile
import pytest
import requests
import test_utils
import torch
from model_archiver import ModelArchiverConfig
from PIL import Image
from torchvision import transforms
import ts
CURR_FILE_PATH = Path(__file__).parent
TORCH_NCCL_PATH = (Path(torch.__file__).parent / "lib").as_posix()
TORCH_NCCL_PATH += (
":" + (Path(torch.__file__).parents[1] / "nvidia" / "nccl" / "lib").as_posix()
)
os.environ["LD_LIBRARY_PATH"] = (
TORCH_NCCL_PATH + ":" + os.environ.get("LD_LIBRARY_PATH", "")
)
@pytest.fixture(scope="module")
def model_name():
yield "mnist_handler"
@pytest.fixture(scope="module")
def work_dir(tmp_path_factory, model_name):
return tmp_path_factory.mktemp(model_name)
@pytest.fixture(scope="module", name="mar_file_path")
def create_mar_file(work_dir, model_archiver, model_name):
mar_file_path = Path(work_dir).joinpath(model_name + ".mar")
mnist_scriptes_pt = (
CURR_FILE_PATH.parents[1]
/ "cpp/build/test/resources/examples/mnist/mnist_handler/mnist_script.pt"
)
config = ModelArchiverConfig(
model_name=model_name,
serialized_file=mnist_scriptes_pt.as_posix(),
model_file=None,
handler="TorchScriptHandler",
extra_files=None,
runtime="LSP",
export_path=work_dir,
archive_format="default",
force=True,
version="1.0",
requirements_file=None,
config_file=None,
)
# Using ZIP_STORED instead of ZIP_DEFLATED reduces test runtime from 54 secs to 10 secs
with patch(
"model_archiver.model_packaging_utils.zipfile.ZipFile",
lambda x, y, _: ZipFile(x, y, ZIP_STORED),
):
model_archiver.generate_model_archive(config=config)
assert mar_file_path.exists()
print(mar_file_path)
yield mar_file_path.as_posix()
# Clean up files
mar_file_path.unlink(missing_ok=True)
@pytest.fixture(scope="module", name="model_name_and_stdout")
def register_model(mar_file_path, model_store, torchserve):
"""
Register the model in torchserve
"""
print(os.environ["LD_LIBRARY_PATH"])
shutil.copy(mar_file_path, model_store)
file_name = Path(mar_file_path).name
model_name = Path(file_name).stem
params = (
("model_name", model_name),
("url", file_name),
("initial_workers", "1"),
("synchronous", "true"),
("batch_size", "1"),
)
test_utils.reg_resp = test_utils.register_model_with_params(params)
assert test_utils.reg_resp.status_code == 200
yield model_name, torchserve
test_utils.unregister_model(model_name)
@pytest.mark.skipif(
not (Path(ts.__file__).parent / "cpp" / "bin" / "model_worker_socket").exists(),
reason="CPP backend not found",
)
def test_cpp_mnist(model_name_and_stdout):
model_name, _ = model_name_and_stdout
for n in range(10):
data_file_mnist = (
CURR_FILE_PATH.parents[1]
/ f"examples/image_classifier/mnist/test_data/{n}.png"
).as_posix()
image_processing = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
)
image = Image.open(data_file_mnist)
image = image_processing(image)
buffer = io.BytesIO()
torch.save(image, buffer)
url = f"http://localhost:8080/predictions/{model_name}/"
buffer.seek(0)
response = requests.post(url=url, data=buffer)
assert response.status_code == 200
assert torch.load(io.BytesIO(response.content)).argmax().item() == n