Add LUKE (#11223)
* Rebase with master * Minor bug fix in docs * Copy files from adding_luke_v2 and improve docs * change the default value of use_entity_aware_attention to True * remove word_hidden_states * fix head models * fix tests * fix the conversion script * add integration tests for the pretrained large model * improve docstring * Improve docs, make style * fix _init_weights for pytorch 1.8 * improve docs * fix tokenizer to construct entity sequence with [MASK] entity when entities=None * Make fix-copies * Make style & quality * Bug fixes * Add LukeTokenizer to init * Address most comments by @patil-suraj and @LysandreJik * rename _compute_extended_attention_mask to get_extended_attention_mask * add comments to LukeSelfAttention * fix the documentation of the tokenizer * address comments by @patil-suraj, @LysandreJik, and @sgugger * improve docs * Make style, quality and fix-copies * Improve docs * fix docs * add "entity_span_classification" task * update example code for LukeForEntitySpanClassification * improve docs * improve docs * improve the code example in luke.rst * rename the classification layer in LukeForEntityClassification from typing to classifier * add bias to the classifier in LukeForEntitySpanClassification * update docs to use fine-tuned hub models in code examples of the head models * update the example sentences * Make style & quality * Add require_torch to tokenizer tests * Add require_torch to tokenizer tests * Address comments by @sgugger and add community notebooks * Make fix-copies Co-authored-by: Ikuya Yamada <ikuya@ikuya.net>
This commit is contained in:
@@ -220,6 +220,7 @@ Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih.
|
||||
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. **[LED](https://huggingface.co/transformers/model_doc/led.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. **[LUKE](https://huggingface.co/transformers/model_doc/luke.html)** (from Studio Ousia) released with the paper [LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention](https://arxiv.org/abs/2010.01057) by Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto.
|
||||
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. **[M2M100](https://huggingface.co/transformers/model_doc/m2m_100.html)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) by by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
|
||||
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.
|
||||
|
||||
@@ -52,3 +52,6 @@ This page regroups resources around 🤗 Transformers developed by the community
|
||||
|[Fine-tune BART for summarization in two languages with Trainer class](https://github.com/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb) | How to fine-tune BART for summarization in two languages with Trainer class | [Eliza Szczechla](https://github.com/elsanns) | [](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb)|
|
||||
|[Evaluate Big Bird on Trivia QA](https://github.com/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb) | How to evaluate BigBird on long document question answering on Trivia QA | [Patrick von Platen](https://github.com/patrickvonplaten) | [](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb)|
|
||||
| [Create video captions using Wav2Vec2](https://github.com/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | How to create YouTube captions from any video by transcribing the audio with Wav2Vec | [Niklas Muennighoff](https://github.com/Muennighoff) |[](https://colab.research.google.com/github/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) |
|
||||
| [Evaluate LUKE on Open Entity, an entity typing dataset](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | How to evaluate *LukeForEntityClassification* on the Open Entity dataset | [Ikuya Yamada](https://github.com/ikuyamada) |[](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) |
|
||||
| [Evaluate LUKE on TACRED, a relation extraction dataset](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | How to evaluate *LukeForEntityPairClassification* on the TACRED dataset | [Ikuya Yamada](https://github.com/ikuyamada) |[](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) |
|
||||
| [Evaluate LUKE on CoNLL-2003, an important NER benchmark](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | How to evaluate *LukeForEntitySpanClassification* on the CoNLL-2003 dataset | [Ikuya Yamada](https://github.com/ikuyamada) |[](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) |
|
||||
|
||||
@@ -170,80 +170,83 @@ conversion utilities for the following models:
|
||||
<https://arxiv.org/abs/2004.05150>`__ by Iz Beltagy, Matthew E. Peters, Arman Cohan.
|
||||
29. :doc:`Longformer <model_doc/longformer>` (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.
|
||||
30. :doc:`LXMERT <model_doc/lxmert>` (from UNC Chapel Hill) released with the paper `LXMERT: Learning Cross-Modality
|
||||
30. :doc:`LUKE <model_doc/luke>` (from Studio Ousia) released with the paper `LUKE: Deep Contextualized Entity
|
||||
Representations with Entity-aware Self-attention <https://arxiv.org/abs/2010.01057>`__ by Ikuya Yamada, Akari Asai,
|
||||
Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto.
|
||||
31. :doc:`LXMERT <model_doc/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.
|
||||
31. :doc:`M2M100 <model_doc/m2m_100>` (from Facebook) released with the paper `Beyond English-Centric Multilingual
|
||||
32. :doc:`M2M100 <model_doc/m2m_100>` (from Facebook) released with the paper `Beyond English-Centric Multilingual
|
||||
Machine Translation <https://arxiv.org/abs/2010.11125>`__ by by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi
|
||||
Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman
|
||||
Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
|
||||
32. :doc:`MarianMT <model_doc/marian>` Machine translation models trained using `OPUS <http://opus.nlpl.eu/>`__ data by
|
||||
33. :doc:`MarianMT <model_doc/marian>` 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.
|
||||
33. :doc:`MBart <model_doc/mbart>` (from Facebook) released with the paper `Multilingual Denoising Pre-training for
|
||||
34. :doc:`MBart <model_doc/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.
|
||||
34. :doc:`MBart-50 <model_doc/mbart>` (from Facebook) released with the paper `Multilingual Translation with Extensible
|
||||
35. :doc:`MBart-50 <model_doc/mbart>` (from Facebook) released with the paper `Multilingual Translation with Extensible
|
||||
Multilingual Pretraining and Finetuning <https://arxiv.org/abs/2008.00401>`__ by Yuqing Tang, Chau Tran, Xian Li,
|
||||
Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan.
|
||||
35. :doc:`Megatron-BERT <model_doc/megatron_bert>` (from NVIDIA) released with the paper `Megatron-LM: Training
|
||||
36. :doc:`Megatron-BERT <model_doc/megatron_bert>` (from NVIDIA) released with the paper `Megatron-LM: Training
|
||||
Multi-Billion Parameter Language Models Using Model Parallelism <https://arxiv.org/abs/1909.08053>`__ by Mohammad
|
||||
Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro.
|
||||
36. :doc:`Megatron-GPT2 <model_doc/megatron_gpt2>` (from NVIDIA) released with the paper `Megatron-LM: Training
|
||||
37. :doc:`Megatron-GPT2 <model_doc/megatron_gpt2>` (from NVIDIA) released with the paper `Megatron-LM: Training
|
||||
Multi-Billion Parameter Language Models Using Model Parallelism <https://arxiv.org/abs/1909.08053>`__ by Mohammad
|
||||
Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro.
|
||||
37. :doc:`MPNet <model_doc/mpnet>` (from Microsoft Research) released with the paper `MPNet: Masked and Permuted
|
||||
38. :doc:`MPNet <model_doc/mpnet>` (from Microsoft Research) released with the paper `MPNet: Masked and Permuted
|
||||
Pre-training for Language Understanding <https://arxiv.org/abs/2004.09297>`__ by Kaitao Song, Xu Tan, Tao Qin,
|
||||
Jianfeng Lu, Tie-Yan Liu.
|
||||
38. :doc:`MT5 <model_doc/mt5>` (from Google AI) released with the paper `mT5: A massively multilingual pre-trained
|
||||
39. :doc:`MT5 <model_doc/mt5>` (from Google AI) released with the paper `mT5: A massively multilingual pre-trained
|
||||
text-to-text transformer <https://arxiv.org/abs/2010.11934>`__ by Linting Xue, Noah Constant, Adam Roberts, Mihir
|
||||
Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel.
|
||||
39. :doc:`Pegasus <model_doc/pegasus>` (from Google) released with the paper `PEGASUS: Pre-training with Extracted
|
||||
40. :doc:`Pegasus <model_doc/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.
|
||||
40. :doc:`ProphetNet <model_doc/prophetnet>` (from Microsoft Research) released with the paper `ProphetNet: Predicting
|
||||
41. :doc:`ProphetNet <model_doc/prophetnet>` (from Microsoft Research) released with the paper `ProphetNet: Predicting
|
||||
Future N-gram for Sequence-to-Sequence Pre-training <https://arxiv.org/abs/2001.04063>`__ by Yu Yan, Weizhen Qi,
|
||||
Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou.
|
||||
41. :doc:`Reformer <model_doc/reformer>` (from Google Research) released with the paper `Reformer: The Efficient
|
||||
42. :doc:`Reformer <model_doc/reformer>` (from Google Research) released with the paper `Reformer: The Efficient
|
||||
Transformer <https://arxiv.org/abs/2001.04451>`__ by Nikita Kitaev, Łukasz Kaiser, Anselm Levskaya.
|
||||
42. :doc:`RoBERTa <model_doc/roberta>` (from Facebook), released together with the paper a `Robustly Optimized BERT
|
||||
43. :doc:`RoBERTa <model_doc/roberta>` (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.
|
||||
43. :doc:`SpeechToTextTransformer <model_doc/speech_to_text>` (from Facebook), released together with the paper
|
||||
44. :doc:`SpeechToTextTransformer <model_doc/speech_to_text>` (from Facebook), released together with the paper
|
||||
`fairseq S2T: Fast Speech-to-Text Modeling with fairseq <https://arxiv.org/abs/2010.05171>`__ by Changhan Wang, Yun
|
||||
Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino.
|
||||
44. :doc:`SqueezeBert <model_doc/squeezebert>` released with the paper `SqueezeBERT: What can computer vision teach NLP
|
||||
45. :doc:`SqueezeBert <model_doc/squeezebert>` 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.
|
||||
45. :doc:`T5 <model_doc/t5>` (from Google AI) released with the paper `Exploring the Limits of Transfer Learning with a
|
||||
46. :doc:`T5 <model_doc/t5>` (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.
|
||||
46. :doc:`TAPAS <model_doc/tapas>` (from Google AI) released with the paper `TAPAS: Weakly Supervised Table Parsing via
|
||||
47. :doc:`TAPAS <model_doc/tapas>` (from Google AI) released with the paper `TAPAS: Weakly Supervised Table Parsing via
|
||||
Pre-training <https://arxiv.org/abs/2004.02349>`__ by Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller,
|
||||
Francesco Piccinno and Julian Martin Eisenschlos.
|
||||
47. :doc:`Transformer-XL <model_doc/transformerxl>` (from Google/CMU) released with the paper `Transformer-XL:
|
||||
48. :doc:`Transformer-XL <model_doc/transformerxl>` (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.
|
||||
48. :doc:`Vision Transformer (ViT) <model_doc/vit>` (from Google AI) released with the paper `An Image is Worth 16x16
|
||||
49. :doc:`Vision Transformer (ViT) <model_doc/vit>` (from Google AI) released with the paper `An Image is Worth 16x16
|
||||
Words: Transformers for Image Recognition at Scale <https://arxiv.org/abs/2010.11929>`__ by Alexey Dosovitskiy,
|
||||
Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias
|
||||
Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby.
|
||||
49. :doc:`Wav2Vec2 <model_doc/wav2vec2>` (from Facebook AI) released with the paper `wav2vec 2.0: A Framework for
|
||||
50. :doc:`Wav2Vec2 <model_doc/wav2vec2>` (from Facebook AI) released with the paper `wav2vec 2.0: A Framework for
|
||||
Self-Supervised Learning of Speech Representations <https://arxiv.org/abs/2006.11477>`__ by Alexei Baevski, Henry
|
||||
Zhou, Abdelrahman Mohamed, Michael Auli.
|
||||
50. :doc:`XLM <model_doc/xlm>` (from Facebook) released together with the paper `Cross-lingual Language Model
|
||||
51. :doc:`XLM <model_doc/xlm>` (from Facebook) released together with the paper `Cross-lingual Language Model
|
||||
Pretraining <https://arxiv.org/abs/1901.07291>`__ by Guillaume Lample and Alexis Conneau.
|
||||
51. :doc:`XLM-ProphetNet <model_doc/xlmprophetnet>` (from Microsoft Research) released with the paper `ProphetNet:
|
||||
52. :doc:`XLM-ProphetNet <model_doc/xlmprophetnet>` (from Microsoft Research) released with the paper `ProphetNet:
|
||||
Predicting Future N-gram for Sequence-to-Sequence Pre-training <https://arxiv.org/abs/2001.04063>`__ by Yu Yan,
|
||||
Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou.
|
||||
52. :doc:`XLM-RoBERTa <model_doc/xlmroberta>` (from Facebook AI), released together with the paper `Unsupervised
|
||||
53. :doc:`XLM-RoBERTa <model_doc/xlmroberta>` (from Facebook AI), released together with the paper `Unsupervised
|
||||
Cross-lingual Representation Learning at Scale <https://arxiv.org/abs/1911.02116>`__ by Alexis Conneau*, Kartikay
|
||||
Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke
|
||||
Zettlemoyer and Veselin Stoyanov.
|
||||
53. :doc:`XLNet <model_doc/xlnet>` (from Google/CMU) released with the paper `XLNet: Generalized Autoregressive
|
||||
54. :doc:`XLNet <model_doc/xlnet>` (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.
|
||||
54. :doc:`XLSR-Wav2Vec2 <model_doc/xlsr_wav2vec2>` (from Facebook AI) released with the paper `Unsupervised
|
||||
55. :doc:`XLSR-Wav2Vec2 <model_doc/xlsr_wav2vec2>` (from Facebook AI) released with the paper `Unsupervised
|
||||
Cross-Lingual Representation Learning For Speech Recognition <https://arxiv.org/abs/2006.13979>`__ by Alexis
|
||||
Conneau, Alexei Baevski, Ronan Collobert, Abdelrahman Mohamed, Michael Auli.
|
||||
|
||||
@@ -308,6 +311,8 @@ Flax), PyTorch, and/or TensorFlow.
|
||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||
| LED | ✅ | ✅ | ✅ | ✅ | ❌ |
|
||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||
| LUKE | ✅ | ❌ | ✅ | ❌ | ❌ |
|
||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||
| LXMERT | ✅ | ✅ | ✅ | ✅ | ❌ |
|
||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||
| LayoutLM | ✅ | ✅ | ✅ | ✅ | ❌ |
|
||||
@@ -469,6 +474,7 @@ Flax), PyTorch, and/or TensorFlow.
|
||||
model_doc/layoutlm
|
||||
model_doc/led
|
||||
model_doc/longformer
|
||||
model_doc/luke
|
||||
model_doc/lxmert
|
||||
model_doc/marian
|
||||
model_doc/m2m_100
|
||||
|
||||
159
docs/source/model_doc/luke.rst
Normal file
159
docs/source/model_doc/luke.rst
Normal file
@@ -0,0 +1,159 @@
|
||||
..
|
||||
Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
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.
|
||||
|
||||
LUKE
|
||||
-----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
Overview
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The LUKE model was proposed in `LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention
|
||||
<https://arxiv.org/abs/2010.01057>`_ by Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda and Yuji Matsumoto.
|
||||
It is based on RoBERTa and adds entity embeddings as well as an entity-aware self-attention mechanism, which helps
|
||||
improve performance on various downstream tasks involving reasoning about entities such as named entity recognition,
|
||||
extractive and cloze-style question answering, entity typing, and relation classification.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Entity representations are useful in natural language tasks involving entities. In this paper, we propose new
|
||||
pretrained contextualized representations of words and entities based on the bidirectional transformer. The proposed
|
||||
model treats words and entities in a given text as independent tokens, and outputs contextualized representations of
|
||||
them. Our model is trained using a new pretraining task based on the masked language model of BERT. The task involves
|
||||
predicting randomly masked words and entities in a large entity-annotated corpus retrieved from Wikipedia. We also
|
||||
propose an entity-aware self-attention mechanism that is an extension of the self-attention mechanism of the
|
||||
transformer, and considers the types of tokens (words or entities) when computing attention scores. The proposed model
|
||||
achieves impressive empirical performance on a wide range of entity-related tasks. In particular, it obtains
|
||||
state-of-the-art results on five well-known datasets: Open Entity (entity typing), TACRED (relation classification),
|
||||
CoNLL-2003 (named entity recognition), ReCoRD (cloze-style question answering), and SQuAD 1.1 (extractive question
|
||||
answering).*
|
||||
|
||||
Tips:
|
||||
|
||||
- This implementation is the same as :class:`~transformers.RobertaModel` with the addition of entity embeddings as well
|
||||
as an entity-aware self-attention mechanism, which improves performance on tasks involving reasoning about entities.
|
||||
- LUKE treats entities as input tokens; therefore, it takes :obj:`entity_ids`, :obj:`entity_attention_mask`,
|
||||
:obj:`entity_token_type_ids` and :obj:`entity_position_ids` as extra input. You can obtain those using
|
||||
:class:`~transformers.LukeTokenizer`.
|
||||
- :class:`~transformers.LukeTokenizer` takes :obj:`entities` and :obj:`entity_spans` (character-based start and end
|
||||
positions of the entities in the input text) as extra input. :obj:`entities` typically consist of [MASK] entities or
|
||||
Wikipedia entities. The brief description when inputting these entities are as follows:
|
||||
|
||||
- *Inputting [MASK] entities to compute entity representations*: The [MASK] entity is used to mask entities to be
|
||||
predicted during pretraining. When LUKE receives the [MASK] entity, it tries to predict the original entity by
|
||||
gathering the information about the entity from the input text. Therefore, the [MASK] entity can be used to address
|
||||
downstream tasks requiring the information of entities in text such as entity typing, relation classification, and
|
||||
named entity recognition.
|
||||
- *Inputting Wikipedia entities to compute knowledge-enhanced token representations*: LUKE learns rich information
|
||||
(or knowledge) about Wikipedia entities during pretraining and stores the information in its entity embedding. By
|
||||
using Wikipedia entities as input tokens, LUKE outputs token representations enriched by the information stored in
|
||||
the embeddings of these entities. This is particularly effective for tasks requiring real-world knowledge, such as
|
||||
question answering.
|
||||
|
||||
- There are three head models for the former use case:
|
||||
|
||||
- :class:`~transformers.LukeForEntityClassification`, for tasks to classify a single entity in an input text such as
|
||||
entity typing, e.g. the `Open Entity dataset <https://www.cs.utexas.edu/~eunsol/html_pages/open_entity.html>`__.
|
||||
This model places a linear head on top of the output entity representation.
|
||||
- :class:`~transformers.LukeForEntityPairClassification`, for tasks to classify the relationship between two entities
|
||||
such as relation classification, e.g. the `TACRED dataset <https://nlp.stanford.edu/projects/tacred/>`__. This
|
||||
model places a linear head on top of the concatenated output representation of the pair of given entities.
|
||||
- :class:`~transformers.LukeForEntitySpanClassification`, for tasks to classify the sequence of entity spans, such as
|
||||
named entity recognition (NER). This model places a linear head on top of the output entity representations. You
|
||||
can address NER using this model by inputting all possible entity spans in the text to the model.
|
||||
|
||||
:class:`~transformers.LukeTokenizer` has a ``task`` argument, which enables you to easily create an input to these
|
||||
head models by specifying ``task="entity_classification"``, ``task="entity_pair_classification"``, or
|
||||
``task="entity_span_classification"``. Please refer to the example code of each head models.
|
||||
|
||||
There are also 3 notebooks available, which showcase how you can reproduce the results as reported in the paper with
|
||||
the HuggingFace implementation of LUKE. They can be found `here
|
||||
<https://github.com/studio-ousia/luke/tree/master/notebooks>`__.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block::
|
||||
|
||||
>>> from transformers import LukeTokenizer, LukeModel, LukeForEntityPairClassification
|
||||
|
||||
>>> model = LukeModel.from_pretrained("studio-ousia/luke-base")
|
||||
>>> tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base")
|
||||
|
||||
# Example 1: Computing the contextualized entity representation corresponding to the entity mention "Beyoncé"
|
||||
>>> text = "Beyoncé lives in Los Angeles."
|
||||
>>> entity_spans = [(0, 7)] # character-based entity span corresponding to "Beyoncé"
|
||||
>>> inputs = tokenizer(text, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
|
||||
>>> outputs = model(**inputs)
|
||||
>>> word_last_hidden_state = outputs.last_hidden_state
|
||||
>>> entity_last_hidden_state = outputs.entity_last_hidden_state
|
||||
|
||||
# Example 2: Inputting Wikipedia entities to obtain enriched contextualized representations
|
||||
>>> entities = ["Beyoncé", "Los Angeles"] # Wikipedia entity titles corresponding to the entity mentions "Beyoncé" and "Los Angeles"
|
||||
>>> entity_spans = [(0, 7), (17, 28)] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
||||
>>> inputs = tokenizer(text, entities=entities, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
|
||||
>>> outputs = model(**inputs)
|
||||
>>> word_last_hidden_state = outputs.last_hidden_state
|
||||
>>> entity_last_hidden_state = outputs.entity_last_hidden_state
|
||||
|
||||
# Example 3: Classifying the relationship between two entities using LukeForEntityPairClassification head model
|
||||
>>> model = LukeForEntityPairClassification.from_pretrained("studio-ousia/luke-large-finetuned-tacred")
|
||||
>>> tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-large-finetuned-tacred")
|
||||
>>> entity_spans = [(0, 7), (17, 28)] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
||||
>>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
|
||||
>>> outputs = model(**inputs)
|
||||
>>> logits = outputs.logits
|
||||
>>> predicted_class_idx = int(logits[0].argmax())
|
||||
>>> print("Predicted class:", model.config.id2label[predicted_class_idx])
|
||||
|
||||
This model was contributed by `ikuyamada <https://huggingface.co/ikuyamada>`__ and `nielsr
|
||||
<https://huggingface.co/nielsr>`__. The original code can be found `here <https://github.com/studio-ousia/luke>`__.
|
||||
|
||||
|
||||
LukeConfig
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: transformers.LukeConfig
|
||||
:members:
|
||||
|
||||
|
||||
LukeTokenizer
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: transformers.LukeTokenizer
|
||||
:members: __call__, save_vocabulary
|
||||
|
||||
|
||||
LukeModel
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: transformers.LukeModel
|
||||
:members: forward
|
||||
|
||||
|
||||
LukeForEntityClassification
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: transformers.LukeForEntityClassification
|
||||
:members: forward
|
||||
|
||||
|
||||
LukeForEntityPairClassification
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: transformers.LukeForEntityPairClassification
|
||||
:members: forward
|
||||
|
||||
|
||||
LukeForEntitySpanClassification
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: transformers.LukeForEntitySpanClassification
|
||||
:members: forward
|
||||
@@ -189,6 +189,7 @@ _import_structure = {
|
||||
"models.layoutlm": ["LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "LayoutLMConfig", "LayoutLMTokenizer"],
|
||||
"models.led": ["LED_PRETRAINED_CONFIG_ARCHIVE_MAP", "LEDConfig", "LEDTokenizer"],
|
||||
"models.longformer": ["LONGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "LongformerConfig", "LongformerTokenizer"],
|
||||
"models.luke": ["LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP", "LukeConfig", "LukeTokenizer"],
|
||||
"models.lxmert": ["LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LxmertConfig", "LxmertTokenizer"],
|
||||
"models.m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config"],
|
||||
"models.marian": ["MarianConfig"],
|
||||
@@ -444,8 +445,8 @@ if is_torch_available():
|
||||
]
|
||||
_import_structure["generation_utils"] = ["top_k_top_p_filtering"]
|
||||
_import_structure["modeling_utils"] = ["Conv1D", "PreTrainedModel", "apply_chunking_to_forward", "prune_layer"]
|
||||
# PyTorch models structure
|
||||
|
||||
# PyTorch models structure
|
||||
_import_structure["models.albert"].extend(
|
||||
[
|
||||
"ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||
@@ -753,6 +754,16 @@ if is_torch_available():
|
||||
"LongformerSelfAttention",
|
||||
]
|
||||
)
|
||||
_import_structure["models.luke"].extend(
|
||||
[
|
||||
"LUKE_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||
"LukeForEntityClassification",
|
||||
"LukeForEntityPairClassification",
|
||||
"LukeForEntitySpanClassification",
|
||||
"LukeModel",
|
||||
"LukePreTrainedModel",
|
||||
]
|
||||
)
|
||||
_import_structure["models.lxmert"].extend(
|
||||
[
|
||||
"LxmertEncoder",
|
||||
@@ -1542,6 +1553,7 @@ if TYPE_CHECKING:
|
||||
from .models.layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig, LayoutLMTokenizer
|
||||
from .models.led import LED_PRETRAINED_CONFIG_ARCHIVE_MAP, LEDConfig, LEDTokenizer
|
||||
from .models.longformer import LONGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, LongformerConfig, LongformerTokenizer
|
||||
from .models.luke import LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP, LukeConfig, LukeTokenizer
|
||||
from .models.lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig, LxmertTokenizer
|
||||
from .models.m2m_100 import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, M2M100Config
|
||||
from .models.marian import MarianConfig
|
||||
@@ -2024,6 +2036,14 @@ if TYPE_CHECKING:
|
||||
LongformerModel,
|
||||
LongformerSelfAttention,
|
||||
)
|
||||
from .models.luke import (
|
||||
LUKE_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
LukeForEntityClassification,
|
||||
LukeForEntityPairClassification,
|
||||
LukeForEntitySpanClassification,
|
||||
LukeModel,
|
||||
LukePreTrainedModel,
|
||||
)
|
||||
from .models.lxmert import (
|
||||
LxmertEncoder,
|
||||
LxmertForPreTraining,
|
||||
|
||||
@@ -48,6 +48,7 @@ from . import (
|
||||
layoutlm,
|
||||
led,
|
||||
longformer,
|
||||
luke,
|
||||
lxmert,
|
||||
m2m_100,
|
||||
marian,
|
||||
|
||||
@@ -47,6 +47,7 @@ from ..ibert.configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBe
|
||||
from ..layoutlm.configuration_layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig
|
||||
from ..led.configuration_led import LED_PRETRAINED_CONFIG_ARCHIVE_MAP, LEDConfig
|
||||
from ..longformer.configuration_longformer import LONGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, LongformerConfig
|
||||
from ..luke.configuration_luke import LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP, LukeConfig
|
||||
from ..lxmert.configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig
|
||||
from ..m2m_100.configuration_m2m_100 import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, M2M100Config
|
||||
from ..marian.configuration_marian import MarianConfig
|
||||
@@ -86,6 +87,7 @@ ALL_PRETRAINED_CONFIG_ARCHIVE_MAP = dict(
|
||||
for pretrained_map in [
|
||||
# Add archive maps here
|
||||
DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
@@ -138,6 +140,7 @@ CONFIG_MAPPING = OrderedDict(
|
||||
[
|
||||
# Add configs here
|
||||
("deit", DeiTConfig),
|
||||
("luke", LukeConfig),
|
||||
("gpt_neo", GPTNeoConfig),
|
||||
("big_bird", BigBirdConfig),
|
||||
("speech_to_text", Speech2TextConfig),
|
||||
@@ -196,6 +199,7 @@ MODEL_NAMES_MAPPING = OrderedDict(
|
||||
[
|
||||
# Add full (and cased) model names here
|
||||
("deit", "DeiT"),
|
||||
("luke", "LUKE"),
|
||||
("gpt_neo", "GPT Neo"),
|
||||
("big_bird", "BigBird"),
|
||||
("speech_to_text", "Speech2Text"),
|
||||
|
||||
@@ -166,6 +166,7 @@ from ..longformer.modeling_longformer import (
|
||||
LongformerForTokenClassification,
|
||||
LongformerModel,
|
||||
)
|
||||
from ..luke.modeling_luke import LukeModel
|
||||
from ..lxmert.modeling_lxmert import LxmertForPreTraining, LxmertForQuestionAnswering, LxmertModel
|
||||
from ..m2m_100.modeling_m2m_100 import M2M100ForConditionalGeneration, M2M100Model
|
||||
from ..marian.modeling_marian import MarianForCausalLM, MarianModel, MarianMTModel
|
||||
@@ -308,6 +309,7 @@ from .configuration_auto import (
|
||||
LayoutLMConfig,
|
||||
LEDConfig,
|
||||
LongformerConfig,
|
||||
LukeConfig,
|
||||
LxmertConfig,
|
||||
M2M100Config,
|
||||
MarianConfig,
|
||||
@@ -343,6 +345,7 @@ MODEL_MAPPING = OrderedDict(
|
||||
[
|
||||
# Base model mapping
|
||||
(DeiTConfig, DeiTModel),
|
||||
(LukeConfig, LukeModel),
|
||||
(GPTNeoConfig, GPTNeoModel),
|
||||
(BigBirdConfig, BigBirdModel),
|
||||
(Speech2TextConfig, Speech2TextModel),
|
||||
|
||||
@@ -41,6 +41,7 @@ from ..herbert.tokenization_herbert import HerbertTokenizer
|
||||
from ..layoutlm.tokenization_layoutlm import LayoutLMTokenizer
|
||||
from ..led.tokenization_led import LEDTokenizer
|
||||
from ..longformer.tokenization_longformer import LongformerTokenizer
|
||||
from ..luke.tokenization_luke import LukeTokenizer
|
||||
from ..lxmert.tokenization_lxmert import LxmertTokenizer
|
||||
from ..mobilebert.tokenization_mobilebert import MobileBertTokenizer
|
||||
from ..mpnet.tokenization_mpnet import MPNetTokenizer
|
||||
@@ -81,6 +82,7 @@ from .configuration_auto import (
|
||||
LayoutLMConfig,
|
||||
LEDConfig,
|
||||
LongformerConfig,
|
||||
LukeConfig,
|
||||
LxmertConfig,
|
||||
M2M100Config,
|
||||
MarianConfig,
|
||||
@@ -235,7 +237,6 @@ TOKENIZER_MAPPING = OrderedDict(
|
||||
(MarianConfig, (MarianTokenizer, None)),
|
||||
(BlenderbotSmallConfig, (BlenderbotSmallTokenizer, None)),
|
||||
(BlenderbotConfig, (BlenderbotTokenizer, None)),
|
||||
(LongformerConfig, (LongformerTokenizer, LongformerTokenizerFast)),
|
||||
(BartConfig, (BartTokenizer, BartTokenizerFast)),
|
||||
(LongformerConfig, (LongformerTokenizer, LongformerTokenizerFast)),
|
||||
(RobertaConfig, (RobertaTokenizer, RobertaTokenizerFast)),
|
||||
@@ -271,6 +272,7 @@ TOKENIZER_MAPPING = OrderedDict(
|
||||
(IBertConfig, (RobertaTokenizer, RobertaTokenizerFast)),
|
||||
(Wav2Vec2Config, (Wav2Vec2CTCTokenizer, None)),
|
||||
(GPTNeoConfig, (GPT2Tokenizer, GPT2TokenizerFast)),
|
||||
(LukeConfig, (LukeTokenizer, None)),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
70
src/transformers/models/luke/__init__.py
Normal file
70
src/transformers/models/luke/__init__.py
Normal file
@@ -0,0 +1,70 @@
|
||||
# flake8: noqa
|
||||
# There's no way to ignore "F401 '...' imported but unused" warnings in this
|
||||
# module, but to preserve other warnings. So, don't check this module at all.
|
||||
|
||||
# Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...file_utils import _BaseLazyModule, is_torch_available
|
||||
|
||||
|
||||
_import_structure = {
|
||||
"configuration_luke": ["LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP", "LukeConfig"],
|
||||
"tokenization_luke": ["LukeTokenizer"],
|
||||
}
|
||||
|
||||
if is_torch_available():
|
||||
_import_structure["modeling_luke"] = [
|
||||
"LUKE_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||
"LukeForEntityClassification",
|
||||
"LukeForEntityPairClassification",
|
||||
"LukeForEntitySpanClassification",
|
||||
"LukeModel",
|
||||
"LukePreTrainedModel",
|
||||
]
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .configuration_luke import LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP, LukeConfig
|
||||
from .tokenization_luke import LukeTokenizer
|
||||
|
||||
if is_torch_available():
|
||||
from .modeling_luke import (
|
||||
LUKE_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
LukeForEntityClassification,
|
||||
LukeForEntityPairClassification,
|
||||
LukeForEntitySpanClassification,
|
||||
LukeModel,
|
||||
LukePreTrainedModel,
|
||||
)
|
||||
|
||||
else:
|
||||
import importlib
|
||||
import os
|
||||
import sys
|
||||
|
||||
class _LazyModule(_BaseLazyModule):
|
||||
"""
|
||||
Module class that surfaces all objects but only performs associated imports when the objects are requested.
|
||||
"""
|
||||
|
||||
__file__ = globals()["__file__"]
|
||||
__path__ = [os.path.dirname(__file__)]
|
||||
|
||||
def _get_module(self, module_name: str):
|
||||
return importlib.import_module("." + module_name, self.__name__)
|
||||
|
||||
sys.modules[__name__] = _LazyModule(__name__, _import_structure)
|
||||
134
src/transformers/models/luke/configuration_luke.py
Normal file
134
src/transformers/models/luke/configuration_luke.py
Normal file
@@ -0,0 +1,134 @@
|
||||
# coding=utf-8
|
||||
# Copyright Studio Ousia 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.
|
||||
""" LUKE configuration """
|
||||
|
||||
from ...configuration_utils import PretrainedConfig
|
||||
from ...utils import logging
|
||||
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
|
||||
LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
||||
"studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json",
|
||||
"studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json",
|
||||
}
|
||||
|
||||
|
||||
class LukeConfig(PretrainedConfig):
|
||||
r"""
|
||||
This is the configuration class to store the configuration of a :class:`~transformers.LukeModel`. It is used to
|
||||
instantiate a LUKE model according to the specified arguments, defining the model architecture.
|
||||
|
||||
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 30522):
|
||||
Vocabulary size of the LUKE model. Defines the number of different tokens that can be represented by the
|
||||
:obj:`inputs_ids` passed when calling :class:`~transformers.LukeModel`.
|
||||
entity_vocab_size (:obj:`int`, `optional`, defaults to 500000):
|
||||
Entity vocabulary size of the LUKE model. Defines the number of different entities that can be represented
|
||||
by the :obj:`entity_ids` passed when calling :class:`~transformers.LukeModel`.
|
||||
hidden_size (:obj:`int`, `optional`, defaults to 768):
|
||||
Dimensionality of the encoder layers and the pooler layer.
|
||||
entity_emb_size (:obj:`int`, `optional`, defaults to 256):
|
||||
The number of dimensions of the entity embedding.
|
||||
num_hidden_layers (:obj:`int`, `optional`, defaults to 12):
|
||||
Number of hidden layers in the Transformer encoder.
|
||||
num_attention_heads (:obj:`int`, `optional`, defaults to 12):
|
||||
Number of attention heads for each attention layer in the Transformer encoder.
|
||||
intermediate_size (:obj:`int`, `optional`, defaults to 3072):
|
||||
Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
|
||||
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. If string,
|
||||
:obj:`"gelu"`, :obj:`"relu"`, :obj:`"silu"` and :obj:`"gelu_new"` are supported.
|
||||
hidden_dropout_prob (:obj:`float`, `optional`, defaults to 0.1):
|
||||
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):
|
||||
The dropout ratio for the attention probabilities.
|
||||
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).
|
||||
type_vocab_size (:obj:`int`, `optional`, defaults to 2):
|
||||
The vocabulary size of the :obj:`token_type_ids` passed when calling :class:`~transformers.LukeModel`.
|
||||
initializer_range (:obj:`float`, `optional`, defaults to 0.02):
|
||||
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
||||
layer_norm_eps (:obj:`float`, `optional`, defaults to 1e-12):
|
||||
The epsilon used by the layer normalization layers.
|
||||
gradient_checkpointing (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||
If True, use gradient checkpointing to save memory at the expense of slower backward pass.
|
||||
use_entity_aware_attention (:obj:`bool`, defaults to :obj:`True`):
|
||||
Whether or not the model should use the entity-aware self-attention mechanism proposed in `LUKE: Deep
|
||||
Contextualized Entity Representations with Entity-aware Self-attention (Yamada et al.)
|
||||
<https://arxiv.org/abs/2010.01057>`__.
|
||||
|
||||
Examples::
|
||||
|
||||
>>> from transformers import LukeConfig, LukeModel
|
||||
|
||||
>>> # Initializing a LUKE configuration
|
||||
>>> configuration = LukeConfig()
|
||||
|
||||
>>> # Initializing a model from the configuration
|
||||
>>> model = LukeModel(configuration)
|
||||
|
||||
>>> # Accessing the model configuration
|
||||
>>> configuration = model.config
|
||||
"""
|
||||
model_type = "luke"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vocab_size=50267,
|
||||
entity_vocab_size=500000,
|
||||
hidden_size=768,
|
||||
entity_emb_size=256,
|
||||
num_hidden_layers=12,
|
||||
num_attention_heads=12,
|
||||
intermediate_size=3072,
|
||||
hidden_act="gelu",
|
||||
hidden_dropout_prob=0.1,
|
||||
attention_probs_dropout_prob=0.1,
|
||||
max_position_embeddings=512,
|
||||
type_vocab_size=2,
|
||||
initializer_range=0.02,
|
||||
layer_norm_eps=1e-12,
|
||||
gradient_checkpointing=False,
|
||||
use_entity_aware_attention=True,
|
||||
pad_token_id=1,
|
||||
bos_token_id=0,
|
||||
eos_token_id=2,
|
||||
**kwargs
|
||||
):
|
||||
"""Constructs LukeConfig."""
|
||||
super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
|
||||
|
||||
self.vocab_size = vocab_size
|
||||
self.entity_vocab_size = entity_vocab_size
|
||||
self.hidden_size = hidden_size
|
||||
self.entity_emb_size = entity_emb_size
|
||||
self.num_hidden_layers = num_hidden_layers
|
||||
self.num_attention_heads = num_attention_heads
|
||||
self.hidden_act = hidden_act
|
||||
self.intermediate_size = intermediate_size
|
||||
self.hidden_dropout_prob = hidden_dropout_prob
|
||||
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
||||
self.max_position_embeddings = max_position_embeddings
|
||||
self.type_vocab_size = type_vocab_size
|
||||
self.initializer_range = initializer_range
|
||||
self.layer_norm_eps = layer_norm_eps
|
||||
self.gradient_checkpointing = gradient_checkpointing
|
||||
self.use_entity_aware_attention = use_entity_aware_attention
|
||||
@@ -0,0 +1,153 @@
|
||||
# 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 LUKE checkpoint."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
|
||||
import torch
|
||||
|
||||
from transformers import LukeConfig, LukeModel, LukeTokenizer, RobertaTokenizer
|
||||
from transformers.tokenization_utils_base import AddedToken
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def convert_luke_checkpoint(checkpoint_path, metadata_path, entity_vocab_path, pytorch_dump_folder_path, model_size):
|
||||
# Load configuration defined in the metadata file
|
||||
with open(metadata_path) as metadata_file:
|
||||
metadata = json.load(metadata_file)
|
||||
config = LukeConfig(use_entity_aware_attention=True, **metadata["model_config"])
|
||||
|
||||
# Load in the weights from the checkpoint_path
|
||||
state_dict = torch.load(checkpoint_path, map_location="cpu")
|
||||
|
||||
# Load the entity vocab file
|
||||
entity_vocab = load_entity_vocab(entity_vocab_path)
|
||||
|
||||
tokenizer = RobertaTokenizer.from_pretrained(metadata["model_config"]["bert_model_name"])
|
||||
|
||||
# Add special tokens to the token vocabulary for downstream tasks
|
||||
entity_token_1 = AddedToken("<ent>", lstrip=False, rstrip=False)
|
||||
entity_token_2 = AddedToken("<ent2>", lstrip=False, rstrip=False)
|
||||
tokenizer.add_special_tokens(dict(additional_special_tokens=[entity_token_1, entity_token_2]))
|
||||
config.vocab_size += 2
|
||||
|
||||
print(f"Saving tokenizer to {pytorch_dump_folder_path}")
|
||||
tokenizer.save_pretrained(pytorch_dump_folder_path)
|
||||
with open(os.path.join(pytorch_dump_folder_path, LukeTokenizer.vocab_files_names["entity_vocab_file"]), "w") as f:
|
||||
json.dump(entity_vocab, f)
|
||||
|
||||
tokenizer = LukeTokenizer.from_pretrained(pytorch_dump_folder_path)
|
||||
|
||||
# Initialize the embeddings of the special tokens
|
||||
word_emb = state_dict["embeddings.word_embeddings.weight"]
|
||||
ent_emb = word_emb[tokenizer.convert_tokens_to_ids(["@"])[0]].unsqueeze(0)
|
||||
ent2_emb = word_emb[tokenizer.convert_tokens_to_ids(["#"])[0]].unsqueeze(0)
|
||||
state_dict["embeddings.word_embeddings.weight"] = torch.cat([word_emb, ent_emb, ent2_emb])
|
||||
|
||||
# Initialize the query layers of the entity-aware self-attention mechanism
|
||||
for layer_index in range(config.num_hidden_layers):
|
||||
for matrix_name in ["query.weight", "query.bias"]:
|
||||
prefix = f"encoder.layer.{layer_index}.attention.self."
|
||||
state_dict[prefix + "w2e_" + matrix_name] = state_dict[prefix + matrix_name]
|
||||
state_dict[prefix + "e2w_" + matrix_name] = state_dict[prefix + matrix_name]
|
||||
state_dict[prefix + "e2e_" + matrix_name] = state_dict[prefix + matrix_name]
|
||||
|
||||
# Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks
|
||||
entity_emb = state_dict["entity_embeddings.entity_embeddings.weight"]
|
||||
entity_emb[entity_vocab["[MASK2]"]] = entity_emb[entity_vocab["[MASK]"]]
|
||||
|
||||
model = LukeModel(config=config).eval()
|
||||
|
||||
missing_keys, unexpected_keys = model.load_state_dict(state_dict, strict=False)
|
||||
assert len(missing_keys) == 1 and missing_keys[0] == "embeddings.position_ids"
|
||||
assert all(key.startswith("entity_predictions") or key.startswith("lm_head") for key in unexpected_keys)
|
||||
|
||||
# Check outputs
|
||||
tokenizer = LukeTokenizer.from_pretrained(pytorch_dump_folder_path, task="entity_classification")
|
||||
|
||||
text = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck as a fortuitous netcord helped the new world number one avoid a humiliating second- round exit at Wimbledon ."
|
||||
span = (39, 42)
|
||||
encoding = tokenizer(text, entity_spans=[span], add_prefix_space=True, return_tensors="pt")
|
||||
|
||||
outputs = model(**encoding)
|
||||
|
||||
# Verify word hidden states
|
||||
if model_size == "large":
|
||||
expected_shape = torch.Size((1, 42, 1024))
|
||||
expected_slice = torch.tensor(
|
||||
[[0.0133, 0.0865, 0.0095], [0.3093, -0.2576, -0.7418], [-0.1720, -0.2117, -0.2869]]
|
||||
)
|
||||
else: # base
|
||||
expected_shape = torch.Size((1, 42, 768))
|
||||
expected_slice = torch.tensor([[0.0037, 0.1368, -0.0091], [0.1099, 0.3329, -0.1095], [0.0765, 0.5335, 0.1179]])
|
||||
|
||||
assert outputs.last_hidden_state.shape == expected_shape
|
||||
assert torch.allclose(outputs.last_hidden_state[0, :3, :3], expected_slice, atol=1e-4)
|
||||
|
||||
# Verify entity hidden states
|
||||
if model_size == "large":
|
||||
expected_shape = torch.Size((1, 1, 1024))
|
||||
expected_slice = torch.tensor([[0.0466, -0.0106, -0.0179]])
|
||||
else: # base
|
||||
expected_shape = torch.Size((1, 1, 768))
|
||||
expected_slice = torch.tensor([[0.1457, 0.1044, 0.0174]])
|
||||
|
||||
assert outputs.entity_last_hidden_state.shape == expected_shape
|
||||
assert torch.allclose(outputs.entity_last_hidden_state[0, :3, :3], expected_slice, atol=1e-4)
|
||||
|
||||
# Finally, save our PyTorch model and tokenizer
|
||||
print("Saving PyTorch model to {}".format(pytorch_dump_folder_path))
|
||||
model.save_pretrained(pytorch_dump_folder_path)
|
||||
|
||||
|
||||
def load_entity_vocab(entity_vocab_path):
|
||||
entity_vocab = {}
|
||||
with open(entity_vocab_path, "r", encoding="utf-8") as f:
|
||||
for (index, line) in enumerate(f):
|
||||
title, _ = line.rstrip().split("\t")
|
||||
entity_vocab[title] = index
|
||||
|
||||
return entity_vocab
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
# Required parameters
|
||||
parser.add_argument("--checkpoint_path", type=str, help="Path to a pytorch_model.bin file.")
|
||||
parser.add_argument(
|
||||
"--metadata_path", default=None, type=str, help="Path to a metadata.json file, defining the configuration."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--entity_vocab_path",
|
||||
default=None,
|
||||
type=str,
|
||||
help="Path to an entity_vocab.tsv file, containing the entity vocabulary.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--pytorch_dump_folder_path", default=None, type=str, help="Path to where to dump the output PyTorch model."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--model_size", default="base", type=str, choices=["base", "large"], help="Size of the model to be converted."
|
||||
)
|
||||
args = parser.parse_args()
|
||||
convert_luke_checkpoint(
|
||||
args.checkpoint_path,
|
||||
args.metadata_path,
|
||||
args.entity_vocab_path,
|
||||
args.pytorch_dump_folder_path,
|
||||
args.model_size,
|
||||
)
|
||||
1367
src/transformers/models/luke/modeling_luke.py
Normal file
1367
src/transformers/models/luke/modeling_luke.py
Normal file
File diff suppressed because it is too large
Load Diff
1531
src/transformers/models/luke/tokenization_luke.py
Normal file
1531
src/transformers/models/luke/tokenization_luke.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -801,7 +801,9 @@ class SpecialTokensMixin:
|
||||
if key in self.SPECIAL_TOKENS_ATTRIBUTES:
|
||||
if key == "additional_special_tokens":
|
||||
assert isinstance(value, (list, tuple)), f"Value {value} is not a list or tuple"
|
||||
assert all(isinstance(t, str) for t in value), "One of the tokens is not a string"
|
||||
assert all(
|
||||
isinstance(t, (str, AddedToken)) for t in value
|
||||
), "One of the tokens is not a string or an AddedToken"
|
||||
setattr(self, key, value)
|
||||
elif isinstance(value, (str, AddedToken)):
|
||||
setattr(self, key, value)
|
||||
|
||||
@@ -1739,6 +1739,42 @@ class LongformerSelfAttention:
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
LUKE_PRETRAINED_MODEL_ARCHIVE_LIST = None
|
||||
|
||||
|
||||
class LukeForEntityClassification:
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class LukeForEntityPairClassification:
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class LukeForEntitySpanClassification:
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class LukeModel:
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
@classmethod
|
||||
def from_pretrained(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class LukePreTrainedModel:
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
@classmethod
|
||||
def from_pretrained(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class LxmertEncoder:
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
609
tests/test_modeling_luke.py
Normal file
609
tests/test_modeling_luke.py
Normal file
@@ -0,0 +1,609 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2021 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# 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.
|
||||
""" Testing suite for the PyTorch LUKE model. """
|
||||
|
||||
import unittest
|
||||
|
||||
from transformers import is_torch_available
|
||||
from transformers.testing_utils import require_torch, slow, torch_device
|
||||
|
||||
from .test_configuration_common import ConfigTester
|
||||
from .test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers import (
|
||||
LukeConfig,
|
||||
LukeForEntityClassification,
|
||||
LukeForEntityPairClassification,
|
||||
LukeForEntitySpanClassification,
|
||||
LukeModel,
|
||||
LukeTokenizer,
|
||||
)
|
||||
from transformers.models.luke.modeling_luke import LUKE_PRETRAINED_MODEL_ARCHIVE_LIST
|
||||
|
||||
|
||||
class LukeModelTester:
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
batch_size=13,
|
||||
seq_length=7,
|
||||
is_training=True,
|
||||
entity_length=3,
|
||||
mention_length=5,
|
||||
use_attention_mask=True,
|
||||
use_token_type_ids=True,
|
||||
use_entity_ids=True,
|
||||
use_entity_attention_mask=True,
|
||||
use_entity_token_type_ids=True,
|
||||
use_entity_position_ids=True,
|
||||
use_labels=True,
|
||||
vocab_size=99,
|
||||
entity_vocab_size=10,
|
||||
entity_emb_size=6,
|
||||
hidden_size=32,
|
||||
num_hidden_layers=5,
|
||||
num_attention_heads=4,
|
||||
intermediate_size=37,
|
||||
hidden_act="gelu",
|
||||
hidden_dropout_prob=0.1,
|
||||
attention_probs_dropout_prob=0.1,
|
||||
max_position_embeddings=512,
|
||||
type_vocab_size=16,
|
||||
type_sequence_label_size=2,
|
||||
initializer_range=0.02,
|
||||
num_entity_classification_labels=9,
|
||||
num_entity_pair_classification_labels=6,
|
||||
num_entity_span_classification_labels=4,
|
||||
use_entity_aware_attention=True,
|
||||
scope=None,
|
||||
):
|
||||
self.parent = parent
|
||||
self.batch_size = batch_size
|
||||
self.seq_length = seq_length
|
||||
self.is_training = is_training
|
||||
self.entity_length = entity_length
|
||||
self.mention_length = mention_length
|
||||
self.use_attention_mask = use_attention_mask
|
||||
self.use_token_type_ids = use_token_type_ids
|
||||
self.use_entity_ids = use_entity_ids
|
||||
self.use_entity_attention_mask = use_entity_attention_mask
|
||||
self.use_entity_token_type_ids = use_entity_token_type_ids
|
||||
self.use_entity_position_ids = use_entity_position_ids
|
||||
self.use_labels = use_labels
|
||||
self.vocab_size = vocab_size
|
||||
self.entity_vocab_size = entity_vocab_size
|
||||
self.entity_emb_size = entity_emb_size
|
||||
self.hidden_size = hidden_size
|
||||
self.num_hidden_layers = num_hidden_layers
|
||||
self.num_attention_heads = num_attention_heads
|
||||
self.intermediate_size = intermediate_size
|
||||
self.hidden_act = hidden_act
|
||||
self.hidden_dropout_prob = hidden_dropout_prob
|
||||
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
||||
self.max_position_embeddings = max_position_embeddings
|
||||
self.type_vocab_size = type_vocab_size
|
||||
self.type_sequence_label_size = type_sequence_label_size
|
||||
self.initializer_range = initializer_range
|
||||
self.num_entity_classification_labels = num_entity_classification_labels
|
||||
self.num_entity_pair_classification_labels = num_entity_pair_classification_labels
|
||||
self.num_entity_span_classification_labels = num_entity_span_classification_labels
|
||||
self.scope = scope
|
||||
self.use_entity_aware_attention = use_entity_aware_attention
|
||||
|
||||
self.encoder_seq_length = seq_length
|
||||
self.key_length = seq_length
|
||||
self.num_hidden_states_types = 2 # hidden_states and entity_hidden_states
|
||||
|
||||
def prepare_config_and_inputs(self):
|
||||
# prepare words
|
||||
input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size)
|
||||
|
||||
attention_mask = None
|
||||
if self.use_attention_mask:
|
||||
attention_mask = random_attention_mask([self.batch_size, self.seq_length])
|
||||
|
||||
token_type_ids = None
|
||||
if self.use_token_type_ids:
|
||||
token_type_ids = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size)
|
||||
|
||||
# prepare entities
|
||||
entity_ids = ids_tensor([self.batch_size, self.entity_length], self.entity_vocab_size)
|
||||
|
||||
entity_attention_mask = None
|
||||
if self.use_entity_attention_mask:
|
||||
entity_attention_mask = random_attention_mask([self.batch_size, self.entity_length])
|
||||
|
||||
entity_token_type_ids = None
|
||||
if self.use_token_type_ids:
|
||||
entity_token_type_ids = ids_tensor([self.batch_size, self.entity_length], self.type_vocab_size)
|
||||
|
||||
entity_position_ids = None
|
||||
if self.use_entity_position_ids:
|
||||
entity_position_ids = ids_tensor(
|
||||
[self.batch_size, self.entity_length, self.mention_length], self.mention_length
|
||||
)
|
||||
|
||||
sequence_labels = None
|
||||
entity_classification_labels = None
|
||||
entity_pair_classification_labels = None
|
||||
entity_span_classification_labels = None
|
||||
|
||||
if self.use_labels:
|
||||
sequence_labels = ids_tensor([self.batch_size], self.type_sequence_label_size)
|
||||
entity_classification_labels = ids_tensor([self.batch_size], self.num_entity_classification_labels)
|
||||
entity_pair_classification_labels = ids_tensor(
|
||||
[self.batch_size], self.num_entity_pair_classification_labels
|
||||
)
|
||||
entity_span_classification_labels = ids_tensor(
|
||||
[self.batch_size, self.entity_length], self.num_entity_span_classification_labels
|
||||
)
|
||||
|
||||
config = LukeConfig(
|
||||
vocab_size=self.vocab_size,
|
||||
entity_vocab_size=self.entity_vocab_size,
|
||||
entity_emb_size=self.entity_emb_size,
|
||||
hidden_size=self.hidden_size,
|
||||
num_hidden_layers=self.num_hidden_layers,
|
||||
num_attention_heads=self.num_attention_heads,
|
||||
intermediate_size=self.intermediate_size,
|
||||
hidden_act=self.hidden_act,
|
||||
hidden_dropout_prob=self.hidden_dropout_prob,
|
||||
attention_probs_dropout_prob=self.attention_probs_dropout_prob,
|
||||
max_position_embeddings=self.max_position_embeddings,
|
||||
type_vocab_size=self.type_vocab_size,
|
||||
is_decoder=False,
|
||||
initializer_range=self.initializer_range,
|
||||
use_entity_aware_attention=self.use_entity_aware_attention,
|
||||
)
|
||||
|
||||
return (
|
||||
config,
|
||||
input_ids,
|
||||
attention_mask,
|
||||
token_type_ids,
|
||||
entity_ids,
|
||||
entity_attention_mask,
|
||||
entity_token_type_ids,
|
||||
entity_position_ids,
|
||||
sequence_labels,
|
||||
entity_classification_labels,
|
||||
entity_pair_classification_labels,
|
||||
entity_span_classification_labels,
|
||||
)
|
||||
|
||||
def create_and_check_model(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
attention_mask,
|
||||
token_type_ids,
|
||||
entity_ids,
|
||||
entity_attention_mask,
|
||||
entity_token_type_ids,
|
||||
entity_position_ids,
|
||||
sequence_labels,
|
||||
entity_classification_labels,
|
||||
entity_pair_classification_labels,
|
||||
entity_span_classification_labels,
|
||||
):
|
||||
model = LukeModel(config=config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
# test with words + entities
|
||||
result = model(
|
||||
input_ids,
|
||||
attention_mask=attention_mask,
|
||||
token_type_ids=token_type_ids,
|
||||
entity_ids=entity_ids,
|
||||
entity_attention_mask=entity_attention_mask,
|
||||
entity_token_type_ids=entity_token_type_ids,
|
||||
entity_position_ids=entity_position_ids,
|
||||
)
|
||||
self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
|
||||
self.parent.assertEqual(
|
||||
result.entity_last_hidden_state.shape, (self.batch_size, self.entity_length, self.hidden_size)
|
||||
)
|
||||
|
||||
# test with words only
|
||||
result = model(input_ids, token_type_ids=token_type_ids)
|
||||
result = model(input_ids)
|
||||
self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
|
||||
|
||||
def create_and_check_for_entity_classification(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
attention_mask,
|
||||
token_type_ids,
|
||||
entity_ids,
|
||||
entity_attention_mask,
|
||||
entity_token_type_ids,
|
||||
entity_position_ids,
|
||||
sequence_labels,
|
||||
entity_classification_labels,
|
||||
entity_pair_classification_labels,
|
||||
entity_span_classification_labels,
|
||||
):
|
||||
config.num_labels = self.num_entity_classification_labels
|
||||
model = LukeForEntityClassification(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
result = model(
|
||||
input_ids,
|
||||
attention_mask=attention_mask,
|
||||
token_type_ids=token_type_ids,
|
||||
entity_ids=entity_ids,
|
||||
entity_attention_mask=entity_attention_mask,
|
||||
entity_token_type_ids=entity_token_type_ids,
|
||||
entity_position_ids=entity_position_ids,
|
||||
labels=entity_classification_labels,
|
||||
)
|
||||
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_entity_classification_labels))
|
||||
|
||||
def create_and_check_for_entity_pair_classification(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
attention_mask,
|
||||
token_type_ids,
|
||||
entity_ids,
|
||||
entity_attention_mask,
|
||||
entity_token_type_ids,
|
||||
entity_position_ids,
|
||||
sequence_labels,
|
||||
entity_classification_labels,
|
||||
entity_pair_classification_labels,
|
||||
entity_span_classification_labels,
|
||||
):
|
||||
config.num_labels = self.num_entity_pair_classification_labels
|
||||
model = LukeForEntityClassification(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
result = model(
|
||||
input_ids,
|
||||
attention_mask=attention_mask,
|
||||
token_type_ids=token_type_ids,
|
||||
entity_ids=entity_ids,
|
||||
entity_attention_mask=entity_attention_mask,
|
||||
entity_token_type_ids=entity_token_type_ids,
|
||||
entity_position_ids=entity_position_ids,
|
||||
labels=entity_pair_classification_labels,
|
||||
)
|
||||
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_entity_pair_classification_labels))
|
||||
|
||||
def create_and_check_for_entity_span_classification(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
attention_mask,
|
||||
token_type_ids,
|
||||
entity_ids,
|
||||
entity_attention_mask,
|
||||
entity_token_type_ids,
|
||||
entity_position_ids,
|
||||
sequence_labels,
|
||||
entity_classification_labels,
|
||||
entity_pair_classification_labels,
|
||||
entity_span_classification_labels,
|
||||
):
|
||||
config.num_labels = self.num_entity_span_classification_labels
|
||||
model = LukeForEntitySpanClassification(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
entity_start_positions = ids_tensor([self.batch_size, self.entity_length], self.seq_length)
|
||||
entity_end_positions = ids_tensor([self.batch_size, self.entity_length], self.seq_length)
|
||||
|
||||
result = model(
|
||||
input_ids,
|
||||
attention_mask=attention_mask,
|
||||
token_type_ids=token_type_ids,
|
||||
entity_ids=entity_ids,
|
||||
entity_attention_mask=entity_attention_mask,
|
||||
entity_token_type_ids=entity_token_type_ids,
|
||||
entity_position_ids=entity_position_ids,
|
||||
entity_start_positions=entity_start_positions,
|
||||
entity_end_positions=entity_end_positions,
|
||||
labels=entity_span_classification_labels,
|
||||
)
|
||||
self.parent.assertEqual(
|
||||
result.logits.shape, (self.batch_size, self.entity_length, self.num_entity_span_classification_labels)
|
||||
)
|
||||
|
||||
def prepare_config_and_inputs_for_common(self):
|
||||
config_and_inputs = self.prepare_config_and_inputs()
|
||||
(
|
||||
config,
|
||||
input_ids,
|
||||
attention_mask,
|
||||
token_type_ids,
|
||||
entity_ids,
|
||||
entity_attention_mask,
|
||||
entity_token_type_ids,
|
||||
entity_position_ids,
|
||||
sequence_labels,
|
||||
entity_classification_labels,
|
||||
entity_pair_classification_labels,
|
||||
entity_span_classification_labels,
|
||||
) = config_and_inputs
|
||||
inputs_dict = {
|
||||
"input_ids": input_ids,
|
||||
"token_type_ids": token_type_ids,
|
||||
"attention_mask": attention_mask,
|
||||
"entity_ids": entity_ids,
|
||||
"entity_token_type_ids": entity_token_type_ids,
|
||||
"entity_attention_mask": entity_attention_mask,
|
||||
"entity_position_ids": entity_position_ids,
|
||||
}
|
||||
return config, inputs_dict
|
||||
|
||||
|
||||
@require_torch
|
||||
class LukeModelTest(ModelTesterMixin, unittest.TestCase):
|
||||
|
||||
all_model_classes = (
|
||||
(
|
||||
LukeModel,
|
||||
LukeForEntityClassification,
|
||||
LukeForEntityPairClassification,
|
||||
LukeForEntitySpanClassification,
|
||||
)
|
||||
if is_torch_available()
|
||||
else ()
|
||||
)
|
||||
test_pruning = False
|
||||
test_torchscript = False
|
||||
test_resize_embeddings = True
|
||||
test_head_masking = True
|
||||
|
||||
def _prepare_for_class(self, inputs_dict, model_class, return_labels=False):
|
||||
inputs_dict = super()._prepare_for_class(inputs_dict, model_class, return_labels=return_labels)
|
||||
if model_class == LukeForEntitySpanClassification:
|
||||
inputs_dict["entity_start_positions"] = torch.zeros(
|
||||
(self.model_tester.batch_size, self.model_tester.entity_length), dtype=torch.long, device=torch_device
|
||||
)
|
||||
inputs_dict["entity_end_positions"] = torch.ones(
|
||||
(self.model_tester.batch_size, self.model_tester.entity_length), dtype=torch.long, device=torch_device
|
||||
)
|
||||
|
||||
if return_labels:
|
||||
if model_class in (LukeForEntityClassification, LukeForEntityPairClassification):
|
||||
inputs_dict["labels"] = torch.zeros(
|
||||
self.model_tester.batch_size, dtype=torch.long, device=torch_device
|
||||
)
|
||||
elif model_class == LukeForEntitySpanClassification:
|
||||
inputs_dict["labels"] = torch.zeros(
|
||||
(self.model_tester.batch_size, self.model_tester.entity_length),
|
||||
dtype=torch.long,
|
||||
device=torch_device,
|
||||
)
|
||||
return inputs_dict
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = LukeModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=LukeConfig, hidden_size=37)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
@slow
|
||||
def test_model_from_pretrained(self):
|
||||
for model_name in LUKE_PRETRAINED_MODEL_ARCHIVE_LIST:
|
||||
model = LukeModel.from_pretrained(model_name)
|
||||
self.assertIsNotNone(model)
|
||||
|
||||
def test_for_entity_classification(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_for_entity_classification(*config_and_inputs)
|
||||
|
||||
def test_for_entity_pair_classification(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_for_entity_pair_classification(*config_and_inputs)
|
||||
|
||||
def test_for_entity_span_classification(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_for_entity_span_classification(*config_and_inputs)
|
||||
|
||||
def test_attention_outputs(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
config.return_dict = True
|
||||
|
||||
seq_length = self.model_tester.seq_length
|
||||
entity_length = self.model_tester.entity_length
|
||||
key_length = seq_length + entity_length
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
inputs_dict["output_attentions"] = True
|
||||
inputs_dict["output_hidden_states"] = False
|
||||
config.return_dict = True
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
attentions = outputs.attentions
|
||||
self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)
|
||||
|
||||
# check that output_attentions also work using config
|
||||
del inputs_dict["output_attentions"]
|
||||
config.output_attentions = True
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
attentions = outputs.attentions
|
||||
self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)
|
||||
|
||||
self.assertListEqual(
|
||||
list(attentions[0].shape[-3:]),
|
||||
[self.model_tester.num_attention_heads, seq_length + entity_length, key_length],
|
||||
)
|
||||
out_len = len(outputs)
|
||||
|
||||
# Check attention is always last and order is fine
|
||||
inputs_dict["output_attentions"] = True
|
||||
inputs_dict["output_hidden_states"] = True
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
|
||||
added_hidden_states = self.model_tester.num_hidden_states_types
|
||||
self.assertEqual(out_len + added_hidden_states, len(outputs))
|
||||
|
||||
self_attentions = outputs.attentions
|
||||
|
||||
self.assertEqual(len(self_attentions), self.model_tester.num_hidden_layers)
|
||||
self.assertListEqual(
|
||||
list(self_attentions[0].shape[-3:]),
|
||||
[self.model_tester.num_attention_heads, seq_length + entity_length, key_length],
|
||||
)
|
||||
|
||||
def test_entity_hidden_states_output(self):
|
||||
def check_hidden_states_output(inputs_dict, config, model_class):
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
|
||||
entity_hidden_states = outputs.entity_hidden_states
|
||||
|
||||
expected_num_layers = getattr(
|
||||
self.model_tester, "expected_num_hidden_layers", self.model_tester.num_hidden_layers + 1
|
||||
)
|
||||
self.assertEqual(len(entity_hidden_states), expected_num_layers)
|
||||
|
||||
entity_length = self.model_tester.entity_length
|
||||
|
||||
self.assertListEqual(
|
||||
list(entity_hidden_states[0].shape[-2:]),
|
||||
[entity_length, self.model_tester.hidden_size],
|
||||
)
|
||||
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
inputs_dict["output_hidden_states"] = True
|
||||
check_hidden_states_output(inputs_dict, config, model_class)
|
||||
|
||||
# check that output_hidden_states also work using config
|
||||
del inputs_dict["output_hidden_states"]
|
||||
config.output_hidden_states = True
|
||||
|
||||
check_hidden_states_output(inputs_dict, config, model_class)
|
||||
|
||||
def test_retain_grad_entity_hidden_states(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
config.output_hidden_states = True
|
||||
config.output_attentions = True
|
||||
|
||||
# no need to test all models as different heads yield the same functionality
|
||||
model_class = self.all_model_classes[0]
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
|
||||
inputs = self._prepare_for_class(inputs_dict, model_class)
|
||||
|
||||
outputs = model(**inputs)
|
||||
|
||||
output = outputs[0]
|
||||
|
||||
entity_hidden_states = outputs.entity_hidden_states[0]
|
||||
entity_hidden_states.retain_grad()
|
||||
|
||||
output.flatten()[0].backward(retain_graph=True)
|
||||
|
||||
self.assertIsNotNone(entity_hidden_states.grad)
|
||||
|
||||
|
||||
@require_torch
|
||||
class LukeModelIntegrationTests(unittest.TestCase):
|
||||
@slow
|
||||
def test_inference_base_model(self):
|
||||
model = LukeModel.from_pretrained("studio-ousia/luke-base").eval()
|
||||
model.to(torch_device)
|
||||
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base", task="entity_classification")
|
||||
text = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck as a fortuitous netcord helped the new world number one avoid a humiliating second- round exit at Wimbledon ."
|
||||
span = (39, 42)
|
||||
encoding = tokenizer(text, entity_spans=[span], add_prefix_space=True, return_tensors="pt")
|
||||
|
||||
# move all values to device
|
||||
for key, value in encoding.items():
|
||||
encoding[key] = encoding[key].to(torch_device)
|
||||
|
||||
outputs = model(**encoding)
|
||||
|
||||
# Verify word hidden states
|
||||
expected_shape = torch.Size((1, 42, 768))
|
||||
self.assertEqual(outputs.last_hidden_state.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor(
|
||||
[[0.0037, 0.1368, -0.0091], [0.1099, 0.3329, -0.1095], [0.0765, 0.5335, 0.1179]]
|
||||
).to(torch_device)
|
||||
self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3], expected_slice, atol=1e-4))
|
||||
|
||||
# Verify entity hidden states
|
||||
expected_shape = torch.Size((1, 1, 768))
|
||||
self.assertEqual(outputs.entity_last_hidden_state.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor([[0.1457, 0.1044, 0.0174]])
|
||||
self.assertTrue(torch.allclose(outputs.entity_last_hidden_state[0, :3, :3], expected_slice, atol=1e-4))
|
||||
|
||||
@slow
|
||||
def test_inference_large_model(self):
|
||||
model = LukeModel.from_pretrained("studio-ousia/luke-large").eval()
|
||||
model.to(torch_device)
|
||||
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-large", task="entity_classification")
|
||||
text = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck as a fortuitous netcord helped the new world number one avoid a humiliating second- round exit at Wimbledon ."
|
||||
span = (39, 42)
|
||||
encoding = tokenizer(text, entity_spans=[span], add_prefix_space=True, return_tensors="pt")
|
||||
|
||||
# move all values to device
|
||||
for key, value in encoding.items():
|
||||
encoding[key] = encoding[key].to(torch_device)
|
||||
|
||||
outputs = model(**encoding)
|
||||
|
||||
# Verify word hidden states
|
||||
expected_shape = torch.Size((1, 42, 1024))
|
||||
self.assertEqual(outputs.last_hidden_state.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor(
|
||||
[[0.0133, 0.0865, 0.0095], [0.3093, -0.2576, -0.7418], [-0.1720, -0.2117, -0.2869]]
|
||||
).to(torch_device)
|
||||
self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3], expected_slice, atol=1e-4))
|
||||
|
||||
# Verify entity hidden states
|
||||
expected_shape = torch.Size((1, 1, 1024))
|
||||
self.assertEqual(outputs.entity_last_hidden_state.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor([[0.0466, -0.0106, -0.0179]])
|
||||
self.assertTrue(torch.allclose(outputs.entity_last_hidden_state[0, :3, :3], expected_slice, atol=1e-4))
|
||||
575
tests/test_tokenization_luke.py
Normal file
575
tests/test_tokenization_luke.py
Normal file
@@ -0,0 +1,575 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from transformers import AddedToken, LukeTokenizer
|
||||
from transformers.testing_utils import require_torch, slow
|
||||
|
||||
from .test_tokenization_common import TokenizerTesterMixin
|
||||
|
||||
|
||||
class Luke(TokenizerTesterMixin, unittest.TestCase):
|
||||
tokenizer_class = LukeTokenizer
|
||||
from_pretrained_kwargs = {"cls_token": "<s>"}
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.special_tokens_map = {"entity_token_1": "<ent>", "entity_token_2": "<ent2>"}
|
||||
|
||||
def get_tokenizer(self, task=None, **kwargs):
|
||||
kwargs.update(self.special_tokens_map)
|
||||
return self.tokenizer_class.from_pretrained("studio-ousia/luke-base", task=task, **kwargs)
|
||||
|
||||
def get_input_output_texts(self, tokenizer):
|
||||
input_text = "lower newer"
|
||||
output_text = "lower newer"
|
||||
return input_text, output_text
|
||||
|
||||
def test_full_tokenizer(self):
|
||||
tokenizer = self.tokenizer_class.from_pretrained("studio-ousia/luke-base")
|
||||
text = "lower newer"
|
||||
bpe_tokens = ["lower", "\u0120newer"]
|
||||
tokens = tokenizer.tokenize(text) # , add_prefix_space=True)
|
||||
self.assertListEqual(tokens, bpe_tokens)
|
||||
|
||||
input_tokens = tokens + [tokenizer.unk_token]
|
||||
input_bpe_tokens = [29668, 13964, 3]
|
||||
self.assertListEqual(tokenizer.convert_tokens_to_ids(input_tokens), input_bpe_tokens)
|
||||
|
||||
def luke_dict_integration_testing(self):
|
||||
tokenizer = self.get_tokenizer()
|
||||
|
||||
self.assertListEqual(tokenizer.encode("Hello world!", add_special_tokens=False), [0, 31414, 232, 328, 2])
|
||||
self.assertListEqual(
|
||||
tokenizer.encode("Hello world! cécé herlolip 418", add_special_tokens=False),
|
||||
[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2],
|
||||
)
|
||||
|
||||
@slow
|
||||
def test_sequence_builders(self):
|
||||
tokenizer = self.tokenizer_class.from_pretrained("studio-ousia/luke-large")
|
||||
|
||||
text = tokenizer.encode("sequence builders", add_special_tokens=False)
|
||||
text_2 = tokenizer.encode("multi-sequence build", add_special_tokens=False)
|
||||
|
||||
encoded_text_from_decode = tokenizer.encode(
|
||||
"sequence builders", add_special_tokens=True, add_prefix_space=False
|
||||
)
|
||||
encoded_pair_from_decode = tokenizer.encode(
|
||||
"sequence builders", "multi-sequence build", add_special_tokens=True, add_prefix_space=False
|
||||
)
|
||||
|
||||
encoded_sentence = tokenizer.build_inputs_with_special_tokens(text)
|
||||
encoded_pair = tokenizer.build_inputs_with_special_tokens(text, text_2)
|
||||
|
||||
assert encoded_sentence == encoded_text_from_decode
|
||||
assert encoded_pair == encoded_pair_from_decode
|
||||
|
||||
def test_space_encoding(self):
|
||||
tokenizer = self.get_tokenizer()
|
||||
|
||||
sequence = "Encode this sequence."
|
||||
space_encoding = tokenizer.byte_encoder[" ".encode("utf-8")[0]]
|
||||
|
||||
# Testing encoder arguments
|
||||
encoded = tokenizer.encode(sequence, add_special_tokens=False, add_prefix_space=False)
|
||||
first_char = tokenizer.convert_ids_to_tokens(encoded[0])[0]
|
||||
self.assertNotEqual(first_char, space_encoding)
|
||||
|
||||
encoded = tokenizer.encode(sequence, add_special_tokens=False, add_prefix_space=True)
|
||||
first_char = tokenizer.convert_ids_to_tokens(encoded[0])[0]
|
||||
self.assertEqual(first_char, space_encoding)
|
||||
|
||||
tokenizer.add_special_tokens({"bos_token": "<s>"})
|
||||
encoded = tokenizer.encode(sequence, add_special_tokens=True)
|
||||
first_char = tokenizer.convert_ids_to_tokens(encoded[1])[0]
|
||||
self.assertNotEqual(first_char, space_encoding)
|
||||
|
||||
# Testing spaces after special tokens
|
||||
mask = "<mask>"
|
||||
tokenizer.add_special_tokens(
|
||||
{"mask_token": AddedToken(mask, lstrip=True, rstrip=False)}
|
||||
) # mask token has a left space
|
||||
mask_ind = tokenizer.convert_tokens_to_ids(mask)
|
||||
|
||||
sequence = "Encode <mask> sequence"
|
||||
sequence_nospace = "Encode <mask>sequence"
|
||||
|
||||
encoded = tokenizer.encode(sequence)
|
||||
mask_loc = encoded.index(mask_ind)
|
||||
first_char = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1])[0]
|
||||
self.assertEqual(first_char, space_encoding)
|
||||
|
||||
encoded = tokenizer.encode(sequence_nospace)
|
||||
mask_loc = encoded.index(mask_ind)
|
||||
first_char = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1])[0]
|
||||
self.assertNotEqual(first_char, space_encoding)
|
||||
|
||||
def test_pretokenized_inputs(self):
|
||||
pass
|
||||
|
||||
def test_embeded_special_tokens(self):
|
||||
for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
|
||||
with self.subTest("{} ({})".format(tokenizer.__class__.__name__, pretrained_name)):
|
||||
tokenizer_r = self.rust_tokenizer_class.from_pretrained(pretrained_name, **kwargs)
|
||||
tokenizer_p = self.tokenizer_class.from_pretrained(pretrained_name, **kwargs)
|
||||
sentence = "A, <mask> AllenNLP sentence."
|
||||
tokens_r = tokenizer_r.encode_plus(sentence, add_special_tokens=True, return_token_type_ids=True)
|
||||
tokens_p = tokenizer_p.encode_plus(sentence, add_special_tokens=True, return_token_type_ids=True)
|
||||
|
||||
# token_type_ids should put 0 everywhere
|
||||
self.assertEqual(sum(tokens_r["token_type_ids"]), sum(tokens_p["token_type_ids"]))
|
||||
|
||||
# token_type_ids should put 0 everywhere
|
||||
self.assertEqual(sum(tokens_r["token_type_ids"]), sum(tokens_p["token_type_ids"]))
|
||||
|
||||
# attention_mask should put 1 everywhere, so sum over length should be 1
|
||||
self.assertEqual(
|
||||
sum(tokens_p["attention_mask"]) / len(tokens_p["attention_mask"]),
|
||||
)
|
||||
|
||||
tokens_p_str = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"])
|
||||
|
||||
# Rust correctly handles the space before the mask while python doesnt
|
||||
self.assertSequenceEqual(tokens_p["input_ids"], [0, 250, 6, 50264, 3823, 487, 21992, 3645, 4, 2])
|
||||
|
||||
self.assertSequenceEqual(
|
||||
tokens_p_str, ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"]
|
||||
)
|
||||
|
||||
|
||||
@require_torch
|
||||
class LukeTokenizerIntegrationTests(unittest.TestCase):
|
||||
tokenizer_class = LukeTokenizer
|
||||
from_pretrained_kwargs = {"cls_token": "<s>"}
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
def test_single_text_no_padding_or_truncation(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base", return_token_type_ids=True)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck."
|
||||
entities = ["Ana Ivanovic", "Thursday", "Dummy Entity"]
|
||||
spans = [(9, 21), (30, 38), (39, 42)]
|
||||
|
||||
encoding = tokenizer(sentence, entities=entities, entity_spans=spans, return_token_type_ids=True)
|
||||
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"], spaces_between_special_tokens=False),
|
||||
"<s>Top seed Ana Ivanovic said on Thursday she could hardly believe her luck.</s>",
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][3:6], spaces_between_special_tokens=False), " Ana Ivanovic"
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][8:9], spaces_between_special_tokens=False), " Thursday"
|
||||
)
|
||||
self.assertEqual(tokenizer.decode(encoding["input_ids"][9:10], spaces_between_special_tokens=False), " she")
|
||||
|
||||
self.assertEqual(
|
||||
encoding["entity_ids"],
|
||||
[
|
||||
tokenizer.entity_vocab["Ana Ivanovic"],
|
||||
tokenizer.entity_vocab["Thursday"],
|
||||
tokenizer.entity_vocab["[UNK]"],
|
||||
],
|
||||
)
|
||||
self.assertEqual(encoding["entity_attention_mask"], [1, 1, 1])
|
||||
self.assertEqual(encoding["entity_token_type_ids"], [0, 0, 0])
|
||||
# fmt: off
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"],
|
||||
[
|
||||
[3, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
]
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
def test_single_text_only_entity_spans_no_padding_or_truncation(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base", return_token_type_ids=True)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck."
|
||||
spans = [(9, 21), (30, 38), (39, 42)]
|
||||
|
||||
encoding = tokenizer(sentence, entity_spans=spans, return_token_type_ids=True)
|
||||
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"], spaces_between_special_tokens=False),
|
||||
"<s>Top seed Ana Ivanovic said on Thursday she could hardly believe her luck.</s>",
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][3:6], spaces_between_special_tokens=False), " Ana Ivanovic"
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][8:9], spaces_between_special_tokens=False), " Thursday"
|
||||
)
|
||||
self.assertEqual(tokenizer.decode(encoding["input_ids"][9:10], spaces_between_special_tokens=False), " she")
|
||||
|
||||
mask_id = tokenizer.entity_vocab["[MASK]"]
|
||||
self.assertEqual(encoding["entity_ids"], [mask_id, mask_id, mask_id])
|
||||
self.assertEqual(encoding["entity_attention_mask"], [1, 1, 1])
|
||||
self.assertEqual(encoding["entity_token_type_ids"], [0, 0, 0])
|
||||
# fmt: off
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"],
|
||||
[
|
||||
[3, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, ],
|
||||
[9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, ]
|
||||
]
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
def test_single_text_padding_pytorch_tensors(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base", return_token_type_ids=True)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck."
|
||||
entities = ["Ana Ivanovic", "Thursday", "Dummy Entity"]
|
||||
spans = [(9, 21), (30, 38), (39, 42)]
|
||||
|
||||
encoding = tokenizer(
|
||||
sentence,
|
||||
entities=entities,
|
||||
entity_spans=spans,
|
||||
return_token_type_ids=True,
|
||||
padding="max_length",
|
||||
max_length=30,
|
||||
max_entity_length=16,
|
||||
return_tensors="pt",
|
||||
)
|
||||
|
||||
# test words
|
||||
self.assertEqual(encoding["input_ids"].shape, (1, 30))
|
||||
self.assertEqual(encoding["attention_mask"].shape, (1, 30))
|
||||
self.assertEqual(encoding["token_type_ids"].shape, (1, 30))
|
||||
|
||||
# test entities
|
||||
self.assertEqual(encoding["entity_ids"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_attention_mask"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_token_type_ids"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_position_ids"].shape, (1, 16, tokenizer.max_mention_length))
|
||||
|
||||
def test_text_pair_no_padding_or_truncation(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base", return_token_type_ids=True)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday"
|
||||
sentence_pair = "She could hardly believe her luck."
|
||||
entities = ["Ana Ivanovic", "Thursday"]
|
||||
entities_pair = ["Dummy Entity"]
|
||||
spans = [(9, 21), (30, 38)]
|
||||
spans_pair = [(0, 3)]
|
||||
|
||||
encoding = tokenizer(
|
||||
sentence,
|
||||
sentence_pair,
|
||||
entities=entities,
|
||||
entities_pair=entities_pair,
|
||||
entity_spans=spans,
|
||||
entity_spans_pair=spans_pair,
|
||||
return_token_type_ids=True,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"], spaces_between_special_tokens=False),
|
||||
"<s>Top seed Ana Ivanovic said on Thursday</s></s>She could hardly believe her luck.</s>",
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][3:6], spaces_between_special_tokens=False), " Ana Ivanovic"
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][8:9], spaces_between_special_tokens=False), " Thursday"
|
||||
)
|
||||
self.assertEqual(tokenizer.decode(encoding["input_ids"][11:12], spaces_between_special_tokens=False), "She")
|
||||
|
||||
self.assertEqual(
|
||||
encoding["entity_ids"],
|
||||
[
|
||||
tokenizer.entity_vocab["Ana Ivanovic"],
|
||||
tokenizer.entity_vocab["Thursday"],
|
||||
tokenizer.entity_vocab["[UNK]"],
|
||||
],
|
||||
)
|
||||
self.assertEqual(encoding["entity_attention_mask"], [1, 1, 1])
|
||||
self.assertEqual(encoding["entity_token_type_ids"], [0, 0, 0])
|
||||
# fmt: off
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"],
|
||||
[
|
||||
[3, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
]
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
def test_text_pair_only_entity_spans_no_padding_or_truncation(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base", return_token_type_ids=True)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday"
|
||||
sentence_pair = "She could hardly believe her luck."
|
||||
spans = [(9, 21), (30, 38)]
|
||||
spans_pair = [(0, 3)]
|
||||
|
||||
encoding = tokenizer(
|
||||
sentence,
|
||||
sentence_pair,
|
||||
entity_spans=spans,
|
||||
entity_spans_pair=spans_pair,
|
||||
return_token_type_ids=True,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"], spaces_between_special_tokens=False),
|
||||
"<s>Top seed Ana Ivanovic said on Thursday</s></s>She could hardly believe her luck.</s>",
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][3:6], spaces_between_special_tokens=False), " Ana Ivanovic"
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][8:9], spaces_between_special_tokens=False), " Thursday"
|
||||
)
|
||||
self.assertEqual(tokenizer.decode(encoding["input_ids"][11:12], spaces_between_special_tokens=False), "She")
|
||||
|
||||
mask_id = tokenizer.entity_vocab["[MASK]"]
|
||||
self.assertEqual(encoding["entity_ids"], [mask_id, mask_id, mask_id])
|
||||
self.assertEqual(encoding["entity_attention_mask"], [1, 1, 1])
|
||||
self.assertEqual(encoding["entity_token_type_ids"], [0, 0, 0])
|
||||
# fmt: off
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"],
|
||||
[
|
||||
[3, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
]
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
def test_text_pair_padding_pytorch_tensors(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base", return_token_type_ids=True)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday"
|
||||
sentence_pair = "She could hardly believe her luck."
|
||||
entities = ["Ana Ivanovic", "Thursday"]
|
||||
entities_pair = ["Dummy Entity"]
|
||||
spans = [(9, 21), (30, 38)]
|
||||
spans_pair = [(0, 3)]
|
||||
|
||||
encoding = tokenizer(
|
||||
sentence,
|
||||
sentence_pair,
|
||||
entities=entities,
|
||||
entities_pair=entities_pair,
|
||||
entity_spans=spans,
|
||||
entity_spans_pair=spans_pair,
|
||||
return_token_type_ids=True,
|
||||
padding="max_length",
|
||||
max_length=30,
|
||||
max_entity_length=16,
|
||||
return_tensors="pt",
|
||||
)
|
||||
|
||||
# test words
|
||||
self.assertEqual(encoding["input_ids"].shape, (1, 30))
|
||||
self.assertEqual(encoding["attention_mask"].shape, (1, 30))
|
||||
self.assertEqual(encoding["token_type_ids"].shape, (1, 30))
|
||||
|
||||
# test entities
|
||||
self.assertEqual(encoding["entity_ids"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_attention_mask"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_token_type_ids"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_position_ids"].shape, (1, 16, tokenizer.max_mention_length))
|
||||
|
||||
def test_entity_classification_no_padding_or_truncation(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base", task="entity_classification")
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck as a fortuitous netcord helped the new world number one avoid a humiliating second- round exit at Wimbledon ."
|
||||
span = (39, 42)
|
||||
|
||||
encoding = tokenizer(sentence, entity_spans=[span], return_token_type_ids=True)
|
||||
|
||||
# test words
|
||||
self.assertEqual(len(encoding["input_ids"]), 42)
|
||||
self.assertEqual(len(encoding["attention_mask"]), 42)
|
||||
self.assertEqual(len(encoding["token_type_ids"]), 42)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"], spaces_between_special_tokens=False),
|
||||
"<s>Top seed Ana Ivanovic said on Thursday<ent> she<ent> could hardly believe her luck as a fortuitous netcord helped the new world number one avoid a humiliating second- round exit at Wimbledon.</s>",
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][9:12], spaces_between_special_tokens=False), "<ent> she<ent>"
|
||||
)
|
||||
|
||||
# test entities
|
||||
self.assertEqual(encoding["entity_ids"], [2])
|
||||
self.assertEqual(encoding["entity_attention_mask"], [1])
|
||||
self.assertEqual(encoding["entity_token_type_ids"], [0])
|
||||
# fmt: off
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"],
|
||||
[
|
||||
[9, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
|
||||
]
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
def test_entity_classification_padding_pytorch_tensors(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained(
|
||||
"studio-ousia/luke-base", task="entity_classification", return_token_type_ids=True
|
||||
)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck as a fortuitous netcord helped the new world number one avoid a humiliating second- round exit at Wimbledon ."
|
||||
# entity information
|
||||
span = (39, 42)
|
||||
|
||||
encoding = tokenizer(
|
||||
sentence, entity_spans=[span], return_token_type_ids=True, padding="max_length", return_tensors="pt"
|
||||
)
|
||||
|
||||
# test words
|
||||
self.assertEqual(encoding["input_ids"].shape, (1, 512))
|
||||
self.assertEqual(encoding["attention_mask"].shape, (1, 512))
|
||||
self.assertEqual(encoding["token_type_ids"].shape, (1, 512))
|
||||
|
||||
# test entities
|
||||
self.assertEqual(encoding["entity_ids"].shape, (1, 1))
|
||||
self.assertEqual(encoding["entity_attention_mask"].shape, (1, 1))
|
||||
self.assertEqual(encoding["entity_token_type_ids"].shape, (1, 1))
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"].shape, (1, tokenizer.max_entity_length, tokenizer.max_mention_length)
|
||||
)
|
||||
|
||||
def test_entity_pair_classification_no_padding_or_truncation(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained(
|
||||
"studio-ousia/luke-base", task="entity_pair_classification", return_token_type_ids=True
|
||||
)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck."
|
||||
# head and tail information
|
||||
spans = [(9, 21), (39, 42)]
|
||||
|
||||
encoding = tokenizer(sentence, entity_spans=spans, return_token_type_ids=True)
|
||||
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"], spaces_between_special_tokens=False),
|
||||
"<s>Top seed<ent> Ana Ivanovic<ent> said on Thursday<ent2> she<ent2> could hardly believe her luck.</s>",
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][3:8], spaces_between_special_tokens=False),
|
||||
"<ent> Ana Ivanovic<ent>",
|
||||
)
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"][11:14], spaces_between_special_tokens=False), "<ent2> she<ent2>"
|
||||
)
|
||||
|
||||
self.assertEqual(encoding["entity_ids"], [2, 3])
|
||||
self.assertEqual(encoding["entity_attention_mask"], [1, 1])
|
||||
self.assertEqual(encoding["entity_token_type_ids"], [0, 0])
|
||||
# fmt: off
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"],
|
||||
[
|
||||
[3, 4, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[11, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
]
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
def test_entity_pair_classification_padding_pytorch_tensors(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained(
|
||||
"studio-ousia/luke-base", task="entity_pair_classification", return_token_type_ids=True
|
||||
)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck."
|
||||
# head and tail information
|
||||
spans = [(9, 21), (39, 42)]
|
||||
|
||||
encoding = tokenizer(
|
||||
sentence,
|
||||
entity_spans=spans,
|
||||
return_token_type_ids=True,
|
||||
padding="max_length",
|
||||
max_length=30,
|
||||
return_tensors="pt",
|
||||
)
|
||||
|
||||
# test words
|
||||
self.assertEqual(encoding["input_ids"].shape, (1, 30))
|
||||
self.assertEqual(encoding["attention_mask"].shape, (1, 30))
|
||||
self.assertEqual(encoding["token_type_ids"].shape, (1, 30))
|
||||
|
||||
# test entities
|
||||
self.assertEqual(encoding["entity_ids"].shape, (1, 2))
|
||||
self.assertEqual(encoding["entity_attention_mask"].shape, (1, 2))
|
||||
self.assertEqual(encoding["entity_token_type_ids"].shape, (1, 2))
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"].shape, (1, tokenizer.max_entity_length, tokenizer.max_mention_length)
|
||||
)
|
||||
|
||||
def test_entity_span_classification_no_padding_or_truncation(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained(
|
||||
"studio-ousia/luke-base", task="entity_span_classification", return_token_type_ids=True
|
||||
)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck."
|
||||
spans = [(0, 8), (9, 21), (39, 42)]
|
||||
|
||||
encoding = tokenizer(sentence, entity_spans=spans, return_token_type_ids=True)
|
||||
|
||||
self.assertEqual(
|
||||
tokenizer.decode(encoding["input_ids"], spaces_between_special_tokens=False),
|
||||
"<s>Top seed Ana Ivanovic said on Thursday she could hardly believe her luck.</s>",
|
||||
)
|
||||
|
||||
self.assertEqual(encoding["entity_ids"], [2, 2, 2])
|
||||
self.assertEqual(encoding["entity_attention_mask"], [1, 1, 1])
|
||||
self.assertEqual(encoding["entity_token_type_ids"], [0, 0, 0])
|
||||
# fmt: off
|
||||
self.assertEqual(
|
||||
encoding["entity_position_ids"],
|
||||
[
|
||||
[1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[3, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
[9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
|
||||
]
|
||||
)
|
||||
# fmt: on
|
||||
self.assertEqual(encoding["entity_start_positions"], [1, 3, 9])
|
||||
self.assertEqual(encoding["entity_end_positions"], [2, 5, 9])
|
||||
|
||||
def test_entity_span_classification_padding_pytorch_tensors(self):
|
||||
tokenizer = LukeTokenizer.from_pretrained(
|
||||
"studio-ousia/luke-base", task="entity_span_classification", return_token_type_ids=True
|
||||
)
|
||||
sentence = "Top seed Ana Ivanovic said on Thursday she could hardly believe her luck."
|
||||
spans = [(0, 8), (9, 21), (39, 42)]
|
||||
|
||||
encoding = tokenizer(
|
||||
sentence,
|
||||
entity_spans=spans,
|
||||
return_token_type_ids=True,
|
||||
padding="max_length",
|
||||
max_length=30,
|
||||
max_entity_length=16,
|
||||
return_tensors="pt",
|
||||
)
|
||||
|
||||
# test words
|
||||
self.assertEqual(encoding["input_ids"].shape, (1, 30))
|
||||
self.assertEqual(encoding["attention_mask"].shape, (1, 30))
|
||||
self.assertEqual(encoding["token_type_ids"].shape, (1, 30))
|
||||
|
||||
# test entities
|
||||
self.assertEqual(encoding["entity_ids"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_attention_mask"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_token_type_ids"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_position_ids"].shape, (1, 16, tokenizer.max_mention_length))
|
||||
self.assertEqual(encoding["entity_start_positions"].shape, (1, 16))
|
||||
self.assertEqual(encoding["entity_end_positions"].shape, (1, 16))
|
||||
@@ -89,6 +89,9 @@ IGNORE_NON_AUTO_CONFIGURED = [
|
||||
"DPRSpanPredictor",
|
||||
"FlaubertForQuestionAnswering",
|
||||
"GPT2DoubleHeadsModel",
|
||||
"LukeForEntityClassification",
|
||||
"LukeForEntityPairClassification",
|
||||
"LukeForEntitySpanClassification",
|
||||
"OpenAIGPTDoubleHeadsModel",
|
||||
"RagModel",
|
||||
"RagSequenceForGeneration",
|
||||
|
||||
Reference in New Issue
Block a user