





"""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   lowercase_					:

       def __init__(      self ,       __SCREAMING_SNAKE_CASE ,       __SCREAMING_SNAKE_CASE=13 ,       __SCREAMING_SNAKE_CASE=32 ,       __SCREAMING_SNAKE_CASE=1 ,       __SCREAMING_SNAKE_CASE=3 ,       __SCREAMING_SNAKE_CASE=16 ,       __SCREAMING_SNAKE_CASE=[1, 2, 1] ,       __SCREAMING_SNAKE_CASE=[2, 2, 4] ,       __SCREAMING_SNAKE_CASE=2 ,       __SCREAMING_SNAKE_CASE=2.0 ,       __SCREAMING_SNAKE_CASE=True ,       __SCREAMING_SNAKE_CASE=0.0 ,       __SCREAMING_SNAKE_CASE=0.0 ,       __SCREAMING_SNAKE_CASE=0.1 ,       __SCREAMING_SNAKE_CASE="gelu" ,       __SCREAMING_SNAKE_CASE=False ,       __SCREAMING_SNAKE_CASE=True ,       __SCREAMING_SNAKE_CASE=0.02 ,       __SCREAMING_SNAKE_CASE=1e-5 ,       __SCREAMING_SNAKE_CASE=True ,       __SCREAMING_SNAKE_CASE=None ,       __SCREAMING_SNAKE_CASE=False ,       __SCREAMING_SNAKE_CASE=2 ,       ):






        '''simple docstring'''

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

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

       def 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

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

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

        snake_case__     				=					self.get_config()

        return config, pixel_values, labels

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






        '''simple docstring'''

        snake_case__     				=					SwinaSRModel(config=__lowerCAmelCase  )
        model.to(__lowerCAmelCase  )
        model.eval()
        snake_case__     				=					model(__lowerCAmelCase  )

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






        '''simple docstring'''

        snake_case__     				=					SwinaSRForImageSuperResolution(__lowerCAmelCase  )
        model.to(__lowerCAmelCase  )
        model.eval()
        snake_case__     				=					model(__lowerCAmelCase  )

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






        '''simple docstring'''

        snake_case__     				=					self.prepare_config_and_inputs()
        snake_case__     				=					config_and_inputs
        snake_case__     				=					{"""pixel_values""": pixel_values}
        return config, inputs_dict



@require_torch
class   lowercase_					(  snake_case__					,snake_case__					,unittest.TestCase				):
       snake_case__					:				Dict									=  (SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
       snake_case__					:				List[str]									=  {'feature-extraction': SwinaSRModel} if is_torch_available() else {}

       snake_case__					:				List[Any]									=  False
       snake_case__					:				Tuple									=  False
       snake_case__					:				Any									=  False
       snake_case__					:				Union[str, Any]									=  False
       snake_case__					:				Union[str, Any]									=  False

       def 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

        snake_case__     				=					SwinaSRModelTester(self  )
        snake_case__     				=					ConfigTester(self ,       config_class=__lowerCAmelCase ,       embed_dim=37  )

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






        '''simple docstring'''

        snake_case__     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowerCAmelCase  )

       def 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

        snake_case__     				=					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 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

        pass

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






        '''simple docstring'''

        pass

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






        '''simple docstring'''

        pass

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






        '''simple docstring'''

        pass

       def 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

        snake_case__     				=					self.model_tester.prepare_config_and_inputs_for_common()

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

       def 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

        snake_case__     				=					self.model_tester.prepare_config_and_inputs_for_common()

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

         snake_case__     				=					["""pixel_values"""]
         self.assertListEqual(arg_names[:1] ,       __lowerCAmelCase  )

       @slow
       def 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

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

       def 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

        snake_case__     				=					self.model_tester.prepare_config_and_inputs_for_common()

        snake_case__     				=					_config_zero_init(__lowerCAmelCase  )
        for model_class in self.all_model_classes:
         snake_case__     				=					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 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

        snake_case__     				=					self.model_tester.prepare_config_and_inputs_for_common()
        snake_case__     				=					True

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

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

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

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

         snake_case__     				=					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   lowercase_					(  unittest.TestCase				):

       def 		_SCREAMING_SNAKE_CASE(      self  ):






        '''simple docstring'''

        snake_case__     				=					SwinaSRImageProcessor()
        snake_case__     				=					SwinaSRForImageSuperResolution.from_pretrained("caidas/swin2SR-classical-sr-x2-64"  ).to(__lowerCAmelCase  )

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

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

        # verify the logits
        snake_case__     				=					torch.Size([1, 3, 976, 1296]  )
        self.assertEqual(outputs.reconstruction.shape ,       __lowerCAmelCase  )
        snake_case__     				=					torch.tensor(
            [[0.5458, 0.5546, 0.5638], [0.5526, 0.5565, 0.5651], [0.5396, 0.5426, 0.5621]]  ).to(__lowerCAmelCase  )
        self.assertTrue(torch.allclose(outputs.reconstruction[0, 0, :3, :3] ,       __lowerCAmelCase ,       atol=1e-4  )  )







'''simple docstring'''





import functools
import operator

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


a		:	Dict       =      logging.get_logger(__name__)

a		:	Optional[int]       =      {
    '''facebook/hubert-base-ls960''': '''https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json''',
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}




class      UpperCamelCase       (	snake_case__       ):
  a     :int          =					'hubert'



  def __init__(			self		,     __lowerCAmelCase=32		,     __lowerCAmelCase=768		,     __lowerCAmelCase=12		,     __lowerCAmelCase=12		,     __lowerCAmelCase=3072		,     __lowerCAmelCase="gelu"		,     __lowerCAmelCase=0.1		,     __lowerCAmelCase=0.1		,     __lowerCAmelCase=0.1		,     __lowerCAmelCase=True		,     __lowerCAmelCase=0.0		,     __lowerCAmelCase=0.1		,     __lowerCAmelCase=0.1		,     __lowerCAmelCase=0.02		,     __lowerCAmelCase=1e-5		,     __lowerCAmelCase="group"		,     __lowerCAmelCase="gelu"		,     __lowerCAmelCase=(512, 512, 512, 512, 512, 512, 512)		,     __lowerCAmelCase=(5, 2, 2, 2, 2, 2, 2)		,     __lowerCAmelCase=(10, 3, 3, 3, 3, 2, 2)		,     __lowerCAmelCase=False		,     __lowerCAmelCase=128		,     __lowerCAmelCase=16		,     __lowerCAmelCase=False		,     __lowerCAmelCase=True		,     __lowerCAmelCase=0.05		,     __lowerCAmelCase=10		,     __lowerCAmelCase=2		,     __lowerCAmelCase=0.0		,     __lowerCAmelCase=10		,     __lowerCAmelCase=0		,     __lowerCAmelCase="sum"		,     __lowerCAmelCase=False		,     __lowerCAmelCase=False		,     __lowerCAmelCase=256		,     __lowerCAmelCase=0		,     __lowerCAmelCase=1		,     __lowerCAmelCase=2		,     **__lowerCAmelCase		,     ):







      '''simple docstring'''


      super().__init__(**__lowerCAmelCase		,     pad_token_id=__lowerCAmelCase		,     bos_token_id=__lowerCAmelCase		,     eos_token_id=__lowerCAmelCase )
      _a				:							List[Any]							       =      hidden_size
      _a				:							Union[str, Any]							       =      feat_extract_norm
      _a				:							Any							       =      feat_extract_activation
      _a				:							Tuple							       =      list(__lowerCAmelCase )
      _a				:							List[Any]							       =      list(__lowerCAmelCase )
      _a				:							Any							       =      list(__lowerCAmelCase )
      _a				:							Optional[int]							       =      conv_bias
      _a				:							Tuple							       =      num_conv_pos_embeddings
      _a				:							Optional[int]							       =      num_conv_pos_embedding_groups
      _a				:							Tuple							       =      len(self.conv_dim )
      _a				:							int							       =      num_hidden_layers
      _a				:							Optional[Any]							       =      intermediate_size
      _a				:							Dict							       =      hidden_act
      _a				:							Tuple							       =      num_attention_heads
      _a				:							Tuple							       =      hidden_dropout
      _a				:							str							       =      attention_dropout
      _a				:							Union[str, Any]							       =      activation_dropout
      _a				:							int							       =      feat_proj_layer_norm
      _a				:							List[Any]							       =      feat_proj_dropout
      _a				:							Any							       =      final_dropout
      _a				:							Dict							       =      layerdrop
      _a				:							List[str]							       =      layer_norm_eps
      _a				:							Any							       =      initializer_range
      _a				:							List[str]							       =      vocab_size
      _a				:							int							       =      do_stable_layer_norm
      _a				:							str							       =      use_weighted_layer_sum
      _a				:							Optional[Any]							       =      classifier_proj_size

      if (
          (len(self.conv_stride ) != self.num_feat_extract_layers)
          or (len(self.conv_kernel ) != self.num_feat_extract_layers)
          or (len(self.conv_dim ) != self.num_feat_extract_layers)
      ):
          raise ValueError(
              """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
              """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
              f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,'''
              f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' )

      # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
      _a				:							str							       =      apply_spec_augment
      _a				:							List[Any]							       =      mask_time_prob
      _a				:							List[Any]							       =      mask_time_length
      _a				:							List[Any]							       =      mask_time_min_masks
      _a				:							Union[str, Any]							       =      mask_feature_prob
      _a				:							str							       =      mask_feature_length
      _a				:							Any							       =      mask_feature_min_masks

      # ctc loss
      _a				:							Tuple							       =      ctc_loss_reduction
      _a				:							Optional[int]							       =      ctc_zero_infinity



  @property
  def     a_							(			self ):







      '''simple docstring'''


      return functools.reduce(operator.mul		,     self.conv_stride		,     1 )


