





'''simple docstring'''






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers.models.swin.modeling_tf_swin import (
         TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFSwinForImageClassification,
         TFSwinForMaskedImageModeling,
         TFSwinModel,
     )


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor


class 	_lowercase    :


   def __init__(			self     , _UpperCAmelCase     , _UpperCAmelCase=13     , _UpperCAmelCase=32     , _UpperCAmelCase=2     , _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=True     , _UpperCAmelCase=10     , _UpperCAmelCase=8     , ):
       A							:							Union[str, Any]				=    parent
       A							:							str				=    batch_size
       A							:							Optional[Any]				=    image_size
       A							:							str				=    patch_size
       A							:							int				=    num_channels
       A							:							Optional[Any]				=    embed_dim
       A							:							Optional[int]				=    depths
       A							:							Dict				=    num_heads
       A							:							Optional[int]				=    window_size
       A							:							Any				=    mlp_ratio
       A							:							Tuple				=    qkv_bias
       A							:							List[Any]				=    hidden_dropout_prob
       A							:							Dict				=    attention_probs_dropout_prob
       A							:							Tuple				=    drop_path_rate
       A							:							List[str]				=    hidden_act
       A							:							int				=    use_absolute_embeddings
       A							:							Tuple				=    patch_norm
       A							:							Union[str, Any]				=    layer_norm_eps
       A							:							Union[str, Any]				=    initializer_range
       A							:							Optional[int]				=    is_training
       A							:							Optional[int]				=    scope
       A							:							Dict				=    use_labels
       A							:							List[Any]				=    type_sequence_label_size
       A							:							Tuple				=    encoder_stride


   def        snake_case					(			self       ):
       A							:							Optional[Any]				=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

       A							:							Optional[int]				=    None
       if self.use_labels:
           A							:							Tuple				=    ids_tensor([self.batch_size]     , self.type_sequence_label_size       )

       A							:							Union[str, Any]				=    self.get_config()

       return config, pixel_values, labels


   def        snake_case					(			self       ):
       return SwinConfig(
           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     , encoder_stride=self.encoder_stride     , )


   def        snake_case					(			self     , _UpperCAmelCase     , _UpperCAmelCase     , _UpperCAmelCase       ):
       A							:							List[str]				=    TFSwinModel(config=lowercase_       )
       A							:							Dict				=    model(lowercase_       )

       A							:							Dict				=    ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
       A							:							Union[str, Any]				=    int(config.embed_dim * 2 ** (len(config.depths       ) - 1)       )

       self.parent.assertEqual(result.last_hidden_state.shape     , (self.batch_size, expected_seq_len, expected_dim)       )


   def        snake_case					(			self     , _UpperCAmelCase     , _UpperCAmelCase     , _UpperCAmelCase       ):
       A							:							str				=    TFSwinForMaskedImageModeling(config=lowercase_       )
       A							:							Optional[Any]				=    model(lowercase_       )
       self.parent.assertEqual(
           result.logits.shape     , (self.batch_size, self.num_channels, self.image_size, self.image_size)       )

       # test greyscale images
       A							:							Dict				=    1
       A							:							str				=    TFSwinForMaskedImageModeling(lowercase_       )

       A							:							Dict				=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
       A							:							List[Any]				=    model(lowercase_       )
       self.parent.assertEqual(result.logits.shape     , (self.batch_size, 1, self.image_size, self.image_size)       )


   def        snake_case					(			self     , _UpperCAmelCase     , _UpperCAmelCase     , _UpperCAmelCase       ):
       A							:							Union[str, Any]				=    self.type_sequence_label_size
       A							:							Dict				=    TFSwinForImageClassification(lowercase_       )
       A							:							Optional[int]				=    model(lowercase_     , labels=lowercase_       )
       self.parent.assertEqual(result.logits.shape     , (self.batch_size, self.type_sequence_label_size)       )

       # test greyscale images
       A							:							List[str]				=    1
       A							:							str				=    TFSwinForImageClassification(lowercase_       )
       A							:							Optional[Any]				=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
       A							:							List[Any]				=    model(lowercase_       )
       self.parent.assertEqual(result.logits.shape     , (self.batch_size, self.type_sequence_label_size)       )


   def        snake_case					(			self       ):
       A							:							Optional[int]				=    self.prepare_config_and_inputs()
       A							:							List[Any]				=    config_and_inputs
       A							:							str				=    {'pixel_values': pixel_values}
       return config, inputs_dict







