code: 
  '''simple docstring'''
  
  
  
  
  
  # This model implementation is heavily based on:
  
  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,
      StableDiffusionControlNetInpaintPipeline,
      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 (
      TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
      TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
      TEXT_TO_IMAGE_IMAGE_PARAMS,
  )
  from ..test_pipelines_common import (
      PipelineKarrasSchedulerTesterMixin,
      PipelineLatentTesterMixin,
      PipelineTesterMixin,
  )
  
  
  enable_full_determinism()
  
  
  
  class  lowerCAmelCase__   (       UpperCamelCase					,							UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    _UpperCamelCase						= StableDiffusionControlNetInpaintPipeline
    _UpperCamelCase						= TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
    _UpperCamelCase						= TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
    _UpperCamelCase						= frozenset({'control_image'}       )  # skip `image` and `mask` for now, only test for control_image
    _UpperCamelCase						= TEXT_TO_IMAGE_IMAGE_PARAMS
  
    def 		_lowerCamelCase	(self  : Tuple    )   ->       Tuple:
          torch.manual_seed(0    )
          snake_case_						       =							UNetaDConditionModel(
              block_out_channels=(32, 64)	, layers_per_block=2	, sample_size=32	, in_channels=9	, out_channels=4	, down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")	, up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")	, cross_attention_dim=32	, )
          torch.manual_seed(0    )
          snake_case_						       =							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    )
          snake_case_						       =							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    )
          snake_case_						       =							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    )
          snake_case_						       =							CLIPTextConfig(
              bos_token_id=0	, eos_token_id=2	, hidden_size=32	, intermediate_size=37	, layer_norm_eps=1E-05	, num_attention_heads=4	, num_hidden_layers=5	, pad_token_id=1	, vocab_size=10_00	, )
          snake_case_						       =							CLIPTextModel(lowerCAmelCase_    )
          snake_case_						       =							CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"    )
  
          snake_case_						       =							{
              "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  : Optional[int]	, lowerCAmelCase_  : List[Any]	, lowerCAmelCase_  : Tuple=0    )   ->       List[Any]:
          if str(lowerCAmelCase_    ).startswith("mps"    ):
                snake_case_						       =							torch.manual_seed(lowerCAmelCase_    )
          else:
                snake_case_						       =							torch.Generator(device=lowerCAmelCase_    ).manual_seed(lowerCAmelCase_    )
  
          snake_case_						       =							2
          snake_case_						       =							randn_tensor(
              (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)	, generator=lowerCAmelCase_	, device=torch.device(lowerCAmelCase_    )	, )
          snake_case_						       =							floats_tensor((1, 3, 32, 32)	, rng=random.Random(lowerCAmelCase_    )    ).to(lowerCAmelCase_    )
          snake_case_						       =							init_image.cpu().permute(0	, 2	, 3	, 1    )[0]
  
          snake_case_						       =							Image.fromarray(np.uinta(lowerCAmelCase_    )    ).convert("RGB"    ).resize((64, 64)    )
          snake_case_						       =							Image.fromarray(np.uinta(init_image + 4    )    ).convert("RGB"    ).resize((64, 64)    )
  
          snake_case_						       =							{
              "prompt": "A painting of a squirrel eating a burger",
              "generator": generator,
              "num_inference_steps": 2,
              "guidance_scale": 6.0,
              "output_type": "numpy",
              "image": image,
              "mask_image": mask_image,
              "control_image": control_image,
          }
  
          return inputs
  
    def 		_lowerCamelCase	(self  : Union[str, Any]    )   ->       int:
          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  : Optional[int]    )   ->       List[Any]:
          self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3    )
  
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Tuple:
          self._test_inference_batch_single_identical(expected_max_diff=2E-3    )
  
  
  
  class  lowerCAmelCase__   (       UpperCamelCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    _UpperCamelCase						= StableDiffusionControlNetInpaintPipeline
    _UpperCamelCase						= TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
    _UpperCamelCase						= TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
    _UpperCamelCase						= frozenset([]       )
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Tuple:
          torch.manual_seed(0    )
          snake_case_						       =							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    )
          snake_case_						       =							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    )
          snake_case_						       =							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    )
          snake_case_						       =							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    )
          snake_case_						       =							CLIPTextConfig(
              bos_token_id=0	, eos_token_id=2	, hidden_size=32	, intermediate_size=37	, layer_norm_eps=1E-05	, num_attention_heads=4	, num_hidden_layers=5	, pad_token_id=1	, vocab_size=10_00	, )
          snake_case_						       =							CLIPTextModel(lowerCAmelCase_    )
          snake_case_						       =							CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"    )
  
          snake_case_						       =							{
              "unet": unet,
              "controlnet": controlnet,
              "scheduler": scheduler,
              "vae": vae,
              "text_encoder": text_encoder,
              "tokenizer": tokenizer,
              "safety_checker": None,
              "feature_extractor": None,
          }
          return components
  
  
  
  class  lowerCAmelCase__   (       UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    _UpperCamelCase						= StableDiffusionControlNetInpaintPipeline
    _UpperCamelCase						= TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
    _UpperCamelCase						= TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
  
    def 		_lowerCamelCase	(self  : str    )   ->       Dict:
          torch.manual_seed(0    )
          snake_case_						       =							UNetaDConditionModel(
              block_out_channels=(32, 64)	, layers_per_block=2	, sample_size=32	, in_channels=9	, 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_  : int    ):
                if isinstance(lowerCAmelCase_	, torch.nn.Convad    ):
                      torch.nn.init.normal(m.weight    )
                      m.bias.data.fill_(1.0    )
  
          snake_case_						       =							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    )
          snake_case_						       =							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    )
          snake_case_						       =							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    )
          snake_case_						       =							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    )
          snake_case_						       =							CLIPTextConfig(
              bos_token_id=0	, eos_token_id=2	, hidden_size=32	, intermediate_size=37	, layer_norm_eps=1E-05	, num_attention_heads=4	, num_hidden_layers=5	, pad_token_id=1	, vocab_size=10_00	, )
          snake_case_						       =							CLIPTextModel(lowerCAmelCase_    )
          snake_case_						       =							CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"    )
  
          snake_case_						       =							MultiControlNetModel([controlneta, controlneta]    )
  
          snake_case_						       =							{
              "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  : List[Any]	, lowerCAmelCase_  : Optional[Any]	, lowerCAmelCase_  : Any=0    )   ->       int:
          if str(lowerCAmelCase_    ).startswith("mps"    ):
                snake_case_						       =							torch.manual_seed(lowerCAmelCase_    )
          else:
                snake_case_						       =							torch.Generator(device=lowerCAmelCase_    ).manual_seed(lowerCAmelCase_    )
  
          snake_case_						       =							2
  
          snake_case_						       =							[
              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_    )	, ),
          ]
          snake_case_						       =							floats_tensor((1, 3, 32, 32)	, rng=random.Random(lowerCAmelCase_    )    ).to(lowerCAmelCase_    )
          snake_case_						       =							init_image.cpu().permute(0	, 2	, 3	, 1    )[0]
  
          snake_case_						       =							Image.fromarray(np.uinta(lowerCAmelCase_    )    ).convert("RGB"    ).resize((64, 64)    )
          snake_case_						       =							Image.fromarray(np.uinta(init_image + 4    )    ).convert("RGB"    ).resize((64, 64)    )
  
          snake_case_						       =							{
              "prompt": "A painting of a squirrel eating a burger",
              "generator": generator,
              "num_inference_steps": 2,
              "guidance_scale": 6.0,
              "output_type": "numpy",
              "image": image,
              "mask_image": mask_image,
              "control_image": control_image,
          }
  
          return inputs
  
    def 		_lowerCamelCase	(self  : List[str]    )   ->       Optional[int]:
          snake_case_						       =							self.get_dummy_components()
          snake_case_						       =							self.pipeline_class(**lowerCAmelCase_    )
          pipe.to(lowerCAmelCase_    )
  
          snake_case_						       =							10.0
          snake_case_						       =							4
  
          snake_case_						       =							self.get_dummy_inputs(lowerCAmelCase_    )
          snake_case_						       =							steps
          snake_case_						       =							scale
          snake_case_						       =							pipe(**lowerCAmelCase_    )[0]
  
          snake_case_						       =							self.get_dummy_inputs(lowerCAmelCase_    )
          snake_case_						       =							steps
          snake_case_						       =							scale
          snake_case_						       =							pipe(**lowerCAmelCase_	, control_guidance_start=0.1	, control_guidance_end=0.2    )[0]
  
          snake_case_						       =							self.get_dummy_inputs(lowerCAmelCase_    )
          snake_case_						       =							steps
          snake_case_						       =							scale
          snake_case_						       =							pipe(**lowerCAmelCase_	, control_guidance_start=[0.1, 0.3]	, control_guidance_end=[0.2, 0.7]    )[0]
  
          snake_case_						       =							self.get_dummy_inputs(lowerCAmelCase_    )
          snake_case_						       =							steps
          snake_case_						       =							scale
          snake_case_						       =							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  : List[Any]    )   ->       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  : str    )   ->       Tuple:
          self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3    )
  
    def 		_lowerCamelCase	(self  : List[str]    )   ->       List[Any]:
          self._test_inference_batch_single_identical(expected_max_diff=2E-3    )
  
  
    def 		_lowerCamelCase	(self  : int    )   ->       Tuple:
          snake_case_						       =							self.get_dummy_components()
          snake_case_						       =							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  lowerCAmelCase__   (       unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    def 		_lowerCamelCase	(self  : List[str]    )   ->       str:
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       int:
          snake_case_						       =							ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny"    )
  
          snake_case_						       =							StableDiffusionControlNetInpaintPipeline.from_pretrained(
              "runwayml/stable-diffusion-inpainting"	, safety_checker=lowerCAmelCase_	, controlnet=lowerCAmelCase_    )
          pipe.enable_model_cpu_offload()
          pipe.set_progress_bar_config(disable=lowerCAmelCase_    )
  
          snake_case_						       =							torch.Generator(device="cpu"    ).manual_seed(0    )
          snake_case_						       =							load_image(
              "https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png"    ).resize((5_12, 5_12)    )
  
          snake_case_						       =							load_image(
              "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
              "/stable_diffusion_inpaint/input_bench_mask.png"    ).resize((5_12, 5_12)    )
  
          snake_case_						       =							"pitch black hole"
  
          snake_case_						       =							load_image(
              "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png"    ).resize((5_12, 5_12)    )
  
          snake_case_						       =							pipe(
              lowerCAmelCase_	, image=lowerCAmelCase_	, mask_image=lowerCAmelCase_	, control_image=lowerCAmelCase_	, generator=lowerCAmelCase_	, output_type="np"	, num_inference_steps=3	, )
  
          snake_case_						       =							output.images[0]
  
          assert image.shape == (5_12, 5_12, 3)
  
          snake_case_						       =							load_numpy(
              "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/inpaint.npy"    )
  
          assert np.abs(expected_image - image    ).max() < 9E-2
  
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       Union[str, Any]:
          snake_case_						       =							ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_inpaint"    )
  
          snake_case_						       =							StableDiffusionControlNetInpaintPipeline.from_pretrained(
              "runwayml/stable-diffusion-v1-5"	, safety_checker=lowerCAmelCase_	, controlnet=lowerCAmelCase_    )
          snake_case_						       =							DDIMScheduler.from_config(pipe.scheduler.config    )
          pipe.enable_model_cpu_offload()
          pipe.set_progress_bar_config(disable=lowerCAmelCase_    )
  
          snake_case_						       =							torch.Generator(device="cpu"    ).manual_seed(33    )
  
          snake_case_						       =							load_image(
              "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy.png"    )
          snake_case_						       =							init_image.resize((5_12, 5_12)    )
  
          snake_case_						       =							load_image(
              "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy_mask.png"    )
          snake_case_						       =							mask_image.resize((5_12, 5_12)    )
  
          snake_case_						       =							"a handsome man with ray-ban sunglasses"
  
          def make_inpaint_condition(lowerCAmelCase_  : str	, lowerCAmelCase_  : List[Any]    ):
                snake_case_						       =							np.array(image.convert("RGB"    )    ).astype(np.floataa    ) / 255.0
                snake_case_						       =							np.array(image_mask.convert("L"    )    ).astype(np.floataa    ) / 255.0
  
                assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
                snake_case_						       =							-1.0  # set as masked pixel
                snake_case_						       =							np.expand_dims(lowerCAmelCase_	, 0    ).transpose(0	, 3	, 1	, 2    )
                snake_case_						       =							torch.from_numpy(lowerCAmelCase_    )
                return image
  
          snake_case_						       =							make_inpaint_condition(lowerCAmelCase_	, lowerCAmelCase_    )
  
          snake_case_						       =							pipe(
              lowerCAmelCase_	, image=lowerCAmelCase_	, mask_image=lowerCAmelCase_	, control_image=lowerCAmelCase_	, guidance_scale=9.0	, eta=1.0	, generator=lowerCAmelCase_	, num_inference_steps=20	, output_type="np"	, )
          snake_case_						       =							output.images[0]
  
          assert image.shape == (5_12, 5_12, 3)
  
          snake_case_						       =							load_numpy(
              "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/boy_ray_ban.npy"    )
  
          assert np.abs(expected_image - image    ).max() < 9E-2
  
  
style_context: 
  '''simple docstring'''
  
  
  
  
  
  from __future__ import annotations
  
  import inspect
  import unittest
  
  import numpy as np
  
  from transformers import SwinConfig
  from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
  from transformers.utils import cached_property, is_tf_available, is_vision_available
  
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
  from ...test_pipeline_mixin import PipelineTesterMixin
  
  
  if is_tf_available():
       import tensorflow as tf
  
       from transformers.models.swin.modeling_tf_swin import (
           TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
           TFSwinForImageClassification,
           TFSwinForMaskedImageModeling,
           TFSwinModel,
       )
  
  
  if is_vision_available():
       from PIL import Image
  
       from transformers import AutoImageProcessor
  
  
  
  class  lowerCAmelCase__   :
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    def __init__(self  : Optional[int]	, lowerCAmelCase_  : List[Any]	, lowerCAmelCase_  : Any=13	, lowerCAmelCase_  : int=32	, lowerCAmelCase_  : int=2	, lowerCAmelCase_  : List[str]=3	, lowerCAmelCase_  : Any=16	, lowerCAmelCase_  : Tuple=[1, 2, 1]	, lowerCAmelCase_  : List[str]=[2, 2, 4]	, lowerCAmelCase_  : Dict=2	, lowerCAmelCase_  : str=2.0	, lowerCAmelCase_  : int=True	, lowerCAmelCase_  : List[Any]=0.0	, lowerCAmelCase_  : List[Any]=0.0	, lowerCAmelCase_  : List[Any]=0.1	, lowerCAmelCase_  : Optional[int]="gelu"	, lowerCAmelCase_  : Dict=False	, lowerCAmelCase_  : str=True	, lowerCAmelCase_  : Optional[int]=0.02	, lowerCAmelCase_  : List[str]=1E-5	, lowerCAmelCase_  : Dict=True	, lowerCAmelCase_  : Tuple=None	, lowerCAmelCase_  : List[str]=True	, lowerCAmelCase_  : str=10	, lowerCAmelCase_  : Tuple=8	, )   ->       None:
          snake_case_						       =							parent
          snake_case_						       =							batch_size
          snake_case_						       =							image_size
          snake_case_						       =							patch_size
          snake_case_						       =							num_channels
          snake_case_						       =							embed_dim
          snake_case_						       =							depths
          snake_case_						       =							num_heads
          snake_case_						       =							window_size
          snake_case_						       =							mlp_ratio
          snake_case_						       =							qkv_bias
          snake_case_						       =							hidden_dropout_prob
          snake_case_						       =							attention_probs_dropout_prob
          snake_case_						       =							drop_path_rate
          snake_case_						       =							hidden_act
          snake_case_						       =							use_absolute_embeddings
          snake_case_						       =							patch_norm
          snake_case_						       =							layer_norm_eps
          snake_case_						       =							initializer_range
          snake_case_						       =							is_training
          snake_case_						       =							scope
          snake_case_						       =							use_labels
          snake_case_						       =							type_sequence_label_size
          snake_case_						       =							encoder_stride
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Dict:
          snake_case_						       =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )
  
          snake_case_						       =							None
          if self.use_labels:
                snake_case_						       =							ids_tensor([self.batch_size]	, self.type_sequence_label_size    )
  
          snake_case_						       =							self.get_config()
  
          return config, pixel_values, labels
  
    def 		_lowerCamelCase	(self  : str    )   ->       Tuple:
          return SwinConfig(
              image_size=self.image_size	, patch_size=self.patch_size	, num_channels=self.num_channels	, embed_dim=self.embed_dim	, depths=self.depths	, num_heads=self.num_heads	, window_size=self.window_size	, mlp_ratio=self.mlp_ratio	, qkv_bias=self.qkv_bias	, hidden_dropout_prob=self.hidden_dropout_prob	, attention_probs_dropout_prob=self.attention_probs_dropout_prob	, drop_path_rate=self.drop_path_rate	, hidden_act=self.hidden_act	, use_absolute_embeddings=self.use_absolute_embeddings	, path_norm=self.patch_norm	, layer_norm_eps=self.layer_norm_eps	, initializer_range=self.initializer_range	, encoder_stride=self.encoder_stride	, )
  
    def 		_lowerCamelCase	(self  : Optional[int]	, lowerCAmelCase_  : int	, lowerCAmelCase_  : List[Any]	, lowerCAmelCase_  : Optional[int]    )   ->       Any:
          snake_case_						       =							TFSwinModel(config=lowerCAmelCase_    )
          snake_case_						       =							model(lowerCAmelCase_    )
  
          snake_case_						       =							((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
          snake_case_						       =							int(config.embed_dim * 2 ** (len(config.depths    ) - 1)    )
  
          self.parent.assertEqual(result.last_hidden_state.shape	, (self.batch_size, expected_seq_len, expected_dim)    )
  
    def 		_lowerCamelCase	(self  : Optional[Any]	, lowerCAmelCase_  : Optional[int]	, lowerCAmelCase_  : Dict	, lowerCAmelCase_  : Optional[int]    )   ->       Optional[int]:
          snake_case_						       =							TFSwinForMaskedImageModeling(config=lowerCAmelCase_    )
          snake_case_						       =							model(lowerCAmelCase_    )
          self.parent.assertEqual(
              result.logits.shape	, (self.batch_size, self.num_channels, self.image_size, self.image_size)    )
  
          # test greyscale images
          snake_case_						       =							1
          snake_case_						       =							TFSwinForMaskedImageModeling(lowerCAmelCase_    )
  
          snake_case_						       =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
          snake_case_						       =							model(lowerCAmelCase_    )
          self.parent.assertEqual(result.logits.shape	, (self.batch_size, 1, self.image_size, self.image_size)    )
  
    def 		_lowerCamelCase	(self  : Dict	, lowerCAmelCase_  : Tuple	, lowerCAmelCase_  : List[str]	, lowerCAmelCase_  : Optional[Any]    )   ->       Tuple:
          snake_case_						       =							self.type_sequence_label_size
          snake_case_						       =							TFSwinForImageClassification(lowerCAmelCase_    )
          snake_case_						       =							model(lowerCAmelCase_	, labels=lowerCAmelCase_    )
          self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.type_sequence_label_size)    )
  
          # test greyscale images
          snake_case_						       =							1
          snake_case_						       =							TFSwinForImageClassification(lowerCAmelCase_    )
          snake_case_						       =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
          snake_case_						       =							model(lowerCAmelCase_    )
          self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.type_sequence_label_size)    )
  
  
    def 		_lowerCamelCase	(self  : Optional[Any]    )   ->       str:
          snake_case_						       =							self.prepare_config_and_inputs()
          snake_case_       ,	snake_case_       ,	snake_case_						       =							config_and_inputs
          snake_case_						       =							{"pixel_values": pixel_values}
          return config, inputs_dict
  
  
  
  @require_tf
  class  lowerCAmelCase__   (       UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    _UpperCamelCase						= (
        (
            TFSwinModel,
            TFSwinForImageClassification,
            TFSwinForMaskedImageModeling,
        )
        if is_tf_available()
        else ()
    )
    _UpperCamelCase						= (
        {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
        if is_tf_available()
        else {}
    )
  
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
  
    def 		_lowerCamelCase	(self  : Any    )   ->       List[Any]:
          snake_case_						       =							TFSwinModelTester(self    )
          snake_case_						       =							ConfigTester(self	, config_class=lowerCAmelCase_	, embed_dim=37    )
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Optional[Any]:
          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  : List[Any]    )   ->       List[str]:
          return
  
    def 		_lowerCamelCase	(self  : Any    )   ->       Dict:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : int    )   ->       Any:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : Tuple    )   ->       Optional[Any]:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_    )
  
    @unittest.skip(reason="Swin does not use inputs_embeds"    )
    def 		_lowerCamelCase	(self  : Optional[Any]    )   ->       Union[str, Any]:
          pass
  
    def 		_lowerCamelCase	(self  : List[str]    )   ->       Any:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                snake_case_						       =							model_class(lowerCAmelCase_    )
                self.assertIsInstance(model.get_input_embeddings()	, tf.keras.layers.Layer    )
                snake_case_						       =							model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(lowerCAmelCase_	, tf.keras.layers.Dense    )    )
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       Dict:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							inspect.signature(model.call    )
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                snake_case_						       =							[*signature.parameters.keys()]
  
                snake_case_						       =							["pixel_values"]
                self.assertListEqual(arg_names[:1]	, lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       Any:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
          snake_case_						       =							True
  
          for model_class in self.all_model_classes:
                snake_case_						       =							True
                snake_case_						       =							False
                snake_case_						       =							True
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
                snake_case_						       =							outputs.attentions
                snake_case_						       =							len(self.model_tester.depths    )
                self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
                # check that output_attentions also work using config
                del inputs_dict["output_attentions"]
                snake_case_						       =							True
                snake_case_						       =							config.window_size**2
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
                snake_case_						       =							outputs.attentions
                self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
                self.assertListEqual(
                    list(attentions[0].shape[-3:]    )	, [self.model_tester.num_heads[0], window_size_squared, window_size_squared]	, )
                snake_case_						       =							len(lowerCAmelCase_    )
  
                # Check attention is always last and order is fine
                snake_case_						       =							True
                snake_case_						       =							True
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
  
                if hasattr(self.model_tester	, "num_hidden_states_types"    ):
                      snake_case_						       =							self.model_tester.num_hidden_states_types
                else:
                      # also another +1 for reshaped_hidden_states
                      snake_case_						       =							2
                self.assertEqual(out_len + added_hidden_states	, len(lowerCAmelCase_    )    )
  
                snake_case_						       =							outputs.attentions
  
                self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
                self.assertListEqual(
                    list(self_attentions[0].shape[-3:]    )	, [self.model_tester.num_heads[0], window_size_squared, window_size_squared]	, )
  
    def 		_lowerCamelCase	(self  : Optional[int]	, lowerCAmelCase_  : Union[str, Any]	, lowerCAmelCase_  : int	, lowerCAmelCase_  : Dict	, lowerCAmelCase_  : List[Any]    )   ->       Optional[int]:
          snake_case_						       =							model_class(lowerCAmelCase_    )
          snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
          snake_case_						       =							outputs.hidden_states
  
          snake_case_						       =							getattr(
              self.model_tester	, "expected_num_hidden_layers"	, len(self.model_tester.depths    ) + 1    )
          self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
          # Swin has a different seq_length
          snake_case_						       =							to_atuple(config.patch_size    )
  
          snake_case_						       =							(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
  
          self.assertListEqual(
              list(hidden_states[0].shape[-2:]    )	, [num_patches, self.model_tester.embed_dim]	, )
  
          snake_case_						       =							outputs.reshaped_hidden_states
          self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
          snake_case_       ,	snake_case_       ,	snake_case_       ,	snake_case_						       =							reshaped_hidden_states[0].shape
  
          snake_case_						       =							tf.reshape(reshaped_hidden_states[0]	, (batch_size, num_channels, height * width)    )
          snake_case_						       =							tf.transpose(lowerCAmelCase_	, (0, 2, 1)    )
  
          self.assertListEqual(
              list(reshaped_hidden_states.shape[-2:]    )	, [num_patches, self.model_tester.embed_dim]	, )
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       str:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
  
          snake_case_						       =							to_atuple(self.model_tester.image_size    )
  
          for model_class in self.all_model_classes:
                snake_case_						       =							True
                self.check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_    )
  
                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                snake_case_						       =							True
  
                self.check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : Tuple    )   ->       List[Any]:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
          snake_case_						       =							3
  
          snake_case_						       =							to_atuple(self.model_tester.image_size    )
          snake_case_						       =							to_atuple(config.patch_size    )
  
          snake_case_						       =							image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
          snake_case_						       =							image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])
  
          for model_class in self.all_model_classes:
                snake_case_						       =							True
                self.check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_	, (padded_height, padded_width)    )
  
                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                snake_case_						       =							True
                self.check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_	, (padded_height, padded_width)    )
  
  
    @slow
    def 		_lowerCamelCase	(self  : Any    )   ->       List[str]:
          for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                snake_case_						       =							TFSwinModel.from_pretrained(lowerCAmelCase_    )
                self.assertIsNotNone(lowerCAmelCase_    )
  
  
  
  @require_vision
  @require_tf
  class  lowerCAmelCase__   (       unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    @cached_property
    def 		_lowerCamelCase	(self  : Dict    )   ->       str:
          return (
              AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224"    )
              if is_vision_available()
              else None
          )
  
  
    @slow
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       str:
          snake_case_						       =							TFSwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224"    )
          snake_case_						       =							self.default_image_processor
  
          snake_case_						       =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
          snake_case_						       =							image_processor(images=lowerCAmelCase_	, return_tensors="tf"    )
  
          # forward pass
          snake_case_						       =							model(lowerCAmelCase_    )
  
          # verify the logits
          snake_case_						       =							tf.TensorShape((1, 10_00)    )
          self.assertEqual(outputs.logits.shape	, lowerCAmelCase_    )
          snake_case_						       =							tf.constant([-0.0_948, -0.6_454, -0.0_921]    )
          self.assertTrue(np.allclose(outputs.logits[0, :3]	, lowerCAmelCase_	, atol=1E-4    )    )
  
  