From d0cd858334a8693a4a0ca87492a2ccb1cced312e Mon Sep 17 00:00:00 2001 From: Niels Rogge Date: Mon, 16 Aug 2021 15:14:12 +0200 Subject: [PATCH] Add head models to model mappings --- src/transformers/models/auto/modeling_auto.py | 376 +----------------- .../models/layoutlmv2/test_fast_tokenizer.py | 25 -- tests/test_modeling_layoutlmv2.py | 4 +- 3 files changed, 6 insertions(+), 399 deletions(-) delete mode 100644 src/transformers/models/layoutlmv2/test_fast_tokenizer.py diff --git a/src/transformers/models/auto/modeling_auto.py b/src/transformers/models/auto/modeling_auto.py index ae4fdcb086e5a0..784a604c5fd5d0 100644 --- a/src/transformers/models/auto/modeling_auto.py +++ b/src/transformers/models/auto/modeling_auto.py @@ -18,381 +18,8 @@ from collections import OrderedDict from ...utils import logging -<<<<<<< HEAD from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES -======= - -# Add modeling imports here -from ..albert.modeling_albert import ( - AlbertForMaskedLM, - AlbertForMultipleChoice, - AlbertForPreTraining, - AlbertForQuestionAnswering, - AlbertForSequenceClassification, - AlbertForTokenClassification, - AlbertModel, -) -from ..bart.modeling_bart import ( - BartForCausalLM, - BartForConditionalGeneration, - BartForQuestionAnswering, - BartForSequenceClassification, - BartModel, -) -from ..beit.modeling_beit import BeitForImageClassification, BeitModel -from ..bert.modeling_bert import ( - BertForMaskedLM, - BertForMultipleChoice, - BertForNextSentencePrediction, - BertForPreTraining, - BertForQuestionAnswering, - BertForSequenceClassification, - BertForTokenClassification, - BertLMHeadModel, - BertModel, -) -from ..bert_generation.modeling_bert_generation import BertGenerationDecoder, BertGenerationEncoder -from ..big_bird.modeling_big_bird import ( - BigBirdForCausalLM, - BigBirdForMaskedLM, - BigBirdForMultipleChoice, - BigBirdForPreTraining, - BigBirdForQuestionAnswering, - BigBirdForSequenceClassification, - BigBirdForTokenClassification, - BigBirdModel, -) -from ..bigbird_pegasus.modeling_bigbird_pegasus import ( - BigBirdPegasusForCausalLM, - BigBirdPegasusForConditionalGeneration, - BigBirdPegasusForQuestionAnswering, - BigBirdPegasusForSequenceClassification, - BigBirdPegasusModel, -) -from ..blenderbot.modeling_blenderbot import BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel -from ..blenderbot_small.modeling_blenderbot_small import ( - BlenderbotSmallForCausalLM, - BlenderbotSmallForConditionalGeneration, - BlenderbotSmallModel, -) -from ..camembert.modeling_camembert import ( - CamembertForCausalLM, - CamembertForMaskedLM, - CamembertForMultipleChoice, - CamembertForQuestionAnswering, - CamembertForSequenceClassification, - CamembertForTokenClassification, - CamembertModel, -) -from ..canine.modeling_canine import ( - CanineForMultipleChoice, - CanineForQuestionAnswering, - CanineForSequenceClassification, - CanineForTokenClassification, - CanineModel, -) -from ..clip.modeling_clip import CLIPModel -from ..convbert.modeling_convbert import ( - ConvBertForMaskedLM, - ConvBertForMultipleChoice, - ConvBertForQuestionAnswering, - ConvBertForSequenceClassification, - ConvBertForTokenClassification, - ConvBertModel, -) -from ..ctrl.modeling_ctrl import CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel -from ..deberta.modeling_deberta import ( - DebertaForMaskedLM, - DebertaForQuestionAnswering, - DebertaForSequenceClassification, - DebertaForTokenClassification, - DebertaModel, -) -from ..deberta_v2.modeling_deberta_v2 import ( - DebertaV2ForMaskedLM, - DebertaV2ForQuestionAnswering, - DebertaV2ForSequenceClassification, - DebertaV2ForTokenClassification, - DebertaV2Model, -) -from ..deit.modeling_deit import DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTModel -from ..detr.modeling_detr import DetrForObjectDetection, DetrModel -from ..distilbert.modeling_distilbert import ( - DistilBertForMaskedLM, - DistilBertForMultipleChoice, - DistilBertForQuestionAnswering, - DistilBertForSequenceClassification, - DistilBertForTokenClassification, - DistilBertModel, -) -from ..dpr.modeling_dpr import DPRQuestionEncoder -from ..electra.modeling_electra import ( - ElectraForMaskedLM, - ElectraForMultipleChoice, - ElectraForPreTraining, - ElectraForQuestionAnswering, - ElectraForSequenceClassification, - ElectraForTokenClassification, - ElectraModel, -) -from ..encoder_decoder.modeling_encoder_decoder import EncoderDecoderModel -from ..flaubert.modeling_flaubert import ( - FlaubertForMultipleChoice, - FlaubertForQuestionAnsweringSimple, - FlaubertForSequenceClassification, - FlaubertForTokenClassification, - FlaubertModel, - FlaubertWithLMHeadModel, -) -from ..fsmt.modeling_fsmt import FSMTForConditionalGeneration, FSMTModel -from ..funnel.modeling_funnel import ( - FunnelBaseModel, - FunnelForMaskedLM, - FunnelForMultipleChoice, - FunnelForPreTraining, - FunnelForQuestionAnswering, - FunnelForSequenceClassification, - FunnelForTokenClassification, - FunnelModel, -) -from ..gpt2.modeling_gpt2 import GPT2ForSequenceClassification, GPT2LMHeadModel, GPT2Model -from ..gpt_neo.modeling_gpt_neo import GPTNeoForCausalLM, GPTNeoForSequenceClassification, GPTNeoModel -from ..hubert.modeling_hubert import HubertModel -from ..ibert.modeling_ibert import ( - IBertForMaskedLM, - IBertForMultipleChoice, - IBertForQuestionAnswering, - IBertForSequenceClassification, - IBertForTokenClassification, - IBertModel, -) -from ..layoutlm.modeling_layoutlm import ( - LayoutLMForMaskedLM, - LayoutLMForSequenceClassification, - LayoutLMForTokenClassification, - LayoutLMModel, -) -from ..layoutlmv2.modeling_layoutlmv2 import ( - LayoutLMv2ForQuestionAnswering, - LayoutLMv2ForSequenceClassification, - LayoutLMv2ForTokenClassification, - LayoutLMv2Model, -) -from ..led.modeling_led import ( - LEDForConditionalGeneration, - LEDForQuestionAnswering, - LEDForSequenceClassification, - LEDModel, -) -from ..longformer.modeling_longformer import ( - LongformerForMaskedLM, - LongformerForMultipleChoice, - LongformerForQuestionAnswering, - LongformerForSequenceClassification, - 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 -from ..mbart.modeling_mbart import ( - MBartForCausalLM, - MBartForConditionalGeneration, - MBartForQuestionAnswering, - MBartForSequenceClassification, - MBartModel, -) -from ..megatron_bert.modeling_megatron_bert import ( - MegatronBertForCausalLM, - MegatronBertForMaskedLM, - MegatronBertForMultipleChoice, - MegatronBertForNextSentencePrediction, - MegatronBertForPreTraining, - MegatronBertForQuestionAnswering, - MegatronBertForSequenceClassification, - MegatronBertForTokenClassification, - MegatronBertModel, -) -from ..mobilebert.modeling_mobilebert import ( - MobileBertForMaskedLM, - MobileBertForMultipleChoice, - MobileBertForNextSentencePrediction, - MobileBertForPreTraining, - MobileBertForQuestionAnswering, - MobileBertForSequenceClassification, - MobileBertForTokenClassification, - MobileBertModel, -) -from ..mpnet.modeling_mpnet import ( - MPNetForMaskedLM, - MPNetForMultipleChoice, - MPNetForQuestionAnswering, - MPNetForSequenceClassification, - MPNetForTokenClassification, - MPNetModel, -) -from ..mt5.modeling_mt5 import MT5ForConditionalGeneration, MT5Model -from ..openai.modeling_openai import OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel -from ..pegasus.modeling_pegasus import PegasusForCausalLM, PegasusForConditionalGeneration, PegasusModel -from ..prophetnet.modeling_prophetnet import ProphetNetForCausalLM, ProphetNetForConditionalGeneration, ProphetNetModel -from ..rag.modeling_rag import ( # noqa: F401 - need to import all RagModels to be in globals() function - RagModel, - RagSequenceForGeneration, - RagTokenForGeneration, -) -from ..reformer.modeling_reformer import ( - ReformerForMaskedLM, - ReformerForQuestionAnswering, - ReformerForSequenceClassification, - ReformerModel, - ReformerModelWithLMHead, -) -from ..rembert.modeling_rembert import ( - RemBertForCausalLM, - RemBertForMaskedLM, - RemBertForMultipleChoice, - RemBertForQuestionAnswering, - RemBertForSequenceClassification, - RemBertForTokenClassification, - RemBertModel, -) -from ..retribert.modeling_retribert import RetriBertModel -from ..roberta.modeling_roberta import ( - RobertaForCausalLM, - RobertaForMaskedLM, - RobertaForMultipleChoice, - RobertaForQuestionAnswering, - RobertaForSequenceClassification, - RobertaForTokenClassification, - RobertaModel, -) -from ..roformer.modeling_roformer import ( - RoFormerForCausalLM, - RoFormerForMaskedLM, - RoFormerForMultipleChoice, - RoFormerForQuestionAnswering, - RoFormerForSequenceClassification, - RoFormerForTokenClassification, - RoFormerModel, -) -from ..speech_to_text.modeling_speech_to_text import Speech2TextForConditionalGeneration, Speech2TextModel -from ..squeezebert.modeling_squeezebert import ( - SqueezeBertForMaskedLM, - SqueezeBertForMultipleChoice, - SqueezeBertForQuestionAnswering, - SqueezeBertForSequenceClassification, - SqueezeBertForTokenClassification, - SqueezeBertModel, -) -from ..t5.modeling_t5 import T5ForConditionalGeneration, T5Model -from ..tapas.modeling_tapas import ( - TapasForMaskedLM, - TapasForQuestionAnswering, - TapasForSequenceClassification, - TapasModel, -) -from ..transfo_xl.modeling_transfo_xl import TransfoXLForSequenceClassification, TransfoXLLMHeadModel, TransfoXLModel -from ..visual_bert.modeling_visual_bert import VisualBertForPreTraining, VisualBertModel -from ..vit.modeling_vit import ViTForImageClassification, ViTModel -from ..wav2vec2.modeling_wav2vec2 import Wav2Vec2ForMaskedLM, Wav2Vec2ForPreTraining, Wav2Vec2Model -from ..xlm.modeling_xlm import ( - XLMForMultipleChoice, - XLMForQuestionAnsweringSimple, - XLMForSequenceClassification, - XLMForTokenClassification, - XLMModel, - XLMWithLMHeadModel, -) -from ..xlm_prophetnet.modeling_xlm_prophetnet import ( - XLMProphetNetForCausalLM, - XLMProphetNetForConditionalGeneration, - XLMProphetNetModel, -) -from ..xlm_roberta.modeling_xlm_roberta import ( - XLMRobertaForCausalLM, - XLMRobertaForMaskedLM, - XLMRobertaForMultipleChoice, - XLMRobertaForQuestionAnswering, - XLMRobertaForSequenceClassification, - XLMRobertaForTokenClassification, - XLMRobertaModel, -) -from ..xlnet.modeling_xlnet import ( - XLNetForMultipleChoice, - XLNetForQuestionAnsweringSimple, - XLNetForSequenceClassification, - XLNetForTokenClassification, - XLNetLMHeadModel, - XLNetModel, -) -from .auto_factory import _BaseAutoModelClass, auto_class_update -from .configuration_auto import ( - AlbertConfig, - BartConfig, - BeitConfig, - BertConfig, - BertGenerationConfig, - BigBirdConfig, - BigBirdPegasusConfig, - BlenderbotConfig, - BlenderbotSmallConfig, - CamembertConfig, - CanineConfig, - CLIPConfig, - ConvBertConfig, - CTRLConfig, - DebertaConfig, - DebertaV2Config, - DeiTConfig, - DetrConfig, - DistilBertConfig, - DPRConfig, - ElectraConfig, - EncoderDecoderConfig, - FlaubertConfig, - FSMTConfig, - FunnelConfig, - GPT2Config, - GPTNeoConfig, - HubertConfig, - IBertConfig, - LayoutLMConfig, - LayoutLMv2Config, - LEDConfig, - LongformerConfig, - LukeConfig, - LxmertConfig, - M2M100Config, - MarianConfig, - MBartConfig, - MegatronBertConfig, - MobileBertConfig, - MPNetConfig, - MT5Config, - OpenAIGPTConfig, - PegasusConfig, - ProphetNetConfig, - ReformerConfig, - RemBertConfig, - RetriBertConfig, - RobertaConfig, - RoFormerConfig, - Speech2TextConfig, - SqueezeBertConfig, - T5Config, - TapasConfig, - TransfoXLConfig, - VisualBertConfig, - ViTConfig, - Wav2Vec2Config, - XLMConfig, - XLMProphetNetConfig, - XLMRobertaConfig, - XLNetConfig, -) ->>>>>>> More improvements, add documentation logger = logging.get_logger(__name__) @@ -658,6 +285,7 @@ MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = OrderedDict( [ # Model for Sequence Classification mapping + ("layoutlmv2", "LayoutLMv2ForSequenceClassification"), ("rembert", "RemBertForSequenceClassification"), ("canine", "CanineForSequenceClassification"), ("roformer", "RoFormerForSequenceClassification"), @@ -700,6 +328,7 @@ MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = OrderedDict( [ # Model for Question Answering mapping + ("layoutlmv2", "LayoutLMv2ForQuestionAnswering"), ("rembert", "RemBertForQuestionAnswering"), ("canine", "CanineForQuestionAnswering"), ("roformer", "RoFormerForQuestionAnswering"), @@ -743,6 +372,7 @@ MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = OrderedDict( [ # Model for Token Classification mapping + ("layoutlmv2", "LayoutLMv2ForTokenClassification"), ("rembert", "RemBertForTokenClassification"), ("canine", "CanineForTokenClassification"), ("roformer", "RoFormerForTokenClassification"), diff --git a/src/transformers/models/layoutlmv2/test_fast_tokenizer.py b/src/transformers/models/layoutlmv2/test_fast_tokenizer.py deleted file mode 100644 index cfc48b3d6e2146..00000000000000 --- a/src/transformers/models/layoutlmv2/test_fast_tokenizer.py +++ /dev/null @@ -1,25 +0,0 @@ -from transformers import LayoutLMv2TokenizerFast - -tokenizer = LayoutLMv2TokenizerFast.from_pretrained("microsoft/layoutlmv2-base-uncased") - -# test non batched -text = ["hello", "my", "name", "is", "Niels"] -boxes = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]] - -encoding = tokenizer(text, boxes=boxes, padding=True, max_length=6, truncation=True, return_overflowing_tokens=True) - -print(encoding.overflow_to_sample_mapping) -print(tokenizer.decode(encoding.input_ids[0])) -print(encoding.bbox[0]) -print(tokenizer.decode(encoding.input_ids[1])) -print(encoding.bbox[1]) - -for k,v in encoding.items(): - print(k, len(v)) - -# test batched -text = [["hello", "my", "name", "is", "Niels"], ["gent"]] -boxes = [[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]], [[1, 2, 3, 4]]] - -encoding = tokenizer(text, boxes=boxes) - diff --git a/tests/test_modeling_layoutlmv2.py b/tests/test_modeling_layoutlmv2.py index 66451c4984fca4..d13a5a8b14bf8d 100644 --- a/tests/test_modeling_layoutlmv2.py +++ b/tests/test_modeling_layoutlmv2.py @@ -19,6 +19,7 @@ from transformers.file_utils import is_detectron2_available, is_torch_available from transformers.testing_utils import require_detectron2, require_torch, slow, torch_device +from transformers.models.auto import get_values from .test_configuration_common import ConfigTester from .test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask @@ -33,6 +34,7 @@ LayoutLMv2ForSequenceClassification, LayoutLMv2ForTokenClassification, LayoutLMv2Model, + MODEL_MAPPING, ) from transformers.models.layoutlmv2.modeling_layoutlmv2 import LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST @@ -291,7 +293,7 @@ def test_for_token_classification(self): def test_for_question_answering(self): config_and_inputs = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*config_and_inputs) - + def test_attention_outputs(self): config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common() config.return_dict = True