






"""simple docstring"""





import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel



def      __lowercase       (     a			:					Dict							,		a			:					Any			)       ->       Optional[Any]:
      __snake_case      ,					__snake_case      ,					__snake_case   :     Any	      =pt_attn_layer.in_proj_weight.chunk(3							,		dim=0			)
      __snake_case      ,					__snake_case      ,					__snake_case   :     Optional[Any]	      =pt_attn_layer.in_proj_bias.chunk(3							,		dim=0			)

      __snake_case   :     Any	      =pt_attn_layer.out_proj.weight
      __snake_case   :     Union[str, Any]	      =pt_attn_layer.out_proj.bias

      __snake_case   :     Tuple	      =q_proj
      __snake_case   :     Any	      =q_proj_bias

      __snake_case   :     Tuple	      =k_proj
      __snake_case   :     List[Any]	      =k_proj_bias

      __snake_case   :     Any	      =v_proj
      __snake_case   :     Optional[Any]	      =v_proj_bias

      __snake_case   :     Union[str, Any]	      =out_proj_weights
      __snake_case   :     Optional[int]	      =out_proj_bias



def      __lowercase       (     a			:					List[str]							,		a			:					Optional[int]			)       ->       Optional[int]:
      copy_linear(hf_mlp.fca							,		pt_mlp.c_fc			)
      copy_linear(hf_mlp.fca							,		pt_mlp.c_proj			)



def      __lowercase       (     a			:					List[str]							,		a			:					Optional[Any]			)       ->       Optional[Any]:
      __snake_case   :     Any	      =pt_linear.weight
      __snake_case   :     Dict	      =pt_linear.bias



def      __lowercase       (     a			:					Dict							,		a			:					str			)       ->       Dict:
      # copy layer norms
      copy_linear(hf_layer.layer_norma							,		pt_layer.ln_a			)
      copy_linear(hf_layer.layer_norma							,		pt_layer.ln_a			)

      # copy MLP
      copy_mlp(hf_layer.mlp							,		pt_layer.mlp			)

      # copy attn
      copy_attn_layer(hf_layer.self_attn							,		pt_layer.attn			)



def      __lowercase       (     a			:					int							,		a			:					Dict			)       ->       Any:
      for hf_layer, pt_layer in zip(a							,		a			):
            copy_layer(a							,		a			)



def      __lowercase       (     a			:					Tuple							,		a			:					Dict			)       ->       Optional[int]:
      # copy  embeds
      __snake_case   :     List[str]	      =pt_model.token_embedding.weight
      __snake_case   :     Any	      =pt_model.positional_embedding

      # copy layer norm
      copy_linear(hf_encoder.final_layer_norm							,		pt_model.ln_final			)

      # copy hidden layers
      copy_layers(hf_encoder.encoder.layers							,		pt_model.transformer.resblocks			)



def      __lowercase       (     a			:					int							,		a			:					Tuple			)       ->       Tuple:
      # copy projection
      __snake_case   :     str	      =pt_model.text_projection.data.T

      # copy text encoder
      copy_encoder(hf_model.text_model							,		a			)



def      __lowercase       (     a			:					Dict							,		a			:					List[Any]			)       ->       List[str]:
      # copy projection
      __snake_case   :     Dict	      =pt_model.visual.proj.data.T

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

      # copy embeds
      __snake_case   :     str	      =pt_model.visual.conva.weight.data
      __snake_case   :     Dict	      =pt_model.visual.class_embedding
      __snake_case   :     Optional[int]	      =pt_model.visual.positional_embedding.data

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



@torch.no_grad()
def      __lowercase       (     a			:					Union[str, Any]							,		a			:					int							,		a			:					Optional[Any]=None			)       ->       Optional[Any]:
      if config_path is not None:
            __snake_case   :     List[Any]	      =CLIPConfig.from_pretrained(a			)
      else:
            __snake_case   :     Optional[int]	      =CLIPConfig(projection_dim=512							,		text_config={}							,		vision_config={}			)

      __snake_case   :     List[str]	      =CLIPModel(a			).eval()

      __snake_case      ,					__snake_case   :     List[Any]	      =load(a							,		device='''cpu'''							,		jit=a			)
      __snake_case   :     Union[str, Any]	      =pt_model.eval()

      copy_text_model_and_projection(a							,		a			)
      copy_vison_model_and_projection(a							,		a			)
      __snake_case   :     Tuple	      =pt_model.logit_scale

      __snake_case   :     Dict	      =torch.arange(0							,		77			).unsqueeze(0			)
      __snake_case   :     Optional[Any]	      =torch.randn(1							,		3							,		224							,		224			)

      __snake_case   :     Tuple	      =hf_model(input_ids=a							,		pixel_values=a							,		return_dict=a			)
      __snake_case   :     List[str]	      =hf_outputs.logits_per_image
      __snake_case   :     List[Any]	      =hf_outputs.logits_per_text
      __snake_case      ,					__snake_case   :     Optional[Any]	      =pt_model(a							,		a			)

      assert torch.allclose(a							,		a							,		atol=1e-3			)
      assert torch.allclose(a							,		a							,		atol=1e-3			)

      hf_model.save_pretrained(a			)


