





import shutil
import tempfile
import unittest

from transformers import (
    SPIECE_UNDERLINE,
    AddedToken,
    BatchEncoding,
    NllbTokenizer,
    NllbTokenizerFast,
    is_torch_available,
)
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


UpperCamelCase__           =			get_tests_dir("fixtures/test_sentencepiece.model")


if is_torch_available():
					from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right

UpperCamelCase__           =			256_047
UpperCamelCase__           =			256_145

@require_sentencepiece
@require_tokenizers
class 							__SCREAMING_SNAKE_CASE       (  _UpperCAmelCase ,		unittest.TestCase							):
							snake_case  :   Union[str, Any]      =							NllbTokenizer
							snake_case  :   str      =							NllbTokenizerFast
							snake_case  :   str      =							True
							snake_case  :   Tuple      =							True
							snake_case  :   List[Any]      =							{}






							def 					_lowerCamelCase		(    self	):
														super().setUp()

														# We have a SentencePiece fixture for testing
														UpperCamelCase__					  =      NllbTokenizer(__lowerCAmelCase	,			keep_accents=__lowerCAmelCase	)
														tokenizer.save_pretrained(self.tmpdirname	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      NllbTokenizer(__lowerCAmelCase	,			keep_accents=__lowerCAmelCase	)

														UpperCamelCase__					  =      tokenizer.tokenize("""This is a test"""	)
														self.assertListEqual(__lowerCAmelCase	,			["""▁This""", """▁is""", """▁a""", """▁t""", """est"""]	)

														self.assertListEqual(
														    tokenizer.convert_tokens_to_ids(__lowerCAmelCase	)	,			[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]	,			)

														UpperCamelCase__					  =      tokenizer.tokenize("""I was born in 92000, and this is falsé."""	)
														self.assertListEqual(
														    __lowerCAmelCase	,			[
														        SPIECE_UNDERLINE + """I""",
														        SPIECE_UNDERLINE + """was""",
														        SPIECE_UNDERLINE + """b""",
														        """or""",
														        """n""",
														        SPIECE_UNDERLINE + """in""",
														        SPIECE_UNDERLINE + """""",
														        """9""",
														        """2""",
														        """0""",
														        """0""",
														        """0""",
														        """,""",
														        SPIECE_UNDERLINE + """and""",
														        SPIECE_UNDERLINE + """this""",
														        SPIECE_UNDERLINE + """is""",
														        SPIECE_UNDERLINE + """f""",
														        """al""",
														        """s""",
														        """é""",
														        """.""",
														    ]	,			)
														UpperCamelCase__					  =      tokenizer.convert_tokens_to_ids(__lowerCAmelCase	)
														self.assertListEqual(
														    __lowerCAmelCase	,			[
														        value + tokenizer.fairseq_offset
														        for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
														    ]	,			)

														UpperCamelCase__					  =      tokenizer.convert_ids_to_tokens(__lowerCAmelCase	)
														self.assertListEqual(
														    __lowerCAmelCase	,			[
														        SPIECE_UNDERLINE + """I""",
														        SPIECE_UNDERLINE + """was""",
														        SPIECE_UNDERLINE + """b""",
														        """or""",
														        """n""",
														        SPIECE_UNDERLINE + """in""",
														        SPIECE_UNDERLINE + """""",
														        """<unk>""",
														        """2""",
														        """0""",
														        """0""",
														        """0""",
														        """,""",
														        SPIECE_UNDERLINE + """and""",
														        SPIECE_UNDERLINE + """this""",
														        SPIECE_UNDERLINE + """is""",
														        SPIECE_UNDERLINE + """f""",
														        """al""",
														        """s""",
														        """<unk>""",
														        """.""",
														    ]	,			)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      (self.rust_tokenizer_class, """hf-internal-testing/tiny-random-nllb""", {})
														for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
																					with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""	):
																												UpperCamelCase__					  =      self.rust_tokenizer_class.from_pretrained(__lowerCAmelCase	,			**__lowerCAmelCase	)
																												UpperCamelCase__					  =      self.tokenizer_class.from_pretrained(__lowerCAmelCase	,			**__lowerCAmelCase	)

																												UpperCamelCase__					  =      tempfile.mkdtemp()

																												UpperCamelCase__					  =      tokenizer_r.save_pretrained(__lowerCAmelCase	)
																												UpperCamelCase__					  =      tokenizer_p.save_pretrained(__lowerCAmelCase	)

																												# Checks it save with the same files + the tokenizer.json file for the fast one
																												self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files	)	)
																												UpperCamelCase__					  =      tuple(f for f in tokenizer_r_files if """tokenizer.json""" not in f	)
																												self.assertSequenceEqual(__lowerCAmelCase	,			__lowerCAmelCase	)

																												# Checks everything loads correctly in the same way
																												UpperCamelCase__					  =      tokenizer_r.from_pretrained(__lowerCAmelCase	)
																												UpperCamelCase__					  =      tokenizer_p.from_pretrained(__lowerCAmelCase	)

																												# Check special tokens are set accordingly on Rust and Python
																												for key in tokenizer_pp.special_tokens_map:
																																			self.assertTrue(hasattr(__lowerCAmelCase	,			__lowerCAmelCase	)	)

																												shutil.rmtree(__lowerCAmelCase	)

																												# Save tokenizer rust, legacy_format=True
																												UpperCamelCase__					  =      tempfile.mkdtemp()

																												UpperCamelCase__					  =      tokenizer_r.save_pretrained(__lowerCAmelCase	,			legacy_format=__lowerCAmelCase	)
																												UpperCamelCase__					  =      tokenizer_p.save_pretrained(__lowerCAmelCase	)

																												# Checks it save with the same files
																												self.assertSequenceEqual(__lowerCAmelCase	,			__lowerCAmelCase	)

																												# Checks everything loads correctly in the same way
																												UpperCamelCase__					  =      tokenizer_r.from_pretrained(__lowerCAmelCase	)
																												UpperCamelCase__					  =      tokenizer_p.from_pretrained(__lowerCAmelCase	)

																												# Check special tokens are set accordingly on Rust and Python
																												for key in tokenizer_pp.special_tokens_map:
																																			self.assertTrue(hasattr(__lowerCAmelCase	,			__lowerCAmelCase	)	)

																												shutil.rmtree(__lowerCAmelCase	)

																												# Save tokenizer rust, legacy_format=False
																												UpperCamelCase__					  =      tempfile.mkdtemp()

																												UpperCamelCase__					  =      tokenizer_r.save_pretrained(__lowerCAmelCase	,			legacy_format=__lowerCAmelCase	)
																												UpperCamelCase__					  =      tokenizer_p.save_pretrained(__lowerCAmelCase	)

																												# Checks it saved the tokenizer.json file
																												self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files	)	)

																												# Checks everything loads correctly in the same way
																												UpperCamelCase__					  =      tokenizer_r.from_pretrained(__lowerCAmelCase	)
																												UpperCamelCase__					  =      tokenizer_p.from_pretrained(__lowerCAmelCase	)

																												# Check special tokens are set accordingly on Rust and Python
																												for key in tokenizer_pp.special_tokens_map:
																																			self.assertTrue(hasattr(__lowerCAmelCase	,			__lowerCAmelCase	)	)

																												shutil.rmtree(__lowerCAmelCase	)






							@require_torch
							def 					_lowerCamelCase		(    self	):
														if not self.test_seqaseq:
																					return

														UpperCamelCase__					  =      self.get_tokenizers()
														for tokenizer in tokenizers:
																					with self.subTest(f"""{tokenizer.__class__.__name__}"""	):
																												# Longer text that will definitely require truncation.
																												UpperCamelCase__					  =      [
																												    """ UN Chief Says There Is No Military Solution in Syria""",
																												    """ Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for"""
																												    """ Syria is that 'there is no military solution' to the nearly five-year conflict and more weapons"""
																												    """ will only worsen the violence and misery for millions of people.""",
																												]
																												UpperCamelCase__					  =      [
																												    """Şeful ONU declară că nu există o soluţie militară în Siria""",
																												    """Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al"""
																												    """ Rusiei pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi"""
																												    """ că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.""",
																												]
																												try:
																																			UpperCamelCase__					  =      tokenizer.prepare_seqaseq_batch(
																																			    src_texts=__lowerCAmelCase	,			tgt_texts=__lowerCAmelCase	,			max_length=3	,			max_target_length=10	,			return_tensors="""pt"""	,			src_lang="""eng_Latn"""	,			tgt_lang="""ron_Latn"""	,			)
																												except NotImplementedError:
																																			return
																												self.assertEqual(batch.input_ids.shape[1]	,			3	)
																												self.assertEqual(batch.labels.shape[1]	,			10	)
																												# max_target_length will default to max_length if not specified
																												UpperCamelCase__					  =      tokenizer.prepare_seqaseq_batch(
																												    __lowerCAmelCase	,			tgt_texts=__lowerCAmelCase	,			max_length=3	,			return_tensors="""pt"""	)
																												self.assertEqual(batch.input_ids.shape[1]	,			3	)
																												self.assertEqual(batch.labels.shape[1]	,			3	)

																												UpperCamelCase__					  =      tokenizer.prepare_seqaseq_batch(
																												    src_texts=__lowerCAmelCase	,			max_length=3	,			max_target_length=10	,			return_tensors="""pt"""	)
																												self.assertEqual(batch_encoder_only.input_ids.shape[1]	,			3	)
																												self.assertEqual(batch_encoder_only.attention_mask.shape[1]	,			3	)
																												self.assertNotIn("""decoder_input_ids"""	,			__lowerCAmelCase	)






							@unittest.skip("""Unfortunately way too slow to build a BPE with SentencePiece."""	)
							def 					_lowerCamelCase		(    self	):
														pass







							def 					_lowerCamelCase		(    self	):
														for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
																					with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""	):
																												UpperCamelCase__					  =      [AddedToken("""<special>"""	,			lstrip=__lowerCAmelCase	)]

																												UpperCamelCase__					  =      self.rust_tokenizer_class.from_pretrained(
																												    __lowerCAmelCase	,			additional_special_tokens=__lowerCAmelCase	,			**__lowerCAmelCase	)
																												UpperCamelCase__					  =      tokenizer_r.encode("""Hey this is a <special> token"""	)

																												UpperCamelCase__					  =      tokenizer_r.encode("""<special>"""	,			add_special_tokens=__lowerCAmelCase	)[0]

																												self.assertTrue(special_token_id in r_output	)

																												if self.test_slow_tokenizer:
																																			UpperCamelCase__					  =      self.rust_tokenizer_class.from_pretrained(
																																			    __lowerCAmelCase	,			additional_special_tokens=__lowerCAmelCase	,			**__lowerCAmelCase	,			)
																																			UpperCamelCase__					  =      self.tokenizer_class.from_pretrained(
																																			    __lowerCAmelCase	,			additional_special_tokens=__lowerCAmelCase	,			**__lowerCAmelCase	)

																																			UpperCamelCase__					  =      tokenizer_p.encode("""Hey this is a <special> token"""	)

																																			UpperCamelCase__					  =      tokenizer_cr.encode("""Hey this is a <special> token"""	)

																																			self.assertEqual(__lowerCAmelCase	,			__lowerCAmelCase	)
																																			self.assertEqual(__lowerCAmelCase	,			__lowerCAmelCase	)
																																			self.assertTrue(special_token_id in p_output	)
																																			self.assertTrue(special_token_id in cr_output	)






