



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							:






   '''simple docstring'''






   def __init__(  self:			Dict   ,lowerCamelCase_:			Any   ,lowerCamelCase_:			int=13   ,lowerCamelCase_:			int=30   ,lowerCamelCase_:			List[Any]=2   ,lowerCamelCase_:			Optional[Any]=3   ,lowerCamelCase_:			str=True   ,lowerCamelCase_:			Union[str, Any]=True   ,lowerCamelCase_:			Tuple=32   ,lowerCamelCase_:			Optional[Any]=5   ,lowerCamelCase_:			Optional[int]=4   ,lowerCamelCase_:			str=37   ,lowerCamelCase_:			Any="gelu"   ,lowerCamelCase_:			List[str]=0.1   ,lowerCamelCase_:			int=0.1   ,lowerCamelCase_:			str=10   ,lowerCamelCase_:			Tuple=0.0_2   ,lowerCamelCase_:			Tuple=3   ,lowerCamelCase_:			List[str]=None   ,lowerCamelCase_:			int=2   ,)     ->	Dict:
       UpperCAmelCase_			:    Union[str, Any]   						= parent
       UpperCAmelCase_			:    List[Any]   						= batch_size
       UpperCAmelCase_			:    Union[str, Any]   						= image_size
       UpperCAmelCase_			:    int   						= patch_size
       UpperCAmelCase_			:    Tuple   						= num_channels
       UpperCAmelCase_			:    Optional[Any]   						= is_training
       UpperCAmelCase_			:    Dict   						= use_labels
       UpperCAmelCase_			:    Union[str, Any]   						= hidden_size
       UpperCAmelCase_			:    List[Any]   						= num_hidden_layers
       UpperCAmelCase_			:    str   						= num_attention_heads
       UpperCAmelCase_			:    Optional[Any]   						= intermediate_size
       UpperCAmelCase_			:    Union[str, Any]   						= hidden_act
       UpperCAmelCase_			:    Tuple   						= hidden_dropout_prob
       UpperCAmelCase_			:    Optional[Any]   						= attention_probs_dropout_prob
       UpperCAmelCase_			:    Tuple   						= type_sequence_label_size
       UpperCAmelCase_			:    Any   						= initializer_range
       UpperCAmelCase_			:    int   						= scope
       UpperCAmelCase_			:    List[str]   						= encoder_stride

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


   def 	A__  (  self:			Union[str, Any]				)     ->	str:
       UpperCAmelCase_			:    Optional[Any]   						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

       UpperCAmelCase_			:    Union[str, Any]   						= None
       if self.use_labels:
           UpperCAmelCase_			:    List[str]   						= ids_tensor([self.batch_size]   ,self.type_sequence_label_size				)

       UpperCAmelCase_			:    Any   						= self.get_config()

       return config, pixel_values, labels


   def 	A__  (  self:			int				)     ->	Dict:
       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=__lowercase   ,initializer_range=self.initializer_range   ,encoder_stride=self.encoder_stride   ,)


   def 	A__  (  self:			Dict   ,lowerCamelCase_:			Dict   ,lowerCamelCase_:			Union[str, Any]   ,lowerCamelCase_:			Optional[int]				)     ->	Tuple:
       UpperCAmelCase_			:    List[Any]   						= DeiTModel(config=__lowercase				)
       model.to(__lowercase				)
       model.eval()
       UpperCAmelCase_			:    Any   						= model(__lowercase				)
       self.parent.assertEqual(result.last_hidden_state.shape   ,(self.batch_size, self.seq_length, self.hidden_size)				)


   def 	A__  (  self:			List[Any]   ,lowerCamelCase_:			int   ,lowerCamelCase_:			Union[str, Any]   ,lowerCamelCase_:			Optional[int]				)     ->	List[Any]:
       UpperCAmelCase_			:    Union[str, Any]   						= DeiTForMaskedImageModeling(config=__lowercase				)
       model.to(__lowercase				)
       model.eval()
       UpperCAmelCase_			:    Optional[int]   						= model(__lowercase				)
       self.parent.assertEqual(
           result.reconstruction.shape   ,(self.batch_size, self.num_channels, self.image_size, self.image_size)				)

       # test greyscale images
       UpperCAmelCase_			:    List[Any]   						= 1
       UpperCAmelCase_			:    Union[str, Any]   						= DeiTForMaskedImageModeling(__lowercase				)
       model.to(__lowercase				)
       model.eval()

       UpperCAmelCase_			:    Union[str, Any]   						= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
       UpperCAmelCase_			:    List[str]   						= model(__lowercase				)
       self.parent.assertEqual(result.reconstruction.shape   ,(self.batch_size, 1, self.image_size, self.image_size)				)


   def 	A__  (  self:			List[Any]   ,lowerCamelCase_:			Optional[Any]   ,lowerCamelCase_:			Optional[int]   ,lowerCamelCase_:			List[str]				)     ->	List[Any]:
       UpperCAmelCase_			:    Optional[int]   						= self.type_sequence_label_size
       UpperCAmelCase_			:    Optional[Any]   						= DeiTForImageClassification(__lowercase				)
       model.to(__lowercase				)
       model.eval()
       UpperCAmelCase_			:    List[Any]   						= model(__lowercase   ,labels=__lowercase				)
       self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.type_sequence_label_size)				)

       # test greyscale images
       UpperCAmelCase_			:    Any   						= 1
       UpperCAmelCase_			:    Union[str, Any]   						= DeiTForImageClassification(__lowercase				)
       model.to(__lowercase				)
       model.eval()

       UpperCAmelCase_			:    Dict   						= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
       UpperCAmelCase_			:    int   						= model(__lowercase   ,labels=__lowercase				)
       self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.type_sequence_label_size)				)






   def 	A__  (  self:			Union[str, Any]				)     ->	List[Any]:
       UpperCAmelCase_			:    str   						= self.prepare_config_and_inputs()
       (
           UpperCAmelCase_ 
       )			:    Dict   						= config_and_inputs
       UpperCAmelCase_			:    Dict   						= {'''pixel_values''': pixel_values}
       return config, inputs_dict







