"""simple docstring"""






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowerCAmelCase       :			int   	=logging.get_logger(__name__)

__lowerCAmelCase       :			List[Any]   	={
    """BAAI/AltCLIP""": """https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json""",
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}





class 					_A		(			lowerCAmelCase      ):
      snake_case__     :  str				    =      'altclip_text_model'

      def __init__(		self						,			__lowerCAmelCase=25_0002						,			__lowerCAmelCase=1024						,			__lowerCAmelCase=24						,			__lowerCAmelCase=16						,			__lowerCAmelCase=4096						,			__lowerCAmelCase="gelu"						,			__lowerCAmelCase=0.1						,			__lowerCAmelCase=0.1						,			__lowerCAmelCase=514						,			__lowerCAmelCase=1						,			__lowerCAmelCase=0.0_2						,			__lowerCAmelCase=0.0_2						,			__lowerCAmelCase=1E-05						,			__lowerCAmelCase=1						,			__lowerCAmelCase=0						,			__lowerCAmelCase=2						,			__lowerCAmelCase="absolute"						,			__lowerCAmelCase=True						,			__lowerCAmelCase=768						,			**__lowerCAmelCase						,			):







            """simple docstring"""



            super().__init__(pad_token_id=__lowerCAmelCase						,			bos_token_id=__lowerCAmelCase						,			eos_token_id=__lowerCAmelCase						,			**__lowerCAmelCase		)

            lowercase								=      vocab_size
            lowercase								=      hidden_size
            lowercase								=      num_hidden_layers
            lowercase								=      num_attention_heads
            lowercase								=      hidden_act
            lowercase								=      intermediate_size
            lowercase								=      hidden_dropout_prob
            lowercase								=      attention_probs_dropout_prob
            lowercase								=      max_position_embeddings
            lowercase								=      type_vocab_size
            lowercase								=      initializer_range
            lowercase								=      initializer_factor
            lowercase								=      layer_norm_eps
            lowercase								=      position_embedding_type
            lowercase								=      use_cache
            lowercase								=      project_dim






