





"""simple docstring"""
import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
		from transformers import (
		    FlaxBertModel,
		    FlaxCLIPVisionModel,
		    FlaxVisionTextDualEncoderModel,
		    FlaxViTModel,
		    VisionTextDualEncoderConfig,
		    VisionTextDualEncoderProcessor,
		)
		from transformers.modeling_flax_pytorch_utils import (
		    convert_pytorch_state_dict_to_flax,
		    load_flax_weights_in_pytorch_model,
		)


if is_torch_available():
		import torch

		from transformers import VisionTextDualEncoderModel

if is_vision_available():
		from PIL import Image






def 					A__				(			UpperCamelCase       ):
							if isinstance(UpperCamelCase					,  collections.abc.Iterable       ):
														return x
							return (x, x)






@require_flax
class 					_UpperCAmelCase   :


					def 	lowerCamelCase       (					self			:str     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Optional[Any]		):
												pass


					def 	lowerCamelCase       (					self			:List[str]		):
												pass


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												pass


					def 	lowerCamelCase       (					self			:Dict     ,       __UpperCamelCase			:np.ndarray     ,       __UpperCamelCase			:np.ndarray     ,       __UpperCamelCase			:float		):
												A		=  np.abs((a - b)		).max()
												self.assertLessEqual(__UpperCamelCase     ,       __UpperCamelCase     ,       f"Difference between torch and flax is {diff} (>= {tol})."		)


					def 	lowerCamelCase       (					self			:Tuple     ,       __UpperCamelCase			:Optional[Any]     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Union[str, Any]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:List[Any]=None     ,       **__UpperCamelCase			:Any		):
												A		=  VisionTextDualEncoderConfig.from_vision_text_configs(__UpperCamelCase     ,       __UpperCamelCase		)

												A		=  FlaxVisionTextDualEncoderModel(__UpperCamelCase		)

												A		=  model(input_ids=__UpperCamelCase     ,       pixel_values=__UpperCamelCase     ,       attention_mask=__UpperCamelCase		)

												self.assertEqual(output["text_embeds"].shape     ,       (input_ids.shape[0], config.projection_dim)		)
												self.assertEqual(output["image_embeds"].shape     ,       (pixel_values.shape[0], config.projection_dim)		)


					def 	lowerCamelCase       (					self			:Dict     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Optional[Any]     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Optional[Any]=None     ,       **__UpperCamelCase			:Union[str, Any]		):
												A,	A		=  self.get_vision_text_model(__UpperCamelCase     ,       __UpperCamelCase		)
												A		=  {"vision_model": vision_model, "text_model": text_model}
												A		=  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__UpperCamelCase		)

												A		=  model(input_ids=__UpperCamelCase     ,       pixel_values=__UpperCamelCase     ,       attention_mask=__UpperCamelCase		)

												self.assertEqual(output["text_embeds"].shape     ,       (input_ids.shape[0], model.config.projection_dim)		)
												self.assertEqual(output["image_embeds"].shape     ,       (pixel_values.shape[0], model.config.projection_dim)		)


					def 	lowerCamelCase       (					self			:List[Any]     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:Union[str, Any]     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:Union[str, Any]=None     ,       **__UpperCamelCase			:str		):
												A,	A		=  self.get_vision_text_model(__UpperCamelCase     ,       __UpperCamelCase		)
												A		=  {"vision_model": vision_model, "text_model": text_model}
												A		=  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__UpperCamelCase		)

												A		=  model(input_ids=__UpperCamelCase     ,       pixel_values=__UpperCamelCase     ,       attention_mask=__UpperCamelCase		)
												A		=  output[0]

												with tempfile.TemporaryDirectory() as tmpdirname:
																			model.save_pretrained(__UpperCamelCase		)
																			A		=  FlaxVisionTextDualEncoderModel.from_pretrained(__UpperCamelCase		)

																			A		=  model(input_ids=__UpperCamelCase     ,       pixel_values=__UpperCamelCase     ,       attention_mask=__UpperCamelCase		)
																			A		=  after_output[0]
																			A		=  np.amax(np.abs(out_a - out_a		)		)
																			self.assertLessEqual(__UpperCamelCase     ,       1e-3		)


					def 	lowerCamelCase       (					self			:Tuple     ,       __UpperCamelCase			:Optional[int]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:str=None     ,       **__UpperCamelCase			:List[str]		):
												A,	A		=  self.get_vision_text_model(__UpperCamelCase     ,       __UpperCamelCase		)
												A		=  {"vision_model": vision_model, "text_model": text_model}
												A		=  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__UpperCamelCase		)

												A		=  model(
												    input_ids=__UpperCamelCase     ,       pixel_values=__UpperCamelCase     ,       attention_mask=__UpperCamelCase     ,       output_attentions=__UpperCamelCase		)

												A		=  output.vision_model_output.attentions
												self.assertEqual(len(__UpperCamelCase		)     ,       vision_config.num_hidden_layers		)

												# in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
												A		=  to_atuple(vision_model.config.image_size		)
												A		=  to_atuple(vision_model.config.patch_size		)
												A		=  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
												A		=  num_patches + 1
												self.assertEqual(vision_attentions[0].shape[-3:]     ,       (vision_config.num_attention_heads, seq_len, seq_len)		)

												A		=  output.text_model_output.attentions
												self.assertEqual(len(__UpperCamelCase		)     ,       text_config.num_hidden_layers		)

												self.assertEqual(
												    text_attentions[0].shape[-3:]     ,       (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])     ,       )


					def 	lowerCamelCase       (					self			:Tuple     ,       __UpperCamelCase			:List[Any]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Tuple		):
												pt_model.to(__UpperCamelCase		)
												pt_model.eval()

												# prepare inputs
												A		=  inputs_dict
												A		=  {k: torch.tensor(v.tolist()		) for k, v in flax_inputs.items()}

												with torch.no_grad():
																			A		=  pt_model(**__UpperCamelCase		).to_tuple()

												A		=  fx_model(**__UpperCamelCase		).to_tuple()
												self.assertEqual(len(__UpperCamelCase		)     ,       len(__UpperCamelCase		)     ,       "Output lengths differ between Flax and PyTorch"		)
												for fx_output, pt_output in zip(fx_outputs[:4]     ,       pt_outputs[:4]		):
																			self.assert_almost_equals(__UpperCamelCase     ,       pt_output.numpy()     ,       4e-2		)

												# PT -> Flax
												with tempfile.TemporaryDirectory() as tmpdirname:
																			pt_model.save_pretrained(__UpperCamelCase		)
																			A		=  FlaxVisionTextDualEncoderModel.from_pretrained(__UpperCamelCase     ,       from_pt=__UpperCamelCase		)

												A		=  fx_model_loaded(**__UpperCamelCase		).to_tuple()
												self.assertEqual(len(__UpperCamelCase		)     ,       len(__UpperCamelCase		)     ,       "Output lengths differ between Flax and PyTorch"		)
												for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4]     ,       pt_outputs[:4]		):
																			self.assert_almost_equals(__UpperCamelCase     ,       pt_output.numpy()     ,       4e-2		)

												# Flax -> PT
												with tempfile.TemporaryDirectory() as tmpdirname:
																			fx_model.save_pretrained(__UpperCamelCase		)
																			A		=  VisionTextDualEncoderModel.from_pretrained(__UpperCamelCase     ,       from_flax=__UpperCamelCase		)

												pt_model_loaded.to(__UpperCamelCase		)
												pt_model_loaded.eval()

												with torch.no_grad():
																			A		=  pt_model_loaded(**__UpperCamelCase		).to_tuple()

												self.assertEqual(len(__UpperCamelCase		)     ,       len(__UpperCamelCase		)     ,       "Output lengths differ between Flax and PyTorch"		)
												for fx_output, pt_output_loaded in zip(fx_outputs[:4]     ,       pt_outputs_loaded[:4]		):
																			self.assert_almost_equals(__UpperCamelCase     ,       pt_output_loaded.numpy()     ,       4e-2		)


					def 	lowerCamelCase       (					self			:Optional[Any]     ,       __UpperCamelCase			:Optional[int]     ,       __UpperCamelCase			:List[Any]     ,       __UpperCamelCase			:Optional[int]		):
												A		=  VisionTextDualEncoderConfig.from_vision_text_configs(__UpperCamelCase     ,       __UpperCamelCase		)

												A		=  VisionTextDualEncoderModel(__UpperCamelCase		)
												A		=  FlaxVisionTextDualEncoderModel(__UpperCamelCase		)

												A		=  convert_pytorch_state_dict_to_flax(pt_model.state_dict()     ,       __UpperCamelCase		)
												A		=  fx_state

												self.check_pt_flax_equivalence(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Tuple     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:List[Any]     ,       __UpperCamelCase			:Union[str, Any]		):
												A		=  VisionTextDualEncoderConfig.from_vision_text_configs(__UpperCamelCase     ,       __UpperCamelCase		)

												A		=  VisionTextDualEncoderModel(__UpperCamelCase		)
												A		=  FlaxVisionTextDualEncoderModel(__UpperCamelCase		)

												A		=  load_flax_weights_in_pytorch_model(__UpperCamelCase     ,       fx_model.params		)

												self.check_pt_flax_equivalence(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Any		):
												A		=  self.prepare_config_and_inputs()
												self.check_model_from_pretrained_configs(**__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  self.prepare_config_and_inputs()
												self.check_vision_text_dual_encoder_from_pretrained(**__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  self.prepare_config_and_inputs()
												self.check_save_load(**__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  self.prepare_config_and_inputs()
												self.check_vision_text_output_attention(**__UpperCamelCase		)


					@is_pt_flax_cross_test
					def 	lowerCamelCase       (					self			:List[Any]		):
												A		=  self.prepare_config_and_inputs()
												A		=  config_inputs_dict.pop("vision_config"		)
												A		=  config_inputs_dict.pop("text_config"		)

												A		=  config_inputs_dict

												self.check_equivalence_pt_to_flax(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase		)
												self.check_equivalence_flax_to_pt(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase		)


					@slow
					def 	lowerCamelCase       (					self			:Dict		):
												A,	A		=  self.get_pretrained_model_and_inputs()

												A		=  model_a(**__UpperCamelCase		)
												A		=  outputs[0]

												with tempfile.TemporaryDirectory() as tmp_dirname:
																			model_a.save_pretrained(__UpperCamelCase		)
																			A		=  FlaxVisionTextDualEncoderModel.from_pretrained(__UpperCamelCase		)

																			A		=  model_a(**__UpperCamelCase		)
																			A		=  after_outputs[0]
																			A		=  np.amax(np.abs(out_a - out_a		)		)
																			self.assertLessEqual(__UpperCamelCase     ,       1e-5		)







@require_flax
class 					_UpperCAmelCase   (	lowercase_     ,							unittest.TestCase ):


					def 	lowerCamelCase       (					self			:Optional[int]		):
												A		=  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
												    "hf-internal-testing/tiny-random-vit"     ,       "hf-internal-testing/tiny-bert"     ,       vision_from_pt=__UpperCamelCase     ,       text_from_pt=__UpperCamelCase     ,       )
												A		=  13
												A		=  floats_tensor(
												    [
												        batch_size,
												        model.config.vision_config.num_channels,
												        model.config.vision_config.image_size,
												        model.config.vision_config.image_size,
												    ]		)
												A		=  ids_tensor([batch_size, 4]     ,       model.config.text_config.vocab_size		)
												A		=  random_attention_mask([batch_size, 4]		)
												A		=  {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

												return model, inputs


					def 	lowerCamelCase       (					self			:Any     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:List[Any]		):
												A		=  FlaxViTModel(__UpperCamelCase		)
												A		=  FlaxBertModel(__UpperCamelCase		)
												return vision_model, text_model


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A		=  FlaxViTModelTester(self		)
												A		=  FlaxBertModelTester(self		)
												A		=  vit_model_tester.prepare_config_and_inputs()
												A		=  bert_model_tester.prepare_config_and_inputs()

												A,	A		=  vision_config_and_inputs

												A,	A,	A,	A		=  text_config_and_inputs

												# make sure that cross attention layers are added
												return {
												    "text_config": text_config,
												    "vision_config": vision_config,
												    "pixel_values": pixel_values,
												    "attention_mask": attention_mask,
												    "input_ids": input_ids,
												    "token_type_ids": token_type_ids,
												}







@require_torch
class 					_UpperCAmelCase   (	lowercase_     ,							unittest.TestCase ):


					def 	lowerCamelCase       (					self			:List[Any]		):
												A		=  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
												    "hf-internal-testing/tiny-random-clip"     ,       "hf-internal-testing/tiny-bert"     ,       vision_from_pt=__UpperCamelCase     ,       text_from_pt=__UpperCamelCase     ,       )
												A		=  13
												A		=  floats_tensor(
												    [
												        batch_size,
												        model.config.vision_config.num_channels,
												        model.config.vision_config.image_size,
												        model.config.vision_config.image_size,
												    ]		)
												A		=  ids_tensor([batch_size, 4]     ,       model.config.text_config.vocab_size		)
												A		=  random_attention_mask([batch_size, 4]		)
												A		=  {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

												return model, inputs


					def 	lowerCamelCase       (					self			:Tuple     ,       __UpperCamelCase			:Union[str, Any]     ,       __UpperCamelCase			:Optional[Any]		):
												A		=  FlaxCLIPVisionModel(__UpperCamelCase		)
												A		=  FlaxBertModel(__UpperCamelCase		)
												return vision_model, text_model


					def 	lowerCamelCase       (					self			:Optional[int]		):
												A		=  FlaxCLIPVisionModelTester(self		)
												A		=  FlaxBertModelTester(self		)
												A		=  clip_model_tester.prepare_config_and_inputs()
												A		=  bert_model_tester.prepare_config_and_inputs()

												A,	A		=  vision_config_and_inputs

												A,	A,	A,	A		=  text_config_and_inputs

												# make sure that cross attention layers are added
												return {
												    "text_config": text_config,
												    "vision_config": vision_config,
												    "pixel_values": pixel_values,
												    "attention_mask": attention_mask,
												    "input_ids": input_ids,
												    "token_type_ids": token_type_ids,
												}







@require_flax
@require_vision
class 					_UpperCAmelCase   (	unittest.TestCase ):


					@slow
					def 	lowerCamelCase       (					self			:Any		):
												A		=  FlaxVisionTextDualEncoderModel.from_pretrained("clip-italian/clip-italian"     ,       logit_scale_init_value=1.0		)
												A		=  VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian"		)

												A		=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
												A		=  processor(
												    text=["una foto di un gatto", "una foto di un cane"]     ,       images=__UpperCamelCase     ,       padding=__UpperCamelCase     ,       return_tensors="np"		)

												A		=  model(**__UpperCamelCase		)

												# verify the logits
												self.assertEqual(outputs.logits_per_image.shape     ,       (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])		)
												self.assertEqual(
												    outputs.logits_per_text.shape     ,       (inputs.input_ids.shape[0], inputs.pixel_values.shape[0])     ,       )

												A		=  np.array([[1.2_284_727, 0.3_104_122]]		)

												self.assertTrue(np.allclose(outputs.logits_per_image     ,       __UpperCamelCase     ,       atol=1e-3		)		)









"""simple docstring"""
import argparse

import torch
from torch import nn

from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration






def 					A__				(			UpperCamelCase       ):
							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(UpperCamelCase					,  UpperCamelCase       )






def 					A__				(			UpperCamelCase       ):
							A		=  list(s_dict.keys()       )
							for key in keys:
														if "transformer_layers" in key:
																					A		=  s_dict.pop(UpperCamelCase       )
														elif "subsample" in key:
																					A		=  s_dict.pop(UpperCamelCase       )






def 					A__				(			UpperCamelCase       ):
							A,	A		=  emb.weight.shape
							A		=  nn.Linear(UpperCamelCase					,  UpperCamelCase					,  bias=UpperCamelCase       )
							A		=  emb.weight.data
							return lin_layer






def 					A__				(			UpperCamelCase					,  UpperCamelCase       ):
							A		=  torch.load(UpperCamelCase					,  map_location="cpu"       )
							A		=  mam_aaa["args"]
							A		=  mam_aaa["model"]
							A		=  state_dict["decoder.output_projection.weight"]

							remove_ignore_keys_(UpperCamelCase       )
							rename_keys(UpperCamelCase       )

							A		=  state_dict["decoder.embed_tokens.weight"].shape[0]

							A		=  args.share_decoder_input_output_embed

							A		=  [int(UpperCamelCase       ) for i in args.conv_kernel_sizes.split(","       )]
							A		=  SpeechaTextConfig(
							    vocab_size=UpperCamelCase					,  max_source_positions=args.max_source_positions					,  max_target_positions=args.max_target_positions					,  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					,  dropout=args.dropout					,  attention_dropout=args.attention_dropout					,  activation_dropout=args.activation_dropout					,  activation_function="relu"					,  num_conv_layers=len(UpperCamelCase       )					,  conv_channels=args.conv_channels					,  conv_kernel_sizes=UpperCamelCase					,  input_feat_per_channel=args.input_feat_per_channel					,  input_channels=args.input_channels					,  tie_word_embeddings=UpperCamelCase					,  num_beams=5					,  max_length=200					,  use_cache=UpperCamelCase					,  decoder_start_token_id=2					,  early_stopping=UpperCamelCase					,  )

							A		=  SpeechaTextForConditionalGeneration(UpperCamelCase       )
							A,	A		=  model.model.load_state_dict(UpperCamelCase					,  strict=UpperCamelCase       )
							if len(UpperCamelCase       ) > 0 and not set(UpperCamelCase       ) <= {
							    "encoder.embed_positions.weights",
							    "decoder.embed_positions.weights",
							}:
														raise ValueError(
														    "Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights`  are allowed to be missing,"
														    F" but all the following weights are missing {missing}"       )

							if tie_embeds:
														A		=  make_linear_from_emb(model.model.decoder.embed_tokens       )
							else:
														A		=  lm_head_weights

							model.save_pretrained(UpperCamelCase       )


if __name__ == "__main__":
		_snake_case     :		Optional[int]       						= argparse.ArgumentParser()
		# Required parameters
		parser.add_argument('--fairseq_path', type=str, help='Path to the fairseq model (.pt) file.')
		parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
		_snake_case     :		str       						= parser.parse_args()
		convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)


