

'''simple docstring'''
import argparse
import os

import torch

from diffusers import (
    CMStochasticIterativeScheduler,
    ConsistencyModelPipeline,
    UNetaDModel,
)


_lowerCamelCase      					=     {
    """sample_size""": 32,
    """in_channels""": 3,
    """out_channels""": 3,
    """layers_per_block""": 2,
    """num_class_embeds""": 1000,
    """block_out_channels""": [32, 64],
    """attention_head_dim""": 8,
    """down_block_types""": [
        """ResnetDownsampleBlock2D""",
        """AttnDownBlock2D""",
    ],
    """up_block_types""": [
        """AttnUpBlock2D""",
        """ResnetUpsampleBlock2D""",
    ],
    """resnet_time_scale_shift""": """scale_shift""",
    """upsample_type""": """resnet""",
    """downsample_type""": """resnet""",
}

_lowerCamelCase      					=     {
    """sample_size""": 64,
    """in_channels""": 3,
    """out_channels""": 3,
    """layers_per_block""": 3,
    """num_class_embeds""": 1000,
    """block_out_channels""": [192, 192 * 2, 192 * 3, 192 * 4],
    """attention_head_dim""": 64,
    """down_block_types""": [
        """ResnetDownsampleBlock2D""",
        """AttnDownBlock2D""",
        """AttnDownBlock2D""",
        """AttnDownBlock2D""",
    ],
    """up_block_types""": [
        """AttnUpBlock2D""",
        """AttnUpBlock2D""",
        """AttnUpBlock2D""",
        """ResnetUpsampleBlock2D""",
    ],
    """resnet_time_scale_shift""": """scale_shift""",
    """upsample_type""": """resnet""",
    """downsample_type""": """resnet""",
}

_lowerCamelCase      					=     {
    """sample_size""": 256,
    """in_channels""": 3,
    """out_channels""": 3,
    """layers_per_block""": 2,
    """num_class_embeds""": None,
    """block_out_channels""": [256, 256, 256 * 2, 256 * 2, 256 * 4, 256 * 4],
    """attention_head_dim""": 64,
    """down_block_types""": [
        """ResnetDownsampleBlock2D""",
        """ResnetDownsampleBlock2D""",
        """ResnetDownsampleBlock2D""",
        """AttnDownBlock2D""",
        """AttnDownBlock2D""",
        """AttnDownBlock2D""",
    ],
    """up_block_types""": [
        """AttnUpBlock2D""",
        """AttnUpBlock2D""",
        """AttnUpBlock2D""",
        """ResnetUpsampleBlock2D""",
        """ResnetUpsampleBlock2D""",
        """ResnetUpsampleBlock2D""",
    ],
    """resnet_time_scale_shift""": """default""",
    """upsample_type""": """resnet""",
    """downsample_type""": """resnet""",
}

_lowerCamelCase      					=     {
    """num_train_timesteps""": 40,
    """sigma_min""": 0.0_02,
    """sigma_max""": 80.0,
}

_lowerCamelCase      					=     {
    """num_train_timesteps""": 201,
    """sigma_min""": 0.0_02,
    """sigma_max""": 80.0,
}

_lowerCamelCase      					=     {
    """num_train_timesteps""": 151,
    """sigma_min""": 0.0_02,
    """sigma_max""": 80.0,
}


def  a__       (    _SCREAMING_SNAKE_CASE   :       Union[str, Any]				)     ->					List[Any]:


	"""simple docstring"""





	if isinstance(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				):
		return v
	if v.lower() in ("yes", "true", "t", "y", "1"):
		return True
	elif v.lower() in ("no", "false", "f", "n", "0"):
		return False
	else:
		raise argparse.ArgumentTypeError("boolean value expected"				)


