






"""simple docstring"""





import argparse
import json
import sys
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import List

import torch
import torch.nn as nn
from huggingface_hub import cached_download, hf_hub_download
from torch import Tensor

from transformers import AutoImageProcessor, VanConfig, VanForImageClassification
from transformers.models.deprecated.van.modeling_van import VanLayerScaling
from transformers.utils import logging


logging.set_verbosity_info()
UpperCamelCase =			logging.get_logger(__name__)







@dataclass
class 		_a   :





   '''simple docstring'''






   UpperCamelCase__			     =				42
   UpperCamelCase__			     =				field(default_factory=__a    )
   UpperCamelCase__			     =				field(default_factory=__a    )





   def __lowercase				(    self ,      UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_)       ->					List[str]:




          '''simple docstring'''




          lowercase__:				Dict			  =			len(list(m.modules())) == 1 or isinstance(lowerCAmelCase__ ,      nn.Convad) or isinstance(lowerCAmelCase__ ,      nn.BatchNormad)
          if has_not_submodules:
                 if not isinstance(lowerCAmelCase__ ,      lowerCAmelCase__):
                        self.traced.append(lowerCAmelCase__)





   def __call__(    self ,      UpperCAmelCase_)       ->					Optional[Any]:




          '''simple docstring'''




          for m in self.module.modules():
                 self.handles.append(m.register_forward_hook(self._forward_hook))
          self.module(lowerCAmelCase__)
          [x.remove() for x in self.handles]
          return self





   @property
   def __lowercase				(    self)       ->					str:




          '''simple docstring'''




          return list(filter(lambda UpperCAmelCase_: len(list(x.state_dict().keys())) > 0 ,      self.traced))







@dataclass
class 		_a   :





   '''simple docstring'''






   UpperCamelCase__			     =				42
   UpperCamelCase__			     =				42
   UpperCamelCase__			     =				0
   UpperCamelCase__			     =				field(default_factory=__a    )
   UpperCamelCase__			     =				field(default_factory=__a    )





   def __call__(    self ,      UpperCAmelCase_)       ->					List[str]:




          '''simple docstring'''




          lowercase__:				str			  =			Tracker(self.dest)(lowerCAmelCase__).parametrized
          lowercase__:				List[str]			  =			Tracker(self.src)(lowerCAmelCase__).parametrized

          lowercase__:				Optional[int]			  =			list(filter(lambda UpperCAmelCase_: type(lowerCAmelCase__) not in self.src_skip ,      lowerCAmelCase__))
          lowercase__:				Any			  =			list(filter(lambda UpperCAmelCase_: type(lowerCAmelCase__) not in self.dest_skip ,      lowerCAmelCase__))

          if len(lowerCAmelCase__) != len(lowerCAmelCase__):
                 raise Exception(
                     F"""Numbers of operations are different. Source module has {len(lowerCAmelCase__)} operations while"""
                     F""" destination module has {len(lowerCAmelCase__)}.""")

          for dest_m, src_m in zip(lowerCAmelCase__ ,      lowerCAmelCase__):
                 dest_m.load_state_dict(src_m.state_dict())
                 if self.verbose == 1:
                        print(F"""Transfered from={src_m} to={dest_m}""")







def 				A(  snake_case_						,   snake_case_		):
       """simple docstring"""





       lowercase__:				Optional[Any]			  =			from_model.state_dict()
       lowercase__:				Union[str, Any]			  =			our_model.state_dict()
       lowercase__:				Union[str, Any]			  =			our_model.config
       lowercase__:				List[str]			  =			[]
       for stage_idx in range(len(config.hidden_sizes		)		):
              for block_id in range(config.depths[stage_idx]		):
                     lowercase__:				Any			  =			F"""block{stage_idx + 1}.{block_id}.layer_scale_1"""
                     lowercase__:				Optional[Any]			  =			F"""van.encoder.stages.{stage_idx}.layers.{block_id}.attention_scaling.weight"""

                     all_keys.append((from_key, to_key)		)
                     lowercase__:				str			  =			F"""block{stage_idx + 1}.{block_id}.layer_scale_2"""
                     lowercase__:				Union[str, Any]			  =			F"""van.encoder.stages.{stage_idx}.layers.{block_id}.mlp_scaling.weight"""

                     all_keys.append((from_key, to_key)		)

       for from_key, to_key in all_keys:
              lowercase__:				str			  =			from_state_dict.pop(lowerCAmelCase_		)

       our_model.load_state_dict(lowerCAmelCase_		)
       return our_model






