




'''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 	lowercase_					(				__UpperCAmelCase     ):


   """simple docstring"""

   def __init__(					self      :			Optional[Any]					):

        """simple docstring"""
        self.register_to_config()
        super().__init__()



   def 							lowerCAmelCase_    (					self      :			int						,   __lowerCamelCase      :			Tuple						,   __lowerCamelCase      :			str					):

        """simple docstring"""



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



   def 							lowerCAmelCase_    (					self      :			Tuple						,   __lowerCamelCase      :			Optional[Any]					):

        """simple docstring"""
        _SCREAMING_SNAKE_CASE =   []
        _SCREAMING_SNAKE_CASE =   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 							lowerCAmelCase_    (					self      :			Any						,   __lowerCamelCase      :			Optional[Any]						,   **__lowerCamelCase      :			int					):

        """simple docstring"""
        _SCREAMING_SNAKE_CASE =   kwargs.pop("cache_dir"						,   lowerCamelCase__					)
        _SCREAMING_SNAKE_CASE =   kwargs.pop("resume_download"						,   lowerCamelCase__					)
        _SCREAMING_SNAKE_CASE =   kwargs.pop("force_download"						,   lowerCamelCase__					)
        _SCREAMING_SNAKE_CASE =   kwargs.pop("proxies"						,   lowerCamelCase__					)
        _SCREAMING_SNAKE_CASE =   kwargs.pop("local_files_only"						,   lowerCamelCase__					)
        _SCREAMING_SNAKE_CASE =   kwargs.pop("use_auth_token"						,   lowerCamelCase__					)
        _SCREAMING_SNAKE_CASE =   kwargs.pop("revision"						,   lowerCamelCase__					)
        _SCREAMING_SNAKE_CASE =   kwargs.pop("torch_dtype"						,   lowerCamelCase__					)
        _SCREAMING_SNAKE_CASE =   kwargs.pop("device_map"						,   lowerCamelCase__					)

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

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

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

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

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

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

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

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

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

        """simple docstring"""
        return ((1 - alpha) * thetaa) + (alpha * thetaa)



   @staticmethod
   def 							lowerCAmelCase_    (					__lowerCamelCase      :			List[Any]						,   __lowerCamelCase      :			str						,   __lowerCamelCase      :			Any						,   __lowerCamelCase      :			Optional[Any]					):

        """simple docstring"""
        _SCREAMING_SNAKE_CASE =   alpha * alpha * (3 - (2 * alpha))
        return thetaa + ((thetaa - thetaa) * alpha)



   @staticmethod
   def 							lowerCAmelCase_    (					__lowerCamelCase      :			Dict						,   __lowerCamelCase      :			int						,   __lowerCamelCase      :			List[Any]						,   __lowerCamelCase      :			Dict					):

        """simple docstring"""
        import math

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



   @staticmethod
   def 							lowerCAmelCase_    (					__lowerCamelCase      :			Dict						,   __lowerCamelCase      :			Union[str, Any]						,   __lowerCamelCase      :			Any						,   __lowerCamelCase      :			Dict					):

        """simple docstring"""
        return thetaa + (thetaa - thetaa) * (1.0 - alpha)







'''simple docstring'''



import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:







 class 	lowercase_					:


    """simple docstring"""

    @staticmethod
    def 							lowerCAmelCase_    (					*__lowerCamelCase      :			Union[str, Any]						,   **__lowerCamelCase      :			str					):

       """simple docstring"""
       pass







@is_pipeline_test
@require_torch_or_tf
@require_vision
class 	lowercase_					(				unittest.TestCase     ):


   """simple docstring"""

   lowerCamelCase_       						=		MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
   lowerCamelCase_       						=		TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING



   def 							lowerCAmelCase_    (					self      :			Any						,   __lowerCamelCase      :			Tuple						,   __lowerCamelCase      :			Dict						,   __lowerCamelCase      :			Optional[Any]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   ImageClassificationPipeline(model=__lowerCamelCase						,   image_processor=__lowerCamelCase						,   top_k=2					)
      _SCREAMING_SNAKE_CASE =   [
          Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					),
          "http://images.cocodataset.org/val2017/000000039769.jpg",
      ]
      return image_classifier, examples



   def 							lowerCAmelCase_    (					self      :			Any						,   __lowerCamelCase      :			Optional[Any]						,   __lowerCamelCase      :			Tuple					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   image_classifier("./tests/fixtures/tests_samples/COCO/000000039769.png"					)

      self.assertEqual(
          __lowerCamelCase						,   [
              {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
          ]						,   )

      import datasets

      _SCREAMING_SNAKE_CASE =   datasets.load_dataset("hf-internal-testing/fixtures_image_utils"						,   "image"						,   split="test"					)

      # Accepts URL + PIL.Image + lists
      _SCREAMING_SNAKE_CASE =   image_classifier(
          [
              Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					),
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              # RGBA
              dataset[0]["file"],
              # LA
              dataset[1]["file"],
              # L
              dataset[2]["file"],
          ]					)
      self.assertEqual(
          __lowerCamelCase						,   [
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
          ]						,   )



   @require_torch
   def 							lowerCAmelCase_    (					self      :			Dict					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   "hf-internal-testing/tiny-random-vit"
      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model=__lowerCamelCase					)

      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]						,   )

      _SCREAMING_SNAKE_CASE =   image_classifier(
          [
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              "http://images.cocodataset.org/val2017/000000039769.jpg",
          ]						,   top_k=2						,   )
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
          ]						,   )



   @require_tf
   def 							lowerCAmelCase_    (					self      :			Optional[Any]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   "hf-internal-testing/tiny-random-vit"
      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model=__lowerCamelCase						,   framework="tf"					)

      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]						,   )

      _SCREAMING_SNAKE_CASE =   image_classifier(
          [
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              "http://images.cocodataset.org/val2017/000000039769.jpg",
          ]						,   top_k=2						,   )
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
          ]						,   )



   def 							lowerCAmelCase_    (					self      :			Any					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   PreTrainedTokenizer()

      # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
      _SCREAMING_SNAKE_CASE =   pipeline(
          "image-classification"						,   model="hf-internal-testing/tiny-random-vit"						,   tokenizer=__lowerCamelCase					)

      self.assertIs(image_classifier.tokenizer						,   __lowerCamelCase					)



   @slow
   @require_torch
   def 							lowerCAmelCase_    (					self      :			List[str]					):

      """simple docstring"""
      # Perceiver is not tested by `run_pipeline_test` properly.
      # That is because the type of feature_extractor and model preprocessor need to be kept
      # in sync, which is not the case in the current design
      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model="deepmind/vision-perceiver-conv"					)
      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              {"score": 0.4_3_8_5, "label": "tabby, tabby cat"},
              {"score": 0.3_2_1, "label": "tiger cat"},
              {"score": 0.0_5_0_2, "label": "Egyptian cat"},
              {"score": 0.0_1_3_7, "label": "crib, cot"},
              {"score": 0.0_0_7, "label": "radiator"},
          ]						,   )

      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model="deepmind/vision-perceiver-fourier"					)
      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              {"score": 0.5_6_5_8, "label": "tabby, tabby cat"},
              {"score": 0.1_3_0_9, "label": "tiger cat"},
              {"score": 0.0_7_2_2, "label": "Egyptian cat"},
              {"score": 0.0_7_0_7, "label": "remote control, remote"},
              {"score": 0.0_0_8_2, "label": "computer keyboard, keypad"},
          ]						,   )

      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model="deepmind/vision-perceiver-learned"					)
      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              {"score": 0.3_0_2_2, "label": "tabby, tabby cat"},
              {"score": 0.2_3_6_2, "label": "Egyptian cat"},
              {"score": 0.1_8_5_6, "label": "tiger cat"},
              {"score": 0.0_3_2_4, "label": "remote control, remote"},
              {"score": 0.0_0_9_6, "label": "quilt, comforter, comfort, puff"},
          ]						,   )

