

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 					__UpperCamelCase					:







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

						'''simple docstring'''





						__magic_name__				=     parent
						__magic_name__				=     batch_size
						__magic_name__				=     image_size
						__magic_name__				=     patch_size
						__magic_name__				=     num_channels
						__magic_name__				=     embed_dim
						__magic_name__				=     depths
						__magic_name__				=     num_heads
						__magic_name__				=     window_size
						__magic_name__				=     mlp_ratio
						__magic_name__				=     qkv_bias
						__magic_name__				=     hidden_dropout_prob
						__magic_name__				=     attention_probs_dropout_prob
						__magic_name__				=     drop_path_rate
						__magic_name__				=     hidden_act
						__magic_name__				=     use_absolute_embeddings
						__magic_name__				=     patch_norm
						__magic_name__				=     layer_norm_eps
						__magic_name__				=     initializer_range
						__magic_name__				=     is_training
						__magic_name__				=     scope
						__magic_name__				=     use_labels
						__magic_name__				=     upscale

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







	def 						_SCREAMING_SNAKE_CASE				( self:			str   ):

						'''simple docstring'''





						__magic_name__				=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

						__magic_name__				=     self.get_config()

						return config, pixel_values, labels







	def 						_SCREAMING_SNAKE_CASE				( self:			Tuple   ):

						'''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 						_SCREAMING_SNAKE_CASE				( self:			List[str]	,			__UpperCamelCase:			Any	,			__UpperCamelCase:			Dict	,			__UpperCamelCase:			List[str]   ):

						'''simple docstring'''





						__magic_name__				=     SwinaSRModel(config=__UpperCamelCase   )
						model.to(__UpperCamelCase   )
						model.eval()
						__magic_name__				=     model(__UpperCamelCase   )

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







	def 						_SCREAMING_SNAKE_CASE				( self:			Tuple	,			__UpperCamelCase:			Union[str, Any]	,			__UpperCamelCase:			Optional[int]	,			__UpperCamelCase:			List[Any]   ):

						'''simple docstring'''





						__magic_name__				=     SwinaSRForImageSuperResolution(__UpperCamelCase   )
						model.to(__UpperCamelCase   )
						model.eval()
						__magic_name__				=     model(__UpperCamelCase   )

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







	def 						_SCREAMING_SNAKE_CASE				( self:			str   ):

						'''simple docstring'''





						__magic_name__				=     self.prepare_config_and_inputs()
						__magic_name__,						__magic_name__,						__magic_name__				=     config_and_inputs
						__magic_name__				=     {'pixel_values': pixel_values}
						return config, inputs_dict




@require_torch
class 					__UpperCamelCase					(       SCREAMING_SNAKE_CASE	,    SCREAMING_SNAKE_CASE	,    unittest.TestCase      ):
	_lowercase	: Optional[int]												=	(SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
	_lowercase	: Optional[int]												=	{"feature-extraction": SwinaSRModel} if is_torch_available() else {}

	_lowercase	: str												=	False
	_lowercase	: str												=	False
	_lowercase	: Optional[Any]												=	False
	_lowercase	: Tuple												=	False
	_lowercase	: List[Any]												=	False







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

						'''simple docstring'''





						__magic_name__				=     SwinaSRModelTester(self   )
						__magic_name__				=     ConfigTester(self	,			config_class=__UpperCamelCase	,			embed_dim=37   )







	def 						_SCREAMING_SNAKE_CASE				( self:			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 						_SCREAMING_SNAKE_CASE				( self:			Dict   ):

						'''simple docstring'''





						__magic_name__				=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__UpperCamelCase   )







	def 						_SCREAMING_SNAKE_CASE				( self:			Any   ):

						'''simple docstring'''





						__magic_name__				=     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 						_SCREAMING_SNAKE_CASE				( self:			Optional[Any]   ):

						'''simple docstring'''





						pass







	@unittest.skip(reason='Swin2SR does not use inputs_embeds'   )
	def 						_SCREAMING_SNAKE_CASE				( self:			int   ):

						'''simple docstring'''





						pass







	@unittest.skip(reason='Swin2SR does not support training yet'   )
	def 						_SCREAMING_SNAKE_CASE				( self:			List[Any]   ):

						'''simple docstring'''





						pass







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

						'''simple docstring'''





						pass







	def 						_SCREAMING_SNAKE_CASE				( self:			Optional[Any]   ):

						'''simple docstring'''





						__magic_name__,						__magic_name__				=     self.model_tester.prepare_config_and_inputs_for_common()

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







	def 						_SCREAMING_SNAKE_CASE				( self:			int   ):

						'''simple docstring'''





						__magic_name__,						__magic_name__				=     self.model_tester.prepare_config_and_inputs_for_common()

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

											__magic_name__				=     ['pixel_values']
											self.assertListEqual(arg_names[:1]	,			__UpperCamelCase   )







	@slow
	def 						_SCREAMING_SNAKE_CASE				( self:			Union[str, Any]   ):

						'''simple docstring'''





						for model_name in SWIN2SR_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__magic_name__				=     SwinaSRModel.from_pretrained(__UpperCamelCase   )
											self.assertIsNotNone(__UpperCamelCase   )







	def 						_SCREAMING_SNAKE_CASE				( self:			Tuple   ):

						'''simple docstring'''





						__magic_name__,						__magic_name__				=     self.model_tester.prepare_config_and_inputs_for_common()

						__magic_name__				=     _config_zero_init(__UpperCamelCase   )
						for model_class in self.all_model_classes:
											__magic_name__				=     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 						_SCREAMING_SNAKE_CASE				( self:			Optional[Any]   ):

						'''simple docstring'''





						__magic_name__,						__magic_name__				=     self.model_tester.prepare_config_and_inputs_for_common()
						__magic_name__				=     True

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

											# check that output_attentions also work using config
											del inputs_dict["output_attentions"]
											__magic_name__				=     True
											__magic_name__				=     config.window_size**2
											__magic_name__				=     model_class(__UpperCamelCase   )
											model.to(__UpperCamelCase   )
											model.eval()
											with torch.no_grad():
																__magic_name__				=     model(**self._prepare_for_class(__UpperCamelCase	,			__UpperCamelCase   )   )
											__magic_name__				=     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]	,			)
											__magic_name__				=     len(__UpperCamelCase   )

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

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

											__magic_name__				=     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 					__UpperCamelCase					(       unittest.TestCase      ):







	def 						_SCREAMING_SNAKE_CASE				( self:			Optional[int]   ):

						'''simple docstring'''





						__magic_name__				=     SwinaSRImageProcessor()
						__magic_name__				=     SwinaSRForImageSuperResolution.from_pretrained('caidas/swin2SR-classical-sr-x2-64'   ).to(__UpperCamelCase   )

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

						# forward pass
						with torch.no_grad():
											__magic_name__				=     model(**__UpperCamelCase   )

						# verify the logits
						__magic_name__				=     torch.Size([1, 3, 9_76, 12_96]   )
						self.assertEqual(outputs.reconstruction.shape	,			__UpperCamelCase   )
						__magic_name__				=     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   )   )







from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__							=				logging.get_logger(__name__)

A__							=				{
    "tau/splinter-base": "https://huggingface.co/tau/splinter-base/resolve/main/config.json",
    "tau/splinter-base-qass": "https://huggingface.co/tau/splinter-base-qass/resolve/main/config.json",
    "tau/splinter-large": "https://huggingface.co/tau/splinter-large/resolve/main/config.json",
    "tau/splinter-large-qass": "https://huggingface.co/tau/splinter-large-qass/resolve/main/config.json",
    # See all Splinter models at https://huggingface.co/models?filter=splinter
}

class 					__UpperCamelCase					(       SCREAMING_SNAKE_CASE      ):
	_lowercase	: int												=	"splinter"







	def __init__( self:			str	,			__UpperCamelCase:			Any=3_05_22	,			__UpperCamelCase:			List[Any]=7_68	,			__UpperCamelCase:			str=12	,			__UpperCamelCase:			Optional[Any]=12	,			__UpperCamelCase:			Optional[int]=30_72	,			__UpperCamelCase:			List[str]="gelu"	,			__UpperCamelCase:			Union[str, Any]=0.1	,			__UpperCamelCase:			Dict=0.1	,			__UpperCamelCase:			str=5_12	,			__UpperCamelCase:			Dict=2	,			__UpperCamelCase:			str=0.02	,			__UpperCamelCase:			Tuple=1E-12	,			__UpperCamelCase:			Optional[int]=True	,			__UpperCamelCase:			Union[str, Any]=0	,			__UpperCamelCase:			Optional[int]=1_04	,			**__UpperCamelCase:			Tuple	,			):

						'''simple docstring'''





						super().__init__(pad_token_id=__UpperCamelCase	,			**__UpperCamelCase   )

						__magic_name__				=     vocab_size
						__magic_name__				=     max_position_embeddings
						__magic_name__				=     hidden_size
						__magic_name__				=     num_hidden_layers
						__magic_name__				=     num_attention_heads
						__magic_name__				=     intermediate_size
						__magic_name__				=     hidden_act
						__magic_name__				=     hidden_dropout_prob
						__magic_name__				=     attention_probs_dropout_prob
						__magic_name__				=     initializer_range
						__magic_name__				=     type_vocab_size
						__magic_name__				=     layer_norm_eps
						__magic_name__				=     use_cache
						__magic_name__				=     question_token_id




