






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   __SCREAMING_SNAKE_CASE:
							def __init__(     self:      Optional[int]       ,	UpperCamelCase:      List[Any]       ,	UpperCamelCase:      int=13       ,	UpperCamelCase:      int=[30, 30]       ,	UpperCamelCase:      Union[str, Any]=2       ,	UpperCamelCase:      Optional[Any]=3       ,	UpperCamelCase:      Dict=True       ,	UpperCamelCase:      Tuple=True       ,	UpperCamelCase:      int=32       ,	UpperCamelCase:      List[str]=5       ,	UpperCamelCase:      List[str]=4       ,	UpperCamelCase:      str=37       ,	UpperCamelCase:      Union[str, Any]="gelu"       ,	UpperCamelCase:      int=0.1       ,	UpperCamelCase:      Any=0.1       ,	UpperCamelCase:      Optional[int]=10       ,	UpperCamelCase:      Any=0.02       ,	UpperCamelCase:      Any=3       ,	UpperCamelCase:      Optional[Any]=None       ,	UpperCamelCase:      Optional[int]=8       ,	UpperCamelCase:      Optional[int]=10       ,	)		->		List[str]:
											snake_case__									=     parent
											snake_case__									=     batch_size
											snake_case__									=     image_size
											snake_case__									=     patch_size
											snake_case__									=     num_channels
											snake_case__									=     is_training
											snake_case__									=     use_labels
											snake_case__									=     hidden_size
											snake_case__									=     num_hidden_layers
											snake_case__									=     num_attention_heads
											snake_case__									=     intermediate_size
											snake_case__									=     hidden_act
											snake_case__									=     hidden_dropout_prob
											snake_case__									=     attention_probs_dropout_prob
											snake_case__									=     type_sequence_label_size
											snake_case__									=     initializer_range
											snake_case__									=     num_labels
											snake_case__									=     scope
											snake_case__									=     n_targets
											snake_case__									=     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
											snake_case__									=     (image_size[1] // patch_size) * (image_size[0] // patch_size)
											snake_case__									=     num_patches + 1 + self.num_detection_tokens
							def lowerCAmelCase_ (     self:      Tuple						)		->		List[str]:
											snake_case__									=     floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]						)

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

											snake_case__									=     self.get_config()

											return config, pixel_values, labels
							def lowerCAmelCase_ (     self:      List[str]						)		->		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=UpperCamelCase       ,	initializer_range=self.initializer_range       ,	num_detection_tokens=self.num_detection_tokens       ,	num_labels=self.num_labels       ,	)
							def lowerCAmelCase_ (     self:      str       ,	UpperCamelCase:      int       ,	UpperCamelCase:      List[str]       ,	UpperCamelCase:      Optional[Any]						)		->		List[str]:
											snake_case__									=     YolosModel(config=UpperCamelCase						)
											model.to(UpperCamelCase						)
											model.eval()
											snake_case__									=     model(UpperCamelCase						)
											self.parent.assertEqual(
											    result.last_hidden_state.shape       ,	(self.batch_size, self.expected_seq_len, self.hidden_size)						)
							def lowerCAmelCase_ (     self:      Tuple       ,	UpperCamelCase:      int       ,	UpperCamelCase:      Tuple       ,	UpperCamelCase:      List[Any]						)		->		Optional[int]:
											snake_case__									=     YolosForObjectDetection(UpperCamelCase						)
											model.to(UpperCamelCase						)
											model.eval()

											snake_case__									=     model(pixel_values=UpperCamelCase						)
											snake_case__									=     model(UpperCamelCase						)

											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)						)

											snake_case__									=     model(pixel_values=UpperCamelCase       ,	labels=UpperCamelCase						)

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