if __name__ == "__main__":
    UpperCamelCase_						:					Tuple      				=			argparse.ArgumentParser()
    parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
    parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""")
    parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
    UpperCamelCase_						:					Dict      				=			parser.parse_args()

    convert_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)










"""simple docstring"""





class   _lowercase			:


      def __init__(     self					:						Optional[Any]				, a					:						Union[str, Any]    ):





            """simple docstring"""







            __snake_case   :     Union[str, Any]	      =data
            __snake_case   :     Optional[Any]	      =None
            __snake_case   :     Any	      =None







      def __str__(     self					:						Any    ):





            """simple docstring"""







            return f'''{self.data}'''



class   _lowercase			:


      def __init__(     self					:						Optional[int]    ):





            """simple docstring"""







            __snake_case   :     Any	      =None
            __snake_case   :     int	      =None


      def __iter__(     self					:						int    ):





            """simple docstring"""







            __snake_case   :     List[str]	      =self.head
            while node:
                  yield node.data
                  __snake_case   :     str	      =node.next


      def __str__(     self					:						Optional[int]    ):





            """simple docstring"""







            return "->".join([str(a    ) for item in self]    )


      def __len__(     self					:						str    ):





            """simple docstring"""







            return sum(1 for _ in self    )


      def _UpperCamelCase    (     self					:						int				, a					:						List[Any]    ):





            """simple docstring"""







            self.insert_at_nth(0				, a    )


      def _UpperCamelCase    (     self					:						str				, a					:						Optional[int]    ):





            """simple docstring"""







            self.insert_at_nth(len(self    )				, a    )


      def _UpperCamelCase    (     self					:						Optional[Any]				, a					:						int				, a					:						Tuple    ):





            """simple docstring"""







            __snake_case   :     List[str]	      =len(self    )

            if not 0 <= index <= length:
                  raise IndexError('''list index out of range'''    )
            __snake_case   :     List[Any]	      =Node(a    )
            if self.head is None:
                  __snake_case   :     Dict	      =new_node
            elif index == 0:
                  __snake_case   :     Union[str, Any]	      =new_node
                  __snake_case   :     Any	      =self.head
                  __snake_case   :     List[Any]	      =new_node
            elif index == length:
                  __snake_case   :     int	      =new_node
                  __snake_case   :     Optional[Any]	      =self.tail
                  __snake_case   :     Optional[Any]	      =new_node
            else:
                  __snake_case   :     Any	      =self.head
                  for _ in range(0				, a    ):
                        __snake_case   :     Optional[Any]	      =temp.next
                  __snake_case   :     Optional[Any]	      =new_node
                  __snake_case   :     Union[str, Any]	      =temp.previous
                  __snake_case   :     List[Any]	      =temp
                  __snake_case   :     str	      =new_node


      def _UpperCamelCase    (     self					:						List[str]    ):





            """simple docstring"""







            return self.delete_at_nth(0    )


      def _UpperCamelCase    (     self					:						List[Any]    ):





            """simple docstring"""







            return self.delete_at_nth(len(self    ) - 1    )


      def _UpperCamelCase    (     self					:						Optional[int]				, a					:						int    ):





            """simple docstring"""







            __snake_case   :     Tuple	      =len(self    )

            if not 0 <= index <= length - 1:
                  raise IndexError('''list index out of range'''    )
            __snake_case   :     str	      =self.head  # default first node
            if length == 1:
                  __snake_case   :     List[Any]	      =None
            elif index == 0:
                  __snake_case   :     List[Any]	      =self.head.next
                  __snake_case   :     Optional[int]	      =None
            elif index == length - 1:
                  __snake_case   :     str	      =self.tail
                  __snake_case   :     Union[str, Any]	      =self.tail.previous
                  __snake_case   :     Optional[Any]	      =None
            else:
                  __snake_case   :     Optional[int]	      =self.head
                  for _ in range(0				, a    ):
                        __snake_case   :     List[Any]	      =temp.next
                  __snake_case   :     Union[str, Any]	      =temp
                  __snake_case   :     Optional[int]	      =temp.previous
                  __snake_case   :     int	      =temp.next
            return delete_node.data


      def _UpperCamelCase    (     self					:						List[Any]				, a					:						Any    ):





            """simple docstring"""







            __snake_case   :     Optional[int]	      =self.head

            while current.data != data:  # Find the position to delete
                  if current.next:
                        __snake_case   :     Dict	      =current.next
                  else:  # We have reached the end an no value matches
                        raise ValueError('''No data matching given value'''    )

            if current == self.head:
                  self.delete_head()

            elif current == self.tail:
                  self.delete_tail()

            else:  # Before: 1 <--> 2(current) <--> 3
                  __snake_case   :     Tuple	      =current.next  # 1 --> 3
                  __snake_case   :     Optional[Any]	      =current.previous  # 1 <--> 3
            return data







      def _UpperCamelCase    (     self					:						int    ):





            """simple docstring"""







            return len(self    ) == 0



def      __lowercase       (     )       ->       None:
      __snake_case   :     List[str]	      =DoublyLinkedList()
      assert linked_list.is_empty() is True
      assert str(a			) == ""

      try:
            linked_list.delete_head()
            raise AssertionError  # This should not happen.
      except IndexError:
            assert True  # This should happen.

      try:
            linked_list.delete_tail()
            raise AssertionError  # This should not happen.
      except IndexError:
            assert True  # This should happen.

      for i in range(10			):
            assert len(a			) == i
            linked_list.insert_at_nth(a							,		i + 1			)
      assert str(a			) == "->".join(str(a			) for i in range(1							,		11			)			)

      linked_list.insert_at_head(0			)
      linked_list.insert_at_tail(11			)
      assert str(a			) == "->".join(str(a			) for i in range(0							,		12			)			)

      assert linked_list.delete_head() == 0
      assert linked_list.delete_at_nth(9			) == 10
      assert linked_list.delete_tail() == 11
      assert len(a			) == 9
      assert str(a			) == "->".join(str(a			) for i in range(1							,		10			)			)


if __name__ == "__main__":
    from doctest import testmod

    testmod()


