"""simple docstring"""


import unittest

from transformers import PegasusTokenizer, PegasusTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


UpperCAmelCase__  		=						get_tests_dir("""fixtures/test_sentencepiece_no_bos.model""")


@require_sentencepiece
@require_tokenizers
class        a				(		lowerCAmelCase_	,							unittest.TestCase   ):
    _snake_case     :  List[Any]     							=							PegasusTokenizer
    _snake_case     :  Union[str, Any]     							=							PegasusTokenizerFast
    _snake_case     :  Optional[Any]     							=							True
    _snake_case     :  Any     							=							True


    def      lowerCAmelCase_  (					self	:  str					):
      super().setUp()

      # We have a SentencePiece fixture for testing
      _UpperCAmelCase						 =      PegasusTokenizer(__lowerCAmelCase					)
      tokenizer.save_pretrained(self.tmpdirname					)


    @cached_property
    def      lowerCAmelCase_  (					self	:  str					):
      return PegasusTokenizer.from_pretrained("""google/pegasus-large"""					)


    def      lowerCAmelCase_  (					self	:  Union[str, Any]   ,				**__lowerCAmelCase	:  Tuple					):
      return PegasusTokenizer.from_pretrained(self.tmpdirname   ,				**__lowerCAmelCase					)


    def      lowerCAmelCase_  (					self	:  List[str]   ,				__lowerCAmelCase	:  Tuple					):
      return ("This is a test", "This is a test")


    def      lowerCAmelCase_  (					self	:  Tuple					):
      _UpperCAmelCase						 =      """</s>"""
      _UpperCAmelCase						 =      1

      self.assertEqual(self.get_tokenizer()._convert_token_to_id(__lowerCAmelCase					)   ,				__lowerCAmelCase					)
      self.assertEqual(self.get_tokenizer()._convert_id_to_token(__lowerCAmelCase					)   ,				__lowerCAmelCase					)


    def      lowerCAmelCase_  (					self	:  Tuple					):
      _UpperCAmelCase						 =      list(self.get_tokenizer().get_vocab().keys()					)

      self.assertEqual(vocab_keys[0]   ,				"""<pad>"""					)
      self.assertEqual(vocab_keys[1]   ,				"""</s>"""					)
      self.assertEqual(vocab_keys[-1]   ,				"""v"""					)
      self.assertEqual(len(__lowerCAmelCase					)   ,				1103					)


    def      lowerCAmelCase_  (					self	:  Union[str, Any]					):
      self.assertEqual(self.get_tokenizer().vocab_size   ,				1103					)


    def      lowerCAmelCase_  (					self	:  List[Any]					):
      _UpperCAmelCase						 =      self.rust_tokenizer_class.from_pretrained(self.tmpdirname					)
      _UpperCAmelCase						 =      self.tokenizer_class.from_pretrained(self.tmpdirname					)
      _UpperCAmelCase						 =      (
          """Let's see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important"""
          """ </s> <pad> <pad> <pad>"""
      )
      _UpperCAmelCase						 =      rust_tokenizer([raw_input_str]   ,				return_tensors=__lowerCAmelCase   ,				add_special_tokens=__lowerCAmelCase					).input_ids[0]
      _UpperCAmelCase						 =      py_tokenizer([raw_input_str]   ,				return_tensors=__lowerCAmelCase   ,				add_special_tokens=__lowerCAmelCase					).input_ids[0]
      self.assertListEqual(__lowerCAmelCase   ,				__lowerCAmelCase					)


    def      lowerCAmelCase_  (					self	:  int					):
      _UpperCAmelCase						 =      self._large_tokenizer
      # <mask_1> masks whole sentence while <mask_2> masks single word
      _UpperCAmelCase						 =      """<mask_1> To ensure a <mask_2> flow of bank resolutions."""
      _UpperCAmelCase						 =      [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1]
      _UpperCAmelCase						 =      tokenizer([raw_input_str]   ,				return_tensors=__lowerCAmelCase					).input_ids[0]
      self.assertListEqual(__lowerCAmelCase   ,				__lowerCAmelCase					)


    def      lowerCAmelCase_  (					self	:  Any					):
      _UpperCAmelCase						 =      self._large_tokenizer
      # The tracebacks for the following asserts are **better** without messages or self.assertEqual
      assert tokenizer.vocab_size == 9_6103
      assert tokenizer.pad_token_id == 0
      assert tokenizer.eos_token_id == 1
      assert tokenizer.offset == 103
      assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105
      assert tokenizer.unk_token == "<unk>"
      assert tokenizer.model_max_length == 1024
      _UpperCAmelCase						 =      """To ensure a smooth flow of bank resolutions."""
      _UpperCAmelCase						 =      [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1]
      _UpperCAmelCase						 =      tokenizer([raw_input_str]   ,				return_tensors=__lowerCAmelCase					).input_ids[0]
      self.assertListEqual(__lowerCAmelCase   ,				__lowerCAmelCase					)
      assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3]					) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"]


    @require_torch
    def      lowerCAmelCase_  (					self	:  str					):
      _UpperCAmelCase						 =      ["""This is going to be way too long.""" * 150, """short example"""]
      _UpperCAmelCase						 =      ["""not super long but more than 5 tokens""", """tiny"""]
      _UpperCAmelCase						 =      self._large_tokenizer(__lowerCAmelCase   ,				padding=__lowerCAmelCase   ,				truncation=__lowerCAmelCase   ,				return_tensors="""pt"""					)
      _UpperCAmelCase						 =      self._large_tokenizer(
          text_target=__lowerCAmelCase   ,				max_length=5   ,				padding=__lowerCAmelCase   ,				truncation=__lowerCAmelCase   ,				return_tensors="""pt"""					)

      assert batch.input_ids.shape == (2, 1024)
      assert batch.attention_mask.shape == (2, 1024)
      assert targets["input_ids"].shape == (2, 5)
      assert len(__lowerCAmelCase					) == 2  # input_ids, attention_mask.


    @slow
    def      lowerCAmelCase_  (					self	:  Union[str, Any]					):
      # fmt: off
      _UpperCAmelCase						 =      {"""input_ids""": [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 5542, 124, 302, 518, 111, 268, 2956, 115, 149, 4427, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [139, 1235, 2799, 1_8289, 1_7780, 204, 109, 9474, 1296, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
      # fmt: on

      self.tokenizer_integration_test_util(
          expected_encoding=__lowerCAmelCase   ,				model_name="""google/bigbird-pegasus-large-arxiv"""   ,				revision="""ba85d0851d708441f91440d509690f1ab6353415"""   ,				)



@require_sentencepiece
@require_tokenizers
class        a				(		lowerCAmelCase_	,							unittest.TestCase   ):
    _snake_case     :  List[Any]     							=							PegasusTokenizer
    _snake_case     :  str     							=							PegasusTokenizerFast
    _snake_case     :  List[Any]     							=							True
    _snake_case     :  str     							=							True


    def      lowerCAmelCase_  (					self	:  Dict					):
      super().setUp()

      # We have a SentencePiece fixture for testing
      _UpperCAmelCase						 =      PegasusTokenizer(__lowerCAmelCase   ,				offset=0   ,				mask_token_sent=__lowerCAmelCase   ,				mask_token="""[MASK]"""					)
      tokenizer.save_pretrained(self.tmpdirname					)


    @cached_property
    def      lowerCAmelCase_  (					self	:  Any					):
      return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv"""					)


    def      lowerCAmelCase_  (					self	:  Union[str, Any]   ,				**__lowerCAmelCase	:  Any					):
      return PegasusTokenizer.from_pretrained(self.tmpdirname   ,				**__lowerCAmelCase					)


    def      lowerCAmelCase_  (					self	:  int   ,				__lowerCAmelCase	:  int					):
      return ("This is a test", "This is a test")


    def      lowerCAmelCase_  (					self	:  Optional[int]					):
      _UpperCAmelCase						 =      self.rust_tokenizer_class.from_pretrained(self.tmpdirname					)
      _UpperCAmelCase						 =      self.tokenizer_class.from_pretrained(self.tmpdirname					)
      _UpperCAmelCase						 =      (
          """Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>"""
          """ <pad> <pad> <pad>"""
      )
      _UpperCAmelCase						 =      rust_tokenizer([raw_input_str]   ,				return_tensors=__lowerCAmelCase   ,				add_special_tokens=__lowerCAmelCase					).input_ids[0]
      _UpperCAmelCase						 =      py_tokenizer([raw_input_str]   ,				return_tensors=__lowerCAmelCase   ,				add_special_tokens=__lowerCAmelCase					).input_ids[0]
      self.assertListEqual(__lowerCAmelCase   ,				__lowerCAmelCase					)


    @require_torch
    def      lowerCAmelCase_  (					self	:  Union[str, Any]					):
      _UpperCAmelCase						 =      ["""This is going to be way too long.""" * 1000, """short example"""]
      _UpperCAmelCase						 =      ["""not super long but more than 5 tokens""", """tiny"""]
      _UpperCAmelCase						 =      self._large_tokenizer(__lowerCAmelCase   ,				padding=__lowerCAmelCase   ,				truncation=__lowerCAmelCase   ,				return_tensors="""pt"""					)
      _UpperCAmelCase						 =      self._large_tokenizer(
          text_target=__lowerCAmelCase   ,				max_length=5   ,				padding=__lowerCAmelCase   ,				truncation=__lowerCAmelCase   ,				return_tensors="""pt"""					)

      assert batch.input_ids.shape == (2, 4096)
      assert batch.attention_mask.shape == (2, 4096)
      assert targets["input_ids"].shape == (2, 5)
      assert len(__lowerCAmelCase					) == 2  # input_ids, attention_mask.


    def      lowerCAmelCase_  (					self	:  Dict					):
      _UpperCAmelCase						 =      (
          """This is an example string that is used to test the original TF implementation against the HF"""
          """ implementation"""
      )

      _UpperCAmelCase						 =      self._large_tokenizer(__lowerCAmelCase					).input_ids

      self.assertListEqual(
          __lowerCAmelCase   ,				[182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1]   ,				)

"""simple docstring"""


# This model implementation is heavily inspired by https://github.com/haofanwang/ControlNet-for-Diffusers/

import gc
import random
import tempfile
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    ControlNetModel,
    DDIMScheduler,
    StableDiffusionControlNetImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
)


enable_full_determinism()


class        a				(		lowerCAmelCase_	,							lowerCAmelCase_	,							lowerCAmelCase_	,							unittest.TestCase   ):
    _snake_case     :  List[str]     							=							StableDiffusionControlNetImgaImgPipeline
    _snake_case     :  Optional[int]     							=							TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'}
    _snake_case     :  Optional[int]     							=							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
    _snake_case     :  List[str]     							=							IMAGE_TO_IMAGE_IMAGE_PARAMS.union({'control_image'}   )
    _snake_case     :  Union[str, Any]     							=							IMAGE_TO_IMAGE_IMAGE_PARAMS


    def      lowerCAmelCase_  (					self	:  Optional[int]					):
      torch.manual_seed(0					)
      _UpperCAmelCase						 =      UNetaDConditionModel(
          block_out_channels=(32, 64)   ,				layers_per_block=2   ,				sample_size=32   ,				in_channels=4   ,				out_channels=4   ,				down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")   ,				up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")   ,				cross_attention_dim=32   ,				)
      torch.manual_seed(0					)
      _UpperCAmelCase						 =      ControlNetModel(
          block_out_channels=(32, 64)   ,				layers_per_block=2   ,				in_channels=4   ,				down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")   ,				cross_attention_dim=32   ,				conditioning_embedding_out_channels=(16, 32)   ,				)
      torch.manual_seed(0					)
      _UpperCAmelCase						 =      DDIMScheduler(
          beta_start=0.00_085   ,				beta_end=0.012   ,				beta_schedule="""scaled_linear"""   ,				clip_sample=__lowerCAmelCase   ,				set_alpha_to_one=__lowerCAmelCase   ,				)
      torch.manual_seed(0					)
      _UpperCAmelCase						 =      AutoencoderKL(
          block_out_channels=[32, 64]   ,				in_channels=3   ,				out_channels=3   ,				down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]   ,				up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]   ,				latent_channels=4   ,				)
      torch.manual_seed(0					)
      _UpperCAmelCase						 =      CLIPTextConfig(
          bos_token_id=0   ,				eos_token_id=2   ,				hidden_size=32   ,				intermediate_size=37   ,				layer_norm_eps=1e-0_5   ,				num_attention_heads=4   ,				num_hidden_layers=5   ,				pad_token_id=1   ,				vocab_size=1000   ,				)
      _UpperCAmelCase						 =      CLIPTextModel(__lowerCAmelCase					)
      _UpperCAmelCase						 =      CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""					)

      _UpperCAmelCase						 =      {
          """unet""": unet,
          """controlnet""": controlnet,
          """scheduler""": scheduler,
          """vae""": vae,
          """text_encoder""": text_encoder,
          """tokenizer""": tokenizer,
          """safety_checker""": None,
          """feature_extractor""": None,
      }
      return components


    def      lowerCAmelCase_  (					self	:  int   ,				__lowerCAmelCase	:  Tuple   ,				__lowerCAmelCase	:  List[str]=0					):
      if str(__lowerCAmelCase					).startswith("""mps"""					):
        _UpperCAmelCase						 =      torch.manual_seed(__lowerCAmelCase					)
      else:
        _UpperCAmelCase						 =      torch.Generator(device=__lowerCAmelCase					).manual_seed(__lowerCAmelCase					)

      _UpperCAmelCase						 =      2
      _UpperCAmelCase						 =      randn_tensor(
          (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)   ,				generator=__lowerCAmelCase   ,				device=torch.device(__lowerCAmelCase					)   ,				)
      _UpperCAmelCase						 =      floats_tensor(control_image.shape   ,				rng=random.Random(__lowerCAmelCase					)					).to(__lowerCAmelCase					)
      _UpperCAmelCase						 =      image.cpu().permute(0   ,				2   ,				3   ,				1					)[0]
      _UpperCAmelCase						 =      Image.fromarray(np.uinta(__lowerCAmelCase					)					).convert("""RGB"""					).resize((64, 64)					)
      _UpperCAmelCase						 =      {
          """prompt""": """A painting of a squirrel eating a burger""",
          """generator""": generator,
          """num_inference_steps""": 2,
          """guidance_scale""": 6.0,
          """output_type""": """numpy""",
          """image""": image,
          """control_image""": control_image,
      }

      return inputs


    def      lowerCAmelCase_  (					self	:  Dict					):
      return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3					)


    @unittest.skipIf(
        torch_device != """cuda""" or not is_xformers_available()   ,				reason="""XFormers attention is only available with CUDA and `xformers` installed"""   ,				)
    def      lowerCAmelCase_  (					self	:  Tuple					):
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2e-3					)


    def      lowerCAmelCase_  (					self	:  List[Any]					):
      self._test_inference_batch_single_identical(expected_max_diff=2e-3					)



class        a				(		lowerCAmelCase_	,							lowerCAmelCase_	,							unittest.TestCase   ):
    _snake_case     :  List[Any]     							=							StableDiffusionControlNetImgaImgPipeline
    _snake_case     :  int     							=							TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'}
    _snake_case     :  Any     							=							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
    _snake_case     :  Optional[int]     							=							frozenset([]   )  # TO_DO: add image_params once refactored VaeImageProcessor.preprocess


    def      lowerCAmelCase_  (					self	:  Any					):
      torch.manual_seed(0					)
      _UpperCAmelCase						 =      UNetaDConditionModel(
          block_out_channels=(32, 64)   ,				layers_per_block=2   ,				sample_size=32   ,				in_channels=4   ,				out_channels=4   ,				down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")   ,				up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")   ,				cross_attention_dim=32   ,				)
      torch.manual_seed(0					)

      def init_weights(__lowerCAmelCase	:  str					):
        if isinstance(__lowerCAmelCase   ,				torch.nn.Convad					):
          torch.nn.init.normal(m.weight					)
          m.bias.data.fill_(1.0					)

      _UpperCAmelCase						 =      ControlNetModel(
          block_out_channels=(32, 64)   ,				layers_per_block=2   ,				in_channels=4   ,				down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")   ,				cross_attention_dim=32   ,				conditioning_embedding_out_channels=(16, 32)   ,				)
      controlneta.controlnet_down_blocks.apply(__lowerCAmelCase					)

      torch.manual_seed(0					)
      _UpperCAmelCase						 =      ControlNetModel(
          block_out_channels=(32, 64)   ,				layers_per_block=2   ,				in_channels=4   ,				down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")   ,				cross_attention_dim=32   ,				conditioning_embedding_out_channels=(16, 32)   ,				)
      controlneta.controlnet_down_blocks.apply(__lowerCAmelCase					)

      torch.manual_seed(0					)
      _UpperCAmelCase						 =      DDIMScheduler(
          beta_start=0.00_085   ,				beta_end=0.012   ,				beta_schedule="""scaled_linear"""   ,				clip_sample=__lowerCAmelCase   ,				set_alpha_to_one=__lowerCAmelCase   ,				)
      torch.manual_seed(0					)
      _UpperCAmelCase						 =      AutoencoderKL(
          block_out_channels=[32, 64]   ,				in_channels=3   ,				out_channels=3   ,				down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]   ,				up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]   ,				latent_channels=4   ,				)
      torch.manual_seed(0					)
      _UpperCAmelCase						 =      CLIPTextConfig(
          bos_token_id=0   ,				eos_token_id=2   ,				hidden_size=32   ,				intermediate_size=37   ,				layer_norm_eps=1e-0_5   ,				num_attention_heads=4   ,				num_hidden_layers=5   ,				pad_token_id=1   ,				vocab_size=1000   ,				)
      _UpperCAmelCase						 =      CLIPTextModel(__lowerCAmelCase					)
      _UpperCAmelCase						 =      CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""					)

      _UpperCAmelCase						 =      MultiControlNetModel([controlneta, controlneta]					)

      _UpperCAmelCase						 =      {
          """unet""": unet,
          """controlnet""": controlnet,
          """scheduler""": scheduler,
          """vae""": vae,
          """text_encoder""": text_encoder,
          """tokenizer""": tokenizer,
          """safety_checker""": None,
          """feature_extractor""": None,
      }
      return components


    def      lowerCAmelCase_  (					self	:  Union[str, Any]   ,				__lowerCAmelCase	:  List[str]   ,				__lowerCAmelCase	:  List[str]=0					):
      if str(__lowerCAmelCase					).startswith("""mps"""					):
        _UpperCAmelCase						 =      torch.manual_seed(__lowerCAmelCase					)
      else:
        _UpperCAmelCase						 =      torch.Generator(device=__lowerCAmelCase					).manual_seed(__lowerCAmelCase					)

      _UpperCAmelCase						 =      2

      _UpperCAmelCase						 =      [
          randn_tensor(
              (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)   ,				generator=__lowerCAmelCase   ,				device=torch.device(__lowerCAmelCase					)   ,				),
          randn_tensor(
              (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)   ,				generator=__lowerCAmelCase   ,				device=torch.device(__lowerCAmelCase					)   ,				),
      ]

      _UpperCAmelCase						 =      floats_tensor(control_image[0].shape   ,				rng=random.Random(__lowerCAmelCase					)					).to(__lowerCAmelCase					)
      _UpperCAmelCase						 =      image.cpu().permute(0   ,				2   ,				3   ,				1					)[0]
      _UpperCAmelCase						 =      Image.fromarray(np.uinta(__lowerCAmelCase					)					).convert("""RGB"""					).resize((64, 64)					)
      _UpperCAmelCase						 =      {
          """prompt""": """A painting of a squirrel eating a burger""",
          """generator""": generator,
          """num_inference_steps""": 2,
          """guidance_scale""": 6.0,
          """output_type""": """numpy""",
          """image""": image,
          """control_image""": control_image,
      }

      return inputs


    def      lowerCAmelCase_  (					self	:  Optional[Any]					):
      _UpperCAmelCase						 =      self.get_dummy_components()
      _UpperCAmelCase						 =      self.pipeline_class(**__lowerCAmelCase					)
      pipe.to(__lowerCAmelCase					)

      _UpperCAmelCase						 =      10.0
      _UpperCAmelCase						 =      4

      _UpperCAmelCase						 =      self.get_dummy_inputs(__lowerCAmelCase					)
      _UpperCAmelCase						 =      steps
      _UpperCAmelCase						 =      scale
      _UpperCAmelCase						 =      pipe(**__lowerCAmelCase					)[0]

      _UpperCAmelCase						 =      self.get_dummy_inputs(__lowerCAmelCase					)
      _UpperCAmelCase						 =      steps
      _UpperCAmelCase						 =      scale
      _UpperCAmelCase						 =      pipe(**__lowerCAmelCase   ,				control_guidance_start=0.1   ,				control_guidance_end=0.2					)[0]

      _UpperCAmelCase						 =      self.get_dummy_inputs(__lowerCAmelCase					)
      _UpperCAmelCase						 =      steps
      _UpperCAmelCase						 =      scale
      _UpperCAmelCase						 =      pipe(**__lowerCAmelCase   ,				control_guidance_start=[0.1, 0.3]   ,				control_guidance_end=[0.2, 0.7]					)[0]

      _UpperCAmelCase						 =      self.get_dummy_inputs(__lowerCAmelCase					)
      _UpperCAmelCase						 =      steps
      _UpperCAmelCase						 =      scale
      _UpperCAmelCase						 =      pipe(**__lowerCAmelCase   ,				control_guidance_start=0.4   ,				control_guidance_end=[0.5, 0.8]					)[0]

      # make sure that all outputs are different
      assert np.sum(np.abs(output_a - output_a					)					) > 1e-3
      assert np.sum(np.abs(output_a - output_a					)					) > 1e-3
      assert np.sum(np.abs(output_a - output_a					)					) > 1e-3


    def      lowerCAmelCase_  (					self	:  Union[str, Any]					):
      return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3					)


    @unittest.skipIf(
        torch_device != """cuda""" or not is_xformers_available()   ,				reason="""XFormers attention is only available with CUDA and `xformers` installed"""   ,				)
    def      lowerCAmelCase_  (					self	:  Dict					):
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2e-3					)


    def      lowerCAmelCase_  (					self	:  Dict					):
      self._test_inference_batch_single_identical(expected_max_diff=2e-3					)


    def      lowerCAmelCase_  (					self	:  List[Any]					):
      _UpperCAmelCase						 =      self.get_dummy_components()
      _UpperCAmelCase						 =      self.pipeline_class(**__lowerCAmelCase					)
      pipe.to(__lowerCAmelCase					)
      pipe.set_progress_bar_config(disable=__lowerCAmelCase					)
      with tempfile.TemporaryDirectory() as tmpdir:
        try:
          # save_pretrained is not implemented for Multi-ControlNet
          pipe.save_pretrained(__lowerCAmelCase					)
        except NotImplementedError:
          pass



@slow
@require_torch_gpu
class        a				(		unittest.TestCase   ):


    def      lowerCAmelCase_  (					self	:  Optional[int]					):
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()


    def      lowerCAmelCase_  (					self	:  List[str]					):
      _UpperCAmelCase						 =      ControlNetModel.from_pretrained("""lllyasviel/sd-controlnet-canny"""					)

      _UpperCAmelCase						 =      StableDiffusionControlNetImgaImgPipeline.from_pretrained(
          """runwayml/stable-diffusion-v1-5"""   ,				safety_checker=__lowerCAmelCase   ,				controlnet=__lowerCAmelCase					)
      pipe.enable_model_cpu_offload()
      pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

      _UpperCAmelCase						 =      torch.Generator(device="""cpu"""					).manual_seed(0					)
      _UpperCAmelCase						 =      """evil space-punk bird"""
      _UpperCAmelCase						 =      load_image(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png"""					).resize((512, 512)					)
      _UpperCAmelCase						 =      load_image(
          """https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png"""					).resize((512, 512)					)

      _UpperCAmelCase						 =      pipe(
          __lowerCAmelCase   ,				__lowerCAmelCase   ,				control_image=__lowerCAmelCase   ,				generator=__lowerCAmelCase   ,				output_type="""np"""   ,				num_inference_steps=50   ,				strength=0.6   ,				)

      _UpperCAmelCase						 =      output.images[0]

      assert image.shape == (512, 512, 3)

      _UpperCAmelCase						 =      load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/img2img.npy"""					)

      assert np.abs(expected_image - image					).max() < 9e-2
