-
Notifications
You must be signed in to change notification settings - Fork 34.2k
[ported model] FSMT (FairSeq MachineTranslation) #6940
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
Merged
Merged
Changes from all commits
Commits
Show all changes
118 commits
Select commit
Hold shift + click to select a range
6ea2650
ready for PR
stas00 825f71e
cleanup
stas00 f05b7c4
correct FSMT_PRETRAINED_MODEL_ARCHIVE_LIST
stas00 08ffb0c
fix
stas00 aab6348
perfectionism
stas00 de7fdd3
revert change from another PR
stas00 2bd939d
odd, already committed this one
stas00 6db7364
non-interactive upload workaround
stas00 1e62879
backup the failed experiment
stas00 7fd4e9e
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 7918e27
store langs in config
stas00 d17bf3d
workaround for localizing model path
stas00 1247623
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 9e15bde
doc clean up as in https://github.com/huggingface/transformers/pull/6956
stas00 c8b16ba
style
stas00 a95e04a
back out debug mode
stas00 e126bdf
document: run_eval.py --num_beams 10
stas00 352f676
remove unneeded constant
stas00 a2a2cca
typo
stas00 0c23e74
re-use bart's Attention
stas00 ae8d10f
re-use EncoderLayer, DecoderLayer from bart
stas00 2795b06
refactor
stas00 5931fe3
send to cuda and fp16
stas00 b027778
cleanup
stas00 c35ef1c
revert (moved to another PR)
stas00 f198df0
better error message
stas00 239a8f1
document run_eval --num_beams
stas00 fbdb96c
solve the problem of tokenizer finding the right files when model is …
stas00 6537979
polish, remove hardcoded config
stas00 8198911
add a note that the file is autogenerated to avoid losing changes
stas00 0efea0f
prep for org change, remove unneeded code
stas00 3ce156c
switch to model4.pt, update scores
stas00 9aae16b
s/python/bash/
stas00 bdc88f0
missing init (but doesn't impact the finetuned model)
stas00 38cc9c1
cleanup
stas00 226dad1
major refactor (reuse-bart)
stas00 25f5392
new model, new expected weights
stas00 486e067
cleanup
stas00 d60a183
cleanup
stas00 a49409b
full link
stas00 86ff534
fix model type
stas00 46ac8f7
merge porting notes
stas00 9e46af1
style
stas00 e2de220
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 64a15ef
cleanup
stas00 5300abd
have to create a DecoderConfig object to handle vocab_size properly
stas00 ef187b3
doc fix
stas00 3f9c449
add note (not a public class)
stas00 2fb447f
parametrize
stas00 fdc24f6
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 fea70dd
- add bleu scores integration tests
stas00 b934c07
skip test if sacrebleu is not installed
stas00 8afc0c5
cache heavy models/tokenizers
stas00 f734eff
some tweaks
stas00 753c770
remove tokens that aren't used
stas00 07e9865
more purging
stas00 bb3f1a8
simplify code
stas00 2c5f13e
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 7ddf3ad
switch to using decoder_start_token_id
stas00 600f056
add doc
stas00 6a05523
Revert "major refactor (reuse-bart)"
stas00 528d73a
decouple from bart
stas00 416fccf
remove unused code #1
stas00 05f09bb
remove unused code #2
stas00 07c0e66
remove unused code #3
stas00 ab78042
update instructions
stas00 9765274
clean up
stas00 661b7fd
move bleu eval to examples
stas00 000a36b
check import only once
stas00 3a69ca5
move data+gen script into files
stas00 08bbda6
reuse via import
stas00 97975f5
take less space
stas00 df2d808
add prepare_seq2seq_batch (auto-tested)
stas00 4347db6
cleanup
stas00 e82832c
recode test to use json instead of yaml
stas00 15ebddc
ignore keys not needed
stas00 014aa1d
use the new -y in transformers-cli upload -y
stas00 bdacd63
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 81c8dc5
[xlm tok] config dict: fix str into int to match definition (#7034)
stas00 cb9d911
[s2s] --eval_max_generate_length (#7018)
sshleifer 4d69131
Fix CI with change of name of nlp (#7054)
sgugger 29bae54
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 5276f96
extending to support allen_nlp wmt models
stas00 463651e
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 e17a2f1
sync with changes
stas00 2babdf8
s/fsmt-wmt/wmt/ in model names
stas00 adc8084
s/fsmt-wmt/wmt/ in model names (p2)
stas00 3bc8115
s/fsmt-wmt/wmt/ in model names (p3)
stas00 101b544
switch to a better checkpoint
stas00 756bff6
Merge remote-tracking branch 'origin/master' into fair-wmt-clean
stas00 37917b0
typo
stas00 12ccdbb
make non-optional args such - adjust tests where possible or skip whe…
stas00 2b3de7a
consistency
stas00 aeca7c6
style
stas00 4587b6a
adjust header
stas00 1b70a5b
cards moved (model rename)
stas00 062cfe0
use best custom hparams
stas00 5963a35
update info
stas00 a6bcd94
remove old cards
stas00 139edb1
cleanup
stas00 a0dda2d
s/stas/facebook/
stas00 7d3058e
update scores
stas00 a5fb882
s/allen_nlp/allenai/
stas00 7f36737
url maps aren't needed
stas00 f894fb9
typo
stas00 99483e0
move all the doc / build /eval generators to their own scripts
stas00 f9f4f83
cleanup
stas00 361299b
Apply suggestions from code review
stas00 2f3da54
Apply suggestions from code review
stas00 d8591d8
fix indent
stas00 78f81b2
duplicated line
stas00 dbfa7c6
style
stas00 a5185ce
use the correct add_start_docstrings
stas00 a3eb3b4
oops
stas00 9e13d10
resizing can't be done with the core approach, due to 2 dicts
stas00 cd0e95e
check that the arg is a list
stas00 5b986cf
style
stas00 1be40e3
style
stas00 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
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| FSMT | ||
| ---------------------------------------------------- | ||
| **DISCLAIMER:** If you see something strange, | ||
| file a `Github Issue <https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title>`__ and assign | ||
| @stas00. | ||
|
|
||
| Overview | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| FSMT (FairSeq MachineTranslation) models were introduced in "Facebook FAIR's WMT19 News Translation Task Submission" <this paper <https://arxiv.org/abs/1907.06616>__ by Nathan Ng, Kyra Yee, Alexei Baevski, Myle Ott, Michael Auli, Sergey Edunov. | ||
|
|
||
| The abstract of the paper is the following: | ||
|
|
||
| This paper describes Facebook FAIR's submission to the WMT19 shared news translation task. We participate in two language pairs and four language directions, English <-> German and English <-> Russian. Following our submission from last year, our baseline systems are large BPE-based transformer models trained with the Fairseq sequence modeling toolkit which rely on sampled back-translations. This year we experiment with different bitext data filtering schemes, as well as with adding filtered back-translated data. We also ensemble and fine-tune our models on domain-specific data, then decode using noisy channel model reranking. Our submissions are ranked first in all four directions of the human evaluation campaign. On En->De, our system significantly outperforms other systems as well as human translations. This system improves upon our WMT'18 submission by 4.5 BLEU points. | ||
|
|
||
| The original code can be found here <https://github.com/pytorch/fairseq/tree/master/examples/wmt19>__. | ||
|
|
||
| Implementation Notes | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| - FSMT uses source and target vocab pair, that aren't combined into one. It doesn't share embed tokens either. Its tokenizer is very similar to `XLMTokenizer` and the main model is derived from `BartModel`. | ||
|
|
||
|
|
||
| FSMTForConditionalGeneration | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| .. autoclass:: transformers.FSMTForConditionalGeneration | ||
| :members: forward | ||
|
|
||
|
|
||
| FSMTConfig | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| .. autoclass:: transformers.FSMTConfig | ||
| :members: | ||
|
|
||
|
|
||
| FSMTTokenizer | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| .. autoclass:: transformers.FSMTTokenizer | ||
| :members: | ||
|
|
||
|
|
||
| FSMTModel | ||
| ~~~~~~~~~~~~~ | ||
|
|
||
| .. autoclass:: transformers.FSMTModel | ||
| :members: forward |
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| import io | ||
| import json | ||
| import subprocess | ||
|
|
||
|
|
||
| pairs = [ | ||
| ["en", "ru"], | ||
| ["ru", "en"], | ||
| ["en", "de"], | ||
| ["de", "en"], | ||
| ] | ||
|
|
||
| n_objs = 8 | ||
|
|
||
|
|
||
| def get_all_data(pairs, n_objs): | ||
| text = {} | ||
| for src, tgt in pairs: | ||
| pair = f"{src}-{tgt}" | ||
| cmd = f"sacrebleu -t wmt19 -l {pair} --echo src".split() | ||
| src_lines = subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode("utf-8").splitlines() | ||
| cmd = f"sacrebleu -t wmt19 -l {pair} --echo ref".split() | ||
| tgt_lines = subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode("utf-8").splitlines() | ||
| text[pair] = {"src": src_lines[:n_objs], "tgt": tgt_lines[:n_objs]} | ||
| return text | ||
|
|
||
|
|
||
| text = get_all_data(pairs, n_objs) | ||
| filename = "./fsmt_val_data.json" | ||
| with io.open(filename, "w", encoding="utf-8") as f: | ||
| bleu_data = json.dump(text, f, indent=2, ensure_ascii=False) |
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| { | ||
| "en-ru": { | ||
| "src": [ | ||
| "Welsh AMs worried about 'looking like muppets'", | ||
| "There is consternation among some AMs at a suggestion their title should change to MWPs (Member of the Welsh Parliament).", | ||
| "It has arisen because of plans to change the name of the assembly to the Welsh Parliament.", | ||
| "AMs across the political spectrum are worried it could invite ridicule.", | ||
| "One Labour AM said his group was concerned \"it rhymes with Twp and Pwp.\"", | ||
| "For readers outside of Wales: In Welsh twp means daft and pwp means poo.", | ||
| "A Plaid AM said the group as a whole was \"not happy\" and has suggested alternatives.", | ||
| "A Welsh Conservative said his group was \"open minded\" about the name change, but noted it was a short verbal hop from MWP to Muppet." | ||
| ], | ||
| "tgt": [ | ||
| "Члены Национальной ассамблеи Уэльса обеспокоены, что \"выглядят как куклы\"", | ||
| "Некоторые члены Национальной ассамблеи Уэльса в ужасе от предложения о том, что их наименование должно измениться на MPW (члены Парламента Уэльса).", | ||
| "Этот вопрос был поднят в связи с планами по переименованию ассамблеи в Парламент Уэльса.", | ||
| "Члены Национальной ассамблеи Уэльса всего политического спектра обеспокоены, что это может породить насмешки.", | ||
| "Один из лейбористских членов Национальной ассамблеи Уэльса сказал, что его партия обеспокоена тем, что \"это рифмуется с Twp и Pwp\".", | ||
| "Для читателей за предлами Уэльса: по-валлийски twp означает \"глупый\", а pwp означает \"какашка\".", | ||
| "Член Национальной ассамблеи от Плайд сказал, что эта партия в целом \"не счастлива\" и предложил альтернативы.", | ||
| "Представитель Консервативной партии Уэльса сказал, что его партия \"открыта\" к переименованию, но отметил, что между WMP и Muppet небольшая разница в произношении." | ||
| ] | ||
| }, | ||
| "ru-en": { | ||
| "src": [ | ||
| "Названо число готовящихся к отправке в Донбасс новобранцев из Украины", | ||
| "Официальный представитель Народной милиции самопровозглашенной Луганской Народной Республики (ЛНР) Андрей Марочко заявил, что зимой 2018-2019 года Украина направит в Донбасс не менее 3 тыс. новобранцев.", | ||
| "По его словам, таким образом Киев планирует \"хоть как-то доукомплектовать подразделения\".", | ||
| "\"Нежелание граждан Украины проходить службу в рядах ВС Украины, массовые увольнения привели к низкой укомплектованности подразделений\", - рассказал Марочко, которого цитирует \"РИА Новости\".", | ||
| "Он также не исключил, что реальные цифры призванных в армию украинцев могут быть увеличены в случае необходимости.", | ||
| "В 2014-2017 годах Киев начал так называемую антитеррористическую операцию (АТО), которую позже сменили на операцию объединенных сил (ООС).", | ||
| "Предполагалось, что эта мера приведет к усилению роли украинских силовиков в урегулировании ситуации.", | ||
| "В конце августа 2018 года ситуация в Донбассе обострилась из-за убийства главы ДНР Александра Захарченко." | ||
| ], | ||
| "tgt": [ | ||
| "The number of new Ukrainian recruits ready to go to Donbass has become public", | ||
| "Official representative of the peoples’ militia of the self-proclaimed Lugansk People’s Republic Andrey Marochko claimed that Ukrainian will send at least 3 thousand new recruits to Donbass in winter 2018-2019.", | ||
| "This is how Kyiv tries “at least somehow to staff the units,” he said.", | ||
| "“The unwillingness of Ukrainian citizens to serve in the Ukraine’s military forces, mass resignments lead to low understaffing,” said Marochko cited by RIA Novosti.", | ||
| "Also, he doesn’t exclude that the real numbers of conscripts in the Ukrainian army can be raised is necessary.", | ||
| "In 2014-2017, Kyiv started so-called antiterrorist operation, that ws later changed to the united forces operation.", | ||
| "This measure was supposed to strengthen the role of the Ukrainian military in settling the situation.", | ||
| "In the late August 2018, the situation in Donbass escalated as the DNR head Aleksandr Zakharchenko was killed." | ||
| ] | ||
| }, | ||
| "en-de": { | ||
| "src": [ | ||
| "Welsh AMs worried about 'looking like muppets'", | ||
| "There is consternation among some AMs at a suggestion their title should change to MWPs (Member of the Welsh Parliament).", | ||
| "It has arisen because of plans to change the name of the assembly to the Welsh Parliament.", | ||
| "AMs across the political spectrum are worried it could invite ridicule.", | ||
| "One Labour AM said his group was concerned \"it rhymes with Twp and Pwp.\"", | ||
| "For readers outside of Wales: In Welsh twp means daft and pwp means poo.", | ||
| "A Plaid AM said the group as a whole was \"not happy\" and has suggested alternatives.", | ||
| "A Welsh Conservative said his group was \"open minded\" about the name change, but noted it was a short verbal hop from MWP to Muppet." | ||
| ], | ||
| "tgt": [ | ||
| "Walisische Ageordnete sorgen sich \"wie Dödel auszusehen\"", | ||
| "Es herrscht Bestürzung unter einigen Mitgliedern der Versammlung über einen Vorschlag, der ihren Titel zu MWPs (Mitglied der walisischen Parlament) ändern soll.", | ||
| "Der Grund dafür waren Pläne, den Namen der Nationalversammlung in Walisisches Parlament zu ändern.", | ||
| "Mitglieder aller Parteien der Nationalversammlung haben Bedenken, dass sie sich dadurch Spott aussetzen könnten.", | ||
| "Ein Labour-Abgeordneter sagte, dass seine Gruppe \"sich mit Twp und Pwp reimt\".", | ||
| "Hinweis für den Leser: „twp“ im Walisischen bedeutet „bescheuert“ und „pwp“ bedeutet „Kacke“.", | ||
| "Ein Versammlungsmitglied von Plaid Cymru sagte, die Gruppe als Ganzes sei \"nicht glücklich\" und hat Alternativen vorgeschlagen.", | ||
| "Ein walisischer Konservativer sagte, seine Gruppe wäre „offen“ für eine Namensänderung, wies aber darauf hin, dass es von „MWP“ (Mitglied des Walisischen Parlaments) nur ein kurzer verbaler Sprung zu „Muppet“ ist." | ||
| ] | ||
| }, | ||
| "de-en": { | ||
| "src": [ | ||
| "Schöne Münchnerin 2018: Schöne Münchnerin 2018 in Hvar: Neun Dates", | ||
| "Von az, aktualisiert am 04.05.2018 um 11:11", | ||
| "Ja, sie will...", | ||
| "\"Schöne Münchnerin\" 2018 werden!", | ||
| "Am Nachmittag wartet erneut eine Überraschung auf unsere Kandidatinnen: sie werden das romantische Candlelight-Shooting vor der MY SOLARIS nicht alleine bestreiten, sondern an der Seite von Male-Model Fabian!", | ||
| "Hvar - Flirten, kokettieren, verführen - keine einfachen Aufgaben für unsere Mädchen.", | ||
| "Insbesondere dann, wenn in Deutschland ein Freund wartet.", | ||
| "Dennoch liefern die neun \"Schöne Münchnerin\"-Kandidatinnen beim Shooting mit People-Fotograf Tuan ab und trotzen Wind, Gischt und Regen wie echte Profis." | ||
| ], | ||
| "tgt": [ | ||
| "The Beauty of Munich 2018: the Beauty of Munich 2018 in Hvar: Nine dates", | ||
| "From A-Z, updated on 04/05/2018 at 11:11", | ||
| "Yes, she wants to...", | ||
| "to become \"The Beauty of Munich\" in 2018!", | ||
| "In the afternoon there is another surprise waiting for our contestants: they will be competing for the romantic candlelight photo shoot at MY SOLARIS not alone, but together with a male-model Fabian!", | ||
| "Hvar with its flirting, coquetting, and seduction is not an easy task for our girls.", | ||
| "Especially when there is a boyfriend waiting in Germany.", | ||
| "Despite dealing with wind, sprays and rain, the nine contestants of \"The Beauty of Munich\" behaved like real professionals at the photo shoot with People-photographer Tuan." | ||
| ] | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # coding=utf-8 | ||
| # Copyright 2020 Huggingface | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import io | ||
| import unittest | ||
|
|
||
|
|
||
| try: | ||
| from .utils import calculate_bleu | ||
| except ImportError: | ||
| from utils import calculate_bleu | ||
|
|
||
| import json | ||
|
|
||
| from parameterized import parameterized | ||
| from transformers import FSMTForConditionalGeneration, FSMTTokenizer | ||
| from transformers.testing_utils import get_tests_dir, require_torch, slow, torch_device | ||
|
|
||
|
|
||
| filename = get_tests_dir() + "/test_data/fsmt/fsmt_val_data.json" | ||
| with io.open(filename, "r", encoding="utf-8") as f: | ||
| bleu_data = json.load(f) | ||
|
|
||
|
|
||
| @require_torch | ||
| class ModelEvalTester(unittest.TestCase): | ||
| def get_tokenizer(self, mname): | ||
| return FSMTTokenizer.from_pretrained(mname) | ||
|
|
||
| def get_model(self, mname): | ||
| model = FSMTForConditionalGeneration.from_pretrained(mname).to(torch_device) | ||
| if torch_device == "cuda": | ||
| model.half() | ||
| return model | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ["en-ru", 26.0], | ||
| ["ru-en", 22.0], | ||
| ["en-de", 22.0], | ||
| ["de-en", 29.0], | ||
| ] | ||
| ) | ||
| @slow | ||
| def test_bleu_scores(self, pair, min_bleu_score): | ||
| # note: this test is not testing the best performance since it only evals a small batch | ||
| # but it should be enough to detect a regression in the output quality | ||
| mname = f"facebook/wmt19-{pair}" | ||
| tokenizer = self.get_tokenizer(mname) | ||
| model = self.get_model(mname) | ||
|
|
||
| src_sentences = bleu_data[pair]["src"] | ||
| tgt_sentences = bleu_data[pair]["tgt"] | ||
|
|
||
| batch = tokenizer(src_sentences, return_tensors="pt", truncation=True, padding="longest").to(torch_device) | ||
| outputs = model.generate( | ||
| input_ids=batch.input_ids, | ||
| num_beams=8, | ||
| ) | ||
| decoded_sentences = tokenizer.batch_decode( | ||
| outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False | ||
| ) | ||
| scores = calculate_bleu(decoded_sentences, tgt_sentences) | ||
| print(scores) | ||
| self.assertGreaterEqual(scores["bleu"], min_bleu_score) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.