def 				A(  snake_case_						,   snake_case_						,   snake_case_						,   snake_case_						,   snake_case_						,   snake_case_ = True						,   ):
       """simple docstring"""





       print(F"""Downloading weights for {name}..."""		)
       lowercase__:				Tuple			  =			cached_download(lowerCAmelCase_		)
       print(F"""Converting {name}..."""		)
       lowercase__:				List[Any]			  =			torch.load(lowerCAmelCase_		)["""state_dict"""]
       from_model.load_state_dict(lowerCAmelCase_		)
       from_model.eval()
       with torch.no_grad():
              lowercase__:				List[str]			  =			VanForImageClassification(lowerCAmelCase_		).eval()
              lowercase__:				int			  =			ModuleTransfer(src=lowerCAmelCase_						,   dest=lowerCAmelCase_		)
              lowercase__:				Union[str, Any]			  =			torch.randn((1, 3, 224, 224)		)
              module_transfer(lowerCAmelCase_		)
              lowercase__:				str			  =			copy_parameters(lowerCAmelCase_						,   lowerCAmelCase_		)

       if not torch.allclose(from_model(lowerCAmelCase_		)						,   our_model(lowerCAmelCase_		).logits		):
              raise ValueError("The model logits don't match the original one."		)

       lowercase__:				Any			  =			name
       print(lowerCAmelCase_		)

       if push_to_hub:
              our_model.push_to_hub(
                  repo_path_or_name=save_directory / checkpoint_name						,   commit_message="Add model"						,   use_temp_dir=lowerCAmelCase_						,   )

              # we can use the convnext one
              lowercase__:				Optional[Any]			  =			AutoImageProcessor.from_pretrained("facebook/convnext-base-224-22k-1k"		)
              image_processor.push_to_hub(
                  repo_path_or_name=save_directory / checkpoint_name						,   commit_message="Add image processor"						,   use_temp_dir=lowerCAmelCase_						,   )

              print(F"""Pushed {checkpoint_name}"""		)






