mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
docs: fix training and dedupe examples (#4932)
## Summary - Save per-rank dataset checkpoints to separate files and load the matching file when resuming. - Add the missing LanceDB import to the data loader example. - Correct the count of distinct rows in the deduplication example. ## Validation - `git diff --check` - Parsed the Python snippets in the affected training pages with `ast.parse`. --------- Co-authored-by: Haocheng Liu <haocheng.liu@general-int.com>
This commit is contained in:
co-authored by
Haocheng Liu
parent
c9faa78cc6
commit
80582fb87d
@@ -66,7 +66,7 @@ both **squared** L2, not the plain L2 a plain SQL `distance()` call returns.
|
||||
|
||||
`dedupe` returns one representative row per cluster of records within
|
||||
`distance_threshold` of each other -- rows 10/20 collapse to one, 30/40/50
|
||||
collapse to one, and the four distinct rows pass through unchanged, leaving
|
||||
collapse to one, and the three distinct rows pass through unchanged, leaving
|
||||
ids 10, 30, 60, 70 and 80:
|
||||
|
||||
<CodeGroup>
|
||||
|
||||
@@ -15,6 +15,8 @@ terminology from [Distributed Training](/training/distributed-training#paralleli
|
||||
|
||||
A stored `StreamingDataset` state reflects only round-robin cycles that have been completed across all splits; samples completed partway through a cycle at the time of saving can be replayed. To ensure efficient saving, make sure `global_batch_size` is a multiple of `num_splits`, as in the example below.
|
||||
|
||||
Each rank saves its own dataset state to a separate file. Keep the files from every rank at the same checkpoint step to resume across different topologies.
|
||||
|
||||
<CodeGroup>
|
||||
```py Python icon=Python
|
||||
import torch
|
||||
@@ -43,7 +45,7 @@ for step, batch in enumerate(dataloader):
|
||||
if (step + 1) % checkpoint_interval == 0:
|
||||
torch.save(
|
||||
{"model": model.state_dict(), "dataset": dataset.state_dict()},
|
||||
f"checkpoint_{step + 1}.pt",
|
||||
f"checkpoint_{step + 1}_rank_{rank}.pt",
|
||||
)
|
||||
```
|
||||
</CodeGroup>
|
||||
@@ -80,7 +82,7 @@ for step, batch in enumerate(dataloader):
|
||||
if (step + 1) % checkpoint_interval == 0:
|
||||
torch.save(
|
||||
{"model": model.state_dict(), "dataset": dataset.state_dict()},
|
||||
f"checkpoint_{step + 1}.pt",
|
||||
f"checkpoint_{step + 1}_rank_{rank}.pt",
|
||||
)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
@@ -12,6 +12,7 @@ Advanced data loading scenarios can be handled by the `StreamingDataset`. This o
|
||||
|
||||
<CodeGroup>
|
||||
```py Python icon=Python
|
||||
import lancedb
|
||||
import torch
|
||||
from lancedb.streaming import StreamingDataset
|
||||
|
||||
|
||||
@@ -47,6 +47,11 @@ The following constraints apply:
|
||||
- The filtered row count must be at least `num_splits`. If it is not evenly divisible by `num_splits`, up to
|
||||
`num_splits - 1` surplus rows are dropped so that every split has the same length.
|
||||
|
||||
These constraints do not guarantee identical per-rank batches when `num_workers` changes. Workers read
|
||||
different subgroups of a rank's splits, so changing worker count can change batch composition even if
|
||||
the remaining rows are the same. For exact batch replay on the same topology, see
|
||||
[Resuming](/training/resuming#same-topology).
|
||||
|
||||
For example, to switch between $8$ and $6$ GPUs, use a common multiple such as $24$ for `num_splits`. Highly composite values can support many layouts; for example, `num_splits = 48` is compatible with $10$ distinct possible GPU counts. Remember to include `num_workers` when checking divisibility.
|
||||
|
||||
<CodeGroup>
|
||||
|
||||
@@ -8,7 +8,7 @@ availability:
|
||||
enterprise: available
|
||||
---
|
||||
|
||||
Resuming loads a `StreamingDataset` checkpoint saved by [Checkpointing](/training/checkpointing) back into a fresh dataset and `DataLoader`, so a failed or restarted run picks up where it left off rather than rereading from the start.
|
||||
Resuming loads a `StreamingDataset` checkpoint saved by [Checkpointing](/training/checkpointing) back into a fresh dataset and loader, so a failed or restarted run picks up where it left off rather than rereading from the start.
|
||||
|
||||
This page assumes the `rank`, `world_size`, `num_splits` and `shuffle_seed`
|
||||
terminology from [Distributed Training](/training/distributed-training#parallelism-and-elasticity).
|
||||
@@ -18,9 +18,9 @@ terminology from [Distributed Training](/training/distributed-training#paralleli
|
||||
<CodeGroup>
|
||||
```py Python icon=Python
|
||||
import torch
|
||||
from lancedb.streaming import StreamingDataset
|
||||
from lancedb.streaming import StreamingDataLoader, StreamingDataset
|
||||
|
||||
checkpoint = torch.load("checkpoint_100.pt")
|
||||
checkpoint = torch.load(f"checkpoint_100_rank_{rank}.pt")
|
||||
model.load_state_dict(checkpoint["model"])
|
||||
|
||||
dataset_state = checkpoint["dataset"]
|
||||
@@ -34,25 +34,49 @@ dataset = StreamingDataset(
|
||||
)
|
||||
dataset.load_state_dict(dataset_state)
|
||||
|
||||
# The combined batch size across every rank, the same value used when the
|
||||
# checkpoint was saved; each rank's DataLoader gets its world_size share of it.
|
||||
# Use the same global batch size as the run that saved the checkpoint.
|
||||
global_batch_size = 48
|
||||
batch_size = global_batch_size // world_size
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Choose the loader that matches the one used to save the checkpoint:
|
||||
|
||||
<CodeGroup>
|
||||
```py "Single-process loader" icon=Python
|
||||
dataloader = torch.utils.data.DataLoader(
|
||||
dataset,
|
||||
batch_size=global_batch_size // world_size,
|
||||
batch_size=batch_size,
|
||||
num_workers=0,
|
||||
)
|
||||
```
|
||||
|
||||
```py "Multi-worker loader" icon=Python
|
||||
dataloader = StreamingDataLoader(
|
||||
dataset,
|
||||
batch_size=batch_size,
|
||||
num_workers=4,
|
||||
)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
Continue training with the selected loader:
|
||||
|
||||
<CodeGroup>
|
||||
```py Python icon=Python
|
||||
for batch in dataloader:
|
||||
train_step(batch)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
`load_state_dict()` rejects a checkpoint whose `num_splits` or `shuffle_seed` differs from the new dataset. For an
|
||||
exact resume, also use the same table snapshot, `epoch`, `shuffle`, `filter`, and other data-selection settings, and
|
||||
keep `world_size` the same -- changing it is a topology change, covered next. The number of DataLoader workers may
|
||||
still change as long as the batch-size divisibility constraints from
|
||||
[Distributed Training](/training/distributed-training#parallelism-and-elasticity) hold.
|
||||
`load_state_dict()` rejects a checkpoint whose `num_splits` or `shuffle_seed` differs from the new dataset.
|
||||
To reproduce the same ordered batches on each rank, use the same table snapshot, `epoch`, `shuffle`,
|
||||
`filter`, and other data-selection settings. Keep `world_size`, the loader type, `num_workers`, the batch
|
||||
size, and collation the same as in the run that saved the checkpoint.
|
||||
|
||||
Changing `num_workers` changes how splits are grouped. A resumed run can recover the same remaining rows
|
||||
but group or order them differently, even when the
|
||||
[divisibility constraints](/training/distributed-training#parallelism-and-elasticity) hold.
|
||||
|
||||
## Resuming Across Different Topologies {#resuming-across-different-topologies}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user