

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 		__magic_name__    (							lowerCamelCase__							):

   '''simple docstring'''




   def __init__(       self )							->					Tuple:






     """simple docstring"""






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



   def 				_UpperCAmelCase				(       self,					lowercase_,					lowercase_ )							->					int:






     """simple docstring"""






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



   def 				_UpperCAmelCase				(       self,					lowercase_ )							->					Tuple:






     """simple docstring"""






     a__						=[]
     a__						=config_dict.copy()
     for key in config_dict.keys():
       if key.startswith('''_''' ):
         temp_dict.pop(lowercase_ )
         meta_keys.append(lowercase_ )
     return (temp_dict, meta_keys)



   @torch.no_grad()
   def 				_UpperCAmelCase				(       self,					lowercase_,					**lowercase_ )							->					Optional[Any]:






     """simple docstring"""






     a__						=kwargs.pop('''cache_dir''',					lowercase_ )
     a__						=kwargs.pop('''resume_download''',					lowercase_ )
     a__						=kwargs.pop('''force_download''',					lowercase_ )
     a__						=kwargs.pop('''proxies''',					lowercase_ )
     a__						=kwargs.pop('''local_files_only''',					lowercase_ )
     a__						=kwargs.pop('''use_auth_token''',					lowercase_ )
     a__						=kwargs.pop('''revision''',					lowercase_ )
     a__						=kwargs.pop('''torch_dtype''',					lowercase_ )
     a__						=kwargs.pop('''device_map''',					lowercase_ )

     a__						=kwargs.pop('''alpha''',					0.5 )
     a__						=kwargs.pop('''interp''',					lowercase_ )

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

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

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

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

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

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

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

         del theta_a
         del theta_a
         update_theta_a(lowercase_ )

         del theta_a
     return final_pipe



   @staticmethod
   def 				_UpperCAmelCase				(       lowercase_,					lowercase_,					lowercase_,					lowercase_ )							->					Union[str, Any]:






     """simple docstring"""






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



   @staticmethod
   def 				_UpperCAmelCase				(       lowercase_,					lowercase_,					lowercase_,					lowercase_ )							->					Tuple:






     """simple docstring"""






     a__						=alpha * alpha * (3 - (2 * alpha))
     return thetaa + ((thetaa - thetaa) * alpha)



   @staticmethod
   def 				_UpperCAmelCase				(       lowercase_,					lowercase_,					lowercase_,					lowercase_ )							->					List[Any]:






     """simple docstring"""






     import math

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



   @staticmethod
   def 				_UpperCAmelCase				(       lowercase_,					lowercase_,					lowercase_,					lowercase_ )							->					Tuple:






     """simple docstring"""






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



import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import TvltImageProcessor




def     UpperCAmelCase__       (    _A   :				Optional[int]			,   _A   :				List[Any]=10			,   _A   :				Optional[int]=10			,   _A   :				Dict=False			,   _A   :				Any=False							):





  '''simple docstring'''







  a__						=[]
  for i in range(image_processor_tester.num_frames							):
    video.append(np.random.randint(2_55			,   size=(image_processor_tester.num_channels, width, height)			,   dtype=np.uinta							)							)

  if not numpify and not torchify:
    # PIL expects the channel dimension as last dimension
    a__						=[Image.fromarray(np.moveaxis(_A			,   0			,   -1							)							) for frame in video]

  if torchify:
    a__						=[torch.from_numpy(_A							) for frame in video]

  return video




def     UpperCAmelCase__       (    _A   :				List[str]			,   _A   :				Optional[int]=False			,   _A   :				Dict=False			,   _A   :				Optional[Any]=False							):





  '''simple docstring'''







  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

  a__						=[]
  for i in range(image_processor_tester.batch_size							):
    if equal_resolution:
      a__						=a__						=image_processor_tester.max_resolution
    else:
      a__,   a__						=np.random.choice(
          np.arange(image_processor_tester.min_resolution			,   image_processor_tester.max_resolution							)			,   2							)
      a__						=prepare_video(
          image_processor_tester=_A			,   width=_A			,   height=_A			,   numpify=_A			,   torchify=_A			,   )
    video_inputs.append(_A							)

  return video_inputs