def 				A(  snake_case_						,   snake_case_ = None						,   snake_case_ = True		):
       """simple docstring"""





       lowercase__:				List[Any]			  =			"""imagenet-1k-id2label.json"""
       lowercase__:				str			  =			1000

       lowercase__:				Optional[int]			  =			"""huggingface/label-files"""
       lowercase__:				Union[str, Any]			  =			num_labels
       lowercase__:				Tuple			  =			json.load(open(hf_hub_download(lowerCAmelCase_						,   lowerCAmelCase_						,   repo_type="dataset"		)						,   "r"		)		)
       lowercase__:				str			  =			{int(lowerCAmelCase_		): v for k, v in idalabel.items()}

       lowercase__:				Optional[Any]			  =			idalabel
       lowercase__:				str			  =			{v: k for k, v in idalabel.items()}

       lowercase__:				List[str]			  =			partial(lowerCAmelCase_						,   num_labels=lowerCAmelCase_						,   idalabel=lowerCAmelCase_						,   labelaid=lowerCAmelCase_		)

       lowercase__:				Dict			  =			{
           """van-tiny""": ImageNetPreTrainedConfig(
               hidden_sizes=[32, 64, 160, 256]						,   depths=[3, 3, 5, 2]						,   mlp_ratios=[8, 8, 4, 4]						,   ),
           """van-small""": ImageNetPreTrainedConfig(
               hidden_sizes=[64, 128, 320, 512]						,   depths=[2, 2, 4, 2]						,   mlp_ratios=[8, 8, 4, 4]						,   ),
           """van-base""": ImageNetPreTrainedConfig(
               hidden_sizes=[64, 128, 320, 512]						,   depths=[3, 3, 12, 3]						,   mlp_ratios=[8, 8, 4, 4]						,   ),
           """van-large""": ImageNetPreTrainedConfig(
               hidden_sizes=[64, 128, 320, 512]						,   depths=[3, 5, 27, 3]						,   mlp_ratios=[8, 8, 4, 4]						,   ),
       }

       lowercase__:				Tuple			  =			{
           """van-tiny""": van_tiny,
           """van-small""": van_small,
           """van-base""": van_base,
           """van-large""": van_large,
       }

       lowercase__:				Tuple			  =			{
           """van-tiny""": (
               """https://huggingface.co/Visual-Attention-Network/VAN-Tiny-original/resolve/main/van_tiny_754.pth.tar"""
           ),
           """van-small""": (
               """https://huggingface.co/Visual-Attention-Network/VAN-Small-original/resolve/main/van_small_811.pth.tar"""
           ),
           """van-base""": (
               """https://huggingface.co/Visual-Attention-Network/VAN-Base-original/resolve/main/van_base_828.pth.tar"""
           ),
           """van-large""": (
               """https://huggingface.co/Visual-Attention-Network/VAN-Large-original/resolve/main/van_large_839.pth.tar"""
           ),
       }

       if model_name:
              convert_weight_and_push(
                  lowerCAmelCase_						,   names_to_config[model_name]						,   checkpoint=names_to_original_checkpoints[model_name]						,   from_model=names_to_original_models[model_name]()						,   save_directory=lowerCAmelCase_						,   push_to_hub=lowerCAmelCase_						,   )
       else:
              for model_name, config in names_to_config.items():
                     convert_weight_and_push(
                         lowerCAmelCase_						,   lowerCAmelCase_						,   checkpoint=names_to_original_checkpoints[model_name]						,   from_model=names_to_original_models[model_name]()						,   save_directory=lowerCAmelCase_						,   push_to_hub=lowerCAmelCase_						,   )


if __name__ == "__main__":
  UpperCamelCase =			argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      """--model-name""",
      default=None,
      type=str,
      help=(
          """The name of the model you wish to convert, it must be one of the supported resnet* architecture,"""
          """ currently: van-tiny/small/base/large. If `None`, all of them will the converted."""
      ),
  )
  parser.add_argument(
      """--pytorch_dump_folder_path""",
      default=None,
      type=Path,
      required=True,
      help="""Path to the output PyTorch model directory.""",
  )
  parser.add_argument(
      """--van_dir""",
      required=True,
      type=Path,
      help=(
          """A path to VAN\'s original implementation directory. You can download from here:"""
          """ https://github.com/Visual-Attention-Network/VAN-Classification"""
      ),
  )
  parser.add_argument(
      """--push_to_hub""",
      default=True,
      type=bool,
      required=False,
      help="""If True, push model and image processor to the hub.""",
  )

  UpperCamelCase =			parser.parse_args()
  UpperCamelCase =			args.pytorch_dump_folder_path
  pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True)
  UpperCamelCase =			args.van_dir
  # append the path to the parents to maskformer dir
  sys.path.append(str(van_dir.parent))
  from van.models.van import van_base, van_large, van_small, van_tiny

  convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)








'''simple docstring'''

from typing import Optional, Tuple

import flax.linen as nn
import jax
import jax.numpy as jnp
from configuration_hybrid_clip import HybridCLIPConfig
from flax.core.frozen_dict import FrozenDict

from transformers import FLAX_MODEL_MAPPING, FlaxCLIPVisionModel
from transformers.modeling_flax_utils import FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPOutput
from transformers.utils import logging


lowerCAmelCase_			:			List[str]			   =     logging.get_logger(__name__)







