



from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase=None  ,  __lowerCAmelCase=None      )   ->					int:


				"""simple docstring"""

				if attention_mask is None:
								snake_case__	:      int     		=	tf.cast(tf.math.not_equal(__lowerCAmelCase  ,  config.pad_token_id      )  ,  tf.inta      )
				return {"input_ids": input_ids, "attention_mask": attention_mask}


@require_tf
class       a							:
		__lowerCAmelCase	:    Union[str, Any]											=					OPTConfig
		__lowerCAmelCase	:    List[str]											=					{}
		__lowerCAmelCase	:    Union[str, Any]											=					"""gelu"""



		def __init__(     self     :Tuple   ,__lowercase     :Optional[Any]   ,__lowercase     :str=1_3   ,__lowercase     :List[Any]=7   ,__lowercase     :List[Any]=True   ,__lowercase     :List[str]=False   ,__lowercase     :Dict=9_9   ,__lowercase     :Any=1_6   ,__lowercase     :Optional[Any]=2   ,__lowercase     :Optional[int]=4   ,__lowercase     :Optional[int]=4   ,__lowercase     :Optional[int]="gelu"   ,__lowercase     :int=0.1   ,__lowercase     :List[Any]=0.1   ,__lowercase     :Optional[int]=2_0   ,__lowercase     :Tuple=2   ,__lowercase     :Optional[Any]=1   ,__lowercase     :str=0   ,__lowercase     :Any=1_6   ,__lowercase     :Dict=1_6   ,):
						snake_case__	:      List[Any]     		=	parent
						snake_case__	:      int     		=	batch_size
						snake_case__	:      List[Any]     		=	seq_length
						snake_case__	:      Any     		=	is_training
						snake_case__	:      Dict     		=	use_labels
						snake_case__	:      int     		=	vocab_size
						snake_case__	:      List[str]     		=	hidden_size
						snake_case__	:      Optional[Any]     		=	num_hidden_layers
						snake_case__	:      Union[str, Any]     		=	num_attention_heads
						snake_case__	:      Any     		=	intermediate_size
						snake_case__	:      List[Any]     		=	hidden_act
						snake_case__	:      List[str]     		=	hidden_dropout_prob
						snake_case__	:      Any     		=	attention_probs_dropout_prob
						snake_case__	:      List[Any]     		=	max_position_embeddings
						snake_case__	:      int     		=	eos_token_id
						snake_case__	:      Union[str, Any]     		=	pad_token_id
						snake_case__	:      Union[str, Any]     		=	bos_token_id
						snake_case__	:      Any     		=	embed_dim
						snake_case__	:      Dict     		=	word_embed_proj_dim
						snake_case__	:      Optional[Any]     		=	False



		def 	__lowerCamelCase			(     self     :Tuple  ):
						snake_case__	:      int     		=	ids_tensor([self.batch_size, self.seq_length - 1]   ,self.vocab_size  )
						snake_case__	:      Tuple     		=	tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size  )   ,1  )
						snake_case__	:      Dict     		=	tf.concat([input_ids, eos_tensor]   ,axis=1  )

						snake_case__	:      Any     		=	self.config_cls(
						    vocab_size=self.vocab_size   ,hidden_size=self.hidden_size   ,num_hidden_layers=self.num_hidden_layers   ,num_attention_heads=self.num_attention_heads   ,ffn_dim=self.intermediate_size   ,dropout=self.hidden_dropout_prob   ,attention_dropout=self.attention_probs_dropout_prob   ,max_position_embeddings=self.max_position_embeddings   ,eos_token_id=self.eos_token_id   ,bos_token_id=self.bos_token_id   ,pad_token_id=self.pad_token_id   ,embed_dim=self.embed_dim   ,word_embed_proj_dim=self.word_embed_proj_dim   ,is_encoder_decoder=__lowercase   ,**self.config_updates   ,)
						snake_case__	:      Any     		=	prepare_opt_inputs_dict(__lowercase   ,__lowercase  )
						return config, inputs_dict






		def 	__lowerCamelCase			(     self     :Union[str, Any]   ,__lowercase     :int   ,__lowercase     :List[str]  ):
						snake_case__	:      Union[str, Any]     		=	TFOPTModel(config=__lowercase  )
						snake_case__	:      Dict     		=	inputs_dict['''input_ids''']

						snake_case__	:      List[str]     		=	input_ids[:1, :]
						snake_case__	:      Tuple     		=	inputs_dict['''attention_mask'''][:1, :]
						snake_case__	:      int     		=	1

						# first forward pass
						snake_case__	:      List[Any]     		=	model(__lowercase   ,attention_mask=__lowercase   ,use_cache=__lowercase  )

						snake_case__		, snake_case__	:      str     		=	outputs.to_tuple()

						# create hypothetical next token and extent to next_input_ids
						snake_case__	:      List[Any]     		=	ids_tensor((self.batch_size, 3)   ,config.vocab_size  )
						snake_case__	:      str     		=	tf.cast(ids_tensor((self.batch_size, 3)   ,2  )   ,tf.inta  )

						# append to next input_ids and
						snake_case__	:      Union[str, Any]     		=	tf.concat([input_ids, next_tokens]   ,axis=-1  )
						snake_case__	:      Optional[int]     		=	tf.concat([attention_mask, next_attn_mask]   ,axis=-1  )

						snake_case__	:      int     		=	model(__lowercase   ,attention_mask=__lowercase  )[0]
						snake_case__	:      Union[str, Any]     		=	model(__lowercase   ,attention_mask=__lowercase   ,past_key_values=__lowercase  )[0]

						self.parent.assertEqual(next_tokens.shape[1]   ,output_from_past.shape[1]  )

						# select random slice
						snake_case__	:      Optional[Any]     		=	int(ids_tensor((1,)   ,output_from_past.shape[-1]  )  )
						snake_case__	:      Optional[int]     		=	output_from_no_past[:, -3:, random_slice_idx]
						snake_case__	:      Optional[Any]     		=	output_from_past[:, :, random_slice_idx]

						# test that outputs are equal for slice
						tf.debugging.assert_near(__lowercase   ,__lowercase   ,rtol=1e-3  )