@require_tf
class 	_lowercase    (				_snake_case		,       _snake_case		,       unittest.TestCase					):
   _UpperCamelCase									= (
       (
           TFSwinModel,
           TFSwinForImageClassification,
           TFSwinForMaskedImageModeling,
       )
       if is_tf_available()
       else ()
   )
   _UpperCamelCase									= (
       {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
       if is_tf_available()
       else {}
   )

   _UpperCamelCase									= False
   _UpperCamelCase									= False
   _UpperCamelCase									= False
   _UpperCamelCase									= False


   def        snake_case					(			self       ):
       A							:							List[Any]				=    TFSwinModelTester(self       )
       A							:							Optional[int]				=    ConfigTester(self     , config_class=lowercase_     , embed_dim=37       )


   def        snake_case					(			self       ):
       self.create_and_test_config_common_properties()
       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        snake_case					(			self       ):
       return


   def        snake_case					(			self       ):
       A							:							List[Any]				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowercase_       )


   def        snake_case					(			self       ):
       A							:							Optional[int]				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*lowercase_       )


   def        snake_case					(			self       ):
       A							:							Optional[Any]				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*lowercase_       )


   @unittest.skip(reason='''Swin does not use inputs_embeds'''       )
   def        snake_case					(			self       ):
       pass


   def        snake_case					(			self       ):
       A							:							Optional[Any]				=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           A							:							Optional[Any]				=    model_class(lowercase_       )
           self.assertIsInstance(model.get_input_embeddings()     , tf.keras.layers.Layer       )
           A							:							List[str]				=    model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(lowercase_     , tf.keras.layers.Dense       )       )


   def        snake_case					(			self       ):
       A							:							Any				=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           A							:							List[Any]				=    model_class(lowercase_       )
           A							:							Optional[Any]				=    inspect.signature(model.call       )
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           A							:							Any				=    [*signature.parameters.keys()]

           A							:							Tuple				=    ['pixel_values']
           self.assertListEqual(arg_names[:1]     , lowercase_       )


   def        snake_case					(			self       ):
       A							:							int				=    self.model_tester.prepare_config_and_inputs_for_common()
       A							:							Optional[int]				=    True

       for model_class in self.all_model_classes:
           A							:							Optional[Any]				=    True
           A							:							List[Any]				=    False
           A							:							List[str]				=    True
           A							:							List[Any]				=    model_class(lowercase_       )
           A							:							Optional[Any]				=    model(**self._prepare_for_class(lowercase_     , lowercase_       )       )
           A							:							int				=    outputs.attentions
           A							:							str				=    len(self.model_tester.depths       )
           self.assertEqual(len(lowercase_       )     , lowercase_       )

           # check that output_attentions also work using config
           del inputs_dict["output_attentions"]
           A							:							str				=    True
           A							:							Tuple				=    config.window_size**2
           A							:							Union[str, Any]				=    model_class(lowercase_       )
           A							:							Optional[Any]				=    model(**self._prepare_for_class(lowercase_     , lowercase_       )       )
           A							:							Optional[int]				=    outputs.attentions
           self.assertEqual(len(lowercase_       )     , lowercase_       )

           self.assertListEqual(
               list(attentions[0].shape[-3:]       )     , [self.model_tester.num_heads[0], window_size_squared, window_size_squared]     , )
           A							:							Optional[int]				=    len(lowercase_       )

           # Check attention is always last and order is fine
           A							:							str				=    True
           A							:							Optional[int]				=    True
           A							:							Dict				=    model_class(lowercase_       )
           A							:							List[str]				=    model(**self._prepare_for_class(lowercase_     , lowercase_       )       )

           if hasattr(self.model_tester     , '''num_hidden_states_types'''       ):
               A							:							Optional[int]				=    self.model_tester.num_hidden_states_types
           else:
               # also another +1 for reshaped_hidden_states
               A							:							Tuple				=    2
           self.assertEqual(out_len + added_hidden_states     , len(lowercase_       )       )

           A							:							Union[str, Any]				=    outputs.attentions

           self.assertEqual(len(lowercase_       )     , lowercase_       )

           self.assertListEqual(
               list(self_attentions[0].shape[-3:]       )     , [self.model_tester.num_heads[0], window_size_squared, window_size_squared]     , )


   def        snake_case					(			self     , _UpperCAmelCase     , _UpperCAmelCase     , _UpperCAmelCase     , _UpperCAmelCase       ):
       A							:							Tuple				=    model_class(lowercase_       )
       A							:							Optional[Any]				=    model(**self._prepare_for_class(lowercase_     , lowercase_       )       )
       A							:							List[str]				=    outputs.hidden_states

       A							:							Any				=    getattr(
           self.model_tester     , '''expected_num_hidden_layers'''     , len(self.model_tester.depths       ) + 1       )
       self.assertEqual(len(lowercase_       )     , lowercase_       )

       # Swin has a different seq_length
       A							:							Union[str, Any]				=    to_atuple(config.patch_size       )

       A							:							Dict				=    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

       self.assertListEqual(
           list(hidden_states[0].shape[-2:]       )     , [num_patches, self.model_tester.embed_dim]     , )

       A							:							Dict				=    outputs.reshaped_hidden_states
       self.assertEqual(len(lowercase_       )     , lowercase_       )

       A							:							Optional[int]				=    reshaped_hidden_states[0].shape

       A							:							List[str]				=    tf.reshape(reshaped_hidden_states[0]     , (batch_size, num_channels, height * width)       )
       A							:							Optional[Any]				=    tf.transpose(lowercase_     , (0, 2, 1)       )

       self.assertListEqual(
           list(reshaped_hidden_states.shape[-2:]       )     , [num_patches, self.model_tester.embed_dim]     , )


   def        snake_case					(			self       ):
       A							:							Dict				=    self.model_tester.prepare_config_and_inputs_for_common()

       A							:							Tuple				=    to_atuple(self.model_tester.image_size       )

       for model_class in self.all_model_classes:
           A							:							Dict				=    True
           self.check_hidden_states_output(lowercase_     , lowercase_     , lowercase_     , lowercase_       )

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           A							:							Tuple				=    True

           self.check_hidden_states_output(lowercase_     , lowercase_     , lowercase_     , lowercase_       )


   def        snake_case					(			self       ):
       A							:							Optional[Any]				=    self.model_tester.prepare_config_and_inputs_for_common()
       A							:							Optional[Any]				=    3

       A							:							List[Any]				=    to_atuple(self.model_tester.image_size       )
       A							:							List[Any]				=    to_atuple(config.patch_size       )

       A							:							Union[str, Any]				=    image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
       A							:							Dict				=    image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

       for model_class in self.all_model_classes:
           A							:							Tuple				=    True
           self.check_hidden_states_output(lowercase_     , lowercase_     , lowercase_     , (padded_height, padded_width)       )

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           A							:							int				=    True
           self.check_hidden_states_output(lowercase_     , lowercase_     , lowercase_     , (padded_height, padded_width)       )


   @slow
   def        snake_case					(			self       ):
       for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           A							:							Any				=    TFSwinModel.from_pretrained(lowercase_       )
           self.assertIsNotNone(lowercase_       )







