

import torch

from accelerate import Accelerator, DistributedType


class     lowerCamelCase_   :




 def __enter__( self       ):
   if self.enabled:
     __UpperCAmelCase  :     List[str]		=    self.model.no_sync()
     self.model_sync_obj.__enter__()

   return self




 def __exit__( self     ,     _UpperCAmelCase     ,     _UpperCAmelCase     ,     _UpperCAmelCase       ):
   if self.enabled:
     # Average all models on exit
     self._sync_and_avg_model_params()
     self.model_sync_obj.__exit__(_UpperCAmelCase     ,     _UpperCAmelCase     ,     _UpperCAmelCase       )




 def __init__( self     ,     _UpperCAmelCase     ,     _UpperCAmelCase     ,     _UpperCAmelCase     ,     _UpperCAmelCase = True       ):
   if accelerator.distributed_type not in [
       DistributedType.NO,
       DistributedType.MULTI_CPU,
       DistributedType.MULTI_GPU,
   ]:
     raise NotImplementedError('LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'       )
   __UpperCAmelCase  :     int		=    enabled and accelerator.distributed_type != DistributedType.NO
   __UpperCAmelCase  :     List[str]		=    0
   if self.enabled:
     __UpperCAmelCase  :     Optional[int]		=    accelerator
     __UpperCAmelCase  :     Union[str, Any]		=    model
     __UpperCAmelCase  :     Optional[Any]		=    local_sgd_steps




 def 						a					( self       ):
   self.num_steps += 1
   if not self.enabled:
     return

   if self.num_steps % self.local_sgd_steps == 0:
     self._sync_and_avg_model_params()




 def 						a					( self       ):
   self.accelerator.wait_for_everyone()
   with self.accelerator.autocast():
     for param in self.model.parameters():
       __UpperCAmelCase  :     Any		=    self.accelerator.reduce(param.data     ,     reduction='mean'       )








import gc
import tempfile
import unittest

import numpy as np
import torch

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


_SCREAMING_SNAKE_CASE          = False


@nightly
@require_torch_gpu
class     lowerCamelCase_   ( unittest.TestCase):




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




 def 						a					( self       ):
   __UpperCAmelCase  :     Dict		=    VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'       )
   # remove text_unet
   pipe.remove_unused_weights()
   pipe.to(_UpperCAmelCase       )
   pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

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

   __UpperCAmelCase  :     Optional[Any]		=    torch.manual_seed(0       )
   __UpperCAmelCase  :     Union[str, Any]		=    pipe(
       prompt='first prompt'     ,     image=_UpperCAmelCase     ,     text_to_image_strength=0.7_5     ,     generator=_UpperCAmelCase     ,     guidance_scale=7.5     ,     num_inference_steps=2     ,     output_type='numpy'     ,     ).images

   with tempfile.TemporaryDirectory() as tmpdirname:
     pipe.save_pretrained(_UpperCAmelCase       )
     __UpperCAmelCase  :     List[str]		=    VersatileDiffusionDualGuidedPipeline.from_pretrained(_UpperCAmelCase       )

   pipe.to(_UpperCAmelCase       )
   pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

   __UpperCAmelCase  :     List[str]		=    generator.manual_seed(0       )
   __UpperCAmelCase  :     Optional[int]		=    pipe(
       prompt='first prompt'     ,     image=_UpperCAmelCase     ,     text_to_image_strength=0.7_5     ,     generator=_UpperCAmelCase     ,     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 						a					( self       ):
   __UpperCAmelCase  :     Optional[Any]		=    VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'       )
   pipe.remove_unused_weights()
   pipe.to(_UpperCAmelCase       )
   pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

   __UpperCAmelCase  :     Tuple		=    'cyberpunk 2077'
   __UpperCAmelCase  :     Dict		=    load_image(
       'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'       )
   __UpperCAmelCase  :     Optional[Any]		=    torch.manual_seed(0       )
   __UpperCAmelCase  :     List[Any]		=    pipe(
       prompt=_UpperCAmelCase     ,     image=_UpperCAmelCase     ,     text_to_image_strength=0.7_5     ,     generator=_UpperCAmelCase     ,     guidance_scale=7.5     ,     num_inference_steps=50     ,     output_type='numpy'     ,     ).images

   __UpperCAmelCase  :     Tuple		=    image[0, 253:256, 253:256, -1]

   assert image.shape == (1, 512, 512, 3)
   __UpperCAmelCase  :     Tuple		=    np.array([0.0_7_8_7, 0.0_8_4_9, 0.0_8_2_6, 0.0_8_1_2, 0.0_8_0_7, 0.0_7_9_5, 0.0_8_1_8, 0.0_7_9_8, 0.0_7_7_9]       )

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