@require_tf
class       a							(			__lowerCamelCase  ,							__lowerCamelCase  ,							unittest.TestCase				):
		__lowerCAmelCase	:    Union[str, Any]											=					(TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
		__lowerCAmelCase	:    Union[str, Any]											=					(TFOPTForCausalLM,) if is_tf_available() else ()
		__lowerCAmelCase	:    str											=					(
		    {"""feature-extraction""": TFOPTModel, """text-generation""": TFOPTForCausalLM} if is_tf_available() else {}
		)
		__lowerCAmelCase	:    str											=					False
		__lowerCAmelCase	:    Optional[Any]											=					False
		__lowerCAmelCase	:    Tuple											=					False
		__lowerCAmelCase	:    Any											=					10



		def 	__lowerCamelCase			(     self     :List[str]  ):
						snake_case__	:      List[Any]     		=	TFOPTModelTester(self  )
						snake_case__	:      int     		=	ConfigTester(self   ,config_class=__lowercase  )



		def 	__lowerCamelCase			(     self     :str  ):
						self.config_tester.run_common_tests()



		def 	__lowerCamelCase			(     self     :int  ):
						snake_case__	:      Tuple     		=	self.model_tester.prepare_config_and_inputs_for_common()
						self.model_tester.check_decoder_model_past_large_inputs(*__lowercase  )






		def 	__lowerCamelCase			(     self     :str  ):
						snake_case__		, snake_case__	:      Dict     		=	self.model_tester.prepare_config_and_inputs_for_common()

						def _get_word_embedding_weight(__lowercase     :int   ,__lowercase     :Any  ):
										if hasattr(__lowercase   ,'''weight'''  ):
														return embedding_layer.weight
										else:
														# Here we build the word embeddings weights if not exists.
														# And then we retry to get the attribute once built.
														model.build()
														if hasattr(__lowercase   ,'''weight'''  ):
																		return embedding_layer.weight
														else:
																		return None

						for model_class in self.all_model_classes:
										for size in [config.vocab_size - 1_0, config.vocab_size + 1_0]:
														# build the embeddings
														snake_case__	:      Tuple     		=	model_class(config=__lowercase  )
														snake_case__	:      Tuple     		=	_get_word_embedding_weight(__lowercase   ,model.get_input_embeddings()  )
														snake_case__	:      Optional[int]     		=	_get_word_embedding_weight(__lowercase   ,model.get_output_embeddings()  )

														# reshape the embeddings
														model.resize_token_embeddings(__lowercase  )
														snake_case__	:      str     		=	_get_word_embedding_weight(__lowercase   ,model.get_input_embeddings()  )
														snake_case__	:      Optional[Any]     		=	_get_word_embedding_weight(__lowercase   ,model.get_output_embeddings()  )

														# check that the resized embeddings size matches the desired size.
														snake_case__	:      Union[str, Any]     		=	size if size is not None else config.vocab_size

														self.assertEqual(new_input_embeddings.shape[0]   ,__lowercase  )

														# check that weights remain the same after resizing
														snake_case__	:      Union[str, Any]     		=	True
														for pa, pa in zip(old_input_embeddings.value()   ,new_input_embeddings.value()  ):
																		if tf.math.reduce_sum(tf.math.abs(pa - pa  )  ) > 0:
																						snake_case__	:      Dict     		=	False
														self.assertTrue(__lowercase  )

														if old_output_embeddings is not None and new_output_embeddings is not None:
																		self.assertEqual(new_output_embeddings.shape[0]   ,__lowercase  )

																		snake_case__	:      str     		=	True
																		for pa, pa in zip(old_output_embeddings.value()   ,new_output_embeddings.value()  ):
																						if tf.math.reduce_sum(tf.math.abs(pa - pa  )  ) > 0:
																										snake_case__	:      str     		=	False
																		self.assertTrue(__lowercase  )



def 			_lowerCAmelCase       (					__lowerCAmelCase      )   ->					Any:


				"""simple docstring"""

				return tf.constant(__lowerCAmelCase  ,  dtype=tf.intaa      )


@require_tf
class       a							(			unittest.TestCase				):
		__lowerCAmelCase	:    Optional[int]											=					99



		def 	__lowerCamelCase			(     self     :Optional[int]  ):
						snake_case__	:      Any     		=	tf.ones((4, 1)   ,dtype=tf.intaa  ) * 2
						snake_case__	:      Union[str, Any]     		=	tf.concat([ids_tensor((4, 6)   ,self.vocab_size - 3  ) + 3, eos_column_vector]   ,axis=1  )
						snake_case__	:      Dict     		=	input_ids.shape[0]
						snake_case__	:      List[str]     		=	OPTConfig(
						    vocab_size=self.vocab_size   ,hidden_size=2_4   ,num_hidden_layers=2   ,num_attention_heads=2   ,ffn_dim=3_2   ,max_position_embeddings=4_8   ,eos_token_id=2   ,pad_token_id=1   ,bos_token_id=0   ,)
						return config, input_ids, batch_size


@require_sentencepiece
@require_tf
class       a							(			unittest.TestCase				):



		@slow
		def 	__lowerCamelCase			(     self     :Tuple  ):
						snake_case__	:      Any     		=	TFOPTModel.from_pretrained('''facebook/opt-350m'''  )
						snake_case__	:      int     		=	_long_tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]  )
						snake_case__	:      Any     		=	tf.not_equal(__lowercase   ,model.config.pad_token_id  )
						with tf.GradientTape():
										snake_case__	:      Union[str, Any]     		=	model(input_ids=__lowercase   ,attention_mask=__lowercase  ).last_hidden_state
						snake_case__	:      Any     		=	(1, 1_1, 5_1_2)
						self.assertEqual(output.shape   ,__lowercase  )
						snake_case__	:      List[str]     		=	tf.constant(
						    [[-0.2873, -1.9218, -0.3033], [-1.2710, -0.1338, -0.1902], [0.4095, 0.1214, -1.3121]]  )
						self.assertTrue(np.allclose(output[:, :3, :3]   ,__lowercase   ,atol=4e-3  )  )

						snake_case__	:      List[str]     		=	tf.function(__lowercase   ,jit_compile=__lowercase  )
						snake_case__	:      Dict     		=	xla_generate(__lowercase   ,__lowercase  )[0]
						self.assertTrue(np.allclose(output[:, :3, :3]   ,__lowercase   ,atol=4e-2  )  )


