Files
lancedb/docs/web-tests/py/test_curation_sampling.py
T
Xuanwo 2e46464589 docs: group ordered takes for training batches (#5351)
Training samplers can group several batches of offsets into one bounded
Enterprise request. The guide uses `take_offsets(...,
preserve_order=True)` on the sampler's table version and checks the
returned row count before splitting the rows back into the original
training batches. It explains the buffering trade-off and how to apply
the same cardinality check to row-ID lookups.

Related to [OSS-3134](https://linear.app/lancedb/issue/OSS-3134).
2026-12-02 09:19:33 +00:00

93 lines
3.0 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
import pytest
def test_sample_dataset():
from tempfile import TemporaryDirectory
import lancedb
from lancedb.permutation import Permutation, permutation_builder
with TemporaryDirectory(prefix="lancedb-sampling-") as uri:
db = lancedb.connect(uri)
# --8<-- [start:sample_dataset]
table = db.create_table("records", [{"id": i} for i in range(10)])
splits = (
permutation_builder(table).split_random(counts=[3, 7], seed=42).execute()
)
sample = Permutation.from_tables(table, splits, split=0).with_format("arrow")
selected_ids = []
for batch in sample.iter(batch_size=3):
selected_ids.extend(batch.column("id").to_pylist())
print(f"Selected {len(selected_ids)} rows: {selected_ids}")
# --8<-- [end:sample_dataset]
assert len(selected_ids) == 3
assert len(set(selected_ids)) == 3
assert all(0 <= value < 10 for value in selected_ids)
# --8<-- [start:grouped_training_takes]
def take_training_batches(table, version, offset_batches, columns):
from itertools import islice
table.checkout(version)
offset_batches = iter(offset_batches)
while group := list(islice(offset_batches, 4)):
batch_sizes = [len(batch) for batch in group]
if sum(batch_sizes) > 1024:
raise ValueError("A grouped take can request at most 1,024 rows")
offsets = [offset for batch in group for offset in batch]
rows = (
table.take_offsets(offsets, preserve_order=True).select(columns).to_arrow()
)
if rows.num_rows != len(offsets):
raise ValueError("Some training offsets are missing from the snapshot")
start = 0
for size in batch_sizes:
yield rows.slice(start, size)
start += size
# --8<-- [end:grouped_training_takes]
def test_grouped_training_takes(tmp_db):
writer = tmp_db.create_table(
"training", [{"id": i, "label": i % 2} for i in range(6)]
)
version = writer.version
writer.delete("id = 0")
table = tmp_db.open_table("training")
offset_batches = iter([[5, 1, 5], [2, 5], [0], [3, 1], [4]])
batches = take_training_batches(table, version, offset_batches, ["id"])
assert [batch.to_pydict() for batch in batches] == [
{"id": [5, 1, 5]},
{"id": [2, 5]},
{"id": [0]},
{"id": [3, 1]},
{"id": [4]},
]
@pytest.mark.parametrize(
("offset_batches", "message"),
[
([[1], [99, 0]], "Some training offsets are missing"),
([[0] * 1025], "A grouped take can request at most 1,024 rows"),
],
)
def test_grouped_training_takes_reject_invalid_group(tmp_db, offset_batches, message):
table = tmp_db.create_table("training", [{"id": 0}, {"id": 1}])
batches = take_training_batches(table, table.version, offset_batches, ["id"])
with pytest.raises(ValueError, match=message):
next(batches)