



from dataclasses import dataclass
from typing import Dict, Optional, Union

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .attention_processor import AttentionProcessor, AttnProcessor
from .embeddings import TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin



@dataclass
class 				lowerCAmelCase_		(  a__							):
       UpperCAmelCase__				:					torch.FloatTensor






class 				lowerCAmelCase_		(  a__       ,    a__							):
       @register_to_config
       def __init__(			self,			SCREAMING_SNAKE_CASE_ = 32,			SCREAMING_SNAKE_CASE_ = 64,			SCREAMING_SNAKE_CASE_ = 20,			SCREAMING_SNAKE_CASE_ = 768,			SCREAMING_SNAKE_CASE_=77,			SCREAMING_SNAKE_CASE_=4,			SCREAMING_SNAKE_CASE_ = 0.0,			SCREAMING_SNAKE_CASE_ = "silu",			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = "linear",			SCREAMING_SNAKE_CASE_ = "prd",			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			)     ->		Union[str, Any]:
          super().__init__()
          UpperCamelCase    :  str							=	num_attention_heads
          UpperCamelCase    :  Optional[int]							=	attention_head_dim
          UpperCamelCase    :  Union[str, Any]							=	num_attention_heads * attention_head_dim
          UpperCamelCase    :  Optional[int]							=	additional_embeddings

          UpperCamelCase    :  Any							=	time_embed_dim or inner_dim
          UpperCamelCase    :  str							=	embedding_proj_dim or embedding_dim
          UpperCamelCase    :  Optional[int]							=	clip_embed_dim or embedding_dim

          UpperCamelCase    :  str							=	Timesteps(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			0       )
          UpperCamelCase    :  Union[str, Any]							=	TimestepEmbedding(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			out_dim=SCREAMING_SNAKE_CASE_,			act_fn=SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  Optional[Any]							=	nn.Linear(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )

          if embedding_proj_norm_type is None:
             UpperCamelCase    :  str							=	None
          elif embedding_proj_norm_type == "layer":
             UpperCamelCase    :  str							=	nn.LayerNorm(SCREAMING_SNAKE_CASE_       )
          else:
             raise ValueError(F"""unsupported embedding_proj_norm_type: {embedding_proj_norm_type}"""       )

          UpperCamelCase    :  List[Any]							=	nn.Linear(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )

          if encoder_hid_proj_type is None:
             UpperCamelCase    :  Tuple							=	None
          elif encoder_hid_proj_type == "linear":
             UpperCamelCase    :  int							=	nn.Linear(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )
          else:
             raise ValueError(F"""unsupported encoder_hid_proj_type: {encoder_hid_proj_type}"""       )

          UpperCamelCase    :  List[str]							=	nn.Parameter(torch.zeros(1,			num_embeddings + additional_embeddings,			SCREAMING_SNAKE_CASE_       )       )

          if added_emb_type == "prd":
             UpperCamelCase    :  Tuple							=	nn.Parameter(torch.zeros(1,			1,			SCREAMING_SNAKE_CASE_       )       )
          elif added_emb_type is None:
             UpperCamelCase    :  Union[str, Any]							=	None
          else:
             raise ValueError(
                 F"""`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `'prd'` or `None`."""       )

          UpperCamelCase    :  List[Any]							=	nn.ModuleList(
              [
                  BasicTransformerBlock(
                      SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			dropout=SCREAMING_SNAKE_CASE_,			activation_fn='gelu',			attention_bias=SCREAMING_SNAKE_CASE_,			)
                  for d in range(SCREAMING_SNAKE_CASE_       )
              ]       )

          if norm_in_type == "layer":
             UpperCamelCase    :  int							=	nn.LayerNorm(SCREAMING_SNAKE_CASE_       )
          elif norm_in_type is None:
             UpperCamelCase    :  Optional[int]							=	None
          else:
             raise ValueError(F"""Unsupported norm_in_type: {norm_in_type}."""       )

          UpperCamelCase    :  Union[str, Any]							=	nn.LayerNorm(SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  str							=	nn.Linear(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  Tuple							=	torch.full(
              [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings],			-1_00_00.0       )
          causal_attention_mask.triu_(1       )
          UpperCamelCase    :  List[str]							=	causal_attention_mask[None, ...]
          self.register_buffer('causal_attention_mask',			SCREAMING_SNAKE_CASE_,			persistent=SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  Tuple							=	nn.Parameter(torch.zeros(1,			SCREAMING_SNAKE_CASE_       )       )
          UpperCamelCase    :  Optional[Any]							=	nn.Parameter(torch.zeros(1,			SCREAMING_SNAKE_CASE_       )       )
       @property
       # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
       def 					snake_case_       (			self       )     ->		Dict[str, AttentionProcessor]:
          UpperCamelCase    :  Dict							=	{}

          def fn_recursive_add_processors(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       ):
             if hasattr(SCREAMING_SNAKE_CASE_,			'set_processor'       ):
                UpperCamelCase    :  Union[str, Any]							=	module.processor

             for sub_name, child in module.named_children():
                fn_recursive_add_processors(F"""{name}.{sub_name}""",			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )

             return processors

          for name, module in self.named_children():
             fn_recursive_add_processors(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )

          return processors
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_       )     ->		Dict:
          UpperCamelCase    :  Dict							=	len(self.attn_processors.keys()       )

          if isinstance(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       ) and len(SCREAMING_SNAKE_CASE_       ) != count:
             raise ValueError(
                 F"""A dict of processors was passed, but the number of processors {len(SCREAMING_SNAKE_CASE_       )} does not match the"""
                 F""" number of attention layers: {count}. Please make sure to pass {count} processor classes."""       )

          def fn_recursive_attn_processor(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       ):
             if hasattr(SCREAMING_SNAKE_CASE_,			'set_processor'       ):
                if not isinstance(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       ):
                   module.set_processor(SCREAMING_SNAKE_CASE_       )
                else:
                   module.set_processor(processor.pop(F"""{name}.processor"""       )       )

             for sub_name, child in module.named_children():
                fn_recursive_attn_processor(F"""{name}.{sub_name}""",			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )

          for name, module in self.named_children():
             fn_recursive_attn_processor(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )
       def 					snake_case_       (			self       )     ->		Optional[Any]:
          self.set_attn_processor(AttnProcessor()       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = True,			)     ->		Optional[Any]:
          UpperCamelCase    :  str							=	hidden_states.shape[0]

          UpperCamelCase    :  List[str]							=	timestep
          if not torch.is_tensor(SCREAMING_SNAKE_CASE_       ):
             UpperCamelCase    :  Optional[Any]							=	torch.tensor([timesteps],			dtype=torch.long,			device=hidden_states.device       )
          elif torch.is_tensor(SCREAMING_SNAKE_CASE_       ) and len(timesteps.shape       ) == 0:
             UpperCamelCase    :  str							=	timesteps[None].to(hidden_states.device       )

          # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
          UpperCamelCase    :  List[str]							=	timesteps * torch.ones(SCREAMING_SNAKE_CASE_,			dtype=timesteps.dtype,			device=timesteps.device       )

          UpperCamelCase    :  int							=	self.time_proj(SCREAMING_SNAKE_CASE_       )

          # timesteps does not contain any weights and will always return f32 tensors
          # but time_embedding might be fp16, so we need to cast here.
          UpperCamelCase    :  Dict							=	timesteps_projected.to(dtype=self.dtype       )
          UpperCamelCase    :  Tuple							=	self.time_embedding(SCREAMING_SNAKE_CASE_       )

          if self.embedding_proj_norm is not None:
             UpperCamelCase    :  Optional[Any]							=	self.embedding_proj_norm(SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  Optional[int]							=	self.embedding_proj(SCREAMING_SNAKE_CASE_       )
          if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
             UpperCamelCase    :  Optional[int]							=	self.encoder_hidden_states_proj(SCREAMING_SNAKE_CASE_       )
          elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None:
             raise ValueError('`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set'       )

          UpperCamelCase    :  Union[str, Any]							=	self.proj_in(SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  Union[str, Any]							=	self.positional_embedding.to(hidden_states.dtype       )

          UpperCamelCase    :  str							=	[]
          UpperCamelCase    :  List[str]							=	0

          if encoder_hidden_states is not None:
             additional_embeds.append(SCREAMING_SNAKE_CASE_       )
             additional_embeddings_len += encoder_hidden_states.shape[1]

          if len(proj_embeddings.shape       ) == 2:
             UpperCamelCase    :  Optional[Any]							=	proj_embeddings[:, None, :]

          if len(hidden_states.shape       ) == 2:
             UpperCamelCase    :  List[str]							=	hidden_states[:, None, :]

          UpperCamelCase    :  Union[str, Any]							=	additional_embeds + [
              proj_embeddings,
              time_embeddings[:, None, :],
              hidden_states,
          ]

          if self.prd_embedding is not None:
             UpperCamelCase    :  Optional[int]							=	self.prd_embedding.to(hidden_states.dtype       ).expand(SCREAMING_SNAKE_CASE_,			-1,			-1       )
             additional_embeds.append(SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  Optional[int]							=	torch.cat(
              SCREAMING_SNAKE_CASE_,			dim=1,			)

          # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens
          UpperCamelCase    :  Union[str, Any]							=	additional_embeddings_len + proj_embeddings.shape[1] + 1
          if positional_embeddings.shape[1] < hidden_states.shape[1]:
             UpperCamelCase    :  Optional[int]							=	F.pad(
                 SCREAMING_SNAKE_CASE_,			(
                     0,
                     0,
                     additional_embeddings_len,
                     self.prd_embedding.shape[1] if self.prd_embedding is not None else 0,
                 ),			value=0.0,			)

          UpperCamelCase    :  Union[str, Any]							=	hidden_states + positional_embeddings

          if attention_mask is not None:
             UpperCamelCase    :  str							=	(1 - attention_mask.to(hidden_states.dtype       )) * -1_00_00.0
             UpperCamelCase    :  Dict							=	F.pad(SCREAMING_SNAKE_CASE_,			(0, self.additional_embeddings),			value=0.0       )
             UpperCamelCase    :  Any							=	(attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype       )
             UpperCamelCase    :  List[str]							=	attention_mask.repeat_interleave(self.config.num_attention_heads,			dim=0       )

          if self.norm_in is not None:
             UpperCamelCase    :  Optional[int]							=	self.norm_in(SCREAMING_SNAKE_CASE_       )

          for block in self.transformer_blocks:
             UpperCamelCase    :  Optional[Any]							=	block(SCREAMING_SNAKE_CASE_,			attention_mask=SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  Optional[Any]							=	self.norm_out(SCREAMING_SNAKE_CASE_       )

          if self.prd_embedding is not None:
             UpperCamelCase    :  Union[str, Any]							=	hidden_states[:, -1]
          else:
             UpperCamelCase    :  int							=	hidden_states[:, additional_embeddings_len:]

          UpperCamelCase    :  Dict							=	self.proj_to_clip_embeddings(SCREAMING_SNAKE_CASE_       )

          if not return_dict:
             return (predicted_image_embedding,)

          return PriorTransformerOutput(predicted_image_embedding=SCREAMING_SNAKE_CASE_       )






       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_       )     ->		int:
          UpperCamelCase    :  List[str]							=	(prior_latents * self.clip_std) + self.clip_mean
          return prior_latents








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_		:
       def __init__(			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=13,			SCREAMING_SNAKE_CASE_=32,			SCREAMING_SNAKE_CASE_=3,			SCREAMING_SNAKE_CASE_=4,			SCREAMING_SNAKE_CASE_=[10, 20, 30, 40],			SCREAMING_SNAKE_CASE_=[2, 2, 3, 2],			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=37,			SCREAMING_SNAKE_CASE_="gelu",			SCREAMING_SNAKE_CASE_=10,			SCREAMING_SNAKE_CASE_=0.02,			SCREAMING_SNAKE_CASE_=["stage2", "stage3", "stage4"],			SCREAMING_SNAKE_CASE_=[2, 3, 4],			SCREAMING_SNAKE_CASE_=None,			)     ->		Optional[int]:
          UpperCamelCase    :  Dict							=	parent
          UpperCamelCase    :  Optional[Any]							=	batch_size
          UpperCamelCase    :  Union[str, Any]							=	image_size
          UpperCamelCase    :  Union[str, Any]							=	num_channels
          UpperCamelCase    :  List[Any]							=	num_stages
          UpperCamelCase    :  Any							=	hidden_sizes
          UpperCamelCase    :  Optional[int]							=	depths
          UpperCamelCase    :  Optional[int]							=	is_training
          UpperCamelCase    :  List[str]							=	use_labels
          UpperCamelCase    :  Dict							=	intermediate_size
          UpperCamelCase    :  List[Any]							=	hidden_act
          UpperCamelCase    :  Union[str, Any]							=	num_labels
          UpperCamelCase    :  str							=	initializer_range
          UpperCamelCase    :  List[str]							=	out_features
          UpperCamelCase    :  List[str]							=	out_indices
          UpperCamelCase    :  str							=	scope
       def 					snake_case_       (			self       )     ->		Optional[Any]:
          UpperCamelCase    :  int							=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

          UpperCamelCase    :  List[Any]							=	self.get_config()
          return config, pixel_values, labels
       def 					snake_case_       (			self       )     ->		Any:
          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 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		Tuple:
          UpperCamelCase    :  Dict							=	ConvNextModel(config=SCREAMING_SNAKE_CASE_       )
          model.to(SCREAMING_SNAKE_CASE_       )
          model.eval()
          UpperCamelCase    :  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 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		Optional[Any]:
          UpperCamelCase    :  Optional[int]							=	ConvNextForImageClassification(SCREAMING_SNAKE_CASE_       )
          model.to(SCREAMING_SNAKE_CASE_       )
          model.eval()
          UpperCamelCase    :  Dict							=	model(SCREAMING_SNAKE_CASE_,			labels=SCREAMING_SNAKE_CASE_       )
          self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.num_labels)       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		Dict:
          UpperCamelCase    :  int							=	ConvNextBackbone(config=SCREAMING_SNAKE_CASE_       )
          model.to(SCREAMING_SNAKE_CASE_       )
          model.eval()
          UpperCamelCase    :  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
          UpperCamelCase    :  Any							=	None
          UpperCamelCase    :  List[str]							=	ConvNextBackbone(config=SCREAMING_SNAKE_CASE_       )
          model.to(SCREAMING_SNAKE_CASE_       )
          model.eval()
          UpperCamelCase    :  Tuple							=	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 					snake_case_       (			self       )     ->		int:
          UpperCamelCase    :  Optional[int]							=	self.prepare_config_and_inputs()
          UpperCamelCase	,     UpperCamelCase	,     UpperCamelCase    :  Optional[Any]							=	config_and_inputs
          UpperCamelCase    :  str							=	{'pixel_values': pixel_values}
          return config, inputs_dict






@require_torch
class 				lowerCAmelCase_		(  a__       ,    a__       ,    unittest.TestCase							):
       UpperCAmelCase__				:					List[Any]        =  (
           (
               ConvNextModel,
               ConvNextForImageClassification,
               ConvNextBackbone,
           )
           if is_torch_available()
           else ()
       )
       UpperCAmelCase__				:					Union[str, Any]        =  (
           {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
           if is_torch_available()
           else {}
       )

       UpperCAmelCase__				:					Dict        =  True
       UpperCAmelCase__				:					List[str]        =  False
       UpperCAmelCase__				:					Tuple        =  False
       UpperCAmelCase__				:					List[Any]        =  False
       UpperCAmelCase__				:					Tuple        =  False
       def 					snake_case_       (			self       )     ->		Optional[Any]:
          UpperCamelCase    :  Dict							=	ConvNextModelTester(self       )
          UpperCamelCase    :  int							=	ConfigTester(self,			config_class=SCREAMING_SNAKE_CASE_,			has_text_modality=SCREAMING_SNAKE_CASE_,			hidden_size=37       )
       def 					snake_case_       (			self       )     ->		List[str]:
          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 					snake_case_       (			self       )     ->		Union[str, Any]:
          return
       @unittest.skip(reason='ConvNext does not use inputs_embeds'       )
       def 					snake_case_       (			self       )     ->		List[str]:
          pass
       @unittest.skip(reason='ConvNext does not support input and output embeddings'       )
       def 					snake_case_       (			self       )     ->		str:
          pass
       @unittest.skip(reason='ConvNext does not use feedforward chunking'       )
       def 					snake_case_       (			self       )     ->		str:
          pass
       def 					snake_case_       (			self       )     ->		Any:
          UpperCamelCase	,     UpperCamelCase    :  Optional[int]							=	self.model_tester.prepare_config_and_inputs_for_common()

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

             UpperCamelCase    :  Union[str, Any]							=	['pixel_values']
             self.assertListEqual(arg_names[:1],			SCREAMING_SNAKE_CASE_       )
       def 					snake_case_       (			self       )     ->		List[str]:
          UpperCamelCase    :  Optional[int]							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_       )
       def 					snake_case_       (			self       )     ->		Dict:
          UpperCamelCase    :  Optional[Any]							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_backbone(*SCREAMING_SNAKE_CASE_       )
       def 					snake_case_       (			self       )     ->		Dict:
          def check_hidden_states_output(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       ):
             UpperCamelCase    :  List[Any]							=	model_class(SCREAMING_SNAKE_CASE_       )
             model.to(SCREAMING_SNAKE_CASE_       )
             model.eval()

             with torch.no_grad():
                UpperCamelCase    :  List[Any]							=	model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )       )

             UpperCamelCase    :  List[str]							=	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

             UpperCamelCase    :  int							=	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],			)

          UpperCamelCase	,     UpperCamelCase    :  Tuple							=	self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             UpperCamelCase    :  Optional[int]							=	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"]
             UpperCamelCase    :  str							=	True

             check_hidden_states_output(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )
       def 					snake_case_       (			self       )     ->		List[str]:
          UpperCamelCase    :  List[Any]							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_       )






       @slow
       def 					snake_case_       (			self       )     ->		Tuple:
          for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase    :  Union[str, Any]							=	ConvNextModel.from_pretrained(SCREAMING_SNAKE_CASE_       )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )






