-
Notifications
You must be signed in to change notification settings - Fork 38
Sort and unsort the input texts for transformers #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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import argparse | ||
| import json | ||
| from pathlib import Path | ||
| from typing import List | ||
|
|
||
| import torch | ||
| from tqdm import tqdm | ||
|
|
@@ -17,6 +18,37 @@ def chunks(lst, n): | |
| for i in range(0, len(lst), n): | ||
| yield lst[i:i + n] | ||
|
|
||
| def sort_sentences(input_ids: List[str], reverse: bool=False): | ||
| """Sort the tokenized sentences by the number of tokens. | ||
|
|
||
| Args: | ||
| input_ids (List[List[int]]): tokenized sentences. | ||
| reverse (bool): indicate the order is ascending(False) or descending. | ||
|
|
||
| Returns: | ||
| tuple(List[List[int]], List[int]): the sorted tokenized sentences and | ||
| the indices in the original input list. | ||
| """ | ||
| is_ascending = -1 if reverse else 1 | ||
| sorted_idx = sorted( | ||
|
Contributor
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. Any perf for large data?
Contributor
Author
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. @yuyan2do Do you mean the benchmarking result on a larger dataset than the data in our benchmark script?
Contributor
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 also have similar worry, as it loaded all data into memory, and then sort. Please check perf on a larger dataset.
Contributor
Author
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. Here is the test results on a larger dataset
|
||
| range(len(input_ids)), key=lambda i: len(input_ids[i])*is_ascending) | ||
| sorted_input_ids = [input_ids[i] for i in sorted_idx] | ||
| return sorted_input_ids, sorted_idx | ||
|
|
||
| def unsort_sentences(sents: List[str], sorted_idx: List[int]): | ||
| """Unsort the sents to be the order specified by sorted_idx. | ||
|
|
||
| Args: | ||
| sents (List[str]): a list of input strings. | ||
| sorted_idx (List[int]): the order that will be restored. | ||
|
|
||
| Returns: | ||
| List[str]: the unsorted list of strings. | ||
| """ | ||
| result = [''] * len(sents) | ||
| for cur_idx, org_idx in enumerate(sorted_idx): | ||
| result[org_idx] = sents[cur_idx] | ||
| return result | ||
|
JiushengChen marked this conversation as resolved.
|
||
|
|
||
| def generate_summaries_or_translations( | ||
| examples: list, | ||
|
|
@@ -34,6 +66,8 @@ def generate_summaries_or_translations( | |
| """Run generation""" | ||
| if fastseq_opt: | ||
| import fastseq #pylint: disable=import-outside-toplevel | ||
| examples, sorted_idx = sort_sentences(examples, reverse=True) | ||
|
|
||
| fout = Path(out_file).open("w", encoding="utf-8") | ||
| model_name = str(model_name) | ||
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device) | ||
|
|
@@ -47,6 +81,7 @@ def generate_summaries_or_translations( | |
| # update config with summarization specific params | ||
| use_task_specific_params(model, task) | ||
|
|
||
| hypothesis = [] | ||
| for batch in tqdm(list(chunks(examples, batch_size))): | ||
| if "t5" in model_name: | ||
| batch = [model.config.prefix + text for text in batch] | ||
|
|
@@ -66,9 +101,14 @@ def generate_summaries_or_translations( | |
| dec = tokenizer.batch_decode(summaries, | ||
| skip_special_tokens=True, | ||
| clean_up_tokenization_spaces=False) | ||
| for hypothesis in dec: | ||
| fout.write(hypothesis + "\n") | ||
| fout.flush() | ||
| hypothesis.extend(dec) | ||
|
|
||
| if fastseq_opt: | ||
| hypothesis = unsort_sentences(hypothesis, sorted_idx) | ||
|
|
||
| for hypo in hypothesis: | ||
| fout.write(hypo + "\n") | ||
| fout.flush() | ||
|
|
||
|
|
||
| def run_generate(): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.