

"""simple docstring"""




import glob
import os
from typing import Dict, List, Union

import torch

from diffusers.utils import is_safetensors_available


if is_safetensors_available():
	import safetensors.torch

from huggingface_hub import snapshot_download

from diffusers import DiffusionPipeline, __version__
from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME
from diffusers.utils import CONFIG_NAME, DIFFUSERS_CACHE, ONNX_WEIGHTS_NAME, WEIGHTS_NAME





class 			__UpperCamelCase (   _A						):


		def __init__(self :		List[str]):
							self.register_to_config()
							super().__init__()


		def 					SCREAMING_SNAKE_CASE__   (self :		str     ,     __SCREAMING_SNAKE_CASE :		Union[str, Any]     ,     __SCREAMING_SNAKE_CASE :		str):
							if dicta == dicta:
												return True
							else:
												A			,    A			=					self._remove_meta_keys(__SCREAMING_SNAKE_CASE)
												A			,    A			=					self._remove_meta_keys(__SCREAMING_SNAKE_CASE)
												if configa == configa:
																	print(F"""Warning !: Mismatch in keys {meta_keysa} and {meta_keysa}.""")
																	return True
							return False


		def 					SCREAMING_SNAKE_CASE__   (self :		List[str]     ,     __SCREAMING_SNAKE_CASE :		Dict):
							A			=					[]
							A			=					config_dict.copy()
							for key in config_dict.keys():
												if key.startswith("_"):
																	temp_dict.pop(__SCREAMING_SNAKE_CASE)
																	meta_keys.append(__SCREAMING_SNAKE_CASE)
							return (temp_dict, meta_keys)


		@torch.no_grad()
		def 					SCREAMING_SNAKE_CASE__   (self :		List[str]     ,     __SCREAMING_SNAKE_CASE :		List[Union[str, os.PathLike]]     ,     **__SCREAMING_SNAKE_CASE :		Tuple):
							A			=					kwargs.pop("cache_dir"     ,     __SCREAMING_SNAKE_CASE)
							A			=					kwargs.pop("resume_download"     ,     __SCREAMING_SNAKE_CASE)
							A			=					kwargs.pop("force_download"     ,     __SCREAMING_SNAKE_CASE)
							A			=					kwargs.pop("proxies"     ,     __SCREAMING_SNAKE_CASE)
							A			=					kwargs.pop("local_files_only"     ,     __SCREAMING_SNAKE_CASE)
							A			=					kwargs.pop("use_auth_token"     ,     __SCREAMING_SNAKE_CASE)
							A			=					kwargs.pop("revision"     ,     __SCREAMING_SNAKE_CASE)
							A			=					kwargs.pop("torch_dtype"     ,     __SCREAMING_SNAKE_CASE)
							A			=					kwargs.pop("device_map"     ,     __SCREAMING_SNAKE_CASE)

							A			=					kwargs.pop("alpha"     ,     0.5)
							A			=					kwargs.pop("interp"     ,     __SCREAMING_SNAKE_CASE)

							print("Received list"     ,     __SCREAMING_SNAKE_CASE)
							print(F"""Combining with alpha={alpha}, interpolation mode={interp}""")

							A			=					len(__SCREAMING_SNAKE_CASE)
							# Ignore result from model_index_json comparision of the two checkpoints
							A			=					kwargs.pop("force"     ,     __SCREAMING_SNAKE_CASE)

							# If less than 2 checkpoints, nothing to merge. If more than 3, not supported for now.
							if checkpoint_count > 3 or checkpoint_count < 2:
												raise ValueError(
												    "Received incorrect number of checkpoints to merge. Ensure that either 2 or 3 checkpoints are being"
												    " passed.")

							print("Received the right number of checkpoints")
							# chkpt0, chkpt1 = pretrained_model_name_or_path_list[0:2]
							# chkpt2 = pretrained_model_name_or_path_list[2] if checkpoint_count == 3 else None

							# Validate that the checkpoints can be merged
							# Step 1: Load the model config and compare the checkpoints. We'll compare the model_index.json first while ignoring the keys starting with '_'
							A			=					[]
							for pretrained_model_name_or_path in pretrained_model_name_or_path_list:
												A			=					DiffusionPipeline.load_config(
												    __SCREAMING_SNAKE_CASE     ,     cache_dir=__SCREAMING_SNAKE_CASE     ,     resume_download=__SCREAMING_SNAKE_CASE     ,     force_download=__SCREAMING_SNAKE_CASE     ,     proxies=__SCREAMING_SNAKE_CASE     ,     local_files_only=__SCREAMING_SNAKE_CASE     ,     use_auth_token=__SCREAMING_SNAKE_CASE     ,     revision=__SCREAMING_SNAKE_CASE     ,     )
												config_dicts.append(__SCREAMING_SNAKE_CASE)

							A			=					True
							for idx in range(1     ,     len(__SCREAMING_SNAKE_CASE)):
												comparison_result &= self._compare_model_configs(config_dicts[idx - 1]     ,     config_dicts[idx])
												if not force and comparison_result is False:
																	raise ValueError("Incompatible checkpoints. Please check model_index.json for the models.")
																	print(config_dicts[0]     ,     config_dicts[1])
							print("Compatible model_index.json files found")
							# Step 2: Basic Validation has succeeded. Let's download the models and save them into our local files.
							A			=					[]
							for pretrained_model_name_or_path, config_dict in zip(__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE):
												A			=					[k for k in config_dict.keys() if not k.startswith("_")]
												A			=					[os.path.join(__SCREAMING_SNAKE_CASE     ,     "*") for k in folder_names]
												allow_patterns += [
												    WEIGHTS_NAME,
												    SCHEDULER_CONFIG_NAME,
												    CONFIG_NAME,
												    ONNX_WEIGHTS_NAME,
												    DiffusionPipeline.config_name,
												]
												A			=					config_dict.get("_class_name")
												A			=					{"diffusers": __version__, "pipeline_class": requested_pipeline_class}

												A			=					(
												    pretrained_model_name_or_path
												    if os.path.isdir(__SCREAMING_SNAKE_CASE)
												    else snapshot_download(
												        __SCREAMING_SNAKE_CASE     ,     cache_dir=__SCREAMING_SNAKE_CASE     ,     resume_download=__SCREAMING_SNAKE_CASE     ,     proxies=__SCREAMING_SNAKE_CASE     ,     local_files_only=__SCREAMING_SNAKE_CASE     ,     use_auth_token=__SCREAMING_SNAKE_CASE     ,     revision=__SCREAMING_SNAKE_CASE     ,     allow_patterns=__SCREAMING_SNAKE_CASE     ,     user_agent=__SCREAMING_SNAKE_CASE     ,     )
												)
												print("Cached Folder"     ,     __SCREAMING_SNAKE_CASE)
												cached_folders.append(__SCREAMING_SNAKE_CASE)

							# Step 3:-
							# Load the first checkpoint as a diffusion pipeline and modify its module state_dict in place
							A			=					DiffusionPipeline.from_pretrained(
							    cached_folders[0]     ,     torch_dtype=__SCREAMING_SNAKE_CASE     ,     device_map=__SCREAMING_SNAKE_CASE)
							final_pipe.to(self.device)

							A			=					None
							if len(__SCREAMING_SNAKE_CASE) > 2:
												A			=					os.path.join(cached_folders[2])

							if interp == "sigmoid":
												A			=					CheckpointMergerPipeline.sigmoid
							elif interp == "inv_sigmoid":
												A			=					CheckpointMergerPipeline.inv_sigmoid
							elif interp == "add_diff":
												A			=					CheckpointMergerPipeline.add_difference
							else:
												A			=					CheckpointMergerPipeline.weighted_sum

							# Find each module's state dict.
							for attr in final_pipe.config.keys():
												if not attr.startswith("_"):
																	A			=					os.path.join(cached_folders[1]     ,     __SCREAMING_SNAKE_CASE)
																	if os.path.exists(__SCREAMING_SNAKE_CASE):
																						A			=					[
																						    *glob.glob(os.path.join(__SCREAMING_SNAKE_CASE     ,     "*.safetensors")),
																						    *glob.glob(os.path.join(__SCREAMING_SNAKE_CASE     ,     "*.bin")),
																						]
																						A			=					files[0] if len(__SCREAMING_SNAKE_CASE) > 0 else None
																	if len(__SCREAMING_SNAKE_CASE) < 3:
																						A			=					None
																	else:
																						A			=					os.path.join(cached_folders[2]     ,     __SCREAMING_SNAKE_CASE)
																						if os.path.exists(__SCREAMING_SNAKE_CASE):
																											A			=					[
																											    *glob.glob(os.path.join(__SCREAMING_SNAKE_CASE     ,     "*.safetensors")),
																											    *glob.glob(os.path.join(__SCREAMING_SNAKE_CASE     ,     "*.bin")),
																											]
																											A			=					files[0] if len(__SCREAMING_SNAKE_CASE) > 0 else None
                # For an attr if both checkpoint_path_1 and 2 are None, ignore.
                # If atleast one is present, deal with it according to interp method, of course only if the state_dict keys match.
																	if checkpoint_path_a is None and checkpoint_path_a is None:
																						print(F"""Skipping {attr}: not present in 2nd or 3d model""")
																						continue
																	try:
																						A			=					getattr(__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE)
																						if isinstance(__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE):  # ignore requires_safety_checker boolean
																											continue
																						A			=					getattr(__SCREAMING_SNAKE_CASE     ,     "state_dict")
																						A			=					theta_a()

																						A			=					getattr(__SCREAMING_SNAKE_CASE     ,     "load_state_dict")
																						A			=					(
																						    safetensors.torch.load_file(__SCREAMING_SNAKE_CASE)
																						    if (is_safetensors_available() and checkpoint_path_a.endswith(".safetensors"))
																						    else torch.load(__SCREAMING_SNAKE_CASE     ,     map_location="cpu")
																						)
																						A			=					None
																						if checkpoint_path_a:
																											A			=					(
																											    safetensors.torch.load_file(__SCREAMING_SNAKE_CASE)
																											    if (is_safetensors_available() and checkpoint_path_a.endswith(".safetensors"))
																											    else torch.load(__SCREAMING_SNAKE_CASE     ,     map_location="cpu")
																											)

																						if not theta_a.keys() == theta_a.keys():
																											print(F"""Skipping {attr}: key mismatch""")
																											continue
																						if theta_a and not theta_a.keys() == theta_a.keys():
																											print(F"""Skipping {attr}:y mismatch""")
																	except Exception as e:
																						print(F"""Skipping {attr} do to an unexpected error: {str(__SCREAMING_SNAKE_CASE)}""")
																						continue
																	print(F"""MERGING {attr}""")

																	for key in theta_a.keys():
																						if theta_a:
																											A			=					theta_func(theta_a[key]     ,     theta_a[key]     ,     theta_a[key]     ,     __SCREAMING_SNAKE_CASE)
																						else:
																											A			=					theta_func(theta_a[key]     ,     theta_a[key]     ,     __SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE)

																	del theta_a
																	del theta_a
																	update_theta_a(__SCREAMING_SNAKE_CASE)

																	del theta_a
							return final_pipe


		@staticmethod
		def 					SCREAMING_SNAKE_CASE__   (__SCREAMING_SNAKE_CASE :		Optional[int]     ,     __SCREAMING_SNAKE_CASE :		List[str]     ,     __SCREAMING_SNAKE_CASE :		Tuple     ,     __SCREAMING_SNAKE_CASE :		Tuple):
							return ((1 - alpha) * thetaa) + (alpha * thetaa)


		@staticmethod
		def 					SCREAMING_SNAKE_CASE__   (__SCREAMING_SNAKE_CASE :		Any     ,     __SCREAMING_SNAKE_CASE :		Tuple     ,     __SCREAMING_SNAKE_CASE :		Union[str, Any]     ,     __SCREAMING_SNAKE_CASE :		Dict):
							A			=					alpha * alpha * (3 - (2 * alpha))
							return thetaa + ((thetaa - thetaa) * alpha)


		@staticmethod
		def 					SCREAMING_SNAKE_CASE__   (__SCREAMING_SNAKE_CASE :		Optional[Any]     ,     __SCREAMING_SNAKE_CASE :		List[Any]     ,     __SCREAMING_SNAKE_CASE :		Dict     ,     __SCREAMING_SNAKE_CASE :		List[str]):
							import math

							A			=					0.5 - math.sin(math.asin(1.0 - 2.0 * alpha) / 3.0)
							return thetaa + ((thetaa - thetaa) * alpha)


		@staticmethod
		def 					SCREAMING_SNAKE_CASE__   (__SCREAMING_SNAKE_CASE :		Dict     ,     __SCREAMING_SNAKE_CASE :		List[Any]     ,     __SCREAMING_SNAKE_CASE :		Tuple     ,     __SCREAMING_SNAKE_CASE :		Dict):
							return thetaa + (thetaa - thetaa) * (1.0 - alpha)







