



"""simple docstring"""


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       :
			"""simple docstring"""



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



			def        lowercase__  (						self   :			List[str]      )->Union[str, Any]:
							_UpperCAmelCase							=			ids_tensor([self.batch_size, self.seq_length] ,      self.vocab_size      )

							_UpperCAmelCase							=			None
							if self.use_input_mask:
											_UpperCAmelCase							=			random_attention_mask([self.batch_size, self.seq_length]      )

							_UpperCAmelCase							=			None

							_UpperCAmelCase							=			None
							_UpperCAmelCase							=			None
							_UpperCAmelCase							=			None
							if self.use_labels:
											_UpperCAmelCase							=			ids_tensor([self.batch_size] ,      self.type_sequence_label_size      )
											_UpperCAmelCase							=			ids_tensor([self.batch_size, self.seq_length] ,      self.num_labels      )
											_UpperCAmelCase							=			ids_tensor([self.batch_size] ,      self.num_choices      )

							_UpperCAmelCase							=			self.get_config()

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



			def        lowercase__  (						self   :			Any      )->Optional[int]:
							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=__UpperCAmelCase ,      initializer_range=self.initializer_range ,      pad_token_id=1 ,      new_decoder_architecture=__UpperCAmelCase ,      )



			def        lowercase__  (						self   :			List[Any] ,      __UpperCamelCase   :			Dict ,      __UpperCamelCase   :			Optional[int] ,      __UpperCamelCase   :			Optional[int] ,      __UpperCamelCase   :			int ,      __UpperCamelCase   :			List[str] ,      __UpperCamelCase   :			Union[str, Any] ,      __UpperCamelCase   :			int      )->List[str]:
							_UpperCAmelCase							=			FalconModel(config=__UpperCAmelCase      )
							model.to(__UpperCAmelCase      )
							model.eval()
							_UpperCAmelCase							=			model(__UpperCAmelCase ,      attention_mask=__UpperCAmelCase      )
							_UpperCAmelCase							=			model(__UpperCAmelCase      )
							self.parent.assertEqual(result.last_hidden_state.shape ,      (self.batch_size, self.seq_length, self.hidden_size)      )



			def        lowercase__  (						self   :			Tuple ,      __UpperCamelCase   :			List[Any] ,      __UpperCamelCase   :			Optional[Any] ,      __UpperCamelCase   :			Union[str, Any] ,      __UpperCamelCase   :			Union[str, Any] ,      __UpperCamelCase   :			str ,      __UpperCamelCase   :			Any ,      __UpperCamelCase   :			int ,      __UpperCamelCase   :			List[Any] ,      __UpperCamelCase   :			Optional[Any] ,      )->str:
							_UpperCAmelCase							=			True
							_UpperCAmelCase							=			FalconModel(__UpperCAmelCase      )
							model.to(__UpperCAmelCase      )
							model.eval()
							_UpperCAmelCase							=			model(
							    __UpperCAmelCase ,      attention_mask=__UpperCAmelCase ,      encoder_hidden_states=__UpperCAmelCase ,      encoder_attention_mask=__UpperCAmelCase ,      )
							_UpperCAmelCase							=			model(
							    __UpperCAmelCase ,      attention_mask=__UpperCAmelCase ,      encoder_hidden_states=__UpperCAmelCase ,      )
							_UpperCAmelCase							=			model(__UpperCAmelCase ,      attention_mask=__UpperCAmelCase      )
							self.parent.assertEqual(result.last_hidden_state.shape ,      (self.batch_size, self.seq_length, self.hidden_size)      )



			def        lowercase__  (						self   :			List[Any] ,      __UpperCamelCase   :			Dict ,      __UpperCamelCase   :			int ,      __UpperCamelCase   :			List[str] ,      __UpperCamelCase   :			Optional[int] ,      __UpperCamelCase   :			List[str] ,      __UpperCamelCase   :			Any ,      __UpperCamelCase   :			Optional[int] ,      __UpperCamelCase   :			str ,      __UpperCamelCase   :			List[str] ,      )->Optional[Any]:
							_UpperCAmelCase							=			FalconForCausalLM(config=__UpperCAmelCase      )
							model.to(__UpperCAmelCase      )
							model.eval()
							_UpperCAmelCase							=			model(__UpperCAmelCase ,      attention_mask=__UpperCAmelCase ,      labels=__UpperCAmelCase      )
							self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.seq_length, self.vocab_size)      )



			def        lowercase__  (						self   :			Dict ,      __UpperCamelCase   :			Optional[Any] ,      __UpperCamelCase   :			Union[str, Any] ,      __UpperCamelCase   :			Dict ,      __UpperCamelCase   :			Union[str, Any] ,      __UpperCamelCase   :			str ,      __UpperCamelCase   :			Tuple ,      __UpperCamelCase   :			int ,      __UpperCamelCase   :			Union[str, Any] ,      __UpperCamelCase   :			Optional[int] ,      )->str:
							_UpperCAmelCase							=			True
							_UpperCAmelCase							=			True
							_UpperCAmelCase							=			FalconForCausalLM(config=__UpperCAmelCase      )
							model.to(__UpperCAmelCase      )
							model.eval()

							# first forward pass
							_UpperCAmelCase							=			model(
							    __UpperCAmelCase ,      attention_mask=__UpperCAmelCase ,      encoder_hidden_states=__UpperCAmelCase ,      encoder_attention_mask=__UpperCAmelCase ,      use_cache=__UpperCAmelCase ,      )
							_UpperCAmelCase							=			outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							_UpperCAmelCase							=			ids_tensor((self.batch_size, 3) ,      config.vocab_size      )
							_UpperCAmelCase							=			ids_tensor((self.batch_size, 3) ,      vocab_size=2      )

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

							_UpperCAmelCase							=			model(
							    __UpperCAmelCase ,      attention_mask=__UpperCAmelCase ,      encoder_hidden_states=__UpperCAmelCase ,      encoder_attention_mask=__UpperCAmelCase ,      output_hidden_states=__UpperCAmelCase ,      )['''hidden_states'''][0]
							_UpperCAmelCase							=			model(
							    __UpperCAmelCase ,      attention_mask=__UpperCAmelCase ,      encoder_hidden_states=__UpperCAmelCase ,      encoder_attention_mask=__UpperCAmelCase ,      past_key_values=__UpperCAmelCase ,      output_hidden_states=__UpperCAmelCase ,      )['''hidden_states'''][0]

							# select random slice
							_UpperCAmelCase							=			ids_tensor((1,) ,      output_from_past.shape[-1]      ).item()
							_UpperCAmelCase							=			output_from_no_past[:, -3:, random_slice_idx].detach()
							_UpperCAmelCase							=			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(__UpperCAmelCase ,      __UpperCAmelCase ,      atol=1e-3      )      )



			def        lowercase__  (						self   :			Any      )->Tuple:
							_UpperCAmelCase							=			self.prepare_config_and_inputs()
							(
							    (
							    _UpperCAmelCase     
							)						,  (
							    _UpperCAmelCase     
							)						,  (
							    _UpperCAmelCase     
							)						,  (
							    _UpperCAmelCase     
							)						,  (
							    _UpperCAmelCase     
							)						,  (
							    _UpperCAmelCase     
							)						,  (
							    _UpperCAmelCase     
							)						,       
							)							=			config_and_inputs
							_UpperCAmelCase							=			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_torch
