code: 
  
  
  
  
  
  import inspect
  import warnings
  
  import torch
  
  from .state import AcceleratorState, GradientState
  from .utils import DistributedType, honor_type, is_tpu_available
  
  
  if is_tpu_available(check_device=False):
       import torch_xla.core.xla_model as xm
  
  
  
  
  
  
  
  def       lowerCAmelCase__(	lowercase       :    Union[str, Any]   ,     lowercase       :    Optional[int]				) ->    int:
    if isinstance(lowercase   ,     (list, tuple)				):
      return honor_type(lowercase   ,     (move_to_device(lowercase   ,     lowercase				) for t in state)				)
    elif isinstance(lowercase   ,     lowercase				):
      return type(lowercase				)({k: move_to_device(lowercase   ,     lowercase				) for k, v in state.items()}				)
    elif isinstance(lowercase   ,     torch.Tensor				):
      return state.to(lowercase				)
    return state
  
  
  
  
  class 				_lowerCamelCase (    torch.optim.Optimizer    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        def __init__( self				,						UpperCAmelCase				,						UpperCAmelCase=True				,						UpperCAmelCase=None   )       ->   int:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Any        =       optimizer
          __snake_case   :				int        =       scaler
          __snake_case   :				Any        =       AcceleratorState()
          __snake_case   :				Tuple        =       GradientState()
          __snake_case   :				Optional[Any]        =       device_placement
          __snake_case   :				Optional[int]        =       False
          __snake_case   :				List[Any]        =       None
  
          # Handle device placement
          if device_placement:
            __snake_case   :				List[str]        =       self.optimizer.state_dict()
            if self.accelerator_state.distributed_type == DistributedType.TPU:
              xm.send_cpu_data_to_device(UpperCAmelCase				,						self.accelerator_state.device   )
            else:
              __snake_case   :				Union[str, Any]        =       move_to_device(UpperCAmelCase				,						self.accelerator_state.device   )
            self.optimizer.load_state_dict(UpperCAmelCase   )
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return self.optimizer.state
  
  
  
  
  
  
  
        @state.setter
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   Tuple:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				List[Any]        =       state
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   Union[str, Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return self.optimizer.param_groups
  
  
  
  
  
  
  
        @param_groups.setter
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   List[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Tuple        =       param_groups
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return self.optimizer.defaults
  
  
  
  
  
  
  
        @defaults.setter
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Tuple        =       defaults
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          self.optimizer.add_param_group(UpperCAmelCase   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
            xm.send_cpu_data_to_device(UpperCAmelCase				,						self.accelerator_state.device   )
          self.optimizer.load_state_dict(UpperCAmelCase   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return self.optimizer.state_dict()
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase=None   )       ->   List[str]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          if self.gradient_state.sync_gradients:
            __snake_case   :				List[Any]        =       "set_to_none" in inspect.signature(self.optimizer.zero_grad   ).parameters
            if accept_arg:
              if set_to_none is None:
                __snake_case   :				List[Any]        =       False
              self.optimizer.zero_grad(set_to_none=UpperCAmelCase   )
            else:
              if set_to_none is not None:
                raise ValueError("`set_to_none` for Optimizer.zero_grad` is not supported by this optimizer."   )
              self.optimizer.zero_grad()
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase=None   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          if self.gradient_state.sync_gradients:
            if self.accelerator_state.distributed_type == DistributedType.TPU:
              __snake_case   :				List[Any]        =       {"closure": closure} if closure is not None else {}
              xm.optimizer_step(self.optimizer				,						optimizer_args=UpperCAmelCase   )
            elif self.scaler is not None:
              __snake_case   :				str        =       False
              if self._last_scale is None:
                # `get_scale` is an async operation requiring full synchronization
                # on CPU and GPUs before finishing. As a result, we store away
                # the prior one to reduce the call overhead
                __snake_case   :				int        =       self.scaler.get_scale()
                __snake_case   :				Tuple        =       True
              self.scaler.step(self.optimizer				,						UpperCAmelCase   )
              self.scaler.update()
              __snake_case   :				Optional[Any]        =       self.scaler.get_scale()
              if not new_scale:
                # If we reduced the loss scale, it means the optimizer step was skipped because of gradient overflow.
                __snake_case   :				Optional[Any]        =       scale_after < self._last_scale
              __snake_case   :				int        =       scale_after
  
            else:
              self.optimizer.step(UpperCAmelCase   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   List[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          for param_group in self.optimizer.param_groups:
            __snake_case   :				int        =       [parameters_map.get(UpperCAmelCase				,						UpperCAmelCase   ) for p in param_group["params"]]
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          warnings.warn(
              "The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use "
              "`optimizer.step_was_skipped` instead."				,						UpperCAmelCase				,						)
          return self._is_overflow
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   Any:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return self._is_overflow
  
  
  
  
  
  
  
        def __getstate__( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return self.__dict__.copy()
  
  
  
  
  
  
  
        def __setstate__( self				,						UpperCAmelCase   )       ->   Tuple:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          self.__dict__.update(UpperCAmelCase   )
  
  
style_context: 
  
  
  
  
  
  from typing import Union
  
  import torch
  from PIL import Image
  from torchvision import transforms as tfms
  from tqdm.auto import tqdm
  from transformers import CLIPTextModel, CLIPTokenizer
  
  from diffusers import (
      AutoencoderKL,
      DDIMScheduler,
      DiffusionPipeline,
      LMSDiscreteScheduler,
      PNDMScheduler,
      UNetaDConditionModel,
  )
  
  
  
  
  class 				_lowerCamelCase (    a    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        def __init__( self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						)       ->   Optional[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          super().__init__()
  
          self.register_modules(vae=UpperCAmelCase				,						text_encoder=UpperCAmelCase				,						tokenizer=UpperCAmelCase				,						unet=UpperCAmelCase				,						scheduler=UpperCAmelCase   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          with torch.no_grad():
            __snake_case   :				Tuple        =       self.vae.encode(tfms.ToTensor()(UpperCAmelCase   ).unsqueeze(0   ).to(self.device   ) * 2 - 1   )
            __snake_case   :				Optional[int]        =       0.18_215 * latent.latent_dist.sample()
          return latent
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Optional[Any]        =       (1 / 0.18_215) * latent
          with torch.no_grad():
            __snake_case   :				Any        =       self.vae.decode(UpperCAmelCase   ).sample
          __snake_case   :				Optional[int]        =       (img / 2 + 0.5).clamp(0				,						1   )
          __snake_case   :				Tuple        =       img.detach().cpu().permute(0				,						2				,						3				,						1   ).numpy()
          __snake_case   :				Dict        =       (img * 255).round().astype("uint8"   )
          return Image.fromarray(img[0]   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				int        =       self.tokenizer(
              UpperCAmelCase				,						padding="max_length"				,						max_length=self.tokenizer.model_max_length				,						truncation=UpperCAmelCase				,						return_tensors="pt"				,						)
  
          __snake_case   :				List[Any]        =       self.text_encoder(text_input.input_ids.to(self.device   )   )[0]
  
          __snake_case   :				Union[str, Any]        =       self.tokenizer(
              ""				,						padding="max_length"				,						max_length=self.tokenizer.model_max_length				,						truncation=UpperCAmelCase				,						return_tensors="pt"				,						)
  
          __snake_case   :				int        =       self.text_encoder(uncond_input.input_ids.to(self.device   )   )[0]
  
          return torch.cat([uncond_embedding, text_embedding]   )
  
  
  
  
  
  
  
        def __call__( self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase = 0.3				,						UpperCAmelCase = 0.6				,						UpperCAmelCase = 0.5				,						UpperCAmelCase = 42				,						UpperCAmelCase = 50				,						UpperCAmelCase = 7.5				,						)       ->   Image.Image:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Tuple        =       steps - int(kmin * steps   )
          __snake_case   :				Optional[int]        =       steps - int(kmax * steps   )
  
          __snake_case   :				Tuple        =       self.prep_text(UpperCAmelCase   )
  
          self.scheduler.set_timesteps(UpperCAmelCase   )
  
          __snake_case    ,   __snake_case   :				Optional[Any]        =       img.size
          __snake_case   :				List[Any]        =       self.encode(UpperCAmelCase   )
  
          torch.manual_seed(UpperCAmelCase   )
          __snake_case   :				str        =       torch.randn(
              (1, self.unet.config.in_channels, height // 8, width // 8)				,						).to(self.device   )
  
          __snake_case   :				int        =       self.scheduler.add_noise(
              UpperCAmelCase				,						UpperCAmelCase				,						timesteps=self.scheduler.timesteps[tmax]				,						)
  
          __snake_case   :				Optional[Any]        =       torch.cat([latents] * 2   )
  
          __snake_case   :				Union[str, Any]        =       self.scheduler.scale_model_input(UpperCAmelCase				,						self.scheduler.timesteps[tmax]   )
  
          with torch.no_grad():
            __snake_case   :				int        =       self.unet(
                UpperCAmelCase				,						self.scheduler.timesteps[tmax]				,						encoder_hidden_states=UpperCAmelCase				,						).sample
  
          __snake_case    ,   __snake_case   :				Union[str, Any]        =       pred.chunk(2   )
          __snake_case   :				Optional[Any]        =       pred_uncond + guidance_scale * (pred_text - pred_uncond)
  
          __snake_case   :				int        =       self.scheduler.step(UpperCAmelCase				,						self.scheduler.timesteps[tmax]				,						UpperCAmelCase   ).prev_sample
  
          for i, t in enumerate(tqdm(self.scheduler.timesteps   )   ):
            if i > tmax:
              if i < tmin:  # layout generation phase
                __snake_case   :				int        =       self.scheduler.add_noise(
                    UpperCAmelCase				,						UpperCAmelCase				,						timesteps=UpperCAmelCase				,						)
  
                __snake_case   :				str        =       (mix_factor * latents) + (
                    1 - mix_factor
                ) * orig_latents  # interpolating between layout noise and conditionally generated noise to preserve layout sematics
                __snake_case   :				List[Any]        =       torch.cat([input] * 2   )
  
              else:  # content generation phase
                __snake_case   :				str        =       torch.cat([latents] * 2   )
  
              __snake_case   :				Tuple        =       self.scheduler.scale_model_input(UpperCAmelCase				,						UpperCAmelCase   )
  
              with torch.no_grad():
                __snake_case   :				List[str]        =       self.unet(
                    UpperCAmelCase				,						UpperCAmelCase				,						encoder_hidden_states=UpperCAmelCase				,						).sample
  
              __snake_case    ,   __snake_case   :				str        =       pred.chunk(2   )
              __snake_case   :				List[Any]        =       pred_uncond + guidance_scale * (pred_text - pred_uncond)
  
              __snake_case   :				List[Any]        =       self.scheduler.step(UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   ).prev_sample
  
          return self.decode(UpperCAmelCase   )
  
  