

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


SCREAMING_SNAKE_CASE_  :      List[Any]							=      logging.get_logger(__name__)


class 		_A							(      nn.Module       ):
      __a             =						42
      __a             =						jnp.floataa





      def        _lowerCamelCase	(	self		) ->       int:
         lowerCamelCase__						       =      self.config.text_config
         lowerCamelCase__						       =      self.config.vision_config

         lowerCamelCase__						       =      self.config.projection_dim
         lowerCamelCase__						       =      text_config.hidden_size
         lowerCamelCase__						       =      vision_config.hidden_size

         lowerCamelCase__						       =      FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
         lowerCamelCase__						       =      FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__     ,    SCREAMING_SNAKE_CASE__		).module_class

         lowerCamelCase__						       =      text_module(SCREAMING_SNAKE_CASE__     ,    dtype=self.dtype		)
         lowerCamelCase__						       =      vision_module(SCREAMING_SNAKE_CASE__     ,    dtype=self.dtype		)

         lowerCamelCase__						       =      nn.Dense(
             self.projection_dim     ,    dtype=self.dtype     ,    kernel_init=jax.nn.initializers.normal(0.02		)     ,    use_bias=SCREAMING_SNAKE_CASE__     ,    )
         lowerCamelCase__						       =      nn.Dense(
             self.projection_dim     ,    dtype=self.dtype     ,    kernel_init=jax.nn.initializers.normal(0.02		)     ,    use_bias=SCREAMING_SNAKE_CASE__     ,    )
         lowerCamelCase__						       =      self.param("logit_scale"     ,    jax.nn.initializers.ones     ,    []		)





      def __call__(	self     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__ = True     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    ) ->       Optional[Any]:
         lowerCamelCase__						       =      return_dict if return_dict is not None else self.config.return_dict

         lowerCamelCase__						       =      self.vision_model(
             pixel_values=SCREAMING_SNAKE_CASE__     ,    deterministic=SCREAMING_SNAKE_CASE__     ,    output_attentions=SCREAMING_SNAKE_CASE__     ,    output_hidden_states=SCREAMING_SNAKE_CASE__     ,    return_dict=SCREAMING_SNAKE_CASE__     ,    )

         lowerCamelCase__						       =      self.text_model(
             input_ids=SCREAMING_SNAKE_CASE__     ,    attention_mask=SCREAMING_SNAKE_CASE__     ,    token_type_ids=SCREAMING_SNAKE_CASE__     ,    position_ids=SCREAMING_SNAKE_CASE__     ,    deterministic=SCREAMING_SNAKE_CASE__     ,    output_attentions=SCREAMING_SNAKE_CASE__     ,    output_hidden_states=SCREAMING_SNAKE_CASE__     ,    return_dict=SCREAMING_SNAKE_CASE__     ,    )

         lowerCamelCase__						       =      vision_outputs[1]
         lowerCamelCase__						       =      self.visual_projection(SCREAMING_SNAKE_CASE__		)

         lowerCamelCase__						       =      text_outputs[1]
         lowerCamelCase__						       =      self.text_projection(SCREAMING_SNAKE_CASE__		)

         # normalized features
         lowerCamelCase__						       =      image_embeds / jnp.linalg.norm(SCREAMING_SNAKE_CASE__     ,    axis=-1     ,    keepdims=SCREAMING_SNAKE_CASE__		)
         lowerCamelCase__						       =      text_embeds / jnp.linalg.norm(SCREAMING_SNAKE_CASE__     ,    axis=-1     ,    keepdims=SCREAMING_SNAKE_CASE__		)

         # cosine similarity as logits
         lowerCamelCase__						       =      jnp.exp(self.logit_scale		)
         lowerCamelCase__						       =      jnp.matmul(SCREAMING_SNAKE_CASE__     ,    image_embeds.T		) * logit_scale
         lowerCamelCase__						       =      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=SCREAMING_SNAKE_CASE__     ,    logits_per_text=SCREAMING_SNAKE_CASE__     ,    text_embeds=SCREAMING_SNAKE_CASE__     ,    image_embeds=SCREAMING_SNAKE_CASE__     ,    text_model_output=SCREAMING_SNAKE_CASE__     ,    vision_model_output=SCREAMING_SNAKE_CASE__     ,    )






