


import importlib
import importlib.metadata
import os
import warnings
from distutils.util import strtobool
from functools import lru_cache

import torch
from packaging import version
from packaging.version import parse

from .environment import parse_flag_from_env
from .versions import compare_versions, is_torch_version


try:
       import torch_xla.core.xla_model as xm  # noqa: F401

       __UpperCamelCase					:	Tuple          =				True
except ImportError:
       __UpperCamelCase					:	Optional[int]          =				False


# Cache this result has it's a C FFI call which can be pretty time-consuming
__UpperCamelCase					:	Union[str, Any]          =				torch.distributed.is_available()
def   snake_case_    (			__lowercase							):
   # Check we're not importing a "pkg_name" directory somewhere but the actual library by trying to grab the version
   UpperCAmelCase_				:   Optional[Any]							= importlib.util.find_spec(__lowercase							) is not None
   if package_exists:
      try:
         UpperCAmelCase_				:   Optional[int]							= importlib.metadata.metadata(__lowercase							)
         return True
      except importlib.metadata.PackageNotFoundError:
         return False
def   snake_case_    (			):
   return _torch_distributed_available
def   snake_case_    (			):
   try:
      pass
   except ImportError:
      print(
          '''Intel(R) oneCCL Bindings for PyTorch* is required to run DDP on Intel(R) GPUs, but it is not'''
          ''' detected. If you see \"ValueError: Invalid backend: \'ccl\'\" error, please install Intel(R) oneCCL'''
          ''' Bindings for PyTorch*.'''							)
   return (
       importlib.util.find_spec('''torch_ccl'''							) is not None
       or importlib.util.find_spec('''oneccl_bindings_for_pytorch'''							) is not None
   )
def   snake_case_    (			):
   return importlib.metadata.version('''oneccl_bind_pt'''							)
def   snake_case_    (			):
   return _is_package_available('''transformer_engine'''							)
@lru_cache
def   snake_case_    (			__lowercase=True							):
   # Due to bugs on the amp series GPUs, we disable torch-xla on them
   if torch.cuda.is_available():
      return False
   if _tpu_available and check_device:
      try:
         # Will raise a RuntimeError if no XLA configuration is found
         UpperCAmelCase_				:   Union[str, Any]							= xm.xla_device()
         return True
      except RuntimeError:
         return False
   return _tpu_available
def   snake_case_    (			):
   return _is_package_available('''deepspeed'''							)
def   snake_case_    (			__lowercase=False							):
   if is_tpu_available():
      return not ignore_tpu
   if torch.cuda.is_available():
      return torch.cuda.is_bfaa_supported()
   if is_npu_available():
      return False
   return True
def   snake_case_    (			):
   UpperCAmelCase_				:   Optional[Any]							= _is_package_available('''bitsandbytes'''							)
   if package_exists:
      UpperCAmelCase_				:   Dict							= version.parse(importlib.metadata.version('''bitsandbytes'''							)							)
      return compare_versions(__lowercase      ,							'''>='''      ,							'''0.39.0'''							)
   return False
def   snake_case_    (			):
   UpperCAmelCase_				:   List[str]							= _is_package_available('''bitsandbytes'''							)
   if package_exists:
      UpperCAmelCase_				:   Dict							= version.parse(importlib.metadata.version('''bitsandbytes'''							)							)
      return compare_versions(__lowercase      ,							'''>='''      ,							'''0.37.2'''							)
   return False
def   snake_case_    (			):
   return _is_package_available('''bitsandbytes'''							)
def   snake_case_    (			):
   if strtobool(os.environ.get('''ACCELERATE_USE_MEGATRON_LM'''      ,							'''False'''							)							) == 1:
      UpperCAmelCase_				:   Union[str, Any]							= importlib.util.find_spec('''megatron'''							) is not None
      if package_exists:
         try:
            UpperCAmelCase_				:   str							= parse(importlib.metadata.version('''megatron-lm'''							)							)
            return compare_versions(__lowercase      ,							'''>='''      ,							'''2.2.0'''							)
         except Exception as e:
            warnings.warn(F'''Parse Megatron version failed. Exception:{e}'''							)
            return False
def   snake_case_    (			):
   return _is_package_available('''safetensors'''							)
def   snake_case_    (			):
   return _is_package_available('''transformers'''							)
def   snake_case_    (			):
   return _is_package_available('''datasets'''							)
def   snake_case_    (			):
   return _is_package_available('''aim'''							)
def   snake_case_    (			):
   return _is_package_available('''tensorboard'''							) or _is_package_available('''tensorboardX'''							)
def   snake_case_    (			):
   return _is_package_available('''wandb'''							)
