




import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
		import sqlitea

		import sqlalchemy


__lowerCAmelCase						:							List[Any]			     =						datasets.utils.logging.get_logger(__name__)




@dataclass
class        snake_case__				(datasets.BuilderConfig  ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			:				Union[str, "sqlalchemy.sql.Selectable"]									=   None
				SCREAMING_SNAKE_CASE_			:				Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]									=   None
				SCREAMING_SNAKE_CASE_			:				Optional[Union[str, List[str]]]									=   None
				SCREAMING_SNAKE_CASE_			:				bool									=   True
				SCREAMING_SNAKE_CASE_			:				Optional[Union[List, Tuple, Dict]]									=   None
				SCREAMING_SNAKE_CASE_			:				Optional[Union[List, Dict]]									=   None
				SCREAMING_SNAKE_CASE_			:				Optional[List[str]]									=   None
				SCREAMING_SNAKE_CASE_			:				Optional[int]									=   1_00_00
				SCREAMING_SNAKE_CASE_			:				Optional[datasets.Features]									=   None





				def 				__UpperCAmelCase				( self							:	Union[str, Any]				)   ->	List[Any]:
							if self.sql is None:
										raise ValueError("sql must be specified"				)
							if self.con is None:
										raise ValueError("con must be specified"				)





				def 				__UpperCAmelCase				( self							:	int   , __lowerCamelCase							:	dict   , __lowerCamelCase							:	Optional[datasets.Features] = None   , )   ->	str:
							a       =     config_kwargs.copy()
							# We need to stringify the Selectable object to make its hash deterministic

							# The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
							a       =     config_kwargs["sql"]
							if not isinstance(__lowerCamelCase   , __lowerCamelCase				):
										if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
													import sqlalchemy

													if isinstance(__lowerCamelCase   , sqlalchemy.sql.Selectable				):
																a       =     sqlalchemy.create_engine(config_kwargs["con"].split("://"				)[0] + "://"				)
																a       =     str(sql.compile(dialect=engine.dialect				)				)
																a       =     sql_str
													else:
																raise TypeError(
																    f"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(__lowerCamelCase				)}: {sql}"""				)
										else:
													raise TypeError(
													    f"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(__lowerCamelCase				)}: {sql}"""				)
							a       =     config_kwargs["con"]
							if not isinstance(__lowerCamelCase   , __lowerCamelCase				):
										a       =     id(__lowerCamelCase				)
										logger.info(
										    f"""SQL connection 'con' of type {type(__lowerCamelCase				)} couldn't be hashed properly. To enable hashing, specify 'con' as URI string instead."""				)

							return super().create_config_id(__lowerCamelCase   , custom_features=__lowerCamelCase				)





				@property
				def 				__UpperCAmelCase				( self							:	Dict				)   ->	Any:
							a       =     {
							    "index_col": self.index_col,
							    "columns": self.columns,
							    "params": self.params,
							    "coerce_float": self.coerce_float,
							    "parse_dates": self.parse_dates,
							}
							return pd_read_sql_kwargs