@require_torch
@require_sentencepiece
@require_tokenizers
class 							__SCREAMING_SNAKE_CASE       (  unittest.TestCase							):
							snake_case  :   Dict      =							"""facebook/nllb-200-distilled-600M"""
							snake_case  :   int      =							[
							    """ UN Chief Says There Is No Military Solution in Syria""",
							    """ Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.""",
							]
							snake_case  :   int      =							[
							    """Şeful ONU declară că nu există o soluţie militară în Siria""",
							    """Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei"""
							    """ pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor"""
							    """ face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.""",
							]
							snake_case  :   str      =							[
							    25_60_47,
							    1_62_97,
							    13_44_08,
							    81_65,
							    24_80_66,
							    1_47_34,
							    9_50,
							    11_35,
							    10_57_21,
							    35_73,
							    83,
							    2_73_52,
							    1_08,
							    4_94_86,
							    2,
							]






							@classmethod
							def 					_lowerCamelCase		(    cls	):
														UpperCamelCase__					  =      NllbTokenizer.from_pretrained(
														    cls.checkpoint_name	,			src_lang="""eng_Latn"""	,			tgt_lang="""ron_Latn"""	)
														UpperCamelCase__					  =      1
														return cls






							def 					_lowerCamelCase		(    self	):
														self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ace_Arab"""]	,			256001	)
														self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ace_Latn"""]	,			256002	)
														self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""fra_Latn"""]	,			256057	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.tokenizer.batch_encode_plus(self.src_text	).input_ids[0]
														self.assertListEqual(self.expected_src_tokens	,			__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														self.assertIn(__lowerCAmelCase	,			self.tokenizer.all_special_ids	)
														# fmt: off
														UpperCamelCase__					  =      [RO_CODE, 4254, 98068, 112923, 39072, 3909, 713, 102767, 26, 17314, 35642, 14683, 33118, 2022, 66987, 2, 256047]
														# fmt: on

														UpperCamelCase__					  =      self.tokenizer.decode(__lowerCAmelCase	,			skip_special_tokens=__lowerCAmelCase	)
														UpperCamelCase__					  =      self.tokenizer.decode(generated_ids[1:]	,			skip_special_tokens=__lowerCAmelCase	)
														self.assertEqual(__lowerCAmelCase	,			__lowerCAmelCase	)
														self.assertNotIn(self.tokenizer.eos_token	,			__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      ["""this is gunna be a long sentence """ * 20]
														assert isinstance(src_text[0]	,			__lowerCAmelCase	)
														UpperCamelCase__					  =      10
														UpperCamelCase__					  =      self.tokenizer(__lowerCAmelCase	,			max_length=__lowerCAmelCase	,			truncation=__lowerCAmelCase	).input_ids[0]
														self.assertEqual(ids[-1]	,			2	)
														self.assertEqual(ids[0]	,			__lowerCAmelCase	)
														self.assertEqual(len(__lowerCAmelCase	)	,			__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["""<mask>""", """ar_AR"""]	)	,			[256203, 3]	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      tempfile.mkdtemp()
														UpperCamelCase__					  =      self.tokenizer.fairseq_tokens_to_ids
														self.tokenizer.save_pretrained(__lowerCAmelCase	)
														UpperCamelCase__					  =      NllbTokenizer.from_pretrained(__lowerCAmelCase	)
														self.assertDictEqual(new_tok.fairseq_tokens_to_ids	,			__lowerCAmelCase	)






							@require_torch
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.tokenizer(
														    self.src_text	,			text_target=self.tgt_text	,			padding=__lowerCAmelCase	,			truncation=__lowerCAmelCase	,			max_length=len(self.expected_src_tokens	)	,			return_tensors="""pt"""	,			)
														UpperCamelCase__					  =      shift_tokens_right(
														    batch["""labels"""]	,			self.tokenizer.pad_token_id	,			self.tokenizer.lang_code_to_id["""ron_Latn"""]	)

														self.assertIsInstance(__lowerCAmelCase	,			__lowerCAmelCase	)

														self.assertEqual((2, 15)	,			batch.input_ids.shape	)
														self.assertEqual((2, 15)	,			batch.attention_mask.shape	)
														UpperCamelCase__					  =      batch.input_ids.tolist()[0]
														self.assertListEqual(self.expected_src_tokens	,			__lowerCAmelCase	)
														self.assertEqual(__lowerCAmelCase	,			batch.decoder_input_ids[0, 0]	)  # EOS
														# Test that special tokens are reset
														self.assertEqual(self.tokenizer.prefix_tokens	,			[EN_CODE]	)
														self.assertEqual(self.tokenizer.suffix_tokens	,			[self.tokenizer.eos_token_id]	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.tokenizer(self.src_text	,			padding=__lowerCAmelCase	,			truncation=__lowerCAmelCase	,			max_length=3	,			return_tensors="""pt"""	)
														UpperCamelCase__					  =      self.tokenizer(
														    text_target=self.tgt_text	,			padding=__lowerCAmelCase	,			truncation=__lowerCAmelCase	,			max_length=10	,			return_tensors="""pt"""	)
														UpperCamelCase__					  =      targets["""input_ids"""]
														UpperCamelCase__					  =      shift_tokens_right(
														    __lowerCAmelCase	,			self.tokenizer.pad_token_id	,			decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang]	,			)

														self.assertEqual(batch.input_ids.shape[1]	,			3	)
														self.assertEqual(batch.decoder_input_ids.shape[1]	,			10	)






							@require_torch
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.tokenizer._build_translation_inputs(
														    """A test"""	,			return_tensors="""pt"""	,			src_lang="""eng_Latn"""	,			tgt_lang="""fra_Latn"""	)

														self.assertEqual(
														    nested_simplify(__lowerCAmelCase	)	,			{
														        # A, test, EOS, en_XX
														        """input_ids""": [[256047, 70, 7356, 2]],
														        """attention_mask""": [[1, 1, 1, 1]],
														        # ar_AR
														        """forced_bos_token_id""": 256057,
														    }	,			)







							@require_torch
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      True
														UpperCamelCase__					  =      self.tokenizer(
														    """UN Chief says there is no military solution in Syria"""	,			src_lang="""eng_Latn"""	,			tgt_lang="""fra_Latn"""	)
														self.assertEqual(
														    inputs.input_ids	,			[16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2, 256047]	)

														UpperCamelCase__					  =      False
														UpperCamelCase__					  =      self.tokenizer(
														    """UN Chief says there is no military solution in Syria"""	,			src_lang="""eng_Latn"""	,			tgt_lang="""fra_Latn"""	)
														self.assertEqual(
														    inputs.input_ids	,			[256047, 16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2]	)











"""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)

