code: 
  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       :int    )     ->Optional[Any]:
  										self.register_to_config()
  										super().__init__()
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Tuple		,						lowerCAmelCase__       :Union[str, Any]		,						lowerCAmelCase__       :Any    )     ->Optional[Any]:
  										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 					SCREAMING_SNAKE_CASE( self       :List[Any]		,						lowerCAmelCase__       :Dict    )     ->Union[str, Any]:
  										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 					SCREAMING_SNAKE_CASE( self       :List[Any]		,						lowerCAmelCase__       :List[Union[str, os.PathLike]]		,						**lowerCAmelCase__       :Tuple    )     ->Any:
  										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 					SCREAMING_SNAKE_CASE( lowerCAmelCase__       :Optional[int]		,						lowerCAmelCase__       :int		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :Tuple    )     ->List[str]:
  										return ((1 - alpha) * thetaa) + (alpha * thetaa)
  
  
  
  
  
  						@staticmethod
  						def 					SCREAMING_SNAKE_CASE( lowerCAmelCase__       :Optional[Any]		,						lowerCAmelCase__       :Dict		,						lowerCAmelCase__       :int		,						lowerCAmelCase__       :Dict    )     ->Union[str, Any]:
  										lowercase						    =			alpha * alpha * (3 - (2 * alpha))
  										return thetaa + ((thetaa - thetaa) * alpha)
  
  
  
  
  
  						@staticmethod
  						def 					SCREAMING_SNAKE_CASE( lowerCAmelCase__       :List[Any]		,						lowerCAmelCase__       :Optional[Any]		,						lowerCAmelCase__       :str		,						lowerCAmelCase__       :List[str]    )     ->int:
  										import math
  
  										lowercase						    =			0.5 - math.sin(math.asin(1.0 - 2.0 * alpha    ) / 3.0    )
  										return thetaa + ((thetaa - thetaa) * alpha)
  
  
  
  
  
  						@staticmethod
  						def 					SCREAMING_SNAKE_CASE( lowerCAmelCase__       :Optional[Any]		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :Tuple		,						lowerCAmelCase__       :List[str]    )     ->Optional[Any]:
  										return thetaa + (thetaa - thetaa) * (1.0 - alpha)
  
style_context: 
  from functools import partial
  from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload
  
  import torch
  import torch.nn as nn
  import torch.types
  
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				if not inplace:
  								lowercase						    =			ma + ma
  				else:
  								ma += ma
  
  				return ma
  
  def    __snake_case       (  __magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				lowercase						    =			-1 * len(__magic_name__	)
  				lowercase						    =			list(range(len(tensor.shape[:zero_index]	)	)	)
  				return tensor.permute(first_inds + [zero_index + i for i in inds]	)
  
  def    __snake_case       (  __magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				return t.reshape(t.shape[:-no_dims] + (-1,)	)
  
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__		,						__magic_name__ = 1e-4	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				lowercase						    =			mask.expand(*value.shape	)
  				return torch.sum(mask * value		,						dim=__magic_name__	) / (eps + torch.sum(__magic_name__		,						dim=__magic_name__	))
  
  def    __snake_case       (  __magic_name__		,						__magic_name__ = 2.3_1_2_5		,						__magic_name__ = 2_1.6_8_7_5		,						__magic_name__ = 64	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				lowercase						    =			torch.linspace(__magic_name__		,						__magic_name__		,						no_bins - 1		,						device=pts.device	)
  				lowercase						    =			torch.sqrt(torch.sum((pts.unsqueeze(-2	) - pts.unsqueeze(-3	)) ** 2		,						dim=-1	)	)
  				return torch.bucketize(__magic_name__		,						__magic_name__	)
  
  def    __snake_case       (  __magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				lowercase						    =			dicts[0]
  				lowercase						    =			{}
  				for k, v in first.items():
  								lowercase						    =			[d[k] for d in dicts]
  								if isinstance(__magic_name__		,						__magic_name__	):
  												lowercase						    =			dict_multimap(__magic_name__		,						__magic_name__	)
  								else:
  												lowercase						    =			fn(__magic_name__	)
  
  				return new_dict
  
  def    __snake_case       (  __magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				lowercase						    =			v_bins.view(((1,) * len(x.shape	)) + (len(__magic_name__	),)	)
  				lowercase						    =			x[..., None] - reshaped_bins
  				lowercase						    =			torch.argmin(torch.abs(__magic_name__	)		,						dim=-1	)
  				return nn.functional.one_hot(__magic_name__		,						num_classes=len(__magic_name__	)	).float()
  
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__ = 0		,						__magic_name__ = 0	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				lowercase						    =			[]
  				for i, s in enumerate(data.shape[:no_batch_dims]	):
  								lowercase						    =			torch.arange(__magic_name__	)
  								lowercase						    =			r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape	) - i - 1)))	)
  								ranges.append(__magic_name__	)
  
  				lowercase						    =			[slice(__magic_name__	) for _ in range(len(data.shape	) - no_batch_dims	)]
  				lowercase						    =			inds
  				ranges.extend(__magic_name__	)
  				# Matt note: Editing this to get around the behaviour of using a list as an array index changing
  				# in recent Numpy versions
  				return data[tuple(__magic_name__	)]
  
  
  _snake_case			:       List[Any]      =						TypeVar("T")
  
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				lowercase						    =			{}
  				for k, v in dic.items():
  								if isinstance(__magic_name__		,						__magic_name__	):
  												lowercase						    =			dict_map(__magic_name__		,						__magic_name__		,						__magic_name__	)
  								else:
  												lowercase						    =			tree_map(__magic_name__		,						__magic_name__		,						__magic_name__	)
  
  				return new_dict
  
  @overload
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				...
  
  @overload
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				...
  
  @overload
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				...
  
  @overload
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				...
  
  def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				if isinstance(__magic_name__		,						__magic_name__	):
  								return dict_map(__magic_name__		,						__magic_name__		,						__magic_name__	)
  				elif isinstance(__magic_name__		,						__magic_name__	):
  								return [tree_map(__magic_name__		,						__magic_name__		,						__magic_name__	) for x in tree]
  				elif isinstance(__magic_name__		,						__magic_name__	):
  								return tuple(tree_map(__magic_name__		,						__magic_name__		,						__magic_name__	) for x in tree	)
  				elif isinstance(__magic_name__		,						__magic_name__	):
  								return fn(__magic_name__	)
  				else:
  								print(type(__magic_name__	)	)
  								raise ValueError("Not supported"	)
  
  
  
  
  _snake_case			:       Union[str, Any]      =						partial(tree_map, leaf_type=torch.Tensor)
  