


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)

__lowerCAmelCase		       =							[
    '''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   __a			(    unittest.TestCase      ):
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ = None				, lowerCAmelCase__ = None )					->				str:




        '''simple docstring'''
        lowercase__:   Tuple  			=   None
        lowercase__:   Optional[Any]  			=   os.path.abspath(os.path.join('examples'				, 'by_feature' ) )
        lowercase__:   int  			=   os.path.abspath('examples' )
        for item in os.listdir(lowerCAmelCase__ ):
               if item not in EXCLUDE_EXAMPLES:
                      lowercase__:   Optional[int]  			=   os.path.join(lowerCAmelCase__				, lowerCAmelCase__ )
                      if os.path.isfile(lowerCAmelCase__ ) and ".py" in item_path:
                             with self.subTest(
                                 tested_script=lowerCAmelCase__				, feature_script=lowerCAmelCase__				, tested_section='main()' if parser_only else 'training_function()'				, ):
                                    lowercase__:   Union[str, Any]  			=   compare_against_test(
                                        os.path.join(lowerCAmelCase__				, lowerCAmelCase__ )				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )
                                    lowercase__:   List[str]  			=   '\n'.join(lowerCAmelCase__ )
                                    if special_strings is not None:
                                           for string in special_strings:
                                                  lowercase__:   Dict  			=   diff.replace(lowerCAmelCase__				, '' )
                                    self.assertEqual(lowerCAmelCase__				, '' )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				str:




        '''simple docstring'''
        self.one_complete_example('complete_nlp_example.py'				, lowerCAmelCase__ )
        self.one_complete_example('complete_nlp_example.py'				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   List[str]  			=   os.path.abspath(os.path.join('examples'				, 'cv_example.py' ) )
        lowercase__:   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'				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )
        self.one_complete_example('complete_cv_example.py'				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )





@mock.patch.dict(os.environ       ,       {'TESTING_MOCKED_DATALOADERS': '1'}      )
class   __a			(    A_      ):
 __lowercase :							str        = False
 @classmethod
 def 		SCREAMING_SNAKE_CASE__   (   cls )					->				Optional[int]:




        '''simple docstring'''
        super().setUpClass()
        lowercase__:   Optional[int]  			=   tempfile.mkdtemp()
        lowercase__:   int  			=   os.path.join(cls._tmpdir				, 'default_config.yml' )

        write_basic_config(save_location=cls.configPath )
        lowercase__:   Dict  			=   ['accelerate', 'launch', '--config_file', cls.configPath]
 @classmethod
 def 		SCREAMING_SNAKE_CASE__   (   cls )					->				List[str]:




        '''simple docstring'''
        super().tearDownClass()
        shutil.rmtree(cls._tmpdir )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Any:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   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 )					->				str:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   F'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
        lowercase__:   int  			=   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'''
        lowercase__:   Any  			=   F'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir				, "epoch_0" )}\n        '.split()
        lowercase__:   Union[str, Any]  			=   run_command(self._launch_args + testargs				, return_stdout=lowerCAmelCase__ )
        self.assertNotIn('epoch 0:'				, lowerCAmelCase__ )
        self.assertIn('epoch 1:'				, lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        lowercase__:   int  			=   F'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir				, "step_2" )}\n        '.split()
        lowercase__:   List[str]  			=   run_command(self._launch_args + testargs				, return_stdout=lowerCAmelCase__ )
        if torch.cuda.is_available():
               lowercase__:   Any  			=   torch.cuda.device_count()
        else:
               lowercase__:   str  			=   1
        if num_processes > 1:
               self.assertNotIn('epoch 0:'				, lowerCAmelCase__ )
               self.assertIn('epoch 1:'				, lowerCAmelCase__ )
        else:
               self.assertIn('epoch 0:'				, lowerCAmelCase__ )
               self.assertIn('epoch 1:'				, lowerCAmelCase__ )
 @slow
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[str]:




        '''simple docstring'''
        lowercase__:   Tuple  			=   '\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        '.split()
        with mock.patch.dict(os.environ				, {'TESTING_MOCKED_DATALOADERS': '0'} ):
               lowercase__:   int  			=   run_command(self._launch_args + testargs				, return_stdout=lowerCAmelCase__ )
               lowercase__:   Optional[Any]  			=   re.findall('({.+})'				, lowerCAmelCase__ )
               lowercase__:   Any  			=   [r for r in results if 'accuracy' in r][-1]
               lowercase__:   List[Any]  			=   ast.literal_eval(lowerCAmelCase__ )
               self.assertGreaterEqual(results['accuracy']				, 0.7_5 )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   Tuple  			=   ['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 )					->				Optional[Any]:




        '''simple docstring'''
        with tempfile.TemporaryDirectory() as tmpdir:
               lowercase__:   List[str]  			=   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(lowerCAmelCase__				, 'tracking' ) ) )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   ['examples/by_feature/gradient_accumulation.py']
        run_command(self._launch_args + testargs )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        lowercase__:   Dict  			=   ['examples/by_feature/local_sgd.py']
        run_command(self._launch_args + testargs )






from __future__ import annotations







def        _lowerCamelCase(	lowercase__   , lowercase__	)						->	Any:

		'''simple docstring'''







		if len(lowercase__	) <= 1 or n <= 1:
				return

		insert_next(lowercase__   , n - 1	)
		rec_insertion_sort(lowercase__   , n - 1	)







def        _lowerCamelCase(	lowercase__   , lowercase__	)						->	Any:

		'''simple docstring'''







		if index >= len(lowercase__	) or collection[index - 1] <= collection[index]:
				return

		# Swaps adjacent elements since they are not in ascending order
		__lowercase,							__lowercase=			(
		    collection[index],
		    collection[index - 1],
		)

		insert_next(lowercase__   , index + 1	)


if __name__ == "__main__":
	lowerCAmelCase           =		input('''Enter integers separated by spaces: ''')
	lowerCAmelCase           =		[int(num) for num in numbers.split()]
	rec_insertion_sort(number_list, len(number_list))
	print(number_list)






