





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 					lowercase__ (			unittest.TestCase       ):

   '''simple docstring'''


   a		:							List[str]     	=	MODEL_FOR_MASKED_LM_MAPPING
   a		:							Optional[int]     	=	TF_MODEL_FOR_MASKED_LM_MAPPING




   def    UpperCamelCase__						(					self							)				->   List[str]:






       """simple docstring"""
       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    UpperCamelCase__						(					self							)				->   str:






       """simple docstring"""
       UpperCamelCase__							:       Union[str, Any]      =   pipeline(task='''fill-mask''',	model='''sshleifer/tiny-distilroberta-base''',	top_k=2,	framework='''tf'''							)
       UpperCamelCase__							:       List[Any]      =   unmasker('''My name is <mask>'''							)
       self.assertEqual(
           nested_simplify(__magic_name__,	decimals=6							),	[
               {'''sequence''': '''My name is grouped''', '''score''': 2.1E-05, '''token''': 38015, '''token_str''': ''' grouped'''},
               {'''sequence''': '''My name is accuser''', '''score''': 2.1E-05, '''token''': 25506, '''token_str''': ''' accuser'''},
           ],	)

       UpperCamelCase__							:       Union[str, Any]      =   unmasker('''The largest city in France is <mask>'''							)
       self.assertEqual(
           nested_simplify(__magic_name__,	decimals=6							),	[
               {
                   '''sequence''': '''The largest city in France is grouped''',
                   '''score''': 2.1E-05,
                   '''token''': 38015,
                   '''token_str''': ''' grouped''',
               },
               {
                   '''sequence''': '''The largest city in France is accuser''',
                   '''score''': 2.1E-05,
                   '''token''': 25506,
                   '''token_str''': ''' accuser''',
               },
           ],	)

       UpperCamelCase__							:       Union[str, Any]      =   unmasker('''My name is <mask>''',	targets=[''' Patrick''', ''' Clara''', ''' Teven'''],	top_k=3							)
       self.assertEqual(
           nested_simplify(__magic_name__,	decimals=6							),	[
               {'''sequence''': '''My name is Clara''', '''score''': 2E-05, '''token''': 13606, '''token_str''': ''' Clara'''},
               {'''sequence''': '''My name is Patrick''', '''score''': 2E-05, '''token''': 3499, '''token_str''': ''' Patrick'''},
               {'''sequence''': '''My name is Te''', '''score''': 1.9E-05, '''token''': 2941, '''token_str''': ''' Te'''},
           ],	)




   @require_torch
   def    UpperCamelCase__						(					self							)				->   Tuple:






       """simple docstring"""
       UpperCamelCase__							:       Dict      =   pipeline(task='''fill-mask''',	model='''sshleifer/tiny-distilroberta-base''',	top_k=2,	framework='''pt'''							)

       UpperCamelCase__							:       Any      =   unmasker('''My name is <mask>'''							)
       self.assertEqual(
           nested_simplify(__magic_name__,	decimals=6							),	[
               {'''sequence''': '''My name is Maul''', '''score''': 2.2E-05, '''token''': 35676, '''token_str''': ''' Maul'''},
               {'''sequence''': '''My name isELS''', '''score''': 2.2E-05, '''token''': 16416, '''token_str''': '''ELS'''},
           ],	)

       UpperCamelCase__							:       Any      =   unmasker('''The largest city in France is <mask>'''							)
       self.assertEqual(
           nested_simplify(__magic_name__,	decimals=6							),	[
               {
                   '''sequence''': '''The largest city in France is Maul''',
                   '''score''': 2.2E-05,
                   '''token''': 35676,
                   '''token_str''': ''' Maul''',
               },
               {'''sequence''': '''The largest city in France isELS''', '''score''': 2.2E-05, '''token''': 16416, '''token_str''': '''ELS'''},
           ],	)

       UpperCamelCase__							:       str      =   unmasker('''My name is <mask>''',	targets=[''' Patrick''', ''' Clara''', ''' Teven'''],	top_k=3							)
       self.assertEqual(
           nested_simplify(__magic_name__,	decimals=6							),	[
               {'''sequence''': '''My name is Patrick''', '''score''': 2.1E-05, '''token''': 3499, '''token_str''': ''' Patrick'''},
               {'''sequence''': '''My name is Te''', '''score''': 2E-05, '''token''': 2941, '''token_str''': ''' Te'''},
               {'''sequence''': '''My name is Clara''', '''score''': 2E-05, '''token''': 13606, '''token_str''': ''' Clara'''},
           ],	)

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

       self.assertEqual(
           nested_simplify(__magic_name__,	decimals=6							),	[
               [
                   {
                       '''score''': 2.2E-05,
                       '''token''': 35676,
                       '''token_str''': ''' Maul''',
                       '''sequence''': '''<s>My name is Maul<mask></s>''',
                   },
                   {'''score''': 2.2E-05, '''token''': 16416, '''token_str''': '''ELS''', '''sequence''': '''<s>My name isELS<mask></s>'''},
               ],
               [
                   {
                       '''score''': 2.2E-05,
                       '''token''': 35676,
                       '''token_str''': ''' Maul''',
                       '''sequence''': '''<s>My name is<mask> Maul</s>''',
                   },
                   {'''score''': 2.2E-05, '''token''': 16416, '''token_str''': '''ELS''', '''sequence''': '''<s>My name is<mask>ELS</s>'''},
               ],
           ],	)




   @require_torch_gpu
   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       Union[str, Any]      =   pipeline('''fill-mask''',	model='''hf-internal-testing/tiny-random-distilbert''',	device=0,	framework='''pt'''							)

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

       UpperCamelCase__							:       Any      =   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(__magic_name__,	__magic_name__							)




   @slow
   @require_torch
   def    UpperCamelCase__						(					self							)				->   List[str]:






       """simple docstring"""
       UpperCamelCase__							:       Dict      =   pipeline(task='''fill-mask''',	model='''distilroberta-base''',	top_k=2,	framework='''pt'''							)
       self.run_large_test(__magic_name__							)




   @slow
   @require_tf
   def    UpperCamelCase__						(					self							)				->   int:






       """simple docstring"""
       UpperCamelCase__							:       int      =   pipeline(task='''fill-mask''',	model='''distilroberta-base''',	top_k=2,	framework='''tf'''							)
       self.run_large_test(__magic_name__							)




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






       """simple docstring"""
       UpperCamelCase__							:       Union[str, Any]      =   unmasker('''My name is <mask>'''							)
       self.assertEqual(
           nested_simplify(__magic_name__							),	[
               {'''sequence''': '''My name is John''', '''score''': 0.008, '''token''': 610, '''token_str''': ''' John'''},
               {'''sequence''': '''My name is Chris''', '''score''': 0.007, '''token''': 1573, '''token_str''': ''' Chris'''},
           ],	)
       UpperCamelCase__							:       int      =   unmasker('''The largest city in France is <mask>'''							)
       self.assertEqual(
           nested_simplify(__magic_name__							),	[
               {
                   '''sequence''': '''The largest city in France is Paris''',
                   '''score''': 0.251,
                   '''token''': 2201,
                   '''token_str''': ''' Paris''',
               },
               {
                   '''sequence''': '''The largest city in France is Lyon''',
                   '''score''': 0.214,
                   '''token''': 12790,
                   '''token_str''': ''' Lyon''',
               },
           ],	)

       UpperCamelCase__							:       List[str]      =   unmasker('''My name is <mask>''',	targets=[''' Patrick''', ''' Clara''', ''' Teven'''],	top_k=3							)
       self.assertEqual(
           nested_simplify(__magic_name__							),	[
               {'''sequence''': '''My name is Patrick''', '''score''': 0.005, '''token''': 3499, '''token_str''': ''' Patrick'''},
               {'''sequence''': '''My name is Clara''', '''score''': 0.000, '''token''': 13606, '''token_str''': ''' Clara'''},
               {'''sequence''': '''My name is Te''', '''score''': 0.000, '''token''': 2941, '''token_str''': ''' Te'''},
           ],	)




   @require_torch
   def    UpperCamelCase__						(					self							)				->   List[Any]:






       """simple docstring"""
       UpperCamelCase__							:       str      =   pipeline(task='''fill-mask''',	model='''sshleifer/tiny-distilroberta-base''',	framework='''pt'''							)
       UpperCamelCase__							:       List[Any]      =   None
       UpperCamelCase__							:       Optional[int]      =   None
       self.run_pipeline_test(__magic_name__,	[]							)




   @require_tf
   def    UpperCamelCase__						(					self							)				->   Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:       Optional[Any]      =   pipeline(task='''fill-mask''',	model='''sshleifer/tiny-distilroberta-base''',	framework='''tf'''							)
       UpperCamelCase__							:       Optional[Any]      =   None
       UpperCamelCase__							:       Tuple      =   None
       self.run_pipeline_test(__magic_name__,	[]							)




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__,	__magic_name__							)				->   Tuple:






       """simple docstring"""
       if tokenizer is None or tokenizer.mask_token_id is None:
           self.skipTest('''The provided tokenizer has no mask token, (probably reformer or wav2vec2)'''							)

       UpperCamelCase__							:       Any      =   FillMaskPipeline(model=__magic_name__,	tokenizer=__magic_name__							)
       UpperCamelCase__							:       str      =   [
           f"This is another {tokenizer.mask_token} test",
       ]
       return fill_masker, examples




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__							)				->   str:






       """simple docstring"""
       UpperCamelCase__							:       Any      =   fill_masker.tokenizer
       UpperCamelCase__							:       List[str]      =   fill_masker.model

       UpperCamelCase__							:       int      =   fill_masker(
           f"This is a {tokenizer.mask_token}",	)
       self.assertEqual(
           __magic_name__,	[
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
           ],	)

       UpperCamelCase__							:       Optional[int]      =   fill_masker([f"This is a {tokenizer.mask_token}"]							)
       self.assertEqual(
           __magic_name__,	[
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
           ],	)

       UpperCamelCase__							:       Union[str, Any]      =   fill_masker([f"This is a {tokenizer.mask_token}", f"Another {tokenizer.mask_token} great test."]							)
       self.assertEqual(
           __magic_name__,	[
               [
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               ],
               [
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               ],
           ],	)

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

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




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






       """simple docstring"""
       UpperCamelCase__							:       str      =   tokenizer.get_vocab()
       UpperCamelCase__							:       Dict      =   sorted(vocab.keys()							)[:2]
       # Pipeline argument
       UpperCamelCase__							:       Union[str, Any]      =   FillMaskPipeline(model=__magic_name__,	tokenizer=__magic_name__,	targets=__magic_name__							)
       UpperCamelCase__							:       str      =   fill_masker(f"This is a {tokenizer.mask_token}"							)
       self.assertEqual(
           __magic_name__,	[
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
           ],	)
       UpperCamelCase__							:       Dict      =   {vocab[el] for el in targets}
       self.assertEqual({el['''token'''] for el in outputs},	__magic_name__							)
       UpperCamelCase__							:       str      =   [tokenizer.decode([x]							) for x in target_ids]
       self.assertEqual({el['''token_str'''] for el in outputs},	set(__magic_name__							)							)

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

       # Score equivalence
       UpperCamelCase__							:       List[str]      =   fill_masker(f"This is a {tokenizer.mask_token}",	targets=__magic_name__							)
       UpperCamelCase__							:       int      =   [top_mask['''token_str'''] for top_mask in outputs]
       UpperCamelCase__							:       str      =   [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(__magic_name__							) == set(__magic_name__							):
           UpperCamelCase__							:       List[str]      =   fill_masker(f"This is a {tokenizer.mask_token}",	targets=__magic_name__							)
           UpperCamelCase__							:       Optional[int]      =   [top_mask['''score'''] for top_mask in unmasked_targets]
           self.assertEqual(nested_simplify(__magic_name__							),	nested_simplify(__magic_name__							)							)

       # Raises with invalid
       with self.assertRaises(__magic_name__							):
           UpperCamelCase__							:       Optional[Any]      =   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(__magic_name__							):
               UpperCamelCase__							:       str      =   fill_masker(f"This is a {tokenizer.mask_token}",	targets=['''''']							)
           with self.assertRaises(__magic_name__							):
               UpperCamelCase__							:       Any      =   fill_masker(f"This is a {tokenizer.mask_token}",	targets=''''''							)




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__							)				->   Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:       int      =   FillMaskPipeline(model=__magic_name__,	tokenizer=__magic_name__,	top_k=2							)
       UpperCamelCase__							:       List[str]      =   fill_masker(f"This is a {tokenizer.mask_token}"							)
       self.assertEqual(
           __magic_name__,	[
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
           ],	)

       UpperCamelCase__							:       Tuple      =   FillMaskPipeline(model=__magic_name__,	tokenizer=__magic_name__							)
       UpperCamelCase__							:       Tuple      =   fill_masker(f"This is a {tokenizer.mask_token}",	top_k=2							)
       self.assertEqual(
           __magic_name__,	[
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
           ],	)
       self.assertEqual(nested_simplify(__magic_name__							),	nested_simplify(__magic_name__							)							)




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       Any      =   tokenizer.get_vocab()
       UpperCamelCase__							:       str      =   FillMaskPipeline(model=__magic_name__,	tokenizer=__magic_name__							)

       # top_k=2, ntargets=3
       UpperCamelCase__							:       Optional[Any]      =   sorted(vocab.keys()							)[:3]
       UpperCamelCase__							:       int      =   fill_masker(f"This is a {tokenizer.mask_token}",	top_k=2,	targets=__magic_name__							)

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




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__							)				->   Any:






       """simple docstring"""
       UpperCamelCase__							:       Union[str, Any]      =   FillMaskPipeline(model=__magic_name__,	tokenizer=__magic_name__							)
       UpperCamelCase__							:       Optional[int]      =   tokenizer.get_vocab()
       # String duplicates + id duplicates
       UpperCamelCase__							:       int      =   sorted(vocab.keys()							)[:3]
       UpperCamelCase__							:       List[str]      =   [targets[0], targets[1], targets[0], targets[2], targets[1]]
       UpperCamelCase__							:       Optional[int]      =   fill_masker(f"My name is {tokenizer.mask_token}",	targets=__magic_name__,	top_k=10							)

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




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       Optional[Any]      =   FillMaskPipeline(model=__magic_name__,	tokenizer=__magic_name__							)

       UpperCamelCase__							:       Any      =   fill_masker(
           f"This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}",	top_k=2							)
       self.assertEqual(
           __magic_name__,	[
               [
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               ],
               [
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               ],
               [
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
                   {'''sequence''': ANY(__magic_name__							), '''score''': ANY(__magic_name__							), '''token''': ANY(__magic_name__							), '''token_str''': ANY(__magic_name__							)},
               ],
           ],	)