def  a__       (    _SCREAMING_SNAKE_CASE   :       Optional[int]	,				_SCREAMING_SNAKE_CASE   :       Optional[int]	,				_SCREAMING_SNAKE_CASE   :       str	,				_SCREAMING_SNAKE_CASE   :       List[Any]	,				_SCREAMING_SNAKE_CASE   :       Optional[int]=False				)     ->					List[Any]:


	"""simple docstring"""





	UpperCAmelCase_     :      Tuple						=  checkpoint[F'''{old_prefix}.in_layers.0.weight''']
	UpperCAmelCase_     :      List[Any]						=  checkpoint[F'''{old_prefix}.in_layers.0.bias''']
	UpperCAmelCase_     :      Optional[int]						=  checkpoint[F'''{old_prefix}.in_layers.2.weight''']
	UpperCAmelCase_     :      Any						=  checkpoint[F'''{old_prefix}.in_layers.2.bias''']
	UpperCAmelCase_     :      str						=  checkpoint[F'''{old_prefix}.emb_layers.1.weight''']
	UpperCAmelCase_     :      Dict						=  checkpoint[F'''{old_prefix}.emb_layers.1.bias''']
	UpperCAmelCase_     :      List[Any]						=  checkpoint[F'''{old_prefix}.out_layers.0.weight''']
	UpperCAmelCase_     :      Dict						=  checkpoint[F'''{old_prefix}.out_layers.0.bias''']
	UpperCAmelCase_     :      List[Any]						=  checkpoint[F'''{old_prefix}.out_layers.3.weight''']
	UpperCAmelCase_     :      Tuple						=  checkpoint[F'''{old_prefix}.out_layers.3.bias''']

	if has_skip:
		UpperCAmelCase_     :      Any						=  checkpoint[F'''{old_prefix}.skip_connection.weight''']
		UpperCAmelCase_     :      Tuple						=  checkpoint[F'''{old_prefix}.skip_connection.bias''']

	return new_checkpoint


def  a__       (    _SCREAMING_SNAKE_CASE   :       List[Any]	,				_SCREAMING_SNAKE_CASE   :       Any	,				_SCREAMING_SNAKE_CASE   :       List[str]	,				_SCREAMING_SNAKE_CASE   :       Optional[int]	,				_SCREAMING_SNAKE_CASE   :       Tuple=None				)     ->					str:


	"""simple docstring"""





	UpperCAmelCase_					,					UpperCAmelCase_					,					UpperCAmelCase_     :      List[str]						=  checkpoint[F'''{old_prefix}.qkv.weight'''].chunk(3	,				dim=0				)
	UpperCAmelCase_					,					UpperCAmelCase_					,					UpperCAmelCase_     :      int						=  checkpoint[F'''{old_prefix}.qkv.bias'''].chunk(3	,				dim=0				)

	UpperCAmelCase_     :      Tuple						=  checkpoint[F'''{old_prefix}.norm.weight''']
	UpperCAmelCase_     :      Optional[Any]						=  checkpoint[F'''{old_prefix}.norm.bias''']

	UpperCAmelCase_     :      Union[str, Any]						=  weight_q.squeeze(-1				).squeeze(-1				)
	UpperCAmelCase_     :      int						=  bias_q.squeeze(-1				).squeeze(-1				)
	UpperCAmelCase_     :      str						=  weight_k.squeeze(-1				).squeeze(-1				)
	UpperCAmelCase_     :      Any						=  bias_k.squeeze(-1				).squeeze(-1				)
	UpperCAmelCase_     :      Tuple						=  weight_v.squeeze(-1				).squeeze(-1				)
	UpperCAmelCase_     :      int						=  bias_v.squeeze(-1				).squeeze(-1				)

	UpperCAmelCase_     :      Tuple						=  (
	    checkpoint[F'''{old_prefix}.proj_out.weight'''].squeeze(-1				).squeeze(-1				)
	)
	UpperCAmelCase_     :      Optional[int]						=  checkpoint[F'''{old_prefix}.proj_out.bias'''].squeeze(-1				).squeeze(-1				)

	return new_checkpoint


