-
Notifications
You must be signed in to change notification settings - Fork 31
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 mT5 #98
Merged
Merged
Add mT5 #98
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
90850b3
add mt5 model
e594938
reformatting
30cfff2
merge with main
1415080
add rest of mt5 languages to dict
7e6b81c
use download caching
d751db8
reformatting
897ca45
start on readme edits
bf35ca3
finish first draft of multilingual model documentation
68a8635
reformatting
5528e40
Merge branch 'main' into nick/mT5
haileyschoelkopf 51be8db
Merge branch 'main' into nick/mT5
92ad57f
[skip-ci] fix readme typo
ce859d6
fix mex additional merge conflict
32a9042
fix changes for merge
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 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
from .textrank_model import TextRankModel | ||
|
||
from .multilingual import MBartModel | ||
from .multilingual import MT5Model |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from .mbart_model import MBartModel | ||
from .mt5_model import MT5Model |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -112,9 +112,10 @@ def show_capability(cls) -> None: | |
"Introduced in 2020, a multilingual variant of BART (a large neural model) " | ||
"trained on web crawl data.\n" | ||
"Strengths: \n - Multilinguality: supports 50 different languages\n" | ||
" - Higher max input length than mT5 (1024)" | ||
"Weaknesses: \n - High memory usage" | ||
"Initialization arguments: \n " | ||
"- `device = 'cpu'` specifies the device the model is stored on and uses for computation. " | ||
"Use `device='gpu'` to run on an Nvidia GPU." | ||
"Use `device='cuda'` to run on an Nvidia GPU." | ||
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. Oh, no. I think this typo is actually common across all our models... Good catch! But do you mind fixing the others as well? 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. will fix! |
||
) | ||
print(f"{basic_description} \n {'#'*20} \n {more_details}") |
This file contains 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 |
---|---|---|
@@ -0,0 +1,176 @@ | ||
from transformers import MT5ForConditionalGeneration, MT5Tokenizer | ||
from .base_multilingual_model import MultilingualSummModel | ||
|
||
|
||
class MT5Model(MultilingualSummModel): | ||
""" | ||
MT5 Model for Multilingual Summarization | ||
""" | ||
|
||
# static variables | ||
model_name = "mT5" | ||
is_extractive = False | ||
is_neural = True | ||
is_multilingual = True | ||
|
||
supported_langs = [ | ||
"am", | ||
"ar", | ||
"az", | ||
"bn", | ||
"my", | ||
"zh-CN", | ||
"zh-TW", | ||
"en", | ||
"fr", | ||
"gu", | ||
"ha", | ||
"hi", | ||
"ig", | ||
"id", | ||
"ja", | ||
"rn", | ||
"ko", | ||
"ky", | ||
"mr", | ||
"np", | ||
"om", | ||
"ps", | ||
"fa", | ||
"pt", # missing pidgin from XLSum--does not have ISO 639-1 code | ||
"pa", | ||
"ru", | ||
"gd", | ||
"sr", | ||
"si", | ||
"so", | ||
"es", | ||
"sw", | ||
"ta", | ||
"te", | ||
"th", | ||
"ti", | ||
"tr", | ||
"uk", | ||
"ur", | ||
"uz", | ||
"vi", | ||
"cy", | ||
"yo", # <- up to here: langs included in XLSum | ||
"af", | ||
"sq", | ||
"hy", | ||
"eu", | ||
"be", | ||
"bg", | ||
"ca", | ||
# cebuano has no ISO-639-1 code | ||
"ceb", | ||
"ny", | ||
"co", | ||
"cs", | ||
"da", | ||
"nl", | ||
"eo", | ||
"et", | ||
"tl", # tagalog in place of filipino | ||
"fi", | ||
"gl", | ||
"ka", | ||
"de", | ||
"el", | ||
"ht", | ||
"haw", # hawaiian 639-3 code (not in fasttext id) | ||
"he", | ||
"hmn", # hmong 639-3 code (not in fasttext id) | ||
"hu", | ||
"is", | ||
"ga", | ||
"it", | ||
"jv", | ||
"kn", | ||
"kk", | ||
"km", | ||
"ku", | ||
"lo", | ||
"la", | ||
"lv", | ||
"lt", | ||
"lb", | ||
"mk", | ||
"mg", | ||
"ms", | ||
"ml", | ||
"mt", | ||
"mi", | ||
"mn", | ||
"ne", | ||
"no", | ||
"pl", | ||
"ro", | ||
"sm", | ||
"sn", | ||
"sd", | ||
"sk", | ||
"sl", | ||
"st", | ||
"su", | ||
"sv", | ||
"tg", | ||
"fy", | ||
"xh", | ||
"yi", | ||
"zu", | ||
] | ||
|
||
lang_tag_dict = {lang: lang for lang in supported_langs} | ||
|
||
def __init__(self, device="cpu"): | ||
|
||
super(MT5Model, self).__init__( | ||
trained_domain="News", | ||
max_input_length=512, | ||
max_output_length=None, | ||
) | ||
|
||
self.device = device | ||
|
||
model_name = "csebuetnlp/mT5_multilingual_XLSum" | ||
self.tokenizer = MT5Tokenizer.from_pretrained(model_name) | ||
self.model = MT5ForConditionalGeneration.from_pretrained(model_name).to(device) | ||
|
||
def summarize(self, corpus, queries=None): | ||
self.assert_summ_input_type(corpus, queries) | ||
|
||
with self.tokenizer.as_target_tokenizer(): | ||
batch = self.tokenizer( | ||
corpus, | ||
truncation=True, | ||
padding="longest", | ||
max_length=self.max_input_length, | ||
return_tensors="pt", | ||
).to(self.device) | ||
|
||
encoded_summaries = self.model.generate( | ||
**batch, num_beams=4, length_penalty=1.0, early_stopping=True | ||
) | ||
|
||
summaries = self.tokenizer.batch_decode( | ||
encoded_summaries, skip_special_tokens=True | ||
) | ||
|
||
return summaries | ||
|
||
@classmethod | ||
def show_capability(cls) -> None: | ||
basic_description = cls.generate_basic_description() | ||
more_details = ( | ||
"Introduced in ____, a massively multilingual variant of Google's T5, a large neural model. " | ||
"Trained on web crawled data and fine-tuned on XLSum, a 45-language multilingual news dataset.\n" | ||
"Strengths: \n - Massively multilingual: supports 101 different languages\n" | ||
"Weaknesses: \n - High memory usage\n - Lower max input length (512)" | ||
"Initialization arguments: \n " | ||
"- `device = 'cpu'` specifies the device the model is stored on and uses for computation. " | ||
"Use `device='cuda'` to run on an Nvidia GPU." | ||
) | ||
print(f"{basic_description} \n {'#'*20} \n {more_details}") |
This file contains 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.
should this be in the
base_model.py
or themultingual_model
?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.
Oh, I see that you are adding the function of returning "english" for non-multilingual models. Okay, then this is good.