def 		UpperCamelCase	(   )	->     int:
   UpperCamelCase    :  Optional[int]							=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
   return image



@require_torch
@require_vision
class 				lowerCAmelCase_		(  unittest.TestCase							):
       @cached_property
       def 					snake_case_       (			self       )     ->		Optional[int]:
          return AutoImageProcessor.from_pretrained('facebook/convnext-tiny-224'       ) if is_vision_available() else None






       @slow
       def 					snake_case_       (			self       )     ->		str:
          UpperCamelCase    :  Dict							=	ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'       ).to(SCREAMING_SNAKE_CASE_       )

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

          # forward pass
          with torch.no_grad():
             UpperCamelCase    :  Optional[Any]							=	model(**SCREAMING_SNAKE_CASE_       )

          # verify the logits
          UpperCamelCase    :  List[Any]							=	torch.Size((1, 1000)       )
          self.assertEqual(outputs.logits.shape,			SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  List[str]							=	torch.tensor([-0.02_60, -0.47_39, 0.19_11]       ).to(SCREAMING_SNAKE_CASE_       )

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






@require_torch
class 				lowerCAmelCase_		(  unittest.TestCase       ,    a__							):
       UpperCAmelCase__				:					Tuple        =  (ConvNextBackbone,) if is_torch_available() else ()
       UpperCAmelCase__				:					List[str]        =  ConvNextConfig

       UpperCAmelCase__				:					Tuple        =  False
       def 					snake_case_       (			self       )     ->		int:
          UpperCamelCase    :  List[Any]							=	ConvNextModelTester(self       )



