





from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers.models.openai.modeling_tf_openai import (
     TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
     TFOpenAIGPTDoubleHeadsModel,
     TFOpenAIGPTForSequenceClassification,
     TFOpenAIGPTLMHeadModel,
     TFOpenAIGPTModel,
 )


class 					__magic_name__  :





     """simple docstring"""



     def __init__(							self       :Tuple			,      snake_case       :Tuple			,      ):





        '''simple docstring'''




        A_					:				Any          =			parent
        A_					:				Dict          =			13
        A_					:				Union[str, Any]          =			7
        A_					:				str          =			True
        A_					:				int          =			True
        A_					:				Tuple          =			True
        A_					:				Optional[Any]          =			True
        A_					:				Any          =			True
        A_					:				Any          =			99
        A_					:				Tuple          =			32
        A_					:				List[Any]          =			2
        A_					:				int          =			4
        A_					:				Dict          =			37
        A_					:				Optional[Any]          =			'''gelu'''
        A_					:				str          =			0.1
        A_					:				List[str]          =			0.1
        A_					:				Optional[Any]          =			512
        A_					:				Tuple          =			16
        A_					:				List[str]          =			2
        A_					:				Dict          =			0.02
        A_					:				List[str]          =			3
        A_					:				Optional[Any]          =			4
        A_					:				int          =			None
        A_					:				List[Any]          =			self.vocab_size - 1



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





        '''simple docstring'''




        A_					:				Tuple          =			ids_tensor([self.batch_size, self.seq_length]			,      self.vocab_size  )

        A_					:				Tuple          =			None
        if self.use_input_mask:
           A_					:				Any          =			random_attention_mask([self.batch_size, self.seq_length]  )

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

        A_					:				Any          =			None
        if self.use_mc_token_ids:
           A_					:				List[Any]          =			ids_tensor([self.batch_size, self.num_choices]			,      self.seq_length  )

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

        A_					:				int          =			OpenAIGPTConfig(
            vocab_size=self.vocab_size			,      n_embd=self.hidden_size			,      n_layer=self.num_hidden_layers			,      n_head=self.num_attention_heads			,      n_positions=self.max_position_embeddings			,      pad_token_id=self.pad_token_id			,      )

        A_					:				Union[str, Any]          =			ids_tensor([self.num_hidden_layers, self.num_attention_heads]			,      2  )

        return (
            config,
            input_ids,
            input_mask,
            head_mask,
            token_type_ids,
            mc_token_ids,
            sequence_labels,
            token_labels,
            choice_labels,
        )



     def 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]			,      snake_case       :int			,      snake_case       :str			,      snake_case       :Optional[int]			,      snake_case       :Union[str, Any]			,      snake_case       :int			,      *snake_case       :List[Any]  ):





        '''simple docstring'''




        A_					:				List[Any]          =			TFOpenAIGPTModel(config=_snake_case  )
        A_					:				Union[str, Any]          =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        A_					:				List[str]          =			model(_snake_case  )

        A_					:				List[str]          =			[input_ids, input_mask]
        A_					:				Dict          =			model(_snake_case  )

        A_					:				Optional[Any]          =			model(_snake_case  )

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



     def 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]			,      snake_case       :Any			,      snake_case       :Optional[int]			,      snake_case       :str			,      snake_case       :Tuple			,      snake_case       :Tuple			,      *snake_case       :Any  ):





        '''simple docstring'''




        A_					:				Optional[int]          =			TFOpenAIGPTLMHeadModel(config=_snake_case  )
        A_					:				Tuple          =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        A_					:				Union[str, Any]          =			model(_snake_case  )
        self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.seq_length, self.vocab_size)  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]			,      snake_case       :Tuple			,      snake_case       :str			,      snake_case       :Dict			,      snake_case       :Any			,      snake_case       :Dict			,      snake_case       :Any			,      *snake_case       :Tuple  ):





        '''simple docstring'''




        A_					:				List[Any]          =			TFOpenAIGPTDoubleHeadsModel(config=_snake_case  )

        A_					:				List[Any]          =			tf.tile(tf.expand_dims(_snake_case			,      1  )			,      (1, self.num_choices, 1)  )
        A_					:				str          =			tf.tile(tf.expand_dims(_snake_case			,      1  )			,      (1, self.num_choices, 1)  )
        A_					:				int          =			tf.tile(tf.expand_dims(_snake_case			,      1  )			,      (1, self.num_choices, 1)  )

        A_					:				List[str]          =			{
            '''input_ids''': multiple_choice_inputs_ids,
            '''mc_token_ids''': mc_token_ids,
            '''attention_mask''': multiple_choice_input_mask,
            '''token_type_ids''': multiple_choice_token_type_ids,
        }
        A_					:				Tuple          =			model(_snake_case  )
        self.parent.assertEqual(
            result.logits.shape			,      (self.batch_size, self.num_choices, self.seq_length, self.vocab_size)  )
        self.parent.assertEqual(result.mc_logits.shape			,      (self.batch_size, self.num_choices)  )



     def 						SCREAMING_SNAKE_CASE		(							self       :str			,      snake_case       :int			,      snake_case       :str			,      snake_case       :str			,      snake_case       :Any			,      snake_case       :List[Any]			,      *snake_case       :Tuple  ):





        '''simple docstring'''




        A_					:				List[str]          =			self.num_labels
        A_					:				Dict          =			ids_tensor([self.batch_size]			,      self.type_sequence_label_size  )
        A_					:				str          =			{
            '''input_ids''': input_ids,
            '''attention_mask''': input_mask,
            '''token_type_ids''': token_type_ids,
            '''labels''': sequence_labels,
        }
        A_					:				Dict          =			TFOpenAIGPTForSequenceClassification(_snake_case  )
        A_					:				str          =			model(_snake_case  )
        self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.num_labels)  )





     def 						SCREAMING_SNAKE_CASE		(							self       :int  ):





        '''simple docstring'''




        A_					:				Tuple          =			self.prepare_config_and_inputs()

        (
            A_							
        )					:				List[str]          =			config_and_inputs

        A_					:				int          =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict




