





def   lowerCamelCase_		(       _UpperCamelCase				)	->		bool:






				"""simple docstring"""


				snake_case_		:					List[str]			    =						[list(_UpperCamelCase				) for row in matrix]
				if matrix and isinstance(_UpperCamelCase				,       _UpperCamelCase				):
								if isinstance(matrix[0]				,       _UpperCamelCase				):
												snake_case_		:					Tuple			    =						0
												for row in matrix:
																if prev_len == 0:
																				snake_case_		:					str			    =						len(_UpperCamelCase				)
																				snake_case_		:					Any			    =						True
																else:
																				snake_case_		:					Optional[Any]			    =						prev_len == len(_UpperCamelCase				)
								else:
												snake_case_		:					Any			    =						True
				else:
								snake_case_		:					Optional[int]			    =						False

				return result






def   lowerCamelCase_		(       _UpperCamelCase				)	->		None:






				"""simple docstring"""


				if check_matrix(_UpperCamelCase				) and len(_UpperCamelCase				) > 0:
								snake_case_		:					str			    =						[list(_UpperCamelCase				) for row in a]
								snake_case_		:					List[Any]			    =						len(_UpperCamelCase				)
								if isinstance(a[0]				,       _UpperCamelCase				):
												snake_case_		:					Dict			    =						len(a[0]				)
								else:
												for dat in a:
																print(_UpperCamelCase				)
												return

								# horizotal printing increasing
								for i in range(0				,       _UpperCamelCase				):
												print(a[0][i]				)
								# vertical printing down
								for i in range(1				,       _UpperCamelCase				):
												print(a[i][mat_col - 1]				)
								# horizotal printing decreasing
								if mat_row > 1:
												for i in range(mat_col - 2				,       -1				,       -1				):
																print(a[mat_row - 1][i]				)
        # vertical printing up
								for i in range(mat_row - 2				,       0				,       -1				):
												print(a[i][0]				)
								snake_case_		:					Optional[int]			    =						[row[1 : mat_col - 1] for row in a[1 : mat_row - 1]]
								if len(_UpperCamelCase				) > 0:
												spiral_print_clockwise(_UpperCamelCase				)
								else:
												return
				else:
								print('''Not a valid matrix'''				)
								return






def   lowerCamelCase_		(       _UpperCamelCase				)	->		list[int]:






				"""simple docstring"""


				if matrix:
								return list(matrix.pop(0				)				) + spiral_traversal(list(zip(*_UpperCamelCase				)				)[::-1]				)
				else:
								return []


# driver code
if __name__ == "__main__":
	import doctest

	doctest.testmod()

	lowerCAmelCase_												=       [[1, 2, 3, 4], [5, 6, 7, 8], [9, 1_0, 1_1, 1_2]]
	spiral_print_clockwise(a)







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       __lowerCAmelCase (    nn.Module  ):
			lowerCamelCase_  :  HybridCLIPConfig
			lowerCamelCase_  :  jnp.dtype     =   jnp.floataa






			def     lowerCamelCase						(self					)  ->     int:


							'''simple docstring'''



							snake_case_		:					Any			    =						self.config.text_config
							snake_case_		:					Any			    =						self.config.vision_config

							snake_case_		:					Union[str, Any]			    =						self.config.projection_dim
							snake_case_		:					Dict			    =						text_config.hidden_size
							snake_case_		:					Union[str, Any]			    =						vision_config.hidden_size

							snake_case_		:					Union[str, Any]			    =						FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
							snake_case_		:					Dict			    =						FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__      ,							__magic_name__					).module_class

							snake_case_		:					Optional[Any]			    =						text_module(__magic_name__      ,							dtype=self.dtype					)
							snake_case_		:					Dict			    =						vision_module(__magic_name__      ,							dtype=self.dtype					)

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






			def __call__(self      ,							__magic_name__=None      ,							__magic_name__=None      ,							__magic_name__=None      ,							__magic_name__=None      ,							__magic_name__=None      ,							__magic_name__ = True      ,							__magic_name__=None      ,							__magic_name__=None      ,							__magic_name__=None      ,							)  ->     Any:


							'''simple docstring'''



							snake_case_		:					Tuple			    =						return_dict if return_dict is not None else self.config.return_dict

							snake_case_		:					Tuple			    =						self.vision_model(
							    pixel_values=__magic_name__      ,							deterministic=__magic_name__      ,							output_attentions=__magic_name__      ,							output_hidden_states=__magic_name__      ,							return_dict=__magic_name__      ,							)

							snake_case_		:					Tuple			    =						self.text_model(
							    input_ids=__magic_name__      ,							attention_mask=__magic_name__      ,							token_type_ids=__magic_name__      ,							position_ids=__magic_name__      ,							deterministic=__magic_name__      ,							output_attentions=__magic_name__      ,							output_hidden_states=__magic_name__      ,							return_dict=__magic_name__      ,							)

							snake_case_		:					Dict			    =						vision_outputs[1]
							snake_case_		:					Optional[Any]			    =						self.visual_projection(__magic_name__					)

							snake_case_		:					List[str]			    =						text_outputs[1]
							snake_case_		:					Union[str, Any]			    =						self.text_projection(__magic_name__					)

							# normalized features
							snake_case_		:					List[str]			    =						image_embeds / jnp.linalg.norm(__magic_name__      ,							axis=-1      ,							keepdims=__magic_name__					)
							snake_case_		:					Any			    =						text_embeds / jnp.linalg.norm(__magic_name__      ,							axis=-1      ,							keepdims=__magic_name__					)

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




