

'''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 		__SCREAMING_SNAKE_CASE							:







											@staticmethod
											def  lowerCamelCase_	(       *UpperCAmelCase__     :		List[Any]		,					**UpperCAmelCase__     :		Optional[int]   ):
																	'''simple docstring'''
																	pass






							def 				_lowerCAmelCase			(   __magic_name__						:     str						)				->  List[str]:
													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.
UpperCamelCase_    						=       (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)





@is_pipeline_test
@require_torch
@require_vision
class 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):
				lowerCamelCase_		       =    MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING







				@require_pytesseract
				@require_vision
				def  lowerCamelCase_	(       self     :		str		,					UpperCAmelCase__     :		List[Any]		,					UpperCAmelCase__     :		List[Any]		,					UpperCAmelCase__     :		str   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=pipeline(
										    '''document-question-answering'''		,					model=UpperCAmelCase__		,					tokenizer=UpperCAmelCase__		,					image_processor=UpperCAmelCase__   )

										lowercase :							str    		=INVOICE_URL
										lowercase :							Dict    		=list(zip(*apply_tesseract(load_image(UpperCAmelCase__   )		,					UpperCAmelCase__		,					''''''   )   )   )
										lowercase :							Optional[int]    		='''What is the placebo?'''
										lowercase :							Optional[int]    		=[
										    {
										        '''image''': load_image(UpperCAmelCase__   ),
										        '''question''': question,
										    },
										    {
										        '''image''': image,
										        '''question''': question,
										    },
										    {
										        '''image''': image,
										        '''question''': question,
										        '''word_boxes''': word_boxes,
										    },
										]
										return dqa_pipeline, examples







				def  lowerCamelCase_	(       self     :		Optional[Any]		,					UpperCAmelCase__     :		Union[str, Any]		,					UpperCAmelCase__     :		Optional[int]   ):
										'''simple docstring'''
										lowercase :							Dict    		=dqa_pipeline(UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(
										    UpperCAmelCase__		,					[
										        [
										            {'''score''': ANY(UpperCAmelCase__   ), '''answer''': ANY(UpperCAmelCase__   ), '''start''': ANY(UpperCAmelCase__   ), '''end''': ANY(UpperCAmelCase__   )},
										            {'''score''': ANY(UpperCAmelCase__   ), '''answer''': ANY(UpperCAmelCase__   ), '''start''': ANY(UpperCAmelCase__   ), '''end''': ANY(UpperCAmelCase__   )},
										        ]
										    ]
										    * 3		,					)







				@require_torch
				@require_detectrona
				@require_pytesseract
				def  lowerCamelCase_	(       self     :		List[str]   ):
										'''simple docstring'''
										lowercase :							str    		=pipeline('''document-question-answering'''		,					model='''hf-internal-testing/tiny-random-layoutlmv2'''   )
										lowercase :							List[str]    		=INVOICE_URL
										lowercase :							Tuple    		='''How many cats are there?'''

										lowercase :							str    		=[
										    {'''score''': 0.00_01, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39},
										    {'''score''': 0.00_01, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40},
										]
										lowercase :							Union[str, Any]    		=dqa_pipeline(image=UpperCAmelCase__		,					question=UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					UpperCAmelCase__   )

										lowercase :							Union[str, Any]    		=dqa_pipeline({'''image''': image, '''question''': question}		,					top_k=2   )
										self.assertEqual(nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					UpperCAmelCase__   )

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

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







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

										lowercase :							Dict    		=dqa_pipeline(image=UpperCAmelCase__		,					question=UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]		,					)

										lowercase :							Any    		=dqa_pipeline({'''image''': image, '''question''': question}		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]		,					)

										lowercase :							Any    		=dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        [
										            {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										            {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        ],
										    ]
										    * 2		,					)







				@slow
				@require_torch
				@require_detectrona
				@require_pytesseract
				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										lowercase :							Any    		=pipeline(
										    '''document-question-answering'''		,					model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''		,					revision='''9977165'''		,					max_seq_len=50		,					)
										lowercase :							Optional[int]    		=INVOICE_URL
										lowercase :							Dict    		='''What is the invoice number?'''

										lowercase :							Optional[Any]    		=dqa_pipeline(image=UpperCAmelCase__		,					question=UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										        {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]		,					)

										lowercase :							Optional[Any]    		=dqa_pipeline({'''image''': image, '''question''': question}		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										        {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]		,					)

										lowercase :							Optional[Any]    		=dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        [
										            {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										            {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        ]
										    ]
										    * 2		,					)







				@slow
				@require_torch
				@require_pytesseract
				@require_vision
				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										lowercase :							Optional[int]    		=AutoTokenizer.from_pretrained(
										    '''impira/layoutlm-document-qa'''		,					revision='''3dc6de3'''		,					add_prefix_space=UpperCAmelCase__   )
										lowercase :							Optional[Any]    		=pipeline(
										    '''document-question-answering'''		,					model='''impira/layoutlm-document-qa'''		,					tokenizer=UpperCAmelCase__		,					revision='''3dc6de3'''		,					)
										lowercase :							Dict    		=INVOICE_URL
										lowercase :							Union[str, Any]    		='''What is the invoice number?'''

										lowercase :							Union[str, Any]    		=dqa_pipeline(image=UpperCAmelCase__		,					question=UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										    ]		,					)

										lowercase :							List[Any]    		=dqa_pipeline({'''image''': image, '''question''': question}		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										    ]		,					)

										lowercase :							Dict    		=dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        [
										            {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										            {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										        ]
										    ]
										    * 2		,					)

										lowercase :							Dict    		=list(zip(*apply_tesseract(load_image(UpperCAmelCase__   )		,					UpperCAmelCase__		,					''''''   )   )   )

										# This model should also work if `image` is set to None
										lowercase :							int    		=dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										    ]		,					)







				@slow
				@require_torch
				@require_pytesseract
				@require_vision
				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							List[str]    		=AutoTokenizer.from_pretrained(
										    '''impira/layoutlm-document-qa'''		,					revision='''3dc6de3'''		,					add_prefix_space=UpperCAmelCase__   )
										lowercase :							Any    		=pipeline(
										    '''document-question-answering'''		,					model='''impira/layoutlm-document-qa'''		,					tokenizer=UpperCAmelCase__		,					revision='''3dc6de3'''		,					max_seq_len=50		,					)
										lowercase :							Union[str, Any]    		=INVOICE_URL
										lowercase :							List[str]    		='''What is the invoice number?'''

										lowercase :							Dict    		=dqa_pipeline(image=UpperCAmelCase__		,					question=UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]		,					)

										lowercase :							Tuple    		=dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        [
										            {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										            {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        ]
										    ]
										    * 2		,					)

										lowercase :							List[Any]    		=list(zip(*apply_tesseract(load_image(UpperCAmelCase__   )		,					UpperCAmelCase__		,					''''''   )   )   )

										# This model should also work if `image` is set to None
										lowercase :							str    		=dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}		,					top_k=2   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[
										        {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]		,					)







				@slow
				@require_torch
				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							Tuple    		=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'''		,					)

										lowercase :							Tuple    		=INVOICE_URL
										lowercase :							str    		='''What is the invoice number?'''
										lowercase :							str    		=dqa_pipeline(image=UpperCAmelCase__		,					question=UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[{'''answer''': '''us-001'''}]   )







				@require_tf
				@unittest.skip('''Document question answering not implemented in TF'''   )
				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										pass











