






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


_UpperCamelCase     			=  logging.get_logger(__name__)







class 					lowerCamelCase__					(       nn.Module ):
    SCREAMING_SNAKE_CASE		=			42
    SCREAMING_SNAKE_CASE		=			jnp.floataa






    def 				_UpperCamelCase							( self					):
          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__	,A					).module_class

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

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






    def __call__( self	,A=None	,A=None	,A=None	,A=None	,A=None	,A = True	,A=None	,A=None	,A=None	,):
          UpperCAmelCase							=       return_dict if return_dict is not None else self.config.return_dict

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

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

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

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

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

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







class 					lowerCamelCase__					(       snake_case ):
    SCREAMING_SNAKE_CASE		=			HybridCLIPConfig
    SCREAMING_SNAKE_CASE		=			FlaxHybridCLIPModule






    def __init__( self	,A	,A = None	,A = 0	,A = jnp.floataa	,**A	,):
          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=A	,dtype=A	,**A					)
          super().__init__(A	,A	,input_shape=A	,seed=A	,dtype=A					)






    def 				_UpperCamelCase							( self	,A	,A	,A = None					):
          # init input tensor
          UpperCAmelCase							=       jnp.zeros(input_shape[0]	,dtype="""i4"""					)
          UpperCAmelCase							=       jnp.broadcast_to(jnp.arange(jnp.atleast_ad(A					).shape[-1]					)	,input_shape[0]					)
          UpperCAmelCase							=       jnp.ones_like(A					)
          UpperCAmelCase							=       jnp.ones_like(A					)

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

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

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






    def __call__( self	,A	,A	,A=None	,A=None	,A=None	,A = None	,A = None	,A = False	,A = None	,A = None	,A = None	,):
          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(A					).shape[-1]					)	,input_ids.shape					)

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

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

          # 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(A	,dtype="""i4"""					)	,jnp.array(A	,dtype=jnp.floataa					)	,jnp.array(A	,dtype="""i4"""					)	,jnp.array(A	,dtype="""i4"""					)	,jnp.array(A	,dtype="""i4"""					)	,not train	,A	,A	,A	,rngs=A	,)






    def 				_UpperCamelCase							( self	,A	,A=None	,A=None	,A=None	,A = None	,A = None	,A=False	,):
          if position_ids is None:
                UpperCAmelCase							=       jnp.broadcast_to(jnp.arange(jnp.atleast_ad(A					).shape[-1]					)	,input_ids.shape					)

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

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

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

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

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






    def 				_UpperCamelCase							( self	,A	,A = None	,A = None	,A=False					):
          UpperCAmelCase							=       {}
          if dropout_rng is not None:
                UpperCAmelCase							=       dropout_rng

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

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






    @classmethod
    def 				_UpperCamelCase							( cls	,A = None	,A = None	,*A	,**A	,):
          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"""	,A					)
          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(A					)
                      UpperCAmelCase							=       text_config

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

          UpperCAmelCase							=       kwargs_vision.pop("""model"""	,A					)
          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(A					)
                      UpperCAmelCase							=       vision_config

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

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

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

          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











"""simple docstring"""







import sys

def _a (		_snake_case      ,       _snake_case							):







      """simple docstring"""






      if not numbers:
            return 0
      if target == 0 and target in numbers:
            return 0
      if not isinstance(_snake_case      ,       (list, tuple)							) or not all(
          isinstance(_snake_case      ,       _snake_case							) for number in numbers							):
            raise ValueError("""numbers must be an iterable of integers"""							)

      UpperCAmelCase							=       UpperCAmelCase							=       UpperCAmelCase							=       0
      UpperCAmelCase							=       sys.maxsize

      while right < len(_snake_case							):
            curr_sum += numbers[right]
            while curr_sum >= target and left <= right:
                  UpperCAmelCase							=       min(_snake_case      ,       right - left + 1							)
                  curr_sum -= numbers[left]
                  left += 1
            right += 1

      return 0 if min_len == sys.maxsize else min_len



