-
Notifications
You must be signed in to change notification settings - Fork 878
/
Copy pathtest_gRPC_inference_api.py
257 lines (204 loc) · 7.69 KB
/
test_gRPC_inference_api.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import json
import os
import platform
import threading
from ast import literal_eval
import inference_pb2
import management_pb2
import pytest
import test_gRPC_utils
import test_utils
inference_data_json = "../postman/inference_data.json"
inference_stream_data_json = "../postman/inference_stream_data.json"
inference_stream2_data_json = "../postman/inference_stream2_data.json"
config_file = test_utils.ROOT_DIR + "/config.properties"
def setup_module(module):
test_utils.torchserve_cleanup()
with open(config_file, "w") as f:
f.write("install_py_dep_per_model=true")
test_utils.start_torchserve(snapshot_file=config_file)
def teardown_module(module):
test_utils.torchserve_cleanup()
if os.path.exists(config_file):
os.remove(config_file)
def __get_change(current, previous):
if current == previous:
return 0
try:
return (abs(current - previous) / previous) * 100.0
except ZeroDivisionError:
return float("inf")
def __infer(stub, model_name, model_input):
with open(model_input, "rb") as f:
data = f.read()
input_data = {"data": data}
response = stub.Predictions(
inference_pb2.PredictionsRequest(model_name=model_name, input=input_data)
)
prediction = response.prediction.decode("utf-8")
return prediction
@pytest.mark.skipif(
platform.machine() == "aarch64", reason="Test skipped on aarch64 architecture"
)
def test_inference_apis():
with open(os.path.join(os.path.dirname(__file__), inference_data_json), "rb") as f:
test_data = json.loads(f.read())
for item in test_data:
# TODO: enable after correctly handling parameter name and header dtype in cpp backend
if "skip_grpc_inference_api" in item and item["skip_grpc_inference_api"]:
print(f"Skipping grpc inference api test for {item['url']}")
continue
if item["url"].startswith("{{mar_path_"):
path = test_utils.mar_file_table[item["url"][2:-2]]
else:
path = item["url"]
managment_stub = test_gRPC_utils.get_management_stub()
response = managment_stub.RegisterModel(
management_pb2.RegisterModelRequest(
url=path,
initial_workers=item["worker"],
synchronous=bool(item["synchronous"]),
model_name=item["model_name"],
)
)
print(response.msg)
model_input = os.path.join(os.path.dirname(__file__), "..", item["file"])
prediction = __infer(
test_gRPC_utils.get_inference_stub(), item["model_name"], model_input
)
print("Prediction is : ", str(prediction))
if "expected" in item:
try:
prediction = literal_eval(prediction)
except SyntaxError:
pass
if isinstance(prediction, list) and "tolerance" in item:
assert len(prediction) == len(item["expected"])
for i in range(len(prediction)):
assert (
__get_change(prediction[i], item["expected"][i])
< item["tolerance"]
)
elif isinstance(prediction, dict) and "tolerance" in item:
assert len(prediction) == len(item["expected"])
for key in prediction:
assert (
__get_change(prediction[key], item["expected"][key])
< item["tolerance"]
)
else:
assert str(prediction) == str(item["expected"])
response = managment_stub.UnregisterModel(
management_pb2.UnregisterModelRequest(
model_name=item["model_name"],
)
)
print(response.msg)
def __infer_stream(stub, model_name, model_input):
with open(model_input, "rb") as f:
data = f.read()
input_data = {"data": data}
responses = stub.StreamPredictions(
inference_pb2.PredictionsRequest(model_name=model_name, input=input_data)
)
prediction = []
for resp in responses:
prediction.append(resp.prediction.decode("utf-8"))
return " ".join(prediction)
def test_inference_stream_apis():
with open(
os.path.join(os.path.dirname(__file__), inference_stream_data_json), "rb"
) as f:
test_data = json.loads(f.read())
for item in test_data:
if item["url"].startswith("{{mar_path_"):
path = test_utils.mar_file_table[item["url"][2:-2]]
else:
path = item["url"]
managment_stub = test_gRPC_utils.get_management_stub()
response = managment_stub.RegisterModel(
management_pb2.RegisterModelRequest(
url=path,
initial_workers=item["worker"],
synchronous=bool(item["synchronous"]),
model_name=item["model_name"],
)
)
print(response.msg)
model_input = os.path.join(os.path.dirname(__file__), "..", item["file"])
prediction = __infer_stream(
test_gRPC_utils.get_inference_stub(), item["model_name"], model_input
)
print("Stream prediction is : ", str(prediction))
if "expected" in item:
assert str(prediction) == str(item["expected"])
response = managment_stub.UnregisterModel(
management_pb2.UnregisterModelRequest(
model_name=item["model_name"],
)
)
print(response.msg)
def __infer_stream2(stub, model_name, sequence_id, expected):
request_iterator = iter(
[
inference_pb2.PredictionsRequest(
model_name=model_name, input={"data": 1}, sequence_id=sequence_id
),
inference_pb2.PredictionsRequest(
model_name=model_name, input={"data": 2}, sequence_id=sequence_id
),
inference_pb2.PredictionsRequest(
model_name=model_name, input={"data": 3}, sequence_id=sequence_id
),
]
)
responses_iterator = stub.StreamPredictions2(request_iterator)
prediction = []
for resp in responses_iterator:
prediction.append(resp.prediction.decode("utf-8"))
assert str(" ".join(prediction)) == expected
def test_inference_stream2_apis():
with open(
os.path.join(os.path.dirname(__file__), inference_stream2_data_json), "rb"
) as f:
test_data = json.loads(f.read())
for item in test_data:
model_artifacts = test_utils.create_model_artifacts(item, force=True)
managment_stub = test_gRPC_utils.get_management_stub()
response = managment_stub.RegisterModel(
management_pb2.RegisterModelRequest(
url=model_artifacts,
initial_workers=item["worker"],
synchronous=bool(item["synchronous"]),
model_name=item["model_name"],
)
)
print(response.msg)
t0 = threading.Thread(
target=__infer_stream2,
args=(
test_gRPC_utils.get_inference_stub(),
item["model_name"],
"seq_0",
str(item["expected"]),
),
)
t1 = threading.Thread(
target=__infer_stream2,
args=(
test_gRPC_utils.get_inference_stub(),
item["model_name"],
"seq_1",
str(item["expected"]),
),
)
t0.start()
t1.start()
t0.join()
t1.join()
response = managment_stub.UnregisterModel(
management_pb2.UnregisterModelRequest(
model_name=item["model_name"],
)
)
print(response.msg)