





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 				__UpperCamelCase  (		snake_case    ,	snake_case    ,	snake_case     )  ->						List[Any]:


   '''simple docstring'''







   __A				 =       FNetConfig.from_json_file(snake_case     )
   print(F"Building PyTorch model from configuration: {config}"     )
   __A				 =       FNetForPreTraining(snake_case     )

   __A				 =       restore_checkpoint(snake_case    ,	snake_case     )
   __A				 =       checkpoint_dict['''target''']

   # Embeddings
   # Position IDs
   __A				 =       fnet_pretraining_model.state_dict()

   __A				 =       state_dict['''fnet.embeddings.position_ids''']
   __A				 =       {'''fnet.embeddings.position_ids''': position_ids}
   # Embedding Layers
   __A				 =       torch.tensor(
       pretrained_model_params['''encoder''']['''embedder''']['''word''']['''embedding''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''encoder''']['''embedder''']['''position''']['''embedding'''][0]     )
   __A				 =       torch.tensor(
       pretrained_model_params['''encoder''']['''embedder''']['''type''']['''embedding''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''encoder''']['''embedder''']['''hidden_mapping_in''']['''kernel''']     ).T
   __A				 =       torch.tensor(
       pretrained_model_params['''encoder''']['''embedder''']['''hidden_mapping_in''']['''bias''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''encoder''']['''embedder''']['''layer_norm''']['''scale''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''encoder''']['''embedder''']['''layer_norm''']['''bias''']     )

   # Encoder Layers
   for layer in range(config.num_hidden_layers     ):
      __A				 =       torch.tensor(
          pretrained_model_params['''encoder'''][F"encoder_{layer}"]['''mixing_layer_norm''']['''scale''']     )
      __A				 =       torch.tensor(
          pretrained_model_params['''encoder'''][F"encoder_{layer}"]['''mixing_layer_norm''']['''bias''']     )

      __A				 =       torch.tensor(
          pretrained_model_params['''encoder'''][F"feed_forward_{layer}"]['''intermediate''']['''kernel''']     ).T
      __A				 =       torch.tensor(
          pretrained_model_params['''encoder'''][F"feed_forward_{layer}"]['''intermediate''']['''bias''']     )

      __A				 =       torch.tensor(
          pretrained_model_params['''encoder'''][F"feed_forward_{layer}"]['''output''']['''kernel''']     ).T
      __A				 =       torch.tensor(
          pretrained_model_params['''encoder'''][F"feed_forward_{layer}"]['''output''']['''bias''']     )

      __A				 =       torch.tensor(
          pretrained_model_params['''encoder'''][F"encoder_{layer}"]['''output_layer_norm''']['''scale''']     )
      __A				 =       torch.tensor(
          pretrained_model_params['''encoder'''][F"encoder_{layer}"]['''output_layer_norm''']['''bias''']     )

   # Pooler Layers
   __A				 =       torch.tensor(pretrained_model_params['''encoder''']['''pooler''']['''kernel''']     ).T
   __A				 =       torch.tensor(pretrained_model_params['''encoder''']['''pooler''']['''bias''']     )

   # Masked LM Layers
   __A				 =       torch.tensor(
       pretrained_model_params['''predictions_dense''']['''kernel''']     ).T
   __A				 =       torch.tensor(
       pretrained_model_params['''predictions_dense''']['''bias''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''predictions_layer_norm''']['''scale''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''predictions_layer_norm''']['''bias''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''encoder''']['''embedder''']['''word''']['''embedding''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''predictions_output''']['''output_bias''']     )
   __A				 =       torch.tensor(pretrained_model_params['''predictions_output''']['''output_bias''']     )

   # Seq Relationship Layers
   __A				 =       torch.tensor(
       pretrained_model_params['''classification''']['''output_kernel''']     )
   __A				 =       torch.tensor(
       pretrained_model_params['''classification''']['''output_bias''']     )

   # Load State Dict
   fnet_pretraining_model.load_state_dict(snake_case     )

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


if __name__ == "__main__":
    _UpperCamelCase   :							Dict     =	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.""")
    _UpperCamelCase   :							Optional[Any]     =	parser.parse_args()
    convert_flax_checkpoint_to_pytorch(args.flax_checkpoint_path, args.fnet_config_file, args.save_path)








from __future__ import annotations

import sys
def 				__UpperCamelCase  (		snake_case    ,	snake_case     )  ->						tuple[int, int]:


   '''simple docstring'''







   if abs(snake_case     ) == 1:
      return a, 0
   elif abs(snake_case     ) == 1:
      return 0, b

   __A ,						__A				 =       a, b
   __A ,						__A				 =       1, 0
   __A ,						__A				 =       0, 1

   while remainder != 0:
      __A				 =       old_remainder // remainder
      __A ,						__A				 =       remainder, old_remainder - quotient * remainder
      __A ,						__A				 =       coeff_a, old_coeff_a - quotient * coeff_a
      __A ,						__A				 =       coeff_b, old_coeff_b - quotient * coeff_b

   # sign correction for negative numbers
   if a < 0:
      __A				 =       -old_coeff_a
   if b < 0:
      __A				 =       -old_coeff_b

   return old_coeff_a, old_coeff_b
def 				__UpperCamelCase  (		)  ->						Tuple:


   '''simple docstring'''







   if len(sys.argv     ) < 3:
      print('''2 integer arguments required'''     )
      return 1
   __A				 =       int(sys.argv[1]     )
   __A				 =       int(sys.argv[2]     )
   print(extended_euclidean_algorithm(snake_case    ,	snake_case     )     )
   return 0


if __name__ == "__main__":
    raise SystemExit(main())

