-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathtest_array_stacked_jax.py
More file actions
301 lines (214 loc) · 7.35 KB
/
test_array_stacked_jax.py
File metadata and controls
301 lines (214 loc) · 7.35 KB
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
from typing import Optional, Union
import pytest
from docarray import BaseDoc, DocList
from docarray.array import DocVec
from docarray.typing import (
AnyEmbedding,
AnyTensor,
AudioTensor,
ImageTensor,
NdArray,
VideoTensor,
)
from docarray.utils._internal.misc import is_jax_available
jax_available = is_jax_available()
if jax_available:
import jax.numpy as jnp
from docarray.typing import JaxArray
@pytest.fixture()
@pytest.mark.jax
def batch():
import jax.numpy as jnp
class Image(BaseDoc):
tensor: JaxArray[3, 224, 224]
batch = DocList[Image]([Image(tensor=jnp.zeros((3, 224, 224))) for _ in range(10)])
return batch.to_doc_vec()
@pytest.fixture()
@pytest.mark.jax
def nested_batch():
class Image(BaseDoc):
tensor: JaxArray[3, 224, 224]
class MMdoc(BaseDoc):
img: DocList[Image]
batch = DocVec[MMdoc](
[
MMdoc(
img=DocList[Image](
[Image(tensor=jnp.zeros((3, 224, 224))) for _ in range(10)]
)
)
for _ in range(10)
]
)
return batch
@pytest.mark.jax
def test_len(batch):
assert len(batch) == 10
@pytest.mark.jax
def test_getitem(batch):
for i in range(len(batch)):
item = batch[i]
assert isinstance(item.tensor, JaxArray)
assert jnp.allclose(item.tensor.tensor, jnp.zeros((3, 224, 224)))
@pytest.mark.jax
def test_get_slice(batch):
sliced = batch[0:2]
assert isinstance(sliced, DocVec)
assert len(sliced) == 2
@pytest.mark.jax
def test_iterator(batch):
for doc in batch:
assert jnp.allclose(doc.tensor.tensor, jnp.zeros((3, 224, 224)))
@pytest.mark.jax
def test_set_after_stacking():
class Image(BaseDoc):
tensor: JaxArray[3, 224, 224]
batch = DocVec[Image]([Image(tensor=jnp.zeros((3, 224, 224))) for _ in range(10)])
batch.tensor = jnp.ones((10, 3, 224, 224))
assert jnp.allclose(batch.tensor.tensor, jnp.ones((10, 3, 224, 224)))
for i, doc in enumerate(batch):
assert jnp.allclose(doc.tensor.tensor, batch.tensor.tensor[i])
@pytest.mark.jax
def test_stack_optional(batch):
assert jnp.allclose(
batch._storage.tensor_columns['tensor'].tensor, jnp.zeros((10, 3, 224, 224))
)
assert jnp.allclose(batch.tensor.tensor, jnp.zeros((10, 3, 224, 224)))
@pytest.mark.jax
def test_stack_mod_nested_document():
class Image(BaseDoc):
tensor: JaxArray[3, 224, 224]
class MMdoc(BaseDoc):
img: Image
batch = DocList[MMdoc](
[MMdoc(img=Image(tensor=jnp.zeros((3, 224, 224)))) for _ in range(10)]
).to_doc_vec()
assert jnp.allclose(
batch._storage.doc_columns['img']._storage.tensor_columns['tensor'].tensor,
jnp.zeros((10, 3, 224, 224)),
)
assert jnp.allclose(batch.img.tensor.tensor, jnp.zeros((10, 3, 224, 224)))
@pytest.mark.jax
def test_stack_nested_DocArray(nested_batch):
for i in range(len(nested_batch)):
assert jnp.allclose(
nested_batch[i].img._storage.tensor_columns['tensor'].tensor,
jnp.zeros((10, 3, 224, 224)),
)
assert jnp.allclose(
nested_batch[i].img.tensor.tensor, jnp.zeros((10, 3, 224, 224))
)
@pytest.mark.jax
def test_convert_to_da(batch):
da = batch.to_doc_list()
for doc in da:
assert jnp.allclose(doc.tensor.tensor, jnp.zeros((3, 224, 224)))
@pytest.mark.jax
def test_unstack_nested_document():
class Image(BaseDoc):
tensor: JaxArray[3, 224, 224]
class MMdoc(BaseDoc):
img: Image
batch = DocVec[MMdoc](
[MMdoc(img=Image(tensor=jnp.zeros((3, 224, 224)))) for _ in range(10)]
)
assert isinstance(batch.img._storage.tensor_columns['tensor'], JaxArray)
da = batch.to_doc_list()
for doc in da:
assert jnp.allclose(doc.img.tensor.tensor, jnp.zeros((3, 224, 224)))
@pytest.mark.jax
def test_unstack_nested_DocArray(nested_batch):
batch = nested_batch.to_doc_list()
for i in range(len(batch)):
assert isinstance(batch[i].img, DocList)
for doc in batch[i].img:
assert jnp.allclose(doc.tensor.tensor, jnp.zeros((3, 224, 224)))
@pytest.mark.jax
def test_stack_call():
class Image(BaseDoc):
tensor: JaxArray[3, 224, 224]
da = DocList[Image]([Image(tensor=jnp.zeros((3, 224, 224))) for _ in range(10)])
da = da.to_doc_vec()
assert len(da) == 10
assert da.tensor.tensor.shape == (10, 3, 224, 224)
@pytest.mark.jax
def test_stack_union():
class Image(BaseDoc):
tensor: Union[JaxArray[3, 224, 224], NdArray[3, 224, 224]]
DocVec[Image](
[Image(tensor=jnp.zeros((3, 224, 224))) for _ in range(10)],
tensor_type=JaxArray,
)
# union fields aren't actually doc_vec
# just checking that there is no error
@pytest.mark.jax
def test_setitem_tensor(batch):
batch[3].tensor.tensor = jnp.zeros((3, 224, 224))
@pytest.mark.jax
@pytest.mark.skip('not working yet')
def test_setitem_tensor_direct(batch):
batch[3].tensor = jnp.zeros((3, 224, 224))
@pytest.mark.jax
@pytest.mark.parametrize(
'cls_tensor', [ImageTensor, AudioTensor, VideoTensor, AnyEmbedding, AnyTensor]
)
def test_generic_tensors_with_jnp(cls_tensor):
tensor = jnp.zeros((3, 224, 224))
class Image(BaseDoc):
tensor: cls_tensor
da = DocVec[Image](
[Image(tensor=tensor) for _ in range(10)],
tensor_type=JaxArray,
)
for i in range(len(da)):
assert jnp.allclose(da[i].tensor.tensor, tensor)
assert 'tensor' in da._storage.tensor_columns.keys()
assert isinstance(da._storage.tensor_columns['tensor'], JaxArray)
@pytest.mark.jax
@pytest.mark.parametrize(
'cls_tensor', [ImageTensor, AudioTensor, VideoTensor, AnyEmbedding, AnyTensor]
)
def test_generic_tensors_with_optional(cls_tensor):
tensor = jnp.zeros((3, 224, 224))
class Image(BaseDoc):
tensor: Optional[cls_tensor] = None
class TopDoc(BaseDoc):
img: Image
da = DocVec[TopDoc](
[TopDoc(img=Image(tensor=tensor)) for _ in range(10)],
tensor_type=JaxArray,
)
for i in range(len(da)):
assert jnp.allclose(da.img[i].tensor.tensor, tensor)
assert 'tensor' in da.img._storage.tensor_columns.keys()
assert isinstance(da.img._storage.tensor_columns['tensor'], JaxArray)
assert isinstance(da.img._storage.tensor_columns['tensor'].tensor, jnp.ndarray)
@pytest.mark.jax
def test_get_from_slice_stacked():
class Doc(BaseDoc):
text: str
tensor: JaxArray
da = DocVec[Doc](
[Doc(text=f'hello{i}', tensor=jnp.zeros((3, 224, 224))) for i in range(10)]
)
da_sliced = da[0:10:2]
assert isinstance(da_sliced, DocVec)
tensors = da_sliced.tensor.tensor
assert tensors.shape == (5, 3, 224, 224)
@pytest.mark.jax
def test_stack_none():
class MyDoc(BaseDoc):
tensor: Optional[AnyTensor] = None
da = DocVec[MyDoc]([MyDoc(tensor=None) for _ in range(10)], tensor_type=JaxArray)
assert 'tensor' in da._storage.tensor_columns.keys()
@pytest.mark.jax
def test_keep_dtype_jnp():
class MyDoc(BaseDoc):
tensor: JaxArray
da = DocList[MyDoc](
[MyDoc(tensor=jnp.zeros([2, 4], dtype=jnp.int32)) for _ in range(3)]
)
assert da[0].tensor.tensor.dtype == jnp.int32
da = da.to_doc_vec()
assert da[0].tensor.tensor.dtype == jnp.int32
assert da.tensor.tensor.dtype == jnp.int32