


import unittest
from functools import partial

import torch

from accelerate import Accelerator, debug_launcher
from accelerate.state import AcceleratorState, GradientState
from accelerate.test_utils import require_cpu, require_huggingface_suite
from accelerate.utils import GradientAccumulationPlugin

def 				lowercase_		(		_A      :   Tuple=2   , _A      :   List[Any]=True   , _A      :   Any=False						):

      """simple docstring"""




      lowerCamelCase__		:    Any   =  Accelerator(step_scheduler_with_optimizer=_A   , split_batches=_A						)
      lowerCamelCase__		:    int   =  torch.nn.Linear(2   , 4						)
      lowerCamelCase__		:    int   =  torch.optim.AdamW(model.parameters()   , lr=1.0						)
      lowerCamelCase__		:    Any   =  torch.optim.lr_scheduler.OneCycleLR(_A   , max_lr=0.01   , steps_per_epoch=2   , epochs=1						)
      lowerCamelCase__   ,       lowerCamelCase__   ,       lowerCamelCase__		:    Optional[Any]   =  accelerator.prepare(_A   , _A   , _A						)

      # Optimizer has stepped
      scheduler.step()
      if step_scheduler_with_optimizer or (num_processes == 1):
            assert (
                scheduler.scheduler.last_epoch == num_processes
            ), F"Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})"
      else:
            assert (
                scheduler.scheduler.last_epoch != num_processes
            ), F"Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})"

def 				lowercase_		(		_A      :   Union[str, Any]=2   , _A      :   List[Any]=True   , _A      :   str=False						):

      """simple docstring"""




      lowerCamelCase__		:    List[Any]   =  Accelerator(step_scheduler_with_optimizer=_A   , split_batches=_A						)
      lowerCamelCase__		:    str   =  torch.nn.Linear(2   , 4						)
      lowerCamelCase__		:    Union[str, Any]   =  torch.optim.AdamW(model.parameters()   , lr=1.0						)
      lowerCamelCase__		:    Dict   =  torch.optim.lr_scheduler.LambdaLR(_A   , lr_lambda=lambda _A						: 1 - n / 10						)
      lowerCamelCase__   ,       lowerCamelCase__   ,       lowerCamelCase__		:    Tuple   =  accelerator.prepare(_A   , _A   , _A						)

      # Optimizer has stepped
      lowerCamelCase__		:    List[Any]   =  False
      scheduler.step()
      lowerCamelCase__		:    Union[str, Any]   =  1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10
      assert (
          scheduler.get_last_lr()[0] == expected_lr
      ), F"Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}"

      # Optimizer has not stepped
      lowerCamelCase__		:    Union[str, Any]   =  True
      scheduler.step()
      if not step_scheduler_with_optimizer:
            lowerCamelCase__		:    Union[str, Any]   =  1 - 2 / 10
      assert (
          scheduler.get_last_lr()[0] == expected_lr
      ), F"Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}"

def 				lowercase_		(		_A      :   int = 2						):

      """simple docstring"""




      from transformers import get_linear_schedule_with_warmup

      lowerCamelCase__		:    List[str]   =  [1, 2, 4]
      for num_steps in steps:
            lowerCamelCase__		:    str   =  GradientAccumulationPlugin(num_steps=_A   , adjust_scheduler=num_steps > 1						)
            lowerCamelCase__		:    Any   =  Accelerator(gradient_accumulation_plugin=_A						)
            lowerCamelCase__		:    Dict   =  torch.nn.Linear(2   , 4						)
            lowerCamelCase__		:    Optional[Any]   =  torch.optim.AdamW(model.parameters()   , lr=10.0						)
            lowerCamelCase__		:    List[Any]   =  get_linear_schedule_with_warmup(optimizer=_A   , num_warmup_steps=0   , num_training_steps=20						)

            lowerCamelCase__   ,       lowerCamelCase__   ,       lowerCamelCase__		:    Optional[Any]   =  accelerator.prepare(_A   , _A   , _A						)

            for i in range(10 * num_steps						):
                  with accelerator.accumulate(_A						):
                        optimizer.step()
                        scheduler.step()

                  if i == (10 * num_steps - 2):
                        assert (
                            scheduler.get_last_lr()[0] != 0
                        ), F"Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}"
            assert (
                scheduler.get_last_lr()[0] == 0
            ), F"Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}"
            GradientState._reset_state()







