



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


snake_case_								=			logging.get_logger(__name__)







class  A_     (	nn.Module							):



						"""simple docstring"""
						__UpperCamelCase													=				42
						__UpperCamelCase													=				jnp.floataa






						def       UpperCAmelCase__	(   self					:Optional[Any]   )							->     List[Any]:
												UpperCAmelCase			 =					self.config.text_config
												UpperCAmelCase			 =					self.config.vision_config

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

												UpperCAmelCase			 =					FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
												UpperCAmelCase			 =					FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__  ,    lowercase_   ).module_class

												UpperCAmelCase			 =					text_module(lowercase_  ,    dtype=self.dtype   )
												UpperCAmelCase			 =					vision_module(lowercase_  ,    dtype=self.dtype   )

												UpperCAmelCase			 =					nn.Dense(
												    self.projection_dim  ,    dtype=self.dtype  ,    kernel_init=jax.nn.initializers.normal(0.02   )  ,    use_bias=lowercase_  ,    )
												UpperCAmelCase			 =					nn.Dense(
												    self.projection_dim  ,    dtype=self.dtype  ,    kernel_init=jax.nn.initializers.normal(0.02   )  ,    use_bias=lowercase_  ,    )
												UpperCAmelCase			 =					self.param('logit_scale'  ,    jax.nn.initializers.ones  ,    []   )






						def __call__(   self					:Dict  ,    lowercase_					:int=None  ,    lowercase_					:Dict=None  ,    lowercase_					:Dict=None  ,    lowercase_					:Optional[Any]=None  ,    lowercase_					:List[Any]=None  ,    lowercase_					:bool = True  ,    lowercase_					:List[Any]=None  ,    lowercase_					:str=None  ,    lowercase_					:Tuple=None  ,    )							->     Union[str, Any]:
												UpperCAmelCase			 =					return_dict if return_dict is not None else self.config.return_dict

												UpperCAmelCase			 =					self.vision_model(
												    pixel_values=lowercase_  ,    deterministic=lowercase_  ,    output_attentions=lowercase_  ,    output_hidden_states=lowercase_  ,    return_dict=lowercase_  ,    )

												UpperCAmelCase			 =					self.text_model(
												    input_ids=lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    position_ids=lowercase_  ,    deterministic=lowercase_  ,    output_attentions=lowercase_  ,    output_hidden_states=lowercase_  ,    return_dict=lowercase_  ,    )

												UpperCAmelCase			 =					vision_outputs[1]
												UpperCAmelCase			 =					self.visual_projection(lowercase_   )

												UpperCAmelCase			 =					text_outputs[1]
												UpperCAmelCase			 =					self.text_projection(lowercase_   )

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

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







