

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





							"""simple docstring"""

							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    ,						)		->       List[str]:
														A      :					Dict      		=					parent
														A      :					List[str]      		=					batch_size
														A      :					int      		=					image_size
														A      :					List[Any]      		=					num_channels
														A      :					Any      		=					num_stages
														A      :					str      		=					hidden_sizes
														A      :					Optional[int]      		=					depths
														A      :					Union[str, Any]      		=					is_training
														A      :					Any      		=					use_labels
														A      :					Tuple      		=					intermediate_size
														A      :					Optional[Any]      		=					hidden_act
														A      :					Tuple      		=					num_labels
														A      :					List[Any]      		=					initializer_range
														A      :					List[Any]      		=					out_features
														A      :					Tuple      		=					out_indices
														A      :					Optional[Any]      		=					scope




							def       snake_case			(					self						)		->       Any:
														A      :					Dict      		=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

														A      :					Optional[int]      		=					self.get_config()
														return config, pixel_values, labels




							def       snake_case			(					self						)		->       List[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=__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						)		->       Optional[Any]:
														A      :					List[Any]      		=					ConvNextModel(config=__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()
														A      :					List[str]      		=					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						)		->       List[Any]:
														A      :					Dict      		=					ConvNextForImageClassification(__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()
														A      :					Union[str, Any]      		=					model(__UpperCAmelCase    ,						labels=__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)		->       List[str]:
														A      :					Tuple      		=					ConvNextBackbone(config=__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()
														A      :					Tuple      		=					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
														A      :					Dict      		=					None
														A      :					Dict      		=					ConvNextBackbone(config=__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()
														A      :					Optional[Any]      		=					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						)		->       List[Any]:
														A      :					Dict      		=					self.prepare_config_and_inputs()
														A      :					List[str]      		=					config_and_inputs
														A      :					List[str]      		=					{"pixel_values": pixel_values}
														return config, inputs_dict







@require_torch
class 					__lowercase  (						_UpperCamelCase		,       _UpperCamelCase		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		List[Any]								=	(
							    (
							        ConvNextModel,
							        ConvNextForImageClassification,
							        ConvNextBackbone,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase_						:		List[Any]								=	(
							    {'feature-extraction': ConvNextModel, 'image-classification': ConvNextForImageClassification}
							    if is_torch_available()
							    else {}
							)

							UpperCAmelCase_						:		Union[str, Any]								=	True
							UpperCAmelCase_						:		List[str]								=	False
							UpperCAmelCase_						:		Optional[Any]								=	False
							UpperCAmelCase_						:		Any								=	False
							UpperCAmelCase_						:		str								=	False




							def       snake_case			(					self						)		->       Dict:
														A      :					Union[str, Any]      		=					ConvNextModelTester(self						)
														A      :					List[str]      		=					ConfigTester(self    ,						config_class=__UpperCAmelCase    ,						has_text_modality=__UpperCAmelCase    ,						hidden_size=37						)




							def       snake_case			(					self						)		->       List[Any]:
														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						)		->       Union[str, Any]:
														pass




							@unittest.skip(reason='''ConvNext does not support input and output embeddings'''						)
							def       snake_case			(					self						)		->       Tuple:
														pass




							@unittest.skip(reason='''ConvNext does not use feedforward chunking'''						)
							def       snake_case			(					self						)		->       Tuple:
														pass




							def       snake_case			(					self						)		->       int:
														A      :					Union[str, Any]      		=					self.model_tester.prepare_config_and_inputs_for_common()

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

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




							def       snake_case			(					self						)		->       Dict:
														A      :					Union[str, Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[int]:
														A      :					List[str]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_backbone(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Dict:
														def check_hidden_states_output(__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						):
																					A      :					Optional[int]      		=					model_class(__UpperCAmelCase						)
																					model.to(__UpperCAmelCase						)
																					model.eval()

																					with torch.no_grad():
																												A      :					Optional[int]      		=					model(**self._prepare_for_class(__UpperCAmelCase    ,						__UpperCAmelCase						)						)

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

																					A      :					Dict      		=					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]    ,						)

														A      :					Dict      		=					self.model_tester.prepare_config_and_inputs_for_common()

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

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

																					check_hidden_states_output(__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)




							def       snake_case			(					self						)		->       List[Any]:
														A      :					Tuple      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase						)






							@slow
							def       snake_case			(					self						)		->       Tuple:
														for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					A      :					Optional[int]      		=					ConvNextModel.from_pretrained(__UpperCAmelCase						)
																					self.assertIsNotNone(__UpperCAmelCase						)






def        snake_case__		(    ):
							A      :					Union[str, Any]      		=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
							return image







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





							"""simple docstring"""

							@cached_property
							def       snake_case			(					self						)		->       List[str]:
														return AutoImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''						) if is_vision_available() else None






							@slow
							def       snake_case			(					self						)		->       Union[str, Any]:
														A      :					Tuple      		=					ConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''						).to(__UpperCAmelCase						)

														A      :					Optional[int]      		=					self.default_image_processor
														A      :					Tuple      		=					prepare_img()
														A      :					Optional[Any]      		=					image_processor(images=__UpperCAmelCase    ,						return_tensors='''pt'''						).to(__UpperCAmelCase						)

														# forward pass
														with torch.no_grad():
																					A      :					int      		=					model(**__UpperCAmelCase						)

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

														A      :					Dict      		=					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 					__lowercase  (						unittest.TestCase		,       _UpperCamelCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		int								=	(ConvNextBackbone,) if is_torch_available() else ()
							UpperCAmelCase_						:		int								=	ConvNextConfig

							UpperCAmelCase_						:		Dict								=	False




							def       snake_case			(					self						)		->       List[Any]:
														A      :					Union[str, Any]      		=					ConvNextModelTester(self						)











"""simple docstring"""




from manim import *






class       UpperCAmelCase_    (							_UpperCamelCase		):



      def 		snake_case_   (	self	:      Tuple		):
           _UpperCAmelCase     :      str														=       Rectangle(height=0.5	,      width=0.5		)
           _UpperCAmelCase     :      List[Any]														=       Rectangle(height=0.25	,      width=0.25		)
           _UpperCAmelCase     :      Tuple														=       Rectangle(height=0.46	,      width=0.46		).set_stroke(width=0		)

           _UpperCAmelCase     :      List[Any]														=       [mem.copy() for i in range(6		)]
           _UpperCAmelCase     :      List[str]														=       [mem.copy() for i in range(6		)]
           _UpperCAmelCase     :      int														=       VGroup(*A		).arrange(A	,      buff=0		)
           _UpperCAmelCase     :      Dict														=       VGroup(*A		).arrange(A	,      buff=0		)
           _UpperCAmelCase     :      int														=       VGroup(A	,      A		).arrange(A	,      buff=0		)
           _UpperCAmelCase     :      str														=       Text("CPU"	,      font_size=2_4		)
           _UpperCAmelCase     :      Union[str, Any]														=       Group(A	,      A		).arrange(A	,      buff=0.5	,      aligned_edge=A		)
           cpu.move_to([-2.5, -0.5, 0]		)
           self.add(A		)

           _UpperCAmelCase     :      Union[str, Any]														=       [mem.copy() for i in range(4		)]
           _UpperCAmelCase     :      Optional[int]														=       VGroup(*A		).arrange(A	,      buff=0		)
           _UpperCAmelCase     :      Any														=       Text("GPU"	,      font_size=2_4		)
           _UpperCAmelCase     :      Dict														=       Group(A	,      A		).arrange(A	,      buff=0.5	,      aligned_edge=A		)
           gpu.move_to([-1, -1, 0]		)
           self.add(A		)

           _UpperCAmelCase     :      Tuple														=       [mem.copy() for i in range(6		)]
           _UpperCAmelCase     :      str														=       VGroup(*A		).arrange(A	,      buff=0		)

           _UpperCAmelCase     :      Any														=       Text("Model"	,      font_size=2_4		)
           _UpperCAmelCase     :      Any														=       Group(A	,      A		).arrange(A	,      buff=0.5	,      aligned_edge=A		)
           model.move_to([3, -1.0, 0]		)
           self.add(A		)

           _UpperCAmelCase     :      List[str]														=       []
           _UpperCAmelCase     :      Optional[int]														=       []
           _UpperCAmelCase     :      str														=       []

           for i, rect in enumerate(A		):
                rect.set_stroke(A		)

                _UpperCAmelCase     :      Dict														=       Rectangle(height=0.46 / 4	,      width=0.46 / 3		).set_stroke(width=0.0		).set_fill(A	,      opacity=0.7		)

                if i == 0:
                     cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT		)	,      buff=0.02	,      direction=A		)
                     cpu_target.set_x(cpu_target.get_x() + 0.1		)
                elif i == 3:
                     cpu_target.next_to(model_cpu_arr[0]	,      direction=A	,      buff=0.0		)
                else:
                     cpu_target.next_to(model_cpu_arr[i - 1]	,      direction=A	,      buff=0.0		)
                self.add(A		)
                model_cpu_arr.append(A		)

           self.add(*A	,      *A	,      *A		)

           _UpperCAmelCase     :      Dict														=       [mem.copy() for i in range(6		)]
           _UpperCAmelCase     :      List[str]														=       VGroup(*A		).arrange(A	,      buff=0		)

           _UpperCAmelCase     :      List[str]														=       Text("Loaded Checkpoint"	,      font_size=2_4		)
           _UpperCAmelCase     :      Optional[int]														=       Group(A	,      A		).arrange(A	,      buff=0.5	,      aligned_edge=A		)
           checkpoint.move_to([3, 0.5, 0]		)

           self.add(A		)

           _UpperCAmelCase     :      Union[str, Any]														=       []
           _UpperCAmelCase     :      Union[str, Any]														=       []

           for i, rect in enumerate(A		):
                _UpperCAmelCase     :      str														=       fill.copy().set_fill(A	,      opacity=0.7		)
                target.move_to(A		)
                ckpt_arr.append(A		)

                _UpperCAmelCase     :      Dict														=       target.copy()
                if i < 5:
                     cpu_target.move_to(cpu_left_col_base[i + 1]		)
                else:
                     cpu_target.move_to(cpu_right_col_base[i - 5]		)
                ckpt_cpu_arr.append(A		)
           self.add(*A	,      *A		)

           _UpperCAmelCase     :      List[str]														=       Square(side_length=2.2		)
           key.move_to([-5, 2, 0]		)

           _UpperCAmelCase     :      List[Any]														=       MarkupText(
               f'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'	,      font_size=1_8	,      )

           key_text.move_to([-5, 2.4, 0]		)

           self.add(A	,      A		)

           _UpperCAmelCase     :      Tuple														=       MarkupText(
               f'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'	,      font_size=1_8	,      )

           blue_text.next_to(A	,      DOWN * 2.4	,      aligned_edge=key_text.get_left()		)
           self.add(A		)

           _UpperCAmelCase     :      Any														=       MarkupText(
               f'Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'	,      font_size=2_4	,      )
           step_a.move_to([2, 2, 0]		)

           _UpperCAmelCase     :      int														=       [meta_mem.copy() for i in range(6		)]
           _UpperCAmelCase     :      Optional[int]														=       [meta_mem.copy() for i in range(6		)]
           _UpperCAmelCase     :      List[str]														=       VGroup(*A		).arrange(A	,      buff=0		)
           _UpperCAmelCase     :      Optional[int]														=       VGroup(*A		).arrange(A	,      buff=0		)
           _UpperCAmelCase     :      List[Any]														=       VGroup(A	,      A		).arrange(A	,      buff=0		)
           _UpperCAmelCase     :      Union[str, Any]														=       Text("Disk"	,      font_size=2_4		)
           _UpperCAmelCase     :      int														=       Group(A	,      A		).arrange(A	,      buff=0.5	,      aligned_edge=A		)
           disk.move_to([-4.0, -1.25, 0]		)
           self.play(Write(A	,      run_time=3		)	,      Write(A	,      run_time=1		)	,      Create(A	,      run_time=1		)		)

           _UpperCAmelCase     :      List[Any]														=       []
           for i, rect in enumerate(A		):
                _UpperCAmelCase     :      Any														=       rect.copy()
                target.generate_target()
                target.target.move_to(disk_left_col_base[i]		).scale(0.5		)
                animations.append(MoveToTarget(A	,      run_time=1.5		)		)
           self.play(*A		)

           self.play(FadeOut(A		)		)

           _UpperCAmelCase     :      int														=       MarkupText(f'Then, the checkpoint is removed from memory\nthrough garbage collection.'	,      font_size=2_4		)
           step_a.move_to([2, 2, 0]		)

           self.play(Write(A	,      run_time=3		)		)

           self.play(
               FadeOut(A	,      A	,      *A	,      *A		)	,      )

           self.wait()
