File size: 12,666 Bytes
187a491 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | """
BoccaccioAI - PyTorch Datasets for Pre-Training and Instruction Fine-Tuning
Provides Dataset classes for loading pre-tokenized binary data (pre-training)
and JSONL instruction data (fine-tuning).
De Lauretis tech
"""
from __future__ import annotations
import json
from pathlib import Path
import numpy as np
import torch
from tokenizers import Tokenizer
from torch.utils.data import Dataset
class PreTokenizedDataset(Dataset):
"""Dataset for loading pre-tokenized binary data during pre-training.
Reads a flat uint16 numpy memmap file produced by ``tokenize_corpus.py``
and serves fixed-length token sequences.
"""
def __init__(self, data_path: str, sequence_length: int = 2048) -> None:
self.data_path = Path(data_path)
self.sequence_length = sequence_length
if not self.data_path.exists():
raise FileNotFoundError(f"Data file not found: {self.data_path}")
self.data = np.memmap(self.data_path, dtype=np.uint16, mode="r")
self.num_sequences = len(self.data) // self.sequence_length
if self.num_sequences == 0:
raise ValueError(
f"Data file {self.data_path} contains {len(self.data)} tokens, "
f"which is fewer than one full sequence of length {self.sequence_length}."
)
def __len__(self) -> int:
return self.num_sequences
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
if idx < 0 or idx >= self.num_sequences:
raise IndexError(
f"Index {idx} out of range for dataset with {self.num_sequences} sequences"
)
start = idx * self.sequence_length
end = start + self.sequence_length
tokens = np.array(self.data[start:end], dtype=np.int64)
tokens = torch.from_numpy(tokens)
return {"input_ids": tokens, "labels": tokens}
class InstructionDataset(Dataset):
"""Dataset for instruction fine-tuning with multi-turn conversations.
Reads JSONL files with the standard format::
{"conversations": [
{"role": "system", "content": "..."}, # optional
{"role": "user", "content": "..."},
{"role": "assistant", "content": "..."},
...
]}
Each conversation is rendered with explicit role markers::
### System:
{system_content}
### Utente:
{user_content}
### Assistente:
{assistant_content}</s>
### Utente:
{user_content_2}
### Assistente:
{assistant_content_2}</s>
Loss is computed only on assistant tokens (plus the trailing </s>).
All user, system, and marker tokens are masked with -100.
Examples longer than ``sequence_length`` after tokenization are skipped
during ``__init__`` to avoid truncating responses (which would prevent
the model from learning the EOS token).
"""
SYSTEM_MARKER = "### System:"
USER_MARKER = "### Utente:"
ASSISTANT_MARKER = "### Assistente:"
def __init__(
self,
data_path: str,
tokenizer_path: str,
sequence_length: int = 2048,
) -> None:
self.data_path = Path(data_path)
self.sequence_length = sequence_length
if not self.data_path.exists():
raise FileNotFoundError(f"Data file not found: {self.data_path}")
# Load tokenizer.
tokenizer_file = Path(tokenizer_path)
if not tokenizer_file.exists():
raise FileNotFoundError(f"Tokenizer not found: {tokenizer_file}")
self.tokenizer = Tokenizer.from_file(str(tokenizer_file))
# Resolve special token IDs.
self.pad_token_id = self.tokenizer.token_to_id("<pad>")
if self.pad_token_id is None:
self.pad_token_id = 0
self.eos_token_id = self.tokenizer.token_to_id("</s>")
if self.eos_token_id is None:
raise ValueError("EOS token '</s>' not found in tokenizer vocabulary")
# Load raw examples (JSONL). Tokenization happens on-the-fly in __getitem__
# to avoid storing 268K pre-tokenized tensors in memory (~13 GB).
self.raw_examples: list[dict] = []
with open(self.data_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
self.raw_examples.append(json.loads(line))
if not self.raw_examples:
raise ValueError(f"No examples found in {self.data_path}")
# Pre-compute token lengths to filter out examples that exceed sequence_length.
# This is fast (just encoding, no tensor creation) and avoids wasting time
# on examples that would be skipped anyway.
valid_indices: list[int] = []
skipped = 0
for i, ex in enumerate(self.raw_examples):
token_len = self._estimate_token_length(ex)
if token_len <= self.sequence_length and token_len >= 10:
valid_indices.append(i)
else:
skipped += 1
if not valid_indices:
raise ValueError(
f"All examples were skipped (too long for sequence_length={sequence_length}). "
f"Consider increasing sequence_length."
)
if skipped > 0:
print(
f" InstructionDataset: skipped {skipped} examples "
f"(exceeded sequence_length={sequence_length})"
)
self.valid_indices = valid_indices
def _render_turn(self, role: str, content: str) -> str:
"""Render a single conversation turn with its role marker."""
if role == "system":
return f"{self.SYSTEM_MARKER}\n{content}\n"
elif role == "user":
return f"{self.USER_MARKER}\n{content}\n"
elif role == "assistant":
return f"{self.ASSISTANT_MARKER}\n{content}</s>\n"
else:
raise ValueError(f"Unknown role: {role}")
def _estimate_token_length(self, ex: dict) -> int:
"""Exact token count for filtering.
Mirrors the per-turn tokenization of _process_example exactly: BPE is
not additive across turn boundaries, so encoding the concatenated text
in one pass can undercount versus per-turn encoding.
"""
conversations = ex.get("conversations", [])
if not conversations or len(conversations) < 2:
return 0
if conversations[-1]["role"] != "assistant":
return 0
total = 0
for turn in conversations:
role = turn.get("role", "")
content = (turn.get("content") or "").strip()
if not content:
continue
if role in ("system", "user"):
text = self._render_turn(role, content)
total += len(self.tokenizer.encode(text).ids)
elif role == "assistant":
marker_text = f"{self.ASSISTANT_MARKER}\n"
total += len(self.tokenizer.encode(marker_text).ids)
content_text = f"{content}</s>\n"
total += len(self.tokenizer.encode(content_text).ids)
return total
def _process_example(self, ex: dict) -> dict[str, torch.Tensor] | None:
"""Tokenize a conversation example and build labels with masking.
Returns None if the example is too long for sequence_length.
"""
conversations = ex.get("conversations", [])
if not conversations or len(conversations) < 2:
return None
# Validate: must end with assistant turn.
if conversations[-1]["role"] != "assistant":
return None
input_ids: list[int] = []
labels: list[int] = [] # -100 for masked, token_id for assistant content + </s>
for turn in conversations:
role = turn.get("role", "")
content = (turn.get("content") or "").strip()
if not content:
continue
if role == "system":
# Render and tokenize the marker + content. All masked.
text = self._render_turn(role, content)
ids = self.tokenizer.encode(text).ids
input_ids.extend(ids)
labels.extend([-100] * len(ids))
elif role == "user":
# Render and tokenize the marker + content. All masked.
text = self._render_turn(role, content)
ids = self.tokenizer.encode(text).ids
input_ids.extend(ids)
labels.extend([-100] * len(ids))
elif role == "assistant":
# Render marker (masked) + content + </s> (not masked).
marker_text = f"{self.ASSISTANT_MARKER}\n"
marker_ids = self.tokenizer.encode(marker_text).ids
content_text = f"{content}</s>\n"
content_ids = self.tokenizer.encode(content_text).ids
input_ids.extend(marker_ids)
labels.extend([-100] * len(marker_ids))
input_ids.extend(content_ids)
labels.extend(content_ids)
else:
# Unknown role, skip.
continue
# Check length: skip if too long (don't truncate - would lose </s>).
if len(input_ids) > self.sequence_length:
return None
if len(input_ids) < 10:
return None
actual_length = len(input_ids)
padding_length = self.sequence_length - actual_length
# Pad input_ids with pad_token_id.
input_ids_padded = input_ids + [self.pad_token_id] * padding_length
# Pad labels with -100 (ignored in loss).
labels_padded = labels + [-100] * padding_length
# Attention mask: 1 for real tokens, 0 for padding.
attention_mask = [1] * actual_length + [0] * padding_length
return {
"input_ids": torch.tensor(input_ids_padded, dtype=torch.long),
"labels": torch.tensor(labels_padded, dtype=torch.long),
"attention_mask": torch.tensor(attention_mask, dtype=torch.long),
}
def __len__(self) -> int:
return len(self.valid_indices)
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
if idx < 0 or idx >= len(self.valid_indices):
raise IndexError(
f"Index {idx} out of range for dataset with {len(self.valid_indices)} examples"
)
raw_idx = self.valid_indices[idx]
ex = self.raw_examples[raw_idx]
processed = self._process_example(ex)
# processed should not be None since we pre-filtered, but guard anyway.
if processed is None:
# Fallback: return a minimal valid example (shouldn't happen).
raise RuntimeError(
f"Example at index {idx} (raw {raw_idx}) failed to process "
f"despite passing pre-filter. This is a bug."
)
return processed
if __name__ == "__main__":
import sys
print("BoccaccioAI Dataset classes")
print(f" PreTokenizedDataset: for pre-training on binary memmap files")
print(f" InstructionDataset: for instruction fine-tuning on JSONL data")
if len(sys.argv) > 1 and sys.argv[1] == "--test-pretokenized":
if len(sys.argv) < 3:
print("Usage: python dataset.py --test-pretokenized <data_path>")
sys.exit(1)
ds = PreTokenizedDataset(sys.argv[2])
print(f" Loaded {len(ds)} sequences from {sys.argv[2]}")
sample = ds[0]
print(f" Sample input_ids shape: {sample['input_ids'].shape}")
print(f" Sample input_ids dtype: {sample['input_ids'].dtype}")
print(f" First 20 token IDs: {sample['input_ids'][:20].tolist()}")
if len(sys.argv) > 1 and sys.argv[1] == "--test-instruction":
if len(sys.argv) < 4:
print("Usage: python dataset.py --test-instruction <data_path> <tokenizer_path>")
sys.exit(1)
ds = InstructionDataset(sys.argv[2], sys.argv[3])
print(f" Loaded {len(ds)} examples from {sys.argv[2]}")
sample = ds[0]
print(f" Sample input_ids shape: {sample['input_ids'].shape}")
print(f" Sample labels shape: {sample['labels'].shape}")
print(f" Sample attn_mask shape: {sample['attention_mask'].shape}")
num_masked = (sample["labels"] == -100).sum().item()
num_active = sample["labels"].numel() - num_masked
print(f" Masked label positions: {num_masked}")
print(f" Active label positions: {num_active} (assistant tokens + </s>)")
|