





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


UpperCAmelCase_     							= logging.get_logger(__name__)




def 		lowerCAmelCase_    (  __UpperCAmelCase:			List[str]      )      ->	Dict:
    UpperCamelCase__							:				List[Any]          =      r'''\w+[.]\d+'''
    UpperCamelCase__							:				Any          =      re.findall(__UpperCAmelCase		,   __UpperCAmelCase      )
    for pat in pats:
        UpperCamelCase__							:				Union[str, Any]          =      key.replace(__UpperCAmelCase		,   '''_'''.join(pat.split('''.'''      )      )      )
    return key




def 		lowerCAmelCase_    (  __UpperCAmelCase:			Tuple		,   __UpperCAmelCase:			Optional[int]		,   __UpperCAmelCase:			int      )      ->	Any:
    UpperCamelCase__							:				str          =      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__							:				int          =      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__							:				Optional[Any]          =      pt_tuple_key[:-1] + ('''embedding''',)
        return renamed_pt_tuple_key, pt_tensor

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

    # linear layer
    UpperCamelCase__							:				Optional[Any]          =      pt_tuple_key[:-1] + ('''kernel''',)
    if pt_tuple_key[-1] == "weight":
        UpperCamelCase__							:				Any          =      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__							:				str          =      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_    (  __UpperCAmelCase:			str		,   __UpperCAmelCase:			Union[str, Any]		,   __UpperCAmelCase:			int=42      )      ->	str:
    # Step 1: Convert pytorch tensor to numpy
    UpperCamelCase__							:				List[str]          =      {k: v.numpy() for k, v in pt_state_dict.items()}

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

    UpperCamelCase__							:				Optional[Any]          =      flatten_dict(__UpperCAmelCase      )
    UpperCamelCase__							:				Any          =      {}

    # Need to change some parameters name to match Flax names
    for pt_key, pt_tensor in pt_state_dict.items():
        UpperCamelCase__							:				Optional[Any]          =      rename_key(__UpperCAmelCase      )
        UpperCamelCase__							:				Tuple          =      tuple(renamed_pt_key.split('''.'''      )      )

        # Correctly rename weight parameters
        UpperCamelCase__   ,UpperCamelCase__							:				List[str]          =      rename_key_and_reshape_tensor(__UpperCAmelCase		,   __UpperCAmelCase		,   __UpperCAmelCase      )

        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__							:				Any          =      jnp.asarray(__UpperCAmelCase      )

    return unflatten_dict(__UpperCAmelCase      )








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase_     							= {
    'configuration_convbert': ['CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConvBertConfig', 'ConvBertOnnxConfig'],
    'tokenization_convbert': ['ConvBertTokenizer'],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= ['ConvBertTokenizerFast']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= [
      'CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
      'ConvBertForMaskedLM',
      'ConvBertForMultipleChoice',
      'ConvBertForQuestionAnswering',
      'ConvBertForSequenceClassification',
      'ConvBertForTokenClassification',
      'ConvBertLayer',
      'ConvBertModel',
      'ConvBertPreTrainedModel',
      'load_tf_weights_in_convbert',
  ]


try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= [
      'TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
      'TFConvBertForMaskedLM',
      'TFConvBertForMultipleChoice',
      'TFConvBertForQuestionAnswering',
      'TFConvBertForSequenceClassification',
      'TFConvBertForTokenClassification',
      'TFConvBertLayer',
      'TFConvBertModel',
      'TFConvBertPreTrainedModel',
  ]


if TYPE_CHECKING:
  from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig
  from .tokenization_convbert import ConvBertTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_convbert_fast import ConvBertTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_convbert import (
        CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        ConvBertForMaskedLM,
        ConvBertForMultipleChoice,
        ConvBertForQuestionAnswering,
        ConvBertForSequenceClassification,
        ConvBertForTokenClassification,
        ConvBertLayer,
        ConvBertModel,
        ConvBertPreTrainedModel,
        load_tf_weights_in_convbert,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_convbert import (
        TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFConvBertForMaskedLM,
        TFConvBertForMultipleChoice,
        TFConvBertForQuestionAnswering,
        TFConvBertForSequenceClassification,
        TFConvBertForTokenClassification,
        TFConvBertLayer,
        TFConvBertModel,
        TFConvBertPreTrainedModel,
    )


else:
  import sys

  UpperCAmelCase_     							= _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