def  a__       (    _SCREAMING_SNAKE_CASE   :       str	,				_SCREAMING_SNAKE_CASE   :       Union[str, Any]				)     ->					int:


	"""simple docstring"""





	UpperCAmelCase_     :      Any						=  torch.load(_SCREAMING_SNAKE_CASE	,				map_location="cpu"				)
	UpperCAmelCase_     :      List[str]						=  {}

	UpperCAmelCase_     :      str						=  checkpoint["time_embed.0.weight"]
	UpperCAmelCase_     :      Any						=  checkpoint["time_embed.0.bias"]
	UpperCAmelCase_     :      int						=  checkpoint["time_embed.2.weight"]
	UpperCAmelCase_     :      Union[str, Any]						=  checkpoint["time_embed.2.bias"]

	if unet_config["num_class_embeds"] is not None:
		UpperCAmelCase_     :      Optional[Any]						=  checkpoint["label_emb.weight"]

	UpperCAmelCase_     :      Tuple						=  checkpoint["input_blocks.0.0.weight"]
	UpperCAmelCase_     :      List[str]						=  checkpoint["input_blocks.0.0.bias"]

	UpperCAmelCase_     :      Union[str, Any]						=  unet_config["down_block_types"]
	UpperCAmelCase_     :      Tuple						=  unet_config["layers_per_block"]
	UpperCAmelCase_     :      Any						=  unet_config["attention_head_dim"]
	UpperCAmelCase_     :      Optional[Any]						=  unet_config["block_out_channels"]
	UpperCAmelCase_     :      Optional[Any]						=  1
	UpperCAmelCase_     :      Any						=  channels_list[0]

	for i, layer_type in enumerate(_SCREAMING_SNAKE_CASE				):
		UpperCAmelCase_     :      Dict						=  channels_list[i]
		UpperCAmelCase_     :      List[Any]						=  current_channels != prev_channels
		if layer_type == "ResnetDownsampleBlock2D":
			for j in range(_SCREAMING_SNAKE_CASE				):
				UpperCAmelCase_     :      Tuple						=  F'''down_blocks.{i}.resnets.{j}'''
				UpperCAmelCase_     :      Union[str, Any]						=  F'''input_blocks.{current_layer}.0'''
				UpperCAmelCase_     :      Optional[int]						=  True if j == 0 and downsample_block_has_skip else False
				UpperCAmelCase_     :      Optional[int]						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				has_skip=_SCREAMING_SNAKE_CASE				)
				current_layer += 1

		elif layer_type == "AttnDownBlock2D":
			for j in range(_SCREAMING_SNAKE_CASE				):
				UpperCAmelCase_     :      List[Any]						=  F'''down_blocks.{i}.resnets.{j}'''
				UpperCAmelCase_     :      List[str]						=  F'''input_blocks.{current_layer}.0'''
				UpperCAmelCase_     :      Optional[Any]						=  True if j == 0 and downsample_block_has_skip else False
				UpperCAmelCase_     :      Dict						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				has_skip=_SCREAMING_SNAKE_CASE				)
				UpperCAmelCase_     :      Tuple						=  F'''down_blocks.{i}.attentions.{j}'''
				UpperCAmelCase_     :      Tuple						=  F'''input_blocks.{current_layer}.1'''
				UpperCAmelCase_     :      Union[str, Any]						=  convert_attention(
				    _SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				)
				current_layer += 1

		if i != len(_SCREAMING_SNAKE_CASE				) - 1:
			UpperCAmelCase_     :      str						=  F'''down_blocks.{i}.downsamplers.0'''
			UpperCAmelCase_     :      int						=  F'''input_blocks.{current_layer}.0'''
			UpperCAmelCase_     :      Any						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				)
			current_layer += 1

		UpperCAmelCase_     :      List[str]						=  current_channels

	# hardcoded the mid-block for now
	UpperCAmelCase_     :      Tuple						=  "mid_block.resnets.0"
	UpperCAmelCase_     :      Dict						=  "middle_block.0"
	UpperCAmelCase_     :      Union[str, Any]						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				)
	UpperCAmelCase_     :      Optional[int]						=  "mid_block.attentions.0"
	UpperCAmelCase_     :      int						=  "middle_block.1"
	UpperCAmelCase_     :      Union[str, Any]						=  convert_attention(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				)
	UpperCAmelCase_     :      str						=  "mid_block.resnets.1"
	UpperCAmelCase_     :      Dict						=  "middle_block.2"
	UpperCAmelCase_     :      List[str]						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				)

	UpperCAmelCase_     :      int						=  0
	UpperCAmelCase_     :      Tuple						=  unet_config["up_block_types"]

	for i, layer_type in enumerate(_SCREAMING_SNAKE_CASE				):
		if layer_type == "ResnetUpsampleBlock2D":
			for j in range(layers_per_block + 1				):
				UpperCAmelCase_     :      Union[str, Any]						=  F'''up_blocks.{i}.resnets.{j}'''
				UpperCAmelCase_     :      Dict						=  F'''output_blocks.{current_layer}.0'''
				UpperCAmelCase_     :      Optional[Any]						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				has_skip=_SCREAMING_SNAKE_CASE				)
				current_layer += 1

			if i != len(_SCREAMING_SNAKE_CASE				) - 1:
				UpperCAmelCase_     :      int						=  F'''up_blocks.{i}.upsamplers.0'''
				UpperCAmelCase_     :      int						=  F'''output_blocks.{current_layer-1}.1'''
				UpperCAmelCase_     :      Dict						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				)
		elif layer_type == "AttnUpBlock2D":
			for j in range(layers_per_block + 1				):
				UpperCAmelCase_     :      Optional[Any]						=  F'''up_blocks.{i}.resnets.{j}'''
				UpperCAmelCase_     :      List[Any]						=  F'''output_blocks.{current_layer}.0'''
				UpperCAmelCase_     :      int						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				has_skip=_SCREAMING_SNAKE_CASE				)
				UpperCAmelCase_     :      Tuple						=  F'''up_blocks.{i}.attentions.{j}'''
				UpperCAmelCase_     :      Dict						=  F'''output_blocks.{current_layer}.1'''
				UpperCAmelCase_     :      int						=  convert_attention(
				    _SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				)
				current_layer += 1

			if i != len(_SCREAMING_SNAKE_CASE				) - 1:
				UpperCAmelCase_     :      List[str]						=  F'''up_blocks.{i}.upsamplers.0'''
				UpperCAmelCase_     :      Optional[Any]						=  F'''output_blocks.{current_layer-1}.2'''
				UpperCAmelCase_     :      str						=  convert_resnet(_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE	,				_SCREAMING_SNAKE_CASE				)

	UpperCAmelCase_     :      Optional[Any]						=  checkpoint["out.0.weight"]
	UpperCAmelCase_     :      Any						=  checkpoint["out.0.bias"]
	UpperCAmelCase_     :      Optional[int]						=  checkpoint["out.2.weight"]
	UpperCAmelCase_     :      Optional[Any]						=  checkpoint["out.2.bias"]

	return new_checkpoint


