
'''simple docstring'''

import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig

from diffusers import AutoencoderKL, PaintByExamplePipeline, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.paint_by_example import PaintByExampleImageEncoder
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







class 	__lowerCAmelCase    (  __a       ,					unittest.TestCase				):
     snake_case   :    List[str]							      =      PaintByExamplePipeline
     snake_case   :    str							      =      IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
     snake_case   :    str							      =      IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     snake_case   :    int							      =      frozenset([]				)  # TO_DO: update the image_prams once refactored VaeImageProcessor.preprocess



     def     snake_case_ (self	):
          torch.manual_seed(0	)
          _UpperCAmelCase					:  Tuple         =    UNetaDConditionModel(
              block_out_channels=(3_2, 6_4)						,	layers_per_block=2						,	sample_size=3_2						,	in_channels=9						,	out_channels=4						,	down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,	up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")						,	cross_attention_dim=3_2						,	)
          _UpperCAmelCase					:  Union[str, Any]         =    PNDMScheduler(skip_prk_steps=lowerCAmelCase__	)
          torch.manual_seed(0	)
          _UpperCAmelCase					:  int         =    AutoencoderKL(
              block_out_channels=[3_2, 6_4]						,	in_channels=3						,	out_channels=3						,	down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]						,	up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]						,	latent_channels=4						,	)
          torch.manual_seed(0	)
          _UpperCAmelCase					:  Tuple         =    CLIPVisionConfig(
              hidden_size=3_2						,	projection_dim=3_2						,	intermediate_size=3_7						,	layer_norm_eps=1e-05						,	num_attention_heads=4						,	num_hidden_layers=5						,	image_size=3_2						,	patch_size=4						,	)
          _UpperCAmelCase					:  Optional[Any]         =    PaintByExampleImageEncoder(lowerCAmelCase__						,	proj_size=3_2	)
          _UpperCAmelCase					:  int         =    CLIPImageProcessor(crop_size=3_2						,	size=3_2	)

          _UpperCAmelCase					:  Tuple         =    {
              """unet""": unet,
              """scheduler""": scheduler,
              """vae""": vae,
              """image_encoder""": image_encoder,
              """safety_checker""": None,
              """feature_extractor""": feature_extractor,
          }
          return components



     def     snake_case_ (self						,	lowerCAmelCase__	):
          _UpperCAmelCase					:  int         =    np.array(image.convert("""RGB"""	)	)
          _UpperCAmelCase					:  Any         =    image[None].transpose(0						,	3						,	1						,	2	)
          _UpperCAmelCase					:  str         =    torch.from_numpy(lowerCAmelCase__	).to(dtype=torch.floataa	) / 1_2_7.5 - 1.0
          return image



     def     snake_case_ (self						,	lowerCAmelCase__="cpu"						,	lowerCAmelCase__=0	):
          # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched
          _UpperCAmelCase					:  Optional[Any]         =    floats_tensor((1, 3, 3_2, 3_2)						,	rng=random.Random(lowerCAmelCase__	)	).to(lowerCAmelCase__	)
          _UpperCAmelCase					:  Tuple         =    image.cpu().permute(0						,	2						,	3						,	1	)[0]
          _UpperCAmelCase					:  Union[str, Any]         =    Image.fromarray(np.uinta(lowerCAmelCase__	)	).convert("""RGB"""	).resize((6_4, 6_4)	)
          _UpperCAmelCase					:  List[str]         =    Image.fromarray(np.uinta(image + 4	)	).convert("""RGB"""	).resize((6_4, 6_4)	)
          _UpperCAmelCase					:  Optional[int]         =    Image.fromarray(np.uinta(lowerCAmelCase__	)	).convert("""RGB"""	).resize((3_2, 3_2)	)

          if str(lowerCAmelCase__	).startswith("""mps"""	):
               _UpperCAmelCase					:  List[str]         =    torch.manual_seed(lowerCAmelCase__	)
          else:
               _UpperCAmelCase					:  Optional[int]         =    torch.Generator(device=lowerCAmelCase__	).manual_seed(lowerCAmelCase__	)
          _UpperCAmelCase					:  Optional[Any]         =    {
              """example_image""": example_image,
              """image""": init_image,
              """mask_image""": mask_image,
              """generator""": generator,
              """num_inference_steps""": 2,
              """guidance_scale""": 6.0,
              """output_type""": """numpy""",
          }
          return inputs



     def     snake_case_ (self	):
          _UpperCAmelCase					:  int         =    self.get_dummy_components()

          # make sure here that pndm scheduler skips prk
          _UpperCAmelCase					:  int         =    PaintByExamplePipeline(**lowerCAmelCase__	)
          _UpperCAmelCase					:  Union[str, Any]         =    pipe.to("""cpu"""	)
          pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

          _UpperCAmelCase					:  Optional[Any]         =    self.get_dummy_inputs()
          _UpperCAmelCase					:  int         =    pipe(**lowerCAmelCase__	)
          _UpperCAmelCase					:  Any         =    output.images

          _UpperCAmelCase					:  Optional[Any]         =    image[0, -3:, -3:, -1]

          assert image.shape == (1, 6_4, 6_4, 3)
          _UpperCAmelCase					:  Optional[Any]         =    np.array([0.4_6_8_6, 0.5_6_8_7, 0.4_0_0_7, 0.5_2_1_8, 0.5_7_4_1, 0.4_4_8_2, 0.4_9_4_0, 0.4_6_2_9, 0.4_5_0_3]	)

          assert np.abs(image_slice.flatten() - expected_slice	).max() < 1e-2



     def     snake_case_ (self	):
          _UpperCAmelCase					:  int         =    """cpu"""
          _UpperCAmelCase					:  str         =    self.get_dummy_inputs()
          inputs.pop("""mask_image"""	)
          _UpperCAmelCase					:  Dict         =    self.convert_to_pt(inputs.pop("""image"""	)	)
          _UpperCAmelCase					:  Optional[int]         =    image.clamp(0						,	1	) / 2

          # make sure here that pndm scheduler skips prk
          _UpperCAmelCase					:  str         =    PaintByExamplePipeline(**self.get_dummy_components()	)
          _UpperCAmelCase					:  Optional[int]         =    pipe.to(lowerCAmelCase__	)
          pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

          _UpperCAmelCase					:  str         =    pipe(image=lowerCAmelCase__						,	mask_image=mask_image[:, 0]						,	**lowerCAmelCase__	)
          _UpperCAmelCase					:  List[Any]         =    output.images

          _UpperCAmelCase					:  Optional[int]         =    image.cpu().permute(0						,	2						,	3						,	1	)[0]
          _UpperCAmelCase					:  int         =    mask_image.cpu().permute(0						,	2						,	3						,	1	)[0]

          _UpperCAmelCase					:  Optional[Any]         =    Image.fromarray(np.uinta(lowerCAmelCase__	)	).convert("""RGB"""	)
          _UpperCAmelCase					:  Tuple         =    Image.fromarray(np.uinta(lowerCAmelCase__	)	).convert("""RGB"""	)

          _UpperCAmelCase					:  Any         =    pipe(**self.get_dummy_inputs()	)
          _UpperCAmelCase					:  List[Any]         =    output.images

          assert out_a.shape == (1, 6_4, 6_4, 3)
          assert np.abs(out_a.flatten() - out_a.flatten()	).max() < 5e-2



     def     snake_case_ (self	):
          super().test_inference_batch_single_identical(expected_max_diff=3e-3	)







