



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



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





													'''simple docstring'''

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



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Any									=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

													__a		:		Any									=       self.get_config()

													return config, pixel_values, labels



						def     lowerCAmelCase__(self							):





													'''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=_lowercase						,     initializer_range=self.initializer_range						,     out_features=self.out_features						,     out_indices=self.out_indices						,     num_labels=self.num_labels						,     )



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		Tuple									=       ConvNextVaModel(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		Tuple									=       model(_lowercase							)
													# 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						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		str									=       ConvNextVaForImageClassification(_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		Dict									=       model(_lowercase						,     labels=_lowercase							)
													self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.num_labels)							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		Union[str, Any]									=       ConvNextVaBackbone(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		Optional[int]									=       model(_lowercase							)

													# 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
													__a		:		Tuple									=       None
													__a		:		Tuple									=       ConvNextVaBackbone(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		List[str]									=       model(_lowercase							)

													# 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							):





													'''simple docstring'''

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





						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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







@require_torch
class SCREAMING_SNAKE_CASE__					(		__snake_case     ,					__snake_case     ,					unittest.TestCase      ):
						_lowerCAmelCase							=					(
						    (
						        ConvNextVaModel,
						        ConvNextVaForImageClassification,
						        ConvNextVaBackbone,
						    )
						    if is_torch_available()
						    else ()
						)
						_lowerCAmelCase							=					(
						    {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
						    if is_torch_available()
						    else {}
						)

						_lowerCAmelCase							=					False
						_lowerCAmelCase							=					False
						_lowerCAmelCase							=					False
						_lowerCAmelCase							=					False
						_lowerCAmelCase							=					False



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Dict									=       ConvNextVaModelTester(self							)
													__a		:		Tuple									=       ConfigTester(self						,     config_class=_lowercase						,     has_text_modality=_lowercase						,     hidden_size=37							)



						def     lowerCAmelCase__(self							):





													'''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							):





													'''simple docstring'''

													return



						@unittest.skip(reason="""ConvNextV2 does not use inputs_embeds"""							)
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													pass



						@unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""							)
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													pass



						@unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""							)
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													pass



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													if not self.model_tester.is_training:
																				return

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

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

																				__a		:		Optional[int]									=       model_class(_lowercase							)
																				model.to(_lowercase							)
																				model.train()
																				__a		:		Tuple									=       self._prepare_for_class(_lowercase						,     _lowercase						,     return_labels=_lowercase							)
																				__a		:		Dict									=       model(**_lowercase							).loss
																				loss.backward()



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													if not self.model_tester.is_training:
																				return

													for model_class in self.all_model_classes:
																				__a		:		List[str]									=       self.model_tester.prepare_config_and_inputs_with_labels()
																				__a		:		Optional[Any]									=       False
																				__a		:		List[Any]									=       True

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

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



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		str									=       self.model_tester.prepare_config_and_inputs_for_common()

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

																				__a		:		Optional[int]									=       ["""pixel_values"""]
																				self.assertListEqual(arg_names[:1]						,     _lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Dict									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													def check_hidden_states_output(_lowercase						,     _lowercase						,     _lowercase							):
																				__a		:		Any									=       model_class(_lowercase							)
																				model.to(_lowercase							)
																				model.eval()

																				with torch.no_grad():
																											__a		:		Tuple									=       model(**self._prepare_for_class(_lowercase						,     _lowercase							)							)

																				__a		:		Any									=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																				__a		:		Optional[Any]									=       self.model_tester.num_stages
																				self.assertEqual(len(_lowercase							)						,     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]						,     )

													__a		:		Optional[Any]									=       self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				__a		:		Tuple									=       True
																				check_hidden_states_output(_lowercase						,     _lowercase						,     _lowercase							)

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

																				check_hidden_states_output(_lowercase						,     _lowercase						,     _lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[int]									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*_lowercase							)





						@slow
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				__a		:		Union[str, Any]									=       ConvNextVaModel.from_pretrained(_lowercase							)
																				self.assertIsNotNone(_lowercase							)







