



'''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 						a__  (					unittest.TestCase						):
	__magic_name__		:     Optional[int]						     =  MODEL_FOR_MASKED_LM_MAPPING
	__magic_name__		:     Any						     =  TF_MODEL_FOR_MASKED_LM_MAPPING


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

					"""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 					lowercase__	(self			:			List[Any]   )  ->      Optional[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Tuple          =			pipeline(task='''fill-mask''',       model='''sshleifer/tiny-distilroberta-base''',       top_k=2,       framework='''tf'''   )
					SCREAMING_SNAKE_CASE	:    List[str]          =			unmasker('''My name is <mask>'''   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase,       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'''},
					    ],       )

					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			unmasker('''The largest city in France is <mask>'''   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase,       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''',
					        },
					    ],       )

					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			unmasker('''My name is <mask>''',       targets=[''' Patrick''', ''' Clara''', ''' Teven'''],       top_k=3   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase,       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 					lowercase__	(self			:			int   )  ->      Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    str          =			pipeline(task='''fill-mask''',       model='''sshleifer/tiny-distilroberta-base''',       top_k=2,       framework='''pt'''   )

					SCREAMING_SNAKE_CASE	:    int          =			unmasker('''My name is <mask>'''   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase,       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'''},
					    ],       )

					SCREAMING_SNAKE_CASE	:    Tuple          =			unmasker('''The largest city in France is <mask>'''   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase,       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'''},
					    ],       )

					SCREAMING_SNAKE_CASE	:    str          =			unmasker('''My name is <mask>''',       targets=[''' Patrick''', ''' Clara''', ''' Teven'''],       top_k=3   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase,       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'''},
					    ],       )

					SCREAMING_SNAKE_CASE	:    str          =			unmasker('''My name is <mask> <mask>''',       top_k=2   )

					self.assertEqual(
					    nested_simplify(__UpperCAmelCase,       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 					lowercase__	(self			:			Optional[int]   )  ->      List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Dict          =			pipeline('''fill-mask''',       model='''hf-internal-testing/tiny-random-distilbert''',       device=0,       framework='''pt'''   )

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

					SCREAMING_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(__UpperCAmelCase,       __UpperCAmelCase   )


	@slow
	@require_torch
	def 					lowercase__	(self			:			Optional[int]   )  ->      Dict:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			pipeline(task='''fill-mask''',       model='''distilroberta-base''',       top_k=2,       framework='''pt'''   )
					self.run_large_test(__UpperCAmelCase   )


	@slow
	@require_tf
	def 					lowercase__	(self			:			List[str]   )  ->      Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    List[str]          =			pipeline(task='''fill-mask''',       model='''distilroberta-base''',       top_k=2,       framework='''tf'''   )
					self.run_large_test(__UpperCAmelCase   )


	def 					lowercase__	(self			:			str,       __UpperCAmelCase			:			Dict   )  ->      str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    List[Any]          =			unmasker('''My name is <mask>'''   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase   ),       [
					        {'''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'''},
					    ],       )
					SCREAMING_SNAKE_CASE	:    int          =			unmasker('''The largest city in France is <mask>'''   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase   ),       [
					        {
					            '''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''',
					        },
					    ],       )

					SCREAMING_SNAKE_CASE	:    str          =			unmasker('''My name is <mask>''',       targets=[''' Patrick''', ''' Clara''', ''' Teven'''],       top_k=3   )
					self.assertEqual(
					    nested_simplify(__UpperCAmelCase   ),       [
					        {'''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 					lowercase__	(self			:			Optional[int]   )  ->      str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Any          =			pipeline(task='''fill-mask''',       model='''sshleifer/tiny-distilroberta-base''',       framework='''pt'''   )
					SCREAMING_SNAKE_CASE	:    List[str]          =			None
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			None
					self.run_pipeline_test(__UpperCAmelCase,       []   )


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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Optional[int]          =			pipeline(task='''fill-mask''',       model='''sshleifer/tiny-distilroberta-base''',       framework='''tf'''   )
					SCREAMING_SNAKE_CASE	:    Any          =			None
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			None
					self.run_pipeline_test(__UpperCAmelCase,       []   )


	def 					lowercase__	(self			:			int,       __UpperCAmelCase			:			Union[str, Any],       __UpperCAmelCase			:			Any,       __UpperCAmelCase			:			List[str]   )  ->      Dict:

					"""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)'''   )

					SCREAMING_SNAKE_CASE	:    Any          =			FillMaskPipeline(model=__UpperCAmelCase,       tokenizer=__UpperCAmelCase   )
					SCREAMING_SNAKE_CASE	:    Dict          =			[
					    F'''This is another {tokenizer.mask_token} test''',
					]
					return fill_masker, examples


	def 					lowercase__	(self			:			Any,       __UpperCAmelCase			:			Any,       __UpperCAmelCase			:			Optional[Any]   )  ->      List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    List[Any]          =			fill_masker.tokenizer
					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			fill_masker.model

					SCREAMING_SNAKE_CASE	:    Optional[int]          =			fill_masker(
					    F'''This is a {tokenizer.mask_token}''',       )
					self.assertEqual(
					    __UpperCAmelCase,       [
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					    ],       )

					SCREAMING_SNAKE_CASE	:    Optional[int]          =			fill_masker([F'''This is a {tokenizer.mask_token}''']   )
					self.assertEqual(
					    __UpperCAmelCase,       [
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					    ],       )

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

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

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


	def 					lowercase__	(self			:			Any,       __UpperCAmelCase			:			Dict,       __UpperCAmelCase			:			Tuple   )  ->      str:

					"""simple docstring"""

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

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

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

					# Raises with invalid
					with self.assertRaises(__UpperCAmelCase   ):
									SCREAMING_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(__UpperCAmelCase   ):
													SCREAMING_SNAKE_CASE	:    Optional[int]          =			fill_masker(F'''This is a {tokenizer.mask_token}''',       targets=['''''']   )
									with self.assertRaises(__UpperCAmelCase   ):
													SCREAMING_SNAKE_CASE	:    str          =			fill_masker(F'''This is a {tokenizer.mask_token}''',       targets=''''''   )


	def 					lowercase__	(self			:			Tuple,       __UpperCAmelCase			:			str,       __UpperCAmelCase			:			List[Any]   )  ->      Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Any          =			FillMaskPipeline(model=__UpperCAmelCase,       tokenizer=__UpperCAmelCase,       top_k=2   )
					SCREAMING_SNAKE_CASE	:    Dict          =			fill_masker(F'''This is a {tokenizer.mask_token}'''   )
					self.assertEqual(
					    __UpperCAmelCase,       [
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					    ],       )

					SCREAMING_SNAKE_CASE	:    Dict          =			FillMaskPipeline(model=__UpperCAmelCase,       tokenizer=__UpperCAmelCase   )
					SCREAMING_SNAKE_CASE	:    Optional[int]          =			fill_masker(F'''This is a {tokenizer.mask_token}''',       top_k=2   )
					self.assertEqual(
					    __UpperCAmelCase,       [
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					    ],       )
					self.assertEqual(nested_simplify(__UpperCAmelCase   ),       nested_simplify(__UpperCAmelCase   )   )


	def 					lowercase__	(self			:			Any,       __UpperCAmelCase			:			Optional[int],       __UpperCAmelCase			:			Any   )  ->      Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			tokenizer.get_vocab()
					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			FillMaskPipeline(model=__UpperCAmelCase,       tokenizer=__UpperCAmelCase   )

					# top_k=2, ntargets=3
					SCREAMING_SNAKE_CASE	:    str          =			sorted(vocab.keys()   )[:3]
					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			fill_masker(F'''This is a {tokenizer.mask_token}''',       top_k=2,       targets=__UpperCAmelCase   )

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


	def 					lowercase__	(self			:			Optional[Any],       __UpperCAmelCase			:			Tuple,       __UpperCAmelCase			:			List[Any]   )  ->      str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Dict          =			FillMaskPipeline(model=__UpperCAmelCase,       tokenizer=__UpperCAmelCase   )
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			tokenizer.get_vocab()
					# String duplicates + id duplicates
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			sorted(vocab.keys()   )[:3]
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			[targets[0], targets[1], targets[0], targets[2], targets[1]]
					SCREAMING_SNAKE_CASE	:    Optional[int]          =			fill_masker(F'''My name is {tokenizer.mask_token}''',       targets=__UpperCAmelCase,       top_k=10   )

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






	def 					lowercase__	(self			:			Tuple,       __UpperCAmelCase			:			Union[str, Any],       __UpperCAmelCase			:			Optional[Any]   )  ->      Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    List[str]          =			FillMaskPipeline(model=__UpperCAmelCase,       tokenizer=__UpperCAmelCase   )

					SCREAMING_SNAKE_CASE	:    Dict          =			fill_masker(
					    F'''This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}''',       top_k=2   )
					self.assertEqual(
					    __UpperCAmelCase,       [
					        [
					            {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					            {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        ],
					        [
					            {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					            {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        ],
					        [
					            {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					            {'''sequence''': ANY(__UpperCAmelCase   ), '''score''': ANY(__UpperCAmelCase   ), '''token''': ANY(__UpperCAmelCase   ), '''token_str''': ANY(__UpperCAmelCase   )},
					        ],
					    ],       )












"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_lowerCAmelCase            =	{
    'configuration_mobilebert': [
        'MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileBertConfig',
        'MobileBertOnnxConfig',
    ],
    'tokenization_mobilebert': ['MobileBertTokenizer'],
}

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_lowerCAmelCase            =	['MobileBertTokenizerFast']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_lowerCAmelCase            =	[
				    'MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'MobileBertForMaskedLM',
				    'MobileBertForMultipleChoice',
				    'MobileBertForNextSentencePrediction',
				    'MobileBertForPreTraining',
				    'MobileBertForQuestionAnswering',
				    'MobileBertForSequenceClassification',
				    'MobileBertForTokenClassification',
				    'MobileBertLayer',
				    'MobileBertModel',
				    'MobileBertPreTrainedModel',
				    'load_tf_weights_in_mobilebert',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_lowerCAmelCase            =	[
				    'TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'TFMobileBertForMaskedLM',
				    'TFMobileBertForMultipleChoice',
				    'TFMobileBertForNextSentencePrediction',
				    'TFMobileBertForPreTraining',
				    'TFMobileBertForQuestionAnswering',
				    'TFMobileBertForSequenceClassification',
				    'TFMobileBertForTokenClassification',
				    'TFMobileBertMainLayer',
				    'TFMobileBertModel',
				    'TFMobileBertPreTrainedModel',
				]


if TYPE_CHECKING:
				from .configuration_mobilebert import (
				    MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
				    MobileBertConfig,
				    MobileBertOnnxConfig,
				)
				from .tokenization_mobilebert import MobileBertTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_mobilebert_fast import MobileBertTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_mobilebert import (
								    MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    MobileBertForMaskedLM,
								    MobileBertForMultipleChoice,
								    MobileBertForNextSentencePrediction,
								    MobileBertForPreTraining,
								    MobileBertForQuestionAnswering,
								    MobileBertForSequenceClassification,
								    MobileBertForTokenClassification,
								    MobileBertLayer,
								    MobileBertModel,
								    MobileBertPreTrainedModel,
								    load_tf_weights_in_mobilebert,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_mobilebert import (
								    TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFMobileBertForMaskedLM,
								    TFMobileBertForMultipleChoice,
								    TFMobileBertForNextSentencePrediction,
								    TFMobileBertForPreTraining,
								    TFMobileBertForQuestionAnswering,
								    TFMobileBertForSequenceClassification,
								    TFMobileBertForTokenClassification,
								    TFMobileBertMainLayer,
								    TFMobileBertModel,
								    TFMobileBertPreTrainedModel,
								)

else:
				import sys

				_lowerCAmelCase            =	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
