

import asyncio
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
from distutils.util import strtobool
from functools import partial
from pathlib import Path
from typing import List, Union
from unittest import mock

import torch

from ..state import AcceleratorState, PartialState
from ..utils import (
    gather,
    is_bnb_available,
    is_comet_ml_available,
    is_datasets_available,
    is_deepspeed_available,
    is_mps_available,
    is_safetensors_available,
    is_tensorboard_available,
    is_torch_version,
    is_tpu_available,
    is_transformers_available,
    is_wandb_available,
    is_xpu_available,
)





def      A__					(			__A  :						Optional[Any]     ,				__A  :						Any=False			)					->Optional[int]:
      try:
            __A    =os.environ[key]
      except KeyError:
            # KEY isn't set, default to `default`.
            __A    =default
      else:
            # KEY is set, convert it to True or False.
            try:
                  __A    =strtobool(__A			)
            except ValueError:
                  # More values are supported, but let's keep the message simple.
                  raise ValueError(F'''If set, {key} must be yes or no.'''			)
      return _value


_lowerCamelCase      :					Dict					    =      parse_flag_from_env('''RUN_SLOW''', default=False)





def      A__					(			__A  :						Union[str, Any]			)					->Tuple:
      return unittest.skip('''Test was skipped'''			)(__A			)





def      A__					(			__A  :						Dict			)					->Optional[Any]:
      return unittest.skipUnless(_run_slow_tests     ,				'''test is slow'''			)(__A			)





def      A__					(			__A  :						Tuple			)					->Any:
      return unittest.skipUnless(not torch.cuda.is_available()     ,				'''test requires only a CPU'''			)(__A			)





def      A__					(			__A  :						List[str]			)					->List[str]:
      return unittest.skipUnless(torch.cuda.is_available()     ,				'''test requires a GPU'''			)(__A			)





def      A__					(			__A  :						str			)					->Optional[int]:
      return unittest.skipUnless(is_xpu_available()     ,				'''test requires a XPU'''			)(__A			)





def      A__					(			__A  :						List[Any]			)					->str:
      return unittest.skipUnless(is_mps_available()     ,				'''test requires a `mps` backend support in `torch`'''			)(__A			)





def      A__					(			__A  :						int			)					->Dict:
      return unittest.skipUnless(
          is_transformers_available() and is_datasets_available()     ,				'''test requires the Hugging Face suite'''			)(__A			)





def      A__					(			__A  :						Tuple			)					->Dict:
      return unittest.skipUnless(is_bnb_available()     ,				'''test requires the bitsandbytes library'''			)(__A			)





def      A__					(			__A  :						List[Any]			)					->Optional[int]:
      return unittest.skipUnless(is_tpu_available()     ,				'''test requires TPU'''			)(__A			)





def      A__					(			__A  :						str			)					->List[Any]:
      return unittest.skipUnless(torch.cuda.device_count() == 1     ,				'''test requires a GPU'''			)(__A			)





def      A__					(			__A  :						Any			)					->Optional[int]:
      return unittest.skipUnless(torch.xpu.device_count() == 1     ,				'''test requires a XPU'''			)(__A			)





def      A__					(			__A  :						Optional[int]			)					->List[Any]:
      return unittest.skipUnless(torch.cuda.device_count() > 1     ,				'''test requires multiple GPUs'''			)(__A			)





def      A__					(			__A  :						Any			)					->Tuple:
      return unittest.skipUnless(torch.xpu.device_count() > 1     ,				'''test requires multiple XPUs'''			)(__A			)





def      A__					(			__A  :						Optional[int]			)					->Optional[Any]:
      return unittest.skipUnless(is_safetensors_available()     ,				'''test requires safetensors'''			)(__A			)





def      A__					(			__A  :						List[str]			)					->List[Any]:
      return unittest.skipUnless(is_deepspeed_available()     ,				'''test requires DeepSpeed'''			)(__A			)





def      A__					(			__A  :						int			)					->Optional[int]:
      return unittest.skipUnless(is_torch_version('''>='''     ,				'''1.12.0'''			)     ,				'''test requires torch version >= 1.12.0'''			)(__A			)





def      A__					(			__A  :						Any=None     ,				__A  :						Any=None			)					->int:
      if test_case is None:
            return partial(__A     ,				version=__A			)
      return unittest.skipUnless(is_torch_version('''>='''     ,				__A			)     ,				F'''test requires torch version >= {version}'''			)(__A			)





def      A__					(			__A  :						str			)					->List[Any]:
      return unittest.skipUnless(is_tensorboard_available()     ,				'''test requires Tensorboard'''			)(__A			)





def      A__					(			__A  :						List[Any]			)					->Optional[Any]:
      return unittest.skipUnless(is_wandb_available()     ,				'''test requires wandb'''			)(__A			)





def      A__					(			__A  :						Optional[Any]			)					->Union[str, Any]:
      return unittest.skipUnless(is_comet_ml_available()     ,				'''test requires comet_ml'''			)(__A			)