@require_vision
@require_tf
class 	_lowercase    (				unittest.TestCase					):


   @cached_property
   def        snake_case					(			self       ):
       return (
           AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''       )
           if is_vision_available()
           else None
       )


   @slow
   def        snake_case					(			self       ):
       A							:							List[str]				=    TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''       )
       A							:							Optional[int]				=    self.default_image_processor

       A							:							Union[str, Any]				=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
       A							:							int				=    image_processor(images=lowercase_     , return_tensors='''tf'''       )

       # forward pass
       A							:							Dict				=    model(lowercase_       )

       # verify the logits
       A							:							Union[str, Any]				=    tf.TensorShape((1, 1_000)       )
       self.assertEqual(outputs.logits.shape     , lowercase_       )
       A							:							Optional[int]				=    tf.constant([-0.0948, -0.6454, -0.0921]       )
       self.assertTrue(np.allclose(outputs.logits[0, :3]     , lowercase_     , atol=1E-4       )       )



'''simple docstring'''

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase			:      Optional[Any]		   =					pt_weights[F"""{prefix}.in_proj_weight"""].chunk(3    ,			dim=0      )
 UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase			:      str		   =					pt_weights[F"""{prefix}.in_proj_bias"""].chunk(3    ,			dim=0      )

 UpperCAmelCase			:      Dict		   =					pt_weights[F"""{prefix}.out_proj.weight"""]
 UpperCAmelCase			:      Tuple		   =					pt_weights[F"""{prefix}.out_proj.bias"""]

 UpperCAmelCase			:      Dict		   =					q_proj
 UpperCAmelCase			:      Optional[Any]		   =					q_proj_bias

 UpperCAmelCase			:      Union[str, Any]		   =					k_proj
 UpperCAmelCase			:      Optional[Any]		   =					k_proj_bias

 UpperCAmelCase			:      Dict		   =					v_proj
 UpperCAmelCase			:      Tuple		   =					v_proj_bias

 UpperCAmelCase			:      List[Any]		   =					out_proj_weights
 UpperCAmelCase			:      Any		   =					out_proj_bias




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 copy_linear(hf_mlp.fca    ,			UpperCAmelCase_    ,			F"""{prefix}.c_fc"""      )
 copy_linear(hf_mlp.fca    ,			UpperCAmelCase_    ,			F"""{prefix}.c_proj"""      )




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 UpperCAmelCase			:      Tuple		   =					pt_weights[F"""{prefix}.weight"""].data
 UpperCAmelCase			:      Dict		   =					pt_weights[F"""{prefix}.bias"""].data




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 # copy layer norms
 copy_linear(hf_layer.layer_norma    ,			UpperCAmelCase_    ,			F"""{prefix}.ln_1"""      )
 copy_linear(hf_layer.layer_norma    ,			UpperCAmelCase_    ,			F"""{prefix}.ln_2"""      )

 # copy MLP
 copy_mlp(hf_layer.mlp    ,			UpperCAmelCase_    ,			F"""{prefix}.mlp"""      )

 # copy attn
 copy_attn_layer(hf_layer.self_attn    ,			UpperCAmelCase_    ,			F"""{prefix}.attn"""      )




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 for layer_id, hf_layer in enumerate(UpperCAmelCase_      ):
  copy_layer(UpperCAmelCase_    ,			UpperCAmelCase_    ,			F"""{prefix}.{layer_id}"""      )




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_      ):
 # copy projection
 UpperCAmelCase			:      Optional[int]		   =					pt_weights['text_projection'].data.T

 # copy text encoder
 for name, param in hf_model.text_model.named_parameters():
  UpperCAmelCase			:      int		   =					pt_weights[F"""bert.{name}"""].data




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_      ):
 # copy projection
 UpperCAmelCase			:      Optional[int]		   =					pt_weights['visual.proj'].data.T

 # copy layer norms
 copy_linear(hf_model.vision_model.pre_layrnorm    ,			UpperCAmelCase_    ,			'visual.ln_pre'      )
 copy_linear(hf_model.vision_model.post_layernorm    ,			UpperCAmelCase_    ,			'visual.ln_post'      )

 # copy embeddings
 UpperCAmelCase			:      List[str]		   =					pt_weights['visual.conv1.weight'].data
 UpperCAmelCase			:      Tuple		   =					pt_weights['visual.class_embedding'].data
 UpperCAmelCase			:      str		   =					pt_weights['visual.positional_embedding'].data

 # copy encoder
 copy_layers(hf_model.vision_model.encoder.layers    ,			UpperCAmelCase_    ,			'visual.transformer.resblocks'      )




