'''simple docstring'''


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


snake_case__					:      str				      =  logging.get_logger(__name__)


def  _lowerCamelCase   (				lowerCamelCase_ :							List[Any]       ):



     """simple docstring"""





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


def  _lowerCamelCase   (				lowerCamelCase_ :							Dict     ,					lowerCamelCase_ :							List[str]     ,					lowerCamelCase_ :							List[str]       ):



     """simple docstring"""





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

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

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

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

     # old PyTorch layer norm bias
     UpperCAmelCase_							:       Optional[Any]       =				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   (				lowerCamelCase_ :							str     ,					lowerCamelCase_ :							Any     ,					lowerCamelCase_ :							Union[str, Any]=42       ):



     """simple docstring"""





     UpperCAmelCase_							:       Dict       =				{k: v.numpy() for k, v in pt_state_dict.items()}

     # Step 2: Since the model is stateless, get random Flax params
     UpperCAmelCase_							:       Optional[int]       =				flax_model.init_weights(PRNGKey(SCREAMING_SNAKE_CASE_       )       )

     UpperCAmelCase_							:       Union[str, Any]       =				flatten_dict(SCREAMING_SNAKE_CASE_       )
     UpperCAmelCase_							:       Union[str, Any]       =				{}

     # Need to change some parameters name to match Flax names
     for pt_key, pt_tensor in pt_state_dict.items():
          UpperCAmelCase_							:       Dict       =				rename_key(SCREAMING_SNAKE_CASE_       )
          UpperCAmelCase_							:       Union[str, Any]       =				tuple(renamed_pt_key.split('.'       )       )

          # Correctly rename weight parameters
          UpperCAmelCase_       ,      UpperCAmelCase_							:       List[str]       =				rename_key_and_reshape_tensor(SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_       )

          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(SCREAMING_SNAKE_CASE_       )

     return unflatten_dict(SCREAMING_SNAKE_CASE_       )







'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


snake_case__					:      Dict				      =  {
    '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''',
    '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''',
    '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''',
    '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''',
    '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''',
    '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''',
    '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''',
    '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''',
}






class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:str						   =		'''albert'''


  def __init__(			self				,		snake_case_=3_0_0_0_0				,		snake_case_=1_2_8				,		snake_case_=4_0_9_6				,		snake_case_=1_2				,		snake_case_=1				,		snake_case_=6_4				,		snake_case_=1_6_3_8_4				,		snake_case_=1				,		snake_case_="gelu_new"				,		snake_case_=0				,		snake_case_=0				,		snake_case_=5_1_2				,		snake_case_=2				,		snake_case_=0.02				,		snake_case_=1E-12				,		snake_case_=0.1				,		snake_case_="absolute"				,		snake_case_=0				,		snake_case_=2				,		snake_case_=3				,		**snake_case_				,		):




       '''simple docstring'''

       super().__init__(pad_token_id=snake_case_				,		bos_token_id=snake_case_				,		eos_token_id=snake_case_				,		**snake_case_						)

       UpperCAmelCase_							:       List[Any]       =				vocab_size
       UpperCAmelCase_							:       Dict       =				embedding_size
       UpperCAmelCase_							:       str       =				hidden_size
       UpperCAmelCase_							:       Any       =				num_hidden_layers
       UpperCAmelCase_							:       Union[str, Any]       =				num_hidden_groups
       UpperCAmelCase_							:       List[str]       =				num_attention_heads
       UpperCAmelCase_							:       Any       =				inner_group_num
       UpperCAmelCase_							:       Optional[int]       =				hidden_act
       UpperCAmelCase_							:       Tuple       =				intermediate_size
       UpperCAmelCase_							:       Any       =				hidden_dropout_prob
       UpperCAmelCase_							:       List[str]       =				attention_probs_dropout_prob
       UpperCAmelCase_							:       Union[str, Any]       =				max_position_embeddings
       UpperCAmelCase_							:       Dict       =				type_vocab_size
       UpperCAmelCase_							:       Union[str, Any]       =				initializer_range
       UpperCAmelCase_							:       Optional[Any]       =				layer_norm_eps
       UpperCAmelCase_							:       Dict       =				classifier_dropout_prob
       UpperCAmelCase_							:       Tuple       =				position_embedding_type







class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       if self.task == "multiple-choice":
            UpperCAmelCase_							:       Optional[Any]       =				{0: 'batch', 1: 'choice', 2: 'sequence'}
       else:
            UpperCAmelCase_							:       List[Any]       =				{0: 'batch', 1: 'sequence'}
       return OrderedDict(
           [
               ('input_ids', dynamic_axis),
               ('attention_mask', dynamic_axis),
               ('token_type_ids', dynamic_axis),
           ]						)






