Compare commits
58
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8100a7fb2 | ||
|
|
e0e0ea4e80 | ||
|
|
03ec02a667 | ||
|
|
827c519494 | ||
|
|
ba4bbd92bc | ||
|
|
26d5475d4b | ||
|
|
c6e18de9f8 | ||
|
|
2c9e83f7b8 | ||
|
|
9618cd6964 | ||
|
|
4dcc424de3 | ||
|
|
a3cea6a8cc | ||
|
|
0af53b1ef9 | ||
|
|
b0f05e0c4c | ||
|
|
bc00b37a0d | ||
|
|
76e05518bb | ||
|
|
9ad830596d | ||
|
|
a1ac082879 | ||
|
|
21ed3a6b99 | ||
|
|
5668fdb09e | ||
|
|
0578a91300 | ||
|
|
297233fa92 | ||
|
|
a1ecc90d6b | ||
|
|
06a973fd2a | ||
|
|
4a00613c24 | ||
|
|
55cb2ee62e | ||
|
|
9aeacb58ba | ||
|
|
4d04120c6d | ||
|
|
aba4e22944 | ||
|
|
e3e6517355 | ||
|
|
960faaaf28 | ||
|
|
aee7967fc4 | ||
|
|
b1c06140f4 | ||
|
|
e10d389561 | ||
|
|
167bce56f2 | ||
|
|
923dd4e5ef | ||
|
|
85ead0fec4 | ||
|
|
c6b9c72eac | ||
|
|
048b4bd2c6 | ||
|
|
c2e0d8ac52 | ||
|
|
e2bb9abb6a | ||
|
|
08ba4b4902 | ||
|
|
8fa0c956b3 | ||
|
|
e084089eb9 | ||
|
|
adfe6ace88 | ||
|
|
f0d20ad328 | ||
|
|
5982431814 | ||
|
|
500be01c5d | ||
|
|
2b574e7c60 | ||
|
|
aa6c3c14b4 | ||
|
|
98fb718577 | ||
|
|
4d541f516f | ||
|
|
8d2c248df7 | ||
|
|
1c80b2c604 | ||
|
|
eda27f4494 | ||
|
|
0257992e4a | ||
|
|
66c72082d0 | ||
|
|
b21a30bdd8 | ||
|
|
d5d2744aa7 |
No files matched your search
@@ -30,7 +30,8 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
pip install --upgrade pip
|
pip install --upgrade pip
|
||||||
pip install torch
|
pip install torch
|
||||||
pip install numpy tokenizers filelock requests tqdm regex sentencepiece sacremoses packaging
|
pip install numpy filelock protobuf requests tqdm regex sentencepiece sacremoses packaging
|
||||||
|
pip install tokenizers==0.9.0.rc2
|
||||||
|
|
||||||
- name: Torch hub list
|
- name: Torch hub list
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
@@ -6,17 +6,17 @@ check_dirs := examples templates tests src utils
|
|||||||
# get modified files since the branch was made
|
# get modified files since the branch was made
|
||||||
fork_point_sha := $(shell git merge-base --fork-point master)
|
fork_point_sha := $(shell git merge-base --fork-point master)
|
||||||
joined_dirs := $(shell echo $(check_dirs) | tr " " "|")
|
joined_dirs := $(shell echo $(check_dirs) | tr " " "|")
|
||||||
modified_files := $(shell git diff --name-only $(fork_point_sha) | egrep '^($(joined_dirs))')
|
modified_py_files := $(shell git diff --name-only $(fork_point_sha) | egrep '^($(joined_dirs))' | egrep '\.py$$')
|
||||||
#$(info modified files are: $(modified_files))
|
#$(info modified files are: $(modified_py_files))
|
||||||
|
|
||||||
modified_only_fixup:
|
modified_only_fixup:
|
||||||
@if [ -n "$(modified_files)" ]; then \
|
@if [ -n "$(modified_py_files)" ]; then \
|
||||||
echo "Checking/fixing $(modified_files)"; \
|
echo "Checking/fixing $(modified_py_files)"; \
|
||||||
black $(modified_files); \
|
black $(modified_py_files); \
|
||||||
isort $(modified_files); \
|
isort $(modified_py_files); \
|
||||||
flake8 $(modified_files); \
|
flake8 $(modified_py_files); \
|
||||||
else \
|
else \
|
||||||
echo "No relevant files were modified"; \
|
echo "No library .py files were modified"; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check that source code meets quality standards
|
# Check that source code meets quality standards
|
||||||
|
|||||||
@@ -161,31 +161,31 @@ If you'd like to play with the examples, you must [install the library from sour
|
|||||||
1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
|
1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
|
||||||
1. **[BART](https://huggingface.co/transformers/model_doc/bart.html)** (from Facebook) released with the paper [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://arxiv.org/pdf/1910.13461.pdf) by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer.
|
1. **[BART](https://huggingface.co/transformers/model_doc/bart.html)** (from Facebook) released with the paper [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://arxiv.org/pdf/1910.13461.pdf) by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer.
|
||||||
1. **[BERT](https://huggingface.co/transformers/model_doc/bert.html)** (from Google) released with the paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805) by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
|
1. **[BERT](https://huggingface.co/transformers/model_doc/bert.html)** (from Google) released with the paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805) by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
|
||||||
1. **[BERT For Sequence Generation](https://tfhub.dev/s?module-type=text-generation&subtype=module,placeholder)** (from Google) released with the paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://arxiv.org/abs/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.
|
1. **[BERT For Sequence Generation](https://huggingface.co/transformers/model_doc/bertgeneration.html)** (from Google) released with the paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://arxiv.org/abs/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.
|
||||||
|
1. **[Blenderbot](https://huggingface.co/transformers/master/model_doc/blenderbot.html)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://arxiv.org/abs/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston.
|
||||||
1. **[CamemBERT](https://huggingface.co/transformers/model_doc/camembert.html)** (from Inria/Facebook/Sorbonne) released with the paper [CamemBERT: a Tasty French Language Model](https://arxiv.org/abs/1911.03894) by Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot.
|
1. **[CamemBERT](https://huggingface.co/transformers/model_doc/camembert.html)** (from Inria/Facebook/Sorbonne) released with the paper [CamemBERT: a Tasty French Language Model](https://arxiv.org/abs/1911.03894) by Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot.
|
||||||
1. **[CTRL](https://huggingface.co/transformers/model_doc/ctrl.html)** (from Salesforce) released with the paper [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://arxiv.org/abs/1909.05858) by Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher.
|
1. **[CTRL](https://huggingface.co/transformers/model_doc/ctrl.html)** (from Salesforce) released with the paper [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://arxiv.org/abs/1909.05858) by Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher.
|
||||||
1. **[DeBERTa](https://huggingface.co/transformers/model_doc/deberta.html)** (from Microsoft Research) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://arxiv.org/abs/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
|
1. **[DeBERTa](https://huggingface.co/transformers/master/model_doc/deberta.html)** (from Microsoft Research) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://arxiv.org/abs/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
|
||||||
1. **[DialoGPT](https://huggingface.co/transformers/model_doc/dialogpt.html)** (from Microsoft Research) released with the paper [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://arxiv.org/abs/1911.00536) by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan.
|
1. **[DialoGPT](https://huggingface.co/transformers/model_doc/dialogpt.html)** (from Microsoft Research) released with the paper [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://arxiv.org/abs/1911.00536) by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan.
|
||||||
1. **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace), released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into [DistilGPT2](https://github.com/huggingface/transformers/tree/master/examples/distillation), RoBERTa into [DistilRoBERTa](https://github.com/huggingface/transformers/tree/master/examples/distillation), Multilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers/tree/master/examples/distillation) and a German version of DistilBERT.
|
1. **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace), released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into [DistilGPT2](https://github.com/huggingface/transformers/tree/master/examples/distillation), RoBERTa into [DistilRoBERTa](https://github.com/huggingface/transformers/tree/master/examples/distillation), Multilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers/tree/master/examples/distillation) and a German version of DistilBERT.
|
||||||
1. **[DPR](https://github.com/facebookresearch/DPR)** (from Facebook) released with the paper [Dense Passage Retrieval
|
1. **[DPR](https://huggingface.co/transformers/model_doc/dpr.html)** (from Facebook) released with the paper [Dense Passage Retrieval
|
||||||
for Open-Domain Question Answering](https://arxiv.org/abs/2004.04906) by Vladimir Karpukhin, Barlas Oğuz, Sewon
|
for Open-Domain Question Answering](https://arxiv.org/abs/2004.04906) by Vladimir Karpukhin, Barlas Oğuz, Sewon
|
||||||
Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih.
|
Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih.
|
||||||
1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning.
|
1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning.
|
||||||
1. **[FlauBERT](https://huggingface.co/transformers/model_doc/flaubert.html)** (from CNRS) released with the paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://arxiv.org/abs/1912.05372) by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé, Laurent Besacier, Didier Schwab.
|
1. **[FlauBERT](https://huggingface.co/transformers/model_doc/flaubert.html)** (from CNRS) released with the paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://arxiv.org/abs/1912.05372) by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé, Laurent Besacier, Didier Schwab.
|
||||||
1. **[Funnel Transformer](https://github.com/laiguokun/Funnel-Transformer)** (from CMU/Google Brain) released with the paper [Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing](https://arxiv.org/abs/2006.03236) by Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le.
|
1. **[Funnel Transformer](https://huggingface.co/transformers/model_doc/funnel.html)** (from CMU/Google Brain) released with the paper [Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing](https://arxiv.org/abs/2006.03236) by Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le.
|
||||||
1. **[GPT](https://huggingface.co/transformers/model_doc/gpt.html)** (from OpenAI) released with the paper [Improving Language Understanding by Generative Pre-Training](https://blog.openai.com/language-unsupervised/) by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.
|
1. **[GPT](https://huggingface.co/transformers/model_doc/gpt.html)** (from OpenAI) released with the paper [Improving Language Understanding by Generative Pre-Training](https://blog.openai.com/language-unsupervised/) by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.
|
||||||
1. **[GPT-2](https://huggingface.co/transformers/model_doc/gpt2.html)** (from OpenAI) released with the paper [Language Models are Unsupervised Multitask Learners](https://blog.openai.com/better-language-models/) by Alec Radford*, Jeffrey Wu*, Rewon Child, David Luan, Dario Amodei** and Ilya Sutskever**.
|
1. **[GPT-2](https://huggingface.co/transformers/model_doc/gpt2.html)** (from OpenAI) released with the paper [Language Models are Unsupervised Multitask Learners](https://blog.openai.com/better-language-models/) by Alec Radford*, Jeffrey Wu*, Rewon Child, David Luan, Dario Amodei** and Ilya Sutskever**.
|
||||||
1. **[LayoutLM](https://github.com/microsoft/unilm/tree/master/layoutlm)** (from Microsoft Research Asia) released with the paper [LayoutLM: Pre-training of Text and Layout for Document Image Understanding](https://arxiv.org/abs/1912.13318) by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou.
|
1. **[LayoutLM](https://huggingface.co/transformers/model_doc/layoutlm.html)** (from Microsoft Research Asia) released with the paper [LayoutLM: Pre-training of Text and Layout for Document Image Understanding](https://arxiv.org/abs/1912.13318) by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou.
|
||||||
1. **[Longformer](https://huggingface.co/transformers/model_doc/longformer.html)** (from AllenAI) released with the paper [Longformer: The Long-Document Transformer](https://arxiv.org/abs/2004.05150) by Iz Beltagy, Matthew E. Peters, Arman Cohan.
|
1. **[Longformer](https://huggingface.co/transformers/model_doc/longformer.html)** (from AllenAI) released with the paper [Longformer: The Long-Document Transformer](https://arxiv.org/abs/2004.05150) by Iz Beltagy, Matthew E. Peters, Arman Cohan.
|
||||||
1. **[LXMERT](https://github.com/airsplay/lxmert)** (from UNC Chapel Hill) released with the paper [LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering](https://arxiv.org/abs/1908.07490) by Hao Tan and Mohit Bansal.
|
1. **[LXMERT](https://huggingface.co/transformers/model_doc/lxmert.html)** (from UNC Chapel Hill) released with the paper [LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering](https://arxiv.org/abs/1908.07490) by Hao Tan and Mohit Bansal.
|
||||||
1. **[MarianMT](https://huggingface.co/transformers/model_doc/marian.html)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jörg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team.
|
1. **[MarianMT](https://huggingface.co/transformers/model_doc/marian.html)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jörg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team.
|
||||||
1. **[MBart](https://github.com/pytorch/fairseq/tree/master/examples/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
1. **[MBart](https://huggingface.co/transformers/model_doc/mbart.html)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
||||||
1. **[MMBT](https://github.com/facebookresearch/mmbt/)** (from Facebook), released together with the paper a [Supervised Multimodal Bitransformers for Classifying Images and Text](https://arxiv.org/pdf/1909.02950.pdf) by Douwe Kiela, Suvrat Bhooshan, Hamed Firooz, Davide Testuggine.
|
1. **[Pegasus](https://huggingface.co/transformers/model_doc/pegasus.html)** (from Google) released with the paper [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://arxiv.org/abs/1912.08777)> by Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu.
|
||||||
1. **[Pegasus](https://github.com/google-research/pegasus)** (from Google) released with the paper [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://arxiv.org/abs/1912.08777)> by Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu.
|
|
||||||
1. **[Reformer](https://huggingface.co/transformers/model_doc/reformer.html)** (from Google Research) released with the paper [Reformer: The Efficient Transformer](https://arxiv.org/abs/2001.04451) by Nikita Kitaev, Łukasz Kaiser, Anselm Levskaya.
|
1. **[Reformer](https://huggingface.co/transformers/model_doc/reformer.html)** (from Google Research) released with the paper [Reformer: The Efficient Transformer](https://arxiv.org/abs/2001.04451) by Nikita Kitaev, Łukasz Kaiser, Anselm Levskaya.
|
||||||
1. **[RoBERTa](https://huggingface.co/transformers/model_doc/roberta.html)** (from Facebook), released together with the paper a [Robustly Optimized BERT Pretraining Approach](https://arxiv.org/abs/1907.11692) by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
|
1. **[RoBERTa](https://huggingface.co/transformers/model_doc/roberta.html)** (from Facebook), released together with the paper a [Robustly Optimized BERT Pretraining Approach](https://arxiv.org/abs/1907.11692) by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
|
||||||
ultilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers/tree/master/examples/distillation) and a German version of DistilBERT.
|
ultilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers/tree/master/examples/distillation) and a German version of DistilBERT.
|
||||||
1. **[SqueezeBert](https://huggingface.co/transformers/model_doc/squeezebert.html)** released with the paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://arxiv.org/abs/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer.
|
1. **[SqueezeBert](https://huggingface.co/transformers/master/model_doc/squeezebert.html)** released with the paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://arxiv.org/abs/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer.
|
||||||
1. **[T5](https://huggingface.co/transformers/model_doc/t5.html)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
|
1. **[T5](https://huggingface.co/transformers/model_doc/t5.html)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
|
||||||
1. **[Transformer-XL](https://huggingface.co/transformers/model_doc/transformerxl.html)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
1. **[Transformer-XL](https://huggingface.co/transformers/model_doc/transformerxl.html)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
||||||
1. **[XLM](https://huggingface.co/transformers/model_doc/xlm.html)** (from Facebook) released together with the paper [Cross-lingual Language Model Pretraining](https://arxiv.org/abs/1901.07291) by Guillaume Lample and Alexis Conneau.
|
1. **[XLM](https://huggingface.co/transformers/model_doc/xlm.html)** (from Facebook) released together with the paper [Cross-lingual Language Model Pretraining](https://arxiv.org/abs/1901.07291) by Guillaume Lample and Alexis Conneau.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM nvidia/cuda:10.1-cudnn7-runtime-ubuntu18.04
|
FROM nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04
|
||||||
LABEL maintainer="Hugging Face"
|
LABEL maintainer="Hugging Face"
|
||||||
LABEL repository="transformers"
|
LABEL repository="transformers"
|
||||||
|
|
||||||
@@ -18,6 +18,11 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
|
|||||||
tensorflow \
|
tensorflow \
|
||||||
torch
|
torch
|
||||||
|
|
||||||
|
RUN git clone https://github.com/NVIDIA/apex
|
||||||
|
RUN cd apex && \
|
||||||
|
python3 setup.py install && \
|
||||||
|
pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./
|
||||||
|
|
||||||
WORKDIR /workspace
|
WORKDIR /workspace
|
||||||
COPY . transformers/
|
COPY . transformers/
|
||||||
RUN cd transformers/ && \
|
RUN cd transformers/ && \
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM nvidia/cuda:10.1-cudnn7-runtime-ubuntu18.04
|
FROM nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04
|
||||||
LABEL maintainer="Hugging Face"
|
LABEL maintainer="Hugging Face"
|
||||||
LABEL repository="transformers"
|
LABEL repository="transformers"
|
||||||
|
|
||||||
@@ -17,6 +17,11 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
|
|||||||
mkl \
|
mkl \
|
||||||
torch
|
torch
|
||||||
|
|
||||||
|
RUN git clone https://github.com/NVIDIA/apex
|
||||||
|
RUN cd apex && \
|
||||||
|
python3 setup.py install && \
|
||||||
|
pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./
|
||||||
|
|
||||||
WORKDIR /workspace
|
WORKDIR /workspace
|
||||||
COPY . transformers/
|
COPY . transformers/
|
||||||
RUN cd transformers/ && \
|
RUN cd transformers/ && \
|
||||||
|
|||||||
+91
-97
@@ -57,112 +57,103 @@ conversion utilities for the following models:
|
|||||||
..
|
..
|
||||||
This list is updated automatically from the README with `make fix-copies`. Do not update manually!
|
This list is updated automatically from the README with `make fix-copies`. Do not update manually!
|
||||||
|
|
||||||
1. `ALBERT <https://huggingface.co/transformers/model_doc/albert.html>`__ (from Google Research and the Toyota
|
1. :doc:`ALBERT <model_doc/albert>` (from Google Research and the Toyota Technological Institute at Chicago) released
|
||||||
Technological Institute at Chicago) released with the paper `ALBERT: A Lite BERT for Self-supervised Learning of
|
with the paper `ALBERT: A Lite BERT for Self-supervised Learning of Language Representations
|
||||||
Language Representations <https://arxiv.org/abs/1909.11942>`__, by Zhenzhong Lan, Mingda Chen, Sebastian Goodman,
|
<https://arxiv.org/abs/1909.11942>`__, by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush
|
||||||
Kevin Gimpel, Piyush Sharma, Radu Soricut.
|
Sharma, Radu Soricut.
|
||||||
2. `BART <https://huggingface.co/transformers/model_doc/bart.html>`__ (from Facebook) released with the paper `BART:
|
2. :doc:`BART <model_doc/bart>` (from Facebook) released with the paper `BART: Denoising Sequence-to-Sequence
|
||||||
Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension
|
Pre-training for Natural Language Generation, Translation, and Comprehension
|
||||||
<https://arxiv.org/pdf/1910.13461.pdf>`__ by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman
|
<https://arxiv.org/pdf/1910.13461.pdf>`__ by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman
|
||||||
Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer.
|
Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer.
|
||||||
3. `BERT <https://huggingface.co/transformers/model_doc/bert.html>`__ (from Google) released with the paper `BERT:
|
3. :doc:`BERT <model_doc/bert>` (from Google) released with the paper `BERT: Pre-training of Deep Bidirectional
|
||||||
Pre-training of Deep Bidirectional Transformers for Language Understanding <https://arxiv.org/abs/1810.04805>`__ by
|
Transformers for Language Understanding <https://arxiv.org/abs/1810.04805>`__ by Jacob Devlin, Ming-Wei Chang,
|
||||||
Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
|
Kenton Lee and Kristina Toutanova.
|
||||||
4. `BERT For Sequence Generation <https://tfhub.dev/s?module-type=text-generation&subtype=module,placeholder>`__ (from
|
4. :doc:`BERT For Sequence Generation <model_doc/bertgeneration>` (from Google) released with the paper `Leveraging
|
||||||
Google) released with the paper `Leveraging Pre-trained Checkpoints for Sequence Generation Tasks
|
Pre-trained Checkpoints for Sequence Generation Tasks <https://arxiv.org/abs/1907.12461>`__ by Sascha Rothe, Shashi
|
||||||
<https://arxiv.org/abs/1907.12461>`__ by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.
|
Narayan, Aliaksei Severyn.
|
||||||
5. `CamemBERT <https://huggingface.co/transformers/model_doc/camembert.html>`__ (from Inria/Facebook/Sorbonne) released
|
5. `Blenderbot <https://huggingface.co/transformers/master/model_doc/blenderbot.html>`__ (from Facebook) released with
|
||||||
with the paper `CamemBERT: a Tasty French Language Model <https://arxiv.org/abs/1911.03894>`__ by Louis Martin*,
|
the paper `Recipes for building an open-domain chatbot <https://arxiv.org/abs/2004.13637>`__ by Stephen Roller,
|
||||||
Benjamin Muller*, Pedro Javier Ortiz Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé
|
Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan
|
||||||
Seddah and Benoît Sagot.
|
Boureau, Jason Weston.
|
||||||
6. `CTRL <https://huggingface.co/transformers/model_doc/ctrl.html>`__ (from Salesforce) released with the paper `CTRL:
|
6. :doc:`CamemBERT <model_doc/camembert>` (from Inria/Facebook/Sorbonne) released with the paper `CamemBERT: a Tasty
|
||||||
A Conditional Transformer Language Model for Controllable Generation <https://arxiv.org/abs/1909.05858>`__ by Nitish
|
French Language Model <https://arxiv.org/abs/1911.03894>`__ by Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz
|
||||||
Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher.
|
Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot.
|
||||||
7. `DeBERTa <https://huggingface.co/transformers/model_doc/deberta.html>`__ (from Microsoft Research) released with the
|
7. :doc:`CTRL <model_doc/ctrl>` (from Salesforce) released with the paper `CTRL: A Conditional Transformer Language
|
||||||
paper `DeBERTa: Decoding-enhanced BERT with Disentangled Attention <https://arxiv.org/abs/2006.03654>`__ by
|
Model for Controllable Generation <https://arxiv.org/abs/1909.05858>`__ by Nitish Shirish Keskar*, Bryan McCann*,
|
||||||
|
Lav R. Varshney, Caiming Xiong and Richard Socher.
|
||||||
|
8. `DeBERTa <https://huggingface.co/transformers/master/model_doc/deberta.html>`__ (from Microsoft Research) released
|
||||||
|
with the paper `DeBERTa: Decoding-enhanced BERT with Disentangled Attention <https://arxiv.org/abs/2006.03654>`__ by
|
||||||
Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
|
Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
|
||||||
8. `DialoGPT <https://huggingface.co/transformers/model_doc/dialogpt.html>`__ (from Microsoft Research) released with
|
9. :doc:`DialoGPT <model_doc/dialogpt>` (from Microsoft Research) released with the paper `DialoGPT: Large-Scale
|
||||||
the paper `DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation
|
Generative Pre-training for Conversational Response Generation <https://arxiv.org/abs/1911.00536>`__ by Yizhe Zhang,
|
||||||
<https://arxiv.org/abs/1911.00536>`__ by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang
|
Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan.
|
||||||
Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan.
|
10. :doc:`DistilBERT <model_doc/distilbert>` (from HuggingFace), released together with the paper `DistilBERT, a
|
||||||
9. `DistilBERT <https://huggingface.co/transformers/model_doc/distilbert.html>`__ (from HuggingFace), released together
|
distilled version of BERT: smaller, faster, cheaper and lighter <https://arxiv.org/abs/1910.01108>`__ by Victor
|
||||||
with the paper `DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter
|
Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into `DistilGPT2
|
||||||
<https://arxiv.org/abs/1910.01108>`__ by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been
|
|
||||||
applied to compress GPT2 into `DistilGPT2
|
|
||||||
<https://github.com/huggingface/transformers/tree/master/examples/distillation>`__, RoBERTa into `DistilRoBERTa
|
<https://github.com/huggingface/transformers/tree/master/examples/distillation>`__, RoBERTa into `DistilRoBERTa
|
||||||
<https://github.com/huggingface/transformers/tree/master/examples/distillation>`__, Multilingual BERT into
|
<https://github.com/huggingface/transformers/tree/master/examples/distillation>`__, Multilingual BERT into
|
||||||
`DistilmBERT <https://github.com/huggingface/transformers/tree/master/examples/distillation>`__ and a German version
|
`DistilmBERT <https://github.com/huggingface/transformers/tree/master/examples/distillation>`__ and a German
|
||||||
of DistilBERT.
|
version of DistilBERT.
|
||||||
10. `DPR <https://github.com/facebookresearch/DPR>`__ (from Facebook) released with the paper `Dense Passage Retrieval
|
11. :doc:`DPR <model_doc/dpr>` (from Facebook) released with the paper `Dense Passage Retrieval for Open-Domain
|
||||||
for Open-Domain Question Answering <https://arxiv.org/abs/2004.04906>`__ by Vladimir Karpukhin, Barlas Oğuz, Sewon
|
Question Answering <https://arxiv.org/abs/2004.04906>`__ by Vladimir Karpukhin, Barlas Oğuz, Sewon Min, Patrick
|
||||||
Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih.
|
Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih.
|
||||||
11. `ELECTRA <https://huggingface.co/transformers/model_doc/electra.html>`__ (from Google Research/Stanford University)
|
12. :doc:`ELECTRA <model_doc/electra>` (from Google Research/Stanford University) released with the paper `ELECTRA:
|
||||||
released with the paper `ELECTRA: Pre-training text encoders as discriminators rather than generators
|
Pre-training text encoders as discriminators rather than generators <https://arxiv.org/abs/2003.10555>`__ by Kevin
|
||||||
<https://arxiv.org/abs/2003.10555>`__ by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning.
|
Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning.
|
||||||
12. `FlauBERT <https://huggingface.co/transformers/model_doc/flaubert.html>`__ (from CNRS) released with the paper
|
13. :doc:`FlauBERT <model_doc/flaubert>` (from CNRS) released with the paper `FlauBERT: Unsupervised Language Model
|
||||||
`FlauBERT: Unsupervised Language Model Pre-training for French <https://arxiv.org/abs/1912.05372>`__ by Hang Le,
|
Pre-training for French <https://arxiv.org/abs/1912.05372>`__ by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne,
|
||||||
Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé,
|
Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé, Laurent Besacier, Didier Schwab.
|
||||||
Laurent Besacier, Didier Schwab.
|
14. :doc:`Funnel Transformer <model_doc/funnel>` (from CMU/Google Brain) released with the paper `Funnel-Transformer:
|
||||||
13. `Funnel Transformer <https://github.com/laiguokun/Funnel-Transformer>`__ (from CMU/Google Brain) released with the
|
Filtering out Sequential Redundancy for Efficient Language Processing <https://arxiv.org/abs/2006.03236>`__ by
|
||||||
paper `Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing
|
Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le.
|
||||||
<https://arxiv.org/abs/2006.03236>`__ by Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le.
|
15. :doc:`GPT <model_doc/gpt>` (from OpenAI) released with the paper `Improving Language Understanding by Generative
|
||||||
14. `GPT <https://huggingface.co/transformers/model_doc/gpt.html>`__ (from OpenAI) released with the paper `Improving
|
Pre-Training <https://blog.openai.com/language-unsupervised/>`__ by Alec Radford, Karthik Narasimhan, Tim Salimans
|
||||||
Language Understanding by Generative Pre-Training <https://blog.openai.com/language-unsupervised/>`__ by Alec
|
and Ilya Sutskever.
|
||||||
Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.
|
16. :doc:`GPT-2 <model_doc/gpt2>` (from OpenAI) released with the paper `Language Models are Unsupervised Multitask
|
||||||
15. `GPT-2 <https://huggingface.co/transformers/model_doc/gpt2.html>`__ (from OpenAI) released with the paper `Language
|
Learners <https://blog.openai.com/better-language-models/>`__ by Alec Radford*, Jeffrey Wu*, Rewon Child, David
|
||||||
Models are Unsupervised Multitask Learners <https://blog.openai.com/better-language-models/>`__ by Alec Radford*,
|
Luan, Dario Amodei** and Ilya Sutskever**.
|
||||||
Jeffrey Wu*, Rewon Child, David Luan, Dario Amodei** and Ilya Sutskever**.
|
17. :doc:`LayoutLM <model_doc/layoutlm>` (from Microsoft Research Asia) released with the paper `LayoutLM: Pre-training
|
||||||
16. `LayoutLM <https://github.com/microsoft/unilm/tree/master/layoutlm>`__ (from Microsoft Research Asia) released with
|
of Text and Layout for Document Image Understanding <https://arxiv.org/abs/1912.13318>`__ by Yiheng Xu, Minghao Li,
|
||||||
the paper `LayoutLM: Pre-training of Text and Layout for Document Image Understanding
|
Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou.
|
||||||
<https://arxiv.org/abs/1912.13318>`__ by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou.
|
18. :doc:`Longformer <model_doc/longformer>` (from AllenAI) released with the paper `Longformer: The Long-Document
|
||||||
17. `Longformer <https://huggingface.co/transformers/model_doc/longformer.html>`__ (from AllenAI) released with the
|
Transformer <https://arxiv.org/abs/2004.05150>`__ by Iz Beltagy, Matthew E. Peters, Arman Cohan.
|
||||||
paper `Longformer: The Long-Document Transformer <https://arxiv.org/abs/2004.05150>`__ by Iz Beltagy, Matthew E.
|
19. :doc:`LXMERT <model_doc/lxmert>` (from UNC Chapel Hill) released with the paper `LXMERT: Learning Cross-Modality
|
||||||
Peters, Arman Cohan.
|
Encoder Representations from Transformers for Open-Domain Question Answering <https://arxiv.org/abs/1908.07490>`__
|
||||||
18. `LXMERT <https://github.com/airsplay/lxmert>`__ (from UNC Chapel Hill) released with the paper `LXMERT: Learning
|
by Hao Tan and Mohit Bansal.
|
||||||
Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering
|
20. :doc:`MarianMT <model_doc/marian>` Machine translation models trained using `OPUS <http://opus.nlpl.eu/>`__ data by
|
||||||
<https://arxiv.org/abs/1908.07490>`__ by Hao Tan and Mohit Bansal.
|
Jörg Tiedemann. The `Marian Framework <https://marian-nmt.github.io/>`__ is being developed by the Microsoft
|
||||||
19. `MarianMT <https://huggingface.co/transformers/model_doc/marian.html>`__ Machine translation models trained using
|
Translator Team.
|
||||||
`OPUS <http://opus.nlpl.eu/>`__ data by Jörg Tiedemann. The `Marian Framework <https://marian-nmt.github.io/>`__ is
|
21. :doc:`MBart <model_doc/mbart>` (from Facebook) released with the paper `Multilingual Denoising Pre-training for
|
||||||
being developed by the Microsoft Translator Team.
|
Neural Machine Translation <https://arxiv.org/abs/2001.08210>`__ by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li,
|
||||||
20. `MBart <https://github.com/pytorch/fairseq/tree/master/examples/mbart>`__ (from Facebook) released with the paper
|
Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
||||||
`Multilingual Denoising Pre-training for Neural Machine Translation <https://arxiv.org/abs/2001.08210>`__ by Yinhan
|
22. :doc:`Pegasus <model_doc/pegasus>` (from Google) released with the paper `PEGASUS: Pre-training with Extracted
|
||||||
Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
Gap-sentences for Abstractive Summarization <https://arxiv.org/abs/1912.08777>`__> by Jingqing Zhang, Yao Zhao,
|
||||||
21. `MMBT <https://github.com/facebookresearch/mmbt/>`__ (from Facebook), released together with the paper a
|
Mohammad Saleh and Peter J. Liu.
|
||||||
`Supervised Multimodal Bitransformers for Classifying Images and Text <https://arxiv.org/pdf/1909.02950.pdf>`__ by
|
23. :doc:`Reformer <model_doc/reformer>` (from Google Research) released with the paper `Reformer: The Efficient
|
||||||
Douwe Kiela, Suvrat Bhooshan, Hamed Firooz, Davide Testuggine.
|
Transformer <https://arxiv.org/abs/2001.04451>`__ by Nikita Kitaev, Łukasz Kaiser, Anselm Levskaya.
|
||||||
22. `Pegasus <https://github.com/google-research/pegasus>`__ (from Google) released with the paper `PEGASUS:
|
24. :doc:`RoBERTa <model_doc/roberta>` (from Facebook), released together with the paper a `Robustly Optimized BERT
|
||||||
Pre-training with Extracted Gap-sentences for Abstractive Summarization <https://arxiv.org/abs/1912.08777>`__> by
|
Pretraining Approach <https://arxiv.org/abs/1907.11692>`__ by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar
|
||||||
Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu.
|
Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. ultilingual BERT into `DistilmBERT
|
||||||
23. `Reformer <https://huggingface.co/transformers/model_doc/reformer.html>`__ (from Google Research) released with the
|
|
||||||
paper `Reformer: The Efficient Transformer <https://arxiv.org/abs/2001.04451>`__ by Nikita Kitaev, Łukasz Kaiser,
|
|
||||||
Anselm Levskaya.
|
|
||||||
24. `RoBERTa <https://huggingface.co/transformers/model_doc/roberta.html>`__ (from Facebook), released together with
|
|
||||||
the paper a `Robustly Optimized BERT Pretraining Approach <https://arxiv.org/abs/1907.11692>`__ by Yinhan Liu, Myle
|
|
||||||
Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
|
|
||||||
ultilingual BERT into `DistilmBERT
|
|
||||||
<https://github.com/huggingface/transformers/tree/master/examples/distillation>`__ and a German version of
|
<https://github.com/huggingface/transformers/tree/master/examples/distillation>`__ and a German version of
|
||||||
DistilBERT.
|
DistilBERT.
|
||||||
25. `SqueezeBert <https://huggingface.co/transformers/model_doc/squeezebert.html>`__ released with the paper
|
25. `SqueezeBert <https://huggingface.co/transformers/master/model_doc/squeezebert.html>`__ released with the paper
|
||||||
`SqueezeBERT: What can computer vision teach NLP about efficient neural networks?
|
`SqueezeBERT: What can computer vision teach NLP about efficient neural networks?
|
||||||
<https://arxiv.org/abs/2006.11316>`__ by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer.
|
<https://arxiv.org/abs/2006.11316>`__ by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer.
|
||||||
26. `T5 <https://huggingface.co/transformers/model_doc/t5.html>`__ (from Google AI) released with the paper `Exploring
|
26. :doc:`T5 <model_doc/t5>` (from Google AI) released with the paper `Exploring the Limits of Transfer Learning with a
|
||||||
the Limits of Transfer Learning with a Unified Text-to-Text Transformer <https://arxiv.org/abs/1910.10683>`__ by
|
Unified Text-to-Text Transformer <https://arxiv.org/abs/1910.10683>`__ by Colin Raffel and Noam Shazeer and Adam
|
||||||
Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi
|
Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
|
||||||
Zhou and Wei Li and Peter J. Liu.
|
27. :doc:`Transformer-XL <model_doc/transformerxl>` (from Google/CMU) released with the paper `Transformer-XL:
|
||||||
27. `Transformer-XL <https://huggingface.co/transformers/model_doc/transformerxl.html>`__ (from Google/CMU) released
|
Attentive Language Models Beyond a Fixed-Length Context <https://arxiv.org/abs/1901.02860>`__ by Zihang Dai*,
|
||||||
with the paper `Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context
|
Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
||||||
<https://arxiv.org/abs/1901.02860>`__ by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le,
|
28. :doc:`XLM <model_doc/xlm>` (from Facebook) released together with the paper `Cross-lingual Language Model
|
||||||
Ruslan Salakhutdinov.
|
Pretraining <https://arxiv.org/abs/1901.07291>`__ by Guillaume Lample and Alexis Conneau.
|
||||||
28. `XLM <https://huggingface.co/transformers/model_doc/xlm.html>`__ (from Facebook) released together with the paper
|
29. :doc:`XLM-RoBERTa <model_doc/xlmroberta>` (from Facebook AI), released together with the paper `Unsupervised
|
||||||
`Cross-lingual Language Model Pretraining <https://arxiv.org/abs/1901.07291>`__ by Guillaume Lample and Alexis
|
Cross-lingual Representation Learning at Scale <https://arxiv.org/abs/1911.02116>`__ by Alexis Conneau*, Kartikay
|
||||||
Conneau.
|
Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke
|
||||||
29. `XLM-RoBERTa <https://huggingface.co/transformers/model_doc/xlmroberta.html>`__ (from Facebook AI), released
|
Zettlemoyer and Veselin Stoyanov.
|
||||||
together with the paper `Unsupervised Cross-lingual Representation Learning at Scale
|
30. :doc:`XLNet <model_doc/xlnet>` (from Google/CMU) released with the paper `XLNet: Generalized Autoregressive
|
||||||
<https://arxiv.org/abs/1911.02116>`__ by Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary,
|
Pretraining for Language Understanding <https://arxiv.org/abs/1906.08237>`__ by Zhilin Yang*, Zihang Dai*, Yiming
|
||||||
Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov.
|
Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le.
|
||||||
30. `XLNet <https://huggingface.co/transformers/model_doc/xlnet.html>`__ (from Google/CMU) released with the paper
|
|
||||||
`XLNet: Generalized Autoregressive Pretraining for Language Understanding <https://arxiv.org/abs/1906.08237>`__ by
|
|
||||||
Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le.
|
|
||||||
31. `Other community models <https://huggingface.co/models>`__, contributed by the `community
|
31. `Other community models <https://huggingface.co/models>`__, contributed by the `community
|
||||||
<https://huggingface.co/users>`__.
|
<https://huggingface.co/users>`__.
|
||||||
|
|
||||||
@@ -213,6 +204,7 @@ conversion utilities for the following models:
|
|||||||
:maxdepth: 2
|
:maxdepth: 2
|
||||||
:caption: Main Classes
|
:caption: Main Classes
|
||||||
|
|
||||||
|
main_classes/callback
|
||||||
main_classes/configuration
|
main_classes/configuration
|
||||||
main_classes/logging
|
main_classes/logging
|
||||||
main_classes/model
|
main_classes/model
|
||||||
@@ -232,6 +224,7 @@ conversion utilities for the following models:
|
|||||||
model_doc/bart
|
model_doc/bart
|
||||||
model_doc/bert
|
model_doc/bert
|
||||||
model_doc/bertgeneration
|
model_doc/bertgeneration
|
||||||
|
model_doc/blenderbot
|
||||||
model_doc/camembert
|
model_doc/camembert
|
||||||
model_doc/ctrl
|
model_doc/ctrl
|
||||||
model_doc/deberta
|
model_doc/deberta
|
||||||
@@ -270,3 +263,4 @@ conversion utilities for the following models:
|
|||||||
internal/modeling_utils
|
internal/modeling_utils
|
||||||
internal/pipelines_utils
|
internal/pipelines_utils
|
||||||
internal/tokenization_utils
|
internal/tokenization_utils
|
||||||
|
internal/trainer_utils
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
Utilities for Trainer
|
||||||
|
-----------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
This page lists all the utility functions used by :class:`~transformers.Trainer`.
|
||||||
|
|
||||||
|
Most of those are only useful if you are studying the code of the Trainer in the library.
|
||||||
|
|
||||||
|
Utilities
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.EvalPrediction
|
||||||
|
|
||||||
|
.. autofunction:: transformers.set_seed
|
||||||
|
|
||||||
|
.. autofunction:: transformers.torch_distributed_zero_first
|
||||||
|
|
||||||
|
|
||||||
|
Callbacks internals
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.trainer_callback.CallbackHandler
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
Callbacks
|
||||||
|
-----------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Callbacks are objects that can customize the behavior of the training loop in the PyTorch
|
||||||
|
:class:`~transformers.Trainer` (this feature is not yet implemented in TensorFlow) that can inspect the training loop
|
||||||
|
state (for progress reporting, logging on TensorBoard or other ML platforms...) and take decisions (like early
|
||||||
|
stopping).
|
||||||
|
|
||||||
|
Callbacks are "read only" pieces of code, apart from the :class:`~transformers.TrainerControl` object they return, they
|
||||||
|
cannot change anything in the training loop. For customizations that require changes in the training loop, you should
|
||||||
|
subclass :class:`~transformers.Trainer` and override the methods you need (see :doc:`trainer` for examples).
|
||||||
|
|
||||||
|
By default a :class:`~transformers.Trainer` will use the following callbacks:
|
||||||
|
|
||||||
|
- :class:`~transformers.DefaultFlowCallback` which handles the default beahvior for logging, saving and evaluation.
|
||||||
|
- :class:`~transformers.PrinterCallback` or :class:`~transformers.ProrgressCallback` to display progress and print the
|
||||||
|
logs (the first one is used if you deactivate tqdm through the :class:`~transformers.TrainingArguments`, otherwise
|
||||||
|
it's the second one).
|
||||||
|
- :class:`~transformers.integrations.TensorBoardCallback` if tensorboard is accessible (either through PyTorch >= 1.4
|
||||||
|
or tensorboardX).
|
||||||
|
- :class:`~transformers.integrations.WandbCallback` if `wandb <https://www.wandb.com/>`__ is installed.
|
||||||
|
- :class:`~transformers.integrations.CometCallback` if `comet_ml <https://www.comet.ml/site/>`__ is installed.
|
||||||
|
|
||||||
|
The main class that implements callbacks is :class:`~transformers.TrainerCallback`. It gets the
|
||||||
|
:class:`~transformers.TrainingArguments` used to instantiate the :class:`~transformers.Trainer`, can access that
|
||||||
|
Trainer's internal state via :class:`~transformers.TrainerState`, and can take some actions on the training loop via
|
||||||
|
:class:`~transformers.TrainerControl`.
|
||||||
|
|
||||||
|
|
||||||
|
Available Callbacks
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Here is the list of the available :class:`~transformers.TrainerCallback` in the library:
|
||||||
|
|
||||||
|
.. autoclass:: transformers.integrations.CometCallback
|
||||||
|
:members: setup
|
||||||
|
|
||||||
|
.. autoclass:: transformers.DefaultFlowCallback
|
||||||
|
|
||||||
|
.. autoclass:: transformers.PrinterCallback
|
||||||
|
|
||||||
|
.. autoclass:: transformers.ProgressCallback
|
||||||
|
|
||||||
|
.. autoclass:: transformers.integrations.TensorBoardCallback
|
||||||
|
|
||||||
|
.. autoclass:: transformers.integrations.WandbCallback
|
||||||
|
:members: setup
|
||||||
|
|
||||||
|
|
||||||
|
TrainerCallback
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.TrainerCallback
|
||||||
|
:members:
|
||||||
|
|
||||||
|
|
||||||
|
TrainerState
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.TrainerState
|
||||||
|
:members:
|
||||||
|
|
||||||
|
|
||||||
|
TrainerControl
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.TrainerControl
|
||||||
|
:members:
|
||||||
@@ -18,7 +18,6 @@ previous features. To inject custom behavior you can subclass them and override
|
|||||||
- **get_eval_dataloader**/**get_eval_tfdataset** -- Creates the evaulation DataLoader (PyTorch) or TF Dataset.
|
- **get_eval_dataloader**/**get_eval_tfdataset** -- Creates the evaulation DataLoader (PyTorch) or TF Dataset.
|
||||||
- **get_test_dataloader**/**get_test_tfdataset** -- Creates the test DataLoader (PyTorch) or TF Dataset.
|
- **get_test_dataloader**/**get_test_tfdataset** -- Creates the test DataLoader (PyTorch) or TF Dataset.
|
||||||
- **log** -- Logs information on the various objects watching training.
|
- **log** -- Logs information on the various objects watching training.
|
||||||
- **setup_wandb** -- Setups wandb (see `here <https://docs.wandb.com/huggingface>`__ for more information).
|
|
||||||
- **create_optimizer_and_scheduler** -- Setups the optimizer and learning rate scheduler if they were not passed at
|
- **create_optimizer_and_scheduler** -- Setups the optimizer and learning rate scheduler if they were not passed at
|
||||||
init.
|
init.
|
||||||
- **compute_loss** - Computes the loss on a batch of training inputs.
|
- **compute_loss** - Computes the loss on a batch of training inputs.
|
||||||
@@ -40,6 +39,10 @@ Here is an example of how to customize :class:`~transformers.Trainer` using a cu
|
|||||||
logits = outputs[0]
|
logits = outputs[0]
|
||||||
return my_custom_loss(logits, labels)
|
return my_custom_loss(logits, labels)
|
||||||
|
|
||||||
|
Another way to customize the training loop behavior for the PyTorch :class:`~transformers.Trainer` is to use
|
||||||
|
:doc:`callbacks <callback>` that can inspect the training loop state (for progress reporting, logging on TensorBoard or
|
||||||
|
other ML platforms...) and take decisions (like early stopping).
|
||||||
|
|
||||||
|
|
||||||
Trainer
|
Trainer
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@@ -47,29 +50,23 @@ Trainer
|
|||||||
.. autoclass:: transformers.Trainer
|
.. autoclass:: transformers.Trainer
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
TFTrainer
|
TFTrainer
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.TFTrainer
|
.. autoclass:: transformers.TFTrainer
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
TrainingArguments
|
TrainingArguments
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.TrainingArguments
|
.. autoclass:: transformers.TrainingArguments
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
TFTrainingArguments
|
TFTrainingArguments
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.TFTrainingArguments
|
.. autoclass:: transformers.TFTrainingArguments
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
Utilities
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. autoclass:: transformers.EvalPrediction
|
|
||||||
|
|
||||||
.. autofunction:: transformers.set_seed
|
|
||||||
|
|
||||||
.. autofunction:: transformers.torch_distributed_zero_first
|
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
Blenderbot
|
||||||
|
-----------------------------------------------------------------------------------------------------------------------
|
||||||
|
**DISCLAIMER:** If you see something strange,
|
||||||
|
file a `Github Issue <https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title>`__ .
|
||||||
|
|
||||||
|
Overview
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
The Blender chatbot model was proposed in `Recipes for building an open-domain chatbot <https://arxiv.org/pdf/2004.13637.pdf>`__ Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston on 30 Apr 2020.
|
||||||
|
|
||||||
|
The abstract of the paper is the following:
|
||||||
|
|
||||||
|
*Building open-domain chatbots is a challenging area for machine learning research. While prior work has shown that scaling neural models in the number of parameters and the size of the data they are trained on gives improved results, we show that other ingredients are important for a high-performing chatbot. Good conversation requires a number of skills that an expert conversationalist blends in a seamless way: providing engaging talking points and listening to their partners, and displaying knowledge, empathy and personality appropriately, while maintaining a consistent persona. We show that large scale models can learn these skills when given appropriate training data and choice of generation strategy. We build variants of these recipes with 90M, 2.7B and 9.4B parameter models, and make our models and code publicly available. Human evaluations show our best models are superior to existing approaches in multi-turn dialogue in terms of engagingness and humanness measurements. We then discuss the limitations of this work by analyzing failure cases of our models.*
|
||||||
|
|
||||||
|
The authors' code can be found `here <https://github.com/facebookresearch/ParlAI>`__ .
|
||||||
|
|
||||||
|
|
||||||
|
Implementation Notes
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
- Blenderbot uses a standard `seq2seq model transformer <https://arxiv.org/pdf/1706.03762.pdf>`__ based architecture.
|
||||||
|
- It inherits completely from :class:`~transformers.BartForConditionalGeneration`
|
||||||
|
- Even though blenderbot is one model, it uses two tokenizers :class:`~transformers.BlenderbotSmallTokenizer` for 90M checkpoint and :class:`~transformers.BlenderbotTokenizer` for all other checkpoints.
|
||||||
|
- :class:`~transformers.BlenderbotSmallTokenizer` will always return :class:`~transformers.BlenderbotSmallTokenizer`, regardless of checkpoint. To use the 3B parameter checkpoint, you must call :class:`~transformers.BlenderbotTokenizer` directly.
|
||||||
|
- Available checkpoints can be found in the `model hub <https://huggingface.co/models?search=blenderbot>`__.
|
||||||
|
|
||||||
|
|
||||||
|
Usage
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Model Usage:
|
||||||
|
|
||||||
|
>>> from transformers import BlenderbotSmallTokenizer, BlenderbotForConditionalGeneration
|
||||||
|
>>> mname = 'facebook/blenderbot-90M'
|
||||||
|
>>> model = BlenderbotForConditionalGeneration.from_pretrained(mname)
|
||||||
|
>>> tokenizer = BlenderbotSmallTokenizer.from_pretrained(mname)
|
||||||
|
>>> UTTERANCE = "My friends are cool but they eat too many carbs."
|
||||||
|
>>> inputs = tokenizer([UTTERANCE], return_tensors='pt')
|
||||||
|
>>> reply_ids = model.generate(**inputs)
|
||||||
|
>>> print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in reply_ids])
|
||||||
|
|
||||||
|
|
||||||
|
See Config Values:
|
||||||
|
|
||||||
|
>>> from transformers import BlenderbotConfig
|
||||||
|
>>> config_90 = BlenderbotConfig.from_pretrained("facebook/blenderbot-90M")
|
||||||
|
>>> config_90.to_diff_dict() # show interesting Values.
|
||||||
|
>>> configuration_3B = BlenderbotConfig("facebook/blenderbot-3B")
|
||||||
|
>>> configuration_3B.to_diff_dict()
|
||||||
|
|
||||||
|
|
||||||
|
BlenderbotConfig
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
.. autoclass:: transformers.BlenderbotConfig
|
||||||
|
:members:
|
||||||
|
|
||||||
|
BlenderbotTokenizer
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.BlenderbotTokenizer
|
||||||
|
:members: build_inputs_with_special_tokens
|
||||||
|
|
||||||
|
BlenderbotSmallTokenizer
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.BlenderbotSmallTokenizer
|
||||||
|
:members:
|
||||||
|
|
||||||
|
|
||||||
|
BlenderbotForConditionalGeneration
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
See :obj:`transformers.BartForConditionalGeneration` for arguments to `forward` and `generate`
|
||||||
|
|
||||||
|
.. autoclass:: transformers.BlenderbotForConditionalGeneration
|
||||||
|
:members:
|
||||||
@@ -88,6 +88,13 @@ GPT2DoubleHeadsModel
|
|||||||
:members: forward
|
:members: forward
|
||||||
|
|
||||||
|
|
||||||
|
GPT2ForSequenceClassification
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.GPT2ForSequenceClassification
|
||||||
|
:members: forward
|
||||||
|
|
||||||
|
|
||||||
TFGPT2Model
|
TFGPT2Model
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
SqueezeBERT
|
SqueezeBERT
|
||||||
----------------------------------------------------
|
-----------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
Overview
|
Overview
|
||||||
~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
The SqueezeBERT model was proposed in
|
The SqueezeBERT model was proposed in
|
||||||
`SqueezeBERT: What can computer vision teach NLP about efficient neural networks?
|
`SqueezeBERT: What can computer vision teach NLP about efficient neural networks?
|
||||||
@@ -40,14 +40,14 @@ Tips:
|
|||||||
`squeezebert/squeezebert-mnli-headless` checkpoint.
|
`squeezebert/squeezebert-mnli-headless` checkpoint.
|
||||||
|
|
||||||
SqueezeBertConfig
|
SqueezeBertConfig
|
||||||
~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertConfig
|
.. autoclass:: transformers.SqueezeBertConfig
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
SqueezeBertTokenizer
|
SqueezeBertTokenizer
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertTokenizer
|
.. autoclass:: transformers.SqueezeBertTokenizer
|
||||||
:members: build_inputs_with_special_tokens, get_special_tokens_mask,
|
:members: build_inputs_with_special_tokens, get_special_tokens_mask,
|
||||||
@@ -55,49 +55,49 @@ SqueezeBertTokenizer
|
|||||||
|
|
||||||
|
|
||||||
SqueezeBertTokenizerFast
|
SqueezeBertTokenizerFast
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertTokenizerFast
|
.. autoclass:: transformers.SqueezeBertTokenizerFast
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
SqueezeBertModel
|
SqueezeBertModel
|
||||||
~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertModel
|
.. autoclass:: transformers.SqueezeBertModel
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
SqueezeBertForMaskedLM
|
SqueezeBertForMaskedLM
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertForMaskedLM
|
.. autoclass:: transformers.SqueezeBertForMaskedLM
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
SqueezeBertForSequenceClassification
|
SqueezeBertForSequenceClassification
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertForSequenceClassification
|
.. autoclass:: transformers.SqueezeBertForSequenceClassification
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
SqueezeBertForMultipleChoice
|
SqueezeBertForMultipleChoice
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertForMultipleChoice
|
.. autoclass:: transformers.SqueezeBertForMultipleChoice
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
SqueezeBertForTokenClassification
|
SqueezeBertForTokenClassification
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertForTokenClassification
|
.. autoclass:: transformers.SqueezeBertForTokenClassification
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
SqueezeBertForQuestionAnswering
|
SqueezeBertForQuestionAnswering
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. autoclass:: transformers.SqueezeBertForQuestionAnswering
|
.. autoclass:: transformers.SqueezeBertForQuestionAnswering
|
||||||
:members:
|
:members:
|
||||||
@@ -294,10 +294,10 @@ For a list that includes community-uploaded models, refer to `https://huggingfac
|
|||||||
| | ``t5-11B`` | | ~11B parameters with 24-layers, 1024-hidden-state, 65536 feed-forward hidden-state, 128-heads, |
|
| | ``t5-11B`` | | ~11B parameters with 24-layers, 1024-hidden-state, 65536 feed-forward hidden-state, 128-heads, |
|
||||||
| | | | Trained on English text: the Colossal Clean Crawled Corpus (C4) |
|
| | | | Trained on English text: the Colossal Clean Crawled Corpus (C4) |
|
||||||
+--------------------+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------+
|
+--------------------+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------+
|
||||||
| XLM-RoBERTa | ``xlm-roberta-base`` | | ~125M parameters with 12-layers, 768-hidden-state, 3072 feed-forward hidden-state, 8-heads, |
|
| XLM-RoBERTa | ``xlm-roberta-base`` | | ~270M parameters with 12-layers, 768-hidden-state, 3072 feed-forward hidden-state, 8-heads, |
|
||||||
| | | | Trained on on 2.5 TB of newly created clean CommonCrawl data in 100 languages |
|
| | | | Trained on on 2.5 TB of newly created clean CommonCrawl data in 100 languages |
|
||||||
| +------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------+
|
| +------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------+
|
||||||
| | ``xlm-roberta-large`` | | ~355M parameters with 24-layers, 1027-hidden-state, 4096 feed-forward hidden-state, 16-heads, |
|
| | ``xlm-roberta-large`` | | ~550M parameters with 24-layers, 1024-hidden-state, 4096 feed-forward hidden-state, 16-heads, |
|
||||||
| | | | Trained on 2.5 TB of newly created clean CommonCrawl data in 100 languages |
|
| | | | Trained on 2.5 TB of newly created clean CommonCrawl data in 100 languages |
|
||||||
+--------------------+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------+
|
+--------------------+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------+
|
||||||
| FlauBERT | ``flaubert/flaubert_small_cased`` | | 6-layer, 512-hidden, 8-heads, 54M parameters |
|
| FlauBERT | ``flaubert/flaubert_small_cased`` | | 6-layer, 512-hidden, 8-heads, 54M parameters |
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ class BaseTransformer(pl.LightningModule):
|
|||||||
def get_lr_scheduler(self):
|
def get_lr_scheduler(self):
|
||||||
get_schedule_func = arg_to_scheduler[self.hparams.lr_scheduler]
|
get_schedule_func = arg_to_scheduler[self.hparams.lr_scheduler]
|
||||||
scheduler = get_schedule_func(
|
scheduler = get_schedule_func(
|
||||||
self.opt, num_warmup_steps=self.hparams.warmup_steps, num_training_steps=self.total_steps
|
self.opt, num_warmup_steps=self.hparams.warmup_steps, num_training_steps=self.total_steps()
|
||||||
)
|
)
|
||||||
scheduler = {"scheduler": scheduler, "interval": "step", "frequency": 1}
|
scheduler = {"scheduler": scheduler, "interval": "step", "frequency": 1}
|
||||||
return scheduler
|
return scheduler
|
||||||
@@ -159,19 +159,20 @@ class BaseTransformer(pl.LightningModule):
|
|||||||
def test_epoch_end(self, outputs):
|
def test_epoch_end(self, outputs):
|
||||||
return self.validation_end(outputs)
|
return self.validation_end(outputs)
|
||||||
|
|
||||||
@property
|
|
||||||
def total_steps(self) -> int:
|
def total_steps(self) -> int:
|
||||||
"""The number of total training steps that will be run. Used for lr scheduler purposes."""
|
"""The number of total training steps that will be run. Used for lr scheduler purposes."""
|
||||||
num_devices = max(1, self.hparams.gpus) # TODO: consider num_tpu_cores
|
num_devices = max(1, self.hparams.gpus) # TODO: consider num_tpu_cores
|
||||||
effective_batch_size = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices
|
effective_batch_size = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices
|
||||||
dataset_size = len(self.train_loader.dataset)
|
return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs
|
||||||
return (dataset_size / effective_batch_size) * self.hparams.max_epochs
|
|
||||||
|
|
||||||
def setup(self, mode):
|
def setup(self, mode):
|
||||||
if mode == "fit":
|
if mode == "test":
|
||||||
|
self.dataset_size = len(self.test_dataloader().dataset)
|
||||||
|
else:
|
||||||
self.train_loader = self.get_dataloader("train", self.hparams.train_batch_size, shuffle=True)
|
self.train_loader = self.get_dataloader("train", self.hparams.train_batch_size, shuffle=True)
|
||||||
|
self.dataset_size = len(self.train_loader.dataset)
|
||||||
|
|
||||||
def get_dataloader(self, type_path, batch_size, shuffle=False):
|
def get_dataloader(self, type_path: str, batch_size: int, shuffle: bool = False):
|
||||||
raise NotImplementedError("You must implement this for your task")
|
raise NotImplementedError("You must implement this for your task")
|
||||||
|
|
||||||
def train_dataloader(self):
|
def train_dataloader(self):
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ psutil
|
|||||||
sacrebleu
|
sacrebleu
|
||||||
rouge-score
|
rouge-score
|
||||||
tensorflow_datasets
|
tensorflow_datasets
|
||||||
pytorch-lightning==0.8.5
|
pytorch-lightning==0.9.0
|
||||||
matplotlib
|
matplotlib
|
||||||
git-python==1.0.3
|
git-python==1.0.3
|
||||||
faiss-cpu
|
faiss-cpu
|
||||||
|
|||||||
@@ -13,13 +13,12 @@ For `bertabs` instructions, see [`bertabs/README.md`](bertabs/README.md).
|
|||||||
- `FSMTForConditionalGeneration`
|
- `FSMTForConditionalGeneration`
|
||||||
- `T5ForConditionalGeneration`
|
- `T5ForConditionalGeneration`
|
||||||
|
|
||||||
|
|
||||||
## Datasets
|
## Datasets
|
||||||
|
|
||||||
#### XSUM:
|
#### XSUM:
|
||||||
```bash
|
```bash
|
||||||
cd examples/seq2seq
|
cd examples/seq2seq
|
||||||
wget https://s3.amazonaws.com/datasets.huggingface.co/summarization/xsum.tar.gz
|
wget https://cdn-datasets.huggingface.co/summarization/xsum.tar.gz
|
||||||
tar -xzvf xsum.tar.gz
|
tar -xzvf xsum.tar.gz
|
||||||
export XSUM_DIR=${PWD}/xsum
|
export XSUM_DIR=${PWD}/xsum
|
||||||
```
|
```
|
||||||
@@ -29,7 +28,7 @@ To use your own data, copy that files format. Each article to be summarized is o
|
|||||||
#### CNN/DailyMail
|
#### CNN/DailyMail
|
||||||
```bash
|
```bash
|
||||||
cd examples/seq2seq
|
cd examples/seq2seq
|
||||||
wget https://s3.amazonaws.com/datasets.huggingface.co/summarization/cnn_dm_v2.tgz
|
wget https://cdn-datasets.huggingface.co/summarization/cnn_dm_v2.tgz
|
||||||
tar -xzvf cnn_dm_v2.tgz # empty lines removed
|
tar -xzvf cnn_dm_v2.tgz # empty lines removed
|
||||||
mv cnn_cln cnn_dm
|
mv cnn_cln cnn_dm
|
||||||
export CNN_DIR=${PWD}/cnn_dm
|
export CNN_DIR=${PWD}/cnn_dm
|
||||||
@@ -39,7 +38,7 @@ this should make a directory called `cnn_dm/` with 6 files.
|
|||||||
#### WMT16 English-Romanian Translation Data:
|
#### WMT16 English-Romanian Translation Data:
|
||||||
download with this command:
|
download with this command:
|
||||||
```bash
|
```bash
|
||||||
wget https://s3.amazonaws.com/datasets.huggingface.co/translation/wmt_en_ro.tar.gz
|
wget https://cdn-datasets.huggingface.co/translation/wmt_en_ro.tar.gz
|
||||||
tar -xzvf wmt_en_ro.tar.gz
|
tar -xzvf wmt_en_ro.tar.gz
|
||||||
export ENRO_DIR=${PWD}/wmt_en_ro
|
export ENRO_DIR=${PWD}/wmt_en_ro
|
||||||
```
|
```
|
||||||
@@ -47,7 +46,7 @@ this should make a directory called `wmt_en_ro/` with 6 files.
|
|||||||
|
|
||||||
#### WMT English-German:
|
#### WMT English-German:
|
||||||
```bash
|
```bash
|
||||||
wget https://s3.amazonaws.com/datasets.huggingface.co/translation/wmt_en_de.tgz
|
wget https://cdn-datasets.huggingface.co/translation/wmt_en_de.tgz
|
||||||
tar -xzvf wmt_en_de.tgz
|
tar -xzvf wmt_en_de.tgz
|
||||||
export DATA_DIR=${PWD}/wmt_en_de
|
export DATA_DIR=${PWD}/wmt_en_de
|
||||||
```
|
```
|
||||||
@@ -265,6 +264,7 @@ export DATA_DIR=cnn_dm
|
|||||||
--fp16 \
|
--fp16 \
|
||||||
--bs 32
|
--bs 32
|
||||||
```
|
```
|
||||||
|
|
||||||
### Multi-GPU Evaluation
|
### Multi-GPU Evaluation
|
||||||
here is a command to run xsum evaluation on 8 GPUS. It is more than linearly faster than run_eval.py in some cases
|
here is a command to run xsum evaluation on 8 GPUS. It is more than linearly faster than run_eval.py in some cases
|
||||||
because it uses SortishSampler to minimize padding. You can also use it on 1 GPU. `data_dir` must have
|
because it uses SortishSampler to minimize padding. You can also use it on 1 GPU. `data_dir` must have
|
||||||
@@ -391,6 +391,17 @@ runtime: 13H on V-100 16GB GPU.
|
|||||||
pytest examples/seq2seq/
|
pytest examples/seq2seq/
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Converting pytorch-lightning checkpoints
|
||||||
|
pytorch lightning ``-do_predict`` often fails, after you are done training, the best way to evaluate your model is to convert it.
|
||||||
|
|
||||||
|
This should be done for you, with a file called `{save_dir}/best_tfmr`.
|
||||||
|
|
||||||
|
If that file doesn't exist but you have a lightning `.ckpt` file, you can run
|
||||||
|
```bash
|
||||||
|
python convert_pl_checkpoint_to_hf.py PATH_TO_CKPT randomly_initialized_hf_model_path save_dir/best_tfmr
|
||||||
|
```
|
||||||
|
Then either `run_eval` or `run_distributed_eval` with `save_dir/best_tfmr` (see previous sections)
|
||||||
|
|
||||||
|
|
||||||
## Experimental Features
|
## Experimental Features
|
||||||
These features are harder to use and not always useful.
|
These features are harder to use and not always useful.
|
||||||
@@ -419,4 +430,3 @@ uses 12,723 batches of length 48 and takes slightly more time 9.5 minutes.
|
|||||||
The feature is still experimental, because:
|
The feature is still experimental, because:
|
||||||
+ we can make it much more robust if we have memory mapped/preprocessed datasets.
|
+ we can make it much more robust if we have memory mapped/preprocessed datasets.
|
||||||
+ The speedup over sortish sampler is not that large at the moment.
|
+ The speedup over sortish sampler is not that large at the moment.
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ from finetune import main as ft_main
|
|||||||
from make_student import create_student_by_copying_alternating_layers, get_layers_to_supervise
|
from make_student import create_student_by_copying_alternating_layers, get_layers_to_supervise
|
||||||
from transformers import AutoModelForSeq2SeqLM, MBartTokenizer, T5ForConditionalGeneration
|
from transformers import AutoModelForSeq2SeqLM, MBartTokenizer, T5ForConditionalGeneration
|
||||||
from transformers.modeling_bart import shift_tokens_right
|
from transformers.modeling_bart import shift_tokens_right
|
||||||
from utils import calculate_bleu, freeze_params, label_smoothed_nll_loss, pickle_load, use_task_specific_params
|
from utils import calculate_bleu, freeze_params, label_smoothed_nll_loss, use_task_specific_params
|
||||||
|
|
||||||
|
|
||||||
# need the parent dir module
|
# need the parent dir module
|
||||||
@@ -28,7 +28,7 @@ from lightning_base import generic_train # noqa
|
|||||||
class BartSummarizationDistiller(SummarizationModule):
|
class BartSummarizationDistiller(SummarizationModule):
|
||||||
"""Supports Bart, Pegasus and other models that inherit from Bart."""
|
"""Supports Bart, Pegasus and other models that inherit from Bart."""
|
||||||
|
|
||||||
loss_names = ["loss", "ce_loss", "mlm_loss", "enc_mse_loss", "hid_loss_enc", "hid_loss_dec"]
|
loss_names = ["loss", "ce_loss", "mlm_loss", "hid_loss_enc", "hid_loss_dec"]
|
||||||
|
|
||||||
def __init__(self, hparams):
|
def __init__(self, hparams):
|
||||||
assert Path(hparams.data_dir).exists()
|
assert Path(hparams.data_dir).exists()
|
||||||
@@ -46,9 +46,19 @@ class BartSummarizationDistiller(SummarizationModule):
|
|||||||
if hparams.length_penalty != -1:
|
if hparams.length_penalty != -1:
|
||||||
student.config.length_penalty = hparams.length_penalty
|
student.config.length_penalty = hparams.length_penalty
|
||||||
super().__init__(hparams, model=student, config=student.config)
|
super().__init__(hparams, model=student, config=student.config)
|
||||||
|
model_type = student.config.model_type
|
||||||
self.e_layer_ids, self.d_layer_ids = e_layer_ids, d_layer_ids # type: List[int], List[int]
|
self.e_layer_ids, self.d_layer_ids = e_layer_ids, d_layer_ids # type: List[int], List[int]
|
||||||
self.different_encoder = hparams.student_encoder_layers != teacher.config.encoder_layers
|
|
||||||
self.different_decoder = hparams.student_decoder_layers != teacher.config.decoder_layers
|
if model_type == "t5":
|
||||||
|
teacher_encoder_layers = len(teacher.get_encoder().block)
|
||||||
|
teacher_decoder_layers = len(teacher.get_decoder().block)
|
||||||
|
else:
|
||||||
|
teacher_encoder_layers = teacher.config.encoder_layers
|
||||||
|
teacher_decoder_layers = teacher.config.decoder_layers
|
||||||
|
|
||||||
|
self.different_encoder = hparams.student_encoder_layers != teacher_encoder_layers
|
||||||
|
self.different_decoder = hparams.student_decoder_layers != teacher_decoder_layers
|
||||||
|
|
||||||
self.teacher = teacher
|
self.teacher = teacher
|
||||||
freeze_params(self.teacher)
|
freeze_params(self.teacher)
|
||||||
|
|
||||||
@@ -59,17 +69,17 @@ class BartSummarizationDistiller(SummarizationModule):
|
|||||||
del self.teacher.encoder
|
del self.teacher.encoder
|
||||||
# Intermediate supervision: Decide which layers to supervise
|
# Intermediate supervision: Decide which layers to supervise
|
||||||
if hparams.supervise_forward:
|
if hparams.supervise_forward:
|
||||||
self.d_matches = get_layers_to_supervise(
|
self.e_matches = get_layers_to_supervise(n_student=len(self.e_layer_ids), n_teacher=teacher_encoder_layers)
|
||||||
n_student=len(self.d_layer_ids), n_teacher=self.teacher.config.decoder_layers
|
self.d_matches = get_layers_to_supervise(n_student=len(self.d_layer_ids), n_teacher=teacher_decoder_layers)
|
||||||
)
|
else: # student layer should emulate hidden states of the teacher layer it was copied from
|
||||||
else:
|
self.e_matches = self.e_layer_ids
|
||||||
self.d_matches = self.d_layer_ids
|
self.d_matches = self.d_layer_ids
|
||||||
|
|
||||||
self.ce_loss_fct = nn.KLDivLoss(reduction="batchmean")
|
self.ce_loss_fct = nn.KLDivLoss(reduction="batchmean")
|
||||||
self.temperature = 2.0
|
self.temperature = 2.0
|
||||||
self.alpha_mlm = hparams.alpha_mlm
|
self.alpha_mlm = hparams.alpha_mlm
|
||||||
self.alpha_ce = hparams.alpha_ce
|
self.alpha_ce = hparams.alpha_ce
|
||||||
self.alpha_hid = hparams.alpha_hid
|
self.alpha_hid = hparams.alpha_hid
|
||||||
self.alpha_encoder_loss = hparams.alpha_encoder_loss
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
torch.cuda.empty_cache()
|
torch.cuda.empty_cache()
|
||||||
|
|
||||||
@@ -129,7 +139,7 @@ class BartSummarizationDistiller(SummarizationModule):
|
|||||||
output_hidden_states=True,
|
output_hidden_states=True,
|
||||||
output_attentions=False,
|
output_attentions=False,
|
||||||
use_cache=False,
|
use_cache=False,
|
||||||
) # TODO(@sshleifer): return_dict=True cleanup
|
)
|
||||||
|
|
||||||
# Same cross entropy vs. label smoothing logic as finetune.py
|
# Same cross entropy vs. label smoothing logic as finetune.py
|
||||||
assert lm_logits.shape[-1] == self.model.config.vocab_size
|
assert lm_logits.shape[-1] == self.model.config.vocab_size
|
||||||
@@ -146,30 +156,32 @@ class BartSummarizationDistiller(SummarizationModule):
|
|||||||
def zero_tensor():
|
def zero_tensor():
|
||||||
return torch.tensor(0.0).type_as(student_lm_loss)
|
return torch.tensor(0.0).type_as(student_lm_loss)
|
||||||
|
|
||||||
loss_encoder, hid_loss_enc, hid_loss_dec = zero_tensor(), zero_tensor(), zero_tensor()
|
hid_loss_enc, hid_loss_dec = zero_tensor(), zero_tensor()
|
||||||
if self.different_encoder:
|
if self.different_encoder: # compute encoder hidden state loss
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
teacher_enc_outputs, teacher_enc_hid, _ = self.teacher.get_encoder()(
|
teacher_enc_hid = self.teacher.get_encoder()(
|
||||||
input_ids, attention_mask=src_mask, output_hidden_states=True
|
input_ids, attention_mask=src_mask, output_hidden_states=True, return_dict=True
|
||||||
|
).hidden_states
|
||||||
|
|
||||||
|
hid_loss_enc = self.calc_hidden_loss(
|
||||||
|
src_mask,
|
||||||
|
enc_hidden_state,
|
||||||
|
teacher_enc_hid,
|
||||||
|
self.e_matches,
|
||||||
|
normalize_hidden=self.hparams.normalize_hidden,
|
||||||
)
|
)
|
||||||
# DEPRECATE THIS
|
|
||||||
if self.hparams.alpha_encoder_loss > 0:
|
|
||||||
loss_encoder = self.calc_mse_loss(enc_outputs, teacher_enc_outputs, src_mask)
|
|
||||||
|
|
||||||
hid_loss_enc = self.calc_hidden_loss(src_mask, enc_hidden_state, teacher_enc_hid, self.e_layer_ids)
|
|
||||||
|
|
||||||
teacher_enc_outputs = (enc_outputs,)
|
|
||||||
assert isinstance(teacher_enc_outputs, tuple), type(teacher_enc_outputs)
|
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
tloss, tlogits, tdec_hidden, _ = self.teacher(
|
outputs = self.teacher(
|
||||||
input_ids,
|
input_ids,
|
||||||
attention_mask=src_mask,
|
attention_mask=src_mask,
|
||||||
encoder_outputs=teacher_enc_outputs,
|
encoder_outputs=(enc_outputs,),
|
||||||
decoder_input_ids=decoder_input_ids,
|
decoder_input_ids=decoder_input_ids,
|
||||||
lm_labels=labels,
|
lm_labels=labels,
|
||||||
output_hidden_states=True,
|
output_hidden_states=True,
|
||||||
|
return_dict=True,
|
||||||
)
|
)
|
||||||
|
tlogits, tdec_hidden = outputs.logits, outputs.decoder_hidden_states
|
||||||
dec_mask = decoder_input_ids.ne(pad_token_id)
|
dec_mask = decoder_input_ids.ne(pad_token_id)
|
||||||
loss_ce = self.calc_ce_loss(dec_mask, lm_logits, tlogits)
|
loss_ce = self.calc_ce_loss(dec_mask, lm_logits, tlogits)
|
||||||
if self.alpha_hid > 0: # Intermediate supervision of decoder hidden states
|
if self.alpha_hid > 0: # Intermediate supervision of decoder hidden states
|
||||||
@@ -180,10 +192,9 @@ class BartSummarizationDistiller(SummarizationModule):
|
|||||||
blended_loss = (
|
blended_loss = (
|
||||||
self.alpha_ce * loss_ce
|
self.alpha_ce * loss_ce
|
||||||
+ self.alpha_mlm * student_lm_loss
|
+ self.alpha_mlm * student_lm_loss
|
||||||
+ self.hparams.alpha_encoder_loss * loss_encoder
|
|
||||||
+ self.hparams.alpha_hid * (hid_loss_enc + hid_loss_dec)
|
+ self.hparams.alpha_hid * (hid_loss_enc + hid_loss_dec)
|
||||||
)
|
)
|
||||||
return blended_loss, loss_ce, student_lm_loss, loss_encoder, hid_loss_enc, hid_loss_dec
|
return blended_loss, loss_ce, student_lm_loss, hid_loss_enc, hid_loss_dec
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def calc_hidden_loss(attention_mask, hidden_states, hidden_states_T, matches, normalize_hidden):
|
def calc_hidden_loss(attention_mask, hidden_states, hidden_states_T, matches, normalize_hidden):
|
||||||
@@ -207,7 +218,6 @@ def add_distill_args(parser):
|
|||||||
parser.add_argument("--teacher", type=str)
|
parser.add_argument("--teacher", type=str)
|
||||||
parser.add_argument("--alpha_ce", default=0.8, type=float)
|
parser.add_argument("--alpha_ce", default=0.8, type=float)
|
||||||
parser.add_argument("--alpha_mlm", default=0.2, type=float)
|
parser.add_argument("--alpha_mlm", default=0.2, type=float)
|
||||||
parser.add_argument("--alpha_encoder_loss", default=0.0, type=float)
|
|
||||||
parser.add_argument("--alpha_hid", default=0.0, type=float, required=False)
|
parser.add_argument("--alpha_hid", default=0.0, type=float, required=False)
|
||||||
parser.add_argument("--student_decoder_layers", default=12, type=int, required=False)
|
parser.add_argument("--student_decoder_layers", default=12, type=int, required=False)
|
||||||
parser.add_argument("--student_encoder_layers", default=12, type=int, required=False)
|
parser.add_argument("--student_encoder_layers", default=12, type=int, required=False)
|
||||||
@@ -254,30 +264,6 @@ def create_module(args):
|
|||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
def evaluate_checkpoint(ckpt_path: Path, dest_dir=None):
|
|
||||||
# TODO(SS): DELETE? Better to convert_pl_ckpt_to_hf and run_eval.py
|
|
||||||
exp_dir = ckpt_path.parent
|
|
||||||
if dest_dir is None:
|
|
||||||
dest_dir = exp_dir
|
|
||||||
clash = list(dest_dir.glob("test_generations*"))
|
|
||||||
if clash:
|
|
||||||
print(f"SKIPPING to avoid overwriting {clash}")
|
|
||||||
ckpt = torch.load(ckpt_path, map_location="cpu")
|
|
||||||
if "hparams" in ckpt:
|
|
||||||
args = argparse.Namespace(**ckpt["hparams"])
|
|
||||||
else:
|
|
||||||
args = argparse.Namespace(**pickle_load(exp_dir / "hparams.pkl"))
|
|
||||||
args.resume_from_checkpoint = str(ckpt_path)
|
|
||||||
args.do_train = False
|
|
||||||
args.output_dir = str(dest_dir)
|
|
||||||
args.n_gpu = 1
|
|
||||||
args.eval_batch_size = 16
|
|
||||||
Path(args.output_dir).mkdir(exist_ok=True)
|
|
||||||
model = create_module(args)
|
|
||||||
trainer: pl.Trainer = generic_train(model, args, early_stopping_callback=False)
|
|
||||||
trainer.test(model)
|
|
||||||
|
|
||||||
|
|
||||||
def distill_main(args):
|
def distill_main(args):
|
||||||
Path(args.output_dir).mkdir(exist_ok=True)
|
Path(args.output_dir).mkdir(exist_ok=True)
|
||||||
if len(os.listdir(args.output_dir)) > 3 and args.do_train:
|
if len(os.listdir(args.output_dir)) > 3 and args.do_train:
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ from utils import (
|
|||||||
lmap,
|
lmap,
|
||||||
pickle_save,
|
pickle_save,
|
||||||
save_git_info,
|
save_git_info,
|
||||||
|
save_json,
|
||||||
use_task_specific_params,
|
use_task_specific_params,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -105,6 +106,7 @@ class SummarizationModule(BaseTransformer):
|
|||||||
self.dataset_class = (
|
self.dataset_class = (
|
||||||
Seq2SeqDataset if hasattr(self.tokenizer, "prepare_seq2seq_batch") else LegacySeq2SeqDataset
|
Seq2SeqDataset if hasattr(self.tokenizer, "prepare_seq2seq_batch") else LegacySeq2SeqDataset
|
||||||
)
|
)
|
||||||
|
self.already_saved_batch = False
|
||||||
self.eval_beams = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams
|
self.eval_beams = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams
|
||||||
if self.hparams.eval_max_gen_length is not None:
|
if self.hparams.eval_max_gen_length is not None:
|
||||||
self.eval_max_length = self.hparams.eval_max_gen_length
|
self.eval_max_length = self.hparams.eval_max_gen_length
|
||||||
@@ -112,6 +114,17 @@ class SummarizationModule(BaseTransformer):
|
|||||||
self.eval_max_length = self.model.config.max_length
|
self.eval_max_length = self.model.config.max_length
|
||||||
self.val_metric = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric
|
self.val_metric = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric
|
||||||
|
|
||||||
|
def save_readable_batch(self, batch: Dict[str, torch.Tensor]) -> Dict[str, List[str]]:
|
||||||
|
"""A debugging utility"""
|
||||||
|
readable_batch = {
|
||||||
|
k: self.tokenizer.batch_decode(v.tolist()) if "mask" not in k else v.shape for k, v in batch.items()
|
||||||
|
}
|
||||||
|
save_json(readable_batch, Path(self.output_dir) / "text_batch.json")
|
||||||
|
save_json({k: v.tolist() for k, v in batch.items()}, Path(self.output_dir) / "tok_batch.json")
|
||||||
|
|
||||||
|
self.already_saved_batch = True
|
||||||
|
return readable_batch
|
||||||
|
|
||||||
def forward(self, input_ids, **kwargs):
|
def forward(self, input_ids, **kwargs):
|
||||||
return self.model(input_ids, **kwargs)
|
return self.model(input_ids, **kwargs)
|
||||||
|
|
||||||
@@ -129,6 +142,9 @@ class SummarizationModule(BaseTransformer):
|
|||||||
decoder_input_ids = self.model._shift_right(tgt_ids)
|
decoder_input_ids = self.model._shift_right(tgt_ids)
|
||||||
else:
|
else:
|
||||||
decoder_input_ids = shift_tokens_right(tgt_ids, pad_token_id)
|
decoder_input_ids = shift_tokens_right(tgt_ids, pad_token_id)
|
||||||
|
if not self.already_saved_batch: # This would be slightly better if it only happened on rank zero
|
||||||
|
batch["decoder_input_ids"] = decoder_input_ids
|
||||||
|
self.save_readable_batch(batch)
|
||||||
|
|
||||||
outputs = self(src_ids, attention_mask=src_mask, decoder_input_ids=decoder_input_ids, use_cache=False)
|
outputs = self(src_ids, attention_mask=src_mask, decoder_input_ids=decoder_input_ids, use_cache=False)
|
||||||
lm_logits = outputs[0]
|
lm_logits = outputs[0]
|
||||||
@@ -165,6 +181,7 @@ class SummarizationModule(BaseTransformer):
|
|||||||
return self._generative_step(batch)
|
return self._generative_step(batch)
|
||||||
|
|
||||||
def validation_epoch_end(self, outputs, prefix="val") -> Dict:
|
def validation_epoch_end(self, outputs, prefix="val") -> Dict:
|
||||||
|
|
||||||
self.step_count += 1
|
self.step_count += 1
|
||||||
losses = {k: torch.stack([x[k] for x in outputs]).mean() for k in self.loss_names}
|
losses = {k: torch.stack([x[k] for x in outputs]).mean() for k in self.loss_names}
|
||||||
loss = losses["loss"]
|
loss = losses["loss"]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# Script for verifying that run_bart_sum can be invoked from its directory
|
# Script for verifying that run_bart_sum can be invoked from its directory
|
||||||
|
|
||||||
# Get tiny dataset with cnn_dm format (4 examples for train, val, test)
|
# Get tiny dataset with cnn_dm format (4 examples for train, val, test)
|
||||||
wget https://s3.amazonaws.com/datasets.huggingface.co/summarization/cnn_tiny.tgz
|
wget https://cdn-datasets.huggingface.co/summarization/cnn_tiny.tgz
|
||||||
tar -xzvf cnn_tiny.tgz
|
tar -xzvf cnn_tiny.tgz
|
||||||
rm cnn_tiny.tgz
|
rm cnn_tiny.tgz
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import sys
|
|||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from seq2seq_trainer import Seq2SeqTrainer
|
from seq2seq_trainer import Seq2SeqTrainer, arg_to_scheduler_choices
|
||||||
from transformers import (
|
from transformers import (
|
||||||
AutoConfig,
|
AutoConfig,
|
||||||
AutoModelForSeq2SeqLM,
|
AutoModelForSeq2SeqLM,
|
||||||
@@ -63,6 +63,9 @@ class Seq2SeqTrainingArguments(TrainingArguments):
|
|||||||
attention_dropout: Optional[float] = field(
|
attention_dropout: Optional[float] = field(
|
||||||
default=None, metadata={"help": "Attention dropout probability. Goes into model.config."}
|
default=None, metadata={"help": "Attention dropout probability. Goes into model.config."}
|
||||||
)
|
)
|
||||||
|
lr_scheduler: Optional[str] = field(
|
||||||
|
default="linear", metadata={"help": f"Which lr scheduler to use. Selected in {arg_to_scheduler_choices}"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ logger = logging.get_logger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
def copy_layers(src_layers: nn.ModuleList, dest_layers: nn.ModuleList, layers_to_copy: List[int]) -> None:
|
def copy_layers(src_layers: nn.ModuleList, dest_layers: nn.ModuleList, layers_to_copy: List[int]) -> None:
|
||||||
layers_to_copy = nn.ModuleList([l for i, l in enumerate(src_layers) if i in layers_to_copy])
|
layers_to_copy = nn.ModuleList([src_layers[i] for i in layers_to_copy])
|
||||||
assert len(dest_layers) == len(layers_to_copy), f"{len(dest_layers)} != {len(layers_to_copy)}"
|
assert len(dest_layers) == len(layers_to_copy), f"{len(dest_layers)} != {len(layers_to_copy)}"
|
||||||
dest_layers.load_state_dict(layers_to_copy.state_dict())
|
dest_layers.load_state_dict(layers_to_copy.state_dict())
|
||||||
|
|
||||||
@@ -81,6 +81,8 @@ def create_student_by_copying_alternating_layers(
|
|||||||
e: Union[int, None] = None,
|
e: Union[int, None] = None,
|
||||||
d: Union[int, None] = None,
|
d: Union[int, None] = None,
|
||||||
copy_first_teacher_layers=False,
|
copy_first_teacher_layers=False,
|
||||||
|
e_layers_to_copy=None,
|
||||||
|
d_layers_to_copy=None,
|
||||||
**extra_config_kwargs
|
**extra_config_kwargs
|
||||||
) -> Tuple[PreTrainedModel, List[int], List[int]]:
|
) -> Tuple[PreTrainedModel, List[int], List[int]]:
|
||||||
"""Make a student by copying alternating layers from a teacher, save it to save_path.
|
"""Make a student by copying alternating layers from a teacher, save it to save_path.
|
||||||
@@ -142,7 +144,9 @@ def create_student_by_copying_alternating_layers(
|
|||||||
return student, e_layers_to_copy, d_layers_to_copy
|
return student, e_layers_to_copy, d_layers_to_copy
|
||||||
|
|
||||||
# Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer.
|
# Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer.
|
||||||
|
if e_layers_to_copy is None:
|
||||||
e_layers_to_copy: List[int] = pick_layers_to_copy(e, teacher_e)
|
e_layers_to_copy: List[int] = pick_layers_to_copy(e, teacher_e)
|
||||||
|
if d_layers_to_copy is None:
|
||||||
d_layers_to_copy: List[int] = pick_layers_to_copy(d, teacher_d)
|
d_layers_to_copy: List[int] = pick_layers_to_copy(d, teacher_d)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
### Saved Pseudo-Labels
|
||||||
|
These are the generations of various large models on various large **training** sets. All in all they took about 200 GPU hours to produce.
|
||||||
|
|
||||||
|
### Available Pseudo-labels
|
||||||
|
| Dataset | Model | Link | Rouge Scores | Notes
|
||||||
|
|---------|-----------------------------|----------------------------------------------------------------------------------------|--------------------|-------------------------------------------------------------------------------------------------------------
|
||||||
|
| XSUM | `facebook/bart-large-xsum` | [download](https://cdn-datasets.huggingface.co/pseudo/xsum/bart_xsum_pl.tgz) | 49.8/28.0/42.5 |
|
||||||
|
| XSUM | `google/pegasus-xsum` | [download](https://cdn-datasets.huggingface.co/pseudo/xsum/pegasus_xsum.tgz) | 53.3/32.7/46.5 |
|
||||||
|
| XSUM | `facebook/bart-large-xsum` | [download](https://cdn-datasets.huggingface.co/pseudo/xsum/xsum_pl2_bart.tgz) | | Bart pseudolabels filtered to those with Rouge2 > 10.0 w GT.
|
||||||
|
| CNN/DM | `sshleifer/pegasus-cnn-ft-v2` | [download](https://cdn-datasets.huggingface.co/pseudo/cnn_dm/pegasus_cnn_cnn_pls.tgz) | 47.316/26.65/44.56 | do not worry about the fact that train.source is one line shorter.
|
||||||
|
| CNN/DM | `facebook/bart-large-cnn` | [download](https://cdn-datasets.huggingface.co/pseudo/cnn_dm/cnn_bart_pl.tgz) | | 5K (2%) are missing, there should be 282173
|
||||||
|
| CNN/DM | `google/pegasus-xsum` | [download](https://cdn-datasets.huggingface.co/pseudo/cnn_dm/pegasus_xsum_on_cnn.tgz) | 21.5/6.76/25 | extra labels for xsum distillation Used max_source_length=512, (and all other pegasus-xsum configuration).
|
||||||
|
| EN-RO | `Helsinki-NLP/opus-mt-en-ro` | [download](https://cdn-datasets.huggingface.co/pseudo/wmt_en_ro/opus_mt_en_ro.tgz) | |
|
||||||
|
| EN-RO | `facebook/mbart-large-en-ro` | [download](https://cdn-datasets.huggingface.co/pseudo/wmt_en_ro/mbart_large_en_ro.tgz) | |
|
||||||
|
|
||||||
|
|
||||||
|
(EN_RO = WMT 2016 English-Romanian).
|
||||||
|
|
||||||
|
Example Download Command:
|
||||||
|
```bash
|
||||||
|
curl -S https://cdn-datasets.huggingface.co/pseudo/xsum/bart_xsum_pl.tgz | tar -xvz -C .
|
||||||
|
```
|
||||||
|
### Generating New Pseudolabels
|
||||||
|
Here is the command I used to generate the pseudolabels in the second row of the table, after downloading XSUM from [here](https://cdn-datasets.huggingface.co/summarization/xsum.tar.gz).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m torch.distributed.launch --nproc_per_node=8 run_distributed_eval.py \
|
||||||
|
--model_name google/pegasus-xsum \
|
||||||
|
--save_dir pegasus_xsum \
|
||||||
|
--data_dir xsum \
|
||||||
|
--bs 8 --sync_timeout 60000 \
|
||||||
|
--max_source_length 512 \
|
||||||
|
--type_path train
|
||||||
|
```
|
||||||
|
|
||||||
|
+ These command takes a while to run. For example, pegasus_cnn_cnn_pls.tgz took 8 hours on 8 GPUs.
|
||||||
|
+ Pegasus does not work in fp16 :(, Bart, mBART and Marian do.
|
||||||
|
+ Even if you have 1 GPU, `run_distributed_eval.py` is 10-20% faster than `run_eval.py` because it uses `SortishSampler` to minimize padding computation.
|
||||||
|
|
||||||
|
### Contributions
|
||||||
|
Feel free to contribute your own pseudolabels via PR. Add a row to this table with a new google drive link (or other command line downloadable link).
|
||||||
|
|
||||||
|
|
||||||
@@ -8,8 +8,17 @@ from torch.utils.data import DistributedSampler, RandomSampler
|
|||||||
from transformers import Trainer
|
from transformers import Trainer
|
||||||
from transformers.configuration_fsmt import FSMTConfig
|
from transformers.configuration_fsmt import FSMTConfig
|
||||||
from transformers.file_utils import is_torch_tpu_available
|
from transformers.file_utils import is_torch_tpu_available
|
||||||
from transformers.optimization import Adafactor, AdamW, get_linear_schedule_with_warmup
|
from transformers.optimization import (
|
||||||
from transformers.trainer import get_tpu_sampler
|
Adafactor,
|
||||||
|
AdamW,
|
||||||
|
get_constant_schedule,
|
||||||
|
get_constant_schedule_with_warmup,
|
||||||
|
get_cosine_schedule_with_warmup,
|
||||||
|
get_cosine_with_hard_restarts_schedule_with_warmup,
|
||||||
|
get_linear_schedule_with_warmup,
|
||||||
|
get_polynomial_decay_schedule_with_warmup,
|
||||||
|
)
|
||||||
|
from transformers.trainer_pt_utils import get_tpu_sampler
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -20,6 +29,16 @@ except ImportError:
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
arg_to_scheduler = {
|
||||||
|
"linear": get_linear_schedule_with_warmup,
|
||||||
|
"cosine": get_cosine_schedule_with_warmup,
|
||||||
|
"cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup,
|
||||||
|
"polynomial": get_polynomial_decay_schedule_with_warmup,
|
||||||
|
"constant": get_constant_schedule,
|
||||||
|
"constant_w_warmup": get_constant_schedule_with_warmup,
|
||||||
|
}
|
||||||
|
arg_to_scheduler_choices = sorted(arg_to_scheduler.keys())
|
||||||
|
|
||||||
|
|
||||||
class Seq2SeqTrainer(Trainer):
|
class Seq2SeqTrainer(Trainer):
|
||||||
def __init__(self, config, data_args, *args, **kwargs):
|
def __init__(self, config, data_args, *args, **kwargs):
|
||||||
@@ -62,9 +81,21 @@ class Seq2SeqTrainer(Trainer):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if self.lr_scheduler is None:
|
if self.lr_scheduler is None:
|
||||||
self.lr_scheduler = get_linear_schedule_with_warmup(
|
self.lr_scheduler = self._get_lr_scheduler(num_training_steps)
|
||||||
|
else: # ignoring --lr_scheduler
|
||||||
|
logger.warn("scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.")
|
||||||
|
|
||||||
|
def _get_lr_scheduler(self, num_training_steps):
|
||||||
|
schedule_func = arg_to_scheduler[self.args.lr_scheduler]
|
||||||
|
if self.args.lr_scheduler == "constant":
|
||||||
|
scheduler = schedule_func(self.optimizer)
|
||||||
|
elif self.args.lr_scheduler == "constant_w_warmup":
|
||||||
|
scheduler = schedule_func(self.optimizer, num_warmup_steps=self.args.warmup_steps)
|
||||||
|
else:
|
||||||
|
scheduler = schedule_func(
|
||||||
self.optimizer, num_warmup_steps=self.args.warmup_steps, num_training_steps=num_training_steps
|
self.optimizer, num_warmup_steps=self.args.warmup_steps, num_training_steps=num_training_steps
|
||||||
)
|
)
|
||||||
|
return scheduler
|
||||||
|
|
||||||
def _get_train_sampler(self) -> Optional[torch.utils.data.sampler.Sampler]:
|
def _get_train_sampler(self) -> Optional[torch.utils.data.sampler.Sampler]:
|
||||||
if isinstance(self.train_dataset, torch.utils.data.IterableDataset):
|
if isinstance(self.train_dataset, torch.utils.data.IterableDataset):
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import tempfile
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from transformers.testing_utils import slow
|
from transformers.testing_utils import slow
|
||||||
from transformers.trainer_utils import TrainerState, set_seed
|
from transformers.trainer_callback import TrainerState
|
||||||
|
from transformers.trainer_utils import set_seed
|
||||||
|
|
||||||
from .finetune_trainer import main
|
from .finetune_trainer import main
|
||||||
from .test_seq2seq_examples import MBART_TINY
|
from .test_seq2seq_examples import MBART_TINY
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import torch
|
|||||||
|
|
||||||
import lightning_base
|
import lightning_base
|
||||||
from convert_pl_checkpoint_to_hf import convert_pl_to_hf
|
from convert_pl_checkpoint_to_hf import convert_pl_to_hf
|
||||||
from distillation import distill_main, evaluate_checkpoint
|
from distillation import distill_main
|
||||||
from finetune import SummarizationModule, main
|
from finetune import SummarizationModule, main
|
||||||
from run_eval import generate_summaries_or_translations, run_generate
|
from run_eval import generate_summaries_or_translations, run_generate
|
||||||
from run_eval_search import run_search
|
from run_eval_search import run_search
|
||||||
@@ -86,7 +86,6 @@ CHEAP_ARGS = {
|
|||||||
"n_val": -1,
|
"n_val": -1,
|
||||||
"n_test": -1,
|
"n_test": -1,
|
||||||
"student_encoder_layers": 1,
|
"student_encoder_layers": 1,
|
||||||
"alpha_encoder_loss": 0.0,
|
|
||||||
"freeze_encoder": False,
|
"freeze_encoder": False,
|
||||||
"auto_scale_batch_size": False,
|
"auto_scale_batch_size": False,
|
||||||
}
|
}
|
||||||
@@ -179,7 +178,6 @@ class TestSummarizationDistiller(unittest.TestCase):
|
|||||||
generate_summaries_or_translations(examples, out_path, str(model.output_dir / "best_tfmr"))
|
generate_summaries_or_translations(examples, out_path, str(model.output_dir / "best_tfmr"))
|
||||||
self.assertTrue(Path(out_path).exists())
|
self.assertTrue(Path(out_path).exists())
|
||||||
|
|
||||||
evaluate_checkpoint(ckpts[0], dest_dir=Path(tempfile.mkdtemp()))
|
|
||||||
out_path_new = tempfile.mkdtemp()
|
out_path_new = tempfile.mkdtemp()
|
||||||
convert_pl_to_hf(ckpts[0], transformer_ckpts[0].parent, out_path_new)
|
convert_pl_to_hf(ckpts[0], transformer_ckpts[0].parent, out_path_new)
|
||||||
assert os.path.exists(os.path.join(out_path_new, "pytorch_model.bin"))
|
assert os.path.exists(os.path.join(out_path_new, "pytorch_model.bin"))
|
||||||
@@ -228,9 +226,6 @@ class TestSummarizationDistiller(unittest.TestCase):
|
|||||||
assert len(all_files) > 2
|
assert len(all_files) > 2
|
||||||
self.assertEqual(len(transformer_ckpts), 2)
|
self.assertEqual(len(transformer_ckpts), 2)
|
||||||
|
|
||||||
evaluate_checkpoint(ckpts[0], dest_dir=Path(tempfile.mkdtemp()))
|
|
||||||
|
|
||||||
@unittest.skip("T5 distillation is broken at the moment")
|
|
||||||
def test_distill_t5(self):
|
def test_distill_t5(self):
|
||||||
updates = dict(
|
updates = dict(
|
||||||
student_encoder_layers=1,
|
student_encoder_layers=1,
|
||||||
@@ -255,7 +250,6 @@ class TestSummarizationDistiller(unittest.TestCase):
|
|||||||
model_name_or_path="sshleifer/tinier_bart",
|
model_name_or_path="sshleifer/tinier_bart",
|
||||||
teacher=CHEAP_ARGS["model_name_or_path"],
|
teacher=CHEAP_ARGS["model_name_or_path"],
|
||||||
val_check_interval=0.5,
|
val_check_interval=0.5,
|
||||||
alpha_encoder_loss=0.4,
|
|
||||||
)
|
)
|
||||||
default_updates.update(updates)
|
default_updates.update(updates)
|
||||||
args_d: dict = CHEAP_ARGS.copy()
|
args_d: dict = CHEAP_ARGS.copy()
|
||||||
@@ -425,6 +419,10 @@ def test_finetune(model):
|
|||||||
assert bart.decoder.embed_tokens == bart.encoder.embed_tokens
|
assert bart.decoder.embed_tokens == bart.encoder.embed_tokens
|
||||||
assert bart.decoder.embed_tokens == bart.shared
|
assert bart.decoder.embed_tokens == bart.shared
|
||||||
|
|
||||||
|
example_batch = load_json(module.output_dir / "text_batch.json")
|
||||||
|
assert isinstance(example_batch, dict)
|
||||||
|
assert len(example_batch) >= 4
|
||||||
|
|
||||||
|
|
||||||
def test_finetune_extra_model_args():
|
def test_finetune_extra_model_args():
|
||||||
args_d: dict = CHEAP_ARGS.copy()
|
args_d: dict = CHEAP_ARGS.copy()
|
||||||
|
|||||||
@@ -116,8 +116,8 @@ class ExamplesTests(TestCasePlus):
|
|||||||
testargs.append("--fp16")
|
testargs.append("--fp16")
|
||||||
|
|
||||||
with patch.object(sys, "argv", testargs):
|
with patch.object(sys, "argv", testargs):
|
||||||
result = run_pl_glue.main()
|
result = run_pl_glue.main()[0]
|
||||||
# for now just testing that the script can run to a completion
|
# for now just testing that the script can run to completion
|
||||||
self.assertGreater(result["acc"], 0.25)
|
self.assertGreater(result["acc"], 0.25)
|
||||||
#
|
#
|
||||||
# TODO: this fails on CI - doesn't get acc/f1>=0.75:
|
# TODO: this fails on CI - doesn't get acc/f1>=0.75:
|
||||||
|
|||||||
@@ -96,6 +96,9 @@ def get_tfds(
|
|||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if train_ds is not None:
|
||||||
|
train_ds = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN])))
|
||||||
|
|
||||||
val_ds = (
|
val_ds = (
|
||||||
tf.data.Dataset.from_generator(
|
tf.data.Dataset.from_generator(
|
||||||
gen_val,
|
gen_val,
|
||||||
@@ -106,6 +109,9 @@ def get_tfds(
|
|||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if val_ds is not None:
|
||||||
|
val_ds = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION])))
|
||||||
|
|
||||||
test_ds = (
|
test_ds = (
|
||||||
tf.data.Dataset.from_generator(
|
tf.data.Dataset.from_generator(
|
||||||
gen_test,
|
gen_test,
|
||||||
@@ -116,6 +122,9 @@ def get_tfds(
|
|||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if test_ds is not None:
|
||||||
|
test_ds = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST])))
|
||||||
|
|
||||||
return train_ds, val_ds, test_ds, label2id
|
return train_ds, val_ds, test_ds, label2id
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,125 @@
|
|||||||
|
---
|
||||||
|
language: protein
|
||||||
|
tags:
|
||||||
|
- protein language model
|
||||||
|
datasets:
|
||||||
|
- BFD
|
||||||
|
---
|
||||||
|
|
||||||
|
# ProtT5-XL-BFD model
|
||||||
|
|
||||||
|
Pretrained model on protein sequences using a masked language modeling (MLM) objective. It was introduced in
|
||||||
|
[this paper](https://doi.org/10.1101/2020.07.12.199554) and first released in
|
||||||
|
[this repository](https://github.com/agemagician/ProtTrans). This model is trained on uppercase amino acids: it only works with capital letter amino acids.
|
||||||
|
|
||||||
|
|
||||||
|
## Model description
|
||||||
|
|
||||||
|
ProtT5-XL-BFD is based on the `t5-3b` model and was pretrained on a large corpus of protein sequences in a self-supervised fashion.
|
||||||
|
This means it was pretrained on the raw protein sequences only, with no humans labelling them in any way (which is why it can use lots of
|
||||||
|
publicly available data) with an automatic process to generate inputs and labels from those protein sequences.
|
||||||
|
|
||||||
|
One important difference between this T5 model and the original T5 version is the denosing objective.
|
||||||
|
The original T5-3B model was pretrained using a span denosing objective, while this model was pre-trained with a Bart-like MLM denosing objective.
|
||||||
|
The masking probability is consistent with the original T5 training by randomly masking 15% of the amino acids in the input.
|
||||||
|
|
||||||
|
It has been shown that the features extracted from this self-supervised model (LM-embeddings) captured important biophysical properties governing protein shape.
|
||||||
|
shape.
|
||||||
|
This implied learning some of the grammar of the language of life realized in protein sequences.
|
||||||
|
|
||||||
|
## Intended uses & limitations
|
||||||
|
|
||||||
|
The model could be used for protein feature extraction or to be fine-tuned on downstream tasks.
|
||||||
|
We have noticed in some tasks on can gain more accuracy by fine-tuning the model rather than using it as a feature extractor.
|
||||||
|
We have also noticed that for feature extraction, its better to use the feature extracted from the encoder not from the decoder.
|
||||||
|
|
||||||
|
### How to use
|
||||||
|
|
||||||
|
Here is how to use this model to extract the features of a given protein sequence in PyTorch:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import T5Tokenizer, T5Model
|
||||||
|
import re
|
||||||
|
import torch
|
||||||
|
|
||||||
|
tokenizer = T5Tokenizer.from_pretrained('Rostlab/prot_t5_xl_bfd', do_lower_case=False)
|
||||||
|
|
||||||
|
model = T5Model.from_pretrained("Rostlab/prot_t5_xl_bfd")
|
||||||
|
|
||||||
|
sequences_Example = ["A E T C Z A O","S K T Z P"]
|
||||||
|
|
||||||
|
sequences_Example = [re.sub(r"[UZOB]", "X", sequence) for sequence in sequences_Example]
|
||||||
|
|
||||||
|
ids = tokenizer.batch_encode_plus(sequences_Example, add_special_tokens=True, padding=True)
|
||||||
|
|
||||||
|
input_ids = torch.tensor(ids['input_ids'])
|
||||||
|
attention_mask = torch.tensor(ids['attention_mask'])
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
embedding = model(input_ids=input_ids,attention_mask=attention_mask,decoder_input_ids=None)
|
||||||
|
|
||||||
|
# For feature extraction we recommend to use the encoder embedding
|
||||||
|
encoder_embedding = embedding[2].cpu().numpy()
|
||||||
|
decoder_embedding = embedding[0].cpu().numpy()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Training data
|
||||||
|
|
||||||
|
The ProtT5-XL-BFD model was pretrained on [BFD](https://bfd.mmseqs.com/), a dataset consisting of 2.1 billion protein sequences.
|
||||||
|
|
||||||
|
## Training procedure
|
||||||
|
|
||||||
|
### Preprocessing
|
||||||
|
|
||||||
|
The protein sequences are uppercased and tokenized using a single space and a vocabulary size of 21. The rare amino acids "U,Z,O,B" were mapped to "X".
|
||||||
|
The inputs of the model are then of the form:
|
||||||
|
|
||||||
|
```
|
||||||
|
Protein Sequence [EOS]
|
||||||
|
```
|
||||||
|
|
||||||
|
The preprocessing step was performed on the fly, by cutting and padding the protein sequences up to 512 tokens.
|
||||||
|
|
||||||
|
The details of the masking procedure for each sequence are as follows:
|
||||||
|
- 15% of the amino acids are masked.
|
||||||
|
- In 90% of the cases, the masked amino acids are replaced by `[MASK]` token.
|
||||||
|
- In 10% of the cases, the masked amino acids are replaced by a random amino acid (different) from the one they replace.
|
||||||
|
|
||||||
|
### Pretraining
|
||||||
|
|
||||||
|
The model was trained on a single TPU Pod V3-1024 for 1.2 million steps in total, using sequence length 512 (batch size 4k).
|
||||||
|
It has a total of approximately 3B parameters and was trained using the encoder-decoder architecture.
|
||||||
|
The optimizer used is AdaFactor with inverse square root learning rate schedule for pre-training.
|
||||||
|
|
||||||
|
|
||||||
|
## Evaluation results
|
||||||
|
|
||||||
|
When the model is used for feature etraction, this model achieves the following results:
|
||||||
|
|
||||||
|
Test results :
|
||||||
|
|
||||||
|
| Task/Dataset | secondary structure (3-states) | secondary structure (8-states) | Localization | Membrane |
|
||||||
|
|:-----:|:-----:|:-----:|:-----:|:-----:|
|
||||||
|
| CASP12 | 77 | 66 | | |
|
||||||
|
| TS115 | 85 | 74 | | |
|
||||||
|
| CB513 | 84 | 71 | | |
|
||||||
|
| DeepLoc | | | 77 | 91 |
|
||||||
|
|
||||||
|
### BibTeX entry and citation info
|
||||||
|
|
||||||
|
```bibtex
|
||||||
|
@article {Elnaggar2020.07.12.199554,
|
||||||
|
author = {Elnaggar, Ahmed and Heinzinger, Michael and Dallago, Christian and Rehawi, Ghalia and Wang, Yu and Jones, Llion and Gibbs, Tom and Feher, Tamas and Angerer, Christoph and Steinegger, Martin and BHOWMIK, DEBSINDHU and Rost, Burkhard},
|
||||||
|
title = {ProtTrans: Towards Cracking the Language of Life{\textquoteright}s Code Through Self-Supervised Deep Learning and High Performance Computing},
|
||||||
|
elocation-id = {2020.07.12.199554},
|
||||||
|
year = {2020},
|
||||||
|
doi = {10.1101/2020.07.12.199554},
|
||||||
|
publisher = {Cold Spring Harbor Laboratory},
|
||||||
|
abstract = {Computational biology and bioinformatics provide vast data gold-mines from protein sequences, ideal for Language Models (LMs) taken from Natural Language Processing (NLP). These LMs reach for new prediction frontiers at low inference costs. Here, we trained two auto-regressive language models (Transformer-XL, XLNet) and two auto-encoder models (Bert, Albert) on data from UniRef and BFD containing up to 393 billion amino acids (words) from 2.1 billion protein sequences (22- and 112 times the entire English Wikipedia). The LMs were trained on the Summit supercomputer at Oak Ridge National Laboratory (ORNL), using 936 nodes (total 5616 GPUs) and one TPU Pod (V3-512 or V3-1024). We validated the advantage of up-scaling LMs to larger models supported by bigger data by predicting secondary structure (3-states: Q3=76-84, 8 states: Q8=65-73), sub-cellular localization for 10 cellular compartments (Q10=74) and whether a protein is membrane-bound or water-soluble (Q2=89). Dimensionality reduction revealed that the LM-embeddings from unlabeled data (only protein sequences) captured important biophysical properties governing protein shape. This implied learning some of the grammar of the language of life realized in protein sequences. The successful up-scaling of protein LMs through HPC to larger data sets slightly reduced the gap between models trained on evolutionary information and LMs. Availability ProtTrans: \<a href="https://github.com/agemagician/ProtTrans"\>https://github.com/agemagician/ProtTrans\</a\>Competing Interest StatementThe authors have declared no competing interest.},
|
||||||
|
URL = {https://www.biorxiv.org/content/early/2020/07/21/2020.07.12.199554},
|
||||||
|
eprint = {https://www.biorxiv.org/content/early/2020/07/21/2020.07.12.199554.full.pdf},
|
||||||
|
journal = {bioRxiv}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> Created by [Ahmed Elnaggar/@Elnaggar_AI](https://twitter.com/Elnaggar_AI) | [LinkedIn](https://www.linkedin.com/in/prof-ahmed-elnaggar/)
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
# Roberta Trained Model For Masked Language Model On French Corpus :robot:
|
||||||
|
|
||||||
|
|
||||||
|
This is a Masked Language Model trained with [Roberta](https://huggingface.co/transformers/model_doc/roberta.html) on a small French News Corpus(Leipzig corpora).
|
||||||
|
The model is built using Huggingface transformers.
|
||||||
|
The model can be found at :[French-Roberta](https://huggingface.co/abhilash1910/french-roberta)
|
||||||
|
|
||||||
|
|
||||||
|
## Specifications
|
||||||
|
|
||||||
|
|
||||||
|
The corpus for training is taken from Leipzig Corpora (French News) , and is trained on a small set of the corpus (300K).
|
||||||
|
|
||||||
|
|
||||||
|
## Model Specification
|
||||||
|
|
||||||
|
|
||||||
|
The model chosen for training is [Roberta](https://arxiv.org/abs/1907.11692) with the following specifications:
|
||||||
|
1. vocab_size=32000
|
||||||
|
2. max_position_embeddings=514
|
||||||
|
3. num_attention_heads=12
|
||||||
|
4. num_hidden_layers=6
|
||||||
|
5. type_vocab_size=1
|
||||||
|
|
||||||
|
|
||||||
|
This is trained by using RobertaConfig from transformers package.The total training parameters :68124416
|
||||||
|
The model is trained for 100 epochs with a gpu batch size of 64 units.
|
||||||
|
More details for building custom models can be found at the [HuggingFace Blog](https://huggingface.co/blog/how-to-train)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Usage Specifications
|
||||||
|
|
||||||
|
|
||||||
|
For using this model, we have to first import AutoTokenizer and AutoModelWithLMHead Modules from transformers
|
||||||
|
After that we have to specify, the pre-trained model,which in this case is 'abhilash1910/french-roberta' for the tokenizers and the model.
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import AutoTokenizer, AutoModelWithLMHead
|
||||||
|
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained("abhilash1910/french-roberta")
|
||||||
|
|
||||||
|
model = AutoModelWithLMHead.from_pretrained("abhilash1910/french-roberta")
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
After this the model will be downloaded, it will take some time to download all the model files.
|
||||||
|
For testing the model, we have to import pipeline module from transformers and create a masked output model for inference as follows:
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import pipeline
|
||||||
|
model_mask = pipeline('fill-mask', model='abhilash1910/french-roberta')
|
||||||
|
model_mask("Le tweet <mask>.")
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Some of the examples are also provided with generic French sentences:
|
||||||
|
|
||||||
|
Example 1:
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
model_mask("À ce jour, <mask> projet a entraîné")
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
|
||||||
|
```bash
|
||||||
|
[{'sequence': '<s>À ce jour, belles projet a entraîné</s>',
|
||||||
|
'score': 0.18685665726661682,
|
||||||
|
'token': 6504,
|
||||||
|
'token_str': 'Ġbelles'},
|
||||||
|
{'sequence': '<s>À ce jour,- projet a entraîné</s>',
|
||||||
|
'score': 0.0005200508167035878,
|
||||||
|
'token': 17,
|
||||||
|
'token_str': '-'},
|
||||||
|
{'sequence': '<s>À ce jour, de projet a entraîné</s>',
|
||||||
|
'score': 0.00045729897101409733,
|
||||||
|
'token': 268,
|
||||||
|
'token_str': 'Ġde'},
|
||||||
|
{'sequence': '<s>À ce jour, du projet a entraîné</s>',
|
||||||
|
'score': 0.0004307595663703978,
|
||||||
|
'token': 326,
|
||||||
|
'token_str': 'Ġdu'},
|
||||||
|
{'sequence': '<s>À ce jour," projet a entraîné</s>',
|
||||||
|
'score': 0.0004219160182401538,
|
||||||
|
'token': 6,
|
||||||
|
'token_str': '"'}]
|
||||||
|
```
|
||||||
|
|
||||||
|
Example 2:
|
||||||
|
|
||||||
|
```python
|
||||||
|
model_mask("C'est un <mask>")
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
[{'sequence': "<s>C'est un belles</s>",
|
||||||
|
'score': 0.16440927982330322,
|
||||||
|
'token': 6504,
|
||||||
|
'token_str': 'Ġbelles'},
|
||||||
|
{'sequence': "<s>C'est un de</s>",
|
||||||
|
'score': 0.0005495127406902611,
|
||||||
|
'token': 268,
|
||||||
|
'token_str': 'Ġde'},
|
||||||
|
{'sequence': "<s>C'est un du</s>",
|
||||||
|
'score': 0.00044988933950662613,
|
||||||
|
'token': 326,
|
||||||
|
'token_str': 'Ġdu'},
|
||||||
|
{'sequence': "<s>C'est un-</s>",
|
||||||
|
'score': 0.00044542422983795404,
|
||||||
|
'token': 17,
|
||||||
|
'token_str': '-'},
|
||||||
|
{'sequence': "<s>C'est un\t</s>",
|
||||||
|
'score': 0.00037563967634923756,
|
||||||
|
'token': 202,
|
||||||
|
'token_str': 'ĉ'}]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Resources
|
||||||
|
|
||||||
|
For all resources , please look into the [HuggingFace](https://huggingface.co/) Site and the [Repositories](https://github.com/huggingface).
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
---
|
||||||
|
|
||||||
|
language:
|
||||||
|
- ar
|
||||||
|
- en
|
||||||
|
|
||||||
|
license: mit
|
||||||
|
---
|
||||||
|
### xlm-r-large-arabic-toxic (toxic/hate speech classifier)
|
||||||
|
Toxic (hate speech) classification (Label_0: non-toxic, Label_1: toxic) of Arabic comments by fine-tuning XLM-Roberta-Large.
|
||||||
|
Zero shot classification of other languages (also works in mixed languages - ex. Arabic & English).
|
||||||
|
Usage and further info: see last section in this [Colab notebook](https://lnkd.in/d3bCFyZ)
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
---
|
||||||
|
language:
|
||||||
|
- en
|
||||||
|
- fr
|
||||||
|
- es
|
||||||
|
- de
|
||||||
|
- zh
|
||||||
|
|
||||||
|
tags:
|
||||||
|
- pytorch
|
||||||
|
- bert
|
||||||
|
- multilingual
|
||||||
|
- en
|
||||||
|
- fr
|
||||||
|
- es
|
||||||
|
- de
|
||||||
|
- zh
|
||||||
|
|
||||||
|
datasets: wikipedia
|
||||||
|
|
||||||
|
license: apache-2.0
|
||||||
|
|
||||||
|
inference: false
|
||||||
|
---
|
||||||
|
|
||||||
|
# bert-base-5lang-cased
|
||||||
|
This is a smaller version of [bert-base-multilingual-cased](https://huggingface.co/bert-base-multilingual-cased) that handles only 5 languages (en, fr, es, de and zh) instead of 104.
|
||||||
|
The model is therefore 30% smaller than the original one (124M parameters instead of 178M) but gives exactly the same representations for the above cited languages.
|
||||||
|
Starting from `bert-base-5lang-cased` will facilitate the deployment of your model on public cloud platforms while keeping similar results.
|
||||||
|
For instance, Google Cloud Platform requires that the model size on disk should be lower than 500 MB for serveless deployments (Cloud Functions / Cloud ML) which is not the case of the original `bert-base-multilingual-cased`.
|
||||||
|
|
||||||
|
For more information about the models size, memory footprint and loading time please refer to the table below:
|
||||||
|
|
||||||
|
| Model | Num parameters | Size | Memory | Loading time |
|
||||||
|
| ---------------------------- | -------------- | -------- | -------- | ------------ |
|
||||||
|
| bert-base-multilingual-cased | 178 million | 714 MB | 1400 MB | 4.2 sec |
|
||||||
|
| bert-base-5lang-cased | 124 million | 495 MB | 950 MB | 3.6 sec |
|
||||||
|
|
||||||
|
These measurements have been computed on a [Google Cloud n1-standard-1 machine (1 vCPU, 3.75 GB)](https://cloud.google.com/compute/docs/machine-types\#n1_machine_type).
|
||||||
|
|
||||||
|
## How to use
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import AutoTokenizer, AutoModel
|
||||||
|
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained("amine/bert-base-5lang-cased")
|
||||||
|
model = AutoModel.from_pretrained("amine/bert-base-5lang-cased")
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### How to cite
|
||||||
|
|
||||||
|
```bibtex
|
||||||
|
@inproceedings{smallermbert,
|
||||||
|
title={Load What You Need: Smaller Versions of Mutlilingual BERT},
|
||||||
|
author={Abdaoui, Amine and Pradel, Camille and Sigel, Grégoire},
|
||||||
|
booktitle={SustaiNLP / EMNLP},
|
||||||
|
year={2020}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
|
||||||
|
Please contact amine@geotrend.fr for any question, feedback or request.
|
||||||
@@ -13,6 +13,9 @@ trained with this notebook
|
|||||||
https://colab.research.google.com/drive/1pfCV3bngAmISNZVfDvBMyEhQKuYw37Rl#scrollTo=AyImj9qZYLRi&uniqifier=3
|
https://colab.research.google.com/drive/1pfCV3bngAmISNZVfDvBMyEhQKuYw37Rl#scrollTo=AyImj9qZYLRi&uniqifier=3
|
||||||
|
|
||||||
config from microsoft/DialoGPT-medium
|
config from microsoft/DialoGPT-medium
|
||||||
|
dataset generated from 2018 opensubtitle from opus folowing these guidelines
|
||||||
|
https://github.com/PolyAI-LDN/conversational-datasets/tree/master/opensubtitles with this notebook
|
||||||
|
https://colab.research.google.com/drive/1uyh3vJ9nEjqOHI68VD73qxt4olJzODxi#scrollTo=deaacv4XfLMk
|
||||||
### How to use
|
### How to use
|
||||||
|
|
||||||
Now we are ready to try out how the model works as a chatting partner!
|
Now we are ready to try out how the model works as a chatting partner!
|
||||||
|
|||||||
@@ -8,15 +8,21 @@ RuDR-BERT - Multilingual, Cased, which pretrained on the raw part of the RuDReC
|
|||||||
|
|
||||||
If you find this repository helpful, feel free to cite our publication:
|
If you find this repository helpful, feel free to cite our publication:
|
||||||
|
|
||||||
[1] https://arxiv.org/abs/2004.03659
|
[1] Tutubalina E, Alimova I, Miftahutdinov Z, et al. The Russian Drug Reaction Corpus and Neural Models for Drug Reactions and Effectiveness Detection in User Reviews.
|
||||||
|
|
||||||
|
preprint: https://arxiv.org/abs/2004.03659
|
||||||
```
|
```
|
||||||
@misc{tutubalina2020russian,
|
@article{10.1093/bioinformatics/btaa675,
|
||||||
title={The Russian Drug Reaction Corpus and Neural Models for Drug Reactions and Effectiveness Detection in User Reviews},
|
author = {Tutubalina, Elena and Alimova, Ilseyar and Miftahutdinov, Zulfat and Sakhovskiy, Andrey and Malykh, Valentin and Nikolenko, Sergey},
|
||||||
author={Elena Tutubalina and Ilseyar Alimova and Zulfat Miftahutdinov and Andrey Sakhovskiy and Valentin Malykh and Sergey Nikolenko},
|
title = "{The Russian Drug Reaction Corpus and Neural Models for Drug Reactions and Effectiveness Detection in User Reviews}",
|
||||||
|
journal = {Bioinformatics},
|
||||||
year = {2020},
|
year = {2020},
|
||||||
eprint={2004.03659},
|
month = {07},
|
||||||
archivePrefix={arXiv},
|
issn = {1367-4803},
|
||||||
primaryClass={cs.CL}
|
doi = {10.1093/bioinformatics/btaa675},
|
||||||
|
url = {https://doi.org/10.1093/bioinformatics/btaa675},
|
||||||
|
note = {btaa675},
|
||||||
|
eprint = {https://academic.oup.com/bioinformatics/advance-article-pdf/doi/10.1093/bioinformatics/btaa675/33539752/btaa675.pdf},
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
[2] Tutubalina, EV and Miftahutdinov, Z Sh and Nugmanov, RI and Madzhidov, TI and Nikolenko, SI and Alimova, IS and Tropsha, AE Using semantic analysis of texts for the identification of drugs with similar therapeutic effects.
|
[2] Tutubalina, EV and Miftahutdinov, Z Sh and Nugmanov, RI and Madzhidov, TI and Nikolenko, SI and Alimova, IS and Tropsha, AE Using semantic analysis of texts for the identification of drugs with similar therapeutic effects.
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
license: apache-2.0
|
||||||
|
---
|
||||||
@@ -4,4 +4,5 @@ datasets:
|
|||||||
- squad
|
- squad
|
||||||
metrics:
|
metrics:
|
||||||
- squad
|
- squad
|
||||||
|
license: apache-2.0
|
||||||
---
|
---
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: de
|
language: de
|
||||||
|
license: apache-2.0
|
||||||
---
|
---
|
||||||
## distilbert-base-german-cased
|
## distilbert-base-german-cased
|
||||||
@@ -6,4 +6,5 @@ widget:
|
|||||||
context: "The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain \"Amazonas\" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."
|
context: "The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain \"Amazonas\" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."
|
||||||
- text: "How many square kilometers of rainforest is covered in the basin?"
|
- text: "How many square kilometers of rainforest is covered in the basin?"
|
||||||
context: "The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain \"Amazonas\" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."
|
context: "The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain \"Amazonas\" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."
|
||||||
|
license: apache-2.0
|
||||||
---
|
---
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
license: apache-2.0
|
||||||
|
---
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
license: mit
|
||||||
|
---
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
---
|
||||||
|
language: tl
|
||||||
|
tags:
|
||||||
|
- bert
|
||||||
|
- tagalog
|
||||||
|
- filipino
|
||||||
|
license: gpl-3.0
|
||||||
|
inference: false
|
||||||
|
---
|
||||||
|
|
||||||
|
# BERT Tagalog Base Cased (Whole Word Masking)
|
||||||
|
Tagalog version of BERT trained on a large preprocessed text corpus scraped and sourced from the internet. This model is part of a larger research project. We open-source the model to allow greater usage within the Filipino NLP community. This particular version uses whole word masking.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
The model can be loaded and used in both PyTorch and TensorFlow through the HuggingFace Transformers package.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import TFAutoModel, AutoModel, AutoTokenizer
|
||||||
|
|
||||||
|
# TensorFlow
|
||||||
|
model = TFAutoModel.from_pretrained('jcblaise/bert-tagalog-base-cased-WWM', from_pt=True)
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/bert-tagalog-base-cased-WWM', do_lower_case=False)
|
||||||
|
|
||||||
|
# PyTorch
|
||||||
|
model = AutoModel.from_pretrained('jcblaise/bert-tagalog-base-cased-WWM')
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/bert-tagalog-base-cased-WWM', do_lower_case=False)
|
||||||
|
```
|
||||||
|
Finetuning scripts and other utilities we use for our projects can be found in our centralized repository at https://github.com/jcblaisecruz02/Filipino-Text-Benchmarks
|
||||||
|
|
||||||
|
## Citations
|
||||||
|
All model details and training setups can be found in our papers. If you use our model or find it useful in your projects, please cite our work:
|
||||||
|
|
||||||
|
```
|
||||||
|
@inproceedings{localization2020cruz,
|
||||||
|
title={{Localization of Fake News Detection via Multitask Transfer Learning}},
|
||||||
|
author={Cruz, Jan Christian Blaise and Tan, Julianne Agatha and Cheng, Charibeth},
|
||||||
|
booktitle={Proceedings of The 12th Language Resources and Evaluation Conference},
|
||||||
|
pages={2589--2597},
|
||||||
|
year={2020},
|
||||||
|
url={https://www.aclweb.org/anthology/2020.lrec-1.315}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2020establishing,
|
||||||
|
title={Establishing Baselines for Text Classification in Low-Resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:2005.02068},
|
||||||
|
year={2020}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2019evaluating,
|
||||||
|
title={Evaluating Language Model Finetuning Techniques for Low-resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:1907.00409},
|
||||||
|
year={2019}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data and Other Resources
|
||||||
|
Data used to train this model as well as other benchmark datasets in Filipino can be found in my website at https://blaisecruz.com
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
If you have questions, concerns, or if you just want to chat about NLP and low-resource languages in general, you may reach me through my work email at jan_christian_cruz@dlsu.edu.ph
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
---
|
||||||
|
language: tl
|
||||||
|
tags:
|
||||||
|
- bert
|
||||||
|
- tagalog
|
||||||
|
- filipino
|
||||||
|
license: gpl-3.0
|
||||||
|
inference: false
|
||||||
|
---
|
||||||
|
|
||||||
|
# BERT Tagalog Base Cased
|
||||||
|
Tagalog version of BERT trained on a large preprocessed text corpus scraped and sourced from the internet. This model is part of a larger research project. We open-source the model to allow greater usage within the Filipino NLP community.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
The model can be loaded and used in both PyTorch and TensorFlow through the HuggingFace Transformers package.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import TFAutoModel, AutoModel, AutoTokenizer
|
||||||
|
|
||||||
|
# TensorFlow
|
||||||
|
model = TFAutoModel.from_pretrained('jcblaise/bert-tagalog-base-cased', from_pt=True)
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/bert-tagalog-base-cased', do_lower_case=False)
|
||||||
|
|
||||||
|
# PyTorch
|
||||||
|
model = AutoModel.from_pretrained('jcblaise/bert-tagalog-base-cased')
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/bert-tagalog-base-cased', do_lower_case=False)
|
||||||
|
```
|
||||||
|
Finetuning scripts and other utilities we use for our projects can be found in our centralized repository at https://github.com/jcblaisecruz02/Filipino-Text-Benchmarks
|
||||||
|
|
||||||
|
## Citations
|
||||||
|
All model details and training setups can be found in our papers. If you use our model or find it useful in your projects, please cite our work:
|
||||||
|
|
||||||
|
```
|
||||||
|
@inproceedings{localization2020cruz,
|
||||||
|
title={{Localization of Fake News Detection via Multitask Transfer Learning}},
|
||||||
|
author={Cruz, Jan Christian Blaise and Tan, Julianne Agatha and Cheng, Charibeth},
|
||||||
|
booktitle={Proceedings of The 12th Language Resources and Evaluation Conference},
|
||||||
|
pages={2589--2597},
|
||||||
|
year={2020},
|
||||||
|
url={https://www.aclweb.org/anthology/2020.lrec-1.315}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2020establishing,
|
||||||
|
title={Establishing Baselines for Text Classification in Low-Resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:2005.02068},
|
||||||
|
year={2020}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2019evaluating,
|
||||||
|
title={Evaluating Language Model Finetuning Techniques for Low-resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:1907.00409},
|
||||||
|
year={2019}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data and Other Resources
|
||||||
|
Data used to train this model as well as other benchmark datasets in Filipino can be found in my website at https://blaisecruz.com
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
If you have questions, concerns, or if you just want to chat about NLP and low-resource languages in general, you may reach me through my work email at jan_christian_cruz@dlsu.edu.ph
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
---
|
||||||
|
language: tl
|
||||||
|
tags:
|
||||||
|
- bert
|
||||||
|
- tagalog
|
||||||
|
- filipino
|
||||||
|
license: gpl-3.0
|
||||||
|
inference: false
|
||||||
|
---
|
||||||
|
|
||||||
|
# BERT Tagalog Base Uncased (Whole Word Masking)
|
||||||
|
Tagalog version of BERT trained on a large preprocessed text corpus scraped and sourced from the internet. This model is part of a larger research project. We open-source the model to allow greater usage within the Filipino NLP community. This particular version uses whole word masking.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
The model can be loaded and used in both PyTorch and TensorFlow through the HuggingFace Transformers package.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import TFAutoModel, AutoModel, AutoTokenizer
|
||||||
|
|
||||||
|
# TensorFlow
|
||||||
|
model = TFAutoModel.from_pretrained('jcblaise/bert-tagalog-base-uncased-WWM', from_pt=True)
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/bert-tagalog-base-uncased-WWM', do_lower_case=True)
|
||||||
|
|
||||||
|
# PyTorch
|
||||||
|
model = AutoModel.from_pretrained('jcblaise/bert-tagalog-base-uncased-WWM')
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/bert-tagalog-base-uncased-WWM', do_lower_case=True)
|
||||||
|
```
|
||||||
|
Finetuning scripts and other utilities we use for our projects can be found in our centralized repository at https://github.com/jcblaisecruz02/Filipino-Text-Benchmarks
|
||||||
|
|
||||||
|
## Citations
|
||||||
|
All model details and training setups can be found in our papers. If you use our model or find it useful in your projects, please cite our work:
|
||||||
|
|
||||||
|
```
|
||||||
|
@inproceedings{localization2020cruz,
|
||||||
|
title={{Localization of Fake News Detection via Multitask Transfer Learning}},
|
||||||
|
author={Cruz, Jan Christian Blaise and Tan, Julianne Agatha and Cheng, Charibeth},
|
||||||
|
booktitle={Proceedings of The 12th Language Resources and Evaluation Conference},
|
||||||
|
pages={2589--2597},
|
||||||
|
year={2020},
|
||||||
|
url={https://www.aclweb.org/anthology/2020.lrec-1.315}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2020establishing,
|
||||||
|
title={Establishing Baselines for Text Classification in Low-Resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:2005.02068},
|
||||||
|
year={2020}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2019evaluating,
|
||||||
|
title={Evaluating Language Model Finetuning Techniques for Low-resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:1907.00409},
|
||||||
|
year={2019}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data and Other Resources
|
||||||
|
Data used to train this model as well as other benchmark datasets in Filipino can be found in my website at https://blaisecruz.com
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
If you have questions, concerns, or if you just want to chat about NLP and low-resource languages in general, you may reach me through my work email at jan_christian_cruz@dlsu.edu.ph
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
---
|
||||||
|
language: tl
|
||||||
|
tags:
|
||||||
|
- bert
|
||||||
|
- tagalog
|
||||||
|
- filipino
|
||||||
|
license: gpl-3.0
|
||||||
|
inference: false
|
||||||
|
---
|
||||||
|
|
||||||
|
# BERT Tagalog Base Uncased
|
||||||
|
Tagalog version of BERT trained on a large preprocessed text corpus scraped and sourced from the internet. This model is part of a larger research project. We open-source the model to allow greater usage within the Filipino NLP community.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
The model can be loaded and used in both PyTorch and TensorFlow through the HuggingFace Transformers package.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import TFAutoModel, AutoModel, AutoTokenizer
|
||||||
|
|
||||||
|
# TensorFlow
|
||||||
|
model = TFAutoModel.from_pretrained('jcblaise/bert-tagalog-base-uncased', from_pt=True)
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/bert-tagalog-base-uncased', do_lower_case=True)
|
||||||
|
|
||||||
|
# PyTorch
|
||||||
|
model = AutoModel.from_pretrained('jcblaise/bert-tagalog-base-uncased')
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/bert-tagalog-base-uncased', do_lower_case=True)
|
||||||
|
```
|
||||||
|
Finetuning scripts and other utilities we use for our projects can be found in our centralized repository at https://github.com/jcblaisecruz02/Filipino-Text-Benchmarks
|
||||||
|
|
||||||
|
## Citations
|
||||||
|
All model details and training setups can be found in our papers. If you use our model or find it useful in your projects, please cite our work:
|
||||||
|
|
||||||
|
```
|
||||||
|
@inproceedings{localization2020cruz,
|
||||||
|
title={{Localization of Fake News Detection via Multitask Transfer Learning}},
|
||||||
|
author={Cruz, Jan Christian Blaise and Tan, Julianne Agatha and Cheng, Charibeth},
|
||||||
|
booktitle={Proceedings of The 12th Language Resources and Evaluation Conference},
|
||||||
|
pages={2589--2597},
|
||||||
|
year={2020},
|
||||||
|
url={https://www.aclweb.org/anthology/2020.lrec-1.315}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2020establishing,
|
||||||
|
title={Establishing Baselines for Text Classification in Low-Resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:2005.02068},
|
||||||
|
year={2020}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2019evaluating,
|
||||||
|
title={Evaluating Language Model Finetuning Techniques for Low-resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:1907.00409},
|
||||||
|
year={2019}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data and Other Resources
|
||||||
|
Data used to train this model as well as other benchmark datasets in Filipino can be found in my website at https://blaisecruz.com
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
If you have questions, concerns, or if you just want to chat about NLP and low-resource languages in general, you may reach me through my work email at jan_christian_cruz@dlsu.edu.ph
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
---
|
||||||
|
language: tl
|
||||||
|
tags:
|
||||||
|
- distilbert
|
||||||
|
- bert
|
||||||
|
- tagalog
|
||||||
|
- filipino
|
||||||
|
license: gpl-3.0
|
||||||
|
inference: false
|
||||||
|
---
|
||||||
|
|
||||||
|
# DistilBERT Tagalog Base Cased
|
||||||
|
Tagalog version of DistilBERT, distilled from [`bert-tagalog-base-cased`](https://huggingface.co/jcblaise/bert-tagalog-base-cased). This model is part of a larger research project. We open-source the model to allow greater usage within the Filipino NLP community.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
The model can be loaded and used in both PyTorch and TensorFlow through the HuggingFace Transformers package.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import TFAutoModel, AutoModel, AutoTokenizer
|
||||||
|
|
||||||
|
# TensorFlow
|
||||||
|
model = TFAutoModel.from_pretrained('jcblaise/distilbert-tagalog-base-cased', from_pt=True)
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/distilbert-tagalog-base-cased', do_lower_case=False)
|
||||||
|
|
||||||
|
# PyTorch
|
||||||
|
model = AutoModel.from_pretrained('jcblaise/distilbert-tagalog-base-cased')
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained('jcblaise/distilbert-tagalog-base-cased', do_lower_case=False)
|
||||||
|
```
|
||||||
|
Finetuning scripts and other utilities we use for our projects can be found in our centralized repository at https://github.com/jcblaisecruz02/Filipino-Text-Benchmarks
|
||||||
|
|
||||||
|
## Citations
|
||||||
|
All model details and training setups can be found in our papers. If you use our model or find it useful in your projects, please cite our work:
|
||||||
|
|
||||||
|
```
|
||||||
|
@inproceedings{localization2020cruz,
|
||||||
|
title={{Localization of Fake News Detection via Multitask Transfer Learning}},
|
||||||
|
author={Cruz, Jan Christian Blaise and Tan, Julianne Agatha and Cheng, Charibeth},
|
||||||
|
booktitle={Proceedings of The 12th Language Resources and Evaluation Conference},
|
||||||
|
pages={2589--2597},
|
||||||
|
year={2020},
|
||||||
|
url={https://www.aclweb.org/anthology/2020.lrec-1.315}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2020establishing,
|
||||||
|
title={Establishing Baselines for Text Classification in Low-Resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:2005.02068},
|
||||||
|
year={2020}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{cruz2019evaluating,
|
||||||
|
title={Evaluating Language Model Finetuning Techniques for Low-resource Languages},
|
||||||
|
author={Cruz, Jan Christian Blaise and Cheng, Charibeth},
|
||||||
|
journal={arXiv preprint arXiv:1907.00409},
|
||||||
|
year={2019}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data and Other Resources
|
||||||
|
Data used to train this model as well as other benchmark datasets in Filipino can be found in my website at https://blaisecruz.com
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
If you have questions, concerns, or if you just want to chat about NLP and low-resource languages in general, you may reach me through my work email at jan_christian_cruz@dlsu.edu.ph
|
||||||
@@ -9,6 +9,7 @@ datasets:
|
|||||||
- xnli
|
- xnli
|
||||||
widget:
|
widget:
|
||||||
- text: "За кого вы голосуете в 2020 году? <sep> This text is about politique."
|
- text: "За кого вы голосуете в 2020 году? <sep> This text is about politique."
|
||||||
|
license: mit
|
||||||
---
|
---
|
||||||
|
|
||||||
# xlm-roberta-large-xnli
|
# xlm-roberta-large-xnli
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
---
|
||||||
|
language: si
|
||||||
|
tags:
|
||||||
|
- SinhalaBERTo
|
||||||
|
- Sinhala
|
||||||
|
- roberta
|
||||||
|
datasets:
|
||||||
|
- oscar
|
||||||
|
---
|
||||||
|
### Overview
|
||||||
|
|
||||||
|
This is a slightly smaller model trained on [OSCAR](https://oscar-corpus.com/) Sinhala dedup dataset. As Sinhala is one of those low resource languages, there are only a handful of models been trained. So, this would be a great place to start training for more downstream tasks.
|
||||||
|
|
||||||
|
## Model Specification
|
||||||
|
|
||||||
|
|
||||||
|
The model chosen for training is [Roberta](https://arxiv.org/abs/1907.11692) with the following specifications:
|
||||||
|
1. vocab_size=52000
|
||||||
|
2. max_position_embeddings=514
|
||||||
|
3. num_attention_heads=12
|
||||||
|
4. num_hidden_layers=6
|
||||||
|
5. type_vocab_size=1
|
||||||
|
|
||||||
|
## How to Use
|
||||||
|
You can use this model directly with a pipeline for masked language modeling:
|
||||||
|
|
||||||
|
```py
|
||||||
|
from transformers import AutoTokenizer, AutoModelWithLMHead, pipeline
|
||||||
|
|
||||||
|
model = BertForMaskedLM.from_pretrained("keshan/SinhalaBERTo")
|
||||||
|
tokenizer = BertTokenizer.from_pretrained("keshan/SinhalaBERTo")
|
||||||
|
|
||||||
|
fill_mask = pipeline('fill-mask', model=model, tokenizer=tokenizer)
|
||||||
|
|
||||||
|
fill_mask("මම ගෙදර <mask>.")
|
||||||
|
|
||||||
|
```
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
---
|
||||||
|
language: is
|
||||||
|
datasets:
|
||||||
|
- Icelandic portion of the OSCAR corpus from INRIA
|
||||||
|
- oscar
|
||||||
|
---
|
||||||
|
|
||||||
|
# IsRoBERTa a RoBERTa-like masked language model
|
||||||
|
|
||||||
|
Probably the first icelandic transformer language model!
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
**Language:** Icelandic
|
||||||
|
**Downstream-task:** masked-lm
|
||||||
|
**Training data:** OSCAR corpus
|
||||||
|
**Code:** See [here](https://github.com/neurocode-io/icelandic-language-model)
|
||||||
|
**Infrastructure**: 1x Nvidia K80
|
||||||
|
|
||||||
|
## Hyperparameters
|
||||||
|
|
||||||
|
```
|
||||||
|
per_device_train_batch_size = 48
|
||||||
|
n_epochs = 1
|
||||||
|
vocab_size = 52.000
|
||||||
|
max_position_embeddings = 514
|
||||||
|
num_attention_heads = 12
|
||||||
|
num_hidden_layers = 6
|
||||||
|
type_vocab_size = 1
|
||||||
|
learning_rate=0.00005
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### In Transformers
|
||||||
|
```python
|
||||||
|
from transformers import (
|
||||||
|
pipeline,
|
||||||
|
AutoTokenizer,
|
||||||
|
AutoModelWithLMHead
|
||||||
|
)
|
||||||
|
|
||||||
|
model_name = "neurocode/IsRoBERTa"
|
||||||
|
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
||||||
|
model = AutoModelWithLMHead.from_pretrained(model_name)
|
||||||
|
>>> fill_mask = pipeline(
|
||||||
|
... "fill-mask",
|
||||||
|
... model=model,
|
||||||
|
... tokenizer=tokenizer
|
||||||
|
... )
|
||||||
|
>>> result = fill_mask("Hann fór út að <mask>.")
|
||||||
|
>>> result
|
||||||
|
[
|
||||||
|
{'sequence': '<s>Hann fór út að nýju.</s>', 'score': 0.03395755589008331, 'token': 2219, 'token_str': 'Ġnýju'},
|
||||||
|
{'sequence': '<s>Hann fór út að undanförnu.</s>', 'score': 0.029087543487548828, 'token': 7590, 'token_str': 'Ġundanförnu'},
|
||||||
|
{'sequence': '<s>Hann fór út að lokum.</s>', 'score': 0.024420788511633873, 'token': 4384, 'token_str': 'Ġlokum'},
|
||||||
|
{'sequence': '<s>Hann fór út að þessu.</s>', 'score': 0.021231256425380707, 'token': 921, 'token_str': 'Ġþessu'},
|
||||||
|
{'sequence': '<s>Hann fór út að honum.</s>', 'score': 0.0205782949924469, 'token': 1136, 'token_str': 'Ġhonum'}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Authors
|
||||||
|
Bobby Donchev: `contact [at] donchev.is`
|
||||||
|
Elena Cramer: `elena.cramer [at] neurocode.io`
|
||||||
|
|
||||||
|
## About us
|
||||||
|
|
||||||
|
We bring AI software for our customers live
|
||||||
|
Our focus: AI software development
|
||||||
|
|
||||||
|
Get in touch:
|
||||||
|
[LinkedIn](https://de.linkedin.com/company/neurocodeio) | [Website](https://neurocode.io)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
language: el
|
||||||
|
---
|
||||||
|
|
||||||
|
## gpt2-greek
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
---
|
||||||
|
language: en
|
||||||
|
tags:
|
||||||
|
- legal
|
||||||
|
---
|
||||||
|
|
||||||
|
# LEGAL-BERT: The Muppets straight out of Law School
|
||||||
|
|
||||||
|
<img align="left" src="https://i.ibb.co/p3kQ7Rw/Screenshot-2020-10-06-at-12-16-36-PM.png" width="100"/>
|
||||||
|
|
||||||
|
LEGAL-BERT is a family of BERT models for the legal domain, intended to assist legal NLP research, computational law, and legal technology applications. To pre-train the different variations of LEGAL-BERT, we collected 12 GB of diverse English legal text from several fields (e.g., legislation, court cases, contracts) scraped from publicly available resources. Sub-domains variants (CONTRACTS-, EURLEX-, ECHR-) and/or general LEGAL-BERT perform better than using BERT out of the box for domain-specific tasks. A light-weight model (33% the size of BERT-BASE) pre-trained from scratch on legal data with competitive perfomance is also available.
|
||||||
|
<br/><br/><br/><br/>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
I. Chalkidis, M. Fergadiotis, P. Malakasiotis, N. Aletras and I. Androutsopoulos. "LEGAL-BERT: The Muppets straight out of Law School". In Findings of Empirical Methods in Natural Language Processing (EMNLP 2020) (Short Papers), to be held online, 2020. (https://arxiv.org/abs/2010.02559)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Pre-training corpora
|
||||||
|
|
||||||
|
The pre-training corpora of LEGAL-BERT include:
|
||||||
|
|
||||||
|
* 116,062 documents of EU legislation, publicly available from EURLEX (http://eur-lex.europa.eu), the repository of EU Law running under the EU Publication Office.
|
||||||
|
|
||||||
|
* 61,826 documents of UK legislation, publicly available from the UK legislation portal (http://www.legislation.gov.uk).
|
||||||
|
|
||||||
|
* 19,867 cases from European Court of Justice (ECJ), also available from EURLEX.
|
||||||
|
|
||||||
|
* 12,554 cases from HUDOC, the repository of the European Court of Human Rights (ECHR) (http://hudoc.echr.coe.int/eng).
|
||||||
|
|
||||||
|
* 164,141 cases from various courts across the USA, hosted in the Case Law Access Project portal (https://case.law).
|
||||||
|
|
||||||
|
* 76,366 US contracts from EDGAR, the database of US Securities and Exchange Commission (SECOM) (https://www.sec.gov/edgar.shtml).
|
||||||
|
|
||||||
|
## Pre-training details
|
||||||
|
|
||||||
|
* We trained BERT using the official code provided in Google BERT's github repository (https://github.com/google-research/bert).
|
||||||
|
* We released a model similar to the English BERT-BASE model (12-layer, 768-hidden, 12-heads, 110M parameters).
|
||||||
|
* We chose to follow the same training set-up: 1 million training steps with batches of 256 sequences of length 512 with an initial learning rate 1e-4.
|
||||||
|
* We were able to use a single Google Cloud TPU v3-8 provided for free from [TensorFlow Research Cloud (TFRC)](https://www.tensorflow.org/tfrc), while also utilizing [GCP research credits](https://edu.google.com/programs/credits/research). Huge thanks to both Google programs for supporting us!
|
||||||
|
* Part of LEGAL-BERT is a light-weight model pre-trained from scratch on legal data, which achieves comparable performance to larger models, while being much more efficient (approximately 4 times faster) with a smaller environmental footprint.
|
||||||
|
## Models list
|
||||||
|
|
||||||
|
| Model name | Model Path | Training corpora |
|
||||||
|
| ------------------- | ------------------------------------ | ------------------- |
|
||||||
|
| CONTRACTS-BERT-BASE | `nlpaueb/bert-base-uncased-contracts` | US contracts |
|
||||||
|
| EURLEX-BERT-BASE | `nlpaueb/bert-base-uncased-eurlex` | EU legislation |
|
||||||
|
| ECHR-BERT-BASE | `nlpaueb/bert-base-uncased-echr` | ECHR cases |
|
||||||
|
| LEGAL-BERT-BASE | `nlpaueb/legal-bert-base-uncased` | All |
|
||||||
|
| LEGAL-BERT-SMALL | `nlpaueb/legal-bert-small-uncased` | All |
|
||||||
|
|
||||||
|
## Load Pretrained Model
|
||||||
|
|
||||||
|
```python
|
||||||
|
from transformers import AutoTokenizer, AutoModel
|
||||||
|
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained("nlpaueb/legal-bert-base-uncased")
|
||||||
|
model = AutoModel.from_pretrained("nlpaueb/legal-bert-base-uncased")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Use LEBAL-BERT variants as Language Models
|
||||||
|
|
||||||
|
| Corpus | Model | Masked token | Predictions |
|
||||||
|
| --------------------------------- | ---------------------------------- | ------------ | ------------ |
|
||||||
|
| | **BERT-BASE-UNCASED** |
|
||||||
|
| (Contracts) | This [MASK] Agreement is between General Motors and John Murray . | employment | ('new', '0.09'), ('current', '0.04'), ('proposed', '0.03'), ('marketing', '0.03'), ('joint', '0.02')
|
||||||
|
| (ECHR) | The applicant submitted that her husband was subjected to treatment amounting to [MASK] whilst in the custody of Adana Security Directorate | torture | ('torture', '0.32'), ('rape', '0.22'), ('abuse', '0.14'), ('death', '0.04'), ('violence', '0.03')
|
||||||
|
| (EURLEX) | Establishing a system for the identification and registration of [MASK] animals and regarding the labelling of beef and beef products . | bovine | ('farm', '0.25'), ('livestock', '0.08'), ('draft', '0.06'), ('domestic', '0.05'), ('wild', '0.05')
|
||||||
|
| | **CONTRACTS-BERT-BASE** |
|
||||||
|
| (Contracts) | This [MASK] Agreement is between General Motors and John Murray . | employment | ('letter', '0.38'), ('dealer', '0.04'), ('employment', '0.03'), ('award', '0.03'), ('contribution', '0.02')
|
||||||
|
| (ECHR) | The applicant submitted that her husband was subjected to treatment amounting to [MASK] whilst in the custody of Adana Security Directorate | torture | ('death', '0.39'), ('imprisonment', '0.07'), ('contempt', '0.05'), ('being', '0.03'), ('crime', '0.02')
|
||||||
|
| (EURLEX) | Establishing a system for the identification and registration of [MASK] animals and regarding the labelling of beef and beef products . | bovine | (('domestic', '0.18'), ('laboratory', '0.07'), ('household', '0.06'), ('personal', '0.06'), ('the', '0.04')
|
||||||
|
| | **EURLEX-BERT-BASE** |
|
||||||
|
| (Contracts) | This [MASK] Agreement is between General Motors and John Murray . | employment | ('supply', '0.11'), ('cooperation', '0.08'), ('service', '0.07'), ('licence', '0.07'), ('distribution', '0.05')
|
||||||
|
| (ECHR) | The applicant submitted that her husband was subjected to treatment amounting to [MASK] whilst in the custody of Adana Security Directorate | torture | ('torture', '0.66'), ('death', '0.07'), ('imprisonment', '0.07'), ('murder', '0.04'), ('rape', '0.02')
|
||||||
|
| (EURLEX) | Establishing a system for the identification and registration of [MASK] animals and regarding the labelling of beef and beef products . | bovine | ('live', '0.43'), ('pet', '0.28'), ('certain', '0.05'), ('fur', '0.03'), ('the', '0.02')
|
||||||
|
| | **ECHR-BERT-BASE** |
|
||||||
|
| (Contracts) | This [MASK] Agreement is between General Motors and John Murray . | employment | ('second', '0.24'), ('latter', '0.10'), ('draft', '0.05'), ('bilateral', '0.05'), ('arbitration', '0.04')
|
||||||
|
| (ECHR) | The applicant submitted that her husband was subjected to treatment amounting to [MASK] whilst in the custody of Adana Security Directorate | torture | ('torture', '0.99'), ('death', '0.01'), ('inhuman', '0.00'), ('beating', '0.00'), ('rape', '0.00')
|
||||||
|
| (EURLEX) | Establishing a system for the identification and registration of [MASK] animals and regarding the labelling of beef and beef products . | bovine | ('pet', '0.17'), ('all', '0.12'), ('slaughtered', '0.10'), ('domestic', '0.07'), ('individual', '0.05')
|
||||||
|
| | **LEGAL-BERT-BASE** |
|
||||||
|
| (Contracts) | This [MASK] Agreement is between General Motors and John Murray . | employment | ('settlement', '0.26'), ('letter', '0.23'), ('dealer', '0.04'), ('master', '0.02'), ('supplemental', '0.02')
|
||||||
|
| (ECHR) | The applicant submitted that her husband was subjected to treatment amounting to [MASK] whilst in the custody of Adana Security Directorate | torture | ('torture', '1.00'), ('detention', '0.00'), ('arrest', '0.00'), ('rape', '0.00'), ('death', '0.00')
|
||||||
|
| (EURLEX) | Establishing a system for the identification and registration of [MASK] animals and regarding the labelling of beef and beef products . | bovine | ('live', '0.67'), ('beef', '0.17'), ('farm', '0.03'), ('pet', '0.02'), ('dairy', '0.01')
|
||||||
|
| | **LEGAL-BERT-SMALL** |
|
||||||
|
| (Contracts) | This [MASK] Agreement is between General Motors and John Murray . | employment | ('license', '0.09'), ('transition', '0.08'), ('settlement', '0.04'), ('consent', '0.03'), ('letter', '0.03')
|
||||||
|
| (ECHR) | The applicant submitted that her husband was subjected to treatment amounting to [MASK] whilst in the custody of Adana Security Directorate | torture | ('torture', '0.59'), ('pain', '0.05'), ('ptsd', '0.05'), ('death', '0.02'), ('tuberculosis', '0.02')
|
||||||
|
| (EURLEX) | Establishing a system for the identification and registration of [MASK] animals and regarding the labelling of beef and beef products . | bovine | ('all', '0.08'), ('live', '0.07'), ('certain', '0.07'), ('the', '0.07'), ('farm', '0.05')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Evaluation on downstream tasks
|
||||||
|
|
||||||
|
Consider the experiments in the article "LEGAL-BERT: The Muppets straight out of Law School". Chalkidis et al., 2018, (https://arxiv.org/abs/2010.02559)
|
||||||
|
|
||||||
|
## Author
|
||||||
|
|
||||||
|
Ilias Chalkidis on behalf of [AUEB's Natural Language Processing Group](http://nlp.cs.aueb.gr)
|
||||||
|
|
||||||
|
| Github: [@ilias.chalkidis](https://github.com/seolhokim) | Twitter: [@KiddoThe2B](https://twitter.com/KiddoThe2B) |
|
||||||
@@ -1658,7 +1658,7 @@
|
|||||||
" 'add_final_layer_norm': False,\n",
|
" 'add_final_layer_norm': False,\n",
|
||||||
" 'attention_dropout': 0.0,\n",
|
" 'attention_dropout': 0.0,\n",
|
||||||
" 'bos_token_id': 0,\n",
|
" 'bos_token_id': 0,\n",
|
||||||
" 'classif_dropout': 0.0,\n",
|
" 'classifier_dropout': 0.0,\n",
|
||||||
" 'd_model': 1024,\n",
|
" 'd_model': 1024,\n",
|
||||||
" 'decoder_attention_heads': 16,\n",
|
" 'decoder_attention_heads': 16,\n",
|
||||||
" 'decoder_ffn_dim': 4096,\n",
|
" 'decoder_ffn_dim': 4096,\n",
|
||||||
|
|||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# this script builds a small sample spm file tests/fixtures/test_sentencepiece_no_bos.model, with features needed by pegasus
|
||||||
|
|
||||||
|
# 1. pip install sentencepiece
|
||||||
|
#
|
||||||
|
# 2. wget https://raw.githubusercontent.com/google/sentencepiece/master/data/botchan.txt
|
||||||
|
|
||||||
|
# 3. build
|
||||||
|
import sentencepiece as spm
|
||||||
|
|
||||||
|
# pegasus:
|
||||||
|
# 1. no bos
|
||||||
|
# 2. eos_id is 1
|
||||||
|
# 3. unk_id is 2
|
||||||
|
# build a sample spm file accordingly
|
||||||
|
spm.SentencePieceTrainer.train('--input=botchan.txt --model_prefix=test_sentencepiece_no_bos --bos_id=-1 --unk_id=2 --eos_id=1 --vocab_size=1000')
|
||||||
|
|
||||||
|
# 4. now update the fixture
|
||||||
|
# mv test_sentencepiece_no_bos.model ../../tests/fixtures/
|
||||||
@@ -3,7 +3,10 @@ Simple check list from AllenNLP repo: https://github.com/allenai/allennlp/blob/m
|
|||||||
|
|
||||||
To create the package for pypi.
|
To create the package for pypi.
|
||||||
|
|
||||||
1. Change the version in __init__.py, setup.py as well as docs/source/conf.py.
|
1. Change the version in __init__.py, setup.py as well as docs/source/conf.py. Remove the master from the links in
|
||||||
|
the new models of the README:
|
||||||
|
(https://huggingface.co/transformers/master/model_doc/ -> https://huggingface.co/transformers/model_doc/)
|
||||||
|
then run `make fix-copies` to fix the index of the documentation.
|
||||||
|
|
||||||
2. Unpin specific versions from setup.py that use a git install.
|
2. Unpin specific versions from setup.py that use a git install.
|
||||||
|
|
||||||
@@ -111,7 +114,7 @@ setup(
|
|||||||
packages=find_packages("src"),
|
packages=find_packages("src"),
|
||||||
install_requires=[
|
install_requires=[
|
||||||
"numpy",
|
"numpy",
|
||||||
"tokenizers == 0.9.0.rc1",
|
"tokenizers == 0.9.0.rc2",
|
||||||
# dataclasses for Python versions that don't have it
|
# dataclasses for Python versions that don't have it
|
||||||
"dataclasses;python_version<'3.7'",
|
"dataclasses;python_version<'3.7'",
|
||||||
# utilities from PyPA to e.g. compare versions
|
# utilities from PyPA to e.g. compare versions
|
||||||
@@ -131,9 +134,7 @@ setup(
|
|||||||
"sacremoses",
|
"sacremoses",
|
||||||
],
|
],
|
||||||
extras_require=extras,
|
extras_require=extras,
|
||||||
entry_points={
|
entry_points={"console_scripts": ["transformers-cli=transformers.commands.transformers_cli:main"]},
|
||||||
"console_scripts": ["transformers-cli=transformers.commands.transformers_cli:main"]
|
|
||||||
},
|
|
||||||
python_requires=">=3.6.0",
|
python_requires=">=3.6.0",
|
||||||
classifiers=[
|
classifiers=[
|
||||||
"Development Status :: 5 - Production/Stable",
|
"Development Status :: 5 - Production/Stable",
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ from .configuration_auto import ALL_PRETRAINED_CONFIG_ARCHIVE_MAP, CONFIG_MAPPIN
|
|||||||
from .configuration_bart import BartConfig
|
from .configuration_bart import BartConfig
|
||||||
from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig
|
from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig
|
||||||
from .configuration_bert_generation import BertGenerationConfig
|
from .configuration_bert_generation import BertGenerationConfig
|
||||||
|
from .configuration_blenderbot import BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig
|
||||||
from .configuration_camembert import CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CamembertConfig
|
from .configuration_camembert import CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CamembertConfig
|
||||||
from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig
|
from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig
|
||||||
from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig
|
from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig
|
||||||
@@ -107,8 +108,6 @@ from .file_utils import (
|
|||||||
is_tf_available,
|
is_tf_available,
|
||||||
is_torch_available,
|
is_torch_available,
|
||||||
is_torch_tpu_available,
|
is_torch_tpu_available,
|
||||||
is_sentencepiece_available,
|
|
||||||
is_tokenizers_available,
|
|
||||||
)
|
)
|
||||||
from .hf_argparser import HfArgumentParser
|
from .hf_argparser import HfArgumentParser
|
||||||
|
|
||||||
@@ -160,6 +159,7 @@ from .tokenization_bert import BasicTokenizer, BertTokenizer, BertTokenizerFast,
|
|||||||
from .tokenization_bert_generation import BertGenerationTokenizer
|
from .tokenization_bert_generation import BertGenerationTokenizer
|
||||||
from .tokenization_bert_japanese import BertJapaneseTokenizer, CharacterTokenizer, MecabTokenizer
|
from .tokenization_bert_japanese import BertJapaneseTokenizer, CharacterTokenizer, MecabTokenizer
|
||||||
from .tokenization_bertweet import BertweetTokenizer
|
from .tokenization_bertweet import BertweetTokenizer
|
||||||
|
from .tokenization_blenderbot import BlenderbotSmallTokenizer, BlenderbotTokenizer
|
||||||
from .tokenization_camembert import CamembertTokenizer, CamembertTokenizerFast
|
from .tokenization_camembert import CamembertTokenizer, CamembertTokenizerFast
|
||||||
from .tokenization_ctrl import CTRLTokenizer
|
from .tokenization_ctrl import CTRLTokenizer
|
||||||
from .tokenization_deberta import DebertaTokenizer
|
from .tokenization_deberta import DebertaTokenizer
|
||||||
@@ -207,7 +207,15 @@ from .tokenization_xlm_roberta import XLMRobertaTokenizer, XLMRobertaTokenizerFa
|
|||||||
from .tokenization_xlnet import SPIECE_UNDERLINE, XLNetTokenizer, XLNetTokenizerFast
|
from .tokenization_xlnet import SPIECE_UNDERLINE, XLNetTokenizer, XLNetTokenizerFast
|
||||||
|
|
||||||
# Trainer
|
# Trainer
|
||||||
from .trainer_utils import EvalPrediction, TrainerState, set_seed
|
from .trainer_callback import (
|
||||||
|
DefaultFlowCallback,
|
||||||
|
PrinterCallback,
|
||||||
|
ProgressCallback,
|
||||||
|
TrainerCallback,
|
||||||
|
TrainerControl,
|
||||||
|
TrainerState,
|
||||||
|
)
|
||||||
|
from .trainer_utils import EvalPrediction, EvaluationStrategy, set_seed
|
||||||
from .training_args import TrainingArguments
|
from .training_args import TrainingArguments
|
||||||
from .training_args_tf import TFTrainingArguments
|
from .training_args_tf import TFTrainingArguments
|
||||||
from .utils import logging
|
from .utils import logging
|
||||||
@@ -303,6 +311,7 @@ if is_torch_available():
|
|||||||
BertGenerationEncoder,
|
BertGenerationEncoder,
|
||||||
load_tf_weights_in_bert_generation,
|
load_tf_weights_in_bert_generation,
|
||||||
)
|
)
|
||||||
|
from .modeling_blenderbot import BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForConditionalGeneration
|
||||||
from .modeling_camembert import (
|
from .modeling_camembert import (
|
||||||
CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
|
CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||||
CamembertForCausalLM,
|
CamembertForCausalLM,
|
||||||
@@ -377,6 +386,7 @@ if is_torch_available():
|
|||||||
from .modeling_gpt2 import (
|
from .modeling_gpt2 import (
|
||||||
GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
|
GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||||
GPT2DoubleHeadsModel,
|
GPT2DoubleHeadsModel,
|
||||||
|
GPT2ForSequenceClassification,
|
||||||
GPT2LMHeadModel,
|
GPT2LMHeadModel,
|
||||||
GPT2Model,
|
GPT2Model,
|
||||||
GPT2PreTrainedModel,
|
GPT2PreTrainedModel,
|
||||||
@@ -530,7 +540,8 @@ if is_torch_available():
|
|||||||
from .tokenization_marian import MarianTokenizer
|
from .tokenization_marian import MarianTokenizer
|
||||||
|
|
||||||
# Trainer
|
# Trainer
|
||||||
from .trainer import EvalPrediction, Trainer, set_seed, torch_distributed_zero_first
|
from .trainer import Trainer
|
||||||
|
from .trainer_pt_utils import torch_distributed_zero_first
|
||||||
else:
|
else:
|
||||||
from .utils.dummy_pt_objects import *
|
from .utils.dummy_pt_objects import *
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertCo
|
|||||||
from .configuration_bart import BART_PRETRAINED_CONFIG_ARCHIVE_MAP, BartConfig
|
from .configuration_bart import BART_PRETRAINED_CONFIG_ARCHIVE_MAP, BartConfig
|
||||||
from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig
|
from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig
|
||||||
from .configuration_bert_generation import BertGenerationConfig
|
from .configuration_bert_generation import BertGenerationConfig
|
||||||
|
from .configuration_blenderbot import BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig
|
||||||
from .configuration_camembert import CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CamembertConfig
|
from .configuration_camembert import CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CamembertConfig
|
||||||
from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig
|
from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig
|
||||||
from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig
|
from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig
|
||||||
@@ -58,6 +59,7 @@ ALL_PRETRAINED_CONFIG_ARCHIVE_MAP = dict(
|
|||||||
for pretrained_map in [
|
for pretrained_map in [
|
||||||
BERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
BERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||||
BART_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
BART_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||||
|
BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||||
MBART_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
MBART_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||||
OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||||
TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||||
@@ -101,6 +103,7 @@ CONFIG_MAPPING = OrderedDict(
|
|||||||
("marian", MarianConfig),
|
("marian", MarianConfig),
|
||||||
("mbart", MBartConfig),
|
("mbart", MBartConfig),
|
||||||
("bart", BartConfig),
|
("bart", BartConfig),
|
||||||
|
("blenderbot", BlenderbotConfig),
|
||||||
("reformer", ReformerConfig),
|
("reformer", ReformerConfig),
|
||||||
("longformer", LongformerConfig),
|
("longformer", LongformerConfig),
|
||||||
("roberta", RobertaConfig),
|
("roberta", RobertaConfig),
|
||||||
@@ -136,6 +139,7 @@ MODEL_NAMES_MAPPING = OrderedDict(
|
|||||||
("camembert", "CamemBERT"),
|
("camembert", "CamemBERT"),
|
||||||
("xlm-roberta", "XLM-RoBERTa"),
|
("xlm-roberta", "XLM-RoBERTa"),
|
||||||
("pegasus", "Pegasus"),
|
("pegasus", "Pegasus"),
|
||||||
|
("blenderbot", "Blenderbot"),
|
||||||
("marian", "Marian"),
|
("marian", "Marian"),
|
||||||
("mbart", "mBART"),
|
("mbart", "mBART"),
|
||||||
("bart", "BART"),
|
("bart", "BART"),
|
||||||
|
|||||||
@@ -84,6 +84,8 @@ class BartConfig(PretrainedConfig):
|
|||||||
Don't learn positional embeddings, use sinusoidal.
|
Don't learn positional embeddings, use sinusoidal.
|
||||||
add_final_layer_norm (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
add_final_layer_norm (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Why not add another layernorm?
|
Why not add another layernorm?
|
||||||
|
do_blenderbot_90_layernorm (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Blenderbot-90m checkpoint uses `layernorm_embedding` one line earlier in the decoder.
|
||||||
scale_embedding (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
scale_embedding (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Scale embeddings by diving by sqrt(d_model).
|
Scale embeddings by diving by sqrt(d_model).
|
||||||
eos_token_id (:obj:`int`, `optional`, defaults to 2)
|
eos_token_id (:obj:`int`, `optional`, defaults to 2)
|
||||||
@@ -113,7 +115,7 @@ class BartConfig(PretrainedConfig):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
activation_dropout=0.0,
|
activation_dropout=0.0,
|
||||||
extra_pos_embeddings=2, # FIXME(@sshleifer): delete?
|
extra_pos_embeddings=2,
|
||||||
activation_function="gelu",
|
activation_function="gelu",
|
||||||
vocab_size=50265,
|
vocab_size=50265,
|
||||||
d_model=1024,
|
d_model=1024,
|
||||||
@@ -137,6 +139,7 @@ class BartConfig(PretrainedConfig):
|
|||||||
eos_token_id=2,
|
eos_token_id=2,
|
||||||
normalize_before=False,
|
normalize_before=False,
|
||||||
add_final_layer_norm=False,
|
add_final_layer_norm=False,
|
||||||
|
do_blenderbot_90_layernorm=False,
|
||||||
scale_embedding=False,
|
scale_embedding=False,
|
||||||
normalize_embedding=True,
|
normalize_embedding=True,
|
||||||
static_position_embeddings=False,
|
static_position_embeddings=False,
|
||||||
@@ -195,13 +198,16 @@ class BartConfig(PretrainedConfig):
|
|||||||
self.dropout = dropout
|
self.dropout = dropout
|
||||||
|
|
||||||
# Classifier stuff
|
# Classifier stuff
|
||||||
self.classif_dropout = classifier_dropout
|
self.classifier_dropout = classifier_dropout
|
||||||
|
|
||||||
# pos embedding offset
|
# pos embedding offset
|
||||||
self.extra_pos_embeddings = self.pad_token_id + 1
|
self.extra_pos_embeddings = extra_pos_embeddings
|
||||||
|
# bart has a hack that offsets positional embeddings by 2, other models don't do this
|
||||||
|
|
||||||
self.force_bos_token_to_be_generated = force_bos_token_to_be_generated
|
self.force_bos_token_to_be_generated = force_bos_token_to_be_generated
|
||||||
|
|
||||||
|
self.do_blenderbot_90_layernorm = do_blenderbot_90_layernorm
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def num_attention_heads(self) -> int:
|
def num_attention_heads(self) -> int:
|
||||||
return self.encoder_attention_heads
|
return self.encoder_attention_heads
|
||||||
|
|||||||
@@ -0,0 +1,178 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# coding=utf-8
|
||||||
|
# Copyright (c) Facebook, Inc. and Huggingface, 2020
|
||||||
|
#
|
||||||
|
# This source code is licensed under the MIT license found in the;
|
||||||
|
# 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.
|
||||||
|
# LICENSE file in the root directory of this source tree.
|
||||||
|
"""BlenderbotConfig has the same signature as BartConfig. We only rewrite the signature in order to document blenderbot-90M defaults."""
|
||||||
|
from .configuration_bart import BartConfig
|
||||||
|
|
||||||
|
|
||||||
|
BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
||||||
|
"facebook/blenderbot-3B": "https://cdn.huggingface.co/facebook/blenderbot-3B/config.json",
|
||||||
|
"facebook/blenderbot-90M": "https://cdn.huggingface.co/facebook/blenderbot-90M/config.json",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class BlenderbotConfig(BartConfig):
|
||||||
|
r"""
|
||||||
|
This is the configuration class to store the configuration of a :class:`~transformers.BlenderbotForConditionalGeneration`.
|
||||||
|
It inherits from :class:`~transformers.BartConfig` and has the same signature with different defaults.
|
||||||
|
|
||||||
|
Configuration objects inherit from :class:`~transformers.PretrainedConfig` and can be used
|
||||||
|
to control the model outputs. Read the documentation from :class:`~transformers.PretrainedConfig`
|
||||||
|
for more information.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
vocab_size (:obj:`int`, `optional`, defaults to 54944):
|
||||||
|
Vocabulary size of the BERT model. Defines the number of different tokens that can be represented by the
|
||||||
|
:obj:`inputs_ids` passed when calling :class:`~transformers.BlenderbotForConditionalGeneration`.
|
||||||
|
d_model (:obj:`int`, `optional`, defaults to 512):
|
||||||
|
Dimensionality of the layers and the pooler layer.
|
||||||
|
encoder_layers (:obj:`int`, `optional`, defaults to 8):
|
||||||
|
Number of encoder layers, 6 are used for the `blenderbot-90M` model.
|
||||||
|
decoder_layers (:obj:`int`, `optional`, defaults to 8):
|
||||||
|
Number of decoder layers, 6 are used for the `blenderbot-90M` model.
|
||||||
|
encoder_attention_heads (:obj:`int`, `optional`, defaults to 16):
|
||||||
|
Number of attention heads for each attention layer in the Transformer encoder.
|
||||||
|
decoder_attention_heads (:obj:`int`, `optional`, defaults to 16):
|
||||||
|
Number of attention heads for each attention layer in the Transformer decoder.
|
||||||
|
decoder_ffn_dim (:obj:`int`, `optional`, defaults to 2048):
|
||||||
|
Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
|
||||||
|
encoder_ffn_dim (:obj:`int`, `optional`, defaults to 2048):
|
||||||
|
Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
|
||||||
|
activation_function (:obj:`str` or :obj:`function`, `optional`, defaults to :obj:`"gelu"`):
|
||||||
|
The non-linear activation function (function or string) in the encoder and pooler.
|
||||||
|
If string, :obj:`"gelu"`, :obj:`"relu"`, :obj:`"swish"` and :obj:`"gelu_new"` are supported.
|
||||||
|
dropout (:obj:`float`, `optional`, defaults to 0.1):
|
||||||
|
The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler.
|
||||||
|
attention_dropout (:obj:`float`, `optional`, defaults to 0.0):
|
||||||
|
The dropout ratio for the attention probabilities.
|
||||||
|
activation_dropout (:obj:`float`, `optional`, defaults to 0.0):
|
||||||
|
The dropout ratio for activations inside the fully connected layer.
|
||||||
|
classifier_dropout (:obj:`float`, `optional`, defaults to 0.0):
|
||||||
|
The dropout ratio for classifier.
|
||||||
|
max_position_embeddings (:obj:`int`, `optional`, defaults to 512):
|
||||||
|
The maximum sequence length that this model might ever be used with.
|
||||||
|
Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
|
||||||
|
init_std (:obj:`float`, `optional`, defaults to 0.02):
|
||||||
|
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
||||||
|
add_bias_logits (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
This should be completed, specific to marian.
|
||||||
|
normalize_before (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Call layernorm before attention ops.
|
||||||
|
normalize_embedding (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
|
Call layernorm after embeddings.
|
||||||
|
static_position_embeddings (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Don't learn positional embeddings, use sinusoidal.
|
||||||
|
add_final_layer_norm (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Why not add another layernorm?
|
||||||
|
do_blenderbot_90_layernorm (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
|
Blenderbot-90m checkpoint uses `layernorm_embedding` one line earlier in the decoder.
|
||||||
|
scale_embedding (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Scale embeddings by diving by sqrt(d_model).
|
||||||
|
eos_token_id (:obj:`int`, `optional`, defaults to 2)
|
||||||
|
End of stream token id.
|
||||||
|
pad_token_id (:obj:`int`, `optional`, defaults to 1)
|
||||||
|
Padding token id.
|
||||||
|
bos_token_id (:obj:`int`, `optional`, defaults to 0)
|
||||||
|
Beginning of stream token id.
|
||||||
|
encoder_layerdrop: (:obj:`float`, `optional`, defaults to 0.0):
|
||||||
|
The LayerDrop probability for the encoder. See the `LayerDrop paper
|
||||||
|
<see https://arxiv.org/abs/1909.11556>`__ for more details.
|
||||||
|
decoder_layerdrop: (:obj:`float`, `optional`, defaults to 0.0):
|
||||||
|
The LayerDrop probability for the decoder. See the `LayerDrop paper
|
||||||
|
<see https://arxiv.org/abs/1909.11556>`__ for more details.
|
||||||
|
extra_pos_embeddings: (:obj:`int`, `optional`, defaults to 2):
|
||||||
|
How many extra learned positional embeddings to use. Should be set to :obj:`pad_token_id+1`.
|
||||||
|
is_encoder_decoder (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
|
Whether this is an encoder/decoder model.
|
||||||
|
force_bos_token_to_be_generated (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Whether or not to force BOS token to be generated at step 1 (after ``decoder_start_token_id``),
|
||||||
|
"""
|
||||||
|
model_type = "blenderbot"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
activation_dropout=0.0,
|
||||||
|
extra_pos_embeddings=0,
|
||||||
|
activation_function="gelu",
|
||||||
|
vocab_size=54944,
|
||||||
|
d_model=512,
|
||||||
|
encoder_ffn_dim=2048,
|
||||||
|
encoder_layers=8,
|
||||||
|
encoder_attention_heads=16,
|
||||||
|
decoder_ffn_dim=2048,
|
||||||
|
decoder_layers=8,
|
||||||
|
decoder_attention_heads=16,
|
||||||
|
encoder_layerdrop=0.0,
|
||||||
|
decoder_layerdrop=0.0,
|
||||||
|
attention_dropout=0.0,
|
||||||
|
dropout=0.1,
|
||||||
|
max_position_embeddings=512,
|
||||||
|
classifier_dropout=0.0,
|
||||||
|
is_encoder_decoder=True,
|
||||||
|
pad_token_id=1,
|
||||||
|
bos_token_id=0,
|
||||||
|
eos_token_id=2,
|
||||||
|
normalize_before=False,
|
||||||
|
add_final_layer_norm=False,
|
||||||
|
do_blenderbot_90_layernorm=True,
|
||||||
|
scale_embedding=False,
|
||||||
|
normalize_embedding=True,
|
||||||
|
static_position_embeddings=False,
|
||||||
|
add_bias_logits=False,
|
||||||
|
force_bos_token_to_be_generated=False,
|
||||||
|
**common_kwargs
|
||||||
|
):
|
||||||
|
r"""
|
||||||
|
Examples::
|
||||||
|
|
||||||
|
>>> from transformers import BlenderbotConfig
|
||||||
|
>>> config = BlenderbotConfig.from_pretrained('facebook/blenderbot-90M')
|
||||||
|
|
||||||
|
"""
|
||||||
|
if "hidden_size" in common_kwargs:
|
||||||
|
raise ValueError("hidden size is called d_model")
|
||||||
|
super().__init__(
|
||||||
|
pad_token_id=pad_token_id,
|
||||||
|
bos_token_id=bos_token_id,
|
||||||
|
eos_token_id=eos_token_id,
|
||||||
|
is_encoder_decoder=is_encoder_decoder,
|
||||||
|
vocab_size=vocab_size,
|
||||||
|
d_model=d_model,
|
||||||
|
encoder_ffn_dim=encoder_ffn_dim,
|
||||||
|
encoder_layers=encoder_layers,
|
||||||
|
encoder_layerdrop=encoder_layerdrop,
|
||||||
|
encoder_attention_heads=encoder_attention_heads,
|
||||||
|
decoder_layerdrop=decoder_layerdrop,
|
||||||
|
decoder_ffn_dim=decoder_ffn_dim,
|
||||||
|
decoder_layers=decoder_layers,
|
||||||
|
normalize_before=normalize_before,
|
||||||
|
normalize_embedding=normalize_embedding,
|
||||||
|
static_position_embeddings=static_position_embeddings,
|
||||||
|
add_bias_logits=add_bias_logits,
|
||||||
|
force_bos_token_to_be_generated=force_bos_token_to_be_generated,
|
||||||
|
do_blenderbot_90_layernorm=do_blenderbot_90_layernorm,
|
||||||
|
add_final_layer_norm=add_final_layer_norm,
|
||||||
|
scale_embedding=scale_embedding,
|
||||||
|
attention_dropout=attention_dropout,
|
||||||
|
dropout=dropout,
|
||||||
|
classifier_dropout=classifier_dropout,
|
||||||
|
activation_dropout=activation_dropout,
|
||||||
|
max_position_embeddings=max_position_embeddings,
|
||||||
|
extra_pos_embeddings=extra_pos_embeddings,
|
||||||
|
activation_function=activation_function,
|
||||||
|
decoder_attention_heads=decoder_attention_heads,
|
||||||
|
**common_kwargs,
|
||||||
|
)
|
||||||
@@ -39,52 +39,51 @@ class SqueezeBertConfig(PretrainedConfig):
|
|||||||
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
vocab_size (:obj:`int`, optional, defaults to 30522):
|
vocab_size (:obj:`int`, `optional`, defaults to 30522):
|
||||||
Vocabulary size of the SqueezeBERT model. Defines the different tokens that
|
Vocabulary size of the SqueezeBERT model. Defines the number of different tokens that can be
|
||||||
can be represented by the :obj:`inputs_ids` passed to the forward method of :class:`~transformers.SqueezeBertModel`.
|
represented by the :obj:`inputs_ids` passed when calling :class:`~transformers.SqueezeBertModel`.
|
||||||
hidden_size (:obj:`int`, optional, defaults to 512):
|
hidden_size (:obj:`int`, `optional`, defaults to 768):
|
||||||
Dimensionality of the encoder layers and the pooler layer.
|
Dimensionality of the encoder layers and the pooler layer.
|
||||||
num_hidden_layers (:obj:`int`, optional, defaults to 24):
|
num_hidden_layers (:obj:`int`, `optional`, defaults to 12):
|
||||||
Number of hidden layers in the Transformer encoder.
|
Number of hidden layers in the Transformer encoder.
|
||||||
num_attention_heads (:obj:`int`, optional, defaults to 4):
|
num_attention_heads (:obj:`int`, `optional`, defaults to 12):
|
||||||
Number of attention heads for each attention layer in the Transformer encoder.
|
Number of attention heads for each attention layer in the Transformer encoder.
|
||||||
intermediate_size (:obj:`int`, optional, defaults to 512):
|
intermediate_size (:obj:`int`, `optional`, defaults to 3072):
|
||||||
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
|
Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
|
||||||
hidden_act (:obj:`str` or :obj:`function`, optional, defaults to :obj:`"gelu"`):
|
hidden_act (:obj:`str` or :obj:`Callable`, `optional`, defaults to :obj:`"gelu"`):
|
||||||
The non-linear activation function (function or string) in the encoder and pooler.
|
The non-linear activation function (function or string) in the encoder and pooler.
|
||||||
If string, :obj:`"gelu"`, :obj:`"relu"`, :obj:`"swish"`, :obj:`"gelu_new"`, :obj:`"gelu_fast"`,
|
If string, :obj:`"gelu"`, :obj:`"relu"`, :obj:`"swish"` and :obj:`"gelu_new"` are supported.
|
||||||
and :obj:`"mish"` are supported.
|
hidden_dropout_prob (:obj:`float`, `optional`, defaults to 0.1):
|
||||||
hidden_dropout_prob (:obj:`float`, optional, defaults to 0.0):
|
The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler.
|
||||||
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
|
attention_probs_dropout_prob (:obj:`float`, `optional`, defaults to 0.1):
|
||||||
attention_probs_dropout_prob (:obj:`float`, optional, defaults to 0.1):
|
|
||||||
The dropout ratio for the attention probabilities.
|
The dropout ratio for the attention probabilities.
|
||||||
max_position_embeddings (:obj:`int`, optional, defaults to 512):
|
max_position_embeddings (:obj:`int`, `optional`, defaults to 512):
|
||||||
The maximum sequence length that this model might ever be used with.
|
The maximum sequence length that this model might ever be used with.
|
||||||
Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
|
Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
|
||||||
type_vocab_size (:obj:`int`, optional, defaults to 2):
|
type_vocab_size (:obj:`int`, `optional`, defaults to 2):
|
||||||
The vocabulary size of the :obj:`token_type_ids` passed into :class:`~transformers.SqueezeBertModel`.
|
The vocabulary size of the :obj:`token_type_ids` passed when calling :class:`~transformers.BertModel` or
|
||||||
initializer_range (:obj:`float`, optional, defaults to 0.02):
|
:class:`~transformers.TFBertModel`.
|
||||||
|
initializer_range (:obj:`float`, `optional`, defaults to 0.02):
|
||||||
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
||||||
layer_norm_eps (:obj:`float`, optional, defaults to 1e-12):
|
layer_norm_eps (:obj:`float`, `optional`, defaults to 1e-12):
|
||||||
The epsilon used by the layer normalization layers.
|
|
||||||
|
|
||||||
pad_token_id (:obj:`int`, optional, defaults to 0):
|
pad_token_id (:obj:`int`, `optional`, defaults to 0):
|
||||||
The ID of the token in the word embedding to use as padding.
|
The ID of the token in the word embedding to use as padding.
|
||||||
embedding_size (:obj:`int`, optional, defaults to 128):
|
embedding_size (:obj:`int`, `optional`, defaults to 768):
|
||||||
The dimension of the word embedding vectors.
|
The dimension of the word embedding vectors.
|
||||||
|
|
||||||
q_groups (:obj:`int`, optional, defaults to 4):
|
q_groups (:obj:`int`, `optional`, defaults to 4):
|
||||||
The number of groups in Q layer
|
The number of groups in Q layer.
|
||||||
k_groups (:obj:`int`, optional, defaults to 4):
|
k_groups (:obj:`int`, `optional`, defaults to 4):
|
||||||
The number of groups in K layer
|
The number of groups in K layer.
|
||||||
v_groups (:obj:`int`, optional, defaults to 4):
|
v_groups (:obj:`int`, `optional`, defaults to 4):
|
||||||
The number of groups in V layer
|
The number of groups in V layer.
|
||||||
post_attention_groups (:obj:`int`, optional, defaults to 1):
|
post_attention_groups (:obj:`int`, `optional`, defaults to 1):
|
||||||
The number of groups in the first feed forward network layer
|
The number of groups in the first feed forward network layer.
|
||||||
intermediate_groups (:obj:`int`, optional, defaults to 4):
|
intermediate_groups (:obj:`int`, `optional`, defaults to 4):
|
||||||
The number of groups in the second feed forward network layer
|
The number of groups in the second feed forward network layer.
|
||||||
output_groups (:obj:`int`, optional, defaults to 4):
|
output_groups (:obj:`int`, `optional`, defaults to 4):
|
||||||
The number of groups in the third feed forward network layer
|
The number of groups in the third feed forward network layer.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,114 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
# Copyright 2020 The HuggingFace Inc. team.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
"""Convert Blenderbot checkpoint."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
import torch
|
||||||
|
|
||||||
|
from transformers import BartConfig, BartForConditionalGeneration
|
||||||
|
from transformers.utils import logging
|
||||||
|
|
||||||
|
|
||||||
|
logging.set_verbosity_info()
|
||||||
|
logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
|
PATTERNS = [
|
||||||
|
["attention", "attn"],
|
||||||
|
["encoder_attention", "encoder_attn"],
|
||||||
|
["q_lin", "q_proj"],
|
||||||
|
["k_lin", "k_proj"],
|
||||||
|
["v_lin", "v_proj"],
|
||||||
|
["out_lin", "out_proj"],
|
||||||
|
["norm_embeddings", "layernorm_embedding"],
|
||||||
|
["position_embeddings", "embed_positions"],
|
||||||
|
["embeddings", "embed_tokens"],
|
||||||
|
["ffn.lin", "fc"],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def rename_state_dict_key(k):
|
||||||
|
if k == "embeddings.weight":
|
||||||
|
return "shared.weight"
|
||||||
|
|
||||||
|
for parlai_name, hf_name in PATTERNS:
|
||||||
|
k = k.replace(parlai_name, hf_name)
|
||||||
|
|
||||||
|
if k.startswith("encoder"):
|
||||||
|
k = k.replace(".attn", ".self_attn")
|
||||||
|
k = k.replace("norm1", "self_attn_layer_norm")
|
||||||
|
k = k.replace("norm2", "final_layer_norm")
|
||||||
|
elif k.startswith("decoder"):
|
||||||
|
k = k.replace("norm1", "self_attn_layer_norm")
|
||||||
|
k = k.replace("norm2", "encoder_attn_layer_norm")
|
||||||
|
k = k.replace("norm3", "final_layer_norm")
|
||||||
|
return k
|
||||||
|
|
||||||
|
|
||||||
|
def rename_layernorm_keys(sd):
|
||||||
|
keys = [
|
||||||
|
"model.encoder.layernorm_embedding.weight",
|
||||||
|
"model.encoder.layernorm_embedding.bias",
|
||||||
|
"model.decoder.layernorm_embedding.weight",
|
||||||
|
"model.decoder.layernorm_embedding.bias",
|
||||||
|
]
|
||||||
|
for k in keys:
|
||||||
|
v = sd.pop(k)
|
||||||
|
new_k = k.replace("layernorm_embedding", "layer_norm")
|
||||||
|
assert new_k not in sd
|
||||||
|
sd[new_k] = v
|
||||||
|
|
||||||
|
|
||||||
|
IGNORE_KEYS = ["START"]
|
||||||
|
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def convert_parlai_checkpoint(checkpoint_path, pytorch_dump_folder_path, config_json_path):
|
||||||
|
"""
|
||||||
|
Copy/paste/tweak model's weights to our BERT structure.
|
||||||
|
"""
|
||||||
|
model = torch.load(checkpoint_path, map_location="cpu")
|
||||||
|
sd = model["model"]
|
||||||
|
cfg = BartConfig.from_json_file(config_json_path)
|
||||||
|
m = BartForConditionalGeneration(cfg)
|
||||||
|
valid_keys = m.model.state_dict().keys()
|
||||||
|
failures = []
|
||||||
|
mapping = {}
|
||||||
|
for k, v in sd.items():
|
||||||
|
if k in IGNORE_KEYS:
|
||||||
|
continue
|
||||||
|
|
||||||
|
new_k = rename_state_dict_key(k)
|
||||||
|
if new_k not in valid_keys:
|
||||||
|
failures.append([k, new_k])
|
||||||
|
else:
|
||||||
|
mapping[new_k] = v
|
||||||
|
if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm
|
||||||
|
rename_layernorm_keys(sd)
|
||||||
|
m.model.load_state_dict(mapping, strict=True)
|
||||||
|
m.half()
|
||||||
|
m.save_pretrained(pytorch_dump_folder_path)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
# Required parameters
|
||||||
|
parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin")
|
||||||
|
parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.")
|
||||||
|
parser.add_argument(
|
||||||
|
"--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use"
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
|
||||||
@@ -1,3 +1,23 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
# Copyright 2018 The HuggingFace Inc. team.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
""" Utilities to convert slow tokenizers in their fast tokenizers counterparts.
|
||||||
|
|
||||||
|
All the conversions are grouped here to gather SentencePiece dependencies outside of
|
||||||
|
the fast tokenizers files and allow to make our dependency on SentencePiece optional.
|
||||||
|
"""
|
||||||
|
|
||||||
from typing import Dict, List, Tuple
|
from typing import Dict, List, Tuple
|
||||||
|
|
||||||
from sentencepiece import SentencePieceProcessor
|
from sentencepiece import SentencePieceProcessor
|
||||||
|
|||||||
@@ -446,7 +446,7 @@ class DataCollatorForNextSentencePrediction:
|
|||||||
"input_ids": input_ids,
|
"input_ids": input_ids,
|
||||||
"attention_mask": self._tensorize_batch(attention_masks),
|
"attention_mask": self._tensorize_batch(attention_masks),
|
||||||
"token_type_ids": self._tensorize_batch(segment_ids),
|
"token_type_ids": self._tensorize_batch(segment_ids),
|
||||||
"masked_lm_labels": mlm_labels if self.mlm else None,
|
"labels": mlm_labels if self.mlm else None,
|
||||||
"next_sentence_label": torch.tensor(nsp_labels),
|
"next_sentence_label": torch.tensor(nsp_labels),
|
||||||
}
|
}
|
||||||
if self.mlm:
|
if self.mlm:
|
||||||
|
|||||||
@@ -614,7 +614,7 @@ class SquadProcessor(DataProcessor):
|
|||||||
Args:
|
Args:
|
||||||
data_dir: Directory containing the data files used for training and evaluating.
|
data_dir: Directory containing the data files used for training and evaluating.
|
||||||
filename: None by default, specify this if the evaluation file has a different name than the original one
|
filename: None by default, specify this if the evaluation file has a different name than the original one
|
||||||
which is `train-v1.1.json` and `train-v2.0.json` for squad versions 1.1 and 2.0 respectively.
|
which is `dev-v1.1.json` and `dev-v2.0.json` for squad versions 1.1 and 2.0 respectively.
|
||||||
"""
|
"""
|
||||||
if data_dir is None:
|
if data_dir is None:
|
||||||
data_dir = ""
|
data_dir = ""
|
||||||
|
|||||||
@@ -143,24 +143,6 @@ except (AttributeError, ImportError):
|
|||||||
_has_sklearn = False
|
_has_sklearn = False
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
import sentencepiece # noqa: F401
|
|
||||||
|
|
||||||
_sentencepiece_available = True
|
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
_sentencepiece_available = False
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
import tokenizers # noqa: F401
|
|
||||||
|
|
||||||
_tokenizers_available = True
|
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
_tokenizers_available = False
|
|
||||||
|
|
||||||
|
|
||||||
default_cache_path = os.path.join(torch_cache_home, "transformers")
|
default_cache_path = os.path.join(torch_cache_home, "transformers")
|
||||||
|
|
||||||
|
|
||||||
@@ -221,18 +203,23 @@ def is_faiss_available():
|
|||||||
return _faiss_available
|
return _faiss_available
|
||||||
|
|
||||||
|
|
||||||
|
def torch_only_method(fn):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
if not _torch_available:
|
||||||
|
raise ImportError(
|
||||||
|
"You need to install pytorch to use this method or class, "
|
||||||
|
"or activate it with environment variables USE_TORCH=1 and USE_TF=0."
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return fn(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
def is_sklearn_available():
|
def is_sklearn_available():
|
||||||
return _has_sklearn
|
return _has_sklearn
|
||||||
|
|
||||||
|
|
||||||
def is_sentencepiece_available():
|
|
||||||
return _sentencepiece_available
|
|
||||||
|
|
||||||
|
|
||||||
def is_tokenizers_available():
|
|
||||||
return _tokenizers_available
|
|
||||||
|
|
||||||
|
|
||||||
DATASETS_IMPORT_ERROR = """
|
DATASETS_IMPORT_ERROR = """
|
||||||
{0} requires the 🤗 Datasets library but it was not found in your enviromnent. You can install it with:
|
{0} requires the 🤗 Datasets library but it was not found in your enviromnent. You can install it with:
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ class TFGenerationMixin:
|
|||||||
assert isinstance(do_sample, bool), "`do_sample` should be a boolean."
|
assert isinstance(do_sample, bool), "`do_sample` should be a boolean."
|
||||||
assert isinstance(early_stopping, bool), "`early_stopping` should be a boolean."
|
assert isinstance(early_stopping, bool), "`early_stopping` should be a boolean."
|
||||||
assert isinstance(use_cache, bool), "`use_cache` should be a boolean."
|
assert isinstance(use_cache, bool), "`use_cache` should be a boolean."
|
||||||
assert isinstance(num_beams, int) and num_beams > 0, "`num_beams` should be a strictely positive integer."
|
assert isinstance(num_beams, int) and num_beams > 0, "`num_beams` should be a strictly positive integer."
|
||||||
assert temperature > 0, "`temperature` should be strictely positive."
|
assert temperature > 0, "`temperature` should be strictely positive."
|
||||||
assert isinstance(top_k, int) and top_k >= 0, "`top_k` should be a positive integer."
|
assert isinstance(top_k, int) and top_k >= 0, "`top_k` should be a positive integer."
|
||||||
assert 0 <= top_p <= 1, "`top_p` should be between 0 and 1."
|
assert 0 <= top_p <= 1, "`top_p` should be between 0 and 1."
|
||||||
|
|||||||
@@ -3,7 +3,10 @@ import math
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
# Import 3rd-party integrations first:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Comet needs to be imported before any ML frameworks
|
||||||
import comet_ml # noqa: F401
|
import comet_ml # noqa: F401
|
||||||
|
|
||||||
_has_comet = True
|
_has_comet = True
|
||||||
@@ -36,15 +39,6 @@ try:
|
|||||||
except (ImportError):
|
except (ImportError):
|
||||||
_has_ray = False
|
_has_ray = False
|
||||||
|
|
||||||
|
|
||||||
# No ML framework or transformer imports above this point
|
|
||||||
|
|
||||||
from .trainer_utils import PREFIX_CHECKPOINT_DIR, BestRun # isort:skip
|
|
||||||
from .utils import logging # isort:skip
|
|
||||||
|
|
||||||
logger = logging.get_logger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from torch.utils.tensorboard import SummaryWriter # noqa: F401
|
from torch.utils.tensorboard import SummaryWriter # noqa: F401
|
||||||
|
|
||||||
@@ -57,9 +51,18 @@ except ImportError:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
_has_tensorboard = False
|
_has_tensorboard = False
|
||||||
|
|
||||||
|
# No transformer imports above this point
|
||||||
|
|
||||||
|
from .file_utils import is_torch_tpu_available
|
||||||
|
from .trainer_callback import TrainerCallback
|
||||||
|
from .trainer_utils import PREFIX_CHECKPOINT_DIR, BestRun
|
||||||
|
from .utils import logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# Integration functions:
|
# Integration functions:
|
||||||
|
|
||||||
|
|
||||||
def is_wandb_available():
|
def is_wandb_available():
|
||||||
return _has_wandb
|
return _has_wandb
|
||||||
|
|
||||||
@@ -128,8 +131,8 @@ def run_hp_search_ray(trainer, n_trials: int, direction: str, **kwargs) -> BestR
|
|||||||
|
|
||||||
# The model and TensorBoard writer do not pickle so we have to remove them (if they exists)
|
# The model and TensorBoard writer do not pickle so we have to remove them (if they exists)
|
||||||
# while doing the ray hp search.
|
# while doing the ray hp search.
|
||||||
_tb_writer = trainer.tb_writer
|
|
||||||
trainer.tb_writer = None
|
_tb_writer = trainer.pop_callback(TensorBoardCallback)
|
||||||
trainer.model = None
|
trainer.model = None
|
||||||
# Setup default `resources_per_trial` and `reporter`.
|
# Setup default `resources_per_trial` and `reporter`.
|
||||||
if "resources_per_trial" not in kwargs and trainer.args.n_gpu > 0:
|
if "resources_per_trial" not in kwargs and trainer.args.n_gpu > 0:
|
||||||
@@ -182,5 +185,159 @@ def run_hp_search_ray(trainer, n_trials: int, direction: str, **kwargs) -> BestR
|
|||||||
analysis = ray.tune.run(_objective, config=trainer.hp_space(None), num_samples=n_trials, **kwargs)
|
analysis = ray.tune.run(_objective, config=trainer.hp_space(None), num_samples=n_trials, **kwargs)
|
||||||
best_trial = analysis.get_best_trial(metric="objective", mode=direction[:3])
|
best_trial = analysis.get_best_trial(metric="objective", mode=direction[:3])
|
||||||
best_run = BestRun(best_trial.trial_id, best_trial.last_result["objective"], best_trial.config)
|
best_run = BestRun(best_trial.trial_id, best_trial.last_result["objective"], best_trial.config)
|
||||||
trainer.tb_writer = _tb_writer
|
if _tb_writer is not None:
|
||||||
|
trainer.add_callback(_tb_writer)
|
||||||
return best_run
|
return best_run
|
||||||
|
|
||||||
|
|
||||||
|
class TensorBoardCallback(TrainerCallback):
|
||||||
|
"""
|
||||||
|
A :class:`~transformers.TrainerCallback` that sends the logs to `TensorBoard
|
||||||
|
<https://www.tensorflow.org/tensorboard>`__.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
tb_writer (:obj:`SummaryWriter`, `optional`):
|
||||||
|
The writer to use. Will instatiate one if not set.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, tb_writer=None):
|
||||||
|
assert (
|
||||||
|
_has_tensorboard
|
||||||
|
), "TensorBoardCallback requires tensorboard to be installed. Either update your PyTorch version or install tensorboardX."
|
||||||
|
self.tb_writer = tb_writer
|
||||||
|
|
||||||
|
def on_init_end(self, args, state, control, **kwargs):
|
||||||
|
if self.tb_writer is None and state.is_world_process_zero:
|
||||||
|
self.tb_writer = SummaryWriter(log_dir=args.logging_dir)
|
||||||
|
|
||||||
|
def on_train_begin(self, args, state, control, **kwargs):
|
||||||
|
if self.tb_writer is not None:
|
||||||
|
self.tb_writer.add_text("args", args.to_json_string())
|
||||||
|
self.tb_writer.add_hparams(args.to_sanitized_dict(), metric_dict={})
|
||||||
|
|
||||||
|
def on_log(self, args, state, control, logs=None, **kwargs):
|
||||||
|
if self.tb_writer:
|
||||||
|
for k, v in logs.items():
|
||||||
|
if isinstance(v, (int, float)):
|
||||||
|
self.tb_writer.add_scalar(k, v, state.global_step)
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
"Trainer is attempting to log a value of "
|
||||||
|
'"%s" of type %s for key "%s" as a scalar. '
|
||||||
|
"This invocation of Tensorboard's writer.add_scalar() "
|
||||||
|
"is incorrect so we dropped this attribute.",
|
||||||
|
v,
|
||||||
|
type(v),
|
||||||
|
k,
|
||||||
|
)
|
||||||
|
self.tb_writer.flush()
|
||||||
|
|
||||||
|
def on_train_end(self, args, state, control, **kwargs):
|
||||||
|
if self.tb_writer:
|
||||||
|
self.tb_writer.close()
|
||||||
|
|
||||||
|
|
||||||
|
class WandbCallback(TrainerCallback):
|
||||||
|
"""
|
||||||
|
A :class:`~transformers.TrainerCallback` that sends the logs to `Weight and Biases
|
||||||
|
<https://www.wandb.com/>`__.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
assert _has_wandb, "WandbCallback requires wandb to be installed. Run `pip install wandb`."
|
||||||
|
self._initialized = False
|
||||||
|
|
||||||
|
def setup(self, args, state, model):
|
||||||
|
"""
|
||||||
|
Setup the optional Weights & Biases (`wandb`) integration.
|
||||||
|
|
||||||
|
One can subclass and override this method to customize the setup if needed. Find more information
|
||||||
|
`here <https://docs.wandb.com/huggingface>`__. You can also override the following environment variables:
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
WANDB_WATCH (:obj:`str`, `optional` defaults to :obj:`"gradients"`):
|
||||||
|
Can be :obj:`"gradients"`, :obj:`"all"` or :obj:`"false"`. Set to :obj:`"false"` to disable gradient
|
||||||
|
logging or :obj:`"all"` to log gradients and parameters.
|
||||||
|
WANDB_PROJECT (:obj:`str`, `optional`, defaults to :obj:`"huggingface"`):
|
||||||
|
Set this to a custom string to store results in a different project.
|
||||||
|
WANDB_DISABLED (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Whether or not to disable wandb entirely.
|
||||||
|
"""
|
||||||
|
self._initialized = True
|
||||||
|
if state.is_world_process_zero:
|
||||||
|
logger.info(
|
||||||
|
'Automatic Weights & Biases logging enabled, to disable set os.environ["WANDB_DISABLED"] = "true"'
|
||||||
|
)
|
||||||
|
combined_dict = {**args.to_sanitized_dict()}
|
||||||
|
if hasattr(model, "config"):
|
||||||
|
combined_dict = {**model.config.to_dict(), **combined_dict}
|
||||||
|
wandb.init(project=os.getenv("WANDB_PROJECT", "huggingface"), config=combined_dict, name=args.run_name)
|
||||||
|
# keep track of model topology and gradients, unsupported on TPU
|
||||||
|
if not is_torch_tpu_available() and os.getenv("WANDB_WATCH") != "false":
|
||||||
|
wandb.watch(model, log=os.getenv("WANDB_WATCH", "gradients"), log_freq=max(100, args.logging_steps))
|
||||||
|
|
||||||
|
def on_train_begin(self, args, state, control, model=None, **kwargs):
|
||||||
|
if not self._initialized:
|
||||||
|
self.setup(args, state, model)
|
||||||
|
|
||||||
|
def on_log(self, args, state, control, model=None, logs=None, **kwargs):
|
||||||
|
if not self._initialized:
|
||||||
|
self.setup(args, state, model)
|
||||||
|
if state.is_world_process_zero:
|
||||||
|
wandb.log(logs, step=state.global_step)
|
||||||
|
|
||||||
|
|
||||||
|
class CometCallback(TrainerCallback):
|
||||||
|
"""
|
||||||
|
A :class:`~transformers.TrainerCallback` that sends the logs to `Comet ML
|
||||||
|
<https://www.comet.ml/site/>`__.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
assert _has_comet, "CometCallback requires comet-ml to be installed. Run `pip install comet-ml`."
|
||||||
|
self._initialized = False
|
||||||
|
|
||||||
|
def setup(self, args, state, model):
|
||||||
|
"""
|
||||||
|
Setup the optional Comet.ml integration.
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
COMET_MODE (:obj:`str`, `optional`):
|
||||||
|
"OFFLINE", "ONLINE", or "DISABLED"
|
||||||
|
COMET_PROJECT_NAME (:obj:`str`, `optional`):
|
||||||
|
Comet.ml project name for experiments
|
||||||
|
COMET_OFFLINE_DIRECTORY (:obj:`str`, `optional`):
|
||||||
|
Folder to use for saving offline experiments when :obj:`COMET_MODE` is "OFFLINE"
|
||||||
|
|
||||||
|
For a number of configurable items in the environment,
|
||||||
|
see `here <https://www.comet.ml/docs/python-sdk/advanced/#comet-configuration-variables>`__.
|
||||||
|
"""
|
||||||
|
self._initialized = True
|
||||||
|
if state.is_world_process_zero:
|
||||||
|
comet_mode = os.getenv("COMET_MODE", "ONLINE").upper()
|
||||||
|
args = {"project_name": os.getenv("COMET_PROJECT_NAME", "huggingface")}
|
||||||
|
experiment = None
|
||||||
|
if comet_mode == "ONLINE":
|
||||||
|
experiment = comet_ml.Experiment(**args)
|
||||||
|
logger.info("Automatic Comet.ml online logging enabled")
|
||||||
|
elif comet_mode == "OFFLINE":
|
||||||
|
args["offline_directory"] = os.getenv("COMET_OFFLINE_DIRECTORY", "./")
|
||||||
|
experiment = comet_ml.OfflineExperiment(**args)
|
||||||
|
logger.info("Automatic Comet.ml offline logging enabled; use `comet upload` when finished")
|
||||||
|
if experiment is not None:
|
||||||
|
experiment._set_model_graph(model, framework="transformers")
|
||||||
|
experiment._log_parameters(args, prefix="args/", framework="transformers")
|
||||||
|
if hasattr(model, "config"):
|
||||||
|
experiment._log_parameters(model.config, prefix="config/", framework="transformers")
|
||||||
|
|
||||||
|
def on_train_begin(self, args, state, control, model=None, **kwargs):
|
||||||
|
if not self._initialized:
|
||||||
|
self.setup(args, state, model)
|
||||||
|
|
||||||
|
def on_log(self, args, state, control, model=None, logs=None, **kwargs):
|
||||||
|
if not self._initialized:
|
||||||
|
self.setup(args, state, model)
|
||||||
|
if state.is_world_process_zero:
|
||||||
|
experiment = comet_ml.config.get_global_experiment()
|
||||||
|
if experiment is not None:
|
||||||
|
experiment._log_metrics(logs, step=state.global_step, epoch=state.epoch, framework="transformers")
|
||||||
@@ -24,6 +24,7 @@ from .configuration_auto import (
|
|||||||
BartConfig,
|
BartConfig,
|
||||||
BertConfig,
|
BertConfig,
|
||||||
BertGenerationConfig,
|
BertGenerationConfig,
|
||||||
|
BlenderbotConfig,
|
||||||
CamembertConfig,
|
CamembertConfig,
|
||||||
CTRLConfig,
|
CTRLConfig,
|
||||||
DebertaConfig,
|
DebertaConfig,
|
||||||
@@ -82,6 +83,7 @@ from .modeling_bert import (
|
|||||||
BertModel,
|
BertModel,
|
||||||
)
|
)
|
||||||
from .modeling_bert_generation import BertGenerationDecoder, BertGenerationEncoder
|
from .modeling_bert_generation import BertGenerationDecoder, BertGenerationEncoder
|
||||||
|
from .modeling_blenderbot import BlenderbotForConditionalGeneration
|
||||||
from .modeling_camembert import (
|
from .modeling_camembert import (
|
||||||
CamembertForCausalLM,
|
CamembertForCausalLM,
|
||||||
CamembertForMaskedLM,
|
CamembertForMaskedLM,
|
||||||
@@ -129,7 +131,7 @@ from .modeling_funnel import (
|
|||||||
FunnelForTokenClassification,
|
FunnelForTokenClassification,
|
||||||
FunnelModel,
|
FunnelModel,
|
||||||
)
|
)
|
||||||
from .modeling_gpt2 import GPT2LMHeadModel, GPT2Model
|
from .modeling_gpt2 import GPT2ForSequenceClassification, GPT2LMHeadModel, GPT2Model
|
||||||
from .modeling_layoutlm import LayoutLMForMaskedLM, LayoutLMForTokenClassification, LayoutLMModel
|
from .modeling_layoutlm import LayoutLMForMaskedLM, LayoutLMForTokenClassification, LayoutLMModel
|
||||||
from .modeling_longformer import (
|
from .modeling_longformer import (
|
||||||
LongformerForMaskedLM,
|
LongformerForMaskedLM,
|
||||||
@@ -353,6 +355,7 @@ MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING = OrderedDict(
|
|||||||
(PegasusConfig, PegasusForConditionalGeneration),
|
(PegasusConfig, PegasusForConditionalGeneration),
|
||||||
(MarianConfig, MarianMTModel),
|
(MarianConfig, MarianMTModel),
|
||||||
(MBartConfig, MBartForConditionalGeneration),
|
(MBartConfig, MBartForConditionalGeneration),
|
||||||
|
(BlenderbotConfig, BlenderbotForConditionalGeneration),
|
||||||
(BartConfig, BartForConditionalGeneration),
|
(BartConfig, BartForConditionalGeneration),
|
||||||
(FSMTConfig, FSMTForConditionalGeneration),
|
(FSMTConfig, FSMTForConditionalGeneration),
|
||||||
(EncoderDecoderConfig, EncoderDecoderModel),
|
(EncoderDecoderConfig, EncoderDecoderModel),
|
||||||
@@ -377,6 +380,7 @@ MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING = OrderedDict(
|
|||||||
(ElectraConfig, ElectraForSequenceClassification),
|
(ElectraConfig, ElectraForSequenceClassification),
|
||||||
(FunnelConfig, FunnelForSequenceClassification),
|
(FunnelConfig, FunnelForSequenceClassification),
|
||||||
(DebertaConfig, DebertaForSequenceClassification),
|
(DebertaConfig, DebertaForSequenceClassification),
|
||||||
|
(GPT2Config, GPT2ForSequenceClassification),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -528,7 +532,7 @@ AUTO_MODEL_PRETRAINED_DOCSTRING = r"""
|
|||||||
class AutoModel:
|
class AutoModel:
|
||||||
r"""
|
r"""
|
||||||
This is a generic model class that will be instantiated as one of the base model classes of the library
|
This is a generic model class that will be instantiated as one of the base model classes of the library
|
||||||
when created with the when created with the :meth:`~transformers.AutoModel.from_pretrained` class method or the
|
when created with the :meth:`~transformers.AutoModel.from_pretrained` class method or the
|
||||||
:meth:`~transformers.AutoModel.from_config` class methods.
|
:meth:`~transformers.AutoModel.from_config` class methods.
|
||||||
|
|
||||||
This class cannot be instantiated directly using ``__init__()`` (throws an error).
|
This class cannot be instantiated directly using ``__init__()`` (throws an error).
|
||||||
|
|||||||
@@ -499,6 +499,7 @@ class BartDecoder(nn.Module):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.dropout = config.dropout
|
self.dropout = config.dropout
|
||||||
self.layerdrop = config.decoder_layerdrop
|
self.layerdrop = config.decoder_layerdrop
|
||||||
|
self.do_blenderbot_90_layernorm = config.do_blenderbot_90_layernorm # layernorm variant
|
||||||
self.padding_idx = embed_tokens.padding_idx
|
self.padding_idx = embed_tokens.padding_idx
|
||||||
self.max_target_positions = config.max_position_embeddings
|
self.max_target_positions = config.max_position_embeddings
|
||||||
self.embed_scale = math.sqrt(config.d_model) if config.scale_embedding else 1.0
|
self.embed_scale = math.sqrt(config.d_model) if config.scale_embedding else 1.0
|
||||||
@@ -578,8 +579,13 @@ class BartDecoder(nn.Module):
|
|||||||
positions = positions[:, -1:]
|
positions = positions[:, -1:]
|
||||||
|
|
||||||
x = self.embed_tokens(input_ids) * self.embed_scale
|
x = self.embed_tokens(input_ids) * self.embed_scale
|
||||||
|
if self.do_blenderbot_90_layernorm:
|
||||||
|
x = self.layernorm_embedding(x)
|
||||||
|
x += positions
|
||||||
|
else:
|
||||||
x += positions
|
x += positions
|
||||||
x = self.layernorm_embedding(x)
|
x = self.layernorm_embedding(x)
|
||||||
|
|
||||||
x = F.dropout(x, p=self.dropout, training=self.training)
|
x = F.dropout(x, p=self.dropout, training=self.training)
|
||||||
|
|
||||||
# Convert to Bart output format: (seq_len, BS, model_dim) -> (BS, seq_len, model_dim)
|
# Convert to Bart output format: (seq_len, BS, model_dim) -> (BS, seq_len, model_dim)
|
||||||
@@ -1176,7 +1182,7 @@ class BartForSequenceClassification(PretrainedBartModel):
|
|||||||
config.d_model,
|
config.d_model,
|
||||||
config.d_model,
|
config.d_model,
|
||||||
config.num_labels,
|
config.num_labels,
|
||||||
config.classif_dropout,
|
config.classifier_dropout,
|
||||||
)
|
)
|
||||||
self.model._init_weights(self.classification_head.dense)
|
self.model._init_weights(self.classification_head.dense)
|
||||||
self.model._init_weights(self.classification_head.out_proj)
|
self.model._init_weights(self.classification_head.out_proj)
|
||||||
|
|||||||
@@ -667,7 +667,7 @@ BERT_INPUTS_DOCSTRING = r"""
|
|||||||
Mask values selected in ``[0, 1]``:
|
Mask values selected in ``[0, 1]``:
|
||||||
|
|
||||||
- 1 for tokens that are **not masked**,
|
- 1 for tokens that are **not masked**,
|
||||||
- 0 for tokens that are **maked**.
|
- 0 for tokens that are **masked**.
|
||||||
|
|
||||||
`What are attention masks? <../glossary.html#attention-mask>`__
|
`What are attention masks? <../glossary.html#attention-mask>`__
|
||||||
token_type_ids (:obj:`torch.LongTensor` of shape :obj:`({0})`, `optional`):
|
token_type_ids (:obj:`torch.LongTensor` of shape :obj:`({0})`, `optional`):
|
||||||
@@ -781,7 +781,7 @@ class BertModel(BertPreTrainedModel):
|
|||||||
Mask values selected in ``[0, 1]``:
|
Mask values selected in ``[0, 1]``:
|
||||||
|
|
||||||
- 1 for tokens that are **not masked**,
|
- 1 for tokens that are **not masked**,
|
||||||
- 0 for tokens that are **maked**.
|
- 0 for tokens that are **masked**.
|
||||||
"""
|
"""
|
||||||
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
||||||
output_hidden_states = (
|
output_hidden_states = (
|
||||||
@@ -1012,7 +1012,7 @@ class BertLMHeadModel(BertPreTrainedModel):
|
|||||||
Mask values selected in ``[0, 1]``:
|
Mask values selected in ``[0, 1]``:
|
||||||
|
|
||||||
- 1 for tokens that are **not masked**,
|
- 1 for tokens that are **not masked**,
|
||||||
- 0 for tokens that are **maked**.
|
- 0 for tokens that are **masked**.
|
||||||
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
|
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
|
||||||
Labels for computing the left-to-right language modeling loss (next word prediction).
|
Labels for computing the left-to-right language modeling loss (next word prediction).
|
||||||
Indices should be in ``[-100, 0, ..., config.vocab_size]`` (see ``input_ids`` docstring)
|
Indices should be in ``[-100, 0, ..., config.vocab_size]`` (see ``input_ids`` docstring)
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# coding=utf-8
|
||||||
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
#
|
||||||
|
# This source code is licensed under the MIT license found in the;
|
||||||
|
# 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.
|
||||||
|
# LICENSE file in the root directory of this source tree.
|
||||||
|
""""BlenderbotForConditionalGeneration which inherits from BART"""
|
||||||
|
|
||||||
|
import torch
|
||||||
|
|
||||||
|
from .configuration_blenderbot import BlenderbotConfig
|
||||||
|
from .file_utils import add_start_docstrings
|
||||||
|
from .modeling_bart import BartForConditionalGeneration
|
||||||
|
|
||||||
|
|
||||||
|
BLENDER_START_DOCSTRING = r"""
|
||||||
|
|
||||||
|
This model inherits from :class:`~transformers.PreTrainedModel`. Check the superclass documentation for the generic
|
||||||
|
methods the library implements for all its model (such as downloading or saving, resizing the input embeddings,
|
||||||
|
pruning heads etc.)
|
||||||
|
|
||||||
|
This model is also a PyTorch `torch.nn.Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`__ subclass.
|
||||||
|
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general
|
||||||
|
usage and behavior.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST = ["facebook/blenderbot-3B", "facebook/blenderbot-90M"]
|
||||||
|
|
||||||
|
|
||||||
|
@add_start_docstrings(
|
||||||
|
"The BART Model with a language modeling head. Can be used for summarization.", BLENDER_START_DOCSTRING
|
||||||
|
)
|
||||||
|
class BlenderbotForConditionalGeneration(BartForConditionalGeneration):
|
||||||
|
"""
|
||||||
|
This class overrides :class:`~transformers.BartForConditionalGeneration`. Please check the
|
||||||
|
superclass for the appropriate documentation alongside usage examples.
|
||||||
|
"""
|
||||||
|
|
||||||
|
config_class = BlenderbotConfig
|
||||||
|
|
||||||
|
def adjust_logits_during_generation(self, logits, cur_len, max_length):
|
||||||
|
logits[:, self.config.bos_token_id] = -torch.finfo(torch.float16).max # near infinity fp16
|
||||||
|
if cur_len == max_length - 1 and self.config.eos_token_id is not None:
|
||||||
|
self._force_token_ids_generation(logits, self.config.eos_token_id)
|
||||||
|
return logits
|
||||||
@@ -22,7 +22,7 @@ from typing import List, Optional, Tuple
|
|||||||
|
|
||||||
import torch
|
import torch
|
||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
from torch.nn import CrossEntropyLoss
|
from torch.nn import CrossEntropyLoss, MSELoss
|
||||||
|
|
||||||
from .activations import ACT2FN
|
from .activations import ACT2FN
|
||||||
from .configuration_gpt2 import GPT2Config
|
from .configuration_gpt2 import GPT2Config
|
||||||
@@ -33,7 +33,7 @@ from .file_utils import (
|
|||||||
add_start_docstrings_to_callable,
|
add_start_docstrings_to_callable,
|
||||||
replace_return_docstrings,
|
replace_return_docstrings,
|
||||||
)
|
)
|
||||||
from .modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
from .modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
|
||||||
from .modeling_utils import (
|
from .modeling_utils import (
|
||||||
Conv1D,
|
Conv1D,
|
||||||
PreTrainedModel,
|
PreTrainedModel,
|
||||||
@@ -946,3 +946,121 @@ class GPT2DoubleHeadsModel(GPT2PreTrainedModel):
|
|||||||
hidden_states=transformer_outputs.hidden_states,
|
hidden_states=transformer_outputs.hidden_states,
|
||||||
attentions=transformer_outputs.attentions,
|
attentions=transformer_outputs.attentions,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@add_start_docstrings(
|
||||||
|
"""The GPT2 Model transformer with a sequence classification head on top
|
||||||
|
(linear layer).
|
||||||
|
|
||||||
|
:class:`~transformers.GPT2ForSequenceClassification` uses the last token in order to do the classification, as
|
||||||
|
other causal models (e.g. GPT-1) do.
|
||||||
|
|
||||||
|
Since it does classification on the last token, it requires to know the position of the last token.
|
||||||
|
If a :obj:`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token
|
||||||
|
in each row. If no :obj:`pad_token_id` is defined, it simply takes the last value in each row of the batch.
|
||||||
|
Since it cannot guess the padding tokens when :obj:`inputs_embeds` are passed instead of :obj:`input_ids`, it
|
||||||
|
does the same (take the last value in each row of the batch).
|
||||||
|
""",
|
||||||
|
GPT2_START_DOCSTRING,
|
||||||
|
)
|
||||||
|
class GPT2ForSequenceClassification(GPT2PreTrainedModel):
|
||||||
|
authorized_missing_keys = [r"h\.\d+\.attn\.masked_bias", r"lm_head\.weight"]
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
super().__init__(config)
|
||||||
|
self.num_labels = config.num_labels
|
||||||
|
self.transformer = GPT2Model(config)
|
||||||
|
self.score = nn.Linear(config.n_embd, self.num_labels, bias=False)
|
||||||
|
|
||||||
|
self.init_weights()
|
||||||
|
|
||||||
|
@add_start_docstrings_to_callable(GPT2_INPUTS_DOCSTRING)
|
||||||
|
@add_code_sample_docstrings(
|
||||||
|
tokenizer_class=_TOKENIZER_FOR_DOC,
|
||||||
|
checkpoint="microsoft/dialogrpt",
|
||||||
|
output_type=SequenceClassifierOutputWithPast,
|
||||||
|
config_class=_CONFIG_FOR_DOC,
|
||||||
|
)
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
input_ids=None,
|
||||||
|
past_key_values=None,
|
||||||
|
attention_mask=None,
|
||||||
|
token_type_ids=None,
|
||||||
|
position_ids=None,
|
||||||
|
head_mask=None,
|
||||||
|
inputs_embeds=None,
|
||||||
|
labels=None,
|
||||||
|
use_cache=None,
|
||||||
|
output_attentions=None,
|
||||||
|
output_hidden_states=None,
|
||||||
|
return_dict=None,
|
||||||
|
):
|
||||||
|
r"""
|
||||||
|
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`):
|
||||||
|
Labels for computing the sequence classification/regression loss.
|
||||||
|
Indices should be in :obj:`[0, ..., config.num_labels - 1]`.
|
||||||
|
If :obj:`config.num_labels == 1` a regression loss is computed (Mean-Square loss),
|
||||||
|
If :obj:`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
||||||
|
"""
|
||||||
|
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
||||||
|
|
||||||
|
transformer_outputs = self.transformer(
|
||||||
|
input_ids,
|
||||||
|
past_key_values=past_key_values,
|
||||||
|
attention_mask=attention_mask,
|
||||||
|
token_type_ids=token_type_ids,
|
||||||
|
position_ids=position_ids,
|
||||||
|
head_mask=head_mask,
|
||||||
|
inputs_embeds=inputs_embeds,
|
||||||
|
use_cache=use_cache,
|
||||||
|
output_attentions=output_attentions,
|
||||||
|
output_hidden_states=output_hidden_states,
|
||||||
|
return_dict=return_dict,
|
||||||
|
)
|
||||||
|
hidden_states = transformer_outputs[0]
|
||||||
|
logits = self.score(hidden_states)
|
||||||
|
|
||||||
|
if input_ids is not None:
|
||||||
|
batch_size, sequence_length = input_ids.shape[:2]
|
||||||
|
else:
|
||||||
|
batch_size, sequence_length = inputs_embeds.shape[:2]
|
||||||
|
|
||||||
|
assert (
|
||||||
|
self.config.pad_token_id is not None or batch_size == 1
|
||||||
|
), "Cannot handle batch sizes > 1 if no padding token is defined."
|
||||||
|
if self.config.pad_token_id is None:
|
||||||
|
sequence_lengths = -1
|
||||||
|
else:
|
||||||
|
if input_ids is not None:
|
||||||
|
sequence_lengths = torch.ne(input_ids, self.config.pad_token_id).sum(-1) - 1
|
||||||
|
else:
|
||||||
|
sequence_lengths = -1
|
||||||
|
logger.warning(
|
||||||
|
f"{self.__class__.__name__} will not detect padding tokens in `inputs_embeds`. Results may be "
|
||||||
|
f"unexpected if using padding tokens in conjuction with `inputs_embeds.`"
|
||||||
|
)
|
||||||
|
|
||||||
|
pooled_logits = logits[range(batch_size), sequence_lengths]
|
||||||
|
|
||||||
|
loss = None
|
||||||
|
if labels is not None:
|
||||||
|
if self.num_labels == 1:
|
||||||
|
# We are doing regression
|
||||||
|
loss_fct = MSELoss()
|
||||||
|
loss = loss_fct(pooled_logits.view(-1), labels.view(-1))
|
||||||
|
else:
|
||||||
|
loss_fct = CrossEntropyLoss()
|
||||||
|
loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
|
||||||
|
|
||||||
|
if not return_dict:
|
||||||
|
output = (pooled_logits,) + transformer_outputs[1:]
|
||||||
|
return ((loss,) + output) if loss is not None else output
|
||||||
|
|
||||||
|
return SequenceClassifierOutputWithPast(
|
||||||
|
loss=loss,
|
||||||
|
logits=pooled_logits,
|
||||||
|
past_key_values=transformer_outputs.past_key_values,
|
||||||
|
hidden_states=transformer_outputs.hidden_states,
|
||||||
|
attentions=transformer_outputs.attentions,
|
||||||
|
)
|
||||||
@@ -964,7 +964,7 @@ class LxmertModel(LxmertPreTrainedModel):
|
|||||||
# Process the visual attention mask
|
# Process the visual attention mask
|
||||||
if visual_attention_mask is not None:
|
if visual_attention_mask is not None:
|
||||||
extended_visual_attention_mask = visual_attention_mask.unsqueeze(1).unsqueeze(2)
|
extended_visual_attention_mask = visual_attention_mask.unsqueeze(1).unsqueeze(2)
|
||||||
extended_visual_attention_mask = extended_visual_attention_mask.to(dtype=next(self.parameters()).dtype)
|
extended_visual_attention_mask = extended_visual_attention_mask.to(dtype=self.dtype)
|
||||||
extended_visual_attention_mask = (1.0 - extended_visual_attention_mask) * -10000.0
|
extended_visual_attention_mask = (1.0 - extended_visual_attention_mask) * -10000.0
|
||||||
else:
|
else:
|
||||||
extended_visual_attention_mask = None
|
extended_visual_attention_mask = None
|
||||||
|
|||||||
@@ -27,5 +27,5 @@ class MBartForConditionalGeneration(BartForConditionalGeneration):
|
|||||||
>>> translation = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
>>> translation = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
||||||
>>> assert translation == "Şeful ONU declară că nu există o soluţie militară în Siria"
|
>>> assert translation == "Şeful ONU declară că nu există o soluţie militară în Siria"
|
||||||
"""
|
"""
|
||||||
|
model_type = "mbart"
|
||||||
config_class = MBartConfig
|
config_class = MBartConfig
|
||||||
@@ -217,6 +217,42 @@ class CausalLMOutputWithPast(ModelOutput):
|
|||||||
attentions: Optional[Tuple[torch.FloatTensor]] = None
|
attentions: Optional[Tuple[torch.FloatTensor]] = None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SequenceClassifierOutputWithPast(ModelOutput):
|
||||||
|
"""
|
||||||
|
Base class for outputs of sentence classification models.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
loss (:obj:`torch.FloatTensor` of shape :obj:`(1,)`, `optional`, returned when :obj:`labels` is provided):
|
||||||
|
Classification (or regression if config.num_labels==1) loss.
|
||||||
|
logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, config.num_labels)`):
|
||||||
|
Classification (or regression if config.num_labels==1) scores (before SoftMax).
|
||||||
|
past_key_values (:obj:`List[torch.FloatTensor]`, `optional`, returned when ``use_cache=True`` is passed or when ``config.use_cache=True``):
|
||||||
|
List of :obj:`torch.FloatTensor` of length :obj:`config.n_layers`, with each tensor of shape
|
||||||
|
:obj:`(2, batch_size, num_heads, sequence_length, embed_size_per_head)`).
|
||||||
|
|
||||||
|
Contains pre-computed hidden-states (key and values in the attention blocks) that can be used (see
|
||||||
|
``past_key_values`` input) to speed up sequential decoding.
|
||||||
|
hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
|
||||||
|
Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
|
||||||
|
of shape :obj:`(batch_size, sequence_length, hidden_size)`.
|
||||||
|
|
||||||
|
Hidden-states of the model at the output of each layer plus the initial embedding outputs.
|
||||||
|
attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
|
||||||
|
Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
|
||||||
|
:obj:`(batch_size, num_heads, sequence_length, sequence_length)`.
|
||||||
|
|
||||||
|
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
|
||||||
|
heads.
|
||||||
|
"""
|
||||||
|
|
||||||
|
loss: Optional[torch.FloatTensor] = None
|
||||||
|
logits: torch.FloatTensor = None
|
||||||
|
past_key_values: Optional[List[torch.FloatTensor]] = None
|
||||||
|
hidden_states: Optional[Tuple[torch.FloatTensor]] = None
|
||||||
|
attentions: Optional[Tuple[torch.FloatTensor]] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class MaskedLMOutput(ModelOutput):
|
class MaskedLMOutput(ModelOutput):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -758,13 +758,13 @@ class RobertaForCausalLM(RobertaPreTrainedModel):
|
|||||||
|
|
||||||
Example::
|
Example::
|
||||||
|
|
||||||
>>> from transformers import RobertaTokenizer, RobertaLMHeadModel, RobertaConfig
|
>>> from transformers import RobertaTokenizer, RobertaForCausalLM, RobertaConfig
|
||||||
>>> import torch
|
>>> import torch
|
||||||
|
|
||||||
>>> tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
|
>>> tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
|
||||||
>>> config = RobertaConfig.from_pretrained("roberta-base")
|
>>> config = RobertaConfig.from_pretrained("roberta-base", return_dict=True)
|
||||||
>>> config.is_decoder = True
|
>>> config.is_decoder = True
|
||||||
>>> model = RobertaLMHeadModel.from_pretrained('roberta-base', config=config, return_dict=True)
|
>>> model = RobertaForCausalLM.from_pretrained('roberta-base', config=config)
|
||||||
|
|
||||||
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
|
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
|
||||||
>>> outputs = model(**inputs)
|
>>> outputs = model(**inputs)
|
||||||
|
|||||||
@@ -42,10 +42,6 @@ logger = logging.get_logger(__name__)
|
|||||||
_CONFIG_FOR_DOC = "SqueezeBertConfig"
|
_CONFIG_FOR_DOC = "SqueezeBertConfig"
|
||||||
_TOKENIZER_FOR_DOC = "SqueezeBertTokenizer"
|
_TOKENIZER_FOR_DOC = "SqueezeBertTokenizer"
|
||||||
|
|
||||||
####################################################
|
|
||||||
# This list contrains shortcut names for some of
|
|
||||||
# the pretrained weights provided with the models
|
|
||||||
####################################################
|
|
||||||
SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
||||||
"squeezebert/squeezebert-uncased",
|
"squeezebert/squeezebert-uncased",
|
||||||
"squeezebert/squeezebert-mnli",
|
"squeezebert/squeezebert-mnli",
|
||||||
@@ -407,11 +403,17 @@ class SqueezeBertPreTrainedModel(PreTrainedModel):
|
|||||||
module.bias.data.zero_()
|
module.bias.data.zero_()
|
||||||
|
|
||||||
|
|
||||||
SQUEEZEBERT_START_DOCSTRING = r""" The SqueezeBERT model was proposed in
|
SQUEEZEBERT_START_DOCSTRING = r"""
|
||||||
|
|
||||||
|
The SqueezeBERT model was proposed in
|
||||||
`SqueezeBERT: What can computer vision teach NLP about efficient neural networks?
|
`SqueezeBERT: What can computer vision teach NLP about efficient neural networks?
|
||||||
<https://arxiv.org/abs/2006.11316>`__ by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer
|
<https://arxiv.org/abs/2006.11316>`__ by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer
|
||||||
|
|
||||||
This model is a PyTorch `torch.nn.Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`_ sub-class.
|
This model inherits from :class:`~transformers.PreTrainedModel`. Check the superclass documentation for the generic
|
||||||
|
methods the library implements for all its model (such as downloading or saving, resizing the input embeddings,
|
||||||
|
pruning heads etc.)
|
||||||
|
|
||||||
|
This model is also a PyTorch `torch.nn.Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`__ subclass.
|
||||||
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general
|
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general
|
||||||
usage and behavior.
|
usage and behavior.
|
||||||
|
|
||||||
@@ -445,47 +447,55 @@ SQUEEZEBERT_START_DOCSTRING = r""" The SqueezeBERT model was proposed in
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
SQUEEZEBERT_INPUTS_DOCSTRING = r"""
|
SQUEEZEBERT_INPUTS_DOCSTRING = r"""
|
||||||
Inputs:
|
Args:
|
||||||
input_ids (:obj:`torch.LongTensor` of shape :obj:`{0}`):
|
input_ids (:obj:`torch.LongTensor` of shape :obj:`({0})`):
|
||||||
Indices of input sequence tokens in the vocabulary.
|
Indices of input sequence tokens in the vocabulary.
|
||||||
|
|
||||||
Indices can be obtained using :class:`transformers.SqueezeBertTokenizer`.
|
Indices can be obtained using :class:`~transformers.SqueezeBertTokenizer`.
|
||||||
See :func:`transformers.PreTrainedTokenizer.encode` and
|
See :meth:`transformers.PreTrainedTokenizer.encode` and
|
||||||
:func:`transformers.PreTrainedTokenizer.__call__` for details.
|
:meth:`transformers.PreTrainedTokenizer.__call__` for details.
|
||||||
|
|
||||||
`What are input IDs? <../glossary.html#input-ids>`__
|
`What are input IDs? <../glossary.html#input-ids>`__
|
||||||
attention_mask (:obj:`torch.FloatTensor` of shape :obj:`{0}`, `optional`, defaults to :obj:`None`):
|
attention_mask (:obj:`torch.FloatTensor` of shape :obj:`({0})`, `optional`):
|
||||||
Mask to avoid performing attention on padding token indices.
|
Mask to avoid performing attention on padding token indices.
|
||||||
Mask values selected in ``[0, 1]``:
|
Mask values selected in ``[0, 1]``:
|
||||||
``1`` for tokens that are NOT MASKED, ``0`` for MASKED tokens.
|
|
||||||
|
- 1 for tokens that are **not masked**,
|
||||||
|
- 0 for tokens that are **maked**.
|
||||||
|
|
||||||
`What are attention masks? <../glossary.html#attention-mask>`__
|
`What are attention masks? <../glossary.html#attention-mask>`__
|
||||||
token_type_ids (:obj:`torch.LongTensor` of shape :obj:`{0}`, `optional`, defaults to :obj:`None`):
|
token_type_ids (:obj:`torch.LongTensor` of shape :obj:`({0})`, `optional`):
|
||||||
Segment token indices to indicate first and second portions of the inputs.
|
Segment token indices to indicate first and second portions of the inputs.
|
||||||
Indices are selected in ``[0, 1]``: ``0`` corresponds to a `sentence A` token, ``1``
|
Indices are selected in ``[0, 1]``:
|
||||||
corresponds to a `sentence B` token
|
|
||||||
|
- 0 corresponds to a `sentence A` token,
|
||||||
|
- 1 corresponds to a `sentence B` token.
|
||||||
|
|
||||||
`What are token type IDs? <../glossary.html#token-type-ids>`_
|
`What are token type IDs? <../glossary.html#token-type-ids>`_
|
||||||
position_ids (:obj:`torch.LongTensor` of shape :obj:`{0}`, `optional`, defaults to :obj:`None`):
|
position_ids (:obj:`torch.LongTensor` of shape :obj:`({0})`, `optional`):
|
||||||
Indices of positions of each input sequence tokens in the position embeddings.
|
Indices of positions of each input sequence tokens in the position embeddings.
|
||||||
Selected in the range ``[0, config.max_position_embeddings - 1]``.
|
Selected in the range ``[0, config.max_position_embeddings - 1]``.
|
||||||
|
|
||||||
`What are position IDs? <../glossary.html#position-ids>`_
|
`What are position IDs? <../glossary.html#position-ids>`_
|
||||||
head_mask (:obj:`torch.FloatTensor` of shape :obj:`(num_heads,)` or :obj:`(num_layers, num_heads)`, `optional`, defaults to :obj:`None`):
|
head_mask (:obj:`torch.FloatTensor` of shape :obj:`(num_heads,)` or :obj:`(num_layers, num_heads)`, `optional`):
|
||||||
Mask to nullify selected heads of the self-attention modules.
|
Mask to nullify selected heads of the self-attention modules.
|
||||||
Mask values selected in ``[0, 1]``:
|
Mask values selected in ``[0, 1]``:
|
||||||
:obj:`1` indicates the head is **not masked**, :obj:`0` indicates the head is **masked**.
|
|
||||||
inputs_embeds (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`, `optional`, defaults to :obj:`None`):
|
- 1 indicates the head is **not masked**,
|
||||||
|
- 0 indicates the head is **masked**.
|
||||||
|
|
||||||
|
inputs_embeds (:obj:`torch.FloatTensor` of shape :obj:`({0}, hidden_size)`, `optional`):
|
||||||
Optionally, instead of passing :obj:`input_ids` you can choose to directly pass an embedded representation.
|
Optionally, instead of passing :obj:`input_ids` you can choose to directly pass an embedded representation.
|
||||||
This is useful if you want more control over how to convert `input_ids` indices into associated vectors
|
This is useful if you want more control over how to convert :obj:`input_ids` indices into associated
|
||||||
than the model's internal embedding lookup matrix.
|
vectors than the model's internal embedding lookup matrix.
|
||||||
output_attentions (:obj:`bool`, `optional`, defaults to :obj:`None`):
|
output_attentions (:obj:`bool`, `optional`):
|
||||||
If set to ``True``, the attentions tensors of all attention layers are returned. See ``attentions`` under returned tensors for more detail.
|
Whether or not to return the attentions tensors of all attention layers. See ``attentions`` under returned
|
||||||
output_hidden_states (:obj:`bool`, `optional`, defaults to :obj:`None`):
|
tensors for more detail.
|
||||||
If set to ``True``, the hidden states of all layers are returned. See ``hidden_states`` under returned tensors for more detail.
|
output_hidden_states (:obj:`bool`, `optional`):
|
||||||
return_dict (:obj:`bool`, `optional`, defaults to :obj:`None`):
|
Whether or not to return the hidden states of all layers. See ``hidden_states`` under returned tensors for
|
||||||
If set to ``True``, the model will return a :class:`~transformers.file_utils.ModelOutput` instead of a
|
more detail.
|
||||||
plain tuple.
|
return_dict (:obj:`bool`, `optional`):
|
||||||
|
Whether or not to return a :class:`~transformers.file_utils.ModelOutput` instead of a plain tuple.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@@ -625,7 +635,7 @@ class SqueezeBertForMaskedLM(SqueezeBertPreTrainedModel):
|
|||||||
return_dict=None,
|
return_dict=None,
|
||||||
):
|
):
|
||||||
r"""
|
r"""
|
||||||
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
|
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
|
||||||
Labels for computing the masked language modeling loss.
|
Labels for computing the masked language modeling loss.
|
||||||
Indices should be in ``[-100, 0, ..., config.vocab_size]`` (see ``input_ids`` docstring)
|
Indices should be in ``[-100, 0, ..., config.vocab_size]`` (see ``input_ids`` docstring)
|
||||||
Tokens with indices set to ``-100`` are ignored (masked), the loss is only computed for the tokens with labels
|
Tokens with indices set to ``-100`` are ignored (masked), the loss is only computed for the tokens with labels
|
||||||
@@ -702,7 +712,7 @@ class SqueezeBertForSequenceClassification(SqueezeBertPreTrainedModel):
|
|||||||
return_dict=None,
|
return_dict=None,
|
||||||
):
|
):
|
||||||
r"""
|
r"""
|
||||||
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`):
|
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`):
|
||||||
Labels for computing the sequence classification/regression loss.
|
Labels for computing the sequence classification/regression loss.
|
||||||
Indices should be in :obj:`[0, ..., config.num_labels - 1]`.
|
Indices should be in :obj:`[0, ..., config.num_labels - 1]`.
|
||||||
If :obj:`config.num_labels == 1` a regression loss is computed (Mean-Square loss),
|
If :obj:`config.num_labels == 1` a regression loss is computed (Mean-Square loss),
|
||||||
@@ -787,7 +797,7 @@ class SqueezeBertForMultipleChoice(SqueezeBertPreTrainedModel):
|
|||||||
return_dict=None,
|
return_dict=None,
|
||||||
):
|
):
|
||||||
r"""
|
r"""
|
||||||
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`):
|
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`):
|
||||||
Labels for computing the multiple choice classification loss.
|
Labels for computing the multiple choice classification loss.
|
||||||
Indices should be in ``[0, ..., num_choices-1]`` where `num_choices` is the size of the second dimension
|
Indices should be in ``[0, ..., num_choices-1]`` where `num_choices` is the size of the second dimension
|
||||||
of the input tensors. (see `input_ids` above)
|
of the input tensors. (see `input_ids` above)
|
||||||
@@ -877,7 +887,7 @@ class SqueezeBertForTokenClassification(SqueezeBertPreTrainedModel):
|
|||||||
return_dict=None,
|
return_dict=None,
|
||||||
):
|
):
|
||||||
r"""
|
r"""
|
||||||
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
|
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
|
||||||
Labels for computing the token classification loss.
|
Labels for computing the token classification loss.
|
||||||
Indices should be in ``[0, ..., config.num_labels - 1]``.
|
Indices should be in ``[0, ..., config.num_labels - 1]``.
|
||||||
"""
|
"""
|
||||||
@@ -927,8 +937,9 @@ class SqueezeBertForTokenClassification(SqueezeBertPreTrainedModel):
|
|||||||
|
|
||||||
|
|
||||||
@add_start_docstrings(
|
@add_start_docstrings(
|
||||||
"""SqueezeBERT Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear
|
"""SqueezeBERT Model with a span classification head on top for extractive question-answering tasks like SQuAD
|
||||||
layers on top of the hidden-states output to compute `span start logits` and `span end logits`). """,
|
(a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`).
|
||||||
|
""",
|
||||||
SQUEEZEBERT_START_DOCSTRING,
|
SQUEEZEBERT_START_DOCSTRING,
|
||||||
)
|
)
|
||||||
class SqueezeBertForQuestionAnswering(SqueezeBertPreTrainedModel):
|
class SqueezeBertForQuestionAnswering(SqueezeBertPreTrainedModel):
|
||||||
@@ -963,11 +974,11 @@ class SqueezeBertForQuestionAnswering(SqueezeBertPreTrainedModel):
|
|||||||
return_dict=None,
|
return_dict=None,
|
||||||
):
|
):
|
||||||
r"""
|
r"""
|
||||||
start_positions (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`):
|
start_positions (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`):
|
||||||
Labels for position (index) of the start of the labelled span for computing the token classification loss.
|
Labels for position (index) of the start of the labelled span for computing the token classification loss.
|
||||||
Positions are clamped to the length of the sequence (`sequence_length`).
|
Positions are clamped to the length of the sequence (`sequence_length`).
|
||||||
Position outside of the sequence are not taken into account for computing the loss.
|
Position outside of the sequence are not taken into account for computing the loss.
|
||||||
end_positions (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`):
|
end_positions (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`):
|
||||||
Labels for position (index) of the end of the labelled span for computing the token classification loss.
|
Labels for position (index) of the end of the labelled span for computing the token classification loss.
|
||||||
Positions are clamped to the length of the sequence (`sequence_length`).
|
Positions are clamped to the length of the sequence (`sequence_length`).
|
||||||
Position outside of the sequence are not taken into account for computing the loss.
|
Position outside of the sequence are not taken into account for computing the loss.
|
||||||
|
|||||||
@@ -85,31 +85,63 @@ if TYPE_CHECKING:
|
|||||||
logger = logging.get_logger(__name__)
|
logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def get_framework(model=None):
|
def get_framework(model):
|
||||||
"""
|
"""
|
||||||
Select framework (TensorFlow or PyTorch) to use.
|
Select framework (TensorFlow or PyTorch) to use.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
model (:obj:`str`, :class:`~transformers.PreTrainedModel` or :class:`~transformers.TFPreTrainedModel`, `optional`):
|
model (:obj:`str`, :class:`~transformers.PreTrainedModel` or :class:`~transformers.TFPreTrainedModel`):
|
||||||
If both frameworks are installed, picks the one corresponding to the model passed (either a model class or
|
If both frameworks are installed, picks the one corresponding to the model passed (either a model class or
|
||||||
the model name). If no specific model is provided, defaults to using PyTorch.
|
the model name). If no specific model is provided, defaults to using PyTorch.
|
||||||
"""
|
"""
|
||||||
if is_tf_available() and is_torch_available() and model is not None and not isinstance(model, str):
|
if not is_tf_available() and not is_torch_available():
|
||||||
# Both framework are available but the user supplied a model class instance.
|
|
||||||
# Try to guess which framework to use from the model classname
|
|
||||||
framework = "tf" if model.__class__.__name__.startswith("TF") else "pt"
|
|
||||||
elif not is_tf_available() and not is_torch_available():
|
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"At least one of TensorFlow 2.0 or PyTorch should be installed. "
|
"At least one of TensorFlow 2.0 or PyTorch should be installed. "
|
||||||
"To install TensorFlow 2.0, read the instructions at https://www.tensorflow.org/install/ "
|
"To install TensorFlow 2.0, read the instructions at https://www.tensorflow.org/install/ "
|
||||||
"To install PyTorch, read the instructions at https://pytorch.org/."
|
"To install PyTorch, read the instructions at https://pytorch.org/."
|
||||||
)
|
)
|
||||||
|
if isinstance(model, str):
|
||||||
|
if is_torch_available() and not is_tf_available():
|
||||||
|
model = AutoModel.from_pretrained(model)
|
||||||
|
elif is_tf_available() and not is_torch_available():
|
||||||
|
model = TFAutoModel.from_pretrained(model)
|
||||||
else:
|
else:
|
||||||
# framework = 'tf' if is_tf_available() else 'pt'
|
try:
|
||||||
framework = "pt" if is_torch_available() else "tf"
|
model = AutoModel.from_pretrained(model)
|
||||||
|
except OSError:
|
||||||
|
model = TFAutoModel.from_pretrained(model)
|
||||||
|
|
||||||
|
framework = "tf" if model.__class__.__name__.startswith("TF") else "pt"
|
||||||
return framework
|
return framework
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_model(targeted_task: Dict, framework: Optional[str]) -> str:
|
||||||
|
"""
|
||||||
|
Select a default model to use for a given task. Defaults to pytorch if ambiguous.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
targeted_task (:obj:`Dict` ):
|
||||||
|
Dictionnary representing the given task, that should contain default models
|
||||||
|
|
||||||
|
framework (:obj:`str`, None)
|
||||||
|
"pt", "tf" or None, representing a specific framework if it was specified, or None if we don't know yet.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
|
||||||
|
:obj:`str` The model string representing the default model for this pipeline
|
||||||
|
"""
|
||||||
|
if is_torch_available() and not is_tf_available():
|
||||||
|
framework = "pt"
|
||||||
|
elif is_tf_available() and not is_torch_available():
|
||||||
|
framework = "tf"
|
||||||
|
|
||||||
|
default_models = targeted_task["default"]["model"]
|
||||||
|
if framework is None:
|
||||||
|
framework = "pt"
|
||||||
|
|
||||||
|
return default_models[framework]
|
||||||
|
|
||||||
|
|
||||||
class PipelineException(Exception):
|
class PipelineException(Exception):
|
||||||
"""
|
"""
|
||||||
Raised by a :class:`~transformers.Pipeline` when handling __call__.
|
Raised by a :class:`~transformers.Pipeline` when handling __call__.
|
||||||
@@ -2678,14 +2710,16 @@ def pipeline(
|
|||||||
if task not in SUPPORTED_TASKS:
|
if task not in SUPPORTED_TASKS:
|
||||||
raise KeyError("Unknown task {}, available tasks are {}".format(task, list(SUPPORTED_TASKS.keys())))
|
raise KeyError("Unknown task {}, available tasks are {}".format(task, list(SUPPORTED_TASKS.keys())))
|
||||||
|
|
||||||
framework = framework or get_framework(model)
|
|
||||||
|
|
||||||
targeted_task = SUPPORTED_TASKS[task]
|
targeted_task = SUPPORTED_TASKS[task]
|
||||||
task_class, model_class = targeted_task["impl"], targeted_task[framework]
|
|
||||||
|
|
||||||
# Use default model/config/tokenizer for the task if no model is provided
|
# Use default model/config/tokenizer for the task if no model is provided
|
||||||
if model is None:
|
if model is None:
|
||||||
model = targeted_task["default"]["model"][framework]
|
# At that point framework might still be undetermined
|
||||||
|
model = get_default_model(targeted_task, framework)
|
||||||
|
|
||||||
|
framework = framework or get_framework(model)
|
||||||
|
|
||||||
|
task_class, model_class = targeted_task["impl"], targeted_task[framework]
|
||||||
|
|
||||||
# Try to infer tokenizer from model or config name (if provided as str)
|
# Try to infer tokenizer from model or config name (if provided as str)
|
||||||
if tokenizer is None:
|
if tokenizer is None:
|
||||||
|
|||||||
@@ -184,13 +184,23 @@ def require_faiss(test_case):
|
|||||||
return test_case
|
return test_case
|
||||||
|
|
||||||
|
|
||||||
def get_tests_dir():
|
def get_tests_dir(append_path=None):
|
||||||
"""
|
"""
|
||||||
returns the full path to the `tests` dir, so that the tests can be invoked from anywhere
|
Args:
|
||||||
|
append_path: optional path to append to the tests dir path
|
||||||
|
|
||||||
|
Return:
|
||||||
|
The full path to the `tests` dir, so that the tests can be invoked from anywhere.
|
||||||
|
Optionally `append_path` is joined after the `tests` dir the former is provided.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# this function caller's __file__
|
# this function caller's __file__
|
||||||
caller__file__ = inspect.stack()[1][1]
|
caller__file__ = inspect.stack()[1][1]
|
||||||
return os.path.abspath(os.path.dirname(caller__file__))
|
tests_dir = os.path.abspath(os.path.dirname(caller__file__))
|
||||||
|
if append_path:
|
||||||
|
return os.path.join(tests_dir, append_path)
|
||||||
|
else:
|
||||||
|
return tests_dir
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -345,49 +345,50 @@ class AlbertTokenizer(PreTrainedTokenizer):
|
|||||||
|
|
||||||
class AlbertTokenizerFast(PreTrainedTokenizerFast):
|
class AlbertTokenizerFast(PreTrainedTokenizerFast):
|
||||||
"""
|
"""
|
||||||
Constructs a Fast ALBERT tokenizer. Based on `SentencePiece <https://github.com/google/sentencepiece>`__
|
Construct a "fast" ALBERT tokenizer (backed by HuggingFace's `tokenizers` library). Based on
|
||||||
|
`SentencePiece <https://github.com/google/sentencepiece>`__.
|
||||||
|
|
||||||
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the methods. Users
|
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the main
|
||||||
should refer to the superclass for more information regarding methods.
|
methods. Users should refer to this superclass for more information regarding those methods.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
vocab_file (:obj:`string`):
|
vocab_file (:obj:`str`):
|
||||||
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a .spm extension) that
|
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a `.spm` extension) that
|
||||||
contains the vocabulary necessary to instantiate a tokenizer.
|
contains the vocabulary necessary to instantiate a tokenizer.
|
||||||
do_lower_case (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
do_lower_case (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
Whether to lowercase the input when tokenizing.
|
Whether or not to lowercase the input when tokenizing.
|
||||||
remove_space (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
remove_space (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
Whether to strip the text when tokenizing (removing excess spaces before and after the string).
|
Whether or not to strip the text when tokenizing (removing excess spaces before and after the string).
|
||||||
keep_accents (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
keep_accents (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Whether to keep accents when tokenizing.
|
Whether or not to keep accents when tokenizing.
|
||||||
bos_token (:obj:`string`, `optional`, defaults to "[CLS]"):
|
bos_token (:obj:`str`, `optional`, defaults to :obj:`"[CLS]"`):
|
||||||
The beginning of sequence token that was used during pre-training. Can be used a sequence classifier token.
|
The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the beginning
|
When building a sequence using special tokens, this is not the token that is used for the beginning
|
||||||
of sequence. The token used is the :obj:`cls_token`.
|
of sequence. The token used is the :obj:`cls_token`.
|
||||||
eos_token (:obj:`string`, `optional`, defaults to "[SEP]"):
|
eos_token (:obj:`str`, `optional`, defaults to :obj:`"[SEP]"`):
|
||||||
The end of sequence token.
|
The end of sequence token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the end
|
When building a sequence using special tokens, this is not the token that is used for the end
|
||||||
of sequence. The token used is the :obj:`sep_token`.
|
of sequence. The token used is the :obj:`sep_token`.
|
||||||
unk_token (:obj:`string`, `optional`, defaults to "<unk>"):
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
||||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||||
token instead.
|
token instead.
|
||||||
sep_token (:obj:`string`, `optional`, defaults to "[SEP]"):
|
sep_token (:obj:`str`, `optional`, defaults to :obj:`"[SEP]"`):
|
||||||
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
||||||
for sequence classification or for a text and a question for question answering.
|
for sequence classification or for a text and a question for question answering.
|
||||||
It is also used as the last token of a sequence built with special tokens.
|
It is also used as the last token of a sequence built with special tokens.
|
||||||
pad_token (:obj:`string`, `optional`, defaults to "<pad>"):
|
pad_token (:obj:`str`, `optional`, defaults to :obj:`"<pad>"`):
|
||||||
The token used for padding, for example when batching sequences of different lengths.
|
The token used for padding, for example when batching sequences of different lengths.
|
||||||
cls_token (:obj:`string`, `optional`, defaults to "[CLS]"):
|
cls_token (:obj:`str`, `optional`, defaults to :obj:`"[CLS]"`):
|
||||||
The classifier token which is used when doing sequence classification (classification of the whole
|
The classifier token which is used when doing sequence classification (classification of the whole
|
||||||
sequence instead of per-token classification). It is the first token of the sequence when built with
|
sequence instead of per-token classification). It is the first token of the sequence when built with
|
||||||
special tokens.
|
special tokens.
|
||||||
mask_token (:obj:`string`, `optional`, defaults to "[MASK]"):
|
mask_token (:obj:`str`, `optional`, defaults to :obj:`"[MASK]"`):
|
||||||
The token used for masking values. This is the token used when training this model with masked language
|
The token used for masking values. This is the token used when training this model with masked language
|
||||||
modeling. This is the token which the model will try to predict.
|
modeling. This is the token which the model will try to predict.
|
||||||
|
|
||||||
@@ -449,12 +450,12 @@ class AlbertTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of IDs to which the special tokens will be added
|
List of IDs to which the special tokens will be added.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
:obj:`List[int]`: List of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
||||||
"""
|
"""
|
||||||
sep = [self.sep_token_id]
|
sep = [self.sep_token_id]
|
||||||
cls = [self.cls_token_id]
|
cls = [self.cls_token_id]
|
||||||
@@ -466,21 +467,20 @@ class AlbertTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
||||||
special tokens using the tokenizer ``prepare_for_model`` method.
|
special tokens using the tokenizer ``prepare_for_model`` method.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of IDs.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Set to True if the token list is already formatted with special tokens for the model
|
Whether or not the token list is already formatted with special tokens for the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if already_has_special_tokens:
|
if already_has_special_tokens:
|
||||||
if token_ids_1 is not None:
|
if token_ids_1 is not None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
@@ -497,7 +497,7 @@ class AlbertTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Creates a mask from the two sequences passed to be used in a sequence-pair classification task.
|
Create a mask from the two sequences passed to be used in a sequence-pair classification task.
|
||||||
An ALBERT sequence pair mask has the following format:
|
An ALBERT sequence pair mask has the following format:
|
||||||
|
|
||||||
::
|
::
|
||||||
@@ -505,12 +505,12 @@ class AlbertTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
||||||
| first sequence | second sequence |
|
| first sequence | second sequence |
|
||||||
|
|
||||||
if token_ids_1 is None, only returns the first portion of the mask (0s).
|
If :obj:`token_ids_1` is :obj:`None`, this method only returns the first portion of the mask (0s).
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of IDs.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|||||||
@@ -1,263 +0,0 @@
|
|||||||
# coding=utf-8
|
|
||||||
# Copyright 2018 Google AI, Google Brain and the HuggingFace Inc. team.
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
""" Tokenization classes for ALBERT model."""
|
|
||||||
|
|
||||||
|
|
||||||
import os
|
|
||||||
from shutil import copyfile
|
|
||||||
from typing import List, Optional
|
|
||||||
|
|
||||||
from .tokenization_utils_fast import PreTrainedTokenizerFast
|
|
||||||
from .file_utils import is_sentencepiece_available, is_tokenizers_available
|
|
||||||
from .utils import logging
|
|
||||||
|
|
||||||
if is_sentencepiece_available():
|
|
||||||
from .tokenization_albert import AlbertTokenizer
|
|
||||||
else:
|
|
||||||
AlbertTokenizer = None
|
|
||||||
|
|
||||||
logger = logging.get_logger(__name__)
|
|
||||||
VOCAB_FILES_NAMES = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}
|
|
||||||
|
|
||||||
PRETRAINED_VOCAB_FILES_MAP = {
|
|
||||||
"tokenizer_file": {
|
|
||||||
"albert-base-v1": "https://s3.amazonaws.com/models.huggingface.co/bert/albert-base-v1-tokenizer.json",
|
|
||||||
"albert-large-v1": "https://s3.amazonaws.com/models.huggingface.co/bert/albert-large-v1-tokenizer.json",
|
|
||||||
"albert-xlarge-v1": "https://s3.amazonaws.com/models.huggingface.co/bert/albert-xlarge-v1-tokenizer.json",
|
|
||||||
"albert-xxlarge-v1": "https://s3.amazonaws.com/models.huggingface.co/bert/albert-xxlarge-v1-tokenizer.json",
|
|
||||||
"albert-base-v2": "https://s3.amazonaws.com/models.huggingface.co/bert/albert-base-v2-tokenizer.json",
|
|
||||||
"albert-large-v2": "https://s3.amazonaws.com/models.huggingface.co/bert/albert-large-v2-tokenizer.json",
|
|
||||||
"albert-xlarge-v2": "https://s3.amazonaws.com/models.huggingface.co/bert/albert-xlarge-v2-tokenizer.json",
|
|
||||||
"albert-xxlarge-v2": "https://s3.amazonaws.com/models.huggingface.co/bert/albert-xxlarge-v2-tokenizer.json",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {
|
|
||||||
"albert-base-v1": 512,
|
|
||||||
"albert-large-v1": 512,
|
|
||||||
"albert-xlarge-v1": 512,
|
|
||||||
"albert-xxlarge-v1": 512,
|
|
||||||
"albert-base-v2": 512,
|
|
||||||
"albert-large-v2": 512,
|
|
||||||
"albert-xlarge-v2": 512,
|
|
||||||
"albert-xxlarge-v2": 512,
|
|
||||||
}
|
|
||||||
|
|
||||||
SPIECE_UNDERLINE = "▁"
|
|
||||||
|
|
||||||
|
|
||||||
class AlbertTokenizerFast(PreTrainedTokenizerFast):
|
|
||||||
"""
|
|
||||||
Constructs a Fast ALBERT tokenizer. Based on `SentencePiece <https://github.com/google/sentencepiece>`__
|
|
||||||
|
|
||||||
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the methods. Users
|
|
||||||
should refer to the superclass for more information regarding methods.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
vocab_file (:obj:`string`):
|
|
||||||
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a .spm extension) that
|
|
||||||
contains the vocabulary necessary to instantiate a tokenizer.
|
|
||||||
do_lower_case (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
|
||||||
Whether to lowercase the input when tokenizing.
|
|
||||||
remove_space (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
|
||||||
Whether to strip the text when tokenizing (removing excess spaces before and after the string).
|
|
||||||
keep_accents (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
|
||||||
Whether to keep accents when tokenizing.
|
|
||||||
bos_token (:obj:`string`, `optional`, defaults to "[CLS]"):
|
|
||||||
The beginning of sequence token that was used during pre-training. Can be used a sequence classifier token.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the beginning
|
|
||||||
of sequence. The token used is the :obj:`cls_token`.
|
|
||||||
eos_token (:obj:`string`, `optional`, defaults to "[SEP]"):
|
|
||||||
The end of sequence token.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the end
|
|
||||||
of sequence. The token used is the :obj:`sep_token`.
|
|
||||||
unk_token (:obj:`string`, `optional`, defaults to "<unk>"):
|
|
||||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
|
||||||
token instead.
|
|
||||||
sep_token (:obj:`string`, `optional`, defaults to "[SEP]"):
|
|
||||||
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
|
||||||
for sequence classification or for a text and a question for question answering.
|
|
||||||
It is also used as the last token of a sequence built with special tokens.
|
|
||||||
pad_token (:obj:`string`, `optional`, defaults to "<pad>"):
|
|
||||||
The token used for padding, for example when batching sequences of different lengths.
|
|
||||||
cls_token (:obj:`string`, `optional`, defaults to "[CLS]"):
|
|
||||||
The classifier token which is used when doing sequence classification (classification of the whole
|
|
||||||
sequence instead of per-token classification). It is the first token of the sequence when built with
|
|
||||||
special tokens.
|
|
||||||
mask_token (:obj:`string`, `optional`, defaults to "[MASK]"):
|
|
||||||
The token used for masking values. This is the token used when training this model with masked language
|
|
||||||
modeling. This is the token which the model will try to predict.
|
|
||||||
|
|
||||||
Attributes:
|
|
||||||
sp_model (:obj:`SentencePieceProcessor`):
|
|
||||||
The `SentencePiece` processor that is used for every conversion (string, tokens and IDs).
|
|
||||||
"""
|
|
||||||
|
|
||||||
vocab_files_names = VOCAB_FILES_NAMES
|
|
||||||
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
|
||||||
max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
|
|
||||||
slow_tokenizer_class = AlbertTokenizer
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
vocab_file,
|
|
||||||
tokenizer_file,
|
|
||||||
do_lower_case=True,
|
|
||||||
remove_space=True,
|
|
||||||
keep_accents=False,
|
|
||||||
bos_token="[CLS]",
|
|
||||||
eos_token="[SEP]",
|
|
||||||
unk_token="<unk>",
|
|
||||||
sep_token="[SEP]",
|
|
||||||
pad_token="<pad>",
|
|
||||||
cls_token="[CLS]",
|
|
||||||
mask_token="[MASK]",
|
|
||||||
**kwargs
|
|
||||||
):
|
|
||||||
super().__init__(
|
|
||||||
vocab_file,
|
|
||||||
tokenizer_file,
|
|
||||||
do_lower_case=do_lower_case,
|
|
||||||
remove_space=remove_space,
|
|
||||||
keep_accents=keep_accents,
|
|
||||||
bos_token=bos_token,
|
|
||||||
eos_token=eos_token,
|
|
||||||
unk_token=unk_token,
|
|
||||||
sep_token=sep_token,
|
|
||||||
pad_token=pad_token,
|
|
||||||
cls_token=cls_token,
|
|
||||||
mask_token=mask_token,
|
|
||||||
**kwargs,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.do_lower_case = do_lower_case
|
|
||||||
self.remove_space = remove_space
|
|
||||||
self.keep_accents = keep_accents
|
|
||||||
self.vocab_file = vocab_file
|
|
||||||
|
|
||||||
def build_inputs_with_special_tokens(
|
|
||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
|
||||||
) -> List[int]:
|
|
||||||
"""
|
|
||||||
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
|
||||||
by concatenating and adding special tokens.
|
|
||||||
An ALBERT sequence has the following format:
|
|
||||||
|
|
||||||
- single sequence: ``[CLS] X [SEP]``
|
|
||||||
- pair of sequences: ``[CLS] A [SEP] B [SEP]``
|
|
||||||
|
|
||||||
Args:
|
|
||||||
token_ids_0 (:obj:`List[int]`):
|
|
||||||
List of IDs to which the special tokens will be added
|
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
|
||||||
Optional second list of IDs for sequence pairs.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
|
||||||
"""
|
|
||||||
sep = [self.sep_token_id]
|
|
||||||
cls = [self.cls_token_id]
|
|
||||||
if token_ids_1 is None:
|
|
||||||
return cls + token_ids_0 + sep
|
|
||||||
return cls + token_ids_0 + sep + token_ids_1 + sep
|
|
||||||
|
|
||||||
def get_special_tokens_mask(
|
|
||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
|
||||||
) -> List[int]:
|
|
||||||
"""
|
|
||||||
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
|
||||||
special tokens using the tokenizer ``prepare_for_model`` method.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
token_ids_0 (:obj:`List[int]`):
|
|
||||||
List of ids.
|
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
|
||||||
Optional second list of IDs for sequence pairs.
|
|
||||||
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
|
||||||
Set to True if the token list is already formatted with special tokens for the model
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if already_has_special_tokens:
|
|
||||||
if token_ids_1 is not None:
|
|
||||||
raise ValueError(
|
|
||||||
"You should not supply a second sequence if the provided sequence of "
|
|
||||||
"ids is already formatted with special tokens for the model."
|
|
||||||
)
|
|
||||||
return list(map(lambda x: 1 if x in [self.sep_token_id, self.cls_token_id] else 0, token_ids_0))
|
|
||||||
|
|
||||||
if token_ids_1 is not None:
|
|
||||||
return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1]
|
|
||||||
return [1] + ([0] * len(token_ids_0)) + [1]
|
|
||||||
|
|
||||||
def create_token_type_ids_from_sequences(
|
|
||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
|
||||||
) -> List[int]:
|
|
||||||
"""
|
|
||||||
Creates a mask from the two sequences passed to be used in a sequence-pair classification task.
|
|
||||||
An ALBERT sequence pair mask has the following format:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
|
||||||
| first sequence | second sequence |
|
|
||||||
|
|
||||||
if token_ids_1 is None, only returns the first portion of the mask (0s).
|
|
||||||
|
|
||||||
Args:
|
|
||||||
token_ids_0 (:obj:`List[int]`):
|
|
||||||
List of ids.
|
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
|
||||||
Optional second list of IDs for sequence pairs.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
:obj:`List[int]`: List of `token type IDs <../glossary.html#token-type-ids>`_ according to the given
|
|
||||||
sequence(s).
|
|
||||||
"""
|
|
||||||
sep = [self.sep_token_id]
|
|
||||||
cls = [self.cls_token_id]
|
|
||||||
|
|
||||||
if token_ids_1 is None:
|
|
||||||
return len(cls + token_ids_0 + sep) * [0]
|
|
||||||
return len(cls + token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1]
|
|
||||||
|
|
||||||
def save_vocabulary(self, save_directory):
|
|
||||||
"""
|
|
||||||
Save the sentencepiece vocabulary (copy original file) and special tokens file to a directory.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
save_directory (:obj:`str`):
|
|
||||||
The directory in which to save the vocabulary.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
:obj:`Tuple(str)`: Paths to the files saved.
|
|
||||||
"""
|
|
||||||
if not os.path.isdir(save_directory):
|
|
||||||
logger.error("Vocabulary path ({}) should be a directory".format(save_directory))
|
|
||||||
return
|
|
||||||
out_vocab_file = os.path.join(save_directory, VOCAB_FILES_NAMES["vocab_file"])
|
|
||||||
|
|
||||||
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file):
|
|
||||||
copyfile(self.vocab_file, out_vocab_file)
|
|
||||||
|
|
||||||
return (out_vocab_file,)
|
|
||||||
@@ -23,6 +23,7 @@ from .configuration_auto import (
|
|||||||
BartConfig,
|
BartConfig,
|
||||||
BertConfig,
|
BertConfig,
|
||||||
BertGenerationConfig,
|
BertGenerationConfig,
|
||||||
|
BlenderbotConfig,
|
||||||
CamembertConfig,
|
CamembertConfig,
|
||||||
CTRLConfig,
|
CTRLConfig,
|
||||||
DebertaConfig,
|
DebertaConfig,
|
||||||
@@ -61,6 +62,7 @@ from .tokenization_bert import BertTokenizer, BertTokenizerFast
|
|||||||
from .tokenization_bert_generation import BertGenerationTokenizer
|
from .tokenization_bert_generation import BertGenerationTokenizer
|
||||||
from .tokenization_bert_japanese import BertJapaneseTokenizer
|
from .tokenization_bert_japanese import BertJapaneseTokenizer
|
||||||
from .tokenization_bertweet import BertweetTokenizer
|
from .tokenization_bertweet import BertweetTokenizer
|
||||||
|
from .tokenization_blenderbot import BlenderbotSmallTokenizer
|
||||||
from .tokenization_camembert import CamembertTokenizer, CamembertTokenizerFast
|
from .tokenization_camembert import CamembertTokenizer, CamembertTokenizerFast
|
||||||
from .tokenization_ctrl import CTRLTokenizer
|
from .tokenization_ctrl import CTRLTokenizer
|
||||||
from .tokenization_deberta import DebertaTokenizer
|
from .tokenization_deberta import DebertaTokenizer
|
||||||
@@ -108,6 +110,8 @@ TOKENIZER_MAPPING = OrderedDict(
|
|||||||
(MBartConfig, (MBartTokenizer, MBartTokenizerFast)),
|
(MBartConfig, (MBartTokenizer, MBartTokenizerFast)),
|
||||||
(XLMRobertaConfig, (XLMRobertaTokenizer, XLMRobertaTokenizerFast)),
|
(XLMRobertaConfig, (XLMRobertaTokenizer, XLMRobertaTokenizerFast)),
|
||||||
(MarianConfig, (MarianTokenizer, None)),
|
(MarianConfig, (MarianTokenizer, None)),
|
||||||
|
(BlenderbotConfig, (BlenderbotSmallTokenizer, None)),
|
||||||
|
(LongformerConfig, (LongformerTokenizer, None)),
|
||||||
(BartConfig, (BartTokenizer, BartTokenizerFast)),
|
(BartConfig, (BartTokenizer, BartTokenizerFast)),
|
||||||
(LongformerConfig, (LongformerTokenizer, LongformerTokenizerFast)),
|
(LongformerConfig, (LongformerTokenizer, LongformerTokenizerFast)),
|
||||||
(RobertaConfig, (BertweetTokenizer, None)),
|
(RobertaConfig, (BertweetTokenizer, None)),
|
||||||
|
|||||||
@@ -398,6 +398,7 @@ class BasicTokenizer(object):
|
|||||||
"""
|
"""
|
||||||
# union() returns a new set by concatenating the two sets.
|
# union() returns a new set by concatenating the two sets.
|
||||||
never_split = self.never_split.union(set(never_split)) if never_split else self.never_split
|
never_split = self.never_split.union(set(never_split)) if never_split else self.never_split
|
||||||
|
text = self._clean_text(text)
|
||||||
|
|
||||||
# This was added on November 1st, 2018 for the multilingual and Chinese
|
# This was added on November 1st, 2018 for the multilingual and Chinese
|
||||||
# models. This is also applied to the English models now, but it doesn't
|
# models. This is also applied to the English models now, but it doesn't
|
||||||
|
|||||||
@@ -0,0 +1,271 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# coding=utf-8
|
||||||
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
#
|
||||||
|
# This source code is licensed under the MIT license found in the;
|
||||||
|
# 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.
|
||||||
|
# LICENSE file in the root directory of this source tree.
|
||||||
|
""""BlenderbotTokenizer and BlenderbotSmallTokenizer"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from typing import Dict, List, Tuple
|
||||||
|
|
||||||
|
import regex as re
|
||||||
|
|
||||||
|
from .tokenization_roberta import RobertaTokenizer
|
||||||
|
from .tokenization_utils import PreTrainedTokenizer
|
||||||
|
from .utils import logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
VOCAB_FILES_NAMES = {
|
||||||
|
"vocab_file": "vocab.json",
|
||||||
|
"merges_file": "merges.txt",
|
||||||
|
# "tokenizer_config_file": "tokenizer_config.json",
|
||||||
|
}
|
||||||
|
CKPT_3B = "facebook/blenderbot-3B"
|
||||||
|
|
||||||
|
|
||||||
|
class BlenderbotTokenizer(RobertaTokenizer):
|
||||||
|
r"""
|
||||||
|
Construct a Blenderbot tokenizer.
|
||||||
|
|
||||||
|
:class:`~transformers.Blenderbot` is nearly identical to :class:`~transformers.RobertaTokenizer` and runs
|
||||||
|
end-to-end tokenization: punctuation splitting and wordpiece. The only difference is that it doesnt add BOS
|
||||||
|
token to the beginning of sequences.
|
||||||
|
|
||||||
|
Refer to superclass :class:`~transformers.RobertaTokenizer` for usage examples and documentation concerning
|
||||||
|
parameters.
|
||||||
|
"""
|
||||||
|
vocab_files_names = {
|
||||||
|
"vocab_file": "vocab.json",
|
||||||
|
"merges_file": "merges.txt",
|
||||||
|
"tokenizer_config_file": "tokenizer_config.json",
|
||||||
|
}
|
||||||
|
pretrained_vocab_files_map = {
|
||||||
|
"vocab_file": {CKPT_3B: "https://cdn.huggingface.co/facebook/blenderbot-3B/vocab.json"},
|
||||||
|
"merges_file": {CKPT_3B: "https://cdn.huggingface.co/facebook/blenderbot-3B/merges.txt"},
|
||||||
|
"tokenizer_config_file": {CKPT_3B: "https://cdn.huggingface.co/facebook/blenderbot-3B/tokenizer_config.json"},
|
||||||
|
}
|
||||||
|
max_model_input_sizes = {"facebook/blenderbot-3B": 128}
|
||||||
|
|
||||||
|
def build_inputs_with_special_tokens(self, token_ids_0: List[int], token_ids_1: List[int] = None):
|
||||||
|
"""
|
||||||
|
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
||||||
|
by concatenating and adding special tokens.
|
||||||
|
A Blenderbot sequence has the following format:
|
||||||
|
|
||||||
|
- single sequence: `` X </s>``
|
||||||
|
|
||||||
|
Args:
|
||||||
|
token_ids_0 (:obj:`List[int]`):
|
||||||
|
List of IDs to which the special tokens will be added
|
||||||
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
|
Will be ignored
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
||||||
|
"""
|
||||||
|
return token_ids_0 + [self.eos_token_id]
|
||||||
|
|
||||||
|
|
||||||
|
def get_pairs(word):
|
||||||
|
"""Return set of symbol pairs in a word.
|
||||||
|
|
||||||
|
Word is represented as tuple of symbols (symbols being variable-length strings).
|
||||||
|
"""
|
||||||
|
pairs = set()
|
||||||
|
prev_char = word[0]
|
||||||
|
for char in word[1:]:
|
||||||
|
pairs.add((prev_char, char))
|
||||||
|
prev_char = char
|
||||||
|
|
||||||
|
pairs = set(pairs)
|
||||||
|
return pairs
|
||||||
|
|
||||||
|
|
||||||
|
class BlenderbotSmallTokenizer(PreTrainedTokenizer):
|
||||||
|
"""
|
||||||
|
Constructs a Blenderbot-90M tokenizer based on BPE (Byte-Pair-Encoding)
|
||||||
|
|
||||||
|
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizer` which contains most of the main methods. Users
|
||||||
|
should refer to the superclass for more information regarding methods.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
vocab_file (:obj:`str`):
|
||||||
|
File containing the vocabulary.
|
||||||
|
merges_file (:obj:`str`):
|
||||||
|
Path to the merges file.
|
||||||
|
bos_token (:obj:`str`, `optional`, defaults to :obj:`"__start__"`):
|
||||||
|
The beginning of sentence token.
|
||||||
|
eos_token (:obj:`str`, `optional`, defaults to :obj:`"__end__"`):
|
||||||
|
The end of sentence token.
|
||||||
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"__unk__"`):
|
||||||
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
|
||||||
|
pad_token (:obj:`str`, `optional`, defaults to :obj:`"__pad__"`):
|
||||||
|
The token used for padding, for example when batching sequences of different lengths.
|
||||||
|
**kwargs
|
||||||
|
Additional keyword arguments passed along to :class:`~transformers.PreTrainedTokenizer`
|
||||||
|
"""
|
||||||
|
|
||||||
|
vocab_files_names = {"vocab_file": "vocab.json", "merges_file": "merges.txt"}
|
||||||
|
pretrained_vocab_files_map = {
|
||||||
|
"vocab_file": {"facebook/blenderbot-90M": "https://cdn.huggingface.co/facebook/blenderbot-90M/vocab.json"},
|
||||||
|
"merges_file": {"facebook/blenderbot-90M": "https://cdn.huggingface.co/facebook/blenderbot-90M/merges.txt"},
|
||||||
|
}
|
||||||
|
max_model_input_sizes = {"facebook/blenderbot-90M": 512}
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
vocab_file,
|
||||||
|
merges_file,
|
||||||
|
bos_token="__start__",
|
||||||
|
eos_token="__end__",
|
||||||
|
unk_token="__unk__",
|
||||||
|
pad_token="__null",
|
||||||
|
**kwargs
|
||||||
|
):
|
||||||
|
super().__init__(unk_token=unk_token, bos_token=bos_token, eos_token=eos_token, pad_token=pad_token, **kwargs)
|
||||||
|
|
||||||
|
with open(vocab_file, encoding="utf-8") as vocab_handle:
|
||||||
|
self.encoder = json.load(vocab_handle)
|
||||||
|
self.decoder = {v: k for k, v in self.encoder.items()}
|
||||||
|
with open(merges_file, encoding="utf-8") as merges_handle:
|
||||||
|
merges = merges_handle.read().split("\n")[1:-1]
|
||||||
|
merges = [tuple(merge.split()) for merge in merges]
|
||||||
|
self.bpe_ranks = dict(zip(merges, range(len(merges))))
|
||||||
|
self.cache = {}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def vocab_size(self) -> int:
|
||||||
|
return len(self.encoder)
|
||||||
|
|
||||||
|
def get_vocab(self) -> Dict:
|
||||||
|
return dict(self.encoder, **self.added_tokens_encoder)
|
||||||
|
|
||||||
|
def bpe(self, token: str) -> str:
|
||||||
|
if token in self.cache:
|
||||||
|
return self.cache[token]
|
||||||
|
token = re.sub("([.,!?()])", r" \1", token)
|
||||||
|
token = re.sub("(')", r" \1 ", token)
|
||||||
|
token = re.sub("\s{2,}", " ", token)
|
||||||
|
if "\n" in token:
|
||||||
|
token = token.replace("\n", " __newln__")
|
||||||
|
|
||||||
|
tokens = token.split(" ")
|
||||||
|
words = []
|
||||||
|
for token in tokens:
|
||||||
|
token = token.lower()
|
||||||
|
word = tuple(token)
|
||||||
|
word = tuple(list(word[:-1]) + [word[-1] + "</w>"])
|
||||||
|
pairs = get_pairs(word)
|
||||||
|
|
||||||
|
if not pairs:
|
||||||
|
words.append(token)
|
||||||
|
continue
|
||||||
|
|
||||||
|
while True:
|
||||||
|
bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf")))
|
||||||
|
if bigram not in self.bpe_ranks:
|
||||||
|
break
|
||||||
|
first, second = bigram
|
||||||
|
new_word = []
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while i < len(word):
|
||||||
|
try:
|
||||||
|
j = word.index(first, i)
|
||||||
|
new_word.extend(word[i:j])
|
||||||
|
i = j
|
||||||
|
except ValueError:
|
||||||
|
new_word.extend(word[i:])
|
||||||
|
break
|
||||||
|
|
||||||
|
if word[i] == first and i < len(word) - 1 and word[i + 1] == second:
|
||||||
|
new_word.append(first + second)
|
||||||
|
i += 2
|
||||||
|
else:
|
||||||
|
new_word.append(word[i])
|
||||||
|
i += 1
|
||||||
|
new_word = tuple(new_word)
|
||||||
|
word = new_word
|
||||||
|
if len(word) == 1:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
pairs = get_pairs(word)
|
||||||
|
word = "@@ ".join(word)
|
||||||
|
word = word[:-4]
|
||||||
|
|
||||||
|
self.cache[token] = word
|
||||||
|
words.append(word)
|
||||||
|
return " ".join(words)
|
||||||
|
|
||||||
|
def _tokenize(self, text: str) -> List[str]:
|
||||||
|
""" Split a string into tokens using BPE."""
|
||||||
|
split_tokens = []
|
||||||
|
|
||||||
|
words = re.findall(r"\S+\n?", text)
|
||||||
|
|
||||||
|
for token in words:
|
||||||
|
split_tokens.extend([t for t in self.bpe(token).split(" ")])
|
||||||
|
return split_tokens
|
||||||
|
|
||||||
|
def _convert_token_to_id(self, token: str) -> int:
|
||||||
|
""" Converts a token to an id using the vocab. """
|
||||||
|
token = token.lower()
|
||||||
|
return self.encoder.get(token, self.encoder.get(self.unk_token))
|
||||||
|
|
||||||
|
def _convert_id_to_token(self, index: int) -> str:
|
||||||
|
"""Converts an index (integer) in a token (str) using the vocab."""
|
||||||
|
return self.decoder.get(index, self.unk_token)
|
||||||
|
|
||||||
|
def convert_tokens_to_string(self, tokens: List[str]) -> str:
|
||||||
|
""" Converts a sequence of tokens in a single string. """
|
||||||
|
out_string = " ".join(tokens).replace("@@ ", "").strip()
|
||||||
|
return out_string
|
||||||
|
|
||||||
|
def save_vocabulary(self, save_directory: str) -> Tuple[str, str]:
|
||||||
|
"""
|
||||||
|
Save the vocabulary and special tokens file to a directory.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
save_directory (:obj:`str`):
|
||||||
|
The directory in which to save the vocabulary.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
:obj:`Tuple(str)`: Paths to the files saved.
|
||||||
|
"""
|
||||||
|
if not os.path.isdir(save_directory):
|
||||||
|
logger.error("Vocabulary path ({}) should be a directory".format(save_directory))
|
||||||
|
return
|
||||||
|
vocab_file = os.path.join(save_directory, VOCAB_FILES_NAMES["vocab_file"])
|
||||||
|
merge_file = os.path.join(save_directory, VOCAB_FILES_NAMES["merges_file"])
|
||||||
|
|
||||||
|
with open(vocab_file, "w", encoding="utf-8") as f:
|
||||||
|
f.write(json.dumps(self.encoder, ensure_ascii=False))
|
||||||
|
|
||||||
|
index = 0
|
||||||
|
with open(merge_file, "w", encoding="utf-8") as writer:
|
||||||
|
writer.write("#version: 0.2\n")
|
||||||
|
for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda kv: kv[1]):
|
||||||
|
if index != token_index:
|
||||||
|
logger.warning(
|
||||||
|
"Saving vocabulary to {}: BPE merge indices are not consecutive."
|
||||||
|
" Please check that the tokenizer is not corrupted!".format(merge_file)
|
||||||
|
)
|
||||||
|
index = token_index
|
||||||
|
writer.write(" ".join(bpe_tokens) + "\n")
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
return vocab_file, merge_file
|
||||||
@@ -293,43 +293,44 @@ class CamembertTokenizer(PreTrainedTokenizer):
|
|||||||
|
|
||||||
class CamembertTokenizerFast(PreTrainedTokenizerFast):
|
class CamembertTokenizerFast(PreTrainedTokenizerFast):
|
||||||
"""
|
"""
|
||||||
Adapted from RobertaTokenizer and XLNetTokenizer
|
Construct a "fast" CamemBERT tokenizer (backed by HuggingFace's `tokenizers` library). Adapted from
|
||||||
SentencePiece based tokenizer.
|
:class:`~transformers.RobertaTokenizer` and :class:`~transformers.XLNetTokenizer`. Based on `SentencePiece
|
||||||
|
<https://github.com/google/sentencepiece>`__.
|
||||||
|
|
||||||
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the methods. Users
|
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the main
|
||||||
should refer to the superclass for more information regarding methods.
|
methods. Users should refer to this superclass for more information regarding those methods.
|
||||||
|
|
||||||
Args:
|
|
||||||
vocab_file (:obj:`str`):
|
vocab_file (:obj:`str`):
|
||||||
Path to the vocabulary file.
|
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a `.spm` extension) that
|
||||||
bos_token (:obj:`string`, `optional`, defaults to "<s>"):
|
contains the vocabulary necessary to instantiate a tokenizer.
|
||||||
The beginning of sequence token that was used during pre-training. Can be used a sequence classifier token.
|
bos_token (:obj:`str`, `optional`, defaults to :obj:`"<s>"`):
|
||||||
|
The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the beginning
|
When building a sequence using special tokens, this is not the token that is used for the beginning
|
||||||
of sequence. The token used is the :obj:`cls_token`.
|
of sequence. The token used is the :obj:`cls_token`.
|
||||||
eos_token (:obj:`string`, `optional`, defaults to "</s>"):
|
eos_token (:obj:`str`, `optional`, defaults to :obj:`"</s>"`):
|
||||||
The end of sequence token.
|
The end of sequence token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the end
|
When building a sequence using special tokens, this is not the token that is used for the end
|
||||||
of sequence. The token used is the :obj:`sep_token`.
|
of sequence. The token used is the :obj:`sep_token`.
|
||||||
sep_token (:obj:`string`, `optional`, defaults to "</s>"):
|
sep_token (:obj:`str`, `optional`, defaults to :obj:`"</s>"`):
|
||||||
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
||||||
for sequence classification or for a text and a question for question answering.
|
for sequence classification or for a text and a question for question answering.
|
||||||
It is also used as the last token of a sequence built with special tokens.
|
It is also used as the last token of a sequence built with special tokens.
|
||||||
cls_token (:obj:`string`, `optional`, defaults to "<s>"):
|
cls_token (:obj:`str`, `optional`, defaults to :obj:`"<s>"`):
|
||||||
The classifier token which is used when doing sequence classification (classification of the whole
|
The classifier token which is used when doing sequence classification (classification of the whole
|
||||||
sequence instead of per-token classification). It is the first token of the sequence when built with
|
sequence instead of per-token classification). It is the first token of the sequence when built with
|
||||||
special tokens.
|
special tokens.
|
||||||
unk_token (:obj:`string`, `optional`, defaults to "<unk>"):
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
||||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||||
token instead.
|
token instead.
|
||||||
pad_token (:obj:`string`, `optional`, defaults to "<pad>"):
|
pad_token (:obj:`str`, `optional`, defaults to :obj:`"<pad>"`):
|
||||||
The token used for padding, for example when batching sequences of different lengths.
|
The token used for padding, for example when batching sequences of different lengths.
|
||||||
mask_token (:obj:`string`, `optional`, defaults to "<mask>"):
|
mask_token (:obj:`str`, `optional`, defaults to :obj:`"<mask>"`):
|
||||||
The token used for masking values. This is the token used when training this model with masked language
|
The token used for masking values. This is the token used when training this model with masked language
|
||||||
modeling. This is the token which the model will try to predict.
|
modeling. This is the token which the model will try to predict.
|
||||||
additional_special_tokens (:obj:`List[str]`, `optional`, defaults to :obj:`["<s>NOTUSED", "</s>NOTUSED"]`):
|
additional_special_tokens (:obj:`List[str]`, `optional`, defaults to :obj:`["<s>NOTUSED", "</s>NOTUSED"]`):
|
||||||
@@ -380,19 +381,19 @@ class CamembertTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
"""
|
"""
|
||||||
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
||||||
by concatenating and adding special tokens.
|
by concatenating and adding special tokens.
|
||||||
A CamemBERT sequence has the following format:
|
An CamemBERT sequence has the following format:
|
||||||
|
|
||||||
- single sequence: ``<s> X </s>``
|
- single sequence: ``<s> X </s>``
|
||||||
- pair of sequences: ``<s> A </s></s> B </s>``
|
- pair of sequences: ``<s> A </s></s> B </s>``
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of IDs to which the special tokens will be added
|
List of IDs to which the special tokens will be added.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
:obj:`List[int]`: List of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if token_ids_1 is None:
|
if token_ids_1 is None:
|
||||||
@@ -405,16 +406,16 @@ class CamembertTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
||||||
special tokens using the tokenizer ``prepare_for_model`` method.
|
special tokens using the tokenizer ``prepare_for_model`` method.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of IDs.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Set to True if the token list is already formatted with special tokens for the model
|
Whether or not the token list is already formatted with special tokens for the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
||||||
@@ -435,18 +436,17 @@ class CamembertTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Creates a mask from the two sequences passed to be used in a sequence-pair classification task.
|
Create a mask from the two sequences passed to be used in a sequence-pair classification task.
|
||||||
CamemBERT, like RoBERTa, does not make use of token type ids, therefore a list of zeros is returned.
|
CamemBERT, like RoBERTa, does not make use of token type ids, therefore a list of zeros is returned.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of IDs.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: List of zeros.
|
:obj:`List[int]`: List of zeros.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
sep = [self.sep_token_id]
|
sep = [self.sep_token_id]
|
||||||
cls = [self.cls_token_id]
|
cls = [self.cls_token_id]
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from typing import List, Optional
|
|||||||
|
|
||||||
from tokenizers import processors
|
from tokenizers import processors
|
||||||
|
|
||||||
from .file_utils import add_start_docstrings, add_start_docstrings_to_callable
|
from .file_utils import add_start_docstrings
|
||||||
from .tokenization_utils import BatchEncoding
|
from .tokenization_utils import BatchEncoding
|
||||||
from .tokenization_utils_base import PREPARE_SEQ2SEQ_BATCH_DOCSTRING
|
from .tokenization_utils_base import PREPARE_SEQ2SEQ_BATCH_DOCSTRING
|
||||||
from .tokenization_xlm_roberta import XLMRobertaTokenizer, XLMRobertaTokenizerFast
|
from .tokenization_xlm_roberta import XLMRobertaTokenizer, XLMRobertaTokenizerFast
|
||||||
@@ -237,21 +237,30 @@ class MBartTokenizer(XLMRobertaTokenizer):
|
|||||||
|
|
||||||
class MBartTokenizerFast(XLMRobertaTokenizerFast):
|
class MBartTokenizerFast(XLMRobertaTokenizerFast):
|
||||||
"""
|
"""
|
||||||
This inherits from XLMRobertaTokenizer. ``prepare_seq2seq_batch`` should be used to encode inputs.
|
Construct a "fast" MBART tokenizer (backed by HuggingFace's `tokenizers` library).
|
||||||
Other tokenizer methods like ``encode`` do not work properly.
|
|
||||||
|
:class:`~transformers.MBartTokenizerFast` is a subclass of :class:`~transformers.XLMRobertaTokenizerFast` and adds
|
||||||
|
a new :meth:`~transformers.MBartTokenizerFast.prepare_seq2seq_batch`.
|
||||||
|
|
||||||
|
Refer to superclass :class:`~transformers.XLMRobertaTokenizerFast` for usage examples and documentation concerning
|
||||||
|
the initialization parameters and other methods.
|
||||||
|
|
||||||
|
.. warning::
|
||||||
|
``prepare_seq2seq_batch`` should be used to encode inputs. Other tokenizer methods like ``encode`` do not work
|
||||||
|
properly.
|
||||||
|
|
||||||
The tokenization method is ``<tokens> <eos> <language code>`` for source language documents, and
|
The tokenization method is ``<tokens> <eos> <language code>`` for source language documents, and
|
||||||
``<language code> <tokens> <eos>``` for target language documents.
|
``<language code> <tokens> <eos>``` for target language documents.
|
||||||
|
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
>>> from transformers import MBartTokenizer
|
>>> from transformers import MBartTokenizerFast
|
||||||
>>> tokenizer = MBartTokenizer.from_pretrained('facebook/mbart-large-en-ro')
|
>>> tokenizer = MBartTokenizerFast.from_pretrained('facebook/mbart-large-en-ro')
|
||||||
>>> example_english_phrase = " UN Chief Says There Is No Military Solution in Syria"
|
>>> example_english_phrase = " UN Chief Says There Is No Military Solution in Syria"
|
||||||
>>> expected_translation_romanian = "Şeful ONU declară că nu există o soluţie militară în Siria"
|
>>> expected_translation_romanian = "Şeful ONU declară că nu există o soluţie militară în Siria"
|
||||||
>>> batch: dict = tokenizer.prepare_seq2seq_batch(
|
>>> batch: dict = tokenizer.prepare_seq2seq_batch(
|
||||||
... example_english_phrase, src_lang="en_XX", tgt_lang="ro_RO", tgt_texts=expected_translation_romanian
|
... example_english_phrase, src_lang="en_XX", tgt_lang="ro_RO", tgt_texts=expected_translation_romanian
|
||||||
... )
|
... )
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
vocab_files_names = {"vocab_file": "sentencepiece.bpe.model"}
|
vocab_files_names = {"vocab_file": "sentencepiece.bpe.model"}
|
||||||
@@ -275,15 +284,15 @@ class MBartTokenizerFast(XLMRobertaTokenizerFast):
|
|||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
||||||
special tokens using the tokenizer ``prepare_for_model`` methods.
|
special tokens using the tokenizer ``prepare_for_model`` method.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of ids.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Set to True if the token list is already formatted with special tokens for the model
|
Whether or not the token list is already formatted with special tokens for the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
||||||
@@ -308,16 +317,19 @@ class MBartTokenizerFast(XLMRobertaTokenizerFast):
|
|||||||
"""
|
"""
|
||||||
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
||||||
by concatenating and adding special tokens. The special tokens depend on calling set_lang.
|
by concatenating and adding special tokens. The special tokens depend on calling set_lang.
|
||||||
|
|
||||||
An MBART sequence has the following format, where ``X`` represents the sequence:
|
An MBART sequence has the following format, where ``X`` represents the sequence:
|
||||||
|
|
||||||
- ``input_ids`` (for encoder) ``X [eos, src_lang_code]``
|
- ``input_ids`` (for encoder) ``X [eos, src_lang_code]``
|
||||||
- ``decoder_input_ids``: (for decoder) ``[tgt_lang_code] X [eos]``
|
- ``decoder_input_ids``: (for decoder) ``[tgt_lang_code] X [eos]``
|
||||||
|
|
||||||
BOS is never used.
|
BOS is never used.
|
||||||
Pairs of sequences are not the expected use case, but they will be handled without a separator.
|
Pairs of sequences are not the expected use case, but they will be handled without a separator.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of IDs to which the special tokens will be added
|
List of IDs to which the special tokens will be added.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -328,7 +340,7 @@ class MBartTokenizerFast(XLMRobertaTokenizerFast):
|
|||||||
# We don't expect to process pairs, but leave the pair logic for API consistency
|
# We don't expect to process pairs, but leave the pair logic for API consistency
|
||||||
return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens
|
return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens
|
||||||
|
|
||||||
@add_start_docstrings_to_callable(PREPARE_SEQ2SEQ_BATCH_DOCSTRING)
|
@add_start_docstrings(PREPARE_SEQ2SEQ_BATCH_DOCSTRING)
|
||||||
def prepare_seq2seq_batch(
|
def prepare_seq2seq_batch(
|
||||||
self,
|
self,
|
||||||
src_texts: List[str],
|
src_texts: List[str],
|
||||||
@@ -342,69 +354,6 @@ class MBartTokenizerFast(XLMRobertaTokenizerFast):
|
|||||||
return_tensors: str = "pt",
|
return_tensors: str = "pt",
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> BatchEncoding:
|
) -> BatchEncoding:
|
||||||
"""Prepare a batch that can be passed directly to an instance of MBartModel.
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
src_texts: (:obj:`list`):
|
|
||||||
list of documents to summarize or source language texts
|
|
||||||
src_lang: (:obj:`str`, `optional`, default='en_XX'):
|
|
||||||
default en_XX (english), the language we are translating from
|
|
||||||
tgt_texts: (:obj:`list`, `optional`):
|
|
||||||
list of tgt language texts or summaries.
|
|
||||||
tgt_lang: (:obj:`str`, `optional`, default='ro_RO'):
|
|
||||||
default ro_RO (romanian), the language we are translating to
|
|
||||||
max_length (:obj:`int`, `optional`):
|
|
||||||
Controls the maximum length for encoder inputs (documents to summarize or source language texts)
|
|
||||||
If left unset or set to :obj:`None`, this will use the predefined model maximum length if a maximum
|
|
||||||
length is required by one of the truncation/padding parameters. If the model has no specific maximum
|
|
||||||
input length (like XLNet) truncation/padding to a maximum length will be deactivated.
|
|
||||||
max_target_length (:obj:`int`, `optional`):
|
|
||||||
Controls the maximum length of decoder inputs (target language texts or summaries)
|
|
||||||
If left unset or set to :obj:`None`, this will use the max_length value.
|
|
||||||
padding (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.PaddingStrategy`, `optional`, defaults to :obj:`False`):
|
|
||||||
Activates and controls padding. Accepts the following values:
|
|
||||||
|
|
||||||
* :obj:`True` or :obj:`'longest'`: Pad to the longest sequence in the batch (or no padding if only a
|
|
||||||
single sequence if provided).
|
|
||||||
* :obj:`'max_length'`: Pad to a maximum length specified with the argument :obj:`max_length` or to the
|
|
||||||
maximum acceptable input length for the model if that argument is not provided.
|
|
||||||
* :obj:`False` or :obj:`'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of
|
|
||||||
different lengths).
|
|
||||||
return_tensors (:obj:`str` or :class:`~transformers.tokenization_utils_base.TensorType`, `optional`, defaults to "pt"):
|
|
||||||
If set, will return tensors instead of list of python integers. Acceptable values are:
|
|
||||||
|
|
||||||
* :obj:`'tf'`: Return TensorFlow :obj:`tf.constant` objects.
|
|
||||||
* :obj:`'pt'`: Return PyTorch :obj:`torch.Tensor` objects.
|
|
||||||
* :obj:`'np'`: Return Numpy :obj:`np.ndarray` objects.
|
|
||||||
truncation (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.TruncationStrategy`, `optional`, defaults to :obj:`True`):
|
|
||||||
Activates and controls truncation. Accepts the following values:
|
|
||||||
|
|
||||||
* :obj:`True` or :obj:`'longest_first'`: Truncate to a maximum length specified with the argument
|
|
||||||
:obj:`max_length` or to the maximum acceptable input length for the model if that argument is not
|
|
||||||
provided. This will truncate token by token, removing a token from the longest sequence in the pair
|
|
||||||
if a pair of sequences (or a batch of pairs) is provided.
|
|
||||||
* :obj:`'only_first'`: Truncate to a maximum length specified with the argument :obj:`max_length` or to
|
|
||||||
the maximum acceptable input length for the model if that argument is not provided. This will only
|
|
||||||
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
|
|
||||||
* :obj:`'only_second'`: Truncate to a maximum length specified with the argument :obj:`max_length` or
|
|
||||||
to the maximum acceptable input length for the model if that argument is not provided. This will only
|
|
||||||
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
|
|
||||||
* :obj:`False` or :obj:`'do_not_truncate'` (default): No truncation (i.e., can output batch with
|
|
||||||
sequence lengths greater than the model maximum admissible input size).
|
|
||||||
|
|
||||||
Return:
|
|
||||||
:class:`~transformers.BatchEncoding`: A :class:`~transformers.BatchEncoding` with the following fields:
|
|
||||||
|
|
||||||
- **input_ids** -- List of token ids to be fed to the encoder.
|
|
||||||
- **attention_mask** -- List of indices specifying which tokens should be attended to by the model.
|
|
||||||
- **decoder_input_ids** -- List of token ids to be fed to the decoder.
|
|
||||||
- **decoder_attention_mask** -- List of indices specifying which tokens should be attended to by the decoder.
|
|
||||||
This does not include causal mask, which is built by the model.
|
|
||||||
|
|
||||||
The full set of keys ``[input_ids, attention_mask, decoder_input_ids, decoder_attention_mask]``,
|
|
||||||
will only be returned if tgt_texts is passed. Otherwise, input_ids, attention_mask will be the only keys.
|
|
||||||
|
|
||||||
"""
|
|
||||||
if max_length is None:
|
if max_length is None:
|
||||||
max_length = self.max_len
|
max_length = self.max_len
|
||||||
self.set_src_lang_special_tokens(src_lang)
|
self.set_src_lang_special_tokens(src_lang)
|
||||||
|
|||||||
@@ -248,8 +248,8 @@ class OpenAIGPTTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
- lower case all inputs
|
- lower case all inputs
|
||||||
- uses BERT's BasicTokenizer for pre-BPE tokenization
|
- uses BERT's BasicTokenizer for pre-BPE tokenization
|
||||||
|
|
||||||
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the methods. Users
|
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the main
|
||||||
should refer to the superclass for more information regarding methods.
|
methods. Users should refer to this superclass for more information regarding those methods.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
vocab_file (:obj:`str`):
|
vocab_file (:obj:`str`):
|
||||||
|
|||||||
@@ -14,30 +14,19 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
from .file_utils import add_start_docstrings, add_start_docstrings_to_callable
|
from .file_utils import add_start_docstrings
|
||||||
from .tokenization_reformer import ReformerTokenizer, ReformerTokenizerFast
|
from .tokenization_reformer import ReformerTokenizer, ReformerTokenizerFast
|
||||||
from .tokenization_utils_base import PREPARE_SEQ2SEQ_BATCH_DOCSTRING, BatchEncoding
|
from .tokenization_utils_base import PREPARE_SEQ2SEQ_BATCH_DOCSTRING, BatchEncoding
|
||||||
|
|
||||||
|
|
||||||
SPIECE_UNDERLINE = "▁"
|
SPIECE_UNDERLINE = "▁"
|
||||||
|
|
||||||
####################################################
|
|
||||||
# Mapping from the keyword arguments names of Tokenizer `__init__`
|
|
||||||
# to file names for serializing Tokenizer instances
|
|
||||||
####################################################
|
|
||||||
VOCAB_FILES_NAMES = {"vocab_file": "spiece.model"}
|
VOCAB_FILES_NAMES = {"vocab_file": "spiece.model"}
|
||||||
|
|
||||||
####################################################
|
|
||||||
# Mapping from the keyword arguments names of Tokenizer `__init__`
|
|
||||||
# to pretrained vocabulary URL for all the model shortcut names.
|
|
||||||
####################################################
|
|
||||||
PRETRAINED_VOCAB_FILES_MAP = {
|
PRETRAINED_VOCAB_FILES_MAP = {
|
||||||
"vocab_file": {"google/pegasus-xsum": "https://cdn.huggingface.co/google/pegasus-xsum/spiece.model"}
|
"vocab_file": {"google/pegasus-xsum": "https://cdn.huggingface.co/google/pegasus-xsum/spiece.model"}
|
||||||
}
|
}
|
||||||
|
|
||||||
####################################################
|
|
||||||
# Mapping from model shortcut names to max length of inputs
|
|
||||||
####################################################
|
|
||||||
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {
|
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {
|
||||||
"google/pegasus-xsum": 512,
|
"google/pegasus-xsum": 512,
|
||||||
}
|
}
|
||||||
@@ -60,7 +49,7 @@ class PegasusTokenizer(ReformerTokenizer):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
# Dont use reserved words added_token_encoder, added_tokens_decoder because of
|
# Don't use reserved words added_token_encoder, added_tokens_decoder because of
|
||||||
# AssertionError: Non-consecutive added token '1' found. in from_pretrained
|
# AssertionError: Non-consecutive added token '1' found. in from_pretrained
|
||||||
assert len(self.added_tokens_decoder) == 0
|
assert len(self.added_tokens_decoder) == 0
|
||||||
self.encoder: Dict[int, str] = {0: self.pad_token, 1: self.eos_token}
|
self.encoder: Dict[int, str] = {0: self.pad_token, 1: self.eos_token}
|
||||||
@@ -69,7 +58,7 @@ class PegasusTokenizer(ReformerTokenizer):
|
|||||||
self.decoder: Dict[str, int] = {v: k for k, v in self.encoder.items()}
|
self.decoder: Dict[str, int] = {v: k for k, v in self.encoder.items()}
|
||||||
|
|
||||||
def _convert_token_to_id(self, token: str) -> int:
|
def _convert_token_to_id(self, token: str) -> int:
|
||||||
""" Converts a token (str) in an id using the vocab. """
|
""" Converts a token (str) to an id using the vocab. """
|
||||||
if token in self.decoder:
|
if token in self.decoder:
|
||||||
return self.decoder[token]
|
return self.decoder[token]
|
||||||
elif token in self.added_tokens_decoder:
|
elif token in self.added_tokens_decoder:
|
||||||
@@ -78,7 +67,7 @@ class PegasusTokenizer(ReformerTokenizer):
|
|||||||
return sp_id + self.offset
|
return sp_id + self.offset
|
||||||
|
|
||||||
def _convert_id_to_token(self, index: int) -> str:
|
def _convert_id_to_token(self, index: int) -> str:
|
||||||
"""Converts an index (integer) in a token (str) using the vocab."""
|
"""Converts an index (integer) to a token (str) using the vocab."""
|
||||||
if index in self.encoder:
|
if index in self.encoder:
|
||||||
return self.encoder[index]
|
return self.encoder[index]
|
||||||
elif index in self.added_tokens_encoder:
|
elif index in self.added_tokens_encoder:
|
||||||
@@ -92,11 +81,6 @@ class PegasusTokenizer(ReformerTokenizer):
|
|||||||
def vocab_size(self) -> int:
|
def vocab_size(self) -> int:
|
||||||
return len(self.sp_model) + self.offset
|
return len(self.sp_model) + self.offset
|
||||||
|
|
||||||
def get_vocab(self) -> Dict[str, int]:
|
|
||||||
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
|
||||||
vocab.update(self.added_tokens_encoder)
|
|
||||||
return vocab
|
|
||||||
|
|
||||||
def num_special_tokens_to_add(self, pair=False):
|
def num_special_tokens_to_add(self, pair=False):
|
||||||
"""Just EOS"""
|
"""Just EOS"""
|
||||||
return 1
|
return 1
|
||||||
@@ -120,7 +104,7 @@ class PegasusTokenizer(ReformerTokenizer):
|
|||||||
|
|
||||||
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]:
|
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
Build model inputs from a sequence or a pair of sequences for sequence classification tasks
|
||||||
by concatenating and adding special tokens.
|
by concatenating and adding special tokens.
|
||||||
A Pegasus sequence has the following format, where ``X`` represents the sequence:
|
A Pegasus sequence has the following format, where ``X`` represents the sequence:
|
||||||
|
|
||||||
@@ -215,7 +199,7 @@ class PegasusTokenizerFast(ReformerTokenizerFast):
|
|||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of IDs to which the special tokens will be added
|
List of IDs to which the special tokens will be added
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -226,7 +210,7 @@ class PegasusTokenizerFast(ReformerTokenizerFast):
|
|||||||
# We don't expect to process pairs, but leave the pair logic for API consistency
|
# We don't expect to process pairs, but leave the pair logic for API consistency
|
||||||
return token_ids_0 + token_ids_1 + [self.eos_token_id]
|
return token_ids_0 + token_ids_1 + [self.eos_token_id]
|
||||||
|
|
||||||
@add_start_docstrings_to_callable(PREPARE_SEQ2SEQ_BATCH_DOCSTRING)
|
@add_start_docstrings(PREPARE_SEQ2SEQ_BATCH_DOCSTRING)
|
||||||
def prepare_seq2seq_batch(
|
def prepare_seq2seq_batch(
|
||||||
self,
|
self,
|
||||||
src_texts: List[str],
|
src_texts: List[str],
|
||||||
@@ -238,10 +222,6 @@ class PegasusTokenizerFast(ReformerTokenizerFast):
|
|||||||
padding="longest",
|
padding="longest",
|
||||||
**unused,
|
**unused,
|
||||||
) -> BatchEncoding:
|
) -> BatchEncoding:
|
||||||
"""
|
|
||||||
Prepare model inputs for summarization or translation.
|
|
||||||
|
|
||||||
"""
|
|
||||||
if "" in src_texts:
|
if "" in src_texts:
|
||||||
raise ValueError(f"found empty string in src_texts: {src_texts}")
|
raise ValueError(f"found empty string in src_texts: {src_texts}")
|
||||||
tokenizer_kwargs = dict(
|
tokenizer_kwargs = dict(
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
from shutil import copyfile
|
from shutil import copyfile
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
from .tokenization_utils import PreTrainedTokenizer
|
from .tokenization_utils import PreTrainedTokenizer
|
||||||
from .tokenization_utils_fast import PreTrainedTokenizerFast
|
from .tokenization_utils_fast import PreTrainedTokenizerFast
|
||||||
@@ -119,7 +120,7 @@ class ReformerTokenizer(PreTrainedTokenizer):
|
|||||||
def vocab_size(self):
|
def vocab_size(self):
|
||||||
return self.sp_model.get_piece_size()
|
return self.sp_model.get_piece_size()
|
||||||
|
|
||||||
def get_vocab(self):
|
def get_vocab(self) -> Dict[str, int]:
|
||||||
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
||||||
vocab.update(self.added_tokens_encoder)
|
vocab.update(self.added_tokens_encoder)
|
||||||
return vocab
|
return vocab
|
||||||
@@ -189,28 +190,29 @@ class ReformerTokenizer(PreTrainedTokenizer):
|
|||||||
|
|
||||||
class ReformerTokenizerFast(PreTrainedTokenizerFast):
|
class ReformerTokenizerFast(PreTrainedTokenizerFast):
|
||||||
"""
|
"""
|
||||||
Constructs an Reformer tokenizer. Based on `SentencePiece <https://github.com/google/sentencepiece>`__ .
|
Construct a "fast" Reformer tokenizer (backed by HuggingFace's `tokenizers` library). Based on `SentencePiece
|
||||||
|
<https://github.com/google/sentencepiece>`__ .
|
||||||
|
|
||||||
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizer` which contains most of the methods. Users
|
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the main
|
||||||
should refer to the superclass for more information regarding methods.
|
methods. Users should refer to this superclass for more information regarding those methods.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
vocab_file (:obj:`string`):
|
vocab_file (:obj:`str`):
|
||||||
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a `.spm` extension) that
|
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a `.spm` extension) that
|
||||||
contains the vocabulary necessary to instantiate a tokenizer.
|
contains the vocabulary necessary to instantiate a tokenizer.
|
||||||
eos_token (:obj:`string`, `optional`, defaults to "</s>"):
|
eos_token (:obj:`str`, `optional`, defaults to :obj:`"</s>"`):
|
||||||
The end of sequence token.
|
The end of sequence token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the end
|
When building a sequence using special tokens, this is not the token that is used for the end
|
||||||
of sequence. The token used is the :obj:`sep_token`.
|
of sequence. The token used is the :obj:`sep_token`.
|
||||||
unk_token (:obj:`string`, `optional`, defaults to "<unk>"):
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
||||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||||
token instead.
|
token instead.
|
||||||
pad_token (:obj:`string`, `optional`, defaults to "<pad>"):
|
pad_token (:obj:`str`, `optional`, defaults to :obj:`"<pad>"`):
|
||||||
The token used for padding, for example when batching sequences of different lengths.
|
The token used for padding, for example when batching sequences of different lengths.
|
||||||
additional_special_tokens (:obj:`List[str]`, `optional`, defaults to :obj:`None`):
|
additional_special_tokens (:obj:`List[str]`, `optional`):
|
||||||
Additional special tokens used by the tokenizer.
|
Additional special tokens used by the tokenizer.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|||||||
@@ -65,8 +65,8 @@ class SqueezeBertTokenizerFast(BertTokenizerFast):
|
|||||||
r"""
|
r"""
|
||||||
Constructs a "Fast" SqueezeBert tokenizer (backed by HuggingFace's `tokenizers` library).
|
Constructs a "Fast" SqueezeBert tokenizer (backed by HuggingFace's `tokenizers` library).
|
||||||
|
|
||||||
:class:`~transformers.SqueezeBertTokenizerFast` is identical to :class:`~transformers.BertTokenizerFast` and runs end-to-end
|
:class:`~transformers.SqueezeBertTokenizerFast` is identical to :class:`~transformers.BertTokenizerFast` and
|
||||||
tokenization: punctuation splitting + wordpiece.
|
runs end-to-end tokenization: punctuation splitting + wordpiece.
|
||||||
|
|
||||||
Refer to superclass :class:`~transformers.BertTokenizerFast` for usage examples and documentation concerning
|
Refer to superclass :class:`~transformers.BertTokenizerFast` for usage examples and documentation concerning
|
||||||
parameters.
|
parameters.
|
||||||
|
|||||||
@@ -327,33 +327,35 @@ class T5Tokenizer(PreTrainedTokenizer):
|
|||||||
|
|
||||||
class T5TokenizerFast(PreTrainedTokenizerFast):
|
class T5TokenizerFast(PreTrainedTokenizerFast):
|
||||||
"""
|
"""
|
||||||
Constructs a T5 tokenizer Fast.
|
Construct a "fast" T5 tokenizer (backed by HuggingFace's `tokenizers` library). Based on `SentencePiece
|
||||||
|
<https://github.com/google/sentencepiece>`__ .
|
||||||
|
|
||||||
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the methods. Users
|
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the main
|
||||||
should refer to the superclass for more information regarding methods.
|
methods. Users should refer to this superclass for more information regarding those methods.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
vocab_file (:obj:`string`):
|
vocab_file (:obj:`str`):
|
||||||
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a `.spm` extension) that
|
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a `.spm` extension) that
|
||||||
contains the vocabulary necessary to instantiate a tokenizer.
|
contains the vocabulary necessary to instantiate a tokenizer.
|
||||||
eos_token (:obj:`string`, `optional`, defaults to "</s>"):
|
eos_token (:obj:`str`, `optional`, defaults to :obj:`"</s>"`):
|
||||||
The end of sequence token.
|
The end of sequence token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the end
|
When building a sequence using special tokens, this is not the token that is used for the end
|
||||||
of sequence. The token used is the :obj:`sep_token`.
|
of sequence. The token used is the :obj:`sep_token`.
|
||||||
unk_token (:obj:`string`, `optional`, defaults to "<unk>"):
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
||||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||||
token instead.
|
token instead.
|
||||||
pad_token (:obj:`string`, `optional`, defaults to "<pad>"):
|
pad_token (:obj:`str`, `optional`, defaults to :obj:`"<pad>"`):
|
||||||
The token used for padding, for example when batching sequences of different lengths.
|
The token used for padding, for example when batching sequences of different lengths.
|
||||||
extra_ids (:obj:`List[str]`, `optional`, defaults to :obj:`100`):
|
extra_ids (:obj:`int`, `optional`, defaults to 100):
|
||||||
Add a number of extra ids added to the end of the vocabulary for use as sentinels.
|
Add a number of extra ids added to the end of the vocabulary for use as sentinels.
|
||||||
These tokens are accessible as "<extra_id_{%d}>" where "{%d}" is a number between 0 and extra_ids-1.
|
These tokens are accessible as "<extra_id_{%d}>" where "{%d}" is a number between 0 and extra_ids-1.
|
||||||
Extra tokens are indexed from the end of the vocabulary up to beginnning ("<extra_id_0>" is the last token in the vocabulary like in T5 preprocessing
|
Extra tokens are indexed from the end of the vocabulary up to beginnning ("<extra_id_0>" is the last token
|
||||||
see: https://github.com/google-research/text-to-text-transfer-transformer/blob/9fd7b14a769417be33bc6c850f9598764913c833/t5/data/preprocessors.py#L2117)
|
in the vocabulary like in T5 preprocessing see `here
|
||||||
additional_special_tokens (:obj:`List[str]`, `optional`, defaults to :obj:`None`):
|
<https://github.com/google-research/text-to-text-transfer-transformer/blob/9fd7b14a769417be33bc6c850f9598764913c833/t5/data/preprocessors.py#L2117>`__).
|
||||||
|
additional_special_tokens (:obj:`List[str]`, `optional`):
|
||||||
Additional special tokens used by the tokenizer.
|
Additional special tokens used by the tokenizer.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -389,8 +391,15 @@ class T5TokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self._extra_ids = extra_ids
|
self._extra_ids = extra_ids
|
||||||
|
|
||||||
def save_vocabulary(self, save_directory):
|
def save_vocabulary(self, save_directory):
|
||||||
"""Save the sentencepiece vocabulary (copy original file) and special tokens file
|
"""
|
||||||
to a directory.
|
Save the sentencepiece vocabulary (copy original file) and special tokens file to a directory.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
save_directory (:obj:`str`):
|
||||||
|
The directory in which to save the vocabulary.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
:obj:`Tuple(str)`: Paths to the files saved.
|
||||||
"""
|
"""
|
||||||
if not os.path.isdir(save_directory):
|
if not os.path.isdir(save_directory):
|
||||||
logger.error("Vocabulary path ({}) should be a directory".format(save_directory))
|
logger.error("Vocabulary path ({}) should be a directory".format(save_directory))
|
||||||
@@ -408,7 +417,6 @@ class T5TokenizerFast(PreTrainedTokenizerFast):
|
|||||||
"""
|
"""
|
||||||
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
||||||
by concatenating and adding special tokens.
|
by concatenating and adding special tokens.
|
||||||
For some t5 tasks, model.config.prefix is specified. This must be used before tokenization.
|
|
||||||
A sequence has the following format:
|
A sequence has the following format:
|
||||||
|
|
||||||
- single sequence: ``X </s>``
|
- single sequence: ``X </s>``
|
||||||
@@ -416,12 +424,12 @@ class T5TokenizerFast(PreTrainedTokenizerFast):
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of IDs to which the special tokens will be added
|
List of IDs to which the special tokens will be added.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
:obj:`List[int]`: List of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
||||||
"""
|
"""
|
||||||
token_ids_0 = token_ids_0 + [self.eos_token_id]
|
token_ids_0 = token_ids_0 + [self.eos_token_id]
|
||||||
if token_ids_1 is None:
|
if token_ids_1 is None:
|
||||||
@@ -430,6 +438,7 @@ class T5TokenizerFast(PreTrainedTokenizerFast):
|
|||||||
token_ids_1 = token_ids_1 + [self.eos_token_id]
|
token_ids_1 = token_ids_1 + [self.eos_token_id]
|
||||||
return self.prefix_tokens + token_ids_0 + token_ids_1
|
return self.prefix_tokens + token_ids_0 + token_ids_1
|
||||||
|
|
||||||
|
@add_start_docstrings(PREPARE_SEQ2SEQ_BATCH_DOCSTRING)
|
||||||
def prepare_seq2seq_batch(
|
def prepare_seq2seq_batch(
|
||||||
self,
|
self,
|
||||||
src_texts: List[str],
|
src_texts: List[str],
|
||||||
@@ -441,60 +450,6 @@ class T5TokenizerFast(PreTrainedTokenizerFast):
|
|||||||
truncation: bool = True,
|
truncation: bool = True,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> BatchEncoding:
|
) -> BatchEncoding:
|
||||||
r"""
|
|
||||||
Prepare a batch that can be passed directly to an instance of :class:`~transformers.T5Model`.
|
|
||||||
Args:
|
|
||||||
src_texts: (:obj:`List[str]`):
|
|
||||||
List of documents to summarize or source language texts.
|
|
||||||
tgt_texts: (:obj:`List[str]`, `optional`):
|
|
||||||
List of summaries or target language texts.
|
|
||||||
max_length (:obj:`int`, `optional`):
|
|
||||||
Controls the maximum length for encoder inputs (documents to summarize or source language texts).
|
|
||||||
If left unset or set to :obj:`None`, this will use the predefined model maximum length if a maximum
|
|
||||||
length is required by one of the truncation/padding parameters. If the model has no specific maximum
|
|
||||||
input length (like XLNet) truncation/padding to a maximum length will be deactivated.
|
|
||||||
max_target_length (:obj:`int`, `optional`):
|
|
||||||
Controls the maximum length of decoder inputs (target language texts or summaries).
|
|
||||||
If left unset or set to :obj:`None`, this will use the max_length value.
|
|
||||||
padding (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.PaddingStrategy`, `optional`, defaults to :obj:`False`):
|
|
||||||
Activates and controls padding. Accepts the following values:
|
|
||||||
* :obj:`True` or :obj:`'longest'`: Pad to the longest sequence in the batch (or no padding if only a
|
|
||||||
single sequence if provided).
|
|
||||||
* :obj:`'max_length'`: Pad to a maximum length specified with the argument :obj:`max_length` or to the
|
|
||||||
maximum acceptable input length for the model if that argument is not provided.
|
|
||||||
* :obj:`False` or :obj:`'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of
|
|
||||||
different lengths).
|
|
||||||
return_tensors (:obj:`str` or :class:`~transformers.tokenization_utils_base.TensorType`, `optional`, defaults to "pt"):
|
|
||||||
If set, will return tensors instead of list of python integers. Acceptable values are:
|
|
||||||
* :obj:`'tf'`: Return TensorFlow :obj:`tf.constant` objects.
|
|
||||||
* :obj:`'pt'`: Return PyTorch :obj:`torch.Tensor` objects.
|
|
||||||
* :obj:`'np'`: Return Numpy :obj:`np.ndarray` objects.
|
|
||||||
truncation (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.TruncationStrategy`, `optional`, defaults to :obj:`True`):
|
|
||||||
Activates and controls truncation. Accepts the following values:
|
|
||||||
* :obj:`True` or :obj:`'longest_first'`: Truncate to a maximum length specified with the argument
|
|
||||||
:obj:`max_length` or to the maximum acceptable input length for the model if that argument is not
|
|
||||||
provided. This will truncate token by token, removing a token from the longest sequence in the pair
|
|
||||||
if a pair of sequences (or a batch of pairs) is provided.
|
|
||||||
* :obj:`'only_first'`: Truncate to a maximum length specified with the argument :obj:`max_length` or to
|
|
||||||
the maximum acceptable input length for the model if that argument is not provided. This will only
|
|
||||||
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
|
|
||||||
* :obj:`'only_second'`: Truncate to a maximum length specified with the argument :obj:`max_length` or
|
|
||||||
to the maximum acceptable input length for the model if that argument is not provided. This will only
|
|
||||||
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
|
|
||||||
* :obj:`False` or :obj:`'do_not_truncate'` (default): No truncation (i.e., can output batch with
|
|
||||||
sequence lengths greater than the model maximum admissible input size).
|
|
||||||
**kwargs:
|
|
||||||
Additional keyword arguments passed along to :obj:`self.__call__`.
|
|
||||||
Returns:
|
|
||||||
:class:`~transformers.BatchEncoding`: A :class:`~transformers.BatchEncoding` with the following fields:
|
|
||||||
- **input_ids** -- List of token ids to be fed to the encoder.
|
|
||||||
- **attention_mask** -- List of indices specifying which tokens should be attended to by the model.
|
|
||||||
- **decoder_input_ids** -- List of token ids to be fed to the decoder.
|
|
||||||
- **decoder_attention_mask** -- List of indices specifying which tokens should be attended to by the decoder.
|
|
||||||
This does not include causal mask, which is built by the model.
|
|
||||||
The full set of keys ``[input_ids, attention_mask, decoder_input_ids, decoder_attention_mask]``,
|
|
||||||
will only be returned if tgt_texts is passed. Otherwise, input_ids, attention_mask will be the only keys.
|
|
||||||
"""
|
|
||||||
if max_length is None:
|
if max_length is None:
|
||||||
max_length = self.max_len
|
max_length = self.max_len
|
||||||
self.prefix_tokens = []
|
self.prefix_tokens = []
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import numpy as np
|
|||||||
|
|
||||||
import sacremoses as sm
|
import sacremoses as sm
|
||||||
|
|
||||||
from .file_utils import cached_path, is_torch_available
|
from .file_utils import cached_path, is_torch_available, torch_only_method
|
||||||
from .tokenization_utils import PreTrainedTokenizer
|
from .tokenization_utils import PreTrainedTokenizer
|
||||||
from .utils import logging
|
from .utils import logging
|
||||||
|
|
||||||
@@ -40,17 +40,15 @@ if is_torch_available():
|
|||||||
|
|
||||||
logger = logging.get_logger(__name__)
|
logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
VOCAB_FILES_NAMES = {"pretrained_vocab_file": "vocab.bin", "vocab_file": "vocab.txt"}
|
VOCAB_FILES_NAMES = {
|
||||||
|
"pretrained_vocab_file": "vocab.pkl",
|
||||||
|
"pretrained_vocab_file_torch": "vocab.bin",
|
||||||
|
"vocab_file": "vocab.txt",
|
||||||
|
}
|
||||||
|
|
||||||
PRETRAINED_VOCAB_FILES_MAP = {
|
PRETRAINED_VOCAB_FILES_MAP = {
|
||||||
"pretrained_vocab_file": {
|
"pretrained_vocab_file": {
|
||||||
"transfo-xl-wt103": "https://s3.amazonaws.com/models.huggingface.co/bert/transfo-xl-wt103-vocab.bin",
|
"transfo-xl-wt103": "https://s3.amazonaws.com/models.huggingface.co/bert/transfo-xl-wt103-vocab.pkl",
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PRETRAINED_VOCAB_FILES_MAP_FAST = {
|
|
||||||
"pretrained_vocab_file": {
|
|
||||||
"transfo-xl-wt103": "https://s3.amazonaws.com/models.huggingface.co/bert/transfo-xl-wt103-vocab.json",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,8 +128,9 @@ class TransfoXLTokenizer(PreTrainedTokenizer):
|
|||||||
File containing the vocabulary (from the original implementation).
|
File containing the vocabulary (from the original implementation).
|
||||||
pretrained_vocab_file (:obj:`str`, `optional`):
|
pretrained_vocab_file (:obj:`str`, `optional`):
|
||||||
File containing the vocabulary as saved with the :obj:`save_pretrained()` method.
|
File containing the vocabulary as saved with the :obj:`save_pretrained()` method.
|
||||||
never_split (xxx, `optional`):
|
never_split (:obj:`List[str]`, `optional`):
|
||||||
Fill me with intesting stuff.
|
List of tokens that should never be split. If no list is specified, will simply use the existing
|
||||||
|
special tokens.
|
||||||
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
||||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||||
token instead.
|
token instead.
|
||||||
@@ -156,7 +155,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer):
|
|||||||
lower_case=False,
|
lower_case=False,
|
||||||
delimiter=None,
|
delimiter=None,
|
||||||
vocab_file=None,
|
vocab_file=None,
|
||||||
pretrained_vocab_file=None,
|
pretrained_vocab_file: str = None,
|
||||||
never_split=None,
|
never_split=None,
|
||||||
unk_token="<unk>",
|
unk_token="<unk>",
|
||||||
eos_token="<eos>",
|
eos_token="<eos>",
|
||||||
@@ -188,19 +187,40 @@ class TransfoXLTokenizer(PreTrainedTokenizer):
|
|||||||
self.moses_tokenizer = sm.MosesTokenizer(language)
|
self.moses_tokenizer = sm.MosesTokenizer(language)
|
||||||
self.moses_detokenizer = sm.MosesDetokenizer(language)
|
self.moses_detokenizer = sm.MosesDetokenizer(language)
|
||||||
|
|
||||||
try:
|
# This try... catch... is not beautiful but honestly this tokenizer was not made to be used
|
||||||
if pretrained_vocab_file is not None:
|
|
||||||
# Hack because, honestly this tokenizer was not made to be used
|
|
||||||
# in a library like ours, at all.
|
# in a library like ours, at all.
|
||||||
|
try:
|
||||||
|
vocab_dict = None
|
||||||
|
if pretrained_vocab_file is not None:
|
||||||
|
# Priority on pickle files (support PyTorch and TF)
|
||||||
|
with open(pretrained_vocab_file, "rb") as f:
|
||||||
|
vocab_dict = pickle.load(f)
|
||||||
|
|
||||||
|
# Loading a torch-saved transfo-xl vocab dict with pickle results in an integer
|
||||||
|
# Entering this if statement means that we tried to load a torch-saved file with pickle, and we failed.
|
||||||
|
# We therefore load it with torch, if it's available.
|
||||||
|
if type(vocab_dict) == int:
|
||||||
|
if not is_torch_available():
|
||||||
|
raise ImportError(
|
||||||
|
"Not trying to load dict with PyTorch as you need to install pytorch to load "
|
||||||
|
"from a PyTorch pretrained vocabulary, "
|
||||||
|
"or activate it with environment variables USE_TORCH=1 and USE_TF=0."
|
||||||
|
)
|
||||||
vocab_dict = torch.load(pretrained_vocab_file)
|
vocab_dict = torch.load(pretrained_vocab_file)
|
||||||
|
|
||||||
|
if vocab_dict is not None:
|
||||||
for key, value in vocab_dict.items():
|
for key, value in vocab_dict.items():
|
||||||
if key not in self.__dict__:
|
if key not in self.__dict__:
|
||||||
self.__dict__[key] = value
|
self.__dict__[key] = value
|
||||||
|
elif vocab_file is not None:
|
||||||
if vocab_file is not None:
|
|
||||||
self.build_vocab()
|
self.build_vocab()
|
||||||
except Exception:
|
|
||||||
raise ValueError("Unable to parse file {}. Unknown format.")
|
except Exception as e:
|
||||||
|
raise ValueError(
|
||||||
|
"Unable to parse file {}. Unknown format. "
|
||||||
|
"If you tried to load a model saved through TransfoXLTokenizerFast,"
|
||||||
|
"please note they are not compatible.".format(pretrained_vocab_file)
|
||||||
|
) from e
|
||||||
|
|
||||||
if vocab_file is not None:
|
if vocab_file is not None:
|
||||||
self.build_vocab()
|
self.build_vocab()
|
||||||
@@ -272,7 +292,8 @@ class TransfoXLTokenizer(PreTrainedTokenizer):
|
|||||||
vocab_file = os.path.join(vocab_path, VOCAB_FILES_NAMES["pretrained_vocab_file"])
|
vocab_file = os.path.join(vocab_path, VOCAB_FILES_NAMES["pretrained_vocab_file"])
|
||||||
else:
|
else:
|
||||||
vocab_file = vocab_path
|
vocab_file = vocab_path
|
||||||
torch.save(self.__dict__, vocab_file)
|
with open(vocab_file, "wb") as f:
|
||||||
|
pickle.dump(self.__dict__, f)
|
||||||
return (vocab_file,)
|
return (vocab_file,)
|
||||||
|
|
||||||
def build_vocab(self):
|
def build_vocab(self):
|
||||||
@@ -295,6 +316,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer):
|
|||||||
|
|
||||||
logger.info("final vocab size {} from {} unique tokens".format(len(self), len(self.counter)))
|
logger.info("final vocab size {} from {} unique tokens".format(len(self), len(self.counter)))
|
||||||
|
|
||||||
|
@torch_only_method
|
||||||
def encode_file(self, path, ordered=False, verbose=False, add_eos=True, add_double_eos=False):
|
def encode_file(self, path, ordered=False, verbose=False, add_eos=True, add_double_eos=False):
|
||||||
if verbose:
|
if verbose:
|
||||||
logger.info("encoding file {} ...".format(path))
|
logger.info("encoding file {} ...".format(path))
|
||||||
@@ -312,6 +334,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer):
|
|||||||
|
|
||||||
return encoded
|
return encoded
|
||||||
|
|
||||||
|
@torch_only_method
|
||||||
def encode_sents(self, sents, ordered=False, verbose=False):
|
def encode_sents(self, sents, ordered=False, verbose=False):
|
||||||
if verbose:
|
if verbose:
|
||||||
logger.info("encoding {} sents ...".format(len(sents)))
|
logger.info("encoding {} sents ...".format(len(sents)))
|
||||||
@@ -422,6 +445,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer):
|
|||||||
out_string = self.moses_detokenizer.detokenize(tokens)
|
out_string = self.moses_detokenizer.detokenize(tokens)
|
||||||
return detokenize_numbers(out_string).strip()
|
return detokenize_numbers(out_string).strip()
|
||||||
|
|
||||||
|
@torch_only_method
|
||||||
def convert_to_tensor(self, symbols):
|
def convert_to_tensor(self, symbols):
|
||||||
return torch.LongTensor(self.convert_tokens_to_ids(symbols))
|
return torch.LongTensor(self.convert_tokens_to_ids(symbols))
|
||||||
|
|
||||||
@@ -533,6 +557,7 @@ class LMShuffledIterator(object):
|
|||||||
for idx in epoch_indices:
|
for idx in epoch_indices:
|
||||||
yield self.data[idx]
|
yield self.data[idx]
|
||||||
|
|
||||||
|
@torch_only_method
|
||||||
def stream_iterator(self, sent_stream):
|
def stream_iterator(self, sent_stream):
|
||||||
# streams for each data in the batch
|
# streams for each data in the batch
|
||||||
streams = [None] * self.bsz
|
streams = [None] * self.bsz
|
||||||
@@ -622,6 +647,7 @@ class LMMultiFileIterator(LMShuffledIterator):
|
|||||||
|
|
||||||
class TransfoXLCorpus(object):
|
class TransfoXLCorpus(object):
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@torch_only_method
|
||||||
def from_pretrained(cls, pretrained_model_name_or_path, cache_dir=None, *inputs, **kwargs):
|
def from_pretrained(cls, pretrained_model_name_or_path, cache_dir=None, *inputs, **kwargs):
|
||||||
"""
|
"""
|
||||||
Instantiate a pre-processed corpus.
|
Instantiate a pre-processed corpus.
|
||||||
@@ -719,10 +745,14 @@ class TransfoXLCorpus(object):
|
|||||||
data_iter = LMOrderedIterator(data, *args, **kwargs)
|
data_iter = LMOrderedIterator(data, *args, **kwargs)
|
||||||
elif self.dataset == "lm1b":
|
elif self.dataset == "lm1b":
|
||||||
data_iter = LMShuffledIterator(data, *args, **kwargs)
|
data_iter = LMShuffledIterator(data, *args, **kwargs)
|
||||||
|
else:
|
||||||
|
data_iter = None
|
||||||
|
raise ValueError(f"Split not recognized: {split}")
|
||||||
|
|
||||||
return data_iter
|
return data_iter
|
||||||
|
|
||||||
|
|
||||||
|
@torch_only_method
|
||||||
def get_lm_corpus(datadir, dataset):
|
def get_lm_corpus(datadir, dataset):
|
||||||
fn = os.path.join(datadir, "cache.pt")
|
fn = os.path.join(datadir, "cache.pt")
|
||||||
fn_pickle = os.path.join(datadir, "cache.pkl")
|
fn_pickle = os.path.join(datadir, "cache.pkl")
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase):
|
|||||||
|
|
||||||
num_added_toks = tokenizer.add_tokens(['new_tok1', 'my_new-tok2'])
|
num_added_toks = tokenizer.add_tokens(['new_tok1', 'my_new-tok2'])
|
||||||
print('We have added', num_added_toks, 'tokens')
|
print('We have added', num_added_toks, 'tokens')
|
||||||
# Notice: resize_token_embeddings expect to receive the full size of the new vocabulary, i.e. the length of the tokenizer.
|
# Note: resize_token_embeddings expects to receive the full size of the new vocabulary, i.e. the length of the tokenizer.
|
||||||
model.resize_token_embeddings(len(tokenizer))
|
model.resize_token_embeddings(len(tokenizer))
|
||||||
"""
|
"""
|
||||||
new_tokens = [str(tok) for tok in new_tokens]
|
new_tokens = [str(tok) for tok in new_tokens]
|
||||||
@@ -768,8 +768,8 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase):
|
|||||||
Whether or not to clean up the tokenization spaces.
|
Whether or not to clean up the tokenization spaces.
|
||||||
spaces_between_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
spaces_between_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
Whether or not to add spaces around special tokens.
|
Whether or not to add spaces around special tokens.
|
||||||
The behavior of Fast tokenizers is to have this to `False`.
|
The behavior of Fast tokenizers is to have this to :obj:`False`.
|
||||||
This is setup to True in slow tokenizers for backward compatibility.
|
This is setup to :obj:`True` in slow tokenizers for backward compatibility.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`str`: The decoded sentence.
|
:obj:`str`: The decoded sentence.
|
||||||
|
|||||||
@@ -1842,6 +1842,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin):
|
|||||||
padding_strategy = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch
|
padding_strategy = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch
|
||||||
elif not isinstance(padding, PaddingStrategy):
|
elif not isinstance(padding, PaddingStrategy):
|
||||||
padding_strategy = PaddingStrategy(padding)
|
padding_strategy = PaddingStrategy(padding)
|
||||||
|
elif isinstance(padding, PaddingStrategy):
|
||||||
|
padding_strategy = padding
|
||||||
else:
|
else:
|
||||||
padding_strategy = PaddingStrategy.DO_NOT_PAD
|
padding_strategy = PaddingStrategy.DO_NOT_PAD
|
||||||
|
|
||||||
@@ -1867,6 +1869,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin):
|
|||||||
) # Default to truncate the longest sequences in pairs of inputs
|
) # Default to truncate the longest sequences in pairs of inputs
|
||||||
elif not isinstance(truncation, TruncationStrategy):
|
elif not isinstance(truncation, TruncationStrategy):
|
||||||
truncation_strategy = TruncationStrategy(truncation)
|
truncation_strategy = TruncationStrategy(truncation)
|
||||||
|
elif isinstance(truncation, TruncationStrategy):
|
||||||
|
truncation_strategy = truncation
|
||||||
else:
|
else:
|
||||||
truncation_strategy = TruncationStrategy.DO_NOT_TRUNCATE
|
truncation_strategy = TruncationStrategy.DO_NOT_TRUNCATE
|
||||||
|
|
||||||
|
|||||||
@@ -278,8 +278,8 @@ class PreTrainedTokenizerFast(PreTrainedTokenizerBase):
|
|||||||
"""
|
"""
|
||||||
Converts a string in a sequence of tokens, using the backend Rust tokenizer.
|
Converts a string in a sequence of tokens, using the backend Rust tokenizer.
|
||||||
|
|
||||||
Note that, unlike slow tokenizers (instances of PreTrainedTokenizer), this method
|
Note that, unlike slow tokenizers (instances of :class:`~transformers.PreTrainedTokenizer`), this method
|
||||||
will replace the unknown tokens with the `unk_token`.
|
will replace the unknown tokens with the :obj:`unk_token`.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
text (:obj:`str`):
|
text (:obj:`str`):
|
||||||
|
|||||||
@@ -312,45 +312,44 @@ class XLMRobertaTokenizer(PreTrainedTokenizer):
|
|||||||
|
|
||||||
class XLMRobertaTokenizerFast(PreTrainedTokenizerFast):
|
class XLMRobertaTokenizerFast(PreTrainedTokenizerFast):
|
||||||
"""
|
"""
|
||||||
Adapted from RobertaTokenizer and XLNetTokenizer
|
Construct a "fast" XLM-RoBERTa tokenizer (backed by HuggingFace's `tokenizers` library). Adapted from
|
||||||
SentencePiece based tokenizer. Peculiarities:
|
:class:`~transfomers.RobertaTokenizer` and class:`~transfomers.XLNetTokenizer`. Based on `SentencePiece
|
||||||
|
<https://github.com/google/sentencepiece>`__.
|
||||||
|
|
||||||
- requires `SentencePiece <https://github.com/google/sentencepiece>`_
|
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the main
|
||||||
|
methods. Users should refer to this superclass for more information regarding those methods.
|
||||||
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizer` which contains most of the methods. Users
|
|
||||||
should refer to the superclass for more information regarding methods.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
vocab_file (:obj:`str`):
|
vocab_file (:obj:`str`):
|
||||||
Path to the vocabulary file.
|
Path to the vocabulary file.
|
||||||
bos_token (:obj:`string`, `optional`, defaults to "<s>"):
|
bos_token (:obj:`str`, `optional`, defaults to :obj:`"<s>"`):
|
||||||
The beginning of sequence token that was used during pre-training. Can be used a sequence classifier token.
|
The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the beginning
|
When building a sequence using special tokens, this is not the token that is used for the beginning
|
||||||
of sequence. The token used is the :obj:`cls_token`.
|
of sequence. The token used is the :obj:`cls_token`.
|
||||||
eos_token (:obj:`string`, `optional`, defaults to "</s>"):
|
eos_token (:obj:`str`, `optional`, defaults to :obj:`"</s>"`):
|
||||||
The end of sequence token.
|
The end of sequence token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the end
|
When building a sequence using special tokens, this is not the token that is used for the end
|
||||||
of sequence. The token used is the :obj:`sep_token`.
|
of sequence. The token used is the :obj:`sep_token`.
|
||||||
sep_token (:obj:`string`, `optional`, defaults to "</s>"):
|
sep_token (:obj:`str`, `optional`, defaults to :obj:`"</s>"`):
|
||||||
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
||||||
for sequence classification or for a text and a question for question answering.
|
for sequence classification or for a text and a question for question answering.
|
||||||
It is also used as the last token of a sequence built with special tokens.
|
It is also used as the last token of a sequence built with special tokens.
|
||||||
cls_token (:obj:`string`, `optional`, defaults to "<s>"):
|
cls_token (:obj:`str`, `optional`, defaults to :obj:`"<s>"`):
|
||||||
The classifier token which is used when doing sequence classification (classification of the whole
|
The classifier token which is used when doing sequence classification (classification of the whole
|
||||||
sequence instead of per-token classification). It is the first token of the sequence when built with
|
sequence instead of per-token classification). It is the first token of the sequence when built with
|
||||||
special tokens.
|
special tokens.
|
||||||
unk_token (:obj:`string`, `optional`, defaults to "<unk>"):
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
||||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||||
token instead.
|
token instead.
|
||||||
pad_token (:obj:`string`, `optional`, defaults to "<pad>"):
|
pad_token (:obj:`str`, `optional`, defaults to :obj:`"<pad>"`):
|
||||||
The token used for padding, for example when batching sequences of different lengths.
|
The token used for padding, for example when batching sequences of different lengths.
|
||||||
mask_token (:obj:`string`, `optional`, defaults to "<mask>"):
|
mask_token (:obj:`str`, `optional`, defaults to :obj:`"<mask>"`):
|
||||||
The token used for masking values. This is the token used when training this model with masked language
|
The token used for masking values. This is the token used when training this model with masked language
|
||||||
modeling. This is the token which the model will try to predict.
|
modeling. This is the token which the model will try to predict.
|
||||||
additional_special_tokens (:obj:`List[str]`, `optional`, defaults to :obj:`["<s>NOTUSED", "</s>NOTUSED"]`):
|
additional_special_tokens (:obj:`List[str]`, `optional`, defaults to :obj:`["<s>NOTUSED", "</s>NOTUSED"]`):
|
||||||
@@ -399,19 +398,19 @@ class XLMRobertaTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
"""
|
"""
|
||||||
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
Build model inputs from a sequence or a pair of sequence for sequence classification tasks
|
||||||
by concatenating and adding special tokens.
|
by concatenating and adding special tokens.
|
||||||
A XLM-R sequence has the following format:
|
An XLM-RoBERTa sequence has the following format:
|
||||||
|
|
||||||
- single sequence: ``<s> X </s>``
|
- single sequence: ``<s> X </s>``
|
||||||
- pair of sequences: ``<s> A </s></s> B </s>``
|
- pair of sequences: ``<s> A </s></s> B </s>``
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of IDs to which the special tokens will be added
|
List of IDs to which the special tokens will be added.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
:obj:`List[int]`: List of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if token_ids_1 is None:
|
if token_ids_1 is None:
|
||||||
@@ -424,16 +423,16 @@ class XLMRobertaTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
||||||
special tokens using the tokenizer ``prepare_for_model`` methods.
|
special tokens using the tokenizer ``prepare_for_model`` method.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of IDs.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Set to True if the token list is already formatted with special tokens for the model
|
Whether or not the token list is already formatted with special tokens for the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
||||||
@@ -455,13 +454,13 @@ class XLMRobertaTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Creates a mask from the two sequences passed to be used in a sequence-pair classification task.
|
Create a mask from the two sequences passed to be used in a sequence-pair classification task.
|
||||||
XLM-R does not make use of token type ids, therefore a list of zeros is returned.
|
XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of IDs.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|||||||
@@ -349,13 +349,14 @@ class XLNetTokenizer(PreTrainedTokenizer):
|
|||||||
|
|
||||||
class XLNetTokenizerFast(PreTrainedTokenizerFast):
|
class XLNetTokenizerFast(PreTrainedTokenizerFast):
|
||||||
"""
|
"""
|
||||||
Constructs an XLNet tokenizer Fast.
|
Construct a "fast" XLNet tokenizer (backed by HuggingFace's `tokenizers` library). Based on
|
||||||
|
`SentencePiece <https://github.com/google/sentencepiece>`__.
|
||||||
|
|
||||||
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the methods. Users
|
This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the main
|
||||||
should refer to the superclass for more information regarding methods.
|
methods. Users should refer to this superclass for more information regarding those methods.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
vocab_file (:obj:`string`):
|
vocab_file (:obj:`str`):
|
||||||
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a .spm extension) that
|
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a .spm extension) that
|
||||||
contains the vocabulary necessary to instantiate a tokenizer.
|
contains the vocabulary necessary to instantiate a tokenizer.
|
||||||
do_lower_case (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
do_lower_case (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
@@ -364,34 +365,34 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
Whether to strip the text when tokenizing (removing excess spaces before and after the string).
|
Whether to strip the text when tokenizing (removing excess spaces before and after the string).
|
||||||
keep_accents (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
keep_accents (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Whether to keep accents when tokenizing.
|
Whether to keep accents when tokenizing.
|
||||||
bos_token (:obj:`string`, `optional`, defaults to "<s>"):
|
bos_token (:obj:`str`, `optional`, defaults to :obj:`"<s>"`):
|
||||||
The beginning of sequence token that was used during pre-training. Can be used a sequence classifier token.
|
The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the beginning
|
When building a sequence using special tokens, this is not the token that is used for the beginning
|
||||||
of sequence. The token used is the :obj:`cls_token`.
|
of sequence. The token used is the :obj:`cls_token`.
|
||||||
eos_token (:obj:`string`, `optional`, defaults to "</s>"):
|
eos_token (:obj:`str`, `optional`, defaults to :obj:`"</s>"`):
|
||||||
The end of sequence token.
|
The end of sequence token.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
When building a sequence using special tokens, this is not the token that is used for the end
|
When building a sequence using special tokens, this is not the token that is used for the end
|
||||||
of sequence. The token used is the :obj:`sep_token`.
|
of sequence. The token used is the :obj:`sep_token`.
|
||||||
unk_token (:obj:`string`, `optional`, defaults to "<unk>"):
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
||||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||||
token instead.
|
token instead.
|
||||||
sep_token (:obj:`string`, `optional`, defaults to "<sep>"):
|
sep_token (:obj:`str`, `optional`, defaults to :obj:`"<sep>"`):
|
||||||
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
|
||||||
for sequence classification or for a text and a question for question answering.
|
for sequence classification or for a text and a question for question answering.
|
||||||
It is also used as the last token of a sequence built with special tokens.
|
It is also used as the last token of a sequence built with special tokens.
|
||||||
pad_token (:obj:`string`, `optional`, defaults to "<pad>"):
|
pad_token (:obj:`str`, `optional`, defaults to :obj:`"<pad>"`):
|
||||||
The token used for padding, for example when batching sequences of different lengths.
|
The token used for padding, for example when batching sequences of different lengths.
|
||||||
cls_token (:obj:`string`, `optional`, defaults to "<cls>"):
|
cls_token (:obj:`str`, `optional`, defaults to :obj:`"<cls>"`):
|
||||||
The classifier token which is used when doing sequence classification (classification of the whole
|
The classifier token which is used when doing sequence classification (classification of the whole
|
||||||
sequence instead of per-token classification). It is the first token of the sequence when built with
|
sequence instead of per-token classification). It is the first token of the sequence when built with
|
||||||
special tokens.
|
special tokens.
|
||||||
mask_token (:obj:`string`, `optional`, defaults to "<mask>"):
|
mask_token (:obj:`str`, `optional`, defaults to :obj:`"<mask>"`):
|
||||||
The token used for masking values. This is the token used when training this model with masked language
|
The token used for masking values. This is the token used when training this model with masked language
|
||||||
modeling. This is the token which the model will try to predict.
|
modeling. This is the token which the model will try to predict.
|
||||||
additional_special_tokens (:obj:`List[str]`, `optional`, defaults to :obj:`["<eop>", "<eod>"]`):
|
additional_special_tokens (:obj:`List[str]`, `optional`, defaults to :obj:`["<eop>", "<eod>"]`):
|
||||||
@@ -459,12 +460,12 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of IDs to which the special tokens will be added
|
List of IDs to which the special tokens will be added.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
:obj:`List[int]`: List of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
||||||
"""
|
"""
|
||||||
sep = [self.sep_token_id]
|
sep = [self.sep_token_id]
|
||||||
cls = [self.cls_token_id]
|
cls = [self.cls_token_id]
|
||||||
@@ -476,16 +477,16 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
||||||
special tokens using the tokenizer ``prepare_for_model`` methods.
|
special tokens using the tokenizer ``prepare_for_model`` method.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of IDs.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
Set to True if the token list is already formatted with special tokens for the model
|
Whether or not the token list is already formatted with special tokens for the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
||||||
@@ -507,17 +508,20 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast):
|
|||||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
||||||
) -> List[int]:
|
) -> List[int]:
|
||||||
"""
|
"""
|
||||||
Creates a mask from the two sequences passed to be used in a sequence-pair classification task.
|
Create a mask from the two sequences passed to be used in a sequence-pair classification task.
|
||||||
An XLNet sequence pair mask has the following format:
|
An XLNet sequence pair mask has the following format:
|
||||||
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 2
|
|
||||||
| first sequence | second sequence | CLS segment ID
|
|
||||||
|
|
||||||
if token_ids_1 is None, only returns the first portion of the mask (0's).
|
::
|
||||||
|
|
||||||
|
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
||||||
|
| first sequence | second sequence |
|
||||||
|
|
||||||
|
If :obj:`token_ids_1` is :obj:`None`, this method only returns the first portion of the mask (0s).
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
token_ids_0 (:obj:`List[int]`):
|
token_ids_0 (:obj:`List[int]`):
|
||||||
List of ids.
|
List of IDs.
|
||||||
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
token_ids_1 (:obj:`List[int]`, `optional`):
|
||||||
Optional second list of IDs for sequence pairs.
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|||||||
+181
-295
@@ -1,10 +1,26 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
# Copyright 2020-present the HuggingFace Inc. team.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
"""
|
||||||
|
The Trainer class, to easily train a 🤗 Transformers from scratch or finetune it on a new task.
|
||||||
|
"""
|
||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
import math
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import warnings
|
import warnings
|
||||||
from contextlib import contextmanager
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
|
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
|
||||||
|
|
||||||
@@ -15,8 +31,7 @@ from torch import nn
|
|||||||
from torch.utils.data.dataloader import DataLoader
|
from torch.utils.data.dataloader import DataLoader
|
||||||
from torch.utils.data.dataset import Dataset
|
from torch.utils.data.dataset import Dataset
|
||||||
from torch.utils.data.distributed import DistributedSampler
|
from torch.utils.data.distributed import DistributedSampler
|
||||||
from torch.utils.data.sampler import RandomSampler, Sampler, SequentialSampler
|
from torch.utils.data.sampler import RandomSampler, SequentialSampler
|
||||||
from tqdm.auto import tqdm, trange
|
|
||||||
|
|
||||||
from .data.data_collator import DataCollator, DataCollatorWithPadding, default_data_collator
|
from .data.data_collator import DataCollator, DataCollatorWithPadding, default_data_collator
|
||||||
from .file_utils import WEIGHTS_NAME, is_datasets_available, is_torch_tpu_available
|
from .file_utils import WEIGHTS_NAME, is_datasets_available, is_torch_tpu_available
|
||||||
@@ -34,23 +49,35 @@ from .modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING
|
|||||||
from .modeling_utils import PreTrainedModel
|
from .modeling_utils import PreTrainedModel
|
||||||
from .optimization import AdamW, get_linear_schedule_with_warmup
|
from .optimization import AdamW, get_linear_schedule_with_warmup
|
||||||
from .tokenization_utils_base import PreTrainedTokenizerBase
|
from .tokenization_utils_base import PreTrainedTokenizerBase
|
||||||
from .trainer_utils import (
|
from .trainer_callback import (
|
||||||
PREFIX_CHECKPOINT_DIR,
|
CallbackHandler,
|
||||||
BestRun,
|
DefaultFlowCallback,
|
||||||
EvalPrediction,
|
PrinterCallback,
|
||||||
EvaluationStrategy,
|
ProgressCallback,
|
||||||
HPSearchBackend,
|
TrainerCallback,
|
||||||
PredictionOutput,
|
TrainerControl,
|
||||||
TrainerState,
|
TrainerState,
|
||||||
TrainOutput,
|
)
|
||||||
default_compute_objective,
|
from .trainer_pt_utils import (
|
||||||
default_hp_space,
|
SequentialDistributedSampler,
|
||||||
distributed_broadcast_scalars,
|
distributed_broadcast_scalars,
|
||||||
distributed_concat,
|
distributed_concat,
|
||||||
|
get_tpu_sampler,
|
||||||
nested_concat,
|
nested_concat,
|
||||||
nested_detach,
|
nested_detach,
|
||||||
nested_numpify,
|
nested_numpify,
|
||||||
nested_xla_mesh_reduce,
|
nested_xla_mesh_reduce,
|
||||||
|
reissue_pt_warnings,
|
||||||
|
)
|
||||||
|
from .trainer_utils import (
|
||||||
|
PREFIX_CHECKPOINT_DIR,
|
||||||
|
BestRun,
|
||||||
|
EvalPrediction,
|
||||||
|
HPSearchBackend,
|
||||||
|
PredictionOutput,
|
||||||
|
TrainOutput,
|
||||||
|
default_compute_objective,
|
||||||
|
default_hp_space,
|
||||||
set_seed,
|
set_seed,
|
||||||
)
|
)
|
||||||
from .training_args import TrainingArguments
|
from .training_args import TrainingArguments
|
||||||
@@ -60,7 +87,8 @@ from .utils import logging
|
|||||||
_use_native_amp = False
|
_use_native_amp = False
|
||||||
_use_apex = False
|
_use_apex = False
|
||||||
|
|
||||||
PT_LR_SCHEDULER_WARNING = "Please also save or load the state of the optimzer when saving or loading the scheduler."
|
DEFAULT_CALLBACKS = [DefaultFlowCallback]
|
||||||
|
|
||||||
|
|
||||||
# Check if Pytorch version >= 1.6 to switch between Native AMP and Apex
|
# Check if Pytorch version >= 1.6 to switch between Native AMP and Apex
|
||||||
if version.parse(torch.__version__) < version.parse("1.6"):
|
if version.parse(torch.__version__) < version.parse("1.6"):
|
||||||
@@ -82,16 +110,20 @@ if is_torch_tpu_available():
|
|||||||
import torch_xla.distributed.parallel_loader as pl
|
import torch_xla.distributed.parallel_loader as pl
|
||||||
|
|
||||||
if is_tensorboard_available():
|
if is_tensorboard_available():
|
||||||
try:
|
from .integrations import TensorBoardCallback
|
||||||
from torch.utils.tensorboard import SummaryWriter
|
|
||||||
except ImportError:
|
DEFAULT_CALLBACKS.append(TensorBoardCallback)
|
||||||
from tensorboardX import SummaryWriter
|
|
||||||
|
|
||||||
if is_wandb_available():
|
if is_wandb_available():
|
||||||
import wandb
|
from .integrations import WandbCallback
|
||||||
|
|
||||||
|
DEFAULT_CALLBACKS.append(WandbCallback)
|
||||||
|
|
||||||
if is_comet_available():
|
if is_comet_available():
|
||||||
import comet_ml
|
from .integrations import CometCallback
|
||||||
|
|
||||||
|
DEFAULT_CALLBACKS.append(CometCallback)
|
||||||
|
|
||||||
if is_optuna_available():
|
if is_optuna_available():
|
||||||
import optuna
|
import optuna
|
||||||
@@ -102,91 +134,20 @@ if is_ray_available():
|
|||||||
logger = logging.get_logger(__name__)
|
logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def reissue_pt_warnings(caught_warnings):
|
|
||||||
# Reissue warnings that are not the PT_LR_SCHEDULER_WARNING
|
|
||||||
if len(caught_warnings) > 1:
|
|
||||||
for w in caught_warnings:
|
|
||||||
if w.category != UserWarning or w.message != PT_LR_SCHEDULER_WARNING:
|
|
||||||
warnings.warn(w.message, w.category)
|
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def torch_distributed_zero_first(local_rank: int):
|
|
||||||
"""
|
|
||||||
Decorator to make all processes in distributed training wait for each local_master to do something.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
local_rank (:obj:`int`): The rank of the local process.
|
|
||||||
"""
|
|
||||||
if local_rank not in [-1, 0]:
|
|
||||||
torch.distributed.barrier()
|
|
||||||
yield
|
|
||||||
if local_rank == 0:
|
|
||||||
torch.distributed.barrier()
|
|
||||||
|
|
||||||
|
|
||||||
class SequentialDistributedSampler(Sampler):
|
|
||||||
"""
|
|
||||||
Distributed Sampler that subsamples indicies sequentially,
|
|
||||||
making it easier to collate all results at the end.
|
|
||||||
|
|
||||||
Even though we only use this sampler for eval and predict (no training),
|
|
||||||
which means that the model params won't have to be synced (i.e. will not hang
|
|
||||||
for synchronization even if varied number of forward passes), we still add extra
|
|
||||||
samples to the sampler to make it evenly divisible (like in `DistributedSampler`)
|
|
||||||
to make it easy to `gather` or `reduce` resulting tensors at the end of the loop.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, dataset, num_replicas=None, rank=None):
|
|
||||||
if num_replicas is None:
|
|
||||||
if not torch.distributed.is_available():
|
|
||||||
raise RuntimeError("Requires distributed package to be available")
|
|
||||||
num_replicas = torch.distributed.get_world_size()
|
|
||||||
if rank is None:
|
|
||||||
if not torch.distributed.is_available():
|
|
||||||
raise RuntimeError("Requires distributed package to be available")
|
|
||||||
rank = torch.distributed.get_rank()
|
|
||||||
self.dataset = dataset
|
|
||||||
self.num_replicas = num_replicas
|
|
||||||
self.rank = rank
|
|
||||||
self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.num_replicas))
|
|
||||||
self.total_size = self.num_samples * self.num_replicas
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
indices = list(range(len(self.dataset)))
|
|
||||||
|
|
||||||
# add extra samples to make it evenly divisible
|
|
||||||
indices += indices[: (self.total_size - len(indices))]
|
|
||||||
assert (
|
|
||||||
len(indices) == self.total_size
|
|
||||||
), f"Indices length {len(indices)} and total size {self.total_size} mismatched"
|
|
||||||
|
|
||||||
# subsample
|
|
||||||
indices = indices[self.rank * self.num_samples : (self.rank + 1) * self.num_samples]
|
|
||||||
assert (
|
|
||||||
len(indices) == self.num_samples
|
|
||||||
), f"Indices length {len(indices)} and sample number {self.num_samples} mismatched"
|
|
||||||
|
|
||||||
return iter(indices)
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return self.num_samples
|
|
||||||
|
|
||||||
|
|
||||||
def get_tpu_sampler(dataset: Dataset):
|
|
||||||
if xm.xrt_world_size() <= 1:
|
|
||||||
return RandomSampler(dataset)
|
|
||||||
return DistributedSampler(dataset, num_replicas=xm.xrt_world_size(), rank=xm.get_ordinal())
|
|
||||||
|
|
||||||
|
|
||||||
class Trainer:
|
class Trainer:
|
||||||
"""
|
"""
|
||||||
Trainer is a simple but feature-complete training and eval loop for PyTorch,
|
Trainer is a simple but feature-complete training and eval loop for PyTorch,
|
||||||
optimized for 🤗 Transformers.
|
optimized for 🤗 Transformers.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
model (:class:`~transformers.PreTrainedModel`, `optional`):
|
model (:class:`~transformers.PreTrainedModel` or :obj:`torch.nn.Module`, `optional`):
|
||||||
The model to train, evaluate or use for predictions. If not provided, a ``model_init`` must be passed.
|
The model to train, evaluate or use for predictions. If not provided, a ``model_init`` must be passed.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
:class:`~transformers.Trainer` is optimized to work with the :class:`~transformers.PreTrainedModel`
|
||||||
|
provided by the library. You can still use your own models defined as :obj:`torch.nn.Module` as long as
|
||||||
|
they work the same way as the 🤗 Transformers models.
|
||||||
args (:class:`~transformers.TrainingArguments`, `optional`):
|
args (:class:`~transformers.TrainingArguments`, `optional`):
|
||||||
The arguments to tweak for training. Will default to a basic instance of :class:`~transformers.TrainingArguments`
|
The arguments to tweak for training. Will default to a basic instance of :class:`~transformers.TrainingArguments`
|
||||||
with the ``output_dir`` set to a directory named `tmp_trainer` in the current directory if not provided.
|
with the ``output_dir`` set to a directory named `tmp_trainer` in the current directory if not provided.
|
||||||
@@ -210,8 +171,11 @@ class Trainer:
|
|||||||
compute_metrics (:obj:`Callable[[EvalPrediction], Dict]`, `optional`):
|
compute_metrics (:obj:`Callable[[EvalPrediction], Dict]`, `optional`):
|
||||||
The function that will be used to compute metrics at evaluation. Must take a
|
The function that will be used to compute metrics at evaluation. Must take a
|
||||||
:class:`~transformers.EvalPrediction` and return a dictionary string to metric values.
|
:class:`~transformers.EvalPrediction` and return a dictionary string to metric values.
|
||||||
tb_writer (:obj:`SummaryWriter`, `optional`):
|
callbacks (List of :obj:`~transformers.TrainerCallback`, `optional`):
|
||||||
Object to write to TensorBoard.
|
A list of callbacks to customize the training loop. Will add those to the list of default callbacks
|
||||||
|
detailed in :doc:`here <callback>`.
|
||||||
|
|
||||||
|
If you want to remove one of the default callbacks used, use the :meth:`Trainer.remove_callback` method.
|
||||||
optimizers (:obj:`Tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR`, `optional`):
|
optimizers (:obj:`Tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR`, `optional`):
|
||||||
A tuple containing the optimizer and the scheduler to use. Will default to an instance of
|
A tuple containing the optimizer and the scheduler to use. Will default to an instance of
|
||||||
:class:`~transformers.AdamW` on your model and a scheduler given by
|
:class:`~transformers.AdamW` on your model and a scheduler given by
|
||||||
@@ -222,7 +186,7 @@ class Trainer:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
model: PreTrainedModel = None,
|
model: Union[PreTrainedModel, torch.nn.Module] = None,
|
||||||
args: TrainingArguments = None,
|
args: TrainingArguments = None,
|
||||||
data_collator: Optional[DataCollator] = None,
|
data_collator: Optional[DataCollator] = None,
|
||||||
train_dataset: Optional[Dataset] = None,
|
train_dataset: Optional[Dataset] = None,
|
||||||
@@ -230,7 +194,7 @@ class Trainer:
|
|||||||
tokenizer: Optional["PreTrainedTokenizerBase"] = None,
|
tokenizer: Optional["PreTrainedTokenizerBase"] = None,
|
||||||
model_init: Callable[[], PreTrainedModel] = None,
|
model_init: Callable[[], PreTrainedModel] = None,
|
||||||
compute_metrics: Optional[Callable[[EvalPrediction], Dict]] = None,
|
compute_metrics: Optional[Callable[[EvalPrediction], Dict]] = None,
|
||||||
tb_writer: Optional["SummaryWriter"] = None,
|
callbacks: Optional[List[TrainerCallback]] = None,
|
||||||
optimizers: Tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR] = (None, None),
|
optimizers: Tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR] = (None, None),
|
||||||
**kwargs,
|
**kwargs,
|
||||||
):
|
):
|
||||||
@@ -259,7 +223,21 @@ class Trainer:
|
|||||||
"Passing a `model_init` is incompatible with providing the `optimizers` argument."
|
"Passing a `model_init` is incompatible with providing the `optimizers` argument."
|
||||||
"You should subclass `Trainer` and override the `create_optimizer_and_scheduler` method."
|
"You should subclass `Trainer` and override the `create_optimizer_and_scheduler` method."
|
||||||
)
|
)
|
||||||
self.tb_writer = tb_writer
|
callbacks = DEFAULT_CALLBACKS if callbacks is None else DEFAULT_CALLBACKS + callbacks
|
||||||
|
self.callback_handler = CallbackHandler(callbacks, self.model, self.optimizer, self.lr_scheduler)
|
||||||
|
self.add_callback(PrinterCallback if self.args.disable_tqdm else ProgressCallback)
|
||||||
|
|
||||||
|
# Deprecated arguments
|
||||||
|
if "tb_writer" in kwargs:
|
||||||
|
warnings.warn(
|
||||||
|
"Passing `tb_writer` as a keyword argument is deprecated and won't be possible in a "
|
||||||
|
+ "future version. Use `TensorBoardCallback(tb_writer=...)` instead and pass it to the `callbacks`"
|
||||||
|
+ "argument",
|
||||||
|
FutureWarning,
|
||||||
|
)
|
||||||
|
tb_writer = kwargs.pop("tb_writer")
|
||||||
|
self.remove_callback(TensorBoardCallback)
|
||||||
|
self.add_callback(TensorBoardCallback(tb_writer=tb_writer))
|
||||||
if "prediction_loss_only" in kwargs:
|
if "prediction_loss_only" in kwargs:
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
"Passing `prediction_loss_only` as a keyword argument is deprecated and won't be possible in a "
|
"Passing `prediction_loss_only` as a keyword argument is deprecated and won't be possible in a "
|
||||||
@@ -270,13 +248,6 @@ class Trainer:
|
|||||||
self.args.prediction_loss_only = kwargs.pop("prediction_loss_only")
|
self.args.prediction_loss_only = kwargs.pop("prediction_loss_only")
|
||||||
assert kwargs == {}, f"Unexpected keyword arguments: {list(kwargs.keys())}."
|
assert kwargs == {}, f"Unexpected keyword arguments: {list(kwargs.keys())}."
|
||||||
|
|
||||||
if tb_writer is None and is_tensorboard_available() and self.is_world_process_zero():
|
|
||||||
self.tb_writer = SummaryWriter(log_dir=self.args.logging_dir)
|
|
||||||
if not is_tensorboard_available():
|
|
||||||
logger.warning(
|
|
||||||
"You are instantiating a Trainer but Tensorboard is not installed. You should consider installing it."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Will be set to True by `self._setup_loggers()` on first call to `self.log()`.
|
# Will be set to True by `self._setup_loggers()` on first call to `self.log()`.
|
||||||
self._loggers_initialized = False
|
self._loggers_initialized = False
|
||||||
|
|
||||||
@@ -304,6 +275,7 @@ class Trainer:
|
|||||||
self._remove_unused_columns(self.eval_dataset, description="evaluation")
|
self._remove_unused_columns(self.eval_dataset, description="evaluation")
|
||||||
|
|
||||||
self.state = TrainerState()
|
self.state = TrainerState()
|
||||||
|
self.control = TrainerControl()
|
||||||
# Internal variable for total_flos used to count as tensors (for distributed + TPU), will be sent in the
|
# Internal variable for total_flos used to count as tensors (for distributed + TPU), will be sent in the
|
||||||
# state at each call to self.log.
|
# state at each call to self.log.
|
||||||
self._total_flos = None
|
self._total_flos = None
|
||||||
@@ -317,6 +289,45 @@ class Trainer:
|
|||||||
else ["labels"]
|
else ["labels"]
|
||||||
)
|
)
|
||||||
self.label_names = default_label_names if self.args.label_names is None else self.args.label_names
|
self.label_names = default_label_names if self.args.label_names is None else self.args.label_names
|
||||||
|
self.control = self.callback_handler.on_init_end(self.args, self.state, self.control)
|
||||||
|
|
||||||
|
def add_callback(self, callback):
|
||||||
|
"""
|
||||||
|
Add a callback to the current list of :class:`~transformer.TrainerCallback`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (:obj:`type` or :class:`~transformer.TrainerCallback`):
|
||||||
|
A :class:`~transformer.TrainerCallback` class or an instance of a :class:`~transformer.TrainerCallback`.
|
||||||
|
In the first case, will instantiate a member of that class.
|
||||||
|
"""
|
||||||
|
self.callback_handler.add_callback(callback)
|
||||||
|
|
||||||
|
def pop_callback(self, callback):
|
||||||
|
"""
|
||||||
|
Remove a callback from the current list of :class:`~transformer.TrainerCallback` and returns it.
|
||||||
|
|
||||||
|
If the callback is not found, returns :obj:`None` (and no error is raised).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (:obj:`type` or :class:`~transformer.TrainerCallback`):
|
||||||
|
A :class:`~transformer.TrainerCallback` class or an instance of a :class:`~transformer.TrainerCallback`.
|
||||||
|
In the first case, will pop the first member of that class found in the list of callbacks.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
:class:`~transformer.TrainerCallback`: The callback removed, if found.
|
||||||
|
"""
|
||||||
|
return self.callback_handler.pop_callback(callback)
|
||||||
|
|
||||||
|
def remove_callback(self, callback):
|
||||||
|
"""
|
||||||
|
Remove a callback from the current list of :class:`~transformer.TrainerCallback`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (:obj:`type` or :class:`~transformer.TrainerCallback`):
|
||||||
|
A :class:`~transformer.TrainerCallback` class or an instance of a :class:`~transformer.TrainerCallback`.
|
||||||
|
In the first case, will remove the first member of that class found in the list of callbacks.
|
||||||
|
"""
|
||||||
|
self.callback_handler.remove_callback(callback)
|
||||||
|
|
||||||
def _remove_unused_columns(self, dataset: "datasets.Dataset", description: Optional[str] = None):
|
def _remove_unused_columns(self, dataset: "datasets.Dataset", description: Optional[str] = None):
|
||||||
if not self.args.remove_unused_columns:
|
if not self.args.remove_unused_columns:
|
||||||
@@ -465,102 +476,12 @@ class Trainer:
|
|||||||
self.optimizer, num_warmup_steps=self.args.warmup_steps, num_training_steps=num_training_steps
|
self.optimizer, num_warmup_steps=self.args.warmup_steps, num_training_steps=num_training_steps
|
||||||
)
|
)
|
||||||
|
|
||||||
def setup_wandb(self):
|
|
||||||
"""
|
|
||||||
Setup the optional Weights & Biases (`wandb`) integration.
|
|
||||||
|
|
||||||
One can subclass and override this method to customize the setup if needed. Find more information
|
|
||||||
`here <https://docs.wandb.com/huggingface>`__. You can also override the following environment variables:
|
|
||||||
|
|
||||||
Environment:
|
|
||||||
WANDB_WATCH:
|
|
||||||
(Optional, ["gradients", "all", "false"]) "gradients" by default, set to "false" to disable gradient logging
|
|
||||||
or "all" to log gradients and parameters
|
|
||||||
WANDB_PROJECT:
|
|
||||||
(Optional): str - "huggingface" by default, set this to a custom string to store results in a different project
|
|
||||||
WANDB_DISABLED:
|
|
||||||
(Optional): boolean - defaults to false, set to "true" to disable wandb entirely
|
|
||||||
"""
|
|
||||||
if hasattr(self, "_setup_wandb"):
|
|
||||||
warnings.warn(
|
|
||||||
"The `_setup_wandb` method is deprecated and won't be called in a future version, define `setup_wandb` in your subclass.",
|
|
||||||
FutureWarning,
|
|
||||||
)
|
|
||||||
return self._setup_wandb()
|
|
||||||
|
|
||||||
if self.is_world_process_zero():
|
|
||||||
logger.info(
|
|
||||||
'Automatic Weights & Biases logging enabled, to disable set os.environ["WANDB_DISABLED"] = "true"'
|
|
||||||
)
|
|
||||||
combined_dict = {**self.args.to_sanitized_dict()}
|
|
||||||
if isinstance(self.model, PreTrainedModel):
|
|
||||||
combined_dict = {**self.model.config.to_dict(), **combined_dict}
|
|
||||||
wandb.init(
|
|
||||||
project=os.getenv("WANDB_PROJECT", "huggingface"), config=combined_dict, name=self.args.run_name
|
|
||||||
)
|
|
||||||
# keep track of model topology and gradients, unsupported on TPU
|
|
||||||
if not is_torch_tpu_available() and os.getenv("WANDB_WATCH") != "false":
|
|
||||||
wandb.watch(
|
|
||||||
self.model, log=os.getenv("WANDB_WATCH", "gradients"), log_freq=max(100, self.args.logging_steps)
|
|
||||||
)
|
|
||||||
|
|
||||||
def setup_comet(self):
|
|
||||||
"""
|
|
||||||
Setup the optional Comet.ml integration.
|
|
||||||
|
|
||||||
Environment:
|
|
||||||
COMET_MODE:
|
|
||||||
(Optional): str - "OFFLINE", "ONLINE", or "DISABLED"
|
|
||||||
COMET_PROJECT_NAME:
|
|
||||||
(Optional): str - Comet.ml project name for experiments
|
|
||||||
COMET_OFFLINE_DIRECTORY:
|
|
||||||
(Optional): str - folder to use for saving offline experiments when `COMET_MODE` is "OFFLINE"
|
|
||||||
|
|
||||||
For a number of configurable items in the environment,
|
|
||||||
see `here <https://www.comet.ml/docs/python-sdk/advanced/#comet-configuration-variables>`__
|
|
||||||
"""
|
|
||||||
if self.is_world_master():
|
|
||||||
comet_mode = os.getenv("COMET_MODE", "ONLINE").upper()
|
|
||||||
args = {"project_name": os.getenv("COMET_PROJECT_NAME", "huggingface")}
|
|
||||||
experiment = None
|
|
||||||
if comet_mode == "ONLINE":
|
|
||||||
experiment = comet_ml.Experiment(**args)
|
|
||||||
logger.info("Automatic Comet.ml online logging enabled")
|
|
||||||
elif comet_mode == "OFFLINE":
|
|
||||||
args["offline_directory"] = os.getenv("COMET_OFFLINE_DIRECTORY", "./")
|
|
||||||
experiment = comet_ml.OfflineExperiment(**args)
|
|
||||||
logger.info("Automatic Comet.ml offline logging enabled; use `comet upload` when finished")
|
|
||||||
if experiment is not None:
|
|
||||||
experiment._set_model_graph(self.model, framework="transformers")
|
|
||||||
experiment._log_parameters(self.args, prefix="args/", framework="transformers")
|
|
||||||
if isinstance(self.model, PreTrainedModel):
|
|
||||||
experiment._log_parameters(self.model.config, prefix="config/", framework="transformers")
|
|
||||||
|
|
||||||
def num_examples(self, dataloader: DataLoader) -> int:
|
def num_examples(self, dataloader: DataLoader) -> int:
|
||||||
"""
|
"""
|
||||||
Helper to get number of samples in a :class:`~torch.utils.data.DataLoader` by accessing its dataset.
|
Helper to get number of samples in a :class:`~torch.utils.data.DataLoader` by accessing its dataset.
|
||||||
"""
|
"""
|
||||||
return len(dataloader.dataset)
|
return len(dataloader.dataset)
|
||||||
|
|
||||||
def _setup_loggers(self):
|
|
||||||
if self._loggers_initialized:
|
|
||||||
return
|
|
||||||
if is_wandb_available():
|
|
||||||
self.setup_wandb()
|
|
||||||
elif os.environ.get("WANDB_DISABLED") != "true":
|
|
||||||
logger.info(
|
|
||||||
"You are instantiating a Trainer but W&B is not installed. To use wandb logging, "
|
|
||||||
"run `pip install wandb; wandb login` see https://docs.wandb.com/huggingface."
|
|
||||||
)
|
|
||||||
if is_comet_available():
|
|
||||||
self.setup_comet()
|
|
||||||
elif os.environ.get("COMET_MODE") != "DISABLED":
|
|
||||||
logger.info(
|
|
||||||
"To use comet_ml logging, run `pip/conda install comet_ml` "
|
|
||||||
"see https://www.comet.ml/docs/python-sdk/huggingface/"
|
|
||||||
)
|
|
||||||
self._loggers_initialized = True
|
|
||||||
|
|
||||||
def _hp_search_setup(self, trial: Union["optuna.Trial", Dict[str, Any]]):
|
def _hp_search_setup(self, trial: Union["optuna.Trial", Dict[str, Any]]):
|
||||||
""" HP search setup code """
|
""" HP search setup code """
|
||||||
if self.hp_search_backend is None or trial is None:
|
if self.hp_search_backend is None or trial is None:
|
||||||
@@ -661,7 +582,7 @@ class Trainer:
|
|||||||
self.lr_scheduler.load_state_dict(torch.load(os.path.join(model_path, "scheduler.pt")))
|
self.lr_scheduler.load_state_dict(torch.load(os.path.join(model_path, "scheduler.pt")))
|
||||||
reissue_pt_warnings(caught_warnings)
|
reissue_pt_warnings(caught_warnings)
|
||||||
|
|
||||||
# Moxed precision training with apex (torch < 1.6)
|
# Mixed precision training with apex (torch < 1.6)
|
||||||
model = self.model
|
model = self.model
|
||||||
if self.args.fp16 and _use_apex:
|
if self.args.fp16 and _use_apex:
|
||||||
if not is_apex_available():
|
if not is_apex_available():
|
||||||
@@ -687,10 +608,6 @@ class Trainer:
|
|||||||
# find_unused_parameters breaks checkpointing as per
|
# find_unused_parameters breaks checkpointing as per
|
||||||
# https://github.com/huggingface/transformers/pull/4659#issuecomment-643356021
|
# https://github.com/huggingface/transformers/pull/4659#issuecomment-643356021
|
||||||
|
|
||||||
if self.tb_writer is not None:
|
|
||||||
self.tb_writer.add_text("args", self.args.to_json_string())
|
|
||||||
self.tb_writer.add_hparams(self.args.to_sanitized_dict(), metric_dict={})
|
|
||||||
|
|
||||||
# Train!
|
# Train!
|
||||||
if is_torch_tpu_available():
|
if is_torch_tpu_available():
|
||||||
total_train_batch_size = self.args.train_batch_size * xm.xrt_world_size()
|
total_train_batch_size = self.args.train_batch_size * xm.xrt_world_size()
|
||||||
@@ -723,17 +640,25 @@ class Trainer:
|
|||||||
logger.info(" Continuing training from global step %d", self.state.global_step)
|
logger.info(" Continuing training from global step %d", self.state.global_step)
|
||||||
logger.info(" Will skip the first %d steps in the first epoch", steps_trained_in_current_epoch)
|
logger.info(" Will skip the first %d steps in the first epoch", steps_trained_in_current_epoch)
|
||||||
|
|
||||||
|
# Update the references
|
||||||
|
self.callback_handler.model = self.model
|
||||||
|
self.callback_handler.optimizer = self.optimizer
|
||||||
|
self.callback_handler.lr_scheduler = self.lr_scheduler
|
||||||
|
self.callback_handler.train_dataloader = train_dataloader
|
||||||
# This should be the same if the state has been saved but in case the training arguments changed, it's safer
|
# This should be the same if the state has been saved but in case the training arguments changed, it's safer
|
||||||
# to set this after the load.
|
# to set this after the load.
|
||||||
self.state.max_steps = max_steps
|
self.state.max_steps = max_steps
|
||||||
self.state.num_train_epochs = num_train_epochs
|
self.state.num_train_epochs = num_train_epochs
|
||||||
|
self.state.is_local_process_zero = self.is_local_process_zero()
|
||||||
|
self.state.is_world_process_zero = self.is_world_process_zero()
|
||||||
|
|
||||||
tr_loss = torch.tensor(0.0).to(self.args.device)
|
tr_loss = torch.tensor(0.0).to(self.args.device)
|
||||||
|
self._logging_loss_scalar = 0
|
||||||
self._total_flos = self.state.total_flos
|
self._total_flos = self.state.total_flos
|
||||||
logging_loss_scalar = 0.0
|
|
||||||
model.zero_grad()
|
model.zero_grad()
|
||||||
disable_tqdm = self.args.disable_tqdm or not self.is_local_process_zero()
|
|
||||||
train_pbar = trange(epochs_trained, num_train_epochs, desc="Epoch", disable=disable_tqdm)
|
self.control = self.callback_handler.on_train_begin(self.args, self.state, self.control)
|
||||||
|
|
||||||
for epoch in range(epochs_trained, num_train_epochs):
|
for epoch in range(epochs_trained, num_train_epochs):
|
||||||
if isinstance(train_dataloader, DataLoader) and isinstance(train_dataloader.sampler, DistributedSampler):
|
if isinstance(train_dataloader, DataLoader) and isinstance(train_dataloader.sampler, DistributedSampler):
|
||||||
train_dataloader.sampler.set_epoch(epoch)
|
train_dataloader.sampler.set_epoch(epoch)
|
||||||
@@ -750,15 +675,18 @@ class Trainer:
|
|||||||
if self.args.past_index >= 0:
|
if self.args.past_index >= 0:
|
||||||
self._past = None
|
self._past = None
|
||||||
|
|
||||||
epoch_pbar = tqdm(epoch_iterator, desc="Iteration", disable=disable_tqdm)
|
self.control = self.callback_handler.on_epoch_begin(self.args, self.state, self.control)
|
||||||
|
|
||||||
for step, inputs in enumerate(epoch_iterator):
|
for step, inputs in enumerate(epoch_iterator):
|
||||||
|
|
||||||
# Skip past any already trained steps if resuming training
|
# Skip past any already trained steps if resuming training
|
||||||
if steps_trained_in_current_epoch > 0:
|
if steps_trained_in_current_epoch > 0:
|
||||||
steps_trained_in_current_epoch -= 1
|
steps_trained_in_current_epoch -= 1
|
||||||
epoch_pbar.update(1)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if (step + 1) % self.args.gradient_accumulation_steps == 0:
|
||||||
|
self.control = self.callback_handler.on_step_begin(self.args, self.state, self.control)
|
||||||
|
|
||||||
tr_loss += self.training_step(model, inputs)
|
tr_loss += self.training_step(model, inputs)
|
||||||
self._total_flos += self.floating_point_ops(inputs)
|
self._total_flos += self.floating_point_ops(inputs)
|
||||||
|
|
||||||
@@ -787,50 +715,15 @@ class Trainer:
|
|||||||
model.zero_grad()
|
model.zero_grad()
|
||||||
self.state.global_step += 1
|
self.state.global_step += 1
|
||||||
self.state.epoch = epoch + (step + 1) / len(epoch_iterator)
|
self.state.epoch = epoch + (step + 1) / len(epoch_iterator)
|
||||||
|
self.control = self.callback_handler.on_step_end(self.args, self.state, self.control)
|
||||||
|
|
||||||
if (self.args.logging_steps > 0 and self.state.global_step % self.args.logging_steps == 0) or (
|
self._maybe_log_save_evalute(tr_loss, model, trial, epoch)
|
||||||
self.state.global_step == 1 and self.args.logging_first_step
|
|
||||||
):
|
|
||||||
logs: Dict[str, float] = {}
|
|
||||||
tr_loss_scalar = tr_loss.item()
|
|
||||||
logs["loss"] = (tr_loss_scalar - logging_loss_scalar) / self.args.logging_steps
|
|
||||||
# backward compatibility for pytorch schedulers
|
|
||||||
logs["learning_rate"] = (
|
|
||||||
self.lr_scheduler.get_last_lr()[0]
|
|
||||||
if version.parse(torch.__version__) >= version.parse("1.4")
|
|
||||||
else self.lr_scheduler.get_lr()[0]
|
|
||||||
)
|
|
||||||
logging_loss_scalar = tr_loss_scalar
|
|
||||||
|
|
||||||
self.log(logs)
|
if self.control.should_epoch_stop or self.control.should_training_stop:
|
||||||
|
|
||||||
if (
|
|
||||||
self.args.evaluation_strategy == EvaluationStrategy.STEPS
|
|
||||||
and self.state.global_step % self.args.eval_steps == 0
|
|
||||||
):
|
|
||||||
metrics = self.evaluate()
|
|
||||||
self._report_to_hp_search(trial, epoch, metrics)
|
|
||||||
if self.args.load_best_model_at_end:
|
|
||||||
self._save_training(model, trial, metrics=metrics)
|
|
||||||
|
|
||||||
if (
|
|
||||||
not self.args.load_best_model_at_end
|
|
||||||
and self.args.save_steps > 0
|
|
||||||
and self.state.global_step % self.args.save_steps == 0
|
|
||||||
):
|
|
||||||
self._save_training(model, trial)
|
|
||||||
|
|
||||||
epoch_pbar.update(1)
|
|
||||||
if self.state.global_step >= max_steps:
|
|
||||||
break
|
break
|
||||||
epoch_pbar.close()
|
|
||||||
train_pbar.update(1)
|
|
||||||
|
|
||||||
if self.args.evaluation_strategy == EvaluationStrategy.EPOCH:
|
self.control = self.callback_handler.on_epoch_end(self.args, self.state, self.control)
|
||||||
metrics = self.evaluate()
|
self._maybe_log_save_evalute(tr_loss, model, trial, epoch)
|
||||||
self._report_to_hp_search(trial, epoch, metrics)
|
|
||||||
if self.args.load_best_model_at_end:
|
|
||||||
self._save_training(model, trial, metrics=metrics)
|
|
||||||
|
|
||||||
if self.args.tpu_metrics_debug or self.args.debug:
|
if self.args.tpu_metrics_debug or self.args.debug:
|
||||||
if is_torch_tpu_available():
|
if is_torch_tpu_available():
|
||||||
@@ -841,12 +734,9 @@ class Trainer:
|
|||||||
"You enabled PyTorch/XLA debug metrics but you don't have a TPU "
|
"You enabled PyTorch/XLA debug metrics but you don't have a TPU "
|
||||||
"configured. Check your training configuration if this is unexpected."
|
"configured. Check your training configuration if this is unexpected."
|
||||||
)
|
)
|
||||||
if self.state.global_step >= max_steps:
|
if self.control.should_training_stop:
|
||||||
break
|
break
|
||||||
|
|
||||||
train_pbar.close()
|
|
||||||
if self.tb_writer:
|
|
||||||
self.tb_writer.close()
|
|
||||||
if self.args.past_index and hasattr(self, "_past"):
|
if self.args.past_index and hasattr(self, "_past"):
|
||||||
# Clean the state at the end of training
|
# Clean the state at the end of training
|
||||||
delattr(self, "_past")
|
delattr(self, "_past")
|
||||||
@@ -863,9 +753,36 @@ class Trainer:
|
|||||||
state_dict = torch.load(os.path.join(self.state.best_model_checkpoint, WEIGHTS_NAME))
|
state_dict = torch.load(os.path.join(self.state.best_model_checkpoint, WEIGHTS_NAME))
|
||||||
self.model.load_state_dict(state_dict)
|
self.model.load_state_dict(state_dict)
|
||||||
|
|
||||||
|
self.control = self.callback_handler.on_train_end(self.args, self.state, self.control)
|
||||||
|
|
||||||
return TrainOutput(self.state.global_step, tr_loss.item() / self.state.global_step)
|
return TrainOutput(self.state.global_step, tr_loss.item() / self.state.global_step)
|
||||||
|
|
||||||
def _save_training(self, model, trial, metrics=None):
|
def _maybe_log_save_evalute(self, tr_loss, model, trial, epoch):
|
||||||
|
if self.control.should_log:
|
||||||
|
logs: Dict[str, float] = {}
|
||||||
|
tr_loss_scalar = tr_loss.item()
|
||||||
|
logs["loss"] = (tr_loss_scalar - self._logging_loss_scalar) / self.args.logging_steps
|
||||||
|
# backward compatibility for pytorch schedulers
|
||||||
|
logs["learning_rate"] = (
|
||||||
|
self.lr_scheduler.get_last_lr()[0]
|
||||||
|
if version.parse(torch.__version__) >= version.parse("1.4")
|
||||||
|
else self.lr_scheduler.get_lr()[0]
|
||||||
|
)
|
||||||
|
self._logging_loss_scalar = tr_loss_scalar
|
||||||
|
|
||||||
|
self.log(logs)
|
||||||
|
|
||||||
|
metrics = None
|
||||||
|
if self.control.should_evaluate:
|
||||||
|
metrics = self.evaluate()
|
||||||
|
self._report_to_hp_search(trial, epoch, metrics)
|
||||||
|
self.control = self.callback_handler.on_evaluate(self.args, self.state, self.control, metrics)
|
||||||
|
|
||||||
|
if self.control.should_save:
|
||||||
|
self._save_checkpoint(model, trial, metrics=metrics)
|
||||||
|
self.control = self.callback_handler.on_save(self.args, self.state, self.control)
|
||||||
|
|
||||||
|
def _save_checkpoint(self, model, trial, metrics=None):
|
||||||
# In all cases (even distributed/parallel), self.model is always a reference
|
# In all cases (even distributed/parallel), self.model is always a reference
|
||||||
# to the model we want to save.
|
# to the model we want to save.
|
||||||
if hasattr(model, "module"):
|
if hasattr(model, "module"):
|
||||||
@@ -896,7 +813,7 @@ class Trainer:
|
|||||||
reissue_pt_warnings(caught_warnings)
|
reissue_pt_warnings(caught_warnings)
|
||||||
|
|
||||||
# Determine the new best metric / best model checkpoint
|
# Determine the new best metric / best model checkpoint
|
||||||
if metrics is not None:
|
if metrics is not None and self.args.metric_for_best_model is not None:
|
||||||
metric_to_check = self.args.metric_for_best_model
|
metric_to_check = self.args.metric_for_best_model
|
||||||
if not metric_to_check.startswith("eval_"):
|
if not metric_to_check.startswith("eval_"):
|
||||||
metric_to_check = f"eval_{metric_to_check}"
|
metric_to_check = f"eval_{metric_to_check}"
|
||||||
@@ -998,7 +915,7 @@ class Trainer:
|
|||||||
self.hp_search_backend = None
|
self.hp_search_backend = None
|
||||||
return best_run
|
return best_run
|
||||||
|
|
||||||
def log(self, logs: Dict[str, float], iterator: Optional[tqdm] = None) -> None:
|
def log(self, logs: Dict[str, float]) -> None:
|
||||||
"""
|
"""
|
||||||
Log :obj:`logs` on the various objects watching training.
|
Log :obj:`logs` on the various objects watching training.
|
||||||
|
|
||||||
@@ -1007,55 +924,22 @@ class Trainer:
|
|||||||
Args:
|
Args:
|
||||||
logs (:obj:`Dict[str, float]`):
|
logs (:obj:`Dict[str, float]`):
|
||||||
The values to log.
|
The values to log.
|
||||||
iterator (:obj:`tqdm`, `optional`):
|
|
||||||
A potential tqdm progress bar to write the logs on.
|
|
||||||
"""
|
"""
|
||||||
# Set up loggers like W&B or Comet ML
|
|
||||||
self._setup_loggers()
|
|
||||||
|
|
||||||
if hasattr(self, "_log"):
|
if hasattr(self, "_log"):
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
"The `_log` method is deprecated and won't be called in a future version, define `log` in your subclass.",
|
"The `_log` method is deprecated and won't be called in a future version, define `log` in your subclass.",
|
||||||
FutureWarning,
|
FutureWarning,
|
||||||
)
|
)
|
||||||
return self._log(logs, iterator=iterator)
|
return self._log(logs)
|
||||||
|
|
||||||
if self.state.epoch is not None:
|
if self.state.epoch is not None:
|
||||||
logs["epoch"] = self.state.epoch
|
logs["epoch"] = self.state.epoch
|
||||||
if self._total_flos is not None:
|
if self._total_flos is not None:
|
||||||
self.store_flos()
|
self.store_flos()
|
||||||
logs["total_flos"] = self.state.total_flos
|
logs["total_flos"] = self.state.total_flos
|
||||||
if self.tb_writer:
|
self.control = self.callback_handler.on_log(self.args, self.state, self.control, logs)
|
||||||
for k, v in logs.items():
|
|
||||||
if isinstance(v, (int, float)):
|
|
||||||
self.tb_writer.add_scalar(k, v, self.state.global_step)
|
|
||||||
else:
|
|
||||||
logger.warning(
|
|
||||||
"Trainer is attempting to log a value of "
|
|
||||||
'"%s" of type %s for key "%s" as a scalar. '
|
|
||||||
"This invocation of Tensorboard's writer.add_scalar() "
|
|
||||||
"is incorrect so we dropped this attribute.",
|
|
||||||
v,
|
|
||||||
type(v),
|
|
||||||
k,
|
|
||||||
)
|
|
||||||
self.tb_writer.flush()
|
|
||||||
if is_wandb_available():
|
|
||||||
if self.is_world_process_zero():
|
|
||||||
wandb.log(logs, step=self.state.global_step)
|
|
||||||
if is_comet_available():
|
|
||||||
if self.is_world_process_zero():
|
|
||||||
experiment = comet_ml.config.get_global_experiment()
|
|
||||||
if experiment is not None:
|
|
||||||
experiment._log_metrics(
|
|
||||||
logs, step=self.state.global_step, epoch=self.state.epoch, framework="transformers"
|
|
||||||
)
|
|
||||||
output = {**logs, **{"step": self.state.global_step}}
|
output = {**logs, **{"step": self.state.global_step}}
|
||||||
self.state.log_history.append(output)
|
self.state.log_history.append(output)
|
||||||
if iterator is not None:
|
|
||||||
iterator.write(output)
|
|
||||||
else:
|
|
||||||
print(output)
|
|
||||||
|
|
||||||
def _prepare_inputs(self, inputs: Dict[str, Union[torch.Tensor, Any]]) -> Dict[str, Union[torch.Tensor, Any]]:
|
def _prepare_inputs(self, inputs: Dict[str, Union[torch.Tensor, Any]]) -> Dict[str, Union[torch.Tensor, Any]]:
|
||||||
"""
|
"""
|
||||||
@@ -1372,8 +1256,9 @@ class Trainer:
|
|||||||
if self.args.past_index >= 0:
|
if self.args.past_index >= 0:
|
||||||
self._past = None
|
self._past = None
|
||||||
|
|
||||||
disable_tqdm = not self.is_local_process_zero() or self.args.disable_tqdm
|
self.callback_handler.eval_dataloader = dataloader
|
||||||
for inputs in tqdm(dataloader, desc=description, disable=disable_tqdm):
|
|
||||||
|
for inputs in dataloader:
|
||||||
loss, logits, labels = self.prediction_step(model, inputs, prediction_loss_only)
|
loss, logits, labels = self.prediction_step(model, inputs, prediction_loss_only)
|
||||||
batch_size = inputs[list(inputs.keys())[0]].shape[0]
|
batch_size = inputs[list(inputs.keys())[0]].shape[0]
|
||||||
if loss is not None:
|
if loss is not None:
|
||||||
@@ -1382,6 +1267,7 @@ class Trainer:
|
|||||||
preds = logits if preds is None else nested_concat(preds, logits, dim=0)
|
preds = logits if preds is None else nested_concat(preds, logits, dim=0)
|
||||||
if labels is not None:
|
if labels is not None:
|
||||||
label_ids = labels if label_ids is None else nested_concat(label_ids, labels, dim=0)
|
label_ids = labels if label_ids is None else nested_concat(label_ids, labels, dim=0)
|
||||||
|
self.control = self.callback_handler.on_prediction_step(self.args, self.state, self.control)
|
||||||
|
|
||||||
if self.args.past_index and hasattr(self, "_past"):
|
if self.args.past_index and hasattr(self, "_past"):
|
||||||
# Clean the state at the end of the evaluation loop
|
# Clean the state at the end of the evaluation loop
|
||||||
|
|||||||
@@ -0,0 +1,468 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
# Copyright 2020-present the HuggingFace Inc. team.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
"""
|
||||||
|
Callbacks to use with the Trainer class and customize the training loop.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
import json
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
|
from tqdm.auto import tqdm
|
||||||
|
|
||||||
|
from .trainer_utils import EvaluationStrategy
|
||||||
|
from .training_args import TrainingArguments
|
||||||
|
from .utils import logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TrainerState:
|
||||||
|
"""
|
||||||
|
A class containing the :class:`~transformers.Trainer` inner state that will be saved along the model and optimizer
|
||||||
|
when checkpointing and passed to the :class:`~transformers.TrainerCallback`.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
In all this class, one step is to be understood as one update step. When using gradient accumulation, one
|
||||||
|
update step may require several forward and backward passes: if you use :obj:`gradient_accumulation_steps=n`,
|
||||||
|
then one update step requires going throuch `n` batches.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
epoch (:obj:`float`, `optional`):
|
||||||
|
Only set during training, will represent the epoch the training is at (the decimal part being the
|
||||||
|
percentage of the current epoch completed).
|
||||||
|
global_step (:obj:`int`, `optional`, defaults to 0):
|
||||||
|
During training, represents the number of update steps completed.
|
||||||
|
max_steps (:obj:`int`, `optional`, defaults to 0):
|
||||||
|
The number of update steps to do during the current training.
|
||||||
|
total_flos (:obj:`int`, `optional`, defaults to 0):
|
||||||
|
The total number of floating operations done by the model since the beginning of training.
|
||||||
|
log_history (:obj:`List[Dict[str, float]]`, `optional`):
|
||||||
|
The list of logs done since the beginning of training.
|
||||||
|
best_metric (:obj:`float`, `optional`):
|
||||||
|
When tracking the best model, the value of the best metric encountered so far.
|
||||||
|
best_model_checkpoint (:obj:`str`, `optional`):
|
||||||
|
When tracking the best model, the value of the name of the checkpoint for the best model encountered so
|
||||||
|
far.
|
||||||
|
is_local_process_zero (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
|
Whether or not this process is the local (e.g., on one machine if training in a distributed fashion on
|
||||||
|
several machines) main process.
|
||||||
|
is_world_process_zero (:obj:`bool`, `optional`, defaults to :obj:`True`):
|
||||||
|
Whether or not this process is the global main process (when training in a distributed fashion on
|
||||||
|
several machines, this is only going to be :obj:`True` for one process).
|
||||||
|
"""
|
||||||
|
|
||||||
|
epoch: Optional[float] = None
|
||||||
|
global_step: int = 0
|
||||||
|
max_steps: int = 0
|
||||||
|
num_train_epochs: int = 0
|
||||||
|
total_flos: int = 0
|
||||||
|
log_history: List[Dict[str, float]] = None
|
||||||
|
best_metric: Optional[float] = None
|
||||||
|
best_model_checkpoint: Optional[str] = None
|
||||||
|
is_local_process_zero: bool = True
|
||||||
|
is_world_process_zero: bool = True
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if self.log_history is None:
|
||||||
|
self.log_history = []
|
||||||
|
|
||||||
|
def save_to_json(self, json_path: str):
|
||||||
|
""" Save the content of this instance in JSON format inside :obj:`json_path`."""
|
||||||
|
json_string = json.dumps(dataclasses.asdict(self), indent=2, sort_keys=True) + "\n"
|
||||||
|
with open(json_path, "w", encoding="utf-8") as f:
|
||||||
|
f.write(json_string)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load_from_json(cls, json_path: str):
|
||||||
|
""" Create an instance from the content of :obj:`json_path`."""
|
||||||
|
with open(json_path, "r", encoding="utf-8") as f:
|
||||||
|
text = f.read()
|
||||||
|
return cls(**json.loads(text))
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TrainerControl:
|
||||||
|
"""
|
||||||
|
A class that handles the :class:`~transformers.Trainer` control flow. This class is used by the
|
||||||
|
:class:`~transformers.TrainerCallback` to activate some switches in the training loop.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
should_training_stop (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Whether or not the training should be interrupted.
|
||||||
|
|
||||||
|
If :obj:`True`, this variable will not be set back to :obj:`False`. The training will just stop.
|
||||||
|
should_epoch_stop (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Whether or not the current epoch should be interrupted.
|
||||||
|
|
||||||
|
If :obj:`True`, this variable will be set back to :obj:`False` at the beginning of the next epoch.
|
||||||
|
should_save (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Whether or not the model should be saved at this step.
|
||||||
|
|
||||||
|
If :obj:`True`, this variable will be set back to :obj:`False` at the beginning of the next step.
|
||||||
|
should_evaluate (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Whether or not the model should be evaluated at this step.
|
||||||
|
|
||||||
|
If :obj:`True`, this variable will be set back to :obj:`False` at the beginning of the next step.
|
||||||
|
should_log (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Whether or not the logs should be reported at this step.
|
||||||
|
|
||||||
|
If :obj:`True`, this variable will be set back to :obj:`False` at the beginning of the next step.
|
||||||
|
"""
|
||||||
|
|
||||||
|
should_training_stop: bool = False
|
||||||
|
should_epoch_stop: bool = False
|
||||||
|
should_save: bool = False
|
||||||
|
should_evaluate: bool = False
|
||||||
|
should_log: bool = False
|
||||||
|
|
||||||
|
def _new_training(self):
|
||||||
|
""" Internal method that resets the variable for a new training. """
|
||||||
|
self.should_training_stop = False
|
||||||
|
|
||||||
|
def _new_epoch(self):
|
||||||
|
""" Internal method that resets the variable for a new epoch. """
|
||||||
|
self.should_epoch_stop = False
|
||||||
|
|
||||||
|
def _new_step(self):
|
||||||
|
""" Internal method that resets the variable for a new step. """
|
||||||
|
self.should_save_model = False
|
||||||
|
self.should_evaluate = False
|
||||||
|
self.should_log = False
|
||||||
|
|
||||||
|
|
||||||
|
class TrainerCallback:
|
||||||
|
"""
|
||||||
|
A class for objects that will inspect the state of the training loop at some events and take some decisions. At
|
||||||
|
each of those events the following arguments are available:
|
||||||
|
|
||||||
|
Args:
|
||||||
|
args (:class:`~transformers.TrainingArguments`):
|
||||||
|
The training arguments used to instantiate the :class:`~transformers.Trainer`.
|
||||||
|
state (:class:`~transformers.TrainerState`):
|
||||||
|
The current state of the :class:`~transformers.Trainer`.
|
||||||
|
control (:class:`~transformers.TrainerControl`):
|
||||||
|
The object that is returned to the :class:`~transformers.Trainer` and can be used to make some decisions.
|
||||||
|
model (:class:`~transformers.PreTrainedModel` or :obj:`torch.nn.Module`):
|
||||||
|
The model being trained.
|
||||||
|
optimizer (:obj:`torch.optim.Optimizer`):
|
||||||
|
The optimizer used for the training steps.
|
||||||
|
lr_scheduler (:obj:`torch.optim.lr_scheduler.LambdaLR`):
|
||||||
|
The scheduler used for setting the learning rate.
|
||||||
|
train_dataloader (:obj:`torch.utils.data.dataloader.DataLoader`, `optional`):
|
||||||
|
The current dataloader used for training.
|
||||||
|
eval_dataloader (:obj:`torch.utils.data.dataloader.DataLoader`, `optional`):
|
||||||
|
The current dataloader used for training.
|
||||||
|
metrics (:obj:`Dict[str, float]`):
|
||||||
|
The metrics computed by the last evaluation phase.
|
||||||
|
|
||||||
|
Those are only accessible in the event :obj:`on_evaluate`.
|
||||||
|
logs (:obj:`Dict[str, float]`):
|
||||||
|
The values to log.
|
||||||
|
|
||||||
|
Those are only accessible in the event :obj:`on_log`.
|
||||||
|
|
||||||
|
The :obj:`control` object is the only one that can be changed by the callback, in which case the event that changes
|
||||||
|
it should return the modified version.
|
||||||
|
|
||||||
|
The argument :obj:`args`, :obj:`state` and :obj:`control` are positionals for all events, all the others are
|
||||||
|
grouped in :obj:`kwargs`. You can unpack the ones you need in the signature of the event using them. As an example,
|
||||||
|
see the code of the simple :class:`~transformer.PrinterCallback`.
|
||||||
|
|
||||||
|
Example::
|
||||||
|
|
||||||
|
class PrinterCallback(TrainerCallback):
|
||||||
|
|
||||||
|
def on_log(self, args, state, control, logs=None, **kwargs):
|
||||||
|
_ = logs.pop("total_flos", None)
|
||||||
|
if state.is_local_process_zero:
|
||||||
|
print(logs)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def on_init_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called at the end of the initialization of the :class:`~transformers.Trainer`.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_train_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called at the beginning of training.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_train_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called at the end of training.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_epoch_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called at the beginning of an epoch.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called at the end of an epoch.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_step_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called at the beginning of a training step. If using gradient accumulation, one training step might take
|
||||||
|
several inputs.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called at the end of a training step. If using gradient accumulation, one training step might take
|
||||||
|
several inputs.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_evaluate(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called after an evaluation phase.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called after a checkpoint save.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_log(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called after logging the last logs.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_prediction_step(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
"""
|
||||||
|
Event called after a prediction step.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class CallbackHandler(TrainerCallback):
|
||||||
|
""" Internal class that just calls the list of callbacks in order. """
|
||||||
|
|
||||||
|
def __init__(self, callbacks, model, optimizer, lr_scheduler):
|
||||||
|
self.callbacks = []
|
||||||
|
for cb in callbacks:
|
||||||
|
self.add_callback(cb)
|
||||||
|
self.model = model
|
||||||
|
self.optimizer = optimizer
|
||||||
|
self.lr_scheduler = lr_scheduler
|
||||||
|
self.train_dataloader = None
|
||||||
|
self.eval_dataloader = None
|
||||||
|
|
||||||
|
if not any(isinstance(cb, DefaultFlowCallback) for cb in self.callbacks):
|
||||||
|
logger.warn(
|
||||||
|
"The Trainer will not work properly if you don't have a `DefaultFlowCallback` in its callbacks. You\n"
|
||||||
|
+ "should add one before training with `trainer.add_callback(DefaultFlowCallback). The current list of"
|
||||||
|
+ "callbacks is\n:"
|
||||||
|
+ self.callback_list
|
||||||
|
)
|
||||||
|
|
||||||
|
def add_callback(self, callback):
|
||||||
|
cb = callback() if isinstance(callback, type) else callback
|
||||||
|
cb_class = callback if isinstance(callback, type) else callback.__class__
|
||||||
|
if cb_class in [c.__class__ for c in self.callbacks]:
|
||||||
|
logger.warn(
|
||||||
|
f"You are adding a {cb_class} to the callbacks of this Trainer, but there is already one. The current"
|
||||||
|
+ "list of callbacks is\n:"
|
||||||
|
+ self.callback_list
|
||||||
|
)
|
||||||
|
self.callbacks.append(cb)
|
||||||
|
|
||||||
|
def pop_callback(self, callback):
|
||||||
|
if isinstance(callback, type):
|
||||||
|
for cb in self.callbacks:
|
||||||
|
if isinstance(cb, callback):
|
||||||
|
self.callbacks.remove(cb)
|
||||||
|
return cb
|
||||||
|
else:
|
||||||
|
for cb in self.callbacks:
|
||||||
|
if cb == callback:
|
||||||
|
self.callbacks.remove(cb)
|
||||||
|
return cb
|
||||||
|
|
||||||
|
def remove_callback(self, callback):
|
||||||
|
if isinstance(callback, type):
|
||||||
|
for cb in self.callbacks:
|
||||||
|
if isinstance(cb, callback):
|
||||||
|
self.callbacks.remove(cb)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
self.callbacks.remove(callback)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def callback_list(self):
|
||||||
|
return "\n".join(self.callbacks)
|
||||||
|
|
||||||
|
def on_init_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
return self.call_event("on_init_end", args, state, control)
|
||||||
|
|
||||||
|
def on_train_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
control.should_training_stop = False
|
||||||
|
return self.call_event("on_train_begin", args, state, control)
|
||||||
|
|
||||||
|
def on_train_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
return self.call_event("on_train_end", args, state, control)
|
||||||
|
|
||||||
|
def on_epoch_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
control.should_epoch_stop = False
|
||||||
|
return self.call_event("on_epoch_begin", args, state, control)
|
||||||
|
|
||||||
|
def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
return self.call_event("on_epoch_end", args, state, control)
|
||||||
|
|
||||||
|
def on_step_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
control.should_log = False
|
||||||
|
control.should_evaluate = False
|
||||||
|
control.should_save = False
|
||||||
|
return self.call_event("on_step_begin", args, state, control)
|
||||||
|
|
||||||
|
def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
return self.call_event("on_step_end", args, state, control)
|
||||||
|
|
||||||
|
def on_evaluate(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics):
|
||||||
|
control.should_evaluate = False
|
||||||
|
return self.call_event("on_evaluate", args, state, control, metrics=metrics)
|
||||||
|
|
||||||
|
def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
control.should_save = False
|
||||||
|
return self.call_event("on_save", args, state, control)
|
||||||
|
|
||||||
|
def on_log(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, logs):
|
||||||
|
control.should_log = False
|
||||||
|
return self.call_event("on_log", args, state, control, logs=logs)
|
||||||
|
|
||||||
|
def on_prediction_step(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
|
||||||
|
return self.call_event("on_prediction_step", args, state, control)
|
||||||
|
|
||||||
|
def call_event(self, event, args, state, control, **kwargs):
|
||||||
|
for callback in self.callbacks:
|
||||||
|
result = getattr(callback, event)(
|
||||||
|
args,
|
||||||
|
state,
|
||||||
|
control,
|
||||||
|
model=self.model,
|
||||||
|
optimizer=self.optimizer,
|
||||||
|
lr_scheduler=self.lr_scheduler,
|
||||||
|
train_dataloader=self.train_dataloader,
|
||||||
|
eval_dataloader=self.eval_dataloader,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
# A Callback can skip the return of `control` if it doesn't change it.
|
||||||
|
if result is not None:
|
||||||
|
control = result
|
||||||
|
return control
|
||||||
|
|
||||||
|
|
||||||
|
class DefaultFlowCallback(TrainerCallback):
|
||||||
|
"""
|
||||||
|
A :class:`~transformers.TrainerCallback` that handles the default flow of the training loop for logs, evaluation
|
||||||
|
and checkpoints.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
# Log
|
||||||
|
if state.global_step == 1 and args.logging_first_step:
|
||||||
|
control.should_log = True
|
||||||
|
if args.logging_steps > 0 and state.global_step % args.logging_steps == 0:
|
||||||
|
control.should_log = True
|
||||||
|
|
||||||
|
# Evaluate
|
||||||
|
if args.evaluation_strategy == EvaluationStrategy.STEPS and state.global_step % args.eval_steps == 0:
|
||||||
|
control.should_evaluate = True
|
||||||
|
if args.load_best_model_at_end:
|
||||||
|
control.should_save = True
|
||||||
|
|
||||||
|
# Save
|
||||||
|
if not args.load_best_model_at_end and args.save_steps > 0 and state.global_step % args.save_steps == 0:
|
||||||
|
control.should_save = True
|
||||||
|
|
||||||
|
# End training
|
||||||
|
if state.global_step >= state.max_steps:
|
||||||
|
control.should_training_stop = True
|
||||||
|
|
||||||
|
return control
|
||||||
|
|
||||||
|
def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
|
||||||
|
if args.evaluation_strategy == EvaluationStrategy.EPOCH:
|
||||||
|
control.should_evaluate = True
|
||||||
|
if args.load_best_model_at_end:
|
||||||
|
control.should_save = True
|
||||||
|
return control
|
||||||
|
|
||||||
|
|
||||||
|
class ProgressCallback(TrainerCallback):
|
||||||
|
"""
|
||||||
|
A :class:`~transformers.TrainerCallback` that displays the progress of training or evaluation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.training_bar = None
|
||||||
|
self.prediction_bar = None
|
||||||
|
|
||||||
|
def on_train_begin(self, args, state, control, **kwargs):
|
||||||
|
if state.is_local_process_zero:
|
||||||
|
self.training_bar = tqdm(total=state.max_steps)
|
||||||
|
|
||||||
|
def on_step_end(self, args, state, control, **kwargs):
|
||||||
|
if state.is_local_process_zero:
|
||||||
|
self.training_bar.update(1)
|
||||||
|
|
||||||
|
def on_prediction_step(self, args, state, control, eval_dataloader=None, **kwargs):
|
||||||
|
if state.is_local_process_zero:
|
||||||
|
if self.prediction_bar is None:
|
||||||
|
self.prediction_bar = tqdm(total=len(eval_dataloader), leave=self.training_bar is None)
|
||||||
|
self.prediction_bar.update(1)
|
||||||
|
|
||||||
|
def on_evaluate(self, args, state, control, **kwargs):
|
||||||
|
if state.is_local_process_zero:
|
||||||
|
self.prediction_bar.close()
|
||||||
|
self.prediction_bar = None
|
||||||
|
|
||||||
|
def on_log(self, args, state, control, logs=None, **kwargs):
|
||||||
|
if state.is_local_process_zero and self.training_bar is not None:
|
||||||
|
_ = logs.pop("total_flos", None)
|
||||||
|
self.training_bar.write(str(logs))
|
||||||
|
|
||||||
|
def on_train_end(self, args, state, control, **kwargs):
|
||||||
|
if state.is_local_process_zero:
|
||||||
|
self.training_bar.close()
|
||||||
|
self.training_bar = None
|
||||||
|
|
||||||
|
|
||||||
|
class PrinterCallback(TrainerCallback):
|
||||||
|
"""
|
||||||
|
A bare :class:`~transformers.TrainerCallback` that just prints the logs.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def on_log(self, args, state, control, logs=None, **kwargs):
|
||||||
|
_ = logs.pop("total_flos", None)
|
||||||
|
if state.is_local_process_zero:
|
||||||
|
print(logs)
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
# Copyright 2020-present the HuggingFace Inc. team.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
"""
|
||||||
|
Torch utilities for the Trainer class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import math
|
||||||
|
import warnings
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from typing import List, Optional, Union
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from torch.utils.data.distributed import DistributedSampler
|
||||||
|
from torch.utils.data.sampler import RandomSampler, Sampler
|
||||||
|
|
||||||
|
from .file_utils import is_torch_tpu_available
|
||||||
|
|
||||||
|
|
||||||
|
if is_torch_tpu_available():
|
||||||
|
import torch_xla.core.xla_model as xm
|
||||||
|
|
||||||
|
PT_LR_SCHEDULER_WARNING = "Please also save or load the state of the optimzer when saving or loading the scheduler."
|
||||||
|
|
||||||
|
|
||||||
|
def nested_concat(tensors, new_tensors, dim=0):
|
||||||
|
"Concat the `new_tensors` to `tensors` on `dim`. Works for tensors or nested list/tuples of tensors."
|
||||||
|
assert type(tensors) == type(
|
||||||
|
new_tensors
|
||||||
|
), f"Expected `tensors` and `new_tensors` to have the same type but found {type(tensors)} and {type(new_tensors)}."
|
||||||
|
if isinstance(tensors, (list, tuple)):
|
||||||
|
return type(tensors)(nested_concat(t, n, dim) for t, n in zip(tensors, new_tensors))
|
||||||
|
return torch.cat((tensors, new_tensors), dim=dim)
|
||||||
|
|
||||||
|
|
||||||
|
def nested_numpify(tensors):
|
||||||
|
"Numpify `tensors` (even if it's a nested list/tuple of tensors)."
|
||||||
|
if isinstance(tensors, (list, tuple)):
|
||||||
|
return type(tensors)(nested_numpify(t) for t in tensors)
|
||||||
|
return tensors.cpu().numpy()
|
||||||
|
|
||||||
|
|
||||||
|
def nested_detach(tensors):
|
||||||
|
"Detach `tensors` (even if it's a nested list/tuple of tensors)."
|
||||||
|
if isinstance(tensors, (list, tuple)):
|
||||||
|
return type(tensors)(nested_detach(t) for t in tensors)
|
||||||
|
return tensors.detach()
|
||||||
|
|
||||||
|
|
||||||
|
def nested_xla_mesh_reduce(tensors, name):
|
||||||
|
if is_torch_tpu_available():
|
||||||
|
import torch_xla.core.xla_model as xm
|
||||||
|
|
||||||
|
if isinstance(tensors, (list, tuple)):
|
||||||
|
return type(tensors)(nested_xla_mesh_reduce(t, f"{name}_{i}") for i, t in enumerate(tensors))
|
||||||
|
return xm.mesh_reduce(name, tensors, torch.cat)
|
||||||
|
else:
|
||||||
|
raise ImportError("Torch xla must be installed to use `nested_xla_mesh_reduce`")
|
||||||
|
|
||||||
|
|
||||||
|
def distributed_concat(tensor: "torch.Tensor", num_total_examples: Optional[int] = None) -> torch.Tensor:
|
||||||
|
try:
|
||||||
|
if isinstance(tensor, (tuple, list)):
|
||||||
|
return type(tensor)(distributed_concat(t, num_total_examples) for t in tensor)
|
||||||
|
output_tensors = [tensor.clone() for _ in range(torch.distributed.get_world_size())]
|
||||||
|
torch.distributed.all_gather(output_tensors, tensor)
|
||||||
|
concat = torch.cat(output_tensors, dim=0)
|
||||||
|
|
||||||
|
# truncate the dummy elements added by SequentialDistributedSampler
|
||||||
|
if num_total_examples is not None:
|
||||||
|
concat = concat[:num_total_examples]
|
||||||
|
return concat
|
||||||
|
except AssertionError:
|
||||||
|
raise AssertionError("Not currently using distributed training")
|
||||||
|
|
||||||
|
|
||||||
|
def distributed_broadcast_scalars(
|
||||||
|
scalars: List[Union[int, float]], num_total_examples: Optional[int] = None
|
||||||
|
) -> torch.Tensor:
|
||||||
|
try:
|
||||||
|
tensorized_scalar = torch.tensor(scalars).cuda()
|
||||||
|
output_tensors = [tensorized_scalar.clone() for _ in range(torch.distributed.get_world_size())]
|
||||||
|
torch.distributed.all_gather(output_tensors, tensorized_scalar)
|
||||||
|
concat = torch.cat(output_tensors, dim=0)
|
||||||
|
|
||||||
|
# truncate the dummy elements added by SequentialDistributedSampler
|
||||||
|
if num_total_examples is not None:
|
||||||
|
concat = concat[:num_total_examples]
|
||||||
|
return concat
|
||||||
|
except AssertionError:
|
||||||
|
raise AssertionError("Not currently using distributed training")
|
||||||
|
|
||||||
|
|
||||||
|
def reissue_pt_warnings(caught_warnings):
|
||||||
|
# Reissue warnings that are not the PT_LR_SCHEDULER_WARNING
|
||||||
|
if len(caught_warnings) > 1:
|
||||||
|
for w in caught_warnings:
|
||||||
|
if w.category != UserWarning or w.message != PT_LR_SCHEDULER_WARNING:
|
||||||
|
warnings.warn(w.message, w.category)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def torch_distributed_zero_first(local_rank: int):
|
||||||
|
"""
|
||||||
|
Decorator to make all processes in distributed training wait for each local_master to do something.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
local_rank (:obj:`int`): The rank of the local process.
|
||||||
|
"""
|
||||||
|
if local_rank not in [-1, 0]:
|
||||||
|
torch.distributed.barrier()
|
||||||
|
yield
|
||||||
|
if local_rank == 0:
|
||||||
|
torch.distributed.barrier()
|
||||||
|
|
||||||
|
|
||||||
|
class SequentialDistributedSampler(Sampler):
|
||||||
|
"""
|
||||||
|
Distributed Sampler that subsamples indicies sequentially,
|
||||||
|
making it easier to collate all results at the end.
|
||||||
|
|
||||||
|
Even though we only use this sampler for eval and predict (no training),
|
||||||
|
which means that the model params won't have to be synced (i.e. will not hang
|
||||||
|
for synchronization even if varied number of forward passes), we still add extra
|
||||||
|
samples to the sampler to make it evenly divisible (like in `DistributedSampler`)
|
||||||
|
to make it easy to `gather` or `reduce` resulting tensors at the end of the loop.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, dataset, num_replicas=None, rank=None):
|
||||||
|
if num_replicas is None:
|
||||||
|
if not torch.distributed.is_available():
|
||||||
|
raise RuntimeError("Requires distributed package to be available")
|
||||||
|
num_replicas = torch.distributed.get_world_size()
|
||||||
|
if rank is None:
|
||||||
|
if not torch.distributed.is_available():
|
||||||
|
raise RuntimeError("Requires distributed package to be available")
|
||||||
|
rank = torch.distributed.get_rank()
|
||||||
|
self.dataset = dataset
|
||||||
|
self.num_replicas = num_replicas
|
||||||
|
self.rank = rank
|
||||||
|
self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.num_replicas))
|
||||||
|
self.total_size = self.num_samples * self.num_replicas
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
indices = list(range(len(self.dataset)))
|
||||||
|
|
||||||
|
# add extra samples to make it evenly divisible
|
||||||
|
indices += indices[: (self.total_size - len(indices))]
|
||||||
|
assert (
|
||||||
|
len(indices) == self.total_size
|
||||||
|
), f"Indices length {len(indices)} and total size {self.total_size} mismatched"
|
||||||
|
|
||||||
|
# subsample
|
||||||
|
indices = indices[self.rank * self.num_samples : (self.rank + 1) * self.num_samples]
|
||||||
|
assert (
|
||||||
|
len(indices) == self.num_samples
|
||||||
|
), f"Indices length {len(indices)} and sample number {self.num_samples} mismatched"
|
||||||
|
|
||||||
|
return iter(indices)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return self.num_samples
|
||||||
|
|
||||||
|
|
||||||
|
def get_tpu_sampler(dataset: torch.utils.data.dataset.Dataset):
|
||||||
|
if xm.xrt_world_size() <= 1:
|
||||||
|
return RandomSampler(dataset)
|
||||||
|
return DistributedSampler(dataset, num_replicas=xm.xrt_world_size(), rank=xm.get_ordinal())
|
||||||
@@ -1,19 +1,30 @@
|
|||||||
import dataclasses
|
# coding=utf-8
|
||||||
import json
|
# Copyright 2020-present the HuggingFace Inc. team.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
"""
|
||||||
|
Utilities for the Trainer and TFTrainer class. Should be independent from PyTorch and TensorFlow.
|
||||||
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
from dataclasses import dataclass
|
from typing import Any, Dict, NamedTuple, Optional, Tuple, Union
|
||||||
from typing import Any, Dict, List, NamedTuple, Optional, Tuple, Union
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from .file_utils import is_tf_available, is_torch_available, is_torch_tpu_available
|
from .file_utils import is_tf_available, is_torch_available
|
||||||
from .tokenization_utils_base import ExplicitEnum
|
from .tokenization_utils_base import ExplicitEnum
|
||||||
|
|
||||||
|
|
||||||
if is_torch_available():
|
|
||||||
import torch
|
|
||||||
|
|
||||||
|
|
||||||
def set_seed(seed: int):
|
def set_seed(seed: int):
|
||||||
"""
|
"""
|
||||||
Helper function for reproducible behavior to set the seed in ``random``, ``numpy``, ``torch`` and/or ``tf``
|
Helper function for reproducible behavior to set the seed in ``random``, ``numpy``, ``torch`` and/or ``tf``
|
||||||
@@ -139,144 +150,3 @@ default_hp_space = {
|
|||||||
HPSearchBackend.OPTUNA: default_hp_space_optuna,
|
HPSearchBackend.OPTUNA: default_hp_space_optuna,
|
||||||
HPSearchBackend.RAY: default_hp_space_ray,
|
HPSearchBackend.RAY: default_hp_space_ray,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def nested_concat(tensors, new_tensors, dim=0):
|
|
||||||
"Concat the `new_tensors` to `tensors` on `dim`. Works for tensors or nested list/tuples of tensors."
|
|
||||||
if is_torch_available():
|
|
||||||
assert type(tensors) == type(
|
|
||||||
new_tensors
|
|
||||||
), f"Expected `tensors` and `new_tensors` to have the same type but found {type(tensors)} and {type(new_tensors)}."
|
|
||||||
if isinstance(tensors, (list, tuple)):
|
|
||||||
return type(tensors)(nested_concat(t, n, dim) for t, n in zip(tensors, new_tensors))
|
|
||||||
return torch.cat((tensors, new_tensors), dim=dim)
|
|
||||||
else:
|
|
||||||
raise ImportError("Torch must be installed to use `nested_concat`")
|
|
||||||
|
|
||||||
|
|
||||||
def nested_deatch(tensors):
|
|
||||||
"Detach `tensors` (even if it's a nested list/tuple of tensors)."
|
|
||||||
if isinstance(tensors, (list, tuple)):
|
|
||||||
return type(tensors)(nested_detach(t) for t in tensors)
|
|
||||||
return tensors.detach()
|
|
||||||
|
|
||||||
|
|
||||||
def nested_numpify(tensors):
|
|
||||||
"Numpify `tensors` (even if it's a nested list/tuple of tensors)."
|
|
||||||
if isinstance(tensors, (list, tuple)):
|
|
||||||
return type(tensors)(nested_numpify(t) for t in tensors)
|
|
||||||
return tensors.cpu().numpy()
|
|
||||||
|
|
||||||
|
|
||||||
def nested_detach(tensors):
|
|
||||||
"Detach `tensors` (even if it's a nested list/tuple of tensors)."
|
|
||||||
if isinstance(tensors, (list, tuple)):
|
|
||||||
return type(tensors)(nested_detach(t) for t in tensors)
|
|
||||||
return tensors.detach()
|
|
||||||
|
|
||||||
|
|
||||||
def nested_xla_mesh_reduce(tensors, name):
|
|
||||||
if is_torch_tpu_available():
|
|
||||||
import torch_xla.core.xla_model as xm
|
|
||||||
|
|
||||||
if isinstance(tensors, (list, tuple)):
|
|
||||||
return type(tensors)(nested_xla_mesh_reduce(t, f"{name}_{i}") for i, t in enumerate(tensors))
|
|
||||||
return xm.mesh_reduce(name, tensors, torch.cat)
|
|
||||||
else:
|
|
||||||
raise ImportError("Torch xla must be installed to use `nested_xla_mesh_reduce`")
|
|
||||||
|
|
||||||
|
|
||||||
def distributed_concat(tensor: "torch.Tensor", num_total_examples: Optional[int] = None) -> "torch.Tensor":
|
|
||||||
if is_torch_available():
|
|
||||||
try:
|
|
||||||
if isinstance(tensor, (tuple, list)):
|
|
||||||
return type(tensor)(distributed_concat(t, num_total_examples) for t in tensor)
|
|
||||||
output_tensors = [tensor.clone() for _ in range(torch.distributed.get_world_size())]
|
|
||||||
torch.distributed.all_gather(output_tensors, tensor)
|
|
||||||
concat = torch.cat(output_tensors, dim=0)
|
|
||||||
|
|
||||||
# truncate the dummy elements added by SequentialDistributedSampler
|
|
||||||
if num_total_examples is not None:
|
|
||||||
concat = concat[:num_total_examples]
|
|
||||||
return concat
|
|
||||||
except AssertionError:
|
|
||||||
raise AssertionError("Not currently using distributed training")
|
|
||||||
else:
|
|
||||||
raise ImportError("Torch must be installed to use `distributed_concat`")
|
|
||||||
|
|
||||||
|
|
||||||
def distributed_broadcast_scalars(
|
|
||||||
scalars: List[Union[int, float]], num_total_examples: Optional[int] = None
|
|
||||||
) -> "torch.Tensor":
|
|
||||||
if is_torch_available():
|
|
||||||
try:
|
|
||||||
tensorized_scalar = torch.tensor(scalars).cuda()
|
|
||||||
output_tensors = [tensorized_scalar.clone() for _ in range(torch.distributed.get_world_size())]
|
|
||||||
torch.distributed.all_gather(output_tensors, tensorized_scalar)
|
|
||||||
concat = torch.cat(output_tensors, dim=0)
|
|
||||||
|
|
||||||
# truncate the dummy elements added by SequentialDistributedSampler
|
|
||||||
if num_total_examples is not None:
|
|
||||||
concat = concat[:num_total_examples]
|
|
||||||
return concat
|
|
||||||
except AssertionError:
|
|
||||||
raise AssertionError("Not currently using distributed training")
|
|
||||||
else:
|
|
||||||
raise ImportError("Torch must be installed to use `distributed_broadcast_scalars`")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class TrainerState:
|
|
||||||
"""
|
|
||||||
A class containing the `Trainer` inner state that will be saved along the model and optimizer.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
In all this class, one step is to be understood as one update step. When using gradient accumulation, one
|
|
||||||
update step may require several forward and backward passes: if you use :obj:`gradient_accumulation_steps=n`,
|
|
||||||
then one update step requires going throuch `n` batches.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
epoch (:obj:`float`, `optional`):
|
|
||||||
Only set during training, will represent the epoch the training is at (the decimal part being the
|
|
||||||
percentage of the current epoch completed).
|
|
||||||
global_step (:obj:`int`, `optional`, defaults to 0):
|
|
||||||
During training, represents the number of update steps completed.
|
|
||||||
max_steps (:obj:`int`, `optional`, defaults to 0):
|
|
||||||
The number of update steps to do during the current training.
|
|
||||||
total_flos (:obj:`int`, `optional`, defaults to 0):
|
|
||||||
The total number of floating operations done by the model since the beginning of training.
|
|
||||||
log_history (:obj:`List[Dict[str, float]]`, `optional`):
|
|
||||||
The list of logs done since the beginning of training.
|
|
||||||
best_metric (:obj:`float`, `optional`):
|
|
||||||
When tracking the best model, the value of the best metric encountered so far.
|
|
||||||
best_model_checkpoint (:obj:`str`, `optional`):
|
|
||||||
When tracking the best model, the value of the name of the checkpoint for the best model encountered so
|
|
||||||
far.
|
|
||||||
"""
|
|
||||||
|
|
||||||
epoch: Optional[float] = None
|
|
||||||
global_step: int = 0
|
|
||||||
max_steps: int = 0
|
|
||||||
num_train_epochs: int = 0
|
|
||||||
total_flos: int = 0
|
|
||||||
log_history: List[Dict[str, float]] = None
|
|
||||||
best_metric: Optional[float] = None
|
|
||||||
best_model_checkpoint: Optional[str] = None
|
|
||||||
|
|
||||||
def __post_init__(self):
|
|
||||||
if self.log_history is None:
|
|
||||||
self.log_history = []
|
|
||||||
|
|
||||||
def save_to_json(self, json_path: str):
|
|
||||||
""" Save the content of this instance in JSON format inside :obj:`json_path`."""
|
|
||||||
json_string = json.dumps(dataclasses.asdict(self), indent=2, sort_keys=True) + "\n"
|
|
||||||
with open(json_path, "w", encoding="utf-8") as f:
|
|
||||||
f.write(json_string)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def load_from_json(cls, json_path: str):
|
|
||||||
""" Create an instance from the content of :obj:`json_path`."""
|
|
||||||
with open(json_path, "r", encoding="utf-8") as f:
|
|
||||||
text = f.read()
|
|
||||||
return cls(**json.loads(text))
|
|
||||||
@@ -449,6 +449,18 @@ def load_tf_weights_in_bert_generation(*args, **kwargs):
|
|||||||
requires_pytorch(load_tf_weights_in_bert_generation)
|
requires_pytorch(load_tf_weights_in_bert_generation)
|
||||||
|
|
||||||
|
|
||||||
|
BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST = None
|
||||||
|
|
||||||
|
|
||||||
|
class BlenderbotForConditionalGeneration:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
requires_pytorch(self)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pretrained(self, *args, **kwargs):
|
||||||
|
requires_pytorch(self)
|
||||||
|
|
||||||
|
|
||||||
CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST = None
|
CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST = None
|
||||||
|
|
||||||
|
|
||||||
@@ -931,6 +943,15 @@ class GPT2DoubleHeadsModel:
|
|||||||
requires_pytorch(self)
|
requires_pytorch(self)
|
||||||
|
|
||||||
|
|
||||||
|
class GPT2ForSequenceClassification:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
requires_pytorch(self)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pretrained(self, *args, **kwargs):
|
||||||
|
requires_pytorch(self)
|
||||||
|
|
||||||
|
|
||||||
class GPT2LMHeadModel:
|
class GPT2LMHeadModel:
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
requires_pytorch(self)
|
requires_pytorch(self)
|
||||||
@@ -1860,19 +1881,10 @@ class MarianTokenizer:
|
|||||||
requires_pytorch(self)
|
requires_pytorch(self)
|
||||||
|
|
||||||
|
|
||||||
class EvalPrediction:
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
requires_pytorch(self)
|
|
||||||
|
|
||||||
|
|
||||||
class Trainer:
|
class Trainer:
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
requires_pytorch(self)
|
requires_pytorch(self)
|
||||||
|
|
||||||
|
|
||||||
def set_seed(*args, **kwargs):
|
|
||||||
requires_pytorch(set_seed)
|
|
||||||
|
|
||||||
|
|
||||||
def torch_distributed_zero_first(*args, **kwargs):
|
def torch_distributed_zero_first(*args, **kwargs):
|
||||||
requires_pytorch(torch_distributed_zero_first)
|
requires_pytorch(torch_distributed_zero_first)
|
||||||
BIN
Binary file not shown.
Loaded 100 of 114 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user