def       __magic_name__    (   ):
							__a		:		Union[str, Any]									=       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
							return image




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



						@cached_property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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





						@slow
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Dict									=       ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""							).to(_lowercase							)

													__a		:		Dict									=       self.default_image_processor
													__a		:		Union[str, Any]									=       prepare_img()
													__a		:		Union[str, Any]									=       preprocessor(images=_lowercase						,     return_tensors="""pt"""							).to(_lowercase							)

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

													# verify the logits
													__a		:		Optional[int]									=       torch.Size((1, 1000)							)
													self.assertEqual(outputs.logits.shape						,     _lowercase							)

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






"""simple docstring"""

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 SCREAMING_SNAKE_CASE__					(		__snake_case     ,					unittest.TestCase      ):
						_lowerCAmelCase							=					KandinskyVaaPriorPipeline
						_lowerCAmelCase							=					["prompt"]
						_lowerCAmelCase							=					["prompt", "negative_prompt"]
						_lowerCAmelCase							=					[
						    "num_images_per_prompt",
						    "generator",
						    "num_inference_steps",
						    "latents",
						    "negative_prompt",
						    "guidance_scale",
						    "output_type",
						    "return_dict",
						]
						_lowerCAmelCase							=					False



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													return 32



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													return 32



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													return self.time_input_dim



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													return self.time_input_dim * 4



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													return 100



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[str]									=       CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""							)
													return tokenizer



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													torch.manual_seed(0							)
													__a		:		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=1000						,     )
													return CLIPTextModelWithProjection(_lowercase							)



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													torch.manual_seed(0							)

													__a		:		Dict									=       {
													    """num_attention_heads""": 2,
													    """attention_head_dim""": 12,
													    """embedding_dim""": self.text_embedder_hidden_size,
													    """num_layers""": 1,
													}

													__a		:		Tuple									=       PriorTransformer(**_lowercase							)
													# 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
													__a		:		int									=       nn.Parameter(torch.ones(model.clip_std.shape							)							)
													return model



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													torch.manual_seed(0							)
													__a		:		List[str]									=       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						,     )

													__a		:		Optional[Any]									=       CLIPVisionModelWithProjection(_lowercase							)
													return model



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[Any]									=       CLIPImageProcessor(
													    crop_size=224						,     do_center_crop=_lowercase						,     do_normalize=_lowercase						,     do_resize=_lowercase						,     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=224						,     )

													return image_processor



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Union[str, Any]									=       self.dummy_prior
													__a		:		int									=       self.dummy_image_encoder
													__a		:		Any									=       self.dummy_text_encoder
													__a		:		int									=       self.dummy_tokenizer
													__a		:		Optional[Any]									=       self.dummy_image_processor

													__a		:		List[Any]									=       UnCLIPScheduler(
													    variance_type="""fixed_small_log"""						,     prediction_type="""sample"""						,     num_train_timesteps=1000						,     clip_sample=_lowercase						,     clip_sample_range=10.0						,     )

													__a		:		List[Any]									=       {
													    """prior""": prior,
													    """image_encoder""": image_encoder,
													    """text_encoder""": text_encoder,
													    """tokenizer""": tokenizer,
													    """scheduler""": scheduler,
													    """image_processor""": image_processor,
													}

													return components



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase=0							):





													'''simple docstring'''

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



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Union[str, Any]									=       """cpu"""

													__a		:		Union[str, Any]									=       self.get_dummy_components()

													__a		:		Dict									=       self.pipeline_class(**_lowercase							)
													__a		:		Tuple									=       pipe.to(_lowercase							)

													pipe.set_progress_bar_config(disable=_lowercase							)

													__a		:		Optional[int]									=       pipe(**self.get_dummy_inputs(_lowercase							)							)
													__a		:		str									=       output.image_embeds

													__a		:		Any									=       pipe(
													    **self.get_dummy_inputs(_lowercase							)						,     return_dict=_lowercase						,     )[0]

													__a		:		List[Any]									=       image[0, -10:]
													__a		:		List[Any]									=       image_from_tuple[0, -10:]

													assert image.shape == (1, 32)

													__a		:		Optional[Any]									=       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							):





													'''simple docstring'''

													__a		:		Any									=       torch_device == """cpu"""
													__a		:		Any									=       True
													__a		:		Any									=       False

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





						@skip_mps
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[int]									=       torch_device == """cpu"""
													__a		:		Union[str, Any]									=       False

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

