


'''simple docstring'''
import inspect
import unittest

from transformers import YolosConfig
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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch
					from torch import nn

					from transformers import YolosForObjectDetection, YolosModel
					from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor




class        _UpperCamelCase	:


				'''simple docstring'''


				def __init__(     self							,      a_							,      a_=1_3							,      a_=[3_0, 3_0]							,      a_=2							,      a_=3							,      a_=True							,      a_=True							,      a_=3_2							,      a_=5							,      a_=4							,      a_=3_7							,      a_="gelu"							,      a_=0.1							,      a_=0.1							,      a_=1_0							,      a_=0.02							,      a_=3							,      a_=None							,      a_=8							,      a_=1_0							,      )    ->  int:
					lowercase						:		Any   					=		parent
					lowercase						:		Union[str, Any]   					=		batch_size
					lowercase						:		Optional[Any]   					=		image_size
					lowercase						:		int   					=		patch_size
					lowercase						:		Any   					=		num_channels
					lowercase						:		int   					=		is_training
					lowercase						:		Optional[int]   					=		use_labels
					lowercase						:		Union[str, Any]   					=		hidden_size
					lowercase						:		str   					=		num_hidden_layers
					lowercase						:		List[str]   					=		num_attention_heads
					lowercase						:		Any   					=		intermediate_size
					lowercase						:		List[str]   					=		hidden_act
					lowercase						:		Union[str, Any]   					=		hidden_dropout_prob
					lowercase						:		List[str]   					=		attention_probs_dropout_prob
					lowercase						:		List[str]   					=		type_sequence_label_size
					lowercase						:		Union[str, Any]   					=		initializer_range
					lowercase						:		Any   					=		num_labels
					lowercase						:		Optional[Any]   					=		scope
					lowercase						:		Optional[int]   					=		n_targets
					lowercase						:		Any   					=		num_detection_tokens
					# we set the expected sequence length (which is used in several tests)
					# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
					lowercase						:		str   					=		(image_size[1] // patch_size) * (image_size[0] // patch_size)
					lowercase						:		Any   					=		num_patches + 1 + self.num_detection_tokens

				def 	a__		(     self							)    ->  int:
					lowercase						:		Union[str, Any]   					=		floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]							)

					lowercase						:		List[str]   					=		None
					if self.use_labels:
						# labels is a list of Dict (each Dict being the labels for a given example in the batch)
						lowercase						:		Any   					=		[]
						for i in range(self.batch_size							):
							lowercase						:		List[str]   					=		{}
							lowercase						:		Union[str, Any]   					=		torch.randint(
							    high=self.num_labels							,      size=(self.n_targets,)							,      device=a_							)
							lowercase						:		Dict   					=		torch.rand(self.n_targets							,      4							,      device=a_							)
							labels.append(a_							)

					lowercase						:		str   					=		self.get_config()

					return config, pixel_values, labels

				def 	a__		(     self							)    ->  Optional[Any]:
					return YolosConfig(
					    image_size=self.image_size							,      patch_size=self.patch_size							,      num_channels=self.num_channels							,      hidden_size=self.hidden_size							,      num_hidden_layers=self.num_hidden_layers							,      num_attention_heads=self.num_attention_heads							,      intermediate_size=self.intermediate_size							,      hidden_act=self.hidden_act							,      hidden_dropout_prob=self.hidden_dropout_prob							,      attention_probs_dropout_prob=self.attention_probs_dropout_prob							,      is_decoder=a_							,      initializer_range=self.initializer_range							,      num_detection_tokens=self.num_detection_tokens							,      num_labels=self.num_labels							,      )

				def 	a__		(     self							,      a_							,      a_							,      a_							)    ->  List[str]:
					lowercase						:		Dict   					=		YolosModel(config=a_							)
					model.to(a_							)
					model.eval()
					lowercase						:		Optional[Any]   					=		model(a_							)
					self.parent.assertEqual(
					    result.last_hidden_state.shape							,      (self.batch_size, self.expected_seq_len, self.hidden_size)							)

				def 	a__		(     self							,      a_							,      a_							,      a_							)    ->  int:
					lowercase						:		Any   					=		YolosForObjectDetection(a_							)
					model.to(a_							)
					model.eval()

					lowercase						:		Any   					=		model(pixel_values=a_							)
					lowercase						:		Tuple   					=		model(a_							)

					self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_detection_tokens, self.num_labels + 1)							)
					self.parent.assertEqual(result.pred_boxes.shape							,      (self.batch_size, self.num_detection_tokens, 4)							)

					lowercase						:		Tuple   					=		model(pixel_values=a_							,      labels=a_							)

					self.parent.assertEqual(result.loss.shape							,      ()							)
					self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_detection_tokens, self.num_labels + 1)							)
					self.parent.assertEqual(result.pred_boxes.shape							,      (self.batch_size, self.num_detection_tokens, 4)							)


				def 	a__		(     self							)    ->  Optional[int]:
					lowercase						:		Any   					=		self.prepare_config_and_inputs()
					lowercase					,		lowercase					,		lowercase						:		int   					=		config_and_inputs
					lowercase						:		Dict   					=		{"pixel_values": pixel_values}
					return config, inputs_dict




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


				'''simple docstring'''


				_snake_case						       =				(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
				_snake_case						       =				(
				    {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {}
				)

				_snake_case						       =				False
				_snake_case						       =				False
				_snake_case						       =				False
				_snake_case						       =				False

				def 	a__		(     self							,      a_							,      a_							,      a_=False							)    ->  Tuple:
					lowercase						:		Optional[int]   					=		super()._prepare_for_class(a_							,      a_							,      return_labels=a_							)

					if return_labels:
						if model_class.__name__ == "YolosForObjectDetection":
							lowercase						:		List[str]   					=		[]
							for i in range(self.model_tester.batch_size							):
								lowercase						:		Optional[int]   					=		{}
								lowercase						:		Optional[Any]   					=		torch.ones(
								    size=(self.model_tester.n_targets,)							,      device=a_							,      dtype=torch.long							)
								lowercase						:		List[Any]   					=		torch.ones(
								    self.model_tester.n_targets							,      4							,      device=a_							,      dtype=torch.float							)
								labels.append(a_							)
							lowercase						:		Union[str, Any]   					=		labels

					return inputs_dict

				def 	a__		(     self							)    ->  List[str]:
					lowercase						:		Union[str, Any]   					=		YolosModelTester(self							)
					lowercase						:		int   					=		ConfigTester(self							,      config_class=a_							,      has_text_modality=a_							,      hidden_size=3_7							)

				def 	a__		(     self							)    ->  Optional[Any]:
					self.config_tester.run_common_tests()

				def 	a__		(     self							)    ->  str:
					# YOLOS does not use inputs_embeds
					pass

				def 	a__		(     self							)    ->  List[Any]:
					lowercase					,		lowercase						:		Any   					=		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						lowercase						:		str   					=		model_class(a_							)
						self.assertIsInstance(model.get_input_embeddings()							,      (nn.Module)							)
						lowercase						:		Dict   					=		model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(a_							,      nn.Linear							)							)

				def 	a__		(     self							)    ->  Any:
					lowercase					,		lowercase						:		Optional[int]   					=		self.model_tester.prepare_config_and_inputs_for_common()

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

						lowercase						:		List[Any]   					=		["pixel_values"]
						self.assertListEqual(arg_names[:1]							,      a_							)

				def 	a__		(     self							)    ->  Dict:
					lowercase						:		Dict   					=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*a_							)

				def 	a__		(     self							)    ->  Dict:
					lowercase					,		lowercase						:		Any   					=		self.model_tester.prepare_config_and_inputs_for_common()
					lowercase						:		int   					=		True

					# in YOLOS, the seq_len is different
					lowercase						:		int   					=		self.model_tester.expected_seq_len
					for model_class in self.all_model_classes:
						lowercase						:		List[str]   					=		True
						lowercase						:		Optional[Any]   					=		False
						lowercase						:		Optional[Any]   					=		True
						lowercase						:		List[Any]   					=		model_class(a_							)
						model.to(a_							)
						model.eval()
						with torch.no_grad():
							lowercase						:		str   					=		model(**self._prepare_for_class(a_							,      a_							)							)
						lowercase						:		List[Any]   					=		outputs.attentions
						self.assertEqual(len(a_							)							,      self.model_tester.num_hidden_layers							)

						# check that output_attentions also work using config
						del inputs_dict["output_attentions"]
						lowercase						:		Optional[Any]   					=		True
						lowercase						:		Optional[int]   					=		model_class(a_							)
						model.to(a_							)
						model.eval()
						with torch.no_grad():
							lowercase						:		Optional[Any]   					=		model(**self._prepare_for_class(a_							,      a_							)							)
						lowercase						:		List[str]   					=		outputs.attentions
						self.assertEqual(len(a_							)							,      self.model_tester.num_hidden_layers							)

						self.assertListEqual(
						    list(attentions[0].shape[-3:]							)							,      [self.model_tester.num_attention_heads, seq_len, seq_len]							,      )
						lowercase						:		Optional[int]   					=		len(a_							)

						# Check attention is always last and order is fine
						lowercase						:		str   					=		True
						lowercase						:		List[Any]   					=		True
						lowercase						:		Any   					=		model_class(a_							)
						model.to(a_							)
						model.eval()
						with torch.no_grad():
							lowercase						:		str   					=		model(**self._prepare_for_class(a_							,      a_							)							)

						lowercase						:		Dict   					=		1
						self.assertEqual(out_len + added_hidden_states							,      len(a_							)							)

						lowercase						:		Optional[int]   					=		outputs.attentions

						self.assertEqual(len(a_							)							,      self.model_tester.num_hidden_layers							)
						self.assertListEqual(
						    list(self_attentions[0].shape[-3:]							)							,      [self.model_tester.num_attention_heads, seq_len, seq_len]							,      )

				def 	a__		(     self							)    ->  List[Any]:
					def check_hidden_states_output(a_							,      a_							,      a_							):
						lowercase						:		int   					=		model_class(a_							)
						model.to(a_							)
						model.eval()

						with torch.no_grad():
							lowercase						:		Optional[Any]   					=		model(**self._prepare_for_class(a_							,      a_							)							)

						lowercase						:		Tuple   					=		outputs.hidden_states

						lowercase						:		str   					=		getattr(
						    self.model_tester							,      "expected_num_hidden_layers"							,      self.model_tester.num_hidden_layers + 1							)
						self.assertEqual(len(a_							)							,      a_							)

						# YOLOS has a different seq_length
						lowercase						:		str   					=		self.model_tester.expected_seq_len

						self.assertListEqual(
						    list(hidden_states[0].shape[-2:]							)							,      [seq_length, self.model_tester.hidden_size]							,      )

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

					for model_class in self.all_model_classes:
						lowercase						:		Any   					=		True
						check_hidden_states_output(a_							,      a_							,      a_							)

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

						check_hidden_states_output(a_							,      a_							,      a_							)

				def 	a__		(     self							)    ->  str:
					lowercase						:		Optional[int]   					=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_object_detection(*a_							)


				@slow
				def 	a__		(     self							)    ->  Optional[Any]:
					for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase						:		Optional[Any]   					=		YolosModel.from_pretrained(a_							)
						self.assertIsNotNone(a_							)




