




import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


_A     =			logging.get_logger(__name__)





def __UpperCamelCase			(       _A  ):
						lowerCAmelCase_      			=  r'''\w+[.]\d+'''
						lowerCAmelCase_      			=  re.findall(a_ ,  a_  )
						for pat in pats:
												lowerCAmelCase_      			=  key.replace(a_ ,  '''_'''.join(pat.split('''.'''  )  )  )
						return key





def __UpperCamelCase			(       _A ,  _A ,  _A  ):
						lowerCAmelCase_      			=  pt_tuple_key[:-1] + ('''scale''',)
						if (
						    any('''norm''' in str_ for str_ in pt_tuple_key  )
						    and (pt_tuple_key[-1] == "bias")
						    and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
						    and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
						):
												lowerCAmelCase_      			=  pt_tuple_key[:-1] + ('''scale''',)
												return renamed_pt_tuple_key, pt_tensor
						elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
												lowerCAmelCase_      			=  pt_tuple_key[:-1] + ('''scale''',)
												return renamed_pt_tuple_key, pt_tensor

						# embedding
						if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
												lowerCAmelCase_      			=  pt_tuple_key[:-1] + ('''embedding''',)
												return renamed_pt_tuple_key, pt_tensor

						# conv layer
						lowerCAmelCase_      			=  pt_tuple_key[:-1] + ('''kernel''',)
						if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
												lowerCAmelCase_      			=  pt_tensor.transpose(2 ,  3 ,  1 ,  0  )
												return renamed_pt_tuple_key, pt_tensor

						# linear layer
						lowerCAmelCase_      			=  pt_tuple_key[:-1] + ('''kernel''',)
						if pt_tuple_key[-1] == "weight":
												lowerCAmelCase_      			=  pt_tensor.T
												return renamed_pt_tuple_key, pt_tensor

						# old PyTorch layer norm weight
						lowerCAmelCase_      			=  pt_tuple_key[:-1] + ('''weight''',)
						if pt_tuple_key[-1] == "gamma":
												return renamed_pt_tuple_key, pt_tensor

						# old PyTorch layer norm bias
						lowerCAmelCase_      			=  pt_tuple_key[:-1] + ('''bias''',)
						if pt_tuple_key[-1] == "beta":
												return renamed_pt_tuple_key, pt_tensor

						return pt_tuple_key, pt_tensor






def __UpperCamelCase			(       _A ,  _A ,  _A=42  ):
						# Step 1: Convert pytorch tensor to numpy
						lowerCAmelCase_      			=  {k: v.numpy() for k, v in pt_state_dict.items()}

						# Step 2: Since the model is stateless, get random Flax params
						lowerCAmelCase_      			=  flax_model.init_weights(PRNGKey(a_  )  )

						lowerCAmelCase_      			=  flatten_dict(a_  )
						lowerCAmelCase_      			=  {}

						# Need to change some parameters name to match Flax names
						for pt_key, pt_tensor in pt_state_dict.items():
												lowerCAmelCase_      			=  rename_key(a_  )
												lowerCAmelCase_      			=  tuple(renamed_pt_key.split('''.'''  )  )

												# Correctly rename weight parameters
												lowerCAmelCase_			,					lowerCAmelCase_      			=  rename_key_and_reshape_tensor(a_ ,  a_ ,  a_  )

												if flax_key in random_flax_state_dict:
																		if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
																								raise ValueError(
																								    f"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape "
																								    f"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}."  )

        # also add unexpected weight so that warning is thrown
												lowerCAmelCase_      			=  jnp.asarray(a_  )

						return unflatten_dict(a_  )












lowercase  							=					"\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n"

lowercase  							=					[{"type": "code", "content": INSTALL_CONTENT}]
lowercase  							=					{
    "{processor_class}": "FakeProcessorClass",
    "{model_class}": "FakeModelClass",
    "{object_class}": "FakeObjectClass",
}