"""simple docstring"""

from transformers import (
    DPRContextEncoderTokenizer,
    DPRContextEncoderTokenizerFast,
    DPRQuestionEncoderTokenizer,
    DPRQuestionEncoderTokenizerFast,
    DPRReaderOutput,
    DPRReaderTokenizer,
    DPRReaderTokenizerFast,
)
from transformers.testing_utils import require_tokenizers, slow
from transformers.tokenization_utils_base import BatchEncoding

from ..bert.test_tokenization_bert import BertTokenizationTest






@require_tokenizers
class        _lowercase   (				__UpperCAmelCase		):
	lowercase_        =	DPRContextEncoderTokenizer
	lowercase_        =	DPRContextEncoderTokenizerFast
	lowercase_        =	True






@require_tokenizers
class        _lowercase   (				__UpperCAmelCase		):
	lowercase_        =	DPRQuestionEncoderTokenizer
	lowercase_        =	DPRQuestionEncoderTokenizerFast
	lowercase_        =	True






@require_tokenizers
class        _lowercase   (				__UpperCAmelCase		):
	lowercase_        =	DPRReaderTokenizer
	lowercase_        =	DPRReaderTokenizerFast
	lowercase_        =	True




	@slow
	def 			_UpperCamelCase    ( self					)		->							Any:
			lowerCamelCase     :			str		=    self.tokenizer_class.from_pretrained('bert-base-uncased'					)

			lowerCamelCase     :			int		=    tokenizer.encode('question sequence'		, add_special_tokens=UpperCAmelCase_					)
			lowerCamelCase     :			List[Any]		=    tokenizer.encode('title sequence'		, add_special_tokens=UpperCAmelCase_					)
			lowerCamelCase     :			Optional[int]		=    tokenizer.encode('text sequence ' * 4		, add_special_tokens=UpperCAmelCase_					)
			lowerCamelCase     :			Union[str, Any]		=    [[101] + text_a + [102] + text_a + [102] + text_a]
			lowerCamelCase     :			List[str]		=    BatchEncoding({'input_ids': input_ids}					)

			lowerCamelCase     :			Optional[int]		=    [[0] * len(input_ids[0]					)]
			lowerCamelCase     :			str		=    [[0] * len(input_ids[0]					)]
			lowerCamelCase     :			List[Any]		=    [0]
			lowerCamelCase     :			Tuple		=    DPRReaderOutput(UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_					)

			lowerCamelCase	,    lowerCamelCase     :			Optional[int]		=    8, 9
			lowerCamelCase     :			Optional[int]		=    10
			lowerCamelCase     :			int		=    10
			lowerCamelCase     :			List[Any]		=    tokenizer.decode_best_spans(UpperCAmelCase_		, UpperCAmelCase_					)
			self.assertEqual(predicted_spans[0].start_index		, UpperCAmelCase_					)
			self.assertEqual(predicted_spans[0].end_index		, UpperCAmelCase_					)
			self.assertEqual(predicted_spans[0].doc_id		, 0					)




	@slow
	def 			_UpperCamelCase    ( self					)		->							List[Any]:
			lowerCamelCase     :			Union[str, Any]		=    self.tokenizer_class.from_pretrained('bert-base-uncased'					)

			lowerCamelCase     :			List[str]		=    tokenizer.encode('question sequence'		, add_special_tokens=UpperCAmelCase_					)
			lowerCamelCase     :			List[str]		=    tokenizer.encode('title sequence'		, add_special_tokens=UpperCAmelCase_					)
			lowerCamelCase     :			List[Any]		=    tokenizer.encode('text sequence'		, add_special_tokens=UpperCAmelCase_					)
			lowerCamelCase     :			List[Any]		=    [101] + text_a + [102] + text_a + [102] + text_a
			lowerCamelCase     :			Optional[Any]		=    tokenizer(questions=['question sequence']		, titles=['title sequence']		, texts=['text sequence']					)
			self.assertIn('input_ids'		, UpperCAmelCase_					)
			self.assertIn('attention_mask'		, UpperCAmelCase_					)
			self.assertListEqual(encoded_input['input_ids'][0]		, UpperCAmelCase_					)






