





'''simple docstring'''


from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule







def    _lowerCamelCase  (__lowerCamelCase  :     Optional[int]					,  __lowerCamelCase  :     Tuple					,  __lowerCamelCase  :     Any=1e-12     )   ->    Union[str, Any]:
			a__     = jnp.divide(emb_a.T					,  jnp.clip(jnp.linalg.norm(__lowerCamelCase					,  axis=1     )					,  a_min=__lowerCamelCase     )     ).T
			a__     = jnp.divide(emb_a.T					,  jnp.clip(jnp.linalg.norm(__lowerCamelCase					,  axis=1     )					,  a_min=__lowerCamelCase     )     ).T
			return jnp.matmul(__lowerCamelCase					,  norm_emb_a.T     )






class       UpperCamelCase__  (		nn.Module     ):
	lowerCAmelCase__					:			CLIPConfig
	lowerCAmelCase__					:			jnp.dtype								=						jnp.floataa







	def 						__a   (				self			:   str		):




				'''simple docstring'''
				a__     = FlaxCLIPVisionModule(self.config.vision_config		)
				a__     = nn.Dense(self.config.projection_dim , use_bias=lowerCamelCase , dtype=self.dtype		)

				a__     = self.param("concept_embeds" , jax.nn.initializers.ones , (1_7, self.config.projection_dim)		)
				a__     = self.param(
				    "special_care_embeds" , jax.nn.initializers.ones , (3, self.config.projection_dim)		)

				a__     = self.param("concept_embeds_weights" , jax.nn.initializers.ones , (1_7,)		)
				a__     = self.param("special_care_embeds_weights" , jax.nn.initializers.ones , (3,)		)







	def __call__(				self			:   Union[str, Any] , lowerCamelCase			:   Union[str, Any]		):




				'''simple docstring'''
				a__     = self.vision_model(lowerCamelCase		)[1]
				a__     = self.visual_projection(lowerCamelCase		)

				a__     = jax_cosine_distance(lowerCamelCase , self.special_care_embeds		)
				a__     = jax_cosine_distance(lowerCamelCase , self.concept_embeds		)

				# increase this value to create a stronger `nfsw` filter
				# at the cost of increasing the possibility of filtering benign image inputs
				a__     = 0.0

				a__     = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
				a__     = jnp.round(lowerCamelCase , 3		)
				a__     = jnp.any(special_scores > 0 , axis=1 , keepdims=lowerCamelCase		)
				# Use a lower threshold if an image has any special care concept
				a__     = is_special_care * 0.01

				a__     = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
				a__     = jnp.round(lowerCamelCase , 3		)
				a__     = jnp.any(concept_scores > 0 , axis=1		)

				return has_nsfw_concepts






class       UpperCamelCase__  (		__lowerCAmelCase     ):
	lowerCAmelCase__					:			List[Any]								=						CLIPConfig
	lowerCAmelCase__					:			Optional[int]								=						"clip_input"
	lowerCAmelCase__					:			Optional[Any]								=						FlaxStableDiffusionSafetyCheckerModule







	def __init__(				self			:   Optional[int] , lowerCamelCase			:   CLIPConfig , lowerCamelCase			:   Optional[Tuple] = None , lowerCamelCase			:   int = 0 , lowerCamelCase			:   jnp.dtype = jnp.floataa , lowerCamelCase			:   bool = True , **lowerCamelCase			:   Dict , ):




				'''simple docstring'''
				if input_shape is None:
							a__     = (1, 2_2_4, 2_2_4, 3)
				a__     = self.module_class(config=lowerCamelCase , dtype=lowerCamelCase , **lowerCamelCase		)
				super().__init__(lowerCamelCase , lowerCamelCase , input_shape=lowerCamelCase , seed=lowerCamelCase , dtype=lowerCamelCase , _do_init=_do_init		)







	def 						__a   (				self			:   Tuple , lowerCamelCase			:   jax.random.KeyArray , lowerCamelCase			:   Tuple , lowerCamelCase			:   FrozenDict = None		):




				'''simple docstring'''
				# init input tensor
				a__     = jax.random.normal(lowerCamelCase , lowerCamelCase		)

				a__   ,				a__     = jax.random.split(lowerCamelCase		)
				a__     = {"params": params_rng, "dropout": dropout_rng}

				a__     = self.module.init(lowerCamelCase , lowerCamelCase		)["params"]

				return random_params







	def __call__(				self			:   int , lowerCamelCase			:   Dict , lowerCamelCase			:   dict = None , ):




				'''simple docstring'''
				a__     = jnp.transpose(lowerCamelCase , (0, 2, 3, 1)		)

				return self.module.apply(
				    {"params": params or self.params} , jnp.array(lowerCamelCase , dtype=jnp.floataa		) , rngs={} , )










'''simple docstring'''


import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration







def    _lowerCamelCase  (__lowerCamelCase  :     Any     )   ->    Optional[int]:
			a__     = [
			    "encoder.version",
			    "decoder.version",
			    "model.encoder.version",
			    "model.decoder.version",
			    "decoder.output_projection.weight",
			    "_float_tensor",
			    "encoder.embed_positions._float_tensor",
			    "decoder.embed_positions._float_tensor",
			]
			for k in ignore_keys:
						state_dict.pop(__lowerCamelCase					,  __lowerCamelCase     )







def    _lowerCamelCase  (__lowerCamelCase  :     int     )   ->    List[Any]:
			a__   ,				a__     = emb.weight.shape
			a__     = nn.Linear(__lowerCamelCase					,  __lowerCamelCase					,  bias=__lowerCamelCase     )
			a__     = emb.weight.data
			return lin_layer







def    _lowerCamelCase  (__lowerCamelCase  :     List[Any]     )   ->    Any:
			a__     = torch.load(__lowerCamelCase					,  map_location="cpu"     )
			a__     = mam_aaa["args"] or mam_aaa["cfg"]["model"]
			a__     = mam_aaa["model"]
			remove_ignore_keys_(__lowerCamelCase     )
			a__     = state_dict["encoder.embed_tokens.weight"].shape[0]

			a__     = MaMaaaConfig(
			    vocab_size=__lowerCamelCase					,  max_position_embeddings=1024					,  encoder_layers=args.encoder_layers					,  decoder_layers=args.decoder_layers					,  encoder_attention_heads=args.encoder_attention_heads					,  decoder_attention_heads=args.decoder_attention_heads					,  encoder_ffn_dim=args.encoder_ffn_embed_dim					,  decoder_ffn_dim=args.decoder_ffn_embed_dim					,  d_model=args.encoder_embed_dim					,  encoder_layerdrop=args.encoder_layerdrop					,  decoder_layerdrop=args.decoder_layerdrop					,  dropout=args.dropout					,  attention_dropout=args.attention_dropout					,  activation_dropout=args.activation_dropout					,  activation_function="relu"					,  )

			a__     = state_dict["decoder.embed_tokens.weight"]
			a__     = MaMaaaForConditionalGeneration(__lowerCamelCase     )
			model.model.load_state_dict(__lowerCamelCase					,  strict=__lowerCamelCase     )
			a__     = make_linear_from_emb(model.model.shared     )

			return model


if __name__ == "__main__":
		lowerCAmelCase_   :   Tuple       =					argparse.ArgumentParser()
		# Required parameters
		parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.")
		parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
		lowerCAmelCase_   :   Tuple       =					parser.parse_args()
		lowerCAmelCase_   :   int       =					convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
		model.save_pretrained(args.pytorch_dump_folder_path)



