


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







						'''simple docstring'''




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




						def   lowercase_       ( self							)    ->			Union[str, Any]:
										lowerCAmelCase_       :					int			 = ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size							)

										lowerCAmelCase_       :					int			 = None
										if self.use_input_mask:
														lowerCAmelCase_       :					Dict			 = random_attention_mask([self.batch_size, self.seq_length]							)

										lowerCAmelCase_       :					Any			 = None

										lowerCAmelCase_       :					Optional[Any]			 = None
										lowerCAmelCase_       :					Tuple			 = None
										lowerCAmelCase_       :					Dict			 = None
										if self.use_labels:
														lowerCAmelCase_       :					str			 = ids_tensor([self.batch_size]      ,  self.type_sequence_label_size							)
														lowerCAmelCase_       :					Tuple			 = ids_tensor([self.batch_size, self.seq_length]      ,  self.num_labels							)
														lowerCAmelCase_       :					str			 = ids_tensor([self.batch_size]      ,  self.num_choices							)

										lowerCAmelCase_       :					Union[str, Any]			 = self.get_config()

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




						def   lowercase_       ( self							)    ->			str:
										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   lowercase_       ( self      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase							)    ->			Tuple:
										lowerCAmelCase_       :					List[Any]			 = FalconModel(config=lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										lowerCAmelCase_       :					List[str]			 = model(lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__							)
										lowerCAmelCase_       :					str			 = model(lowerCAmelCase__							)
										self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)							)




						def   lowercase_       ( self      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  )    ->			Any:
										lowerCAmelCase_       :					List[Any]			 = True
										lowerCAmelCase_       :					Any			 = FalconModel(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										lowerCAmelCase_       :					int			 = model(
										    lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__      ,  encoder_hidden_states=lowerCAmelCase__      ,  encoder_attention_mask=lowerCAmelCase__      ,  )
										lowerCAmelCase_       :					int			 = model(
										    lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__      ,  encoder_hidden_states=lowerCAmelCase__      ,  )
										lowerCAmelCase_       :					Any			 = model(lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__							)
										self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)							)




						def   lowercase_       ( self      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  )    ->			Union[str, Any]:
										lowerCAmelCase_       :					Any			 = FalconForCausalLM(config=lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										lowerCAmelCase_       :					Tuple			 = model(lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__      ,  labels=lowerCAmelCase__							)
										self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.vocab_size)							)




						def   lowercase_       ( self      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  __lowercase      ,  )    ->			Dict:
										lowerCAmelCase_       :					str			 = True
										lowerCAmelCase_       :					Union[str, Any]			 = True
										lowerCAmelCase_       :					str			 = FalconForCausalLM(config=lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()

										# first forward pass
										lowerCAmelCase_       :					int			 = model(
										    lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__      ,  encoder_hidden_states=lowerCAmelCase__      ,  encoder_attention_mask=lowerCAmelCase__      ,  use_cache=lowerCAmelCase__      ,  )
										lowerCAmelCase_       :					str			 = outputs.past_key_values

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

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

										lowerCAmelCase_       :					str			 = model(
										    lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__      ,  encoder_hidden_states=lowerCAmelCase__      ,  encoder_attention_mask=lowerCAmelCase__      ,  output_hidden_states=lowerCAmelCase__      ,  )["hidden_states"][0]
										lowerCAmelCase_       :					Tuple			 = 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
										lowerCAmelCase_       :					int			 = ids_tensor((1,)      ,  output_from_past.shape[-1]							).item()
										lowerCAmelCase_       :					List[Any]			 = output_from_no_past[:, -3:, random_slice_idx].detach()
										lowerCAmelCase_       :					Tuple			 = 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   lowercase_       ( self							)    ->			Any:
										lowerCAmelCase_       :					Dict			 = self.prepare_config_and_inputs()
										(
										    lowerCAmelCase_							
										)       :					Tuple			 = config_and_inputs
										lowerCAmelCase_       :					Optional[Any]			 = {"input_ids": input_ids, "attention_mask": input_mask}
										return config, inputs_dict







@require_torch
class  snake_case__(							UpperCamelCase__,  UpperCamelCase__,  UpperCamelCase__,  unittest.TestCase							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__					:     int    =							(
						    (
						        FalconModel,
						        FalconForCausalLM,
						        FalconForSequenceClassification,
						        FalconForTokenClassification,
						        FalconForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						SCREAMING_SNAKE_CASE__					:     Any    =							(FalconForCausalLM,) if is_torch_available() else ()
						SCREAMING_SNAKE_CASE__					:     int    =							(
						    {
						        """feature-extraction""": FalconModel,
						        """text-classification""": FalconForSequenceClassification,
						        """text-generation""": FalconForCausalLM,
						        """question-answering""": FalconForQuestionAnswering,
						        """token-classification""": FalconForTokenClassification,
						        """zero-shot""": FalconForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						SCREAMING_SNAKE_CASE__					:     Dict    =							False
						SCREAMING_SNAKE_CASE__					:     int    =							False




						def   lowercase_       ( self							)    ->			Union[str, Any]:
										lowerCAmelCase_       :					str			 = FalconModelTester(self							)
										lowerCAmelCase_       :					Any			 = ConfigTester(self      ,  config_class=lowerCAmelCase__      ,  hidden_size=3_7							)




						def   lowercase_       ( self							)    ->			Dict:
										self.config_tester.run_common_tests()




						def   lowercase_       ( self							)    ->			Dict:
										lowerCAmelCase_       :					Optional[int]			 = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__							)




						def   lowercase_       ( self							)    ->			List[str]:
										lowerCAmelCase_       :					Dict			 = self.model_tester.prepare_config_and_inputs()
										for alibi in [True, False]:
														lowerCAmelCase_       :					Any			 = alibi
														self.model_tester.create_and_check_model(lowerCAmelCase__      ,  *lowerCAmelCase__							)




						def   lowercase_       ( self							)    ->			Optional[Any]:
										lowerCAmelCase_       :					List[str]			 = self.model_tester.prepare_config_and_inputs_for_common()
										lowerCAmelCase_       :					List[str]			 = 3
										lowerCAmelCase_       :					List[Any]			 = input_dict["input_ids"]
										lowerCAmelCase_       :					Tuple			 = input_ids.ne(1							).to(lowerCAmelCase__							)
										lowerCAmelCase_       :					Optional[int]			 = ids_tensor([self.model_tester.batch_size]      ,  self.model_tester.type_sequence_label_size							)
										lowerCAmelCase_       :					Tuple			 = FalconForSequenceClassification(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										lowerCAmelCase_       :					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   lowercase_       ( self							)    ->			int:
										lowerCAmelCase_       :					int			 = self.model_tester.prepare_config_and_inputs_for_common()
										lowerCAmelCase_       :					Optional[Any]			 = 3
										lowerCAmelCase_       :					Dict			 = "single_label_classification"
										lowerCAmelCase_       :					Optional[int]			 = input_dict["input_ids"]
										lowerCAmelCase_       :					Any			 = input_ids.ne(1							).to(lowerCAmelCase__							)
										lowerCAmelCase_       :					Optional[Any]			 = ids_tensor([self.model_tester.batch_size]      ,  self.model_tester.type_sequence_label_size							)
										lowerCAmelCase_       :					Dict			 = FalconForSequenceClassification(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										lowerCAmelCase_       :					Optional[int]			 = model(lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__      ,  labels=lowerCAmelCase__							)
										self.assertEqual(result.logits.shape      ,  (self.model_tester.batch_size, self.model_tester.num_labels)							)




						def   lowercase_       ( self							)    ->			List[str]:
										lowerCAmelCase_       :					Union[str, Any]			 = self.model_tester.prepare_config_and_inputs_for_common()
										lowerCAmelCase_       :					str			 = input_dict["input_ids"]
										lowerCAmelCase_       :					Any			 = FalconForCausalLM(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										lowerCAmelCase_       :					Union[str, Any]			 = model(lowerCAmelCase__      ,  use_cache=lowerCAmelCase__							)
										lowerCAmelCase_       :					Union[str, Any]			 = input_ids.shape[0]
										lowerCAmelCase_       :					Union[str, Any]			 = model._convert_to_rw_cache(result.past_key_values							)
										lowerCAmelCase_       :					int			 = 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   lowercase_       ( self							)    ->			int:
										lowerCAmelCase_       :					List[str]			 = self.model_tester.prepare_config_and_inputs_for_common()
										lowerCAmelCase_       :					List[str]			 = 3
										lowerCAmelCase_       :					Union[str, Any]			 = "multi_label_classification"
										lowerCAmelCase_       :					Optional[int]			 = input_dict["input_ids"]
										lowerCAmelCase_       :					List[Any]			 = input_ids.ne(1							).to(lowerCAmelCase__							)
										lowerCAmelCase_       :					Tuple			 = ids_tensor(
										    [self.model_tester.batch_size, config.num_labels]      ,  self.model_tester.type_sequence_label_size							).to(torch.float							)
										lowerCAmelCase_       :					Tuple			 = FalconForSequenceClassification(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										lowerCAmelCase_       :					Tuple			 = model(lowerCAmelCase__      ,  attention_mask=lowerCAmelCase__      ,  labels=lowerCAmelCase__							)
										self.assertEqual(result.logits.shape      ,  (self.model_tester.batch_size, self.model_tester.num_labels)							)




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




										for model_class in self.all_generative_model_classes:
														lowerCAmelCase_       :					int			 = 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

														lowerCAmelCase_       :					int			 = model_class(lowerCAmelCase__							).to(lowerCAmelCase__							)
														if "use_cache" not in inputs:
																		lowerCAmelCase_       :					Optional[int]			 = True
														lowerCAmelCase_       :					Dict			 = 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

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

														lowerCAmelCase_       :					List[Any]			 = outputs["past_key_values"]
														self.assertEqual(len(lowerCAmelCase__							)      ,  lowerCAmelCase__							)

														lowerCAmelCase_       :					Tuple			 = inputs["input_ids"].shape
														for i in range(lowerCAmelCase__							):
																		if config.new_decoder_architecture:
																						lowerCAmelCase_       :					Dict			 = config.num_attention_heads
																		elif config.multi_query:
																						lowerCAmelCase_       :					List[str]			 = 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  snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						@slow
						def   lowercase_       ( self							)    ->			Dict:
										lowerCAmelCase_       :					str			 = AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''							)
										lowerCAmelCase_       :					str			 = FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''							)
										model.eval()
										model.to(lowerCAmelCase__							)
										lowerCAmelCase_       :					int			 = tokenizer('''My favorite food is'''      ,  return_tensors='''pt'''							).to(lowerCAmelCase__							)

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

										lowerCAmelCase_       :					Tuple			 = model.generate(**lowerCAmelCase__      ,  do_sample=lowerCAmelCase__      ,  max_new_tokens=1_9							)
										lowerCAmelCase_       :					Tuple			 = tokenizer.batch_decode(lowerCAmelCase__							)[0]

										self.assertEqual(lowerCAmelCase__      ,  lowerCAmelCase__							)




						@slow
						def   lowercase_       ( self							)    ->			List[Any]:




										for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
														lowerCAmelCase_       :					Union[str, Any]			 = AutoTokenizer.from_pretrained(lowerCAmelCase__							)
														lowerCAmelCase_       :					Any			 = FalconForCausalLM.from_pretrained(lowerCAmelCase__							)
														model.eval()
														model.to(lowerCAmelCase__							)
														lowerCAmelCase_       :					Optional[int]			 = 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   lowercase_       ( self							)    ->			Optional[Any]:




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

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






def      _A       (		SCREAMING_SNAKE_CASE     :   int = 50						):





  """simple docstring"""



  a__	:					Any										=[1] * (length + 1)

  for row_length in range(3       ,       length + 1						):
    for block_length in range(3       ,       row_length + 1						):
      for block_start in range(row_length - block_length						):
        ways_number[row_length] += ways_number[
            row_length - block_start - block_length - 1
        ]

      ways_number[row_length] += 1

  return ways_number[length]


if __name__ == "__main__":
     print(F"""{solution() = }""")






