


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







 """simple docstring"""





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

      # here we set some attributes to make tests pass
      UpperCAmelCase    :					Optional[Any]			=      len(a_					)
      UpperCAmelCase    :					Optional[int]			=      embed_dim
      UpperCAmelCase    :					Dict			=      (image_size // patch_size) ** 2
 def 	_UpperCamelCase     (      self       :			Any					)     ->      Any:
      UpperCAmelCase    :					Dict			=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

      UpperCAmelCase    :					Tuple			=      self.get_config()

      return config, pixel_values, labels
 def 	_UpperCamelCase     (      self       :			Any					)     ->      Union[str, Any]:
      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							,     _a       :			Union[str, Any]							,     _a       :			Dict							,     _a       :			Tuple					)     ->      Union[str, Any]:
      UpperCAmelCase    :					str			=      SwinaSRModel(config=a_					)
      model.to(a_					)
      model.eval()
      UpperCAmelCase    :					Tuple			=      model(a_					)

      self.parent.assertEqual(
          result.last_hidden_state.shape							,     (self.batch_size, self.embed_dim, self.image_size, self.image_size)					)
 def 	_UpperCamelCase     (      self       :			List[Any]							,     _a       :			str							,     _a       :			List[str]							,     _a       :			int					)     ->      Optional[int]:
      UpperCAmelCase    :					List[str]			=      SwinaSRForImageSuperResolution(a_					)
      model.to(a_					)
      model.eval()
      UpperCAmelCase    :					Optional[Any]			=      model(a_					)

      UpperCAmelCase    :					Tuple			=      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       :			Any					)     ->      Tuple:
      UpperCAmelCase    :					Dict			=      self.prepare_config_and_inputs()
      UpperCAmelCase	,			UpperCAmelCase	,			UpperCAmelCase    :					str			=      config_and_inputs
      UpperCAmelCase    :					Any			=      {"pixel_values": pixel_values}
      return config, inputs_dict




