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

       # here we set some attributes to make tests pass
       _lowercase							     =       len(_lowercase		)
       _lowercase							     =       embed_dim
       _lowercase							     =       (image_size // patch_size) ** 2
    def 		_lowerCamelCase      (	self			:       str		)						->    int:
       _lowercase							     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

       _lowercase							     =       self.get_config()

       return config, pixel_values, labels
    def 		_lowerCamelCase      (	self			:       str		)						->    List[str]:
       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			:       str   ,						_lowercase			:       int   ,						_lowercase			:       str   ,						_lowercase			:       int		)						->    List[Any]:
       _lowercase							     =       SwinaSRModel(config=_lowercase		)
       model.to(_lowercase		)
       model.eval()
       _lowercase							     =       model(_lowercase		)

       self.parent.assertEqual(
           result.last_hidden_state.shape   ,						(self.batch_size, self.embed_dim, self.image_size, self.image_size)		)
    def 		_lowerCamelCase      (	self			:       Any   ,						_lowercase			:       Tuple   ,						_lowercase			:       Dict   ,						_lowercase			:       Optional[Any]		)						->    Optional[int]:
       _lowercase							     =       SwinaSRForImageSuperResolution(_lowercase		)
       model.to(_lowercase		)
       model.eval()
       _lowercase							     =       model(_lowercase		)

       _lowercase							     =       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			:       Dict		)						->    Optional[int]:
       _lowercase							     =       self.prepare_config_and_inputs()
       _lowercase     ,  _lowercase     ,  _lowercase							     =       config_and_inputs
       _lowercase							     =       {"pixel_values": pixel_values}
       return config, inputs_dict
@require_torch
class   UpperCAmelCase_	(				lowercase__,    lowercase__,    unittest.TestCase   ):
    snake_case_											=      (SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
    snake_case_											=      {"""feature-extraction""": SwinaSRModel} if is_torch_available() else {}

    snake_case_											=      False
    snake_case_											=      False
    snake_case_											=      False
    snake_case_											=      False
    snake_case_											=      False
    def 		_lowerCamelCase      (	self			:       Any		)						->    List[Any]:
       _lowercase							     =       SwinaSRModelTester(self		)
       _lowercase							     =       ConfigTester(self   ,						config_class=_lowercase   ,						embed_dim=3_7		)
    def 		_lowerCamelCase      (	self			:       Optional[int]		)						->    Dict:
       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			:       int		)						->    Optional[int]:
       _lowercase							     =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowercase		)
    def 		_lowerCamelCase      (	self			:       Optional[int]		)						->    str:
       _lowercase							     =       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 		_lowerCamelCase      (	self			:       List[str]		)						->    Optional[Any]:
       pass
    @unittest.skip(reason="Swin2SR does not use inputs_embeds"		)
    def 		_lowerCamelCase      (	self			:       int		)						->    int:
       pass
    @unittest.skip(reason="Swin2SR does not support training yet"		)
    def 		_lowerCamelCase      (	self			:       Any		)						->    Union[str, Any]:
       pass
    @unittest.skip(reason="Swin2SR does not support training yet"		)
    def 		_lowerCamelCase      (	self			:       List[str]		)						->    List[str]:
       pass
    def 		_lowerCamelCase      (	self			:       List[Any]		)						->    Dict:
       _lowercase     ,  _lowercase							     =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          _lowercase							     =       model_class(_lowercase		)
          self.assertIsInstance(model.get_input_embeddings()   ,						(nn.Module)		)
          _lowercase							     =       model.get_output_embeddings()
          self.assertTrue(x is None or isinstance(_lowercase   ,						nn.Linear		)		)
    def 		_lowerCamelCase      (	self			:       Any		)						->    Tuple:
       _lowercase     ,  _lowercase							     =       self.model_tester.prepare_config_and_inputs_for_common()

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

          _lowercase							     =       ["pixel_values"]
          self.assertListEqual(arg_names[:1]   ,						_lowercase		)
    @slow
    def 		_lowerCamelCase      (	self			:       Union[str, Any]		)						->    int:






       for model_name in SWIN2SR_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          _lowercase							     =       SwinaSRModel.from_pretrained(_lowercase		)
          self.assertIsNotNone(_lowercase		)
    def 		_lowerCamelCase      (	self			:       Tuple		)						->    Optional[int]:
       _lowercase     ,  _lowercase							     =       self.model_tester.prepare_config_and_inputs_for_common()

       _lowercase							     =       _config_zero_init(_lowercase		)
       for model_class in self.all_model_classes:
          _lowercase							     =       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 		_lowerCamelCase      (	self			:       int		)						->    Optional[int]:
       _lowercase     ,  _lowercase							     =       self.model_tester.prepare_config_and_inputs_for_common()
       _lowercase							     =       True

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

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

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

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

          _lowercase							     =       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   UpperCAmelCase_	(				unittest.TestCase   ):
    def 		_lowerCamelCase      (	self			:       str		)						->    str:
       _lowercase							     =       SwinaSRImageProcessor()
       _lowercase							     =       SwinaSRForImageSuperResolution.from_pretrained("caidas/swin2SR-classical-sr-x2-64"		).to(_lowercase		)

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

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

       # verify the logits
       _lowercase							     =       torch.Size([1, 3, 9_7_6, 1_2_9_6]		)
       self.assertEqual(outputs.reconstruction.shape   ,						_lowercase		)
       _lowercase							     =       torch.tensor(
           [[0.54_58, 0.55_46, 0.56_38], [0.55_26, 0.55_65, 0.56_51], [0.53_96, 0.54_26, 0.56_21]]		).to(_lowercase		)
       self.assertTrue(torch.allclose(outputs.reconstruction[0, 0, :3, :3]   ,						_lowercase   ,						atol=1e-4		)		)