@require_tf
class 					__magic_name__  (							a_    ,				a_    ,				unittest.TestCase							):





     """simple docstring"""



     __UpperCamelCase					    =     (
         (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
         if is_tf_available()
         else ()
     )
     __UpperCamelCase					    =     (
         (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     __UpperCamelCase					    =     (
         {
             'feature-extraction': TFOpenAIGPTModel,
             'text-classification': TFOpenAIGPTForSequenceClassification,
             'text-generation': TFOpenAIGPTLMHeadModel,
             'zero-shot': TFOpenAIGPTForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     __UpperCamelCase					    =     False
     __UpperCamelCase					    =     False



     def 						SCREAMING_SNAKE_CASE		(							self       :Any			,      snake_case       :List[str]			,      snake_case       :str			,      snake_case       :Tuple			,      snake_case       :Optional[int]			,      snake_case       :Dict  ):





        '''simple docstring'''




        if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
           # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
           # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
           # tiny config could not be created.
           return True

        return False



     def 						SCREAMING_SNAKE_CASE		(							self       :Any  ):





        '''simple docstring'''




        A_					:				Dict          =			TFOpenAIGPTModelTester(self  )
        A_					:				Optional[int]          =			ConfigTester(self			,      config_class=_snake_case			,      n_embd=37  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Dict  ):





        '''simple docstring'''




        self.config_tester.run_common_tests()



     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[int]  ):





        '''simple docstring'''




        A_					:				int          =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*_snake_case  )



     def 						SCREAMING_SNAKE_CASE		(							self       :List[str]  ):





        '''simple docstring'''




        A_					:				Optional[int]          =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_lm_head(*_snake_case  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[Any]  ):





        '''simple docstring'''




        A_					:				Dict          =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*_snake_case  )



     def 						SCREAMING_SNAKE_CASE		(							self       :List[Any]  ):





        '''simple docstring'''




        A_					:				Optional[int]          =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_snake_case  )





     @slow
     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[Any]  ):





        '''simple docstring'''




        for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           A_					:				int          =			TFOpenAIGPTModel.from_pretrained(_snake_case  )
           self.assertIsNotNone(_snake_case  )




@require_tf
class 					__magic_name__  (							unittest.TestCase							):





     """simple docstring"""



     @slow
     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[Any]  ):





        '''simple docstring'''




        A_					:				Union[str, Any]          =			TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"  )
        A_					:				Optional[Any]          =			tf.convert_to_tensor([[481, 4_735, 544]]			,      dtype=tf.intaa  )  # the president is
        A_					:				Union[str, Any]          =			[
            481,
            4_735,
            544,
            246,
            963,
            870,
            762,
            239,
            244,
            40_477,
            244,
            249,
            719,
            881,
            487,
            544,
            240,
            244,
            603,
            481,
        ]  # the president is a very good man. " \n " i\'m sure he is, " said the

        A_					:				Union[str, Any]          =			model.generate(_snake_case			,      do_sample=_snake_case  )
        self.assertListEqual(output_ids[0].numpy().tolist()			,      _snake_case  )







import argparse

import torch
from flax.training.checkpoints import restore_checkpoint

from transformers import FNetConfig, FNetForPreTraining
from transformers.utils import logging


logging.set_verbosity_info()