@require_torch
class  A_ (			_UpperCAmelCase   ,    _UpperCAmelCase   ,    unittest.TestCase  ):







 """simple docstring"""





 _UpperCamelCase					:			str     	=       (SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
 _UpperCamelCase					:			Optional[Any]     	=       {'''feature-extraction''': SwinaSRModel} if is_torch_available() else {}

 _UpperCamelCase					:			List[str]     	=       False
 _UpperCamelCase					:			Optional[Any]     	=       False
 _UpperCamelCase					:			str     	=       False
 _UpperCamelCase					:			Optional[int]     	=       False
 _UpperCamelCase					:			List[Any]     	=       False
 def 	_UpperCamelCase     (      self       :			int					)     ->      Union[str, Any]:
      UpperCAmelCase    :					Optional[Any]			=      SwinaSRModelTester(self					)
      UpperCAmelCase    :					Any			=      ConfigTester(self							,     config_class=a_							,     embed_dim=3_7					)
 def 	_UpperCamelCase     (      self       :			Tuple					)     ->      Union[str, Any]:
      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					)     ->      int:
      UpperCAmelCase    :					Union[str, Any]			=      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*a_					)
 def 	_UpperCamelCase     (      self       :			Optional[int]					)     ->      Optional[int]:
      UpperCAmelCase    :					List[Any]			=      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_super_resolution(*a_					)
 @unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0."					)
 def 	_UpperCamelCase     (      self       :			Tuple					)     ->      int:
      pass
 @unittest.skip(reason="Swin2SR does not use inputs_embeds"					)
 def 	_UpperCamelCase     (      self       :			Tuple					)     ->      Union[str, Any]:
      pass
 @unittest.skip(reason="Swin2SR does not support training yet"					)
 def 	_UpperCamelCase     (      self       :			Any					)     ->      Union[str, Any]:
      pass
 @unittest.skip(reason="Swin2SR does not support training yet"					)
 def 	_UpperCamelCase     (      self       :			int					)     ->      Any:
      pass
 def 	_UpperCamelCase     (      self       :			int					)     ->      Tuple:
      UpperCAmelCase	,			UpperCAmelCase    :					int			=      self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           UpperCAmelCase    :					List[Any]			=      model_class(a_					)
           self.assertIsInstance(model.get_input_embeddings()							,     (nn.Module)					)
           UpperCAmelCase    :					Tuple			=      model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(a_							,     nn.Linear					)					)
 def 	_UpperCamelCase     (      self       :			int					)     ->      Optional[Any]:
      UpperCAmelCase	,			UpperCAmelCase    :					Optional[int]			=      self.model_tester.prepare_config_and_inputs_for_common()

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

           UpperCAmelCase    :					Tuple			=      ["pixel_values"]
           self.assertListEqual(arg_names[:1]							,     a_					)
 @slow
 def 	_UpperCamelCase     (      self       :			Union[str, Any]					)     ->      Dict:


      for model_name in SWIN2SR_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCAmelCase    :					List[str]			=      SwinaSRModel.from_pretrained(a_					)
           self.assertIsNotNone(a_					)
 def 	_UpperCamelCase     (      self       :			Union[str, Any]					)     ->      Dict:
      UpperCAmelCase	,			UpperCAmelCase    :					int			=      self.model_tester.prepare_config_and_inputs_for_common()

      UpperCAmelCase    :					Union[str, Any]			=      _config_zero_init(a_					)
      for model_class in self.all_model_classes:
           UpperCAmelCase    :					Tuple			=      model_class(config=a_					)
           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[int]					)     ->      Tuple:
      UpperCAmelCase	,			UpperCAmelCase    :					Any			=      self.model_tester.prepare_config_and_inputs_for_common()
      UpperCAmelCase    :					Dict			=      True

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

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

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

           # Check attention is always last and order is fine
           UpperCAmelCase    :					Any			=      True
           UpperCAmelCase    :					List[str]			=      True
           UpperCAmelCase    :					Dict			=      model_class(a_					)
           model.to(a_					)
           model.eval()
           with torch.no_grad():
                UpperCAmelCase    :					Optional[Any]			=      model(**self._prepare_for_class(a_							,     a_					)					)

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

           UpperCAmelCase    :					Optional[Any]			=      outputs.attentions

           self.assertEqual(len(a_					)							,     a_					)

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







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			Union[str, Any]					)     ->      Optional[Any]:
      UpperCAmelCase    :					Optional[Any]			=      SwinaSRImageProcessor()
      UpperCAmelCase    :					int			=      SwinaSRForImageSuperResolution.from_pretrained("caidas/swin2SR-classical-sr-x2-64"					).to(a_					)

      UpperCAmelCase    :					List[Any]			=      Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
      UpperCAmelCase    :					str			=      processor(images=a_							,     return_tensors="pt"					).to(a_					)

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

      # verify the logits
      UpperCAmelCase    :					Optional[Any]			=      torch.Size([1, 3, 9_7_6, 1_2_9_6]					)
      self.assertEqual(outputs.reconstruction.shape							,     a_					)
      UpperCAmelCase    :					Optional[int]			=      torch.tensor(
          [[0.5_4_5_8, 0.5_5_4_6, 0.5_6_3_8], [0.5_5_2_6, 0.5_5_6_5, 0.5_6_5_1], [0.5_3_9_6, 0.5_4_2_6, 0.5_6_2_1]]					).to(a_					)
      self.assertTrue(torch.allclose(outputs.reconstruction[0, 0, :3, :3]							,     a_							,     atol=1E-4					)					)











"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase											=     {"""configuration_pegasus""": ["""PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PegasusConfig"""]}

try:
     if not is_sentencepiece_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     ["""PegasusTokenizer"""]

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     ["""PegasusTokenizerFast"""]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """PegasusForCausalLM""",
         """PegasusForConditionalGeneration""",
         """PegasusModel""",
         """PegasusPreTrainedModel""",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """TFPegasusForConditionalGeneration""",
         """TFPegasusModel""",
         """TFPegasusPreTrainedModel""",
     ]

try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """FlaxPegasusForConditionalGeneration""",
         """FlaxPegasusModel""",
         """FlaxPegasusPreTrainedModel""",
     ]


if TYPE_CHECKING:
     from .configuration_pegasus import PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusConfig

     try:
          if not is_sentencepiece_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_pegasus import PegasusTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_pegasus_fast import PegasusTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_pegasus import (
              PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST,
              PegasusForCausalLM,
              PegasusForConditionalGeneration,
              PegasusModel,
              PegasusPreTrainedModel,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_pegasus import TFPegasusForConditionalGeneration, TFPegasusModel, TFPegasusPreTrainedModel

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_pegasus import (
              FlaxPegasusForConditionalGeneration,
              FlaxPegasusModel,
              FlaxPegasusPreTrainedModel,
          )

else:
     import sys

     UpperCamelCase											=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


