

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     __lowercase :
 def __init__(				self ,				A_ ,				A_=13 ,				A_=32 ,				A_=3 ,				A_=4 ,				A_=[10, 20, 30, 40] ,				A_=[2, 2, 3, 2] ,				A_=True ,				A_=True ,				A_=37 ,				A_="gelu" ,				A_=10 ,				A_=0.02 ,				A_=["stage2", "stage3", "stage4"] ,				A_=[2, 3, 4] ,				A_=None ,				)   ->List[str]:



       '''simple docstring'''





       __lowerCAmelCase   :		Any   =   parent
       __lowerCAmelCase   :		Any   =   batch_size
       __lowerCAmelCase   :		str   =   image_size
       __lowerCAmelCase   :		Optional[int]   =   num_channels
       __lowerCAmelCase   :		Union[str, Any]   =   num_stages
       __lowerCAmelCase   :		List[str]   =   hidden_sizes
       __lowerCAmelCase   :		List[Any]   =   depths
       __lowerCAmelCase   :		Dict   =   is_training
       __lowerCAmelCase   :		Optional[Any]   =   use_labels
       __lowerCAmelCase   :		Union[str, Any]   =   intermediate_size
       __lowerCAmelCase   :		Tuple   =   hidden_act
       __lowerCAmelCase   :		str   =   num_labels
       __lowerCAmelCase   :		Dict   =   initializer_range
       __lowerCAmelCase   :		Dict   =   out_features
       __lowerCAmelCase   :		Optional[Any]   =   out_indices
       __lowerCAmelCase   :		Dict   =   scope
 def       UpperCamelCase__    (				self   )   ->Dict:



       '''simple docstring'''





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

       __lowerCAmelCase   :		int   =   None
       if self.use_labels:
             __lowerCAmelCase   :		str   =   ids_tensor([self.batch_size] ,				self.num_labels   )

       __lowerCAmelCase   :		List[Any]   =   self.get_config()

       return config, pixel_values, labels
 def       UpperCamelCase__    (				self   )   ->Optional[int]:



       '''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=__snake_case ,				initializer_range=self.initializer_range ,				out_features=self.out_features ,				out_indices=self.out_indices ,				num_labels=self.num_labels ,				)
 def       UpperCamelCase__    (				self ,				A_ ,				A_ ,				A_   )   ->List[str]:



       '''simple docstring'''





       __lowerCAmelCase   :		List[str]   =   ConvNextVaModel(config=__snake_case   )
       model.to(__snake_case   )
       model.eval()
       __lowerCAmelCase   :		int   =   model(__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       UpperCamelCase__    (				self ,				A_ ,				A_ ,				A_   )   ->Dict:



       '''simple docstring'''





       __lowerCAmelCase   :		List[str]   =   ConvNextVaForImageClassification(__snake_case   )
       model.to(__snake_case   )
       model.eval()
       __lowerCAmelCase   :		Tuple   =   model(__snake_case ,				labels=__snake_case   )
       self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)   )
 def       UpperCamelCase__    (				self ,				A_ ,				A_ ,				A_   )   ->List[Any]:



       '''simple docstring'''





       __lowerCAmelCase   :		Optional[int]   =   ConvNextVaBackbone(config=__snake_case   )
       model.to(__snake_case   )
       model.eval()
       __lowerCAmelCase   :		str   =   model(__snake_case   )

       # verify hidden states
       self.parent.assertEqual(len(result.feature_maps   ) ,				len(config.out_features   )   )
       self.parent.assertListEqual(list(result.feature_maps[0].shape   ) ,				[self.batch_size, self.hidden_sizes[1], 4, 4]   )

       # verify channels
       self.parent.assertEqual(len(model.channels   ) ,				len(config.out_features   )   )
       self.parent.assertListEqual(model.channels ,				config.hidden_sizes[1:]   )

       # verify backbone works with out_features=None
       __lowerCAmelCase   :		Tuple   =   None
       __lowerCAmelCase   :		Tuple   =   ConvNextVaBackbone(config=__snake_case   )
       model.to(__snake_case   )
       model.eval()
       __lowerCAmelCase   :		Optional[Any]   =   model(__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       UpperCamelCase__    (				self   )   ->Dict:



       '''simple docstring'''





       __lowerCAmelCase   :		Any   =   self.prepare_config_and_inputs()
       __lowerCAmelCase,				__lowerCAmelCase,				__lowerCAmelCase   :		int   =   config_and_inputs
       __lowerCAmelCase   :		Dict   =   {'''pixel_values''': pixel_values}
       return config, inputs_dict





 def       UpperCamelCase__    (				self   )   ->Dict:



       '''simple docstring'''





       __lowerCAmelCase   :		int   =   self.prepare_config_and_inputs()
       __lowerCAmelCase,				__lowerCAmelCase,				__lowerCAmelCase   :		Any   =   config_and_inputs
       __lowerCAmelCase   :		List[Any]   =   {'''pixel_values''': pixel_values, '''labels''': labels}
       return config, inputs_dict







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

 _UpperCamelCase         =    False
 _UpperCamelCase         =    False
 _UpperCamelCase         =    False
 _UpperCamelCase         =    False
 _UpperCamelCase         =    False
 def       UpperCamelCase__    (				self   )   ->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase   :		str   =   ConvNextVaModelTester(self   )
       __lowerCAmelCase   :		Optional[Any]   =   ConfigTester(self ,				config_class=__snake_case ,				has_text_modality=__snake_case ,				hidden_size=37   )
 def       UpperCamelCase__    (				self   )   ->Union[str, Any]:



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



       '''simple docstring'''





       return
 @unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''   )
 def       UpperCamelCase__    (				self   )   ->List[Any]:



       '''simple docstring'''





       pass
 @unittest.skip(reason='''ConvNextV2 does not support input and output embeddings'''   )
 def       UpperCamelCase__    (				self   )   ->Optional[int]:



       '''simple docstring'''





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



       '''simple docstring'''





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



       '''simple docstring'''





       if not self.model_tester.is_training:
             return

       for model_class in self.all_model_classes:
             __lowerCAmelCase,				__lowerCAmelCase   :		Dict   =   self.model_tester.prepare_config_and_inputs_with_labels()
             __lowerCAmelCase   :		Optional[int]   =   True

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

             __lowerCAmelCase   :		List[str]   =   model_class(__snake_case   )
             model.to(__snake_case   )
             model.train()
             __lowerCAmelCase   :		List[Any]   =   self._prepare_for_class(__snake_case ,				__snake_case ,				return_labels=__snake_case   )
             __lowerCAmelCase   :		List[Any]   =   model(**__snake_case   ).loss
             loss.backward()
 def       UpperCamelCase__    (				self   )   ->str:



       '''simple docstring'''





       if not self.model_tester.is_training:
             return

       for model_class in self.all_model_classes:
             __lowerCAmelCase,				__lowerCAmelCase   :		str   =   self.model_tester.prepare_config_and_inputs_with_labels()
             __lowerCAmelCase   :		Any   =   False
             __lowerCAmelCase   :		Optional[int]   =   True

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

             __lowerCAmelCase   :		Tuple   =   model_class(__snake_case   )
             model.to(__snake_case   )
             model.gradient_checkpointing_enable()
             model.train()
             __lowerCAmelCase   :		Any   =   self._prepare_for_class(__snake_case ,				__snake_case ,				return_labels=__snake_case   )
             __lowerCAmelCase   :		int   =   model(**__snake_case   ).loss
             loss.backward()
 def       UpperCamelCase__    (				self   )   ->str:



       '''simple docstring'''





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

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

             __lowerCAmelCase   :		int   =   ['''pixel_values''']
             self.assertListEqual(arg_names[:1] ,				__snake_case   )
 def       UpperCamelCase__    (				self   )   ->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase   :		Any   =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__snake_case   )
 def       UpperCamelCase__    (				self   )   ->str:



       '''simple docstring'''





       def check_hidden_states_output(A_ ,				A_ ,				A_   ):
             __lowerCAmelCase   :		Optional[Any]   =   model_class(__snake_case   )
             model.to(__snake_case   )
             model.eval()

             with torch.no_grad():
                   __lowerCAmelCase   :		str   =   model(**self._prepare_for_class(__snake_case ,				__snake_case   )   )

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

             __lowerCAmelCase   :		List[str]   =   self.model_tester.num_stages
             self.assertEqual(len(__snake_case   ) ,				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] ,				)

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

       for model_class in self.all_model_classes:
             __lowerCAmelCase   :		Any   =   True
             check_hidden_states_output(__snake_case ,				__snake_case ,				__snake_case   )

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

             check_hidden_states_output(__snake_case ,				__snake_case ,				__snake_case   )
 def       UpperCamelCase__    (				self   )   ->List[str]:



       '''simple docstring'''





       __lowerCAmelCase   :		Union[str, Any]   =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__snake_case   )





 @slow
 def       UpperCamelCase__    (				self   )   ->Optional[Any]:



       '''simple docstring'''





       for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __lowerCAmelCase   :		str   =   ConvNextVaModel.from_pretrained(__snake_case   )
             self.assertIsNotNone(__snake_case   )







def 						_lowercase					(	):
      __lowerCAmelCase   :		List[Any]   =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
      return image







@require_torch
@require_vision
class     __lowercase (unittest.TestCase					):
 @cached_property
 def       UpperCamelCase__    (				self   )   ->List[str]:



       '''simple docstring'''





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





 @slow
 def       UpperCamelCase__    (				self   )   ->Any:



       '''simple docstring'''





       __lowerCAmelCase   :		int   =   ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''   ).to(__snake_case   )

       __lowerCAmelCase   :		int   =   self.default_image_processor
       __lowerCAmelCase   :		Optional[int]   =   prepare_img()
       __lowerCAmelCase   :		List[Any]   =   preprocessor(images=__snake_case ,				return_tensors='''pt'''   ).to(__snake_case   )

       # forward pass
       with torch.no_grad():
             __lowerCAmelCase   :		Union[str, Any]   =   model(**__snake_case   )

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

       __lowerCAmelCase   :		Any   =   torch.tensor([0.9_996, 0.1_966, -0.4_386]   ).to(__snake_case   )
       self.assertTrue(torch.allclose(outputs.logits[0, :3] ,				__snake_case ,				atol=1e-4   )   )






from math import factorial

class     A__   :







    """simple docstring"""

    def __init__(			self     ,   __snake_case     ,   __snake_case       ):
         snake_case											=							real
         if isinstance(__snake_case     ,   __snake_case       ):
              snake_case											=							[1] * rank
         else:
              snake_case											=							rank
    def __repr__(			self       ):
         return (
             F'''{self.real}+'''
             F'''{"+".join(str(__snake_case       )+"E"+str(n+1       )for n,dual in enumerate(self.duals       )       )}'''
         )
    def 						a_     (			self       ):
         snake_case											=							self.duals.copy()
         while cur[-1] == 0:
              cur.pop(-1       )
         return Dual(self.real     ,   __snake_case       )
    def __add__(			self     ,   __snake_case       ):
         if not isinstance(__snake_case     ,   __snake_case       ):
              return Dual(self.real + other     ,   self.duals       )
         snake_case											=							self.duals.copy()
         snake_case											=							other.duals.copy()
         if len(__snake_case       ) > len(__snake_case       ):
              o_dual.extend([1] * (len(__snake_case       ) - len(__snake_case       ))       )
         elif len(__snake_case       ) < len(__snake_case       ):
              s_dual.extend([1] * (len(__snake_case       ) - len(__snake_case       ))       )
         snake_case											=							[]
         for i in range(len(__snake_case       )       ):
              new_duals.append(s_dual[i] + o_dual[i]       )
         return Dual(self.real + other.real     ,   __snake_case       )

    __magic_name__         =						__add__
    def __sub__(			self     ,   __snake_case       ):
         return self + other * -1
    def __mul__(			self     ,   __snake_case       ):
         if not isinstance(__snake_case     ,   __snake_case       ):
              snake_case											=							[]
              for i in self.duals:
                   new_duals.append(i * other       )
              return Dual(self.real * other     ,   __snake_case       )
         snake_case											=							[0] * (len(self.duals       ) + len(other.duals       ) + 1)
         for i, item in enumerate(self.duals       ):
              for j, jtem in enumerate(other.duals       ):
                   new_duals[i + j + 1] += item * jtem
         for k in range(len(self.duals       )       ):
              new_duals[k] += self.duals[k] * other.real
         for index in range(len(other.duals       )       ):
              new_duals[index] += other.duals[index] * self.real
         return Dual(self.real * other.real     ,   __snake_case       )

    __magic_name__         =						__mul__
    def __truediv__(			self     ,   __snake_case       ):
         if not isinstance(__snake_case     ,   __snake_case       ):
              snake_case											=							[]
              for i in self.duals:
                   new_duals.append(i / other       )
              return Dual(self.real / other     ,   __snake_case       )
         raise ValueError
    def __floordiv__(			self     ,   __snake_case       ):
         if not isinstance(__snake_case     ,   __snake_case       ):
              snake_case											=							[]
              for i in self.duals:
                   new_duals.append(i // other       )
              return Dual(self.real // other     ,   __snake_case       )
         raise ValueError




    def __pow__(			self     ,   __snake_case       ):
         if n < 0 or isinstance(__snake_case     ,   __snake_case       ):
              raise ValueError('''power must be a positive integer'''       )
         if n == 0:
              return 1
         if n == 1:
              return self
         snake_case											=							self
         for _ in range(n - 1       ):
              x *= self
         return x






def      UpperCAmelCase__		(UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_							):



     """simple docstring"""






     if not callable(UpperCamelCase_							):
          raise ValueError('''differentiate() requires a function as input for func'''							)
     if not isinstance(UpperCamelCase_				,(float, int)							):
          raise ValueError('''differentiate() requires a float as input for position'''							)
     if not isinstance(UpperCamelCase_				,UpperCamelCase_							):
          raise ValueError('''differentiate() requires an int as input for order'''							)
     snake_case											=							Dual(UpperCamelCase_				,1							)
     snake_case											=							func(UpperCamelCase_							)
     if order == 0:
          return result.real
     return result.duals[order - 1] * factorial(UpperCamelCase_							)


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 def      UpperCAmelCase__		(UpperCamelCase_							):



      """simple docstring"""






      return y**2 * y**4

 print(differentiate(f, 9, 2))




