






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



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

				"""simple docstring"""






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

				# here we set some attributes to make tests pass
				lowercase__						:	Optional[int]         = len(_lowerCamelCase	)
				lowercase__						:	Union[str, Any]         = embed_dim
				lowercase__						:	Union[str, Any]         = (image_size // patch_size) ** 2



	def 						UpperCAmelCase    (	self   :   Optional[Any]	):

				"""simple docstring"""






				lowercase__						:	int         = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				lowercase__						:	List[str]         = None
				if self.use_labels:
							lowercase__						:	Optional[int]         = ids_tensor([self.batch_size] ,		self.type_sequence_label_size	)

				lowercase__						:	List[str]         = self.get_config()

				return config, pixel_values, labels



	def 						UpperCAmelCase    (	self   :   int	):

				"""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 						UpperCAmelCase    (	self   :   Tuple ,		UpperCAmelCase__   :   str ,		UpperCAmelCase__   :   Optional[Any] ,		UpperCAmelCase__   :   List[str]	):

				"""simple docstring"""






				lowercase__						:	Dict         = SwinaSRModel(config=_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()
				lowercase__						:	List[str]         = model(_lowerCamelCase	)

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



	def 						UpperCAmelCase    (	self   :   List[str] ,		UpperCAmelCase__   :   List[str] ,		UpperCAmelCase__   :   Any ,		UpperCAmelCase__   :   Tuple	):

				"""simple docstring"""






				lowercase__						:	Tuple         = SwinaSRForImageSuperResolution(_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()
				lowercase__						:	Any         = model(_lowerCamelCase	)

				lowercase__						:	Union[str, 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 						UpperCAmelCase    (	self   :   List[str]	):

				"""simple docstring"""






				lowercase__						:	str         = self.prepare_config_and_inputs()
				lowercase__						:	Union[str, Any]         = config_and_inputs
				lowercase__						:	List[str]         = {"""pixel_values""": pixel_values}
				return config, inputs_dict
@require_torch
class        __snake_case(					lowerCAmelCase__     ,       lowerCAmelCase__     ,       unittest.TestCase      ):
	lowercase :       Optional[int]		      =		(SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
	lowercase :       Any		      =		{'''feature-extraction''': SwinaSRModel} if is_torch_available() else {}

	lowercase :       str		      =		False
	lowercase :       Any		      =		False
	lowercase :       Dict		      =		False
	lowercase :       Tuple		      =		False
	lowercase :       List[str]		      =		False



	def 						UpperCAmelCase    (	self   :   int	):

				"""simple docstring"""






				lowercase__						:	Union[str, Any]         = SwinaSRModelTester(self	)
				lowercase__						:	List[Any]         = ConfigTester(self ,		config_class=_lowerCamelCase ,		embed_dim=37	)



	def 						UpperCAmelCase    (	self   :   str	):

				"""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 						UpperCAmelCase    (	self   :   Any	):

				"""simple docstring"""






				lowercase__						:	List[str]         = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_lowerCamelCase	)



	def 						UpperCAmelCase    (	self   :   List[Any]	):

				"""simple docstring"""






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

				"""simple docstring"""






				pass



	@unittest.skip(reason="""Swin2SR does not use inputs_embeds"""	)
	def 						UpperCAmelCase    (	self   :   Union[str, Any]	):

				"""simple docstring"""






				pass



	@unittest.skip(reason="""Swin2SR does not support training yet"""	)
	def 						UpperCAmelCase    (	self   :   str	):

				"""simple docstring"""






				pass



	@unittest.skip(reason="""Swin2SR does not support training yet"""	)
	def 						UpperCAmelCase    (	self   :   List[str]	):

				"""simple docstring"""






				pass



	def 						UpperCAmelCase    (	self   :   List[str]	):

				"""simple docstring"""






				lowercase__						:	Tuple         = self.model_tester.prepare_config_and_inputs_for_common()

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



	def 						UpperCAmelCase    (	self   :   List[Any]	):

				"""simple docstring"""






				lowercase__						:	Optional[Any]         = self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							lowercase__						:	str         = model_class(_lowerCamelCase	)
							lowercase__						:	Tuple         = inspect.signature(model.forward	)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							lowercase__						:	Optional[int]         = [*signature.parameters.keys()]

							lowercase__						:	str         = ["""pixel_values"""]
							self.assertListEqual(arg_names[:1] ,		_lowerCamelCase	)



	@slow
	def 						UpperCAmelCase    (	self   :   int	):

				"""simple docstring"""






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



	def 						UpperCAmelCase    (	self   :   List[Any]	):

				"""simple docstring"""






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

				lowercase__						:	List[Any]         = _config_zero_init(_lowerCamelCase	)
				for model_class in self.all_model_classes:
							lowercase__						:	Dict         = 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 						UpperCAmelCase    (	self   :   Optional[Any]	):

				"""simple docstring"""






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

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

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

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

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

							lowercase__						:	Dict         = 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        __snake_case(					unittest.TestCase      ):



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

				"""simple docstring"""






				lowercase__						:	str         = SwinaSRImageProcessor()
				lowercase__						:	Any         = SwinaSRForImageSuperResolution.from_pretrained("""caidas/swin2SR-classical-sr-x2-64"""	).to(_lowerCamelCase	)

				lowercase__						:	Optional[int]         = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
				lowercase__						:	Dict         = processor(images=_lowerCamelCase ,		return_tensors="""pt"""	).to(_lowerCamelCase	)

				# forward pass
				with torch.no_grad():
							lowercase__						:	Optional[int]         = model(**_lowerCamelCase	)

				# verify the logits
				lowercase__						:	Tuple         = torch.Size([1, 3, 976, 1_296]	)
				self.assertEqual(outputs.reconstruction.shape ,		_lowerCamelCase	)
				lowercase__						:	Optional[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	)	)










