


import unittest

from transformers import FunnelConfig, FunnelTokenizer, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        MODEL_FOR_PRETRAINING_MAPPING,
        FunnelBaseModel,
        FunnelForMaskedLM,
        FunnelForMultipleChoice,
        FunnelForPreTraining,
        FunnelForQuestionAnswering,
        FunnelForSequenceClassification,
        FunnelForTokenClassification,
        FunnelModel,
    )



class   __lowerCamelCase :







       def __init__( self:   str,A_:   int,A_:   Optional[int]=13,A_:   Any=7,A_:   Dict=True,A_:   Dict=True,A_:   int=True,A_:   int=True,A_:   Any=99,A_:   Optional[int]=[1, 1, 2],A_:   Union[str, Any]=1,A_:   Dict=32,A_:   str=4,A_:   Any=8,A_:   Optional[Any]=37,A_:   Any="gelu_new",A_:   Any=0.1,A_:   Any=0.1,A_:   List[str]=0.0,A_:   Union[str, Any]=512,A_:   int=3,A_:   Dict=0.0_2,A_:   Union[str, Any]=3,A_:   Optional[Any]=4,A_:   Tuple=None,A_:   int=False,):






            '''simple docstring'''




            __UpperCamelCase   = parent
            __UpperCamelCase   = batch_size
            __UpperCamelCase   = seq_length
            __UpperCamelCase   = is_training
            __UpperCamelCase   = use_input_mask
            __UpperCamelCase   = use_token_type_ids
            __UpperCamelCase   = use_labels
            __UpperCamelCase   = vocab_size
            __UpperCamelCase   = block_sizes
            __UpperCamelCase   = num_decoder_layers
            __UpperCamelCase   = d_model
            __UpperCamelCase   = n_head
            __UpperCamelCase   = d_head
            __UpperCamelCase   = d_inner
            __UpperCamelCase   = hidden_act
            __UpperCamelCase   = hidden_dropout
            __UpperCamelCase   = attention_dropout
            __UpperCamelCase   = activation_dropout
            __UpperCamelCase   = max_position_embeddings
            __UpperCamelCase   = type_vocab_size
            __UpperCamelCase   = 2
            __UpperCamelCase   = num_labels
            __UpperCamelCase   = num_choices
            __UpperCamelCase   = scope
            __UpperCamelCase   = initializer_std

            # Used in the tests to check the size of the first attention layer
            __UpperCamelCase   = n_head
            # Used in the tests to check the size of the first hidden state
            __UpperCamelCase   = self.d_model
            # Used in the tests to check the number of output hidden states/attentions
            __UpperCamelCase   = sum(self.block_sizes				) + (0 if base else self.num_decoder_layers)
            # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
            # the last hidden state of the first block (which is the first hidden state of the decoder).
            if not base:
                 __UpperCamelCase   = self.num_hidden_layers + 2







       def 		snake_case_  ( self:   Optional[Any]				):






            '''simple docstring'''




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

            __UpperCamelCase   = None
            if self.use_input_mask:
                 __UpperCamelCase   = ids_tensor([self.batch_size, self.seq_length],vocab_size=2				)

            __UpperCamelCase   = None
            if self.use_token_type_ids:
                 __UpperCamelCase   = ids_tensor([self.batch_size, self.seq_length],self.type_vocab_size				)

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

            __UpperCamelCase   = self.get_config()

            return (
                config,
                input_ids,
                token_type_ids,
                input_mask,
                sequence_labels,
                token_labels,
                choice_labels,
                fake_token_labels,
            )







       def 		snake_case_  ( self:   Any				):






            '''simple docstring'''




            return FunnelConfig(
                vocab_size=self.vocab_size,block_sizes=self.block_sizes,num_decoder_layers=self.num_decoder_layers,d_model=self.d_model,n_head=self.n_head,d_head=self.d_head,d_inner=self.d_inner,hidden_act=self.hidden_act,hidden_dropout=self.hidden_dropout,attention_dropout=self.attention_dropout,activation_dropout=self.activation_dropout,max_position_embeddings=self.max_position_embeddings,type_vocab_size=self.type_vocab_size,initializer_std=self.initializer_std,)







       def 		snake_case_  ( self:   str,A_:   str,A_:   Tuple,A_:   Union[str, Any],A_:   Optional[Any],A_:   int,A_:   List[str],A_:   str,A_:   List[str],):






            '''simple docstring'''




            __UpperCamelCase   = FunnelModel(config=A_				)
            model.to(A_				)
            model.eval()
            __UpperCamelCase   = model(A_,attention_mask=A_,token_type_ids=A_				)
            __UpperCamelCase   = model(A_,token_type_ids=A_				)
            __UpperCamelCase   = model(A_				)
            self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.d_model)				)

            __UpperCamelCase   = False
            __UpperCamelCase   = model(A_				)
            self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.d_model)				)

            __UpperCamelCase   = False
            __UpperCamelCase   = model(A_				)
            self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.d_model)				)







       def 		snake_case_  ( self:   Dict,A_:   Optional[Any],A_:   Dict,A_:   Tuple,A_:   List[Any],A_:   Optional[Any],A_:   List[str],A_:   Optional[int],A_:   List[Any],):






            '''simple docstring'''




            __UpperCamelCase   = FunnelBaseModel(config=A_				)
            model.to(A_				)
            model.eval()
            __UpperCamelCase   = model(A_,attention_mask=A_,token_type_ids=A_				)
            __UpperCamelCase   = model(A_,token_type_ids=A_				)
            __UpperCamelCase   = model(A_				)
            self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, 2, self.d_model)				)

            __UpperCamelCase   = False
            __UpperCamelCase   = model(A_				)
            self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, 3, self.d_model)				)

            __UpperCamelCase   = False
            __UpperCamelCase   = model(A_				)
            self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, 2, self.d_model)				)







       def 		snake_case_  ( self:   List[str],A_:   List[Any],A_:   int,A_:   Union[str, Any],A_:   Optional[int],A_:   Dict,A_:   List[str],A_:   List[str],A_:   str,):






            '''simple docstring'''




            __UpperCamelCase   = self.num_labels
            __UpperCamelCase   = FunnelForPreTraining(config=A_				)
            model.to(A_				)
            model.eval()
            __UpperCamelCase   = model(A_,attention_mask=A_,token_type_ids=A_,labels=A_				)
            self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length)				)







       def 		snake_case_  ( self:   List[Any],A_:   List[Any],A_:   Tuple,A_:   Optional[Any],A_:   Tuple,A_:   str,A_:   List[Any],A_:   Dict,A_:   Optional[int],):






            '''simple docstring'''




            __UpperCamelCase   = FunnelForMaskedLM(config=A_				)
            model.to(A_				)
            model.eval()
            __UpperCamelCase   = model(A_,attention_mask=A_,token_type_ids=A_,labels=A_				)
            self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.vocab_size)				)







       def 		snake_case_  ( self:   Tuple,A_:   Tuple,A_:   Any,A_:   str,A_:   Optional[Any],A_:   Optional[Any],A_:   Dict,A_:   Union[str, Any],A_:   Tuple,):






            '''simple docstring'''




            __UpperCamelCase   = self.num_labels
            __UpperCamelCase   = FunnelForSequenceClassification(A_				)
            model.to(A_				)
            model.eval()
            __UpperCamelCase   = model(A_,attention_mask=A_,token_type_ids=A_,labels=A_				)
            self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)				)







       def 		snake_case_  ( self:   Tuple,A_:   int,A_:   int,A_:   Dict,A_:   Dict,A_:   Union[str, Any],A_:   Optional[Any],A_:   List[Any],A_:   Union[str, Any],):






            '''simple docstring'''




            __UpperCamelCase   = self.num_choices
            __UpperCamelCase   = FunnelForMultipleChoice(config=A_				)
            model.to(A_				)
            model.eval()
            __UpperCamelCase   = input_ids.unsqueeze(1				).expand(-1,self.num_choices,-1				).contiguous()
            __UpperCamelCase   = token_type_ids.unsqueeze(1				).expand(-1,self.num_choices,-1				).contiguous()
            __UpperCamelCase   = input_mask.unsqueeze(1				).expand(-1,self.num_choices,-1				).contiguous()
            __UpperCamelCase   = model(
                A_,attention_mask=A_,token_type_ids=A_,labels=A_,)
            self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_choices)				)







       def 		snake_case_  ( self:   Union[str, Any],A_:   List[Any],A_:   Tuple,A_:   List[Any],A_:   List[str],A_:   Union[str, Any],A_:   Optional[int],A_:   List[str],A_:   List[str],):






            '''simple docstring'''




            __UpperCamelCase   = self.num_labels
            __UpperCamelCase   = FunnelForTokenClassification(config=A_				)
            model.to(A_				)
            model.eval()
            __UpperCamelCase   = model(A_,attention_mask=A_,token_type_ids=A_,labels=A_				)
            self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.num_labels)				)







       def 		snake_case_  ( self:   Union[str, Any],A_:   Tuple,A_:   Tuple,A_:   int,A_:   List[str],A_:   Optional[int],A_:   str,A_:   List[str],A_:   List[str],):






            '''simple docstring'''




            __UpperCamelCase   = FunnelForQuestionAnswering(config=A_				)
            model.to(A_				)
            model.eval()
            __UpperCamelCase   = model(
                A_,attention_mask=A_,token_type_ids=A_,start_positions=A_,end_positions=A_,)
            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 		snake_case_  ( self:   int				):






            '''simple docstring'''




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



