






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







				def __init__(    self							,       __lowerCAmelCase							,       __lowerCAmelCase=3							,       __lowerCAmelCase=7							,       __lowerCAmelCase=True							,       __lowerCAmelCase=True							,       __lowerCAmelCase=False							,       __lowerCAmelCase=True							,       __lowerCAmelCase=9_9							,       __lowerCAmelCase=3_2							,       __lowerCAmelCase=5							,       __lowerCAmelCase=4							,       __lowerCAmelCase=3_7							,       __lowerCAmelCase="gelu"							,       __lowerCAmelCase=0.1							,       __lowerCAmelCase=0.1							,       __lowerCAmelCase=5_1_2							,       __lowerCAmelCase=1_6							,       __lowerCAmelCase=2							,       __lowerCAmelCase=0.02							,       __lowerCAmelCase=3							,       __lowerCAmelCase=4							,       __lowerCAmelCase=None							,       ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   parent
								__magic_name__		:Any   =   batch_size
								__magic_name__		:Any   =   seq_length
								__magic_name__		:Optional[Any]   =   is_training
								__magic_name__		:int   =   use_input_mask
								__magic_name__		:str   =   use_token_type_ids
								__magic_name__		:Any   =   use_labels
								__magic_name__		:Optional[int]   =   vocab_size
								__magic_name__		:Tuple   =   hidden_size
								__magic_name__		:Dict   =   num_hidden_layers
								__magic_name__		:int   =   num_attention_heads
								__magic_name__		:Optional[Any]   =   intermediate_size
								__magic_name__		:Tuple   =   hidden_act
								__magic_name__		:Union[str, Any]   =   hidden_dropout_prob
								__magic_name__		:int   =   attention_probs_dropout_prob
								__magic_name__		:Tuple   =   max_position_embeddings
								__magic_name__		:Any   =   type_vocab_size
								__magic_name__		:str   =   type_sequence_label_size
								__magic_name__		:Any   =   initializer_range
								__magic_name__		:Union[str, Any]   =   num_labels
								__magic_name__		:int   =   num_choices
								__magic_name__		:Any   =   scope







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Optional[int]   =   ids_tensor([self.batch_size, self.seq_length]							,       self.vocab_size  )

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

								__magic_name__		:Any   =   None

								__magic_name__		:int   =   None
								__magic_name__		:Optional[int]   =   None
								__magic_name__		:str   =   None
								if self.use_labels:
												__magic_name__		:Any   =   ids_tensor([self.batch_size]							,       self.type_sequence_label_size  )
												__magic_name__		:List[str]   =   ids_tensor([self.batch_size, self.seq_length]							,       self.num_labels  )
												__magic_name__		:Optional[int]   =   ids_tensor([self.batch_size]							,       self.num_choices  )

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

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







				def       A						(    self  ):
								"""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=__lowerCAmelCase							,       initializer_range=self.initializer_range							,       pad_token_id=1							,       new_decoder_architecture=__lowerCAmelCase							,       )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase  ):
								"""simple docstring"""
								__magic_name__		:List[str]   =   FalconModel(config=__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:str   =   model(__lowerCAmelCase							,       attention_mask=__lowerCAmelCase  )
								__magic_name__		:str   =   model(__lowerCAmelCase  )
								self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.hidden_size)  )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   True
								__magic_name__		:str   =   FalconModel(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:int   =   model(
								    __lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       encoder_hidden_states=__lowerCAmelCase							,       encoder_attention_mask=__lowerCAmelCase							,       )
								__magic_name__		:Optional[Any]   =   model(
								    __lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       encoder_hidden_states=__lowerCAmelCase							,       )
								__magic_name__		:int   =   model(__lowerCAmelCase							,       attention_mask=__lowerCAmelCase  )
								self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.hidden_size)  )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   FalconForCausalLM(config=__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:Optional[Any]   =   model(__lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       labels=__lowerCAmelCase  )
								self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.seq_length, self.vocab_size)  )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:List[Any]   =   True
								__magic_name__		:Union[str, Any]   =   True
								__magic_name__		:Tuple   =   FalconForCausalLM(config=__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()

								# first forward pass
								__magic_name__		:Optional[Any]   =   model(
								    __lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       encoder_hidden_states=__lowerCAmelCase							,       encoder_attention_mask=__lowerCAmelCase							,       use_cache=__lowerCAmelCase							,       )
								__magic_name__		:Dict   =   outputs.past_key_values

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

								# append to next input_ids and
								__magic_name__		:int   =   torch.cat([input_ids, next_tokens]							,       dim=-1  )
								__magic_name__		:Union[str, Any]   =   torch.cat([input_mask, next_mask]							,       dim=-1  )

								__magic_name__		:Tuple   =   model(
								    __lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       encoder_hidden_states=__lowerCAmelCase							,       encoder_attention_mask=__lowerCAmelCase							,       output_hidden_states=__lowerCAmelCase							,       )['''hidden_states'''][0]
								__magic_name__		:List[Any]   =   model(
								    __lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       encoder_hidden_states=__lowerCAmelCase							,       encoder_attention_mask=__lowerCAmelCase							,       past_key_values=__lowerCAmelCase							,       output_hidden_states=__lowerCAmelCase							,       )['''hidden_states'''][0]

								# select random slice
								__magic_name__		:Tuple   =   ids_tensor((1,)							,       output_from_past.shape[-1]  ).item()
								__magic_name__		:Optional[Any]   =   output_from_no_past[:, -3:, random_slice_idx].detach()
								__magic_name__		:Optional[int]   =   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(__lowerCAmelCase							,       __lowerCAmelCase							,       atol=1E-3  )  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Optional[int]   =   self.prepare_config_and_inputs()
								(
								    (
								    __magic_name__			
								)  ,					(
								    __magic_name__			
								)  ,					(
								    __magic_name__			
								)  ,					(
								    __magic_name__			
								)  ,					(
								    __magic_name__			
								)  ,					(
								    __magic_name__			
								)  ,					(
								    __magic_name__			
								)  ,								
								)		:List[Any]   =   config_and_inputs
								__magic_name__		:List[str]   =   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict







@require_torch
class     lowerCamelCase_					(   lowerCamelCase      ,					lowerCamelCase      ,					lowerCamelCase      ,					unittest.TestCase			):
				a__       							=	(
				    (
				        FalconModel,
				        FalconForCausalLM,
				        FalconForSequenceClassification,
				        FalconForTokenClassification,
				        FalconForQuestionAnswering,
				    )
				    if is_torch_available()
				    else ()
				)
				a__       							=	(FalconForCausalLM,) if is_torch_available() else ()
				a__       							=	(
				    {
				        '''feature-extraction''': FalconModel,
				        '''text-classification''': FalconForSequenceClassification,
				        '''text-generation''': FalconForCausalLM,
				        '''question-answering''': FalconForQuestionAnswering,
				        '''token-classification''': FalconForTokenClassification,
				        '''zero-shot''': FalconForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				a__       							=	False
				a__       							=	False







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   FalconModelTester(self  )
								__magic_name__		:int   =   ConfigTester(self							,       config_class=__lowerCAmelCase							,       hidden_size=3_7  )







				def       A						(    self  ):
								"""simple docstring"""
								self.config_tester.run_common_tests()







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Dict   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__  ,					*__magic_name__		:Tuple   =   self.model_tester.prepare_config_and_inputs()
								for alibi in [True, False]:
												__magic_name__		:str   =   alibi
												self.model_tester.create_and_check_model(__lowerCAmelCase							,       *__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__  ,					__magic_name__		:str   =   self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__		:Optional[Any]   =   3
								__magic_name__		:str   =   input_dict['''input_ids''']
								__magic_name__		:List[Any]   =   input_ids.ne(1  ).to(__lowerCAmelCase  )
								__magic_name__		:List[Any]   =   ids_tensor([self.model_tester.batch_size]							,       self.model_tester.type_sequence_label_size  )
								__magic_name__		:Dict   =   FalconForSequenceClassification(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:str   =   model(__lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       labels=__lowerCAmelCase  )
								self.assertEqual(result.logits.shape							,       (self.model_tester.batch_size, self.model_tester.num_labels)  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__  ,					__magic_name__		:Union[str, Any]   =   self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__		:Optional[Any]   =   3
								__magic_name__		:List[str]   =   '''single_label_classification'''
								__magic_name__		:Any   =   input_dict['''input_ids''']
								__magic_name__		:Any   =   input_ids.ne(1  ).to(__lowerCAmelCase  )
								__magic_name__		:List[Any]   =   ids_tensor([self.model_tester.batch_size]							,       self.model_tester.type_sequence_label_size  )
								__magic_name__		:List[Any]   =   FalconForSequenceClassification(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:Optional[Any]   =   model(__lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       labels=__lowerCAmelCase  )
								self.assertEqual(result.logits.shape							,       (self.model_tester.batch_size, self.model_tester.num_labels)  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__  ,					__magic_name__		:Any   =   self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__		:List[str]   =   input_dict['''input_ids''']
								__magic_name__		:Optional[int]   =   FalconForCausalLM(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:List[Any]   =   model(__lowerCAmelCase							,       use_cache=__lowerCAmelCase  )
								__magic_name__		:str   =   input_ids.shape[0]
								__magic_name__		:str   =   model._convert_to_rw_cache(result.past_key_values  )
								__magic_name__		:str   =   model._convert_cache_to_standard_format(__lowerCAmelCase							,       __lowerCAmelCase  )
								for layer in range(len(__lowerCAmelCase  )  ):
												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       A						(    self  ):
								"""simple docstring"""
								__magic_name__  ,					__magic_name__		:Any   =   self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__		:List[str]   =   3
								__magic_name__		:Dict   =   '''multi_label_classification'''
								__magic_name__		:List[Any]   =   input_dict['''input_ids''']
								__magic_name__		:List[Any]   =   input_ids.ne(1  ).to(__lowerCAmelCase  )
								__magic_name__		:Tuple   =   ids_tensor(
								    [self.model_tester.batch_size, config.num_labels]							,       self.model_tester.type_sequence_label_size  ).to(torch.float  )
								__magic_name__		:List[Any]   =   FalconForSequenceClassification(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:str   =   model(__lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       labels=__lowerCAmelCase  )
								self.assertEqual(result.logits.shape							,       (self.model_tester.batch_size, self.model_tester.num_labels)  )







				def       A						(    self  ):
								"""simple docstring"""


								for model_class in self.all_generative_model_classes:
												__magic_name__  ,					__magic_name__		:Optional[Any]   =   self.model_tester.prepare_config_and_inputs_for_common()

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

												__magic_name__		:List[str]   =   model_class(__lowerCAmelCase  ).to(__lowerCAmelCase  )
												if "use_cache" not in inputs:
																__magic_name__		:str   =   True
												__magic_name__		:Any   =   model(**__lowerCAmelCase  )

												# 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

												__magic_name__		:Tuple   =   (
												    getattr(__lowerCAmelCase							,       '''decoder_layers'''							,       __lowerCAmelCase  )
												    or getattr(__lowerCAmelCase							,       '''num_decoder_layers'''							,       __lowerCAmelCase  )
												    or config.num_hidden_layers
												)
												__magic_name__		:int   =   getattr(__lowerCAmelCase							,       '''num_kv_heads'''							,       config.num_attention_heads  )
												__magic_name__		:str   =   getattr(__lowerCAmelCase							,       '''d_model'''							,       config.hidden_size  )
												__magic_name__		:int   =   embed_dim // num_attention_heads

												__magic_name__		:Tuple   =   outputs['''past_key_values''']
												self.assertEqual(len(__lowerCAmelCase  )							,       __lowerCAmelCase  )

												__magic_name__  ,					__magic_name__		:int   =   inputs['''input_ids'''].shape
												for i in range(__lowerCAmelCase  ):
																if config.new_decoder_architecture:
																				__magic_name__		:List[str]   =   config.num_attention_heads
																elif config.multi_query:
																				__magic_name__		: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     lowerCamelCase_					(   unittest.TestCase			):







				@slow
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[Any]   =   AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''  )
								__magic_name__		:Optional[Any]   =   FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''  )
								model.eval()
								model.to(__lowerCAmelCase  )
								__magic_name__		:str   =   tokenizer('''My favorite food is'''							,       return_tensors='''pt'''  ).to(__lowerCAmelCase  )

								__magic_name__		:List[str]   =   (
								    '''My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'''
								)

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

								self.assertEqual(__lowerCAmelCase							,       __lowerCAmelCase  )







				@slow
				def       A						(    self  ):
								"""simple docstring"""


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

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







				@slow
				def       A						(    self  ):
								"""simple docstring"""


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

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












'''simple docstring'''







import socket







def 						_A      ( ):






   '''simple docstring'''







   A__       =     socket.socket(socket.AF_INET      ,socket.SOCK_STREAM						)
   A__       =     socket.gethostname()
   A__       =     12312

   sock.connect((host, port)						)
   sock.send(B'Hello server!'						)

   with open('Received_file'      ,'wb'						) as out_file:
      print('File opened'						)
      print('Receiving data...'						)
      while True:
         A__       =     sock.recv(1024						)
         if not data:
            break
         out_file.write(UpperCAmelCase						)

   print('Successfully received the file'						)
   sock.close()
   print('Connection closed'						)


if __name__ == "__main__":
    main()


