llm.data — Datasets, DataModules, and Sources
The llm.data package is split into three layers:
- Sources (
llm.data.sources) — pluggable text iterators backed by local files or HuggingFace streaming. Plugins register intoSOURCE_REGISTRY. - Datasets (
llm.data.datasets) —IterableDatasetandMapwrappers that turn raw text into token chunks ready for the trainer. - DataModules (
llm.data.modules) —Lightning-stylesetup/prepare_data/train_dataloader/val_dataloadercontainers that combine the above with config validation and checkpoint resume.
See the data guide for end-to-end usage.
Built-in Dataset Presets
The presets module ships well-known pretraining dataset configurations so users don't have to hand-author the HF triples.
presets
Built-in data presets for common pretraining datasets.
The project already ships a streaming data pipeline
(:class:llm.data.modules.streaming.StreamingTextDataModule +
:class:llm.data.sources.HFStreamTextSource), but every well-known
dataset requires hand-authoring the DataConfig triple
(dataset_name, dataset_config, text_column). This module
ships those triples out of the box so users can pick a dataset by
name instead of looking up the HF identifier every time.
The presets are intentionally decoupled from the datasets
package: this module imports nothing from
llm.data.datasets or llm.data.modules, only
:class:llm.training.core.config.DataConfig. That keeps the import
cheap on hosts that don't have datasets installed.
Example
from llm.training.core.config import DataConfig from llm.data.presets import C4_PRESET, apply_to_config cfg = DataConfig(data_source="hf", max_seq_len=2048) apply_to_config(cfg, C4_PRESET) cfg.dataset_name 'allenai/c4' cfg.dataset_config 'en' cfg.text_column 'text'
DatasetPreset
dataclass
A well-known HuggingFace dataset configuration.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
dataset_name |
str
|
HuggingFace dataset identifier (e.g.
|
dataset_config |
str | None
|
HF dataset config name (subset /
|
dataset_split |
str
|
Split to stream. |
text_column |
str
|
Name of the text field in each row. Most
English-text datasets use |
description |
str
|
Human-readable one-liner for CLI / docs. |
preset_name |
str
|
Canonical short name (lowercase, kebab-case)
used by :func: |
源代码位于: src/llm/data/presets.py
apply_to_config
Mutate config (a :class:DataConfig) to bind to preset.
Sets data_source="hf" and the four HF fields
(dataset_name, dataset_config, dataset_split,
text_column). Unrelated fields (max_seq_len,
tokenizer_*, val_dataset_path, …) are left untouched.
The mutated config is returned for fluent use:
.. code-block:: python
cfg = apply_to_config(DataConfig(...), C4_PRESET)
引发:
| 类型 | 描述 |
|---|---|
TypeError
|
if |
源代码位于: src/llm/data/presets.py
resolve_preset
Look up a preset by name.
name may be:
- the preset's canonical short name (
"c4","the-pile","redpajama/c4"…), or - the full HuggingFace dataset id (
"allenai/c4").
引发:
| 类型 | 描述 |
|---|---|
KeyError
|
if no preset matches. The error message includes the available preset names so callers can self-correct. |
源代码位于: src/llm/data/presets.py
list_presets
Pluggable Text Sources
The TextSource abstraction + SOURCE_REGISTRY plugin entry
points. Most users won't need to read this — the built-in
local and hf sources cover the common cases — but custom
sources (S3, GCS, private archives) plug in here.
sources
Pluggable text sources for streaming data pipelines.
TextSource
Bases: ABC
Abstract source of text records for streaming datasets.
源代码位于: src/llm/data/sources.py
iter_texts
abstractmethod
source_fingerprint
LocalLineTextSource
Bases: TextSource
Stream UTF-8 text line-by-line from a local file.
源代码位于: src/llm/data/sources.py
HFStreamTextSource
Bases: TextSource
Stream text from a HuggingFace dataset in streaming mode.
源代码位于: src/llm/data/sources.py
DedupTextSource
Bases: TextSource
TextSource wrapper that drops duplicate records by content hash.
Useful for pretraining data preparation where web-crawl-derived corpora contain substantial exact duplicates. The wrapper:
- hashes the normalized text and drops records whose hash has already been yielded this run;
- optionally loads a pre-populated "seen hashes" file on construction so dedup state is shared across runs / shards;
- optionally appends new hashes to that file so dedup state grows monotonically;
- exposes a stable :meth:
source_fingerprintthat includes the inner source's fingerprint plus the dedup strategy, so :func:validate_source_fingerprintcatches configuration drift on checkpoint resume.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
inner
|
TextSource
|
The wrapped source. Records yielded by
|
必需 |
normalize
|
Callable[[str], str] | None
|
Optional callable that normalizes text before
hashing. Default: :func: |
None
|
seen_hashes_path
|
str | Path | None
|
Optional path to a file containing previously seen hashes (one per line, hex-encoded). If the file exists when the wrapper is constructed, its contents are loaded into the seen-set so dedup state survives across runs. |
None
|
write_seen_hashes
|
bool
|
If True, append new hashes to
|
False
|
hash_algo
|
str
|
Name of any algorithm accepted by :func: |
'sha256'
|
Example
src = LocalLineTextSource("data.txt") dedup = DedupTextSource(src, seen_hashes_path="seen.txt") unique_texts = list(dedup.iter_texts())
源代码位于: src/llm/data/sources.py
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 | |
build_text_source
Resolve TextSource from DataConfig via SOURCE_REGISTRY.
源代码位于: src/llm/data/sources.py
source_fingerprint_from_config
Build a stable fingerprint for the configured text source without loading data.
validate_source_fingerprint
Raise if checkpoint source metadata does not match the active DataModule config.