






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






class __a ( nn.Module					):
  __lowercase     :     Union[str, Any]       						=       42
  __lowercase     :     Union[str, Any]       						=       jnp.floataa



  def     A_					(self	:		List[Any] )		->				Dict:
   __A						:							int      =						self.config.text_config
   __A						:							Tuple      =						self.config.vision_config

   __A						:							str      =						self.config.projection_dim
   __A						:							Dict      =						text_config.hidden_size
   __A						:							List[Any]      =						vision_config.hidden_size

   __A						:							Dict      =						FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
   __A						:							List[Any]      =						FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__					,      lowercase_ ).module_class

   __A						:							Optional[Any]      =						text_module(lowercase_					,      dtype=self.dtype )
   __A						:							Dict      =						vision_module(lowercase_					,      dtype=self.dtype )

   __A						:							str      =						nn.Dense(
       self.projection_dim					,      dtype=self.dtype					,      kernel_init=jax.nn.initializers.normal(0.02 )					,      use_bias=lowercase_					,      )
   __A						:							Optional[Any]      =						nn.Dense(
       self.projection_dim					,      dtype=self.dtype					,      kernel_init=jax.nn.initializers.normal(0.02 )					,      use_bias=lowercase_					,      )
   __A						:							str      =						self.param('logit_scale'					,      jax.nn.initializers.ones					,      [] )



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

   __A						:							Dict      =						self.vision_model(
       pixel_values=lowercase_					,      deterministic=lowercase_					,      output_attentions=lowercase_					,      output_hidden_states=lowercase_					,      return_dict=lowercase_					,      )

   __A						:							Union[str, Any]      =						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						:							Union[str, Any]      =						vision_outputs[1]
   __A						:							str      =						self.visual_projection(lowercase_ )

   __A						:							Tuple      =						text_outputs[1]
   __A						:							Union[str, Any]      =						self.text_projection(lowercase_ )

   # normalized features
   __A						:							Tuple      =						image_embeds / jnp.linalg.norm(lowercase_					,      axis=-1					,      keepdims=lowercase_ )
   __A						:							Optional[int]      =						text_embeds / jnp.linalg.norm(lowercase_					,      axis=-1					,      keepdims=lowercase_ )

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






