Datasets:
Upload load_dataset.py with huggingface_hub
Browse files- load_dataset.py +62 -0
load_dataset.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Load the FineWeb-Edu Byte-Level dataset from HuggingFace."""
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
from torch.utils.data import IterableDataset, DataLoader, get_worker_info
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
BOS_ID = 257
|
| 11 |
+
EOS_ID = 258
|
| 12 |
+
PAD_ID = 256
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class FineWebEduByteLevelDataset(IterableDataset):
|
| 16 |
+
def __init__(self, data_dir, seq_len=2048, rank=0, world_size=1):
|
| 17 |
+
self.seq_len = seq_len
|
| 18 |
+
self.data_dir = data_dir
|
| 19 |
+
self.rank = rank
|
| 20 |
+
self.world_size = world_size
|
| 21 |
+
self._files = self._discover_files()
|
| 22 |
+
|
| 23 |
+
def _discover_files(self):
|
| 24 |
+
import glob as _glob
|
| 25 |
+
files = sorted(_glob.glob(os.path.join(self.data_dir, "**/*.bin"), recursive=True))
|
| 26 |
+
return [f for i, f in enumerate(files) if i % self.world_size == self.rank]
|
| 27 |
+
|
| 28 |
+
def __iter__(self):
|
| 29 |
+
worker = get_worker_info()
|
| 30 |
+
num_workers = worker.num_workers if worker else 1
|
| 31 |
+
worker_id = worker.id if worker else 0
|
| 32 |
+
files = [f for i, f in enumerate(self._files) if i % num_workers == worker_id]
|
| 33 |
+
for filepath in files:
|
| 34 |
+
arr = np.memmap(filepath, dtype=np.uint16, mode='r')
|
| 35 |
+
pos = 0
|
| 36 |
+
while pos + self.seq_len + 1 <= len(arr):
|
| 37 |
+
chunk = arr[pos:pos + self.seq_len + 1]
|
| 38 |
+
pos += self.seq_len + 1
|
| 39 |
+
x = torch.tensor(chunk[:-1], dtype=torch.long)
|
| 40 |
+
y = torch.tensor(chunk[1:], dtype=torch.long)
|
| 41 |
+
y[y == PAD_ID] = -100
|
| 42 |
+
yield x, y
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def encode(text: str) -> list:
|
| 46 |
+
return [BOS_ID] + list(text.encode('utf-8')) + [EOS_ID]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def decode(ids: list) -> str:
|
| 50 |
+
return bytes(i for i in ids if 0 <= i <= 255).decode('utf-8', errors='replace')
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
from huggingface_hub import snapshot_download
|
| 55 |
+
data_dir = snapshot_download("CLIWorks/Spider-FLEXITOKENS-FP8", repo_type="dataset", allow_patterns=["data/*.bin", "data/metadata.json"])
|
| 56 |
+
data_dir = os.path.join(data_dir, "data")
|
| 57 |
+
ds = FineWebEduByteLevelDataset(data_dir, seq_len=2048)
|
| 58 |
+
loader = DataLoader(ds, batch_size=4, num_workers=0, pin_memory=True)
|
| 59 |
+
for i, (x, y) in enumerate(loader):
|
| 60 |
+
print(f"Batch {i}: x={x.shape}, y={y.shape}")
|
| 61 |
+
if i >= 2:
|
| 62 |
+
break
|