

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

       lowerCamelCase     =      True
except ImportError:
       lowerCamelCase     =      False


# Cache this result has it's a C FFI call which can be pretty time-consuming
lowerCamelCase     =      torch.distributed.is_available()




def     UpperCAmelCase__       (    _A   :				Tuple							):





  '''simple docstring'''







  a__						=importlib.util.find_spec(UpperCamelCase_							) is not None
  if package_exists:
    try:
      a__						=importlib.metadata.metadata(UpperCamelCase_							)
      return True
    except importlib.metadata.PackageNotFoundError:
      return False




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _torch_distributed_available




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  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     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return importlib.metadata.version('''oneccl_bind_pt'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''transformer_engine'''							)




@lru_cache
def     UpperCAmelCase__       (    _A   :				Optional[Any]=True							):





  '''simple docstring'''







  if torch.cuda.is_available():
    return False
  if _tpu_available and check_device:
    try:
      # Will raise a RuntimeError if no XLA configuration is found
      a__						=xm.xla_device()
      return True
    except RuntimeError:
      return False
  return _tpu_available




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''deepspeed'''							)




def     UpperCAmelCase__       (    _A   :				List[Any]=False							):





  '''simple docstring'''







  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     UpperCAmelCase__       (    ):





  '''simple docstring'''







  a__						=_is_package_available('''bitsandbytes'''							)
  if package_exists:
    a__						=version.parse(importlib.metadata.version('''bitsandbytes'''							)							)
    return compare_versions(UpperCamelCase_			,   '''>='''			,   '''0.39.0'''							)
  return False




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  a__						=_is_package_available('''bitsandbytes'''							)
  if package_exists:
    a__						=version.parse(importlib.metadata.version('''bitsandbytes'''							)							)
    return compare_versions(UpperCamelCase_			,   '''>='''			,   '''0.37.2'''							)
  return False




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''bitsandbytes'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  if strtobool(os.environ.get('''ACCELERATE_USE_MEGATRON_LM'''			,   '''False'''							)							) == 1:
    a__						=importlib.util.find_spec('''megatron'''							) is not None
    if package_exists:
      try:
        a__						=parse(importlib.metadata.version('''megatron-lm'''							)							)
        return compare_versions(UpperCamelCase_			,   '''>='''			,   '''2.2.0'''							)
      except Exception as e:
        warnings.warn(F"""Parse Megatron version failed. Exception:{e}"""							)
        return False




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''safetensors'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''transformers'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''datasets'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''aim'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''tensorboard'''							) or _is_package_available('''tensorboardX'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''wandb'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''comet_ml'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''boto3'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  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'''			,   UpperCamelCase_							)
    return parse_flag_from_env('''ACCELERATE_ENABLE_RICH'''			,   UpperCamelCase_							)
  return False




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''sagemaker'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''tqdm'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return _is_package_available('''mlflow'''							)




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  return is_torch_version('''>='''			,   '''1.12'''							) and torch.backends.mps.is_available() and torch.backends.mps.is_built()




def     UpperCAmelCase__       (    ):





  '''simple docstring'''







  def get_major_and_minor_from_version(_A   :				List[str]							):
    return str(version.parse(UpperCamelCase_							).major							) + "." + str(version.parse(UpperCamelCase_							).minor							)

  a__						=importlib.metadata.version('''torch'''							)
  if importlib.util.find_spec('''intel_extension_for_pytorch'''							) is None:
    return False
  a__						='''N/A'''
  try:
    a__						=importlib.metadata.version('''intel_extension_for_pytorch'''							)
  except importlib.metadata.PackageNotFoundError:
    return False
  a__						=get_major_and_minor_from_version(_torch_version							)
  a__						=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     UpperCAmelCase__       (    _A   :				str=False							):





  '''simple docstring'''







  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
      a__						=torch.npu.device_count()
      return torch.npu.is_available()
    except RuntimeError:
      return False
  return hasattr(UpperCamelCase_			,   '''npu'''							) and torch.npu.is_available()




