forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_async_infer_request.py
415 lines (330 loc) · 13 KB
/
test_async_infer_request.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from collections.abc import Iterable
from copy import deepcopy
import numpy as np
import pytest
import time
import openvino.runtime.opset13 as ops
from openvino import (
Core,
InferRequest,
AsyncInferQueue,
Model,
Shape,
Type,
Tensor,
)
from tests import skip_need_mock_op
from tests.utils.helpers import generate_image, get_relu_model
def concat_model_with_data(device, ov_type, numpy_dtype):
core = Core()
input_shape = [5]
params = []
params += [ops.parameter(input_shape, ov_type)]
if ov_type == Type.bf16:
params += [ops.parameter(input_shape, ov_type)]
else:
params += [ops.parameter(input_shape, numpy_dtype)]
model = Model(ops.concat(params, 0), params)
compiled_model = core.compile_model(model, device)
request = compiled_model.create_infer_request()
tensor1 = Tensor(ov_type, input_shape)
tensor1.data[:] = np.array([6, 7, 8, 9, 0])
array1 = np.array([1, 2, 3, 4, 5], dtype=numpy_dtype)
return request, tensor1, array1
def abs_model_with_data(device, ov_type, numpy_dtype):
input_shape = [1, 4]
param = ops.parameter(input_shape, ov_type)
model = Model(ops.abs(param), [param])
core = Core()
compiled_model = core.compile_model(model, device)
request = compiled_model.create_infer_request()
tensor1 = Tensor(ov_type, input_shape)
tensor1.data[:] = np.array([6, -7, -8, 9])
array1 = np.array([[-1, 2, 5, -3]]).astype(numpy_dtype)
return compiled_model, request, tensor1, array1
@pytest.mark.parametrize("share_inputs", [True, False])
def test_infer_queue(device, share_inputs):
jobs = 8
num_request = 4
core = Core()
model = get_relu_model()
compiled_model = core.compile_model(model, device)
infer_queue = AsyncInferQueue(compiled_model, num_request)
jobs_done = [{"finished": False, "latency": 0} for _ in range(jobs)]
def callback(request, job_id):
jobs_done[job_id]["finished"] = True
jobs_done[job_id]["latency"] = request.latency
img = None
if not share_inputs:
img = generate_image()
infer_queue.set_callback(callback)
assert infer_queue.is_ready()
for i in range(jobs):
if share_inputs:
img = generate_image()
infer_queue.start_async({"data": img}, i, share_inputs=share_inputs)
infer_queue.wait_all()
assert all(job["finished"] for job in jobs_done)
assert all(job["latency"] > 0 for job in jobs_done)
def test_infer_queue_iteration(device):
core = Core()
param = ops.parameter([10])
model = Model(ops.relu(param), [param])
compiled_model = core.compile_model(model, device)
infer_queue = AsyncInferQueue(compiled_model, 1)
assert isinstance(infer_queue, Iterable)
for infer_req in infer_queue:
assert isinstance(infer_req, InferRequest)
it = iter(infer_queue)
infer_request = next(it)
assert isinstance(infer_request, InferRequest)
assert infer_request.userdata is None
with pytest.raises(StopIteration):
next(it)
def test_infer_queue_userdata_is_empty(device):
core = Core()
param = ops.parameter([10])
model = Model(ops.relu(param), [param])
compiled_model = core.compile_model(model, device)
infer_queue = AsyncInferQueue(compiled_model, 1)
assert infer_queue.userdata == [None]
def test_infer_queue_userdata_is_empty_more_jobs(device):
core = Core()
param = ops.parameter([10])
model = Model(ops.relu(param), [param])
compiled_model = core.compile_model(model, device)
infer_queue = AsyncInferQueue(compiled_model, 5)
assert infer_queue.userdata == [None, None, None, None, None]
def test_infer_queue_fail_on_cpp_model(device):
jobs = 6
num_request = 4
core = Core()
model = get_relu_model()
compiled_model = core.compile_model(model, device)
infer_queue = AsyncInferQueue(compiled_model, num_request)
def callback(request, _):
request.get_tensor("Unknown")
img = generate_image()
infer_queue.set_callback(callback)
with pytest.raises(RuntimeError) as e:
for _ in range(jobs):
infer_queue.start_async({"data": img})
infer_queue.wait_all()
assert "Port for tensor name Unknown was not found" in str(e.value)
def test_infer_queue_fail_on_py_model(device):
jobs = 1
num_request = 1
core = Core()
model = get_relu_model()
compiled_model = core.compile_model(model, device)
infer_queue = AsyncInferQueue(compiled_model, num_request)
def callback(request, _):
request = request + 21
img = generate_image()
infer_queue.set_callback(callback)
with pytest.raises(TypeError) as e:
for _ in range(jobs):
infer_queue.start_async({"data": img})
infer_queue.wait_all()
assert "unsupported operand type(s) for +" in str(e.value)
@skip_need_mock_op
@pytest.mark.parametrize("with_callback", [False, True])
def test_infer_queue_fail_in_inference(device, with_callback):
jobs = 6
num_request = 4
core = Core()
data = ops.parameter([10], dtype=np.float32, name="data")
k_op = ops.parameter(Shape([]), dtype=np.int32, name="k")
emb = ops.topk(data, k_op, axis=0, mode="max", sort="value")
model = Model(emb, [data, k_op])
compiled_model = core.compile_model(model, device)
infer_queue = AsyncInferQueue(compiled_model, num_request)
def callback(request, _):
pytest.fail("Callback should not be called")
if with_callback:
infer_queue.set_callback(callback)
data_tensor = Tensor(np.arange(10).astype(np.float32))
k_tensor = Tensor(np.array(11, dtype=np.int32))
with pytest.raises(RuntimeError) as e:
for _ in range(jobs):
infer_queue.start_async({"data": data_tensor, "k": k_tensor})
infer_queue.wait_all()
assert "Can not clone with new dims" in str(e.value)
def test_infer_queue_get_idle_handle(device):
param = ops.parameter([10])
model = Model(ops.relu(param), [param])
core = Core()
compiled_model = core.compile_model(model, device)
queue = AsyncInferQueue(compiled_model, 2)
niter = 10
for _ in range(len(queue)):
queue.start_async()
queue.wait_all()
for request in queue:
assert request.wait_for(0)
for _ in range(niter):
idle_id = queue.get_idle_request_id()
assert queue[idle_id].wait_for(0)
queue.start_async()
queue.wait_all()
@pytest.mark.parametrize("share_inputs", [True, False])
def test_results_async_infer(device, share_inputs):
jobs = 8
num_request = 4
core = Core()
model = get_relu_model()
compiled_model = core.compile_model(model, device)
infer_queue = AsyncInferQueue(compiled_model, num_request)
jobs_done = [{"finished": False, "latency": 0} for _ in range(jobs)]
def callback(request, job_id):
jobs_done[job_id]["finished"] = True
jobs_done[job_id]["latency"] = request.latency
img = generate_image()
infer_queue.set_callback(callback)
for i in range(jobs):
infer_queue.start_async({"data": img}, i, share_inputs=share_inputs)
infer_queue.wait_all()
request = compiled_model.create_infer_request()
outputs = request.infer({0: img})
for i in range(num_request):
assert np.allclose(list(outputs.values()), list(
infer_queue[i].results.values()))
@pytest.mark.parametrize("share_inputs", [True, False])
def test_array_like_input_async_infer_queue(device, share_inputs):
class ArrayLikeObject:
# Array-like object accepted by np.array to test inputs similar to torch tensor and tf.Tensor
def __init__(self, array) -> None:
self.data = array
def __array__(self):
return self.data
jobs = 8
ov_type = Type.f32
input_shape = [2, 2]
input_data = np.ascontiguousarray([[-2, -1], [0, 1]])
param = ops.parameter(input_shape, ov_type)
layer = ops.abs(param)
model = Model([layer], [param])
core = Core()
compiled_model = core.compile_model(model, "CPU")
model_input_object = ArrayLikeObject(input_data)
model_input_list = [
[ArrayLikeObject(deepcopy(input_data))] for _ in range(jobs)]
# Test single array-like object in AsyncInferQueue.start_async()
infer_queue_object = AsyncInferQueue(compiled_model, jobs)
for _i in range(jobs):
infer_queue_object.start_async(model_input_object)
infer_queue_object.wait_all()
for i in range(jobs):
assert np.array_equal(
infer_queue_object[i].get_output_tensor().data, np.abs(input_data))
# Test list of array-like objects in AsyncInferQueue.start_async()
infer_queue_list = AsyncInferQueue(compiled_model, jobs)
for i in range(jobs):
infer_queue_list.start_async(
model_input_list[i], share_inputs=share_inputs)
infer_queue_list.wait_all()
for i in range(jobs):
assert np.array_equal(
infer_queue_list[i].get_output_tensor().data, np.abs(input_data))
@pytest.mark.skip(reason="Sporadically failed. Need further investigation. Ticket - 95967")
def test_cancel(device):
core = Core()
model = get_relu_model()
compiled_model = core.compile_model(model, device)
img = generate_image()
request = compiled_model.create_infer_request()
request.start_async({0: img})
request.cancel()
with pytest.raises(RuntimeError) as e:
request.wait()
assert "[ INFER_CANCELLED ]" in str(e.value)
request.start_async({"data": img})
request.cancel()
with pytest.raises(RuntimeError) as e:
request.wait_for(1)
assert "[ INFER_CANCELLED ]" in str(e.value)
@pytest.mark.parametrize("share_inputs", [True, False])
def test_start_async(device, share_inputs):
core = Core()
model = get_relu_model()
compiled_model = core.compile_model(model, device)
img = generate_image()
jobs = 3
requests = []
for _ in range(jobs):
requests.append(compiled_model.create_infer_request())
def callback(callbacks_info):
time.sleep(0.01)
callbacks_info["finished"] += 1
callbacks_info = {}
callbacks_info["finished"] = 0
for request in requests:
request.set_callback(callback, callbacks_info)
request.start_async({0: img}, share_inputs=share_inputs)
for request in requests:
request.wait()
assert request.latency > 0
assert callbacks_info["finished"] == jobs
@pytest.mark.parametrize(("ov_type", "numpy_dtype"), [
(Type.f32, np.float32),
(Type.f64, np.float64),
(Type.f16, np.float16),
(Type.bf16, np.float16),
(Type.i8, np.int8),
(Type.u8, np.uint8),
(Type.i32, np.int32),
(Type.u32, np.uint32),
(Type.i16, np.int16),
(Type.u16, np.uint16),
(Type.i64, np.int64),
(Type.u64, np.uint64),
(Type.boolean, bool),
])
@pytest.mark.parametrize("share_inputs", [True, False])
def test_async_mixed_values(device, ov_type, numpy_dtype, share_inputs):
request, tensor1, array1 = concat_model_with_data(device, ov_type, numpy_dtype)
request.start_async([tensor1, array1], share_inputs=share_inputs)
request.wait()
assert np.array_equal(request.output_tensors[0].data, np.concatenate((tensor1.data, array1)))
@pytest.mark.parametrize(("ov_type", "numpy_dtype"), [
(Type.f32, np.float32),
(Type.f64, np.float64),
(Type.f16, np.float16),
(Type.i8, np.int8),
(Type.u8, np.uint8),
(Type.i32, np.int32),
(Type.i16, np.int16),
(Type.u16, np.uint16),
(Type.i64, np.int64),
])
@pytest.mark.parametrize("share_inputs", [True, False])
def test_async_single_input(device, ov_type, numpy_dtype, share_inputs):
_, request, tensor1, array1 = abs_model_with_data(device, ov_type, numpy_dtype)
request.start_async(array1, share_inputs=share_inputs)
request.wait()
assert np.array_equal(request.get_output_tensor().data, np.abs(array1))
request.start_async(tensor1, share_inputs=share_inputs)
request.wait()
assert np.array_equal(request.get_output_tensor().data, np.abs(tensor1.data))
@pytest.mark.parametrize("share_inputs", [True, False])
def test_array_like_input_async(device, share_inputs):
class ArrayLikeObject:
# Array-like object accepted by np.array to test inputs similar to torch tensor and tf.Tensor
def __init__(self, array) -> None:
self.data = array
def __array__(self):
return np.array(self.data)
_, request, _, input_data = abs_model_with_data(device, Type.f32, np.single)
model_input_object = ArrayLikeObject(input_data.tolist())
model_input_list = [ArrayLikeObject(input_data.tolist())]
# Test single array-like object in InferRequest().start_async()
request.start_async(model_input_object, share_inputs=share_inputs)
request.wait()
assert np.array_equal(request.get_output_tensor().data, np.abs(input_data))
# Test list of array-like objects in InferRequest().start_async()
request.start_async(model_input_list)
request.wait()
assert np.array_equal(request.get_output_tensor().data, np.abs(input_data))