def   snake_case_    (			):
   return _is_package_available('''comet_ml'''							)
def   snake_case_    (			):
   return _is_package_available('''boto3'''							)
def   snake_case_    (			):
   if _is_package_available('''rich'''							):
      if "ACCELERATE_DISABLE_RICH" in os.environ:
         warnings.warn(
             '''`ACCELERATE_DISABLE_RICH` is deprecated and will be removed in v0.22.0 and deactivated by default. Please use `ACCELERATE_ENABLE_RICH` if you wish to use `rich`.'''							)
         return not parse_flag_from_env('''ACCELERATE_DISABLE_RICH'''      ,							__lowercase							)
      return parse_flag_from_env('''ACCELERATE_ENABLE_RICH'''      ,							__lowercase							)
   return False
def   snake_case_    (			):
   return _is_package_available('''sagemaker'''							)
def   snake_case_    (			):
   return _is_package_available('''tqdm'''							)
def   snake_case_    (			):
   return _is_package_available('''mlflow'''							)
def   snake_case_    (			):
   return is_torch_version('''>='''      ,							'''1.12'''							) and torch.backends.mps.is_available() and torch.backends.mps.is_built()
def   snake_case_    (			):
   def get_major_and_minor_from_version(__lowercase							):
      return str(version.parse(__lowercase							).major							) + "." + str(version.parse(__lowercase							).minor							)

   UpperCAmelCase_				:   int							= importlib.metadata.version('''torch'''							)
   if importlib.util.find_spec('''intel_extension_for_pytorch'''							) is None:
      return False
   UpperCAmelCase_				:   Optional[Any]							= '''N/A'''
   try:
      UpperCAmelCase_				:   List[Any]							= importlib.metadata.version('''intel_extension_for_pytorch'''							)
   except importlib.metadata.PackageNotFoundError:
      return False
   UpperCAmelCase_				:   Optional[int]							= get_major_and_minor_from_version(_torch_version							)
   UpperCAmelCase_				:   Optional[Any]							= get_major_and_minor_from_version(_ipex_version							)
   if torch_major_and_minor != ipex_major_and_minor:
      warnings.warn(
          F'''Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*,'''
          F''' but PyTorch {_torch_version} is found. Please switch to the matching version and run again.'''							)
      return False
   return True
@lru_cache
def   snake_case_    (			__lowercase=False							):
   if importlib.util.find_spec('''torch'''							) is None or importlib.util.find_spec('''torch_npu'''							) is None:
      return False

   import torch
   import torch_npu  # noqa: F401

   if check_device:
      try:
         # Will raise a RuntimeError if no NPU is found
         UpperCAmelCase_				:   Tuple							= torch.npu.device_count()
         return torch.npu.is_available()
      except RuntimeError:
         return False
   return hasattr(__lowercase      ,							'''npu'''							) and torch.npu.is_available()


@lru_cache
def   snake_case_    (			__lowercase=False							):
   if not parse_flag_from_env('''ACCELERATE_USE_XPU'''      ,							default=__lowercase							):
      return False
   if is_ipex_available():
      import torch

      if is_torch_version('''<='''      ,							'''1.12'''							):
         return False
   else:
      return False

   import intel_extension_for_pytorch  # noqa: F401

   if check_device:
      try:
         # Will raise a RuntimeError if no XPU  is found
         UpperCAmelCase_				:   Optional[Any]							= torch.xpu.device_count()
         return torch.xpu.is_available()
      except RuntimeError:
         return False
   return hasattr(__lowercase      ,							'''xpu'''							) and torch.xpu.is_available()



import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
       from transformers.models.electra.modeling_flax_electra import (
           FlaxElectraForCausalLM,
           FlaxElectraForMaskedLM,
           FlaxElectraForMultipleChoice,
           FlaxElectraForPreTraining,
           FlaxElectraForQuestionAnswering,
           FlaxElectraForSequenceClassification,
           FlaxElectraForTokenClassification,
           FlaxElectraModel,
       )





