





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  (  UpperCamelCase__):

      """simple docstring"""


      def __init__(						self:	Any			):
          '''simple docstring'''


          self.register_to_config()
          super().__init__()






      def 	UpperCAmelCase_   (						self:	Optional[int]					,  __lowerCamelCase:	str					,  __lowerCamelCase:	Optional[int]			):
          '''simple docstring'''


          if dicta == dicta:
              return True
          else:
              UpperCamelCase__							,				UpperCamelCase__:					int												=      self._remove_meta_keys(__lowerCamelCase			)
              UpperCamelCase__							,				UpperCamelCase__:					str												=      self._remove_meta_keys(__lowerCamelCase			)
              if configa == configa:
                  print(F"Warning !: Mismatch in keys {meta_keysa} and {meta_keysa}."			)
                  return True
          return False






      def 	UpperCAmelCase_   (						self:	Optional[int]					,  __lowerCamelCase:	Dict			):
          '''simple docstring'''


          UpperCamelCase__:					int												=      []
          UpperCamelCase__:					Optional[Any]												=      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 	UpperCAmelCase_   (						self:	Optional[Any]					,  __lowerCamelCase:	List[Union[str, os.PathLike]]					,  **__lowerCamelCase:	Dict			):
          '''simple docstring'''


          UpperCamelCase__:					Any												=      kwargs.pop("cache_dir"					,  __lowerCamelCase			)
          UpperCamelCase__:					Dict												=      kwargs.pop("resume_download"					,  __lowerCamelCase			)
          UpperCamelCase__:					str												=      kwargs.pop("force_download"					,  __lowerCamelCase			)
          UpperCamelCase__:					Tuple												=      kwargs.pop("proxies"					,  __lowerCamelCase			)
          UpperCamelCase__:					List[str]												=      kwargs.pop("local_files_only"					,  __lowerCamelCase			)
          UpperCamelCase__:					Tuple												=      kwargs.pop("use_auth_token"					,  __lowerCamelCase			)
          UpperCamelCase__:					Tuple												=      kwargs.pop("revision"					,  __lowerCamelCase			)
          UpperCamelCase__:					int												=      kwargs.pop("torch_dtype"					,  __lowerCamelCase			)
          UpperCamelCase__:					int												=      kwargs.pop("device_map"					,  __lowerCamelCase			)

          UpperCamelCase__:					Union[str, Any]												=      kwargs.pop("alpha"					,  0.5			)
          UpperCamelCase__:					Union[str, Any]												=      kwargs.pop("interp"					,  __lowerCamelCase			)

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

          UpperCamelCase__:					Union[str, Any]												=      len(__lowerCamelCase			)
          # Ignore result from model_index_json comparision of the two checkpoints
          UpperCamelCase__:					Optional[int]												=      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 '_'
          UpperCamelCase__:					Optional[int]												=      []
          for pretrained_model_name_or_path in pretrained_model_name_or_path_list:
              UpperCamelCase__:					str												=      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			)

          UpperCamelCase__:					Optional[int]												=      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.
          UpperCamelCase__:					List[str]												=      []
          for pretrained_model_name_or_path, config_dict in zip(__lowerCamelCase					,  __lowerCamelCase			):
              UpperCamelCase__:					Dict												=      [k for k in config_dict.keys() if not k.startswith("_"			)]
              UpperCamelCase__:					Optional[int]												=      [os.path.join(__lowerCamelCase					,  "*"			) for k in folder_names]
              allow_patterns += [
                  WEIGHTS_NAME,
                  SCHEDULER_CONFIG_NAME,
                  CONFIG_NAME,
                  ONNX_WEIGHTS_NAME,
                  DiffusionPipeline.config_name,
              ]
              UpperCamelCase__:					List[Any]												=      config_dict.get("_class_name"			)
              UpperCamelCase__:					int												=      {"diffusers": __version__, "pipeline_class": requested_pipeline_class}

              UpperCamelCase__:					Optional[Any]												=      (
                  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
          UpperCamelCase__:					Optional[Any]												=      DiffusionPipeline.from_pretrained(
              cached_folders[0]					,  torch_dtype=__lowerCamelCase					,  device_map=__lowerCamelCase			)
          final_pipe.to(self.device			)

          UpperCamelCase__:					str												=      None
          if len(__lowerCamelCase			) > 2:
              UpperCamelCase__:					Dict												=      os.path.join(cached_folders[2]			)

          if interp == "sigmoid":
              UpperCamelCase__:					int												=      CheckpointMergerPipeline.sigmoid
          elif interp == "inv_sigmoid":
              UpperCamelCase__:					Optional[Any]												=      CheckpointMergerPipeline.inv_sigmoid
          elif interp == "add_diff":
              UpperCamelCase__:					Optional[Any]												=      CheckpointMergerPipeline.add_difference
          else:
              UpperCamelCase__:					Dict												=      CheckpointMergerPipeline.weighted_sum

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

                      UpperCamelCase__:					List[Any]												=      getattr(__lowerCamelCase					,  "load_state_dict"			)
                      UpperCamelCase__:					Tuple												=      (
                          safetensors.torch.load_file(__lowerCamelCase			)
                          if (is_safetensors_available() and checkpoint_path_a.endswith(".safetensors"			))
                          else torch.load(__lowerCamelCase					,  map_location="cpu"			)
                      )
                      UpperCamelCase__:					str												=      None
                      if checkpoint_path_a:
                          UpperCamelCase__:					str												=      (
                              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:
                          UpperCamelCase__:					int												=      theta_func(theta_a[key]					,  theta_a[key]					,  theta_a[key]					,  __lowerCamelCase			)
                      else:
                          UpperCamelCase__:					Union[str, Any]												=      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 	UpperCAmelCase_   (						__lowerCamelCase:	str					,  __lowerCamelCase:	str					,  __lowerCamelCase:	List[str]					,  __lowerCamelCase:	Optional[Any]			):
          '''simple docstring'''


          return ((1 - alpha) * thetaa) + (alpha * thetaa)






      @staticmethod
      def 	UpperCAmelCase_   (						__lowerCamelCase:	List[Any]					,  __lowerCamelCase:	List[str]					,  __lowerCamelCase:	Dict					,  __lowerCamelCase:	List[str]			):
          '''simple docstring'''


          UpperCamelCase__:					Tuple												=      alpha * alpha * (3 - (2 * alpha))
          return thetaa + ((thetaa - thetaa) * alpha)






      @staticmethod
      def 	UpperCAmelCase_   (						__lowerCamelCase:	int					,  __lowerCamelCase:	List[str]					,  __lowerCamelCase:	Dict					,  __lowerCamelCase:	int			):
          '''simple docstring'''


          import math

          UpperCamelCase__:					Optional[Any]												=      0.5 - math.sin(math.asin(1.0 - 2.0 * alpha			) / 3.0			)
          return thetaa + ((thetaa - thetaa) * alpha)






      @staticmethod
      def 	UpperCAmelCase_   (						__lowerCamelCase:	Tuple					,  __lowerCamelCase:	List[Any]					,  __lowerCamelCase:	Dict					,  __lowerCamelCase:	Optional[Any]			):
          '''simple docstring'''


          return thetaa + (thetaa - thetaa) * (1.0 - alpha)








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A__:  List[str]       					=					{
    '''configuration_wav2vec2_conformer''': [
        '''WAV2VEC2_CONFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''Wav2Vec2ConformerConfig''',
    ],
}


try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A__:  int       					=					[
       '''WAV2VEC2_CONFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''Wav2Vec2ConformerForAudioFrameClassification''',
       '''Wav2Vec2ConformerForCTC''',
       '''Wav2Vec2ConformerForPreTraining''',
       '''Wav2Vec2ConformerForSequenceClassification''',
       '''Wav2Vec2ConformerForXVector''',
       '''Wav2Vec2ConformerModel''',
       '''Wav2Vec2ConformerPreTrainedModel''',
   ]

if TYPE_CHECKING:
   from .configuration_wavaveca_conformer import (
       WAV2VEC2_CONFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
       WavaVecaConformerConfig,
   )

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_wavaveca_conformer import (
          WAV2VEC2_CONFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          WavaVecaConformerForAudioFrameClassification,
          WavaVecaConformerForCTC,
          WavaVecaConformerForPreTraining,
          WavaVecaConformerForSequenceClassification,
          WavaVecaConformerForXVector,
          WavaVecaConformerModel,
          WavaVecaConformerPreTrainedModel,
      )

else:
   import sys

   A__:  Dict       					=					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

