


'''simple docstring'''





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 				UpperCAmelCase			(			unittest.TestCase						):
 UpperCAmelCase				=					MODEL_FOR_MASKED_LM_MAPPING
 UpperCAmelCase				=					TF_MODEL_FOR_MASKED_LM_MAPPING


 def        __SCREAMING_SNAKE_CASE					(      self							:	Tuple ):
    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        __SCREAMING_SNAKE_CASE					(      self							:	Any ):
    UpperCAmelCase__	:Optional[Any]      =      pipeline(task='''fill-mask'''			,       model='''sshleifer/tiny-distilroberta-base'''			,       top_k=2			,       framework='''tf''' )
    UpperCAmelCase__	:Optional[Any]      =      unmasker('''My name is <mask>''' )
    self.assertEqual(
        nested_simplify(__lowerCamelCase			,       decimals=6 )			,       [
            {'''sequence''': '''My name is grouped''', '''score''': 2.1e-05, '''token''': 3_8_0_1_5, '''token_str''': ''' grouped'''},
            {'''sequence''': '''My name is accuser''', '''score''': 2.1e-05, '''token''': 2_5_5_0_6, '''token_str''': ''' accuser'''},
        ]			,       )

    UpperCAmelCase__	:Optional[Any]      =      unmasker('''The largest city in France is <mask>''' )
    self.assertEqual(
        nested_simplify(__lowerCamelCase			,       decimals=6 )			,       [
            {
                '''sequence''': '''The largest city in France is grouped''',
                '''score''': 2.1e-05,
                '''token''': 3_8_0_1_5,
                '''token_str''': ''' grouped''',
            },
            {
                '''sequence''': '''The largest city in France is accuser''',
                '''score''': 2.1e-05,
                '''token''': 2_5_5_0_6,
                '''token_str''': ''' accuser''',
            },
        ]			,       )

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


 @require_torch
 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    UpperCAmelCase__	:Dict      =      pipeline(task='''fill-mask'''			,       model='''sshleifer/tiny-distilroberta-base'''			,       top_k=2			,       framework='''pt''' )

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

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

    UpperCAmelCase__	:Dict      =      unmasker('''My name is <mask>'''			,       targets=[''' Patrick''', ''' Clara''', ''' Teven''']			,       top_k=3 )
    self.assertEqual(
        nested_simplify(__lowerCamelCase			,       decimals=6 )			,       [
            {'''sequence''': '''My name is Patrick''', '''score''': 2.1e-05, '''token''': 3_4_9_9, '''token_str''': ''' Patrick'''},
            {'''sequence''': '''My name is Te''', '''score''': 2e-05, '''token''': 2_9_4_1, '''token_str''': ''' Te'''},
            {'''sequence''': '''My name is Clara''', '''score''': 2e-05, '''token''': 1_3_6_0_6, '''token_str''': ''' Clara'''},
        ]			,       )

    UpperCAmelCase__	:Tuple      =      unmasker('''My name is <mask> <mask>'''			,       top_k=2 )

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


 @require_torch_gpu
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):
    UpperCAmelCase__	:Any      =      pipeline('''fill-mask'''			,       model='''hf-internal-testing/tiny-random-distilbert'''			,       device=0			,       framework='''pt''' )

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

    UpperCAmelCase__	:List[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(__lowerCamelCase			,       __lowerCamelCase )


 @slow
 @require_torch
 def        __SCREAMING_SNAKE_CASE					(      self							:	Tuple ):
    UpperCAmelCase__	:Tuple      =      pipeline(task='''fill-mask'''			,       model='''distilroberta-base'''			,       top_k=2			,       framework='''pt''' )
    self.run_large_test(__lowerCamelCase )


 @slow
 @require_tf
 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    UpperCAmelCase__	:Tuple      =      pipeline(task='''fill-mask'''			,       model='''distilroberta-base'''			,       top_k=2			,       framework='''tf''' )
    self.run_large_test(__lowerCamelCase )


 def        __SCREAMING_SNAKE_CASE					(      self							:	str			,       __lowerCamelCase							:	Optional[Any] ):
    UpperCAmelCase__	:List[str]      =      unmasker('''My name is <mask>''' )
    self.assertEqual(
        nested_simplify(__lowerCamelCase )			,       [
            {'''sequence''': '''My name is John''', '''score''': 0.0_08, '''token''': 6_1_0, '''token_str''': ''' John'''},
            {'''sequence''': '''My name is Chris''', '''score''': 0.0_07, '''token''': 1_5_7_3, '''token_str''': ''' Chris'''},
        ]			,       )
    UpperCAmelCase__	:Dict      =      unmasker('''The largest city in France is <mask>''' )
    self.assertEqual(
        nested_simplify(__lowerCamelCase )			,       [
            {
                '''sequence''': '''The largest city in France is Paris''',
                '''score''': 0.2_51,
                '''token''': 2_2_0_1,
                '''token_str''': ''' Paris''',
            },
            {
                '''sequence''': '''The largest city in France is Lyon''',
                '''score''': 0.2_14,
                '''token''': 1_2_7_9_0,
                '''token_str''': ''' Lyon''',
            },
        ]			,       )

    UpperCAmelCase__	:Union[str, Any]      =      unmasker('''My name is <mask>'''			,       targets=[''' Patrick''', ''' Clara''', ''' Teven''']			,       top_k=3 )
    self.assertEqual(
        nested_simplify(__lowerCamelCase )			,       [
            {'''sequence''': '''My name is Patrick''', '''score''': 0.0_05, '''token''': 3_4_9_9, '''token_str''': ''' Patrick'''},
            {'''sequence''': '''My name is Clara''', '''score''': 0.0_00, '''token''': 1_3_6_0_6, '''token_str''': ''' Clara'''},
            {'''sequence''': '''My name is Te''', '''score''': 0.0_00, '''token''': 2_9_4_1, '''token_str''': ''' Te'''},
        ]			,       )


 @require_torch
 def        __SCREAMING_SNAKE_CASE					(      self							:	int ):
    UpperCAmelCase__	:Tuple      =      pipeline(task='''fill-mask'''			,       model='''sshleifer/tiny-distilroberta-base'''			,       framework='''pt''' )
    UpperCAmelCase__	:Optional[Any]      =      None
    UpperCAmelCase__	:Dict      =      None
    self.run_pipeline_test(__lowerCamelCase			,       [] )


 @require_tf
 def        __SCREAMING_SNAKE_CASE					(      self							:	Any ):
    UpperCAmelCase__	:Tuple      =      pipeline(task='''fill-mask'''			,       model='''sshleifer/tiny-distilroberta-base'''			,       framework='''tf''' )
    UpperCAmelCase__	:Optional[int]      =      None
    UpperCAmelCase__	:Optional[int]      =      None
    self.run_pipeline_test(__lowerCamelCase			,       [] )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	int ):
    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__	:int      =      FillMaskPipeline(model=__lowerCamelCase			,       tokenizer=__lowerCamelCase )
    UpperCAmelCase__	:Any      =      [
        f'''This is another {tokenizer.mask_token} test''',
    ]
    return fill_masker, examples


 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int]			,       __lowerCamelCase							:	int			,       __lowerCamelCase							:	Tuple ):
    UpperCAmelCase__	:List[str]      =      fill_masker.tokenizer
    UpperCAmelCase__	:str      =      fill_masker.model

    UpperCAmelCase__	:List[str]      =      fill_masker(
        f'''This is a {tokenizer.mask_token}'''			,       )
    self.assertEqual(
        __lowerCamelCase			,       [
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
        ]			,       )

    UpperCAmelCase__	:List[Any]      =      fill_masker([f'''This is a {tokenizer.mask_token}'''] )
    self.assertEqual(
        __lowerCamelCase			,       [
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
        ]			,       )

    UpperCAmelCase__	:List[Any]      =      fill_masker([f'''This is a {tokenizer.mask_token}''', f'''Another {tokenizer.mask_token} great test.'''] )
    self.assertEqual(
        __lowerCamelCase			,       [
            [
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            ],
            [
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            ],
        ]			,       )

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

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


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

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

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

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


 def        __SCREAMING_SNAKE_CASE					(      self							:	Tuple			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Optional[Any] ):
    UpperCAmelCase__	:str      =      FillMaskPipeline(model=__lowerCamelCase			,       tokenizer=__lowerCamelCase			,       top_k=2 )
    UpperCAmelCase__	:Optional[Any]      =      fill_masker(f'''This is a {tokenizer.mask_token}''' )
    self.assertEqual(
        __lowerCamelCase			,       [
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
        ]			,       )

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


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

    # top_k=2, ntargets=3
    UpperCAmelCase__	:List[Any]      =      sorted(vocab.keys() )[:3]
    UpperCAmelCase__	:List[str]      =      fill_masker(f'''This is a {tokenizer.mask_token}'''			,       top_k=2			,       targets=__lowerCamelCase )

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


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

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





 def        __SCREAMING_SNAKE_CASE					(      self							:	str			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	Optional[Any] ):
    UpperCAmelCase__	:str      =      FillMaskPipeline(model=__lowerCamelCase			,       tokenizer=__lowerCamelCase )

    UpperCAmelCase__	:str      =      fill_masker(
        f'''This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}'''			,       top_k=2 )
    self.assertEqual(
        __lowerCamelCase			,       [
            [
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            ],
            [
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            ],
            [
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
                {'''sequence''': ANY(__lowerCamelCase ), '''score''': ANY(__lowerCamelCase ), '''token''': ANY(__lowerCamelCase ), '''token_str''': ANY(__lowerCamelCase )},
            ],
        ]			,       )







