forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tensor_string.py
293 lines (259 loc) · 10.6 KB
/
test_tensor_string.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
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import openvino as ov
import pytest
from enum import Enum
class DataGetter(Enum):
BYTES = 1
STRINGS = 2
def _check_tensor_string(tensor_data, test_data):
assert tensor_data.shape == test_data.shape
assert tensor_data.strides == test_data.strides
assert np.array_equal(tensor_data, test_data)
assert not (np.shares_memory(tensor_data, test_data))
def check_bytes_based(tensor, string_data, to_flat=False):
tensor_data = tensor.bytes_data
encoded_data = string_data if string_data.dtype.kind == "S" else np.char.encode(string_data)
assert tensor_data.dtype.kind == "S"
_check_tensor_string(tensor_data.flatten() if to_flat else tensor_data, encoded_data.flatten() if to_flat else encoded_data)
def check_string_based(tensor, string_data, to_flat=False):
tensor_data = tensor.str_data
decoded_data = string_data if string_data.dtype.kind == "U" else np.char.decode(string_data)
assert tensor_data.dtype.kind == "U"
_check_tensor_string(tensor_data.flatten() if to_flat else tensor_data, decoded_data.flatten() if to_flat else decoded_data)
def test_string_tensor_shared_memory_fails():
data = np.array(["You", "shall", "not", "pass!"])
with pytest.raises(RuntimeError) as e:
_ = ov.Tensor(data, shared_memory=True)
assert "SHARED MEMORY MODE FOR THIS TENSOR IS NOT APPLICABLE! String types can be only copied." in str(e.value)
def test_string_tensor_data_warning():
data = np.array(["You", "shall", "not", "pass!"])
tensor = ov.Tensor(data, shared_memory=False)
with pytest.warns(RuntimeWarning) as w:
_ = tensor.data
assert "Data of string type will be copied! Please use dedicated properties" in str(w[0].message)
@pytest.mark.parametrize(
("init_type"),
[
(ov.Type.string),
(str),
(bytes),
(np.str_),
(np.bytes_),
],
)
def test_empty_string_tensor(init_type):
tensor = ov.Tensor(type=init_type, shape=ov.Shape([2, 2]))
assert tensor.element_type == ov.Type.string
@pytest.mark.parametrize(
("string_data"),
[
([bytes("text", encoding="utf-8"), bytes("openvino", encoding="utf-8")]),
([[b"xyz"], [b"abc"], [b"this is my last"]]),
([[b"text\0with\0null"], [b"openvino\0"]]),
(["text", "abc", "openvino"]),
(["text", "больше текста", "jeszcze więcej słów", "효과가 있었어"]),
([["text"], ["abc"], ["openvino"]]),
([["text"]]),
(["tex\u0000t\u0000tt"]),
([["abĆ"]]),
([["tex\u0000tttt"], ["abĆ"]]),
([["jeszcze więcej słówe"], [u"효#과가 있었어"]]),
([["jeszcze\u0000 więcej słówekó"]]),
([["효과가 있었어"]]),
(["ab\u0000Ć"]),
])
def test_init_with_list(string_data):
tensor = ov.Tensor(string_data)
assert tensor.element_type == ov.Type.string
# Convert to numpy to perform all checks. Memory is not shared,
# so it does not matter if data is stored in numpy format.
_string_data = np.array(string_data)
# Encoded:
check_bytes_based(tensor, _string_data)
# Decoded:
check_string_based(tensor, _string_data)
def test_init_with_list_rare_real_scenario():
input_data = ["tex\u0000\u0000ttt\u0000\u0000", "ab\u0000Ć"]
tensor = ov.Tensor(input_data)
assert tensor.element_type == ov.Type.string
# Convert to numpy to perform all checks. Memory is not shared,
np_string_data = np.array(input_data)
# Encoded:
check_bytes_based(tensor, np_string_data)
# Decoded:
str_tensor_data = tensor.str_data
assert str_tensor_data.shape == np_string_data.shape
# case when OV is not aligned with numpy format
# strides are different as trailing null characters are not stored in the tensor
# is rare to have any use of trailing null character in the string
assert str_tensor_data.strides != np_string_data.strides
assert np.array_equal(str_tensor_data, np_string_data)
assert not (np.shares_memory(str_tensor_data, np_string_data))
@pytest.mark.parametrize(
("string_data"),
[
(np.array(["text", "abc", "openvino"]).astype("S")), # "|S"
(np.array([["xyz"], ["abc"]]).astype(np.bytes_)), # "|S"
(np.array(["text", "abc", "openvino"])), # "<U"
(np.array(["text", "больше текста", "jeszcze więcej słów", "효과가 있었어"])), # "<U"
(np.array([["text"], ["abc"], ["openvino"]])), # "<U"
(np.array([["jeszcze więcej słów", "효과가 있었어"]])), # "<U"
],
)
def test_init_with_numpy(string_data):
tensor = ov.Tensor(string_data, shared_memory=False)
assert tensor.element_type == ov.Type.string
# Encoded:
check_bytes_based(tensor, string_data)
# Decoded:
check_string_based(tensor, string_data)
@pytest.mark.parametrize(
("init_type"),
[
(ov.Type.string),
(str),
(bytes),
(np.str_),
(np.bytes_),
],
)
@pytest.mark.parametrize(
("init_shape"),
[
(ov.Shape()),
(ov.Shape([])),
(ov.Shape([5])),
(ov.Shape([1, 1])),
(ov.Shape([2, 4, 5])),
],
)
@pytest.mark.parametrize(
("string_data"),
[
(np.array(["text", "abc", "openvino"]).astype(np.bytes_)), # "|S8"
(np.array([["text!"], ["abc?"]]).astype("S")), # "|S8"
(np.array(["text", "abc", "openvino"])), # "<U", depending on platform
(np.array([["text"], ["abc"], ["openvino"]])), # "<U", depending on platform
(np.array([["text", "больше текста"], ["jeszcze więcej słów", "효과가 있었어"]])), # "<U"
(np.array([["#text@", "больше текста"]])), # "<U"
],
)
def test_empty_tensor_copy_from(init_type, init_shape, string_data):
tensor = ov.Tensor(init_type, init_shape)
assert tensor.element_type == ov.Type.string
tensor.copy_from(string_data)
# Encoded:
check_bytes_based(tensor, string_data)
# Decoded:
check_string_based(tensor, string_data)
@pytest.mark.parametrize(
("init_shape"),
[
(ov.Shape()),
(ov.Shape([])),
(ov.Shape([1])),
(ov.Shape([8])),
(ov.Shape([4, 4])),
],
)
@pytest.mark.parametrize(
("string_data"),
[
(np.array(["text", "abc", "openvino"]).astype(np.bytes_)), # "|S"
(np.array([["text!"], ["abc?"]]).astype("S")), # "|S"
(np.array(["text", "abc", "openvino"])), # "<U"
(np.array([["text"], ["abc"], ["openvino"]])), # "<U"
(np.array([["text", "больше текста"], ["jeszcze więcej słów", "효과가 있었어"]])), # "<U"
(np.array([["#text@", "больше текста"]])), # "<U"
([bytes("text", encoding="utf-8"), bytes("openvino", encoding="utf-8")]),
([[b"xyz"], [b"abc"], [b"this is my last"]]),
(["text", "abc", "openvino"]),
(["text", "больше текста", "jeszcze więcej słów", "효과가 있었어"]),
([["text"], ["abc"], ["openvino"]]),
([["jeszcze więcej słów", "효과가 있었어"]]),
],
)
def test_populate_fails_size_check(init_shape, string_data):
tensor = ov.Tensor(ov.Type.string, init_shape)
assert tensor.element_type == ov.Type.string
with pytest.raises(RuntimeError) as e:
tensor.bytes_data = string_data
assert "Passed array must have the same size (number of elements) as the Tensor!" in str(e.value)
with pytest.raises(RuntimeError) as e:
tensor.str_data = string_data
assert "Passed array must have the same size (number of elements) as the Tensor!" in str(e.value)
@pytest.mark.parametrize(
("string_data"),
[
(np.array([0.6, 2.1, 3.7, 7.8])),
([1, 2, 3, 4, 5, 6, 7, 8, 9]),
],
)
def test_populate_fails_type_check(string_data):
tensor = ov.Tensor(ov.Type.string, ov.Shape([1]))
assert tensor.element_type == ov.Type.string
with pytest.raises(RuntimeError) as e:
tensor.bytes_data = string_data
assert "Unknown string kind passed to fill the Tensor's data!" in str(e.value)
with pytest.raises(RuntimeError) as e:
tensor.str_data = string_data
assert "Unknown string kind passed to fill the Tensor's data!" in str(e.value)
@pytest.mark.parametrize(
("init_type"),
[
(ov.Type.string),
(str),
(bytes),
(np.str_),
(np.bytes_),
],
)
@pytest.mark.parametrize(
("init_shape", "string_data"),
[
(ov.Shape([3]), np.array(["text", "abc", "openvino"]).astype(np.bytes_)),
(ov.Shape([3]), np.array(["text", "больше текста", "jeszcze więcej słów"])),
(ov.Shape([3]), [b"xyz", b"abc", b"this is my last"]),
(ov.Shape([3]), ["text", "abc", "openvino"]),
(ov.Shape([2]), [[b"text\0with\0null"], [b"openvino\0"]]),
(ov.Shape([3]), ["text", "больше текста", "jeszcze więcej słów"]),
(ov.Shape([2, 2]), np.array(["text", "abc", "openvino", "different"]).astype(np.bytes_)),
(ov.Shape([2, 2]), np.array(["text", "больше текста", "jeszcze więcej słów", "abcdefg"])),
(ov.Shape([2, 2]), [b"xyz", b"abc", b"this is my last", b"this is my final"]),
(ov.Shape([2, 2]), [["text", "abc"], ["openvino", "abcdefg"]]),
(ov.Shape([2, 2]), ["text", "больше текста", "jeszcze więcej słów", "śćżó"]),
],
)
@pytest.mark.parametrize(
("data_getter"),
[
(DataGetter.BYTES),
(DataGetter.STRINGS),
],
)
def test_empty_tensor_populate(init_type, init_shape, string_data, data_getter):
tensor = ov.Tensor(init_type, init_shape)
assert tensor.element_type == ov.Type.string
if data_getter == DataGetter.BYTES:
tensor.bytes_data = string_data
elif data_getter == DataGetter.STRINGS:
tensor.str_data = string_data
else:
raise AttributeError("Unknown DataGetter passed!")
_string_data = np.array(string_data) if isinstance(string_data, list) else string_data
# Need to flatten the numpy array as Tensor can have different shape.
# It only checks if strings are filling the data correctly.
# Encoded:
check_bytes_based(tensor, _string_data, to_flat=True)
# Decoded:
check_string_based(tensor, _string_data, to_flat=True)
def test_invalid_bytes_replaced():
string_data = np.array(b"\xe2\x80")
tensor = ov.Tensor(string_data, shared_memory=False)
# Encoded:
check_bytes_based(tensor, string_data, to_flat=True)
# Decoded:
check_string_based(tensor, np.char.decode(string_data, encoding="utf=8", errors="replace"), to_flat=True)