



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      :Dict  ,		_lowercase      :List[Any]  ,		_lowercase      :List[str]=13  ,		_lowercase      :Optional[Any]=32  ,		_lowercase      :str=1  ,		_lowercase      :Dict=3  ,		_lowercase      :List[Any]=16  ,		_lowercase      :Optional[Any]=[1, 2, 1]  ,		_lowercase      :Any=[2, 2, 4]  ,		_lowercase      :Any=2  ,		_lowercase      :Dict=2.0  ,		_lowercase      :str=True  ,		_lowercase      :Optional[Any]=0.0  ,		_lowercase      :Tuple=0.0  ,		_lowercase      :List[str]=0.1  ,		_lowercase      :Any="gelu"  ,		_lowercase      :Optional[Any]=False  ,		_lowercase      :Optional[int]=True  ,		_lowercase      :str=0.02  ,		_lowercase      :Union[str, Any]=1E-5  ,		_lowercase      :str=True  ,		_lowercase      :Optional[int]=None  ,		_lowercase      :Tuple=False  ,		_lowercase      :List[str]=2  ,		)						->   Optional[Any]:
												UpperCAmelCase_					=	parent
												UpperCAmelCase_					=	batch_size
												UpperCAmelCase_					=	image_size
												UpperCAmelCase_					=	patch_size
												UpperCAmelCase_					=	num_channels
												UpperCAmelCase_					=	embed_dim
												UpperCAmelCase_					=	depths
												UpperCAmelCase_					=	num_heads
												UpperCAmelCase_					=	window_size
												UpperCAmelCase_					=	mlp_ratio
												UpperCAmelCase_					=	qkv_bias
												UpperCAmelCase_					=	hidden_dropout_prob
												UpperCAmelCase_					=	attention_probs_dropout_prob
												UpperCAmelCase_					=	drop_path_rate
												UpperCAmelCase_					=	hidden_act
												UpperCAmelCase_					=	use_absolute_embeddings
												UpperCAmelCase_					=	patch_norm
												UpperCAmelCase_					=	layer_norm_eps
												UpperCAmelCase_					=	initializer_range
												UpperCAmelCase_					=	is_training
												UpperCAmelCase_					=	scope
												UpperCAmelCase_					=	use_labels
												UpperCAmelCase_					=	upscale

												# here we set some attributes to make tests pass
												UpperCAmelCase_					=	len(_lowercase)
												UpperCAmelCase_					=	embed_dim
												UpperCAmelCase_					=	(image_size // patch_size) ** 2

						def       __a			(       self      :List[Any])						->   List[str]:
												UpperCAmelCase_					=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

												UpperCAmelCase_					=	None
												if self.use_labels:
																		UpperCAmelCase_					=	ids_tensor([self.batch_size]  ,		self.type_sequence_label_size)

												UpperCAmelCase_					=	self.get_config()

												return config, pixel_values, labels

						def       __a			(       self      :int)						->   Optional[int]:
												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       __a			(       self      :List[str]  ,		_lowercase      :Any  ,		_lowercase      :List[str]  ,		_lowercase      :Union[str, Any])						->   Any:
												UpperCAmelCase_					=	SwinaSRModel(config=_lowercase)
												model.to(_lowercase)
												model.eval()
												UpperCAmelCase_					=	model(_lowercase)

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

						def       __a			(       self      :int  ,		_lowercase      :Union[str, Any]  ,		_lowercase      :Optional[Any]  ,		_lowercase      :List[str])						->   Tuple:
												UpperCAmelCase_					=	SwinaSRForImageSuperResolution(_lowercase)
												model.to(_lowercase)
												model.eval()
												UpperCAmelCase_					=	model(_lowercase)

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




						def       __a			(       self      :List[str])						->   Union[str, Any]:
												UpperCAmelCase_					=	self.prepare_config_and_inputs()
												UpperCAmelCase_				, UpperCAmelCase_				, UpperCAmelCase_					=	config_and_inputs
												UpperCAmelCase_					=	{'''pixel_values''': pixel_values}
												return config, inputs_dict







@require_torch
class 				a_    (						_snake_case   ,					_snake_case   ,					unittest.TestCase							):
						UpperCamelCase__ :		Dict										=(SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
						UpperCamelCase__ :		Tuple										={"feature-extraction": SwinaSRModel} if is_torch_available() else {}

						UpperCamelCase__ :		int										=False
						UpperCamelCase__ :		List[str]										=False
						UpperCamelCase__ :		str										=False
						UpperCamelCase__ :		List[str]										=False
						UpperCamelCase__ :		Tuple										=False

						def       __a			(       self      :str)						->   str:
												UpperCAmelCase_					=	SwinaSRModelTester(self)
												UpperCAmelCase_					=	ConfigTester(self  ,		config_class=_lowercase  ,		embed_dim=37)

						def       __a			(       self      :int)						->   Tuple:
												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      :Dict)						->   Optional[int]:
												UpperCAmelCase_					=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_lowercase)

						def       __a			(       self      :Any)						->   Optional[int]:
												UpperCAmelCase_					=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_super_resolution(*_lowercase)

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

						@unittest.skip(reason='''Swin2SR does not use inputs_embeds''')
						def       __a			(       self      :str)						->   str:
												pass

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

						@unittest.skip(reason='''Swin2SR does not support training yet''')
						def       __a			(       self      :Union[str, Any])						->   str:
												pass

						def       __a			(       self      :List[Any])						->   Any:
												UpperCAmelCase_				, UpperCAmelCase_					=	self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		UpperCAmelCase_					=	model_class(_lowercase)
																		self.assertIsInstance(model.get_input_embeddings()  ,		(nn.Module))
																		UpperCAmelCase_					=	model.get_output_embeddings()
																		self.assertTrue(x is None or isinstance(_lowercase  ,		nn.Linear))

						def       __a			(       self      :str)						->   Union[str, Any]:
												UpperCAmelCase_				, UpperCAmelCase_					=	self.model_tester.prepare_config_and_inputs_for_common()

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

																		UpperCAmelCase_					=	['''pixel_values''']
																		self.assertListEqual(arg_names[:1]  ,		_lowercase)

						@slow
						def       __a			(       self      :Any)						->   int:
												for model_name in SWIN2SR_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		UpperCAmelCase_					=	SwinaSRModel.from_pretrained(_lowercase)
																		self.assertIsNotNone(_lowercase)

						def       __a			(       self      :Dict)						->   Tuple:
												UpperCAmelCase_				, UpperCAmelCase_					=	self.model_tester.prepare_config_and_inputs_for_common()

												UpperCAmelCase_					=	_config_zero_init(_lowercase)
												for model_class in self.all_model_classes:
																		UpperCAmelCase_					=	model_class(config=_lowercase)
																		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       __a			(       self      :int)						->   List[str]:
												UpperCAmelCase_				, UpperCAmelCase_					=	self.model_tester.prepare_config_and_inputs_for_common()
												UpperCAmelCase_					=	True

												for model_class in self.all_model_classes:
																		UpperCAmelCase_					=	True
																		UpperCAmelCase_					=	False
																		UpperCAmelCase_					=	True
																		UpperCAmelCase_					=	model_class(_lowercase)
																		model.to(_lowercase)
																		model.eval()
																		with torch.no_grad():
																								UpperCAmelCase_					=	model(**self._prepare_for_class(_lowercase  ,		_lowercase))
																		UpperCAmelCase_					=	outputs.attentions
																		UpperCAmelCase_					=	len(self.model_tester.depths)
																		self.assertEqual(len(_lowercase)  ,		_lowercase)

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

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

																		# Check attention is always last and order is fine
																		UpperCAmelCase_					=	True
																		UpperCAmelCase_					=	True
																		UpperCAmelCase_					=	model_class(_lowercase)
																		model.to(_lowercase)
																		model.eval()
																		with torch.no_grad():
																								UpperCAmelCase_					=	model(**self._prepare_for_class(_lowercase  ,		_lowercase))

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

																		UpperCAmelCase_					=	outputs.attentions

																		self.assertEqual(len(_lowercase)  ,		_lowercase)

																		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       __a			(       self      :Optional[int])						->   List[str]:
												UpperCAmelCase_					=	SwinaSRImageProcessor()
												UpperCAmelCase_					=	SwinaSRForImageSuperResolution.from_pretrained('''caidas/swin2SR-classical-sr-x2-64''').to(_lowercase)

												UpperCAmelCase_					=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
												UpperCAmelCase_					=	processor(images=_lowercase  ,		return_tensors='''pt''').to(_lowercase)

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

												# verify the logits
												UpperCAmelCase_					=	torch.Size([1, 3, 976, 1296])
												self.assertEqual(outputs.reconstruction.shape  ,		_lowercase)
												UpperCAmelCase_					=	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(_lowercase)
												self.assertTrue(torch.allclose(outputs.reconstruction[0, 0, :3, :3]  ,		_lowercase  ,		atol=1E-4))