class 	__lowerCAmelCase    (  nn.Module				):
     snake_case   :    HybridCLIPConfig
     snake_case   :    jnp.dtype							      =      jnp.floataa



     def     snake_case_ (self	):
          _UpperCAmelCase					:  int         =    self.config.text_config
          _UpperCAmelCase					:  Dict         =    self.config.vision_config

          _UpperCAmelCase					:  str         =    self.config.projection_dim
          _UpperCAmelCase					:  int         =    text_config.hidden_size
          _UpperCAmelCase					:  int         =    vision_config.hidden_size

          _UpperCAmelCase					:  Optional[int]         =    FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
          _UpperCAmelCase					:  List[Any]         =    FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__						,	lowerCAmelCase__	).module_class

          _UpperCAmelCase					:  List[Any]         =    text_module(lowerCAmelCase__						,	dtype=self.dtype	)
          _UpperCAmelCase					:  Union[str, Any]         =    vision_module(lowerCAmelCase__						,	dtype=self.dtype	)

          _UpperCAmelCase					:  Optional[int]         =    nn.Dense(
              self.projection_dim						,	dtype=self.dtype						,	kernel_init=jax.nn.initializers.normal(0.0_2	)						,	use_bias=lowerCAmelCase__						,	)
          _UpperCAmelCase					:  List[str]         =    nn.Dense(
              self.projection_dim						,	dtype=self.dtype						,	kernel_init=jax.nn.initializers.normal(0.0_2	)						,	use_bias=lowerCAmelCase__						,	)
          _UpperCAmelCase					:  List[Any]         =    self.param("""logit_scale"""						,	jax.nn.initializers.ones						,	[]	)



     def __call__(self						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__ = True						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	):
          _UpperCAmelCase					:  int         =    return_dict if return_dict is not None else self.config.return_dict

          _UpperCAmelCase					:  Dict         =    self.vision_model(
              pixel_values=lowerCAmelCase__						,	deterministic=lowerCAmelCase__						,	output_attentions=lowerCAmelCase__						,	output_hidden_states=lowerCAmelCase__						,	return_dict=lowerCAmelCase__						,	)

          _UpperCAmelCase					:  int         =    self.text_model(
              input_ids=lowerCAmelCase__						,	attention_mask=lowerCAmelCase__						,	token_type_ids=lowerCAmelCase__						,	position_ids=lowerCAmelCase__						,	deterministic=lowerCAmelCase__						,	output_attentions=lowerCAmelCase__						,	output_hidden_states=lowerCAmelCase__						,	return_dict=lowerCAmelCase__						,	)

          _UpperCAmelCase					:  int         =    vision_outputs[1]
          _UpperCAmelCase					:  Dict         =    self.visual_projection(lowerCAmelCase__	)

          _UpperCAmelCase					:  List[str]         =    text_outputs[1]
          _UpperCAmelCase					:  List[Any]         =    self.text_projection(lowerCAmelCase__	)

          # normalized features
          _UpperCAmelCase					:  List[Any]         =    image_embeds / jnp.linalg.norm(lowerCAmelCase__						,	axis=-1						,	keepdims=lowerCAmelCase__	)
          _UpperCAmelCase					:  Optional[Any]         =    text_embeds / jnp.linalg.norm(lowerCAmelCase__						,	axis=-1						,	keepdims=lowerCAmelCase__	)

          # cosine similarity as logits
          _UpperCAmelCase					:  str         =    jnp.exp(self.logit_scale	)
          _UpperCAmelCase					:  str         =    jnp.matmul(lowerCAmelCase__						,	image_embeds.T	) * logit_scale
          _UpperCAmelCase					:  Any         =    logits_per_text.T

          if not return_dict:
               return (logits_per_image, logits_per_text, text_embeds, image_embeds, text_outputs, vision_outputs)

          return FlaxCLIPOutput(
              logits_per_image=lowerCAmelCase__						,	logits_per_text=lowerCAmelCase__						,	text_embeds=lowerCAmelCase__						,	image_embeds=lowerCAmelCase__						,	text_model_output=lowerCAmelCase__						,	vision_model_output=lowerCAmelCase__						,	)