class        snake_case__				(datasets.ArrowBasedBuilder  ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			:				str									=   SqlConfig





				def 				__UpperCAmelCase				( self							:	Optional[Any]				)   ->	Optional[Any]:
							return datasets.DatasetInfo(features=self.config.features				)





				def 				__UpperCAmelCase				( self							:	Optional[Any]   , __lowerCamelCase							:	Dict				)   ->	Union[str, Any]:
							return [datasets.SplitGenerator(name=datasets.Split.TRAIN   , gen_kwargs={}				)]





				def 				__UpperCAmelCase				( self							:	List[Any]   , __lowerCamelCase							:	pa.Table				)   ->	pa.Table:
							if self.config.features is not None:
										a       =     self.config.features.arrow_schema
										if all(not require_storage_cast(__lowerCamelCase				) for feature in self.config.features.values()				):
													# cheaper cast
													a       =     pa.Table.from_arrays([pa_table[field.name] for field in schema]   , schema=__lowerCamelCase				)
										else:
													# more expensive cast; allows str <-> int/float or str to Audio for example
													a       =     table_cast(__lowerCamelCase   , __lowerCamelCase				)
							return pa_table





				def 				__UpperCAmelCase				( self							:	int				)   ->	Any:
							a       =     self.config.chunksize
							a       =     pd.read_sql(
							    self.config.sql   , self.config.con   , chunksize=__lowerCamelCase   , **self.config.pd_read_sql_kwargs				)
							a       =     [sql_reader] if chunksize is None else sql_reader
							for chunk_idx, df in enumerate(__lowerCamelCase				):
										a       =     pa.Table.from_pandas(__lowerCamelCase				)
										yield chunk_idx, self._cast_table(__lowerCamelCase				)












import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def      __magic_name__							(					A						:   str,    A						:   List[str],    A						:   int  ):






			'''simple docstring'''







			a       ,						a       ,						a       =     pt_weights[F"""{prefix}.in_proj_weight"""].chunk(3,    dim=0  )
			a       ,						a       ,						a       =     pt_weights[F"""{prefix}.in_proj_bias"""].chunk(3,    dim=0  )

			a       =     pt_weights[F"""{prefix}.out_proj.weight"""]
			a       =     pt_weights[F"""{prefix}.out_proj.bias"""]

			a       =     q_proj
			a       =     q_proj_bias

			a       =     k_proj
			a       =     k_proj_bias

			a       =     v_proj
			a       =     v_proj_bias

			a       =     out_proj_weights
			a       =     out_proj_bias




def      __magic_name__							(					A						:   Optional[int],    A						:   str,    A						:   Union[str, Any]  ):






			'''simple docstring'''







			copy_linear(hf_mlp.fca,    A,    F"""{prefix}.c_fc"""  )
			copy_linear(hf_mlp.fca,    A,    F"""{prefix}.c_proj"""  )




def      __magic_name__							(					A						:   int,    A						:   Tuple,    A						:   Optional[int]  ):






			'''simple docstring'''







			a       =     pt_weights[F"""{prefix}.weight"""].data
			a       =     pt_weights[F"""{prefix}.bias"""].data




def      __magic_name__							(					A						:   List[Any],    A						:   List[str],    A						:   Any  ):






			'''simple docstring'''







			copy_linear(hf_layer.layer_norma,    A,    F"""{prefix}.ln_1"""  )
			copy_linear(hf_layer.layer_norma,    A,    F"""{prefix}.ln_2"""  )

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

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




def      __magic_name__							(					A						:   int,    A						:   List[str],    A						:   Any  ):






			'''simple docstring'''







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




def      __magic_name__							(					A						:   Any,    A						:   Any  ):






			'''simple docstring'''







			a       =     pt_weights["text_projection"].data.T

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




def      __magic_name__							(					A						:   Union[str, Any],    A						:   Optional[Any]  ):






			'''simple docstring'''







			a       =     pt_weights["visual.proj"].data.T

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

			# copy embeddings
			a       =     pt_weights["visual.conv1.weight"].data
			a       =     pt_weights["visual.class_embedding"].data
			a       =     pt_weights["visual.positional_embedding"].data

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




@torch.no_grad()
def      __magic_name__							(					A						:   Optional[Any],    A						:   List[str],    A						:   Dict=None  ):






			'''simple docstring'''







			assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
			a       =     ChineseCLIPConfig.from_pretrained(A  )

			a       =     ChineseCLIPModel(A  ).eval()

			a       =     torch.load(A,    map_location="cpu"  )["state_dict"]
			a       =     {(name[7:] if name.startswith("module."  ) else name): value for name, value in pt_weights.items()}

			copy_text_model_and_projection(A,    A  )
			copy_vision_model_and_projection(A,    A  )
			a       =     pt_weights["logit_scale"].data

			hf_model.save_pretrained(A  )


if __name__ == "__main__":
		__lowerCAmelCase						:							Dict			     =						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.'
		)
		__lowerCAmelCase						:							Dict			     =						parser.parse_args()

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






