





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 A_   (     _lowerCamelCase  ):




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




   def _lowerCAmelCase (self      :Optional[Any]       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :Optional[Any]					)->   List[Any]:
         if dicta == dicta:
               return True
         else:
               __A  ,		__A			  =							self._remove_meta_keys(_UpperCamelCase					)
               __A  ,		__A			  =							self._remove_meta_keys(_UpperCamelCase					)
               if configa == configa:
                     print(f"""Warning !: Mismatch in keys {meta_keysa} and {meta_keysa}."""					)
                     return True
         return False




   def _lowerCAmelCase (self      :Union[str, Any]       ,  _UpperCamelCase      :Dict					)->   str:
         __A			  =							[]
         __A			  =							config_dict.copy()
         for key in config_dict.keys():
               if key.startswith('''_'''					):
                     temp_dict.pop(_UpperCamelCase					)
                     meta_keys.append(_UpperCamelCase					)
         return (temp_dict, meta_keys)




   @torch.no_grad()
   def _lowerCAmelCase (self      :Any       ,  _UpperCamelCase      :List[Union[str, os.PathLike]]       ,  **_UpperCamelCase      :Any					)->   Union[str, Any]:
         __A			  =							kwargs.pop('''cache_dir'''       ,  _UpperCamelCase					)
         __A			  =							kwargs.pop('''resume_download'''       ,  _UpperCamelCase					)
         __A			  =							kwargs.pop('''force_download'''       ,  _UpperCamelCase					)
         __A			  =							kwargs.pop('''proxies'''       ,  _UpperCamelCase					)
         __A			  =							kwargs.pop('''local_files_only'''       ,  _UpperCamelCase					)
         __A			  =							kwargs.pop('''use_auth_token'''       ,  _UpperCamelCase					)
         __A			  =							kwargs.pop('''revision'''       ,  _UpperCamelCase					)
         __A			  =							kwargs.pop('''torch_dtype'''       ,  _UpperCamelCase					)
         __A			  =							kwargs.pop('''device_map'''       ,  _UpperCamelCase					)

         __A			  =							kwargs.pop('''alpha'''       ,  0.5					)
         __A			  =							kwargs.pop('''interp'''       ,  _UpperCamelCase					)

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

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

         # 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(
                   _UpperCamelCase       ,  cache_dir=_UpperCamelCase       ,  resume_download=_UpperCamelCase       ,  force_download=_UpperCamelCase       ,  proxies=_UpperCamelCase       ,  local_files_only=_UpperCamelCase       ,  use_auth_token=_UpperCamelCase       ,  revision=_UpperCamelCase       ,  )
               config_dicts.append(_UpperCamelCase					)

         __A			  =							True
         for idx in range(1       ,  len(_UpperCamelCase					)					):
               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(_UpperCamelCase       ,  _UpperCamelCase					):
               __A			  =							[k for k in config_dict.keys() if not k.startswith('''_'''					)]
               __A			  =							[os.path.join(_UpperCamelCase       ,  '''*'''					) 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(_UpperCamelCase					)
                   else snapshot_download(
                       _UpperCamelCase       ,  cache_dir=_UpperCamelCase       ,  resume_download=_UpperCamelCase       ,  proxies=_UpperCamelCase       ,  local_files_only=_UpperCamelCase       ,  use_auth_token=_UpperCamelCase       ,  revision=_UpperCamelCase       ,  allow_patterns=_UpperCamelCase       ,  user_agent=_UpperCamelCase       ,  )
               )
               print('''Cached Folder'''       ,  _UpperCamelCase					)
               cached_folders.append(_UpperCamelCase					)

         # 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=_UpperCamelCase       ,  device_map=_UpperCamelCase					)
         final_pipe.to(self.device					)

         __A			  =							None
         if len(_UpperCamelCase					) > 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]       ,  _UpperCamelCase					)
                     if os.path.exists(_UpperCamelCase					):
                           __A			  =							[
                               *glob.glob(os.path.join(_UpperCamelCase       ,  '''*.safetensors'''					)					),
                               *glob.glob(os.path.join(_UpperCamelCase       ,  '''*.bin'''					)					),
                           ]
                           __A			  =							files[0] if len(_UpperCamelCase					) > 0 else None
                     if len(_UpperCamelCase					) < 3:
                           __A			  =							None
                     else:
                           __A			  =							os.path.join(cached_folders[2]       ,  _UpperCamelCase					)
                           if os.path.exists(_UpperCamelCase					):
                                 __A			  =							[
                                     *glob.glob(os.path.join(_UpperCamelCase       ,  '''*.safetensors'''					)					),
                                     *glob.glob(os.path.join(_UpperCamelCase       ,  '''*.bin'''					)					),
                                 ]
                                 __A			  =							files[0] if len(_UpperCamelCase					) > 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(_UpperCamelCase       ,  _UpperCamelCase					)
                           if isinstance(_UpperCamelCase       ,  _UpperCamelCase					):  # ignore requires_safety_checker boolean
                                 continue
                           __A			  =							getattr(_UpperCamelCase       ,  '''state_dict'''					)
                           __A			  =							theta_a()

                           __A			  =							getattr(_UpperCamelCase       ,  '''load_state_dict'''					)
                           __A			  =							(
                               safetensors.torch.load_file(_UpperCamelCase					)
                               if (is_safetensors_available() and checkpoint_path_a.endswith('''.safetensors'''					))
                               else torch.load(_UpperCamelCase       ,  map_location='''cpu'''					)
                           )
                           __A			  =							None
                           if checkpoint_path_a:
                                 __A			  =							(
                                     safetensors.torch.load_file(_UpperCamelCase					)
                                     if (is_safetensors_available() and checkpoint_path_a.endswith('''.safetensors'''					))
                                     else torch.load(_UpperCamelCase       ,  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(_UpperCamelCase					)}"""					)
                           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]       ,  _UpperCamelCase					)
                           else:
                                 __A			  =							theta_func(theta_a[key]       ,  theta_a[key]       ,  _UpperCamelCase       ,  _UpperCamelCase					)

                     del theta_a
                     del theta_a
                     update_theta_a(_UpperCamelCase					)

                     del theta_a
         return final_pipe




   @staticmethod
   def _lowerCAmelCase (_UpperCamelCase      :Any       ,  _UpperCamelCase      :Union[str, Any]       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :List[str]					)->   Optional[Any]:
         return ((1 - alpha) * thetaa) + (alpha * thetaa)




   @staticmethod
   def _lowerCAmelCase (_UpperCamelCase      :Any       ,  _UpperCamelCase      :List[Any]       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :Any					)->   int:
         __A			  =							alpha * alpha * (3 - (2 * alpha))
         return thetaa + ((thetaa - thetaa) * alpha)




   @staticmethod
   def _lowerCAmelCase (_UpperCamelCase      :int       ,  _UpperCamelCase      :Optional[int]       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :Tuple					)->   Union[str, Any]:
         import math

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






   @staticmethod
   def _lowerCAmelCase (_UpperCamelCase      :Tuple       ,  _UpperCamelCase      :Optional[Any]       ,  _UpperCamelCase      :List[Any]       ,  _UpperCamelCase      :Union[str, Any]					)->   List[str]:
         return thetaa + (thetaa - thetaa) * (1.0 - alpha)












from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


snake_case__   :				Dict						=	{
    'configuration_videomae': ['VIDEOMAE_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VideoMAEConfig'],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       snake_case__   :				Any						=	[
           'VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST',
           'VideoMAEForPreTraining',
           'VideoMAEModel',
           'VideoMAEPreTrainedModel',
           'VideoMAEForVideoClassification',
       ]

try:
       if not is_vision_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       snake_case__   :				str						=	['VideoMAEFeatureExtractor']
       snake_case__   :				Tuple						=	['VideoMAEImageProcessor']

if TYPE_CHECKING:
       from .configuration_videomae import VIDEOMAE_PRETRAINED_CONFIG_ARCHIVE_MAP, VideoMAEConfig

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_videomae import (
                  VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST,
                  VideoMAEForPreTraining,
                  VideoMAEForVideoClassification,
                  VideoMAEModel,
                  VideoMAEPreTrainedModel,
              )

       try:
              if not is_vision_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .feature_extraction_videomae import VideoMAEFeatureExtractor
              from .image_processing_videomae import VideoMAEImageProcessor

else:
       import sys

       snake_case__   :				Optional[Any]						=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





