"""simple docstring"""






import inspect
import unittest
import warnings
from typing import List

from transformers import EfficientFormerConfig
from transformers.models.auto import get_values
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 (
						    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
						    MODEL_MAPPING,
						    EfficientFormerForImageClassification,
						    EfficientFormerForImageClassificationWithTeacher,
						    EfficientFormerModel,
						)
						from transformers.models.efficientformer.modeling_efficientformer import (
						    EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
						)


if is_vision_available():
						from PIL import Image

						from transformers import EfficientFormerImageProcessor
class 							snake_case			:




					"""simple docstring"""

					def __init__(							self  :      int       ,lowerCamelCase__  :      Tuple       ,lowerCamelCase__  :      int = 13       ,lowerCamelCase__  :      int = 64       ,lowerCamelCase__  :      int = 2       ,lowerCamelCase__  :      int = 3       ,lowerCamelCase__  :      int = 3       ,lowerCamelCase__  :      bool = True       ,lowerCamelCase__  :      bool = True       ,lowerCamelCase__  :      int = 128       ,lowerCamelCase__  :      List[Any]=[16, 32, 64, 128]       ,lowerCamelCase__  :      int = 7       ,lowerCamelCase__  :      int = 4       ,lowerCamelCase__  :      int = 37       ,lowerCamelCase__  :      str = "gelu"       ,lowerCamelCase__  :      float = 0.1       ,lowerCamelCase__  :      float = 0.1       ,lowerCamelCase__  :      int = 10       ,lowerCamelCase__  :      float = 0.0_2       ,lowerCamelCase__  :      int = 2       ,lowerCamelCase__  :      int = 1       ,lowerCamelCase__  :      int = 128       ,lowerCamelCase__  :      List[int] = [2, 2, 2, 2]       ,lowerCamelCase__  :      int = 2       ,lowerCamelCase__  :      int = 2       ,):
									UpperCAmelCase__			 =				parent
									UpperCAmelCase__			 =				batch_size
									UpperCAmelCase__			 =				image_size
									UpperCAmelCase__			 =				patch_size
									UpperCAmelCase__			 =				num_channels
									UpperCAmelCase__			 =				is_training
									UpperCAmelCase__			 =				use_labels
									UpperCAmelCase__			 =				hidden_size
									UpperCAmelCase__			 =				num_hidden_layers
									UpperCAmelCase__			 =				num_attention_heads
									UpperCAmelCase__			 =				intermediate_size
									UpperCAmelCase__			 =				hidden_act
									UpperCAmelCase__			 =				hidden_dropout_prob
									UpperCAmelCase__			 =				attention_probs_dropout_prob
									UpperCAmelCase__			 =				type_sequence_label_size
									UpperCAmelCase__			 =				initializer_range
									UpperCAmelCase__			 =				encoder_stride
									UpperCAmelCase__			 =				num_attention_outputs
									UpperCAmelCase__			 =				embed_dim
									UpperCAmelCase__			 =				embed_dim + 1
									UpperCAmelCase__			 =				resolution
									UpperCAmelCase__			 =				depths
									UpperCAmelCase__			 =				hidden_sizes
									UpperCAmelCase__			 =				dim
									UpperCAmelCase__			 =				mlp_expansion_ratio







					def 						__lowerCAmelCase		(							self  :      List[Any]     ):
									UpperCAmelCase__			 =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

									UpperCAmelCase__			 =				None
									if self.use_labels:
													UpperCAmelCase__			 =				ids_tensor([self.batch_size]       ,self.type_sequence_label_size     )

									UpperCAmelCase__			 =				self.get_config()
									return config, pixel_values, labels







					def 						__lowerCAmelCase		(							self  :      int     ):
									return EfficientFormerConfig(
									    image_size=self.image_size       ,patch_size=self.patch_size       ,num_channels=self.num_channels       ,hidden_size=self.hidden_size       ,num_hidden_layers=self.num_hidden_layers       ,num_attention_heads=self.num_attention_heads       ,intermediate_size=self.intermediate_size       ,hidden_act=self.hidden_act       ,hidden_dropout_prob=self.hidden_dropout_prob       ,attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,is_decoder=lowerCamelCase__       ,initializer_range=self.initializer_range       ,encoder_stride=self.encoder_stride       ,resolution=self.resolution       ,depths=self.depths       ,hidden_sizes=self.hidden_sizes       ,dim=self.dim       ,mlp_expansion_ratio=self.mlp_expansion_ratio       ,)







					def 						__lowerCAmelCase		(							self  :      Union[str, Any]       ,lowerCamelCase__  :      Optional[Any]       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      Any     ):
									UpperCAmelCase__			 =				EfficientFormerModel(config=lowerCamelCase__     )
									model.to(lowerCamelCase__     )
									model.eval()
									UpperCAmelCase__			 =				model(lowerCamelCase__     )
									self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.hidden_size)     )







					def 						__lowerCAmelCase		(							self  :      Optional[Any]       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      int       ,lowerCamelCase__  :      Optional[int]     ):
									UpperCAmelCase__			 =				self.type_sequence_label_size
									UpperCAmelCase__			 =				EfficientFormerForImageClassification(lowerCamelCase__     )
									model.to(lowerCamelCase__     )
									model.eval()
									UpperCAmelCase__			 =				model(lowerCamelCase__       ,labels=lowerCamelCase__     )
									self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.type_sequence_label_size)     )

									# test greyscale images
									UpperCAmelCase__			 =				1
									UpperCAmelCase__			 =				EfficientFormerForImageClassification(lowerCamelCase__     )
									model.to(lowerCamelCase__     )
									model.eval()

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







					def 						__lowerCAmelCase		(							self  :      int     ):
									UpperCAmelCase__			 =				self.prepare_config_and_inputs()
									(
									    (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      
									)			 =				config_and_inputs
									UpperCAmelCase__			 =				{'pixel_values': pixel_values}
									return config, inputs_dict






@require_torch
class 							snake_case			(					__UpperCAmelCase ,       __UpperCAmelCase ,       unittest.TestCase							):




					"""simple docstring"""

					snake_case__							       =					(
					    (
					        EfficientFormerModel,
					        EfficientFormerForImageClassificationWithTeacher,
					        EfficientFormerForImageClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					snake_case__							       =					(
					    {
					        "feature-extraction": EfficientFormerModel,
					        "image-classification": (
					            EfficientFormerForImageClassification,
					            EfficientFormerForImageClassificationWithTeacher,
					        ),
					    }
					    if is_torch_available()
					    else {}
					)
					snake_case__							       =					False

					snake_case__							       =					False
					snake_case__							       =					False
					snake_case__							       =					False







					def 						__lowerCAmelCase		(							self  :      int     ):
									UpperCAmelCase__			 =				EfficientFormerModelTester(self     )
									UpperCAmelCase__			 =				ConfigTester(
									    self       ,config_class=lowerCamelCase__       ,has_text_modality=lowerCamelCase__       ,hidden_size=37     )







					def 						__lowerCAmelCase		(							self  :      Tuple     ):
									self.config_tester.run_common_tests()







					@unittest.skip(reason='EfficientFormer does not use inputs_embeds'     )
					def 						__lowerCAmelCase		(							self  :      Any     ):
									pass







					@unittest.skip(reason='EfficientFormer does not support input and output embeddings'     )
					def 						__lowerCAmelCase		(							self  :      Union[str, Any]     ):
									pass







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

									for model_class in self.all_model_classes:
													UpperCAmelCase__			 =				model_class(lowerCamelCase__     )
													UpperCAmelCase__			 =				inspect.signature(model.forward     )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													UpperCAmelCase__			 =				[*signature.parameters.keys()]

													UpperCAmelCase__			 =				['pixel_values']
													self.assertListEqual(arg_names[:1]       ,lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      Union[str, Any]     ):
									def check_hidden_states_output(lowerCamelCase__  :      str       ,lowerCamelCase__  :      Optional[int]       ,lowerCamelCase__  :      Optional[Any]     ):
													UpperCAmelCase__			 =				model_class(lowerCamelCase__     )
													model.to(lowerCamelCase__     )
													model.eval()

													with torch.no_grad():
																	UpperCAmelCase__			 =				model(**self._prepare_for_class(lowerCamelCase__       ,lowerCamelCase__     )     )

													UpperCAmelCase__			 =				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													UpperCAmelCase__			 =				getattr(
													    self.model_tester       ,'expected_num_hidden_layers'       ,self.model_tester.num_hidden_layers + 1     )
													self.assertEqual(len(lowerCamelCase__     )       ,lowerCamelCase__     )

													if hasattr(self.model_tester       ,'encoder_seq_length'     ):
																	UpperCAmelCase__			 =				self.model_tester.encoder_seq_length
																	if hasattr(self.model_tester       ,'chunk_length'     ) and self.model_tester.chunk_length > 1:
																					UpperCAmelCase__			 =				seq_length * self.model_tester.chunk_length
													else:
																	UpperCAmelCase__			 =				self.model_tester.seq_length

													self.assertListEqual(
													    list(hidden_states[-1].shape[-2:]     )       ,[seq_length, self.model_tester.hidden_size]       ,)

													if config.is_encoder_decoder:
																	UpperCAmelCase__			 =				outputs.decoder_hidden_states

																	self.assertIsInstance(lowerCamelCase__       ,(list, tuple)     )
																	self.assertEqual(len(lowerCamelCase__     )       ,lowerCamelCase__     )
																	UpperCAmelCase__			 =				getattr(self.model_tester       ,'seq_length'       ,lowerCamelCase__     )
																	UpperCAmelCase__			 =				getattr(self.model_tester       ,'decoder_seq_length'       ,lowerCamelCase__     )

																	self.assertListEqual(
																	    list(hidden_states[-1].shape[-2:]     )       ,[decoder_seq_length, self.model_tester.hidden_size]       ,)

									UpperCAmelCase__ ,      UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													UpperCAmelCase__			 =				True
													check_hidden_states_output(lowerCamelCase__       ,lowerCamelCase__       ,lowerCamelCase__     )

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													UpperCAmelCase__			 =				True

													check_hidden_states_output(lowerCamelCase__       ,lowerCamelCase__       ,lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      int       ,lowerCamelCase__  :      Union[str, Any]       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      Optional[Any]=False     ):
									UpperCAmelCase__			 =				super()._prepare_for_class(lowerCamelCase__       ,lowerCamelCase__       ,return_labels=lowerCamelCase__     )

									if return_labels:
													if model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher":
																	del inputs_dict["labels"]

									return inputs_dict







					def 						__lowerCAmelCase		(							self  :      Any     ):
									UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__     )







					@unittest.skip(reason='EfficientFormer does not implement masked image modeling yet'     )
					def 						__lowerCAmelCase		(							self  :      str     ):
									UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      Dict     ):
									UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      int     ):
									if not self.model_tester.is_training:
													return

									UpperCAmelCase__ ,      UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs_for_common()
									UpperCAmelCase__			 =				True

									for model_class in self.all_model_classes:
													# EfficientFormerForImageClassificationWithTeacher supports inference-only
													if (
													    model_class in get_values(lowerCamelCase__     )
													    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
													):
																	continue
													UpperCAmelCase__			 =				model_class(lowerCamelCase__     )
													model.to(lowerCamelCase__     )
													model.train()
													UpperCAmelCase__			 =				self._prepare_for_class(lowerCamelCase__       ,lowerCamelCase__       ,return_labels=lowerCamelCase__     )
													UpperCAmelCase__			 =				model(**lowerCamelCase__     ).loss
													loss.backward()







					def 						__lowerCAmelCase		(							self  :      List[Any]     ):
									UpperCAmelCase__ ,      UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs_for_common()

									UpperCAmelCase__			 =				[
									    {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
									    {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
									    {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
									]

									for model_class in self.all_model_classes:
													if (
													    model_class
													    not in [
													        *get_values(lowerCamelCase__     ),
													    ]
													    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
													):
																	continue

													for problem_type in problem_types:
																	with self.subTest(msg=f'''Testing {model_class} with {problem_type["title"]}'''     ):
																					UpperCAmelCase__			 =				problem_type['title']
																					UpperCAmelCase__			 =				problem_type['num_labels']

																					UpperCAmelCase__			 =				model_class(lowerCamelCase__     )
																					model.to(lowerCamelCase__     )
																					model.train()

																					UpperCAmelCase__			 =				self._prepare_for_class(lowerCamelCase__       ,lowerCamelCase__       ,return_labels=lowerCamelCase__     )

																					if problem_type["num_labels"] > 1:
																									UpperCAmelCase__			 =				inputs['labels'].unsqueeze(1     ).repeat(1       ,problem_type['num_labels']     )

																					UpperCAmelCase__			 =				inputs['labels'].to(problem_type['dtype']     )

																					# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																					# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																					# they have the same size." which is a symptom something in wrong for the regression problem.
																					# See https://github.com/huggingface/transformers/issues/11780
																					with warnings.catch_warnings(record=lowerCamelCase__     ) as warning_list:
																									UpperCAmelCase__			 =				model(**lowerCamelCase__     ).loss
																					for w in warning_list:
																									if "Using a target size that is different to the input size" in str(w.message     ):
																													raise ValueError(
																													    f'''Something is going wrong in the regression problem: intercepted {w.message}'''     )

																					loss.backward()







					@slow
					def 						__lowerCAmelCase		(							self  :      Optional[Any]     ):
									for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCAmelCase__			 =				EfficientFormerModel.from_pretrained(lowerCamelCase__     )
													self.assertIsNotNone(lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      Optional[Any]     ):
									UpperCAmelCase__ ,      UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs_for_common()
									UpperCAmelCase__			 =				True

									UpperCAmelCase__			 =				getattr(self.model_tester       ,'seq_length'       ,lowerCamelCase__     )
									UpperCAmelCase__			 =				getattr(self.model_tester       ,'encoder_seq_length'       ,lowerCamelCase__     )
									UpperCAmelCase__			 =				getattr(self.model_tester       ,'key_length'       ,lowerCamelCase__     )
									UpperCAmelCase__			 =				getattr(self.model_tester       ,'chunk_length'       ,lowerCamelCase__     )

									if chunk_length is not None and hasattr(self.model_tester       ,'num_hashes'     ):
													UpperCAmelCase__			 =				encoder_seq_length * self.model_tester.num_hashes

									for model_class in self.all_model_classes:
													UpperCAmelCase__			 =				True
													UpperCAmelCase__			 =				False
													UpperCAmelCase__			 =				True
													UpperCAmelCase__			 =				model_class(lowerCamelCase__     )
													model.to(lowerCamelCase__     )
													model.eval()
													with torch.no_grad():
																	UpperCAmelCase__			 =				model(**self._prepare_for_class(lowerCamelCase__       ,lowerCamelCase__     )     )
													UpperCAmelCase__			 =				outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
													self.assertEqual(len(lowerCamelCase__     )       ,self.model_tester.num_attention_outputs     )

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													UpperCAmelCase__			 =				True
													UpperCAmelCase__			 =				model_class(lowerCamelCase__     )
													model.to(lowerCamelCase__     )
													model.eval()
													with torch.no_grad():
																	UpperCAmelCase__			 =				model(**self._prepare_for_class(lowerCamelCase__       ,lowerCamelCase__     )     )
													UpperCAmelCase__			 =				outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
													self.assertEqual(len(lowerCamelCase__     )       ,self.model_tester.num_attention_outputs     )

													if chunk_length is not None:
																	self.assertListEqual(
																	    list(attentions[0].shape[-4:]     )       ,[self.model_tester.num_attention_heads, encoder_seq_length, chunk_length, encoder_key_length]       ,)
													else:
																	self.assertListEqual(
																	    list(attentions[0].shape[-3:]     )       ,[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]       ,)






def     a_		(   ):
				UpperCAmelCase__			 =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
				return image







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




					"""simple docstring"""

					@cached_property
					def 						__lowerCAmelCase		(							self  :      List[str]     ):
									return (
									    EfficientFormerImageProcessor.from_pretrained('snap-research/efficientformer-l1-300'     )
									    if is_vision_available()
									    else None
									)







					@slow
					def 						__lowerCAmelCase		(							self  :      Dict     ):
									UpperCAmelCase__			 =				EfficientFormerForImageClassification.from_pretrained('snap-research/efficientformer-l1-300'     ).to(
									    lowerCamelCase__     )

									UpperCAmelCase__			 =				self.default_image_processor
									UpperCAmelCase__			 =				prepare_img()
									UpperCAmelCase__			 =				image_processor(images=lowerCamelCase__       ,return_tensors='pt'     ).to(lowerCamelCase__     )

									# forward pass
									with torch.no_grad():
													UpperCAmelCase__			 =				model(**lowerCamelCase__     )

									# verify the logits
									UpperCAmelCase__			 =				(1, 1_000)
									self.assertEqual(outputs.logits.shape       ,lowerCamelCase__     )

									UpperCAmelCase__			 =				torch.tensor([-0.0_5_5_5, 0.4_8_2_5, -0.0_8_5_2]     ).to(lowerCamelCase__     )
									self.assertTrue(torch.allclose(outputs.logits[0][:3]       ,lowerCamelCase__       ,atol=1e-4     )     )







					@slow
					def 						__lowerCAmelCase		(							self  :      Tuple     ):
									UpperCAmelCase__			 =				EfficientFormerForImageClassificationWithTeacher.from_pretrained(
									    'snap-research/efficientformer-l1-300'     ).to(lowerCamelCase__     )

									UpperCAmelCase__			 =				self.default_image_processor
									UpperCAmelCase__			 =				prepare_img()
									UpperCAmelCase__			 =				image_processor(images=lowerCamelCase__       ,return_tensors='pt'     ).to(lowerCamelCase__     )

									# forward pass
									with torch.no_grad():
													UpperCAmelCase__			 =				model(**lowerCamelCase__     )

									# verify the logits
									UpperCAmelCase__			 =				(1, 1_000)
									self.assertEqual(outputs.logits.shape       ,lowerCamelCase__     )

									UpperCAmelCase__			 =				torch.tensor([-0.1_3_1_2, 0.4_3_5_3, -1.0_4_9_9]     ).to(lowerCamelCase__     )
									self.assertTrue(torch.allclose(outputs.logits[0][:3]       ,lowerCamelCase__       ,atol=1e-4     )     )




"""simple docstring"""






import random
import unittest

import numpy as np
import torch
from PIL import Image
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorEmbaEmbPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class 							snake_case			(					__UpperCAmelCase ,       unittest.TestCase							):




					"""simple docstring"""

					snake_case__							       =					KandinskyVaaPriorEmbaEmbPipeline
					snake_case__							       =					["prompt", "image"]
					snake_case__							       =					["prompt", "image"]
					snake_case__							       =					[
					    "num_images_per_prompt",
					    "strength",
					    "generator",
					    "num_inference_steps",
					    "latents",
					    "negative_prompt",
					    "guidance_scale",
					    "output_type",
					    "return_dict",
					]
					snake_case__							       =					False







					@property
					def 						__lowerCAmelCase		(							self  :      Tuple     ):
									return 32







					@property
					def 						__lowerCAmelCase		(							self  :      Union[str, Any]     ):
									return 32







					@property
					def 						__lowerCAmelCase		(							self  :      str     ):
									return self.time_input_dim







					@property
					def 						__lowerCAmelCase		(							self  :      Union[str, Any]     ):
									return self.time_input_dim * 4







					@property
					def 						__lowerCAmelCase		(							self  :      Optional[Any]     ):
									return 100







					@property
					def 						__lowerCAmelCase		(							self  :      int     ):
									UpperCAmelCase__			 =				CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )
									return tokenizer







					@property
					def 						__lowerCAmelCase		(							self  :      str     ):
									torch.manual_seed(0     )
									UpperCAmelCase__			 =				CLIPTextConfig(
									    bos_token_id=0       ,eos_token_id=2       ,hidden_size=self.text_embedder_hidden_size       ,projection_dim=self.text_embedder_hidden_size       ,intermediate_size=37       ,layer_norm_eps=1e-05       ,num_attention_heads=4       ,num_hidden_layers=5       ,pad_token_id=1       ,vocab_size=1_000       ,)
									return CLIPTextModelWithProjection(lowerCamelCase__     )







					@property
					def 						__lowerCAmelCase		(							self  :      str     ):
									torch.manual_seed(0     )

									UpperCAmelCase__			 =				{
									    'num_attention_heads': 2,
									    'attention_head_dim': 12,
									    'embedding_dim': self.text_embedder_hidden_size,
									    'num_layers': 1,
									}

									UpperCAmelCase__			 =				PriorTransformer(**lowerCamelCase__     )
									# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
									UpperCAmelCase__			 =				nn.Parameter(torch.ones(model.clip_std.shape     )     )
									return model







					@property
					def 						__lowerCAmelCase		(							self  :      Optional[int]     ):
									torch.manual_seed(0     )
									UpperCAmelCase__			 =				CLIPVisionConfig(
									    hidden_size=self.text_embedder_hidden_size       ,image_size=224       ,projection_dim=self.text_embedder_hidden_size       ,intermediate_size=37       ,num_attention_heads=4       ,num_channels=3       ,num_hidden_layers=5       ,patch_size=14       ,)

									UpperCAmelCase__			 =				CLIPVisionModelWithProjection(lowerCamelCase__     )
									return model







					@property
					def 						__lowerCAmelCase		(							self  :      List[str]     ):
									UpperCAmelCase__			 =				CLIPImageProcessor(
									    crop_size=224       ,do_center_crop=lowerCamelCase__       ,do_normalize=lowerCamelCase__       ,do_resize=lowerCamelCase__       ,image_mean=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]       ,image_std=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]       ,resample=3       ,size=224       ,)

									return image_processor







					def 						__lowerCAmelCase		(							self  :      List[Any]     ):
									UpperCAmelCase__			 =				self.dummy_prior
									UpperCAmelCase__			 =				self.dummy_image_encoder
									UpperCAmelCase__			 =				self.dummy_text_encoder
									UpperCAmelCase__			 =				self.dummy_tokenizer
									UpperCAmelCase__			 =				self.dummy_image_processor

									UpperCAmelCase__			 =				UnCLIPScheduler(
									    variance_type='fixed_small_log'       ,prediction_type='sample'       ,num_train_timesteps=1_000       ,clip_sample=lowerCamelCase__       ,clip_sample_range=1_0.0       ,)

									UpperCAmelCase__			 =				{
									    'prior': prior,
									    'image_encoder': image_encoder,
									    'text_encoder': text_encoder,
									    'tokenizer': tokenizer,
									    'scheduler': scheduler,
									    'image_processor': image_processor,
									}

									return components







					def 						__lowerCAmelCase		(							self  :      Optional[int]       ,lowerCamelCase__  :      Any       ,lowerCamelCase__  :      str=0     ):
									if str(lowerCamelCase__     ).startswith('mps'     ):
													UpperCAmelCase__			 =				torch.manual_seed(lowerCamelCase__     )
									else:
													UpperCAmelCase__			 =				torch.Generator(device=lowerCamelCase__     ).manual_seed(lowerCamelCase__     )

									UpperCAmelCase__			 =				floats_tensor((1, 3, 64, 64)       ,rng=random.Random(lowerCamelCase__     )     ).to(lowerCamelCase__     )
									UpperCAmelCase__			 =				image.cpu().permute(0       ,2       ,3       ,1     )[0]
									UpperCAmelCase__			 =				Image.fromarray(np.uinta(lowerCamelCase__     )     ).convert('RGB'     ).resize((256, 256)     )

									UpperCAmelCase__			 =				{
									    'prompt': 'horse',
									    'image': init_image,
									    'strength': 0.5,
									    'generator': generator,
									    'guidance_scale': 4.0,
									    'num_inference_steps': 2,
									    'output_type': 'np',
									}
									return inputs







					def 						__lowerCAmelCase		(							self  :      List[Any]     ):
									UpperCAmelCase__			 =				'cpu'

									UpperCAmelCase__			 =				self.get_dummy_components()

									UpperCAmelCase__			 =				self.pipeline_class(**lowerCamelCase__     )
									UpperCAmelCase__			 =				pipe.to(lowerCamelCase__     )

									pipe.set_progress_bar_config(disable=lowerCamelCase__     )

									UpperCAmelCase__			 =				pipe(**self.get_dummy_inputs(lowerCamelCase__     )     )
									UpperCAmelCase__			 =				output.image_embeds

									UpperCAmelCase__			 =				pipe(
									    **self.get_dummy_inputs(lowerCamelCase__     )       ,return_dict=lowerCamelCase__       ,)[0]

									UpperCAmelCase__			 =				image[0, -10:]
									UpperCAmelCase__			 =				image_from_tuple[0, -10:]

									assert image.shape == (1, 32)

									UpperCAmelCase__			 =				np.array(
									    [
									        0.1_0_7_1_2_8_4,
									        1.3_3_3_0_2_7_1,
									        0.6_1_2_6_0_2_2_3,
									        -0.6_6_9_1_0_6_5,
									        -0.3_8_4_6_8_5_2,
									        -1.0_3_0_3_6_6_1,
									        0.2_2_7_1_6_1_1_1,
									        0.0_3_3_4_8_9_0_1,
									        0.3_0_0_4_0_6_7_5,
									        -0.2_4_8_0_5_0_2_9,
									    ]     )

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







					@skip_mps
					def 						__lowerCAmelCase		(							self  :      Optional[int]     ):
									UpperCAmelCase__			 =				torch_device == 'cpu'
									UpperCAmelCase__			 =				True
									UpperCAmelCase__			 =				False

									self._test_inference_batch_single_identical(
									    test_max_difference=lowerCamelCase__       ,relax_max_difference=lowerCamelCase__       ,test_mean_pixel_difference=lowerCamelCase__       ,)







					@skip_mps
					def 						__lowerCAmelCase		(							self  :      int     ):
									UpperCAmelCase__			 =				torch_device == 'cpu'
									UpperCAmelCase__			 =				False

									self._test_attention_slicing_forward_pass(
									    test_max_difference=lowerCamelCase__       ,test_mean_pixel_difference=lowerCamelCase__       ,)