class 		_A							(      __a       ):
      __a             =						HybridCLIPConfig
      __a             =						FlaxHybridCLIPModule





      def __init__(	self     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__ = 0     ,    SCREAMING_SNAKE_CASE__ = jnp.floataa     ,    **SCREAMING_SNAKE_CASE__     ,    ) ->       int:
         if input_shape is None:
            lowerCamelCase__						       =      ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

         lowerCamelCase__						       =      self.module_class(config=SCREAMING_SNAKE_CASE__     ,    dtype=SCREAMING_SNAKE_CASE__     ,    **SCREAMING_SNAKE_CASE__		)
         super().__init__(SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    input_shape=SCREAMING_SNAKE_CASE__     ,    seed=SCREAMING_SNAKE_CASE__     ,    dtype=SCREAMING_SNAKE_CASE__		)





      def        _lowerCamelCase	(	self     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__ = None		) ->       FrozenDict:
         # init input tensor
         lowerCamelCase__						       =      jnp.zeros(input_shape[0]     ,    dtype="i4"		)
         lowerCamelCase__						       =      jnp.broadcast_to(jnp.arange(jnp.atleast_ad(SCREAMING_SNAKE_CASE__		).shape[-1]		)     ,    input_shape[0]		)
         lowerCamelCase__						       =      jnp.ones_like(SCREAMING_SNAKE_CASE__		)
         lowerCamelCase__						       =      jnp.ones_like(SCREAMING_SNAKE_CASE__		)

         lowerCamelCase__						       =      jax.random.normal(SCREAMING_SNAKE_CASE__     ,    input_shape[1]		)

         lowerCamelCase__       ,							lowerCamelCase__						       =      jax.random.split(SCREAMING_SNAKE_CASE__		)
         lowerCamelCase__						       =      {"params": params_rng, "dropout": dropout_rng}

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





      def __call__(	self     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__ = False     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__ = None     ,    ) ->       int:
         lowerCamelCase__						       =      output_attentions if output_attentions is not None else self.config.output_attentions
         lowerCamelCase__						       =      (
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         lowerCamelCase__						       =      return_dict if return_dict is not None else self.config.return_dict

         if position_ids is None:
            lowerCamelCase__						       =      jnp.broadcast_to(jnp.arange(jnp.atleast_ad(SCREAMING_SNAKE_CASE__		).shape[-1]		)     ,    input_ids.shape		)

         if token_type_ids is None:
            lowerCamelCase__						       =      jnp.zeros_like(SCREAMING_SNAKE_CASE__		)

         if attention_mask is None:
            lowerCamelCase__						       =      jnp.ones_like(SCREAMING_SNAKE_CASE__		)

         # Handle any PRNG if needed
         lowerCamelCase__						       =      {}
         if dropout_rng is not None:
            lowerCamelCase__						       =      dropout_rng

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





      def        _lowerCamelCase	(	self     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__=None     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__=False     ,    ) ->       Optional[Any]:
         if position_ids is None:
            lowerCamelCase__						       =      jnp.broadcast_to(jnp.arange(jnp.atleast_ad(SCREAMING_SNAKE_CASE__		).shape[-1]		)     ,    input_ids.shape		)

         if token_type_ids is None:
            lowerCamelCase__						       =      jnp.zeros_like(SCREAMING_SNAKE_CASE__		)

         if attention_mask is None:
            lowerCamelCase__						       =      jnp.ones_like(SCREAMING_SNAKE_CASE__		)

         # Handle any PRNG if needed
         lowerCamelCase__						       =      {}
         if dropout_rng is not None:
            lowerCamelCase__						       =      dropout_rng

         def _get_features(SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__		):
            lowerCamelCase__						       =      module.text_model(
                input_ids=SCREAMING_SNAKE_CASE__     ,    attention_mask=SCREAMING_SNAKE_CASE__     ,    position_ids=SCREAMING_SNAKE_CASE__     ,    token_type_ids=SCREAMING_SNAKE_CASE__     ,    deterministic=SCREAMING_SNAKE_CASE__     ,    )
            lowerCamelCase__						       =      text_outputs[1]
            lowerCamelCase__						       =      module.text_projection(SCREAMING_SNAKE_CASE__		)
            return text_features

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





      def        _lowerCamelCase	(	self     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__=False		) ->       Union[str, Any]:
         lowerCamelCase__						       =      {}
         if dropout_rng is not None:
            lowerCamelCase__						       =      dropout_rng

         def _get_features(SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__		):
            lowerCamelCase__						       =      module.vision_model(pixel_values=SCREAMING_SNAKE_CASE__     ,    deterministic=SCREAMING_SNAKE_CASE__		)
            lowerCamelCase__						       =      vision_outputs[1]  # pooled_output
            lowerCamelCase__						       =      module.visual_projection(SCREAMING_SNAKE_CASE__		)
            return image_features

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





      @classmethod
      def        _lowerCamelCase	(	cls     ,    SCREAMING_SNAKE_CASE__ = None     ,    SCREAMING_SNAKE_CASE__ = None     ,    *SCREAMING_SNAKE_CASE__     ,    **SCREAMING_SNAKE_CASE__     ,    ) ->       FlaxPreTrainedModel:
         lowerCamelCase__						       =      {
             argument[len("text_"		) :]: value for argument, value in kwargs.items() if argument.startswith("text_"		)
         }

         lowerCamelCase__						       =      {
             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
         lowerCamelCase__						       =      kwargs_text.pop("model"     ,    SCREAMING_SNAKE_CASE__		)
         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

               lowerCamelCase__						       =      AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__		)
               lowerCamelCase__						       =      text_config

            lowerCamelCase__						       =      FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE__     ,    *SCREAMING_SNAKE_CASE__     ,    **SCREAMING_SNAKE_CASE__		)

         lowerCamelCase__						       =      kwargs_vision.pop("model"     ,    SCREAMING_SNAKE_CASE__		)
         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

               lowerCamelCase__						       =      AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__		)
               lowerCamelCase__						       =      vision_config

            lowerCamelCase__						       =      FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE__     ,    *SCREAMING_SNAKE_CASE__     ,    **SCREAMING_SNAKE_CASE__		)

         # instantiate config with corresponding kwargs
         lowerCamelCase__						       =      kwargs.pop("dtype"     ,    jnp.floataa		)
         lowerCamelCase__						       =      HybridCLIPConfig.from_text_vision_configs(text_model.config     ,    vision_model.config     ,    **SCREAMING_SNAKE_CASE__		)

         # init model
         lowerCamelCase__						       =      cls(SCREAMING_SNAKE_CASE__     ,    *SCREAMING_SNAKE_CASE__     ,    dtype=SCREAMING_SNAKE_CASE__     ,    **SCREAMING_SNAKE_CASE__		)

         if vision_config.model_type == "clip":
            lowerCamelCase__						       =      vision_model.params["vision_model"]
            lowerCamelCase__						       =      vision_model.params["visual_projection"]["kernel"]
         else:
            lowerCamelCase__						       =      vision_model.params

         lowerCamelCase__						       =      text_model.params

         return model







