



"""simple docstring"""
import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

A:     Any										=	[
    "cross_validation.py",
    "gradient_accumulation.py",
    "local_sgd.py",
    "multi_process_metrics.py",
    "memory.py",
    "automatic_gradient_accumulation.py",
    "fsdp_with_peak_mem_tracking.py",
    "deepspeed_with_config_support.py",
    "megatron_lm_gpt_pretraining.py",
]



class    SCREAMING_SNAKE_CASE__						(      unittest.TestCase				):

   def 			SCREAMING_SNAKE_CASE  (     self   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE = None   ,  _SCREAMING_SNAKE_CASE = None		)						->   Tuple:





    '''simple docstring'''




    UpperCAmelCase			:      str			=			None
    UpperCAmelCase			:      Optional[Any]			=			os.path.abspath(os.path.join("""examples"""   ,  """by_feature"""		)		)
    UpperCAmelCase			:      str			=			os.path.abspath("""examples"""		)
    for item in os.listdir(_SCREAMING_SNAKE_CASE		):
     if item not in EXCLUDE_EXAMPLES:
      UpperCAmelCase			:      int			=			os.path.join(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)
      if os.path.isfile(_SCREAMING_SNAKE_CASE		) and ".py" in item_path:
       with self.subTest(
           tested_script=_SCREAMING_SNAKE_CASE   ,  feature_script=_SCREAMING_SNAKE_CASE   ,  tested_section="""main()""" if parser_only else """training_function()"""   ,  ):
        UpperCAmelCase			:      Optional[int]			=			compare_against_test(
            os.path.join(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)
        UpperCAmelCase			:      List[str]			=			"""\n""".join(_SCREAMING_SNAKE_CASE		)
        if special_strings is not None:
         for string in special_strings:
          UpperCAmelCase			:      List[Any]			=			diff.replace(_SCREAMING_SNAKE_CASE   ,  """"""		)
        self.assertEqual(_SCREAMING_SNAKE_CASE   ,  """"""		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   List[Any]:





    '''simple docstring'''




    self.one_complete_example("""complete_nlp_example.py"""   ,  _SCREAMING_SNAKE_CASE		)
    self.one_complete_example("""complete_nlp_example.py"""   ,  _SCREAMING_SNAKE_CASE		)




   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Dict:





    '''simple docstring'''




    UpperCAmelCase			:      Tuple			=			os.path.abspath(os.path.join("""examples"""   ,  """cv_example.py"""		)		)
    UpperCAmelCase			:      Tuple			=			[
        """ """ * 16 + """{\n\n""",
        """ """ * 20 + """\"accuracy\": eval_metric[\"accuracy\"],\n\n""",
        """ """ * 20 + """\"f1\": eval_metric[\"f1\"],\n\n""",
        """ """ * 20 + """\"train_loss\": total_loss.item() / len(train_dataloader),\n\n""",
        """ """ * 20 + """\"epoch\": epoch,\n\n""",
        """ """ * 16 + """},\n\n""",
        """ """ * 16 + """step=epoch,\n""",
        """ """ * 12,
        """ """ * 8 + """for step, batch in enumerate(active_dataloader):\n""",
    ]
    self.one_complete_example("""complete_cv_example.py"""   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)
    self.one_complete_example("""complete_cv_example.py"""   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)



@mock.patch.dict(os.environ   ,    {'TESTING_MOCKED_DATALOADERS': '1'}				)
class    SCREAMING_SNAKE_CASE__						(      UpperCAmelCase__				):
   __lowerCAmelCase						:     Optional[int]		=		False

   @classmethod
   def 			SCREAMING_SNAKE_CASE  (     cls		)						->   Any:





    '''simple docstring'''




    super().setUpClass()
    UpperCAmelCase			:      List[str]			=			tempfile.mkdtemp()
    UpperCAmelCase			:      Union[str, Any]			=			os.path.join(cls._tmpdir   ,  """default_config.yml"""		)

    write_basic_config(save_location=cls.configPath		)
    UpperCAmelCase			:      Union[str, Any]			=			["""accelerate""", """launch""", """--config_file""", cls.configPath]

   @classmethod
   def 			SCREAMING_SNAKE_CASE  (     cls		)						->   str:





    '''simple docstring'''




    super().tearDownClass()
    shutil.rmtree(cls._tmpdir		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Optional[Any]:





    '''simple docstring'''




    UpperCAmelCase			:      Tuple			=			F"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps epoch\n        --output_dir {self.tmpdir}\n        ".split()
    run_command(self._launch_args + testargs		)
    self.assertTrue(os.path.exists(os.path.join(self.tmpdir   ,  """epoch_0"""		)		)		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Union[str, Any]:





    '''simple docstring'''




    UpperCAmelCase			:      Union[str, Any]			=			F"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        ".split()
    UpperCAmelCase			:      Tuple			=			run_command(self._launch_args + testargs		)
    self.assertTrue(os.path.exists(os.path.join(self.tmpdir   ,  """step_2"""		)		)		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Tuple:





    '''simple docstring'''




    UpperCAmelCase			:      List[Any]			=			F"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir   ,  'epoch_0'		)}\n        ".split()
    UpperCAmelCase			:      str			=			run_command(self._launch_args + testargs   ,  return_stdout=_SCREAMING_SNAKE_CASE		)
    self.assertNotIn("""epoch 0:"""   ,  _SCREAMING_SNAKE_CASE		)
    self.assertIn("""epoch 1:"""   ,  _SCREAMING_SNAKE_CASE		)

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





    '''simple docstring'''




    UpperCAmelCase			:      Any			=			F"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir   ,  'step_2'		)}\n        ".split()
    UpperCAmelCase			:      Any			=			run_command(self._launch_args + testargs   ,  return_stdout=_SCREAMING_SNAKE_CASE		)
    if torch.cuda.is_available():
     UpperCAmelCase			:      Any			=			torch.cuda.device_count()
    else:
     UpperCAmelCase			:      Union[str, Any]			=			1
    if num_processes > 1:
     self.assertNotIn("""epoch 0:"""   ,  _SCREAMING_SNAKE_CASE		)
     self.assertIn("""epoch 1:"""   ,  _SCREAMING_SNAKE_CASE		)
    else:
     self.assertIn("""epoch 0:"""   ,  _SCREAMING_SNAKE_CASE		)
     self.assertIn("""epoch 1:"""   ,  _SCREAMING_SNAKE_CASE		)

   @slow
   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Tuple:





    '''simple docstring'''




    UpperCAmelCase			:      Dict			=			"""
        examples/by_feature/cross_validation.py
        --num_folds 2
        """.split()
    with mock.patch.dict(os.environ   ,  {"""TESTING_MOCKED_DATALOADERS""": """0"""}		):
     UpperCAmelCase			:      Dict			=			run_command(self._launch_args + testargs   ,  return_stdout=_SCREAMING_SNAKE_CASE		)
     UpperCAmelCase			:      Tuple			=			re.findall("""({.+})"""   ,  _SCREAMING_SNAKE_CASE		)
     UpperCAmelCase			:      Tuple			=			[r for r in results if """accuracy""" in r][-1]
     UpperCAmelCase			:      str			=			ast.literal_eval(_SCREAMING_SNAKE_CASE		)
     self.assertGreaterEqual(results["""accuracy"""]   ,  0.75		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   List[Any]:





    '''simple docstring'''




    UpperCAmelCase			:      int			=			["""examples/by_feature/multi_process_metrics.py"""]
    run_command(self._launch_args + testargs		)

   @require_trackers
   @mock.patch.dict(os.environ   ,  {"""WANDB_MODE""": """offline"""}		)
   def 			SCREAMING_SNAKE_CASE  (     self		)						->   str:





    '''simple docstring'''




    with tempfile.TemporaryDirectory() as tmpdir:
     UpperCAmelCase			:      List[Any]			=			F"\n            examples/by_feature/tracking.py\n            --with_tracking\n            --project_dir {tmpdir}\n            ".split()
     run_command(self._launch_args + testargs		)
     self.assertTrue(os.path.exists(os.path.join(_SCREAMING_SNAKE_CASE   ,  """tracking"""		)		)		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Tuple:





    '''simple docstring'''




    UpperCAmelCase			:      str			=			["""examples/by_feature/gradient_accumulation.py"""]
    run_command(self._launch_args + testargs		)




   def 			SCREAMING_SNAKE_CASE  (     self		)						->   List[Any]:





    '''simple docstring'''




    UpperCAmelCase			:      List[Any]			=			["""examples/by_feature/local_sgd.py"""]
    run_command(self._launch_args + testargs		)







"""simple docstring"""
import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin



class    SCREAMING_SNAKE_CASE__						(      UpperCAmelCase__   ,    unittest.TestCase				):
   __lowerCAmelCase						:     List[Any]		=		TransfoXLTokenizer
   __lowerCAmelCase						:     Optional[int]		=		False
   __lowerCAmelCase						:     Optional[Any]		=		False

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   List[str]:





    '''simple docstring'''




    super().setUp()

    UpperCAmelCase			:      Optional[Any]			=			[
        """<unk>""",
        """[CLS]""",
        """[SEP]""",
        """want""",
        """unwanted""",
        """wa""",
        """un""",
        """running""",
        """,""",
        """low""",
        """l""",
    ]
    UpperCAmelCase			:      int			=			os.path.join(self.tmpdirname   ,  VOCAB_FILES_NAMES["""vocab_file"""]		)
    with open(self.vocab_file   ,  """w"""   ,  encoding="""utf-8"""		) as vocab_writer:
     vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]		)		)

   def 			SCREAMING_SNAKE_CASE  (     self   ,  **_SCREAMING_SNAKE_CASE		)						->   List[Any]:





    '''simple docstring'''




    UpperCAmelCase			:      Optional[Any]			=			True
    return TransfoXLTokenizer.from_pretrained(self.tmpdirname   ,  **_SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self   ,  _SCREAMING_SNAKE_CASE		)						->   List[Any]:





    '''simple docstring'''




    UpperCAmelCase			:      int			=			"""<unk> UNwanted , running"""
    UpperCAmelCase			:      Dict			=			"""<unk> unwanted, running"""
    return input_text, output_text

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Any:





    '''simple docstring'''




    UpperCAmelCase			:      List[str]			=			TransfoXLTokenizer(vocab_file=self.vocab_file   ,  lower_case=_SCREAMING_SNAKE_CASE		)

    UpperCAmelCase			:      Dict			=			tokenizer.tokenize("""<unk> UNwanted , running"""		)
    self.assertListEqual(_SCREAMING_SNAKE_CASE   ,  ["""<unk>""", """unwanted""", """,""", """running"""]		)

    self.assertListEqual(tokenizer.convert_tokens_to_ids(_SCREAMING_SNAKE_CASE		)   ,  [0, 4, 8, 7]		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Tuple:





    '''simple docstring'''




    UpperCAmelCase			:      Tuple			=			TransfoXLTokenizer(lower_case=_SCREAMING_SNAKE_CASE		)

    self.assertListEqual(
        tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """		)   ,  ["""hello""", """!""", """how""", """are""", """you""", """?"""]		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Optional[Any]:





    '''simple docstring'''




    UpperCAmelCase			:      Optional[Any]			=			TransfoXLTokenizer(lower_case=_SCREAMING_SNAKE_CASE		)

    self.assertListEqual(
        tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """		)   ,  ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   int:





    '''simple docstring'''




    UpperCAmelCase			:      List[Any]			=			TransfoXLTokenizer(lower_case=_SCREAMING_SNAKE_CASE		)
    UpperCAmelCase			:      Dict			=			"""Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
    UpperCAmelCase			:      Optional[Any]			=			[
        """Hello""",
        """(""",
        """bracket""",
        """)""",
        """and""",
        """side""",
        """@-@""",
        """scrolled""",
        """[""",
        """and""",
        """]""",
        """Henry""",
        """'s""",
        """$""",
        """5""",
        """@,@""",
        """000""",
        """with""",
        """3""",
        """@.@""",
        """34""",
        """m""",
        """.""",
        """What""",
        """'s""",
        """up""",
        """!""",
        """?""",
    ]

    self.assertListEqual(tokenizer.tokenize(_SCREAMING_SNAKE_CASE		)   ,  _SCREAMING_SNAKE_CASE		)

    self.assertEqual(tokenizer.convert_tokens_to_string(_SCREAMING_SNAKE_CASE		)   ,  _SCREAMING_SNAKE_CASE		)




   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Dict:





    '''simple docstring'''




    UpperCAmelCase			:      List[str]			=			self.get_tokenizer()
    UpperCAmelCase			:      Tuple			=			len(_SCREAMING_SNAKE_CASE		)

    tokenizer.add_tokens(["""new1""", """new2"""]		)
    tokenizer.move_added_token("""new1"""   ,  1		)

    # Check that moved token is not copied (duplicate)
    self.assertEqual(len(_SCREAMING_SNAKE_CASE		)   ,  original_len + 2		)
    # Check that token is moved to specified id
    self.assertEqual(tokenizer.encode("""new1"""		)   ,  [1]		)
    self.assertEqual(tokenizer.decode([1]		)   ,  """new1"""		)
