





"""simple docstring"""




import inspect
import unittest

from transformers import SwinaSRConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import 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 SwinaSRForImageSuperResolution, SwinaSRModel
							from transformers.models.swinasr.modeling_swinasr import SWIN2SR_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
							from PIL import Image

							from transformers import SwinaSRImageProcessor




class 				_a		:






				def __init__(				self	:					Union[str, Any],				lowerCAmelCase__	:					str,				lowerCAmelCase__	:					Optional[Any]=1_3,				lowerCAmelCase__	:					Optional[int]=3_2,				lowerCAmelCase__	:					Tuple=1,				lowerCAmelCase__	:					Any=3,				lowerCAmelCase__	:					str=1_6,				lowerCAmelCase__	:					Tuple=[1, 2, 1],				lowerCAmelCase__	:					List[str]=[2, 2, 4],				lowerCAmelCase__	:					int=2,				lowerCAmelCase__	:					Dict=2.0,				lowerCAmelCase__	:					Any=True,				lowerCAmelCase__	:					Any=0.0,				lowerCAmelCase__	:					str=0.0,				lowerCAmelCase__	:					str=0.1,				lowerCAmelCase__	:					Dict="gelu",				lowerCAmelCase__	:					Optional[Any]=False,				lowerCAmelCase__	:					Any=True,				lowerCAmelCase__	:					Dict=0.02,				lowerCAmelCase__	:					Any=1e-5,				lowerCAmelCase__	:					Tuple=True,				lowerCAmelCase__	:					Optional[Any]=None,				lowerCAmelCase__	:					Tuple=False,				lowerCAmelCase__	:					Union[str, Any]=2,				)			->   Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase    :      Any						     =					parent
										_UpperCamelCase    :      Tuple						     =					batch_size
										_UpperCamelCase    :      Dict						     =					image_size
										_UpperCamelCase    :      int						     =					patch_size
										_UpperCamelCase    :      Any						     =					num_channels
										_UpperCamelCase    :      List[str]						     =					embed_dim
										_UpperCamelCase    :      Union[str, Any]						     =					depths
										_UpperCamelCase    :      int						     =					num_heads
										_UpperCamelCase    :      List[str]						     =					window_size
										_UpperCamelCase    :      int						     =					mlp_ratio
										_UpperCamelCase    :      Union[str, Any]						     =					qkv_bias
										_UpperCamelCase    :      Dict						     =					hidden_dropout_prob
										_UpperCamelCase    :      List[Any]						     =					attention_probs_dropout_prob
										_UpperCamelCase    :      Optional[Any]						     =					drop_path_rate
										_UpperCamelCase    :      Dict						     =					hidden_act
										_UpperCamelCase    :      Tuple						     =					use_absolute_embeddings
										_UpperCamelCase    :      str						     =					patch_norm
										_UpperCamelCase    :      int						     =					layer_norm_eps
										_UpperCamelCase    :      Union[str, Any]						     =					initializer_range
										_UpperCamelCase    :      Optional[Any]						     =					is_training
										_UpperCamelCase    :      Union[str, Any]						     =					scope
										_UpperCamelCase    :      Dict						     =					use_labels
										_UpperCamelCase    :      Optional[Any]						     =					upscale

										# here we set some attributes to make tests pass
										_UpperCamelCase    :      List[str]						     =					len(lowerCamelCase__							)
										_UpperCamelCase    :      List[str]						     =					embed_dim
										_UpperCamelCase    :      Optional[int]						     =					(image_size // patch_size) ** 2






				def        snake_case			(				self	:					Optional[int]							)			->   Optional[int]:




										'''simple docstring'''



										_UpperCamelCase    :      str						     =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

										_UpperCamelCase    :      Optional[int]						     =					None
										if self.use_labels:
																_UpperCamelCase    :      Union[str, Any]						     =					ids_tensor([self.batch_size],				self.type_sequence_label_size							)

										_UpperCamelCase    :      Union[str, Any]						     =					self.get_config()

										return config, pixel_values, labels






				def        snake_case			(				self	:					int							)			->   List[Any]:




										'''simple docstring'''



										return SwinaSRConfig(
										    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,				upscale=self.upscale,				)






				def        snake_case			(				self	:					Optional[Any],				lowerCAmelCase__	:					Dict,				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					Tuple							)			->   int:




										'''simple docstring'''



										_UpperCamelCase    :      List[Any]						     =					SwinaSRModel(config=lowerCamelCase__							)
										model.to(lowerCamelCase__							)
										model.eval()
										_UpperCamelCase    :      Tuple						     =					model(lowerCamelCase__							)

										self.parent.assertEqual(
										    result.last_hidden_state.shape,				(self.batch_size, self.embed_dim, self.image_size, self.image_size)							)






				def        snake_case			(				self	:					List[Any],				lowerCAmelCase__	:					Any,				lowerCAmelCase__	:					Any,				lowerCAmelCase__	:					List[str]							)			->   Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase    :      List[Any]						     =					SwinaSRForImageSuperResolution(lowerCamelCase__							)
										model.to(lowerCamelCase__							)
										model.eval()
										_UpperCamelCase    :      str						     =					model(lowerCamelCase__							)

										_UpperCamelCase    :      List[str]						     =					self.image_size * self.upscale
										self.parent.assertEqual(
										    result.reconstruction.shape,				(self.batch_size, self.num_channels, expected_image_size, expected_image_size)							)






				def        snake_case			(				self	:					Any							)			->   Optional[int]:




										'''simple docstring'''



										_UpperCamelCase    :      str						     =					self.prepare_config_and_inputs()
										_UpperCamelCase    :      Optional[Any]						     =					config_and_inputs
										_UpperCamelCase    :      Union[str, Any]						     =					{"pixel_values": pixel_values}
										return config, inputs_dict




@require_torch
class 				_a		(   UpperCAmelCase__			,       UpperCAmelCase__			,       unittest.TestCase       ):
				UpperCamelCase			=					(SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
				UpperCamelCase			=					{'feature-extraction': SwinaSRModel} if is_torch_available() else {}

				UpperCamelCase			=					False
				UpperCamelCase			=					False
				UpperCamelCase			=					False
				UpperCamelCase			=					False
				UpperCamelCase			=					False






				def        snake_case			(				self	:					Any							)			->   Dict:




										'''simple docstring'''



										_UpperCamelCase    :      List[Any]						     =					SwinaSRModelTester(self							)
										_UpperCamelCase    :      Tuple						     =					ConfigTester(self,				config_class=lowerCamelCase__,				embed_dim=3_7							)






				def        snake_case			(				self	:					List[Any]							)			->   Tuple:




										'''simple docstring'''



										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        snake_case			(				self	:					Optional[Any]							)			->   Optional[int]:




										'''simple docstring'''



										_UpperCamelCase    :      List[str]						     =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCamelCase__							)






				def        snake_case			(				self	:					str							)			->   Tuple:




										'''simple docstring'''



										_UpperCamelCase    :      Optional[Any]						     =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_super_resolution(*lowerCamelCase__							)






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




										'''simple docstring'''



										pass






				@unittest.skip(reason='''Swin2SR does not use inputs_embeds'''							)
				def        snake_case			(				self	:					List[str]							)			->   Optional[Any]:




										'''simple docstring'''



										pass






				@unittest.skip(reason='''Swin2SR does not support training yet'''							)
				def        snake_case			(				self	:					Optional[int]							)			->   Dict:




										'''simple docstring'''



										pass






				@unittest.skip(reason='''Swin2SR does not support training yet'''							)
				def        snake_case			(				self	:					Optional[Any]							)			->   List[str]:




										'''simple docstring'''



										pass






				def        snake_case			(				self	:					Dict							)			->   int:




										'''simple docstring'''



										_UpperCamelCase    :      Union[str, Any]						     =					self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																_UpperCamelCase    :      Optional[Any]						     =					model_class(lowerCamelCase__							)
																self.assertIsInstance(model.get_input_embeddings(),				(nn.Module)							)
																_UpperCamelCase    :      List[Any]						     =					model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(lowerCamelCase__,				nn.Linear							)							)






				def        snake_case			(				self	:					List[Any]							)			->   Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase    :      Any						     =					self.model_tester.prepare_config_and_inputs_for_common()

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

																_UpperCamelCase    :      Optional[int]						     =					["pixel_values"]
																self.assertListEqual(arg_names[:1],				lowerCamelCase__							)






				@slow
				def        snake_case			(				self	:					List[Any]							)			->   Any:




										'''simple docstring'''



										for model_name in SWIN2SR_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCamelCase    :      str						     =					SwinaSRModel.from_pretrained(lowerCamelCase__							)
																self.assertIsNotNone(lowerCamelCase__							)






				def        snake_case			(				self	:					Optional[int]							)			->   Any:




										'''simple docstring'''



										_UpperCamelCase    :      List[str]						     =					self.model_tester.prepare_config_and_inputs_for_common()

										_UpperCamelCase    :      List[Any]						     =					_config_zero_init(lowerCamelCase__							)
										for model_class in self.all_model_classes:
																_UpperCamelCase    :      List[str]						     =					model_class(config=lowerCamelCase__							)
																for name, param in model.named_parameters():
																						if "logit_scale" in name:
																												continue
																						if 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""",				)






				def        snake_case			(				self	:					int							)			->   Optional[int]:




										'''simple docstring'''



										_UpperCamelCase    :      List[Any]						     =					self.model_tester.prepare_config_and_inputs_for_common()
										_UpperCamelCase    :      Tuple						     =					True

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

																# check that output_attentions also work using config
																del inputs_dict["output_attentions"]
																_UpperCamelCase    :      Dict						     =					True
																_UpperCamelCase    :      Tuple						     =					config.window_size**2
																_UpperCamelCase    :      List[Any]						     =					model_class(lowerCamelCase__							)
																model.to(lowerCamelCase__							)
																model.eval()
																with torch.no_grad():
																						_UpperCamelCase    :      Union[str, Any]						     =					model(**self._prepare_for_class(lowerCamelCase__,				lowerCamelCase__							)							)
																_UpperCamelCase    :      int						     =					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],				)
																_UpperCamelCase    :      Optional[Any]						     =					len(lowerCamelCase__							)

																# Check attention is always last and order is fine
																_UpperCamelCase    :      Tuple						     =					True
																_UpperCamelCase    :      Optional[Any]						     =					True
																_UpperCamelCase    :      Union[str, Any]						     =					model_class(lowerCamelCase__							)
																model.to(lowerCamelCase__							)
																model.eval()
																with torch.no_grad():
																						_UpperCamelCase    :      List[Any]						     =					model(**self._prepare_for_class(lowerCamelCase__,				lowerCamelCase__							)							)

																self.assertEqual(out_len + 1,				len(lowerCamelCase__							)							)

																_UpperCamelCase    :      Optional[Any]						     =					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],				)




@require_vision
@require_torch
@slow
class 				_a		(   unittest.TestCase       ):






				def        snake_case			(				self	:					Any							)			->   Any:




										'''simple docstring'''



										_UpperCamelCase    :      Any						     =					SwinaSRImageProcessor()
										_UpperCamelCase    :      Union[str, Any]						     =					SwinaSRForImageSuperResolution.from_pretrained('''caidas/swin2SR-classical-sr-x2-64'''							).to(lowerCamelCase__							)

										_UpperCamelCase    :      List[Any]						     =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
										_UpperCamelCase    :      Union[str, Any]						     =					processor(images=lowerCamelCase__,				return_tensors='''pt'''							).to(lowerCamelCase__							)

										# forward pass
										with torch.no_grad():
																_UpperCamelCase    :      int						     =					model(**lowerCamelCase__							)

										# verify the logits
										_UpperCamelCase    :      str						     =					torch.Size([1, 3, 9_7_6, 1_2_9_6]							)
										self.assertEqual(outputs.reconstruction.shape,				lowerCamelCase__							)
										_UpperCamelCase    :      List[Any]						     =					torch.tensor(
										    [[0.5_458, 0.5_546, 0.5_638], [0.5_526, 0.5_565, 0.5_651], [0.5_396, 0.5_426, 0.5_621]]							).to(lowerCamelCase__							)
										self.assertTrue(torch.allclose(outputs.reconstruction[0, 0, :3, :3],				lowerCamelCase__,				atol=1e-4							)							)










"""simple docstring"""




def  a_       (							_lowercase     ):
						return 1 if input_a == 0 else 0





def  a_       (							):
						assert not_gate(0     ) == 1
						assert not_gate(1     ) == 0


if __name__ == "__main__":
						print(not_gate(0))
						print(not_gate(1))