@torch.no_grad()
def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_=None      ):
 assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
 UpperCAmelCase			:      str		   =					ChineseCLIPConfig.from_pretrained(UpperCAmelCase_      )

 UpperCAmelCase			:      List[Any]		   =					ChineseCLIPModel(UpperCAmelCase_      ).eval()

 UpperCAmelCase			:      Optional[Any]		   =					torch.load(UpperCAmelCase_    ,			map_location='cpu'      )['state_dict']
 UpperCAmelCase			:      Any		   =					{(name[7:] if name.startswith('module.'      ) else name): value for name, value in pt_weights.items()}

 copy_text_model_and_projection(UpperCAmelCase_    ,			UpperCAmelCase_      )
 copy_vision_model_and_projection(UpperCAmelCase_    ,			UpperCAmelCase_      )
 UpperCAmelCase			:      Optional[int]		   =					pt_weights['logit_scale'].data

 hf_model.save_pretrained(UpperCAmelCase_      )


if __name__ == "__main__":
 lowercase__            =     argparse.ArgumentParser()
 parser.add_argument(
     "--pytorch_dump_folder_path",
     default=None,
     type=str,
     help="Path to the output folder storing converted hf PyTorch model.",
 )
 parser.add_argument(
     "--checkpoint_path", default=None, type=str, help="Path to original github format ChineseCLIP checkpoint."
 )
 parser.add_argument(
     "--config_path", default=None, required=True, type=str, help="Path to hf config.json of model to convert."
 )
 lowercase__            =     parser.parse_args()

 convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
 print("The conversion is finished!")




