"""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   ,      snake_case_   ,      snake_case_=13   ,      snake_case_=[30, 30]   ,      snake_case_=2   ,      snake_case_=3   ,      snake_case_=True   ,      snake_case_=True   ,      snake_case_=32   ,      snake_case_=5   ,      snake_case_=4   ,      snake_case_=37   ,      snake_case_="gelu"   ,      snake_case_=0.1   ,      snake_case_=0.1   ,      snake_case_=10   ,      snake_case_=0.02   ,      snake_case_=3   ,      snake_case_=None   ,      snake_case_=8   ,      snake_case_=10   ,      ):







										'''simple docstring'''


										A__       :     Union[str, Any]								=			parent
										A__       :     Tuple								=			batch_size
										A__       :     int								=			image_size
										A__       :     int								=			patch_size
										A__       :     Any								=			num_channels
										A__       :     List[Any]								=			is_training
										A__       :     Dict								=			use_labels
										A__       :     str								=			hidden_size
										A__       :     Optional[int]								=			num_hidden_layers
										A__       :     Any								=			num_attention_heads
										A__       :     Any								=			intermediate_size
										A__       :     int								=			hidden_act
										A__       :     int								=			hidden_dropout_prob
										A__       :     Union[str, Any]								=			attention_probs_dropout_prob
										A__       :     Dict								=			type_sequence_label_size
										A__       :     Any								=			initializer_range
										A__       :     Any								=			num_labels
										A__       :     Optional[Any]								=			scope
										A__       :     Any								=			n_targets
										A__       :     Optional[int]								=			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
										A__       :     Union[str, Any]								=			(image_size[1] // patch_size) * (image_size[0] // patch_size)
										A__       :     int								=			num_patches + 1 + self.num_detection_tokens





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Dict								=			floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]							)

										A__       :     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)
																	A__       :     Dict								=			[]
																	for i in range(self.batch_size							):
																								A__       :     Optional[Any]								=			{}
																								A__       :     Tuple								=			torch.randint(
																								    high=self.num_labels   ,      size=(self.n_targets,)   ,      device=snake_case_							)
																								A__       :     Any								=			torch.rand(self.n_targets   ,      4   ,      device=snake_case_							)
																								labels.append(snake_case_							)

										A__       :     Optional[int]								=			self.get_config()

										return config, pixel_values, labels





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										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=snake_case_   ,      initializer_range=self.initializer_range   ,      num_detection_tokens=self.num_detection_tokens   ,      num_labels=self.num_labels   ,      )





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


										A__       :     Any								=			YolosModel(config=snake_case_							)
										model.to(snake_case_							)
										model.eval()
										A__       :     Optional[int]								=			model(snake_case_							)
										self.parent.assertEqual(
										    result.last_hidden_state.shape   ,      (self.batch_size, self.expected_seq_len, self.hidden_size)							)





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


										A__       :     Dict								=			YolosForObjectDetection(snake_case_							)
										model.to(snake_case_							)
										model.eval()

										A__       :     List[str]								=			model(pixel_values=snake_case_							)
										A__       :     Dict								=			model(snake_case_							)

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

										A__       :     List[str]								=			model(pixel_values=snake_case_   ,      labels=snake_case_							)

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







										'''simple docstring'''


										A__       :     Union[str, Any]								=			self.prepare_config_and_inputs()
										A__				,							A__				,							A__       :     Optional[Any]								=			config_and_inputs
										A__       :     int								=			{"""pixel_values""": pixel_values}
										return config, inputs_dict





@require_torch
class 						__UpperCAmelCase    (__A							,   __A							,   unittest.TestCase     ):







			'''simple docstring'''
			_UpperCamelCase  :						List[str]					     =				(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
			_UpperCamelCase  :						Optional[Any]					     =				(
			    {'feature-extraction': YolosModel, 'object-detection': YolosForObjectDetection} if is_torch_available() else {}
			)

			_UpperCamelCase  :						Optional[int]					     =				False
			_UpperCamelCase  :						Optional[int]					     =				False
			_UpperCamelCase  :						int					     =				False
			_UpperCamelCase  :						Any					     =				False





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_=False							):







										'''simple docstring'''


										A__       :     Tuple								=			super()._prepare_for_class(snake_case_   ,      snake_case_   ,      return_labels=snake_case_							)

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

										return inputs_dict





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			YolosModelTester(self							)
										A__       :     Optional[int]								=			ConfigTester(self   ,      config_class=snake_case_   ,      has_text_modality=snake_case_   ,      hidden_size=37							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										self.config_tester.run_common_tests()





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										pass





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__				,							A__       :     Optional[Any]								=			self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																	A__       :     int								=			model_class(snake_case_							)
																	self.assertIsInstance(model.get_input_embeddings()   ,      (nn.Module)							)
																	A__       :     int								=			model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(snake_case_   ,      nn.Linear							)							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__				,							A__       :     Union[str, Any]								=			self.model_tester.prepare_config_and_inputs_for_common()

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

																	A__       :     List[str]								=			["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     str								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__				,							A__       :     Optional[int]								=			self.model_tester.prepare_config_and_inputs_for_common()
										A__       :     Any								=			True

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

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

																	self.assertListEqual(
																	    list(attentions[0].shape[-3:]							)   ,      [self.model_tester.num_attention_heads, seq_len, seq_len]   ,      )
																	A__       :     List[Any]								=			len(snake_case_							)

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

																	A__       :     Tuple								=			1
																	self.assertEqual(out_len + added_hidden_states   ,      len(snake_case_							)							)

																	A__       :     Union[str, Any]								=			outputs.attentions

																	self.assertEqual(len(snake_case_							)   ,      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							):







										'''simple docstring'''


										def check_hidden_states_output(snake_case_   ,      snake_case_   ,      snake_case_							):
																	A__       :     Any								=			model_class(snake_case_							)
																	model.to(snake_case_							)
																	model.eval()

																	with torch.no_grad():
																								A__       :     List[str]								=			model(**self._prepare_for_class(snake_case_   ,      snake_case_							)							)

																	A__       :     List[str]								=			outputs.hidden_states

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

																	# YOLOS has a different seq_length
																	A__       :     Dict								=			self.model_tester.expected_seq_len

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

										A__				,							A__       :     Any								=			self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																	A__       :     Dict								=			True
																	check_hidden_states_output(snake_case_   ,      snake_case_   ,      snake_case_							)

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	A__       :     Optional[Any]								=			True

																	check_hidden_states_output(snake_case_   ,      snake_case_   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Dict								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_object_detection(*snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__       :     Any								=			YolosModel.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)





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



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







			'''simple docstring'''





			@cached_property
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										return AutoImageProcessor.from_pretrained("""hustvl/yolos-small"""							) if is_vision_available() else None





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[str]								=			YolosForObjectDetection.from_pretrained("""hustvl/yolos-small"""							).to(snake_case_							)

										A__       :     str								=			self.default_image_processor
										A__       :     int								=			prepare_img()
										A__       :     Tuple								=			image_processor(images=snake_case_   ,      return_tensors="""pt"""							).to(snake_case_							)

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

										# verify outputs
										A__       :     Union[str, Any]								=			torch.Size((1, 100, 92)							)
										self.assertEqual(outputs.logits.shape   ,      snake_case_							)

										A__       :     str								=			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=snake_case_   ,      )
										A__       :     str								=			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=snake_case_							)
										self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]   ,      snake_case_   ,      atol=1E-4							)							)
										self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]   ,      snake_case_   ,      atol=1E-4							)							)

										# verify postprocessing
										A__       :     Optional[Any]								=			image_processor.post_process_object_detection(
										    snake_case_   ,      threshold=0.3   ,      target_sizes=[image.size[::-1]]							)[0]
										A__       :     Dict								=			torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61]							).to(snake_case_							)
										A__       :     Optional[Any]								=			[75, 75, 17, 63, 17]
										A__       :     Any								=			torch.tensor([3_35.06_09, 79.38_48, 3_75.42_16, 1_87.24_95]							).to(snake_case_							)

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






"""simple docstring"""



from collections.abc import Sequence

def  _A(		lowerCAmelCase			,  lowerCAmelCase							):
							return sum(c * (x**i) for i, c in enumerate(lowerCAmelCase							)							)

def  _A(		lowerCAmelCase			,  lowerCAmelCase							):
							A__       :     str								=			0.0
							for coeff in reversed(lowerCAmelCase							):
														A__       :     Optional[int]								=			result * x + coeff
							return result


if __name__ == "__main__":
				_UpperCamelCase								=		(0.0, 0.0, 5.0, 9.3, 7.0)
				_UpperCamelCase								=		10.0
				print(evaluate_poly(poly, x))
				print(horner(poly, x))





