




'''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      _a       (		__lowerCAmelCase							):






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






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)							->    List[str]:
          if dicta == dicta:
               return True
          else:
               _snake_case							,      _snake_case							=   self._remove_meta_keys(_SCREAMING_SNAKE_CASE					)
               _snake_case							,      _snake_case							=   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        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE					)							->    Tuple:
          _snake_case							=   []
          _snake_case							=   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        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)							->    Tuple:
          _snake_case							=   kwargs.pop("cache_dir"      ,_SCREAMING_SNAKE_CASE					)
          _snake_case							=   kwargs.pop("resume_download"      ,_SCREAMING_SNAKE_CASE					)
          _snake_case							=   kwargs.pop("force_download"      ,_SCREAMING_SNAKE_CASE					)
          _snake_case							=   kwargs.pop("proxies"      ,_SCREAMING_SNAKE_CASE					)
          _snake_case							=   kwargs.pop("local_files_only"      ,_SCREAMING_SNAKE_CASE					)
          _snake_case							=   kwargs.pop("use_auth_token"      ,_SCREAMING_SNAKE_CASE					)
          _snake_case							=   kwargs.pop("revision"      ,_SCREAMING_SNAKE_CASE					)
          _snake_case							=   kwargs.pop("torch_dtype"      ,_SCREAMING_SNAKE_CASE					)
          _snake_case							=   kwargs.pop("device_map"      ,_SCREAMING_SNAKE_CASE					)

          _snake_case							=   kwargs.pop("alpha"      ,0.5					)
          _snake_case							=   kwargs.pop("interp"      ,_SCREAMING_SNAKE_CASE					)

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

          _snake_case							=   len(_SCREAMING_SNAKE_CASE					)
          # Ignore result from model_index_json comparision of the two checkpoints
          _snake_case							=   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 '_'
          _snake_case							=   []
          for pretrained_model_name_or_path in pretrained_model_name_or_path_list:
               _snake_case							=   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					)

          _snake_case							=   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.
          _snake_case							=   []
          for pretrained_model_name_or_path, config_dict in zip(_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					):
               _snake_case							=   [k for k in config_dict.keys() if not k.startswith("_"					)]
               _snake_case							=   [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,
               ]
               _snake_case							=   config_dict.get("_class_name"					)
               _snake_case							=   {"diffusers": __version__, "pipeline_class": requested_pipeline_class}

               _snake_case							=   (
                   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
          _snake_case							=   DiffusionPipeline.from_pretrained(
              cached_folders[0]      ,torch_dtype=_SCREAMING_SNAKE_CASE      ,device_map=_SCREAMING_SNAKE_CASE					)
          final_pipe.to(self.device					)

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

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

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

                         _snake_case							=   getattr(_SCREAMING_SNAKE_CASE      ,"load_state_dict"					)
                         _snake_case							=   (
                             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"					)
                         )
                         _snake_case							=   None
                         if checkpoint_path_a:
                              _snake_case							=   (
                                  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:
                              _snake_case							=   theta_func(theta_a[key]      ,theta_a[key]      ,theta_a[key]      ,_SCREAMING_SNAKE_CASE					)
                         else:
                              _snake_case							=   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        _lowercase			(				_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)							->    Optional[int]:
          return ((1 - alpha) * thetaa) + (alpha * thetaa)






     @staticmethod
     def        _lowercase			(				_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)							->    Any:
          _snake_case							=   alpha * alpha * (3 - (2 * alpha))
          return thetaa + ((thetaa - thetaa) * alpha)






     @staticmethod
     def        _lowercase			(				_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)							->    List[Any]:
          import math

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






     @staticmethod
     def        _lowercase			(				_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)							->    Tuple:
          return thetaa + (thetaa - thetaa) * (1.0 - alpha)








'''simple docstring'''





import argparse
import os


UpperCamelCase_		:    Union[str, Any]									=		{
    '''pytorch''': {
        '''1.13''': {
            '''torch''': '''1.13.1''',
            '''torchvision''': '''0.14.1''',
            '''torchaudio''': '''0.13.1''',
            '''python''': 3.9,
            '''cuda''': '''cu116''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu116'''
            ),
            '''base_image''': '''nvidia/cuda:11.6.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.12''': {
            '''torch''': '''1.12.1''',
            '''torchvision''': '''0.13.1''',
            '''torchaudio''': '''0.12.1''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.11''': {
            '''torch''': '''1.11.0''',
            '''torchvision''': '''0.12.0''',
            '''torchaudio''': '''0.11.0''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.10''': {
            '''torch''': '''1.10.2''',
            '''torchvision''': '''0.11.3''',
            '''torchaudio''': '''0.10.2''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.10.2 torchvision==0.11.3 torchaudio==0.10.2'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        # torchaudio < 0.10 has no CUDA-enabled binary distributions
        '''1.9''': {
            '''torch''': '''1.9.1''',
            '''torchvision''': '''0.10.1''',
            '''torchaudio''': '''0.9.1''',
            '''python''': 3.9,
            '''cuda''': '''cu111''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.9.1 torchvision==0.10.1 torchaudio==0.9.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu111'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
    },
    '''tensorflow''': {
        '''2.11''': {
            '''tensorflow''': '''2.11.1''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.11.1''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.10''': {
            '''tensorflow''': '''2.10.1''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.10.1''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.9''': {
            '''tensorflow''': '''2.9.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.9.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.8''': {
            '''tensorflow''': '''2.8.2''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.8.2''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.7''': {
            '''tensorflow''': '''2.7.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.7.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.6''': {
            '''tensorflow''': '''2.6.5''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.6.5''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.5''': {
            '''tensorflow''': '''2.5.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.5.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
    },
}


if __name__ == "__main__":
   UpperCamelCase_		:    Optional[Any]									=		argparse.ArgumentParser('''Choose the framework and version to install''')
   parser.add_argument(
       '''--framework''', help='''The framework to install. Should be `torch` or `tensorflow`''', type=str, required=True
   )
   parser.add_argument('''--version''', help='''The version of the framework to install.''', type=str, required=True)
   UpperCamelCase_		:    Tuple									=		parser.parse_args()

   UpperCamelCase_		:    Optional[int]									=		past_versions_testing[args.framework][args.version]

   os.system(F'echo "export INSTALL_CMD=\'{info["install"]}\'" >> ~/.profile')
   print(F'echo "export INSTALL_CMD=\'{info["install"]}\'" >> ~/.profile')

   UpperCamelCase_		:    List[Any]									=		''''''
   if args.framework == "pytorch":
      UpperCamelCase_		:    str									=		info['''cuda''']
   os.system(F'echo \"export CUDA=\'{cuda}\'\" >> ~/.profile')
   print(F'echo \"export CUDA=\'{cuda}\'\" >> ~/.profile')


