

"""simple docstring"""


import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
			from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor





class     snake_case	:

				def __init__(	self							:				int     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				Dict=1_3     ,  UpperCamelCase__							:				Tuple=3_2     ,  UpperCamelCase__							:				List[Any]=3     ,  UpperCamelCase__							:				Any=4     ,  UpperCamelCase__							:				str=[1_0, 2_0, 3_0, 4_0]     ,  UpperCamelCase__							:				Dict=[2, 2, 3, 2]     ,  UpperCamelCase__							:				Tuple=True     ,  UpperCamelCase__							:				List[str]=True     ,  UpperCamelCase__							:				Union[str, Any]=3_7     ,  UpperCamelCase__							:				str="gelu"     ,  UpperCamelCase__							:				Union[str, Any]=1_0     ,  UpperCamelCase__							:				Optional[Any]=0.02     ,  UpperCamelCase__							:				int=["stage2", "stage3", "stage4"]     ,  UpperCamelCase__							:				Union[str, Any]=[2, 3, 4]     ,  UpperCamelCase__							:				List[str]=None     ,  )->   Optional[Any]:



						'''simple docstring'''


						__lowerCAmelCase:       List[str]          =			parent
						__lowerCAmelCase:       int          =			batch_size
						__lowerCAmelCase:       Optional[Any]          =			image_size
						__lowerCAmelCase:       int          =			num_channels
						__lowerCAmelCase:       Dict          =			num_stages
						__lowerCAmelCase:       str          =			hidden_sizes
						__lowerCAmelCase:       List[Any]          =			depths
						__lowerCAmelCase:       Union[str, Any]          =			is_training
						__lowerCAmelCase:       List[Any]          =			use_labels
						__lowerCAmelCase:       int          =			intermediate_size
						__lowerCAmelCase:       Optional[Any]          =			hidden_act
						__lowerCAmelCase:       str          =			num_labels
						__lowerCAmelCase:       str          =			initializer_range
						__lowerCAmelCase:       Union[str, Any]          =			out_features
						__lowerCAmelCase:       Union[str, Any]          =			out_indices
						__lowerCAmelCase:       str          =			scope

				def 							lowercase_     (	self							:				Union[str, Any])->   Any:



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

						__lowerCAmelCase:       List[Any]          =			None
						if self.use_labels:
								__lowerCAmelCase:       Optional[int]          =			ids_tensor([self.batch_size]     ,  self.num_labels)

						__lowerCAmelCase:       Union[str, Any]          =			self.get_config()

						return config, pixel_values, labels

				def 							lowercase_     (	self							:				Optional[int])->   List[str]:



						'''simple docstring'''


						return ConvNextVaConfig(
						    num_channels=self.num_channels     ,  hidden_sizes=self.hidden_sizes     ,  depths=self.depths     ,  num_stages=self.num_stages     ,  hidden_act=self.hidden_act     ,  is_decoder=UpperCamelCase__     ,  initializer_range=self.initializer_range     ,  out_features=self.out_features     ,  out_indices=self.out_indices     ,  num_labels=self.num_labels     ,  )

				def 							lowercase_     (	self							:				Dict     ,  UpperCamelCase__							:				Tuple     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Tuple)->   List[str]:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			ConvNextVaModel(config=UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()
						__lowerCAmelCase:       List[Any]          =			model(UpperCamelCase__)
						# expected last hidden states: B, C, H // 32, W // 32
						self.parent.assertEqual(
						    result.last_hidden_state.shape     ,  (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)     ,  )

				def 							lowercase_     (	self							:				str     ,  UpperCamelCase__							:				Any     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Optional[Any])->   Dict:



						'''simple docstring'''


						__lowerCAmelCase:       Tuple          =			ConvNextVaForImageClassification(UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()
						__lowerCAmelCase:       int          =			model(UpperCamelCase__     ,  labels=UpperCamelCase__)
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels))

				def 							lowercase_     (	self							:				int     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				Optional[Any]     ,  UpperCamelCase__							:				Union[str, Any])->   Any:



						'''simple docstring'''


						__lowerCAmelCase:       Optional[int]          =			ConvNextVaBackbone(config=UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()
						__lowerCAmelCase:       int          =			model(UpperCamelCase__)

						# verify hidden states
						self.parent.assertEqual(len(result.feature_maps)     ,  len(config.out_features))
						self.parent.assertListEqual(list(result.feature_maps[0].shape)     ,  [self.batch_size, self.hidden_sizes[1], 4, 4])

						# verify channels
						self.parent.assertEqual(len(model.channels)     ,  len(config.out_features))
						self.parent.assertListEqual(model.channels     ,  config.hidden_sizes[1:])

						# verify backbone works with out_features=None
						__lowerCAmelCase:       List[Any]          =			None
						__lowerCAmelCase:       Any          =			ConvNextVaBackbone(config=UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()
						__lowerCAmelCase:       Tuple          =			model(UpperCamelCase__)

						# verify feature maps
						self.parent.assertEqual(len(result.feature_maps)     ,  1)
						self.parent.assertListEqual(list(result.feature_maps[0].shape)     ,  [self.batch_size, self.hidden_sizes[-1], 1, 1])

						# verify channels
						self.parent.assertEqual(len(model.channels)     ,  1)
						self.parent.assertListEqual(model.channels     ,  [config.hidden_sizes[-1]])

				def 							lowercase_     (	self							:				int)->   Dict:



						'''simple docstring'''


						__lowerCAmelCase:       List[Any]          =			self.prepare_config_and_inputs()
						__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase:       int          =			config_and_inputs
						__lowerCAmelCase:       Optional[int]          =			{"pixel_values": pixel_values}
						return config, inputs_dict




				def 							lowercase_     (	self							:				Optional[int])->   Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:       str          =			self.prepare_config_and_inputs()
						__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase:       List[Any]          =			config_and_inputs
						__lowerCAmelCase:       List[str]          =			{"pixel_values": pixel_values, "labels": labels}
						return config, inputs_dict





@require_torch
class     snake_case	(				__snake_case,							__snake_case,							unittest.TestCase  ):
				SCREAMING_SNAKE_CASE_   :      str         =  (
				    (
				        ConvNextVaModel,
				        ConvNextVaForImageClassification,
				        ConvNextVaBackbone,
				    )
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_   :      Optional[Any]         =  (
				    {"""feature-extraction""": ConvNextVaModel, """image-classification""": ConvNextVaForImageClassification}
				    if is_torch_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE_   :      Union[str, Any]         =  False
				SCREAMING_SNAKE_CASE_   :      Union[str, Any]         =  False
				SCREAMING_SNAKE_CASE_   :      int         =  False
				SCREAMING_SNAKE_CASE_   :      Union[str, Any]         =  False
				SCREAMING_SNAKE_CASE_   :      Dict         =  False

				def 							lowercase_     (	self							:				Optional[Any])->   Any:



						'''simple docstring'''


						__lowerCAmelCase:       List[str]          =			ConvNextVaModelTester(self)
						__lowerCAmelCase:       Optional[Any]          =			ConfigTester(self     ,  config_class=UpperCamelCase__     ,  has_text_modality=UpperCamelCase__     ,  hidden_size=3_7)

				def 							lowercase_     (	self							:				Dict)->   str:



						'''simple docstring'''


						self.create_and_test_config_common_properties()
						self.config_tester.create_and_test_config_to_json_string()
						self.config_tester.create_and_test_config_to_json_file()
						self.config_tester.create_and_test_config_from_and_save_pretrained()
						self.config_tester.create_and_test_config_with_num_labels()
						self.config_tester.check_config_can_be_init_without_params()
						self.config_tester.check_config_arguments_init()

				def 							lowercase_     (	self							:				Tuple)->   Dict:



						'''simple docstring'''


						return

				@unittest.skip(reason="ConvNextV2 does not use inputs_embeds")
				def 							lowercase_     (	self							:				Optional[int])->   List[Any]:



						'''simple docstring'''


						pass

				@unittest.skip(reason="ConvNextV2 does not support input and output embeddings")
				def 							lowercase_     (	self							:				Optional[int])->   int:



						'''simple docstring'''


						pass

				@unittest.skip(reason="ConvNextV2 does not use feedforward chunking")
				def 							lowercase_     (	self							:				str)->   Any:



						'''simple docstring'''


						pass

				def 							lowercase_     (	self							:				str)->   Any:



						'''simple docstring'''


						if not self.model_tester.is_training:
								return

						for model_class in self.all_model_classes:
								__lowerCAmelCase      , __lowerCAmelCase:       Dict          =			self.model_tester.prepare_config_and_inputs_with_labels()
								__lowerCAmelCase:       Optional[int]          =			True

								if model_class.__name__ in [
								    *get_values(UpperCamelCase__),
								    *get_values(UpperCamelCase__),
								]:
										continue

								__lowerCAmelCase:       Optional[Any]          =			model_class(UpperCamelCase__)
								model.to(UpperCamelCase__)
								model.train()
								__lowerCAmelCase:       str          =			self._prepare_for_class(UpperCamelCase__     ,  UpperCamelCase__     ,  return_labels=UpperCamelCase__)
								__lowerCAmelCase:       List[Any]          =			model(**UpperCamelCase__).loss
								loss.backward()

				def 							lowercase_     (	self							:				Dict)->   str:



						'''simple docstring'''


						if not self.model_tester.is_training:
								return

						for model_class in self.all_model_classes:
								__lowerCAmelCase      , __lowerCAmelCase:       int          =			self.model_tester.prepare_config_and_inputs_with_labels()
								__lowerCAmelCase:       str          =			False
								__lowerCAmelCase:       List[str]          =			True

								if (
								    model_class.__name__
								    in [*get_values(UpperCamelCase__), *get_values(UpperCamelCase__)]
								    or not model_class.supports_gradient_checkpointing
								):
										continue

								__lowerCAmelCase:       Any          =			model_class(UpperCamelCase__)
								model.to(UpperCamelCase__)
								model.gradient_checkpointing_enable()
								model.train()
								__lowerCAmelCase:       int          =			self._prepare_for_class(UpperCamelCase__     ,  UpperCamelCase__     ,  return_labels=UpperCamelCase__)
								__lowerCAmelCase:       Optional[Any]          =			model(**UpperCamelCase__).loss
								loss.backward()

				def 							lowercase_     (	self							:				Dict)->   Dict:



						'''simple docstring'''


						__lowerCAmelCase      , __lowerCAmelCase:       Optional[int]          =			self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								__lowerCAmelCase:       str          =			model_class(UpperCamelCase__)
								__lowerCAmelCase:       Optional[Any]          =			inspect.signature(model.forward)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								__lowerCAmelCase:       Optional[int]          =			[*signature.parameters.keys()]

								__lowerCAmelCase:       Optional[Any]          =			["pixel_values"]
								self.assertListEqual(arg_names[:1]     ,  UpperCamelCase__)

				def 							lowercase_     (	self							:				Any)->   int:



						'''simple docstring'''


						__lowerCAmelCase:       int          =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase__)

				def 							lowercase_     (	self							:				Optional[int])->   Union[str, Any]:



						'''simple docstring'''


						def check_hidden_states_output(UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				List[Any]     ,  UpperCamelCase__							:				Any):
								__lowerCAmelCase:       Optional[int]          =			model_class(UpperCamelCase__)
								model.to(UpperCamelCase__)
								model.eval()

								with torch.no_grad():
										__lowerCAmelCase:       int          =			model(**self._prepare_for_class(UpperCamelCase__     ,  UpperCamelCase__))

								__lowerCAmelCase:       str          =			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

								__lowerCAmelCase:       Optional[int]          =			self.model_tester.num_stages
								self.assertEqual(len(UpperCamelCase__)     ,  expected_num_stages + 1)

								# ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width)
								self.assertListEqual(
								    list(hidden_states[0].shape[-2:])     ,  [self.model_tester.image_size // 4, self.model_tester.image_size // 4]     ,  )

						__lowerCAmelCase      , __lowerCAmelCase:       Union[str, Any]          =			self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								__lowerCAmelCase:       Tuple          =			True
								check_hidden_states_output(UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__)

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								__lowerCAmelCase:       Dict          =			True

								check_hidden_states_output(UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__)

				def 							lowercase_     (	self							:				int)->   Any:



						'''simple docstring'''


						__lowerCAmelCase:       Optional[Any]          =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__)




				@slow
				def 							lowercase_     (	self							:				Tuple)->   int:



						'''simple docstring'''


						for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowerCAmelCase:       List[str]          =			ConvNextVaModel.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)






def     a__    (    )	->	str:
		__lowerCAmelCase:       str          =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
		return image





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

				@cached_property
				def 							lowercase_     (	self							:				List[Any])->   int:



						'''simple docstring'''


						return AutoImageProcessor.from_pretrained("facebook/convnextv2-tiny-1k-224") if is_vision_available() else None




				@slow
				def 							lowercase_     (	self							:				Optional[int])->   Optional[int]:



						'''simple docstring'''


						__lowerCAmelCase:       str          =			ConvNextVaForImageClassification.from_pretrained("facebook/convnextv2-tiny-1k-224").to(UpperCamelCase__)

						__lowerCAmelCase:       Optional[int]          =			self.default_image_processor
						__lowerCAmelCase:       Any          =			prepare_img()
						__lowerCAmelCase:       List[Any]          =			preprocessor(images=UpperCamelCase__     ,  return_tensors="pt").to(UpperCamelCase__)

						# forward pass
						with torch.no_grad():
								__lowerCAmelCase:       Optional[Any]          =			model(**UpperCamelCase__)

						# verify the logits
						__lowerCAmelCase:       List[str]          =			torch.Size((1, 1_0_0_0))
						self.assertEqual(outputs.logits.shape     ,  UpperCamelCase__)

						__lowerCAmelCase:       str          =			torch.tensor([0.9996, 0.1966, -0.4386]).to(UpperCamelCase__)
						self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,  UpperCamelCase__     ,  atol=1e-4))





"""simple docstring"""


import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





class     snake_case	(				unittest.TestCase  ):

				def 							lowercase_     (	self							:				Optional[int])->   int:



						'''simple docstring'''


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

				@property
				def 							lowercase_     (	self							:				int)->   str:



						'''simple docstring'''


						__lowerCAmelCase:       str          =			1
						__lowerCAmelCase:       Union[str, Any]          =			3
						__lowerCAmelCase:       Union[str, Any]          =			(3_2, 3_2)

						__lowerCAmelCase:       Union[str, Any]          =			floats_tensor((batch_size, num_channels) + sizes     ,  rng=random.Random(0)).to(UpperCamelCase__)
						return image

				@property
				def 							lowercase_     (	self							:				Tuple)->   str:



						'''simple docstring'''


						torch.manual_seed(0)
						__lowerCAmelCase:       Optional[Any]          =			UNetaDConditionModel(
						    block_out_channels=(3_2, 3_2, 6_4)     ,  layers_per_block=2     ,  sample_size=3_2     ,  in_channels=7     ,  out_channels=4     ,  down_block_types=("DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D")     ,  up_block_types=("CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D")     ,  cross_attention_dim=3_2     ,  attention_head_dim=8     ,  use_linear_projection=UpperCamelCase__     ,  only_cross_attention=(True, True, False)     ,  num_class_embeds=1_0_0     ,  )
						return model

				@property
				def 							lowercase_     (	self							:				Any)->   Optional[Any]:



						'''simple docstring'''


						torch.manual_seed(0)
						__lowerCAmelCase:       Tuple          =			AutoencoderKL(
						    block_out_channels=[3_2, 3_2, 6_4]     ,  in_channels=3     ,  out_channels=3     ,  down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D"]     ,  up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"]     ,  latent_channels=4     ,  )
						return model

				@property
				def 							lowercase_     (	self							:				Any)->   Optional[Any]:



						'''simple docstring'''


						torch.manual_seed(0)
						__lowerCAmelCase:       List[str]          =			CLIPTextConfig(
						    bos_token_id=0     ,  eos_token_id=2     ,  hidden_size=3_2     ,  intermediate_size=3_7     ,  layer_norm_eps=1e-05     ,  num_attention_heads=4     ,  num_hidden_layers=5     ,  pad_token_id=1     ,  vocab_size=1_0_0_0     ,  hidden_act="gelu"     ,  projection_dim=5_1_2     ,  )
						return CLIPTextModel(UpperCamelCase__)

				def 							lowercase_     (	self							:				List[str])->   Dict:



						'''simple docstring'''


						__lowerCAmelCase:       Tuple          =			"cpu"  # ensure determinism for the device-dependent torch.Generator
						__lowerCAmelCase:       int          =			self.dummy_cond_unet_upscale
						__lowerCAmelCase:       int          =			DDPMScheduler()
						__lowerCAmelCase:       List[str]          =			DDIMScheduler(prediction_type="v_prediction")
						__lowerCAmelCase:       Tuple          =			self.dummy_vae
						__lowerCAmelCase:       Optional[Any]          =			self.dummy_text_encoder
						__lowerCAmelCase:       Any          =			CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")

						__lowerCAmelCase:       Tuple          =			self.dummy_image.cpu().permute(0     ,  2     ,  3     ,  1)[0]
						__lowerCAmelCase:       List[Any]          =			Image.fromarray(np.uinta(UpperCamelCase__)).convert("RGB").resize((6_4, 6_4))

						# make sure here that pndm scheduler skips prk
						__lowerCAmelCase:       Optional[int]          =			StableDiffusionUpscalePipeline(
						    unet=UpperCamelCase__     ,  low_res_scheduler=UpperCamelCase__     ,  scheduler=UpperCamelCase__     ,  vae=UpperCamelCase__     ,  text_encoder=UpperCamelCase__     ,  tokenizer=UpperCamelCase__     ,  max_noise_level=3_5_0     ,  )
						__lowerCAmelCase:       Tuple          =			sd_pipe.to(UpperCamelCase__)
						sd_pipe.set_progress_bar_config(disable=UpperCamelCase__)

						__lowerCAmelCase:       Any          =			"A painting of a squirrel eating a burger"
						__lowerCAmelCase:       str          =			torch.Generator(device=UpperCamelCase__).manual_seed(0)
						__lowerCAmelCase:       Optional[int]          =			sd_pipe(
						    [prompt]     ,  image=UpperCamelCase__     ,  generator=UpperCamelCase__     ,  guidance_scale=6.0     ,  noise_level=2_0     ,  num_inference_steps=2     ,  output_type="np"     ,  )

						__lowerCAmelCase:       List[str]          =			output.images

						__lowerCAmelCase:       Union[str, Any]          =			torch.Generator(device=UpperCamelCase__).manual_seed(0)
						__lowerCAmelCase:       List[str]          =			sd_pipe(
						    [prompt]     ,  image=UpperCamelCase__     ,  generator=UpperCamelCase__     ,  guidance_scale=6.0     ,  noise_level=2_0     ,  num_inference_steps=2     ,  output_type="np"     ,  return_dict=UpperCamelCase__     ,  )[0]

						__lowerCAmelCase:       int          =			image[0, -3:, -3:, -1]
						__lowerCAmelCase:       Dict          =			image_from_tuple[0, -3:, -3:, -1]

						__lowerCAmelCase:       Dict          =			low_res_image.size[0] * 4
						assert image.shape == (1, expected_height_width, expected_height_width, 3)
						__lowerCAmelCase:       List[Any]          =			np.array([0.3113, 0.3910, 0.4272, 0.4859, 0.5061, 0.4652, 0.5362, 0.5715, 0.5661])

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

				def 							lowercase_     (	self							:				List[str])->   Optional[Any]:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			"cpu"  # ensure determinism for the device-dependent torch.Generator
						__lowerCAmelCase:       Dict          =			self.dummy_cond_unet_upscale
						__lowerCAmelCase:       List[str]          =			DDPMScheduler()
						__lowerCAmelCase:       Union[str, Any]          =			DDIMScheduler(prediction_type="v_prediction")
						__lowerCAmelCase:       Optional[int]          =			self.dummy_vae
						__lowerCAmelCase:       List[Any]          =			self.dummy_text_encoder
						__lowerCAmelCase:       Optional[int]          =			CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")

						__lowerCAmelCase:       List[Any]          =			self.dummy_image.cpu().permute(0     ,  2     ,  3     ,  1)[0]
						__lowerCAmelCase:       str          =			Image.fromarray(np.uinta(UpperCamelCase__)).convert("RGB").resize((6_4, 6_4))

						# make sure here that pndm scheduler skips prk
						__lowerCAmelCase:       Optional[int]          =			StableDiffusionUpscalePipeline(
						    unet=UpperCamelCase__     ,  low_res_scheduler=UpperCamelCase__     ,  scheduler=UpperCamelCase__     ,  vae=UpperCamelCase__     ,  text_encoder=UpperCamelCase__     ,  tokenizer=UpperCamelCase__     ,  max_noise_level=3_5_0     ,  )
						__lowerCAmelCase:       Optional[int]          =			sd_pipe.to(UpperCamelCase__)
						sd_pipe.set_progress_bar_config(disable=UpperCamelCase__)

						__lowerCAmelCase:       List[str]          =			"A painting of a squirrel eating a burger"
						__lowerCAmelCase:       List[Any]          =			sd_pipe(
						    2 * [prompt]     ,  image=2 * [low_res_image]     ,  guidance_scale=6.0     ,  noise_level=2_0     ,  num_inference_steps=2     ,  output_type="np"     ,  )
						__lowerCAmelCase:       List[Any]          =			output.images
						assert image.shape[0] == 2

						__lowerCAmelCase:       Dict          =			torch.Generator(device=UpperCamelCase__).manual_seed(0)
						__lowerCAmelCase:       Optional[Any]          =			sd_pipe(
						    [prompt]     ,  image=UpperCamelCase__     ,  generator=UpperCamelCase__     ,  num_images_per_prompt=2     ,  guidance_scale=6.0     ,  noise_level=2_0     ,  num_inference_steps=2     ,  output_type="np"     ,  )
						__lowerCAmelCase:       List[Any]          =			output.images
						assert image.shape[0] == 2




				@unittest.skipIf(torch_device != "cuda"     ,  "This test requires a GPU")
				def 							lowercase_     (	self							:				Tuple)->   Any:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			self.dummy_cond_unet_upscale
						__lowerCAmelCase:       int          =			DDPMScheduler()
						__lowerCAmelCase:       int          =			DDIMScheduler(prediction_type="v_prediction")
						__lowerCAmelCase:       Dict          =			self.dummy_vae
						__lowerCAmelCase:       int          =			self.dummy_text_encoder
						__lowerCAmelCase:       List[Any]          =			CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")

						__lowerCAmelCase:       List[Any]          =			self.dummy_image.cpu().permute(0     ,  2     ,  3     ,  1)[0]
						__lowerCAmelCase:       Optional[int]          =			Image.fromarray(np.uinta(UpperCamelCase__)).convert("RGB").resize((6_4, 6_4))

						# put models in fp16, except vae as it overflows in fp16
						__lowerCAmelCase:       List[Any]          =			unet.half()
						__lowerCAmelCase:       List[str]          =			text_encoder.half()

						# make sure here that pndm scheduler skips prk
						__lowerCAmelCase:       List[Any]          =			StableDiffusionUpscalePipeline(
						    unet=UpperCamelCase__     ,  low_res_scheduler=UpperCamelCase__     ,  scheduler=UpperCamelCase__     ,  vae=UpperCamelCase__     ,  text_encoder=UpperCamelCase__     ,  tokenizer=UpperCamelCase__     ,  max_noise_level=3_5_0     ,  )
						__lowerCAmelCase:       str          =			sd_pipe.to(UpperCamelCase__)
						sd_pipe.set_progress_bar_config(disable=UpperCamelCase__)

						__lowerCAmelCase:       Optional[Any]          =			"A painting of a squirrel eating a burger"
						__lowerCAmelCase:       str          =			torch.manual_seed(0)
						__lowerCAmelCase:       Dict          =			sd_pipe(
						    [prompt]     ,  image=UpperCamelCase__     ,  generator=UpperCamelCase__     ,  num_inference_steps=2     ,  output_type="np"     ,  ).images

						__lowerCAmelCase:       Optional[Any]          =			low_res_image.size[0] * 4
						assert image.shape == (1, expected_height_width, expected_height_width, 3)





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

				def 							lowercase_     (	self							:				Tuple)->   Tuple:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				List[Any])->   Tuple:



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			load_image(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
						    "/sd2-upscale/low_res_cat.png")
						__lowerCAmelCase:       Optional[Any]          =			load_numpy(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"
						    "/upsampled_cat.npy")

						__lowerCAmelCase:       str          =			"stabilityai/stable-diffusion-x4-upscaler"
						__lowerCAmelCase:       Optional[int]          =			StableDiffusionUpscalePipeline.from_pretrained(UpperCamelCase__)
						pipe.to(UpperCamelCase__)
						pipe.set_progress_bar_config(disable=UpperCamelCase__)
						pipe.enable_attention_slicing()

						__lowerCAmelCase:       Tuple          =			"a cat sitting on a park bench"

						__lowerCAmelCase:       int          =			torch.manual_seed(0)
						__lowerCAmelCase:       List[Any]          =			pipe(
						    prompt=UpperCamelCase__     ,  image=UpperCamelCase__     ,  generator=UpperCamelCase__     ,  output_type="np"     ,  )
						__lowerCAmelCase:       Dict          =			output.images[0]

						assert image.shape == (5_1_2, 5_1_2, 3)
						assert np.abs(expected_image - image).max() < 1e-3

				def 							lowercase_     (	self							:				Optional[int])->   Any:



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			load_image(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
						    "/sd2-upscale/low_res_cat.png")
						__lowerCAmelCase:       Tuple          =			load_numpy(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"
						    "/upsampled_cat_fp16.npy")

						__lowerCAmelCase:       Optional[Any]          =			"stabilityai/stable-diffusion-x4-upscaler"
						__lowerCAmelCase:       Tuple          =			StableDiffusionUpscalePipeline.from_pretrained(
						    UpperCamelCase__     ,  torch_dtype=torch.floataa     ,  )
						pipe.to(UpperCamelCase__)
						pipe.set_progress_bar_config(disable=UpperCamelCase__)
						pipe.enable_attention_slicing()

						__lowerCAmelCase:       str          =			"a cat sitting on a park bench"

						__lowerCAmelCase:       List[str]          =			torch.manual_seed(0)
						__lowerCAmelCase:       Optional[Any]          =			pipe(
						    prompt=UpperCamelCase__     ,  image=UpperCamelCase__     ,  generator=UpperCamelCase__     ,  output_type="np"     ,  )
						__lowerCAmelCase:       Union[str, Any]          =			output.images[0]

						assert image.shape == (5_1_2, 5_1_2, 3)
						assert np.abs(expected_image - image).max() < 5e-1




				def 							lowercase_     (	self							:				Optional[int])->   Dict:



						'''simple docstring'''


						torch.cuda.empty_cache()
						torch.cuda.reset_max_memory_allocated()
						torch.cuda.reset_peak_memory_stats()

						__lowerCAmelCase:       Tuple          =			load_image(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
						    "/sd2-upscale/low_res_cat.png")

						__lowerCAmelCase:       Union[str, Any]          =			"stabilityai/stable-diffusion-x4-upscaler"
						__lowerCAmelCase:       Any          =			StableDiffusionUpscalePipeline.from_pretrained(
						    UpperCamelCase__     ,  torch_dtype=torch.floataa     ,  )
						pipe.to(UpperCamelCase__)
						pipe.set_progress_bar_config(disable=UpperCamelCase__)
						pipe.enable_attention_slicing(1)
						pipe.enable_sequential_cpu_offload()

						__lowerCAmelCase:       int          =			"a cat sitting on a park bench"

						__lowerCAmelCase:       Dict          =			torch.manual_seed(0)
						__lowerCAmelCase:       Dict          =			pipe(
						    prompt=UpperCamelCase__     ,  image=UpperCamelCase__     ,  generator=UpperCamelCase__     ,  num_inference_steps=5     ,  output_type="np"     ,  )

						__lowerCAmelCase:       Optional[int]          =			torch.cuda.max_memory_allocated()
						# make sure that less than 2.9 GB is allocated
						assert mem_bytes < 2.9 * 1_0**9