@require_torch
class 	_snake_case							(			__lowerCamelCase      ,				__lowerCamelCase      ,				unittest.TestCase      ):






   '''simple docstring'''






   A__	:      Optional[int]    =		(
       (
           DeiTModel,
           DeiTForImageClassification,
           DeiTForImageClassificationWithTeacher,
           DeiTForMaskedImageModeling,
       )
       if is_torch_available()
       else ()
   )
   A__	:      List[Any]    =		(
       {
           """feature-extraction""": DeiTModel,
           """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
       }
       if is_torch_available()
       else {}
   )

   A__	:      Dict    =		False
   A__	:      str    =		False
   A__	:      Dict    =		False


   def 	A__  (  self:			Tuple				)     ->	Union[str, Any]:
       UpperCAmelCase_			:    int   						= DeiTModelTester(self				)
       UpperCAmelCase_			:    Any   						= ConfigTester(self   ,config_class=__lowercase   ,has_text_modality=__lowercase   ,hidden_size=37				)


   def 	A__  (  self:			Union[str, Any]				)     ->	int:
       self.config_tester.run_common_tests()


   @unittest.skip(reason="""DeiT does not use inputs_embeds"""				)
   def 	A__  (  self:			Union[str, Any]				)     ->	Optional[Any]:
       pass


   def 	A__  (  self:			List[Any]				)     ->	Optional[Any]:
       UpperCAmelCase_			:    str   						= self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCAmelCase_			:    Tuple   						= model_class(__lowercase				)
           self.assertIsInstance(model.get_input_embeddings()   ,(nn.Module)				)
           UpperCAmelCase_			:    Union[str, Any]   						= model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(__lowercase   ,nn.Linear				)				)


   def 	A__  (  self:			Union[str, Any]				)     ->	Tuple:
       UpperCAmelCase_			:    List[Any]   						= self.model_tester.prepare_config_and_inputs_for_common()

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

           UpperCAmelCase_			:    Union[str, Any]   						= ['''pixel_values''']
           self.assertListEqual(arg_names[:1]   ,__lowercase				)


   def 	A__  (  self:			Union[str, Any]				)     ->	Union[str, Any]:
       UpperCAmelCase_			:    Optional[int]   						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowercase				)


   def 	A__  (  self:			List[Any]				)     ->	Dict:
       UpperCAmelCase_			:    int   						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase				)


   def 	A__  (  self:			List[Any]				)     ->	Optional[int]:
       UpperCAmelCase_			:    Any   						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__lowercase				)


   def 	A__  (  self:			Any   ,lowerCamelCase_:			int   ,lowerCamelCase_:			List[str]   ,lowerCamelCase_:			Tuple=False				)     ->	List[str]:
       UpperCAmelCase_			:    Optional[int]   						= super()._prepare_for_class(__lowercase   ,__lowercase   ,return_labels=__lowercase				)

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

       return inputs_dict


   def 	A__  (  self:			Tuple				)     ->	List[str]:
       if not self.model_tester.is_training:
           return

       UpperCAmelCase_			:    Optional[int]   						= self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase_			:    Optional[int]   						= True

       for model_class in self.all_model_classes:
           # DeiTForImageClassificationWithTeacher supports inference-only
           if (
               model_class in get_values(__lowercase				)
               or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
           ):
               continue
           UpperCAmelCase_			:    str   						= model_class(__lowercase				)
           model.to(__lowercase				)
           model.train()
           UpperCAmelCase_			:    Tuple   						= self._prepare_for_class(__lowercase   ,__lowercase   ,return_labels=__lowercase				)
           UpperCAmelCase_			:    List[str]   						= model(**__lowercase				).loss
           loss.backward()


   def 	A__  (  self:			str				)     ->	List[Any]:
       UpperCAmelCase_			:    Optional[int]   						= self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
           return

       UpperCAmelCase_			:    Dict   						= False
       UpperCAmelCase_			:    List[Any]   						= True

       for model_class in self.all_model_classes:
           if model_class in get_values(__lowercase				) or not model_class.supports_gradient_checkpointing:
               continue
           # DeiTForImageClassificationWithTeacher supports inference-only
           if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
               continue
           UpperCAmelCase_			:    List[Any]   						= model_class(__lowercase				)
           model.gradient_checkpointing_enable()
           model.to(__lowercase				)
           model.train()
           UpperCAmelCase_			:    str   						= self._prepare_for_class(__lowercase   ,__lowercase   ,return_labels=__lowercase				)
           UpperCAmelCase_			:    str   						= model(**__lowercase				).loss
           loss.backward()


   def 	A__  (  self:			Optional[int]				)     ->	Any:
       UpperCAmelCase_			:    List[str]   						= self.model_tester.prepare_config_and_inputs_for_common()

       UpperCAmelCase_			:    Optional[Any]   						= [
           {'''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(__lowercase				),
                   *get_values(__lowercase				),
               ]
               or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
           ):
               continue

           for problem_type in problem_types:
               with self.subTest(msg=F'''Testing {model_class} with {problem_type['title']}'''				):
                   UpperCAmelCase_			:    Dict   						= problem_type['''title''']
                   UpperCAmelCase_			:    Any   						= problem_type['''num_labels''']

                   UpperCAmelCase_			:    int   						= model_class(__lowercase				)
                   model.to(__lowercase				)
                   model.train()

                   UpperCAmelCase_			:    Dict   						= self._prepare_for_class(__lowercase   ,__lowercase   ,return_labels=__lowercase				)

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

                   UpperCAmelCase_			:    List[str]   						= 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=__lowercase				) as warning_list:
                       UpperCAmelCase_			:    List[str]   						= model(**__lowercase				).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 	A__  (  self:			Tuple				)     ->	Dict:
       for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCAmelCase_			:    List[Any]   						= DeiTModel.from_pretrained(__lowercase				)
           self.assertIsNotNone(__lowercase				)







def 	lowerCamelCase_   (  ):



    '''simple docstring'''




    UpperCAmelCase_			:    List[str]   						= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
    return image







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






   '''simple docstring'''






   @cached_property
   def 	A__  (  self:			Union[str, Any]				)     ->	Tuple:
       return (
           DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""				)
           if is_vision_available()
           else None
       )


   @slow
   def 	A__  (  self:			List[str]				)     ->	List[Any]:
       UpperCAmelCase_			:    List[str]   						= DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""				).to(
           __lowercase				)

       UpperCAmelCase_			:    List[str]   						= self.default_image_processor
       UpperCAmelCase_			:    Optional[Any]   						= prepare_img()
       UpperCAmelCase_			:    int   						= image_processor(images=__lowercase   ,return_tensors="""pt"""				).to(__lowercase				)

       # forward pass
       with torch.no_grad():
           UpperCAmelCase_			:    Dict   						= model(**__lowercase				)

       # verify the logits
       UpperCAmelCase_			:    List[Any]   						= torch.Size((1, 1000)				)
       self.assertEqual(outputs.logits.shape   ,__lowercase				)

       UpperCAmelCase_			:    List[str]   						= torch.tensor([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1]				).to(__lowercase				)

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






   @slow
   @require_accelerate
   @require_torch_gpu
   def 	A__  (  self:			Optional[Any]				)     ->	Optional[int]:
       UpperCAmelCase_			:    List[str]   						= DeiTModel.from_pretrained(
           """facebook/deit-base-distilled-patch16-224"""   ,torch_dtype=torch.floataa   ,device_map="""auto"""				)
       UpperCAmelCase_			:    str   						= self.default_image_processor

       UpperCAmelCase_			:    Any   						= prepare_img()
       UpperCAmelCase_			:    Optional[int]   						= image_processor(images=__lowercase   ,return_tensors="""pt"""				)
       UpperCAmelCase_			:    str   						= inputs.pixel_values.to(__lowercase				)

       # forward pass to make sure inference works in fp16
       with torch.no_grad():
           UpperCAmelCase_			:    int   						= model(__lowercase				)





import gc
import unittest

from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline
from transformers.pipelines import PipelineException
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_gpu,
    slow,
)

from .test_pipelines_common import ANY


@is_pipeline_test
class       a							(			unittest.TestCase				):
		__lowerCAmelCase	:    Any											=					MODEL_FOR_MASKED_LM_MAPPING
		__lowerCAmelCase	:    Optional[Any]											=					TF_MODEL_FOR_MASKED_LM_MAPPING



		def 	__lowerCamelCase			(     self     :str  ):
						super().tearDown()
						# clean-up as much as possible GPU memory occupied by PyTorch
						gc.collect()
						if is_torch_available():
										import torch

										torch.cuda.empty_cache()



		@require_tf
		def 	__lowerCamelCase			(     self     :Any  ):
						snake_case__	:      Optional[Any]     		=	pipeline(task='''fill-mask'''   ,model='''sshleifer/tiny-distilroberta-base'''   ,top_k=2   ,framework='''tf'''  )
						snake_case__	:      int     		=	unmasker('''My name is <mask>'''  )
						self.assertEqual(
						    nested_simplify(__lowercase   ,decimals=6  )   ,[
						        {'''sequence''': '''My name is grouped''', '''score''': 2.1e-0_5, '''token''': 3_8_0_1_5, '''token_str''': ''' grouped'''},
						        {'''sequence''': '''My name is accuser''', '''score''': 2.1e-0_5, '''token''': 2_5_5_0_6, '''token_str''': ''' accuser'''},
						    ]   ,)

						snake_case__	:      int     		=	unmasker('''The largest city in France is <mask>'''  )
						self.assertEqual(
						    nested_simplify(__lowercase   ,decimals=6  )   ,[
						        {
						            '''sequence''': '''The largest city in France is grouped''',
						            '''score''': 2.1e-0_5,
						            '''token''': 3_8_0_1_5,
						            '''token_str''': ''' grouped''',
						        },
						        {
						            '''sequence''': '''The largest city in France is accuser''',
						            '''score''': 2.1e-0_5,
						            '''token''': 2_5_5_0_6,
						            '''token_str''': ''' accuser''',
						        },
						    ]   ,)

						snake_case__	:      Optional[int]     		=	unmasker('''My name is <mask>'''   ,targets=[''' Patrick''', ''' Clara''', ''' Teven''']   ,top_k=3  )
						self.assertEqual(
						    nested_simplify(__lowercase   ,decimals=6  )   ,[
						        {'''sequence''': '''My name is Clara''', '''score''': 2e-0_5, '''token''': 1_3_6_0_6, '''token_str''': ''' Clara'''},
						        {'''sequence''': '''My name is Patrick''', '''score''': 2e-0_5, '''token''': 3_4_9_9, '''token_str''': ''' Patrick'''},
						        {'''sequence''': '''My name is Te''', '''score''': 1.9e-0_5, '''token''': 2_9_4_1, '''token_str''': ''' Te'''},
						    ]   ,)



		@require_torch
		def 	__lowerCamelCase			(     self     :Optional[int]  ):
						snake_case__	:      str     		=	pipeline(task='''fill-mask'''   ,model='''sshleifer/tiny-distilroberta-base'''   ,top_k=2   ,framework='''pt'''  )

						snake_case__	:      str     		=	unmasker('''My name is <mask>'''  )
						self.assertEqual(
						    nested_simplify(__lowercase   ,decimals=6  )   ,[
						        {'''sequence''': '''My name is Maul''', '''score''': 2.2e-0_5, '''token''': 3_5_6_7_6, '''token_str''': ''' Maul'''},
						        {'''sequence''': '''My name isELS''', '''score''': 2.2e-0_5, '''token''': 1_6_4_1_6, '''token_str''': '''ELS'''},
						    ]   ,)

						snake_case__	:      List[str]     		=	unmasker('''The largest city in France is <mask>'''  )
						self.assertEqual(
						    nested_simplify(__lowercase   ,decimals=6  )   ,[
						        {
						            '''sequence''': '''The largest city in France is Maul''',
						            '''score''': 2.2e-0_5,
						            '''token''': 3_5_6_7_6,
						            '''token_str''': ''' Maul''',
						        },
						        {'''sequence''': '''The largest city in France isELS''', '''score''': 2.2e-0_5, '''token''': 1_6_4_1_6, '''token_str''': '''ELS'''},
						    ]   ,)

						snake_case__	:      Union[str, Any]     		=	unmasker('''My name is <mask>'''   ,targets=[''' Patrick''', ''' Clara''', ''' Teven''']   ,top_k=3  )
						self.assertEqual(
						    nested_simplify(__lowercase   ,decimals=6  )   ,[
						        {'''sequence''': '''My name is Patrick''', '''score''': 2.1e-0_5, '''token''': 3_4_9_9, '''token_str''': ''' Patrick'''},
						        {'''sequence''': '''My name is Te''', '''score''': 2e-0_5, '''token''': 2_9_4_1, '''token_str''': ''' Te'''},
						        {'''sequence''': '''My name is Clara''', '''score''': 2e-0_5, '''token''': 1_3_6_0_6, '''token_str''': ''' Clara'''},
						    ]   ,)

						snake_case__	:      Optional[int]     		=	unmasker('''My name is <mask> <mask>'''   ,top_k=2  )

						self.assertEqual(
						    nested_simplify(__lowercase   ,decimals=6  )   ,[
						        [
						            {
						                '''score''': 2.2e-0_5,
						                '''token''': 3_5_6_7_6,
						                '''token_str''': ''' Maul''',
						                '''sequence''': '''<s>My name is Maul<mask></s>''',
						            },
						            {'''score''': 2.2e-0_5, '''token''': 1_6_4_1_6, '''token_str''': '''ELS''', '''sequence''': '''<s>My name isELS<mask></s>'''},
						        ],
						        [
						            {
						                '''score''': 2.2e-0_5,
						                '''token''': 3_5_6_7_6,
						                '''token_str''': ''' Maul''',
						                '''sequence''': '''<s>My name is<mask> Maul</s>''',
						            },
						            {'''score''': 2.2e-0_5, '''token''': 1_6_4_1_6, '''token_str''': '''ELS''', '''sequence''': '''<s>My name is<mask>ELS</s>'''},
						        ],
						    ]   ,)



		@require_torch_gpu
		def 	__lowerCamelCase			(     self     :int  ):
						snake_case__	:      Optional[int]     		=	pipeline('''fill-mask'''   ,model='''hf-internal-testing/tiny-random-distilbert'''   ,device=0   ,framework='''pt'''  )

						# convert model to fp16
						pipe.model.half()

						snake_case__	:      List[str]     		=	pipe('''Paris is the [MASK] of France.'''  )
						# We actually don't care about the result, we just want to make sure
						# it works, meaning the float16 tensor got casted back to float32
						# for postprocessing.
						self.assertIsInstance(__lowercase   ,__lowercase  )



		@slow
		@require_torch
		def 	__lowerCamelCase			(     self     :str  ):
						snake_case__	:      List[str]     		=	pipeline(task='''fill-mask'''   ,model='''distilroberta-base'''   ,top_k=2   ,framework='''pt'''  )
						self.run_large_test(__lowercase  )



		@slow
		@require_tf
		def 	__lowerCamelCase			(     self     :Any  ):
						snake_case__	:      Optional[Any]     		=	pipeline(task='''fill-mask'''   ,model='''distilroberta-base'''   ,top_k=2   ,framework='''tf'''  )
						self.run_large_test(__lowercase  )



		def 	__lowerCamelCase			(     self     :Optional[Any]   ,__lowercase     :List[Any]  ):
						snake_case__	:      Optional[Any]     		=	unmasker('''My name is <mask>'''  )
						self.assertEqual(
						    nested_simplify(__lowercase  )   ,[
						        {'''sequence''': '''My name is John''', '''score''': 0.008, '''token''': 6_1_0, '''token_str''': ''' John'''},
						        {'''sequence''': '''My name is Chris''', '''score''': 0.007, '''token''': 1_5_7_3, '''token_str''': ''' Chris'''},
						    ]   ,)
						snake_case__	:      str     		=	unmasker('''The largest city in France is <mask>'''  )
						self.assertEqual(
						    nested_simplify(__lowercase  )   ,[
						        {
						            '''sequence''': '''The largest city in France is Paris''',
						            '''score''': 0.251,
						            '''token''': 2_2_0_1,
						            '''token_str''': ''' Paris''',
						        },
						        {
						            '''sequence''': '''The largest city in France is Lyon''',
						            '''score''': 0.214,
						            '''token''': 1_2_7_9_0,
						            '''token_str''': ''' Lyon''',
						        },
						    ]   ,)

						snake_case__	:      Dict     		=	unmasker('''My name is <mask>'''   ,targets=[''' Patrick''', ''' Clara''', ''' Teven''']   ,top_k=3  )
						self.assertEqual(
						    nested_simplify(__lowercase  )   ,[
						        {'''sequence''': '''My name is Patrick''', '''score''': 0.005, '''token''': 3_4_9_9, '''token_str''': ''' Patrick'''},
						        {'''sequence''': '''My name is Clara''', '''score''': 0.000, '''token''': 1_3_6_0_6, '''token_str''': ''' Clara'''},
						        {'''sequence''': '''My name is Te''', '''score''': 0.000, '''token''': 2_9_4_1, '''token_str''': ''' Te'''},
						    ]   ,)



		@require_torch
		def 	__lowerCamelCase			(     self     :List[str]  ):
						snake_case__	:      List[Any]     		=	pipeline(task='''fill-mask'''   ,model='''sshleifer/tiny-distilroberta-base'''   ,framework='''pt'''  )
						snake_case__	:      str     		=	None
						snake_case__	:      int     		=	None
						self.run_pipeline_test(__lowercase   ,[]  )



		@require_tf
		def 	__lowerCamelCase			(     self     :int  ):
						snake_case__	:      Optional[int]     		=	pipeline(task='''fill-mask'''   ,model='''sshleifer/tiny-distilroberta-base'''   ,framework='''tf'''  )
						snake_case__	:      int     		=	None
						snake_case__	:      List[str]     		=	None
						self.run_pipeline_test(__lowercase   ,[]  )



		def 	__lowerCamelCase			(     self     :Any   ,__lowercase     :Any   ,__lowercase     :str   ,__lowercase     :Union[str, Any]  ):
						if tokenizer is None or tokenizer.mask_token_id is None:
										self.skipTest('''The provided tokenizer has no mask token, (probably reformer or wav2vec2)'''  )

						snake_case__	:      Optional[int]     		=	FillMaskPipeline(model=__lowercase   ,tokenizer=__lowercase  )
						snake_case__	:      List[str]     		=	[
						    F"""This is another {tokenizer.mask_token} test""",
						]
						return fill_masker, examples



		def 	__lowerCamelCase			(     self     :Optional[Any]   ,__lowercase     :List[Any]   ,__lowercase     :Optional[Any]  ):
						snake_case__	:      List[str]     		=	fill_masker.tokenizer
						snake_case__	:      List[Any]     		=	fill_masker.model

						snake_case__	:      Dict     		=	fill_masker(
						    F"""This is a {tokenizer.mask_token}"""   ,)
						self.assertEqual(
						    __lowercase   ,[
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						    ]   ,)

						snake_case__	:      Tuple     		=	fill_masker([F"""This is a {tokenizer.mask_token}"""]  )
						self.assertEqual(
						    __lowercase   ,[
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						    ]   ,)

						snake_case__	:      List[str]     		=	fill_masker([F"""This is a {tokenizer.mask_token}""", F"""Another {tokenizer.mask_token} great test."""]  )
						self.assertEqual(
						    __lowercase   ,[
						        [
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        ],
						        [
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        ],
						    ]   ,)

						with self.assertRaises(__lowercase  ):
										fill_masker([None]  )
						# No mask_token is not supported
						with self.assertRaises(__lowercase  ):
										fill_masker('''This is'''  )

						self.run_test_top_k(__lowercase   ,__lowercase  )
						self.run_test_targets(__lowercase   ,__lowercase  )
						self.run_test_top_k_targets(__lowercase   ,__lowercase  )
						self.fill_mask_with_duplicate_targets_and_top_k(__lowercase   ,__lowercase  )
						self.fill_mask_with_multiple_masks(__lowercase   ,__lowercase  )



		def 	__lowerCamelCase			(     self     :Union[str, Any]   ,__lowercase     :Optional[int]   ,__lowercase     :int  ):
						snake_case__	:      int     		=	tokenizer.get_vocab()
						snake_case__	:      Dict     		=	sorted(vocab.keys()  )[:2]
						# Pipeline argument
						snake_case__	:      List[Any]     		=	FillMaskPipeline(model=__lowercase   ,tokenizer=__lowercase   ,targets=__lowercase  )
						snake_case__	:      str     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""  )
						self.assertEqual(
						    __lowercase   ,[
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						    ]   ,)
						snake_case__	:      Optional[Any]     		=	{vocab[el] for el in targets}
						self.assertEqual({el['''token'''] for el in outputs}   ,__lowercase  )
						snake_case__	:      Any     		=	[tokenizer.decode([x]  ) for x in target_ids]
						self.assertEqual({el['''token_str'''] for el in outputs}   ,set(__lowercase  )  )

						# Call argument
						snake_case__	:      str     		=	FillMaskPipeline(model=__lowercase   ,tokenizer=__lowercase  )
						snake_case__	:      int     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,targets=__lowercase  )
						self.assertEqual(
						    __lowercase   ,[
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						    ]   ,)
						snake_case__	:      str     		=	{vocab[el] for el in targets}
						self.assertEqual({el['''token'''] for el in outputs}   ,__lowercase  )
						snake_case__	:      Optional[Any]     		=	[tokenizer.decode([x]  ) for x in target_ids]
						self.assertEqual({el['''token_str'''] for el in outputs}   ,set(__lowercase  )  )

						# Score equivalence
						snake_case__	:      Dict     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,targets=__lowercase  )
						snake_case__	:      Union[str, Any]     		=	[top_mask['''token_str'''] for top_mask in outputs]
						snake_case__	:      Tuple     		=	[top_mask['''score'''] for top_mask in outputs]

						# For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
						if set(__lowercase  ) == set(__lowercase  ):
										snake_case__	:      List[Any]     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,targets=__lowercase  )
										snake_case__	:      int     		=	[top_mask['''score'''] for top_mask in unmasked_targets]
										self.assertEqual(nested_simplify(__lowercase  )   ,nested_simplify(__lowercase  )  )

						# Raises with invalid
						with self.assertRaises(__lowercase  ):
										snake_case__	:      List[str]     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,targets=[]  )
						# For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised
						if "" not in tokenizer.get_vocab():
										with self.assertRaises(__lowercase  ):
														snake_case__	:      List[Any]     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,targets=['''''']  )
										with self.assertRaises(__lowercase  ):
														snake_case__	:      Optional[int]     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,targets=''''''  )



		def 	__lowerCamelCase			(     self     :Any   ,__lowercase     :Union[str, Any]   ,__lowercase     :Dict  ):
						snake_case__	:      int     		=	FillMaskPipeline(model=__lowercase   ,tokenizer=__lowercase   ,top_k=2  )
						snake_case__	:      Tuple     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""  )
						self.assertEqual(
						    __lowercase   ,[
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						    ]   ,)

						snake_case__	:      Any     		=	FillMaskPipeline(model=__lowercase   ,tokenizer=__lowercase  )
						snake_case__	:      Optional[Any]     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,top_k=2  )
						self.assertEqual(
						    __lowercase   ,[
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						    ]   ,)
						self.assertEqual(nested_simplify(__lowercase  )   ,nested_simplify(__lowercase  )  )



		def 	__lowerCamelCase			(     self     :List[Any]   ,__lowercase     :Tuple   ,__lowercase     :str  ):
						snake_case__	:      Optional[int]     		=	tokenizer.get_vocab()
						snake_case__	:      int     		=	FillMaskPipeline(model=__lowercase   ,tokenizer=__lowercase  )

						# top_k=2, ntargets=3
						snake_case__	:      int     		=	sorted(vocab.keys()  )[:3]
						snake_case__	:      List[Any]     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,top_k=2   ,targets=__lowercase  )

						# If we use the most probably targets, and filter differently, we should still
						# have the same results
						snake_case__	:      Dict     		=	[el['''token_str'''] for el in sorted(__lowercase   ,key=lambda __lowercase  : x["score"]   ,reverse=__lowercase  )]
						# For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
						if set(__lowercase  ).issubset(__lowercase  ):
										snake_case__	:      List[Any]     		=	fill_masker(F"""This is a {tokenizer.mask_token}"""   ,top_k=3   ,targets=__lowercase  )
										# They should yield exactly the same result
										self.assertEqual(nested_simplify(__lowercase  )   ,nested_simplify(__lowercase  )  )



		def 	__lowerCamelCase			(     self     :Union[str, Any]   ,__lowercase     :Dict   ,__lowercase     :Dict  ):
						snake_case__	:      Union[str, Any]     		=	FillMaskPipeline(model=__lowercase   ,tokenizer=__lowercase  )
						snake_case__	:      str     		=	tokenizer.get_vocab()
						# String duplicates + id duplicates
						snake_case__	:      int     		=	sorted(vocab.keys()  )[:3]
						snake_case__	:      Optional[Any]     		=	[targets[0], targets[1], targets[0], targets[2], targets[1]]
						snake_case__	:      Optional[Any]     		=	fill_masker(F"""My name is {tokenizer.mask_token}"""   ,targets=__lowercase   ,top_k=1_0  )

						# The target list contains duplicates, so we can't output more
						# than them
						self.assertEqual(len(__lowercase  )   ,3  )






		def 	__lowerCamelCase			(     self     :Optional[Any]   ,__lowercase     :List[Any]   ,__lowercase     :Optional[Any]  ):
						snake_case__	:      Any     		=	FillMaskPipeline(model=__lowercase   ,tokenizer=__lowercase  )

						snake_case__	:      Tuple     		=	fill_masker(
						    F"""This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}"""   ,top_k=2  )
						self.assertEqual(
						    __lowercase   ,[
						        [
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        ],
						        [
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        ],
						        [
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						            {'''sequence''': ANY(__lowercase  ), '''score''': ANY(__lowercase  ), '''token''': ANY(__lowercase  ), '''token_str''': ANY(__lowercase  )},
						        ],
						    ]   ,)