"""simple docstring"""




SCREAMING_SNAKE_CASE_  :      Optional[int]							=      {0: [1, 2], 1: [0, 3], 2: [0], 3: [1], 4: [5, 6], 5: [4, 6], 6: [4, 5]}

SCREAMING_SNAKE_CASE_  :      str							=      {0: [1, 2, 3], 1: [0, 3], 2: [0], 3: [0, 1], 4: [], 5: []}



def 						UpperCAmelCase__ (					A__						,     A__						,     A__ )   ->   list:




   """simple docstring"""
   lowerCamelCase__						       =      True
   lowerCamelCase__						       =      []

   for neighbour in graph[vert]:
      if not visited[neighbour]:
         connected_verts += dfs(A__						,     A__						,     A__ )

   return [vert, *connected_verts]



def 						UpperCAmelCase__ (					A__ )   ->   list:




   """simple docstring"""
   lowerCamelCase__						       =      len(A__ )
   lowerCamelCase__						       =      graph_size * [False]
   lowerCamelCase__						       =      []

   for i in range(A__ ):
      if not visited[i]:
         lowerCamelCase__						       =      dfs(A__						,     A__						,     A__ )
         components_list.append(A__ )

   return components_list


if __name__ == "__main__":
     import doctest

     doctest.testmod()




