

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


A__							=				logging.get_logger(__name__)

class 					__UpperCamelCase					(       nn.Module      ):
	_lowercase	: HybridCLIPConfig
	_lowercase	: jnp.dtype												=	jnp.floataa







	def 						_SCREAMING_SNAKE_CASE				( self:			Union[str, Any]   ):

						'''simple docstring'''





						__magic_name__				=     self.config.text_config
						__magic_name__				=     self.config.vision_config

						__magic_name__				=     self.config.projection_dim
						__magic_name__				=     text_config.hidden_size
						__magic_name__				=     vision_config.hidden_size

						__magic_name__				=     FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
						__magic_name__				=     FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__	,			_SCREAMING_SNAKE_CASE   ).module_class

						__magic_name__				=     text_module(_SCREAMING_SNAKE_CASE	,			dtype=self.dtype   )
						__magic_name__				=     vision_module(_SCREAMING_SNAKE_CASE	,			dtype=self.dtype   )

						__magic_name__				=     nn.Dense(
						    self.projection_dim	,			dtype=self.dtype	,			kernel_init=jax.nn.initializers.normal(0.02   )	,			use_bias=_SCREAMING_SNAKE_CASE	,			)
						__magic_name__				=     nn.Dense(
						    self.projection_dim	,			dtype=self.dtype	,			kernel_init=jax.nn.initializers.normal(0.02   )	,			use_bias=_SCREAMING_SNAKE_CASE	,			)
						__magic_name__				=     self.param('logit_scale'	,			jax.nn.initializers.ones	,			[]   )







	def __call__( self:			Dict	,			__UpperCamelCase:			Any=None	,			__UpperCamelCase:			List[Any]=None	,			__UpperCamelCase:			str=None	,			__UpperCamelCase:			str=None	,			__UpperCamelCase:			str=None	,			__UpperCamelCase:			List[str] = True	,			__UpperCamelCase:			List[str]=None	,			__UpperCamelCase:			Union[str, Any]=None	,			__UpperCamelCase:			Dict=None	,			):

						'''simple docstring'''





						__magic_name__				=     return_dict if return_dict is not None else self.config.return_dict

						__magic_name__				=     self.vision_model(
						    pixel_values=_SCREAMING_SNAKE_CASE	,			deterministic=_SCREAMING_SNAKE_CASE	,			output_attentions=_SCREAMING_SNAKE_CASE	,			output_hidden_states=_SCREAMING_SNAKE_CASE	,			return_dict=_SCREAMING_SNAKE_CASE	,			)

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

						__magic_name__				=     vision_outputs[1]
						__magic_name__				=     self.visual_projection(_SCREAMING_SNAKE_CASE   )

						__magic_name__				=     text_outputs[1]
						__magic_name__				=     self.text_projection(_SCREAMING_SNAKE_CASE   )

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

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