_lowerCamelCase      :					Any					    =      (
    any([is_wandb_available(), is_tensorboard_available()]) and not is_comet_ml_available()
)





def      A__					(			__A  :						Tuple			)					->Tuple:
      return unittest.skipUnless(
          _atleast_one_tracker_available     ,				'''test requires at least one tracker to be available and for `comet_ml` to not be installed'''     ,				)(__A			)





class        lowerCAmelCase__  (					unittest.TestCase			):



       '''simple docstring'''







       lowercase_						=    True

       @classmethod
       def     __UpperCamelCase     (							cls     ):







             '''simple docstring'''



             __A    =tempfile.mkdtemp()

       @classmethod
       def     __UpperCamelCase     (							cls     ):







             '''simple docstring'''



             if os.path.exists(cls.tmpdir     ):
                   shutil.rmtree(cls.tmpdir     )


       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             if self.clear_on_setup:
                   for path in Path(self.tmpdir     ).glob('''**/*'''     ):
                         if path.is_file():
                               path.unlink()
                         elif path.is_dir():
                               shutil.rmtree(lowercase__     )





class        lowerCAmelCase__  (					unittest.TestCase			):



       '''simple docstring'''







       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             super().tearDown()
             # Reset the state of the AcceleratorState singleton.
             AcceleratorState._reset_state()
             PartialState._reset_state()





class        lowerCAmelCase__  (					unittest.TestCase			):



       '''simple docstring'''







       def     __UpperCamelCase     (							self ,   lowercase__     ):







             '''simple docstring'''



             __A    =mocks if isinstance(lowercase__ ,   (tuple, list)     ) else [mocks]
             for m in self.mocks:
                   m.start()
                   self.addCleanup(m.stop     )





def      A__					(			__A  :						Dict			)					->Union[str, Any]:
      __A    =AcceleratorState()
      __A    =tensor[None].clone().to(state.device			)
      __A    =gather(__A			).cpu()
      __A    =tensor[0].cpu()
      for i in range(tensors.shape[0]			):
            if not torch.equal(tensors[i]     ,				__A			):
                  return False
      return True





class        lowerCAmelCase__  :



       '''simple docstring'''







       def __init__(							self ,   lowercase__ ,   lowercase__ ,   lowercase__     ):







             '''simple docstring'''



             __A    =returncode
             __A    =stdout
             __A    =stderr





async def      A__					(			__A  :						List[Any]     ,				__A  :						Optional[int]			)					->List[str]:
      while True:
            __A    =await stream.readline()
            if line:
                  callback(__A			)
            else:
                  break





async def      A__					(			__A  :						List[str]     ,				__A  :						Optional[Any]=None     ,				__A  :						List[str]=None     ,				__A  :						Tuple=None     ,				__A  :						Optional[int]=False     ,				__A  :						Optional[int]=False			)					->_RunOutput:
      if echo:
            print('''\nRunning: '''     ,				''' '''.join(__A			)			)

      __A    =await asyncio.create_subprocess_exec(
          cmd[0]     ,				*cmd[1:]     ,				stdin=__A     ,				stdout=asyncio.subprocess.PIPE     ,				stderr=asyncio.subprocess.PIPE     ,				env=__A     ,				)

      # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe
      # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait
      #
      # If it starts hanging, will need to switch to the following code. The problem is that no data
      # will be seen until it's done and if it hangs for example there will be no debug info.
      # out, err = await p.communicate()
      # return _RunOutput(p.returncode, out, err)

      __A    =[]
      __A    =[]

      def tee(__A  :						Union[str, Any]     ,				__A  :						str     ,				__A  :						Tuple     ,				__A  :						Union[str, Any]=""			):
            __A    =line.decode('''utf-8'''			).rstrip()
            sink.append(__A			)
            if not quiet:
                  print(__A     ,				__A     ,				file=__A			)

    # XXX: the timeout doesn't seem to make any difference here
      await asyncio.wait(
          [
              asyncio.create_task(_read_stream(p.stdout     ,				lambda __A			: tee(__A     ,				__A     ,				sys.stdout     ,				label='''stdout:'''			)			)			),
              asyncio.create_task(_read_stream(p.stderr     ,				lambda __A			: tee(__A     ,				__A     ,				sys.stderr     ,				label='''stderr:'''			)			)			),
          ]     ,				timeout=__A     ,				)
      return _RunOutput(await p.wait()     ,				__A     ,				__A			)





def      A__					(			__A  :						int     ,				__A  :						Optional[Any]=None     ,				__A  :						Tuple=None     ,				__A  :						Dict=1_80     ,				__A  :						Optional[Any]=False     ,				__A  :						Dict=True			)					->_RunOutput:
      __A    =asyncio.get_event_loop()
      __A    =loop.run_until_complete(
          _stream_subprocess(__A     ,				env=__A     ,				stdin=__A     ,				timeout=__A     ,				quiet=__A     ,				echo=__A			)			)

      __A    =''' '''.join(__A			)
      if result.returncode > 0:
            __A    ='''\n'''.join(result.stderr			)
            raise RuntimeError(
                F'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n'''
                F'''The combined stderr from workers follows:\n{stderr}'''			)

      return result





