



'''simple docstring'''
import collections
import inspect
import unittest

from transformers import FocalNetConfig
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_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch
					from torch import nn

					from transformers import (
					    FocalNetBackbone,
					    FocalNetForImageClassification,
					    FocalNetForMaskedImageModeling,
					    FocalNetModel,
					)
					from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor






class 				lowercase__      :





	def __init__(					self   :							Optional[Any]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							str=13					,lowerCamelCase__   :							Union[str, Any]=32					,lowerCamelCase__   :							str=2					,lowerCamelCase__   :							int=3					,lowerCamelCase__   :							Dict=16					,lowerCamelCase__   :							Tuple=[32, 64, 128]					,lowerCamelCase__   :							List[Any]=[1, 2, 1]					,lowerCamelCase__   :							Any=[2, 2, 4]					,lowerCamelCase__   :							Dict=2					,lowerCamelCase__   :							Any=2.0					,lowerCamelCase__   :							Optional[int]=True					,lowerCamelCase__   :							Optional[Any]=0.0					,lowerCamelCase__   :							Union[str, Any]=0.0					,lowerCamelCase__   :							str=0.1					,lowerCamelCase__   :							int="gelu"					,lowerCamelCase__   :							List[Any]=False					,lowerCamelCase__   :							Optional[Any]=True					,lowerCamelCase__   :							List[str]=0.0_2					,lowerCamelCase__   :							Dict=1E-5					,lowerCamelCase__   :							str=True					,lowerCamelCase__   :							List[Any]=None					,lowerCamelCase__   :							Optional[Any]=True					,lowerCamelCase__   :							List[str]=10					,lowerCamelCase__   :							Any=8					,lowerCamelCase__   :							List[str]=["stage1", "stage2"]					,lowerCamelCase__   :							str=[1, 2]					,):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  parent
							_UpperCamelCase   :						Optional[int]												=  batch_size
							_UpperCamelCase   :						Optional[Any]												=  image_size
							_UpperCamelCase   :						Union[str, Any]												=  patch_size
							_UpperCamelCase   :						Tuple												=  num_channels
							_UpperCamelCase   :						int												=  embed_dim
							_UpperCamelCase   :						List[str]												=  hidden_sizes
							_UpperCamelCase   :						List[Any]												=  depths
							_UpperCamelCase   :						Optional[int]												=  num_heads
							_UpperCamelCase   :						Optional[Any]												=  window_size
							_UpperCamelCase   :						str												=  mlp_ratio
							_UpperCamelCase   :						int												=  qkv_bias
							_UpperCamelCase   :						Optional[Any]												=  hidden_dropout_prob
							_UpperCamelCase   :						Union[str, Any]												=  attention_probs_dropout_prob
							_UpperCamelCase   :						Union[str, Any]												=  drop_path_rate
							_UpperCamelCase   :						List[Any]												=  hidden_act
							_UpperCamelCase   :						Optional[Any]												=  use_absolute_embeddings
							_UpperCamelCase   :						str												=  patch_norm
							_UpperCamelCase   :						Any												=  layer_norm_eps
							_UpperCamelCase   :						str												=  initializer_range
							_UpperCamelCase   :						Dict												=  is_training
							_UpperCamelCase   :						Optional[int]												=  scope
							_UpperCamelCase   :						Any												=  use_labels
							_UpperCamelCase   :						Tuple												=  type_sequence_label_size
							_UpperCamelCase   :						List[Any]												=  encoder_stride
							_UpperCamelCase   :						Optional[Any]												=  out_features
							_UpperCamelCase   :						Optional[int]												=  out_indices





	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

							_UpperCamelCase   :						int												=  None
							if self.use_labels:
													_UpperCamelCase   :						Union[str, Any]												=  ids_tensor([self.batch_size]					,self.type_sequence_label_size					)

							_UpperCamelCase   :						List[str]												=  self.get_config()

							return config, pixel_values, labels





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							return FocalNetConfig(
							    image_size=self.image_size					,patch_size=self.patch_size					,num_channels=self.num_channels					,embed_dim=self.embed_dim					,hidden_sizes=self.hidden_sizes					,depths=self.depths					,num_heads=self.num_heads					,window_size=self.window_size					,mlp_ratio=self.mlp_ratio					,qkv_bias=self.qkv_bias					,hidden_dropout_prob=self.hidden_dropout_prob					,attention_probs_dropout_prob=self.attention_probs_dropout_prob					,drop_path_rate=self.drop_path_rate					,hidden_act=self.hidden_act					,use_absolute_embeddings=self.use_absolute_embeddings					,path_norm=self.patch_norm					,layer_norm_eps=self.layer_norm_eps					,initializer_range=self.initializer_range					,encoder_stride=self.encoder_stride					,out_features=self.out_features					,out_indices=self.out_indices					,)





	def       UpperCamelCase_    (					self   :							List[str]					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  FocalNetModel(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Union[str, Any]												=  model(lowerCamelCase__					)

							_UpperCamelCase   :						Union[str, Any]												=  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths					) - 1))
							_UpperCamelCase   :						Dict												=  int(config.embed_dim * 2 ** (len(config.depths					) - 1)					)

							self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, expected_seq_len, expected_dim)					)





	def       UpperCamelCase_    (					self   :							List[str]					,lowerCamelCase__   :							str					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  FocalNetBackbone(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Optional[Any]												=  model(lowerCamelCase__					)

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps					)					,len(config.out_features					)					)
							self.parent.assertListEqual(list(result.feature_maps[0].shape					)					,[self.batch_size, self.image_size, 8, 8]					)

							# 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
							_UpperCamelCase   :						Dict												=  None
							_UpperCamelCase   :						List[Any]												=  FocalNetBackbone(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Union[str, Any]												=  model(lowerCamelCase__					)

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps					)					,1					)
							self.parent.assertListEqual(list(result.feature_maps[0].shape					)					,[self.batch_size, self.image_size * 2, 4, 4]					)

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





	def       UpperCamelCase_    (					self   :							Optional[Any]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  FocalNetForMaskedImageModeling(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Optional[int]												=  model(lowerCamelCase__					)
							self.parent.assertEqual(
							    result.reconstruction.shape					,(self.batch_size, self.num_channels, self.image_size, self.image_size)					)

							# test greyscale images
							_UpperCamelCase   :						Any												=  1
							_UpperCamelCase   :						Union[str, Any]												=  FocalNetForMaskedImageModeling(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()

							_UpperCamelCase   :						Dict												=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
							_UpperCamelCase   :						List[str]												=  model(lowerCamelCase__					)
							self.parent.assertEqual(result.reconstruction.shape					,(self.batch_size, 1, self.image_size, self.image_size)					)





	def       UpperCamelCase_    (					self   :							str					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  self.type_sequence_label_size
							_UpperCamelCase   :						Any												=  FocalNetForImageClassification(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						List[Any]												=  model(lowerCamelCase__					,labels=lowerCamelCase__					)
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.type_sequence_label_size)					)

							# test greyscale images
							_UpperCamelCase   :						List[Any]												=  1
							_UpperCamelCase   :						Union[str, Any]												=  FocalNetForImageClassification(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()

							_UpperCamelCase   :						str												=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
							_UpperCamelCase   :						Tuple												=  model(lowerCamelCase__					)
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.type_sequence_label_size)					)





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







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  self.prepare_config_and_inputs()

							_UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase   :						Tuple												=  config_and_inputs
							_UpperCamelCase   :						int												=  {'pixel_values': pixel_values}
							return config, inputs_dict






@require_torch
class 				lowercase__      (				lowercase		,   lowercase		,   unittest.TestCase  ):
	lowercase__    							=     (
	    (
	        FocalNetModel,
	        FocalNetForImageClassification,
	        FocalNetForMaskedImageModeling,
	        FocalNetBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	lowercase__    							=     (
	    {"""feature-extraction""": FocalNetModel, """image-classification""": FocalNetForImageClassification}
	    if is_torch_available()
	    else {}
	)
	lowercase__    							=     False

	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False





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







							'''simple docstring'''
							_UpperCamelCase   :						str												=  FocalNetModelTester(self					)
							_UpperCamelCase   :						Optional[int]												=  ConfigTester(self					,config_class=lowerCamelCase__					,embed_dim=37					,has_text_modality=lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''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       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							return





	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__					)





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







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_backbone(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Union[str, Any]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__					)





	@unittest.skip(reason='FocalNet does not use inputs_embeds'					)
	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							pass





	@unittest.skip(reason='FocalNet does not use feedforward chunking'					)
	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						List[str]												=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes[:-1]:
													_UpperCamelCase   :						List[Any]												=  model_class(lowerCamelCase__					)
													self.assertIsInstance(model.get_input_embeddings()					,(nn.Module)					)
													_UpperCamelCase   :						str												=  model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCamelCase__					,nn.Linear					)					)





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						List[Any]												=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes[:-1]:
													_UpperCamelCase   :						List[Any]												=  model_class(lowerCamelCase__					)
													_UpperCamelCase   :						Dict												=  inspect.signature(model.forward					)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													_UpperCamelCase   :						Union[str, Any]												=  [*signature.parameters.keys()]

													_UpperCamelCase   :						Optional[Any]												=  ['pixel_values']
													self.assertListEqual(arg_names[:1]					,lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							List[Any]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  model_class(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()

							with torch.no_grad():
													_UpperCamelCase   :						Optional[int]												=  model(**self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)					)

							_UpperCamelCase   :						Optional[int]												=  outputs.hidden_states

							_UpperCamelCase   :						Union[str, Any]												=  getattr(
							    self.model_tester					,'expected_num_hidden_layers'					,len(self.model_tester.depths					) + 1					)
							self.assertEqual(len(lowerCamelCase__					)					,lowerCamelCase__					)

							# FocalNet has a different seq_length
							_UpperCamelCase   :						Optional[Any]												=  (
							    config.patch_size
							    if isinstance(config.patch_size					,collections.abc.Iterable					)
							    else (config.patch_size, config.patch_size)
							)

							_UpperCamelCase   :						List[str]												=  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

							self.assertListEqual(
							    list(hidden_states[0].shape[-2:]					)					,[num_patches, self.model_tester.embed_dim]					,)

							_UpperCamelCase   :						List[Any]												=  outputs.reshaped_hidden_states
							self.assertEqual(len(lowerCamelCase__					)					,lowerCamelCase__					)

							_UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase   :						str												=  reshaped_hidden_states[0].shape
							_UpperCamelCase   :						Optional[Any]												=  (
							    reshaped_hidden_states[0].view(lowerCamelCase__					,lowerCamelCase__					,height * width					).permute(0					,2					,1					)
							)
							self.assertListEqual(
							    list(reshaped_hidden_states.shape[-2:]					)					,[num_patches, self.model_tester.embed_dim]					,)





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						List[str]												=  self.model_tester.prepare_config_and_inputs_for_common()

							_UpperCamelCase   :						Union[str, Any]												=  (
							    self.model_tester.image_size
							    if isinstance(self.model_tester.image_size					,collections.abc.Iterable					)
							    else (self.model_tester.image_size, self.model_tester.image_size)
							)

							for model_class in self.all_model_classes[:-1]:
													_UpperCamelCase   :						Optional[int]												=  True
													self.check_hidden_states_output(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													_UpperCamelCase   :						List[str]												=  True

													self.check_hidden_states_output(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCamelCase   :						Tuple												=  3

							_UpperCamelCase   :						str												=  (
							    self.model_tester.image_size
							    if isinstance(self.model_tester.image_size					,collections.abc.Iterable					)
							    else (self.model_tester.image_size, self.model_tester.image_size)
							)
							_UpperCamelCase   :						Optional[Any]												=  (
							    config.patch_size
							    if isinstance(config.patch_size					,collections.abc.Iterable					)
							    else (config.patch_size, config.patch_size)
							)

							_UpperCamelCase   :						List[str]												=  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
							_UpperCamelCase   :						List[Any]												=  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

							for model_class in self.all_model_classes[:-1]:
													_UpperCamelCase   :						List[str]												=  True
													self.check_hidden_states_output(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					,(padded_height, padded_width)					)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													_UpperCamelCase   :						Tuple												=  True
													self.check_hidden_states_output(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					,(padded_height, padded_width)					)





	@slow
	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCamelCase   :						Optional[Any]												=  FocalNetModel.from_pretrained(lowerCamelCase__					)
													self.assertIsNotNone(lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						int												=  self.model_tester.prepare_config_and_inputs_for_common()

							_UpperCamelCase   :						Any												=  _config_zero_init(lowerCamelCase__					)
							for model_class in self.all_model_classes:
													_UpperCamelCase   :						Optional[int]												=  model_class(config=lowerCamelCase__					)
													for name, param in model.named_parameters():
																			if "embeddings" not in name and param.requires_grad:
																									self.assertIn(
																									    ((param.data.mean() * 1E9).round() / 1E9).item()					,[0.0, 1.0]					,msg=F'Parameter {name} of model {model_class} seems not properly initialized'					,)






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





	@cached_property
	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							# TODO update organization
							return AutoImageProcessor.from_pretrained('microsoft/focalnet-tiny'					) if is_vision_available() else None





	@slow
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  FocalNetForImageClassification.from_pretrained('microsoft/focalnet-tiny'					).to(lowerCamelCase__					)
							_UpperCamelCase   :						Tuple												=  self.default_image_processor

							_UpperCamelCase   :						Optional[Any]												=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
							_UpperCamelCase   :						Optional[Any]												=  image_processor(images=lowerCamelCase__					,return_tensors='pt'					).to(lowerCamelCase__					)

							# forward pass
							with torch.no_grad():
													_UpperCamelCase   :						List[Any]												=  model(**lowerCamelCase__					)

							# verify the logits
							_UpperCamelCase   :						Union[str, Any]												=  torch.Size((1, 1000)					)
							self.assertEqual(outputs.logits.shape					,lowerCamelCase__					)
							_UpperCamelCase   :						str												=  torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]					).to(lowerCamelCase__					)
							self.assertTrue(torch.allclose(outputs.logits[0, :3]					,lowerCamelCase__					,atol=1E-4					)					)
							self.assertTrue(outputs.logits.argmax(dim=-1					).item()					,281					)






@require_torch
class 				lowercase__      (				lowercase		,   unittest.TestCase  ):
	lowercase__    							=     (FocalNetBackbone,) if is_torch_available() else ()
	lowercase__    							=     FocalNetConfig

	lowercase__    							=     False





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  FocalNetModelTester(self					)







'''simple docstring'''
import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


snake_case_					:      Tuple    			=    False






@skip_mps
class 				lowercase__      (				lowercase		,   lowercase		,   lowercase		,   unittest.TestCase  ):
	lowercase__    							=     StableDiffusionAttendAndExcitePipeline
	lowercase__    							=     False
	lowercase__    							=     TEXT_TO_IMAGE_PARAMS
	lowercase__    							=     TEXT_TO_IMAGE_BATCH_PARAMS.union({"""token_indices"""}  )
	lowercase__    							=     TEXT_TO_IMAGE_IMAGE_PARAMS
	lowercase__    							=     TEXT_TO_IMAGE_IMAGE_PARAMS





	@classmethod
	def       UpperCamelCase_    (					cls   :							Any					):







							'''simple docstring'''
							super().setUpClass()
							torch.use_deterministic_algorithms(lowerCamelCase__					)





	@classmethod
	def       UpperCamelCase_    (					cls   :							List[Any]					):







							'''simple docstring'''
							super().tearDownClass()
							torch.use_deterministic_algorithms(lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							torch.manual_seed(0					)
							_UpperCamelCase   :						Union[str, Any]												=  UNetaDConditionModel(
							    block_out_channels=(32, 64)					,layers_per_block=1					,sample_size=32					,in_channels=4					,out_channels=4					,down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')					,up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')					,cross_attention_dim=32					,attention_head_dim=(2, 4)					,use_linear_projection=lowerCamelCase__					,)
							_UpperCamelCase   :						List[str]												=  DDIMScheduler(
							    beta_start=0.0_0_0_8_5					,beta_end=0.0_1_2					,beta_schedule='scaled_linear'					,clip_sample=lowerCamelCase__					,set_alpha_to_one=lowerCamelCase__					,)
							torch.manual_seed(0					)
							_UpperCamelCase   :						List[str]												=  AutoencoderKL(
							    block_out_channels=[32, 64]					,in_channels=3					,out_channels=3					,down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']					,up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']					,latent_channels=4					,sample_size=128					,)
							torch.manual_seed(0					)
							_UpperCamelCase   :						List[str]												=  CLIPTextConfig(
							    bos_token_id=0					,eos_token_id=2					,hidden_size=32					,intermediate_size=37					,layer_norm_eps=1E-05					,num_attention_heads=4					,num_hidden_layers=5					,pad_token_id=1					,vocab_size=1000					,hidden_act='gelu'					,projection_dim=512					,)
							_UpperCamelCase   :						List[str]												=  CLIPTextModel(lowerCamelCase__					)
							_UpperCamelCase   :						Any												=  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'					)

							_UpperCamelCase   :						str												=  {
							    'unet': unet,
							    'scheduler': scheduler,
							    'vae': vae,
							    'text_encoder': text_encoder,
							    'tokenizer': tokenizer,
							    'safety_checker': None,
							    'feature_extractor': None,
							}

							return components





	def       UpperCamelCase_    (					self   :							Tuple					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							str=0					):







							'''simple docstring'''
							if str(lowerCamelCase__					).startswith('mps'					):
													_UpperCamelCase   :						Union[str, Any]												=  torch.manual_seed(lowerCamelCase__					)
							else:
													_UpperCamelCase   :						int												=  torch.Generator(device=lowerCamelCase__					).manual_seed(lowerCamelCase__					)
							_UpperCamelCase   :						Union[str, Any]												=  {
							    'prompt': 'a cat and a frog',
							    'token_indices': [2, 5],
							    'generator': generator,
							    'num_inference_steps': 1,
							    'guidance_scale': 6.0,
							    'output_type': 'numpy',
							    'max_iter_to_alter': 2,
							    'thresholds': {0: 0.7},
							}
							return inputs





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  'cpu'

							_UpperCamelCase   :						List[Any]												=  self.get_dummy_components()
							_UpperCamelCase   :						Union[str, Any]												=  self.pipeline_class(**lowerCamelCase__					)
							pipe.to(lowerCamelCase__					)
							pipe.set_progress_bar_config(disable=lowerCamelCase__					)

							_UpperCamelCase   :						Optional[Any]												=  self.get_dummy_inputs(lowerCamelCase__					)
							_UpperCamelCase   :						Optional[int]												=  pipe(**lowerCamelCase__					).images
							_UpperCamelCase   :						Union[str, Any]												=  image[0, -3:, -3:, -1]

							self.assertEqual(image.shape					,(1, 64, 64, 3)					)
							_UpperCamelCase   :						Union[str, Any]												=  np.array(
							    [0.6_3_9_0_5_3_6_4, 0.6_2_8_9_7_3_0_7, 0.4_8_5_9_9_0_1_7, 0.5_1_3_3_6_2_4, 0.5_5_5_0_0_4_8, 0.4_5_7_6_9_5_1_6, 0.5_0_3_2_6_9_7_3, 0.5_0_2_3_1_3_9, 0.4_5_3_8_4_4_9_6]					)
							_UpperCamelCase   :						Tuple												=  np.abs(image_slice.flatten() - expected_slice					).max()
							self.assertLessEqual(lowerCamelCase__					,1E-3					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							super().test_cpu_offload_forward_pass(expected_max_diff=5E-4					)





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							# NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
							self._test_inference_batch_consistent(batch_sizes=[1, 2]					)





	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							self._test_inference_batch_single_identical(batch_size=2					,expected_max_diff=7E-4					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3					)





	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5E-4					)





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							super().test_save_load_local(expected_max_difference=5E-4					)





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







							'''simple docstring'''
							super().test_save_load_optional_components(expected_max_difference=4E-4					)






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





	@classmethod
	def       UpperCamelCase_    (					cls   :							Dict					):







							'''simple docstring'''
							super().setUpClass()
							torch.use_deterministic_algorithms(lowerCamelCase__					)





	@classmethod
	def       UpperCamelCase_    (					cls   :							Optional[Any]					):







							'''simple docstring'''
							super().tearDownClass()
							torch.use_deterministic_algorithms(lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  torch.manual_seed(51					)

							_UpperCamelCase   :						Optional[Any]												=  StableDiffusionAttendAndExcitePipeline.from_pretrained(
							    'CompVis/stable-diffusion-v1-4'					,safety_checker=lowerCamelCase__					,torch_dtype=torch.floataa					)
							pipe.to('cuda'					)

							_UpperCamelCase   :						Any												=  'a painting of an elephant with glasses'
							_UpperCamelCase   :						str												=  [5, 7]

							_UpperCamelCase   :						int												=  pipe(
							    prompt=lowerCamelCase__					,token_indices=lowerCamelCase__					,guidance_scale=7.5					,generator=lowerCamelCase__					,num_inference_steps=5					,max_iter_to_alter=5					,output_type='numpy'					,).images[0]

							_UpperCamelCase   :						Tuple												=  load_numpy(
							    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy'					)
							assert np.abs((expected_image - image).max()					) < 5E-1


