




import argparse
import os

import align
import numpy as np
import requests
import tensorflow as tf
import torch
from PIL import Image
from tokenizer import Tokenizer

from transformers import (
    AlignConfig,
    AlignModel,
    AlignProcessor,
    BertConfig,
    BertTokenizer,
    EfficientNetConfig,
    EfficientNetImageProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
__A						:		Dict          =						logging.get_logger(__name__)





def      UpperCAmelCase  (						UpperCamelCase__   )     ->     List[str]:







				'''simple docstring'''


				__lowerCAmelCase					=  tf.image.resize(UpperCamelCase__						,			(3_46, 3_46)   )
				__lowerCAmelCase					=  tf.image.crop_to_bounding_box(UpperCamelCase__						,			(3_46 - 2_89) // 2						,			(3_46 - 2_89) // 2						,			2_89						,			2_89   )
				return image





def      UpperCAmelCase  (						)     ->     Optional[Any]:







				'''simple docstring'''


				__lowerCAmelCase					=  EfficientNetConfig.from_pretrained("""google/efficientnet-b7"""   )
				__lowerCAmelCase					=  2_89
				__lowerCAmelCase					=  6_40
				__lowerCAmelCase					=  {"""0""": """LABEL_0""", """1""": """LABEL_1"""}
				__lowerCAmelCase					=  {"""LABEL_0""": 0, """LABEL_1""": 1}
				__lowerCAmelCase					=  []

				__lowerCAmelCase					=  BertConfig()
				__lowerCAmelCase					=  AlignConfig.from_text_vision_configs(
				    text_config=UpperCamelCase__						,			vision_config=UpperCamelCase__						,			projection_dim=6_40   )
				return config





def      UpperCAmelCase  (						)     ->     Dict:







				'''simple docstring'''


				__lowerCAmelCase					=  """http://images.cocodataset.org/val2017/000000039769.jpg"""
				__lowerCAmelCase					=  Image.open(requests.get(UpperCamelCase__						,			stream=UpperCamelCase__   ).raw   )
				return im





def      UpperCAmelCase  (						)     ->     Optional[Any]:







				'''simple docstring'''


				__lowerCAmelCase					=  EfficientNetImageProcessor(
				    do_center_crop=UpperCamelCase__						,			rescale_factor=1 / 127.5						,			rescale_offset=UpperCamelCase__						,			do_normalize=UpperCamelCase__						,			include_top=UpperCamelCase__						,			resample=Image.BILINEAR						,			)
				__lowerCAmelCase					=  BertTokenizer.from_pretrained("""bert-base-uncased"""   )
				__lowerCAmelCase					=  64
				__lowerCAmelCase					=  AlignProcessor(image_processor=UpperCamelCase__						,			tokenizer=UpperCamelCase__   )
				return processor





def      UpperCAmelCase  (						UpperCamelCase__   )     ->     Tuple:







				'''simple docstring'''


				__lowerCAmelCase					=  [v.split("""_"""   )[0].split("""block"""   )[1] for v in original_param_names if v.startswith("""block"""   )]
				__lowerCAmelCase					=  list(set(UpperCamelCase__   )   )
				__lowerCAmelCase					=  sorted(UpperCamelCase__   )
				__lowerCAmelCase					=  len(UpperCamelCase__   )
				__lowerCAmelCase					=  {b: str(UpperCamelCase__   ) for b, i in zip(UpperCamelCase__						,			range(UpperCamelCase__   )   )}

				__lowerCAmelCase					=  []
				rename_keys.append(("""stem_conv/kernel:0""", """embeddings.convolution.weight""")   )
				rename_keys.append(("""stem_bn/gamma:0""", """embeddings.batchnorm.weight""")   )
				rename_keys.append(("""stem_bn/beta:0""", """embeddings.batchnorm.bias""")   )
				rename_keys.append(("""stem_bn/moving_mean:0""", """embeddings.batchnorm.running_mean""")   )
				rename_keys.append(("""stem_bn/moving_variance:0""", """embeddings.batchnorm.running_var""")   )

				for b in block_names:
								__lowerCAmelCase					=  block_name_mapping[b]
								rename_keys.append((F'''block{b}_expand_conv/kernel:0''', F'''encoder.blocks.{hf_b}.expansion.expand_conv.weight''')   )
								rename_keys.append((F'''block{b}_expand_bn/gamma:0''', F'''encoder.blocks.{hf_b}.expansion.expand_bn.weight''')   )
								rename_keys.append((F'''block{b}_expand_bn/beta:0''', F'''encoder.blocks.{hf_b}.expansion.expand_bn.bias''')   )
								rename_keys.append(
								    (F'''block{b}_expand_bn/moving_mean:0''', F'''encoder.blocks.{hf_b}.expansion.expand_bn.running_mean''')   )
								rename_keys.append(
								    (F'''block{b}_expand_bn/moving_variance:0''', F'''encoder.blocks.{hf_b}.expansion.expand_bn.running_var''')   )
								rename_keys.append(
								    (F'''block{b}_dwconv/depthwise_kernel:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight''')   )
								rename_keys.append((F'''block{b}_bn/gamma:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight''')   )
								rename_keys.append((F'''block{b}_bn/beta:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias''')   )
								rename_keys.append(
								    (F'''block{b}_bn/moving_mean:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean''')   )
								rename_keys.append(
								    (F'''block{b}_bn/moving_variance:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var''')   )

								rename_keys.append((F'''block{b}_se_reduce/kernel:0''', F'''encoder.blocks.{hf_b}.squeeze_excite.reduce.weight''')   )
								rename_keys.append((F'''block{b}_se_reduce/bias:0''', F'''encoder.blocks.{hf_b}.squeeze_excite.reduce.bias''')   )
								rename_keys.append((F'''block{b}_se_expand/kernel:0''', F'''encoder.blocks.{hf_b}.squeeze_excite.expand.weight''')   )
								rename_keys.append((F'''block{b}_se_expand/bias:0''', F'''encoder.blocks.{hf_b}.squeeze_excite.expand.bias''')   )
								rename_keys.append(
								    (F'''block{b}_project_conv/kernel:0''', F'''encoder.blocks.{hf_b}.projection.project_conv.weight''')   )
								rename_keys.append((F'''block{b}_project_bn/gamma:0''', F'''encoder.blocks.{hf_b}.projection.project_bn.weight''')   )
								rename_keys.append((F'''block{b}_project_bn/beta:0''', F'''encoder.blocks.{hf_b}.projection.project_bn.bias''')   )
								rename_keys.append(
								    (F'''block{b}_project_bn/moving_mean:0''', F'''encoder.blocks.{hf_b}.projection.project_bn.running_mean''')   )
								rename_keys.append(
								    (F'''block{b}_project_bn/moving_variance:0''', F'''encoder.blocks.{hf_b}.projection.project_bn.running_var''')   )

				__lowerCAmelCase					=  {}
				for item in rename_keys:
								if item[0] in original_param_names:
												__lowerCAmelCase					=  """vision_model.""" + item[1]

    # BERT text encoder
				__lowerCAmelCase					=  []
				__lowerCAmelCase					=  """tf_bert_model/bert"""
				__lowerCAmelCase					=  """text_model"""
				for i in range(12   ):
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/self/query/kernel:0''',
								        F'''{new}.encoder.layer.{i}.attention.self.query.weight''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/self/query/bias:0''',
								        F'''{new}.encoder.layer.{i}.attention.self.query.bias''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/self/key/kernel:0''',
								        F'''{new}.encoder.layer.{i}.attention.self.key.weight''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/self/key/bias:0''',
								        F'''{new}.encoder.layer.{i}.attention.self.key.bias''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/self/value/kernel:0''',
								        F'''{new}.encoder.layer.{i}.attention.self.value.weight''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/self/value/bias:0''',
								        F'''{new}.encoder.layer.{i}.attention.self.value.bias''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/output/dense/kernel:0''',
								        F'''{new}.encoder.layer.{i}.attention.output.dense.weight''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/output/dense/bias:0''',
								        F'''{new}.encoder.layer.{i}.attention.output.dense.bias''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/output/LayerNorm/gamma:0''',
								        F'''{new}.encoder.layer.{i}.attention.output.LayerNorm.weight''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/attention/output/LayerNorm/beta:0''',
								        F'''{new}.encoder.layer.{i}.attention.output.LayerNorm.bias''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/intermediate/dense/kernel:0''',
								        F'''{new}.encoder.layer.{i}.intermediate.dense.weight''',
								    )   )
								rename_keys.append(
								    (
								        F'''{old}/encoder/layer_._{i}/intermediate/dense/bias:0''',
								        F'''{new}.encoder.layer.{i}.intermediate.dense.bias''',
								    )   )
								rename_keys.append(
								    (F'''{old}/encoder/layer_._{i}/output/dense/kernel:0''', F'''{new}.encoder.layer.{i}.output.dense.weight''')   )
								rename_keys.append(
								    (F'''{old}/encoder/layer_._{i}/output/dense/bias:0''', F'''{new}.encoder.layer.{i}.output.dense.bias''')   )
								rename_keys.append(
								    (F'''{old}/encoder/layer_._{i}/output/LayerNorm/gamma:0''', F'''{new}.encoder.layer.{i}.output.LayerNorm.weight''')   )
								rename_keys.append(
								    (F'''{old}/encoder/layer_._{i}/output/LayerNorm/beta:0''', F'''{new}.encoder.layer.{i}.output.LayerNorm.bias''')   )

				rename_keys.append((F'''{old}/embeddings/word_embeddings/weight:0''', F'''{new}.embeddings.word_embeddings.weight''')   )
				rename_keys.append(
				    (F'''{old}/embeddings/position_embeddings/embeddings:0''', F'''{new}.embeddings.position_embeddings.weight''')   )
				rename_keys.append(
				    (F'''{old}/embeddings/token_type_embeddings/embeddings:0''', F'''{new}.embeddings.token_type_embeddings.weight''')   )
				rename_keys.append((F'''{old}/embeddings/LayerNorm/gamma:0''', F'''{new}.embeddings.LayerNorm.weight''')   )
				rename_keys.append((F'''{old}/embeddings/LayerNorm/beta:0''', F'''{new}.embeddings.LayerNorm.bias''')   )

				rename_keys.append((F'''{old}/pooler/dense/kernel:0''', F'''{new}.pooler.dense.weight''')   )
				rename_keys.append((F'''{old}/pooler/dense/bias:0''', F'''{new}.pooler.dense.bias''')   )
				rename_keys.append(("""dense/kernel:0""", """text_projection.weight""")   )
				rename_keys.append(("""dense/bias:0""", """text_projection.bias""")   )
				rename_keys.append(("""dense/bias:0""", """text_projection.bias""")   )
				rename_keys.append(("""temperature:0""", """temperature""")   )

				for item in rename_keys:
								if item[0] in original_param_names:
												__lowerCAmelCase					=  item[1]
				return key_mapping





def      UpperCAmelCase  (						UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__   )     ->     Optional[Any]:







				'''simple docstring'''


				list(hf_params.keys()   )

				for key, value in tf_params.items():
								if key not in key_mapping:
												continue

								__lowerCAmelCase					=  key_mapping[key]
								if "_conv" in key and "kernel" in key:
												__lowerCAmelCase					=  torch.from_numpy(UpperCamelCase__   ).permute(3						,			2						,			0						,			1   )
								elif "embeddings" in key:
												__lowerCAmelCase					=  torch.from_numpy(UpperCamelCase__   )
								elif "depthwise_kernel" in key:
												__lowerCAmelCase					=  torch.from_numpy(UpperCamelCase__   ).permute(2						,			3						,			0						,			1   )
								elif "kernel" in key:
												__lowerCAmelCase					=  torch.from_numpy(np.transpose(UpperCamelCase__   )   )
								elif "temperature" in key:
												__lowerCAmelCase					=  value
								elif "bn/gamma" or "bn/beta" in key:
												__lowerCAmelCase					=  torch.from_numpy(np.transpose(UpperCamelCase__   )   ).squeeze()
								else:
												__lowerCAmelCase					=  torch.from_numpy(UpperCamelCase__   )

								# Replace HF parameters with original TF model parameters
								hf_params[hf_key].copy_(UpperCamelCase__   )





@torch.no_grad()
def      UpperCAmelCase  (						UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__   )     ->     List[str]:







				'''simple docstring'''


				__lowerCAmelCase					=  64
				__lowerCAmelCase					=  Tokenizer(UpperCamelCase__   )
				__lowerCAmelCase					=  align.Align("""efficientnet-b7"""						,			"""bert-base"""						,			6_40						,			UpperCamelCase__						,			tok.get_vocab_size()   )
				original_model.compile()
				original_model.load_weights(UpperCamelCase__   )

				__lowerCAmelCase					=  original_model.trainable_variables
				__lowerCAmelCase					=  original_model.non_trainable_variables
				__lowerCAmelCase					=  {param.name: param.numpy() for param in tf_params}
				for param in tf_non_train_params:
								__lowerCAmelCase					=  param.numpy()
				__lowerCAmelCase					=  list(tf_params.keys()   )

				# Load HuggingFace model
				__lowerCAmelCase					=  get_align_config()
				__lowerCAmelCase					=  AlignModel(UpperCamelCase__   ).eval()
				__lowerCAmelCase					=  hf_model.state_dict()

				# Create src-to-dst parameter name mapping dictionary
				print("""Converting parameters..."""   )
				__lowerCAmelCase					=  rename_keys(UpperCamelCase__   )
				replace_params(UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__   )

				# Initialize processor
				__lowerCAmelCase					=  get_processor()
				__lowerCAmelCase					=  processor(
				    images=prepare_img()						,			text="""A picture of a cat"""						,			padding="""max_length"""						,			max_length=64						,			return_tensors="""pt"""   )

				# HF model inference
				hf_model.eval()
				with torch.no_grad():
								__lowerCAmelCase					=  hf_model(**UpperCamelCase__   )

				__lowerCAmelCase					=  outputs.image_embeds.detach().numpy()
				__lowerCAmelCase					=  outputs.text_embeds.detach().numpy()

				# Original model inference
				__lowerCAmelCase					=  False
				__lowerCAmelCase					=  EfficientNetImageProcessor(
				    do_center_crop=UpperCamelCase__						,			do_rescale=UpperCamelCase__						,			do_normalize=UpperCamelCase__						,			include_top=UpperCamelCase__						,			resample=Image.BILINEAR						,			)
				__lowerCAmelCase					=  tf_image_processor(images=prepare_img()						,			return_tensors="""tf"""						,			data_format="""channels_last"""   )["""pixel_values"""]
				__lowerCAmelCase					=  tok(tf.constant(["""A picture of a cat"""]   )   )

				__lowerCAmelCase					=  original_model.image_encoder(UpperCamelCase__						,			training=UpperCamelCase__   )
				__lowerCAmelCase					=  original_model.text_encoder(UpperCamelCase__						,			training=UpperCamelCase__   )

				__lowerCAmelCase					=  tf.nn.la_normalize(UpperCamelCase__						,			axis=-1   )
				__lowerCAmelCase					=  tf.nn.la_normalize(UpperCamelCase__						,			axis=-1   )

				# Check whether original and HF model outputs match  -> np.allclose
				if not np.allclose(UpperCamelCase__						,			UpperCamelCase__						,			atol=1e-3   ):
								raise ValueError("""The predicted image features are not the same."""   )
				if not np.allclose(UpperCamelCase__						,			UpperCamelCase__						,			atol=1e-3   ):
								raise ValueError("""The predicted text features are not the same."""   )
				print("""Model outputs match!"""   )

				if save_model:
								# Create folder to save model
								if not os.path.isdir(UpperCamelCase__   ):
												os.mkdir(UpperCamelCase__   )
								# Save converted model and image processor
								hf_model.save_pretrained(UpperCamelCase__   )
								processor.save_pretrained(UpperCamelCase__   )

				if push_to_hub:
								# Push model and image processor to hub
								print("""Pushing converted ALIGN to the hub..."""   )
								processor.push_to_hub("""align-base"""   )
								hf_model.push_to_hub("""align-base"""   )


if __name__ == "__main__":
	__A						:		str          =						argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    "--checkpoint_path",
	    default="./weights/model-weights",
	    type=str,
	    help="Path to the pretrained TF ALIGN checkpoint.",
	)
	parser.add_argument(
	    "--pytorch_dump_folder_path",
	    default="hf_model",
	    type=str,
	    help="Path to the output PyTorch model directory.",
	)
	parser.add_argument("--save_model", action="store_true", help="Save model to local")
	parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub")

	__A						:		Union[str, Any]          =						parser.parse_args()
	convert_align_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)






import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
			from transformers.models.dpr.modeling_dpr import (
			    DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
			)

class 						_SCREAMING_SNAKE_CASE				:






		"""simple docstring"""







		def __init__(      self     :					List[Any]	,       __SCREAMING_SNAKE_CASE     :					List[Any]	,       __SCREAMING_SNAKE_CASE     :					List[Any]=13	,       __SCREAMING_SNAKE_CASE     :					str=7	,       __SCREAMING_SNAKE_CASE     :					Any=False	,       __SCREAMING_SNAKE_CASE     :					Dict=True	,       __SCREAMING_SNAKE_CASE     :					Any=True	,       __SCREAMING_SNAKE_CASE     :					List[str]=True	,       __SCREAMING_SNAKE_CASE     :					Union[str, Any]=99	,       __SCREAMING_SNAKE_CASE     :					Dict=32	,       __SCREAMING_SNAKE_CASE     :					Union[str, Any]=5	,       __SCREAMING_SNAKE_CASE     :					Dict=4	,       __SCREAMING_SNAKE_CASE     :					Tuple=37	,       __SCREAMING_SNAKE_CASE     :					List[str]="gelu"	,       __SCREAMING_SNAKE_CASE     :					List[str]=0.1	,       __SCREAMING_SNAKE_CASE     :					Optional[Any]=0.1	,       __SCREAMING_SNAKE_CASE     :					List[Any]=512	,       __SCREAMING_SNAKE_CASE     :					Optional[int]=16	,       __SCREAMING_SNAKE_CASE     :					Any=2	,       __SCREAMING_SNAKE_CASE     :					Dict=0.0_2	,       __SCREAMING_SNAKE_CASE     :					Union[str, Any]=3	,       __SCREAMING_SNAKE_CASE     :					int=4	,       __SCREAMING_SNAKE_CASE     :					Optional[Any]=None	,       __SCREAMING_SNAKE_CASE     :					List[Any]=0	,       ):






					"""simple docstring"""
					_snake_case														=   parent
					_snake_case														=   batch_size
					_snake_case														=   seq_length
					_snake_case														=   is_training
					_snake_case														=   use_input_mask
					_snake_case														=   use_token_type_ids
					_snake_case														=   use_labels
					_snake_case														=   vocab_size
					_snake_case														=   hidden_size
					_snake_case														=   num_hidden_layers
					_snake_case														=   num_attention_heads
					_snake_case														=   intermediate_size
					_snake_case														=   hidden_act
					_snake_case														=   hidden_dropout_prob
					_snake_case														=   attention_probs_dropout_prob
					_snake_case														=   max_position_embeddings
					_snake_case														=   type_vocab_size
					_snake_case														=   type_sequence_label_size
					_snake_case														=   initializer_range
					_snake_case														=   num_labels
					_snake_case														=   num_choices
					_snake_case														=   scope
					_snake_case														=   projection_dim



		def 					_a					(      self     :					Tuple ):






					"""simple docstring"""
					_snake_case														=   ids_tensor([self.batch_size, self.seq_length]	,       self.vocab_size )

					_snake_case														=   None
					if self.use_input_mask:
								_snake_case														=   random_attention_mask([self.batch_size, self.seq_length] )

					_snake_case														=   None
					if self.use_token_type_ids:
								_snake_case														=   ids_tensor([self.batch_size, self.seq_length]	,       self.type_vocab_size )

					_snake_case														=   None
					_snake_case														=   None
					_snake_case														=   None
					if self.use_labels:
								_snake_case														=   ids_tensor([self.batch_size]	,       self.type_sequence_label_size )
								_snake_case														=   ids_tensor([self.batch_size, self.seq_length]	,       self.num_labels )
								_snake_case														=   ids_tensor([self.batch_size]	,       self.num_choices )

					_snake_case														=   self.get_config()

					return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



		def 					_a					(      self     :					List[Any] ):






					"""simple docstring"""
					return DPRConfig(
					    projection_dim=self.projection_dim	,       vocab_size=self.vocab_size	,       hidden_size=self.hidden_size	,       num_hidden_layers=self.num_hidden_layers	,       num_attention_heads=self.num_attention_heads	,       intermediate_size=self.intermediate_size	,       hidden_act=self.hidden_act	,       hidden_dropout_prob=self.hidden_dropout_prob	,       attention_probs_dropout_prob=self.attention_probs_dropout_prob	,       max_position_embeddings=self.max_position_embeddings	,       type_vocab_size=self.type_vocab_size	,       initializer_range=self.initializer_range	,       )



		def 					_a					(      self     :					List[Any]	,       __SCREAMING_SNAKE_CASE     :					Union[str, Any]	,       __SCREAMING_SNAKE_CASE     :					List[str]	,       __SCREAMING_SNAKE_CASE     :					List[str]	,       __SCREAMING_SNAKE_CASE     :					List[Any]	,       __SCREAMING_SNAKE_CASE     :					Any	,       __SCREAMING_SNAKE_CASE     :					List[Any]	,       __SCREAMING_SNAKE_CASE     :					List[str] ):






					"""simple docstring"""
					_snake_case														=   DPRContextEncoder(config=__SCREAMING_SNAKE_CASE )
					model.to(__SCREAMING_SNAKE_CASE )
					model.eval()
					_snake_case														=   model(__SCREAMING_SNAKE_CASE	,       attention_mask=__SCREAMING_SNAKE_CASE	,       token_type_ids=__SCREAMING_SNAKE_CASE )
					_snake_case														=   model(__SCREAMING_SNAKE_CASE	,       token_type_ids=__SCREAMING_SNAKE_CASE )
					_snake_case														=   model(__SCREAMING_SNAKE_CASE )
					self.parent.assertEqual(result.pooler_output.shape	,       (self.batch_size, self.projection_dim or self.hidden_size) )



		def 					_a					(      self     :					str	,       __SCREAMING_SNAKE_CASE     :					Optional[Any]	,       __SCREAMING_SNAKE_CASE     :					List[str]	,       __SCREAMING_SNAKE_CASE     :					List[str]	,       __SCREAMING_SNAKE_CASE     :					List[str]	,       __SCREAMING_SNAKE_CASE     :					Dict	,       __SCREAMING_SNAKE_CASE     :					Optional[Any]	,       __SCREAMING_SNAKE_CASE     :					str ):






					"""simple docstring"""
					_snake_case														=   DPRQuestionEncoder(config=__SCREAMING_SNAKE_CASE )
					model.to(__SCREAMING_SNAKE_CASE )
					model.eval()
					_snake_case														=   model(__SCREAMING_SNAKE_CASE	,       attention_mask=__SCREAMING_SNAKE_CASE	,       token_type_ids=__SCREAMING_SNAKE_CASE )
					_snake_case														=   model(__SCREAMING_SNAKE_CASE	,       token_type_ids=__SCREAMING_SNAKE_CASE )
					_snake_case														=   model(__SCREAMING_SNAKE_CASE )
					self.parent.assertEqual(result.pooler_output.shape	,       (self.batch_size, self.projection_dim or self.hidden_size) )



		def 					_a					(      self     :					List[str]	,       __SCREAMING_SNAKE_CASE     :					int	,       __SCREAMING_SNAKE_CASE     :					List[Any]	,       __SCREAMING_SNAKE_CASE     :					int	,       __SCREAMING_SNAKE_CASE     :					Union[str, Any]	,       __SCREAMING_SNAKE_CASE     :					Any	,       __SCREAMING_SNAKE_CASE     :					Optional[int]	,       __SCREAMING_SNAKE_CASE     :					Any ):






					"""simple docstring"""
					_snake_case														=   DPRReader(config=__SCREAMING_SNAKE_CASE )
					model.to(__SCREAMING_SNAKE_CASE )
					model.eval()
					_snake_case														=   model(
					    __SCREAMING_SNAKE_CASE	,       attention_mask=__SCREAMING_SNAKE_CASE	,       )

					self.parent.assertEqual(result.start_logits.shape	,       (self.batch_size, self.seq_length) )
					self.parent.assertEqual(result.end_logits.shape	,       (self.batch_size, self.seq_length) )
					self.parent.assertEqual(result.relevance_logits.shape	,       (self.batch_size,) )







		def 					_a					(      self     :					Any ):






					"""simple docstring"""
					_snake_case														=   self.prepare_config_and_inputs()
					(
					    (
					    _snake_case
					)							,			(
					    _snake_case
					)							,			(
					    _snake_case
					)							,			(
					    _snake_case
					)							,			(
					    _snake_case
					)							,			(
					    _snake_case
					)							,			(
					    _snake_case
					)							,			
					)														=   config_and_inputs
					_snake_case														=   {"input_ids": input_ids}
					return config, inputs_dict

@require_torch
class 						_SCREAMING_SNAKE_CASE				(  _lowercase   ,  _lowercase   ,  unittest.TestCase				):






		"""simple docstring"""







		A_    			=   (
		    (
		        DPRContextEncoder,
		        DPRQuestionEncoder,
		        DPRReader,
		    )
		    if is_torch_available()
		    else ()
		)
		A_    			=   {'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

		A_    			=   False
		A_    			=   False  # why?
		A_    			=   False
		A_    			=   False



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






					"""simple docstring"""
					_snake_case														=   DPRModelTester(self )
					_snake_case														=   ConfigTester(self	,       config_class=__SCREAMING_SNAKE_CASE	,       hidden_size=37 )



		def 					_a					(      self     :					int ):






					"""simple docstring"""
					self.config_tester.run_common_tests()



		def 					_a					(      self     :					List[str] ):






					"""simple docstring"""
					_snake_case														=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_context_encoder(*__SCREAMING_SNAKE_CASE )



		def 					_a					(      self     :					List[str] ):






					"""simple docstring"""
					_snake_case														=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_question_encoder(*__SCREAMING_SNAKE_CASE )



		def 					_a					(      self     :					Optional[Any] ):






					"""simple docstring"""
					_snake_case														=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_reader(*__SCREAMING_SNAKE_CASE )



		def 					_a					(      self     :					Optional[Any] ):






					"""simple docstring"""
					_snake_case														=   self.model_tester.prepare_config_and_inputs()[0]

					_snake_case														=   DPRQuestionEncoder(config=__SCREAMING_SNAKE_CASE )
					model.to(__SCREAMING_SNAKE_CASE )
					model.eval()

					with tempfile.TemporaryDirectory() as tmp_dirname:
								model.save_pretrained(__SCREAMING_SNAKE_CASE )
								_snake_case														=   DPRQuestionEncoder.from_pretrained(__SCREAMING_SNAKE_CASE	,       projection_dim=512 )

					self.assertIsNotNone(__SCREAMING_SNAKE_CASE )







		@slow
		def 					_a					(      self     :					List[str] ):






					"""simple docstring"""
					for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								_snake_case														=   DPRContextEncoder.from_pretrained(__SCREAMING_SNAKE_CASE )
								self.assertIsNotNone(__SCREAMING_SNAKE_CASE )

					for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								_snake_case														=   DPRContextEncoder.from_pretrained(__SCREAMING_SNAKE_CASE )
								self.assertIsNotNone(__SCREAMING_SNAKE_CASE )

					for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								_snake_case														=   DPRQuestionEncoder.from_pretrained(__SCREAMING_SNAKE_CASE )
								self.assertIsNotNone(__SCREAMING_SNAKE_CASE )

					for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								_snake_case														=   DPRReader.from_pretrained(__SCREAMING_SNAKE_CASE )
								self.assertIsNotNone(__SCREAMING_SNAKE_CASE )

@require_torch
class 						_SCREAMING_SNAKE_CASE				(  unittest.TestCase				):






		"""simple docstring"""







		@slow
		def 					_a					(      self     :					Dict ):






					"""simple docstring"""
					_snake_case														=   DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"	,       return_dict=__SCREAMING_SNAKE_CASE )
					model.to(__SCREAMING_SNAKE_CASE )

					_snake_case														=   torch.tensor(
					    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]	,       dtype=torch.long	,       device=__SCREAMING_SNAKE_CASE )  # [CLS] hello, is my dog cute? [SEP]
					_snake_case														=   model(__SCREAMING_SNAKE_CASE )[0]  # embedding shape = (1, 768)
					# compare the actual values for a slice.
					_snake_case														=   torch.tensor(
					    [
					        [
					            0.0_3_2_3_6_2_5_3,
					            0.1_2_7_5_3_3_3_5,
					            0.1_6_8_1_8_5_0_9,
					            0.0_0_2_7_9_7_8_6,
					            0.3_8_9_6_9_3_3,
					            0.2_4_2_6_4_9_4_5,
					            0.2_1_7_8_9_7_1,
					            -0.0_2_3_3_5_2_2_7,
					            -0.0_8_4_8_1_9_5_9,
					            -0.1_4_3_2_4_1_1_7,
					        ]
					    ]	,       dtype=torch.float	,       device=__SCREAMING_SNAKE_CASE	,       )
					self.assertTrue(torch.allclose(output[:, :10]	,       __SCREAMING_SNAKE_CASE	,       atol=1e-4 ) )







		@slow
		def 					_a					(      self     :					Optional[int] ):






					"""simple docstring"""
					_snake_case														=   DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base" )
					_snake_case														=   DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base" )
					model.to(__SCREAMING_SNAKE_CASE )

					_snake_case														=   tokenizer(
					    questions="What is love ?"	,       titles="Haddaway"	,       texts="What Is Love is a song recorded by the artist Haddaway"	,       padding=__SCREAMING_SNAKE_CASE	,       return_tensors="pt"	,       )
					encoded_inputs.to(__SCREAMING_SNAKE_CASE )

					_snake_case														=   model(**__SCREAMING_SNAKE_CASE )

					# compare the actual values for a slice.
					_snake_case														=   torch.tensor(
					    [[-1_0.3_0_0_5, -1_0.7_7_6_5, -1_1.4_8_7_2, -1_1.6_8_4_1, -1_1.9_3_1_2, -1_0.3_0_0_2, -9.8_5_4_4, -1_1.7_3_7_8, -1_2.0_8_2_1, -1_0.2_9_7_5]]	,       dtype=torch.float	,       device=__SCREAMING_SNAKE_CASE	,       )

					_snake_case														=   torch.tensor(
					    [[-1_1.0_6_8_4, -1_1.7_0_4_1, -1_1.5_3_9_7, -1_0.3_4_6_5, -1_0.8_7_9_1, -6.8_4_4_3, -1_1.9_9_5_9, -1_1.0_3_6_4, -1_0.0_0_9_6, -6.8_4_0_5]]	,       dtype=torch.float	,       device=__SCREAMING_SNAKE_CASE	,       )
					self.assertTrue(torch.allclose(outputs.start_logits[:, :10]	,       __SCREAMING_SNAKE_CASE	,       atol=1e-4 ) )
					self.assertTrue(torch.allclose(outputs.end_logits[:, :10]	,       __SCREAMING_SNAKE_CASE	,       atol=1e-4 ) )

