


'''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 					lowerCAmelCase_     :






       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 ,    )->			Union[str, Any]:
            _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      UpperCamelCase	(   self      )->			Union[str, Any]:
            _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      UpperCamelCase	(   self      )->			Dict:
            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 ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			Dict:
            _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      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			List[Any]:
            _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      UpperCamelCase	(   self      )->			str:
            _A				=       self.prepare_config_and_inputs()
            _A		,							_A		,							_A				=       config_and_inputs
            _A				=       {'pixel_values': pixel_values}
            return config, inputs_dict






@require_torch
class 					lowerCAmelCase_     (    UpperCAmelCase			,       UpperCAmelCase			,       unittest.TestCase  ):
       __UpperCAmelCase				=(SwinaSRModel, SwinaSRForImageSuperResolution) if is_torch_available() else ()
       __UpperCAmelCase				={'feature-extraction': SwinaSRModel} if is_torch_available() else {}

       __UpperCAmelCase				=False
       __UpperCAmelCase				=False
       __UpperCAmelCase				=False
       __UpperCAmelCase				=False
       __UpperCAmelCase				=False






       def      UpperCamelCase	(   self      )->			Optional[int]:
            _A				=       SwinaSRModelTester(self      )
            _A				=       ConfigTester(self ,    config_class=_UpperCamelCase ,    embed_dim=37      )






       def      UpperCamelCase	(   self      )->			List[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      )->			int:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*_UpperCamelCase      )






       def      UpperCamelCase	(   self      )->			Tuple:
            _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      UpperCamelCase	(   self      )->			Optional[Any]:
            pass






       @unittest.skip(reason='Swin2SR does not use inputs_embeds'      )
       def      UpperCamelCase	(   self      )->			str:
            pass






       @unittest.skip(reason='Swin2SR does not support training yet'      )
       def      UpperCamelCase	(   self      )->			Optional[Any]:
            pass






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






       def      UpperCamelCase	(   self      )->			Dict:
            _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      UpperCamelCase	(   self      )->			int:
            _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      UpperCamelCase	(   self      )->			int:
            for model_name in SWIN2SR_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _A				=       SwinaSRModel.from_pretrained(_UpperCamelCase      )
                 self.assertIsNotNone(_UpperCamelCase      )






       def      UpperCamelCase	(   self      )->			List[str]:
            _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      UpperCamelCase	(   self      )->			Dict:
            _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 					lowerCAmelCase_     (    unittest.TestCase  ):






       def      UpperCamelCase	(   self      )->			int:
            _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, 1296]      )
            self.assertEqual(outputs.reconstruction.shape ,    _UpperCamelCase      )
            _A				=       torch.tensor(
                [[0.5458, 0.5546, 0.5638], [0.5526, 0.5565, 0.5651], [0.5396, 0.5426, 0.5621]]      ).to(_UpperCamelCase      )
            self.assertTrue(torch.allclose(outputs.reconstruction[0, 0, :3, :3] ,    _UpperCamelCase ,    atol=1e-4      )      )









'''simple docstring'''
import argparse

from . import (
    BertConfig,
    BertGenerationConfig,
    BertGenerationDecoder,
    BertGenerationEncoder,
    load_tf_weights_in_bert_generation,
    logging,
)


logging.set_verbosity_info()

def 				lowerCamelCase_   (   __UpperCamelCase				:					Tuple       ,						__UpperCamelCase				:					Any       ,						__UpperCamelCase				:					Dict       ,						__UpperCamelCase				:					List[str]       ,						__UpperCamelCase				:					str		)				->  List[str]:







     """simple docstring"""
     # Initialise PyTorch model
     _A				=       BertConfig.from_pretrained(
         'bert-large-cased'       ,						vocab_size=__UpperCamelCase       ,						max_position_embeddings=5_1_2       ,						is_decoder=__UpperCamelCase       ,						add_cross_attention=__UpperCamelCase       ,						)
     _A				=       bert_config.to_dict()
     del bert_config_dict["type_vocab_size"]
     _A				=       BertGenerationConfig(**__UpperCamelCase		)
     if is_encoder:
          _A				=       BertGenerationEncoder(__UpperCamelCase		)
     else:
          _A				=       BertGenerationDecoder(__UpperCamelCase		)
     print(F'Building PyTorch model from configuration: {config}'		)

     # Load weights from tf checkpoint
     load_tf_weights_in_bert_generation(
         __UpperCamelCase       ,						__UpperCamelCase       ,						model_class='bert'       ,						is_encoder_named_decoder=__UpperCamelCase       ,						is_encoder=__UpperCamelCase       ,						)

     # Save pytorch-model
     print(F'Save PyTorch model and config to {pytorch_dump_path}'		)
     model.save_pretrained(__UpperCamelCase		)


if __name__ == "__main__":
    lowerCAmelCase  =				argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        """--tf_hub_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
    )
    parser.add_argument(
        """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
    )
    parser.add_argument(
        """--is_encoder_named_decoder""",
        action="""store_true""",
        help="""If decoder has to be renamed to encoder in PyTorch model.""",
    )
    parser.add_argument("""--is_encoder""", action="""store_true""", help="""If model is an encoder.""")
    parser.add_argument("""--vocab_size""", default=50_358, type=int, help="""Vocab size of model""")
    lowerCAmelCase  =				parser.parse_args()
    convert_tf_checkpoint_to_pytorch(
        args.tf_hub_path,
        args.pytorch_dump_path,
        args.is_encoder_named_decoder,
        args.vocab_size,
        is_encoder=args.is_encoder,
    )





