'''simple docstring'''




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






       def __init__(      self				,     UpperCamelCase_				,     UpperCamelCase_=13				,     UpperCamelCase_=32				,     UpperCamelCase_=3				,     UpperCamelCase_=4				,     UpperCamelCase_=[10, 20, 30, 40]				,     UpperCamelCase_=[2, 2, 3, 2]				,     UpperCamelCase_=True				,     UpperCamelCase_=True				,     UpperCamelCase_=37				,     UpperCamelCase_="gelu"				,     UpperCamelCase_=10				,     UpperCamelCase_=0.0_2				,     UpperCamelCase_=["stage2", "stage3", "stage4"]				,     UpperCamelCase_=[2, 3, 4]				,     UpperCamelCase_=None				,     ):
              __UpperCAmelCase  :				Optional[Any]       				=		parent
              __UpperCAmelCase  :				Tuple       				=		batch_size
              __UpperCAmelCase  :				Optional[int]       				=		image_size
              __UpperCAmelCase  :				int       				=		num_channels
              __UpperCAmelCase  :				str       				=		num_stages
              __UpperCAmelCase  :				Tuple       				=		hidden_sizes
              __UpperCAmelCase  :				Dict       				=		depths
              __UpperCAmelCase  :				int       				=		is_training
              __UpperCAmelCase  :				List[Any]       				=		use_labels
              __UpperCAmelCase  :				Optional[int]       				=		intermediate_size
              __UpperCAmelCase  :				Tuple       				=		hidden_act
              __UpperCAmelCase  :				Dict       				=		num_labels
              __UpperCAmelCase  :				Optional[int]       				=		initializer_range
              __UpperCAmelCase  :				List[Any]       				=		out_features
              __UpperCAmelCase  :				Optional[int]       				=		out_indices
              __UpperCAmelCase  :				int       				=		scope






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Dict       				=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

              __UpperCAmelCase  :				int       				=		self.get_config()
              return config, pixel_values, labels






       def 			_snake_case						(      self      ):
              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=UpperCamelCase_				,     initializer_range=self.initializer_range				,     out_features=self.out_features				,     out_indices=self.out_indices				,     num_labels=self.num_labels				,     )






       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Dict       				=		ConvNextModel(config=UpperCamelCase_      )
              model.to(UpperCamelCase_      )
              model.eval()
              __UpperCAmelCase  :				int       				=		model(UpperCamelCase_      )
              # 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				,     UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Tuple       				=		ConvNextForImageClassification(UpperCamelCase_      )
              model.to(UpperCamelCase_      )
              model.eval()
              __UpperCAmelCase  :				int       				=		model(UpperCamelCase_				,     labels=UpperCamelCase_      )
              self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels)      )






       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Dict       				=		ConvNextBackbone(config=UpperCamelCase_      )
              model.to(UpperCamelCase_      )
              model.eval()
              __UpperCAmelCase  :				Dict       				=		model(UpperCamelCase_      )

              # 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  :				List[Any]       				=		None
              __UpperCAmelCase  :				List[str]       				=		ConvNextBackbone(config=UpperCamelCase_      )
              model.to(UpperCamelCase_      )
              model.eval()
              __UpperCAmelCase  :				Dict       				=		model(UpperCamelCase_      )

              # 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      ):
              __UpperCAmelCase  :				Union[str, Any]       				=		self.prepare_config_and_inputs()
              __UpperCAmelCase				, __UpperCAmelCase				, __UpperCAmelCase  :				Union[str, Any]       				=		config_and_inputs
              __UpperCAmelCase  :				Any       				=		{"pixel_values": pixel_values}
              return config, inputs_dict