@require_torch
class   __SCREAMING_SNAKE_CASE(	a_    ,  a_    ,  unittest.TestCase	):
							_UpperCAmelCase     =       (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
							_UpperCAmelCase     =       (
							    {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {}
							)

							_UpperCAmelCase     =       False
							_UpperCAmelCase     =       False
							_UpperCAmelCase     =       False
							_UpperCAmelCase     =       False
							def lowerCAmelCase_ (     self:      str       ,	UpperCamelCase:      Dict       ,	UpperCamelCase:      int       ,	UpperCamelCase:      str=False						)		->		List[str]:
											snake_case__									=     super()._prepare_for_class(UpperCamelCase       ,	UpperCamelCase       ,	return_labels=UpperCamelCase						)

											if return_labels:
															if model_class.__name__ == "YolosForObjectDetection":
																			snake_case__									=     []
																			for i in range(self.model_tester.batch_size						):
																							snake_case__									=     {}
																							snake_case__									=     torch.ones(
																							    size=(self.model_tester.n_targets,)       ,	device=UpperCamelCase       ,	dtype=torch.long						)
																							snake_case__									=     torch.ones(
																							    self.model_tester.n_targets       ,	4       ,	device=UpperCamelCase       ,	dtype=torch.float						)
																							labels.append(UpperCamelCase						)
																			snake_case__									=     labels

											return inputs_dict
							def lowerCAmelCase_ (     self:      Union[str, Any]						)		->		Dict:
											snake_case__									=     YolosModelTester(self						)
											snake_case__									=     ConfigTester(self       ,	config_class=UpperCamelCase       ,	has_text_modality=UpperCamelCase       ,	hidden_size=37						)
							def lowerCAmelCase_ (     self:      Any						)		->		Optional[Any]:
											self.config_tester.run_common_tests()
							def lowerCAmelCase_ (     self:      Dict						)		->		Optional[Any]:
											# YOLOS does not use inputs_embeds
											pass
							def lowerCAmelCase_ (     self:      str						)		->		int:
											snake_case__							,						snake_case__									=     self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															snake_case__									=     model_class(UpperCamelCase						)
															self.assertIsInstance(model.get_input_embeddings()       ,	(nn.Module)						)
															snake_case__									=     model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(UpperCamelCase       ,	nn.Linear						)						)
							def lowerCAmelCase_ (     self:      Optional[int]						)		->		Optional[int]:
											snake_case__							,						snake_case__									=     self.model_tester.prepare_config_and_inputs_for_common()

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

															snake_case__									=     ['pixel_values']
															self.assertListEqual(arg_names[:1]       ,	UpperCamelCase						)
							def lowerCAmelCase_ (     self:      Optional[int]						)		->		Tuple:
											snake_case__									=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCamelCase						)
							def lowerCAmelCase_ (     self:      Optional[int]						)		->		Union[str, Any]:
											snake_case__							,						snake_case__									=     self.model_tester.prepare_config_and_inputs_for_common()
											snake_case__									=     True

											# in YOLOS, the seq_len is different
											snake_case__									=     self.model_tester.expected_seq_len
											for model_class in self.all_model_classes:
															snake_case__									=     True
															snake_case__									=     False
															snake_case__									=     True
															snake_case__									=     model_class(UpperCamelCase						)
															model.to(UpperCamelCase						)
															model.eval()
															with torch.no_grad():
																			snake_case__									=     model(**self._prepare_for_class(UpperCamelCase       ,	UpperCamelCase						)						)
															snake_case__									=     outputs.attentions
															self.assertEqual(len(UpperCamelCase						)       ,	self.model_tester.num_hidden_layers						)

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

															self.assertListEqual(
															    list(attentions[0].shape[-3:]						)       ,	[self.model_tester.num_attention_heads, seq_len, seq_len]       ,	)
															snake_case__									=     len(UpperCamelCase						)

															# Check attention is always last and order is fine
															snake_case__									=     True
															snake_case__									=     True
															snake_case__									=     model_class(UpperCamelCase						)
															model.to(UpperCamelCase						)
															model.eval()
															with torch.no_grad():
																			snake_case__									=     model(**self._prepare_for_class(UpperCamelCase       ,	UpperCamelCase						)						)

															snake_case__									=     1
															self.assertEqual(out_len + added_hidden_states       ,	len(UpperCamelCase						)						)

															snake_case__									=     outputs.attentions

															self.assertEqual(len(UpperCamelCase						)       ,	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 lowerCAmelCase_ (     self:      List[str]						)		->		Optional[int]:
											def check_hidden_states_output(UpperCamelCase:      Optional[int]       ,	UpperCamelCase:      Union[str, Any]       ,	UpperCamelCase:      Optional[int]						):
															snake_case__									=     model_class(UpperCamelCase						)
															model.to(UpperCamelCase						)
															model.eval()

															with torch.no_grad():
																			snake_case__									=     model(**self._prepare_for_class(UpperCamelCase       ,	UpperCamelCase						)						)

															snake_case__									=     outputs.hidden_states

															snake_case__									=     getattr(
															    self.model_tester       ,	'expected_num_hidden_layers'       ,	self.model_tester.num_hidden_layers + 1						)
															self.assertEqual(len(UpperCamelCase						)       ,	UpperCamelCase						)

															# YOLOS has a different seq_length
															snake_case__									=     self.model_tester.expected_seq_len

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

											snake_case__							,						snake_case__									=     self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															snake_case__									=     True
															check_hidden_states_output(UpperCamelCase       ,	UpperCamelCase       ,	UpperCamelCase						)

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

															check_hidden_states_output(UpperCamelCase       ,	UpperCamelCase       ,	UpperCamelCase						)
							def lowerCAmelCase_ (     self:      Optional[Any]						)		->		Optional[Any]:
											snake_case__									=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_object_detection(*UpperCamelCase						)


							@slow
							def lowerCAmelCase_ (     self:      int						)		->		str:
											for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case__									=     YolosModel.from_pretrained(UpperCamelCase						)
															self.assertIsNotNone(UpperCamelCase						)


def      a_ (      )			->			Tuple:







				"""simple docstring"""
				snake_case__									=     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
				return image


@require_torch
@require_vision
class   __SCREAMING_SNAKE_CASE(	unittest.TestCase	):
							@cached_property
							def lowerCAmelCase_ (     self:      Tuple						)		->		str:
											return AutoImageProcessor.from_pretrained('hustvl/yolos-small'						) if is_vision_available() else None


							@slow
							def lowerCAmelCase_ (     self:      Optional[Any]						)		->		Optional[int]:
											snake_case__									=     YolosForObjectDetection.from_pretrained('hustvl/yolos-small'						).to(UpperCamelCase						)

											snake_case__									=     self.default_image_processor
											snake_case__									=     prepare_img()
											snake_case__									=     image_processor(images=UpperCamelCase       ,	return_tensors='pt'						).to(UpperCamelCase						)

											# forward pass
											with torch.no_grad():
															snake_case__									=     model(inputs.pixel_values						)

											# verify outputs
											snake_case__									=     torch.Size((1, 1_00, 92)						)
											self.assertEqual(outputs.logits.shape       ,	UpperCamelCase						)

											snake_case__									=     torch.tensor(
											    [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]]       ,	device=UpperCamelCase       ,	)
											snake_case__									=     torch.tensor(
											    [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]]       ,	device=UpperCamelCase						)
											self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]       ,	UpperCamelCase       ,	atol=1e-4						)						)
											self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]       ,	UpperCamelCase       ,	atol=1e-4						)						)

											# verify postprocessing
											snake_case__									=     image_processor.post_process_object_detection(
											    UpperCamelCase       ,	threshold=0.3       ,	target_sizes=[image.size[::-1]]						)[0]
											snake_case__									=     torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861]						).to(UpperCamelCase						)
											snake_case__									=     [75, 75, 17, 63, 17]
											snake_case__									=     torch.tensor([335.0_609, 79.3_848, 375.4_216, 187.2_495]						).to(UpperCamelCase						)

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












def      a_ (      _A      ,   _A  )			->			float:







				"""simple docstring"""
				if digit_amount > 0:
								return round(number - int(_A  )      ,   _A  )
				return number - int(_A  )


if __name__ == "__main__":
							print(decimal_isolate(1.5_3, 0))
							print(decimal_isolate(3_5.3_4_5, 1))
							print(decimal_isolate(3_5.3_4_5, 2))
							print(decimal_isolate(3_5.3_4_5, 3))
							print(decimal_isolate(-1_4.7_8_9, 3))
							print(decimal_isolate(0, 2))
							print(decimal_isolate(-1_4.1_2_3, 1))
							print(decimal_isolate(-1_4.1_2_3, 2))
							print(decimal_isolate(-1_4.1_2_3, 3))




