forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_data.py
More file actions
326 lines (263 loc) · 9.23 KB
/
test_data.py
File metadata and controls
326 lines (263 loc) · 9.23 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
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
import shutil
from itertools import chain
from pathlib import Path
import lance
import numpy as np
import pyarrow as pa
import pytest
from lance.sampler import ShardedBatchSampler, ShardedFragmentSampler
torch = pytest.importorskip("torch")
from lance.torch.data import LanceDataset # noqa: E402
def test_iter_over_dataset_fixed_shape_tensor(tmp_path):
data = np.random.random((10240, 32)).astype("f")
tensor_array = pa.FixedShapeTensorArray.from_numpy_ndarray(data)
ids = pa.array(range(0, 10240), type=pa.int32())
tbl = pa.Table.from_arrays([ids, tensor_array], ["ids", "vec"])
lance.write_dataset(tbl, tmp_path / "data.lance")
iter_over_dataset(tmp_path)
def test_iter_over_dataset_fixed_size_lists(tmp_path):
# 10240 of 32-d vectors.
data = np.random.random(10240 * 32).astype("f")
fsl = pa.FixedSizeListArray.from_arrays(data, 32)
ids = pa.array(range(0, 10240), type=pa.int32())
tbl = pa.Table.from_arrays([ids, fsl], ["ids", "vec"])
lance.write_dataset(tbl, tmp_path / "data.lance", max_rows_per_group=32)
iter_over_dataset(tmp_path)
def iter_over_dataset(tmp_path):
ds = lance.dataset(tmp_path / "data.lance")
# test when sample size is smaller than max_takes
torch_ds_small = LanceDataset(
ds, batch_size=256, samples=1024, columns=["ids", "vec"], cache=True
)
total_rows = 0
for batch in torch_ds_small:
assert set(batch.keys()) == {"ids", "vec"}
# row groups of 32 can be batched into 256 exactly.
assert batch["vec"].shape[0] == 256
total_rows += batch["vec"].shape[0]
assert batch["ids"].dtype == torch.int32
assert batch["vec"].shape[1] == 32
assert total_rows == 1024
# test when sample size is greater than max_takes
torch_ds = LanceDataset(
ds,
batch_size=256,
samples=4096,
columns=["ids", "vec"],
cache=True,
batch_readahead=2,
)
total_rows = 0
for batch in torch_ds:
assert set(batch.keys()) == {"ids", "vec"}
# row groups of 32 can be batched into 256 exactly.
assert batch["vec"].shape[0] == 256
total_rows += batch["vec"].shape[0]
assert batch["ids"].dtype == torch.int32
assert batch["vec"].shape[1] == 32
assert total_rows == 4096
shutil.rmtree(tmp_path / "data.lance")
total_rows = 0
# it should read from cache this time.
for batch in torch_ds_small:
assert set(batch.keys()) == {"ids", "vec"}
assert batch["ids"].dtype == torch.int32
total_rows += batch["vec"].shape[0]
assert batch["vec"].shape[1] == 32
assert total_rows == 1024
total_rows = 0
# it should read from cache this time.
for batch in torch_ds:
assert set(batch.keys()) == {"ids", "vec"}
assert batch["ids"].dtype == torch.int32
total_rows += batch["vec"].shape[0]
assert batch["vec"].shape[1] == 32
assert total_rows == 4096
def test_iter_filter(tmp_path):
arr = pa.array(range(1000))
tbl = pa.Table.from_arrays([arr], ["ids"])
ds = lance.write_dataset(tbl, tmp_path / "data.lance", max_rows_per_group=32)
def check(dataset):
total_rows = 0
for batch in dataset:
assert torch.where(batch >= 300, True, False).all()
total_rows += batch.size(dim=0)
assert batch.dtype == torch.int64
assert total_rows == 700
# No shard_grandularity
check(
LanceDataset(
ds,
batch_size=10,
filter="ids >= 300",
columns=["ids"],
)
)
# shard_grandularity fragment ok
check(
LanceDataset(
ds,
batch_size=10,
filter="ids >= 300",
columns=["ids"],
sampler=ShardedFragmentSampler(0, 1),
)
)
# sampling with filter
with pytest.raises(NotImplementedError):
check(
LanceDataset(
ds,
batch_size=10,
filter="ids >= 300",
samples=100,
columns=["ids"],
)
)
def test_sample_fragments(tmp_path: Path):
arr = pa.array(range(2000))
tbl = pa.Table.from_arrays([arr], ["ids"])
# Write 20 files
lance.write_dataset(tbl, tmp_path, max_rows_per_file=100)
ds = LanceDataset(
tmp_path,
batch_size=25,
columns=["ids"],
with_row_id=True,
sampler=ShardedFragmentSampler(rank=1, world_size=2),
)
all_ids = list(chain.from_iterable([batch["ids"].cpu().numpy() for batch in ds]))
assert all_ids == [i for i in range(2000) if i // 100 % 2 == 1]
def test_sample_batches(tmp_path: Path):
arr = pa.array(range(2000))
tbl = pa.Table.from_arrays([arr], ["ids"])
# Write 20 files
lance.write_dataset(tbl, tmp_path, max_rows_per_file=100)
ds = LanceDataset(
tmp_path,
batch_size=25,
columns=["ids"],
with_row_id=True,
sampler=ShardedBatchSampler(rank=1, world_size=2),
)
all_ids = list(chain.from_iterable([batch.cpu().numpy() for batch in ds]))
assert all_ids == [i for i in range(2000) if i // 25 % 2 == 1]
def test_filtered_sampling_odd_batch_size(tmp_path: Path):
tbl = pa.Table.from_pydict(
{
"vector": pa.array(
[[1.0, 2.0, 3.0] for _ in range(10000)], pa.list_(pa.float32(), 3)
),
"filterme": [i % 2 for i in range(10000)],
}
)
lance.write_dataset(tbl, tmp_path, max_rows_per_file=200)
ds = LanceDataset(
tmp_path,
batch_size=38,
columns=["vector"],
samples=38 * 256,
filter="vector is not null",
)
x = next(iter(ds))
assert x.shape[0] == 38
assert x.shape[1] == 3
def test_sample_batches_with_filter(tmp_path: Path):
NUM_ROWS = 10000
tbl = pa.Table.from_pydict(
{
"id": range(NUM_ROWS),
"filterme": [i % 2 for i in range(NUM_ROWS)],
}
)
lance.write_dataset(tbl, tmp_path, max_rows_per_file=2000)
ds = LanceDataset(
tmp_path,
batch_size=25,
columns=["id"],
with_row_id=True,
filter="filterme == 0",
sampler=ShardedBatchSampler(rank=3, world_size=5),
)
# The filtered sequence is 0, 2, 4, ...
#
# With rank 3 and world size 5 we should get
#
# - - - 6 -
# - - - 16 -
# - - - 26 -
# ...
all_ids = list(chain.from_iterable([batch.cpu().numpy() for batch in ds]))
# Half of the data is filtered out, divided amongst 5 workers s
# each should see 1/10th of the data
assert len(all_ids) == 1000
assert all_ids == [6 + (10 * i) for i in range(len(all_ids))]
# Now test with random order
ds = LanceDataset(
tmp_path,
batch_size=25,
columns=["id"],
with_row_id=True,
filter="filterme == 0",
sampler=ShardedBatchSampler(rank=3, world_size=5, randomize=True),
)
randomized_ids = list(chain.from_iterable([batch.cpu().numpy() for batch in ds]))
assert randomized_ids != all_ids
randomized_ids.sort()
assert randomized_ids == all_ids
@pytest.mark.parametrize("dtype", [np.uint8, np.int64])
def test_convert_int_tensors(tmp_path: Path, dtype):
data = np.random.randint(0, 256, size=128 * 32, dtype=dtype)
fsl = pa.FixedSizeListArray.from_arrays(data, 32)
ids = pa.array(range(0, 128), type=pa.int32())
tbl = pa.Table.from_arrays([ids, fsl], ["ids", "vec"])
ds = lance.write_dataset(tbl, tmp_path / "data.lance", max_rows_per_group=32)
torch_ds = LanceDataset(
ds,
batch_size=4,
)
first = next(iter(torch_ds))
assert first["vec"].dtype == torch.uint8 if dtype == np.uint8 else torch.int64
assert first["vec"].shape == (4, 32)
def test_blob_api(tmp_path: Path):
ints = pa.array(range(100), type=pa.int64())
vals = pa.array([b"0" * 1024 for _ in range(100)], pa.large_binary())
schema = pa.schema(
[
pa.field("int", ints.type),
pa.field(
"val", pa.large_binary(), metadata={"lance-encoding:blob": "true"}
),
]
)
tbl = pa.Table.from_arrays([ints, vals], schema=schema)
uri = tmp_path / "data.lance"
dataset = lance.write_dataset(tbl, uri)
torch_ds = LanceDataset(
uri, batch_size=4, dataset_options={"version": dataset.version}
)
with pytest.raises(NotImplementedError):
next(iter(torch_ds))
def to_tensor_fn(batch, *args, **kwargs):
ints = torch.tensor(batch["int"].to_numpy())
vals = []
for blob in batch["val"]:
blob.seek(100)
data = blob.read(100)
tensor = torch.tensor(np.frombuffer(data, dtype=np.uint8))
vals.append(tensor)
# vals.append(torch.tensor(blob))
vals = torch.stack(vals)
return {"int": ints, "val": vals}
torch_ds = LanceDataset(
dataset,
batch_size=4,
to_tensor_fn=to_tensor_fn,
)
first = next(iter(torch_ds))
assert first["int"].dtype == torch.int64
assert first["int"].shape == (4,)
assert first["val"].dtype == torch.uint8
assert first["val"].shape == (4, 100)