@slow
@require_torch_gpu
class 	__lowerCAmelCase    (  unittest.TestCase				):



     def     snake_case_ (self	):
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()



     def     snake_case_ (self	):
          # make sure here that pndm scheduler skips prk
          _UpperCAmelCase					:  Optional[int]         =    load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/paint_by_example/dog_in_bucket.png"""	)
          _UpperCAmelCase					:  Dict         =    load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/paint_by_example/mask.png"""	)
          _UpperCAmelCase					:  List[str]         =    load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/paint_by_example/panda.jpg"""	)

          _UpperCAmelCase					:  List[Any]         =    PaintByExamplePipeline.from_pretrained("""Fantasy-Studio/Paint-by-Example"""	)
          _UpperCAmelCase					:  Optional[Any]         =    pipe.to(lowerCAmelCase__	)
          pipe.set_progress_bar_config(disable=lowerCAmelCase__	)

          _UpperCAmelCase					:  str         =    torch.manual_seed(3_2_1	)
          _UpperCAmelCase					:  Any         =    pipe(
              image=lowerCAmelCase__						,	mask_image=lowerCAmelCase__						,	example_image=lowerCAmelCase__						,	generator=lowerCAmelCase__						,	guidance_scale=5.0						,	num_inference_steps=5_0						,	output_type="""np"""						,	)

          _UpperCAmelCase					:  str         =    output.images

          _UpperCAmelCase					:  Any         =    image[0, -3:, -3:, -1]

          assert image.shape == (1, 5_1_2, 5_1_2, 3)
          _UpperCAmelCase					:  Optional[int]         =    np.array([0.4_8_3_4, 0.4_8_1_1, 0.4_8_7_4, 0.5_1_2_2, 0.5_0_8_1, 0.5_1_4_4, 0.5_2_9_1, 0.5_2_9_0, 0.5_3_7_4]	)

          assert np.abs(image_slice.flatten() - expected_slice	).max() < 1e-2




'''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