import math







def 							A     (						__UpperCAmelCase      )			->			str:



						'''simple docstring'''






						if number == 0:
												UpperCAmelCase_					=	'''Zero'''
												return words
						else:
												UpperCAmelCase_					=	math.logaa(__UpperCAmelCase      )
												UpperCAmelCase_					=	digits + 1
												UpperCAmelCase_					=	{}
												UpperCAmelCase_					=	''''''
												UpperCAmelCase_					=	'''One'''
												UpperCAmelCase_					=	'''Two'''
												UpperCAmelCase_					=	'''Three'''
												UpperCAmelCase_					=	'''Four'''
												UpperCAmelCase_					=	'''Five'''
												UpperCAmelCase_					=	'''Six'''
												UpperCAmelCase_					=	'''Seven'''
												UpperCAmelCase_					=	'''Eight'''
												UpperCAmelCase_					=	'''Nine'''

												UpperCAmelCase_					=	{}
												UpperCAmelCase_					=	''''''
												UpperCAmelCase_					=	'''Twenty'''
												UpperCAmelCase_					=	'''Thirty'''
												UpperCAmelCase_					=	'''Forty'''
												UpperCAmelCase_					=	'''Fifty'''
												UpperCAmelCase_					=	'''Sixty'''
												UpperCAmelCase_					=	'''Seventy'''
												UpperCAmelCase_					=	'''Eighty'''
												UpperCAmelCase_					=	'''Ninety'''

												UpperCAmelCase_					=	{}
												UpperCAmelCase_					=	'''Ten'''
												UpperCAmelCase_					=	'''Eleven'''
												UpperCAmelCase_					=	'''Twelve'''
												UpperCAmelCase_					=	'''Thirteen'''
												UpperCAmelCase_					=	'''Fourteen'''
												UpperCAmelCase_					=	'''Fifteen'''
												UpperCAmelCase_					=	'''Sixteen'''
												UpperCAmelCase_					=	'''Seventeen'''
												UpperCAmelCase_					=	'''Eighteen'''
												UpperCAmelCase_					=	'''Nineteen'''

												UpperCAmelCase_					=	{}
												UpperCAmelCase_					=	'''Hundred,'''
												UpperCAmelCase_					=	'''Thousand,'''
												UpperCAmelCase_					=	'''Lakh,'''
												UpperCAmelCase_					=	'''Crore,'''

												UpperCAmelCase_					=	number
												UpperCAmelCase_					=	''''''
												UpperCAmelCase_					=	0
												UpperCAmelCase_					=	int(__UpperCAmelCase      )
												while counter < digits:
																		UpperCAmelCase_					=	temp_num % 10
																		if counter % 2 == 0:
																								UpperCAmelCase_					=	''''''
																								if counter in placevalue and current != 0:
																														UpperCAmelCase_					=	placevalue[counter]
																								if counter == 2:
																														UpperCAmelCase_					=	singles[current] + addition + words
																								elif counter == 0:
																														if ((temp_num % 100) // 10) == 1:
																																				UpperCAmelCase_					=	teens[current] + addition + words
																																				UpperCAmelCase_					=	temp_num // 10
																																				counter += 1
																														else:
																																				UpperCAmelCase_					=	singles[current] + addition + words

																								else:
																														UpperCAmelCase_					=	doubles[current] + addition + words

																		else:
																								if counter == 1:
																														if current == 1:
																																				UpperCAmelCase_					=	teens[number % 10] + words
																														else:
																																				UpperCAmelCase_					=	''''''
																																				if counter in placevalue:
																																										UpperCAmelCase_					=	placevalue[counter]
																																				UpperCAmelCase_					=	doubles[current] + addition + words
																								else:
																														UpperCAmelCase_					=	''''''
																														if counter in placevalue:
																																				if current == 0 and ((temp_num % 100) // 10) == 0:
																																										UpperCAmelCase_					=	''''''
																																				else:
																																										UpperCAmelCase_					=	placevalue[counter]
																														if ((temp_num % 100) // 10) == 1:
																																				UpperCAmelCase_					=	teens[current] + addition + words
																																				UpperCAmelCase_					=	temp_num // 10
																																				counter += 1
																														else:
																																				UpperCAmelCase_					=	singles[current] + addition + words
																		counter += 1
																		UpperCAmelCase_					=	temp_num // 10
						return words


if __name__ == "__main__":
							import doctest

							doctest.testmod()