if __name__ == "__main__":
					_lowerCamelCase      					=     argparse.ArgumentParser()

					parser.add_argument("""--unet_path""", default=None, type=str, required=True, help="""Path to the unet.pt to convert.""")
					parser.add_argument(
					    """--dump_path""", default=None, type=str, required=True, help="""Path to output the converted UNet model."""
					)
					parser.add_argument("""--class_cond""", default=True, type=str, help="""Whether the model is class-conditional.""")

					_lowerCamelCase      					=     parser.parse_args()
					_lowerCamelCase      					=     strabool(args.class_cond)

					_lowerCamelCase      					=     os.path.basename(args.unet_path)
					print(f"""Checkpoint: {ckpt_name}""")

					# Get U-Net config
					if "imagenet64" in ckpt_name:
										_lowerCamelCase      					=     IMAGENET_64_UNET_CONFIG
					elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)):
										_lowerCamelCase      					=     LSUN_256_UNET_CONFIG
					elif "test" in ckpt_name:
										_lowerCamelCase      					=     TEST_UNET_CONFIG
					else:
										raise ValueError(f"""Checkpoint type {ckpt_name} is not currently supported.""")

					if not args.class_cond:
										_lowerCamelCase      					=     None

					_lowerCamelCase      					=     con_pt_to_diffuser(args.unet_path, unet_config)

					_lowerCamelCase      					=     UNetaDModel(**unet_config)
					image_unet.load_state_dict(converted_unet_ckpt)

					# Get scheduler config
					if "cd" in ckpt_name or "test" in ckpt_name:
										_lowerCamelCase      					=     CD_SCHEDULER_CONFIG
					elif "ct" in ckpt_name and "imagenet64" in ckpt_name:
										_lowerCamelCase      					=     CT_IMAGENET_64_SCHEDULER_CONFIG
					elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)):
										_lowerCamelCase      					=     CT_LSUN_256_SCHEDULER_CONFIG
					else:
										raise ValueError(f"""Checkpoint type {ckpt_name} is not currently supported.""")

					_lowerCamelCase      					=     CMStochasticIterativeScheduler(**scheduler_config)

					_lowerCamelCase      					=     ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler)
					consistency_model.save_pretrained(args.dump_path)






from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import numpy
							import tensorflow as tf

							from transformers import (
							    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
							    BertConfig,
							    DPRConfig,
							    TFDPRContextEncoder,
							    TFDPRQuestionEncoder,
							    TFDPRReader,
							)