from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a    =       logging.get_logger(__name__)

a    =       {
    'nghuyong/ernie-1.0-base-zh': 'https://huggingface.co/nghuyong/ernie-1.0-base-zh/resolve/main/config.json',
    'nghuyong/ernie-2.0-base-en': 'https://huggingface.co/nghuyong/ernie-2.0-base-en/resolve/main/config.json',
    'nghuyong/ernie-2.0-large-en': 'https://huggingface.co/nghuyong/ernie-2.0-large-en/resolve/main/config.json',
    'nghuyong/ernie-3.0-base-zh': 'https://huggingface.co/nghuyong/ernie-3.0-base-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-medium-zh': 'https://huggingface.co/nghuyong/ernie-3.0-medium-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-mini-zh': 'https://huggingface.co/nghuyong/ernie-3.0-mini-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-micro-zh': 'https://huggingface.co/nghuyong/ernie-3.0-micro-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-nano-zh': 'https://huggingface.co/nghuyong/ernie-3.0-nano-zh/resolve/main/config.json',
    'nghuyong/ernie-gram-zh': 'https://huggingface.co/nghuyong/ernie-gram-zh/resolve/main/config.json',
    'nghuyong/ernie-health-zh': 'https://huggingface.co/nghuyong/ernie-health-zh/resolve/main/config.json',
}
class        __snake_case(					__UpperCamelCase      ):
 lowercase :       Dict		      =		'''ernie'''



 def __init__(	self   :   Tuple ,		UpperCAmelCase__   :   int=30_522 ,		UpperCAmelCase__   :   List[str]=768 ,		UpperCAmelCase__   :   Tuple=12 ,		UpperCAmelCase__   :   Optional[int]=12 ,		UpperCAmelCase__   :   Any=3_072 ,		UpperCAmelCase__   :   Tuple="gelu" ,		UpperCAmelCase__   :   Union[str, Any]=0.1 ,		UpperCAmelCase__   :   str=0.1 ,		UpperCAmelCase__   :   List[str]=512 ,		UpperCAmelCase__   :   str=2 ,		UpperCAmelCase__   :   Optional[int]=3 ,		UpperCAmelCase__   :   Dict=False ,		UpperCAmelCase__   :   Any=0.02 ,		UpperCAmelCase__   :   Any=1e-12 ,		UpperCAmelCase__   :   str=0 ,		UpperCAmelCase__   :   Any="absolute" ,		UpperCAmelCase__   :   List[str]=True ,		UpperCAmelCase__   :   Tuple=None ,		**UpperCAmelCase__   :   Dict ,		):

    """simple docstring"""






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

    lowercase__						:	List[str]         = vocab_size
    lowercase__						:	Optional[int]         = hidden_size
    lowercase__						:	Union[str, Any]         = num_hidden_layers
    lowercase__						:	Any         = num_attention_heads
    lowercase__						:	Optional[int]         = hidden_act
    lowercase__						:	str         = intermediate_size
    lowercase__						:	Tuple         = hidden_dropout_prob
    lowercase__						:	Tuple         = attention_probs_dropout_prob
    lowercase__						:	str         = max_position_embeddings
    lowercase__						:	List[Any]         = type_vocab_size
    lowercase__						:	Any         = task_type_vocab_size
    lowercase__						:	Optional[Any]         = use_task_id
    lowercase__						:	Optional[Any]         = initializer_range
    lowercase__						:	Union[str, Any]         = layer_norm_eps
    lowercase__						:	Dict         = position_embedding_type
    lowercase__						:	Union[str, Any]         = use_cache
    lowercase__						:	int         = classifier_dropout
class        __snake_case(					__UpperCamelCase      ):



 @property
 def 						UpperCAmelCase    (	self   :   List[Any]	):

    """simple docstring"""






    if self.task == "multiple-choice":
       lowercase__						:	Dict         = {0: """batch""", 1: """choice""", 2: """sequence"""}
    else:
       lowercase__						:	Dict         = {0: """batch""", 1: """sequence"""}
    return OrderedDict(
        [
            ("""input_ids""", dynamic_axis),
            ("""attention_mask""", dynamic_axis),
            ("""token_type_ids""", dynamic_axis),
            ("""task_type_ids""", dynamic_axis),
        ]	)