@require_cpu
class       _lowercase			(							unittest.TestCase):


       """simple docstring"""




       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             debug_launcher(partial(__lowerCamelCase ,				num_processes=1	) ,				num_processes=1	)
             debug_launcher(partial(__lowerCamelCase ,				num_processes=1 ,				split_batches=__lowerCamelCase	) ,				num_processes=1	)





       def 						lowerCAmelCase					(       self							:      List[str]	):


             '''simple docstring'''



             debug_launcher(partial(__lowerCamelCase ,				num_processes=1	) ,				num_processes=1	)
             debug_launcher(partial(__lowerCamelCase ,				num_processes=1 ,				split_batches=__lowerCamelCase	) ,				num_processes=1	)





       def 						lowerCAmelCase					(       self							:      List[Any]	):


             '''simple docstring'''



             debug_launcher(partial(__lowerCamelCase ,				num_processes=1 ,				step_scheduler_with_optimizer=__lowerCamelCase	) ,				num_processes=1	)





       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



             debug_launcher(partial(__lowerCamelCase ,				num_processes=1 ,				step_scheduler_with_optimizer=__lowerCamelCase	) ,				num_processes=1	)





       def 						lowerCAmelCase					(       self							:      Union[str, Any]	):


             '''simple docstring'''



             AcceleratorState._reset_state(__lowerCamelCase	)
             debug_launcher(__lowerCamelCase	)
             debug_launcher(partial(__lowerCamelCase ,				num_processes=1 ,				split_batches=__lowerCamelCase	) ,				num_processes=1	)





       def 						lowerCAmelCase					(       self							:      Optional[int]	):


             '''simple docstring'''



             AcceleratorState._reset_state(__lowerCamelCase	)
             debug_launcher(__lowerCamelCase	)
             debug_launcher(partial(__lowerCamelCase ,				num_processes=1 ,				split_batches=__lowerCamelCase	) ,				num_processes=1	)





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             AcceleratorState._reset_state(__lowerCamelCase	)
             debug_launcher(partial(__lowerCamelCase ,				step_scheduler_with_optimizer=__lowerCamelCase	)	)





       def 						lowerCAmelCase					(       self							:      str	):


             '''simple docstring'''



             AcceleratorState._reset_state(__lowerCamelCase	)
             debug_launcher(partial(__lowerCamelCase ,				step_scheduler_with_optimizer=__lowerCamelCase	)	)





       @require_huggingface_suite
       def 						lowerCAmelCase					(       self							:      int	):


             '''simple docstring'''



             AcceleratorState._reset_state(__lowerCamelCase	)
             debug_launcher(partial(__lowerCamelCase ,				num_processes=1	)	)
             debug_launcher(__lowerCamelCase	)






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import EfficientNetImageProcessor