class   __lowercase						:






	def __init__(self				: Dict      ,	snake_case				: Dict      ,	snake_case				: List[Any]=13      ,	snake_case				: int=7      ,	snake_case				: List[str]=True      ,	snake_case				: List[Any]=True      ,	snake_case				: Union[str, Any]=True      ,	snake_case				: Dict=True      ,	snake_case				: Optional[Any]=99      ,	snake_case				: str=32      ,	snake_case				: Any=2      ,	snake_case				: Optional[Any]=4      ,	snake_case				: int=37      ,	snake_case				: Dict="gelu"      ,	snake_case				: Dict=0.1      ,	snake_case				: Dict=0.1      ,	snake_case				: Optional[int]=512      ,	snake_case				: Dict=16      ,	snake_case				: Union[str, Any]=2      ,	snake_case				: Dict=0.02      ,	snake_case				: str=3      ,	snake_case				: str=4      ,	snake_case				: Optional[Any]=None      ,	snake_case				: str=0      ,	) ->			Optional[Any]:
							_lowercase       :	int           =   parent
							_lowercase       :	Tuple           =   batch_size
							_lowercase       :	str           =   seq_length
							_lowercase       :	Optional[Any]           =   is_training
							_lowercase       :	List[Any]           =   use_input_mask
							_lowercase       :	Tuple           =   use_token_type_ids
							_lowercase       :	List[Any]           =   use_labels
							_lowercase       :	List[str]           =   vocab_size
							_lowercase       :	Dict           =   hidden_size
							_lowercase       :	str           =   num_hidden_layers
							_lowercase       :	Tuple           =   num_attention_heads
							_lowercase       :	Dict           =   intermediate_size
							_lowercase       :	List[Any]           =   hidden_act
							_lowercase       :	Optional[Any]           =   hidden_dropout_prob
							_lowercase       :	Dict           =   attention_probs_dropout_prob
							_lowercase       :	int           =   max_position_embeddings
							_lowercase       :	Tuple           =   type_vocab_size
							_lowercase       :	List[str]           =   type_sequence_label_size
							_lowercase       :	str           =   initializer_range
							_lowercase       :	Tuple           =   num_labels
							_lowercase       :	List[Any]           =   num_choices
							_lowercase       :	Dict           =   scope
							_lowercase       :	Optional[Any]           =   projection_dim






	def       _a(self				: Tuple    ) ->			List[str]:
							_lowercase       :	Optional[int]           =   ids_tensor([self.batch_size, self.seq_length]      ,	self.vocab_size    )

							_lowercase       :	Optional[int]           =   None
							if self.use_input_mask:
													# follow test_modeling_tf_ctrl.py
													_lowercase       :	List[Any]           =   random_attention_mask([self.batch_size, self.seq_length]    )

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

							_lowercase       :	Tuple           =   None
							_lowercase       :	Optional[Any]           =   None
							_lowercase       :	Optional[Any]           =   None
							if self.use_labels:
													_lowercase       :	Optional[Any]           =   ids_tensor([self.batch_size]      ,	self.type_sequence_label_size    )
													_lowercase       :	Union[str, Any]           =   ids_tensor([self.batch_size, self.seq_length]      ,	self.num_labels    )
													_lowercase       :	List[str]           =   ids_tensor([self.batch_size]      ,	self.num_choices    )

							_lowercase       :	Union[str, Any]           =   BertConfig(
							    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      ,	is_decoder=snake_case      ,	initializer_range=self.initializer_range      ,	)
							_lowercase       :	Any           =   DPRConfig(projection_dim=self.projection_dim      ,	**config.to_dict()    )

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






	def       _a(self				: Union[str, Any]      ,	snake_case				: Any      ,	snake_case				: Tuple      ,	snake_case				: str      ,	snake_case				: str      ,	snake_case				: Union[str, Any]      ,	snake_case				: Tuple      ,	snake_case				: str    ) ->			List[str]:
							_lowercase       :	List[Any]           =   TFDPRContextEncoder(config=snake_case    )
							_lowercase       :	Dict           =   model(snake_case      ,	attention_mask=snake_case      ,	token_type_ids=snake_case    )
							_lowercase       :	Optional[int]           =   model(snake_case      ,	token_type_ids=snake_case    )
							_lowercase       :	Union[str, Any]           =   model(snake_case    )
							self.parent.assertEqual(result.pooler_output.shape      ,	(self.batch_size, self.projection_dim or self.hidden_size)    )






	def       _a(self				: List[Any]      ,	snake_case				: List[str]      ,	snake_case				: Tuple      ,	snake_case				: Any      ,	snake_case				: int      ,	snake_case				: Optional[int]      ,	snake_case				: Any      ,	snake_case				: Union[str, Any]    ) ->			int:
							_lowercase       :	Optional[Any]           =   TFDPRQuestionEncoder(config=snake_case    )
							_lowercase       :	int           =   model(snake_case      ,	attention_mask=snake_case      ,	token_type_ids=snake_case    )
							_lowercase       :	str           =   model(snake_case      ,	token_type_ids=snake_case    )
							_lowercase       :	str           =   model(snake_case    )
							self.parent.assertEqual(result.pooler_output.shape      ,	(self.batch_size, self.projection_dim or self.hidden_size)    )






	def       _a(self				: Union[str, Any]      ,	snake_case				: Any      ,	snake_case				: int      ,	snake_case				: List[str]      ,	snake_case				: Union[str, Any]      ,	snake_case				: Tuple      ,	snake_case				: Optional[Any]      ,	snake_case				: Union[str, Any]    ) ->			Any:
							_lowercase       :	Any           =   TFDPRReader(config=snake_case    )
							_lowercase       :	Optional[Any]           =   model(snake_case      ,	attention_mask=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				: int    ) ->			Optional[Any]:
							_lowercase       :	Union[str, Any]           =   self.prepare_config_and_inputs()
							(
							    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    
							)       :	int           =   config_and_inputs
							_lowercase       :	int           =   {"input_ids": input_ids}
							return config, inputs_dict