@require_tf
@slow
class       a							(			unittest.TestCase				):



		def 	__lowerCamelCase			(     self     :Dict  ):
						super().setUp()
						snake_case__	:      Dict     		=	'''facebook/opt-350m'''






		def 	__lowerCamelCase			(     self     :int  ):
						snake_case__	:      Dict     		=	TFOPTForCausalLM.from_pretrained(self.path_model  )
						snake_case__	:      Any     		=	GPTaTokenizer.from_pretrained(self.path_model  )

						snake_case__	:      List[Any]     		=	[
						    '''Today is a beautiful day and I want to''',
						    '''In the city of''',
						    '''Paris is the capital of France and''',
						    '''Computers and mobile phones have taken''',
						]
						# verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
						snake_case__	:      Any     		=	tokenizer(__lowercase   ,return_tensors='''tf'''   ,padding=__lowercase   ,add_special_tokens=__lowercase  )
						snake_case__	:      List[str]     		=	tf.math.reduce_mean(model(inputs.input_ids   ,attention_mask=inputs.attention_mask  )[0]   ,axis=-1  )
						snake_case__	:      Tuple     		=	tf.constant(
						    [
						        [1.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670],
						        [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822],
						        [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703],
						        [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477],
						    ]  )
						self.assertTrue(np.allclose(__lowercase   ,__lowercase   ,atol=1e-4  )  )

						snake_case__	:      List[Any]     		=	tf.function(__lowercase   ,jit_compile=__lowercase  )
						snake_case__	:      Optional[Any]     		=	tf.math.reduce_mean(xla_generate(inputs.input_ids   ,attention_mask=inputs.attention_mask  )[0]   ,axis=-1  )
						self.assertTrue(np.allclose(__lowercase   ,__lowercase   ,atol=1e-4  )  )


