




from importlib import import_module

from .logging import get_logger


_lowerCAmelCase						:   int		=   get_logger(__name__)
class 			_UpperCamelCase      :
      def __init__(			self   :int ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :Optional[Any]=None      )     ->			int:
       UpperCAmelCase__							 =						attrs or []
       if module is not None:
        for key in module.__dict__:
         if key in attrs or not key.startswith("__"      ):
          setattr(self ,      lowerCamelCase_ ,      getattr(lowerCamelCase_ ,      lowerCamelCase_      )      )
       UpperCAmelCase__							 =						module._original_module if isinstance(lowerCamelCase_ ,      _PatchedModuleObj      ) else module



class 			_UpperCamelCase      :
      UpperCAmelCase_        =		[]
      def __init__(			self   :str ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :str ,      lowerCamelCase   :Union[str, Any] ,      lowerCamelCase   :str=None      )     ->			int:
       UpperCAmelCase__							 =						obj
       UpperCAmelCase__							 =						target
       UpperCAmelCase__							 =						new
       UpperCAmelCase__							 =						target.split("."      )[0]
       UpperCAmelCase__							 =						{}
       UpperCAmelCase__							 =						attrs or []
      def __enter__(			self   :str      )     ->			Tuple:
       UpperCAmelCase__							 =						self.target.split("."      )

       # Patch modules:
       # it's used to patch attributes of submodules like "os.path.join";
       # in this case we need to patch "os" and "os.path"

       for i in range(len(lowerCamelCase_      )      ):
        try:
         UpperCAmelCase__							 =						import_module(".".join(submodules[: i + 1]      )      )
        except ModuleNotFoundError:
         continue
        # We iterate over all the globals in self.obj in case we find "os" or "os.path"
        for attr in self.obj.__dir__():
         UpperCAmelCase__							 =						getattr(self.obj ,      lowerCamelCase_      )
         # We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
         # This allows to patch renamed modules like "from os import path as ospath".
         if obj_attr is submodule or (
             (isinstance(lowerCamelCase_ ,      _PatchedModuleObj      ) and obj_attr._original_module is submodule)
         ):
          UpperCAmelCase__							 =						obj_attr
          # patch at top level
          setattr(self.obj ,      lowerCamelCase_ ,      _PatchedModuleObj(lowerCamelCase_ ,      attrs=self.attrs      )      )
          UpperCAmelCase__							 =						getattr(self.obj ,      lowerCamelCase_      )
          # construct lower levels patches
          for key in submodules[i + 1 :]:
           setattr(lowerCamelCase_ ,      lowerCamelCase_ ,      _PatchedModuleObj(getattr(lowerCamelCase_ ,      lowerCamelCase_ ,      lowerCamelCase_      ) ,      attrs=self.attrs      )      )
           UpperCAmelCase__							 =						getattr(lowerCamelCase_ ,      lowerCamelCase_      )
          # finally set the target attribute
          setattr(lowerCamelCase_ ,      lowerCamelCase_ ,      self.new      )

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

       if submodules:  # if it's an attribute of a submodule like "os.path.join"
        try:
         UpperCAmelCase__							 =						getattr(import_module(".".join(lowerCamelCase_      )      ) ,      lowerCamelCase_      )
        except (AttributeError, ModuleNotFoundError):
         return
        # We iterate over all the globals in self.obj in case we find "os.path.join"
        for attr in self.obj.__dir__():
         # We don't check for the name of the global, but rather if its value *is* "os.path.join".
         # This allows to patch renamed attributes like "from os.path import join as pjoin".
         if getattr(self.obj ,      lowerCamelCase_      ) is attr_value:
          UpperCAmelCase__							 =						getattr(self.obj ,      lowerCamelCase_      )
          setattr(self.obj ,      lowerCamelCase_ ,      self.new      )
       elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
        UpperCAmelCase__							 =						globals()['''__builtins__'''][target_attr]
        setattr(self.obj ,      lowerCamelCase_ ,      self.new      )
       else:
        raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.'''      )
      def __exit__(			self   :Union[str, Any] ,      *lowerCamelCase   :Optional[Any]      )     ->			List[Any]:




       for attr in list(self.original      ):
        setattr(self.obj ,      lowerCamelCase_ ,      self.original.pop(lowerCamelCase_      )      )
      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			Dict:
       self.__enter__()
       self._active_patches.append(self      )







      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Optional[int]:




       try:
        self._active_patches.remove(self      )
       except ValueError:
        # If the patch hasn't been started this will fail
        return None

       return self.__exit__()









from typing import TYPE_CHECKING

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


_lowerCAmelCase						:   Union[str, Any]		=   {
    "configuration_bert": ["BERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BertConfig", "BertOnnxConfig"],
    "tokenization_bert": ["BasicTokenizer", "BertTokenizer", "WordpieceTokenizer"],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCAmelCase						:   str		=   ["BertTokenizerFast"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCAmelCase						:   int		=   [
      "BERT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "BertForMaskedLM",
      "BertForMultipleChoice",
      "BertForNextSentencePrediction",
      "BertForPreTraining",
      "BertForQuestionAnswering",
      "BertForSequenceClassification",
      "BertForTokenClassification",
      "BertLayer",
      "BertLMHeadModel",
      "BertModel",
      "BertPreTrainedModel",
      "load_tf_weights_in_bert",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCAmelCase						:   Dict		=   [
      "TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "TFBertEmbeddings",
      "TFBertForMaskedLM",
      "TFBertForMultipleChoice",
      "TFBertForNextSentencePrediction",
      "TFBertForPreTraining",
      "TFBertForQuestionAnswering",
      "TFBertForSequenceClassification",
      "TFBertForTokenClassification",
      "TFBertLMHeadModel",
      "TFBertMainLayer",
      "TFBertModel",
      "TFBertPreTrainedModel",
  ]
try:
  if not is_tensorflow_text_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCAmelCase						:   str		=   ["TFBertTokenizer"]

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCAmelCase						:   Optional[Any]		=   [
      "FlaxBertForCausalLM",
      "FlaxBertForMaskedLM",
      "FlaxBertForMultipleChoice",
      "FlaxBertForNextSentencePrediction",
      "FlaxBertForPreTraining",
      "FlaxBertForQuestionAnswering",
      "FlaxBertForSequenceClassification",
      "FlaxBertForTokenClassification",
      "FlaxBertModel",
      "FlaxBertPreTrainedModel",
  ]

if TYPE_CHECKING:
  from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig
  from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_bert_fast import BertTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_bert import (
        BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        BertForMaskedLM,
        BertForMultipleChoice,
        BertForNextSentencePrediction,
        BertForPreTraining,
        BertForQuestionAnswering,
        BertForSequenceClassification,
        BertForTokenClassification,
        BertLayer,
        BertLMHeadModel,
        BertModel,
        BertPreTrainedModel,
        load_tf_weights_in_bert,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_bert import (
        TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFBertEmbeddings,
        TFBertForMaskedLM,
        TFBertForMultipleChoice,
        TFBertForNextSentencePrediction,
        TFBertForPreTraining,
        TFBertForQuestionAnswering,
        TFBertForSequenceClassification,
        TFBertForTokenClassification,
        TFBertLMHeadModel,
        TFBertMainLayer,
        TFBertModel,
        TFBertPreTrainedModel,
    )

  try:
    if not is_tensorflow_text_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_bert_tf import TFBertTokenizer

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_bert import (
        FlaxBertForCausalLM,
        FlaxBertForMaskedLM,
        FlaxBertForMultipleChoice,
        FlaxBertForNextSentencePrediction,
        FlaxBertForPreTraining,
        FlaxBertForQuestionAnswering,
        FlaxBertForSequenceClassification,
        FlaxBertForTokenClassification,
        FlaxBertModel,
        FlaxBertPreTrainedModel,
    )

else:
  import sys

  _lowerCAmelCase						:   Union[str, Any]		=   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