'''simple docstring'''







import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import (
        MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
        MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
        MODEL_MAPPING,
        DeiTForImageClassification,
        DeiTForImageClassificationWithTeacher,
        DeiTForMaskedImageModeling,
        DeiTModel,
    )
    from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import DeiTImageProcessor

class 	_snake_case:






 def __init__(self       :  List[Any]    ,		a       :  str    ,		a       :  Any=13    ,		a       :  Optional[Any]=30    ,		a       :  Union[str, Any]=2    ,		a       :  List[str]=3    ,		a       :  List[str]=True    ,		a       :  List[Any]=True    ,		a       :  Tuple=32    ,		a       :  Optional[Any]=5    ,		a       :  str=4    ,		a       :  List[str]=37    ,		a       :  List[str]="gelu"    ,		a       :  int=0.1    ,		a       :  int=0.1    ,		a       :  str=10    ,		a       :  Tuple=0.02    ,		a       :  Union[str, Any]=3    ,		a       :  List[str]=None    ,		a       :  Any=2    ,		)       ->   Optional[int]:






    """simple docstring"""


    A__       =     parent
    A__       =     batch_size
    A__       =     image_size
    A__       =     patch_size
    A__       =     num_channels
    A__       =     is_training
    A__       =     use_labels
    A__       =     hidden_size
    A__       =     num_hidden_layers
    A__       =     num_attention_heads
    A__       =     intermediate_size
    A__       =     hidden_act
    A__       =     hidden_dropout_prob
    A__       =     attention_probs_dropout_prob
    A__       =     type_sequence_label_size
    A__       =     initializer_range
    A__       =     scope
    A__       =     encoder_stride

    # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
    A__       =     (image_size // patch_size) ** 2
    A__       =     num_patches + 2






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   List[str]:






    """simple docstring"""


    A__       =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

    A__       =     None
    if self.use_labels:
       A__       =     ids_tensor([self.batch_size]    ,		self.type_sequence_label_size    )

    A__       =     self.get_config()

    return config, pixel_values, labels






 def 						_UpperCamelCase (self       :  Union[str, Any]    )       ->   int:






    """simple docstring"""


    return DeiTConfig(
        image_size=self.image_size    ,		patch_size=self.patch_size    ,		num_channels=self.num_channels    ,		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    ,		is_decoder=a    ,		initializer_range=self.initializer_range    ,		encoder_stride=self.encoder_stride    ,		)






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  Tuple    ,		a       :  Dict    ,		a       :  Optional[Any]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     DeiTModel(config=a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    )
    self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)    )






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  Any    ,		a       :  Optional[Any]    ,		a       :  Optional[int]    )       ->   Dict:






    """simple docstring"""


    A__       =     DeiTForMaskedImageModeling(config=a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    )
    self.parent.assertEqual(
        result.reconstruction.shape    ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

    # test greyscale images
    A__       =     1
    A__       =     DeiTForMaskedImageModeling(a    )
    model.to(a    )
    model.eval()

    A__       =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
    A__       =     model(a    )
    self.parent.assertEqual(result.reconstruction.shape    ,		(self.batch_size, 1, self.image_size, self.image_size)    )






 def 						_UpperCamelCase (self       :  Optional[Any]    ,		a       :  Optional[Any]    ,		a       :  Union[str, Any]    ,		a       :  Union[str, Any]    )       ->   str:






    """simple docstring"""


    A__       =     self.type_sequence_label_size
    A__       =     DeiTForImageClassification(a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    ,		labels=a    )
    self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.type_sequence_label_size)    )

    # test greyscale images
    A__       =     1
    A__       =     DeiTForImageClassification(a    )
    model.to(a    )
    model.eval()

    A__       =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
    A__       =     model(a    ,		labels=a    )
    self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.type_sequence_label_size)    )






 def 						_UpperCamelCase (self       :  List[Any]    )       ->   Optional[int]:






    """simple docstring"""


    A__       =     self.prepare_config_and_inputs()
    (
        (
        A__
    )  ,			(
        A__
    )  ,			(
        A__
    )  ,			
    )       =     config_and_inputs
    A__       =     {'pixel_values': pixel_values}
    return config, inputs_dict