import sys

sys.set_int_max_str_digits(0)  # type: ignore





def 				A      (	_UpperCAmelCase							:  int       )							->					bool:







			'''simple docstring'''
			_UpperCAmelCase									=  [0] * 11
			_UpperCAmelCase									=  [0] * 11

			# mark last 9 numbers
			for _ in range(9       ):
						_UpperCAmelCase									=  1
						_UpperCAmelCase									=  number // 10
			# flag
			_UpperCAmelCase									=  True

			# check last 9 numbers for pandigitality

			for x in range(9       ):
						if not check_last[x + 1]:
									_UpperCAmelCase									=  False
			if not f:
						return f

			# mark first 9 numbers
			_UpperCAmelCase									=  int(str(_UpperCAmelCase       )[:9]       )

			for _ in range(9       ):
						_UpperCAmelCase									=  1
						_UpperCAmelCase									=  number // 10

			# check first 9 numbers for pandigitality

			for x in range(9       ):
						if not check_front[x + 1]:
									_UpperCAmelCase									=  False
			return f





def 				A      (	_UpperCAmelCase							:  int       )							->					bool:







			'''simple docstring'''
			_UpperCAmelCase									=  [0] * 11

			# mark last 9 numbers
			for _ in range(9       ):
						_UpperCAmelCase									=  1
						_UpperCAmelCase									=  number // 10
			# flag
			_UpperCAmelCase									=  True

			# check last 9 numbers for pandigitality

			for x in range(9       ):
						if not check_last[x + 1]:
									_UpperCAmelCase									=  False
			return f





def 				A      (	)							->					int:







			'''simple docstring'''
			_UpperCAmelCase									=  1
			_UpperCAmelCase									=  1
			_UpperCAmelCase									=  2
			# temporary Fibonacci numbers

			_UpperCAmelCase									=  1
			_UpperCAmelCase									=  1
			_UpperCAmelCase									=  2
			# temporary Fibonacci numbers mod 1e9

			# mod m=1e9, done for fast optimisation
			_UpperCAmelCase									=  [0] * 1_000_000
			_UpperCAmelCase									=  1_000_000_000

			for x in range(1_000_000       ):
						_UpperCAmelCase									=  (aa + ba) % m
						_UpperCAmelCase									=  ba % m
						_UpperCAmelCase									=  ca % m
						if checka(_UpperCAmelCase       ):
									_UpperCAmelCase									=  1

			for x in range(1_000_000       ):
						_UpperCAmelCase									=  a + b
						_UpperCAmelCase									=  b
						_UpperCAmelCase									=  c
						# perform check only if in tocheck
						if tocheck[x + 3] and check(_UpperCAmelCase       ):
									return x + 3  # first 2 already done
			return -1


if __name__ == "__main__":
							print(f"""{solution() = }""")
