



"""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  ):
   _lowerCamelCase       				=					42
   _lowerCamelCase       				=					jnp.floataa


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

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

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

         lowercase	:   Optional[Any]											= text_module(lowerCAmelCase,  dtype=self.dtype      )
         lowercase	:   Dict											= vision_module(lowerCAmelCase,  dtype=self.dtype      )

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






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

         lowercase	:   Dict											= self.vision_model(
             pixel_values=lowerCAmelCase,  deterministic=lowerCAmelCase,  output_attentions=lowerCAmelCase,  output_hidden_states=lowerCAmelCase,  return_dict=lowerCAmelCase,  )

         lowercase	:   Union[str, Any]											= self.text_model(
             input_ids=lowerCAmelCase,  attention_mask=lowerCAmelCase,  token_type_ids=lowerCAmelCase,  position_ids=lowerCAmelCase,  deterministic=lowerCAmelCase,  output_attentions=lowerCAmelCase,  output_hidden_states=lowerCAmelCase,  return_dict=lowerCAmelCase,  )

         lowercase	:   Union[str, Any]											= vision_outputs[1]
         lowercase	:   str											= self.visual_projection(lowerCAmelCase      )

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

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

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




class 	a__		(    SCREAMING_SNAKE_CASE__  ):
   _lowerCamelCase       				=					HybridCLIPConfig
   _lowerCamelCase       				=					FlaxHybridCLIPModule


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

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


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

         lowercase	:   Any											= jax.random.normal(lowerCAmelCase,  input_shape[1]      )

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

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


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

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

         if token_type_ids is None:
               lowercase	:   List[Any]											= jnp.zeros_like(lowerCAmelCase      )

         if attention_mask is None:
               lowercase	:   Optional[int]											= jnp.ones_like(lowerCAmelCase      )

         # Handle any PRNG if needed
         lowercase	:   int											= {}
         if dropout_rng is not None:
               lowercase	:   Dict											= dropout_rng

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


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

         if token_type_ids is None:
               lowercase	:   Optional[Any]											= jnp.zeros_like(lowerCAmelCase      )

         if attention_mask is None:
               lowercase	:   str											= jnp.ones_like(lowerCAmelCase      )

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

         def _get_features(lowerCAmelCase   :				Union[str, Any],  lowerCAmelCase   :				int,  lowerCAmelCase   :				Any,  lowerCAmelCase   :				List[str],  lowerCAmelCase   :				Union[str, Any],  lowerCAmelCase   :				Optional[Any]      ):
               lowercase	:   List[str]											= module.text_model(
                   input_ids=lowerCAmelCase,  attention_mask=lowerCAmelCase,  position_ids=lowerCAmelCase,  token_type_ids=lowerCAmelCase,  deterministic=lowerCAmelCase,  )
               lowercase	:   int											= text_outputs[1]
               lowercase	:   List[str]											= module.text_projection(lowerCAmelCase      )
               return text_features

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


   def 							lowercase					(				self   :				int,  lowerCAmelCase   :				Tuple,  lowerCAmelCase   :				dict = None,  lowerCAmelCase   :				jax.random.PRNGKey = None,  lowerCAmelCase   :				Union[str, Any]=False      )     ->							Union[str, Any]:
         lowercase	:   List[Any]											= {}
         if dropout_rng is not None:
               lowercase	:   int											= dropout_rng

         def _get_features(lowerCAmelCase   :				Optional[Any],  lowerCAmelCase   :				List[Any],  lowerCAmelCase   :				Tuple      ):
               lowercase	:   Dict											= module.vision_model(pixel_values=lowerCAmelCase,  deterministic=lowerCAmelCase      )
               lowercase	:   Optional[Any]											= vision_outputs[1]  # pooled_output
               lowercase	:   Optional[Any]											= module.visual_projection(lowerCAmelCase      )
               return image_features

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






   @classmethod
   def 							lowercase					(				cls   :				Optional[Any],  lowerCAmelCase   :				str = None,  lowerCAmelCase   :				str = None,  *lowerCAmelCase   :				str,  **lowerCAmelCase   :				Optional[Any],  )     ->							FlaxPreTrainedModel:
         lowercase	:   Union[str, Any]											= {
             argument[len('text_'      ) :]: value for argument, value in kwargs.items() if argument.startswith('text_'      )
         }

         lowercase	:   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
         lowercase	:   List[str]											= kwargs_text.pop('model',  lowerCAmelCase      )
         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

                     lowercase	:   int											= AutoConfig.from_pretrained(lowerCAmelCase      )
                     lowercase	:   int											= text_config

               lowercase	:   Optional[Any]											= FlaxAutoModel.from_pretrained(lowerCAmelCase,  *lowerCAmelCase,  **lowerCAmelCase      )

         lowercase	:   List[Any]											= kwargs_vision.pop('model',  lowerCAmelCase      )
         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

                     lowercase	:   Optional[Any]											= AutoConfig.from_pretrained(lowerCAmelCase      )
                     lowercase	:   str											= vision_config

               lowercase	:   Dict											= FlaxAutoModel.from_pretrained(lowerCAmelCase,  *lowerCAmelCase,  **lowerCAmelCase      )

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

         # init model
         lowercase	:   int											= cls(lowerCAmelCase,  *lowerCAmelCase,  dtype=lowerCAmelCase,  **lowerCAmelCase      )

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

         lowercase	:   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()
