


'''simple docstring'''
import collections
import inspect
import unittest

from transformers import SwinvaConfig
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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch
					from torch import nn

					from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
					from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor




class        _UpperCamelCase	:


				'''simple docstring'''


				def __init__(     self							,      a_							,      a_=1_3							,      a_=3_2							,      a_=2							,      a_=3							,      a_=1_6							,      a_=[1, 2, 1]							,      a_=[2, 2, 4]							,      a_=2							,      a_=2.0							,      a_=True							,      a_=0.0							,      a_=0.0							,      a_=0.1							,      a_="gelu"							,      a_=False							,      a_=True							,      a_=0.02							,      a_=1e-5							,      a_=True							,      a_=None							,      a_=True							,      a_=1_0							,      a_=8							,      )    ->  str:
					lowercase						:		List[str]   					=		parent
					lowercase						:		Optional[Any]   					=		batch_size
					lowercase						:		str   					=		image_size
					lowercase						:		Optional[Any]   					=		patch_size
					lowercase						:		str   					=		num_channels
					lowercase						:		int   					=		embed_dim
					lowercase						:		List[str]   					=		depths
					lowercase						:		Union[str, Any]   					=		num_heads
					lowercase						:		Optional[Any]   					=		window_size
					lowercase						:		Any   					=		mlp_ratio
					lowercase						:		Optional[int]   					=		qkv_bias
					lowercase						:		Union[str, Any]   					=		hidden_dropout_prob
					lowercase						:		Optional[int]   					=		attention_probs_dropout_prob
					lowercase						:		List[Any]   					=		drop_path_rate
					lowercase						:		int   					=		hidden_act
					lowercase						:		List[str]   					=		use_absolute_embeddings
					lowercase						:		str   					=		patch_norm
					lowercase						:		Union[str, Any]   					=		layer_norm_eps
					lowercase						:		Tuple   					=		initializer_range
					lowercase						:		Dict   					=		is_training
					lowercase						:		List[str]   					=		scope
					lowercase						:		Any   					=		use_labels
					lowercase						:		str   					=		type_sequence_label_size
					lowercase						:		Dict   					=		encoder_stride

				def 	a__		(     self							)    ->  Optional[Any]:
					lowercase						:		Union[str, Any]   					=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

					lowercase						:		Dict   					=		None
					if self.use_labels:
						lowercase						:		Tuple   					=		ids_tensor([self.batch_size]							,      self.type_sequence_label_size							)

					lowercase						:		Optional[int]   					=		self.get_config()

					return config, pixel_values, labels

				def 	a__		(     self							)    ->  int:
					return SwinvaConfig(
					    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 	a__		(     self							,      a_							,      a_							,      a_							)    ->  str:
					lowercase						:		List[str]   					=		SwinvaModel(config=a_							)
					model.to(a_							)
					model.eval()
					lowercase						:		str   					=		model(a_							)

					lowercase						:		Optional[int]   					=		((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
					lowercase						:		str   					=		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 	a__		(     self							,      a_							,      a_							,      a_							)    ->  Union[str, Any]:
					lowercase						:		Tuple   					=		SwinvaForMaskedImageModeling(config=a_							)
					model.to(a_							)
					model.eval()
					lowercase						:		Union[str, Any]   					=		model(a_							)
					self.parent.assertEqual(
					    result.logits.shape							,      (self.batch_size, self.num_channels, self.image_size, self.image_size)							)

					# test greyscale images
					lowercase						:		Tuple   					=		1
					lowercase						:		Any   					=		SwinvaForMaskedImageModeling(a_							)
					model.to(a_							)
					model.eval()

					lowercase						:		Any   					=		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
					lowercase						:		List[Any]   					=		model(a_							)
					self.parent.assertEqual(result.logits.shape							,      (self.batch_size, 1, self.image_size, self.image_size)							)

				def 	a__		(     self							,      a_							,      a_							,      a_							)    ->  Optional[Any]:
					lowercase						:		Optional[Any]   					=		self.type_sequence_label_size
					lowercase						:		Dict   					=		SwinvaForImageClassification(a_							)
					model.to(a_							)
					model.eval()
					lowercase						:		int   					=		model(a_							,      labels=a_							)
					self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.type_sequence_label_size)							)


				def 	a__		(     self							)    ->  Tuple:
					lowercase						:		Optional[int]   					=		self.prepare_config_and_inputs()
					lowercase					,		lowercase					,		lowercase						:		Dict   					=		config_and_inputs
					lowercase						:		List[str]   					=		{"pixel_values": pixel_values}
					return config, inputs_dict




@require_torch
class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			unittest.TestCase):


				'''simple docstring'''


				_snake_case						       =				(
				    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
				)
				_snake_case						       =				(
				    {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
				    if is_torch_available()
				    else {}
				)

				_snake_case						       =				False
				_snake_case						       =				False
				_snake_case						       =				False
				_snake_case						       =				False

				def 	a__		(     self							)    ->  Any:
					lowercase						:		Union[str, Any]   					=		SwinvaModelTester(self							)
					lowercase						:		Any   					=		ConfigTester(self							,      config_class=a_							,      embed_dim=3_7							)

				def 	a__		(     self							)    ->  List[str]:
					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 	a__		(     self							)    ->  List[str]:
					lowercase						:		Dict   					=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*a_							)

				@unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0."							)
				def 	a__		(     self							)    ->  Optional[int]:
					pass

				@unittest.skip(reason="Swinv2 does not use inputs_embeds"							)
				def 	a__		(     self							)    ->  Optional[int]:
					pass

				def 	a__		(     self							)    ->  List[str]:
					lowercase					,		lowercase						:		str   					=		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						lowercase						:		Dict   					=		model_class(a_							)
						self.assertIsInstance(model.get_input_embeddings()							,      (nn.Module)							)
						lowercase						:		List[str]   					=		model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(a_							,      nn.Linear							)							)

				def 	a__		(     self							)    ->  List[str]:
					lowercase					,		lowercase						:		Optional[int]   					=		self.model_tester.prepare_config_and_inputs_for_common()

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

						lowercase						:		Dict   					=		["pixel_values"]
						self.assertListEqual(arg_names[:1]							,      a_							)

				def 	a__		(     self							)    ->  int:
					lowercase					,		lowercase						:		str   					=		self.model_tester.prepare_config_and_inputs_for_common()
					lowercase						:		str   					=		True

					for model_class in self.all_model_classes:
						lowercase						:		str   					=		True
						lowercase						:		List[str]   					=		False
						lowercase						:		Union[str, Any]   					=		True
						lowercase						:		Any   					=		model_class(a_							)
						model.to(a_							)
						model.eval()
						with torch.no_grad():
							lowercase						:		Optional[Any]   					=		model(**self._prepare_for_class(a_							,      a_							)							)
						lowercase						:		List[str]   					=		outputs.attentions
						lowercase						:		Tuple   					=		len(self.model_tester.depths							)
						self.assertEqual(len(a_							)							,      a_							)

						# check that output_attentions also work using config
						del inputs_dict["output_attentions"]
						lowercase						:		List[Any]   					=		True
						lowercase						:		Dict   					=		config.window_size**2
						lowercase						:		Union[str, Any]   					=		model_class(a_							)
						model.to(a_							)
						model.eval()
						with torch.no_grad():
							lowercase						:		str   					=		model(**self._prepare_for_class(a_							,      a_							)							)
						lowercase						:		Any   					=		outputs.attentions
						self.assertEqual(len(a_							)							,      a_							)

						self.assertListEqual(
						    list(attentions[0].shape[-3:]							)							,      [self.model_tester.num_heads[0], window_size_squared, window_size_squared]							,      )
						lowercase						:		str   					=		len(a_							)

						# Check attention is always last and order is fine
						lowercase						:		List[str]   					=		True
						lowercase						:		Dict   					=		True
						lowercase						:		Any   					=		model_class(a_							)
						model.to(a_							)
						model.eval()
						with torch.no_grad():
							lowercase						:		str   					=		model(**self._prepare_for_class(a_							,      a_							)							)

						if hasattr(self.model_tester							,      "num_hidden_states_types"							):
							lowercase						:		int   					=		self.model_tester.num_hidden_states_types
						else:
							# also another +1 for reshaped_hidden_states
							lowercase						:		str   					=		2
						self.assertEqual(out_len + added_hidden_states							,      len(a_							)							)

						lowercase						:		List[str]   					=		outputs.attentions

						self.assertEqual(len(a_							)							,      a_							)

						self.assertListEqual(
						    list(self_attentions[0].shape[-3:]							)							,      [self.model_tester.num_heads[0], window_size_squared, window_size_squared]							,      )

				def 	a__		(     self							,      a_							,      a_							,      a_							,      a_							)    ->  Optional[int]:
					lowercase						:		Union[str, Any]   					=		model_class(a_							)
					model.to(a_							)
					model.eval()

					with torch.no_grad():
						lowercase						:		List[str]   					=		model(**self._prepare_for_class(a_							,      a_							)							)

					lowercase						:		Optional[Any]   					=		outputs.hidden_states

					lowercase						:		Tuple   					=		getattr(
					    self.model_tester							,      "expected_num_hidden_layers"							,      len(self.model_tester.depths							) + 1							)
					self.assertEqual(len(a_							)							,      a_							)

					# Swinv2 has a different seq_length
					lowercase						:		Optional[Any]   					=		(
					    config.patch_size
					    if isinstance(config.patch_size							,      collections.abc.Iterable							)
					    else (config.patch_size, config.patch_size)
					)

					lowercase						:		int   					=		(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]							,      )

					lowercase						:		Tuple   					=		outputs.reshaped_hidden_states
					self.assertEqual(len(a_							)							,      a_							)

					lowercase					,		lowercase					,		lowercase					,		lowercase						:		Any   					=		reshaped_hidden_states[0].shape
					lowercase						:		Tuple   					=		(
					    reshaped_hidden_states[0].view(a_							,      a_							,      height * width							).permute(0							,      2							,      1							)
					)
					self.assertListEqual(
					    list(reshaped_hidden_states.shape[-2:]							)							,      [num_patches, self.model_tester.embed_dim]							,      )

				def 	a__		(     self							)    ->  str:
					lowercase					,		lowercase						:		int   					=		self.model_tester.prepare_config_and_inputs_for_common()

					lowercase						:		Optional[Any]   					=		(
					    self.model_tester.image_size
					    if isinstance(self.model_tester.image_size							,      collections.abc.Iterable							)
					    else (self.model_tester.image_size, self.model_tester.image_size)
					)

					for model_class in self.all_model_classes:
						lowercase						:		int   					=		True
						self.check_hidden_states_output(a_							,      a_							,      a_							,      a_							)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase						:		str   					=		True

						self.check_hidden_states_output(a_							,      a_							,      a_							,      a_							)

				def 	a__		(     self							)    ->  Any:
					lowercase					,		lowercase						:		int   					=		self.model_tester.prepare_config_and_inputs_for_common()
					lowercase						:		int   					=		3

					lowercase						:		List[str]   					=		(
					    self.model_tester.image_size
					    if isinstance(self.model_tester.image_size							,      collections.abc.Iterable							)
					    else (self.model_tester.image_size, self.model_tester.image_size)
					)
					lowercase						:		List[str]   					=		(
					    config.patch_size
					    if isinstance(config.patch_size							,      collections.abc.Iterable							)
					    else (config.patch_size, config.patch_size)
					)

					lowercase						:		Optional[Any]   					=		image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
					lowercase						:		Union[str, Any]   					=		image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

					for model_class in self.all_model_classes:
						lowercase						:		Dict   					=		True
						self.check_hidden_states_output(a_							,      a_							,      a_							,      (padded_height, padded_width)							)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase						:		List[str]   					=		True
						self.check_hidden_states_output(a_							,      a_							,      a_							,      (padded_height, padded_width)							)

				def 	a__		(     self							)    ->  int:
					lowercase						:		List[str]   					=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*a_							)

				def 	a__		(     self							)    ->  Optional[Any]:
					lowercase						:		str   					=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*a_							)

				@slow
				def 	a__		(     self							)    ->  Tuple:
					for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase						:		Tuple   					=		SwinvaModel.from_pretrained(a_							)
						self.assertIsNotNone(a_							)


				def 	a__		(     self							)    ->  List[Any]:
					lowercase					,		lowercase						:		int   					=		self.model_tester.prepare_config_and_inputs_for_common()

					lowercase						:		List[Any]   					=		_config_zero_init(a_							)
					for model_class in self.all_model_classes:
						lowercase						:		int   					=		model_class(config=a_							)
						for name, param in model.named_parameters():
							if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
								self.assertIn(
								    ((param.data.mean() * 1e9).round() / 1e9).item()							,      [0.0, 1.0]							,      msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''							,      )




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


				'''simple docstring'''


				@cached_property
				def 	a__		(     self							)    ->  Any:
					return (
					    AutoImageProcessor.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"							)
					    if is_vision_available()
					    else None
					)


				@slow
				def 	a__		(     self							)    ->  Optional[int]:
					lowercase						:		List[str]   					=		SwinvaForImageClassification.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"							).to(
					    a_							)
					lowercase						:		str   					=		self.default_image_processor

					lowercase						:		Tuple   					=		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
					lowercase						:		List[Any]   					=		image_processor(images=a_							,      return_tensors="pt"							).to(a_							)

					# forward pass
					with torch.no_grad():
						lowercase						:		List[Any]   					=		model(**a_							)

					# verify the logits
					lowercase						:		int   					=		torch.Size((1, 1_0_0_0)							)
					self.assertEqual(outputs.logits.shape							,      a_							)
					lowercase						:		Dict   					=		torch.tensor([-0.39_47, -0.43_06, 0.00_26]							).to(a_							)
					self.assertTrue(torch.allclose(outputs.logits[0, :3]							,      a_							,      atol=1e-4							)							)






'''simple docstring'''
from ...utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_torch_available,
    is_transformers_available,
)


try:
					if not (is_transformers_available() and is_torch_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
					from .multicontrolnet import MultiControlNetModel
					from .pipeline_controlnet import StableDiffusionControlNetPipeline
					from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline
					from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline


if is_transformers_available() and is_flax_available():
					from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline


