



import inspect
import unittest

from transformers import ConvNextConfig
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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
							from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor



class 	lowerCamelCase__      :




		'''simple docstring'''



		def __init__(self		,__lowerCamelCase		,__lowerCamelCase=13		,__lowerCamelCase=32		,__lowerCamelCase=3		,__lowerCamelCase=4		,__lowerCamelCase=[10, 20, 30, 40]		,__lowerCamelCase=[2, 2, 3, 2]		,__lowerCamelCase=True		,__lowerCamelCase=True		,__lowerCamelCase=37		,__lowerCamelCase="gelu"		,__lowerCamelCase=10		,__lowerCamelCase=0.02		,__lowerCamelCase=["stage2", "stage3", "stage4"]		,__lowerCamelCase=[2, 3, 4]		,__lowerCamelCase=None		,)					->      int:

						"""simple docstring"""






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



		def 		lowerCAmelCase__				(self			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

						lowerCAmelCase__					:    int												=					self.get_config()
						return config, pixel_values, labels



		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						return ConvNextConfig(
						    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=SCREAMING_SNAKE_CASE_		,initializer_range=self.initializer_range		,out_features=self.out_features		,out_indices=self.out_indices		,num_labels=self.num_labels		,)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      List[str]:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					ConvNextModel(config=SCREAMING_SNAKE_CASE_			)
						model.to(SCREAMING_SNAKE_CASE_			)
						model.eval()
						lowerCAmelCase__					:    List[str]												=					model(SCREAMING_SNAKE_CASE_			)
						# 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 // 32, self.image_size // 32)		,)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      Tuple:

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					ConvNextForImageClassification(SCREAMING_SNAKE_CASE_			)
						model.to(SCREAMING_SNAKE_CASE_			)
						model.eval()
						lowerCAmelCase__					:    Dict												=					model(SCREAMING_SNAKE_CASE_		,labels=SCREAMING_SNAKE_CASE_			)
						self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_labels)			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      int:

						"""simple docstring"""






						lowerCAmelCase__					:    Union[str, Any]												=					ConvNextBackbone(config=SCREAMING_SNAKE_CASE_			)
						model.to(SCREAMING_SNAKE_CASE_			)
						model.eval()
						lowerCAmelCase__					:    Union[str, Any]												=					model(SCREAMING_SNAKE_CASE_			)

						# 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__					:    Any												=					None
						lowerCAmelCase__					:    str												=					ConvNextBackbone(config=SCREAMING_SNAKE_CASE_			)
						model.to(SCREAMING_SNAKE_CASE_			)
						model.eval()
						lowerCAmelCase__					:    Dict												=					model(SCREAMING_SNAKE_CASE_			)

						# 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 		lowerCAmelCase__				(self			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    int												=					self.prepare_config_and_inputs()
						lowerCAmelCase__					:    Dict												=					config_and_inputs
						lowerCAmelCase__					:    int												=					{"""pixel_values""": pixel_values}
						return config, inputs_dict



@require_torch
class 	lowerCamelCase__      (							_lowerCAmelCase				,    _lowerCAmelCase				,    unittest.TestCase):




		'''simple docstring'''



		snake_case_     =(
		    (
		        ConvNextModel,
		        ConvNextForImageClassification,
		        ConvNextBackbone,
		    )
		    if is_torch_available()
		    else ()
		)
		snake_case_     =(
		    {"""feature-extraction""": ConvNextModel, """image-classification""": ConvNextForImageClassification}
		    if is_torch_available()
		    else {}
		)

		snake_case_     =True
		snake_case_     =False
		snake_case_     =False
		snake_case_     =False
		snake_case_     =False



		def 		lowerCAmelCase__				(self			)					->      int:

						"""simple docstring"""






						lowerCAmelCase__					:    Any												=					ConvNextModelTester(self			)
						lowerCAmelCase__					:    Union[str, Any]												=					ConfigTester(self		,config_class=SCREAMING_SNAKE_CASE_		,has_text_modality=SCREAMING_SNAKE_CASE_		,hidden_size=37			)



		def 		lowerCAmelCase__				(self			)					->      int:

						"""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 		lowerCAmelCase__				(self			)					->      Optional[int]:

						"""simple docstring"""






						return



		@unittest.skip(reason='''ConvNext does not use inputs_embeds'''			)
		def 		lowerCAmelCase__				(self			)					->      Optional[int]:

						"""simple docstring"""






						pass



		@unittest.skip(reason='''ConvNext does not support input and output embeddings'''			)
		def 		lowerCAmelCase__				(self			)					->      Any:

						"""simple docstring"""






						pass



		@unittest.skip(reason='''ConvNext does not use feedforward chunking'''			)
		def 		lowerCAmelCase__				(self			)					->      Dict:

						"""simple docstring"""






						pass



		def 		lowerCAmelCase__				(self			)					->      Tuple:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					self.model_tester.prepare_config_and_inputs_for_common()

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

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



		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_			)



		def 		lowerCAmelCase__				(self			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_backbone(*SCREAMING_SNAKE_CASE_			)



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

						"""simple docstring"""






						def check_hidden_states_output(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			):
										lowerCAmelCase__					:    Optional[int]												=					model_class(SCREAMING_SNAKE_CASE_			)
										model.to(SCREAMING_SNAKE_CASE_			)
										model.eval()

										with torch.no_grad():
														lowerCAmelCase__					:    Dict												=					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_		,SCREAMING_SNAKE_CASE_			)			)

										lowerCAmelCase__					:    Union[str, Any]												=					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										lowerCAmelCase__					:    Dict												=					self.model_tester.num_stages
										self.assertEqual(len(SCREAMING_SNAKE_CASE_			)		,expected_num_stages + 1			)

										# ConvNext'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__					:    Dict												=					self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										lowerCAmelCase__					:    str												=					True
										check_hidden_states_output(SCREAMING_SNAKE_CASE_		,SCREAMING_SNAKE_CASE_		,SCREAMING_SNAKE_CASE_			)

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

										check_hidden_states_output(SCREAMING_SNAKE_CASE_		,SCREAMING_SNAKE_CASE_		,SCREAMING_SNAKE_CASE_			)



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

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_			)



		@slow
		def 		lowerCAmelCase__				(self			)					->      Tuple:

						"""simple docstring"""






						for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__					:    int												=					ConvNextModel.from_pretrained(SCREAMING_SNAKE_CASE_			)
										self.assertIsNotNone(SCREAMING_SNAKE_CASE_			)






def      lowerCAmelCase__		(   ):





				'''simple docstring'''

				lowerCAmelCase__					:    Any												=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
				return image



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




		'''simple docstring'''



		@cached_property
		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						return AutoImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''			) if is_vision_available() else None



		@slow
		def 		lowerCAmelCase__				(self			)					->      Tuple:

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					ConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''			).to(SCREAMING_SNAKE_CASE_			)

						lowerCAmelCase__					:    List[str]												=					self.default_image_processor
						lowerCAmelCase__					:    Any												=					prepare_img()
						lowerCAmelCase__					:    Any												=					image_processor(images=SCREAMING_SNAKE_CASE_		,return_tensors='''pt'''			).to(SCREAMING_SNAKE_CASE_			)

						# forward pass
						with torch.no_grad():
										lowerCAmelCase__					:    Tuple												=					model(**SCREAMING_SNAKE_CASE_			)

						# verify the logits
						lowerCAmelCase__					:    Tuple												=					torch.Size((1, 10_00)			)
						self.assertEqual(outputs.logits.shape		,SCREAMING_SNAKE_CASE_			)

						lowerCAmelCase__					:    Union[str, Any]												=					torch.tensor([-0.0260, -0.4739, 0.1911]			).to(SCREAMING_SNAKE_CASE_			)

						self.assertTrue(torch.allclose(outputs.logits[0, :3]		,SCREAMING_SNAKE_CASE_		,atol=1e-4			)			)



@require_torch
class 	lowerCamelCase__      (							unittest.TestCase				,    _lowerCAmelCase):




		'''simple docstring'''



		snake_case_     =(ConvNextBackbone,) if is_torch_available() else ()
		snake_case_     =ConvNextConfig

		snake_case_     =False



		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						lowerCAmelCase__					:    Any												=					ConvNextModelTester(self			)






import unittest

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

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

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



class 	lowerCamelCase__      (							lowerCamelCase__				,    unittest.TestCase):




		'''simple docstring'''



		snake_case_     =KandinskyVaaPriorPipeline
		snake_case_     =["""prompt"""]
		snake_case_     =["""prompt""", """negative_prompt"""]
		snake_case_     =[
		    """num_images_per_prompt""",
		    """generator""",
		    """num_inference_steps""",
		    """latents""",
		    """negative_prompt""",
		    """guidance_scale""",
		    """output_type""",
		    """return_dict""",
		]
		snake_case_     =False



		@property
		def 		lowerCAmelCase__				(self			)					->      Any:

						"""simple docstring"""






						return 32



		@property
		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						return 32



		@property
		def 		lowerCAmelCase__				(self			)					->      Tuple:

						"""simple docstring"""






						return self.time_input_dim



		@property
		def 		lowerCAmelCase__				(self			)					->      Optional[int]:

						"""simple docstring"""






						return self.time_input_dim * 4



		@property
		def 		lowerCAmelCase__				(self			)					->      Dict:

						"""simple docstring"""






						return 1_00



		@property
		def 		lowerCAmelCase__				(self			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    Any												=					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''			)
						return tokenizer



		@property
		def 		lowerCAmelCase__				(self			)					->      str:

						"""simple docstring"""






						torch.manual_seed(0			)
						lowerCAmelCase__					:    str												=					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=10_00		,)
						return CLIPTextModelWithProjection(__lowerCamelCase			)



		@property
		def 		lowerCAmelCase__				(self			)					->      int:

						"""simple docstring"""






						torch.manual_seed(0			)

						lowerCAmelCase__					:    List[str]												=					{
						    '''num_attention_heads''': 2,
						    '''attention_head_dim''': 12,
						    '''embedding_dim''': self.text_embedder_hidden_size,
						    '''num_layers''': 1,
						}

						lowerCAmelCase__					:    List[str]												=					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
						lowerCAmelCase__					:    int												=					nn.Parameter(torch.ones(model.clip_std.shape			)			)
						return model



		@property
		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						torch.manual_seed(0			)
						lowerCAmelCase__					:    Any												=					CLIPVisionConfig(
						    hidden_size=self.text_embedder_hidden_size		,image_size=2_24		,projection_dim=self.text_embedder_hidden_size		,intermediate_size=37		,num_attention_heads=4		,num_channels=3		,num_hidden_layers=5		,patch_size=14		,)

						lowerCAmelCase__					:    int												=					CLIPVisionModelWithProjection(__lowerCamelCase			)
						return model



		@property
		def 		lowerCAmelCase__				(self			)					->      Any:

						"""simple docstring"""






						lowerCAmelCase__					:    Optional[Any]												=					CLIPImageProcessor(
						    crop_size=2_24		,do_center_crop=__lowerCamelCase		,do_normalize=__lowerCamelCase		,do_resize=__lowerCamelCase		,image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073]		,image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711]		,resample=3		,size=2_24		,)

						return image_processor



		def 		lowerCAmelCase__				(self			)					->      Any:

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					self.dummy_prior
						lowerCAmelCase__					:    List[Any]												=					self.dummy_image_encoder
						lowerCAmelCase__					:    Optional[Any]												=					self.dummy_text_encoder
						lowerCAmelCase__					:    Optional[int]												=					self.dummy_tokenizer
						lowerCAmelCase__					:    str												=					self.dummy_image_processor

						lowerCAmelCase__					:    Union[str, Any]												=					UnCLIPScheduler(
						    variance_type='''fixed_small_log'''		,prediction_type='''sample'''		,num_train_timesteps=10_00		,clip_sample=__lowerCamelCase		,clip_sample_range=10.0		,)

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

						return components



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase=0			)					->      int:

						"""simple docstring"""






						if str(__lowerCamelCase			).startswith('''mps'''			):
										lowerCAmelCase__					:    Dict												=					torch.manual_seed(__lowerCamelCase			)
						else:
										lowerCAmelCase__					:    Any												=					torch.Generator(device=__lowerCamelCase			).manual_seed(__lowerCamelCase			)
						lowerCAmelCase__					:    Optional[Any]												=					{
						    '''prompt''': '''horse''',
						    '''generator''': generator,
						    '''guidance_scale''': 4.0,
						    '''num_inference_steps''': 2,
						    '''output_type''': '''np''',
						}
						return inputs



		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					'''cpu'''

						lowerCAmelCase__					:    Optional[int]												=					self.get_dummy_components()

						lowerCAmelCase__					:    int												=					self.pipeline_class(**__lowerCamelCase			)
						lowerCAmelCase__					:    int												=					pipe.to(__lowerCamelCase			)

						pipe.set_progress_bar_config(disable=__lowerCamelCase			)

						lowerCAmelCase__					:    List[Any]												=					pipe(**self.get_dummy_inputs(__lowerCamelCase			)			)
						lowerCAmelCase__					:    Union[str, Any]												=					output.image_embeds

						lowerCAmelCase__					:    Tuple												=					pipe(
						    **self.get_dummy_inputs(__lowerCamelCase			)		,return_dict=__lowerCamelCase		,)[0]

						lowerCAmelCase__					:    Union[str, Any]												=					image[0, -10:]
						lowerCAmelCase__					:    str												=					image_from_tuple[0, -10:]

						assert image.shape == (1, 32)

						lowerCAmelCase__					:    int												=					np.array(
						    [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156]			)

						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			)					->      List[Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					torch_device == '''cpu'''
						lowerCAmelCase__					:    List[Any]												=					True
						lowerCAmelCase__					:    Optional[int]												=					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			)					->      Any:

						"""simple docstring"""






						lowerCAmelCase__					:    int												=					torch_device == '''cpu'''
						lowerCAmelCase__					:    int												=					False

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

