code: 
  
  
  import unittest
  
  import numpy as np
  import torch
  
  from diffusers import DDPMPipeline, DDPMScheduler, UNetaDModel
  from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device
  
  
  enable_full_determinism()
  
  class 						A__      (					unittest.TestCase			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      @property
      def 	a_    ( self					):
           torch.manual_seed(0					)
           snake_case					=       UNetaDModel(
               block_out_channels=(3_2, 6_4)    ,      layers_per_block=2    ,      sample_size=3_2    ,      in_channels=3    ,      out_channels=3    ,      down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')    ,      up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')    ,      )
           return model
      def 	a_    ( self					):
           snake_case					=       '''cpu'''
           snake_case					=       self.dummy_uncond_unet
           snake_case					=       DDPMScheduler()
  
           snake_case					=       DDPMPipeline(unet=__snake_case    ,      scheduler=__snake_case					)
           ddpm.to(__snake_case					)
           ddpm.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       torch.Generator(device=__snake_case					).manual_seed(0					)
           snake_case					=       ddpm(generator=__snake_case    ,      num_inference_steps=2    ,      output_type='''numpy'''					).images
  
           snake_case					=       torch.Generator(device=__snake_case					).manual_seed(0					)
           snake_case					=       ddpm(generator=__snake_case    ,      num_inference_steps=2    ,      output_type='''numpy'''    ,      return_dict=__snake_case					)[0]
  
           snake_case					=       image[0, -3:, -3:, -1]
           snake_case					=       image_from_tuple[0, -3:, -3:, -1]
  
           assert image.shape == (1, 3_2, 3_2, 3)
           snake_case					=       np.array(
               [9.9_56E-01, 5.7_85E-01, 4.6_75E-01, 9.9_30E-01, 0.0, 1.000, 1.1_99E-03, 2.6_48E-04, 5.1_01E-04]					)
  
           assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2
           assert np.abs(image_from_tuple_slice.flatten() - expected_slice					).max() < 1E-2
  
  
  
  
      def 	a_    ( self					):
           snake_case					=       self.dummy_uncond_unet
           snake_case					=       DDPMScheduler(prediction_type='''sample'''					)
  
           snake_case					=       DDPMPipeline(unet=__snake_case    ,      scheduler=__snake_case					)
           ddpm.to(__snake_case					)
           ddpm.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       torch.manual_seed(0					)
           snake_case					=       ddpm(generator=__snake_case    ,      num_inference_steps=2    ,      output_type='''numpy'''					).images
  
           snake_case					=       torch.manual_seed(0					)
           snake_case					=       ddpm(generator=__snake_case    ,      num_inference_steps=2    ,      output_type='''numpy'''					)[0]
  
           snake_case					=       image[0, -3:, -3:, -1]
           snake_case					=       image_eps[0, -3:, -3:, -1]
  
           assert image.shape == (1, 3_2, 3_2, 3)
           snake_case					=       1E-2 if torch_device != '''mps''' else 3E-2
           assert np.abs(image_slice.flatten() - image_eps_slice.flatten()					).max() < tolerance
  
  
  
  
  
  
  @slow
  @require_torch_gpu
  class 						A__      (					unittest.TestCase			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def 	a_    ( self					):
           snake_case					=       '''google/ddpm-cifar10-32'''
  
           snake_case					=       UNetaDModel.from_pretrained(__snake_case					)
           snake_case					=       DDPMScheduler.from_pretrained(__snake_case					)
  
           snake_case					=       DDPMPipeline(unet=__snake_case    ,      scheduler=__snake_case					)
           ddpm.to(__snake_case					)
           ddpm.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       torch.manual_seed(0					)
           snake_case					=       ddpm(generator=__snake_case    ,      output_type='''numpy'''					).images
  
           snake_case					=       image[0, -3:, -3:, -1]
  
           assert image.shape == (1, 3_2, 3_2, 3)
           snake_case					=       np.array([0.4200, 0.3588, 0.1939, 0.3847, 0.3382, 0.2647, 0.4155, 0.3582, 0.3385]					)
           assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2
  
  
  
  
  
style_context: 
  
  
  import torch
  
  from accelerate import Accelerator, DistributedType
  
  class 						A__      :
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __enter__( self					):
           if self.enabled:
                snake_case					=       self.model.no_sync()
                self.model_sync_obj.__enter__()
  
           return self
      def __exit__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
           if self.enabled:
                # Average all models on exit
                self._sync_and_avg_model_params()
                self.model_sync_obj.__exit__(__snake_case    ,      __snake_case    ,      __snake_case					)
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      __snake_case = 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)'''					)
           snake_case					=       enabled and accelerator.distributed_type != DistributedType.NO
           snake_case					=       0
           if self.enabled:
                snake_case					=       accelerator
                snake_case					=       model
                snake_case					=       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():
                     snake_case					=       self.accelerator.reduce(param.data    ,      reduction='''mean'''					)
  
  
  
  
  