





"""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     :						List[str]						       =  [
    '''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    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case = None				,       _snake_case = None     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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'				,       _snake_case				,       _snake_case				,       _snake_case     )
  self.one_complete_example('complete_cv_example.py'				,       _snake_case				,       _snake_case				,       _snake_case     )







@mock.patch.dict(os.environ   , {'''TESTING_MOCKED_DATALOADERS''': '''1'''}  )
class       a							(   a__  ):
 snake_case__             =       False



 @classmethod
 def    UpperCamelCase__  (     cls     ):







  """simple docstring"""






  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    UpperCamelCase__  (     cls     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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=_snake_case     )
  self.assertNotIn('epoch 0:'				,       _snake_case     )
  self.assertIn('epoch 1:'				,       _snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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=_snake_case     )
  if torch.cuda.is_available():
   lowerCAmelCase    				= torch.cuda.device_count()
  else:
   lowerCAmelCase    				= 1
  if num_processes > 1:
   self.assertNotIn('epoch 0:'				,       _snake_case     )
   self.assertIn('epoch 1:'				,       _snake_case     )
  else:
   self.assertIn('epoch 0:'				,       _snake_case     )
   self.assertIn('epoch 1:'				,       _snake_case     )



 @slow
 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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(_snake_case				,       'tracking'     )     )     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






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










import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


_lowerCAmelCase	:   Any    =   False


class 	__snake_case   ( unittest.TestCase ):
		pass



@nightly
@require_torch_gpu
class 	__snake_case   ( unittest.TestCase ):
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'    ,torch_dtype=torch.floataa						)
						pipe.to(a_						)
						pipe.set_progress_bar_config(disable=a_						)

						lowerCAmelCase__		  =  load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'						)

						lowerCAmelCase__		  =  torch.manual_seed(0						)
						lowerCAmelCase__		  =  pipe.dual_guided(
						    prompt='first prompt'    ,image=a_    ,text_to_image_strength=0.75    ,generator=a_    ,guidance_scale=7.5    ,num_inference_steps=2    ,output_type='numpy'    ,).images

						with tempfile.TemporaryDirectory() as tmpdirname:
										pipe.save_pretrained(a_						)
										lowerCAmelCase__		  =  VersatileDiffusionPipeline.from_pretrained(a_    ,torch_dtype=torch.floataa						)
						pipe.to(a_						)
						pipe.set_progress_bar_config(disable=a_						)

						lowerCAmelCase__		  =  generator.manual_seed(0						)
						lowerCAmelCase__		  =  pipe.dual_guided(
						    prompt='first prompt'    ,image=a_    ,text_to_image_strength=0.75    ,generator=a_    ,guidance_scale=7.5    ,num_inference_steps=2    ,output_type='numpy'    ,).images

						assert np.abs(image - new_image						).sum() < 1e-5, "Models don't have the same forward pass"



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'    ,torch_dtype=torch.floataa						)
						pipe.to(a_						)
						pipe.set_progress_bar_config(disable=a_						)

						lowerCAmelCase__		  =  'cyberpunk 2077'
						lowerCAmelCase__		  =  load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'						)
						lowerCAmelCase__		  =  torch.manual_seed(0						)
						lowerCAmelCase__		  =  pipe.dual_guided(
						    prompt=a_    ,image=a_    ,text_to_image_strength=0.75    ,generator=a_    ,guidance_scale=7.5    ,num_inference_steps=50    ,output_type='numpy'    ,).images

						lowerCAmelCase__		  =  image[0, 253:256, 253:256, -1]

						assert image.shape == (1, 512, 512, 3)
						lowerCAmelCase__		  =  np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001]						)

						assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1

						lowerCAmelCase__		  =  'A painting of a squirrel eating a burger '
						lowerCAmelCase__		  =  torch.manual_seed(0						)
						lowerCAmelCase__		  =  pipe.text_to_image(
						    prompt=a_    ,generator=a_    ,guidance_scale=7.5    ,num_inference_steps=50    ,output_type='numpy'						).images

						lowerCAmelCase__		  =  image[0, 253:256, 253:256, -1]

						assert image.shape == (1, 512, 512, 3)
						lowerCAmelCase__		  =  np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]						)

						assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1

						lowerCAmelCase__		  =  pipe.image_variation(a_    ,generator=a_    ,output_type='numpy'						).images

						lowerCAmelCase__		  =  image[0, 253:256, 253:256, -1]

						assert image.shape == (1, 512, 512, 3)
						lowerCAmelCase__		  =  np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456]						)

						assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1
