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					:




  def __init__(      self     :	Any     , a_     :	int     , a_     :	Union[str, Any]=3     , a_     :	int=7     , a_     :	List[Any]=True     , a_     :	Dict=True     , a_     :	Union[str, Any]=False     , a_     :	List[str]=True     , a_     :	int=99     , a_     :	Any=32     , a_     :	Tuple=5     , a_     :	List[Any]=4     , a_     :	str=37     , a_     :	int="gelu"     , a_     :	Any=0.1     , a_     :	Optional[Any]=0.1     , a_     :	int=512     , a_     :	str=16     , a_     :	List[str]=2     , a_     :	Dict=0.02     , a_     :	str=3     , a_     :	Optional[Any]=4     , a_     :	Optional[Any]=None     , )->       List[Any]:




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




  def 	__lowercase(      self     :	Union[str, Any]				)->       Any:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				List[str]			=		ids_tensor([self.batch_size, self.seq_length]     , self.vocab_size				)

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

       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		None

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

       SCREAMING_SNAKE_CASE__				:				Tuple			=		self.get_config()

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




  def 	__lowercase(      self     :	List[Any]				)->       Dict:




       """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 	__lowercase(      self     :	int     , a_     :	Tuple     , a_     :	List[Any]     , a_     :	str     , a_     :	str     , a_     :	Dict     , a_     :	List[Any]     , a_     :	Any				)->       str:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Tuple			=		FalconModel(config=a_				)
       model.to(a_				)
       model.eval()
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		model(a_     , attention_mask=a_				)
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		model(a_				)
       self.parent.assertEqual(result.last_hidden_state.shape     , (self.batch_size, self.seq_length, self.hidden_size)				)




  def 	__lowercase(      self     :	Tuple     , a_     :	Optional[Any]     , a_     :	Tuple     , a_     :	Any     , a_     :	Tuple     , a_     :	Optional[int]     , a_     :	Optional[int]     , a_     :	int     , a_     :	Union[str, Any]     , a_     :	Dict     , )->       int:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Tuple			=		True
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		FalconModel(a_				)
       model.to(a_				)
       model.eval()
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		model(
           a_     , attention_mask=a_     , encoder_hidden_states=a_     , encoder_attention_mask=a_     , )
       SCREAMING_SNAKE_CASE__				:				Any			=		model(
           a_     , attention_mask=a_     , encoder_hidden_states=a_     , )
       SCREAMING_SNAKE_CASE__				:				List[str]			=		model(a_     , attention_mask=a_				)
       self.parent.assertEqual(result.last_hidden_state.shape     , (self.batch_size, self.seq_length, self.hidden_size)				)




  def 	__lowercase(      self     :	Optional[Any]     , a_     :	List[Any]     , a_     :	Optional[int]     , a_     :	int     , a_     :	Dict     , a_     :	Optional[int]     , a_     :	Tuple     , a_     :	Optional[Any]     , a_     :	Tuple     , a_     :	List[str]     , )->       Optional[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Tuple			=		FalconForCausalLM(config=a_				)
       model.to(a_				)
       model.eval()
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		model(a_     , attention_mask=a_     , labels=a_				)
       self.parent.assertEqual(result.logits.shape     , (self.batch_size, self.seq_length, self.vocab_size)				)




  def 	__lowercase(      self     :	Any     , a_     :	Optional[Any]     , a_     :	List[str]     , a_     :	Union[str, Any]     , a_     :	Any     , a_     :	Dict     , a_     :	Any     , a_     :	Dict     , a_     :	List[str]     , a_     :	Optional[Any]     , )->       Any:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Dict			=		True
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		True
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		FalconForCausalLM(config=a_				)
       model.to(a_				)
       model.eval()

       # first forward pass
       SCREAMING_SNAKE_CASE__				:				Dict			=		model(
           a_     , attention_mask=a_     , encoder_hidden_states=a_     , encoder_attention_mask=a_     , use_cache=a_     , )
       SCREAMING_SNAKE_CASE__				:				Tuple			=		outputs.past_key_values

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

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

       SCREAMING_SNAKE_CASE__				:				List[Any]			=		model(
           a_     , attention_mask=a_     , encoder_hidden_states=a_     , encoder_attention_mask=a_     , output_hidden_states=a_     , )['hidden_states'][0]
       SCREAMING_SNAKE_CASE__				:				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
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		ids_tensor((1,)     , output_from_past.shape[-1]				).item()
       SCREAMING_SNAKE_CASE__				:				Dict			=		output_from_no_past[:, -3:, random_slice_idx].detach()
       SCREAMING_SNAKE_CASE__				:				Union[str, 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(a_     , a_     , atol=1e-3				)				)




  def 	__lowercase(      self     :	int				)->       int:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		self.prepare_config_and_inputs()
       (
           (
           SCREAMING_SNAKE_CASE__
       )			,							(
           SCREAMING_SNAKE_CASE__
       )			,							(
           SCREAMING_SNAKE_CASE__
       )			,							(
           SCREAMING_SNAKE_CASE__
       )			,							(
           SCREAMING_SNAKE_CASE__
       )			,							(
           SCREAMING_SNAKE_CASE__
       )			,							(
           SCREAMING_SNAKE_CASE__
       )			,							
       )				:				Dict			=		config_and_inputs
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		{'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict




@require_torch
class       snake_case					(      UpperCamelCase_   ,						UpperCamelCase_   ,						UpperCamelCase_   ,						unittest.TestCase						):
  lowercase_			=     (
      (
          FalconModel,
          FalconForCausalLM,
          FalconForSequenceClassification,
          FalconForTokenClassification,
          FalconForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  lowercase_			=     (FalconForCausalLM,) if is_torch_available() else ()
  lowercase_			=     (
      {
          'feature-extraction': FalconModel,
          'text-classification': FalconForSequenceClassification,
          'text-generation': FalconForCausalLM,
          'question-answering': FalconForQuestionAnswering,
          'token-classification': FalconForTokenClassification,
          'zero-shot': FalconForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  lowercase_			=     False
  lowercase_			=     False




  def 	__lowercase(      self     :	Tuple				)->       Any:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		FalconModelTester(self				)
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		ConfigTester(self     , config_class=a_     , hidden_size=37				)




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




       """simple docstring"""
       self.config_tester.run_common_tests()




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




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				int			=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a_				)




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




       """simple docstring"""
       SCREAMING_SNAKE_CASE__			,							*SCREAMING_SNAKE_CASE__				:				Optional[int]			=		self.model_tester.prepare_config_and_inputs()
       for alibi in [True, False]:
            SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		alibi
            self.model_tester.create_and_check_model(a_     , *a_				)




  def 	__lowercase(      self     :	int				)->       Union[str, Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__			,							SCREAMING_SNAKE_CASE__				:				str			=		self.model_tester.prepare_config_and_inputs_for_common()
       SCREAMING_SNAKE_CASE__				:				int			=		3
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		input_dict['input_ids']
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		input_ids.ne(1				).to(a_				)
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		ids_tensor([self.model_tester.batch_size]     , self.model_tester.type_sequence_label_size				)
       SCREAMING_SNAKE_CASE__				:				Any			=		FalconForSequenceClassification(a_				)
       model.to(a_				)
       model.eval()
       SCREAMING_SNAKE_CASE__				:				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 	__lowercase(      self     :	Union[str, Any]				)->       List[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__			,							SCREAMING_SNAKE_CASE__				:				int			=		self.model_tester.prepare_config_and_inputs_for_common()
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		3
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		'single_label_classification'
       SCREAMING_SNAKE_CASE__				:				Tuple			=		input_dict['input_ids']
       SCREAMING_SNAKE_CASE__				:				str			=		input_ids.ne(1				).to(a_				)
       SCREAMING_SNAKE_CASE__				:				str			=		ids_tensor([self.model_tester.batch_size]     , self.model_tester.type_sequence_label_size				)
       SCREAMING_SNAKE_CASE__				:				Any			=		FalconForSequenceClassification(a_				)
       model.to(a_				)
       model.eval()
       SCREAMING_SNAKE_CASE__				:				Any			=		model(a_     , attention_mask=a_     , labels=a_				)
       self.assertEqual(result.logits.shape     , (self.model_tester.batch_size, self.model_tester.num_labels)				)




  def 	__lowercase(      self     :	List[Any]				)->       Dict:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__			,							SCREAMING_SNAKE_CASE__				:				Dict			=		self.model_tester.prepare_config_and_inputs_for_common()
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		input_dict['input_ids']
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		FalconForCausalLM(a_				)
       model.to(a_				)
       model.eval()
       SCREAMING_SNAKE_CASE__				:				List[str]			=		model(a_     , use_cache=a_				)
       SCREAMING_SNAKE_CASE__				:				str			=		input_ids.shape[0]
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		model._convert_to_rw_cache(result.past_key_values				)
       SCREAMING_SNAKE_CASE__				:				Tuple			=		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 	__lowercase(      self     :	int				)->       List[str]:




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




  def 	__lowercase(      self     :	Tuple				)->       Optional[int]:




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

            SCREAMING_SNAKE_CASE__				:				List[Any]			=		model_class(a_				).to(a_				)
            if "use_cache" not in inputs:
                 SCREAMING_SNAKE_CASE__				:				str			=		True
            SCREAMING_SNAKE_CASE__				:				List[Any]			=		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

            SCREAMING_SNAKE_CASE__				:				Any			=		(
                getattr(a_     , 'decoder_layers'     , a_				)
                or getattr(a_     , 'num_decoder_layers'     , a_				)
                or config.num_hidden_layers
            )
            SCREAMING_SNAKE_CASE__				:				Dict			=		getattr(a_     , 'num_kv_heads'     , config.num_attention_heads				)
            SCREAMING_SNAKE_CASE__				:				List[Any]			=		getattr(a_     , 'd_model'     , config.hidden_size				)
            SCREAMING_SNAKE_CASE__				:				List[str]			=		embed_dim // num_attention_heads

            SCREAMING_SNAKE_CASE__				:				int			=		outputs['past_key_values']
            self.assertEqual(len(a_				)     , a_				)

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




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




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		AutoTokenizer.from_pretrained('Rocketknight1/falcon-rw-1b'				)
       SCREAMING_SNAKE_CASE__				:				Tuple			=		FalconForCausalLM.from_pretrained('Rocketknight1/falcon-rw-1b'				)
       model.eval()
       model.to(a_				)
       SCREAMING_SNAKE_CASE__				:				List[str]			=		tokenizer('My favorite food is'     , return_tensors='pt'				).to(a_				)

       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		(
           'My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'
       )

       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		model.generate(**a_     , do_sample=a_     , max_new_tokens=19				)
       SCREAMING_SNAKE_CASE__				:				str			=		tokenizer.batch_decode(a_				)[0]

       self.assertEqual(a_     , a_				)




  @slow
  def 	__lowercase(      self     :	Optional[int]				)->       int:




       """simple docstring"""
       # 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"]:
            SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		AutoTokenizer.from_pretrained(a_				)
            SCREAMING_SNAKE_CASE__				:				Dict			=		FalconForCausalLM.from_pretrained(a_				)
            model.eval()
            model.to(a_				)
            SCREAMING_SNAKE_CASE__				:				List[str]			=		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 	__lowercase(      self     :	List[Any]				)->       Dict:




       """simple docstring"""
       # 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",
            ]:
                 SCREAMING_SNAKE_CASE__				:				List[Any]			=		AutoTokenizer.from_pretrained(a_				)
                 SCREAMING_SNAKE_CASE__				:				List[str]			=		FalconForCausalLM.from_pretrained(a_				)
                 model.eval()
                 model.to(device=a_				)
                 SCREAMING_SNAKE_CASE__				:				Any			=		tokenizer('My favorite food is'     , return_tensors='pt'				).to(a_				)

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




def     _a				(      lowercase__  :			int = 1_00_00_00 ):







     '''simple docstring'''


     SCREAMING_SNAKE_CASE__				:				str			=		[i - 1 for i in range(limit + 1 )]

     for i in range(2     ,  limit + 1 ):
          if phi[i] == i - 1:
               for j in range(2 * i     ,  limit + 1     ,  lowercase__ ):
                    phi[j] -= phi[j] // i

     return sum(phi[2 : limit + 1] )


if __name__ == "__main__":
      print(solution())