class 			lowerCAmelCase__(     unittest.TestCase    ):






    '''simple docstring'''





    def __init__(						self					: Any							, __snake_case					: Tuple							, __snake_case					: Union[str, Any]=13							, __snake_case					: Tuple=7							, __snake_case					: str=True							, __snake_case					: List[Any]=True							, __snake_case					: Optional[int]=True							, __snake_case					: List[str]=True							, __snake_case					: Tuple=99							, __snake_case					: int=24							, __snake_case					: int=32							, __snake_case					: Tuple=5							, __snake_case					: int=4							, __snake_case					: Any=37							, __snake_case					: int="gelu"							, __snake_case					: Union[str, Any]=0.1							, __snake_case					: Optional[Any]=0.1							, __snake_case					: str=512							, __snake_case					: List[Any]=16							, __snake_case					: Tuple=2							, __snake_case					: List[str]=0.02							, __snake_case					: Any=4							, ):





       '''simple docstring'''
       UpperCAmelCase_				:   Union[str, Any]							= parent
       UpperCAmelCase_				:   Dict							= batch_size
       UpperCAmelCase_				:   Union[str, Any]							= seq_length
       UpperCAmelCase_				:   Any							= is_training
       UpperCAmelCase_				:   Tuple							= use_attention_mask
       UpperCAmelCase_				:   Dict							= use_token_type_ids
       UpperCAmelCase_				:   str							= use_labels
       UpperCAmelCase_				:   Union[str, Any]							= vocab_size
       UpperCAmelCase_				:   Union[str, Any]							= hidden_size
       UpperCAmelCase_				:   Union[str, Any]							= embedding_size
       UpperCAmelCase_				:   Optional[int]							= num_hidden_layers
       UpperCAmelCase_				:   Tuple							= num_attention_heads
       UpperCAmelCase_				:   List[Any]							= intermediate_size
       UpperCAmelCase_				:   Union[str, Any]							= hidden_act
       UpperCAmelCase_				:   Any							= hidden_dropout_prob
       UpperCAmelCase_				:   List[str]							= attention_probs_dropout_prob
       UpperCAmelCase_				:   List[str]							= max_position_embeddings
       UpperCAmelCase_				:   Dict							= type_vocab_size
       UpperCAmelCase_				:   Tuple							= type_sequence_label_size
       UpperCAmelCase_				:   Dict							= initializer_range
       UpperCAmelCase_				:   Any							= num_choices





    def        _lowerCamelCase			(						self					: Optional[int]						):





       '''simple docstring'''
       UpperCAmelCase_				:   Any							= ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size						)

       UpperCAmelCase_				:   Any							= None
       if self.use_attention_mask:
          UpperCAmelCase_				:   List[str]							= random_attention_mask([self.batch_size, self.seq_length]						)

       UpperCAmelCase_				:   List[str]							= None
       if self.use_token_type_ids:
          UpperCAmelCase_				:   str							= ids_tensor([self.batch_size, self.seq_length]							, self.type_vocab_size						)

       UpperCAmelCase_				:   int							= ElectraConfig(
           vocab_size=self.vocab_size							, hidden_size=self.hidden_size							, embedding_size=self.embedding_size							, num_hidden_layers=self.num_hidden_layers							, num_attention_heads=self.num_attention_heads							, intermediate_size=self.intermediate_size							, hidden_act=self.hidden_act							, hidden_dropout_prob=self.hidden_dropout_prob							, attention_probs_dropout_prob=self.attention_probs_dropout_prob							, max_position_embeddings=self.max_position_embeddings							, type_vocab_size=self.type_vocab_size							, initializer_range=self.initializer_range							, )

       return config, input_ids, token_type_ids, attention_mask





    def        _lowerCamelCase			(						self					: int						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[str]							= self.prepare_config_and_inputs()
       UpperCAmelCase_		,   UpperCAmelCase_		,   UpperCAmelCase_		,   UpperCAmelCase_				:   Union[str, Any]							= config_and_inputs
       UpperCAmelCase_				:   Optional[Any]							= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
       return config, inputs_dict





@require_flax
class 			lowerCAmelCase__(     snake_case__     ,     unittest.TestCase    ):






    '''simple docstring'''





    A_	:  Any								=			True

    A_	:  int								=			(
        (
            FlaxElectraModel,
            FlaxElectraForCausalLM,
            FlaxElectraForMaskedLM,
            FlaxElectraForPreTraining,
            FlaxElectraForTokenClassification,
            FlaxElectraForQuestionAnswering,
            FlaxElectraForMultipleChoice,
            FlaxElectraForSequenceClassification,
        )
        if is_flax_available()
        else ()
    )





    def        _lowerCamelCase			(						self					: Optional[int]						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[str]							= FlaxElectraModelTester(self						)





    @slow
    def        _lowerCamelCase			(						self					: Union[str, Any]						):





       '''simple docstring'''
       for model_class_name in self.all_model_classes:
          if model_class_name == FlaxElectraForMaskedLM:
             UpperCAmelCase_				:   List[str]							= model_class_name.from_pretrained('''google/electra-small-generator'''						)
          else:
             UpperCAmelCase_				:   Optional[Any]							= model_class_name.from_pretrained('''google/electra-small-discriminator'''						)
          UpperCAmelCase_				:   Tuple							= model(np.ones((1, 1)						)						)
          self.assertIsNotNone(__snake_case						)