class 					__UpperCamelCase					(       _UpperCamelCase      ):
	_lowercase	: int												=	HybridCLIPConfig
	_lowercase	: Optional[Any]												=	FlaxHybridCLIPModule







	def __init__( self:			Optional[Any]	,			__UpperCamelCase:			Tuple	,			__UpperCamelCase:			Dict = None	,			__UpperCamelCase:			List[Any] = 0	,			__UpperCamelCase:			int = jnp.floataa	,			**__UpperCamelCase:			str	,			):

						'''simple docstring'''





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

						__magic_name__				=     self.module_class(config=_SCREAMING_SNAKE_CASE	,			dtype=_SCREAMING_SNAKE_CASE	,			**_SCREAMING_SNAKE_CASE   )
						super().__init__(_SCREAMING_SNAKE_CASE	,			_SCREAMING_SNAKE_CASE	,			input_shape=_SCREAMING_SNAKE_CASE	,			seed=_SCREAMING_SNAKE_CASE	,			dtype=_SCREAMING_SNAKE_CASE   )







	def 						_SCREAMING_SNAKE_CASE				( self:			List[Any]	,			__UpperCamelCase:			List[Any]	,			__UpperCamelCase:			Optional[Any]	,			__UpperCamelCase:			Optional[Any] = None   ):

						'''simple docstring'''





						__magic_name__				=     jnp.zeros(input_shape[0]	,			dtype='i4'   )
						__magic_name__				=     jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_SCREAMING_SNAKE_CASE   ).shape[-1]   )	,			input_shape[0]   )
						__magic_name__				=     jnp.ones_like(_SCREAMING_SNAKE_CASE   )
						__magic_name__				=     jnp.ones_like(_SCREAMING_SNAKE_CASE   )

						__magic_name__				=     jax.random.normal(_SCREAMING_SNAKE_CASE	,			input_shape[1]   )

						__magic_name__,						__magic_name__				=     jax.random.split(_SCREAMING_SNAKE_CASE   )
						__magic_name__				=     {'params': params_rng, 'dropout': dropout_rng}

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







	def __call__( self:			Dict	,			__UpperCamelCase:			Dict	,			__UpperCamelCase:			int	,			__UpperCamelCase:			int=None	,			__UpperCamelCase:			List[Any]=None	,			__UpperCamelCase:			Dict=None	,			__UpperCamelCase:			str = None	,			__UpperCamelCase:			Tuple = None	,			__UpperCamelCase:			Any = False	,			__UpperCamelCase:			Optional[int] = None	,			__UpperCamelCase:			str = None	,			__UpperCamelCase:			List[str] = None	,			):

						'''simple docstring'''





						__magic_name__				=     output_attentions if output_attentions is not None else self.config.output_attentions
						__magic_name__				=     (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						__magic_name__				=     return_dict if return_dict is not None else self.config.return_dict

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

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

						if attention_mask is None:
											__magic_name__				=     jnp.ones_like(_SCREAMING_SNAKE_CASE   )

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

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







	def 						_SCREAMING_SNAKE_CASE				( self:			Union[str, Any]	,			__UpperCamelCase:			Optional[int]	,			__UpperCamelCase:			str=None	,			__UpperCamelCase:			str=None	,			__UpperCamelCase:			Union[str, Any]=None	,			__UpperCamelCase:			List[str] = None	,			__UpperCamelCase:			Optional[Any] = None	,			__UpperCamelCase:			Any=False	,			):

						'''simple docstring'''





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

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

						if attention_mask is None:
											__magic_name__				=     jnp.ones_like(_SCREAMING_SNAKE_CASE   )

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

						def _get_features(__UpperCamelCase:			Optional[Any]	,			__UpperCamelCase:			int	,			__UpperCamelCase:			Optional[int]	,			__UpperCamelCase:			List[str]	,			__UpperCamelCase:			Any	,			__UpperCamelCase:			Dict   ):
											__magic_name__				=     module.text_model(
											    input_ids=_SCREAMING_SNAKE_CASE	,			attention_mask=_SCREAMING_SNAKE_CASE	,			position_ids=_SCREAMING_SNAKE_CASE	,			token_type_ids=_SCREAMING_SNAKE_CASE	,			deterministic=_SCREAMING_SNAKE_CASE	,			)
											__magic_name__				=     text_outputs[1]
											__magic_name__				=     module.text_projection(_SCREAMING_SNAKE_CASE   )
											return text_features

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







	def 						_SCREAMING_SNAKE_CASE				( self:			int	,			__UpperCamelCase:			Tuple	,			__UpperCamelCase:			Optional[Any] = None	,			__UpperCamelCase:			Dict = None	,			__UpperCamelCase:			Dict=False   ):

						'''simple docstring'''





						__magic_name__				=     {}
						if dropout_rng is not None:
											__magic_name__				=     dropout_rng

						def _get_features(__UpperCamelCase:			Union[str, Any]	,			__UpperCamelCase:			Union[str, Any]	,			__UpperCamelCase:			Any   ):
											__magic_name__				=     module.vision_model(pixel_values=_SCREAMING_SNAKE_CASE	,			deterministic=_SCREAMING_SNAKE_CASE   )
											__magic_name__				=     vision_outputs[1]  # pooled_output
											__magic_name__				=     module.visual_projection(_SCREAMING_SNAKE_CASE   )
											return image_features

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







	@classmethod
	def 						_SCREAMING_SNAKE_CASE				( cls:			int	,			__UpperCamelCase:			List[str] = None	,			__UpperCamelCase:			Union[str, Any] = None	,			*__UpperCamelCase:			Tuple	,			**__UpperCamelCase:			Optional[int]	,			):

						'''simple docstring'''





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

						__magic_name__				=     {
						    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
						__magic_name__				=     kwargs_text.pop('model'	,			_SCREAMING_SNAKE_CASE   )
						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

																__magic_name__				=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE   )
																__magic_name__				=     text_config

											__magic_name__				=     FlaxAutoModel.from_pretrained(_SCREAMING_SNAKE_CASE	,			*_SCREAMING_SNAKE_CASE	,			**_SCREAMING_SNAKE_CASE   )

						__magic_name__				=     kwargs_vision.pop('model'	,			_SCREAMING_SNAKE_CASE   )
						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

																__magic_name__				=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE   )
																__magic_name__				=     vision_config

											__magic_name__				=     FlaxAutoModel.from_pretrained(_SCREAMING_SNAKE_CASE	,			*_SCREAMING_SNAKE_CASE	,			**_SCREAMING_SNAKE_CASE   )

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

						# init model
						__magic_name__				=     cls(_SCREAMING_SNAKE_CASE	,			*_SCREAMING_SNAKE_CASE	,			dtype=_SCREAMING_SNAKE_CASE	,			**_SCREAMING_SNAKE_CASE   )

						if vision_config.model_type == "clip":
											__magic_name__				=     vision_model.params['vision_model']
											__magic_name__				=     vision_model.params['visual_projection']['kernel']
						else:
											__magic_name__				=     vision_model.params

						__magic_name__				=     text_model.params

						return model