@require_torch
class 		__A       (__magic_name__ ,					__magic_name__ ,					unittest.TestCase   ):
       snake_case       :Optional[int]				    =					(
           (
               ConvNextModel,
               ConvNextForImageClassification,
               ConvNextBackbone,
           )
           if is_torch_available()
           else ()
       )
       snake_case       :int				    =					(
           {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
           if is_torch_available()
           else {}
       )

       snake_case       :int				    =					True
       snake_case       :Optional[Any]				    =					False
       snake_case       :List[str]				    =					False
       snake_case       :Optional[Any]				    =					False
       snake_case       :Dict				    =					False






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Optional[Any]       				=		ConvNextModelTester(self      )
              __UpperCAmelCase  :				Union[str, Any]       				=		ConfigTester(self				,     config_class=UpperCamelCase_				,     has_text_modality=UpperCamelCase_				,     hidden_size=37      )






       def 			_snake_case						(      self      ):
              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      ):
              return






       @unittest.skip(reason="ConvNext does not use inputs_embeds"      )
       def 			_snake_case						(      self      ):
              pass






       @unittest.skip(reason="ConvNext does not support input and output embeddings"      )
       def 			_snake_case						(      self      ):
              pass






       @unittest.skip(reason="ConvNext does not use feedforward chunking"      )
       def 			_snake_case						(      self      ):
              pass






       def 			_snake_case						(      self      ):
              __UpperCAmelCase				, __UpperCAmelCase  :				Any       				=		self.model_tester.prepare_config_and_inputs_for_common()

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

                     __UpperCAmelCase  :				int       				=		["pixel_values"]
                     self.assertListEqual(arg_names[:1]				,     UpperCamelCase_      )






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Optional[Any]       				=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*UpperCamelCase_      )






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				str       				=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_backbone(*UpperCamelCase_      )






       def 			_snake_case						(      self      ):
              def check_hidden_states_output(UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      ):
                     __UpperCAmelCase  :				List[Any]       				=		model_class(UpperCamelCase_      )
                     model.to(UpperCamelCase_      )
                     model.eval()

                     with torch.no_grad():
                            __UpperCAmelCase  :				Tuple       				=		model(**self._prepare_for_class(UpperCamelCase_				,     UpperCamelCase_      )      )

                     __UpperCAmelCase  :				int       				=		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                     __UpperCAmelCase  :				Tuple       				=		self.model_tester.num_stages
                     self.assertEqual(len(UpperCamelCase_      )				,     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  :				Union[str, Any]       				=		self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     __UpperCAmelCase  :				Dict       				=		True
                     check_hidden_states_output(UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      )

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

                     check_hidden_states_output(UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      )






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Dict       				=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_      )







       @slow
       def 			_snake_case						(      self      ):
              for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     __UpperCAmelCase  :				int       				=		ConvNextModel.from_pretrained(UpperCamelCase_      )
                     self.assertIsNotNone(UpperCamelCase_      )





def _lowercase	(      )					->       List[str]:





       """simple docstring"""


       __UpperCAmelCase  :				List[str]       				=		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
       return image






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






       @cached_property
       def 			_snake_case						(      self      ):
              return AutoImageProcessor.from_pretrained("facebook/convnext-tiny-224"      ) if is_vision_available() else None







       @slow
       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				List[str]       				=		ConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"      ).to(UpperCamelCase_      )

              __UpperCAmelCase  :				Union[str, Any]       				=		self.default_image_processor
              __UpperCAmelCase  :				Dict       				=		prepare_img()
              __UpperCAmelCase  :				Union[str, Any]       				=		image_processor(images=UpperCamelCase_				,     return_tensors="pt"      ).to(UpperCamelCase_      )

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

              # verify the logits
              __UpperCAmelCase  :				Union[str, Any]       				=		torch.Size((1, 10_00)      )
              self.assertEqual(outputs.logits.shape				,     UpperCamelCase_      )

              __UpperCAmelCase  :				int       				=		torch.tensor([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]      ).to(UpperCamelCase_      )

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






@require_torch
class 		__A       (unittest.TestCase ,					__magic_name__   ):
       snake_case       :List[Any]				    =					(ConvNextBackbone,) if is_torch_available() else ()
       snake_case       :Tuple				    =					ConvNextConfig

       snake_case       :Optional[Any]				    =					False






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				int       				=		ConvNextModelTester(self      )







'''simple docstring'''




def _lowercase	(      lowerCamelCase__							,    lowerCamelCase__						)					->       List[str]:





       """simple docstring"""


       __UpperCAmelCase  :				Dict       				=		(boundary[1] - boundary[0]) / steps
       __UpperCAmelCase  :				Tuple       				=		boundary[0]
       __UpperCAmelCase  :				List[str]       				=		boundary[1]
       __UpperCAmelCase  :				List[Any]       				=		make_points(lowerCamelCase__							,    lowerCamelCase__							,    lowerCamelCase__						)
       __UpperCAmelCase  :				int       				=		0.0
       y += (h / 2.0) * f(lowerCamelCase__						)
       for i in x_i:
              # print(i)
              y += h * f(lowerCamelCase__						)
       y += (h / 2.0) * f(lowerCamelCase__						)
       return y

def _lowercase	(      lowerCamelCase__							,    lowerCamelCase__							,    lowerCamelCase__						)					->       List[Any]:





       """simple docstring"""


       __UpperCAmelCase  :				Optional[Any]       				=		a + h
       while x < (b - h):
              yield x
              __UpperCAmelCase  :				List[str]       				=		x + h

def _lowercase	(      lowerCamelCase__						)					->       Optional[Any]:  # enter your function here





       """simple docstring"""


       __UpperCAmelCase  :				str       				=		(x - 0) * (x - 0)
       return y

def _lowercase	(      )					->       int:





       """simple docstring"""


       __UpperCAmelCase  :				Tuple       				=		0.0  # Lower bound of integration
       __UpperCAmelCase  :				Union[str, Any]       				=		1.0  # Upper bound of integration
       __UpperCAmelCase  :				Union[str, Any]       				=		10.0  # define number of steps or resolution
       __UpperCAmelCase  :				Dict       				=		[a, b]  # define boundary of integration
       __UpperCAmelCase  :				Optional[int]       				=		method_a(lowerCamelCase__							,    lowerCamelCase__						)
       print(f"""y = {y}"""						)


if __name__ == "__main__":
   main()