@require_tf
@slow
class       a							(			unittest.TestCase				):



		@property
		def 	__lowerCamelCase			(     self     :Any  ):
						return [
						    "Today is a beautiful day and I want",
						    "In the city of",
						    "Paris is the capital of France and",
						    "Computers and mobile phones have taken",
						]



		def 	__lowerCamelCase			(     self     :str  ):
						snake_case__	:      Dict     		=	'''facebook/opt-125m'''

						snake_case__	:      Tuple     		=	[
						    '''Today is a beautiful day and I want to''',
						    '''In the city of New York, the city''',
						    '''Paris is the capital of France and the capital''',
						    '''Computers and mobile phones have taken over the''',
						]

						snake_case__	:      List[Any]     		=	[]
						snake_case__	:      List[str]     		=	GPTaTokenizer.from_pretrained(__lowercase  )
						snake_case__	:      List[str]     		=	TFOPTForCausalLM.from_pretrained(__lowercase  )

						for prompt in self.prompts:
										snake_case__	:      List[str]     		=	tokenizer(__lowercase   ,return_tensors='''tf'''  ).input_ids

										snake_case__	:      Optional[int]     		=	model.generate(__lowercase   ,max_length=1_0  )

										snake_case__	:      str     		=	tokenizer.batch_decode(__lowercase   ,skip_special_tokens=__lowercase  )
										predicted_outputs += generated_string

						self.assertListEqual(__lowercase   ,__lowercase  )



		def 	__lowerCamelCase			(     self     :Tuple  ):
						snake_case__	:      str     		=	'''facebook/opt-350m'''

						snake_case__	:      int     		=	GPTaTokenizer.from_pretrained(__lowercase  )
						snake_case__	:      List[str]     		=	TFOPTForCausalLM.from_pretrained(__lowercase  )

						snake_case__	:      Tuple     		=	'''left'''

						# use different length sentences to test batching
						snake_case__	:      List[str]     		=	[
						    '''Hello, my dog is a little''',
						    '''Today, I''',
						]

						snake_case__	:      Optional[Any]     		=	tokenizer(__lowercase   ,return_tensors='''tf'''   ,padding=__lowercase  )
						snake_case__	:      Tuple     		=	inputs['''input_ids''']

						snake_case__	:      Optional[Any]     		=	model.generate(input_ids=__lowercase   ,attention_mask=inputs['''attention_mask''']  )

						snake_case__	:      Optional[Any]     		=	tokenizer(sentences[0]   ,return_tensors='''tf'''  ).input_ids
						snake_case__	:      Any     		=	model.generate(input_ids=__lowercase  )

						snake_case__	:      Optional[Any]     		=	inputs_non_padded.shape[-1] - tf.math.reduce_sum(
						    tf.cast(inputs['''attention_mask'''][-1]   ,tf.intaa  )  )
						snake_case__	:      List[str]     		=	tokenizer(sentences[1]   ,return_tensors='''tf'''  ).input_ids
						snake_case__	:      List[str]     		=	model.generate(input_ids=__lowercase   ,max_length=model.config.max_length - num_paddings  )

						snake_case__	:      Dict     		=	tokenizer.batch_decode(__lowercase   ,skip_special_tokens=__lowercase  )
						snake_case__	:      Dict     		=	tokenizer.decode(output_non_padded[0]   ,skip_special_tokens=__lowercase  )
						snake_case__	:      Union[str, Any]     		=	tokenizer.decode(output_padded[0]   ,skip_special_tokens=__lowercase  )

						snake_case__	:      Tuple     		=	[
						    '''Hello, my dog is a little bit of a dork.\nI\'m a little bit''',
						    '''Today, I was in the middle of a conversation with a friend about the''',
						]
						self.assertListEqual(__lowercase   ,__lowercase  )
						self.assertListEqual(__lowercase   ,[non_padded_sentence, padded_sentence]  )






		def 	__lowerCamelCase			(     self     :Dict  ):
						snake_case__	:      List[Any]     		=	'''facebook/opt-350m'''

						snake_case__	:      int     		=	[
						    '''Today is a beautiful day and I want to''',
						    '''In the city of San Francisco, the city''',
						    '''Paris is the capital of France and the capital''',
						    '''Computers and mobile phones have taken over the''',
						]

						snake_case__	:      Dict     		=	[]
						snake_case__	:      Union[str, Any]     		=	GPTaTokenizer.from_pretrained(__lowercase  )
						snake_case__	:      str     		=	TFOPTForCausalLM.from_pretrained(__lowercase  )

						for prompt in self.prompts:
										snake_case__	:      str     		=	tokenizer(__lowercase   ,return_tensors='''tf'''  ).input_ids

										snake_case__	:      List[Any]     		=	model.generate(__lowercase   ,max_length=1_0  )

										snake_case__	:      Tuple     		=	tokenizer.batch_decode(__lowercase   ,skip_special_tokens=__lowercase  )
										predicted_outputs += generated_string

						self.assertListEqual(__lowercase   ,__lowercase  )






import gc
import unittest

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

from .test_pipelines_common import ANY


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



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

										torch.cuda.empty_cache()



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

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

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



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

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

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

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

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

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



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

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

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



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



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



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

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



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



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



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

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



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

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

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

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

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

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



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

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

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

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

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



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

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



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

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

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



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

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






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

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