def 	_A  (       )  ->				str:
	lowercase						:		Any   					=		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
	return image






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


				'''simple docstring'''


				@cached_property
				def 	a__		(     self							)    ->  Optional[int]:
					return AutoImageProcessor.from_pretrained("hustvl/yolos-small"							) if is_vision_available() else None


				@slow
				def 	a__		(     self							)    ->  str:
					lowercase						:		str   					=		YolosForObjectDetection.from_pretrained("hustvl/yolos-small"							).to(a_							)

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

					# forward pass
					with torch.no_grad():
						lowercase						:		List[str]   					=		model(inputs.pixel_values							)

					# verify outputs
					lowercase						:		Tuple   					=		torch.Size((1, 1_0_0, 9_2)							)
					self.assertEqual(outputs.logits.shape							,      a_							)

					lowercase						:		Optional[int]   					=		torch.tensor(
					    [[-24.02_48, -10.30_24, -14.82_90], [-42.03_92, -16.82_00, -27.43_34], [-27.27_43, -11.81_54, -18.71_48]]							,      device=a_							,      )
					lowercase						:		Dict   					=		torch.tensor(
					    [[0.25_59, 0.54_55, 0.47_06], [0.29_89, 0.72_79, 0.18_75], [0.77_32, 0.40_17, 0.44_62]]							,      device=a_							)
					self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]							,      a_							,      atol=1e-4							)							)
					self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]							,      a_							,      atol=1e-4							)							)

					# verify postprocessing
					lowercase						:		Optional[int]   					=		image_processor.post_process_object_detection(
					    a_							,      threshold=0.3							,      target_sizes=[image.size[::-1]]							)[0]
					lowercase						:		Dict   					=		torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61]							).to(a_							)
					lowercase						:		Any   					=		[7_5, 7_5, 1_7, 6_3, 1_7]
					lowercase						:		Optional[Any]   					=		torch.tensor([3_35.06_09, 79.38_48, 3_75.42_16, 1_87.24_95]							).to(a_							)

					self.assertEqual(len(results["scores"]							)							,      5							)
					self.assertTrue(torch.allclose(results["scores"]							,      a_							,      atol=1e-4							)							)
					self.assertSequenceEqual(results["labels"].tolist()							,      a_							)
					self.assertTrue(torch.allclose(results["boxes"][0, :]							,      a_							)							)