@require_tf
class   __lowercase						(						__snake_case   , __snake_case   , unittest.TestCase       ):
	_A       =   (
	    (
	        TFDPRContextEncoder,
	        TFDPRQuestionEncoder,
	        TFDPRReader,
	    )
	    if is_tf_available()
	    else ()
	)
	_A       =   {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

	_A       =   False
	_A       =   False
	_A       =   False
	_A       =   False
	_A       =   False






	def       _a(self				: str    ) ->			List[str]:
							_lowercase       :	List[Any]           =   TFDPRModelTester(self    )
							_lowercase       :	Any           =   ConfigTester(self      ,	config_class=snake_case      ,	hidden_size=37    )






	def       _a(self				: str    ) ->			Union[str, Any]:
							self.config_tester.run_common_tests()






	def       _a(self				: Any    ) ->			Tuple:
							_lowercase       :	List[str]           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_context_encoder(*snake_case    )






	def       _a(self				: Dict    ) ->			Optional[Any]:
							_lowercase       :	str           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_question_encoder(*snake_case    )






	def       _a(self				: List[Any]    ) ->			Any:
							_lowercase       :	Optional[Any]           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_reader(*snake_case    )






	@slow
	def       _a(self				: int    ) ->			str:
							for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowercase       :	str           =   TFDPRContextEncoder.from_pretrained(snake_case    )
													self.assertIsNotNone(snake_case    )

							for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowercase       :	List[str]           =   TFDPRContextEncoder.from_pretrained(snake_case    )
													self.assertIsNotNone(snake_case    )

							for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowercase       :	int           =   TFDPRQuestionEncoder.from_pretrained(snake_case    )
													self.assertIsNotNone(snake_case    )

							for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowercase       :	Any           =   TFDPRReader.from_pretrained(snake_case    )
													self.assertIsNotNone(snake_case    )




@require_tf
class   __lowercase						(						unittest.TestCase       ):






	@slow
	def       _a(self				: Dict    ) ->			Any:
							_lowercase       :	Any           =   TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"    )

							_lowercase       :	Optional[int]           =   tf.constant(
							    [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]    )  # [CLS] hello, is my dog cute? [SEP]
							_lowercase       :	List[Any]           =   model(snake_case    )[0]  # embedding shape = (1, 768)
							# compare the actual values for a slice.
							_lowercase       :	Optional[Any]           =   tf.constant(
							    [
							        [
							            0.03_23_62_53,
							            0.12_75_33_35,
							            0.16_81_85_09,
							            0.00_27_97_86,
							            0.3_89_69_33,
							            0.24_26_49_45,
							            0.2_17_89_71,
							            -0.02_33_52_27,
							            -0.08_48_19_59,
							            -0.14_32_41_17,
							        ]
							    ]    )
							self.assertTrue(numpy.allclose(output[:, :10].numpy()      ,	expected_slice.numpy()      ,	atol=1e-4    )    )




