Skip to content

Commit f19129a

Browse files
committed
Avoid full dataset materialization for non-packing SFT runs
1 parent 7b5a8e2 commit f19129a

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

src/leap_finetune/data_loaders/tokenize_data.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datasets import Dataset, Features, Sequence, Value
55
from rich.console import Console
66
from trl.data_utils import maybe_apply_chat_template, maybe_extract_prompt
7-
from trl.data_utils import pack_dataset, truncate_dataset
7+
from trl.data_utils import pack_dataset
88

99
logger = logging.getLogger(__name__)
1010
console = Console()
@@ -47,35 +47,29 @@ def tokenize_and_pack_sft(
4747
4848
Pipeline:
4949
1. Distributed tokenization via ray_ds.map()
50-
2. Materialize to HF Dataset
51-
3. If packing: pack_dataset (BFD) → adds seq_lengths column
52-
If not packing: truncate_dataset
53-
4. Convert back to Ray Dataset
50+
2. If packing: materialize to HF Dataset → pack_dataset (BFD) → back to Ray
51+
If not packing: return directly (tokenizer already truncated)
5452
"""
5553
# === 1. Distributed tokenization ===
5654
ds = ds.map(
5755
tokenize_sft,
5856
fn_kwargs={"tokenizer": tokenizer, "max_length": max_length},
5957
)
6058

61-
# === 2. Materialize to HF Dataset for TRL utilities ===
62-
# Explicit features ensure input_ids is a proper Arrow list column
63-
# (Ray may serialize variable-length lists as pickled objects otherwise)
64-
rows = list(ds.iter_rows())
65-
features = Features({"input_ids": Sequence(Value("int64"))})
66-
hf_ds = Dataset.from_list(rows, features=features)
67-
console.print(f"[dim]Tokenized {len(hf_ds):,} rows[/dim]")
68-
69-
# === 3. Pack or truncate ===
59+
# === 2. Pack or truncate ===
7060
if packing:
61+
# Packing requires full materialization into an HF Dataset
62+
rows = list(ds.iter_rows())
63+
features = Features({"input_ids": Sequence(Value("int64"))})
64+
hf_ds = Dataset.from_list(rows, features=features)
65+
console.print(f"[dim]Tokenized {len(hf_ds):,} rows[/dim]")
7166
console.print(f"[dim]Packing sequences (BFD, max_length={max_length})...[/dim]")
7267
hf_ds = pack_dataset(hf_ds, seq_length=max_length, strategy="bfd")
7368
console.print(f"[dim]Packed into {len(hf_ds):,} rows[/dim]")
74-
else:
75-
hf_ds = truncate_dataset(hf_ds, max_length=max_length)
69+
return ray.data.from_arrow(hf_ds.data.table)
7670

77-
# === 4. Convert back to Ray Dataset ===
78-
return ray.data.from_arrow(hf_ds.data.table)
71+
# Non-packing: tokenizer already truncated to max_length, just return
72+
return ds
7973

8074

8175
# === DPO Tokenization ===

0 commit comments

Comments
 (0)