


"""simple docstring"""





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


snake_case__    				=				False



class 		snake_case_(       unittest.TestCase			):
     pass



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






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






     def      lowerCamelCase__     (	self  :					Optional[int]				):
           lowerCAmelCase					:	Any       =   VersatileDiffusionTextToImagePipeline.from_pretrained('''shi-labs/versatile-diffusion'''				)
           # remove text_unet
           pipe.remove_unused_weights()
           pipe.to(UpperCamelCase_				)
           pipe.set_progress_bar_config(disable=UpperCamelCase_				)

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

           with tempfile.TemporaryDirectory() as tmpdirname:
                 pipe.save_pretrained(UpperCamelCase_				)
                 lowerCAmelCase					:	Dict       =   VersatileDiffusionTextToImagePipeline.from_pretrained(UpperCamelCase_				)
           pipe.to(UpperCamelCase_				)
           pipe.set_progress_bar_config(disable=UpperCamelCase_				)

           lowerCAmelCase					:	Optional[int]       =   generator.manual_seed(0				)
           lowerCAmelCase					:	Optional[int]       =   pipe(
               prompt=UpperCamelCase_					,				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      lowerCamelCase__     (	self  :					Tuple				):
           lowerCAmelCase					:	List[Any]       =   VersatileDiffusionTextToImagePipeline.from_pretrained(
               '''shi-labs/versatile-diffusion'''					,				torch_dtype=torch.floataa				)
           pipe.to(UpperCamelCase_				)
           pipe.set_progress_bar_config(disable=UpperCamelCase_				)

           lowerCAmelCase					:	Optional[Any]       =   '''A painting of a squirrel eating a burger '''
           lowerCAmelCase					:	Any       =   torch.manual_seed(0				)
           lowerCAmelCase					:	int       =   pipe(
               prompt=UpperCamelCase_					,				generator=UpperCamelCase_					,				guidance_scale=7.5					,				num_inference_steps=5_0					,				output_type='''numpy'''				).images

           lowerCAmelCase					:	Any       =   image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

           assert image.shape == (1, 5_1_2, 5_1_2, 3)
           lowerCAmelCase					:	Any       =   np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778]				)

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






"""simple docstring"""





def      _snake_case						(						_snake_case				:			float ,   _snake_case				:			list[float]			):
      if discount_rate < 0:
            raise ValueError('''Discount rate cannot be negative'''			)
      if not cash_flows:
            raise ValueError('''Cash flows list cannot be empty'''			)
      lowerCAmelCase					:	List[str]       =   sum(
          cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_snake_case			)			)
      return round(_snake_case ,   ndigits=2			)


if __name__ == "__main__":
 import doctest

 doctest.testmod()


