






"""simple docstring"""




from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFImageClassifierOutputWithNoAttention,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_resnet import ResNetConfig


UpperCamelCase											=     logging.get_logger(__name__)

# General docstring
UpperCamelCase											=     """ResNetConfig"""

# Base docstring
UpperCamelCase											=     """microsoft/resnet-50"""
UpperCamelCase											=     [1, 2_048, 7, 7]

# Image classification docstring
UpperCamelCase											=     """microsoft/resnet-50"""
UpperCamelCase											=     """tiger cat"""

UpperCamelCase											=     [
    """microsoft/resnet-50""",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]




class  lowercase_     (tf.keras.layers.Layer  ):






      def __init__(     self  ,  a_  ,  a_ = 3  ,  a_ = 1  ,  a_ = "relu"  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(**a_						)
        _a					      =						kernel_size // 2
        _a					      =						tf.keras.layers.ConvaD(
            a_  ,  kernel_size=a_  ,  strides=a_  ,  padding="valid"  ,  use_bias=a_  ,  name="convolution"						)
        # Use same default momentum and epsilon as PyTorch equivalent
        _a					      =						tf.keras.layers.BatchNormalization(epsilon=1E-5  ,  momentum=0.9  ,  name="normalization"						)
        _a					      =						ACTaFN[activation] if activation is not None else tf.keras.layers.Activation("linear"						)






      def       lowerCamelCase__     (     self  ,  a_						)						->tf.Tensor:







        '''simple docstring'''


        _a					      =						_a					      =						(self.pad_value, self.pad_value)
        _a					      =						tf.pad(a_  ,  [(0, 0), height_pad, width_pad, (0, 0)]						)
        _a					      =						self.conv(a_						)
        return hidden_state






      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = False						)						->tf.Tensor:







        '''simple docstring'''


        _a					      =						self.convolution(a_						)
        _a					      =						self.normalization(a_  ,  training=a_						)
        _a					      =						self.activation(a_						)
        return hidden_state







class  lowercase_     (tf.keras.layers.Layer  ):






      def __init__(     self  ,  a_  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(**a_						)
        _a					      =						TFResNetConvLayer(
            config.embedding_size  ,  kernel_size=7  ,  stride=2  ,  activation=config.hidden_act  ,  name="embedder"  ,  )
        _a					      =						tf.keras.layers.MaxPoolaD(pool_size=3  ,  strides=2  ,  padding="valid"  ,  name="pooler"						)
        _a					      =						config.num_channels






      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = False						)						->tf.Tensor:







        '''simple docstring'''


        _a      ,						_a      ,						_a      ,						_a					      =						shape_list(a_						)
        if tf.executing_eagerly() and num_channels != self.num_channels:
          raise ValueError(
              "Make sure that the channel dimension of the pixel values match with the one set in the configuration."						)
        _a					      =						pixel_values
        _a					      =						self.embedder(a_						)
        _a					      =						tf.pad(a_  ,  [[0, 0], [1, 1], [1, 1], [0, 0]]						)
        _a					      =						self.pooler(a_						)
        return hidden_state







class  lowercase_     (tf.keras.layers.Layer  ):






      def __init__(     self  ,  a_  ,  a_ = 2  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(**a_						)
        _a					      =						tf.keras.layers.ConvaD(
            a_  ,  kernel_size=1  ,  strides=a_  ,  use_bias=a_  ,  name="convolution"						)
        # Use same default momentum and epsilon as PyTorch equivalent
        _a					      =						tf.keras.layers.BatchNormalization(epsilon=1E-5  ,  momentum=0.9  ,  name="normalization"						)






      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = False						)						->tf.Tensor:







        '''simple docstring'''


        _a					      =						x
        _a					      =						self.convolution(a_						)
        _a					      =						self.normalization(a_  ,  training=a_						)
        return hidden_state







class  lowercase_     (tf.keras.layers.Layer  ):






      def __init__(     self  ,  a_  ,  a_  ,  a_ = 1  ,  a_ = "relu"  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(**a_						)
        _a					      =						in_channels != out_channels or stride != 1
        _a					      =						TFResNetConvLayer(a_  ,  stride=a_  ,  name="layer.0"						)
        _a					      =						TFResNetConvLayer(a_  ,  activation=a_  ,  name="layer.1"						)
        _a					      =						(
            TFResNetShortCut(a_  ,  stride=a_  ,  name="shortcut"						)
            if should_apply_shortcut
            else tf.keras.layers.Activation("linear"  ,  name="shortcut"						)
        )
        _a					      =						ACTaFN[activation]






      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = False						)						->tf.Tensor:







        '''simple docstring'''


        _a					      =						hidden_state
        _a					      =						self.conva(a_  ,  training=a_						)
        _a					      =						self.conva(a_  ,  training=a_						)
        _a					      =						self.shortcut(a_  ,  training=a_						)
        hidden_state += residual
        _a					      =						self.activation(a_						)
        return hidden_state