class       __lowerCAmelCase (    _a  ):
			lowerCamelCase_  :  Dict     =   HybridCLIPConfig
			lowerCamelCase_  :  int     =   FlaxHybridCLIPModule






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


							'''simple docstring'''



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

							snake_case_		:					Union[str, Any]			    =						self.module_class(config=__magic_name__      ,							dtype=__magic_name__      ,							**__magic_name__					)
							super().__init__(__magic_name__      ,							__magic_name__      ,							input_shape=__magic_name__      ,							seed=__magic_name__      ,							dtype=__magic_name__					)






			def     lowerCamelCase						(self      ,							__magic_name__      ,							__magic_name__      ,							__magic_name__ = None					)  ->     FrozenDict:


							'''simple docstring'''



							snake_case_		:					List[Any]			    =						jnp.zeros(input_shape[0]      ,							dtype='''i4'''					)
							snake_case_		:					Optional[Any]			    =						jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__magic_name__					).shape[-1]					)      ,							input_shape[0]					)
							snake_case_		:					Union[str, Any]			    =						jnp.ones_like(__magic_name__					)
							snake_case_		:					List[Any]			    =						jnp.ones_like(__magic_name__					)

							snake_case_		:					Union[str, Any]			    =						jax.random.normal(__magic_name__      ,							input_shape[1]					)

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

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






			def __call__(self      ,							__magic_name__      ,							__magic_name__      ,							__magic_name__=None      ,							__magic_name__=None      ,							__magic_name__=None      ,							__magic_name__ = None      ,							__magic_name__ = None      ,							__magic_name__ = False      ,							__magic_name__ = None      ,							__magic_name__ = None      ,							__magic_name__ = None      ,							)  ->     Any:


							'''simple docstring'''



							snake_case_		:					Optional[int]			    =						output_attentions if output_attentions is not None else self.config.output_attentions
							snake_case_		:					Tuple			    =						(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							snake_case_		:					List[Any]			    =						return_dict if return_dict is not None else self.config.return_dict

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

							if token_type_ids is None:
											snake_case_		:					Tuple			    =						jnp.zeros_like(__magic_name__					)

							if attention_mask is None:
											snake_case_		:					Optional[Any]			    =						jnp.ones_like(__magic_name__					)

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

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






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


							'''simple docstring'''



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

							if token_type_ids is None:
											snake_case_		:					List[str]			    =						jnp.zeros_like(__magic_name__					)

							if attention_mask is None:
											snake_case_		:					Union[str, Any]			    =						jnp.ones_like(__magic_name__					)

							# Handle any PRNG if needed
							snake_case_		:					Optional[int]			    =						{}
							if dropout_rng is not None:
											snake_case_		:					int			    =						dropout_rng

							def _get_features(__magic_name__      ,							__magic_name__      ,							__magic_name__      ,							__magic_name__      ,							__magic_name__      ,							__magic_name__					):
											snake_case_		:					int			    =						module.text_model(
											    input_ids=__magic_name__      ,							attention_mask=__magic_name__      ,							position_ids=__magic_name__      ,							token_type_ids=__magic_name__      ,							deterministic=__magic_name__      ,							)
											snake_case_		:					Optional[int]			    =						text_outputs[1]
											snake_case_		:					int			    =						module.text_projection(__magic_name__					)
											return text_features

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






			def     lowerCamelCase						(self      ,							__magic_name__      ,							__magic_name__ = None      ,							__magic_name__ = None      ,							__magic_name__=False					)  ->     Dict:


							'''simple docstring'''



							snake_case_		:					Any			    =						{}
							if dropout_rng is not None:
											snake_case_		:					Optional[int]			    =						dropout_rng

							def _get_features(__magic_name__      ,							__magic_name__      ,							__magic_name__					):
											snake_case_		:					Dict			    =						module.vision_model(pixel_values=__magic_name__      ,							deterministic=__magic_name__					)
											snake_case_		:					str			    =						vision_outputs[1]  # pooled_output
											snake_case_		:					str			    =						module.visual_projection(__magic_name__					)
											return image_features

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






			@classmethod
			def     lowerCamelCase						(cls      ,							__magic_name__ = None      ,							__magic_name__ = None      ,							*__magic_name__      ,							**__magic_name__      ,							)  ->     FlaxPreTrainedModel:


							'''simple docstring'''



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

							snake_case_		:					Dict			    =						{
							    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_		:					str			    =						kwargs_text.pop('''model'''      ,							__magic_name__					)
							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_		:					Dict			    =						AutoConfig.from_pretrained(__magic_name__					)
															snake_case_		:					int			    =						text_config

											snake_case_		:					Optional[int]			    =						FlaxAutoModel.from_pretrained(__magic_name__      ,							*__magic_name__      ,							**__magic_name__					)

							snake_case_		:					Any			    =						kwargs_vision.pop('''model'''      ,							__magic_name__					)
							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(__magic_name__					)
															snake_case_		:					List[str]			    =						vision_config

											snake_case_		:					List[Any]			    =						FlaxAutoModel.from_pretrained(__magic_name__      ,							*__magic_name__      ,							**__magic_name__					)

							# instantiate config with corresponding kwargs
							snake_case_		:					Dict			    =						kwargs.pop('''dtype'''      ,							jnp.floataa					)
							snake_case_		:					str			    =						HybridCLIPConfig.from_text_vision_configs(text_model.config      ,							vision_model.config      ,							**__magic_name__					)

							# init model
							snake_case_		:					int			    =						cls(__magic_name__      ,							*__magic_name__      ,							dtype=__magic_name__      ,							**__magic_name__					)

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

							snake_case_		:					Union[str, Any]			    =						text_model.params

							return model
