forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_remote_api.py
269 lines (197 loc) · 9.18 KB
/
test_remote_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
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import numpy as np
import openvino as ov
from tests.utils.helpers import get_relu_model
@pytest.mark.skipif(
"CPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on CPU device!",
)
def test_get_default_context_cpu():
core = ov.Core()
with pytest.raises(RuntimeError) as cpu_error:
_ = core.get_default_context("CPU")
possible_errors = ["is not supported by CPU plugin!", "Not Implemented"]
assert any(error in str(cpu_error.value) for error in possible_errors)
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_get_default_context_gpu():
core = ov.Core()
context = core.get_default_context("GPU")
assert isinstance(context, ov.RemoteContext)
assert "GPU" in context.get_device_name()
context_params = context.get_params()
assert isinstance(context_params, dict)
assert list(context_params.keys()) == ["CONTEXT_TYPE", "OCL_CONTEXT", "OCL_QUEUE"]
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_create_host_tensor_gpu():
core = ov.Core()
context = core.get_default_context("GPU")
assert isinstance(context, ov.RemoteContext)
assert "GPU" in context.get_device_name()
tensor = context.create_host_tensor(ov.Type.f32, ov.Shape([1, 2, 3]))
assert isinstance(tensor, ov.Tensor)
assert not isinstance(tensor, ov.RemoteTensor)
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_create_device_tensor_gpu():
core = ov.Core()
context = core.get_default_context("GPU")
assert isinstance(context, ov.RemoteContext)
assert "GPU" in context.get_device_name()
tensor = context.create_tensor(ov.Type.f32, ov.Shape([1, 2, 3]), {})
tensor_params = tensor.get_params()
assert isinstance(tensor_params, dict)
assert list(tensor_params.keys()) == ["MEM_HANDLE", "OCL_CONTEXT", "SHARED_MEM_TYPE"]
assert isinstance(tensor, ov.Tensor)
assert isinstance(tensor, ov.RemoteTensor)
assert "GPU" in tensor.get_device_name()
assert tensor.get_shape() == ov.Shape([1, 2, 3])
assert tensor.get_element_type() == ov.Type.f32
assert tensor.get_size() == 6
assert tensor.get_byte_size() == 24
assert list(tensor.get_strides()) == [24, 12, 4]
tensor.set_shape([1, 1, 1])
assert tensor.get_shape()
assert tensor.get_size() == 1
assert tensor.get_byte_size() == 4
assert list(tensor.get_strides()) == [4, 4, 4]
with pytest.raises(TypeError) as constructor_error:
_ = ov.RemoteTensor(np.ones((1, 2, 3)))
assert "No constructor defined!" in str(constructor_error.value)
with pytest.raises(RuntimeError) as data_error:
_ = tensor.data
assert "This function is not implemented." in str(data_error.value)
with pytest.raises(RuntimeError) as bytes_data_error:
_ = tensor.bytes_data
assert "This function is not implemented." in str(bytes_data_error.value)
with pytest.raises(RuntimeError) as str_data_error:
_ = tensor.str_data
assert "This function is not implemented." in str(str_data_error.value)
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_compile_with_context():
core = ov.Core()
context = core.get_default_context("GPU")
model = get_relu_model()
compiled = core.compile_model(model, context)
assert isinstance(compiled, ov.CompiledModel)
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_va_context():
core = ov.Core()
with pytest.raises(RuntimeError) as context_error:
_ = ov.VAContext(core, None)
assert "user handle is nullptr!" in str(context_error.value)
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_copy_host_to_device_gpu():
core = ov.Core()
context = core.get_default_context("GPU")
assert isinstance(context, ov.RemoteContext)
assert "GPU" in context.get_device_name()
host_tensor_ref = ov.Tensor(ov.Type.f32, ov.Shape([1, 2, 3]))
random_arr = np.random.rand(*host_tensor_ref.shape).astype(np.float32)
host_tensor_ref.data[:] = random_arr
# allocate remote tensor with smaller shape and expect proper reallocation
device_tensor = context.create_tensor(ov.Type.f32, ov.Shape([1, 1, 1]), {})
# copy to device tensor from host tensor
host_tensor_ref.copy_to(device_tensor)
assert host_tensor_ref.get_shape() == device_tensor.get_shape()
assert host_tensor_ref.get_byte_size() == device_tensor.get_byte_size()
host_tensor_res = ov.Tensor(ov.Type.f32, ov.Shape([1, 2, 3]))
# copy from device tensor from host tensor
host_tensor_res.copy_from(device_tensor)
assert np.array_equal(host_tensor_res.data, host_tensor_ref.data)
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_copy_device_to_host_gpu():
core = ov.Core()
context = core.get_default_context("GPU")
assert isinstance(context, ov.RemoteContext)
assert "GPU" in context.get_device_name()
host_tensor_ref = ov.Tensor(ov.Type.f32, ov.Shape([1, 2, 3]))
random_arr = np.random.rand(*host_tensor_ref.shape).astype(np.float32)
host_tensor_ref.data[:] = random_arr
# allocate remote tensor with smaller shape and expect proper reallocation
device_tensor = context.create_tensor(ov.Type.f32, ov.Shape([1, 1, 1]), {})
# copy from host tensor to device tensor
device_tensor.copy_from(host_tensor_ref)
assert host_tensor_ref.get_shape() == device_tensor.get_shape()
assert host_tensor_ref.get_byte_size() == device_tensor.get_byte_size()
host_tensor_res = ov.Tensor(ov.Type.f32, ov.Shape([1, 2, 3]))
# copy to host tensor from device tensor
device_tensor.copy_to(host_tensor_res)
assert np.array_equal(host_tensor_res.data, host_tensor_ref.data)
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_roi_copy_host_to_device_gpu():
core = ov.Core()
context = core.get_default_context("GPU")
assert isinstance(context, ov.RemoteContext)
assert "GPU" in context.get_device_name()
host_tensor_ref = ov.Tensor(ov.Type.f32, ov.Shape([4, 4, 4]))
random_arr = np.random.rand(*host_tensor_ref.shape).astype(np.float32)
host_tensor_ref.data[:] = random_arr
begin_roi = ov.Coordinate([0, 0, 0])
end_roi = ov.Coordinate([3, 4, 4])
roi_host_tensor_ref = ov.Tensor(host_tensor_ref, begin_roi, end_roi)
device_tensor = context.create_tensor(ov.Type.f32, ov.Shape([4, 4, 4]), {})
roi_device_tensor = ov.RemoteTensor(device_tensor, begin_roi, end_roi)
# copy to roi device tensor from roi host tensor
roi_host_tensor_ref.copy_to(roi_device_tensor)
assert roi_host_tensor_ref.get_shape() == roi_device_tensor.get_shape()
assert roi_host_tensor_ref.get_byte_size() == roi_device_tensor.get_byte_size()
host_tensor_res = ov.Tensor(ov.Type.f32, roi_host_tensor_ref.get_shape())
# copy from roi device tensor from roi host tensor
host_tensor_res.copy_from(roi_device_tensor)
host_tensor_wo_roi = ov.Tensor(ov.Type.f32, roi_host_tensor_ref.get_shape())
host_tensor_wo_roi.copy_from(roi_host_tensor_ref)
assert np.array_equal(host_tensor_res.data, host_tensor_wo_roi.data)
@pytest.mark.skipif(
"GPU" not in os.environ.get("TEST_DEVICE", ""),
reason="Test can be only performed on GPU device!",
)
def test_roi_copy_device_to_host_gpu():
core = ov.Core()
context = core.get_default_context("GPU")
assert isinstance(context, ov.RemoteContext)
assert "GPU" in context.get_device_name()
host_tensor_ref = ov.Tensor(ov.Type.f32, ov.Shape([4, 4, 4]))
random_arr = np.random.rand(*host_tensor_ref.shape).astype(np.float32)
host_tensor_ref.data[:] = random_arr
begin_roi = ov.Coordinate([1, 2, 1])
end_roi = ov.Coordinate([3, 4, 4])
roi_host_tensor_ref = ov.Tensor(host_tensor_ref, begin_roi, end_roi)
device_tensor = context.create_tensor(ov.Type.f32, ov.Shape([4, 4, 4]), {})
roi_device_tensor = ov.RemoteTensor(device_tensor, begin_roi, end_roi)
# copy from roi host tensor to roi device tensor
roi_device_tensor.copy_from(roi_host_tensor_ref)
assert roi_host_tensor_ref.get_shape() == roi_device_tensor.get_shape()
assert roi_host_tensor_ref.get_byte_size() == roi_device_tensor.get_byte_size()
host_tensor_res = ov.Tensor(ov.Type.f32, roi_host_tensor_ref.get_shape())
# copy to roi host tensor from roi remote tensor
host_tensor_res.copy_from(roi_device_tensor)
host_tensor_wo_roi = ov.Tensor(ov.Type.f32, roi_host_tensor_ref.get_shape())
host_tensor_wo_roi.copy_from(roi_host_tensor_ref)
assert np.array_equal(host_tensor_res.data, host_tensor_wo_roi.data)