forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tf.py
More file actions
351 lines (288 loc) · 10.3 KB
/
test_tf.py
File metadata and controls
351 lines (288 loc) · 10.3 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
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
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
import os
import warnings
import lance
import numpy as np
import pandas as pd
import pyarrow as pa
import pytest
from lance.arrow import ImageArray
from lance.fragment import LanceFragment
pytest.skip("Skip tensorflow tests", allow_module_level=True)
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
import tensorflow as tf # noqa: F401
except ImportError:
pytest.skip(
"Tensorflow is not installed. Please install tensorflow to "
+ "test lance.tf module.",
allow_module_level=True,
)
from lance.tf.data import ( # noqa: E402
from_lance,
from_lance_batches,
lance_fragments,
lance_take_batches,
)
@pytest.fixture
def tf_dataset(tmp_path):
df = pd.DataFrame(
{
"a": range(10000),
"s": [f"val-{i}" for i in range(10000)],
"vec": [[i * 0.2] * 128 for i in range(10000)],
}
)
schema = pa.schema(
[
pa.field("a", pa.int64()),
pa.field("s", pa.string()),
pa.field("vec", pa.list_(pa.float32(), 128)),
]
)
tbl = pa.Table.from_pandas(df, schema=schema)
uri = tmp_path / "dataset.lance"
lance.write_dataset(
tbl,
uri,
schema=tbl.schema,
max_rows_per_group=100,
max_rows_per_file=1000,
)
return uri
def test_fragment_dataset(tf_dataset):
ds = from_lance(tf_dataset, batch_size=100)
for idx, batch in enumerate(ds):
assert batch["a"].numpy()[0] == idx * 100
assert batch["s"].numpy()[0] == f"val-{idx * 100}".encode("utf-8")
assert batch["a"].shape == (100,)
assert batch["vec"].shape == (
100,
128,
) # Fixed size list
def test_projection(tf_dataset):
ds = from_lance(tf_dataset, batch_size=100, columns=["a"])
for idx, batch in enumerate(ds):
assert list(batch.keys()) == ["a"]
assert batch["a"].numpy()[0] == idx * 100
assert batch["a"].shape == (100,)
def test_filter(tf_dataset):
ds = from_lance(tf_dataset, batch_size=100, filter="a >= 5000")
for idx, batch in enumerate(ds):
assert batch["a"].numpy()[0] == idx * 100 + 5000
assert batch["a"].shape == (100,)
def test_namespace_table_id(monkeypatch):
calls = {}
class DummyScanner:
def __init__(self):
self._batch = pa.record_batch([pa.array([1, 2])], names=["a"])
self.projected_schema = self._batch.schema
def to_batches(self):
yield self._batch
class DummyDataset:
def scanner(self, **kwargs):
return DummyScanner()
def fake_dataset(uri=None, **kwargs):
calls["uri"] = uri
calls["kwargs"] = kwargs
return DummyDataset()
monkeypatch.setattr(lance, "dataset", fake_dataset)
ns = object()
ds = from_lance(
None,
namespace=ns,
table_id=["tbl"],
ignore_namespace_table_storage_options=True,
)
assert calls["kwargs"]["namespace"] is ns
assert calls["kwargs"]["table_id"] == ["tbl"]
assert calls["kwargs"]["ignore_namespace_table_storage_options"] is True
batches = list(ds)
assert [b["a"].numpy().tolist() for b in batches] == [[1, 2]]
def test_scan_use_tf_data(tf_dataset):
ds = tf.data.Dataset.from_lance(tf_dataset)
for idx, batch in enumerate(ds):
assert batch["a"].numpy()[0] == idx * 100
assert batch["s"].numpy()[0] == f"val-{idx * 100}".encode("utf-8")
assert batch["a"].shape == (100,)
assert batch["vec"].shape == (
100,
128,
) # Fixed size list
def test_pass_fragments(tf_dataset):
# Can pass fragments directly to from_lance
dataset = lance.dataset(tf_dataset)
ds = from_lance(tf_dataset, fragments=dataset.get_fragments(), batch_size=100)
ds_default = from_lance(tf_dataset, batch_size=100)
for batch, batch_default in zip(ds, ds_default):
assert batch["a"].numpy()[0] == batch_default["a"].numpy()[0]
assert batch["a"].numpy().shape == (100,)
assert batch["vec"].shape == (
100,
128,
)
# Can pass ids directly to from_lance
ds = from_lance(tf_dataset, fragments=[0, 1, 2], batch_size=100)
for idx, batch in enumerate(ds):
assert batch["a"].numpy()[0] == idx * 100
assert batch["a"].numpy().shape == (100,)
def test_shuffle(tf_dataset):
fragments = lance_fragments(tf_dataset).shuffle(4, seed=20).take(3)
ds = from_lance(tf_dataset, fragments=fragments, batch_size=100)
raw_ds = lance.dataset(tf_dataset)
scanner = raw_ds.scanner(
fragments=[LanceFragment(raw_ds, fid) for fid in [0, 3, 1]], batch_size=100
)
for batch, raw_batch in zip(ds, scanner.to_batches()):
assert batch["a"].numpy()[0] == raw_batch.to_pydict()["a"][0]
assert batch["a"].numpy().shape == (100,)
assert batch["vec"].shape == (
100,
128,
) # Fixed size list
def test_dataset_batches(tf_dataset):
tf_dataset = lance.dataset(tf_dataset)
batch_size = 300
batches = list(
from_lance_batches(tf_dataset, batch_size=batch_size).as_numpy_iterator()
)
assert tf_dataset.count_rows() // batch_size + 1 == len(batches)
assert all(end - start == batch_size for start, end in batches[:-2])
assert batches[-1][1] - batches[-1][0] == tf_dataset.count_rows() % batch_size
skip = 5
batches_skipped = list(
from_lance_batches(
tf_dataset, batch_size=batch_size, skip=skip
).as_numpy_iterator()
)
assert batches_skipped == batches[skip:]
batches_shuffled = list(
from_lance_batches(
tf_dataset, batch_size=batch_size, shuffle=True, seed=42
).as_numpy_iterator()
)
# make sure it does a shuffle
assert batches_shuffled != batches
batches_shuffled2 = list(
from_lance_batches(
tf_dataset, batch_size=batch_size, shuffle=True, seed=42
).as_numpy_iterator()
)
# make sure the shuffle can be deterministic
assert batches_shuffled == batches_shuffled2
def test_take_dataset(tf_dataset):
tf_dataset = lance.dataset(tf_dataset)
batch_ds = from_lance_batches(
tf_dataset, batch_size=100, shuffle=True, seed=42
).as_numpy_iterator()
lance_ds = lance_take_batches(tf_dataset, batch_ds)
lance_ds = lance_ds.unbatch().shuffle(400, seed=42).batch(100)
for batch in lance_ds:
assert batch["a"].numpy().shape == (100,)
batches = [(0, 200), (100, 200)]
lance_ds = lance_take_batches(tf_dataset, batches, columns=["a"])
for (start, end), batch in zip(batches, lance_ds):
assert batch["a"].numpy().tolist() == np.arange(start, end).tolist()
assert batch.keys() == {"a"}
def test_var_length_list(tmp_path):
"""Treat var length list as RaggedTensor."""
df = pd.DataFrame(
{
"a": range(200),
"l": [[i] * (i % 5 + 1) for i in range(200)],
}
)
schema = pa.schema(
[
pa.field("a", pa.int64()),
pa.field("l", pa.list_(pa.int32())),
]
)
tbl = pa.Table.from_pandas(df, schema=schema)
uri = tmp_path / "dataset.lance"
lance.write_dataset(
tbl,
uri,
schema=tbl.schema,
)
output_signature = {
"a": tf.TensorSpec(shape=(None,), dtype=tf.int64),
"l": tf.RaggedTensorSpec(dtype=tf.dtypes.int32, shape=(8, None), ragged_rank=1),
}
ds = tf.data.Dataset.from_lance(
uri,
batch_size=8,
output_signature=output_signature,
)
for idx, batch in enumerate(ds):
assert batch["a"].numpy()[0] == idx * 8
assert batch["l"].shape == (8, None)
assert isinstance(batch["l"], tf.RaggedTensor)
def test_nested_struct(tmp_path):
table = pa.table(
{
"x": pa.array(
[
{
"a": 1,
"json": {"b": "hello", "x": b"abc"},
},
{
"a": 24,
"json": {"b": "world", "x": b"def"},
},
]
)
}
)
uri = tmp_path / "dataset.lance"
dataset = lance.write_dataset(table, uri)
ds = tf.data.Dataset.from_lance(
dataset,
batch_size=8,
)
for batch in ds:
tf.debugging.assert_equal(batch["x"]["a"], tf.constant([1, 24], dtype=tf.int64))
tf.debugging.assert_equal(
batch["x"]["json"]["b"], tf.constant(["hello", "world"])
)
tf.debugging.assert_equal(
batch["x"]["json"]["x"], tf.constant([b"abc", b"def"])
)
def test_tensor(tmp_path):
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]], dtype=np.float32)
table = pa.table({"x": pa.FixedShapeTensorArray.from_numpy_ndarray(arr)})
uri = tmp_path / "dataset.lance"
dataset = lance.write_dataset(table, uri)
ds = tf.data.Dataset.from_lance(dataset)
for batch in ds:
assert batch["x"].shape == (2, 2, 3)
assert batch["x"].dtype == tf.float32
assert batch["x"].numpy().tolist() == arr.tolist()
def test_image_types(tmp_path):
path = [os.path.join(os.path.dirname(__file__), "images/1.png")]
uris = ImageArray.from_array(path * 3)
encoded_images = uris.read_uris()
tensors = encoded_images.to_tensor()
table = pa.table(
{
"uris": uris,
"encoded_images": encoded_images,
"tensor_images": tensors,
}
)
uri = tmp_path / "dataset.lance"
dataset = lance.write_dataset(table, uri)
ds = tf.data.Dataset.from_lance(dataset)
for batch in ds:
assert batch["uris"].shape == (3,)
assert batch["uris"].dtype == tf.string
assert batch["uris"].numpy().astype("str").tolist() == uris.tolist()
assert batch["encoded_images"].shape == (3,)
assert batch["encoded_images"].dtype == tf.string
assert batch["encoded_images"].numpy().tolist() == encoded_images.tolist()
assert batch["tensor_images"].shape == (3, 1, 1, 4)
assert batch["tensor_images"].dtype == tf.uint8
assert batch["tensor_images"].numpy().tolist() == tensors.to_numpy().tolist()