






'''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      ,  UpperCamelCase__      ,  UpperCamelCase__=13      ,  UpperCamelCase__=32      ,  UpperCamelCase__=1      ,  UpperCamelCase__=3      ,  UpperCamelCase__=16      ,  UpperCamelCase__=[1, 2, 1]      ,  UpperCamelCase__=[2, 2, 4]      ,  UpperCamelCase__=2      ,  UpperCamelCase__=2.0      ,  UpperCamelCase__=True      ,  UpperCamelCase__=0.0      ,  UpperCamelCase__=0.0      ,  UpperCamelCase__=0.1      ,  UpperCamelCase__="gelu"      ,  UpperCamelCase__=False      ,  UpperCamelCase__=True      ,  UpperCamelCase__=0.02      ,  UpperCamelCase__=1e-5      ,  UpperCamelCase__=True      ,  UpperCamelCase__=None      ,  UpperCamelCase__=False      ,  UpperCamelCase__=2      ,  ):






         """simple docstring"""


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

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







  def       _a					( self  ):






         """simple docstring"""


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

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

         a_	    = self.get_config()

         return config, pixel_values, labels







  def       _a					( self  ):






         """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       _a					( self      ,  UpperCamelCase__      ,  UpperCamelCase__      ,  UpperCamelCase__  ):






         """simple docstring"""


         a_	    = SwinaSRModel(config=UpperCamelCase__  )
         model.to(UpperCamelCase__  )
         model.eval()
         a_	    = model(UpperCamelCase__  )

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







  def       _a					( self      ,  UpperCamelCase__      ,  UpperCamelCase__      ,  UpperCamelCase__  ):






         """simple docstring"""


         a_	    = SwinaSRForImageSuperResolution(UpperCamelCase__  )
         model.to(UpperCamelCase__  )
         model.eval()
         a_	    = model(UpperCamelCase__  )

         a_	    = 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  ):






         """simple docstring"""


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





@require_torch
class      __SCREAMING_SNAKE_CASE					(__A       ,					__A       ,					unittest.TestCase					):


  """simple docstring"""


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

  _a     :   Dict			 =    False
  _a     :   str			 =    False
  _a     :   List[Any]			 =    False
  _a     :   Optional[int]			 =    False
  _a     :   Optional[Any]			 =    False







  def       _a					( self  ):






         """simple docstring"""


         a_	    = SwinaSRModelTester(self  )
         a_	    = ConfigTester(self      ,  config_class=UpperCamelCase__      ,  embed_dim=37  )







  def       _a					( self  ):






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






         """simple docstring"""


         a_	    = self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase__  )







  def       _a					( self  ):






         """simple docstring"""


         a_	    = 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       _a					( self  ):






         """simple docstring"""


         pass







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






         """simple docstring"""


         pass







  @unittest.skip(reason='Swin2SR does not support training yet'  )
  def       _a					( self  ):






         """simple docstring"""


         pass







  @unittest.skip(reason='Swin2SR does not support training yet'  )
  def       _a					( self  ):






         """simple docstring"""


         pass







  def       _a					( self  ):






         """simple docstring"""


         a_    ,					a_	    = self.model_tester.prepare_config_and_inputs_for_common()

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







  def       _a					( self  ):






         """simple docstring"""


         a_    ,					a_	    = self.model_tester.prepare_config_and_inputs_for_common()

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

                a_	    = ['pixel_values']
                self.assertListEqual(arg_names[:1]      ,  UpperCamelCase__  )







  @slow
  def       _a					( self  ):






         """simple docstring"""


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







  def       _a					( self  ):






         """simple docstring"""


         a_    ,					a_	    = self.model_tester.prepare_config_and_inputs_for_common()

         a_	    = _config_zero_init(UpperCamelCase__  )
         for model_class in self.all_model_classes:
                a_	    = 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       _a					( self  ):






         """simple docstring"""


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

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

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

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

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

                a_	    = 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       _a					( self  ):






         """simple docstring"""


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

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

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

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












'''simple docstring'''







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


__lowerCAmelCase					   =       logging.get_logger(__name__)

__lowerCAmelCase					   =       {
    "uw-madison/nystromformer-512": "https://huggingface.co/uw-madison/nystromformer-512/resolve/main/config.json",
    # See all Nystromformer models at https://huggingface.co/models?filter=nystromformer
}




class      __SCREAMING_SNAKE_CASE					(__A					):


  """simple docstring"""


  _a     :   Union[str, Any]			 =    '''nystromformer'''







  def __init__( self      ,  UpperCamelCase__=30_000      ,  UpperCamelCase__=768      ,  UpperCamelCase__=12      ,  UpperCamelCase__=12      ,  UpperCamelCase__=3_072      ,  UpperCamelCase__="gelu_new"      ,  UpperCamelCase__=0.1      ,  UpperCamelCase__=0.1      ,  UpperCamelCase__=510      ,  UpperCamelCase__=2      ,  UpperCamelCase__=64      ,  UpperCamelCase__=64      ,  UpperCamelCase__=65      ,  UpperCamelCase__=False      ,  UpperCamelCase__=0.02      ,  UpperCamelCase__=1e-5      ,  UpperCamelCase__=1      ,  UpperCamelCase__=0      ,  UpperCamelCase__=2      ,  **UpperCamelCase__      ,  ):






         """simple docstring"""


         a_	    = vocab_size
         a_	    = max_position_embeddings
         a_	    = hidden_size
         a_	    = num_hidden_layers
         a_	    = num_attention_heads
         a_	    = intermediate_size
         a_	    = hidden_act
         a_	    = hidden_dropout_prob
         a_	    = attention_probs_dropout_prob
         a_	    = initializer_range
         a_	    = type_vocab_size
         a_	    = segment_means_seq_len
         a_	    = num_landmarks
         a_	    = conv_kernel_size
         a_	    = inv_coeff_init_option
         a_	    = layer_norm_eps
         super().__init__(pad_token_id=UpperCamelCase__      ,  bos_token_id=UpperCamelCase__      ,  eos_token_id=UpperCamelCase__      ,  **UpperCamelCase__  )




