





"""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	:						str					=					logging.get_logger(__name__)






class    _UpperCAmelCase  (      nn.Module					):
					UpperCamelCase						=				42
					UpperCamelCase						=				jnp.floataa


					def       lowerCamelCase			(   self     :str						):
												A						=    self.config.text_config
												A						=    self.config.vision_config

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

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

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

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


					def __call__(   self     :List[Any]	,			__UpperCamelCase     :List[Any]=None	,			__UpperCamelCase     :Union[str, Any]=None	,			__UpperCamelCase     :Optional[int]=None	,			__UpperCamelCase     :str=None	,			__UpperCamelCase     :Dict=None	,			__UpperCamelCase     :Tuple = True	,			__UpperCamelCase     :str=None	,			__UpperCamelCase     :List[str]=None	,			__UpperCamelCase     :str=None	,			):
												A						=    return_dict if return_dict is not None else self.config.return_dict

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

												A						=    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_	,			)

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

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

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

												# cosine similarity as logits
												A						=    jnp.exp(self.logit_scale						)
												A						=    jnp.matmul(lowercase_	,			image_embeds.T						) * logit_scale
												A						=    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    _UpperCAmelCase  (      lowerCamelCase__					):
					UpperCamelCase						=				HybridCLIPConfig
					UpperCamelCase						=				FlaxHybridCLIPModule


					def __init__(   self     :Tuple	,			__UpperCamelCase     :str	,			__UpperCamelCase     :Tuple = None	,			__UpperCamelCase     :Optional[Any] = 0	,			__UpperCamelCase     :List[str] = jnp.floataa	,			**__UpperCamelCase     :int	,			):






												if input_shape is None:
																			A						=    ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

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


					def       lowerCamelCase			(   self     :str	,			__UpperCamelCase     :Dict	,			__UpperCamelCase     :Optional[Any]	,			__UpperCamelCase     :List[Any] = None						):
												A						=    jnp.zeros(input_shape[0]	,			dtype="i4"						)
												A						=    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_						).shape[-1]						)	,			input_shape[0]						)
												A						=    jnp.ones_like(lowercase_						)
												A						=    jnp.ones_like(lowercase_						)

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

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

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


					def __call__(   self     :Union[str, Any]	,			__UpperCamelCase     :str	,			__UpperCamelCase     :List[Any]	,			__UpperCamelCase     :Tuple=None	,			__UpperCamelCase     :int=None	,			__UpperCamelCase     :Dict=None	,			__UpperCamelCase     :str = None	,			__UpperCamelCase     :List[Any] = None	,			__UpperCamelCase     :Optional[int] = False	,			__UpperCamelCase     :int = None	,			__UpperCamelCase     :Optional[int] = None	,			__UpperCamelCase     :Optional[int] = None	,			):
												A						=    output_attentions if output_attentions is not None else self.config.output_attentions
												A						=    (
												    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
												)
												A						=    return_dict if return_dict is not None else self.config.return_dict

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

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

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

												# Handle any PRNG if needed
												A						=    {}
												if dropout_rng is not None:
																			A						=    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       lowerCamelCase			(   self     :int	,			__UpperCamelCase     :Optional[Any]	,			__UpperCamelCase     :int=None	,			__UpperCamelCase     :Any=None	,			__UpperCamelCase     :List[str]=None	,			__UpperCamelCase     :int = None	,			__UpperCamelCase     :Any = None	,			__UpperCamelCase     :Union[str, Any]=False	,			):






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

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

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

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

												def _get_features(__UpperCamelCase     :Union[str, Any]	,			__UpperCamelCase     :List[Any]	,			__UpperCamelCase     :List[str]	,			__UpperCamelCase     :str	,			__UpperCamelCase     :Optional[Any]	,			__UpperCamelCase     :Tuple						):
																			A						=    module.text_model(
																			    input_ids=lowercase_	,			attention_mask=lowercase_	,			position_ids=lowercase_	,			token_type_ids=lowercase_	,			deterministic=lowercase_	,			)
																			A						=    text_outputs[1]
																			A						=    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       lowerCamelCase			(   self     :str	,			__UpperCamelCase     :Dict	,			__UpperCamelCase     :Dict = None	,			__UpperCamelCase     :Optional[int] = None	,			__UpperCamelCase     :Tuple=False						):
												A						=    {}
												if dropout_rng is not None:
																			A						=    dropout_rng

												def _get_features(__UpperCamelCase     :List[str]	,			__UpperCamelCase     :Any	,			__UpperCamelCase     :Any						):
																			A						=    module.vision_model(pixel_values=lowercase_	,			deterministic=lowercase_						)
																			A						=    vision_outputs[1]  # pooled_output
																			A						=    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       lowerCamelCase			(   cls     :Tuple	,			__UpperCamelCase     :int = None	,			__UpperCamelCase     :int = None	,			*__UpperCamelCase     :Optional[Any]	,			**__UpperCamelCase     :List[Any]	,			):
												A						=    {
												    argument[len("text_"						) :]: value for argument, value in kwargs.items() if argument.startswith("text_"						)
												}

												A						=    {
												    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
												A						=    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

																										A						=    AutoConfig.from_pretrained(lowercase_						)
																										A						=    text_config

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

												A						=    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

																										A						=    AutoConfig.from_pretrained(lowercase_						)
																										A						=    vision_config

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

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

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

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

												A						=    text_model.params

												return model





import copy

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


lowerCamelCase 	=   logging.get_logger(__name__)

lowerCamelCase 	=   {
    '''facebook/sam-vit-huge''': '''https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json''',
    '''facebook/sam-vit-large''': '''https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json''',
    '''facebook/sam-vit-base''': '''https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json''',
}







class 		__magic_name__     (				lowerCamelCase__				):

   '''simple docstring'''




   def __init__(							self,       lowercase_=256,       lowercase_=1024,       lowercase_=16,       lowercase_=16,       lowercase_=4,       lowercase_="gelu",       lowercase_=1E-6,       **lowercase_,       )				->       int:






     """simple docstring"""






     super().__init__(**lowercase_			)
     a__    =hidden_size
     a__    =image_size
     a__    =patch_size
     a__    =image_size // patch_size
     a__    =mask_input_channels
     a__    =num_point_embeddings
     a__    =hidden_act
     a__    =layer_norm_eps







class 		__magic_name__     (				lowerCamelCase__				):

   '''simple docstring'''




   def __init__(							self,       lowercase_=256,       lowercase_="relu",       lowercase_=2048,       lowercase_=2,       lowercase_=8,       lowercase_=2,       lowercase_=3,       lowercase_=3,       lowercase_=256,       lowercase_=1E-6,       **lowercase_,       )				->       int:






     """simple docstring"""






     super().__init__(**lowercase_			)
     a__    =hidden_size
     a__    =hidden_act
     a__    =mlp_dim
     a__    =num_hidden_layers
     a__    =num_attention_heads
     a__    =attention_downsample_rate
     a__    =num_multimask_outputs
     a__    =iou_head_depth
     a__    =iou_head_hidden_dim
     a__    =layer_norm_eps







class 		__magic_name__     (				lowerCamelCase__				):

   '''simple docstring'''




   def __init__(							self,       lowercase_=768,       lowercase_=256,       lowercase_=12,       lowercase_=12,       lowercase_=3,       lowercase_=1024,       lowercase_=16,       lowercase_="gelu",       lowercase_=1E-06,       lowercase_=0.0,       lowercase_=1E-10,       lowercase_=True,       lowercase_=4.0,       lowercase_=True,       lowercase_=True,       lowercase_=14,       lowercase_=[2, 5, 8, 11],       lowercase_=128,       lowercase_=None,       **lowercase_,       )				->       Union[str, Any]:






     """simple docstring"""






     super().__init__(**lowercase_			)

     a__    =hidden_size
     a__    =output_channels
     a__    =num_hidden_layers
     a__    =num_attention_heads
     a__    =num_channels
     a__    =image_size
     a__    =patch_size
     a__    =hidden_act
     a__    =layer_norm_eps
     a__    =attention_dropout
     a__    =initializer_range
     a__    =qkv_bias
     a__    =mlp_ratio
     a__    =use_abs_pos
     a__    =use_rel_pos
     a__    =window_size
     a__    =global_attn_indexes
     a__    =num_pos_feats
     a__    =int(hidden_size * mlp_ratio			) if mlp_dim is None else mlp_dim







class 		__magic_name__     (				lowerCamelCase__				):

   '''simple docstring'''




   lowerCamelCase__					:      Optional[int]        =  'sam'
   lowerCamelCase__					:      str        =  True



   def __init__(							self,       lowercase_=None,       lowercase_=None,       lowercase_=None,       lowercase_=0.02,       **lowercase_,       )				->       Tuple:






     """simple docstring"""






     super().__init__(**lowercase_			)
     a__    =vision_config if vision_config is not None else {}
     a__    =prompt_encoder_config if prompt_encoder_config is not None else {}
     a__    =mask_decoder_config if mask_decoder_config is not None else {}

     if isinstance(lowercase_,       lowercase_			):
       a__    =vision_config.to_dict()
     if isinstance(lowercase_,       lowercase_			):
       a__    =prompt_encoder_config.to_dict()
     if isinstance(lowercase_,       lowercase_			):
       a__    =mask_decoder_config.to_dict()

     a__    =SamVisionConfig(**lowercase_			)
     a__    =SamPromptEncoderConfig(**lowercase_			)
     a__    =SamMaskDecoderConfig(**lowercase_			)
     a__    =initializer_range



   def 					_UpperCAmelCase					(							self			)				->       List[str]:






     """simple docstring"""






     a__    =copy.deepcopy(self.__dict__			)
     a__    =self.vision_config.to_dict()
     a__    =self.prompt_encoder_config.to_dict()
     a__    =self.mask_decoder_config.to_dict()
     a__    =self.__class__.model_type
     return output
