Files
lancedb/docs/web/training/permutation.mdx
T
Yang Cen 00ead8d0b6 fix(dataloader): skip oversized grouped split sorts (#5373)
A random split assigns split IDs in ascending order after shuffling.
When that produces a single large batch, DataFusion can reject the batch
before it has anything to spill. On main, a 4M-row split requests 122.1
MiB and a 6M-row split requests 183.1 MiB. The default 100 MiB pool has
90 MiB available after the merge reservation.

Write the already-grouped batch directly when that final sort cannot
admit it. Keep the existing sort when the batch fits, for multiple
shuffle runs and for an explicit shuffle after split assignment.
Document the client-side memory override and include it in
resource-exhausted sort errors.

The 11M-row multi-run case and the 4M-row explicit post-split shuffle
still need a larger budget. Both keep their exact order with the same
override. The multi-run path can interleave equal split IDs across runs,
so this patch keeps that merge behavior.

## Validation

On isolated Linux x86-64, baseline `614f8c32` and this head used
`release-with-debug`, the same Embedded fixture, seed 42, and four
alternating AB/BA repetitions per case. Each run used a fresh 4 GiB
cgroup on the same 8-vCPU EC2 instance. EOF includes permutation build
and reading all output rows. Every successful pair produced the same
ordered `(row_id, split_id)` digest and split counts. The 4M and 6M
single-run cases had zero temporary spill bytes. The 11M case used
46,046,296 temporary bytes in both arms and cleaned up after success and
failure.

| Scenario / metric (lower is better) | Baseline | This PR | Benefit |
| --- | ---: | ---: | ---: |
| 2M random splits, default / default, median EOF | 169.5 ms | 163.0 ms
| 1.04× faster |
| 2M random splits, default / default, median peak RSS | 214.7 MiB |
209.6 MiB | 1.02× lower |
| 4M random splits, override / default, median EOF | 337.5 ms | 265.0 ms
| 1.27× faster |
| 4M random splits, override / default, median peak RSS | 290.2 MiB |
243.0 MiB | 1.19× lower |
| 6M random splits, override / default, median EOF | 504.5 ms | 424.5 ms
| 1.19× faster |
| 6M random splits, override / default, median peak RSS | 357.4 MiB |
283.1 MiB | 1.26× lower |

The override is 2 GiB. The 4M and 6M baseline cases failed at the
default limit. The 6M override / override control measured 511.5 ms
versus 507.5 ms median EOF and 357.5 MiB versus 357.1 MiB median peak
RSS.

Adds a small regression that compares exact filtered split order at two
sort budgets and checks that an explicit shuffle still uses the sorter.


[OSS-3341](https://linear.app/lancedb/issue/OSS-3341/streamingdataset-permutationbuilder-fails-on-tables-over-3m-rows)
2026-12-03 10:21:28 +08:00

252 lines
12 KiB
Plaintext

---
title: "Permutation"
sidebarTitle: "Permutation"
description: "The low-level, customizable row ordering that StreamingDataset is built on."
icon: shuffle
availability:
embedded: available
enterprise: available
---
Most model training frameworks feed data into a model by sequencing through successive batches, a process
often referred to as **data loading**. The simplest way to perform data loading is to read a LanceDB table in batches,
in the order its rows are stored, through an identity `Permutation`. A table itself is not iterable.
<CodeGroup>
```py Python icon=Python
from lancedb.permutation import Permutation
db = lancedb.connect("file://some/db/path")
table = db.open_table("some_table")
for batch in Permutation.identity(table).with_format("arrow").iter(batch_size=1024):
print(batch.to_pydict())
```
</CodeGroup>
In specific use cases, you may need the flexibility to shuffle, split, and select data without using the full
[`StreamingDataset`](/training/data-loading) object. In these cases, use `Permutation`, the lower-level
functionality `StreamingDataset` is built on. A `Permutation` defines a customizable row ordering, and supports
map-style access through `__getitem__()` and batched access through `__getitems__()`.
## Persisting a permutation {#persisting}
By default a permutation table is stored in memory. Call `persist` before `execute` to write it to an embedded
LanceDB connection backed by local files or object storage. The plan becomes an ordinary table, so another process
with access to the same storage can open it and use `Permutation.from_tables` to read the same split.
Only the plan destination must be embedded. The base table can be Embedded or Enterprise; an Enterprise base is
pinned through the plan's recorded base branch and version.
<CodeGroup>
```py Python icon=Python
from lancedb.permutation import Permutation, permutation_builder
db = lancedb.connect("file://some/db/path")
table = db.open_table("some_table")
plan = (
permutation_builder(table)
.split_random(ratios=[0.8, 0.2], seed=42, split_names=["train", "validation"])
.persist(db, "splits_v1")
.execute()
)
train = Permutation.from_tables(table, db.open_table("splits_v1"), split="train")
print(train.base_version, train.base_branch)
```
</CodeGroup>
The table returned by `execute` keeps the destination connection and namespace context, so it can be pickled and
reopened directly in a worker process.
`mode="create"` is the default and fails when the destination table already exists. Pass `mode="overwrite"` to
replace it explicitly. Overwriting writes a new table version; readers that already opened the previous version keep
reading that snapshot. Use `namespace_path` to create the plan in a namespace. Persisting the plan itself to a
LanceDB Enterprise connection is not supported yet, because reproducible Enterprise plan ordering requires
deterministic remote row ordering.
The plan records the base table version and branch in its schema metadata. `Permutation.base_version` and
`Permutation.base_branch` read those values without querying the base table; both are `None` for older plans that did
not record them.
## Version Pinning {#version-pinning}
A `Permutation` is pinned to the version of its base table at the time it was constructed, and every read (including future reads
from a `StreamingDataset` that wraps it, and every DataLoader worker after it starts) resolves against that pinned
version. This makes iteration deterministic across compactions and worker restarts: the permutation only addresses rows
that existed when it was built, and rows appended to the base table afterwards are not visible through the existing
permutation.
To include newly appended rows, build a new `Permutation` (and any `StreamingDataset` that depends on it)
against the updated table. Permutations written before this behavior shipped continue to
read as they did before.
## Sort memory for permutation builds
The permutation sort runs on the SDK client for both Embedded and Enterprise tables. Its memory pool defaults to
100 MiB. If a permutation sort runs out of memory, set `LANCEDB_PERM_BUILDER_MEMORY_LIMIT` to a byte count in the
client environment. Shuffle buffers and the materialized permutation can use additional client memory.
## Output formats {#output-formats}
By default, a `Table` data loader emits Arrow data. `collate_fn` is PyTorch's batching hook: PyTorch calls it to
turn the fetched items into one batch. PyTorch's default collate function only knows how to combine tensors, NumPy
arrays, numbers, dicts, and lists, so it does not accept Arrow data directly. When using a `Table` directly, pass
LanceDB's `lancedb.util.tbl_to_tensor` helper as PyTorch's `collate_fn`; it converts numeric Arrow columns into a
column-major `torch.Tensor` with shape `(columns, rows)`.
`Permutation` works differently: its default output is a list of Python dicts, which PyTorch's default collate function
can batch into a dict of tensors. However, converting every value to a Python object has a significant performance
penalty. To convert the Arrow data in different ways, call `with_format()` with one of the formats below. Here `B` is
the batch size, `C` is the number of columns, and `*S` is the shape of one sample.
| Format | Output of `iter(batch_size=B)` | DataLoader collate |
|---|---|---|
| `"arrow"` | `pyarrow.RecordBatch` | Not accepted directly |
| `"torch_dict"` | `dict[str, Tensor]`, one `(B, *S)` tensor per column | Identity function |
| `"torch"` | `list[dict[str, Tensor]]`, one `(*S)` tensor per column per row | Default collate produces `dict[str, Tensor]` with `(B, *S)` |
| `"torch_col"` | One `(C, B, *S)` tensor | Identity function |
| `"torch_row"` | `list[Tensor]`, one `(C, *S)` tensor per row | Default collate produces `(B, C, *S)` |
| `"numpy"` | One NumPy ndarray | Not accepted directly |
`torch_dict` is usually the best choice for training because it keeps column names, handles columns with different
sample shapes, and shares Arrow storage where the layout allows. `torch_col` and `torch_row` require every column to
have the same sample shape. `torch_row` combines numeric dtypes with `torch.promote_types`, and `torch_col` promotes
supported mixed numeric dtypes through `torch.stack`. If they see mixed scalar and vector columns, they name the
mismatched fields and shapes and point you to `torch_dict`.
`torch_dict`, `torch`, and `torch_col` reject null values. `torch_row` preserves the previous NaN behavior for
nullable scalar integer and floating columns, but rejects nulls in nested fixed-size-list and tensor columns. Use the
`"python"` or `"arrow"` format for other nullable columns, or drop/fill the nulls first. `lance.bfloat16` values are
supported when the leaf field is `fixed_size_binary[2]` with the `ARROW:extension:name` metadata set to
`lance.bfloat16`; these become `torch.bfloat16` tensors. An unmarked binary column is rejected rather than guessed to
be bfloat16.
The `numpy` format returns one ndarray built with `batch.to_pandas().to_numpy()`. The pandas conversion may copy data
and may promote mixed numeric columns to a common numeric dtype, for example int64 and float32 to float64. Nested,
list, or incompatible columns produce an object ndarray rather than a numeric array.
### Using torch_dict with a torch data loader {#using-torch-dict-with-a-torch-data-loader}
`torch_dict` produces a dictionary of per-column tensors for each batch. Pass an identity function as `collate_fn` so
PyTorch returns that dictionary unchanged. Scalar columns have shape `(B,)`, a `FixedSizeList<float, D>` column has
shape `(B, D)`, and nested fixed-size lists contribute one dimension per level. Fixed-shape tensor columns use their
logical shape, with the Arrow axis permutation applied as a view.
<CodeGroup>
```python Python icon="python"
import torch
from lancedb.permutation import Permutation
permutation = Permutation.identity(table).with_format("torch_dict")
dataloader = torch.utils.data.DataLoader(
permutation,
batch_size=1024,
shuffle=True,
collate_fn=lambda batch: batch,
)
for batch in dataloader:
print(batch["id"].shape) # (1024,)
print(batch["vector"].shape) # (1024, 128)
```
</CodeGroup>
When `num_workers > 0`, replace the lambda with a picklable top-level function that returns its argument.
### Using the torch column format with a torch data loader {#using-the-torch-column-format-with-a-torch-data-loader}
The `torch_col` format converts the entire Arrow batch to one tensor with shape `(C, B, *S)`. For a table of scalar
columns this is the familiar `(columns, rows)` tensor. For a single column it is an `unsqueeze(0)` view of the
per-column tensor, so no data is copied; stacking multiple columns does copy.
PyTorch's default collate does not accept a single multi-dimensional tensor. Pass an identity function as `collate_fn`
to receive the tensor directly from the data loader.
<CodeGroup>
```python Python icon="python"
from lancedb.permutation import Permutation
permutation = Permutation.identity(table).with_format("torch_col")
dataloader = torch.utils.data.DataLoader(permutation, collate_fn=lambda x: x)
```
</CodeGroup>
## Transforming rows with map {#transforming-rows-with-map}
To change rows in Python as they are read, call `map()` with a function that takes a row as a dict and returns a dict.
Calls can be chained, and the functions run in order. Pass `batched=True` to receive the whole batch as a dict of
column lists instead; the function must then return one value per input row for every column.
<CodeGroup>
```python Python icon="python"
import lancedb
import pyarrow as pa
from lancedb.permutation import Permutation
db = lancedb.connect("memory://")
reviews = db.create_table(
"reviews",
pa.table({"text": ["Great", "Awful", "Fine"], "stars": [5, 1, 3]}),
)
def lowercase(row):
return {"text": row["text"].lower(), "stars": row["stars"]}
def add_label(batch):
return {**batch, "label": [int(stars >= 3) for stars in batch["stars"]]}
permutation = Permutation.identity(reviews).map(lowercase).map(add_label, batched=True)
print(permutation.take_offsets([0, 1]))
# [{'text': 'great', 'stars': 5, 'label': 1}, {'text': 'awful', 'stars': 1, 'label': 0}]
```
</CodeGroup>
The dict a function returns replaces the row. A column the function does not return is dropped, so return every
column you want to keep. A mapped `Permutation` always produces a list of dicts, like the default format, whether or
not `batched` is set, so PyTorch's default collate function can batch it. `select_columns()` and `schema` still
describe the columns read from the table, which are the input of the first function.
`map()` cannot be combined with `with_format()` or `with_transform()`. Calling one after the other, in either order,
raises a `ValueError`. To build tensors from mapped rows, convert them in the DataLoader's `collate_fn`. When
`num_workers > 0`, pass picklable top-level functions to `map()`, not lambdas.
## Selecting columns {#selecting-columns}
By default, the `Table` class will return all columns in the table when used as input to PyTorch. If you only need
a subset of columns, you can significantly reduce your I/O requirements by selecting only the columns you need. The
`Permutation` class provides a `select_columns` method that provides this functionality.
<CodeGroup>
```python Python icon="python"
import pyarrow as pa
import torch
from lancedb.permutation import Permutation
db = lancedb.connect("memory://")
prompts = db.create_table(
"prompts",
pa.table(
{
"id": range(4),
"prompt": ["north", "south", "east", "west"],
"embedding": [[0.1, 0.2], [0.3, 0.4], [0.5, 0.6], [0.7, 0.8]],
}
),
)
permutation = Permutation.identity(prompts).select_columns(["id", "prompt"])
dataloader = torch.utils.data.DataLoader(
permutation, batch_size=1024, shuffle=True
)
for batch in dataloader:
print(sorted(batch)) # ['id', 'prompt']: the embedding is never read
```
</CodeGroup>
Each batch is a dict of the selected columns, the default `Permutation` format described above.