





import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    FalconForCausalLM,
							    FalconForQuestionAnswering,
							    FalconForSequenceClassification,
							    FalconForTokenClassification,
							    FalconModel,
							)






class        _a:







		def __init__(    self ,						__snake_case ,						__snake_case=3 ,						__snake_case=7 ,						__snake_case=True ,						__snake_case=True ,						__snake_case=False ,						__snake_case=True ,						__snake_case=9_9 ,						__snake_case=3_2 ,						__snake_case=5 ,						__snake_case=4 ,						__snake_case=3_7 ,						__snake_case="gelu" ,						__snake_case=0.1 ,						__snake_case=0.1 ,						__snake_case=5_1_2 ,						__snake_case=1_6 ,						__snake_case=2 ,						__snake_case=0.02 ,						__snake_case=3 ,						__snake_case=4 ,						__snake_case=None ,						)      ->      str:


							'''simple docstring'''

							_snake_case       :					Any				       =   parent
							_snake_case       :					str				       =   batch_size
							_snake_case       :					Dict				       =   seq_length
							_snake_case       :					Dict				       =   is_training
							_snake_case       :					List[str]				       =   use_input_mask
							_snake_case       :					Optional[Any]				       =   use_token_type_ids
							_snake_case       :					Any				       =   use_labels
							_snake_case       :					str				       =   vocab_size
							_snake_case       :					Union[str, Any]				       =   hidden_size
							_snake_case       :					Dict				       =   num_hidden_layers
							_snake_case       :					int				       =   num_attention_heads
							_snake_case       :					str				       =   intermediate_size
							_snake_case       :					str				       =   hidden_act
							_snake_case       :					Tuple				       =   hidden_dropout_prob
							_snake_case       :					Optional[int]				       =   attention_probs_dropout_prob
							_snake_case       :					Tuple				       =   max_position_embeddings
							_snake_case       :					Dict				       =   type_vocab_size
							_snake_case       :					Optional[Any]				       =   type_sequence_label_size
							_snake_case       :					Tuple				       =   initializer_range
							_snake_case       :					int				       =   num_labels
							_snake_case       :					List[Any]				       =   num_choices
							_snake_case       :					Dict				       =   scope







		def        lowercase       (    self				)      ->      int:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   ids_tensor([self.batch_size, self.seq_length] ,						self.vocab_size				)

							_snake_case       :					Optional[int]				       =   None
							if self.use_input_mask:
												_snake_case       :					List[Any]				       =   random_attention_mask([self.batch_size, self.seq_length]				)

							_snake_case       :					Optional[Any]				       =   None

							_snake_case       :					Dict				       =   None
							_snake_case       :					Tuple				       =   None
							_snake_case       :					str				       =   None
							if self.use_labels:
												_snake_case       :					Union[str, Any]				       =   ids_tensor([self.batch_size] ,						self.type_sequence_label_size				)
												_snake_case       :					int				       =   ids_tensor([self.batch_size, self.seq_length] ,						self.num_labels				)
												_snake_case       :					List[Any]				       =   ids_tensor([self.batch_size] ,						self.num_choices				)

							_snake_case       :					Optional[int]				       =   self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







		def        lowercase       (    self				)      ->      Optional[int]:


							'''simple docstring'''

							return FalconConfig(
							    vocab_size=self.vocab_size ,						hidden_size=self.hidden_size ,						num_hidden_layers=self.num_hidden_layers ,						num_attention_heads=self.num_attention_heads ,						intermediate_size=self.intermediate_size ,						hidden_act=self.hidden_act ,						hidden_dropout_prob=self.hidden_dropout_prob ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob ,						max_position_embeddings=self.max_position_embeddings ,						type_vocab_size=self.type_vocab_size ,						is_decoder=__snake_case ,						initializer_range=self.initializer_range ,						pad_token_id=1 ,						new_decoder_architecture=__snake_case ,						)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)      ->      str:


							'''simple docstring'''

							_snake_case       :					Optional[int]				       =   FalconModel(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Tuple				       =   model(__snake_case ,						attention_mask=__snake_case				)
							_snake_case       :					str				       =   model(__snake_case				)
							self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						)      ->      Any:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   True
							_snake_case       :					List[Any]				       =   FalconModel(__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Union[str, Any]				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						encoder_hidden_states=__snake_case ,						encoder_attention_mask=__snake_case ,						)
							_snake_case       :					str				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						encoder_hidden_states=__snake_case ,						)
							_snake_case       :					Optional[Any]				       =   model(__snake_case ,						attention_mask=__snake_case				)
							self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						)      ->      int:


							'''simple docstring'''

							_snake_case       :					Tuple				       =   FalconForCausalLM(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Dict				       =   model(__snake_case ,						attention_mask=__snake_case ,						labels=__snake_case				)
							self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.vocab_size)				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						)      ->      Optional[Any]:


							'''simple docstring'''

							_snake_case       :					Optional[int]				       =   True
							_snake_case       :					Optional[Any]				       =   True
							_snake_case       :					Optional[int]				       =   FalconForCausalLM(config=__snake_case				)
							model.to(__snake_case				)
							model.eval()

							# first forward pass
							_snake_case       :					int				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						encoder_hidden_states=__snake_case ,						encoder_attention_mask=__snake_case ,						use_cache=__snake_case ,						)
							_snake_case       :					int				       =   outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							_snake_case       :					Any				       =   ids_tensor((self.batch_size, 3) ,						config.vocab_size				)
							_snake_case       :					Union[str, Any]				       =   ids_tensor((self.batch_size, 3) ,						vocab_size=2				)

							# append to next input_ids and
							_snake_case       :					List[str]				       =   torch.cat([input_ids, next_tokens] ,						dim=-1				)
							_snake_case       :					str				       =   torch.cat([input_mask, next_mask] ,						dim=-1				)

							_snake_case       :					Optional[int]				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						encoder_hidden_states=__snake_case ,						encoder_attention_mask=__snake_case ,						output_hidden_states=__snake_case ,						)["hidden_states"][0]
							_snake_case       :					Dict				       =   model(
							    __snake_case ,						attention_mask=__snake_case ,						encoder_hidden_states=__snake_case ,						encoder_attention_mask=__snake_case ,						past_key_values=__snake_case ,						output_hidden_states=__snake_case ,						)["hidden_states"][0]

							# select random slice
							_snake_case       :					List[str]				       =   ids_tensor((1,) ,						output_from_past.shape[-1]				).item()
							_snake_case       :					str				       =   output_from_no_past[:, -3:, random_slice_idx].detach()
							_snake_case       :					Union[str, Any]				       =   output_from_past[:, :, random_slice_idx].detach()

							self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]				)

							# test that outputs are equal for slice
							self.parent.assertTrue(torch.allclose(__snake_case ,						__snake_case ,						atol=1E-3				)				)







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


							'''simple docstring'''

							_snake_case       :					Optional[int]				       =   self.prepare_config_and_inputs()
							(
							    (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , (
							    _snake_case
							)       , 
							)       :					Dict				       =   config_and_inputs
							_snake_case       :					Union[str, Any]				       =   {"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict






@require_torch
class        _a(    __A	,   __A	,   __A	,   unittest.TestCase    ):
		lowerCamelCase__     :List[Any]   				=		(
		    (
		        FalconModel,
		        FalconForCausalLM,
		        FalconForSequenceClassification,
		        FalconForTokenClassification,
		        FalconForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		lowerCamelCase__     :Optional[Any]   				=		(FalconForCausalLM,) if is_torch_available() else ()
		lowerCamelCase__     :Optional[int]   				=		(
		    {
		        'feature-extraction': FalconModel,
		        'text-classification': FalconForSequenceClassification,
		        'text-generation': FalconForCausalLM,
		        'question-answering': FalconForQuestionAnswering,
		        'token-classification': FalconForTokenClassification,
		        'zero-shot': FalconForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		lowerCamelCase__     :Any   				=		False
		lowerCamelCase__     :List[Any]   				=		False







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


							'''simple docstring'''

							_snake_case       :					Tuple				       =   FalconModelTester(self				)
							_snake_case       :					List[Any]				       =   ConfigTester(self ,						config_class=__snake_case ,						hidden_size=3_7				)







		def        lowercase       (    self				)      ->      Dict:


							'''simple docstring'''

							self.config_tester.run_common_tests()







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


							'''simple docstring'''

							_snake_case       :					Union[str, Any]				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__snake_case				)







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


							'''simple docstring'''

							_snake_case       , *_snake_case       :					int				       =   self.model_tester.prepare_config_and_inputs()
							for alibi in [True, False]:
												_snake_case       :					int				       =   alibi
												self.model_tester.create_and_check_model(__snake_case ,						*__snake_case				)







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


							'''simple docstring'''

							_snake_case       , _snake_case       :					Optional[int]				       =   self.model_tester.prepare_config_and_inputs_for_common()
							_snake_case       :					Union[str, Any]				       =   3
							_snake_case       :					Optional[Any]				       =   input_dict["input_ids"]
							_snake_case       :					Dict				       =   input_ids.ne(1				).to(__snake_case				)
							_snake_case       :					str				       =   ids_tensor([self.model_tester.batch_size] ,						self.model_tester.type_sequence_label_size				)
							_snake_case       :					Any				       =   FalconForSequenceClassification(__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					List[Any]				       =   model(__snake_case ,						attention_mask=__snake_case ,						labels=__snake_case				)
							self.assertEqual(result.logits.shape ,						(self.model_tester.batch_size, self.model_tester.num_labels)				)







		def        lowercase       (    self				)      ->      str:


							'''simple docstring'''

							_snake_case       , _snake_case       :					Tuple				       =   self.model_tester.prepare_config_and_inputs_for_common()
							_snake_case       :					Any				       =   3
							_snake_case       :					Union[str, Any]				       =   "single_label_classification"
							_snake_case       :					Dict				       =   input_dict["input_ids"]
							_snake_case       :					List[str]				       =   input_ids.ne(1				).to(__snake_case				)
							_snake_case       :					Optional[int]				       =   ids_tensor([self.model_tester.batch_size] ,						self.model_tester.type_sequence_label_size				)
							_snake_case       :					Any				       =   FalconForSequenceClassification(__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Optional[int]				       =   model(__snake_case ,						attention_mask=__snake_case ,						labels=__snake_case				)
							self.assertEqual(result.logits.shape ,						(self.model_tester.batch_size, self.model_tester.num_labels)				)







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


							'''simple docstring'''

							_snake_case       , _snake_case       :					str				       =   self.model_tester.prepare_config_and_inputs_for_common()
							_snake_case       :					List[str]				       =   input_dict["input_ids"]
							_snake_case       :					List[str]				       =   FalconForCausalLM(__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Optional[Any]				       =   model(__snake_case ,						use_cache=__snake_case				)
							_snake_case       :					List[Any]				       =   input_ids.shape[0]
							_snake_case       :					Optional[Any]				       =   model._convert_to_rw_cache(result.past_key_values				)
							_snake_case       :					Tuple				       =   model._convert_cache_to_standard_format(__snake_case ,						__snake_case				)
							for layer in range(len(__snake_case				)				):
												for tensor_idx in range(2				):
																	self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3				)
																	self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4				)
																	self.assertTrue(
																	    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]				)				)







		def        lowercase       (    self				)      ->      Optional[int]:


							'''simple docstring'''

							_snake_case       , _snake_case       :					Dict				       =   self.model_tester.prepare_config_and_inputs_for_common()
							_snake_case       :					Dict				       =   3
							_snake_case       :					List[str]				       =   "multi_label_classification"
							_snake_case       :					List[str]				       =   input_dict["input_ids"]
							_snake_case       :					Union[str, Any]				       =   input_ids.ne(1				).to(__snake_case				)
							_snake_case       :					int				       =   ids_tensor(
							    [self.model_tester.batch_size, config.num_labels] ,						self.model_tester.type_sequence_label_size				).to(torch.float				)
							_snake_case       :					Any				       =   FalconForSequenceClassification(__snake_case				)
							model.to(__snake_case				)
							model.eval()
							_snake_case       :					Union[str, Any]				       =   model(__snake_case ,						attention_mask=__snake_case ,						labels=__snake_case				)
							self.assertEqual(result.logits.shape ,						(self.model_tester.batch_size, self.model_tester.num_labels)				)







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


							'''simple docstring'''

							for model_class in self.all_generative_model_classes:
												_snake_case       , _snake_case       :					Dict				       =   self.model_tester.prepare_config_and_inputs_for_common()

												# If it doesn't support cache, pass the test
												if not hasattr(__snake_case ,						"use_cache"				):
																	return

												_snake_case       :					Dict				       =   model_class(__snake_case				).to(__snake_case				)
												if "use_cache" not in inputs:
																	_snake_case       :					str				       =   True
												_snake_case       :					str				       =   model(**__snake_case				)

												# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
												if "past_key_values" not in outputs:
																	return

												_snake_case       :					List[str]				       =   (
												    getattr(__snake_case ,						"decoder_layers" ,						__snake_case				)
												    or getattr(__snake_case ,						"num_decoder_layers" ,						__snake_case				)
												    or config.num_hidden_layers
												)
												_snake_case       :					int				       =   getattr(__snake_case ,						"num_kv_heads" ,						config.num_attention_heads				)
												_snake_case       :					Dict				       =   getattr(__snake_case ,						"d_model" ,						config.hidden_size				)
												_snake_case       :					List[str]				       =   embed_dim // num_attention_heads

												_snake_case       :					Optional[Any]				       =   outputs["past_key_values"]
												self.assertEqual(len(__snake_case				) ,						__snake_case				)

												_snake_case       , _snake_case       :					int				       =   inputs["input_ids"].shape
												for i in range(__snake_case				):
																	if config.new_decoder_architecture:
																						_snake_case       :					Optional[int]				       =   config.num_attention_heads
																	elif config.multi_query:
																						_snake_case       :					Optional[int]				       =   1
																	self.assertEqual(len(past_kv[0]				) ,						2				)  # K V for the decoder = 2
																	self.assertEqual(
																	    past_kv[i][0].shape ,						(batch_size, num_attention_heads, seq_length, per_head_embed_dim)				)
																	self.assertEqual(
																	    past_kv[i][1].shape ,						(batch_size, num_attention_heads, seq_length, per_head_embed_dim)				)






@require_torch
class        _a(    unittest.TestCase    ):







		@slow
		def        lowercase       (    self				)      ->      Dict:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   AutoTokenizer.from_pretrained("Rocketknight1/falcon-rw-1b"				)
							_snake_case       :					List[str]				       =   FalconForCausalLM.from_pretrained("Rocketknight1/falcon-rw-1b"				)
							model.eval()
							model.to(__snake_case				)
							_snake_case       :					List[Any]				       =   tokenizer("My favorite food is" ,						return_tensors="pt"				).to(__snake_case				)

							_snake_case       :					Dict				       =   (
							    "My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."
							)

							_snake_case       :					Optional[int]				       =   model.generate(**__snake_case ,						do_sample=__snake_case ,						max_new_tokens=1_9				)
							_snake_case       :					List[Any]				       =   tokenizer.batch_decode(__snake_case				)[0]

							self.assertEqual(__snake_case ,						__snake_case				)







		@slow
		def        lowercase       (    self				)      ->      Optional[Any]:


							'''simple docstring'''

							for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
												_snake_case       :					List[str]				       =   AutoTokenizer.from_pretrained(__snake_case				)
												_snake_case       :					Tuple				       =   FalconForCausalLM.from_pretrained(__snake_case				)
												model.eval()
												model.to(__snake_case				)
												_snake_case       :					Optional[Any]				       =   tokenizer("My favorite food is" ,						return_tensors="pt"				).to(__snake_case				)

												# We just test that these run without errors - the models are randomly initialized
												# and so the actual text outputs will be garbage
												model.generate(**__snake_case ,						do_sample=__snake_case ,						max_new_tokens=4				)
												model.generate(**__snake_case ,						do_sample=__snake_case ,						max_new_tokens=4				)
												model.generate(**__snake_case ,						num_beams=2 ,						max_new_tokens=4				)







		@slow
		def        lowercase       (    self				)      ->      Any:


							'''simple docstring'''

							with torch.no_grad():
												for repo in [
												    "Rocketknight1/falcon-rw-1b",
												    "Rocketknight1/tiny-random-falcon-7b",
												    "Rocketknight1/tiny-random-falcon-40b",
												]:
																	_snake_case       :					int				       =   AutoTokenizer.from_pretrained(__snake_case				)
																	_snake_case       :					List[str]				       =   FalconForCausalLM.from_pretrained(__snake_case				)
																	model.eval()
																	model.to(device=__snake_case				)
																	_snake_case       :					Union[str, Any]				       =   tokenizer("My favorite food is" ,						return_tensors="pt"				).to(__snake_case				)

																	# Test results are the same with and without cache
																	_snake_case       :					int				       =   model.generate(**__snake_case ,						do_sample=__snake_case ,						max_new_tokens=2_0 ,						use_cache=__snake_case				)
																	_snake_case       :					Optional[int]				       =   model.generate(**__snake_case ,						do_sample=__snake_case ,						max_new_tokens=2_0 ,						use_cache=__snake_case				)
																	self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0				)






import numpy as np

__lowerCAmelCase :Dict     =    [
    ['a', 'b', 'c', 'd', 'e'],
    ['f', 'g', 'h', 'i', 'k'],
    ['l', 'm', 'n', 'o', 'p'],
    ['q', 'r', 's', 't', 'u'],
    ['v', 'w', 'x', 'y', 'z'],
]






class        _a:







		def __init__(    self				)      ->      None:


							'''simple docstring'''

							_snake_case       :					Optional[Any]				       =   np.array(__snake_case				)







		def        lowercase       (    self ,						__snake_case				)      ->      np.ndarray:


							'''simple docstring'''

							_snake_case       , _snake_case       :					List[Any]				       =   np.where(letter == self.SQUARE				)
							_snake_case       :					Any				       =   np.concatenate([indexa + 1, indexa + 1]				)
							return indexes







		def        lowercase       (    self ,						__snake_case ,						__snake_case				)      ->      str:


							'''simple docstring'''

							_snake_case       :					int				       =   self.SQUARE[indexa - 1, indexa - 1]
							return letter







		def        lowercase       (    self ,						__snake_case				)      ->      str:


							'''simple docstring'''

							_snake_case       :					Optional[Any]				       =   message.lower()
							_snake_case       :					List[Any]				       =   message.replace(" " ,						""				)
							_snake_case       :					Any				       =   message.replace("j" ,						"i"				)

							_snake_case       :					Tuple				       =   np.empty((2, len(__snake_case				))				)
							for letter_index in range(len(__snake_case				)				):
												_snake_case       :					Dict				       =   self.letter_to_numbers(message[letter_index]				)

												_snake_case       :					Tuple				       =   numbers[0]
												_snake_case       :					List[Any]				       =   numbers[1]

							_snake_case       :					Any				       =   first_step.reshape(2 * len(__snake_case				)				)
							_snake_case       :					Optional[Any]				       =   ""
							for numbers_index in range(len(__snake_case				)				):
												_snake_case       :					Optional[int]				       =   int(second_step[numbers_index * 2]				)
												_snake_case       :					List[str]				       =   int(second_step[(numbers_index * 2) + 1]				)
												_snake_case       :					Any				       =   self.numbers_to_letter(__snake_case ,						__snake_case				)
												_snake_case       :					Any				       =   encoded_message + letter

							return encoded_message







		def        lowercase       (    self ,						__snake_case				)      ->      str:


							'''simple docstring'''

							_snake_case       :					Union[str, Any]				       =   message.lower()
							message.replace(" " ,						""				)
							_snake_case       :					Tuple				       =   np.empty(2 * len(__snake_case				)				)
							for letter_index in range(len(__snake_case				)				):
												_snake_case       :					Union[str, Any]				       =   self.letter_to_numbers(message[letter_index]				)
												_snake_case       :					Tuple				       =   numbers[0]
												_snake_case       :					Any				       =   numbers[1]

							_snake_case       :					Optional[Any]				       =   first_step.reshape((2, len(__snake_case				))				)
							_snake_case       :					Union[str, Any]				       =   ""
							for numbers_index in range(len(__snake_case				)				):
												_snake_case       :					Dict				       =   int(second_step[0, numbers_index]				)
												_snake_case       :					Optional[Any]				       =   int(second_step[1, numbers_index]				)
												_snake_case       :					Union[str, Any]				       =   self.numbers_to_letter(__snake_case ,						__snake_case				)
												_snake_case       :					str				       =   decoded_message + letter

							return decoded_message