class 	__lowerCAmelCase    (  __a				):
     snake_case   :    List[Any]							      =      HybridCLIPConfig
     snake_case   :    int							      =      FlaxHybridCLIPModule



     def __init__(self						,	lowerCAmelCase__						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = 0						,	lowerCAmelCase__ = jnp.floataa						,	**lowerCAmelCase__						,	):
          if input_shape is None:
               _UpperCAmelCase					:  Optional[Any]         =    ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

          _UpperCAmelCase					:  Union[str, Any]         =    self.module_class(config=lowerCAmelCase__						,	dtype=lowerCAmelCase__						,	**lowerCAmelCase__	)
          super().__init__(lowerCAmelCase__						,	lowerCAmelCase__						,	input_shape=lowerCAmelCase__						,	seed=lowerCAmelCase__						,	dtype=lowerCAmelCase__	)



     def     snake_case_ (self						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__ = None	):
          # init input tensor
          _UpperCAmelCase					:  List[Any]         =    jnp.zeros(input_shape[0]						,	dtype="""i4"""	)
          _UpperCAmelCase					:  str         =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowerCAmelCase__	).shape[-1]	)						,	input_shape[0]	)
          _UpperCAmelCase					:  List[Any]         =    jnp.ones_like(lowerCAmelCase__	)
          _UpperCAmelCase					:  List[str]         =    jnp.ones_like(lowerCAmelCase__	)

          _UpperCAmelCase					:  Any         =    jax.random.normal(lowerCAmelCase__						,	input_shape[1]	)

          _UpperCAmelCase  ,     _UpperCAmelCase					:  int         =    jax.random.split(lowerCAmelCase__	)
          _UpperCAmelCase					:  Any         =    {"""params""": params_rng, """dropout""": dropout_rng}

          return self.module.init(lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__	)["params"]



     def __call__(self						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = False						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	):
          _UpperCAmelCase					:  Any         =    output_attentions if output_attentions is not None else self.config.output_attentions
          _UpperCAmelCase					:  int         =    (
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          _UpperCAmelCase					:  int         =    return_dict if return_dict is not None else self.config.return_dict

          if position_ids is None:
               _UpperCAmelCase					:  List[Any]         =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowerCAmelCase__	).shape[-1]	)						,	input_ids.shape	)

          if token_type_ids is None:
               _UpperCAmelCase					:  List[str]         =    jnp.zeros_like(lowerCAmelCase__	)

          if attention_mask is None:
               _UpperCAmelCase					:  Any         =    jnp.ones_like(lowerCAmelCase__	)

          # Handle any PRNG if needed
          _UpperCAmelCase					:  List[str]         =    {}
          if dropout_rng is not None:
               _UpperCAmelCase					:  Union[str, Any]         =    dropout_rng

          return self.module.apply(
              {"""params""": params or self.params}						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype=jnp.floataa	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	not train						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	rngs=lowerCAmelCase__						,	)



     def     snake_case_ (self						,	lowerCAmelCase__						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__=False						,	):
          if position_ids is None:
               _UpperCAmelCase					:  str         =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowerCAmelCase__	).shape[-1]	)						,	input_ids.shape	)

          if token_type_ids is None:
               _UpperCAmelCase					:  str         =    jnp.zeros_like(lowerCAmelCase__	)

          if attention_mask is None:
               _UpperCAmelCase					:  Optional[Any]         =    jnp.ones_like(lowerCAmelCase__	)

          # Handle any PRNG if needed
          _UpperCAmelCase					:  str         =    {}
          if dropout_rng is not None:
               _UpperCAmelCase					:  List[Any]         =    dropout_rng

          def _get_features(lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__	):
               _UpperCAmelCase					:  Union[str, Any]         =    module.text_model(
                   input_ids=lowerCAmelCase__						,	attention_mask=lowerCAmelCase__						,	position_ids=lowerCAmelCase__						,	token_type_ids=lowerCAmelCase__						,	deterministic=lowerCAmelCase__						,	)
               _UpperCAmelCase					:  Optional[int]         =    text_outputs[1]
               _UpperCAmelCase					:  List[Any]         =    module.text_projection(lowerCAmelCase__	)
               return text_features

          return self.module.apply(
              {"""params""": params or self.params}						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	not train						,	method=_get_features						,	rngs=lowerCAmelCase__						,	)



     def     snake_case_ (self						,	lowerCAmelCase__						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__=False	):
          _UpperCAmelCase					:  Any         =    {}
          if dropout_rng is not None:
               _UpperCAmelCase					:  int         =    dropout_rng

          def _get_features(lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__	):
               _UpperCAmelCase					:  Optional[Any]         =    module.vision_model(pixel_values=lowerCAmelCase__						,	deterministic=lowerCAmelCase__	)
               _UpperCAmelCase					:  int         =    vision_outputs[1]  # pooled_output
               _UpperCAmelCase					:  Optional[int]         =    module.visual_projection(lowerCAmelCase__	)
               return image_features

          return self.module.apply(
              {"""params""": params or self.params}						,	jnp.array(lowerCAmelCase__						,	dtype=jnp.floataa	)						,	not train						,	method=_get_features						,	rngs=lowerCAmelCase__						,	)



     @classmethod
     def     snake_case_ (cls						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	*lowerCAmelCase__						,	**lowerCAmelCase__						,	):
          _UpperCAmelCase					:  List[Any]         =    {
              argument[len("""text_"""	) :]: value for argument, value in kwargs.items() if argument.startswith("""text_"""	)
          }

          _UpperCAmelCase					:  Tuple         =    {
              argument[len("""vision_"""	) :]: value for argument, value in kwargs.items() if argument.startswith("""vision_"""	)
          }

          # remove text, vision kwargs from kwargs
          for key in kwargs_text.keys():
               del kwargs["text_" + key]
          for key in kwargs_vision.keys():
               del kwargs["vision_" + key]

          # Load and initialize the text and vision model
          _UpperCAmelCase					:  List[Any]         =    kwargs_text.pop("""model"""						,	lowerCAmelCase__	)
          if text_model is None:
               assert (
                   text_model_name_or_path is not None
               ), "If `model` is not defined as an argument, a `text_model_name_or_path` has to be defined"
               from transformers import FlaxAutoModel

               if "config" not in kwargs_text:
                    from transformers import AutoConfig

                    _UpperCAmelCase					:  Any         =    AutoConfig.from_pretrained(lowerCAmelCase__	)
                    _UpperCAmelCase					:  int         =    text_config

               _UpperCAmelCase					:  Any         =    FlaxAutoModel.from_pretrained(lowerCAmelCase__						,	*lowerCAmelCase__						,	**lowerCAmelCase__	)

          _UpperCAmelCase					:  List[Any]         =    kwargs_vision.pop("""model"""						,	lowerCAmelCase__	)
          if vision_model is None:
               assert (
                   vision_model_name_or_path is not None
               ), "If `model` is not defined as an argument, a `vision_model_name_or_path` has to be defined"
               from transformers import FlaxAutoModel

               if "config" not in kwargs_vision:
                    from transformers import AutoConfig

                    _UpperCAmelCase					:  Union[str, Any]         =    AutoConfig.from_pretrained(lowerCAmelCase__	)
                    _UpperCAmelCase					:  Any         =    vision_config

               _UpperCAmelCase					:  Tuple         =    FlaxAutoModel.from_pretrained(lowerCAmelCase__						,	*lowerCAmelCase__						,	**lowerCAmelCase__	)

          # instantiate config with corresponding kwargs
          _UpperCAmelCase					:  List[Any]         =    kwargs.pop("""dtype"""						,	jnp.floataa	)
          _UpperCAmelCase					:  List[Any]         =    HybridCLIPConfig.from_text_vision_configs(text_model.config						,	vision_model.config						,	**lowerCAmelCase__	)

          # init model
          _UpperCAmelCase					:  Optional[Any]         =    cls(lowerCAmelCase__						,	*lowerCAmelCase__						,	dtype=lowerCAmelCase__						,	**lowerCAmelCase__	)

          if vision_config.model_type == "clip":
               _UpperCAmelCase					:  str         =    vision_model.params["""vision_model"""]
               _UpperCAmelCase					:  int         =    vision_model.params["""visual_projection"""]["""kernel"""]
          else:
               _UpperCAmelCase					:  List[str]         =    vision_model.params

          _UpperCAmelCase					:  Any         =    text_model.params

          return model


