






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__     ):

				"""simple docstring"""




				def __init__(					self						:   Optional[int]  ):

								'''simple docstring'''
								self.register_to_config()
								super().__init__()




				def     lowercase__       (					self						:   Optional[int],  lowerCamelCase						:   Any,  lowerCamelCase						:   str  ):

								'''simple docstring'''
								if dicta == dicta:
												return True
								else:
												lowercase__			,						lowercase__       			=      self._remove_meta_keys(lowerCamelCase  )
												lowercase__			,						lowercase__       			=      self._remove_meta_keys(lowerCamelCase  )
												if configa == configa:
																print(F"""Warning !: Mismatch in keys {meta_keysa} and {meta_keysa}."""  )
																return True
								return False




				def     lowercase__       (					self						:   List[str],  lowerCamelCase						:   Dict  ):

								'''simple docstring'''
								lowercase__       			=      []
								lowercase__       			=      config_dict.copy()
								for key in config_dict.keys():
												if key.startswith('''_'''  ):
																temp_dict.pop(lowerCamelCase  )
																meta_keys.append(lowerCamelCase  )
								return (temp_dict, meta_keys)




				@torch.no_grad()
				def     lowercase__       (					self						:   int,  lowerCamelCase						:   List[Union[str, os.PathLike]],  **lowerCamelCase						:   str  ):

								'''simple docstring'''
								lowercase__       			=      kwargs.pop('''cache_dir''',  lowerCamelCase  )
								lowercase__       			=      kwargs.pop('''resume_download''',  lowerCamelCase  )
								lowercase__       			=      kwargs.pop('''force_download''',  lowerCamelCase  )
								lowercase__       			=      kwargs.pop('''proxies''',  lowerCamelCase  )
								lowercase__       			=      kwargs.pop('''local_files_only''',  lowerCamelCase  )
								lowercase__       			=      kwargs.pop('''use_auth_token''',  lowerCamelCase  )
								lowercase__       			=      kwargs.pop('''revision''',  lowerCamelCase  )
								lowercase__       			=      kwargs.pop('''torch_dtype''',  lowerCamelCase  )
								lowercase__       			=      kwargs.pop('''device_map''',  lowerCamelCase  )

								lowercase__       			=      kwargs.pop('''alpha''',  0.5  )
								lowercase__       			=      kwargs.pop('''interp''',  lowerCamelCase  )

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

								lowercase__       			=      len(lowerCamelCase  )
								# Ignore result from model_index_json comparision of the two checkpoints
								lowercase__       			=      kwargs.pop('''force''',  lowerCamelCase  )

								# 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 '_'
								lowercase__       			=      []
								for pretrained_model_name_or_path in pretrained_model_name_or_path_list:
												lowercase__       			=      DiffusionPipeline.load_config(
												    lowerCamelCase,  cache_dir=lowerCamelCase,  resume_download=lowerCamelCase,  force_download=lowerCamelCase,  proxies=lowerCamelCase,  local_files_only=lowerCamelCase,  use_auth_token=lowerCamelCase,  revision=lowerCamelCase,  )
												config_dicts.append(lowerCamelCase  )

								lowercase__       			=      True
								for idx in range(1,  len(lowerCamelCase  )  ):
												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.
								lowercase__       			=      []
								for pretrained_model_name_or_path, config_dict in zip(lowerCamelCase,  lowerCamelCase  ):
												lowercase__       			=      [k for k in config_dict.keys() if not k.startswith('''_'''  )]
												lowercase__       			=      [os.path.join(lowerCamelCase,  '''*'''  ) for k in folder_names]
												allow_patterns += [
												    WEIGHTS_NAME,
												    SCHEDULER_CONFIG_NAME,
												    CONFIG_NAME,
												    ONNX_WEIGHTS_NAME,
												    DiffusionPipeline.config_name,
												]
												lowercase__       			=      config_dict.get('''_class_name'''  )
												lowercase__       			=      {'''diffusers''': __version__, '''pipeline_class''': requested_pipeline_class}

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

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

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

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

								# Find each module's state dict.
								for attr in final_pipe.config.keys():
												if not attr.startswith('''_'''  ):
																lowercase__       			=      os.path.join(cached_folders[1],  lowerCamelCase  )
																if os.path.exists(lowerCamelCase  ):
																				lowercase__       			=      [
																				    *glob.glob(os.path.join(lowerCamelCase,  '''*.safetensors'''  )  ),
																				    *glob.glob(os.path.join(lowerCamelCase,  '''*.bin'''  )  ),
																				]
																				lowercase__       			=      files[0] if len(lowerCamelCase  ) > 0 else None
																if len(lowerCamelCase  ) < 3:
																				lowercase__       			=      None
																else:
																				lowercase__       			=      os.path.join(cached_folders[2],  lowerCamelCase  )
																				if os.path.exists(lowerCamelCase  ):
																								lowercase__       			=      [
																								    *glob.glob(os.path.join(lowerCamelCase,  '''*.safetensors'''  )  ),
																								    *glob.glob(os.path.join(lowerCamelCase,  '''*.bin'''  )  ),
																								]
																								lowercase__       			=      files[0] if len(lowerCamelCase  ) > 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:
																				lowercase__       			=      getattr(lowerCamelCase,  lowerCamelCase  )
																				if isinstance(lowerCamelCase,  lowerCamelCase  ):  # ignore requires_safety_checker boolean
																								continue
																				lowercase__       			=      getattr(lowerCamelCase,  '''state_dict'''  )
																				lowercase__       			=      theta_a()

																				lowercase__       			=      getattr(lowerCamelCase,  '''load_state_dict'''  )
																				lowercase__       			=      (
																				    safetensors.torch.load_file(lowerCamelCase  )
																				    if (is_safetensors_available() and checkpoint_path_a.endswith('''.safetensors'''  ))
																				    else torch.load(lowerCamelCase,  map_location='''cpu'''  )
																				)
																				lowercase__       			=      None
																				if checkpoint_path_a:
																								lowercase__       			=      (
																								    safetensors.torch.load_file(lowerCamelCase  )
																								    if (is_safetensors_available() and checkpoint_path_a.endswith('''.safetensors'''  ))
																								    else torch.load(lowerCamelCase,  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(lowerCamelCase  )}"""  )
																				continue
																print(F"""MERGING {attr}"""  )

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

																del theta_a
																del theta_a
																update_theta_a(lowerCamelCase  )

																del theta_a
								return final_pipe




				@staticmethod
				def     lowercase__       (					lowerCamelCase						:   List[Any],  lowerCamelCase						:   Union[str, Any],  lowerCamelCase						:   Optional[Any],  lowerCamelCase						:   List[str]  ):

								'''simple docstring'''
								return ((1 - alpha) * thetaa) + (alpha * thetaa)




				@staticmethod
				def     lowercase__       (					lowerCamelCase						:   int,  lowerCamelCase						:   Any,  lowerCamelCase						:   str,  lowerCamelCase						:   Tuple  ):

								'''simple docstring'''
								lowercase__       			=      alpha * alpha * (3 - (2 * alpha))
								return thetaa + ((thetaa - thetaa) * alpha)




				@staticmethod
				def     lowercase__       (					lowerCamelCase						:   List[str],  lowerCamelCase						:   str,  lowerCamelCase						:   Optional[int],  lowerCamelCase						:   str  ):

								'''simple docstring'''
								import math

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




				@staticmethod
				def     lowercase__       (					lowerCamelCase						:   Tuple,  lowerCamelCase						:   int,  lowerCamelCase						:   Optional[int],  lowerCamelCase						:   int  ):

								'''simple docstring'''
								return thetaa + (thetaa - thetaa) * (1.0 - alpha)












import dataclasses
import enum
import functools
import math
import re

# The following script is adapted from the script of TaPas.
# Original: https://github.com/google-research/tapas/master/wikisql_utils.py
from typing import Any, List, Text


A__     :      Dict             =			'none'
A__     :      Optional[Any]             =			'none'






def   a      (						lowerCamelCase_		,       lowerCamelCase_    ):






				'''simple docstring'''
				lowercase__       			=      value.split(lowerCamelCase_    )
				return len(lowerCamelCase_    ) > 1 and any((len(lowerCamelCase_    ) == 3 for x in split)    )






def   a      (						lowerCamelCase_    ):






				'''simple docstring'''
				if isinstance(lowerCamelCase_		,       lowerCamelCase_    ):
								return value
				if isinstance(lowerCamelCase_		,       lowerCamelCase_    ):
								return float(lowerCamelCase_    )
				if not isinstance(lowerCamelCase_		,       lowerCamelCase_    ):
								raise ValueError('''Argument value is not a string. Can\'t parse it as float'''    )
				lowercase__       			=      value

				try:
								# Example: 1,000.7
								if "." in sanitized and "," in sanitized:
												return float(sanitized.replace(''','''		,       ''''''    )    )
								# 1,000
								if "," in sanitized and _split_thousands(''','''		,       lowerCamelCase_    ):
												return float(sanitized.replace(''','''		,       ''''''    )    )
								# 5,5556
								if "," in sanitized and sanitized.count(''','''    ) == 1 and not _split_thousands(''','''		,       lowerCamelCase_    ):
												return float(sanitized.replace(''','''		,       '''.'''    )    )
								# 0.0.0.1
								if sanitized.count('''.'''    ) > 1:
												return float(sanitized.replace('''.'''		,       ''''''    )    )
								# 0,0,0,1
								if sanitized.count(''','''    ) > 1:
												return float(sanitized.replace(''','''		,       ''''''    )    )
								return float(lowerCamelCase_    )
				except ValueError:
								# Avoid adding the sanitized value in the error message.
								raise ValueError('''Unable to convert value to float'''    )






def   a      (						lowerCamelCase_    ):






				'''simple docstring'''
				if answer is None:
								return None
				try:
								lowercase__       			=      convert_to_float(lowerCamelCase_    )
								if isinstance(lowerCamelCase_		,       lowerCamelCase_    ) and math.isnan(lowerCamelCase_    ):
												return None
								return value
				except ValueError:
								return answer.lower()


A__     :      Tuple             =			{
    'text': lambda x: x,
    'real': convert_to_float,
}
class 				_UpperCAmelCase (	enum.Enum     ):

				"""simple docstring"""




				lowercase__    							=	0
				lowercase__    							=	1
				lowercase__    							=	2
				lowercase__    							=	3
				lowercase__    							=	4
				lowercase__    							=	5



class 				_UpperCAmelCase (	enum.Enum     ):

				"""simple docstring"""




				lowercase__    							=	0
				lowercase__    							=	1
				lowercase__    							=	2



@dataclasses.dataclass
class 				_UpperCAmelCase :

				"""simple docstring"""




				lowercase__    							=	42
				lowercase__    							=	42
				lowercase__    							=	42



A__     :      str             =			re.compile(r'\w+|[^\w\s]+', re.UNICODE | re.MULTILINE | re.DOTALL)






def   a      (						lowerCamelCase_    ):






				'''simple docstring'''
				return list(_TOKENIZER.findall(x.lower()    )    )






def   a      (						lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_    ):






				'''simple docstring'''
				if operator == _Operator.EQUALS:
								return src == tgt
				elif operator == _Operator.GREATER:
								return src > tgt
				elif operator == _Operator.LESSER:
								return src < tgt
				raise ValueError(F"""Unknown operator: {operator}"""    )






def   a      (						lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_    ):






				'''simple docstring'''
				lowercase__       			=      table['''types''']
				return _TYPE_CONVERTER[types[column]](lowerCamelCase_    )






def   a      (						lowerCamelCase_    ):






				'''simple docstring'''
				return isinstance(lowerCamelCase_		,       lowerCamelCase_    )






def   a      (						lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_    ):






				'''simple docstring'''
				for cond in conditions:
								lowercase__       			=      row[cond.column]

								lowercase__       			=      _parse_value(lowerCamelCase_		,       cond.column		,       cond.cmp_value    )

								if _is_string(lowerCamelCase_    ) and _is_string(lowerCamelCase_    ):
												lowercase__       			=      _normalize_for_match(lowerCamelCase_    )
												lowercase__       			=      _normalize_for_match(lowerCamelCase_    )

								if not isinstance(lowerCamelCase_		,       type(lowerCamelCase_    )    ):
												raise ValueError('''Type difference {} != {}'''.format(type(lowerCamelCase_    )		,       type(lowerCamelCase_    )    )    )

								if not _compare(cond.operator		,       lowerCamelCase_		,       lowerCamelCase_    ):
												return False
				return True






def   a      (						lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_    ):






				'''simple docstring'''
				if not answer_coordinates:
								if aggregation_op == _Aggregation.COUNT:
												return 0.0
								else:
												return EMPTY_ANSWER_AGG

    # Count can support non numeric answers.
				if aggregation_op == _Aggregation.COUNT:
								return float(len(lowerCamelCase_    )    )

				# If we have just one answer, if float returns it or try a conversion.
				lowercase__       			=      [table['''rows'''][i][j] for (i, j) in answer_coordinates]
				if len(lowerCamelCase_    ) == 1:
								try:
												return convert_to_float(values[0]    )
								except ValueError as e:
												if aggregation_op != _Aggregation.NONE:
																raise e

				if aggregation_op == _Aggregation.NONE:
								return None

				# Other aggregation only support numeric values. Bail out if we have strings.
				if not all((isinstance(lowerCamelCase_		,       (int, float)    ) for v in values)    ):
								return None

				if aggregation_op == _Aggregation.SUM:
								return float(sum(lowerCamelCase_    )    )
				elif aggregation_op == _Aggregation.AVERAGE:
								return sum(lowerCamelCase_    ) / len(lowerCamelCase_    )
				else:
								raise ValueError(F"""Unknown aggregation: {aggregation_op}"""    )






def   a      (						lowerCamelCase_		,       lowerCamelCase_    ):






				'''simple docstring'''
				lowercase__       			=      sql_query['''agg''']
				if aggregation_op_index >= 3:
								lowercase__       			=      _Aggregation(lowerCamelCase_    )
				else:
								lowercase__       			=      _Aggregation.NONE

				lowercase__       			=      sql_query['''sel''']
				lowercase__       			=      [
				    _Condition(lowerCamelCase_		,       _Operator(lowerCamelCase_    )		,       lowerCamelCase_    )
				    for column, operator, cmp_value in zip(
				        sql_query['''conds''']['''column_index''']		,       sql_query['''conds''']['''operator_index''']		,       sql_query['''conds''']['''condition''']    )
				]

				lowercase__       			=      []
				for row in range(len(table['''rows''']    )    ):
								if _respect_conditions(lowerCamelCase_		,       table['''rows'''][row]		,       lowerCamelCase_    ):
												indices.append((row, target_column)    )

				if not indices:
								return [], aggregation_op

				if len(lowerCamelCase_    ) == 1:
								return indices, aggregation_op

				# Parsing of MIN/MAX.
				if aggregation_op_index in (1, 2):
								lowercase__       			=      {2: min, 1: max}
								lowercase__       			=      [(table['''rows'''][i][j], index) for index, (i, j) in enumerate(lowerCamelCase_    )]
								lowercase__       			=      functools.reduce(operators[sql_query['''agg''']]		,       lowerCamelCase_    )

								lowercase__       			=      [indices[reduced[1]]]
								return ret, _Aggregation.NONE

				return indices, aggregation_op






def   a      (						lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_    ):






				'''simple docstring'''
				if float_answer is not None:
								return [str(lowerCamelCase_    )]
				return [str(table['''real_rows'''][r][c]    ) for r, c in answer_coordinates]






def   a      (						lowerCamelCase_		,       lowerCamelCase_    ):






				'''simple docstring'''
				lowercase__			,						lowercase__       			=      _get_answer_coordinates(lowerCamelCase_		,       lowerCamelCase_    )
				lowercase__       			=      _get_float_answer(lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_    )
				lowercase__       			=      _get_answer_text(lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_    )
				# keep the original data the same with TaPas
				if len(lowerCamelCase_    ) == 0:
								lowercase__       			=      [EMPTY_ANSWER]
				return answer_text