@require_torch
class 	_snake_case(			UpperCAmelCase       , UpperCAmelCase       , unittest.TestCase      ):
 __snake_case:							Optional[Any]						=					(
     (
         DeiTModel,
         DeiTForImageClassification,
         DeiTForImageClassificationWithTeacher,
         DeiTForMaskedImageModeling,
     )
     if is_torch_available()
     else ()
 )
 __snake_case:							int						=					(
     {
         '''feature-extraction''': DeiTModel,
         '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
     }
     if is_torch_available()
     else {}
 )

 __snake_case:							Any						=					False
 __snake_case:							Any						=					False
 __snake_case:							Any						=					False






 def 						_UpperCamelCase (self       :  Any    )       ->   int:






    """simple docstring"""


    A__       =     DeiTModelTester(self    )
    A__       =     ConfigTester(self    ,		config_class=a    ,		has_text_modality=a    ,		hidden_size=37    )






 def 						_UpperCamelCase (self       :  Dict    )       ->   Tuple:






    """simple docstring"""


    self.config_tester.run_common_tests()






 @unittest.skip(reason='DeiT does not use inputs_embeds'    )
 def 						_UpperCamelCase (self       :  List[str]    )       ->   List[str]:






    """simple docstring"""


    pass






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   Tuple:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       A__       =     model_class(a    )
       self.assertIsInstance(model.get_input_embeddings()    ,		(nn.Module)    )
       A__       =     model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(a    ,		nn.Linear    )    )






 def 						_UpperCamelCase (self       :  Union[str, Any]    )       ->   str:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       A__       =     model_class(a    )
       A__       =     inspect.signature(model.forward    )
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       A__       =     [*signature.parameters.keys()]

       A__       =     ['pixel_values']
       self.assertListEqual(arg_names[:1]    ,		a    )






 def 						_UpperCamelCase (self       :  Union[str, Any]    )       ->   Dict:






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*a    )






 def 						_UpperCamelCase (self       :  Dict    )       ->   Any:






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_image_modeling(*a    )






 def 						_UpperCamelCase (self       :  List[Any]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*a    )






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  int    ,		a       :  Union[str, Any]    ,		a       :  List[Any]=False    )       ->   Optional[int]:






    """simple docstring"""


    A__       =     super()._prepare_for_class(a    ,		a    ,		return_labels=a    )

    if return_labels:
       if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
          del inputs_dict["labels"]

    return inputs_dict






 def 						_UpperCamelCase (self       :  Any    )       ->   Tuple:






    """simple docstring"""


    if not self.model_tester.is_training:
       return

    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()
    A__       =     True

    for model_class in self.all_model_classes:
       # DeiTForImageClassificationWithTeacher supports inference-only
       if (
           model_class in get_values(a    )
           or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
       ):
          continue
       A__       =     model_class(a    )
       model.to(a    )
       model.train()
       A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )
       A__       =     model(**a    ).loss
       loss.backward()






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   int:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()
    if not self.model_tester.is_training:
       return

    A__       =     False
    A__       =     True

    for model_class in self.all_model_classes:
       if model_class in get_values(a    ) or not model_class.supports_gradient_checkpointing:
          continue
       # DeiTForImageClassificationWithTeacher supports inference-only
       if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
          continue
       A__       =     model_class(a    )
       model.gradient_checkpointing_enable()
       model.to(a    )
       model.train()
       A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )
       A__       =     model(**a    ).loss
       loss.backward()






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   List[str]:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

    A__       =     [
        {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
        {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
        {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
    ]

    for model_class in self.all_model_classes:
       if (
           model_class
           not in [
               *get_values(a    ),
               *get_values(a    ),
           ]
           or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
       ):
          continue

       for problem_type in problem_types:
          with self.subTest(msg=f"""Testing {model_class} with {problem_type["title"]}"""    ):
             A__       =     problem_type['title']
             A__       =     problem_type['num_labels']

             A__       =     model_class(a    )
             model.to(a    )
             model.train()

             A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )

             if problem_type["num_labels"] > 1:
                A__       =     inputs['labels'].unsqueeze(1    ).repeat(1    ,		problem_type['num_labels']    )

             A__       =     inputs['labels'].to(problem_type['dtype']    )

             # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
             # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
             # they have the same size." which is a symptom something in wrong for the regression problem.
             # See https://github.com/huggingface/transformers/issues/11780
             with warnings.catch_warnings(record=a    ) as warning_list:
                A__       =     model(**a    ).loss
             for w in warning_list:
                if "Using a target size that is different to the input size" in str(w.message    ):
                   raise ValueError(
                       f"""Something is going wrong in the regression problem: intercepted {w.message}"""    )

             loss.backward()






 @slow
 def 						_UpperCamelCase (self       :  Union[str, Any]    )       ->   Tuple:






    """simple docstring"""


    for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       A__       =     DeiTModel.from_pretrained(a    )
       self.assertIsNotNone(a    )







def 						_A      ( ):






   '''simple docstring'''







   A__       =     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
   return image







@require_torch
@require_vision
class 	_snake_case(			unittest.TestCase      ):






 @cached_property
 def 						_UpperCamelCase (self       :  Tuple    )       ->   Union[str, Any]:






    """simple docstring"""


    return (
        DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'    )
        if is_vision_available()
        else None
    )






 @slow
 def 						_UpperCamelCase (self       :  List[str]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'    ).to(
        a    )

    A__       =     self.default_image_processor
    A__       =     prepare_img()
    A__       =     image_processor(images=a    ,		return_tensors='pt'    ).to(a    )

    # forward pass
    with torch.no_grad():
       A__       =     model(**a    )

    # verify the logits
    A__       =     torch.Size((1, 10_00)    )
    self.assertEqual(outputs.logits.shape    ,		a    )

    A__       =     torch.tensor([-1.0266, 0.1912, -1.2861]    ).to(a    )

    self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,		a    ,		atol=1e-4    )    )






 @slow
 @require_accelerate
 @require_torch_gpu
 def 						_UpperCamelCase (self       :  Tuple    )       ->   str:






    """simple docstring"""


    A__       =     DeiTModel.from_pretrained(
        'facebook/deit-base-distilled-patch16-224'    ,		torch_dtype=torch.floataa    ,		device_map='auto'    )
    A__       =     self.default_image_processor

    A__       =     prepare_img()
    A__       =     image_processor(images=a    ,		return_tensors='pt'    )
    A__       =     inputs.pixel_values.to(a    )

    # forward pass to make sure inference works in fp16
    with torch.no_grad():
       A__       =     model(a    )


