

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    ):


					'''simple docstring'''






					UpperCAmelCase__:       Any  							=	MODEL_FOR_MASKED_LM_MAPPING
					UpperCAmelCase__:       Any  							=	TF_MODEL_FOR_MASKED_LM_MAPPING






					def   __A      (			self      ):
						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   __A      (			self      ):
						A__							:   Any					       =		pipeline(task="""fill-mask"""   ,	model="""sshleifer/tiny-distilroberta-base"""   ,	top_k=2   ,	framework="""tf"""      )
						A__							:   Dict					       =		unmasker("""My name is <mask>"""      )
						self.assertEqual(
						    nested_simplify(A__   ,	decimals=6      )   ,	[
						        {"""sequence""": """My name is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped"""},
						        {"""sequence""": """My name is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser"""},
						    ]   ,	)

						A__							:   Dict					       =		unmasker("""The largest city in France is <mask>"""      )
						self.assertEqual(
						    nested_simplify(A__   ,	decimals=6      )   ,	[
						        {
						            """sequence""": """The largest city in France is grouped""",
						            """score""": 2.1e-05,
						            """token""": 3_8015,
						            """token_str""": """ grouped""",
						        },
						        {
						            """sequence""": """The largest city in France is accuser""",
						            """score""": 2.1e-05,
						            """token""": 2_5506,
						            """token_str""": """ accuser""",
						        },
						    ]   ,	)

						A__							:   List[Any]					       =		unmasker("""My name is <mask>"""   ,	targets=[""" Patrick""", """ Clara""", """ Teven"""]   ,	top_k=3      )
						self.assertEqual(
						    nested_simplify(A__   ,	decimals=6      )   ,	[
						        {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """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   __A      (			self      ):
						A__							:   Tuple					       =		pipeline(task="""fill-mask"""   ,	model="""sshleifer/tiny-distilroberta-base"""   ,	top_k=2   ,	framework="""pt"""      )

						A__							:   List[Any]					       =		unmasker("""My name is <mask>"""      )
						self.assertEqual(
						    nested_simplify(A__   ,	decimals=6      )   ,	[
						        {"""sequence""": """My name is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul"""},
						        {"""sequence""": """My name isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""},
						    ]   ,	)

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

						A__							:   Optional[Any]					       =		unmasker("""My name is <mask>"""   ,	targets=[""" Patrick""", """ Clara""", """ Teven"""]   ,	top_k=3      )
						self.assertEqual(
						    nested_simplify(A__   ,	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""": 1_3606, """token_str""": """ Clara"""},
						    ]   ,	)

						A__							:   List[Any]					       =		unmasker("""My name is <mask> <mask>"""   ,	top_k=2      )

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






					@require_torch_gpu
					def   __A      (			self      ):
						A__							:   Tuple					       =		pipeline("""fill-mask"""   ,	model="""hf-internal-testing/tiny-random-distilbert"""   ,	device=0   ,	framework="""pt"""      )

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

						A__							:   Optional[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(A__   ,	A__      )






					@slow
					@require_torch
					def   __A      (			self      ):
						A__							:   Tuple					       =		pipeline(task="""fill-mask"""   ,	model="""distilroberta-base"""   ,	top_k=2   ,	framework="""pt"""      )
						self.run_large_test(A__      )






					@slow
					@require_tf
					def   __A      (			self      ):
						A__							:   Union[str, Any]					       =		pipeline(task="""fill-mask"""   ,	model="""distilroberta-base"""   ,	top_k=2   ,	framework="""tf"""      )
						self.run_large_test(A__      )






					def   __A      (			self   ,	A__      ):
						A__							:   List[Any]					       =		unmasker("""My name is <mask>"""      )
						self.assertEqual(
						    nested_simplify(A__      )   ,	[
						        {"""sequence""": """My name is John""", """score""": 0.0_0_8, """token""": 610, """token_str""": """ John"""},
						        {"""sequence""": """My name is Chris""", """score""": 0.0_0_7, """token""": 1573, """token_str""": """ Chris"""},
						    ]   ,	)
						A__							:   Any					       =		unmasker("""The largest city in France is <mask>"""      )
						self.assertEqual(
						    nested_simplify(A__      )   ,	[
						        {
						            """sequence""": """The largest city in France is Paris""",
						            """score""": 0.2_5_1,
						            """token""": 2201,
						            """token_str""": """ Paris""",
						        },
						        {
						            """sequence""": """The largest city in France is Lyon""",
						            """score""": 0.2_1_4,
						            """token""": 1_2790,
						            """token_str""": """ Lyon""",
						        },
						    ]   ,	)

						A__							:   List[str]					       =		unmasker("""My name is <mask>"""   ,	targets=[""" Patrick""", """ Clara""", """ Teven"""]   ,	top_k=3      )
						self.assertEqual(
						    nested_simplify(A__      )   ,	[
						        {"""sequence""": """My name is Patrick""", """score""": 0.0_0_5, """token""": 3499, """token_str""": """ Patrick"""},
						        {"""sequence""": """My name is Clara""", """score""": 0.0_0_0, """token""": 1_3606, """token_str""": """ Clara"""},
						        {"""sequence""": """My name is Te""", """score""": 0.0_0_0, """token""": 2941, """token_str""": """ Te"""},
						    ]   ,	)






					@require_torch
					def   __A      (			self      ):
						A__							:   Dict					       =		pipeline(task="""fill-mask"""   ,	model="""sshleifer/tiny-distilroberta-base"""   ,	framework="""pt"""      )
						A__							:   Tuple					       =		None
						A__							:   List[str]					       =		None
						self.run_pipeline_test(A__   ,	[]      )






					@require_tf
					def   __A      (			self      ):
						A__							:   Optional[Any]					       =		pipeline(task="""fill-mask"""   ,	model="""sshleifer/tiny-distilroberta-base"""   ,	framework="""tf"""      )
						A__							:   List[str]					       =		None
						A__							:   Optional[Any]					       =		None
						self.run_pipeline_test(A__   ,	[]      )






					def   __A      (			self   ,	A__   ,	A__   ,	A__      ):
						if tokenizer is None or tokenizer.mask_token_id is None:
							self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)"""      )

						A__							:   int					       =		FillMaskPipeline(model=A__   ,	tokenizer=A__      )
						A__							:   Dict					       =		[
						    F"""This is another {tokenizer.mask_token} test""",
						]
						return fill_masker, examples






					def   __A      (			self   ,	A__   ,	A__      ):
						A__							:   List[str]					       =		fill_masker.tokenizer
						A__							:   Optional[Any]					       =		fill_masker.model

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

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

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

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

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






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

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

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

						# Raises with invalid
						with self.assertRaises(A__      ):
							A__							:   List[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(A__      ):
								A__							:   Union[str, Any]					       =		fill_masker(F"""This is a {tokenizer.mask_token}"""   ,	targets=[""""""]      )
							with self.assertRaises(A__      ):
								A__							:   Tuple					       =		fill_masker(F"""This is a {tokenizer.mask_token}"""   ,	targets=""""""      )






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

						A__							:   Dict					       =		FillMaskPipeline(model=A__   ,	tokenizer=A__      )
						A__							:   str					       =		fill_masker(F"""This is a {tokenizer.mask_token}"""   ,	top_k=2      )
						self.assertEqual(
						    A__   ,	[
						        {"""sequence""": ANY(A__      ), """score""": ANY(A__      ), """token""": ANY(A__      ), """token_str""": ANY(A__      )},
						        {"""sequence""": ANY(A__      ), """score""": ANY(A__      ), """token""": ANY(A__      ), """token_str""": ANY(A__      )},
						    ]   ,	)
						self.assertEqual(nested_simplify(A__      )   ,	nested_simplify(A__      )      )






					def   __A      (			self   ,	A__   ,	A__      ):
						A__							:   Optional[Any]					       =		tokenizer.get_vocab()
						A__							:   Tuple					       =		FillMaskPipeline(model=A__   ,	tokenizer=A__      )

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

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






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

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






					def   __A      (			self   ,	A__   ,	A__      ):
						A__							:   List[Any]					       =		FillMaskPipeline(model=A__   ,	tokenizer=A__      )

						A__							:   List[Any]					       =		fill_masker(
						    F"""This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}"""   ,	top_k=2      )
						self.assertEqual(
						    A__   ,	[
						        [
						            {"""sequence""": ANY(A__      ), """score""": ANY(A__      ), """token""": ANY(A__      ), """token_str""": ANY(A__      )},
						            {"""sequence""": ANY(A__      ), """score""": ANY(A__      ), """token""": ANY(A__      ), """token_str""": ANY(A__      )},
						        ],
						        [
						            {"""sequence""": ANY(A__      ), """score""": ANY(A__      ), """token""": ANY(A__      ), """token_str""": ANY(A__      )},
						            {"""sequence""": ANY(A__      ), """score""": ANY(A__      ), """token""": ANY(A__      ), """token_str""": ANY(A__      )},
						        ],
						        [
						            {"""sequence""": ANY(A__      ), """score""": ANY(A__      ), """token""": ANY(A__      ), """token_str""": ANY(A__      )},
						            {"""sequence""": ANY(A__      ), """score""": ANY(A__      ), """token""": ANY(A__      ), """token_str""": ANY(A__      )},
						        ],
						    ]   ,	)






from ...configuration_utils import PretrainedConfig
from ...utils import logging


A_	:			str  	=      logging.get_logger(__name__)

A_	:			Any  	=      {
    'tiiuae/falcon-40b': 'https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json',
    'tiiuae/falcon-7b': 'https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json',
}



class    _a						(__magic_name__    ):


					'''simple docstring'''






					UpperCAmelCase__:       Dict  							=	'''falcon'''
					UpperCAmelCase__:       Any  							=	['''past_key_values''']






					def __init__(			self   ,	A__=6_5024   ,	A__=4544   ,	A__=32   ,	A__=71   ,	A__=1e-5   ,	A__=0.0_2   ,	A__=True   ,	A__=0.0   ,	A__=0.0   ,	A__=None   ,	A__=False   ,	A__=False   ,	A__=True   ,	A__=True   ,	A__=False   ,	A__=11   ,	A__=11   ,	**A__   ,	):
						A__							:   Dict					       =		vocab_size
						# Backward compatibility with n_embed kwarg
						A__							:   Union[str, Any]					       =		kwargs.pop("""n_embed"""   ,	A__      )
						A__							:   Optional[Any]					       =		hidden_size if n_embed is None else n_embed
						A__							:   Any					       =		num_hidden_layers
						A__							:   Any					       =		num_attention_heads
						A__							:   Optional[Any]					       =		layer_norm_epsilon
						A__							:   Tuple					       =		initializer_range
						A__							:   Tuple					       =		use_cache
						A__							:   str					       =		hidden_dropout
						A__							:   List[str]					       =		attention_dropout

						A__							:   List[Any]					       =		bos_token_id
						A__							:   Optional[Any]					       =		eos_token_id
						A__							:   Optional[Any]					       =		num_attention_heads if num_kv_heads is None else num_kv_heads
						A__							:   List[str]					       =		alibi
						A__							:   Tuple					       =		new_decoder_architecture
						A__							:   List[str]					       =		multi_query  # Ignored when new_decoder_architecture is True
						A__							:   List[Any]					       =		parallel_attn
						A__							:   int					       =		bias

						super().__init__(bos_token_id=A__   ,	eos_token_id=A__   ,	**A__      )






					@property
					def   __A      (			self      ):
						return self.hidden_size // self.num_attention_heads






					@property
					def   __A      (			self      ):
						return not self.alibi



