Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load_clean_dataset function to dataset.py #18

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/delphi/dataset/dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from datasets import load_dataset
from tqdm.auto import tqdm

def load_clean_dataset(split: str, tokenized: bool = False) -> list[str]:
# checking just startswith, because you can include slice like "train[:1000]"
assert split.startswith("train") or split.startswith("validation")
hf_ds = load_dataset(f"jbrinkma/tinystories-v2-clean{'-tokenized' if tokenized else ''}", split=split)
dataset = []
# hf_ds technically isn't guaranteed to be subscriptable, but it is in this case
for sample in tqdm(hf_ds["tokens" if tokenized else "text"]): # type: ignore
dataset.append(sample)
return dataset

def token_map(tokenized_dataset: list[list[int]]) -> dict[int, list[tuple[int, int]]]:
mapping = {}

for prompt_idx, prompt in enumerate(tokenized_dataset):
for token_idx, token in enumerate(prompt):
mapping.setdefault(token, []).append((prompt_idx, token_idx))

return mapping
Loading