class 					_A		(			lowerCAmelCase      ):
      snake_case__     :  Dict				    =      'altclip_vision_model'

      def __init__(		self						,			__lowerCAmelCase=768						,			__lowerCAmelCase=3072						,			__lowerCAmelCase=512						,			__lowerCAmelCase=12						,			__lowerCAmelCase=12						,			__lowerCAmelCase=3						,			__lowerCAmelCase=224						,			__lowerCAmelCase=32						,			__lowerCAmelCase="quick_gelu"						,			__lowerCAmelCase=1E-5						,			__lowerCAmelCase=0.0						,			__lowerCAmelCase=0.0_2						,			__lowerCAmelCase=1.0						,			**__lowerCAmelCase						,			):







            """simple docstring"""



            super().__init__(**__lowerCAmelCase		)

            lowercase								=      hidden_size
            lowercase								=      intermediate_size
            lowercase								=      projection_dim
            lowercase								=      num_hidden_layers
            lowercase								=      num_attention_heads
            lowercase								=      num_channels
            lowercase								=      patch_size
            lowercase								=      image_size
            lowercase								=      initializer_range
            lowercase								=      initializer_factor
            lowercase								=      attention_dropout
            lowercase								=      layer_norm_eps
            lowercase								=      hidden_act







      @classmethod
      def 							A__			(		cls						,			__lowerCAmelCase						,			**__lowerCAmelCase		):







            """simple docstring"""



            cls._set_token_in_kwargs(__lowerCAmelCase		)

            lowercase				,				lowercase								=      cls.get_config_dict(__lowerCAmelCase						,			**__lowerCAmelCase		)

            # get the vision config dict if we are loading from AltCLIPConfig
            if config_dict.get("""model_type"""		) == "altclip":
                  lowercase								=      config_dict["""vision_config"""]

            if "model_type" in config_dict and hasattr(cls						,			"""model_type"""		) and config_dict["model_type"] != cls.model_type:
                  logger.warning(
                      f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
                      f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'		)

            return cls.from_dict(__lowerCAmelCase						,			**__lowerCAmelCase		)






class 					_A		(			lowerCAmelCase      ):
      snake_case__     :  Union[str, Any]				    =      'altclip'
      snake_case__     :  Dict				    =      True

      def __init__(		self						,			__lowerCAmelCase=None						,			__lowerCAmelCase=None						,			__lowerCAmelCase=768						,			__lowerCAmelCase=2.6_5_9_2						,			**__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      kwargs.pop("""text_config_dict"""						,			__lowerCAmelCase		)
            lowercase								=      kwargs.pop("""vision_config_dict"""						,			__lowerCAmelCase		)

            super().__init__(**__lowerCAmelCase		)

            # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
            # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
            # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
            if text_config_dict is not None:
                  if text_config is None:
                        lowercase								=      {}

                  # This is the complete result when using `text_config_dict`.
                  lowercase								=      AltCLIPTextConfig(**__lowerCAmelCase		).to_dict()

                  # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
                  for key, value in _text_config_dict.items():
                        if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
                              # If specified in `text_config_dict`
                              if key in text_config_dict:
                                    lowercase								=      (
                                        f'`{key}` is found in both `text_config_dict` and `text_config` but with different values. '
                                        f'The value `text_config_dict["{key}"]` will be used instead.'
                                    )
                              # If inferred from default argument values (just to be super careful)
                              else:
                                    lowercase								=      (
                                        f'`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '
                                        f'value `text_config["{key}"]` will be overriden.'
                                    )
                              logger.warning(__lowerCAmelCase		)

            # Update all values in `text_config` with the ones in `_text_config_dict`.
                  text_config.update(_text_config_dict		)

            if vision_config_dict is not None:
                  if vision_config is None:
                        lowercase								=      {}

                  # This is the complete result when using `vision_config_dict`.
                  lowercase								=      AltCLIPVisionConfig(**__lowerCAmelCase		).to_dict()
                  # convert keys to string instead of integer
                  if "id2label" in _vision_config_dict:
                        lowercase								=      {
                            str(__lowerCAmelCase		): value for key, value in _vision_config_dict["""id2label"""].items()
                        }

                  # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
                  for key, value in _vision_config_dict.items():
                        if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
                              # If specified in `vision_config_dict`
                              if key in vision_config_dict:
                                    lowercase								=      (
                                        f'`{key}` is found in both `vision_config_dict` and `vision_config` but with different '
                                        f'values. The value `vision_config_dict["{key}"]` will be used instead.'
                                    )
                              # If inferred from default argument values (just to be super careful)
                              else:
                                    lowercase								=      (
                                        f'`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '
                                        f'The value `vision_config["{key}"]` will be overriden.'
                                    )
                              logger.warning(__lowerCAmelCase		)

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
                  vision_config.update(_vision_config_dict		)

            if text_config is None:
                  lowercase								=      {}
                  logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."""		)

            if vision_config is None:
                  lowercase								=      {}
                  logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."""		)

            lowercase								=      AltCLIPTextConfig(**__lowerCAmelCase		)
            lowercase								=      AltCLIPVisionConfig(**__lowerCAmelCase		)

            lowercase								=      projection_dim
            lowercase								=      logit_scale_init_value
            lowercase								=      1.0

      @classmethod
      def 							A__			(		cls						,			__lowerCAmelCase						,			__lowerCAmelCase						,			**__lowerCAmelCase		):







            """simple docstring"""



            return cls(text_config=text_config.to_dict()						,			vision_config=vision_config.to_dict()						,			**__lowerCAmelCase		)







      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      copy.deepcopy(self.__dict__		)
            lowercase								=      self.text_config.to_dict()
            lowercase								=      self.vision_config.to_dict()
            lowercase								=      self.__class__.model_type
            return output




"""simple docstring"""






import argparse
import collections

import torch
from flax import traverse_util
from tax import checkpoints

from transformers import TaConfig, TaEncoderModel, TaForConditionalGeneration
from transformers.utils import logging


logging.set_verbosity_info()


def      UpperCAmelCase__			(							lowerCAmelCase__	:Optional[int]   ,						lowerCAmelCase__	:Tuple   ,						lowerCAmelCase__	:Optional[int]   ,						lowerCAmelCase__	:Optional[int]="attention"						)					->       str:
      '''simple docstring'''


      lowercase								=      params[f'{prefix}/layers_{i}/{layer_name}/key/kernel']
      lowercase								=      params[f'{prefix}/layers_{i}/{layer_name}/out/kernel']
      lowercase								=      params[f'{prefix}/layers_{i}/{layer_name}/query/kernel']
      lowercase								=      params[f'{prefix}/layers_{i}/{layer_name}/value/kernel']
      return k, o, q, v


def      UpperCAmelCase__			(							lowerCAmelCase__	:Union[str, Any]   ,						lowerCAmelCase__	:Optional[Any]   ,						lowerCAmelCase__	:Optional[Any]   ,						lowerCAmelCase__	:Union[str, Any]=False						)					->       str:
      '''simple docstring'''


      if split_mlp_wi:
            lowercase								=      params[f'{prefix}/layers_{i}/mlp/wi_0/kernel']
            lowercase								=      params[f'{prefix}/layers_{i}/mlp/wi_1/kernel']
            lowercase								=      (wi_a, wi_a)
      else:
            lowercase								=      params[f'{prefix}/layers_{i}/mlp/wi/kernel']

      lowercase								=      params[f'{prefix}/layers_{i}/mlp/wo/kernel']
      return wi, wo


def      UpperCAmelCase__			(							lowerCAmelCase__	:str   ,						lowerCAmelCase__	:Tuple   ,						lowerCAmelCase__	:int   ,						lowerCAmelCase__	:int						)					->       Any:
      '''simple docstring'''


      return params[f'{prefix}/layers_{i}/{layer_name}/scale']


def      UpperCAmelCase__			(							lowerCAmelCase__	:dict   ,						*, lowerCAmelCase__	:int   ,						lowerCAmelCase__	:bool						)					->       List[str]:
      '''simple docstring'''


      lowercase								=      traverse_util.flatten_dict(variables["""target"""]						)
      lowercase								=      {"""/""".join(lowerCAmelCase__						): v for k, v in old.items()}

      # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi
      lowercase								=      """encoder/layers_0/mlp/wi_0/kernel""" in old
      print("""Split MLP:"""   ,						lowerCAmelCase__						)

      lowercase								=      collections.OrderedDict()

      # Shared embeddings.
      lowercase								=      old["""token_embedder/embedding"""]

      # Encoder.
      for i in range(lowerCAmelCase__						):
            # Block i, layer 0 (Self Attention).
            lowercase								=      tax_layer_norm_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""encoder"""   ,						"""pre_attention_layer_norm"""						)
            lowercase				,				lowercase				,				lowercase				,				lowercase								=      tax_attention_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""encoder"""   ,						"""attention"""						)
            lowercase								=      layer_norm
            lowercase								=      k.T
            lowercase								=      o.T
            lowercase								=      q.T
            lowercase								=      v.T

            # Block i, layer 1 (MLP).
            lowercase								=      tax_layer_norm_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""encoder"""   ,						"""pre_mlp_layer_norm"""						)
            lowercase				,				lowercase								=      tax_mlp_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""encoder"""   ,						lowerCAmelCase__						)
            lowercase								=      layer_norm
            if split_mlp_wi:
                  lowercase								=      wi[0].T
                  lowercase								=      wi[1].T
            else:
                  lowercase								=      wi.T
            lowercase								=      wo.T

      lowercase								=      old[
          """encoder/relpos_bias/rel_embedding"""
      ].T
      lowercase								=      old["""encoder/encoder_norm/scale"""]

      if not is_encoder_only:
            # Decoder.
            for i in range(lowerCAmelCase__						):
                  # Block i, layer 0 (Self Attention).
                  lowercase								=      tax_layer_norm_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""decoder"""   ,						"""pre_self_attention_layer_norm"""						)
                  lowercase				,				lowercase				,				lowercase				,				lowercase								=      tax_attention_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""decoder"""   ,						"""self_attention"""						)
                  lowercase								=      layer_norm
                  lowercase								=      k.T
                  lowercase								=      o.T
                  lowercase								=      q.T
                  lowercase								=      v.T

                  # Block i, layer 1 (Cross Attention).
                  lowercase								=      tax_layer_norm_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""decoder"""   ,						"""pre_cross_attention_layer_norm"""						)
                  lowercase				,				lowercase				,				lowercase				,				lowercase								=      tax_attention_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""decoder"""   ,						"""encoder_decoder_attention"""						)
                  lowercase								=      layer_norm
                  lowercase								=      k.T
                  lowercase								=      o.T
                  lowercase								=      q.T
                  lowercase								=      v.T

                  # Block i, layer 2 (MLP).
                  lowercase								=      tax_layer_norm_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""decoder"""   ,						"""pre_mlp_layer_norm"""						)
                  lowercase				,				lowercase								=      tax_mlp_lookup(lowerCAmelCase__   ,						lowerCAmelCase__   ,						"""decoder"""   ,						lowerCAmelCase__						)
                  lowercase								=      layer_norm
                  if split_mlp_wi:
                        lowercase								=      wi[0].T
                        lowercase								=      wi[1].T
                  else:
                        lowercase								=      wi.T
                  lowercase								=      wo.T

            lowercase								=      old["""decoder/decoder_norm/scale"""]
            lowercase								=      old[
                """decoder/relpos_bias/rel_embedding"""
            ].T

            # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead)
            if "decoder/logits_dense/kernel" in old:
                  lowercase								=      old["""decoder/logits_dense/kernel"""].T

      return new


def      UpperCAmelCase__			(							lowerCAmelCase__	:List[Any]   ,						lowerCAmelCase__	:bool						)					->       int:
      '''simple docstring'''


      lowercase								=      collections.OrderedDict([(k, torch.from_numpy(v.copy()						)) for (k, v) in converted_params.items()]						)

      # Add what is missing.
      if "encoder.embed_tokens.weight" not in state_dict:
            lowercase								=      state_dict["""shared.weight"""]

      if not is_encoder_only:
            if "decoder.embed_tokens.weight" not in state_dict:
                  lowercase								=      state_dict["""shared.weight"""]

            if "lm_head.weight" not in state_dict:  # For old 1.0 models.
                  print("""Using shared word embeddings as lm_head."""						)
                  lowercase								=      state_dict["""shared.weight"""]

      return state_dict


def      UpperCAmelCase__			(							lowerCAmelCase__	:Optional[Any]   ,						lowerCAmelCase__	:Optional[int]   ,						lowerCAmelCase__	:Optional[Any]   ,						lowerCAmelCase__	:List[Any]						)					->       Union[str, Any]:
      '''simple docstring'''


      lowercase								=      checkpoints.load_tax_checkpoint(lowerCAmelCase__						)
      lowercase								=      convert_tax_to_pytorch(lowerCAmelCase__   ,						num_layers=config.num_layers   ,						is_encoder_only=lowerCAmelCase__						)
      lowercase								=      make_state_dict(lowerCAmelCase__   ,						lowerCAmelCase__						)
      model.load_state_dict(lowerCAmelCase__   ,						strict=lowerCAmelCase__						)


def      UpperCAmelCase__			(							lowerCAmelCase__	:str   ,						lowerCAmelCase__	:int   ,						lowerCAmelCase__	:Union[str, Any]   ,						lowerCAmelCase__	:bool = False						)					->       Tuple:
      '''simple docstring'''


      lowercase								=      TaConfig.from_json_file(lowerCAmelCase__						)
      print(f'Building PyTorch model from configuration: {config}'						)
      # Non-v1.1 checkpoints could also use T5Model, but this works for all.
      # The v1.0 checkpoints will simply have an LM head that is the word embeddings.
      if is_encoder_only:
            lowercase								=      TaEncoderModel(lowerCAmelCase__						)
      else:
            lowercase								=      TaForConditionalGeneration(lowerCAmelCase__						)

      # Load weights from tf checkpoint
      load_tax_weights_in_ta(lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__						)

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

      # Verify that we can load the checkpoint.
      model.from_pretrained(lowerCAmelCase__						)
      print("""Done"""						)


if __name__ == "__main__":
       __lowerCAmelCase       :			List[Any]   	=argparse.ArgumentParser(description="""Converts a native T5X checkpoint into a PyTorch checkpoint.""")
       # Required parameters
       parser.add_argument(
           """--t5x_checkpoint_path""", default=None, type=str, required=True, help="""Path to the T5X checkpoint."""
       )
       parser.add_argument(
           """--config_file""",
           default=None,
           type=str,
           required=True,
           help="""The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.""",
       )
       parser.add_argument(
           """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
       )
       parser.add_argument(
           """--is_encoder_only""", action="""store_true""", help="""Check if the model is encoder-decoder model""", default=False
       )
       __lowerCAmelCase       :			List[Any]   	=parser.parse_args()
       convert_tax_checkpoint_to_pytorch(
           args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only
       )



