






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)

snake_case     					=		[
    """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       ):
     '''simple docstring'''


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


     def 					_A							(					self							:						List[Any]   ):
       self.one_complete_example("complete_nlp_example.py"       ,			UpperCAmelCase_   )
       self.one_complete_example("complete_nlp_example.py"       ,			UpperCAmelCase_   )






     def 					_A							(					self							:						Any   ):
       SCREAMING_SNAKE_CASE							:   Any						    =     os.path.abspath(os.path.join("examples"       ,			"cv_example.py"   )   )
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     [
           " " * 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"       ,			UpperCAmelCase_       ,			UpperCAmelCase_       ,			UpperCAmelCase_   )
       self.one_complete_example("complete_cv_example.py"       ,			UpperCAmelCase_       ,			UpperCAmelCase_       ,			UpperCAmelCase_   )





@mock.patch.dict(os.environ   ,		{'''TESTING_MOCKED_DATALOADERS''': '''1'''}       )
class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		Optional[int]								=						False


     @classmethod
     def 					_A							(					cls							:						Union[str, Any]   ):
       super().setUpClass()
       SCREAMING_SNAKE_CASE							:   Tuple						    =     tempfile.mkdtemp()
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     os.path.join(cls._tmpdir       ,			"default_config.yml"   )

       write_basic_config(save_location=cls.configPath   )
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     ["accelerate", "launch", "--config_file", cls.configPath]


     @classmethod
     def 					_A							(					cls							:						Union[str, Any]   ):
       super().tearDownClass()
       shutil.rmtree(cls._tmpdir   )


     def 					_A							(					self							:						List[Any]   ):
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     f'''
        examples/by_feature/checkpointing.py
        --checkpointing_steps epoch
        --output_dir {self.tmpdir}
        '''.split()
       run_command(self._launch_args + testargs   )
       self.assertTrue(os.path.exists(os.path.join(self.tmpdir       ,			"epoch_0"   )   )   )


     def 					_A							(					self							:						str   ):
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     f'''
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        '''.split()
       SCREAMING_SNAKE_CASE							:   str						    =     run_command(self._launch_args + testargs   )
       self.assertTrue(os.path.exists(os.path.join(self.tmpdir       ,			"step_2"   )   )   )


     def 					_A							(					self							:						Dict   ):
       SCREAMING_SNAKE_CASE							:   str						    =     f'''
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir       ,			'epoch_0'   )}
        '''.split()
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     run_command(self._launch_args + testargs       ,			return_stdout=UpperCAmelCase_   )
       self.assertNotIn("epoch 0:"       ,			UpperCAmelCase_   )
       self.assertIn("epoch 1:"       ,			UpperCAmelCase_   )


     def 					_A							(					self							:						List[Any]   ):
       SCREAMING_SNAKE_CASE							:   str						    =     f'''
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir       ,			'step_2'   )}
        '''.split()
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     run_command(self._launch_args + testargs       ,			return_stdout=UpperCAmelCase_   )
       if torch.cuda.is_available():
         SCREAMING_SNAKE_CASE							:   str						    =     torch.cuda.device_count()
       else:
         SCREAMING_SNAKE_CASE							:   Dict						    =     1
       if num_processes > 1:
         self.assertNotIn("epoch 0:"       ,			UpperCAmelCase_   )
         self.assertIn("epoch 1:"       ,			UpperCAmelCase_   )
       else:
         self.assertIn("epoch 0:"       ,			UpperCAmelCase_   )
         self.assertIn("epoch 1:"       ,			UpperCAmelCase_   )


     @slow
     def 					_A							(					self							:						Optional[Any]   ):
       SCREAMING_SNAKE_CASE							:   Any						    =     "\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        ".split()
       with mock.patch.dict(os.environ       ,			{"TESTING_MOCKED_DATALOADERS": "0"}   ):
         SCREAMING_SNAKE_CASE							:   int						    =     run_command(self._launch_args + testargs       ,			return_stdout=UpperCAmelCase_   )
         SCREAMING_SNAKE_CASE							:   List[Any]						    =     re.findall("({.+})"       ,			UpperCAmelCase_   )
         SCREAMING_SNAKE_CASE							:   List[str]						    =     [r for r in results if "accuracy" in r][-1]
         SCREAMING_SNAKE_CASE							:   List[Any]						    =     ast.literal_eval(UpperCAmelCase_   )
         self.assertGreaterEqual(results["accuracy"]       ,			0.75   )


     def 					_A							(					self							:						Dict   ):
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     ["examples/by_feature/multi_process_metrics.py"]
       run_command(self._launch_args + testargs   )


     @require_trackers
     @mock.patch.dict(os.environ       ,			{"WANDB_MODE": "offline"}   )
     def 					_A							(					self							:						Union[str, Any]   ):
       with tempfile.TemporaryDirectory() as tmpdir:
         SCREAMING_SNAKE_CASE							:   Any						    =     f'''
            examples/by_feature/tracking.py
            --with_tracking
            --project_dir {tmpdir}
            '''.split()
         run_command(self._launch_args + testargs   )
         self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase_       ,			"tracking"   )   )   )


     def 					_A							(					self							:						str   ):
       SCREAMING_SNAKE_CASE							:   List[str]						    =     ["examples/by_feature/gradient_accumulation.py"]
       run_command(self._launch_args + testargs   )






     def 					_A							(					self							:						Any   ):
       SCREAMING_SNAKE_CASE							:   str						    =     ["examples/by_feature/local_sgd.py"]
       run_command(self._launch_args + testargs   )










'''simple docstring'''






import os
from typing import List, Optional, Union

from ...tokenization_utils import PreTrainedTokenizer
from ...tokenization_utils_base import AddedToken
from ...utils import logging


__UpperCAmelCase       							=				logging.get_logger(__name__)

__UpperCAmelCase       							=				{'''vocab_file''': '''vocab.txt'''}

__UpperCAmelCase       							=				{
    '''vocab_file''': {
        '''facebook/esm2_t6_8M_UR50D''': '''https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt''',
        '''facebook/esm2_t12_35M_UR50D''': '''https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt''',
    },
}

__UpperCAmelCase       							=				{
    '''facebook/esm2_t6_8M_UR50D''': 1_024,
    '''facebook/esm2_t12_35M_UR50D''': 1_024,
}


def    _snake_case  (	A			)		->       Optional[Any]:
							with open(A					,       '''r'''			) as f:
														lowerCAmelCase__			  =      f.read().splitlines()
														return [l.strip() for l in lines]






class 						a__							(				a__    ):





							'''simple docstring'''






							lowercase__     :  Optional[Any]      		= VOCAB_FILES_NAMES
							lowercase__     :  List[Any]      		= PRETRAINED_VOCAB_FILES_MAP
							lowercase__     :  Union[str, Any]      		= PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							lowercase__     :  Union[str, Any]      		= ["input_ids", "attention_mask"]







							def __init__(						self   ,       lowerCamelCase_   ,       lowerCamelCase_="<unk>"   ,       lowerCamelCase_="<cls>"   ,       lowerCamelCase_="<pad>"   ,       lowerCamelCase_="<mask>"   ,       lowerCamelCase_="<eos>"   ,       **lowerCamelCase_   ,       )			->				Tuple:
														super().__init__(**lowerCamelCase_  )
														lowerCAmelCase__			  =      load_vocab_file(lowerCamelCase_  )
														lowerCAmelCase__			  =      dict(enumerate(self.all_tokens  )  )
														lowerCAmelCase__			  =      {tok: ind for ind, tok in enumerate(self.all_tokens  )}
														lowerCAmelCase__			  =      unk_token
														lowerCAmelCase__			  =      cls_token
														lowerCAmelCase__			  =      pad_token
														lowerCAmelCase__			  =      mask_token
														lowerCAmelCase__			  =      eos_token
														lowerCAmelCase__			  =      self.all_tokens
														self._create_trie(self.unique_no_split_tokens  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				str:
														return self._id_to_token.get(lowerCamelCase_   ,       self.unk_token  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				int:
														return self._token_to_id.get(lowerCamelCase_   ,       self._token_to_id.get(self.unk_token  )  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       **lowerCamelCase_  )			->				Union[str, Any]:
														return text.split()







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_=False  )			->				Dict:
														return len(self._id_to_token  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														return {token: i for i, token in enumerate(self.all_tokens  )}







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				int:
														return self._token_to_id.get(lowerCamelCase_   ,       self._token_to_id.get(self.unk_token  )  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				str:
														return self._id_to_token.get(lowerCamelCase_   ,       self.unk_token  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_ = None  )			->				List[int]:
														lowerCAmelCase__			  =      [self.cls_token_id]
														lowerCAmelCase__			  =      [self.eos_token_id]  # No sep token in ESM vocabulary
														if token_ids_a is None:
																					if self.eos_token_id is None:
																												return cls + token_ids_a
																					else:
																												return cls + token_ids_a + sep
														elif self.eos_token_id is None:
																					raise ValueError('''Cannot tokenize multiple sequences when EOS token is not set!'''  )
														return cls + token_ids_a + sep + token_ids_a + sep  # Multiple inputs always have an EOS token







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = False  )			->				List[int]:
														if already_has_special_tokens:
																					if token_ids_a is not None:
																												raise ValueError(
																												    '''You should not supply a second sequence if the provided sequence of '''
																												    '''ids is already formatted with special tokens for the model.'''  )

																					return [1 if token in self.all_special_ids else 0 for token in token_ids_a]
														lowerCAmelCase__			  =      [1] + ([0] * len(lowerCamelCase_  )) + [1]
														if token_ids_a is not None:
																					mask += [0] * len(lowerCamelCase_  ) + [1]
														return mask







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      os.path.join(lowerCamelCase_   ,       (filename_prefix + '''-''' if filename_prefix else '''''') + '''vocab.txt'''  )
														with open(lowerCamelCase_   ,       '''w'''  ) as f:
																					f.write('''\n'''.join(self.all_tokens  )  )
														return (vocab_file,)







							@property
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														return self.get_vocab_size(with_added_tokens=lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_ = False  )			->				int:
														return super()._add_tokens(lowerCamelCase_   ,       special_tokens=lowerCamelCase_  )