"""simple docstring"""



from typing import Any, Dict, List, Mapping

import numpy as np
import torch

from ...utils import is_cython_available, requires_backends


if is_cython_available():
						import pyximport

						pyximport.install(setup_args={"include_dirs": np.get_include()})
						from . import algos_graphormer  # noqa E402




def     lowerCamelCase_					(	__A	:		List[str]      ,						__A	:		int = 512      ):
					lowerCamelCase__					=x.shape[1] if len(x.shape      ) > 1 else 1
					lowerCamelCase__					=1 + np.arange(0      ,						feature_num * offset      ,						__A      ,						dtype=np.intaa      )
					lowerCamelCase__					=x + feature_offset
					return x




def     lowerCamelCase_					(	__A	:		str      ,						__A	:		Optional[Any]=True      ):
					requires_backends(__A      ,						['cython']      )

					if keep_features and "edge_attr" in item.keys():  # edge_attr
										lowerCamelCase__					=np.asarray(item['edge_attr']      ,						dtype=np.intaa      )
					else:
										lowerCamelCase__					=np.ones((len(item['edge_index'][0]      ), 1)      ,						dtype=np.intaa      )  # same embedding for all

					if keep_features and "node_feat" in item.keys():  # input_nodes
										lowerCamelCase__					=np.asarray(item['node_feat']      ,						dtype=np.intaa      )
					else:
										lowerCamelCase__					=np.ones((item['num_nodes'], 1)      ,						dtype=np.intaa      )  # same embedding for all

					lowerCamelCase__					=np.asarray(item['edge_index']      ,						dtype=np.intaa      )

					lowerCamelCase__					=convert_to_single_emb(__A      ) + 1
					lowerCamelCase__					=item['num_nodes']

					if len(edge_attr.shape      ) == 1:
										lowerCamelCase__					=edge_attr[:, None]
					lowerCamelCase__					=np.zeros([num_nodes, num_nodes, edge_attr.shape[-1]]      ,						dtype=np.intaa      )
					lowerCamelCase__					=convert_to_single_emb(__A      ) + 1

					# node adj matrix [num_nodes, num_nodes] bool
					lowerCamelCase__					=np.zeros([num_nodes, num_nodes]      ,						dtype=__A      )
					lowerCamelCase__					=True

					lowerCamelCase__					,  lowerCamelCase__					=algos_graphormer.floyd_warshall(__A      )
					lowerCamelCase__					=np.amax(__A      )

					lowerCamelCase__					=algos_graphormer.gen_edge_input(__A      ,						__A      ,						__A      )
					lowerCamelCase__					=np.zeros([num_nodes + 1, num_nodes + 1]      ,						dtype=np.single      )  # with graph token

					# combine
					lowerCamelCase__					=input_nodes + 1  # we shift all indices by one for padding
					lowerCamelCase__					=attn_bias
					lowerCamelCase__					=attn_edge_type
					lowerCamelCase__					=shortest_path_result.astype(np.intaa      ) + 1  # we shift all indices by one for padding
					lowerCamelCase__					=np.sum(__A      ,						axis=1      ).reshape(-1      ) + 1  # we shift all indices by one for padding
					lowerCamelCase__					=item['in_degree']  # for undirected graph
					lowerCamelCase__					=input_edges + 1  # we shift all indices by one for padding
					if "labels" not in item:
										lowerCamelCase__					=item['y']

					return item




