





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


lowerCAmelCase_   =   logging.get_logger(__name__)

class 				A    (nn.Module				):
      _SCREAMING_SNAKE_CASE					=							42
      _SCREAMING_SNAKE_CASE					=							jnp.floataa



      def    __a (					self					)      ->   Tuple:



             '''simple docstring'''

             _snake_case      :							Optional[Any]     				=						self.config.text_config
             _snake_case      :							Tuple     				=						self.config.vision_config

             _snake_case      :							List[Any]     				=						self.config.projection_dim
             _snake_case      :							str     				=						text_config.hidden_size
             _snake_case      :							Optional[Any]     				=						vision_config.hidden_size

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

             _snake_case      :							Optional[int]     				=						text_module(lowercase_     ,  dtype=self.dtype					)
             _snake_case      :							Dict     				=						vision_module(lowercase_     ,  dtype=self.dtype					)

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



      def __call__(					self     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_ = True     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_=None     ,  )      ->   Optional[int]:



             '''simple docstring'''

             _snake_case      :							Dict     				=						return_dict if return_dict is not None else self.config.return_dict

             _snake_case      :							Union[str, Any]     				=						self.vision_model(
                 pixel_values=lowercase_     ,  deterministic=lowercase_     ,  output_attentions=lowercase_     ,  output_hidden_states=lowercase_     ,  return_dict=lowercase_     ,  )

             _snake_case      :							str     				=						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_     ,  )

             _snake_case      :							Tuple     				=						vision_outputs[1]
             _snake_case      :							Dict     				=						self.visual_projection(lowercase_					)

             _snake_case      :							str     				=						text_outputs[1]
             _snake_case      :							List[str]     				=						self.text_projection(lowercase_					)

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

             # cosine similarity as logits
             _snake_case      :							Dict     				=						jnp.exp(self.logit_scale					)
             _snake_case      :							Union[str, Any]     				=						jnp.matmul(lowercase_     ,  image_embeds.T					) * logit_scale
             _snake_case      :							Tuple     				=						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    (__UpperCAmelCase				):
      _SCREAMING_SNAKE_CASE					=							HybridCLIPConfig
      _SCREAMING_SNAKE_CASE					=							FlaxHybridCLIPModule



      def __init__(					self     ,  lowercase_     ,  lowercase_ = None     ,  lowercase_ = 0     ,  lowercase_ = jnp.floataa     ,  **lowercase_     ,  )      ->   str:



             '''simple docstring'''

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

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



      def    __a (					self     ,  lowercase_     ,  lowercase_     ,  lowercase_ = None					)      ->   FrozenDict:



             '''simple docstring'''

             _snake_case      :							Union[str, Any]     				=						jnp.zeros(input_shape[0]     ,  dtype='''i4'''					)
             _snake_case      :							int     				=						jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_					).shape[-1]					)     ,  input_shape[0]					)
             _snake_case      :							Union[str, Any]     				=						jnp.ones_like(lowercase_					)
             _snake_case      :							Tuple     				=						jnp.ones_like(lowercase_					)

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

             _snake_case      :							Optional[int]     				=						jax.random.split(lowercase_					)
             _snake_case      :							str     				=						{'''params''': params_rng, '''dropout''': dropout_rng}

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



      def __call__(					self     ,  lowercase_     ,  lowercase_     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_ = None     ,  lowercase_ = None     ,  lowercase_ = False     ,  lowercase_ = None     ,  lowercase_ = None     ,  lowercase_ = None     ,  )      ->   Union[str, Any]:



             '''simple docstring'''

             _snake_case      :							Union[str, Any]     				=						output_attentions if output_attentions is not None else self.config.output_attentions
             _snake_case      :							int     				=						(
                 output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
             )
             _snake_case      :							Union[str, Any]     				=						return_dict if return_dict is not None else self.config.return_dict

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

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

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

             # Handle any PRNG if needed
             _snake_case      :							List[str]     				=						{}
             if dropout_rng is not None:
                    _snake_case      :							Union[str, Any]     				=						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     ,  lowercase_     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_=None     ,  lowercase_ = None     ,  lowercase_ = None     ,  lowercase_=False     ,  )      ->   Union[str, Any]:



             '''simple docstring'''

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

             if token_type_ids is None:
                    _snake_case      :							List[str]     				=						jnp.zeros_like(lowercase_					)

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

             # Handle any PRNG if needed
             _snake_case      :							List[str]     				=						{}
             if dropout_rng is not None:
                    _snake_case      :							List[Any]     				=						dropout_rng

             def _get_features(lowercase_     ,  lowercase_     ,  lowercase_     ,  lowercase_     ,  lowercase_     ,  lowercase_					):
                    _snake_case      :							Optional[Any]     				=						module.text_model(
                        input_ids=lowercase_     ,  attention_mask=lowercase_     ,  position_ids=lowercase_     ,  token_type_ids=lowercase_     ,  deterministic=lowercase_     ,  )
                    _snake_case      :							Optional[int]     				=						text_outputs[1]
                    _snake_case      :							Optional[Any]     				=						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     ,  lowercase_     ,  lowercase_ = None     ,  lowercase_ = None     ,  lowercase_=False					)      ->   List[Any]:



             '''simple docstring'''

             _snake_case      :							Union[str, Any]     				=						{}
             if dropout_rng is not None:
                    _snake_case      :							Dict     				=						dropout_rng

             def _get_features(lowercase_     ,  lowercase_     ,  lowercase_					):
                    _snake_case      :							Optional[Any]     				=						module.vision_model(pixel_values=lowercase_     ,  deterministic=lowercase_					)
                    _snake_case      :							Tuple     				=						vision_outputs[1]  # pooled_output
                    _snake_case      :							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     ,  lowercase_ = None     ,  lowercase_ = None     ,  *lowercase_     ,  **lowercase_     ,  )      ->   FlaxPreTrainedModel:



             '''simple docstring'''

             _snake_case      :							int     				=						{
                 argument[len('''text_'''					) :]: value for argument, value in kwargs.items() if argument.startswith('''text_'''					)
             }

             _snake_case      :							str     				=						{
                 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
             _snake_case      :							Optional[Any]     				=						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

                           _snake_case      :							List[Any]     				=						AutoConfig.from_pretrained(lowercase_					)
                           _snake_case      :							Union[str, Any]     				=						text_config

                    _snake_case      :							Optional[int]     				=						FlaxAutoModel.from_pretrained(lowercase_     ,  *lowercase_     ,  **lowercase_					)

             _snake_case      :							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

                           _snake_case      :							Optional[int]     				=						AutoConfig.from_pretrained(lowercase_					)
                           _snake_case      :							Dict     				=						vision_config

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

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

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

             if vision_config.model_type == "clip":
                    _snake_case      :							Union[str, Any]     				=						vision_model.params['''vision_model''']
                    _snake_case      :							Union[str, Any]     				=						vision_model.params['''visual_projection''']['''kernel''']
             else:
                    _snake_case      :							str     				=						vision_model.params

             _snake_case      :							Optional[Any]     				=						text_model.params

             return model













from __future__ import annotations

class 				A    :



      def __init__(					self     ,  lowercase_					)      ->   None:



             '''simple docstring'''

             _snake_case      :							Optional[Any]     				=						vertices
             _snake_case      :							int     				=						[[0] * vertices for _ in range(lowercase_					)]



      def    __a (					self     ,  lowercase_					)      ->   None:



             '''simple docstring'''

             print('''Vertex \t Distance from Source'''					)
             for vertex in range(self.vertices					):
                    print(lowercase_     ,  '''\t\t'''     ,  distances_from_source[vertex]					)



      def    __a (					self     ,  lowercase_     ,  lowercase_					)      ->   int:



             '''simple docstring'''

             _snake_case      :							List[Any]     				=						1E7
             _snake_case      :							Tuple     				=						0

             # Search not nearest vertex not in the shortest path tree
             for vertex in range(self.vertices					):
                    if distances_from_source[vertex] < minimum and visited[vertex] is False:
                           _snake_case      :							Dict     				=						distances_from_source[vertex]
                           _snake_case      :							int     				=						vertex
             return min_index



      def    __a (					self     ,  lowercase_					)      ->   None:



             '''simple docstring'''

             _snake_case      :							List[Any]     				=						[int(1E7					)] * self.vertices  # distances from the source
             _snake_case      :							List[Any]     				=						0
             _snake_case      :							List[str]     				=						[False] * self.vertices

             for _ in range(self.vertices					):
                    _snake_case      :							List[Any]     				=						self.minimum_distance(lowercase_     ,  lowercase_					)
                    _snake_case      :							List[str]     				=						True

                    # Update dist value of the adjacent vertices
                    # of the picked vertex only if the current
                    # distance is greater than new distance and
                    # the vertex in not in the shortest path tree
                    for v in range(self.vertices					):
                           if (
                               self.graph[u][v] > 0
                               and visited[v] is False
                               and distances[v] > distances[u] + self.graph[u][v]
                           ):
                                  _snake_case      :							Any     				=						distances[u] + self.graph[u][v]

             self.print_solution(lowercase_					)


if __name__ == "__main__":
   lowerCAmelCase_   =   Graph(9)
   lowerCAmelCase_   =   [
       [0, 4, 0, 0, 0, 0, 0, 8, 0],
       [4, 0, 8, 0, 0, 0, 0, 1_1, 0],
       [0, 8, 0, 7, 0, 4, 0, 0, 2],
       [0, 0, 7, 0, 9, 1_4, 0, 0, 0],
       [0, 0, 0, 9, 0, 1_0, 0, 0, 0],
       [0, 0, 4, 1_4, 1_0, 0, 2, 0, 0],
       [0, 0, 0, 0, 0, 2, 0, 1, 6],
       [8, 1_1, 0, 0, 0, 0, 1, 0, 7],
       [0, 0, 2, 0, 0, 0, 6, 7, 0],
   ]
   graph.dijkstra(0)






