


'''simple docstring'''






import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 CvtForImageClassification, CvtModel
		from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor




class 	a__       (							a__						):





							'''simple docstring'''






							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			self.config_class(**self.inputs_dict     )
														self.parent.assertTrue(hasattr(lowerCamelCase_      ,					'''embed_dim'''     )     )
														self.parent.assertTrue(hasattr(lowerCamelCase_      ,					'''num_heads'''     )     )




class 	a__       :





							'''simple docstring'''






							def __init__(	self      ,					lowerCamelCase_      ,					lowerCamelCase_=13      ,					lowerCamelCase_=64      ,					lowerCamelCase_=3      ,					lowerCamelCase_=[16, 48, 96]      ,					lowerCamelCase_=[1, 3, 6]      ,					lowerCamelCase_=[1, 2, 10]      ,					lowerCamelCase_=[7, 3, 3]      ,					lowerCamelCase_=[4, 2, 2]      ,					lowerCamelCase_=[2, 1, 1]      ,					lowerCamelCase_=[2, 2, 2]      ,					lowerCamelCase_=[False, False, True]      ,					lowerCamelCase_=[0.0, 0.0, 0.0]      ,					lowerCamelCase_=0.02      ,					lowerCamelCase_=1e-12      ,					lowerCamelCase_=True      ,					lowerCamelCase_=True      ,					lowerCamelCase_=2      ,					) ->							Optional[int]:
														lowerCAmelCase__      =			parent
														lowerCAmelCase__      =			batch_size
														lowerCAmelCase__      =			image_size
														lowerCAmelCase__      =			patch_sizes
														lowerCAmelCase__      =			patch_stride
														lowerCAmelCase__      =			patch_padding
														lowerCAmelCase__      =			is_training
														lowerCAmelCase__      =			use_labels
														lowerCAmelCase__      =			num_labels
														lowerCAmelCase__      =			num_channels
														lowerCAmelCase__      =			embed_dim
														lowerCAmelCase__      =			num_heads
														lowerCAmelCase__      =			stride_kv
														lowerCAmelCase__      =			depth
														lowerCAmelCase__      =			cls_token
														lowerCAmelCase__      =			attention_drop_rate
														lowerCAmelCase__      =			initializer_range
														lowerCAmelCase__      =			layer_norm_eps







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__      =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

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







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														return CvtConfig(
														    image_size=self.image_size      ,					num_labels=self.num_labels      ,					num_channels=self.num_channels      ,					embed_dim=self.embed_dim      ,					num_heads=self.num_heads      ,					patch_sizes=self.patch_sizes      ,					patch_padding=self.patch_padding      ,					patch_stride=self.patch_stride      ,					stride_kv=self.stride_kv      ,					depth=self.depth      ,					cls_token=self.cls_token      ,					attention_drop_rate=self.attention_drop_rate      ,					initializer_range=self.initializer_range      ,					)







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Optional[Any]:
														lowerCAmelCase__      =			CvtModel(config=lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()
														lowerCAmelCase__      =			model(lowerCamelCase_     )
														lowerCAmelCase__      =			(self.image_size, self.image_size)
														lowerCAmelCase__     ,      lowerCAmelCase__      =			image_size[0], image_size[1]
														for i in range(len(self.depth     )     ):
																					lowerCAmelCase__      =			floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1     )
																					lowerCAmelCase__      =			floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1     )
														self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.embed_dim[-1], height, width)     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Tuple:
														lowerCAmelCase__      =			self.num_labels
														lowerCAmelCase__      =			CvtForImageClassification(lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					labels=lowerCamelCase_     )
														self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_labels)     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														lowerCAmelCase__      =			self.prepare_config_and_inputs()
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			config_and_inputs
														lowerCAmelCase__      =			{'''pixel_values''': pixel_values}
														return config, inputs_dict