class 							__lowercase:






						def __init__(       self     ,    _SCREAMING_SNAKE_CASE=20     ,    _SCREAMING_SNAKE_CASE=False)      ->	Any:




											"""simple docstring"""





											if not is_cython_available():
																raise ImportError('Graphormer preprocessing needs Cython (pyximport)')

											lowerCamelCase__					=spatial_pos_max
											lowerCamelCase__					=on_the_fly_processing






						def __call__(       self     ,    _SCREAMING_SNAKE_CASE)      ->	Dict[str, Any]:




											"""simple docstring"""





											if self.on_the_fly_processing:
																lowerCamelCase__					=[preprocess_item(_SCREAMING_SNAKE_CASE) for i in features]

											if not isinstance(features[0]     ,    _SCREAMING_SNAKE_CASE):
																lowerCamelCase__					=[vars(_SCREAMING_SNAKE_CASE) for f in features]
											lowerCamelCase__					={}

											lowerCamelCase__					=max(len(i['input_nodes']) for i in features)
											lowerCamelCase__					=len(features[0]['input_nodes'][0])
											lowerCamelCase__					=len(features[0]['attn_edge_type'][0][0])
											lowerCamelCase__					=max(len(i['input_edges'][0][0]) for i in features)
											lowerCamelCase__					=len(features[0]['input_edges'][0][0][0])
											lowerCamelCase__					=len(_SCREAMING_SNAKE_CASE)

											lowerCamelCase__					=torch.zeros(_SCREAMING_SNAKE_CASE     ,    max_node_num + 1     ,    max_node_num + 1     ,    dtype=torch.float)
											lowerCamelCase__					=torch.zeros(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    dtype=torch.long)
											lowerCamelCase__					=torch.zeros(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    dtype=torch.long)
											lowerCamelCase__					=torch.zeros(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    dtype=torch.long)
											lowerCamelCase__					=torch.zeros(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    dtype=torch.long)
											lowerCamelCase__					=torch.zeros(
											    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    dtype=torch.long)

											for ix, f in enumerate(_SCREAMING_SNAKE_CASE):
																for k in ["attn_bias", "attn_edge_type", "spatial_pos", "in_degree", "input_nodes", "input_edges"]:
																					lowerCamelCase__					=torch.tensor(f[k])

																if len(f['attn_bias'][1:, 1:][f['spatial_pos'] >= self.spatial_pos_max]) > 0:
																					lowerCamelCase__					=float('-inf')

																lowerCamelCase__					=f['attn_bias']
																lowerCamelCase__					=f[
																    'attn_edge_type'
																]
																lowerCamelCase__					=f['spatial_pos']
																lowerCamelCase__					=f['in_degree']
																lowerCamelCase__					=f['input_nodes']
																lowerCamelCase__					=f['input_edges']

											lowerCamelCase__					=batch['in_degree']

											lowerCamelCase__					=features[0]['labels']
											if len(_SCREAMING_SNAKE_CASE) == 1:  # one task
																if isinstance(sample[0]     ,    _SCREAMING_SNAKE_CASE):  # regression
																					lowerCamelCase__					=torch.from_numpy(np.concatenate([i['labels'] for i in features]))
																else:  # binary classification
																					lowerCamelCase__					=torch.from_numpy(np.concatenate([i['labels'] for i in features]))
											else:  # multi task classification, left to float to keep the NaNs
																lowerCamelCase__					=torch.from_numpy(np.stack([i['labels'] for i in features]     ,    axis=0))

											return batch