class  A_     (	SCREAMING_SNAKE_CASE_							):



						"""simple docstring"""
						__UpperCamelCase													=				HybridCLIPConfig
						__UpperCamelCase													=				FlaxHybridCLIPModule






						def __init__(   self					:Optional[int]  ,    lowercase_					:HybridCLIPConfig  ,    lowercase_					:Optional[Tuple] = None  ,    lowercase_					:int = 0  ,    lowercase_					:jnp.dtype = jnp.floataa  ,    **lowercase_					:List[str]  ,    )							->     Union[str, Any]:
												if input_shape is None:
																		UpperCAmelCase			 =					((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

												UpperCAmelCase			 =					self.module_class(config=lowercase_  ,    dtype=lowercase_  ,    **lowercase_   )
												super().__init__(lowercase_  ,    lowercase_  ,    input_shape=lowercase_  ,    seed=lowercase_  ,    dtype=lowercase_   )






						def       UpperCAmelCase__	(   self					:Tuple  ,    lowercase_					:jax.random.PRNGKey  ,    lowercase_					:Tuple  ,    lowercase_					:FrozenDict = None   )							->     FrozenDict:
												# init input tensor
												UpperCAmelCase			 =					jnp.zeros(input_shape[0]  ,    dtype='i4'   )
												UpperCAmelCase			 =					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_   ).shape[-1]   )  ,    input_shape[0]   )
												UpperCAmelCase			 =					jnp.ones_like(lowercase_   )
												UpperCAmelCase			 =					jnp.ones_like(lowercase_   )

												UpperCAmelCase			 =					jax.random.normal(lowercase_  ,    input_shape[1]   )

												UpperCAmelCase ,       UpperCAmelCase			 =					jax.random.split(lowercase_   )
												UpperCAmelCase			 =					{'params': params_rng, 'dropout': dropout_rng}

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






						def __call__(   self					:str  ,    lowercase_					:Dict  ,    lowercase_					:Optional[int]  ,    lowercase_					:List[Any]=None  ,    lowercase_					:Dict=None  ,    lowercase_					:Tuple=None  ,    lowercase_					:dict = None  ,    lowercase_					:jax.random.PRNGKey = None  ,    lowercase_					:bool = False  ,    lowercase_					:Optional[bool] = None  ,    lowercase_					:Optional[bool] = None  ,    lowercase_					:Optional[bool] = None  ,    )							->     Union[str, Any]:
												UpperCAmelCase			 =					output_attentions if output_attentions is not None else self.config.output_attentions
												UpperCAmelCase			 =					(
												    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
												)
												UpperCAmelCase			 =					return_dict if return_dict is not None else self.config.return_dict

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

												if token_type_ids is None:
																		UpperCAmelCase			 =					jnp.zeros_like(lowercase_   )

												if attention_mask is None:
																		UpperCAmelCase			 =					jnp.ones_like(lowercase_   )

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

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






						def       UpperCAmelCase__	(   self					:Tuple  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[int]=None  ,    lowercase_					:Union[str, Any]=None  ,    lowercase_					:Tuple=None  ,    lowercase_					:dict = None  ,    lowercase_					:jax.random.PRNGKey = None  ,    lowercase_					:Optional[Any]=False  ,    )							->     str:
												if position_ids is None:
																		UpperCAmelCase			 =					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_   ).shape[-1]   )  ,    input_ids.shape   )

												if token_type_ids is None:
																		UpperCAmelCase			 =					jnp.zeros_like(lowercase_   )

												if attention_mask is None:
																		UpperCAmelCase			 =					jnp.ones_like(lowercase_   )

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

												def _get_features(lowercase_					:Dict  ,    lowercase_					:List[Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Any  ,    lowercase_					:List[Any]   ):
																		UpperCAmelCase			 =					module.text_model(
																		    input_ids=lowercase_  ,    attention_mask=lowercase_  ,    position_ids=lowercase_  ,    token_type_ids=lowercase_  ,    deterministic=lowercase_  ,    )
																		UpperCAmelCase			 =					text_outputs[1]
																		UpperCAmelCase			 =					module.text_projection(lowercase_   )
																		return text_features

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






						def       UpperCAmelCase__	(   self					:Optional[Any]  ,    lowercase_					:Optional[int]  ,    lowercase_					:dict = None  ,    lowercase_					:jax.random.PRNGKey = None  ,    lowercase_					:int=False   )							->     List[Any]:
												UpperCAmelCase			 =					{}
												if dropout_rng is not None:
																		UpperCAmelCase			 =					dropout_rng

												def _get_features(lowercase_					:Any  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:int   ):
																		UpperCAmelCase			 =					module.vision_model(pixel_values=lowercase_  ,    deterministic=lowercase_   )
																		UpperCAmelCase			 =					vision_outputs[1]  # pooled_output
																		UpperCAmelCase			 =					module.visual_projection(lowercase_   )
																		return image_features

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






						@classmethod
						def       UpperCAmelCase__	(   cls					:str  ,    lowercase_					:str = None  ,    lowercase_					:str = None  ,    *lowercase_					:Tuple  ,    **lowercase_					:str  ,    )							->     FlaxPreTrainedModel:
												UpperCAmelCase			 =					{
												    argument[len('text_'   ) :]: value for argument, value in kwargs.items() if argument.startswith('text_'   )
												}

												UpperCAmelCase			 =					{
												    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			 =					kwargs_text.pop('model'  ,    lowercase_   )
												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			 =					AutoConfig.from_pretrained(lowercase_   )
																								UpperCAmelCase			 =					text_config

																		UpperCAmelCase			 =					FlaxAutoModel.from_pretrained(lowercase_  ,    *lowercase_  ,    **lowercase_   )

												UpperCAmelCase			 =					kwargs_vision.pop('model'  ,    lowercase_   )
												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			 =					AutoConfig.from_pretrained(lowercase_   )
																								UpperCAmelCase			 =					vision_config

																		UpperCAmelCase			 =					FlaxAutoModel.from_pretrained(lowercase_  ,    *lowercase_  ,    **lowercase_   )

												# instantiate config with corresponding kwargs
												UpperCAmelCase			 =					kwargs.pop('dtype'  ,    jnp.floataa   )
												UpperCAmelCase			 =					HybridCLIPConfig.from_text_vision_configs(text_model.config  ,    vision_model.config  ,    **lowercase_   )

												# init model
												UpperCAmelCase			 =					cls(lowercase_  ,    *lowercase_  ,    dtype=lowercase_  ,    **lowercase_   )

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

												UpperCAmelCase			 =					text_model.params

												return model












from __future__ import annotations

from collections import deque
from enum import Enum
from math import atana, degrees
from sys import maxsize







class        __snake_case			(				__lowerCamelCase     ):





       '''simple docstring'''







       lowerCAmelCase__ 						=	1
       lowerCAmelCase__ 						=	2
       lowerCAmelCase__ 						=	3




       def __repr__(    self					:       Optional[int]						):
         return f'''{self.__class__.__name__}.{self.name}'''







def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__)  ->       float:
  __snake_case    ,  __snake_case:		Union[str, Any]					    =		point
  return degrees(atana(y - miny   ,   x - minx))







def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__)  ->       Direction:
  __snake_case    ,  __snake_case:		Dict					    =		starting
  __snake_case    ,  __snake_case:		List[str]					    =		via
  __snake_case    ,  __snake_case:		Optional[int]					    =		target
  __snake_case:		int					    =		degrees(atana(ya - ya   ,   xa - xa))
  via_angle %= 360
  __snake_case:		List[Any]					    =		degrees(atana(ya - ya   ,   xa - xa))
  target_angle %= 360
  # t-
  #  \ \
  #   \ v
  #    \|
  #     s
  # via_angle is always lower than target_angle, if direction is left.
  # If they are same, it means they are on a same line of convex hull.
  if target_angle > via_angle:
    return Direction.left
  elif target_angle == via_angle:
    return Direction.straight
  else:
    return Direction.right