class __a ( SCREAMING_SNAKE_CASE__					):
  __lowercase     :     Any       						=       HybridCLIPConfig
  __lowercase     :     Any       						=       FlaxHybridCLIPModule



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

   __A						:							Tuple      =						self.module_class(config=lowercase_					,      dtype=lowercase_					,      **lowercase_ )
   super().__init__(lowercase_					,      lowercase_					,      input_shape=lowercase_					,      seed=lowercase_					,      dtype=lowercase_ )



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

   __A						:							Any      =						jax.random.normal(lowercase_					,      input_shape[1] )

   __A						:							Tuple      =						jax.random.split(lowercase_ )
   __A						:							List[Any]      =						{'params': params_rng, 'dropout': dropout_rng}

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



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

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

   if token_type_ids is None:
    __A						:							List[Any]      =						jnp.zeros_like(lowercase_ )

   if attention_mask is None:
    __A						:							Optional[int]      =						jnp.ones_like(lowercase_ )

   # Handle any PRNG if needed
   __A						:							int      =						{}
   if dropout_rng is not None:
    __A						:							Dict      =						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     A_					(self	:		str					,      lowercase_	:		Optional[int]					,      lowercase_	:		Any=None					,      lowercase_	:		Optional[int]=None					,      lowercase_	:		List[Any]=None					,      lowercase_	:		dict = None					,      lowercase_	:		jax.random.PRNGKey = None					,      lowercase_	:		List[str]=False					,      )		->				Union[str, Any]:
   if position_ids is None:
    __A						:							Optional[Any]      =						jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_ ).shape[-1] )					,      input_ids.shape )

   if token_type_ids is None:
    __A						:							Optional[Any]      =						jnp.zeros_like(lowercase_ )

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

   # Handle any PRNG if needed
   __A						:							Dict      =						{}
   if dropout_rng is not None:
    __A						:							Tuple      =						dropout_rng

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

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

   __A						:							Any      =						{
       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						:							List[str]      =						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						:							int      =						AutoConfig.from_pretrained(lowercase_ )
     __A						:							int      =						text_config

    __A						:							Optional[Any]      =						FlaxAutoModel.from_pretrained(lowercase_					,      *lowercase_					,      **lowercase_ )

   __A						:							List[Any]      =						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						:							Optional[Any]      =						AutoConfig.from_pretrained(lowercase_ )
     __A						:							str      =						vision_config

    __A						:							Dict      =						FlaxAutoModel.from_pretrained(lowercase_					,      *lowercase_					,      **lowercase_ )

   # instantiate config with corresponding kwargs
   __A						:							str      =						kwargs.pop('dtype'					,      jnp.floataa )
   __A						:							Optional[Any]      =						HybridCLIPConfig.from_text_vision_configs(text_model.config					,      vision_model.config					,      **lowercase_ )

   # init model
   __A						:							int      =						cls(lowercase_					,      *lowercase_					,      dtype=lowercase_					,      **lowercase_ )

   if vision_config.model_type == "clip":
    __A						:							int      =						vision_model.params['vision_model']
    __A						:							List[Any]      =						vision_model.params['visual_projection']['kernel']
   else:
    __A						:							int      =						vision_model.params

   __A						:							Optional[int]      =						text_model.params

   return model










"""simple docstring"""




from __future__ import annotations

from collections import defaultdict
from dataclasses import dataclass


@dataclass
class 	a__		:
   _lowerCamelCase       				=					42
   _lowerCamelCase       				=					None
   _lowerCamelCase       				=					None




def 	lowercase__  (      )  ->      TreeNode:






      '''simple docstring'''


      return TreeNode(3						,   TreeNode(9					)						,   TreeNode(20						,   TreeNode(15					)						,   TreeNode(7					)					)					)


def 	lowercase__  (      _UpperCAmelCase					)  ->      list[int]:






      '''simple docstring'''


      def depth_first_search(
          _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase					) -> None:
            if not root:
                  return

            if depth == len(_UpperCAmelCase					):
                  right_view.append(root.val					)

            depth_first_search(root.right						,   depth + 1						,   _UpperCAmelCase					)
            depth_first_search(root.left						,   depth + 1						,   _UpperCAmelCase					)

      lowercase	:   list											= []
      if not root:
            return right_view

      depth_first_search(_UpperCAmelCase						,   0						,   _UpperCAmelCase					)
      return right_view


def 	lowercase__  (      _UpperCAmelCase					)  ->      list[int]:






      '''simple docstring'''


      def depth_first_search(
          _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase					) -> None:
            if not root:
                  return

            if depth == len(_UpperCAmelCase					):
                  left_view.append(root.val					)

            depth_first_search(root.left						,   depth + 1						,   _UpperCAmelCase					)
            depth_first_search(root.right						,   depth + 1						,   _UpperCAmelCase					)

      lowercase	:   list											= []
      if not root:
            return left_view

      depth_first_search(_UpperCAmelCase						,   0						,   _UpperCAmelCase					)
      return left_view


def 	lowercase__  (      _UpperCAmelCase					)  ->      list[int]:






      '''simple docstring'''


      def breadth_first_search(_UpperCAmelCase						,   _UpperCAmelCase					) -> None:
            lowercase	:   List[str]											= [(root, 0)]
            lowercase	:   List[Any]											= defaultdict(_UpperCAmelCase					)

            while queue:
                  lowercase	:   Union[str, Any]											= queue.pop(0					)
                  lowercase						,     lowercase	:   str											= first

                  lookup[hd].append(node.val					)

                  if node.left:
                        queue.append((node.left, hd - 1)					)
                  if node.right:
                        queue.append((node.right, hd + 1)					)

            for pair in sorted(lookup.items()						,   key=lambda _UpperCAmelCase					: each[0]					):
                  top_view.append(pair[1][0]					)

      lowercase	:   list											= []
      if not root:
            return top_view

      breadth_first_search(_UpperCAmelCase						,   _UpperCAmelCase					)
      return top_view


def 	lowercase__  (      _UpperCAmelCase					)  ->      list[int]:






      '''simple docstring'''


      from collections import defaultdict

      def breadth_first_search(_UpperCAmelCase						,   _UpperCAmelCase					) -> None:
            lowercase	:   Any											= [(root, 0)]
            lowercase	:   Dict											= defaultdict(_UpperCAmelCase					)

            while queue:
                  lowercase	:   str											= queue.pop(0					)
                  lowercase						,     lowercase	:   List[Any]											= first
                  lookup[hd].append(node.val					)

                  if node.left:
                        queue.append((node.left, hd - 1)					)
                  if node.right:
                        queue.append((node.right, hd + 1)					)

            for pair in sorted(lookup.items()						,   key=lambda _UpperCAmelCase					: each[0]					):
                  bottom_view.append(pair[1][-1]					)

      lowercase	:   list											= []
      if not root:
            return bottom_view

      breadth_first_search(_UpperCAmelCase						,   _UpperCAmelCase					)
      return bottom_view


if __name__ == "__main__":
     import doctest

     doctest.testmod()
