




from ... import PretrainedConfig


__a          =			{
    'sijunhe/nezha-cn-base': 'https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json',
}

class   __a(					_a       ):






    """simple docstring"""





    lowerCAmelCase        =				NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP
    lowerCAmelCase        =				'''nezha'''





    def __init__(				self       ,_SCREAMING_SNAKE_CASE=21_128       ,_SCREAMING_SNAKE_CASE=768       ,_SCREAMING_SNAKE_CASE=12       ,_SCREAMING_SNAKE_CASE=12       ,_SCREAMING_SNAKE_CASE=3_072       ,_SCREAMING_SNAKE_CASE="gelu"       ,_SCREAMING_SNAKE_CASE=0.1       ,_SCREAMING_SNAKE_CASE=0.1       ,_SCREAMING_SNAKE_CASE=512       ,_SCREAMING_SNAKE_CASE=64       ,_SCREAMING_SNAKE_CASE=2       ,_SCREAMING_SNAKE_CASE=0.02       ,_SCREAMING_SNAKE_CASE=1e-12       ,_SCREAMING_SNAKE_CASE=0.1       ,_SCREAMING_SNAKE_CASE=0       ,_SCREAMING_SNAKE_CASE=2       ,_SCREAMING_SNAKE_CASE=3       ,_SCREAMING_SNAKE_CASE=True       ,**_SCREAMING_SNAKE_CASE       ,)			->		Optional[Any]:
         super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE       ,bos_token_id=_SCREAMING_SNAKE_CASE       ,eos_token_id=_SCREAMING_SNAKE_CASE       ,**_SCREAMING_SNAKE_CASE  )

         UpperCAmelCase_      :							Optional[Any]       =      vocab_size
         UpperCAmelCase_      :							int       =      hidden_size
         UpperCAmelCase_      :							Optional[Any]       =      num_hidden_layers
         UpperCAmelCase_      :							List[Any]       =      num_attention_heads
         UpperCAmelCase_      :							int       =      hidden_act
         UpperCAmelCase_      :							List[Any]       =      intermediate_size
         UpperCAmelCase_      :							List[Any]       =      hidden_dropout_prob
         UpperCAmelCase_      :							Optional[Any]       =      attention_probs_dropout_prob
         UpperCAmelCase_      :							Dict       =      max_position_embeddings
         UpperCAmelCase_      :							str       =      max_relative_position
         UpperCAmelCase_      :							Optional[int]       =      type_vocab_size
         UpperCAmelCase_      :							Dict       =      initializer_range
         UpperCAmelCase_      :							Tuple       =      layer_norm_eps
         UpperCAmelCase_      :							Any       =      classifier_dropout
         UpperCAmelCase_      :							List[str]       =      use_cache





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 lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							str       =      r'''\w+[.]\d+'''
     UpperCAmelCase_      :							int       =      re.findall(_lowercase			,						_lowercase				)
     for pat in pats:
          UpperCAmelCase_      :							List[Any]       =      key.replace(_lowercase			,						'''_'''.join(pat.split('''.'''				)				)				)
     return key







def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							Dict       =      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)
     ):
          UpperCAmelCase_      :							List[Any]       =      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:
          UpperCAmelCase_      :							Any       =      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:
          UpperCAmelCase_      :							Tuple       =      pt_tuple_key[:-1] + ('''embedding''',)
          return renamed_pt_tuple_key, pt_tensor

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

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

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

     # old PyTorch layer norm bias
     UpperCAmelCase_      :							Tuple       =      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 lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase=42				):

     '''simple docstring'''


     UpperCAmelCase_      :							Union[str, Any]       =      {k: v.numpy() for k, v in pt_state_dict.items()}

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

     UpperCAmelCase_      :							List[Any]       =      flatten_dict(_lowercase				)
     UpperCAmelCase_      :							int       =      {}

     # Need to change some parameters name to match Flax names
     for pt_key, pt_tensor in pt_state_dict.items():
          UpperCAmelCase_      :							Optional[int]       =      rename_key(_lowercase				)
          UpperCAmelCase_      :							List[str]       =      tuple(renamed_pt_key.split('''.'''				)				)

          # Correctly rename weight parameters
          UpperCAmelCase_,   UpperCAmelCase_      :							Any       =      rename_key_and_reshape_tensor(_lowercase			,						_lowercase			,						_lowercase				)

          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
          UpperCAmelCase_      :							int       =      jnp.asarray(_lowercase				)

     return unflatten_dict(_lowercase				)