class  lowercase_     (tf.keras.layers.Layer  ):






      def __init__(     self  ,  a_  ,  a_  ,  a_ = 1  ,  a_ = "relu"  ,  a_ = 4  ,  **a_  ,  )						->None:







        '''simple docstring'''


        super().__init__(**a_						)
        _a					      =						in_channels != out_channels or stride != 1
        _a					      =						out_channels // reduction
        _a					      =						TFResNetConvLayer(a_  ,  kernel_size=1  ,  name="layer.0"						)
        _a					      =						TFResNetConvLayer(a_  ,  stride=a_  ,  name="layer.1"						)
        _a					      =						TFResNetConvLayer(a_  ,  kernel_size=1  ,  activation=a_  ,  name="layer.2"						)
        _a					      =						(
            TFResNetShortCut(a_  ,  stride=a_  ,  name="shortcut"						)
            if should_apply_shortcut
            else tf.keras.layers.Activation("linear"  ,  name="shortcut"						)
        )
        _a					      =						ACTaFN[activation]






      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = False						)						->tf.Tensor:







        '''simple docstring'''


        _a					      =						hidden_state
        _a					      =						self.conva(a_  ,  training=a_						)
        _a					      =						self.conva(a_  ,  training=a_						)
        _a					      =						self.conva(a_  ,  training=a_						)
        _a					      =						self.shortcut(a_  ,  training=a_						)
        hidden_state += residual
        _a					      =						self.activation(a_						)
        return hidden_state







class  lowercase_     (tf.keras.layers.Layer  ):






      def __init__(     self  ,  a_  ,  a_  ,  a_  ,  a_ = 2  ,  a_ = 2  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(**a_						)

        _a					      =						TFResNetBottleNeckLayer if config.layer_type == "bottleneck" else TFResNetBasicLayer

        _a					      =						[layer(a_  ,  a_  ,  stride=a_  ,  activation=config.hidden_act  ,  name="layers.0"						)]
        layers += [
            layer(a_  ,  a_  ,  activation=config.hidden_act  ,  name=f'''layers.{i + 1}'''						)
            for i in range(depth - 1						)
        ]
        _a					      =						layers






      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = False						)						->tf.Tensor:







        '''simple docstring'''


        for layer in self.stage_layers:
          _a					      =						layer(a_  ,  training=a_						)
        return hidden_state







class  lowercase_     (tf.keras.layers.Layer  ):






      def __init__(     self  ,  a_  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(**a_						)
        # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
        _a					      =						[
            TFResNetStage(
                a_  ,  config.embedding_size  ,  config.hidden_sizes[0]  ,  stride=2 if config.downsample_in_first_stage else 1  ,  depth=config.depths[0]  ,  name="stages.0"  ,  )
        ]
        for i, (in_channels, out_channels, depth) in enumerate(
            zip(config.hidden_sizes  ,  config.hidden_sizes[1:]  ,  config.depths[1:]						)						):
          self.stages.append(TFResNetStage(a_  ,  a_  ,  a_  ,  depth=a_  ,  name=f'''stages.{i + 1}'''						)						)






      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = False  ,  a_ = True  ,  a_ = False  ,  )						->TFBaseModelOutputWithNoAttention:







        '''simple docstring'''


        _a					      =						() if output_hidden_states else None

        for stage_module in self.stages:
          if output_hidden_states:
            _a					      =						hidden_states + (hidden_state,)

          _a					      =						stage_module(a_  ,  training=a_						)

        if output_hidden_states:
          _a					      =						hidden_states + (hidden_state,)

        if not return_dict:
          return tuple(v for v in [hidden_state, hidden_states] if v is not None						)

        return TFBaseModelOutputWithNoAttention(last_hidden_state=a_  ,  hidden_states=a_						)







class  lowercase_     (_UpperCAmelCase  ):
      A__						:		str							      =					ResNetConfig
      A__						:		int							      =					'''resnet'''
      A__						:		List[str]							      =					'''pixel_values'''






      @property
      def       lowerCamelCase__     (     self						)						->Union[str, Any]:







        '''simple docstring'''


        return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_2_4, 2_2_4)  ,  dtype=tf.floataa						)}







UpperCamelCase											=     r"""
    This model is a TensorFlow
    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a
    regular TensorFlow Module and refer to the TensorFlow documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
"""