'''simple docstring'''
from manim import *




class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE):


				'''simple docstring'''


				def 	a__		(     self							)    ->  List[str]:
					lowercase						:		List[Any]   					=		Rectangle(height=0.5							,      width=0.5							)
					lowercase						:		str   					=		Rectangle(height=0.25							,      width=0.25							)
					lowercase						:		Union[str, Any]   					=		Rectangle(height=0.46							,      width=0.46							).set_stroke(width=0							)

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

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

					lowercase						:		Tuple   					=		[mem.copy() for i in range(6							)]
					lowercase						:		Optional[int]   					=		VGroup(*a_							).arrange(a_							,      buff=0							)

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

					lowercase						:		Dict   					=		[]
					lowercase						:		Tuple   					=		[]
					lowercase						:		List[Any]   					=		[]

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

						lowercase						:		Tuple   					=		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_							)

					lowercase						:		Any   					=		[mem.copy() for i in range(6							)]
					lowercase						:		Dict   					=		VGroup(*a_							).arrange(a_							,      buff=0							)

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

					self.add(a_							)

					lowercase						:		Any   					=		[]
					lowercase						:		int   					=		[]

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

						lowercase						:		Optional[Any]   					=		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_							)

					lowercase						:		Optional[Any]   					=		Square(side_length=2.2							)
					key.move_to([-5, 2, 0]							)

					lowercase						:		str   					=		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_							)

					lowercase						:		Any   					=		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_							)

					lowercase						:		List[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]							)

					lowercase						:		Any   					=		[meta_mem.copy() for i in range(6							)]
					lowercase						:		Dict   					=		[meta_mem.copy() for i in range(6							)]
					lowercase						:		Union[str, Any]   					=		VGroup(*a_							).arrange(a_							,      buff=0							)
					lowercase						:		Dict   					=		VGroup(*a_							).arrange(a_							,      buff=0							)
					lowercase						:		Any   					=		VGroup(a_							,      a_							).arrange(a_							,      buff=0							)
					lowercase						:		Optional[Any]   					=		Text("Disk"							,      font_size=2_4							)
					lowercase						:		List[str]   					=		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							)							)

					lowercase						:		Optional[Any]   					=		[]
					for i, rect in enumerate(a_							):
						lowercase						:		int   					=		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_							)							)

					lowercase						:		List[Any]   					=		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()


