






"""simple docstring"""







import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
			import torch

			from transformers import XLMRobertaModel


@require_sentencepiece
@require_tokenizers
@require_torch
class    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Any=	XLMRobertaModel.from_pretrained("xlm-roberta-base"      )
												lowercase__    :				int=	torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]      )
												# The dog is cute and lives in the garden house

												lowercase__    :				Optional[int]=	torch.Size((1, 12, 768)      )  # batch_size, sequence_length, embedding_vector_dim
												lowercase__    :				int=	torch.tensor(
												    [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]]      )
												#  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
												#  xlmr.eval()
												#  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
												with torch.no_grad():
																		lowercase__    :				Union[str, Any]=	model(snake_case__      )["last_hidden_state"].detach()
												self.assertEqual(output.shape	,  snake_case__      )
												# compare the actual values for a slice of last dim
												self.assertTrue(torch.allclose(output[:, :, -1]	,  snake_case__	,  atol=1e-3      )      )




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	XLMRobertaModel.from_pretrained("xlm-roberta-large"      )
												lowercase__    :				List[Any]=	torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]      )
												# The dog is cute and lives in the garden house

												lowercase__    :				Optional[int]=	torch.Size((1, 12, 1024)      )  # batch_size, sequence_length, embedding_vector_dim
												lowercase__    :				Optional[int]=	torch.tensor(
												    [[-0.06_99, -0.03_18, 0.07_05, -0.12_41, 0.09_99, -0.05_20, 0.10_04, -0.18_38, -0.47_04, 0.14_37, 0.08_21, 0.01_26]]      )
												#  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
												#  xlmr.eval()
												#  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
												with torch.no_grad():
																		lowercase__    :				Optional[Any]=	model(snake_case__      )["last_hidden_state"].detach()
												self.assertEqual(output.shape	,  snake_case__      )
												# compare the actual values for a slice of last dim
												self.assertTrue(torch.allclose(output[:, :, -1]	,  snake_case__	,  atol=1e-3      )      )














"""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


a			:    Union[str, Any]				 =		False


class    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						pass


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




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Dict=	VersatileDiffusionTextToImagePipeline.from_pretrained("shi-labs/versatile-diffusion"      )
												# remove text_unet
												pipe.remove_unused_weights()
												pipe.to(snake_case__      )
												pipe.set_progress_bar_config(disable=snake_case__      )

												lowercase__    :				Any=	"A painting of a squirrel eating a burger "
												lowercase__    :				Optional[Any]=	torch.manual_seed(0      )
												lowercase__    :				List[str]=	pipe(
												    prompt=snake_case__	,  generator=snake_case__	,  guidance_scale=7.5	,  num_inference_steps=2	,  output_type="numpy"      ).images

												with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(snake_case__      )
																		lowercase__    :				Optional[Any]=	VersatileDiffusionTextToImagePipeline.from_pretrained(snake_case__      )
												pipe.to(snake_case__      )
												pipe.set_progress_bar_config(disable=snake_case__      )

												lowercase__    :				Any=	generator.manual_seed(0      )
												lowercase__    :				Tuple=	pipe(
												    prompt=snake_case__	,  generator=snake_case__	,  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      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	VersatileDiffusionTextToImagePipeline.from_pretrained(
												    "shi-labs/versatile-diffusion"	,  torch_dtype=torch.floataa      )
												pipe.to(snake_case__      )
												pipe.set_progress_bar_config(disable=snake_case__      )

												lowercase__    :				List[str]=	"A painting of a squirrel eating a burger "
												lowercase__    :				Union[str, Any]=	torch.manual_seed(0      )
												lowercase__    :				Optional[Any]=	pipe(
												    prompt=snake_case__	,  generator=snake_case__	,  guidance_scale=7.5	,  num_inference_steps=50	,  output_type="numpy"      ).images

												lowercase__    :				List[str]=	image[0, 253:256, 253:256, -1]

												assert image.shape == (1, 512, 512, 3)
												lowercase__    :				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