@require_torch
class   __lowerCamelCase (_a							,			_a							,			unittest.TestCase	):
       _lowercase       							=    False
       _lowercase       							=    False
       _lowercase       							=    (
           (
               FunnelModel,
               FunnelForMaskedLM,
               FunnelForPreTraining,
               FunnelForQuestionAnswering,
               FunnelForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       _lowercase       							=    (
           {
               """feature-extraction""": (FunnelBaseModel, FunnelModel),
               """fill-mask""": FunnelForMaskedLM,
               """question-answering""": FunnelForQuestionAnswering,
               """text-classification""": FunnelForSequenceClassification,
               """token-classification""": FunnelForTokenClassification,
               """zero-shot""": FunnelForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )







       def 		snake_case_  ( self:   List[Any],A_:   List[str],A_:   Optional[int],A_:   List[Any]=False				):






            '''simple docstring'''




            __UpperCamelCase   = super()._prepare_for_class(A_,A_,return_labels=A_				)

            if return_labels:
                 if model_class in get_values(A_				):
                      __UpperCamelCase   = torch.zeros(
                          (self.model_tester.batch_size, self.model_tester.seq_length),dtype=torch.long,device=A_				)
            return inputs_dict







       def 		snake_case_  ( self:   int				):






            '''simple docstring'''




            __UpperCamelCase   = FunnelModelTester(self				)
            __UpperCamelCase   = ConfigTester(self,config_class=A_				)







       def 		snake_case_  ( self:   str				):






            '''simple docstring'''




            self.config_tester.run_common_tests()







       def 		snake_case_  ( self:   Dict				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*A_				)







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






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_pretraining(*A_				)







       def 		snake_case_  ( self:   List[str]				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*A_				)







       def 		snake_case_  ( self:   Dict				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*A_				)







       def 		snake_case_  ( self:   int				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*A_				)







       def 		snake_case_  ( self:   str,A_:   Optional[int]				):






            '''simple docstring'''




            if hasattr(A_,'weight'				) and module.weight is not None:
                 module.weight.data.fill_(3				)
            if hasattr(A_,'bias'				) and module.bias is not None:
                 module.bias.data.fill_(3				)

            for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
                 if hasattr(A_,A_				) and getattr(A_,A_				) is not None:
                      __UpperCamelCase   = getattr(A_,A_				)
                      weight.data.fill_(3				)



@require_torch
class   __lowerCamelCase (_a							,			unittest.TestCase	):
       _lowercase       							=    False
       _lowercase       							=    False
       _lowercase       							=    (
           (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
       )







       def 		snake_case_  ( self:   Tuple				):






            '''simple docstring'''




            __UpperCamelCase   = FunnelModelTester(self,base=A_				)
            __UpperCamelCase   = ConfigTester(self,config_class=A_				)







       def 		snake_case_  ( self:   Optional[Any]				):






            '''simple docstring'''




            self.config_tester.run_common_tests()







       def 		snake_case_  ( self:   List[str]				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_base_model(*A_				)







       def 		snake_case_  ( self:   List[str]				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*A_				)







       def 		snake_case_  ( self:   Tuple				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*A_				)







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






            '''simple docstring'''




            __UpperCamelCase,						__UpperCamelCase   = self.model_tester.prepare_config_and_inputs_for_common()
            __UpperCamelCase   = True

            for model_class in self.all_model_classes:
                 if model_class.__name__ == "FunnelBaseModel":
                      continue
                 __UpperCamelCase   = model_class(A_				)
                 model.to(A_				)
                 model.train()
                 __UpperCamelCase   = self._prepare_for_class(A_,A_,return_labels=A_				)
                 __UpperCamelCase   = model(**A_				).loss
                 loss.backward()







       def 		snake_case_  ( self:   Optional[int],A_:   str				):






            '''simple docstring'''




            if hasattr(A_,'weight'				) and module.weight is not None:
                 module.weight.data.fill_(3				)
            if hasattr(A_,'bias'				) and module.bias is not None:
                 module.bias.data.fill_(3				)

            for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
                 if hasattr(A_,A_				) and getattr(A_,A_				) is not None:
                      __UpperCamelCase   = getattr(A_,A_				)
                      weight.data.fill_(3				)



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







       def 		snake_case_  ( self:   Any				):






            '''simple docstring'''




            __UpperCamelCase   = 13
            __UpperCamelCase   = 7
            __UpperCamelCase   = torch.arange(0,batch_size * sequence_length				).long().reshape(A_,A_				)
            __UpperCamelCase   = [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
            __UpperCamelCase   = torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]				)

            __UpperCamelCase   = FunnelModel.from_pretrained('sgugger/funnel-random-tiny'				)
            __UpperCamelCase   = model(A_,token_type_ids=A_				)[0].abs()

            __UpperCamelCase   = torch.tensor(2_3_4_4.8_3_5_2				)
            __UpperCamelCase   = torch.tensor(0.8_0_5_2				)
            self.assertTrue(torch.allclose(output.sum(),A_,atol=1E-4				)				)
            self.assertTrue(torch.allclose(output.mean(),A_,atol=1E-4				)				)

            __UpperCamelCase   = torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]				)
            __UpperCamelCase   = model(A_,attention_mask=A_,token_type_ids=A_				)[0].abs()

            __UpperCamelCase   = torch.tensor(2_3_4_3.8_4_2_5				)
            __UpperCamelCase   = torch.tensor(0.8_0_4_9				)
            self.assertTrue(torch.allclose(output.sum(),A_,atol=1E-4				)				)
            self.assertTrue(torch.allclose(output.mean(),A_,atol=1E-4				)				)







       @slow
       def 		snake_case_  ( self:   Union[str, Any]				):






            '''simple docstring'''




            __UpperCamelCase   = FunnelTokenizer.from_pretrained('huggingface/funnel-small'				)
            __UpperCamelCase   = FunnelModel.from_pretrained('huggingface/funnel-small'				)
            __UpperCamelCase   = tokenizer('Hello! I am the Funnel Transformer model.',return_tensors='pt'				)
            __UpperCamelCase   = model(**A_				)[0]

            __UpperCamelCase   = torch.tensor(2_3_5.7_2_4_6				)
            __UpperCamelCase   = torch.tensor(0.0_2_5_6				)
            self.assertTrue(torch.allclose(output.sum(),A_,atol=1E-4				)				)
            self.assertTrue(torch.allclose(output.mean(),A_,atol=1E-4				)				)






import argparse
import json

from transformers import GPTNeoConfig, GPTNeoForCausalLM, load_tf_weights_in_gpt_neo
from transformers.utils import logging


logging.set_verbosity_info()






def 		_A		(		_lowercase				,     _lowercase				,     _lowercase   )				->   List[str]:


     """simple docstring"""


     __UpperCamelCase   = json.load(open(_lowercase				,     'r'   )   )
     __UpperCamelCase   = GPTNeoConfig(
         hidden_size=config_json['n_embd']				,     num_layers=config_json['n_layer']				,     num_heads=config_json['n_head']				,     attention_types=config_json['attention_types']				,     max_position_embeddings=config_json['n_positions']				,     resid_dropout=config_json['res_dropout']				,     embed_dropout=config_json['embed_dropout']				,     attention_dropout=config_json['attn_dropout']				,     )
     print(f'''Building PyTorch model from configuration: {config}'''   )
     __UpperCamelCase   = GPTNeoForCausalLM(_lowercase   )

     # Load weights from tf checkpoint
     load_tf_weights_in_gpt_neo(_lowercase				,     _lowercase				,     _lowercase   )

     # Save pytorch-model
     print(f'''Save PyTorch model to {pytorch_dump_path}'''   )
     model.save_pretrained(_lowercase   )


if __name__ == "__main__":
    __snake_case             =	argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
    )
    parser.add_argument(
        '''--config_file''',
        default=None,
        type=str,
        required=True,
        help=(
            '''The config json file corresponding to the pre-trained mesh-tf model. \n'''
            '''This specifies the model architecture.'''
        ),
    )
    parser.add_argument(
        '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
    )
    __snake_case             =	parser.parse_args()
    convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)