class       _lowercase			(							unittest.TestCase):


       """simple docstring"""




       def __init__(       self							:      Optional[Any] ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      int=13 ,				__lowerCamelCase							:      Tuple=3 ,				__lowerCamelCase							:      Optional[Any]=18 ,				__lowerCamelCase							:      List[Any]=30 ,				__lowerCamelCase							:      Union[str, Any]=400 ,				__lowerCamelCase							:      Union[str, Any]=True ,				__lowerCamelCase							:      Union[str, Any]=None ,				__lowerCamelCase							:      Optional[Any]=True ,				__lowerCamelCase							:      str=[0.5, 0.5, 0.5] ,				__lowerCamelCase							:      Dict=[0.5, 0.5, 0.5] ,				):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  size if size is not None else {"height": 18, "width": 18}
             lowerCamelCase__		:    Dict   =  parent
             lowerCamelCase__		:    str   =  batch_size
             lowerCamelCase__		:    Tuple   =  num_channels
             lowerCamelCase__		:    Tuple   =  image_size
             lowerCamelCase__		:    Any   =  min_resolution
             lowerCamelCase__		:    Dict   =  max_resolution
             lowerCamelCase__		:    Tuple   =  do_resize
             lowerCamelCase__		:    List[str]   =  size
             lowerCamelCase__		:    Optional[Any]   =  do_normalize
             lowerCamelCase__		:    Union[str, Any]   =  image_mean
             lowerCamelCase__		:    Optional[Any]   =  image_std





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             return {
                 "image_mean": self.image_mean,
                 "image_std": self.image_std,
                 "do_normalize": self.do_normalize,
                 "do_resize": self.do_resize,
                 "size": self.size,
             }







@require_torch
@require_vision
class       _lowercase			(							lowercase__							,  unittest.TestCase):


       """simple docstring"""




       A__ 						=      EfficientNetImageProcessor if is_vision_available() else None





       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[Any]   =  EfficientNetImageProcessorTester(self	)





       @property
       def 						lowerCAmelCase					(       self							:      Union[str, Any]	):


             '''simple docstring'''



             return self.image_processor_tester.prepare_image_processor_dict()





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             lowerCamelCase__		:    Union[str, Any]   =  self.image_processing_class(**self.image_processor_dict	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"image_mean"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"image_std"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"do_normalize"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"do_resize"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"size"	)	)





       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[Any]   =  self.image_processing_class.from_dict(self.image_processor_dict	)
             self.assertEqual(image_processor.size ,				{"height": 18, "width": 18}	)

             lowerCamelCase__		:    Optional[int]   =  self.image_processing_class.from_dict(self.image_processor_dict ,				size=42	)
             self.assertEqual(image_processor.size ,				{"height": 42, "width": 42}	)





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             lowerCamelCase__		:    Any   =  self.image_processing_class(**self.image_processor_dict	)
             # create random PIL images
             lowerCamelCase__		:    Any   =  prepare_image_inputs(self.image_processor_tester ,				equal_resolution=__lowerCamelCase	)
             for image in image_inputs:
                   self.assertIsInstance(__lowerCamelCase ,				Image.Image	)

             # Test not batched input
             lowerCamelCase__		:    Union[str, Any]   =  image_processing(image_inputs[0] ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    Dict   =  image_processing(__lowerCamelCase ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)





       def 						lowerCAmelCase					(       self							:      List[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  self.image_processing_class(**self.image_processor_dict	)
             # create random numpy tensors
             lowerCamelCase__		:    List[Any]   =  prepare_image_inputs(self.image_processor_tester ,				equal_resolution=__lowerCamelCase ,				numpify=__lowerCamelCase	)
             for image in image_inputs:
                   self.assertIsInstance(__lowerCamelCase ,				np.ndarray	)

             # Test not batched input
             lowerCamelCase__		:    List[str]   =  image_processing(image_inputs[0] ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    Union[str, Any]   =  image_processing(__lowerCamelCase ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)





       def 						lowerCAmelCase					(       self							:      List[str]	):


             '''simple docstring'''



             lowerCamelCase__		:    int   =  self.image_processing_class(**self.image_processor_dict	)
             # create random PyTorch tensors
             lowerCamelCase__		:    int   =  prepare_image_inputs(self.image_processor_tester ,				equal_resolution=__lowerCamelCase ,				torchify=__lowerCamelCase	)
             for image in image_inputs:
                   self.assertIsInstance(__lowerCamelCase ,				torch.Tensor	)

             # Test not batched input
             lowerCamelCase__		:    Dict   =  image_processing(image_inputs[0] ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    List[Any]   =  image_processing(__lowerCamelCase ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)