@require_torch
class 	a__       (							a__       ,       a__       ,       unittest.TestCase						):





							'''simple docstring'''






							lowercase__    :     Tuple					=   (CvtModel, CvtForImageClassification) if is_torch_available() else ()
							lowercase__    :     List[Any]					=   (
							    {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
							    if is_torch_available()
							    else {}
							)

							lowercase__    :     Optional[Any]					=   False
							lowercase__    :     Union[str, Any]					=   False
							lowercase__    :     List[Any]					=   False
							lowercase__    :     Dict					=   False
							lowercase__    :     List[Any]					=   False







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														lowerCAmelCase__      =			CvtModelTester(self     )
														lowerCAmelCase__      =			ConfigTester(self      ,					config_class=lowerCamelCase_      ,					has_text_modality=lowerCamelCase_      ,					hidden_size=37     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														return







							@unittest.skip(reason='''Cvt does not output attentions'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														pass







							@unittest.skip(reason='''Cvt does not use inputs_embeds'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														pass







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







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_common()

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

																					lowerCAmelCase__      =			['''pixel_values''']
																					self.assertListEqual(arg_names[:1]      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														def check_hidden_states_output(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ):
																					lowerCAmelCase__      =			model_class(lowerCamelCase_     )
																					model.to(lowerCamelCase_     )
																					model.eval()

																					with torch.no_grad():
																												lowerCAmelCase__      =			model(**self._prepare_for_class(lowerCamelCase_      ,					lowerCamelCase_     )     )

																					lowerCAmelCase__      =			outputs.hidden_states

																					lowerCAmelCase__      =			len(self.model_tester.depth     )
																					self.assertEqual(len(lowerCamelCase_     )      ,					lowerCamelCase_     )

																					# verify the first hidden states (first block)
																					self.assertListEqual(
																					    list(hidden_states[0].shape[-3:]     )      ,					[
																					        self.model_tester.embed_dim[0],
																					        self.model_tester.image_size // 4,
																					        self.model_tester.image_size // 4,
																					    ]      ,					)

														lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__      =			True
																					check_hidden_states_output(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )

																					# check that output_hidden_states also work using config
																					del inputs_dict["output_hidden_states"]
																					lowerCAmelCase__      =			True

																					check_hidden_states_output(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_     )







							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														pass







							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCAmelCase__      =			CvtModel.from_pretrained(lowerCamelCase_     )
																					self.assertIsNotNone(lowerCamelCase_     )


def       _snake_case     (  )		->					List[Any]:
							lowerCAmelCase__      =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
							return image






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





							'''simple docstring'''






							@cached_property
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]     )







							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														lowerCAmelCase__      =			CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]     ).to(lowerCamelCase_     )

														lowerCAmelCase__      =			self.default_image_processor
														lowerCAmelCase__      =			prepare_img()
														lowerCAmelCase__      =			image_processor(images=lowerCamelCase_      ,					return_tensors='''pt'''     ).to(lowerCamelCase_     )

														# forward pass
														with torch.no_grad():
																					lowerCAmelCase__      =			model(**lowerCamelCase_     )

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

														lowerCAmelCase__      =			torch.tensor([0.9_285, 0.9_015, -0.3_150]     ).to(lowerCamelCase_     )

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



'''simple docstring'''






from manim import *




class 	a__       (							a__						):





							'''simple docstring'''






							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__      =			Rectangle(height=0.5      ,					width=0.5     )
														lowerCAmelCase__      =			Rectangle(height=0.46      ,					width=0.46     ).set_stroke(width=0     )

														lowerCAmelCase__      =			[mem.copy() for i in range(6     )]
														lowerCAmelCase__      =			[mem.copy() for i in range(6     )]
														lowerCAmelCase__      =			VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )
														lowerCAmelCase__      =			VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )
														lowerCAmelCase__      =			VGroup(lowerCamelCase_      ,					lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )
														lowerCAmelCase__      =			Text('''CPU'''      ,					font_size=24     )
														lowerCAmelCase__      =			Group(lowerCamelCase_      ,					lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0.5      ,					aligned_edge=lowerCamelCase_     )
														cpu.move_to([-2.5, -0.5, 0]     )
														self.add(lowerCamelCase_     )

														lowerCAmelCase__      =			[mem.copy() for i in range(1     )]
														lowerCAmelCase__      =			VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )
														lowerCAmelCase__      =			Text('''GPU'''      ,					font_size=24     )
														lowerCAmelCase__      =			Group(lowerCamelCase_      ,					lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0.5      ,					aligned_edge=lowerCamelCase_     )
														gpu.align_to(lowerCamelCase_      ,					lowerCamelCase_     )
														gpu.set_x(gpu.get_x() - 1     )

														self.add(lowerCamelCase_     )

														lowerCAmelCase__      =			[mem.copy() for i in range(6     )]
														lowerCAmelCase__      =			VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )

														lowerCAmelCase__      =			Text('''Model'''      ,					font_size=24     )
														lowerCAmelCase__      =			Group(lowerCamelCase_      ,					lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0.5      ,					aligned_edge=lowerCamelCase_     )
														model.move_to([3, -1.0, 0]     )

														self.play(
														    Create(lowerCamelCase_      ,					run_time=1     )      ,					Create(lowerCamelCase_      ,					run_time=1     )      ,					Create(lowerCamelCase_      ,					run_time=1     )      ,					)

														lowerCAmelCase__      =			MarkupText(
														    F"""First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM."""      ,					font_size=24      ,					)

														lowerCAmelCase__      =			Square(side_length=2.2     )
														key.move_to([-5, 2, 0]     )

														lowerCAmelCase__      =			MarkupText(
														    F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""      ,					font_size=18      ,					)

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

														step_a.move_to([2, 2, 0]     )
														self.play(Write(lowerCamelCase_      ,					run_time=2.5     )      ,					Write(lowerCamelCase_     )      ,					Write(lowerCamelCase_     )     )

														self.add(lowerCamelCase_     )

														lowerCAmelCase__      =			[]
														lowerCAmelCase__      =			[]
														lowerCAmelCase__      =			[]
														for i, rect in enumerate(lowerCamelCase_     ):
																					lowerCAmelCase__      =			Rectangle(height=0.46      ,					width=0.46     ).set_stroke(width=0.0     ).set_fill(lowerCamelCase_      ,					opacity=0.7     )
																					cpu_target.move_to(lowerCamelCase_     )
																					cpu_target.generate_target()
																					lowerCAmelCase__      =			0.46 / 4
																					lowerCAmelCase__      =			0.46 / 3

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

																					first_animations.append(rect.animate(run_time=0.5     ).set_stroke(lowerCamelCase_     )     )
																					second_animations.append(MoveToTarget(lowerCamelCase_      ,					run_time=1.5     )     )

														self.play(*lowerCamelCase_     )
														self.play(*lowerCamelCase_     )

														self.wait()