class 	_a       (    lowerCAmelCase	,							lowerCAmelCase	,							lowerCAmelCase	,							unittest.TestCase):
			"""simple docstring"""
			UpperCamelCase__						=		(
			    (
			        FalconModel,
			        FalconForCausalLM,
			        FalconForSequenceClassification,
			        FalconForTokenClassification,
			        FalconForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__						=		(FalconForCausalLM,) if is_torch_available() else ()
			UpperCamelCase__						=		(
			    {
			        """feature-extraction""": FalconModel,
			        """text-classification""": FalconForSequenceClassification,
			        """text-generation""": FalconForCausalLM,
			        """question-answering""": FalconForQuestionAnswering,
			        """token-classification""": FalconForTokenClassification,
			        """zero-shot""": FalconForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			UpperCamelCase__						=		False
			UpperCamelCase__						=		False



			def        lowercase__  (						self   :			Dict      )->str:
							_UpperCAmelCase							=			FalconModelTester(self      )
							_UpperCAmelCase							=			ConfigTester(self ,      config_class=__UpperCAmelCase ,      hidden_size=3_7      )



			def        lowercase__  (						self   :			str      )->Union[str, Any]:
							self.config_tester.run_common_tests()



			def        lowercase__  (						self   :			List[str]      )->Tuple:
							_UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__UpperCAmelCase      )



			def        lowercase__  (						self   :			Union[str, Any]      )->Any:
							_UpperCAmelCase						,  *_UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs()
							for alibi in [True, False]:
											_UpperCAmelCase							=			alibi
											self.model_tester.create_and_check_model(__UpperCAmelCase ,      *__UpperCAmelCase      )



			def        lowercase__  (						self   :			Dict      )->int:
							_UpperCAmelCase						,  _UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCAmelCase							=			3
							_UpperCAmelCase							=			input_dict['''input_ids''']
							_UpperCAmelCase							=			input_ids.ne(1      ).to(__UpperCAmelCase      )
							_UpperCAmelCase							=			ids_tensor([self.model_tester.batch_size] ,      self.model_tester.type_sequence_label_size      )
							_UpperCAmelCase							=			FalconForSequenceClassification(__UpperCAmelCase      )
							model.to(__UpperCAmelCase      )
							model.eval()
							_UpperCAmelCase							=			model(__UpperCAmelCase ,      attention_mask=__UpperCAmelCase ,      labels=__UpperCAmelCase      )
							self.assertEqual(result.logits.shape ,      (self.model_tester.batch_size, self.model_tester.num_labels)      )



			def        lowercase__  (						self   :			str      )->Tuple:
							_UpperCAmelCase						,  _UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCAmelCase							=			3
							_UpperCAmelCase							=			'''single_label_classification'''
							_UpperCAmelCase							=			input_dict['''input_ids''']
							_UpperCAmelCase							=			input_ids.ne(1      ).to(__UpperCAmelCase      )
							_UpperCAmelCase							=			ids_tensor([self.model_tester.batch_size] ,      self.model_tester.type_sequence_label_size      )
							_UpperCAmelCase							=			FalconForSequenceClassification(__UpperCAmelCase      )
							model.to(__UpperCAmelCase      )
							model.eval()
							_UpperCAmelCase							=			model(__UpperCAmelCase ,      attention_mask=__UpperCAmelCase ,      labels=__UpperCAmelCase      )
							self.assertEqual(result.logits.shape ,      (self.model_tester.batch_size, self.model_tester.num_labels)      )



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



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


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

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

											_UpperCAmelCase							=			model_class(__UpperCAmelCase      ).to(__UpperCAmelCase      )
											if "use_cache" not in inputs:
															_UpperCAmelCase							=			True
											_UpperCAmelCase							=			model(**__UpperCAmelCase      )

											# 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

											_UpperCAmelCase							=			(
											    getattr(__UpperCAmelCase ,      '''decoder_layers''' ,      __UpperCAmelCase      )
											    or getattr(__UpperCAmelCase ,      '''num_decoder_layers''' ,      __UpperCAmelCase      )
											    or config.num_hidden_layers
											)
											_UpperCAmelCase							=			getattr(__UpperCAmelCase ,      '''num_kv_heads''' ,      config.num_attention_heads      )
											_UpperCAmelCase							=			getattr(__UpperCAmelCase ,      '''d_model''' ,      config.hidden_size      )
											_UpperCAmelCase							=			embed_dim // num_attention_heads

											_UpperCAmelCase							=			outputs['''past_key_values''']
											self.assertEqual(len(__UpperCAmelCase      ) ,      __UpperCAmelCase      )

											_UpperCAmelCase						,  _UpperCAmelCase							=			inputs['''input_ids'''].shape
											for i in range(__UpperCAmelCase      ):
															if config.new_decoder_architecture:
																			_UpperCAmelCase							=			config.num_attention_heads
															elif config.multi_query:
																			_UpperCAmelCase							=			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):
			"""simple docstring"""



			@slow
			def        lowercase__  (						self   :			Dict      )->Tuple:
							_UpperCAmelCase							=			AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''      )
							_UpperCAmelCase							=			FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''      )
							model.eval()
							model.to(__UpperCAmelCase      )
							_UpperCAmelCase							=			tokenizer('''My favorite food is''' ,      return_tensors='''pt'''      ).to(__UpperCAmelCase      )

							_UpperCAmelCase							=			(
							    '''My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'''
							)

							_UpperCAmelCase							=			model.generate(**__UpperCAmelCase ,      do_sample=__UpperCAmelCase ,      max_new_tokens=1_9      )
							_UpperCAmelCase							=			tokenizer.batch_decode(__UpperCAmelCase      )[0]

							self.assertEqual(__UpperCAmelCase ,      __UpperCAmelCase      )



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


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

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



			@slow
			def        lowercase__  (						self   :			Tuple      )->Dict:


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

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











def 						_a    (      a :int )							->			list:

 # bit count represents no. of bits in the gray code
 if bit_count < 0:
  raise ValueError('''The given input must be positive''' )

 # get the generated string sequence
 a		  =      gray_code_sequence_string(a )
 #
 # convert them to integers
 for i in range(len(a ) ):
  a		  =      int(sequence[i]	,							2 )

 return sequence
def 						_a    (      a :int )							->			list:

 # The approach is a recursive one
 # Base case achieved when either n = 0 or n=1
 if bit_count == 0:
  return ["0"]

 if bit_count == 1:
  return ["0", "1"]

 a		  =      1 << bit_count  # defines the length of the sequence
 # 1<< n is equivalent to 2^n

 # recursive answer will generate answer for n-1 bits
 a		  =      gray_code_sequence_string(bit_count - 1 )

 a		  =      []

 # append 0 to first half of the smaller sequence generated
 for i in range(seq_len // 2 ):
  a		  =      '''0''' + smaller_sequence[i]
  sequence.append(a )

 # append 1 to second half ... start from the end of the list
 for i in reversed(range(seq_len // 2 ) ):
  a		  =      '''1''' + smaller_sequence[i]
  sequence.append(a )

 return sequence


if __name__ == "__main__":
     import doctest

     doctest.testmod()


