
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			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Dict					, SCREAMING_SNAKE_CASE__			:			int=13					, SCREAMING_SNAKE_CASE__			:			Optional[int]=32					, SCREAMING_SNAKE_CASE__			:			List[Any]=3					, SCREAMING_SNAKE_CASE__			:			List[Any]=4					, SCREAMING_SNAKE_CASE__			:			List[Any]=[10, 20, 30, 40]					, SCREAMING_SNAKE_CASE__			:			Any=[2, 2, 3, 2]					, SCREAMING_SNAKE_CASE__			:			int=True					, SCREAMING_SNAKE_CASE__			:			List[Any]=True					, SCREAMING_SNAKE_CASE__			:			Any=37					, SCREAMING_SNAKE_CASE__			:			Optional[Any]="gelu"					, SCREAMING_SNAKE_CASE__			:			List[str]=10					, SCREAMING_SNAKE_CASE__			:			List[str]=0.02					, SCREAMING_SNAKE_CASE__			:			Any=["stage2", "stage3", "stage4"]					, SCREAMING_SNAKE_CASE__			:			str=[2, 3, 4]					, SCREAMING_SNAKE_CASE__			:			int=None					, ):






      """simple docstring"""



      UpperCamelCase						    =					parent
      UpperCamelCase						    =					batch_size
      UpperCamelCase						    =					image_size
      UpperCamelCase						    =					num_channels
      UpperCamelCase						    =					num_stages
      UpperCamelCase						    =					hidden_sizes
      UpperCamelCase						    =					depths
      UpperCamelCase						    =					is_training
      UpperCamelCase						    =					use_labels
      UpperCamelCase						    =					intermediate_size
      UpperCamelCase						    =					hidden_act
      UpperCamelCase						    =					num_labels
      UpperCamelCase						    =					initializer_range
      UpperCamelCase						    =					out_features
      UpperCamelCase						    =					out_indices
      UpperCamelCase						    =					scope

 def      __lowerCAmelCase  (						self			:			Optional[int]   ):






      """simple docstring"""



      UpperCamelCase						    =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

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

 def      __lowerCAmelCase  (						self			:			int   ):






      """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			:			Optional[Any]					, SCREAMING_SNAKE_CASE__			:			Optional[int]					, SCREAMING_SNAKE_CASE__			:			Optional[int]					, SCREAMING_SNAKE_CASE__			:			str   ):






      """simple docstring"""



      UpperCamelCase						    =					ConvNextModel(config=SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					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			:			int					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Tuple					, SCREAMING_SNAKE_CASE__			:			Optional[int]   ):






      """simple docstring"""



      UpperCamelCase						    =					ConvNextForImageClassification(SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					model(SCREAMING_SNAKE_CASE__					, labels=SCREAMING_SNAKE_CASE__   )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_labels)   )

 def      __lowerCAmelCase  (						self			:			int					, SCREAMING_SNAKE_CASE__			:			Dict					, SCREAMING_SNAKE_CASE__			:			Optional[Any]					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					ConvNextBackbone(config=SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					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						    =					None
      UpperCamelCase						    =					ConvNextBackbone(config=SCREAMING_SNAKE_CASE__   )
      model.to(SCREAMING_SNAKE_CASE__   )
      model.eval()
      UpperCamelCase						    =					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			:			str   ):






      """simple docstring"""



      UpperCamelCase						    =					self.prepare_config_and_inputs()
      UpperCamelCase    ,		UpperCamelCase    ,		UpperCamelCase						    =					config_and_inputs
      UpperCamelCase						    =					{'pixel_values': pixel_values}
      return config, inputs_dict




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

 """simple docstring"""
 SCREAMING_SNAKE_CASE_				:					List[str]       						=(
     (
         ConvNextModel,
         ConvNextForImageClassification,
         ConvNextBackbone,
     )
     if is_torch_available()
     else ()
 )
 SCREAMING_SNAKE_CASE_				:					str       						=(
     {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
     if is_torch_available()
     else {}
 )

 SCREAMING_SNAKE_CASE_				:					Dict       						=True
 SCREAMING_SNAKE_CASE_				:					List[Any]       						=False
 SCREAMING_SNAKE_CASE_				:					Tuple       						=False
 SCREAMING_SNAKE_CASE_				:					List[str]       						=False
 SCREAMING_SNAKE_CASE_				:					str       						=False

 def      __lowerCAmelCase  (						self			:			Dict   ):






      """simple docstring"""



      UpperCamelCase						    =					ConvNextModelTester(self   )
      UpperCamelCase						    =					ConfigTester(self					, config_class=SCREAMING_SNAKE_CASE__					, has_text_modality=SCREAMING_SNAKE_CASE__					, hidden_size=37   )

 def      __lowerCAmelCase  (						self			:			Dict   ):






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






      """simple docstring"""



      return

 @unittest.skip(reason='ConvNext does not use inputs_embeds'   )
 def      __lowerCAmelCase  (						self			:			Tuple   ):






      """simple docstring"""



      pass

 @unittest.skip(reason='ConvNext does not support input and output embeddings'   )
 def      __lowerCAmelCase  (						self			:			str   ):






      """simple docstring"""



      pass

 @unittest.skip(reason='ConvNext does not use feedforward chunking'   )
 def      __lowerCAmelCase  (						self			:			Any   ):






      """simple docstring"""



      pass

 def      __lowerCAmelCase  (						self			:			int   ):






      """simple docstring"""



      UpperCamelCase    ,		UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs_for_common()

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

           UpperCamelCase						    =					['pixel_values']
           self.assertListEqual(arg_names[:1]					, SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			Tuple   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			Optional[int]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_backbone(*SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			str   ):






      """simple docstring"""



      def check_hidden_states_output(SCREAMING_SNAKE_CASE__			:			str					, SCREAMING_SNAKE_CASE__			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			Dict   ):
           UpperCamelCase						    =					model_class(SCREAMING_SNAKE_CASE__   )
           model.to(SCREAMING_SNAKE_CASE__   )
           model.eval()

           with torch.no_grad():
                UpperCamelCase						    =					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__   )   )

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

           UpperCamelCase						    =					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						    =					self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           UpperCamelCase						    =					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						    =					True

           check_hidden_states_output(SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__					, SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			List[Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__   )






 @slow
 def      __lowerCAmelCase  (						self			:			List[str]   ):






      """simple docstring"""



      for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase						    =					ConvNextModel.from_pretrained(SCREAMING_SNAKE_CASE__   )
           self.assertIsNotNone(SCREAMING_SNAKE_CASE__   )




def  __lowerCamelCase					( )       ->      Dict:
     UpperCamelCase						    =					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[Any]   ):






      """simple docstring"""



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






 @slow
 def      __lowerCAmelCase  (						self			:			Union[str, Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'   ).to(SCREAMING_SNAKE_CASE__   )

      UpperCamelCase						    =					self.default_image_processor
      UpperCamelCase						    =					prepare_img()
      UpperCamelCase						    =					image_processor(images=SCREAMING_SNAKE_CASE__					, return_tensors='pt'   ).to(SCREAMING_SNAKE_CASE__   )

      # forward pass
      with torch.no_grad():
           UpperCamelCase						    =					model(**SCREAMING_SNAKE_CASE__   )

      # verify the logits
      UpperCamelCase						    =					torch.Size((1, 10_00)   )
      self.assertEqual(outputs.logits.shape					, SCREAMING_SNAKE_CASE__   )

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

 """simple docstring"""
 SCREAMING_SNAKE_CASE_				:					List[str]       						=(ConvNextBackbone,) if is_torch_available() else ()
 SCREAMING_SNAKE_CASE_				:					str       						=ConvNextConfig

 SCREAMING_SNAKE_CASE_				:					Union[str, Any]       						=False

 def      __lowerCAmelCase  (						self			:			Optional[Any]   ):






      """simple docstring"""



      UpperCamelCase						    =					ConvNextModelTester(self   )




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

import flax
import jax.numpy as jnp
from jax import random

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .scheduling_utils_flax import FlaxSchedulerMixin
@flax.struct.dataclass
class       _lowerCAmelCase  :

 """simple docstring"""
 # setable values
 SCREAMING_SNAKE_CASE_				:					Optional[int]       						=None
 SCREAMING_SNAKE_CASE_				:					Optional[jnp.ndarray]       						=None
 SCREAMING_SNAKE_CASE_				:					Optional[jnp.ndarray]       						=None  # sigma(t_i)

 @classmethod
 def      __lowerCAmelCase  (						cls			:			Optional[Any]   ):






      """simple docstring"""



      return cls()




@dataclass
class       _lowerCAmelCase  (		__magic_name__							):

 """simple docstring"""
 SCREAMING_SNAKE_CASE_				:					jnp.ndarray
 SCREAMING_SNAKE_CASE_				:					jnp.ndarray
 SCREAMING_SNAKE_CASE_				:					KarrasVeSchedulerState




class       _lowerCAmelCase  (		__magic_name__			,	__magic_name__							):

 """simple docstring"""

 @property
 def      __lowerCAmelCase  (						self			:			Any   ):






      """simple docstring"""



      return True

 @register_to_config
 def __init__(						self			:			Dict					, SCREAMING_SNAKE_CASE__			:			float = 0.02					, SCREAMING_SNAKE_CASE__			:			float = 1_00					, SCREAMING_SNAKE_CASE__			:			float = 1.007					, SCREAMING_SNAKE_CASE__			:			float = 80					, SCREAMING_SNAKE_CASE__			:			float = 0.05					, SCREAMING_SNAKE_CASE__			:			float = 50					, ):






      """simple docstring"""



      pass

 def      __lowerCAmelCase  (						self			:			int   ):






      """simple docstring"""



      return KarrasVeSchedulerState.create()

 def      __lowerCAmelCase  (						self			:			Union[str, Any]					, SCREAMING_SNAKE_CASE__			:			KarrasVeSchedulerState					, SCREAMING_SNAKE_CASE__			:			int					, SCREAMING_SNAKE_CASE__			:			Tuple = ()   ):






      """simple docstring"""



      UpperCamelCase						    =					jnp.arange(0					, SCREAMING_SNAKE_CASE__   )[::-1].copy()
      UpperCamelCase						    =					[
          (
              self.config.sigma_max**2
              * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1))
          )
          for i in timesteps
      ]

      return state.replace(
          num_inference_steps=SCREAMING_SNAKE_CASE__					, schedule=jnp.array(SCREAMING_SNAKE_CASE__					, dtype=jnp.floataa   )					, timesteps=SCREAMING_SNAKE_CASE__					, )

 def      __lowerCAmelCase  (						self			:			List[Any]					, SCREAMING_SNAKE_CASE__			:			KarrasVeSchedulerState					, SCREAMING_SNAKE_CASE__			:			jnp.ndarray					, SCREAMING_SNAKE_CASE__			:			float					, SCREAMING_SNAKE_CASE__			:			random.KeyArray					, ):






      """simple docstring"""



      if self.config.s_min <= sigma <= self.config.s_max:
           UpperCamelCase						    =					min(self.config.s_churn / state.num_inference_steps					, 2**0.5 - 1   )
      else:
           UpperCamelCase						    =					0

      # sample eps ~ N(0, S_noise^2 * I)
      UpperCamelCase						    =					random.split(SCREAMING_SNAKE_CASE__					, num=1   )
      UpperCamelCase						    =					self.config.s_noise * random.normal(key=SCREAMING_SNAKE_CASE__					, shape=sample.shape   )
      UpperCamelCase						    =					sigma + gamma * sigma
      UpperCamelCase						    =					sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps)

      return sample_hat, sigma_hat

 def      __lowerCAmelCase  (						self			:			Optional[int]					, SCREAMING_SNAKE_CASE__			:			KarrasVeSchedulerState					, SCREAMING_SNAKE_CASE__			:			jnp.ndarray					, SCREAMING_SNAKE_CASE__			:			float					, SCREAMING_SNAKE_CASE__			:			float					, SCREAMING_SNAKE_CASE__			:			jnp.ndarray					, SCREAMING_SNAKE_CASE__			:			bool = True					, ):






      """simple docstring"""



      UpperCamelCase						    =					sample_hat + sigma_hat * model_output
      UpperCamelCase						    =					(sample_hat - pred_original_sample) / sigma_hat
      UpperCamelCase						    =					sample_hat + (sigma_prev - sigma_hat) * derivative

      if not return_dict:
           return (sample_prev, derivative, state)

      return FlaxKarrasVeOutput(prev_sample=SCREAMING_SNAKE_CASE__					, derivative=SCREAMING_SNAKE_CASE__					, state=SCREAMING_SNAKE_CASE__   )

 def      __lowerCAmelCase  (						self			:			Tuple					, SCREAMING_SNAKE_CASE__			:			KarrasVeSchedulerState					, SCREAMING_SNAKE_CASE__			:			jnp.ndarray					, SCREAMING_SNAKE_CASE__			:			float					, SCREAMING_SNAKE_CASE__			:			float					, SCREAMING_SNAKE_CASE__			:			jnp.ndarray					, SCREAMING_SNAKE_CASE__			:			jnp.ndarray					, SCREAMING_SNAKE_CASE__			:			jnp.ndarray					, SCREAMING_SNAKE_CASE__			:			bool = True					, ):






      """simple docstring"""



      UpperCamelCase						    =					sample_prev + sigma_prev * model_output
      UpperCamelCase						    =					(sample_prev - pred_original_sample) / sigma_prev
      UpperCamelCase						    =					sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr)

      if not return_dict:
           return (sample_prev, derivative, state)

      return FlaxKarrasVeOutput(prev_sample=SCREAMING_SNAKE_CASE__					, derivative=SCREAMING_SNAKE_CASE__					, state=SCREAMING_SNAKE_CASE__   )






 def      __lowerCAmelCase  (						self			:			Any					, SCREAMING_SNAKE_CASE__			:			KarrasVeSchedulerState					, SCREAMING_SNAKE_CASE__			:			Any					, SCREAMING_SNAKE_CASE__			:			str					, SCREAMING_SNAKE_CASE__			:			int   ):






      """simple docstring"""



      raise NotImplementedError()


