Skip to content

Commit 618dcaa

Browse files
committed
fix(data): load local dataset files via explicit builder, not path auto-detect
load_dataset(path, ...) auto-detects local files from the bare path string, but that inference is unreliable for a single literal file (as opposed to a directory it can glob into) Dispatch local files through the explicit builder + data_files= form instead; directories and Hub repo ids are unaffected.
1 parent a2f03da commit 618dcaa

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

src/post_training/data/loader.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import logging
1212
import os
1313
from collections.abc import Callable
14+
from pathlib import Path
1415
from typing import TYPE_CHECKING
1516

1617
from datasets import Dataset, Features, concatenate_datasets, load_dataset
@@ -24,6 +25,37 @@
2425

2526
_MAX_NUM_PROC = len(os.sched_getaffinity(0))
2627

28+
_EXTENSION_TO_BUILDER = {
29+
".parquet": "parquet",
30+
".csv": "csv",
31+
".json": "json",
32+
".jsonl": "json",
33+
".txt": "text",
34+
}
35+
36+
37+
def _load_dataset_entry(path: str, split: str, **load_kwargs) -> Dataset:
38+
"""Load *path* as a local data file, a local dataset directory, or a Hub repo id.
39+
40+
``load_dataset(path, ...)`` auto-detects a local file from the bare
41+
``path`` string, but that inference is unreliable for a single literal
42+
file (as opposed to a directory it can glob into) — it can report "no
43+
data file found" for a file that demonstrably exists. Passing the
44+
builder name explicitly via ``data_files=`` for local files sidesteps
45+
that ambiguity; local directories and Hub repo ids keep working through
46+
the normal bare-path form.
47+
"""
48+
local_path = Path(path)
49+
if local_path.is_file():
50+
builder = _EXTENSION_TO_BUILDER.get(local_path.suffix)
51+
if builder is None:
52+
raise ValueError(
53+
f"Unsupported local dataset file extension '{local_path.suffix}' for '{path}'. "
54+
f"Supported extensions: {sorted(_EXTENSION_TO_BUILDER)}"
55+
)
56+
return load_dataset(builder, data_files=str(local_path), split=split, **load_kwargs)
57+
return load_dataset(path, split=split, **load_kwargs)
58+
2759

2860
def _resolve_num_proc(configured: int | None) -> int:
2961
"""Return the number of worker processes for ``.map()`` / ``.filter()``.
@@ -137,7 +169,7 @@ def load_and_mix_datasets(
137169
if entry.subset is not None:
138170
load_kwargs["name"] = entry.subset
139171

140-
ds = load_dataset(entry.path, split=entry.split, **load_kwargs)
172+
ds = _load_dataset_entry(entry.path, entry.split, **load_kwargs)
141173

142174
# Apply optional per-dataset transform.
143175
if entry.transform is not None:

0 commit comments

Comments
 (0)