class        lowerCAmelCase__  (					__magic_name__			):



       '''simple docstring'''







       pass





def      A__					(			__A  :						List[str]     ,				__A  :						Any=False			)					->List[str]:
      try:
            __A    =subprocess.check_output(__A     ,				stderr=subprocess.STDOUT			)
            if return_stdout:
                  if hasattr(__A     ,				'''decode'''			):
                        __A    =output.decode('''utf-8'''			)
                  return output
      except subprocess.CalledProcessError as e:
            raise SubprocessCallException(
                F'''Command `{' '.join(__A			)}` failed with the following error:\n\n{e.output.decode()}'''			) from e





import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


_lowerCamelCase      :					List[str]					    =      version.parse(importlib_metadata.version('''nltk'''))
if NLTK_VERSION >= version.Version('''3.6.4'''):
   from nltk import word_tokenize


_lowerCamelCase      :					Dict					    =      '''\
@inproceedings{banarjee2005,
  title     = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},
  author    = {Banerjee, Satanjeev  and Lavie, Alon},
  booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},
  month     = jun,
  year      = {2005},
  address   = {Ann Arbor, Michigan},
  publisher = {Association for Computational Linguistics},
  url       = {https://www.aclweb.org/anthology/W05-0909},
  pages     = {65--72},
}
'''

_lowerCamelCase      :					str					    =      '''\
METEOR, an automatic metric for machine translation evaluation
that is based on a generalized concept of unigram matching between the
machine-produced translation and human-produced reference translations.
Unigrams can be matched based on their surface forms, stemmed forms,
and meanings; furthermore, METEOR can be easily extended to include more
advanced matching strategies. Once all generalized unigram matches
between the two strings have been found, METEOR computes a score for
this matching using a combination of unigram-precision, unigram-recall, and
a measure of fragmentation that is designed to directly capture how
well-ordered the matched words in the machine translation are in relation
to the reference.

METEOR gets an R correlation value of 0.347 with human evaluation on the Arabic
data and 0.331 on the Chinese data. This is shown to be an improvement on
using simply unigram-precision, unigram-recall and their harmonic F1
combination.
'''

_lowerCamelCase      :					Union[str, Any]					    =      '''
Computes METEOR score of translated segments against one or more references.
Args:
    predictions: list of predictions to score. Each prediction
        should be a string with tokens separated by spaces.
    references: list of reference for each prediction. Each
        reference should be a string with tokens separated by spaces.
    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9
    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3
    gamma: Relative weight assigned to fragmentation penalty. default: 0.5
Returns:
    \'meteor\': meteor score.
Examples:

    >>> meteor = datasets.load_metric(\'meteor\')
    >>> predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party"]
    >>> references = ["It is a guide to action that ensures that the military will forever heed Party commands"]
    >>> results = meteor.compute(predictions=predictions, references=references)
    >>> print(round(results["meteor"], 4))
    0.6944
'''





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



       '''simple docstring'''







       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             return datasets.MetricInfo(
                 description=_DESCRIPTION ,   citation=_CITATION ,   inputs_description=_KWARGS_DESCRIPTION ,   features=datasets.Features(
                     {
                         '''predictions''': datasets.Value('''string''' ,   id='''sequence'''     ),
                         '''references''': datasets.Value('''string''' ,   id='''sequence'''     ),
                     }     ) ,   codebase_urls=['''https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py'''] ,   reference_urls=[
                     '''https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score''',
                     '''https://en.wikipedia.org/wiki/METEOR''',
                 ] ,   )

       def     __UpperCamelCase     (							self ,   lowercase__     ):







             '''simple docstring'''



             import nltk

             nltk.download('''wordnet'''     )
             if NLTK_VERSION >= version.Version('''3.6.5'''     ):
                   nltk.download('''punkt'''     )
             if NLTK_VERSION >= version.Version('''3.6.6'''     ):
                   nltk.download('''omw-1.4'''     )


       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__=0.9 ,   lowercase__=3 ,   lowercase__=0.5     ):







             '''simple docstring'''



             if NLTK_VERSION >= version.Version('''3.6.5'''     ):
                   __A    =[
                       meteor_score.single_meteor_score(
                           word_tokenize(lowercase__     ) ,   word_tokenize(lowercase__     ) ,   alpha=lowercase__ ,   beta=lowercase__ ,   gamma=lowercase__     )
                       for ref, pred in zip(lowercase__ ,   lowercase__     )
                   ]
             else:
                   __A    =[
                       meteor_score.single_meteor_score(lowercase__ ,   lowercase__ ,   alpha=lowercase__ ,   beta=lowercase__ ,   gamma=lowercase__     )
                       for ref, pred in zip(lowercase__ ,   lowercase__     )
                   ]

             return {"meteor": np.mean(lowercase__     )}