import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 transformers import CvtForImageClassification, CvtModel
  from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor






class 					lowercase__ (			__lowerCamelCase       ):

   '''simple docstring'''


   def    UpperCamelCase__						(					self							)				->   Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:       Optional[Any]      =   self.config_class(**self.inputs_dict							)
       self.parent.assertTrue(hasattr(__magic_name__,	'''embed_dim'''							)							)
       self.parent.assertTrue(hasattr(__magic_name__,	'''num_heads'''							)							)






class 					lowercase__ :

   '''simple docstring'''


   def __init__(					self,	__magic_name__,	__magic_name__=13,	__magic_name__=64,	__magic_name__=3,	__magic_name__=[16, 48, 96],	__magic_name__=[1, 3, 6],	__magic_name__=[1, 2, 10],	__magic_name__=[7, 3, 3],	__magic_name__=[4, 2, 2],	__magic_name__=[2, 1, 1],	__magic_name__=[2, 2, 2],	__magic_name__=[False, False, True],	__magic_name__=[0.0, 0.0, 0.0],	__magic_name__=0.02,	__magic_name__=1E-12,	__magic_name__=True,	__magic_name__=True,	__magic_name__=2,	)				->   int:






       """simple docstring"""
       UpperCamelCase__							:       Optional[int]      =   parent
       UpperCamelCase__							:       Optional[Any]      =   batch_size
       UpperCamelCase__							:       Dict      =   image_size
       UpperCamelCase__							:       Optional[Any]      =   patch_sizes
       UpperCamelCase__							:       List[str]      =   patch_stride
       UpperCamelCase__							:       Tuple      =   patch_padding
       UpperCamelCase__							:       Dict      =   is_training
       UpperCamelCase__							:       Optional[int]      =   use_labels
       UpperCamelCase__							:       Union[str, Any]      =   num_labels
       UpperCamelCase__							:       List[Any]      =   num_channels
       UpperCamelCase__							:       Optional[Any]      =   embed_dim
       UpperCamelCase__							:       List[str]      =   num_heads
       UpperCamelCase__							:       Any      =   stride_kv
       UpperCamelCase__							:       Any      =   depth
       UpperCamelCase__							:       Tuple      =   cls_token
       UpperCamelCase__							:       List[Any]      =   attention_drop_rate
       UpperCamelCase__							:       Any      =   initializer_range
       UpperCamelCase__							:       str      =   layer_norm_eps




   def    UpperCamelCase__						(					self							)				->   Any:






       """simple docstring"""
       UpperCamelCase__							:       Optional[int]      =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

       UpperCamelCase__							:       int      =   None
       if self.use_labels:
           UpperCamelCase__							:       Optional[int]      =   ids_tensor([self.batch_size],	self.num_labels							)

       UpperCamelCase__							:       str      =   self.get_config()
       return config, pixel_values, labels




   def    UpperCamelCase__						(					self							)				->   int:






       """simple docstring"""
       return CvtConfig(
           image_size=self.image_size,	num_labels=self.num_labels,	num_channels=self.num_channels,	embed_dim=self.embed_dim,	num_heads=self.num_heads,	patch_sizes=self.patch_sizes,	patch_padding=self.patch_padding,	patch_stride=self.patch_stride,	stride_kv=self.stride_kv,	depth=self.depth,	cls_token=self.cls_token,	attention_drop_rate=self.attention_drop_rate,	initializer_range=self.initializer_range,	)




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__,	__magic_name__							)				->   Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:       Optional[Any]      =   CvtModel(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:       Optional[int]      =   model(__magic_name__							)
       UpperCamelCase__							:       List[str]      =   (self.image_size, self.image_size)
       UpperCamelCase__     ,UpperCamelCase__							:       Tuple      =   image_size[0], image_size[1]
       for i in range(len(self.depth							)							):
           UpperCamelCase__							:       Optional[int]      =   floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1							)
           UpperCamelCase__							:       int      =   floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1							)
       self.parent.assertEqual(result.last_hidden_state.shape,	(self.batch_size, self.embed_dim[-1], height, width)							)




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__,	__magic_name__							)				->   List[str]:






       """simple docstring"""
       UpperCamelCase__							:       Tuple      =   self.num_labels
       UpperCamelCase__							:       str      =   CvtForImageClassification(__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:       Any      =   model(__magic_name__,	labels=__magic_name__							)
       self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.num_labels)							)




   def    UpperCamelCase__						(					self							)				->   Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:       int      =   self.prepare_config_and_inputs()
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Optional[int]      =   config_and_inputs
       UpperCamelCase__							:       List[Any]      =   {'''pixel_values''': pixel_values}
       return config, inputs_dict






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

   '''simple docstring'''


   a		:							List[Any]     	=	(CvtModel, CvtForImageClassification) if is_torch_available() else ()
   a		:							Any     	=	(
       {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
       if is_torch_available()
       else {}
   )

   a		:							List[str]     	=	False
   a		:							int     	=	False
   a		:							Tuple     	=	False
   a		:							int     	=	False
   a		:							Tuple     	=	False




   def    UpperCamelCase__						(					self							)				->   str:






       """simple docstring"""
       UpperCamelCase__							:       Dict      =   CvtModelTester(self							)
       UpperCamelCase__							:       List[Any]      =   ConfigTester(self,	config_class=__magic_name__,	has_text_modality=__magic_name__,	hidden_size=37							)




   def    UpperCamelCase__						(					self							)				->   List[str]:






       """simple docstring"""
       self.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()




   def    UpperCamelCase__						(					self							)				->   List[str]:






       """simple docstring"""
       return




   @unittest.skip(reason='''Cvt does not output attentions'''							)
   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       pass




   @unittest.skip(reason='''Cvt does not use inputs_embeds'''							)
   def    UpperCamelCase__						(					self							)				->   Tuple:






       """simple docstring"""
       pass




   @unittest.skip(reason='''Cvt does not support input and output embeddings'''							)
   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       pass




   def    UpperCamelCase__						(					self							)				->   Optional[Any]:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__							:       Optional[int]      =   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCamelCase__							:       List[Any]      =   model_class(__magic_name__							)
           UpperCamelCase__							:       List[Any]      =   inspect.signature(model.forward							)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           UpperCamelCase__							:       Optional[int]      =   [*signature.parameters.keys()]

           UpperCamelCase__							:       Tuple      =   ['''pixel_values''']
           self.assertListEqual(arg_names[:1],	__magic_name__							)




   def    UpperCamelCase__						(					self							)				->   Tuple:






       """simple docstring"""
       UpperCamelCase__							:       Union[str, Any]      =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__magic_name__							)




   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       def check_hidden_states_output(__magic_name__,	__magic_name__,	__magic_name__							):
           UpperCamelCase__							:       str      =   model_class(__magic_name__							)
           model.to(__magic_name__							)
           model.eval()

           with torch.no_grad():
               UpperCamelCase__							:       List[Any]      =   model(**self._prepare_for_class(__magic_name__,	__magic_name__							)							)

           UpperCamelCase__							:       str      =   outputs.hidden_states

           UpperCamelCase__							:       Dict      =   len(self.model_tester.depth							)
           self.assertEqual(len(__magic_name__							),	__magic_name__							)

           # verify the first hidden states (first block)
           self.assertListEqual(
               list(hidden_states[0].shape[-3:]							),	[
                   self.model_tester.embed_dim[0],
                   self.model_tester.image_size // 4,
                   self.model_tester.image_size // 4,
               ],	)

       UpperCamelCase__     ,UpperCamelCase__							:       List[str]      =   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCamelCase__							:       Optional[int]      =   True
           check_hidden_states_output(__magic_name__,	__magic_name__,	__magic_name__							)

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           UpperCamelCase__							:       int      =   True

           check_hidden_states_output(__magic_name__,	__magic_name__,	__magic_name__							)




   def    UpperCamelCase__						(					self							)				->   List[Any]:






       """simple docstring"""
       UpperCamelCase__							:       Optional[int]      =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__magic_name__							)




   @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''							)
   def    UpperCamelCase__						(					self							)				->   Optional[Any]:






       """simple docstring"""
       pass




   @slow
   def    UpperCamelCase__						(					self							)				->   Tuple:






       """simple docstring"""
       for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__							:       Union[str, Any]      =   CvtModel.from_pretrained(__magic_name__							)
           self.assertIsNotNone(__magic_name__							)




def 	lowerCAmelCase_					(						)   ->			int:
    UpperCamelCase__							:       Tuple      =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
    return image






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

   '''simple docstring'''


   @cached_property
   def    UpperCamelCase__						(					self							)				->   Any:






       """simple docstring"""
       return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)




   @slow
   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       Optional[Any]      =   CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]							).to(__magic_name__							)

       UpperCamelCase__							:       Union[str, Any]      =   self.default_image_processor
       UpperCamelCase__							:       Any      =   prepare_img()
       UpperCamelCase__							:       List[str]      =   image_processor(images=__magic_name__,	return_tensors='''pt'''							).to(__magic_name__							)

       # forward pass
       with torch.no_grad():
           UpperCamelCase__							:       Tuple      =   model(**__magic_name__							)

       # verify the logits
       UpperCamelCase__							:       Tuple      =   torch.Size((1, 1000)							)
       self.assertEqual(outputs.logits.shape,	__magic_name__							)

       UpperCamelCase__							:       List[Any]      =   torch.tensor([0.9285, 0.9015, -0.3150]							).to(__magic_name__							)

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

