





"""simple docstring"""


import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
    from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor


class        a				:


    def __init__(					self	:  Optional[Any]   ,				__lowerCAmelCase	:  Optional[int]   ,				__lowerCAmelCase	:  int=13   ,				__lowerCAmelCase	:  Optional[Any]=32   ,				__lowerCAmelCase	:  Dict=3   ,				__lowerCAmelCase	:  Tuple=4   ,				__lowerCAmelCase	:  Union[str, Any]=[10, 20, 30, 40]   ,				__lowerCAmelCase	:  Tuple=[2, 2, 3, 2]   ,				__lowerCAmelCase	:  int=True   ,				__lowerCAmelCase	:  Dict=True   ,				__lowerCAmelCase	:  List[str]=37   ,				__lowerCAmelCase	:  List[Any]="gelu"   ,				__lowerCAmelCase	:  Any=10   ,				__lowerCAmelCase	:  List[str]=0.02   ,				__lowerCAmelCase	:  List[str]=["stage2", "stage3", "stage4"]   ,				__lowerCAmelCase	:  Tuple=[2, 3, 4]   ,				__lowerCAmelCase	:  Tuple=None   ,				):
      _UpperCAmelCase						 =      parent
      _UpperCAmelCase						 =      batch_size
      _UpperCAmelCase						 =      image_size
      _UpperCAmelCase						 =      num_channels
      _UpperCAmelCase						 =      num_stages
      _UpperCAmelCase						 =      hidden_sizes
      _UpperCAmelCase						 =      depths
      _UpperCAmelCase						 =      is_training
      _UpperCAmelCase						 =      use_labels
      _UpperCAmelCase						 =      intermediate_size
      _UpperCAmelCase						 =      hidden_act
      _UpperCAmelCase						 =      num_labels
      _UpperCAmelCase						 =      initializer_range
      _UpperCAmelCase						 =      out_features
      _UpperCAmelCase						 =      out_indices
      _UpperCAmelCase						 =      scope


    def      lowerCAmelCase_  (					self	:  Tuple					):
      _UpperCAmelCase						 =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

      _UpperCAmelCase						 =      None
      if self.use_labels:
        _UpperCAmelCase						 =      ids_tensor([self.batch_size]   ,				self.num_labels					)

      _UpperCAmelCase						 =      self.get_config()

      return config, pixel_values, labels


    def      lowerCAmelCase_  (					self	:  Union[str, Any]					):
      return ConvNextVaConfig(
          num_channels=self.num_channels   ,				hidden_sizes=self.hidden_sizes   ,				depths=self.depths   ,				num_stages=self.num_stages   ,				hidden_act=self.hidden_act   ,				is_decoder=__lowerCamelCase   ,				initializer_range=self.initializer_range   ,				out_features=self.out_features   ,				out_indices=self.out_indices   ,				num_labels=self.num_labels   ,				)


    def      lowerCAmelCase_  (					self	:  Optional[int]   ,				__lowerCAmelCase	:  Optional[int]   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  int					):
      _UpperCAmelCase						 =      ConvNextVaModel(config=__lowerCamelCase					)
      model.to(__lowerCamelCase					)
      model.eval()
      _UpperCAmelCase						 =      model(__lowerCamelCase					)
      # expected last hidden states: B, C, H // 32, W // 32
      self.parent.assertEqual(
          result.last_hidden_state.shape   ,				(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)   ,				)


    def      lowerCAmelCase_  (					self	:  Optional[Any]   ,				__lowerCAmelCase	:  Union[str, Any]   ,				__lowerCAmelCase	:  Dict   ,				__lowerCAmelCase	:  Dict					):
      _UpperCAmelCase						 =      ConvNextVaForImageClassification(__lowerCamelCase					)
      model.to(__lowerCamelCase					)
      model.eval()
      _UpperCAmelCase						 =      model(__lowerCamelCase   ,				labels=__lowerCamelCase					)
      self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels)					)


    def      lowerCAmelCase_  (					self	:  Any   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  str   ,				__lowerCAmelCase	:  Dict					):
      _UpperCAmelCase						 =      ConvNextVaBackbone(config=__lowerCamelCase					)
      model.to(__lowerCamelCase					)
      model.eval()
      _UpperCAmelCase						 =      model(__lowerCamelCase					)

      # verify hidden states
      self.parent.assertEqual(len(result.feature_maps					)   ,				len(config.out_features					)					)
      self.parent.assertListEqual(list(result.feature_maps[0].shape					)   ,				[self.batch_size, self.hidden_sizes[1], 4, 4]					)

      # verify channels
      self.parent.assertEqual(len(model.channels					)   ,				len(config.out_features					)					)
      self.parent.assertListEqual(model.channels   ,				config.hidden_sizes[1:]					)

      # verify backbone works with out_features=None
      _UpperCAmelCase						 =      None
      _UpperCAmelCase						 =      ConvNextVaBackbone(config=__lowerCamelCase					)
      model.to(__lowerCamelCase					)
      model.eval()
      _UpperCAmelCase						 =      model(__lowerCamelCase					)

      # verify feature maps
      self.parent.assertEqual(len(result.feature_maps					)   ,				1					)
      self.parent.assertListEqual(list(result.feature_maps[0].shape					)   ,				[self.batch_size, self.hidden_sizes[-1], 1, 1]					)

      # verify channels
      self.parent.assertEqual(len(model.channels					)   ,				1					)
      self.parent.assertListEqual(model.channels   ,				[config.hidden_sizes[-1]]					)


    def      lowerCAmelCase_  (					self	:  Tuple					):
      _UpperCAmelCase						 =      self.prepare_config_and_inputs()
      _UpperCAmelCase						 =      config_and_inputs
      _UpperCAmelCase						 =      {'''pixel_values''': pixel_values}
      return config, inputs_dict


    def      lowerCAmelCase_  (					self	:  List[Any]					):
      _UpperCAmelCase						 =      self.prepare_config_and_inputs()
      _UpperCAmelCase						 =      config_and_inputs
      _UpperCAmelCase						 =      {'''pixel_values''': pixel_values, '''labels''': labels}
      return config, inputs_dict



