



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


lowerCAmelCase_     =	False




class  _snake_case		(    unittest.TestCase ):







				"""simple docstring"""






				pass




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







				"""simple docstring"""






				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







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





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    VersatileDiffusionTextToImagePipeline.from_pretrained("""shi-labs/versatile-diffusion""")
											# remove text_unet
											pipe.remove_unused_weights()
											pipe.to(_lowerCAmelCase)
											pipe.set_progress_bar_config(disable=_lowerCAmelCase)

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

											with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(_lowerCAmelCase)
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    VersatileDiffusionTextToImagePipeline.from_pretrained(_lowerCAmelCase)
											pipe.to(_lowerCAmelCase)
											pipe.set_progress_bar_config(disable=_lowerCAmelCase)

											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    generator.manual_seed(0)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    pipe(
											    prompt=_lowerCAmelCase      ,    generator=_lowerCAmelCase      ,    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							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    VersatileDiffusionTextToImagePipeline.from_pretrained(
											    """shi-labs/versatile-diffusion"""      ,    torch_dtype=torch.floataa)
											pipe.to(_lowerCAmelCase)
											pipe.set_progress_bar_config(disable=_lowerCAmelCase)

											_SCREAMING_SNAKE_CASE		:						Dict			  =    """A painting of a squirrel eating a burger """
											_SCREAMING_SNAKE_CASE		:						Dict			  =    torch.manual_seed(0)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    pipe(
											    prompt=_lowerCAmelCase      ,    generator=_lowerCAmelCase      ,    guidance_scale=7.5      ,    num_inference_steps=5_0      ,    output_type="""numpy""").images

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    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)
											_SCREAMING_SNAKE_CASE		:						List[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"""





import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
			import torch

			from transformers import AutoModelForImageClassification

if is_vision_available():
			from transformers import AutoImageProcessor




@require_torch
@require_vision
class  _snake_case		(    unittest.TestCase ):







				"""simple docstring"""






				@slow
				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    AutoImageProcessor.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip""")
											_SCREAMING_SNAKE_CASE		:						str			  =    AutoModelForImageClassification.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip""")
											model.to(_A)

											from datasets import load_dataset

											_SCREAMING_SNAKE_CASE		:						Any			  =    load_dataset("""nielsr/rvlcdip-demo""")

											_SCREAMING_SNAKE_CASE		:						Any			  =    dataset["""train"""][0]["""image"""].convert("""RGB""")

											_SCREAMING_SNAKE_CASE		:						str			  =    image_processor(_A      ,    return_tensors="""pt""").to(_A)

											# forward pass
											with torch.no_grad():
																		_SCREAMING_SNAKE_CASE		:						Any			  =    model(**_A)
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    outputs.logits

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    torch.Size((1, 1_6))
											self.assertEqual(logits.shape      ,    _A)

											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    torch.tensor(
											    [-0.4_158, -0.4_092, -0.4_347]      ,    device=_A      ,    dtype=torch.float      ,    )
											self.assertTrue(torch.allclose(logits[0, :3]      ,    _A      ,    atol=1e-4))


