-
Notifications
You must be signed in to change notification settings - Fork 502
support infinite loop over alpaca dataset #66
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ class AlpacaDataset(IterableDataset): | |
seq_len (int): max sequence length | ||
world_size (int): number of data parallel processes participating in training | ||
rank (int): rank of the current data parallel process | ||
infinite: whether to loop infinitely over the dataset | ||
|
||
Data input format: | ||
{ | ||
|
@@ -43,38 +44,48 @@ def __init__( | |
seq_len: int = 2048, | ||
world_size: int = 1, | ||
rank: int = 0, | ||
infinite: bool = False, | ||
**kwargs | ||
) -> None: | ||
# TODO: This is a temporary solution for small datasets like Alpaca. | ||
# For larger datasets we need to use a more scalable approach. | ||
# Setting `streaming=True` works for large dataset, but the speed is slow. | ||
ds = load_dataset("tatsu-lab/alpaca", split="train") | ||
self.data_iterator = iter(split_dataset_by_node(ds, rank, world_size)) | ||
self._data = split_dataset_by_node(ds, rank, world_size) | ||
self._tokenizer = tokenizer | ||
self.seq_len = seq_len | ||
self.infinite = infinite | ||
|
||
def __iter__(self): | ||
max_buffer_token_len = 1 + self.seq_len | ||
all_tokens: List[int] = [] | ||
|
||
for sample in self.data_iterator: | ||
sample_text = sample["text"] | ||
sample_tokens = self._tokenizer.encode(sample_text, bos=True, eos=True) | ||
all_tokens.extend(sample_tokens) | ||
while True: | ||
for sample in iter(self._data): | ||
sample_text = sample["text"] | ||
sample_tokens = self._tokenizer.encode(sample_text, bos=True, eos=True) | ||
all_tokens.extend(sample_tokens) | ||
|
||
while len(all_tokens) >= max_buffer_token_len: | ||
x = torch.LongTensor(all_tokens[:max_buffer_token_len]) | ||
# batched_x = x.reshape(self.batch_size, -1) | ||
# update tokens to the remaining tokens | ||
all_tokens = all_tokens[max_buffer_token_len:] | ||
input = x[:-1] | ||
label = x[1:] | ||
yield input, label | ||
while len(all_tokens) >= max_buffer_token_len: | ||
x = torch.LongTensor(all_tokens[:max_buffer_token_len]) | ||
# batched_x = x.reshape(self.batch_size, -1) | ||
# update tokens to the remaining tokens | ||
all_tokens = all_tokens[max_buffer_token_len:] | ||
input = x[:-1] | ||
label = x[1:] | ||
yield input, label | ||
if not self.infinite: | ||
break | ||
Comment on lines
+77
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add some mechanic to allow a stop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think cmd + c should be sufficient? |
||
|
||
|
||
def build_alpaca_data_loader( | ||
tokenizer: TokenizerIf, batch_size: int, seq_len: int, world_size, rank | ||
tokenizer: TokenizerIf, | ||
batch_size: int, | ||
seq_len: int, | ||
world_size: int, | ||
rank: int, | ||
infinite: bool = True, | ||
): | ||
alpaca_ds = AlpacaDataset(tokenizer, seq_len, world_size, rank) | ||
alpaca_ds = AlpacaDataset(tokenizer, seq_len, world_size, rank, infinite) | ||
|
||
return DataLoader(alpaca_ds, batch_size=batch_size) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we can delete the staled comment?