'''simple docstring'''





import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__lowerCamelCase												=						logging.get_logger(__name__)

__lowerCamelCase												=						{
    '''nvidia/segformer-b0-finetuned-ade-512-512''': (
        '''https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'''
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					"segformer"


 def __init__(      self							:	Dict			,       __lowerCamelCase							:	Dict=3			,       __lowerCamelCase							:	List[str]=4			,       __lowerCamelCase							:	List[Any]=[2, 2, 2, 2]			,       __lowerCamelCase							:	List[str]=[8, 4, 2, 1]			,       __lowerCamelCase							:	Optional[Any]=[3_2, 6_4, 1_6_0, 2_5_6]			,       __lowerCamelCase							:	Tuple=[7, 3, 3, 3]			,       __lowerCamelCase							:	Tuple=[4, 2, 2, 2]			,       __lowerCamelCase							:	Any=[1, 2, 5, 8]			,       __lowerCamelCase							:	Optional[Any]=[4, 4, 4, 4]			,       __lowerCamelCase							:	Union[str, Any]="gelu"			,       __lowerCamelCase							:	Dict=0.0			,       __lowerCamelCase							:	Union[str, Any]=0.0			,       __lowerCamelCase							:	Tuple=0.1			,       __lowerCamelCase							:	Optional[Any]=0.02			,       __lowerCamelCase							:	Optional[int]=0.1			,       __lowerCamelCase							:	str=1e-6			,       __lowerCamelCase							:	List[Any]=2_5_6			,       __lowerCamelCase							:	int=2_5_5			,       **__lowerCamelCase							:	str			,       ):
    super().__init__(**__lowerCamelCase )

    if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
       warnings.warn(
           '''Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'''
           ''' removed, as the behaviour will default to that of reshape_last_stage = True.'''			,       __lowerCamelCase			,       )

    UpperCAmelCase__	:Dict      =      num_channels
    UpperCAmelCase__	:List[Any]      =      num_encoder_blocks
    UpperCAmelCase__	:List[str]      =      depths
    UpperCAmelCase__	:str      =      sr_ratios
    UpperCAmelCase__	:Optional[Any]      =      hidden_sizes
    UpperCAmelCase__	:List[str]      =      patch_sizes
    UpperCAmelCase__	:List[str]      =      strides
    UpperCAmelCase__	:List[str]      =      mlp_ratios
    UpperCAmelCase__	:Dict      =      num_attention_heads
    UpperCAmelCase__	:Tuple      =      hidden_act
    UpperCAmelCase__	:List[str]      =      hidden_dropout_prob
    UpperCAmelCase__	:List[Any]      =      attention_probs_dropout_prob
    UpperCAmelCase__	:int      =      classifier_dropout_prob
    UpperCAmelCase__	:str      =      initializer_range
    UpperCAmelCase__	:List[str]      =      drop_path_rate
    UpperCAmelCase__	:Dict      =      layer_norm_eps
    UpperCAmelCase__	:List[str]      =      decoder_hidden_size
    UpperCAmelCase__	:Optional[int]      =      kwargs.get('''reshape_last_stage'''			,       __lowerCamelCase )
    UpperCAmelCase__	:str      =      semantic_loss_ignore_index




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					version.parse("1.11"						)


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any] ):
    return OrderedDict(
        [
            ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
        ] )


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int] ):
    return 1e-4





 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[str] ):
    return 1_2



