





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


lowerCAmelCase_						   =    False



class        _lowerCAmelCase    (			unittest.TestCase							):


  '''simple docstring'''
  pass




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


  '''simple docstring'''

  def UpperCamelCase_ (      self      :					Tuple							):


    '''simple docstring'''





    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()

  def UpperCamelCase_ (      self      :					Tuple							):


    '''simple docstring'''





    _snake_case   :				str														=	VersatileDiffusionTextToImagePipeline.from_pretrained('shi-labs/versatile-diffusion'							)
    # remove text_unet
    pipe.remove_unused_weights()
    pipe.to(A_							)
    pipe.set_progress_bar_config(disable=A_							)

    _snake_case   :				Optional[Any]														=	'A painting of a squirrel eating a burger '
    _snake_case   :				int														=	torch.manual_seed(0							)
    _snake_case   :				Union[str, Any]														=	pipe(
        prompt=A_      ,				generator=A_      ,				guidance_scale=7.5      ,				num_inference_steps=2      ,				output_type='numpy'							).images

    with tempfile.TemporaryDirectory() as tmpdirname:
      pipe.save_pretrained(A_							)
      _snake_case   :				List[str]														=	VersatileDiffusionTextToImagePipeline.from_pretrained(A_							)
    pipe.to(A_							)
    pipe.set_progress_bar_config(disable=A_							)

    _snake_case   :				str														=	generator.manual_seed(0							)
    _snake_case   :				Optional[int]														=	pipe(
        prompt=A_      ,				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 UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	VersatileDiffusionTextToImagePipeline.from_pretrained(
        'shi-labs/versatile-diffusion'      ,				torch_dtype=torch.floataa							)
    pipe.to(A_							)
    pipe.set_progress_bar_config(disable=A_							)

    _snake_case   :				str														=	'A painting of a squirrel eating a burger '
    _snake_case   :				Optional[int]														=	torch.manual_seed(0							)
    _snake_case   :				Any														=	pipe(
        prompt=A_      ,				generator=A_      ,				guidance_scale=7.5      ,				num_inference_steps=50      ,				output_type='numpy'							).images

    _snake_case   :				Optional[Any]														=	image[0, 2_53:2_56, 2_53:2_56, -1]

    assert image.shape == (1, 5_12, 5_12, 3)
    _snake_case   :				Optional[int]														=	np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]							)

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











from random import randint, random


def     lowerCamelCase_	(      lowerCAmelCase:	int    ,							lowerCAmelCase:	int    ,							lowerCAmelCase:	int    ,							lowerCAmelCase:	bool = False    ,							lowerCAmelCase:	bool = False    ,							lowerCAmelCase:	int = 5    ,							)->		list:
  _snake_case   :				Dict														=	[[-1] * number_of_cells]  # Create a highway without any car
  _snake_case   :				List[str]														=	0
  _snake_case   :				List[str]														=	max(lowerCAmelCase    ,							0				)
  while i < number_of_cells:
    _snake_case   :				Optional[Any]														=	(
        randint(0    ,							lowerCAmelCase				) if random_speed else initial_speed
    )  # Place the cars
    i += (
        randint(1    ,							max_speed * 2				) if random_frequency else frequency
    )  # Arbitrary number, may need tuning
  return highway


def     lowerCamelCase_	(      lowerCAmelCase:	list    ,							lowerCAmelCase:	int				)->		int:
  _snake_case   :				Dict														=	0
  _snake_case   :				Optional[Any]														=	highway_now[car_index + 1 :]
  for cell in range(len(lowerCAmelCase				)				):  # May need a better name for this
    if cells[cell] != -1:  # If the cell is not empty then
      return distance  # we have the distance we wanted
    distance += 1
  # Here if the car is near the end of the highway
  return distance + get_distance(lowerCAmelCase    ,							-1				)


def     lowerCamelCase_	(      lowerCAmelCase:	list    ,							lowerCAmelCase:	float    ,							lowerCAmelCase:	int				)->		list:
  _snake_case   :				List[Any]														=	len(lowerCAmelCase				)
  # Beforce calculations, the highway is empty
  _snake_case   :				List[Any]														=	[-1] * number_of_cells

  for car_index in range(lowerCAmelCase				):
    if highway_now[car_index] != -1:
      # Add 1 to the current speed of the car and cap the speed
      _snake_case   :				int														=	min(highway_now[car_index] + 1    ,							lowerCAmelCase				)
      # Number of empty cell before the next car
      _snake_case   :				Tuple														=	get_distance(lowerCAmelCase    ,							lowerCAmelCase				) - 1
      # We can't have the car causing an accident
      _snake_case   :				Union[str, Any]														=	min(next_highway[car_index]    ,							lowerCAmelCase				)
      if random() < probability:
        # Randomly, a driver will slow down
        _snake_case   :				int														=	max(next_highway[car_index] - 1    ,							0				)
  return next_highway


def     lowerCamelCase_	(      lowerCAmelCase:	list    ,							lowerCAmelCase:	int    ,							lowerCAmelCase:	float    ,							lowerCAmelCase:	int				)->		list:
  _snake_case   :				Dict														=	len(highway[0]				)

  for i in range(lowerCAmelCase				):
    _snake_case   :				Any														=	update(highway[i]    ,							lowerCAmelCase    ,							lowerCAmelCase				)
    _snake_case   :				Tuple														=	[-1] * number_of_cells

    for car_index in range(lowerCAmelCase				):
      _snake_case   :				Union[str, Any]														=	next_speeds_calculated[car_index]
      if speed != -1:
        # Change the position based on the speed (with % to create the loop)
        _snake_case   :				Union[str, Any]														=	(car_index + speed) % number_of_cells
        # Commit the change of position
        _snake_case   :				Tuple														=	speed
    highway.append(lowerCAmelCase				)

  return highway


if __name__ == "__main__":
      import doctest

      doctest.testmod()




