Download spotlight/data.py from Vibelet/spotlight-memory-synth-1b: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/Vibelet/spotlight-memory-synth-1b/resolve/main/spotlight/data.py
- Command line
-
hf download hf://Vibelet/spotlight-memory-synth-1b/spotlight/data.py
-
curl -L -o data.py https://huggingface.co/Vibelet/spotlight-memory-synth-1b/resolve/main/spotlight/data.py
1.4 kB
| """Memory-mapped token windows over the prepacked SYNTH int32 bins.""" | |
| from __future__ import annotations | |
| import numpy as np | |
| import torch | |
| from torch.utils.data import DataLoader, Dataset | |
| class TokenWindows(Dataset): | |
| """Fixed windows over a flat int32 token file (GPT-2 tokens, ``prepack`` output).""" | |
| def __init__(self, path: str, ctx_len: int): | |
| self.path = path | |
| self.ctx = ctx_len | |
| self.tokens = np.memmap(path, dtype=np.int32, mode="r") | |
| if self.tokens.size < ctx_len + 1: | |
| raise ValueError(f"{path} holds {self.tokens.size} tokens, need > {ctx_len}") | |
| self.n = (self.tokens.size - 1) // ctx_len | |
| def __len__(self) -> int: | |
| return self.n | |
| def __getitem__(self, i: int) -> tuple[torch.Tensor, torch.Tensor]: | |
| start = i * self.ctx | |
| chunk = torch.as_tensor( | |
| np.asarray(self.tokens[start:start + self.ctx + 1], dtype=np.int64)) | |
| return chunk[:-1], chunk[1:] | |
| def make_token_loader(bin_path: str, ctx_len: int, batch_size: int, | |
| shuffle: bool, seed: int = 0, num_workers: int = 0) -> DataLoader: | |
| gen = torch.Generator().manual_seed(seed) | |
| return DataLoader( | |
| TokenWindows(bin_path, ctx_len), | |
| batch_size=batch_size, | |
| shuffle=shuffle, | |
| drop_last=True, | |
| num_workers=num_workers, | |
| generator=gen if shuffle else None, | |
| ) | |