UpperCamelCase											=     r"""
    Args:
        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""




@keras_serializable
class  lowercase_     (tf.keras.layers.Layer  ):
      A__						:		Dict							      =					ResNetConfig






      def __init__(     self  ,  a_  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(**a_						)
        _a					      =						config
        _a					      =						TFResNetEmbeddings(a_  ,  name="embedder"						)
        _a					      =						TFResNetEncoder(a_  ,  name="encoder"						)
        _a					      =						tf.keras.layers.GlobalAveragePoolingaD(keepdims=a_						)






      @unpack_inputs
      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = None  ,  a_ = None  ,  a_ = False  ,  )						->Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:







        '''simple docstring'''


        _a					      =						(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        _a					      =						return_dict if return_dict is not None else self.config.use_return_dict

        # TF 2.0 image layers can't use NCHW format when running on CPU.
        # We transpose to NHWC format and then transpose back after the full forward pass.
        # (batch_size, num_channels, height, width) -> (batch_size, height, width, num_channels)
        _a					      =						tf.transpose(a_  ,  perm=[0, 2, 3, 1]						)
        _a					      =						self.embedder(a_  ,  training=a_						)

        _a					      =						self.encoder(
            a_  ,  output_hidden_states=a_  ,  return_dict=a_  ,  training=a_						)

        _a					      =						encoder_outputs[0]

        _a					      =						self.pooler(a_						)

        # Transpose all the outputs to the NCHW format
        # (batch_size, height, width, num_channels) -> (batch_size, num_channels, height, width)
        _a					      =						tf.transpose(a_  ,  (0, 3, 1, 2)						)
        _a					      =						tf.transpose(a_  ,  (0, 3, 1, 2)						)
        _a					      =						()
        for hidden_state in encoder_outputs[1:]:
          _a					      =						hidden_states + tuple(tf.transpose(a_  ,  (0, 3, 1, 2)						) for h in hidden_state						)

        if not return_dict:
          return (last_hidden_state, pooled_output) + hidden_states

        _a					      =						hidden_states if output_hidden_states else None

        return TFBaseModelOutputWithPoolingAndNoAttention(
            last_hidden_state=a_  ,  pooler_output=a_  ,  hidden_states=a_  ,  )







@add_start_docstrings(
    '''The bare ResNet model outputting raw features without any specific head on top.''',     _UpperCAmelCase,     )
class  lowercase_     (_UpperCAmelCase  ):






      def __init__(     self  ,  a_  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(a_  ,  **a_						)
        _a					      =						TFResNetMainLayer(config=a_  ,  name="resnet"						)






      @add_start_docstrings_to_model_forward(a_						)
      @add_code_sample_docstrings(
          checkpoint=_CHECKPOINT_FOR_DOC  ,  output_type=a_  ,  config_class=_CONFIG_FOR_DOC  ,  modality="vision"  ,  expected_output=_EXPECTED_OUTPUT_SHAPE  ,  )
      @unpack_inputs
      def       lowerCamelCase__     (     self  ,  a_  ,  a_ = None  ,  a_ = None  ,  a_ = False  ,  )						->Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:







        '''simple docstring'''


        _a					      =						(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        _a					      =						return_dict if return_dict is not None else self.config.use_return_dict

        _a					      =						self.resnet(
            pixel_values=a_  ,  output_hidden_states=a_  ,  return_dict=a_  ,  training=a_  ,  )
        return resnet_outputs







@add_start_docstrings(
    '''
    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    ''',     _UpperCAmelCase,     )
class  lowercase_     (_UpperCAmelCase,     _UpperCAmelCase  ):






      def __init__(     self  ,  a_  ,  **a_						)						->None:







        '''simple docstring'''


        super().__init__(a_  ,  **a_						)
        _a					      =						config.num_labels
        _a					      =						TFResNetMainLayer(a_  ,  name="resnet"						)
        # classification head
        _a					      =						(
            tf.keras.layers.Dense(config.num_labels  ,  name="classifier.1"						)
            if config.num_labels > 0
            else tf.keras.layers.Activation("linear"  ,  name="classifier.1"						)
        )






      def       lowerCamelCase__     (     self  ,  a_						)						->tf.Tensor:







        '''simple docstring'''


        _a					      =						tf.keras.layers.Flatten()(a_						)
        _a					      =						self.classifier_layer(a_						)
        return logits






      @add_start_docstrings_to_model_forward(a_						)
      @add_code_sample_docstrings(
          checkpoint=_IMAGE_CLASS_CHECKPOINT  ,  output_type=a_  ,  config_class=_CONFIG_FOR_DOC  ,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT  ,  )
      @unpack_inputs
      def       lowerCamelCase__     (     self  ,  a_ = None  ,  a_ = None  ,  a_ = None  ,  a_ = None  ,  a_ = False  ,  )						->Union[Tuple[tf.Tensor], TFImageClassifierOutputWithNoAttention]:







        '''simple docstring'''


        _a					      =						return_dict if return_dict is not None else self.config.use_return_dict

        _a					      =						self.resnet(
            a_  ,  output_hidden_states=a_  ,  return_dict=a_  ,  training=a_						)

        _a					      =						outputs.pooler_output if return_dict else outputs[1]

        _a					      =						self.classifier(a_						)

        _a					      =						None if labels is None else self.hf_compute_loss(a_  ,  a_						)

        if not return_dict:
          _a					      =						(logits,) + outputs[2:]
          return (loss,) + output if loss is not None else output

        return TFImageClassifierOutputWithNoAttention(loss=a_  ,  logits=a_  ,  hidden_states=outputs.hidden_states						)










"""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  ,  a_  ,  a_=1_3  ,  a_=3_2  ,  a_=1  ,  a_=3  ,  a_=1_6  ,  a_=[1, 2, 1]  ,  a_=[2, 2, 4]  ,  a_=2  ,  a_=2.0  ,  a_=True  ,  a_=0.0  ,  a_=0.0  ,  a_=0.1  ,  a_="gelu"  ,  a_=False  ,  a_=True  ,  a_=0.02  ,  a_=1E-5  ,  a_=True  ,  a_=None  ,  a_=False  ,  a_=2  ,  )						->str:







        '''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(a_						)
        _a					      =						embed_dim
        _a					      =						(image_size // patch_size) ** 2






      def       lowerCamelCase__     (     self						)						->Any:







        '''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       lowerCamelCase__     (     self						)						->Union[str, Any]:







        '''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       lowerCamelCase__     (     self  ,  a_  ,  a_  ,  a_						)						->Union[str, Any]:







        '''simple docstring'''


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

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






      def       lowerCamelCase__     (     self  ,  a_  ,  a_  ,  a_						)						->Optional[int]:







        '''simple docstring'''


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

        _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       lowerCamelCase__     (     self						)						->Tuple:







        '''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  lowercase_     (_UpperCAmelCase,     _UpperCAmelCase,     unittest.TestCase  ):
      A__						:		str							      =					(SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
      A__						:		Optional[Any]							      =					{'''feature-extraction''': SwinaSRModel} if is_torch_available() else {}

      A__						:		List[str]							      =					False
      A__						:		Optional[Any]							      =					False
      A__						:		str							      =					False
      A__						:		Optional[int]							      =					False
      A__						:		List[Any]							      =					False






      def       lowerCamelCase__     (     self						)						->Union[str, Any]:







        '''simple docstring'''


        _a					      =						SwinaSRModelTester(self						)
        _a					      =						ConfigTester(self  ,  config_class=a_  ,  embed_dim=3_7						)






      def       lowerCamelCase__     (     self						)						->Union[str, Any]:







        '''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       lowerCamelCase__     (     self						)						->int:







        '''simple docstring'''


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






      def       lowerCamelCase__     (     self						)						->Optional[int]:







        '''simple docstring'''


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







        '''simple docstring'''


        pass






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







        '''simple docstring'''


        pass






      @unittest.skip(reason="Swin2SR does not support training yet"						)
      def       lowerCamelCase__     (     self						)						->Union[str, Any]:







        '''simple docstring'''


        pass






      @unittest.skip(reason="Swin2SR does not support training yet"						)
      def       lowerCamelCase__     (     self						)						->Any:







        '''simple docstring'''


        pass






      def       lowerCamelCase__     (     self						)						->Tuple:







        '''simple docstring'''


        _a      ,						_a					      =						self.model_tester.prepare_config_and_inputs_for_common()

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






      def       lowerCamelCase__     (     self						)						->Optional[Any]:







        '''simple docstring'''


        _a      ,						_a					      =						self.model_tester.prepare_config_and_inputs_for_common()

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






      @slow
      def       lowerCamelCase__     (     self						)						->Dict:







        '''simple docstring'''


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






      def       lowerCamelCase__     (     self						)						->Dict:







        '''simple docstring'''


        _a      ,						_a					      =						self.model_tester.prepare_config_and_inputs_for_common()

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







        '''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(a_						)
          model.to(a_						)
          model.eval()
          with torch.no_grad():
            _a					      =						model(**self._prepare_for_class(a_  ,  a_						)						)
          _a					      =						outputs.attentions
          _a					      =						len(self.model_tester.depths						)
          self.assertEqual(len(a_						)  ,  a_						)

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

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

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

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






      def       lowerCamelCase__     (     self						)						->Optional[Any]:







        '''simple docstring'''


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

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

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

        # verify the logits
        _a					      =						torch.Size([1, 3, 9_7_6, 1_2_9_6]						)
        self.assertEqual(outputs.reconstruction.shape  ,  a_						)
        _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(a_						)
        self.assertTrue(torch.allclose(outputs.reconstruction[0, 0, :3, :3]  ,  a_  ,  atol=1E-4						)						)


