


"""simple docstring"""






import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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 (
      MODEL_FOR_PRETRAINING_MAPPING,
      MegatronBertForCausalLM,
      MegatronBertForMaskedLM,
      MegatronBertForMultipleChoice,
      MegatronBertForNextSentencePrediction,
      MegatronBertForPreTraining,
      MegatronBertForQuestionAnswering,
      MegatronBertForSequenceClassification,
      MegatronBertForTokenClassification,
      MegatronBertModel,
  )




class 							A_      :


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







     '''simple docstring'''







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


  def 							_lowercase					(			self:						Tuple    ):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size    )

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

     _lowerCamelCase   :				Any							=     None
     if self.use_token_type_ids:
        _lowerCamelCase   :				Union[str, Any]							=     ids_tensor([self.batch_size, self.seq_length]							,self.type_vocab_size    )

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

     _lowerCamelCase   :				Optional[Any]							=     self.get_config()

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


  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     return MegatronBertConfig(
         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							,embedding_size=self.embedding_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							,)


  def 							_lowercase					(			self:						Union[str, Any]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						int							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						Any    ):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     MegatronBertModel(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				str							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase    )
     _lowerCamelCase   :				int							=     model(__lowerCAmelCase							,token_type_ids=__lowerCAmelCase    )
     _lowerCamelCase   :				Dict							=     model(__lowerCAmelCase    )

     self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)    )
     self.parent.assertEqual(result.pooler_output.shape							,(self.batch_size, self.hidden_size)    )


  def 							_lowercase					(			self:						List[str]							,__lowerCAmelCase:						str							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     MegatronBertForMaskedLM(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				List[str]							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase							,labels=__lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)    )


  def 							_lowercase					(			self:						Tuple							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						str							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						str							,__lowerCAmelCase:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				int							=     MegatronBertForCausalLM(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				Dict							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase							,labels=__lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)    )


  def 							_lowercase					(			self:						Tuple							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     MegatronBertForNextSentencePrediction(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				List[Any]							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase							,labels=__lowerCAmelCase							,)
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, 2)    )


  def 							_lowercase					(			self:						Tuple							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						str							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     MegatronBertForPreTraining(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				int							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase							,labels=__lowerCAmelCase							,next_sentence_label=__lowerCAmelCase							,)
     self.parent.assertEqual(result.prediction_logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)    )
     self.parent.assertEqual(result.seq_relationship_logits.shape							,(self.batch_size, 2)    )


  def 							_lowercase					(			self:						int							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						int							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     MegatronBertForQuestionAnswering(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				Optional[int]							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase							,start_positions=__lowerCAmelCase							,end_positions=__lowerCAmelCase							,)
     self.parent.assertEqual(result.start_logits.shape							,(self.batch_size, self.seq_length)    )
     self.parent.assertEqual(result.end_logits.shape							,(self.batch_size, self.seq_length)    )


  def 							_lowercase					(			self:						Optional[Any]							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						str							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						Union[str, Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     self.num_labels
     _lowerCamelCase   :				List[str]							=     MegatronBertForSequenceClassification(__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				Optional[Any]							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase							,labels=__lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels)    )


  def 							_lowercase					(			self:						Optional[Any]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						int							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						int							,__lowerCAmelCase:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Tuple							=     self.num_labels
     _lowerCamelCase   :				Dict							=     MegatronBertForTokenClassification(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				Optional[int]							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase							,labels=__lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.num_labels)    )


  def 							_lowercase					(			self:						str							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						int							,__lowerCAmelCase:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     self.num_choices
     _lowerCamelCase   :				Optional[int]							=     MegatronBertForMultipleChoice(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				List[Any]							=     input_ids.unsqueeze(1    ).expand(-1							,self.num_choices							,-1    ).contiguous()
     _lowerCamelCase   :				Any							=     token_type_ids.unsqueeze(1    ).expand(-1							,self.num_choices							,-1    ).contiguous()
     _lowerCamelCase   :				Dict							=     input_mask.unsqueeze(1    ).expand(-1							,self.num_choices							,-1    ).contiguous()
     _lowerCamelCase   :				Dict							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,token_type_ids=__lowerCAmelCase							,labels=__lowerCAmelCase							,)
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_choices)    )






  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[str]							=     self.prepare_config_and_inputs()
     (
         (
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					
     )   :				Any							=     config_and_inputs
     _lowerCamelCase   :				int							=     {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict




@require_torch
class 							A_      (       _a     ,	_a     ,	unittest.TestCase ):
  lowerCAmelCase__      			=	(
      (
          MegatronBertModel,
          MegatronBertForMaskedLM,
          MegatronBertForCausalLM,
          MegatronBertForMultipleChoice,
          MegatronBertForNextSentencePrediction,
          MegatronBertForPreTraining,
          MegatronBertForQuestionAnswering,
          MegatronBertForSequenceClassification,
          MegatronBertForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  lowerCAmelCase__      			=	(
      {
          'feature-extraction': MegatronBertModel,
          'fill-mask': MegatronBertForMaskedLM,
          'question-answering': MegatronBertForQuestionAnswering,
          'text-classification': MegatronBertForSequenceClassification,
          'text-generation': MegatronBertForCausalLM,
          'token-classification': MegatronBertForTokenClassification,
          'zero-shot': MegatronBertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  lowerCAmelCase__      			=	True
  # test_resize_embeddings = False
  lowerCAmelCase__      			=	False


  def 							_lowercase					(			self:						List[Any]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Optional[Any]=False    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[str]							=     super()._prepare_for_class(__lowerCAmelCase							,__lowerCAmelCase							,return_labels=__lowerCAmelCase    )

     if return_labels:
        if model_class in get_values(__lowerCAmelCase    ):
           _lowerCamelCase   :				Dict							=     torch.zeros(
               (self.model_tester.batch_size, self.model_tester.seq_length)							,dtype=torch.long							,device=__lowerCAmelCase    )
           _lowerCamelCase   :				List[Any]							=     torch.zeros(
               self.model_tester.batch_size							,dtype=torch.long							,device=__lowerCAmelCase    )
     return inputs_dict


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     _lowerCamelCase   :				str							=     MegatronBertModelTester(self    )
     _lowerCamelCase   :				Dict							=     ConfigTester(self							,config_class=__lowerCAmelCase							,hidden_size=37    )


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     self.config_tester.run_common_tests()


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Dict							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_model(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						Union[str, Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Dict							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				str							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_pretraining(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Tuple							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_question_answering(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						int    ):







     '''simple docstring'''







     _lowerCamelCase   :				int							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__lowerCAmelCase    )






  def 							_lowercase					(			self:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[str]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_token_classification(*__lowerCAmelCase    )







def        lowerCamelCase_(		_lowerCamelCase       )    ->     int:
   '''simple docstring'''





   return torch.tensor(
       _lowerCamelCase			,		dtype=torch.long			,		device=_lowerCamelCase			,		)


_lowerCAmelCase							:    List[Any]					   =	1e-4




@require_torch
@require_sentencepiece
@require_tokenizers
class 							A_      (       unittest.TestCase ):


  @slow
  @unittest.skip("Model is not available."    )
  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     "nvidia/megatron-bert-uncased-345m"
     if "MYDIR" in os.environ:
        _lowerCamelCase   :				Optional[int]							=     os.path.join(os.environ["MYDIR"]							,__lowerCAmelCase    )
     _lowerCamelCase   :				Optional[int]							=     MegatronBertModel.from_pretrained(__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.half()
     _lowerCamelCase   :				Optional[int]							=     _long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]    )
     with torch.no_grad():
        _lowerCamelCase   :				Any							=     model(__lowerCAmelCase    )[0]
     _lowerCamelCase   :				List[str]							=     torch.Size((1, 9, 1_024)    )
     self.assertEqual(output.shape							,__lowerCAmelCase    )

     _lowerCamelCase   :				Any							=     [-0.60_40, -0.25_17, -0.10_25, 0.34_20, -0.67_58, -0.00_17, -0.10_89, -0.19_90, 0.57_28]
     for ii in range(3    ):
        for jj in range(3    ):
           _lowerCamelCase   :				str							=     output[0, ii, jj]
           _lowerCamelCase   :				Optional[int]							=     expected[3 * ii + jj]
           _lowerCamelCase   :				Union[str, Any]							=     "ii={} jj={} a={} b={}".format(__lowerCAmelCase							,__lowerCAmelCase							,__lowerCAmelCase							,__lowerCAmelCase    )
           self.assertTrue(math.isclose(__lowerCAmelCase							,__lowerCAmelCase							,rel_tol=__lowerCAmelCase							,abs_tol=__lowerCAmelCase    )							,msg=__lowerCAmelCase    )
"""simple docstring"""




from collections.abc import Generator







def 				_UpperCAmelCase		(							):





		"""simple docstring"""




		lowerCAmelCase__ ,       lowerCAmelCase__   = 0, 1
		while True:
				lowerCAmelCase__ ,       lowerCAmelCase__   = b, a + b
				yield b







def 				_UpperCAmelCase		(							lowerCamelCase__ = 1000  ):





		"""simple docstring"""




		lowerCAmelCase__   = 1
		lowerCAmelCase__   = fibonacci_generator()
		while len(str(next(lowerCamelCase__  )  )  ) < n:
				answer += 1
		return answer + 1


if __name__ == "__main__":
			print(solution(int(str(input()).strip())))