class 		__magic_name__    (							unittest.TestCase							):

   '''simple docstring'''




   def __init__(       self,					lowercase_,					lowercase_=7,					lowercase_=3,					lowercase_=4,					lowercase_=18,					lowercase_=30,					lowercase_=400,					lowercase_=True,					lowercase_=None,					lowercase_=True,					lowercase_=[0.5, 0.5, 0.5],					lowercase_=[0.5, 0.5, 0.5],					lowercase_=True,					lowercase_=None,					)							->					Optional[int]:






     """simple docstring"""






     a__						=size if size is not None else {'''shortest_edge''': 18}
     a__						=crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

     a__						=parent
     a__						=batch_size
     a__						=num_channels
     a__						=num_frames
     a__						=image_size
     a__						=min_resolution
     a__						=max_resolution
     a__						=do_resize
     a__						=size
     a__						=do_normalize
     a__						=image_mean
     a__						=image_std
     a__						=do_center_crop
     a__						=crop_size



   def 				_UpperCAmelCase				(       self )							->					Any:






     """simple docstring"""






     return {
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_normalize": self.do_normalize,
         "do_resize": self.do_resize,
         "size": self.size,
         "do_center_crop": self.do_center_crop,
         "crop_size": self.crop_size,
     }







@require_torch
@require_vision
class 		__magic_name__    (							lowerCamelCase__	, unittest.TestCase							):

   '''simple docstring'''




   lowerCamelCase__				:			str												=     TvltImageProcessor if is_vision_available() else None



   def 				_UpperCAmelCase				(       self )							->					Optional[Any]:






     """simple docstring"""






     a__						=TvltImageProcessorTester(self )



   @property
   def 				_UpperCAmelCase				(       self )							->					Optional[Any]:






     """simple docstring"""






     return self.image_processor_tester.prepare_image_processor_dict()



   def 				_UpperCAmelCase				(       self )							->					str:






     """simple docstring"""






     a__						=self.image_processing_class(**self.image_processor_dict )
     self.assertTrue(hasattr(lowercase_,					'''image_mean''' ) )
     self.assertTrue(hasattr(lowercase_,					'''image_std''' ) )
     self.assertTrue(hasattr(lowercase_,					'''do_normalize''' ) )
     self.assertTrue(hasattr(lowercase_,					'''do_resize''' ) )
     self.assertTrue(hasattr(lowercase_,					'''do_center_crop''' ) )
     self.assertTrue(hasattr(lowercase_,					'''size''' ) )



   def 				_UpperCAmelCase				(       self )							->					Tuple:






     """simple docstring"""






     a__						=self.image_processing_class(**self.image_processor_dict )
     # create random PIL videos
     a__						=prepare_video_inputs(self.image_processor_tester,					equal_resolution=lowercase_ )
     for video in video_inputs:
       self.assertIsInstance(lowercase_,					lowercase_ )
       self.assertIsInstance(video[0],					Image.Image )

     # Test not batched input
     a__						=image_processor(video_inputs[0],					return_tensors='''pt''' ).pixel_values
     self.assertEqual(
         encoded_videos.shape,					(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),					)

     # Test batched
     a__						=image_processor(lowercase_,					return_tensors='''pt''' ).pixel_values
     self.assertEqual(
         encoded_videos.shape,					(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),					)



   def 				_UpperCAmelCase				(       self )							->					int:






     """simple docstring"""






     a__						=self.image_processing_class(**self.image_processor_dict )
     # create random numpy tensors
     a__						=prepare_video_inputs(self.image_processor_tester,					equal_resolution=lowercase_,					numpify=lowercase_ )
     for video in video_inputs:
       self.assertIsInstance(lowercase_,					lowercase_ )
       self.assertIsInstance(video[0],					np.ndarray )

     # Test not batched input
     a__						=image_processor(video_inputs[0],					return_tensors='''pt''' ).pixel_values
     self.assertEqual(
         encoded_videos.shape,					(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),					)

     # Test batched
     a__						=image_processor(lowercase_,					return_tensors='''pt''' ).pixel_values
     self.assertEqual(
         encoded_videos.shape,					(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),					)



   def 				_UpperCAmelCase				(       self )							->					int:






     """simple docstring"""






     a__						=self.image_processing_class(**self.image_processor_dict )
     # create random PyTorch tensors
     a__						=prepare_video_inputs(self.image_processor_tester,					equal_resolution=lowercase_,					torchify=lowercase_ )
     for video in video_inputs:
       self.assertIsInstance(lowercase_,					lowercase_ )
       self.assertIsInstance(video[0],					torch.Tensor )

     # Test not batched input
     a__						=image_processor(video_inputs[0],					return_tensors='''pt''' ).pixel_values
     self.assertEqual(
         encoded_videos.shape,					(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),					)

     # Test batched
     a__						=image_processor(lowercase_,					return_tensors='''pt''' ).pixel_values
     self.assertEqual(
         encoded_videos.shape,					(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),					)