@require_torch
class        a				(		lowerCamelCase__	,							lowerCamelCase__	,							unittest.TestCase   ):
    _snake_case     :  List[Any]     							=							(
        (
            ConvNextVaModel,
            ConvNextVaForImageClassification,
            ConvNextVaBackbone,
        )
        if is_torch_available()
        else ()
    )
    _snake_case     :  Tuple     							=							(
        {'feature-extraction': ConvNextVaModel, 'image-classification': ConvNextVaForImageClassification}
        if is_torch_available()
        else {}
    )

    _snake_case     :  Dict     							=							False
    _snake_case     :  Optional[Any]     							=							False
    _snake_case     :  str     							=							False
    _snake_case     :  Optional[Any]     							=							False
    _snake_case     :  Tuple     							=							False


    def      lowerCAmelCase_  (					self	:  Union[str, Any]					):
      _UpperCAmelCase						 =      ConvNextVaModelTester(self					)
      _UpperCAmelCase						 =      ConfigTester(self   ,				config_class=__lowerCamelCase   ,				has_text_modality=__lowerCamelCase   ,				hidden_size=37					)


    def      lowerCAmelCase_  (					self	:  str					):
      self.create_and_test_config_common_properties()
      self.config_tester.create_and_test_config_to_json_string()
      self.config_tester.create_and_test_config_to_json_file()
      self.config_tester.create_and_test_config_from_and_save_pretrained()
      self.config_tester.create_and_test_config_with_num_labels()
      self.config_tester.check_config_can_be_init_without_params()
      self.config_tester.check_config_arguments_init()


    def      lowerCAmelCase_  (					self	:  Union[str, Any]					):
      return


    @unittest.skip(reason="""ConvNextV2 does not use inputs_embeds"""					)
    def      lowerCAmelCase_  (					self	:  Dict					):
      pass


    @unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""					)
    def      lowerCAmelCase_  (					self	:  Dict					):
      pass


    @unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""					)
    def      lowerCAmelCase_  (					self	:  Tuple					):
      pass


    def      lowerCAmelCase_  (					self	:  Tuple					):






      if not self.model_tester.is_training:
        return

      for model_class in self.all_model_classes:
        _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs_with_labels()
        _UpperCAmelCase						 =      True

        if model_class.__name__ in [
            *get_values(__lowerCamelCase					),
            *get_values(__lowerCamelCase					),
        ]:
          continue

        _UpperCAmelCase						 =      model_class(__lowerCamelCase					)
        model.to(__lowerCamelCase					)
        model.train()
        _UpperCAmelCase						 =      self._prepare_for_class(__lowerCamelCase   ,				__lowerCamelCase   ,				return_labels=__lowerCamelCase					)
        _UpperCAmelCase						 =      model(**__lowerCamelCase					).loss
        loss.backward()


    def      lowerCAmelCase_  (					self	:  Tuple					):






      if not self.model_tester.is_training:
        return

      for model_class in self.all_model_classes:
        _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs_with_labels()
        _UpperCAmelCase						 =      False
        _UpperCAmelCase						 =      True

        if (
            model_class.__name__
            in [*get_values(__lowerCamelCase					), *get_values(__lowerCamelCase					)]
            or not model_class.supports_gradient_checkpointing
        ):
          continue

        _UpperCAmelCase						 =      model_class(__lowerCamelCase					)
        model.to(__lowerCamelCase					)
        model.gradient_checkpointing_enable()
        model.train()
        _UpperCAmelCase						 =      self._prepare_for_class(__lowerCamelCase   ,				__lowerCamelCase   ,				return_labels=__lowerCamelCase					)
        _UpperCAmelCase						 =      model(**__lowerCamelCase					).loss
        loss.backward()


    def      lowerCAmelCase_  (					self	:  Any					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        _UpperCAmelCase						 =      model_class(__lowerCamelCase					)
        _UpperCAmelCase						 =      inspect.signature(model.forward					)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        _UpperCAmelCase						 =      [*signature.parameters.keys()]

        _UpperCAmelCase						 =      ['''pixel_values''']
        self.assertListEqual(arg_names[:1]   ,				__lowerCamelCase					)


    def      lowerCAmelCase_  (					self	:  List[str]					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCamelCase					)


    def      lowerCAmelCase_  (					self	:  Tuple					):






      def check_hidden_states_output(__lowerCAmelCase	:  Optional[int]   ,				__lowerCAmelCase	:  Any   ,				__lowerCAmelCase	:  List[Any]					):
        _UpperCAmelCase						 =      model_class(__lowerCamelCase					)
        model.to(__lowerCamelCase					)
        model.eval()

        with torch.no_grad():
          _UpperCAmelCase						 =      model(**self._prepare_for_class(__lowerCamelCase   ,				__lowerCamelCase					)					)

        _UpperCAmelCase						 =      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        _UpperCAmelCase						 =      self.model_tester.num_stages
        self.assertEqual(len(__lowerCamelCase					)   ,				expected_num_stages + 1					)

        # ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width)
        self.assertListEqual(
            list(hidden_states[0].shape[-2:]					)   ,				[self.model_tester.image_size // 4, self.model_tester.image_size // 4]   ,				)

      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        _UpperCAmelCase						 =      True
        check_hidden_states_output(__lowerCamelCase   ,				__lowerCamelCase   ,				__lowerCamelCase					)

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        _UpperCAmelCase						 =      True

        check_hidden_states_output(__lowerCamelCase   ,				__lowerCamelCase   ,				__lowerCamelCase					)


    def      lowerCAmelCase_  (					self	:  List[Any]					):
      _UpperCAmelCase						 =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase					)


    @slow
    def      lowerCAmelCase_  (					self	:  Tuple					):






      for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _UpperCAmelCase						 =      ConvNextVaModel.from_pretrained(__lowerCamelCase					)
        self.assertIsNotNone(__lowerCamelCase					)






def     __UpperCAmelCase					(			):







  """simple docstring"""
  _UpperCAmelCase						 =      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
  return image


@require_torch
@require_vision
class        a				(		unittest.TestCase   ):


    @cached_property
    def      lowerCAmelCase_  (					self	:  Dict					):
      return AutoImageProcessor.from_pretrained("""facebook/convnextv2-tiny-1k-224"""					) if is_vision_available() else None


    @slow
    def      lowerCAmelCase_  (					self	:  int					):
      _UpperCAmelCase						 =      ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""					).to(__lowerCamelCase					)

      _UpperCAmelCase						 =      self.default_image_processor
      _UpperCAmelCase						 =      prepare_img()
      _UpperCAmelCase						 =      preprocessor(images=__lowerCamelCase   ,				return_tensors="""pt"""					).to(__lowerCamelCase					)

      # forward pass
      with torch.no_grad():
        _UpperCAmelCase						 =      model(**__lowerCamelCase					)

      # verify the logits
      _UpperCAmelCase						 =      torch.Size((1, 1000)					)
      self.assertEqual(outputs.logits.shape   ,				__lowerCamelCase					)

      _UpperCAmelCase						 =      torch.tensor([0.9_996, 0.1_966, -0.4_386]					).to(__lowerCamelCase					)
      self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,				__lowerCamelCase   ,				atol=1e-4					)					)

"""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
