





'''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 			SCREAMING_SNAKE_CASE__	:







					"""simple docstring"""






					def __init__(					self    :	Tuple						,     UpperCAmelCase_    :	Any						,     UpperCAmelCase_    :	List[Any]=13						,     UpperCAmelCase_    :	Union[str, Any]=32						,     UpperCAmelCase_    :	Optional[Any]=1						,     UpperCAmelCase_    :	Union[str, Any]=3						,     UpperCAmelCase_    :	List[str]=16						,     UpperCAmelCase_    :	Optional[Any]=[1, 2, 1]						,     UpperCAmelCase_    :	Any=[2, 2, 4]						,     UpperCAmelCase_    :	int=2						,     UpperCAmelCase_    :	List[str]=2.0						,     UpperCAmelCase_    :	Dict=True						,     UpperCAmelCase_    :	Union[str, Any]=0.0						,     UpperCAmelCase_    :	Union[str, Any]=0.0						,     UpperCAmelCase_    :	Optional[int]=0.1						,     UpperCAmelCase_    :	Optional[int]="gelu"						,     UpperCAmelCase_    :	Any=False						,     UpperCAmelCase_    :	Dict=True						,     UpperCAmelCase_    :	int=0.02						,     UpperCAmelCase_    :	Tuple=1e-5						,     UpperCAmelCase_    :	List[str]=True						,     UpperCAmelCase_    :	List[str]=None						,     UpperCAmelCase_    :	str=False						,     UpperCAmelCase_    :	Any=2						,     ):







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

							# here we set some attributes to make tests pass
							__UpperCAmelCase     :				str					   =			len(UpperCAmelCase_						)
							__UpperCAmelCase     :				List[Any]					   =			embed_dim
							__UpperCAmelCase     :				List[Any]					   =			(image_size // patch_size) ** 2
					def 						lowerCamelCase_					(					self    :	str						):







							"""simple docstring"""
							__UpperCAmelCase     :				List[Any]					   =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

							__UpperCAmelCase     :				List[str]					   =			None
							if self.use_labels:
									__UpperCAmelCase     :				Tuple					   =			ids_tensor([self.batch_size]						,     self.type_sequence_label_size						)

							__UpperCAmelCase     :				Any					   =			self.get_config()

							return config, pixel_values, labels
					def 						lowerCamelCase_					(					self    :	str						):







							"""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 						lowerCamelCase_					(					self    :	Union[str, Any]						,     UpperCAmelCase_    :	Dict						,     UpperCAmelCase_    :	Union[str, Any]						,     UpperCAmelCase_    :	List[str]						):







							"""simple docstring"""
							__UpperCAmelCase     :				List[Any]					   =			SwinaSRModel(config=UpperCAmelCase_						)
							model.to(UpperCAmelCase_						)
							model.eval()
							__UpperCAmelCase     :				Optional[int]					   =			model(UpperCAmelCase_						)

							self.parent.assertEqual(
							    result.last_hidden_state.shape						,     (self.batch_size, self.embed_dim, self.image_size, self.image_size)						)
					def 						lowerCamelCase_					(					self    :	List[str]						,     UpperCAmelCase_    :	Tuple						,     UpperCAmelCase_    :	str						,     UpperCAmelCase_    :	Tuple						):







							"""simple docstring"""
							__UpperCAmelCase     :				Optional[Any]					   =			SwinaSRForImageSuperResolution(UpperCAmelCase_						)
							model.to(UpperCAmelCase_						)
							model.eval()
							__UpperCAmelCase     :				List[str]					   =			model(UpperCAmelCase_						)

							__UpperCAmelCase     :				Any					   =			self.image_size * self.upscale
							self.parent.assertEqual(
							    result.reconstruction.shape						,     (self.batch_size, self.num_channels, expected_image_size, expected_image_size)						)






					def 						lowerCamelCase_					(					self    :	Union[str, Any]						):







							"""simple docstring"""
							__UpperCAmelCase     :				Tuple					   =			self.prepare_config_and_inputs()
							__UpperCAmelCase     :				Union[str, Any]					   =			config_and_inputs
							__UpperCAmelCase     :				List[str]					   =			{'pixel_values': pixel_values}
							return config, inputs_dict






@require_torch
class 			SCREAMING_SNAKE_CASE__	(					snake_case__				,snake_case__				,unittest.TestCase						):







					"""simple docstring"""






					SCREAMING_SNAKE_CASE				=     (SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
					SCREAMING_SNAKE_CASE				=     {'''feature-extraction''': SwinaSRModel} if is_torch_available() else {}

					SCREAMING_SNAKE_CASE				=     False
					SCREAMING_SNAKE_CASE				=     False
					SCREAMING_SNAKE_CASE				=     False
					SCREAMING_SNAKE_CASE				=     False
					SCREAMING_SNAKE_CASE				=     False
					def 						lowerCamelCase_					(					self    :	Optional[int]						):







							"""simple docstring"""
							__UpperCAmelCase     :				List[str]					   =			SwinaSRModelTester(self						)
							__UpperCAmelCase     :				int					   =			ConfigTester(self						,     config_class=UpperCAmelCase_						,     embed_dim=37						)
					def 						lowerCamelCase_					(					self    :	Optional[int]						):







							"""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 						lowerCamelCase_					(					self    :	List[Any]						):







							"""simple docstring"""
							__UpperCAmelCase     :				str					   =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCAmelCase_						)
					def 						lowerCamelCase_					(					self    :	Tuple						):







							"""simple docstring"""
							__UpperCAmelCase     :				Any					   =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_super_resolution(*UpperCAmelCase_						)
					@unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0."						)
					def 						lowerCamelCase_					(					self    :	str						):







							"""simple docstring"""
							pass
					@unittest.skip(reason="Swin2SR does not use inputs_embeds"						)
					def 						lowerCamelCase_					(					self    :	Tuple						):







							"""simple docstring"""
							pass
					@unittest.skip(reason="Swin2SR does not support training yet"						)
					def 						lowerCamelCase_					(					self    :	Optional[int]						):







							"""simple docstring"""
							pass
					@unittest.skip(reason="Swin2SR does not support training yet"						)
					def 						lowerCamelCase_					(					self    :	int						):







							"""simple docstring"""
							pass
					def 						lowerCamelCase_					(					self    :	int						):







							"""simple docstring"""
							__UpperCAmelCase     :				Optional[int]					   =			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									__UpperCAmelCase     :				Union[str, Any]					   =			model_class(UpperCAmelCase_						)
									self.assertIsInstance(model.get_input_embeddings()						,     (nn.Module)						)
									__UpperCAmelCase     :				Optional[int]					   =			model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(UpperCAmelCase_						,     nn.Linear						)						)
					def 						lowerCamelCase_					(					self    :	Optional[int]						):







							"""simple docstring"""
							__UpperCAmelCase     :				int					   =			self.model_tester.prepare_config_and_inputs_for_common()

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

									__UpperCAmelCase     :				Tuple					   =			['pixel_values']
									self.assertListEqual(arg_names[:1]						,     UpperCAmelCase_						)
					@slow
					def 						lowerCamelCase_					(					self    :	List[str]						):







							"""simple docstring"""




							for model_name in SWIN2SR_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase     :				Optional[int]					   =			SwinaSRModel.from_pretrained(UpperCAmelCase_						)
									self.assertIsNotNone(UpperCAmelCase_						)
					def 						lowerCamelCase_					(					self    :	Dict						):







							"""simple docstring"""
							__UpperCAmelCase     :				str					   =			self.model_tester.prepare_config_and_inputs_for_common()

							__UpperCAmelCase     :				int					   =			_config_zero_init(UpperCAmelCase_						)
							for model_class in self.all_model_classes:
									__UpperCAmelCase     :				List[str]					   =			model_class(config=UpperCAmelCase_						)
									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 						lowerCamelCase_					(					self    :	List[str]						):







							"""simple docstring"""
							__UpperCAmelCase     :				Union[str, Any]					   =			self.model_tester.prepare_config_and_inputs_for_common()
							__UpperCAmelCase     :				List[str]					   =			True

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

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

									self.assertListEqual(
									    list(attentions[0].shape[-3:]						)						,     [self.model_tester.num_heads[0], window_size_squared, window_size_squared]						,     )
									__UpperCAmelCase     :				int					   =			len(UpperCAmelCase_						)

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

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

									__UpperCAmelCase     :				Tuple					   =			outputs.attentions

									self.assertEqual(len(UpperCAmelCase_						)						,     UpperCAmelCase_						)

									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 			SCREAMING_SNAKE_CASE__	(					unittest.TestCase						):







					"""simple docstring"""






					def 						lowerCamelCase_					(					self    :	Optional[int]						):







							"""simple docstring"""
							__UpperCAmelCase     :				Optional[int]					   =			SwinaSRImageProcessor()
							__UpperCAmelCase     :				Optional[int]					   =			SwinaSRForImageSuperResolution.from_pretrained("caidas/swin2SR-classical-sr-x2-64"						).to(UpperCAmelCase_						)

							__UpperCAmelCase     :				str					   =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
							__UpperCAmelCase     :				Union[str, Any]					   =			processor(images=UpperCAmelCase_						,     return_tensors="pt"						).to(UpperCAmelCase_						)

							# forward pass
							with torch.no_grad():
									__UpperCAmelCase     :				Tuple					   =			model(**UpperCAmelCase_						)

							# verify the logits
							__UpperCAmelCase     :				Optional[int]					   =			torch.Size([1, 3, 976, 1_296]						)
							self.assertEqual(outputs.reconstruction.shape						,     UpperCAmelCase_						)
							__UpperCAmelCase     :				Tuple					   =			torch.tensor(
							    [[0.5458, 0.5546, 0.5638], [0.5526, 0.5565, 0.5651], [0.5396, 0.5426, 0.5621]]						).to(UpperCAmelCase_						)
							self.assertTrue(torch.allclose(outputs.reconstruction[0, 0, :3, :3]						,     UpperCAmelCase_						,     atol=1e-4						)						)












"""simple docstring"""


import torch

from diffusers import LMSDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest






class 						snake_case  (  UpperCAmelCase       ):
    __magic_name__         =     (LMSDiscreteScheduler,)
    __magic_name__         =     10





    def 				lowerCamelCase__      (							self	:					Any				,		**A	:					Dict			):


      '''simple docstring'''




      a     :			Optional[Any]				=   {
          'num_train_timesteps': 1_1_0_0,
          'beta_start': 0.00_01,
          'beta_end': 0.02,
          'beta_schedule': 'linear',
      }

      config.update(**A			)
      return config





    def 				lowerCamelCase__      (							self	:					Optional[int]			):


      '''simple docstring'''




      for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]:
        self.check_over_configs(num_train_timesteps=A			)





    def 				lowerCamelCase__      (							self	:					List[str]			):


      '''simple docstring'''




      for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01]				,		[0.00_02, 0.0_02, 0.02]			):
        self.check_over_configs(beta_start=A				,		beta_end=A			)





    def 				lowerCamelCase__      (							self	:					List[Any]			):


      '''simple docstring'''




      for schedule in ["linear", "scaled_linear"]:
        self.check_over_configs(beta_schedule=A			)





    def 				lowerCamelCase__      (							self	:					Any			):


      '''simple docstring'''




      for prediction_type in ["epsilon", "v_prediction"]:
        self.check_over_configs(prediction_type=A			)





    def 				lowerCamelCase__      (							self	:					Dict			):


      '''simple docstring'''




      for t in [0, 5_0_0, 8_0_0]:
        self.check_over_forward(time_step=A			)





    def 				lowerCamelCase__      (							self	:					Any			):


      '''simple docstring'''




      a     :			Any				=   self.scheduler_classes[0]
      a     :			Dict				=   self.get_scheduler_config()
      a     :			str				=   scheduler_class(**A			)

      scheduler.set_timesteps(self.num_inference_steps			)

      a     :			List[Any]				=   self.dummy_model()
      a     :			str				=   self.dummy_sample_deter * scheduler.init_noise_sigma

      for i, t in enumerate(scheduler.timesteps			):
        a     :			int				=   scheduler.scale_model_input(A				,		A			)

        a     :			int				=   model(A				,		A			)

        a     :			Optional[int]				=   scheduler.step(A				,		A				,		A			)
        a     :			Union[str, Any]				=   output.prev_sample

      a     :			Any				=   torch.sum(torch.abs(A			)			)
      a     :			List[str]				=   torch.mean(torch.abs(A			)			)

      assert abs(result_sum.item() - 10_06.3_88			) < 1E-2
      assert abs(result_mean.item() - 1.31			) < 1E-3





    def 				lowerCamelCase__      (							self	:					Optional[int]			):


      '''simple docstring'''




      a     :			List[str]				=   self.scheduler_classes[0]
      a     :			Any				=   self.get_scheduler_config(prediction_type='v_prediction'			)
      a     :			str				=   scheduler_class(**A			)

      scheduler.set_timesteps(self.num_inference_steps			)

      a     :			Tuple				=   self.dummy_model()
      a     :			Tuple				=   self.dummy_sample_deter * scheduler.init_noise_sigma

      for i, t in enumerate(scheduler.timesteps			):
        a     :			Any				=   scheduler.scale_model_input(A				,		A			)

        a     :			List[str]				=   model(A				,		A			)

        a     :			Optional[int]				=   scheduler.step(A				,		A				,		A			)
        a     :			Tuple				=   output.prev_sample

      a     :			Optional[Any]				=   torch.sum(torch.abs(A			)			)
      a     :			Optional[int]				=   torch.mean(torch.abs(A			)			)

      assert abs(result_sum.item() - 0.00_17			) < 1E-2
      assert abs(result_mean.item() - 2.2676E-06			) < 1E-3





    def 				lowerCamelCase__      (							self	:					Tuple			):


      '''simple docstring'''




      a     :			Optional[Any]				=   self.scheduler_classes[0]
      a     :			Union[str, Any]				=   self.get_scheduler_config()
      a     :			Union[str, Any]				=   scheduler_class(**A			)

      scheduler.set_timesteps(self.num_inference_steps				,		device=A			)

      a     :			Optional[Any]				=   self.dummy_model()
      a     :			Dict				=   self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
      a     :			Union[str, Any]				=   sample.to(A			)

      for i, t in enumerate(scheduler.timesteps			):
        a     :			Optional[int]				=   scheduler.scale_model_input(A				,		A			)

        a     :			List[str]				=   model(A				,		A			)

        a     :			Optional[Any]				=   scheduler.step(A				,		A				,		A			)
        a     :			str				=   output.prev_sample

      a     :			Dict				=   torch.sum(torch.abs(A			)			)
      a     :			Tuple				=   torch.mean(torch.abs(A			)			)

      assert abs(result_sum.item() - 10_06.3_88			) < 1E-2
      assert abs(result_mean.item() - 1.31			) < 1E-3





    def 				lowerCamelCase__      (							self	:					Dict			):


      '''simple docstring'''




      a     :			Tuple				=   self.scheduler_classes[0]
      a     :			Dict				=   self.get_scheduler_config()
      a     :			Optional[Any]				=   scheduler_class(**A				,		use_karras_sigmas=A			)

      scheduler.set_timesteps(self.num_inference_steps				,		device=A			)

      a     :			Any				=   self.dummy_model()
      a     :			Union[str, Any]				=   self.dummy_sample_deter.to(A			) * scheduler.init_noise_sigma
      a     :			Any				=   sample.to(A			)

      for t in scheduler.timesteps:
        a     :			int				=   scheduler.scale_model_input(A				,		A			)

        a     :			Tuple				=   model(A				,		A			)

        a     :			List[Any]				=   scheduler.step(A				,		A				,		A			)
        a     :			int				=   output.prev_sample

      a     :			Tuple				=   torch.sum(torch.abs(A			)			)
      a     :			Any				=   torch.mean(torch.abs(A			)			)

      assert abs(result_sum.item() - 38_12.99_27			) < 2E-2
      assert abs(result_mean.item() - 4.96_48			) < 1E-3




