

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





    def __init__(				self						,       a						,       a=3						,       a=7						,       a=True						,       a=True						,       a=False						,       a=True						,       a=99						,       a=32						,       a=5						,       a=4						,       a=37						,       a="gelu"						,       a=0.1						,       a=0.1						,       a=512						,       a=16						,       a=2						,       a=0.02						,       a=3						,       a=4						,       a=None						,       ):
         lowercase__   :       List[Any]				=	parent
         lowercase__   :       Optional[Any]				=	batch_size
         lowercase__   :       Tuple				=	seq_length
         lowercase__   :       Any				=	is_training
         lowercase__   :       str				=	use_input_mask
         lowercase__   :       str				=	use_token_type_ids
         lowercase__   :       List[Any]				=	use_labels
         lowercase__   :       Union[str, Any]				=	vocab_size
         lowercase__   :       Optional[Any]				=	hidden_size
         lowercase__   :       Dict				=	num_hidden_layers
         lowercase__   :       List[Any]				=	num_attention_heads
         lowercase__   :       int				=	intermediate_size
         lowercase__   :       Tuple				=	hidden_act
         lowercase__   :       Any				=	hidden_dropout_prob
         lowercase__   :       Union[str, Any]				=	attention_probs_dropout_prob
         lowercase__   :       Optional[int]				=	max_position_embeddings
         lowercase__   :       Optional[int]				=	type_vocab_size
         lowercase__   :       Optional[int]				=	type_sequence_label_size
         lowercase__   :       Optional[int]				=	initializer_range
         lowercase__   :       str				=	num_labels
         lowercase__   :       Union[str, Any]				=	num_choices
         lowercase__   :       Any				=	scope





    def 				snake_case_    (				self):
         lowercase__   :       int				=	ids_tensor([self.batch_size, self.seq_length]						,       self.vocab_size)

         lowercase__   :       Union[str, Any]				=	None
         if self.use_input_mask:
              lowercase__   :       List[Any]				=	random_attention_mask([self.batch_size, self.seq_length])

         lowercase__   :       List[Any]				=	None

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

         lowercase__   :       int				=	self.get_config()

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





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





    def 				snake_case_    (				self						,       a						,       a						,       a						,       a						,       a						,       a						,       a):
         lowercase__   :       str				=	FalconModel(config=a)
         model.to(a)
         model.eval()
         lowercase__   :       Dict				=	model(a						,       attention_mask=a)
         lowercase__   :       List[str]				=	model(a)
         self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.hidden_size))





    def 				snake_case_    (				self						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       ):
         lowercase__   :       Optional[Any]				=	True
         lowercase__   :       Optional[int]				=	FalconModel(a)
         model.to(a)
         model.eval()
         lowercase__   :       int				=	model(
             a						,       attention_mask=a						,       encoder_hidden_states=a						,       encoder_attention_mask=a						,       )
         lowercase__   :       List[Any]				=	model(
             a						,       attention_mask=a						,       encoder_hidden_states=a						,       )
         lowercase__   :       Any				=	model(a						,       attention_mask=a)
         self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.hidden_size))





    def 				snake_case_    (				self						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       ):
         lowercase__   :       Tuple				=	FalconForCausalLM(config=a)
         model.to(a)
         model.eval()
         lowercase__   :       List[Any]				=	model(a						,       attention_mask=a						,       labels=a)
         self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.seq_length, self.vocab_size))





    def 				snake_case_    (				self						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       a						,       ):
         lowercase__   :       Optional[int]				=	True
         lowercase__   :       Union[str, Any]				=	True
         lowercase__   :       Optional[Any]				=	FalconForCausalLM(config=a)
         model.to(a)
         model.eval()

         # first forward pass
         lowercase__   :       Dict				=	model(
             a						,       attention_mask=a						,       encoder_hidden_states=a						,       encoder_attention_mask=a						,       use_cache=a						,       )
         lowercase__   :       Any				=	outputs.past_key_values

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

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

         lowercase__   :       Any				=	model(
             a						,       attention_mask=a						,       encoder_hidden_states=a						,       encoder_attention_mask=a						,       output_hidden_states=a						,       )['hidden_states'][0]
         lowercase__   :       List[str]				=	model(
             a						,       attention_mask=a						,       encoder_hidden_states=a						,       encoder_attention_mask=a						,       past_key_values=a						,       output_hidden_states=a						,       )['hidden_states'][0]

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





    def 				snake_case_    (				self):
         lowercase__   :       Any				=	self.prepare_config_and_inputs()
         (
             (
             lowercase__
         )				,			(
             lowercase__
         )				,			(
             lowercase__
         )				,			(
             lowercase__
         )				,			(
             lowercase__
         )				,			(
             lowercase__
         )				,			(
             lowercase__
         )				,			
         )   :       Dict				=	config_and_inputs
         lowercase__   :       Optional[Any]				=	{'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict



@require_torch
class 				SCREAMING_SNAKE_CASE__ (__snake_case		,	__snake_case		,	__snake_case		,	unittest.TestCase					):
    __lowerCamelCase						:	Optional[int]														=					(
        (
            FalconModel,
            FalconForCausalLM,
            FalconForSequenceClassification,
            FalconForTokenClassification,
            FalconForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    __lowerCamelCase						:	int														=					(FalconForCausalLM,) if is_torch_available() else ()
    __lowerCamelCase						:	Optional[Any]														=					(
        {
            """feature-extraction""": FalconModel,
            """text-classification""": FalconForSequenceClassification,
            """text-generation""": FalconForCausalLM,
            """question-answering""": FalconForQuestionAnswering,
            """token-classification""": FalconForTokenClassification,
            """zero-shot""": FalconForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    __lowerCamelCase						:	List[Any]														=					False
    __lowerCamelCase						:	Union[str, Any]														=					False





    def 				snake_case_    (				self):
         lowercase__   :       List[Any]				=	FalconModelTester(self)
         lowercase__   :       str				=	ConfigTester(self						,       config_class=a						,       hidden_size=37)





    def 				snake_case_    (				self):
         self.config_tester.run_common_tests()





    def 				snake_case_    (				self):
         lowercase__   :       Dict				=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*a)





    def 				snake_case_    (				self):
         lowercase__				,			*lowercase__   :       Tuple				=	self.model_tester.prepare_config_and_inputs()
         for alibi in [True, False]:
              lowercase__   :       int				=	alibi
              self.model_tester.create_and_check_model(a						,       *a)





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





    def 				snake_case_    (				self):
         lowercase__				,			lowercase__   :       List[str]				=	self.model_tester.prepare_config_and_inputs_for_common()
         lowercase__   :       List[Any]				=	3
         lowercase__   :       str				=	'single_label_classification'
         lowercase__   :       Optional[int]				=	input_dict['input_ids']
         lowercase__   :       List[Any]				=	input_ids.ne(1).to(a)
         lowercase__   :       Dict				=	ids_tensor([self.model_tester.batch_size]						,       self.model_tester.type_sequence_label_size)
         lowercase__   :       List[str]				=	FalconForSequenceClassification(a)
         model.to(a)
         model.eval()
         lowercase__   :       int				=	model(a						,       attention_mask=a						,       labels=a)
         self.assertEqual(result.logits.shape						,       (self.model_tester.batch_size, self.model_tester.num_labels))





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





    def 				snake_case_    (				self):
         # Falcon can have different numbers of KV-heads than the number of query heads, so we need
         # to override this test to use the right head counts.
         for model_class in self.all_generative_model_classes:
              lowercase__				,			lowercase__   :       List[Any]				=	self.model_tester.prepare_config_and_inputs_for_common()

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

              lowercase__   :       Union[str, Any]				=	model_class(a).to(a)
              if "use_cache" not in inputs:
                   lowercase__   :       Tuple				=	True
              lowercase__   :       Optional[int]				=	model(**a)

              # 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

              lowercase__   :       int				=	(
                  getattr(a						,       'decoder_layers'						,       a)
                  or getattr(a						,       'num_decoder_layers'						,       a)
                  or config.num_hidden_layers
              )
              lowercase__   :       List[str]				=	getattr(a						,       'num_kv_heads'						,       config.num_attention_heads)
              lowercase__   :       str				=	getattr(a						,       'd_model'						,       config.hidden_size)
              lowercase__   :       int				=	embed_dim // num_attention_heads

              lowercase__   :       Any				=	outputs['past_key_values']
              self.assertEqual(len(a)						,       a)

              lowercase__				,			lowercase__   :       Dict				=	inputs['input_ids'].shape
              for i in range(a):
                   if config.new_decoder_architecture:
                        lowercase__   :       int				=	config.num_attention_heads
                   elif config.multi_query:
                        lowercase__   :       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 				SCREAMING_SNAKE_CASE__ (unittest.TestCase					):





    @slow
    def 				snake_case_    (				self):
         lowercase__   :       int				=	AutoTokenizer.from_pretrained('Rocketknight1/falcon-rw-1b')
         lowercase__   :       Union[str, Any]				=	FalconForCausalLM.from_pretrained('Rocketknight1/falcon-rw-1b')
         model.eval()
         model.to(a)
         lowercase__   :       Tuple				=	tokenizer('My favorite food is'						,       return_tensors='pt').to(a)

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

         lowercase__   :       Union[str, Any]				=	model.generate(**a						,       do_sample=a						,       max_new_tokens=19)
         lowercase__   :       List[Any]				=	tokenizer.batch_decode(a)[0]

         self.assertEqual(a						,       a)





    @slow
    def 				snake_case_    (				self):
         # The big models are way too big for the CI, so we use tiny random models that resemble their
         # architectures but with much smaller and fewer layers
         for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
              lowercase__   :       Optional[Any]				=	AutoTokenizer.from_pretrained(a)
              lowercase__   :       Optional[Any]				=	FalconForCausalLM.from_pretrained(a)
              model.eval()
              model.to(a)
              lowercase__   :       Union[str, Any]				=	tokenizer('My favorite food is'						,       return_tensors='pt').to(a)

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





    @slow
    def 				snake_case_    (				self):
         # The big models are way too big for the CI, so we use tiny random models that resemble their
         # architectures but with much smaller and fewer layers
         with torch.no_grad():
              for repo in [
                  "Rocketknight1/falcon-rw-1b",
                  "Rocketknight1/tiny-random-falcon-7b",
                  "Rocketknight1/tiny-random-falcon-40b",
              ]:
                   lowercase__   :       Dict				=	AutoTokenizer.from_pretrained(a)
                   lowercase__   :       Dict				=	FalconForCausalLM.from_pretrained(a)
                   model.eval()
                   model.to(device=a)
                   lowercase__   :       str				=	tokenizer('My favorite food is'						,       return_tensors='pt').to(a)

                   # Test results are the same with and without cache
                   lowercase__   :       Tuple				=	model.generate(**a						,       do_sample=a						,       max_new_tokens=20						,       use_cache=a)
                   lowercase__   :       List[str]				=	model.generate(**a						,       do_sample=a						,       max_new_tokens=20						,       use_cache=a)
                   self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0)




def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   float      ):



     '''simple docstring'''





     if edge <= 0 or not isinstance(SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_      ):
          raise ValueError('Length must be a positive.'      )
     return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2)

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   float      ):



     '''simple docstring'''





     if edge <= 0 or not isinstance(SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_      ):
          raise ValueError('Length must be a positive.'      )
     return ((15 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3)


if __name__ == "__main__":
       import doctest

       doctest.testmod()

