collate(batch: list[object], collate_fn_map: dict[type, Callable[..., object]] | None = None)Composable collate dispatcher with user-overridable type handlers.
Same dispatch behaviour as default_collate plus an optional
collate_fn_map that overrides handling for specific element
types. Use this when a custom record type needs special-case
batching while everything else (tensors, dicts, namedtuples, ...)
should follow the default rules.
Parameters
batchlist of objectSamples emitted by a dataset for a single mini-batch.
collate_fn_mapdict= NoneMapping from element type to a callable
fn(batch, *, collate_fn_map=...). The first
isinstance(batch[0], t) match wins; on miss the function
falls back to default_collate's dispatch chain.Returns
Tensor or list or dict or tupleCollated batch. Structure mirrors default_collate's output.
Notes
When collate_fn_map is None this is exactly
default_collate. The recursive forwarding of
collate_fn_map lets nested containers reuse the same overrides.
Examples
>>> import lucid
>>> from dataclasses import dataclass
>>> from lucid.utils.data import DataLoader
>>> from lucid.utils.data.dataloader import collate
>>> @dataclass
... class MyRecord:
... text: str
... score: float
>>> def my_record_collate(batch, *, collate_fn_map=None):
... return [r.text for r in batch], lucid.tensor([r.score for r in batch])
>>> ds = [MyRecord("a", 0.5), MyRecord("b", 1.5), MyRecord("c", 2.5)]
>>> loader = DataLoader(ds, batch_size=3, collate_fn=lambda b: collate(
... b, collate_fn_map={MyRecord: my_record_collate}))
>>> texts, scores = next(iter(loader))
>>> texts, scores.tolist()
(['a', 'b', 'c'], [0.5, 1.5, 2.5])