
import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()




@slow
@require_torch_gpu
class 				_lowerCamelCase (      unittest.TestCase ):





     """simple docstring"""







     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[Any]:




           """simple docstring"""







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






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Tuple:




           """simple docstring"""







           UpperCamelCase__    :   Optional[int]					=				StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''  )
           UpperCamelCase__    :   Optional[int]					=				sd_pipe.to(__SCREAMING_SNAKE_CASE  )
           sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE  )

           sd_pipe.set_scheduler('''sample_euler'''  )

           UpperCamelCase__    :   List[str]					=				'''A painting of a squirrel eating a burger'''
           UpperCamelCase__    :   Tuple					=				torch.manual_seed(0  )
           UpperCamelCase__    :   Any					=				sd_pipe([prompt]						,						generator=__SCREAMING_SNAKE_CASE						,						guidance_scale=9.0						,						num_inference_steps=2_0						,						output_type='''np'''  )

           UpperCamelCase__    :   Optional[int]					=				output.images

           UpperCamelCase__    :   Tuple					=				image[0, -3:, -3:, -1]

           assert image.shape == (1, 5_1_2, 5_1_2, 3)
           UpperCamelCase__    :   List[Any]					=				np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]  )

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






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   Any:




           """simple docstring"""







           UpperCamelCase__    :   Tuple					=				StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''  )
           UpperCamelCase__    :   Tuple					=				sd_pipe.to(__SCREAMING_SNAKE_CASE  )
           sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE  )

           sd_pipe.set_scheduler('''sample_euler'''  )

           UpperCamelCase__    :   Optional[Any]					=				'''A painting of a squirrel eating a burger'''
           UpperCamelCase__    :   Optional[Any]					=				torch.manual_seed(0  )
           UpperCamelCase__    :   Any					=				sd_pipe([prompt]						,						generator=__SCREAMING_SNAKE_CASE						,						guidance_scale=9.0						,						num_inference_steps=2_0						,						output_type='''np'''  )

           UpperCamelCase__    :   List[str]					=				output.images

           UpperCamelCase__    :   Optional[int]					=				image[0, -3:, -3:, -1]

           assert image.shape == (1, 5_1_2, 5_1_2, 3)
           UpperCamelCase__    :   Dict					=				np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]  )

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






     def    __SCREAMING_SNAKE_CASE			(							self  )  ->   List[Any]:




           """simple docstring"""







           UpperCamelCase__    :   int					=				StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''  )
           UpperCamelCase__    :   Union[str, Any]					=				sd_pipe.to(__SCREAMING_SNAKE_CASE  )
           sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE  )

           sd_pipe.set_scheduler('''sample_dpmpp_2m'''  )

           UpperCamelCase__    :   List[str]					=				'''A painting of a squirrel eating a burger'''
           UpperCamelCase__    :   Tuple					=				torch.manual_seed(0  )
           UpperCamelCase__    :   Any					=				sd_pipe(
               [prompt]						,						generator=__SCREAMING_SNAKE_CASE						,						guidance_scale=7.5						,						num_inference_steps=1_5						,						output_type='''np'''						,						use_karras_sigmas=__SCREAMING_SNAKE_CASE						,						)

           UpperCamelCase__    :   Tuple					=				output.images

           UpperCamelCase__    :   Any					=				image[0, -3:, -3:, -1]

           assert image.shape == (1, 5_1_2, 5_1_2, 3)
           UpperCamelCase__    :   Dict					=				np.array(
               [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048]  )

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




import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel




class 				_lowerCamelCase (      UpperCamelCase_ ):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		   =  '''M-CLIP'''






     def __init__(							self						,						__SCREAMING_SNAKE_CASE=1_0_2_4						,						__SCREAMING_SNAKE_CASE=7_6_8						,						**__SCREAMING_SNAKE_CASE  )  ->   List[Any]:




           """simple docstring"""







           UpperCamelCase__    :   Dict					=				transformerDimSize
           UpperCamelCase__    :   Any					=				imageDimSize
           super().__init__(**__SCREAMING_SNAKE_CASE  )




class 				_lowerCamelCase (      UpperCamelCase_ ):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		   =  MCLIPConfig






     def __init__(							self						,						__SCREAMING_SNAKE_CASE						,						*__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )  ->   Optional[int]:




           """simple docstring"""







           super().__init__(__SCREAMING_SNAKE_CASE						,						*__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )
           UpperCamelCase__    :   Union[str, Any]					=				XLMRobertaModel(__SCREAMING_SNAKE_CASE  )
           UpperCamelCase__    :   Tuple					=				torch.nn.Linear(
               in_features=config.transformerDimensions						,						out_features=config.numDims  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   List[str]:




           """simple docstring"""







           UpperCamelCase__    :   Tuple					=				self.transformer(input_ids=__SCREAMING_SNAKE_CASE						,						attention_mask=__SCREAMING_SNAKE_CASE  )[0]
           UpperCamelCase__    :   Tuple					=				(embs * attention_mask.unsqueeze(2  )).sum(dim=1  ) / attention_mask.sum(dim=1  )[:, None]
           return self.LinearTransformation(__SCREAMING_SNAKE_CASE  ), embs