def      __snake_case						(					_lowerCAmelCase : Optional[int]				,				_lowerCAmelCase : List[str]				,				_lowerCAmelCase : List[Any]	) ->						Any:
   # Initialise PyTorch model
   A_					:				Tuple          =			FNetConfig.from_json_file(_lowerCAmelCase	)
   print(f"Building PyTorch model from configuration: {config}"	)
   A_					:				Tuple          =			FNetForPreTraining(_lowerCAmelCase	)

   A_					:				List[str]          =			restore_checkpoint(_lowerCAmelCase				,				_lowerCAmelCase	)
   A_					:				Any          =			checkpoint_dict["target"]

   # Embeddings
   # Position IDs
   A_					:				Optional[Any]          =			fnet_pretraining_model.state_dict()

   A_					:				Optional[int]          =			state_dict["fnet.embeddings.position_ids"]
   A_					:				Union[str, Any]          =			{"fnet.embeddings.position_ids": position_ids}
   # Embedding Layers
   A_					:				str          =			torch.tensor(
       pretrained_model_params["encoder"]["embedder"]["word"]["embedding"]	)
   A_					:				List[Any]          =			torch.tensor(
       pretrained_model_params["encoder"]["embedder"]["position"]["embedding"][0]	)
   A_					:				int          =			torch.tensor(
       pretrained_model_params["encoder"]["embedder"]["type"]["embedding"]	)
   A_					:				List[Any]          =			torch.tensor(
       pretrained_model_params["encoder"]["embedder"]["hidden_mapping_in"]["kernel"]	).T
   A_					:				Dict          =			torch.tensor(
       pretrained_model_params["encoder"]["embedder"]["hidden_mapping_in"]["bias"]	)
   A_					:				int          =			torch.tensor(
       pretrained_model_params["encoder"]["embedder"]["layer_norm"]["scale"]	)
   A_					:				Any          =			torch.tensor(
       pretrained_model_params["encoder"]["embedder"]["layer_norm"]["bias"]	)

   # Encoder Layers
   for layer in range(config.num_hidden_layers	):
      A_					:				List[str]          =			torch.tensor(
          pretrained_model_params["encoder"][f"encoder_{layer}"]["mixing_layer_norm"]["scale"]	)
      A_					:				Union[str, Any]          =			torch.tensor(
          pretrained_model_params["encoder"][f"encoder_{layer}"]["mixing_layer_norm"]["bias"]	)

      A_					:				Union[str, Any]          =			torch.tensor(
          pretrained_model_params["encoder"][f"feed_forward_{layer}"]["intermediate"]["kernel"]	).T
      A_					:				List[Any]          =			torch.tensor(
          pretrained_model_params["encoder"][f"feed_forward_{layer}"]["intermediate"]["bias"]	)

      A_					:				Optional[int]          =			torch.tensor(
          pretrained_model_params["encoder"][f"feed_forward_{layer}"]["output"]["kernel"]	).T
      A_					:				int          =			torch.tensor(
          pretrained_model_params["encoder"][f"feed_forward_{layer}"]["output"]["bias"]	)

      A_					:				Any          =			torch.tensor(
          pretrained_model_params["encoder"][f"encoder_{layer}"]["output_layer_norm"]["scale"]	)
      A_					:				Tuple          =			torch.tensor(
          pretrained_model_params["encoder"][f"encoder_{layer}"]["output_layer_norm"]["bias"]	)

   # Pooler Layers
   A_					:				Optional[Any]          =			torch.tensor(pretrained_model_params["encoder"]["pooler"]["kernel"]	).T
   A_					:				List[Any]          =			torch.tensor(pretrained_model_params["encoder"]["pooler"]["bias"]	)

   # Masked LM Layers
   A_					:				Optional[Any]          =			torch.tensor(
       pretrained_model_params["predictions_dense"]["kernel"]	).T
   A_					:				int          =			torch.tensor(
       pretrained_model_params["predictions_dense"]["bias"]	)
   A_					:				str          =			torch.tensor(
       pretrained_model_params["predictions_layer_norm"]["scale"]	)
   A_					:				List[Any]          =			torch.tensor(
       pretrained_model_params["predictions_layer_norm"]["bias"]	)
   A_					:				Tuple          =			torch.tensor(
       pretrained_model_params["encoder"]["embedder"]["word"]["embedding"]	)
   A_					:				Any          =			torch.tensor(
       pretrained_model_params["predictions_output"]["output_bias"]	)
   A_					:				List[str]          =			torch.tensor(pretrained_model_params["predictions_output"]["output_bias"]	)

   # Seq Relationship Layers
   A_					:				str          =			torch.tensor(
       pretrained_model_params["classification"]["output_kernel"]	)
   A_					:				str          =			torch.tensor(
       pretrained_model_params["classification"]["output_bias"]	)

   # Load State Dict
   fnet_pretraining_model.load_state_dict(_lowerCAmelCase	)

   # Save PreTrained
   print(f"Saving pretrained model to {save_path}"	)
   fnet_pretraining_model.save_pretrained(_lowerCAmelCase	)


if __name__ == "__main__":
 _lowerCAmelCase							:      Tuple						=	argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '''--flax_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
 )
 parser.add_argument(
     '''--fnet_config_file''',
     default=None,
     type=str,
     required=True,
     help=(
         '''The config json file corresponding to the pre-trained FNet model. \n'''
         '''This specifies the model architecture.'''
     ),
 )
 parser.add_argument('''--save_path''', default=None, type=str, required=True, help='''Path to the output model.''')
 _lowerCAmelCase							:      Optional[int]						=	parser.parse_args()
 convert_flax_checkpoint_to_pytorch(args.flax_checkpoint_path, args.fnet_config_file, args.save_path)