def      A__				(       SCREAMING_SNAKE_CASE__)  ->       list[tuple[int, int]]:

  if len(SCREAMING_SNAKE_CASE__) <= 2:
    # There is no convex hull
    raise ValueError("""graham_scan: argument must contain more than 3 points.""")
  if len(SCREAMING_SNAKE_CASE__) == 3:
    return points
  # find the lowest and the most left point
  __snake_case:		Optional[int]					    =		0
  __snake_case    ,  __snake_case:		Optional[int]					    =		maxsize, maxsize
  for i, point in enumerate(SCREAMING_SNAKE_CASE__):
    __snake_case:		Tuple					    =		point[0]
    __snake_case:		Optional[Any]					    =		point[1]
    if y < miny:
      __snake_case:		Optional[Any]					    =		y
      __snake_case:		Union[str, Any]					    =		x
      __snake_case:		Any					    =		i
    if y == miny and x < minx:
      __snake_case:		Optional[Any]					    =		x
      __snake_case:		str					    =		i

    # remove the lowest and the most left point from points for preparing for sort
  points.pop(SCREAMING_SNAKE_CASE__)

  __snake_case:		Optional[int]					    =		sorted(SCREAMING_SNAKE_CASE__   ,   key=lambda SCREAMING_SNAKE_CASE__: angle_comparer(SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__))
  # This insert actually costs complexity,
  # and you should instead add (minx, miny) into stack later.
  # I'm using insert just for easy understanding.
  sorted_points.insert(0   ,   (minx, miny))

  __snake_case:		deque[tuple[int, int]]					    =		deque()
  stack.append(sorted_points[0])
  stack.append(sorted_points[1])
  stack.append(sorted_points[2])
  # In any ways, the first 3 points line are towards left.
  # Because we sort them the angle from minx, miny.
  __snake_case:		Optional[Any]					    =		Direction.left

  for i in range(3   ,   len(SCREAMING_SNAKE_CASE__)):
    while True:
      __snake_case:		Optional[Any]					    =		stack[-2]
      __snake_case:		List[Any]					    =		stack[-1]
      __snake_case:		int					    =		sorted_points[i]
      __snake_case:		Union[str, Any]					    =		check_direction(SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__)

      if next_direction == Direction.left:
        __snake_case:		Dict					    =		Direction.left
        break
      if next_direction == Direction.straight:
        if current_direction == Direction.left:
          # We keep current_direction as left.
          # Because if the straight line keeps as straight,
          # we want to know if this straight line is towards left.
          break
        elif current_direction == Direction.right:
          # If the straight line is towards right,
          # every previous points on those straigh line is not convex hull.
          stack.pop()
      if next_direction == Direction.right:
        stack.pop()
    stack.append(sorted_points[i])
  return list(SCREAMING_SNAKE_CASE__)