@lru_cache
def     UpperCAmelCase__       (    _A   :				Any=False							):





  '''simple docstring'''







  if not parse_flag_from_env('''ACCELERATE_USE_XPU'''			,   default=UpperCamelCase_							):
    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
      a__						=torch.xpu.device_count()
      return torch.xpu.is_available()
    except RuntimeError:
      return False
  return hasattr(UpperCamelCase_			,   '''xpu'''							) and torch.xpu.is_available()



import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments

import datasets


lowerCamelCase     =      '''\
@article{eddine2021frugalscore,
  title={FrugalScore: Learning Cheaper, Lighter and Faster Evaluation Metrics for Automatic Text Generation},
  author={Eddine, Moussa Kamal and Shang, Guokan and Tixier, Antoine J-P and Vazirgiannis, Michalis},
  journal={arXiv preprint arXiv:2110.08559},
  year={2021}
}
'''

lowerCamelCase     =      '''\
FrugalScore is a reference-based metric for NLG models evaluation. It is based on a distillation approach that allows to learn a fixed, low cost version of any expensive NLG metric, while retaining most of its original performance.
'''


lowerCamelCase     =      '''
Calculates how good are predictions given some references, using certain scores.
Args:
    predictions (list of str): list of predictions to score. Each predictions
        should be a string.
    references (list of str): list of reference for each prediction. Each
        reference should be a string.
    batch_size (int): the batch size for predictions.
    max_length (int): maximum sequence length.
    device (str): either gpu or cpu
Returns:
    scores (list of int): list of scores.
Examples:
    >>> frugalscore = datasets.load_metric("frugalscore")
    >>> results = frugalscore.compute(predictions=[\'hello there\', \'huggingface\'], references=[\'hello world\', \'hugging face\'])
    >>> print([round(s, 3) for s in results["scores"]])
    [0.631, 0.645]
'''







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION	, _KWARGS_DESCRIPTION							)
class 		__magic_name__    (							datasets.Metric							):

   '''simple docstring'''




   def 				_UpperCAmelCase				(       self )							->					Optional[int]:






     """simple docstring"""






     return datasets.MetricInfo(
         description=_DESCRIPTION,					citation=_CITATION,					inputs_description=_KWARGS_DESCRIPTION,					features=datasets.Features(
             {
                 '''predictions''': datasets.Value('''string''' ),
                 '''references''': datasets.Value('''string''' ),
             } ),					homepage='''https://github.com/moussaKam/FrugalScore''',					)



   def 				_UpperCAmelCase				(       self,					lowercase_ )							->					Dict:






     """simple docstring"""






     if self.config_name == "default":
       a__						='''moussaKam/frugalscore_tiny_bert-base_bert-score'''
     else:
       a__						=self.config_name
     a__						=AutoModelForSequenceClassification.from_pretrained(lowercase_ )
     a__						=AutoTokenizer.from_pretrained(lowercase_ )



   def 				_UpperCAmelCase				(       self,					lowercase_,					lowercase_,					lowercase_=32,					lowercase_=128,					lowercase_=None,					)							->					Union[str, Any]:






     """simple docstring"""






     assert len(lowercase_ ) == len(
         lowercase_ ), "predictions and references should have the same number of sentences."
     if device is not None:
       assert device in ["gpu", "cpu"], "device should be either gpu or cpu."
     else:
       a__						='''gpu''' if torch.cuda.is_available() else '''cpu'''
     a__						=TrainingArguments(
         '''trainer''',					fpaa=(device == '''gpu'''),					per_device_eval_batch_size=lowercase_,					report_to='''all''',					no_cuda=(device == '''cpu'''),					log_level='''warning''',					)
     a__						={'''sentence1''': predictions, '''sentence2''': references}
     a__						=datasets.Dataset.from_dict(lowercase_ )

     def tokenize_function(lowercase_ ):
       return self.tokenizer(
           data['''sentence1'''],					data['''sentence2'''],					max_length=lowercase_,					truncation=lowercase_,					padding=lowercase_ )

     a__						=raw_datasets.map(lowercase_,					batched=lowercase_ )
     tokenized_datasets.remove_columns(['''sentence1''', '''sentence2'''] )
     a__						=Trainer(self.model,					lowercase_,					tokenizer=self.tokenizer )
     a__						=trainer.predict(lowercase_ )
     return {"scores": list(predictions.predictions.squeeze(-1 ) )}