SCREAMING_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 						lowercase   (   unittest.TestCase   ):





      def        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase = None ,		lowercase = None					)						->	Dict:
            lowerCAmelCase    		=			None
            lowerCAmelCase    		=			os.path.abspath(os.path.join("""examples""" ,		"""by_feature"""					)					)
            lowerCAmelCase    		=			os.path.abspath("""examples"""					)
            for item in os.listdir(lowercase					):
                  if item not in EXCLUDE_EXAMPLES:
                        lowerCAmelCase    		=			os.path.join(lowercase ,		lowercase					)
                        if os.path.isfile(lowercase					) and ".py" in item_path:
                              with self.subTest(
                                  tested_script=lowercase ,		feature_script=lowercase ,		tested_section="""main()""" if parser_only else """training_function()""" ,		):
                                    lowerCAmelCase    		=			compare_against_test(
                                        os.path.join(lowercase ,		lowercase					) ,		lowercase ,		lowercase ,		lowercase					)
                                    lowerCAmelCase    		=			"""\n""".join(lowercase					)
                                    if special_strings is not None:
                                          for string in special_strings:
                                                lowerCAmelCase    		=			diff.replace(lowercase ,		""""""					)
                                    self.assertEqual(lowercase ,		""""""					)





      def        _snake_case     (			self					)						->	List[Any]:
            self.one_complete_example("""complete_nlp_example.py""" ,		lowercase					)
            self.one_complete_example("""complete_nlp_example.py""" ,		lowercase					)





      def        _snake_case     (			self					)						->	Any:
            lowerCAmelCase    		=			os.path.abspath(os.path.join("""examples""" ,		"""cv_example.py"""					)					)
            lowerCAmelCase    		=			[
                """ """ * 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""" ,		lowercase ,		lowercase ,		lowercase					)
            self.one_complete_example("""complete_cv_example.py""" ,		lowercase ,		lowercase ,		lowercase					)







@mock.patch.dict(os.environ	,    {'TESTING_MOCKED_DATALOADERS': '1'}   )
class 						lowercase   (   _UpperCAmelCase   ):
      _SCREAMING_SNAKE_CASE							      = False





      @classmethod
      def        _snake_case     (			cls					)						->	Optional[int]:
            super().setUpClass()
            lowerCAmelCase    		=			tempfile.mkdtemp()
            lowerCAmelCase    		=			os.path.join(cls._tmpdir ,		"""default_config.yml"""					)

            write_basic_config(save_location=cls.configPath					)
            lowerCAmelCase    		=			["""accelerate""", """launch""", """--config_file""", cls.configPath]





      @classmethod
      def        _snake_case     (			cls					)						->	Optional[int]:
            super().tearDownClass()
            shutil.rmtree(cls._tmpdir					)





      def        _snake_case     (			self					)						->	str:
            lowerCAmelCase    		=			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        _snake_case     (			self					)						->	Optional[int]:
            lowerCAmelCase    		=			f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
            lowerCAmelCase    		=			run_command(self._launch_args + testargs					)
            self.assertTrue(os.path.exists(os.path.join(self.tmpdir ,		"""step_2"""					)					)					)





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





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





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





      def        _snake_case     (			self					)						->	int:
            lowerCAmelCase    		=			["""examples/by_feature/multi_process_metrics.py"""]
            run_command(self._launch_args + testargs					)





      @require_trackers
      @mock.patch.dict(os.environ ,		{"""WANDB_MODE""": """offline"""}					)
      def        _snake_case     (			self					)						->	Any:
            with tempfile.TemporaryDirectory() as tmpdir:
                  lowerCAmelCase    		=			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(lowercase ,		"""tracking"""					)					)					)





      def        _snake_case     (			self					)						->	Union[str, Any]:
            lowerCAmelCase    		=			["""examples/by_feature/gradient_accumulation.py"""]
            run_command(self._launch_args + testargs					)





      def        _snake_case     (			self					)						->	int:
            lowerCAmelCase    		=			["""examples/by_feature/local_sgd.py"""]
            run_command(self._launch_args + testargs					)






