
"""simple docstring"""




import inspect
from typing import Callable, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import DiffusionPipeline
from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
from diffusers.utils import logging


UpperCAmelCase__      :    Any    							= logging.get_logger(__name__)  # pylint: disable=invalid-name



class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				def __init__(self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       )					->		Optional[int]:



											"""simple docstring"""




											super().__init__()
											self.register_modules(
											    vae=SCREAMING_SNAKE_CASE__   ,       text_encoder=SCREAMING_SNAKE_CASE__   ,       tokenizer=SCREAMING_SNAKE_CASE__   ,       unet=SCREAMING_SNAKE_CASE__   ,       scheduler=SCREAMING_SNAKE_CASE__   ,       safety_checker=SCREAMING_SNAKE_CASE__   ,       feature_extractor=SCREAMING_SNAKE_CASE__   ,       )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__ = "auto"    )					->		List[str]:



											"""simple docstring"""




											if slice_size == "auto":
																		# half the attention head size is usually a good trade-off between
																		# speed and memory
																		SCREAMING_SNAKE_CASE__						:       Dict           =   self.unet.config.attention_head_dim // 2
											self.unet.set_attention_slice(SCREAMING_SNAKE_CASE__    )



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



											"""simple docstring"""




											self.enable_attention_slicing(SCREAMING_SNAKE_CASE__    )



				@torch.no_grad()
				def __call__(self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__ = 5_12   ,       SCREAMING_SNAKE_CASE__ = 5_12   ,       SCREAMING_SNAKE_CASE__ = 50   ,       SCREAMING_SNAKE_CASE__ = 7.5   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = 1   ,       SCREAMING_SNAKE_CASE__ = 0.0   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = "pil"   ,       SCREAMING_SNAKE_CASE__ = True   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = 1   ,       SCREAMING_SNAKE_CASE__ = None   ,       **SCREAMING_SNAKE_CASE__   ,       )					->		Union[str, Any]:



											"""simple docstring"""




											if isinstance(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ):
																		SCREAMING_SNAKE_CASE__						:       str           =   1
											elif isinstance(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ):
																		SCREAMING_SNAKE_CASE__						:       List[str]           =   len(SCREAMING_SNAKE_CASE__    )
											else:
																		raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(SCREAMING_SNAKE_CASE__    )}'''    )

											if height % 8 != 0 or width % 8 != 0:
																		raise ValueError(F'''`height` and `width` have to be divisible by 8 but are {height} and {width}.'''    )

											if (callback_steps is None) or (
											    callback_steps is not None and (not isinstance(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ) or callback_steps <= 0)
											):
																		raise ValueError(
																		    F'''`callback_steps` has to be a positive integer but is {callback_steps} of type'''
																		    F''' {type(SCREAMING_SNAKE_CASE__    )}.'''    )

											# get prompt text embeddings
											SCREAMING_SNAKE_CASE__						:       int           =   self.tokenizer(
											    SCREAMING_SNAKE_CASE__   ,       padding="""max_length"""   ,       max_length=self.tokenizer.model_max_length   ,       return_tensors="""pt"""   ,       )
											SCREAMING_SNAKE_CASE__						:       str           =   text_inputs.input_ids

											if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
																		SCREAMING_SNAKE_CASE__						:       Dict           =   self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]    )
																		logger.warning(
																		    """The following part of your input was truncated because CLIP can only handle sequences up to"""
																		    F''' {self.tokenizer.model_max_length} tokens: {removed_text}'''    )
																		SCREAMING_SNAKE_CASE__						:       str           =   text_input_ids[:, : self.tokenizer.model_max_length]

											if text_embeddings is None:
																		SCREAMING_SNAKE_CASE__						:       Dict           =   self.text_encoder(text_input_ids.to(self.device    )    )[0]

											# duplicate text embeddings for each generation per prompt, using mps friendly method
											SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__						:       Optional[int]           =   text_embeddings.shape
											SCREAMING_SNAKE_CASE__						:       int           =   text_embeddings.repeat(1   ,       SCREAMING_SNAKE_CASE__   ,       1    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   text_embeddings.view(bs_embed * num_images_per_prompt   ,       SCREAMING_SNAKE_CASE__   ,       -1    )

											# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
											# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
											# corresponds to doing no classifier free guidance.
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   guidance_scale > 1.0
											# get unconditional embeddings for classifier free guidance
											if do_classifier_free_guidance:
																		SCREAMING_SNAKE_CASE__						:       List[str]
																		if negative_prompt is None:
																									SCREAMING_SNAKE_CASE__						:       str           =   [""""""]
																		elif type(SCREAMING_SNAKE_CASE__    ) is not type(SCREAMING_SNAKE_CASE__    ):
																									raise TypeError(
																									    F'''`negative_prompt` should be the same type to `prompt`, but got {type(SCREAMING_SNAKE_CASE__    )} !='''
																									    F''' {type(SCREAMING_SNAKE_CASE__    )}.'''    )
																		elif isinstance(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ):
																									SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   [negative_prompt]
																		elif batch_size != len(SCREAMING_SNAKE_CASE__    ):
																									raise ValueError(
																									    F'''`negative_prompt`: {negative_prompt} has batch size {len(SCREAMING_SNAKE_CASE__    )}, but `prompt`:'''
																									    F''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches'''
																									    """ the batch size of `prompt`."""    )
																		else:
																									SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   negative_prompt

																		SCREAMING_SNAKE_CASE__						:       Tuple           =   text_input_ids.shape[-1]
																		SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   self.tokenizer(
																		    SCREAMING_SNAKE_CASE__   ,       padding="""max_length"""   ,       max_length=SCREAMING_SNAKE_CASE__   ,       truncation=SCREAMING_SNAKE_CASE__   ,       return_tensors="""pt"""   ,       )
																		SCREAMING_SNAKE_CASE__						:       List[str]           =   self.text_encoder(uncond_input.input_ids.to(self.device    )    )[0]

																		# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
																		SCREAMING_SNAKE_CASE__						:       Any           =   uncond_embeddings.shape[1]
																		SCREAMING_SNAKE_CASE__						:       Any           =   uncond_embeddings.repeat(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       1    )
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   uncond_embeddings.view(batch_size * num_images_per_prompt   ,       SCREAMING_SNAKE_CASE__   ,       -1    )

																		# For classifier free guidance, we need to do two forward passes.
																		# Here we concatenate the unconditional and text embeddings into a single batch
																		# to avoid doing two forward passes
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   torch.cat([uncond_embeddings, text_embeddings]    )

											# get the initial random noise unless the user supplied it

											# Unlike in other pipelines, latents need to be generated in the target device
											# for 1-to-1 results reproducibility with the CompVis implementation.
											# However this currently doesn't work in `mps`.
											SCREAMING_SNAKE_CASE__						:       List[str]           =   (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
											SCREAMING_SNAKE_CASE__						:       str           =   (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64)
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   text_embeddings.dtype
											if latents is None:
																		if self.device.type == "mps":
																									# randn does not exist on mps
																									SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   torch.randn(
																									    SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       device="""cpu"""   ,       dtype=SCREAMING_SNAKE_CASE__    ).to(self.device    )
																									SCREAMING_SNAKE_CASE__						:       List[Any]           =   torch.randn(SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       device="""cpu"""   ,       dtype=SCREAMING_SNAKE_CASE__    ).to(
																									    self.device    )
																		else:
																									SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   torch.randn(
																									    SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       device=self.device   ,       dtype=SCREAMING_SNAKE_CASE__    )
																									SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   torch.randn(SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       device=self.device   ,       dtype=SCREAMING_SNAKE_CASE__    )
											else:
																		if latents_reference.shape != latents_shape:
																									raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}'''    )
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   latents_reference.to(self.device    )
																		SCREAMING_SNAKE_CASE__						:       Any           =   latents.to(self.device    )

											# This is the key part of the pipeline where we
											# try to ensure that the generated images w/ the same seed
											# but different sizes actually result in similar images
											SCREAMING_SNAKE_CASE__						:       List[str]           =   (latents_shape[3] - latents_shape_reference[3]) // 2
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   (latents_shape[2] - latents_shape_reference[2]) // 2
											SCREAMING_SNAKE_CASE__						:       Dict           =   latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx
											SCREAMING_SNAKE_CASE__						:       Dict           =   latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy
											SCREAMING_SNAKE_CASE__						:       int           =   0 if dx < 0 else dx
											SCREAMING_SNAKE_CASE__						:       Tuple           =   0 if dy < 0 else dy
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   max(-dx   ,       0    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   max(-dy   ,       0    )
											# import pdb
											# pdb.set_trace()
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   latents_reference[:, :, dy : dy + h, dx : dx + w]

											# set timesteps
											self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__    )

											# Some schedulers like PNDM have timesteps as arrays
											# It's more optimized to move all timesteps to correct device beforehand
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.scheduler.timesteps.to(self.device    )

											# scale the initial noise by the standard deviation required by the scheduler
											SCREAMING_SNAKE_CASE__						:       int           =   latents * self.scheduler.init_noise_sigma

											# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
											# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
											# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
											# and should be between [0, 1]
											SCREAMING_SNAKE_CASE__						:       Tuple           =   """eta""" in set(inspect.signature(self.scheduler.step    ).parameters.keys()    )
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   {}
											if accepts_eta:
																		SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   eta

											for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__    )    ):
																		# expand the latents if we are doing classifier free guidance
																		SCREAMING_SNAKE_CASE__						:       List[str]           =   torch.cat([latents] * 2    ) if do_classifier_free_guidance else latents
																		SCREAMING_SNAKE_CASE__						:       List[str]           =   self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

																		# predict the noise residual
																		SCREAMING_SNAKE_CASE__						:       Dict           =   self.unet(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       encoder_hidden_states=SCREAMING_SNAKE_CASE__    ).sample

																		# perform guidance
																		if do_classifier_free_guidance:
																									SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__						:       Optional[int]           =   noise_pred.chunk(2    )
																									SCREAMING_SNAKE_CASE__						:       Dict           =   noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

																		# compute the previous noisy sample x_t -> x_t-1
																		SCREAMING_SNAKE_CASE__						:       int           =   self.scheduler.step(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       **SCREAMING_SNAKE_CASE__    ).prev_sample

																		# call the callback, if provided
																		if callback is not None and i % callback_steps == 0:
																									callback(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   1 / 0.18215 * latents
											SCREAMING_SNAKE_CASE__						:       Any           =   self.vae.decode(SCREAMING_SNAKE_CASE__    ).sample

											SCREAMING_SNAKE_CASE__						:       Dict           =   (image / 2 + 0.5).clamp(0   ,       1    )

											# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   image.cpu().permute(0   ,       2   ,       3   ,       1    ).float().numpy()

											if self.safety_checker is not None:
																		SCREAMING_SNAKE_CASE__						:       str           =   self.feature_extractor(self.numpy_to_pil(SCREAMING_SNAKE_CASE__    )   ,       return_tensors="""pt"""    ).to(
																		    self.device    )
																		SCREAMING_SNAKE_CASE__      ,      SCREAMING_SNAKE_CASE__						:       Tuple           =   self.safety_checker(
																		    images=SCREAMING_SNAKE_CASE__   ,       clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype    )    )
											else:
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   None

											if output_type == "pil":
																		SCREAMING_SNAKE_CASE__						:       Dict           =   self.numpy_to_pil(SCREAMING_SNAKE_CASE__    )

											if not return_dict:
																		return (image, has_nsfw_concept)

											return StableDiffusionPipelineOutput(images=SCREAMING_SNAKE_CASE__   ,       nsfw_content_detected=SCREAMING_SNAKE_CASE__    )












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 						lowerCAmelCase   :

					'''simple docstring'''

					def __init__(     self :		List[Any]						,   __a :		int						,   __a :		Tuple=13						,   __a :		int=32						,   __a :		List[Any]=3						,   __a :		Dict=4						,   __a :		List[str]=[10, 20, 30, 40]						,   __a :		Any=[2, 2, 3, 2]						,   __a :		List[str]=True						,   __a :		Any=True						,   __a :		Optional[Any]=37						,   __a :		Dict="gelu"						,   __a :		Tuple=10						,   __a :		Dict=0.02						,   __a :		Optional[Any]=["stage2", "stage3", "stage4"]						,   __a :		Optional[int]=[2, 3, 4]						,   __a :		int=None						,   )	->     Optional[Any]:


												"""simple docstring"""

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



					def 					lowerCAmelCase (     self :		List[Any]							)	->     List[Any]:


												"""simple docstring"""

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

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

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

												return config, pixel_values, labels



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


												"""simple docstring"""

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



					def 					lowerCAmelCase (     self :		int						,   __a :		Tuple						,   __a :		Dict						,   __a :		List[str]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							str			=   ConvNextVaModel(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Union[str, Any]			=   model(__a							)
												# 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 :		Any						,   __a :		Tuple						,   __a :		Any						,   __a :		Union[str, Any]							)	->     Optional[int]:


												"""simple docstring"""

												__lowercase					:							int			=   ConvNextVaForImageClassification(__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Union[str, Any]			=   model(__a						,   labels=__a							)
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_labels)							)



					def 					lowerCAmelCase (     self :		int						,   __a :		str						,   __a :		Any						,   __a :		Optional[int]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							Union[str, Any]			=   ConvNextVaBackbone(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase					:							Dict			=   model(__a							)

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

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


												"""simple docstring"""

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



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


												"""simple docstring"""

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







@require_torch
class 						lowerCAmelCase   (       __a					,				__a					,				unittest.TestCase	):

					'''simple docstring'''

					_A  :	Any     =					(
					    (
					        ConvNextVaModel,
					        ConvNextVaForImageClassification,
					        ConvNextVaBackbone,
					    )
					    if is_torch_available()
					    else ()
					)
					_A  :	Any     =					(
					    {'''feature-extraction''': ConvNextVaModel, '''image-classification''': ConvNextVaForImageClassification}
					    if is_torch_available()
					    else {}
					)

					_A  :	str     =					False
					_A  :	Dict     =					False
					_A  :	str     =					False
					_A  :	str     =					False
					_A  :	Dict     =					False



					def 					lowerCAmelCase (     self :		Optional[Any]							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   ConvNextVaModelTester(self							)
												__lowercase					:							Optional[int]			=   ConfigTester(self						,   config_class=__a						,   has_text_modality=__a						,   hidden_size=37							)



					def 					lowerCAmelCase (     self :		int							)	->     Optional[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 :		int							)	->     str:


												"""simple docstring"""

												return



					@unittest.skip(reason="""ConvNextV2 does not use inputs_embeds"""							)
					def 					lowerCAmelCase (     self :		int							)	->     Any:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""							)
					def 					lowerCAmelCase (     self :		Any							)	->     Union[str, Any]:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""							)
					def 					lowerCAmelCase (     self :		Any							)	->     Optional[int]:


												"""simple docstring"""

												pass



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


												"""simple docstring"""

												if not self.model_tester.is_training:
																			return

												for model_class in self.all_model_classes:
																			__lowercase	,	__lowercase					:							Union[str, Any]			=   self.model_tester.prepare_config_and_inputs_with_labels()
																			__lowercase					:							str			=   True

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

																			__lowercase					:							Union[str, Any]			=   model_class(__a							)
																			model.to(__a							)
																			model.train()
																			__lowercase					:							Tuple			=   self._prepare_for_class(__a						,   __a						,   return_labels=__a							)
																			__lowercase					:							Dict			=   model(**__a							).loss
																			loss.backward()



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


												"""simple docstring"""

												if not self.model_tester.is_training:
																			return

												for model_class in self.all_model_classes:
																			__lowercase	,	__lowercase					:							Tuple			=   self.model_tester.prepare_config_and_inputs_with_labels()
																			__lowercase					:							Dict			=   False
																			__lowercase					:							Optional[Any]			=   True

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

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



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


												"""simple docstring"""

												__lowercase	,	__lowercase					:							List[str]			=   self.model_tester.prepare_config_and_inputs_for_common()

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

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



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


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



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


												"""simple docstring"""

												def check_hidden_states_output(__a :		Any						,   __a :		Tuple						,   __a :		Tuple							):
																			__lowercase					:							Optional[Any]			=   model_class(__a							)
																			model.to(__a							)
																			model.eval()

																			with torch.no_grad():
																										__lowercase					:							int			=   model(**self._prepare_for_class(__a						,   __a							)							)

																			__lowercase					:							Optional[int]			=   outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

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

												for model_class in self.all_model_classes:
																			__lowercase					:							int			=   True
																			check_hidden_states_output(__a						,   __a						,   __a							)

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

																			check_hidden_states_output(__a						,   __a						,   __a							)



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


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__a							)



					@slow
					def 					lowerCAmelCase (     self :		Dict							)	->     Tuple:


												"""simple docstring"""

												for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowercase					:							Optional[int]			=   ConvNextVaModel.from_pretrained(__a							)
																			self.assertIsNotNone(__a							)







def 					snake_case_						(   ):
							__lowercase					:							int			=   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 :		Any							)	->     List[str]:


												"""simple docstring"""

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



					@slow
					def 					lowerCAmelCase (     self :		Dict							)	->     str:


												"""simple docstring"""

												__lowercase					:							Optional[Any]			=   ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""							).to(__a							)

												__lowercase					:							Dict			=   self.default_image_processor
												__lowercase					:							Any			=   prepare_img()
												__lowercase					:							int			=   preprocessor(images=__a						,   return_tensors="""pt"""							).to(__a							)

												# forward pass
												with torch.no_grad():
																			__lowercase					:							int			=   model(**__a							)

												# verify the logits
												__lowercase					:							List[str]			=   torch.Size((1, 1000)							)
												self.assertEqual(outputs.logits.shape						,   __a							)

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