


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

UpperCAmelCase_ :     Tuple								=      [
    '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 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase      ,      __lowercase = None      ,      __lowercase = None						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			None
  __A  :		Optional[Any]       =			os.path.abspath(os.path.join('examples'      ,      'by_feature'						)						)
  __A  :		List[str]       =			os.path.abspath('examples'						)
  for item in os.listdir(__lowercase						):
   if item not in EXCLUDE_EXAMPLES:
    __A  :		int       =			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()'      ,      ):
      __A  :		str       =			compare_against_test(
          os.path.join(__lowercase      ,      __lowercase						)      ,      __lowercase      ,      __lowercase      ,      __lowercase						)
      __A  :		Dict       =			'\n'.join(__lowercase						)
      if special_strings is not None:
       for string in special_strings:
        __A  :		int       =			diff.replace(__lowercase      ,      ''						)
      self.assertEqual(__lowercase      ,      ''						)


 def  snake_case__   (      self						):

  """simple docstring"""







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







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[Any]       =			os.path.abspath(os.path.join('examples'      ,      'cv_example.py'						)						)
  __A  :		Dict       =			[
      ' ' * 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 						_lowerCamelCase					(    snake_case_				):




 '''simple docstring'''
 __lowercase				:		int    							=						False


 @classmethod
 def  snake_case__   (      cls						):

  """simple docstring"""







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

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


 @classmethod
 def  snake_case__   (      cls						):

  """simple docstring"""







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


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[str]       =			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  snake_case__   (      self						):

  """simple docstring"""







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


 def  snake_case__   (      self						):

  """simple docstring"""







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


 def  snake_case__   (      self						):

  """simple docstring"""







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

  """simple docstring"""







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


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[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  snake_case__   (      self						):

  """simple docstring"""







  with tempfile.TemporaryDirectory() as tmpdir:
   __A  :		Union[str, 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(__lowercase      ,      'tracking'						)						)						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			['examples/by_feature/gradient_accumulation.py']
  run_command(self._launch_args + testargs						)







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[str]       =			['examples/by_feature/local_sgd.py']
  run_command(self._launch_args + testargs						)










'''simple docstring'''
from collections import defaultdict
from math import ceil, sqrt


def 			_lowercase       (				UpperCamelCase__       :					int = 1000000,				UpperCamelCase__       :					int = 10			):
 __A  :		defaultdict       =			defaultdict(UpperCamelCase__			)

 for outer_width in range(3,				(t_limit // 4) + 2			):
  if outer_width * outer_width > t_limit:
   __A  :		Optional[int]       =			max(
       ceil(sqrt(outer_width * outer_width - t_limit			)			),				1			)
  else:
   __A  :		Optional[Any]       =			1

  hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2

  for hole_width in range(UpperCamelCase__,				outer_width - 1,				2			):
   count[outer_width * outer_width - hole_width * hole_width] += 1

 return sum(1 for n in count.values() if 1 <= n <= 10			)


if __name__ == "__main__":
 print(f'''{solution() = }''')






