



'''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				,     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				,     )					->  str:


      """simple docstring"""



      _a											=						parent
      _a											=						batch_size
      _a											=						seq_length
      _a											=						is_training
      _a											=						use_input_mask
      _a											=						use_token_type_ids
      _a											=						use_labels
      _a											=						vocab_size
      _a											=						hidden_size
      _a											=						num_hidden_layers
      _a											=						num_attention_heads
      _a											=						intermediate_size
      _a											=						hidden_act
      _a											=						hidden_dropout_prob
      _a											=						attention_probs_dropout_prob
      _a											=						max_position_embeddings
      _a											=						type_vocab_size
      _a											=						type_sequence_label_size
      _a											=						initializer_range
      _a											=						num_labels
      _a											=						num_choices
      _a											=						scope



 def       a__						(self )					->  List[Any]:


      """simple docstring"""



      _a											=						ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size )

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

      _a											=						None

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

      _a											=						self.get_config()

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



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


      """simple docstring"""



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



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  int:


      """simple docstring"""



      _a											=						FalconModel(config=A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A )
      _a											=						model(A )
      self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size) )



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     )					->  Tuple:


      """simple docstring"""



      _a											=						True
      _a											=						FalconModel(A )
      model.to(A )
      model.eval()
      _a											=						model(
          A				,     attention_mask=A				,     encoder_hidden_states=A				,     encoder_attention_mask=A				,     )
      _a											=						model(
          A				,     attention_mask=A				,     encoder_hidden_states=A				,     )
      _a											=						model(A				,     attention_mask=A )
      self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size) )



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     )					->  str:


      """simple docstring"""



      _a											=						FalconForCausalLM(config=A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A				,     labels=A )
      self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size) )



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     A				,     )					->  int:


      """simple docstring"""



      _a											=						True
      _a											=						True
      _a											=						FalconForCausalLM(config=A )
      model.to(A )
      model.eval()

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

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

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

      _a											=						model(
          A				,     attention_mask=A				,     encoder_hidden_states=A				,     encoder_attention_mask=A				,     output_hidden_states=A				,     )['''hidden_states'''][0]
      _a											=						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
      _a											=						ids_tensor((1,)				,     output_from_past.shape[-1] ).item()
      _a											=						output_from_no_past[:, -3:, random_slice_idx].detach()
      _a											=						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       a__						(self )					->  List[Any]:


      """simple docstring"""



      _a											=						self.prepare_config_and_inputs()
      (
          (
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					
      )											=						config_and_inputs
      _a											=						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict






@require_torch
class __A    (						A     ,     A     ,     A     ,     unittest.TestCase		):



 '''simple docstring'''







 __lowerCamelCase					: List[str]    = (
     (
         FalconModel,
         FalconForCausalLM,
         FalconForSequenceClassification,
         FalconForTokenClassification,
         FalconForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 __lowerCamelCase					: Tuple    = (FalconForCausalLM,) if is_torch_available() else ()
 __lowerCamelCase					: int    = (
     {
         'feature-extraction': FalconModel,
         'text-classification': FalconForSequenceClassification,
         'text-generation': FalconForCausalLM,
         'question-answering': FalconForQuestionAnswering,
         'token-classification': FalconForTokenClassification,
         'zero-shot': FalconForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __lowerCamelCase					: Union[str, Any]    = False
 __lowerCamelCase					: Union[str, Any]    = False



 def       a__						(self )					->  str:


      """simple docstring"""



      _a											=						FalconModelTester(self )
      _a											=						ConfigTester(self				,     config_class=A				,     hidden_size=37 )



 def       a__						(self )					->  str:


      """simple docstring"""



      self.config_tester.run_common_tests()



 def       a__						(self )					->  Any:


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A )



 def       a__						(self )					->  Tuple:


      """simple docstring"""



      _a						,					*_a											=						self.model_tester.prepare_config_and_inputs()
      for alibi in [True, False]:
           _a											=						alibi
           self.model_tester.create_and_check_model(A				,     *A )



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


      """simple docstring"""



      _a						,					_a											=						self.model_tester.prepare_config_and_inputs_for_common()
      _a											=						3
      _a											=						input_dict['''input_ids''']
      _a											=						input_ids.ne(1 ).to(A )
      _a											=						ids_tensor([self.model_tester.batch_size]				,     self.model_tester.type_sequence_label_size )
      _a											=						FalconForSequenceClassification(A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A				,     labels=A )
      self.assertEqual(result.logits.shape				,     (self.model_tester.batch_size, self.model_tester.num_labels) )



 def       a__						(self )					->  List[Any]:


      """simple docstring"""



      _a						,					_a											=						self.model_tester.prepare_config_and_inputs_for_common()
      _a											=						3
      _a											=						'''single_label_classification'''
      _a											=						input_dict['''input_ids''']
      _a											=						input_ids.ne(1 ).to(A )
      _a											=						ids_tensor([self.model_tester.batch_size]				,     self.model_tester.type_sequence_label_size )
      _a											=						FalconForSequenceClassification(A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A				,     labels=A )
      self.assertEqual(result.logits.shape				,     (self.model_tester.batch_size, self.model_tester.num_labels) )



 def       a__						(self )					->  Any:


      """simple docstring"""



      _a						,					_a											=						self.model_tester.prepare_config_and_inputs_for_common()
      _a											=						input_dict['''input_ids''']
      _a											=						FalconForCausalLM(A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     use_cache=A )
      _a											=						input_ids.shape[0]
      _a											=						model._convert_to_rw_cache(result.past_key_values )
      _a											=						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       a__						(self )					->  List[Any]:


      """simple docstring"""



      _a						,					_a											=						self.model_tester.prepare_config_and_inputs_for_common()
      _a											=						3
      _a											=						'''multi_label_classification'''
      _a											=						input_dict['''input_ids''']
      _a											=						input_ids.ne(1 ).to(A )
      _a											=						ids_tensor(
          [self.model_tester.batch_size, config.num_labels]				,     self.model_tester.type_sequence_label_size ).to(torch.float )
      _a											=						FalconForSequenceClassification(A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A				,     labels=A )
      self.assertEqual(result.logits.shape				,     (self.model_tester.batch_size, self.model_tester.num_labels) )



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


      """simple docstring"""



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

           _a											=						model_class(A ).to(A )
           if "use_cache" not in inputs:
                _a											=						True
           _a											=						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

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

           _a											=						outputs['''past_key_values''']
           self.assertEqual(len(A )				,     A )

           _a						,					_a											=						inputs['''input_ids'''].shape
           for i in range(A ):
                if config.new_decoder_architecture:
                     _a											=						config.num_attention_heads
                elif config.multi_query:
                     _a											=						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       a__						(self )					->  Any:


      """simple docstring"""



      _a											=						AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b''' )
      _a											=						FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b''' )
      model.eval()
      model.to(A )
      _a											=						tokenizer('''My favorite food is'''				,     return_tensors='''pt''' ).to(A )

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

      _a											=						model.generate(**A				,     do_sample=A				,     max_new_tokens=19 )
      _a											=						tokenizer.batch_decode(A )[0]

      self.assertEqual(A				,     A )



 @slow
 def       a__						(self )					->  int:


      """simple docstring"""



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


      """simple docstring"""



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

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








'''simple docstring'''





def lowerCAmelCase						(__A):



     """simple docstring"""



     _a											=						set()
     # To detect a back edge, keep track of vertices currently in the recursion stack
     _a											=						set()
     return any(
         node not in visited and depth_first_search(__A						,			__A						,			__A						,			__A)
         for node in graph)




def lowerCAmelCase						(__A						,			__A						,			__A						,			__A):



     """simple docstring"""



     visited.add(__A)
     rec_stk.add(__A)

     for node in graph[vertex]:
          if node not in visited:
               if depth_first_search(__A						,			__A						,			__A						,			__A):
                    return True
          elif node in rec_stk:
               return True

    # The node needs to be removed from recursion stack before function ends
     rec_stk.remove(__A)
     return False


if __name__ == "__main__":
  from doctest import testmod

  testmod()



