





"""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 		__snake_case :





 """simple docstring"""


 def __init__(   self ,						__lowerCamelCase ,						__lowerCamelCase=3 ,						__lowerCamelCase=7 ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=False ,						__lowerCamelCase=True ,						__lowerCamelCase=99 ,						__lowerCamelCase=32 ,						__lowerCamelCase=5 ,						__lowerCamelCase=4 ,						__lowerCamelCase=37 ,						__lowerCamelCase="gelu" ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=512 ,						__lowerCamelCase=16 ,						__lowerCamelCase=2 ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=3 ,						__lowerCamelCase=4 ,						__lowerCamelCase=None ,						):



     '''simple docstring'''




     __A   :						Tuple									=					parent
     __A   :						List[Any]									=					batch_size
     __A   :						Dict									=					seq_length
     __A   :						int									=					is_training
     __A   :						Dict									=					use_input_mask
     __A   :						int									=					use_token_type_ids
     __A   :						Tuple									=					use_labels
     __A   :						Dict									=					vocab_size
     __A   :						Optional[int]									=					hidden_size
     __A   :						Optional[int]									=					num_hidden_layers
     __A   :						int									=					num_attention_heads
     __A   :						int									=					intermediate_size
     __A   :						Optional[int]									=					hidden_act
     __A   :						Optional[Any]									=					hidden_dropout_prob
     __A   :						Any									=					attention_probs_dropout_prob
     __A   :						Tuple									=					max_position_embeddings
     __A   :						str									=					type_vocab_size
     __A   :						List[str]									=					type_sequence_label_size
     __A   :						Optional[Any]									=					initializer_range
     __A   :						Union[str, Any]									=					num_labels
     __A   :						Tuple									=					num_choices
     __A   :						Union[str, Any]									=					scope

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					ids_tensor([self.batch_size, self.seq_length] ,						self.vocab_size  )

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

     __A   :						Optional[Any]									=					None

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

     __A   :						Tuple									=					self.get_config()

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

 def  UpperCamelCase__(   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  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						List[Any]									=					FalconModel(config=__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A   :						str									=					model(__lowerCamelCase ,						attention_mask=__lowerCamelCase  )
     __A   :						Dict									=					model(__lowerCamelCase  )
     self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						int									=					True
     __A   :						Tuple									=					FalconModel(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A   :						List[Any]									=					model(
         __lowerCamelCase ,						attention_mask=__lowerCamelCase ,						encoder_hidden_states=__lowerCamelCase ,						encoder_attention_mask=__lowerCamelCase ,						)
     __A   :						Union[str, Any]									=					model(
         __lowerCamelCase ,						attention_mask=__lowerCamelCase ,						encoder_hidden_states=__lowerCamelCase ,						)
     __A   :						Optional[Any]									=					model(__lowerCamelCase ,						attention_mask=__lowerCamelCase  )
     self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						List[str]									=					FalconForCausalLM(config=__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A   :						List[str]									=					model(__lowerCamelCase ,						attention_mask=__lowerCamelCase ,						labels=__lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.vocab_size)  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						List[Any]									=					True
     __A   :						List[str]									=					True
     __A   :						Dict									=					FalconForCausalLM(config=__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()

     # first forward pass
     __A   :						Tuple									=					model(
         __lowerCamelCase ,						attention_mask=__lowerCamelCase ,						encoder_hidden_states=__lowerCamelCase ,						encoder_attention_mask=__lowerCamelCase ,						use_cache=__lowerCamelCase ,						)
     __A   :						Optional[int]									=					outputs.past_key_values

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

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

     __A   :						int									=					model(
         __lowerCamelCase ,						attention_mask=__lowerCamelCase ,						encoder_hidden_states=__lowerCamelCase ,						encoder_attention_mask=__lowerCamelCase ,						output_hidden_states=__lowerCamelCase ,						)['''hidden_states'''][0]
     __A   :						str									=					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
     __A   :						int									=					ids_tensor((1,) ,						output_from_past.shape[-1]  ).item()
     __A   :						Optional[int]									=					output_from_no_past[:, -3:, random_slice_idx].detach()
     __A   :						Any									=					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  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[str]									=					self.prepare_config_and_inputs()
     (
         __A	
     )   :						int									=					config_and_inputs
     __A   :						int									=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
     return config, inputs_dict






@require_torch
class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							unittest.TestCase					):





 """simple docstring"""


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

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Any									=					FalconModelTester(self  )
     __A   :						Dict									=					ConfigTester(self ,						config_class=__lowerCamelCase ,						hidden_size=37  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     self.config_tester.run_common_tests()

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Tuple									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[Any]									=					self.model_tester.prepare_config_and_inputs()
     for alibi in [True, False]:
         __A   :						Union[str, Any]									=					alibi
         self.model_tester.create_and_check_model(__lowerCamelCase ,						*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




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

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					self.model_tester.prepare_config_and_inputs_for_common()
     __A   :						int									=					3
     __A   :						str									=					'''single_label_classification'''
     __A   :						Any									=					input_dict['''input_ids''']
     __A   :						Tuple									=					input_ids.ne(1  ).to(__lowerCamelCase  )
     __A   :						Tuple									=					ids_tensor([self.model_tester.batch_size] ,						self.model_tester.type_sequence_label_size  )
     __A   :						Dict									=					FalconForSequenceClassification(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A   :						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  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					self.model_tester.prepare_config_and_inputs_for_common()
     __A   :						List[str]									=					input_dict['''input_ids''']
     __A   :						int									=					FalconForCausalLM(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A   :						List[Any]									=					model(__lowerCamelCase ,						use_cache=__lowerCamelCase  )
     __A   :						List[str]									=					input_ids.shape[0]
     __A   :						Tuple									=					model._convert_to_rw_cache(result.past_key_values  )
     __A   :						Union[str, Any]									=					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  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Dict									=					self.model_tester.prepare_config_and_inputs_for_common()
     __A   :						Tuple									=					3
     __A   :						Dict									=					'''multi_label_classification'''
     __A   :						Tuple									=					input_dict['''input_ids''']
     __A   :						Any									=					input_ids.ne(1  ).to(__lowerCamelCase  )
     __A   :						Union[str, Any]									=					ids_tensor(
         [self.model_tester.batch_size, config.num_labels] ,						self.model_tester.type_sequence_label_size  ).to(torch.float  )
     __A   :						Optional[Any]									=					FalconForSequenceClassification(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A   :						int									=					model(__lowerCamelCase ,						attention_mask=__lowerCamelCase ,						labels=__lowerCamelCase  )
     self.assertEqual(result.logits.shape ,						(self.model_tester.batch_size, self.model_tester.num_labels)  )







 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     for model_class in self.all_generative_model_classes:
         __A   :						List[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

         __A   :						int									=					model_class(__lowerCamelCase  ).to(__lowerCamelCase  )
         if "use_cache" not in inputs:
             __A   :						str									=					True
         __A   :						Union[str, 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

         __A   :						Optional[Any]									=					(
             getattr(__lowerCamelCase ,						'''decoder_layers''' ,						__lowerCamelCase  )
             or getattr(__lowerCamelCase ,						'''num_decoder_layers''' ,						__lowerCamelCase  )
             or config.num_hidden_layers
         )
         __A   :						Any									=					getattr(__lowerCamelCase ,						'''num_kv_heads''' ,						config.num_attention_heads  )
         __A   :						Union[str, Any]									=					getattr(__lowerCamelCase ,						'''d_model''' ,						config.hidden_size  )
         __A   :						Dict									=					embed_dim // num_attention_heads

         __A   :						str									=					outputs['''past_key_values''']
         self.assertEqual(len(__lowerCamelCase  ) ,						__lowerCamelCase  )

         __A   :						Optional[int]									=					inputs['''input_ids'''].shape
         for i in range(__lowerCamelCase  ):
             if config.new_decoder_architecture:
                 __A   :						Tuple									=					config.num_attention_heads
             elif config.multi_query:
                 __A   :						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 		__snake_case (		unittest.TestCase					):





 """simple docstring"""


 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Dict									=					AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''  )
     __A   :						Union[str, Any]									=					FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''  )
     model.eval()
     model.to(__lowerCamelCase  )
     __A   :						Any									=					tokenizer('''My favorite food is''' ,						return_tensors='''pt'''  ).to(__lowerCamelCase  )

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

     __A   :						Dict									=					model.generate(**__lowerCamelCase ,						do_sample=__lowerCamelCase ,						max_new_tokens=19  )
     __A   :						Optional[int]									=					tokenizer.batch_decode(__lowerCamelCase  )[0]

     self.assertEqual(__lowerCamelCase ,						__lowerCamelCase  )

 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
         __A   :						str									=					AutoTokenizer.from_pretrained(__lowerCamelCase  )
         __A   :						List[Any]									=					FalconForCausalLM.from_pretrained(__lowerCamelCase  )
         model.eval()
         model.to(__lowerCamelCase  )
         __A   :						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  UpperCamelCase__(   self  ):



     '''simple docstring'''




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

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











"""simple docstring"""



from decimal import Decimal, getcontext
from math import ceil, factorial







def 		__lowercase					(       snake_case_  :  int						)       ->str:



    '''simple docstring'''







    if not isinstance(snake_case_    ,snake_case_						):
        raise TypeError('''Undefined for non-integers'''						)
    elif precision < 1:
        raise ValueError('''Undefined for non-natural numbers'''						)

    __A   :						int									=					precision
    __A   :						Tuple									=					ceil(precision / 14						)
    __A   :						Dict									=					426880 * Decimal(10005						).sqrt()
    __A   :						Optional[Any]									=					1
    __A   :						int									=					13591409
    __A   :						Optional[int]									=					Decimal(snake_case_						)
    for k in range(1    ,snake_case_						):
        __A   :						int									=					factorial(6 * k						) // (factorial(3 * k						) * factorial(snake_case_						) ** 3)
        linear_term += 545140134
        exponential_term *= -262537412640768000
        partial_sum += Decimal(multinomial_term * linear_term						) / exponential_term
    return str(constant_term / partial_sum						)[:-1]


if __name__ == "__main__":
       a_			=      50
       print(f'''The first {n} digits of pi is: {pi(n)}''')




