|
4 | 4 | from datasets import Dataset, Features, Sequence, Value |
5 | 5 | from rich.console import Console |
6 | 6 | 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 |
8 | 8 |
|
9 | 9 | logger = logging.getLogger(__name__) |
10 | 10 | console = Console() |
@@ -47,35 +47,29 @@ def tokenize_and_pack_sft( |
47 | 47 |
|
48 | 48 | Pipeline: |
49 | 49 | 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) |
54 | 52 | """ |
55 | 53 | # === 1. Distributed tokenization === |
56 | 54 | ds = ds.map( |
57 | 55 | tokenize_sft, |
58 | 56 | fn_kwargs={"tokenizer": tokenizer, "max_length": max_length}, |
59 | 57 | ) |
60 | 58 |
|
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 === |
70 | 60 | 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]") |
71 | 66 | console.print(f"[dim]Packing sequences (BFD, max_length={max_length})...[/dim]") |
72 | 67 | hf_ds = pack_dataset(hf_ds, seq_length=max_length, strategy="bfd") |
73 | 68 | 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) |
76 | 70 |
|
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 |
79 | 73 |
|
80 | 74 |
|
81 | 75 | # === DPO Tokenization === |
|
0 commit comments