





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







	'''simple docstring'''

	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase = None     ,_lowerCamelCase = None )							->							Optional[int]:







				'''simple docstring'''





				__lowercase										=							None
				__lowercase										=							os.path.abspath(os.path.join('''examples'''     ,'''by_feature''' ) )
				__lowercase										=							os.path.abspath('''examples''' )
				for item in os.listdir(_lowerCamelCase ):
							if item not in EXCLUDE_EXAMPLES:
										__lowercase										=							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										=							compare_against_test(
																    os.path.join(_lowerCamelCase     ,_lowerCamelCase )     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )
																__lowercase										=							'''\n'''.join(_lowerCamelCase )
																if special_strings is not None:
																			for string in special_strings:
																						__lowercase										=							diff.replace(_lowerCamelCase     ,'''''' )
																self.assertEqual(_lowerCamelCase     ,'''''' )


	def 						_UpperCAmelCase     (self )							->							int:







				'''simple docstring'''





				self.one_complete_example('''complete_nlp_example.py'''     ,_lowerCamelCase )
				self.one_complete_example('''complete_nlp_example.py'''     ,_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							int:







				'''simple docstring'''





				__lowercase										=							os.path.abspath(os.path.join('''examples'''     ,'''cv_example.py''' ) )
				__lowercase										=							[
				    ''' ''' * 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 							__lowercase       (      lowerCAmelCase__						):







	'''simple docstring'''

	a    :				str											=       False


	@classmethod
	def 						_UpperCAmelCase     (cls )							->							Union[str, Any]:







				'''simple docstring'''





				super().setUpClass()
				__lowercase										=							tempfile.mkdtemp()
				__lowercase										=							os.path.join(cls._tmpdir     ,'''default_config.yml''' )

				write_basic_config(save_location=cls.configPath )
				__lowercase										=							['''accelerate''', '''launch''', '''--config_file''', cls.configPath]


	@classmethod
	def 						_UpperCAmelCase     (cls )							->							Union[str, Any]:







				'''simple docstring'''





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


	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				__lowercase										=							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 						_UpperCAmelCase     (self )							->							Optional[Any]:







				'''simple docstring'''





				__lowercase										=							f"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        ".split()
				__lowercase										=							run_command(self._launch_args + testargs )
				self.assertTrue(os.path.exists(os.path.join(self.tmpdir     ,'''step_2''' ) ) )


	def 						_UpperCAmelCase     (self )							->							Any:







				'''simple docstring'''





				__lowercase										=							f"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir     ,'epoch_0' )}\n        ".split()
				__lowercase										=							run_command(self._launch_args + testargs     ,return_stdout=_lowerCamelCase )
				self.assertNotIn('''epoch 0:'''     ,_lowerCamelCase )
				self.assertIn('''epoch 1:'''     ,_lowerCamelCase )


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







				'''simple docstring'''





				__lowercase										=							f"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir     ,'step_2' )}\n        ".split()
				__lowercase										=							run_command(self._launch_args + testargs     ,return_stdout=_lowerCamelCase )
				if torch.cuda.is_available():
							__lowercase										=							torch.cuda.device_count()
				else:
							__lowercase										=							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 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				__lowercase										=							'''
        examples/by_feature/cross_validation.py
        --num_folds 2
        '''.split()
				with mock.patch.dict(os.environ     ,{'''TESTING_MOCKED_DATALOADERS''': '''0'''} ):
							__lowercase										=							run_command(self._launch_args + testargs     ,return_stdout=_lowerCamelCase )
							__lowercase										=							re.findall('''({.+})'''     ,_lowerCamelCase )
							__lowercase										=							[r for r in results if '''accuracy''' in r][-1]
							__lowercase										=							ast.literal_eval(_lowerCamelCase )
							self.assertGreaterEqual(results['''accuracy''']     ,0.7_5 )


	def 						_UpperCAmelCase     (self )							->							Any:







				'''simple docstring'''





				__lowercase										=							['''examples/by_feature/multi_process_metrics.py''']
				run_command(self._launch_args + testargs )


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







				'''simple docstring'''





				with tempfile.TemporaryDirectory() as tmpdir:
							__lowercase										=							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 						_UpperCAmelCase     (self )							->							List[str]:







				'''simple docstring'''





				__lowercase										=							['''examples/by_feature/gradient_accumulation.py''']
				run_command(self._launch_args + testargs )


	def 						_UpperCAmelCase     (self )							->							int:







				'''simple docstring'''





				__lowercase										=							['''examples/by_feature/local_sgd.py''']
				run_command(self._launch_args + testargs )












'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE					=    {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']}

try:
	if not is_sentencepiece_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_SCREAMING_SNAKE_CASE					=    ['''XGLMTokenizer''']

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_SCREAMING_SNAKE_CASE					=    ['''XGLMTokenizerFast''']

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_SCREAMING_SNAKE_CASE					=    [
	    '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''XGLMForCausalLM''',
	    '''XGLMModel''',
	    '''XGLMPreTrainedModel''',
	]


try:
	if not is_flax_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_SCREAMING_SNAKE_CASE					=    [
	    '''FlaxXGLMForCausalLM''',
	    '''FlaxXGLMModel''',
	    '''FlaxXGLMPreTrainedModel''',
	]


try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_SCREAMING_SNAKE_CASE					=    [
	    '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''TFXGLMForCausalLM''',
	    '''TFXGLMModel''',
	    '''TFXGLMPreTrainedModel''',
	]


if TYPE_CHECKING:
	from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig

	try:
		if not is_sentencepiece_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_xglm import XGLMTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_xglm_fast import XGLMTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel

	try:
		if not is_flax_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_xglm import (
		    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFXGLMForCausalLM,
		    TFXGLMModel,
		    TFXGLMPreTrainedModel,
		)


else:
	import sys

	_SCREAMING_SNAKE_CASE					=    _LazyModule(__name__, globals()['''__file__'''], _import_structure)





