Pad each batch, not the whole dataset#30
Open
sshleifer wants to merge 16 commits intohuggingface:masterfrom
Open
Pad each batch, not the whole dataset#30sshleifer wants to merge 16 commits intohuggingface:masterfrom
sshleifer wants to merge 16 commits intohuggingface:masterfrom
Conversation
sshleifer
commented
Oct 1, 2019
| return train_loader, valid_loader, train_sampler, valid_sampler | ||
|
|
||
|
|
||
| def make_data_lists(args, personachat, tokenizer): |
| for utterance in dialog["utterances"]: | ||
| history = utterance["history"][-(2*args.max_history+1):] | ||
| candidate_instances = defaultdict(list) | ||
| history = utterance["history"][-(2 * args.max_history + 1):] |
Contributor
Author
There was a problem hiding this comment.
could add assert len(utterance['candidates']) >= num_candidates
| return instance, sequence # TODO: second arg is never used, delete it | ||
|
|
||
|
|
||
| def pad_and_tensorize(batch_dict, padding): |
Contributor
Author
There was a problem hiding this comment.
this and ChatDataset should be easy to unit test
| valid_dataset = ChatDataset(datasets['valid'], pad_id) | ||
|
|
||
| logger.info("Build train and validation dataloaders") | ||
| train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset) if args.distributed else None |
Contributor
Author
There was a problem hiding this comment.
(maybe) put this in ChatDataset.to_loader(self, args, shuffle) -> sampler, loader
Contributor
Author
There was a problem hiding this comment.
at some point might also want to document which tensors are 3D
| for input_name, input_array in instance.items(): | ||
| datasets[dataset_name][input_name].append(input_array) | ||
| candidate_instances[input_name].append(input_array) | ||
| for k in candidate_instances.keys(): |
Contributor
Author
There was a problem hiding this comment.
.items() will save some chars
| for j, candidate in enumerate(utterance["candidates"][-num_candidates:]): | ||
| lm_labels = bool(j == num_candidates-1) | ||
| instance, _ = build_input_from_segments(persona, history, candidate, tokenizer, lm_labels) | ||
| lm_labels = bool(j == num_candidates - 1) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Previously, each sequence was padded to the length of the longest sequence in the dataset.
In this PR, each batch is padded to the length of the longest sequence in the batch. This results in a 30% speedup with negligible impact on metrics.
Code Changes
ChatDatasetyields example dicts like{'input_ids': [[hist + cand1], ..[hist +cand_n]],}for thePADDED_INPUTSandmc_token_idsandmc_labelsin the same format as previously.ChatDataset().collate_fn(examples: list)turns a list of example dicts into the list of 5 tensors by batching them and padding themget_dataloadersdoes much lessconvai_evaluation.pystill calls the oldpad_dataset1 Epoch Sanity Check
Before Change: 85 minutes
Validation: {'accuracy': 0.7483655941545956,
'average_accuracy': 0.7483655941545956,
'average_nll': 2.6815188920676687,
'average_ppl': 14.607263311061963,
'nll': 2.6815188920676687}
After Change: 60 minutes
Validation: {'accuracy': 0.7466991411357519,
'average_accuracy': 0.7466991411357519,
'average_nll': 2.6821035040007972,
'average_ppl': 14.615805388160778,
'nll': 2.6821035040007972}
Command: