'''simple docstring'''





import unittest

from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available
from transformers.pipelines import pipeline
from transformers.pipelines.document_question_answering import apply_tesseract
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_detectrona,
    require_pytesseract,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image

 from transformers.image_utils import load_image
else:






 class 				UpperCAmelCase :




     '''simple docstring'''







     @staticmethod
     def       _UpperCamelCase    (       *lowercase__    ,							**lowercase__ )							-> Optional[int]:
          pass






 def     __lowerCAmelCase						(       a_		)      ->	List[str]:





      '''simple docstring'''





      return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
_lowerCAmelCase  :Optional[Any]  				=    (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)






@is_pipeline_test
@require_torch
@require_vision
class 				UpperCAmelCase (       unittest.TestCase ):




    '''simple docstring'''







    snake_case__							:   Tuple							=						MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






    @require_pytesseract
    @require_vision
    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> Tuple:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       pipeline(
             'document-question-answering'    ,							model=lowercase__    ,							tokenizer=lowercase__    ,							image_processor=lowercase__ )

         SCREAMING_SNAKE_CASE					:			Dict    	=       INVOICE_URL
         SCREAMING_SNAKE_CASE					:			Dict    	=       list(zip(*apply_tesseract(load_image(lowercase__ )    ,							lowercase__    ,							'' ) ) )
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       'What is the placebo?'
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       [
             {
                 'image': load_image(lowercase__ ),
                 'question': question,
             },
             {
                 'image': image,
                 'question': question,
             },
             {
                 'image': image,
                 'question': question,
                 'word_boxes': word_boxes,
             },
         ]
         return dqa_pipeline, examples






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ )							-> str:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       dqa_pipeline(lowercase__    ,							top_k=2 )
         self.assertEqual(
             lowercase__    ,							[
                 [
                     {'score': ANY(lowercase__ ), 'answer': ANY(lowercase__ ), 'start': ANY(lowercase__ ), 'end': ANY(lowercase__ )},
                     {'score': ANY(lowercase__ ), 'answer': ANY(lowercase__ ), 'start': ANY(lowercase__ ), 'end': ANY(lowercase__ )},
                 ]
             ]
             * 3    ,							)






    @require_torch
    @require_detectrona
    @require_pytesseract
    def       _UpperCamelCase    (       self )							-> Dict:
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       pipeline('document-question-answering'    ,							model='hf-internal-testing/tiny-random-layoutlmv2' )
         SCREAMING_SNAKE_CASE					:			int    	=       INVOICE_URL
         SCREAMING_SNAKE_CASE					:			int    	=       'How many cats are there?'

         SCREAMING_SNAKE_CASE					:			str    	=       [
             {'score': 0.0_0_0_1, 'answer': 'oy 2312/2019', 'start': 38, 'end': 39},
             {'score': 0.0_0_0_1, 'answer': 'oy 2312/2019 DUE', 'start': 38, 'end': 40},
         ]
         SCREAMING_SNAKE_CASE					:			Dict    	=       dqa_pipeline(image=lowercase__    ,							question=lowercase__    ,							top_k=2 )
         self.assertEqual(nested_simplify(lowercase__    ,							decimals=4 )    ,							lowercase__ )

         SCREAMING_SNAKE_CASE					:			str    	=       dqa_pipeline({'image': image, 'question': question}    ,							top_k=2 )
         self.assertEqual(nested_simplify(lowercase__    ,							decimals=4 )    ,							lowercase__ )

         # This image does not detect ANY text in it, meaning layoutlmv2 should fail.
         # Empty answer probably
         SCREAMING_SNAKE_CASE					:			List[str]    	=       './tests/fixtures/tests_samples/COCO/000000039769.png'
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       dqa_pipeline(image=lowercase__    ,							question=lowercase__    ,							top_k=2 )
         self.assertEqual(lowercase__    ,							[] )

         # We can optionnally pass directly the words and bounding boxes
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       './tests/fixtures/tests_samples/COCO/000000039769.png'
         SCREAMING_SNAKE_CASE					:			Dict    	=       []
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       []
         SCREAMING_SNAKE_CASE					:			Tuple    	=       dqa_pipeline(image=lowercase__    ,							question=lowercase__    ,							words=lowercase__    ,							boxes=lowercase__    ,							top_k=2 )
         self.assertEqual(lowercase__    ,							[] )






    @slow
    @require_torch
    @require_detectrona
    @require_pytesseract
    def       _UpperCamelCase    (       self )							-> Tuple:
         SCREAMING_SNAKE_CASE					:			str    	=       pipeline(
             'document-question-answering'    ,							model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'    ,							revision='9977165'    ,							)
         SCREAMING_SNAKE_CASE					:			List[str]    	=       INVOICE_URL
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       'What is the invoice number?'

         SCREAMING_SNAKE_CASE					:			int    	=       dqa_pipeline(image=lowercase__    ,							question=lowercase__    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.9_9_4_4, 'answer': 'us-001', 'start': 16, 'end': 16},
                 {'score': 0.0_0_0_9, 'answer': 'us-001', 'start': 16, 'end': 16},
             ]    ,							)

         SCREAMING_SNAKE_CASE					:			Any    	=       dqa_pipeline({'image': image, 'question': question}    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.9_9_4_4, 'answer': 'us-001', 'start': 16, 'end': 16},
                 {'score': 0.0_0_0_9, 'answer': 'us-001', 'start': 16, 'end': 16},
             ]    ,							)

         SCREAMING_SNAKE_CASE					:			int    	=       dqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 [
                     {'score': 0.9_9_4_4, 'answer': 'us-001', 'start': 16, 'end': 16},
                     {'score': 0.0_0_0_9, 'answer': 'us-001', 'start': 16, 'end': 16},
                 ],
             ]
             * 2    ,							)






    @slow
    @require_torch
    @require_detectrona
    @require_pytesseract
    def       _UpperCamelCase    (       self )							-> Optional[Any]:
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       pipeline(
             'document-question-answering'    ,							model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'    ,							revision='9977165'    ,							max_seq_len=50    ,							)
         SCREAMING_SNAKE_CASE					:			str    	=       INVOICE_URL
         SCREAMING_SNAKE_CASE					:			Tuple    	=       'What is the invoice number?'

         SCREAMING_SNAKE_CASE					:			Tuple    	=       dqa_pipeline(image=lowercase__    ,							question=lowercase__    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.9_9_7_4, 'answer': '1110212019', 'start': 23, 'end': 23},
                 {'score': 0.9_9_4_8, 'answer': 'us-001', 'start': 16, 'end': 16},
             ]    ,							)

         SCREAMING_SNAKE_CASE					:			List[str]    	=       dqa_pipeline({'image': image, 'question': question}    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.9_9_7_4, 'answer': '1110212019', 'start': 23, 'end': 23},
                 {'score': 0.9_9_4_8, 'answer': 'us-001', 'start': 16, 'end': 16},
             ]    ,							)

         SCREAMING_SNAKE_CASE					:			Dict    	=       dqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 [
                     {'score': 0.9_9_7_4, 'answer': '1110212019', 'start': 23, 'end': 23},
                     {'score': 0.9_9_4_8, 'answer': 'us-001', 'start': 16, 'end': 16},
                 ]
             ]
             * 2    ,							)






    @slow
    @require_torch
    @require_pytesseract
    @require_vision
    def       _UpperCamelCase    (       self )							-> int:
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       AutoTokenizer.from_pretrained(
             'impira/layoutlm-document-qa'    ,							revision='3dc6de3'    ,							add_prefix_space=lowercase__ )
         SCREAMING_SNAKE_CASE					:			List[str]    	=       pipeline(
             'document-question-answering'    ,							model='impira/layoutlm-document-qa'    ,							tokenizer=lowercase__    ,							revision='3dc6de3'    ,							)
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       INVOICE_URL
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       'What is the invoice number?'

         SCREAMING_SNAKE_CASE					:			int    	=       dqa_pipeline(image=lowercase__    ,							question=lowercase__    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.4_2_5_1, 'answer': 'us-001', 'start': 16, 'end': 16},
                 {'score': 0.0_8_1_9, 'answer': '1110212019', 'start': 23, 'end': 23},
             ]    ,							)

         SCREAMING_SNAKE_CASE					:			List[str]    	=       dqa_pipeline({'image': image, 'question': question}    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.4_2_5_1, 'answer': 'us-001', 'start': 16, 'end': 16},
                 {'score': 0.0_8_1_9, 'answer': '1110212019', 'start': 23, 'end': 23},
             ]    ,							)

         SCREAMING_SNAKE_CASE					:			str    	=       dqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 [
                     {'score': 0.4_2_5_1, 'answer': 'us-001', 'start': 16, 'end': 16},
                     {'score': 0.0_8_1_9, 'answer': '1110212019', 'start': 23, 'end': 23},
                 ]
             ]
             * 2    ,							)

         SCREAMING_SNAKE_CASE					:			List[str]    	=       list(zip(*apply_tesseract(load_image(lowercase__ )    ,							lowercase__    ,							'' ) ) )

         # This model should also work if `image` is set to None
         SCREAMING_SNAKE_CASE					:			Dict    	=       dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.4_2_5_1, 'answer': 'us-001', 'start': 16, 'end': 16},
                 {'score': 0.0_8_1_9, 'answer': '1110212019', 'start': 23, 'end': 23},
             ]    ,							)






    @slow
    @require_torch
    @require_pytesseract
    @require_vision
    def       _UpperCamelCase    (       self )							-> Tuple:
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       AutoTokenizer.from_pretrained(
             'impira/layoutlm-document-qa'    ,							revision='3dc6de3'    ,							add_prefix_space=lowercase__ )
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       pipeline(
             'document-question-answering'    ,							model='impira/layoutlm-document-qa'    ,							tokenizer=lowercase__    ,							revision='3dc6de3'    ,							max_seq_len=50    ,							)
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       INVOICE_URL
         SCREAMING_SNAKE_CASE					:			int    	=       'What is the invoice number?'

         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       dqa_pipeline(image=lowercase__    ,							question=lowercase__    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.9_9_9_9, 'answer': 'us-001', 'start': 16, 'end': 16},
                 {'score': 0.9_9_9_8, 'answer': 'us-001', 'start': 16, 'end': 16},
             ]    ,							)

         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       dqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 [
                     {'score': 0.9_9_9_9, 'answer': 'us-001', 'start': 16, 'end': 16},
                     {'score': 0.9_9_9_8, 'answer': 'us-001', 'start': 16, 'end': 16},
                 ]
             ]
             * 2    ,							)

         SCREAMING_SNAKE_CASE					:			List[str]    	=       list(zip(*apply_tesseract(load_image(lowercase__ )    ,							lowercase__    ,							'' ) ) )

         # This model should also work if `image` is set to None
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}    ,							top_k=2 )
         self.assertEqual(
             nested_simplify(lowercase__    ,							decimals=4 )    ,							[
                 {'score': 0.9_9_9_9, 'answer': 'us-001', 'start': 16, 'end': 16},
                 {'score': 0.9_9_9_8, 'answer': 'us-001', 'start': 16, 'end': 16},
             ]    ,							)






    @slow
    @require_torch
    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       pipeline(
             'document-question-answering'    ,							model='naver-clova-ix/donut-base-finetuned-docvqa'    ,							tokenizer=AutoTokenizer.from_pretrained('naver-clova-ix/donut-base-finetuned-docvqa' )    ,							feature_extractor='naver-clova-ix/donut-base-finetuned-docvqa'    ,							)

         SCREAMING_SNAKE_CASE					:			Dict    	=       INVOICE_URL
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       'What is the invoice number?'
         SCREAMING_SNAKE_CASE					:			int    	=       dqa_pipeline(image=lowercase__    ,							question=lowercase__    ,							top_k=2 )
         self.assertEqual(nested_simplify(lowercase__    ,							decimals=4 )    ,							[{'answer': 'us-001'}] )






    @require_tf
    @unittest.skip('Document question answering not implemented in TF' )
    def       _UpperCamelCase    (       self )							-> List[str]:
         pass




'''simple docstring'''





import unittest

from transformers import MPNetConfig, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     MPNetForMaskedLM,
     MPNetForMultipleChoice,
     MPNetForQuestionAnswering,
     MPNetForSequenceClassification,
     MPNetForTokenClassification,
     MPNetModel,
 )






class 				UpperCAmelCase :




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__=13    ,							lowercase__=7    ,							lowercase__=True    ,							lowercase__=True    ,							lowercase__=False    ,							lowercase__=True    ,							lowercase__=99    ,							lowercase__=64    ,							lowercase__=5    ,							lowercase__=4    ,							lowercase__=64    ,							lowercase__="gelu"    ,							lowercase__=0.1    ,							lowercase__=0.1    ,							lowercase__=512    ,							lowercase__=16    ,							lowercase__=2    ,							lowercase__=0.0_2    ,							lowercase__=3    ,							lowercase__=4    ,							lowercase__=None    ,							)							-> int:
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       parent
         SCREAMING_SNAKE_CASE					:			int    	=       batch_size
         SCREAMING_SNAKE_CASE					:			Any    	=       seq_length
         SCREAMING_SNAKE_CASE					:			Any    	=       is_training
         SCREAMING_SNAKE_CASE					:			Any    	=       use_input_mask
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       use_token_type_ids
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       use_labels
         SCREAMING_SNAKE_CASE					:			int    	=       vocab_size
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       hidden_size
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       num_hidden_layers
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       num_attention_heads
         SCREAMING_SNAKE_CASE					:			Any    	=       intermediate_size
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       hidden_act
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       hidden_dropout_prob
         SCREAMING_SNAKE_CASE					:			Any    	=       attention_probs_dropout_prob
         SCREAMING_SNAKE_CASE					:			Tuple    	=       max_position_embeddings
         SCREAMING_SNAKE_CASE					:			Tuple    	=       type_vocab_size
         SCREAMING_SNAKE_CASE					:			List[str]    	=       type_sequence_label_size
         SCREAMING_SNAKE_CASE					:			int    	=       initializer_range
         SCREAMING_SNAKE_CASE					:			Dict    	=       num_labels
         SCREAMING_SNAKE_CASE					:			Dict    	=       num_choices
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       scope






    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         return MPNetConfig.from_pretrained('microsoft/mpnet-base' )






    def       _UpperCamelCase    (       self )							-> List[str]:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       ids_tensor([self.batch_size, self.seq_length]    ,							self.vocab_size )

         SCREAMING_SNAKE_CASE					:			str    	=       None
         if self.use_input_mask:
              SCREAMING_SNAKE_CASE					:			Dict    	=       random_attention_mask([self.batch_size, self.seq_length] )

         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       None
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       None
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       None
         if self.use_labels:
              SCREAMING_SNAKE_CASE					:			Dict    	=       ids_tensor([self.batch_size]    ,							self.type_sequence_label_size )
              SCREAMING_SNAKE_CASE					:			Dict    	=       ids_tensor([self.batch_size, self.seq_length]    ,							self.num_labels )
              SCREAMING_SNAKE_CASE					:			Tuple    	=       ids_tensor([self.batch_size]    ,							self.num_choices )

         SCREAMING_SNAKE_CASE					:			Tuple    	=       self.get_config()
         return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






    def       _UpperCamelCase    (       self )							-> Tuple:
         return MPNetConfig(
             vocab_size=self.vocab_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    ,							initializer_range=self.initializer_range    ,							)






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> List[str]:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       MPNetModel(config=lowercase__ )
         model.to(lowercase__ )
         model.eval()
         SCREAMING_SNAKE_CASE					:			List[str]    	=       model(lowercase__    ,							lowercase__ )
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       model(lowercase__ )
         self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size) )
         self.parent.assertEqual(result.pooler_output.shape    ,							(self.batch_size, self.hidden_size) )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			str    	=       MPNetForQuestionAnswering(config=lowercase__ )
         model.to(lowercase__ )
         model.eval()
         SCREAMING_SNAKE_CASE					:			Any    	=       model(
             lowercase__    ,							attention_mask=lowercase__    ,							start_positions=lowercase__    ,							end_positions=lowercase__    ,							)
         self.parent.assertEqual(result.start_logits.shape    ,							(self.batch_size, self.seq_length) )
         self.parent.assertEqual(result.end_logits.shape    ,							(self.batch_size, self.seq_length) )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			str    	=       self.num_labels
         SCREAMING_SNAKE_CASE					:			Tuple    	=       MPNetForSequenceClassification(lowercase__ )
         model.to(lowercase__ )
         model.eval()
         SCREAMING_SNAKE_CASE					:			List[str]    	=       model(lowercase__    ,							attention_mask=lowercase__    ,							labels=lowercase__ )
         self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_labels) )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       self.num_choices
         SCREAMING_SNAKE_CASE					:			Any    	=       MPNetForMultipleChoice(config=lowercase__ )
         model.to(lowercase__ )
         model.eval()
         SCREAMING_SNAKE_CASE					:			str    	=       input_ids.unsqueeze(1 ).expand(-1    ,							self.num_choices    ,							-1 ).contiguous()
         SCREAMING_SNAKE_CASE					:			List[str]    	=       input_mask.unsqueeze(1 ).expand(-1    ,							self.num_choices    ,							-1 ).contiguous()
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       model(
             lowercase__    ,							attention_mask=lowercase__    ,							labels=lowercase__    ,							)
         self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_choices) )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> List[str]:
         SCREAMING_SNAKE_CASE					:			str    	=       self.num_labels
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       MPNetForTokenClassification(config=lowercase__ )
         model.to(lowercase__ )
         model.eval()
         SCREAMING_SNAKE_CASE					:			Any    	=       model(lowercase__    ,							attention_mask=lowercase__    ,							labels=lowercase__ )
         self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.seq_length, self.num_labels) )






    def       _UpperCamelCase    (       self )							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			Any    	=       self.prepare_config_and_inputs()
         ((SCREAMING_SNAKE_CASE)	,   (SCREAMING_SNAKE_CASE)	,   (SCREAMING_SNAKE_CASE)	,   (SCREAMING_SNAKE_CASE)	,   (SCREAMING_SNAKE_CASE)	,   (SCREAMING_SNAKE_CASE))					:			str    	=       config_and_inputs
         SCREAMING_SNAKE_CASE					:			Dict    	=       {'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict






@require_torch
class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE   ,    _SCREAMING_SNAKE_CASE   ,    unittest.TestCase ):




    '''simple docstring'''







    snake_case__							:   Optional[int]							=						(
        (
            MPNetForMaskedLM,
            MPNetForMultipleChoice,
            MPNetForQuestionAnswering,
            MPNetForSequenceClassification,
            MPNetForTokenClassification,
            MPNetModel,
        )
        if is_torch_available()
        else ()
    )
    snake_case__							:   Optional[int]							=						(
        {
            "feature-extraction": MPNetModel,
            "fill-mask": MPNetForMaskedLM,
            "question-answering": MPNetForQuestionAnswering,
            "text-classification": MPNetForSequenceClassification,
            "token-classification": MPNetForTokenClassification,
            "zero-shot": MPNetForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    snake_case__							:   List[str]							=						False
    snake_case__							:   int							=						True






    def       _UpperCamelCase    (       self )							-> Dict:
         SCREAMING_SNAKE_CASE					:			int    	=       MPNetModelTester(self )
         SCREAMING_SNAKE_CASE					:			int    	=       ConfigTester(self    ,							config_class=lowercase__    ,							hidden_size=37 )






    def       _UpperCamelCase    (       self )							-> Any:
         self.config_tester.run_common_tests()






    def       _UpperCamelCase    (       self )							-> Tuple:
         SCREAMING_SNAKE_CASE					:			int    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_model(*lowercase__ )






    def       _UpperCamelCase    (       self )							-> Dict:
         SCREAMING_SNAKE_CASE					:			str    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowercase__ )






    def       _UpperCamelCase    (       self )							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowercase__ )






    def       _UpperCamelCase    (       self )							-> Dict:
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_token_classification(*lowercase__ )






    def       _UpperCamelCase    (       self )							-> Optional[int]:
         SCREAMING_SNAKE_CASE					:			str    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_question_answering(*lowercase__ )






@require_torch
class 				UpperCAmelCase (       unittest.TestCase ):




    '''simple docstring'''







    @slow
    def       _UpperCamelCase    (       self )							-> List[str]:
         SCREAMING_SNAKE_CASE					:			Tuple    	=       MPNetModel.from_pretrained('microsoft/mpnet-base' )
         SCREAMING_SNAKE_CASE					:			str    	=       torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] )
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       model(lowercase__ )[0]
         SCREAMING_SNAKE_CASE					:			List[str]    	=       torch.Size((1, 11, 768) )
         self.assertEqual(output.shape    ,							lowercase__ )
         SCREAMING_SNAKE_CASE					:			str    	=       torch.tensor(
             [[[-0.0_5_5_0, 0.1_9_4_3, -0.0_7_4_0], [-0.0_5_6_2, 0.2_2_1_1, -0.0_5_7_9], [-0.0_4_3_7, 0.3_3_3_7, -0.0_6_4_1]]] )
         # compare the actual values for a slice.
         self.assertTrue(torch.allclose(output[:, :3, :3]    ,							lowercase__    ,							atol=1E-4 ) )



