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

Dataloader now shuffles the shards and documents within #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 23 additions & 5 deletions train_gpt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ def __init__(self, B, T, process_rank, num_processes, split):
self.T = T
self.process_rank = process_rank
self.num_processes = num_processes
self.split = split
assert split in {'train', 'val'}
self.rng = np.random.default_rng(1337)

# get the shard filenames
data_root = "edu_fineweb10B"
Expand All @@ -231,10 +233,22 @@ def __init__(self, B, T, process_rank, num_processes, split):
print(f"found {len(shards)} shards for split {split}")
self.reset()

def load_shard(self, filename):
shard = load_tokens(filename)
if self.split == "train":
# split tokens into documents using the <|endoftext|> token and shuffle
eot_positions = (torch.where(shard == enc.eot_token)[0] + 1).tolist()
documents = [shard[start:end] for start, end in zip([0] + eot_positions[:-1], eot_positions)]
self.rng.shuffle(documents)
shard = torch.cat(documents) # concatenate the documents back together
return shard

def reset(self):
# state, init at shard zero
self.current_shard = 0
self.tokens = load_tokens(self.shards[self.current_shard])
if self.split == "train":
self.rng.shuffle(self.shards)
self.tokens = self.load_shard(self.shards[self.current_shard])
self.current_position = self.B * self.T * self.process_rank

def next_batch(self):
Expand All @@ -246,9 +260,13 @@ def next_batch(self):
self.current_position += B * T * self.num_processes
# if loading the next batch would be out of bounds, advance to next shard
if self.current_position + (B * T * self.num_processes + 1) > len(self.tokens):
self.current_shard = (self.current_shard + 1) % len(self.shards)
self.tokens = load_tokens(self.shards[self.current_shard])
self.current_position = B * T * self.process_rank
self.current_shard += 1
# reshuffle after each epoch
if self.current_shard == len(self.shards):
self.reset()
else:
self.tokens = self.load_shard(self.shards[self.current_shard])
self.current_position = B * T * self.process_rank
return x, y

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -518,4 +536,4 @@ def get_lr(it):
f.write(f"{step} train {loss_accum.item():.6f}\n")

if ddp:
destroy_process_group()
destroy_process_group()