






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




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]      )

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

												lowercase__    :				List[Any]=	self.get_config()

												return config, pixel_values, labels




						def 		UpperCAmelCase_	(       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 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				str=	YolosModel(config=snake_case__      )
												model.to(snake_case__      )
												model.eval()
												lowercase__    :				Tuple=	model(snake_case__      )
												self.parent.assertEqual(
												    result.last_hidden_state.shape	,  (self.batch_size, self.expected_seq_len, self.hidden_size)      )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Optional[Any]=	YolosForObjectDetection(snake_case__      )
												model.to(snake_case__      )
												model.eval()

												lowercase__    :				List[Any]=	model(pixel_values=snake_case__      )
												lowercase__    :				str=	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)      )

												lowercase__    :				List[Any]=	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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	self.prepare_config_and_inputs()
												lowercase__,     lowercase__,     lowercase__    :				str=	config_and_inputs
												lowercase__    :				str=	{"pixel_values": pixel_values}
												return config, inputs_dict


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




						"""simple docstring"""




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

						__lowerCamelCase    	= False
						__lowerCamelCase    	= False
						__lowerCamelCase    	= False
						__lowerCamelCase    	= False




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__	,  snake_case__=False      ):
												'''simple docstring'''
												lowercase__    :				int=	super()._prepare_for_class(snake_case__	,  snake_case__	,  return_labels=snake_case__      )

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

												return inputs_dict




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[int]=	YolosModelTester(self      )
												lowercase__    :				List[str]=	ConfigTester(self	,  config_class=snake_case__	,  has_text_modality=snake_case__	,  hidden_size=37      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												self.config_tester.run_common_tests()




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# YOLOS does not use inputs_embeds
												pass




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__,     lowercase__    :				str=	self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowercase__    :				Any=	model_class(snake_case__      )
																		self.assertIsInstance(model.get_input_embeddings()	,  (nn.Module)      )
																		lowercase__    :				Tuple=	model.get_output_embeddings()
																		self.assertTrue(x is None or isinstance(snake_case__	,  nn.Linear      )      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__,     lowercase__    :				Dict=	self.model_tester.prepare_config_and_inputs_for_common()

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

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




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Dict=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__,     lowercase__    :				str=	self.model_tester.prepare_config_and_inputs_for_common()
												lowercase__    :				Optional[int]=	True

												# in YOLOS, the seq_len is different
												lowercase__    :				List[Any]=	self.model_tester.expected_seq_len
												for model_class in self.all_model_classes:
																		lowercase__    :				Union[str, Any]=	True
																		lowercase__    :				List[str]=	False
																		lowercase__    :				Optional[Any]=	True
																		lowercase__    :				Dict=	model_class(snake_case__      )
																		model.to(snake_case__      )
																		model.eval()
																		with torch.no_grad():
																								lowercase__    :				List[str]=	model(**self._prepare_for_class(snake_case__	,  snake_case__      )      )
																		lowercase__    :				Optional[Any]=	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"]
																		lowercase__    :				Union[str, Any]=	True
																		lowercase__    :				Tuple=	model_class(snake_case__      )
																		model.to(snake_case__      )
																		model.eval()
																		with torch.no_grad():
																								lowercase__    :				List[Any]=	model(**self._prepare_for_class(snake_case__	,  snake_case__      )      )
																		lowercase__    :				List[str]=	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]	,  )
																		lowercase__    :				List[str]=	len(snake_case__      )

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

																		lowercase__    :				str=	1
																		self.assertEqual(out_len + added_hidden_states	,  len(snake_case__      )      )

																		lowercase__    :				Dict=	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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												def check_hidden_states_output(snake_case__	,  snake_case__	,  snake_case__      ):
																		lowercase__    :				int=	model_class(snake_case__      )
																		model.to(snake_case__      )
																		model.eval()

																		with torch.no_grad():
																								lowercase__    :				List[Any]=	model(**self._prepare_for_class(snake_case__	,  snake_case__      )      )

																		lowercase__    :				Optional[Any]=	outputs.hidden_states

																		lowercase__    :				Tuple=	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
																		lowercase__    :				int=	self.model_tester.expected_seq_len

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

												lowercase__,     lowercase__    :				str=	self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowercase__    :				Union[str, Any]=	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"]
																		lowercase__    :				Any=	True

																		check_hidden_states_output(snake_case__	,  snake_case__	,  snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Dict=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_object_detection(*snake_case__      )




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		lowercase__    :				List[str]=	YolosModel.from_pretrained(snake_case__      )
																		self.assertIsNotNone(snake_case__      )





def 						lowercase__()    ->Union[str, Any]:




						"""simple docstring"""



						lowercase__    :				Union[str, 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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return AutoImageProcessor.from_pretrained("hustvl/yolos-small"      ) if is_vision_available() else None




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	YolosForObjectDetection.from_pretrained("hustvl/yolos-small"      ).to(snake_case__      )

												lowercase__    :				Any=	self.default_image_processor
												lowercase__    :				str=	prepare_img()
												lowercase__    :				List[Any]=	image_processor(images=snake_case__	,  return_tensors="pt"      ).to(snake_case__      )

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

												# verify outputs
												lowercase__    :				Tuple=	torch.Size((1, 100, 92)      )
												self.assertEqual(outputs.logits.shape	,  snake_case__      )

												lowercase__    :				List[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__	,  )
												lowercase__    :				List[Any]=	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
												lowercase__    :				Union[str, Any]=	image_processor.post_process_object_detection(
												    snake_case__	,  threshold=0.3	,  target_sizes=[image.size[::-1]]      )[0]
												lowercase__    :				Optional[int]=	torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61]      ).to(snake_case__      )
												lowercase__    :				int=	[75, 75, 17, 63, 17]
												lowercase__    :				Tuple=	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 typing import Dict, List, Optional, Tuple, Union

import torch

from ...models import AutoencoderKL, TransformeraDModel
from ...schedulers import KarrasDiffusionSchedulers
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__ = None	,  ):
												'''simple docstring'''
												super().__init__()
												self.register_modules(transformer=snake_case__	,  vae=snake_case__	,  scheduler=snake_case__      )

												# create a imagenet -> id dictionary for easier use
												lowercase__    :				int=	{}
												if idalabel is not None:
																		for key, value in idalabel.items():
																								for label in value.split(","      ):
																														lowercase__    :				Tuple=	int(snake_case__      )
																		lowercase__    :				Union[str, Any]=	dict(sorted(self.labels.items()      )      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''

												if not isinstance(snake_case__	,  snake_case__      ):
																		lowercase__    :				List[Any]=	list(snake_case__      )

												for l in label:
																		if l not in self.labels:
																								raise ValueError(
																								    F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.'''      )

												return [self.labels[l] for l in label]




						@torch.no_grad()
						def __call__(       self	,  snake_case__	,  snake_case__ = 4.0	,  snake_case__ = None	,  snake_case__ = 50	,  snake_case__ = "pil"	,  snake_case__ = True	,  ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	len(snake_case__      )
												lowercase__    :				Optional[int]=	self.transformer.config.sample_size
												lowercase__    :				List[str]=	self.transformer.config.in_channels

												lowercase__    :				Any=	randn_tensor(
												    shape=(batch_size, latent_channels, latent_size, latent_size)	,  generator=snake_case__	,  device=self.device	,  dtype=self.transformer.dtype	,  )
												lowercase__    :				Any=	torch.cat([latents] * 2      ) if guidance_scale > 1 else latents

												lowercase__    :				Tuple=	torch.tensor(snake_case__	,  device=self.device      ).reshape(-1      )
												lowercase__    :				Any=	torch.tensor([1000] * batch_size	,  device=self.device      )
												lowercase__    :				Tuple=	torch.cat([class_labels, class_null]	,  0      ) if guidance_scale > 1 else class_labels

												# set step values
												self.scheduler.set_timesteps(snake_case__      )

												for t in self.progress_bar(self.scheduler.timesteps      ):
																		if guidance_scale > 1:
																								lowercase__    :				List[str]=	latent_model_input[: len(snake_case__      ) // 2]
																								lowercase__    :				int=	torch.cat([half, half]	,  dim=0      )
																		lowercase__    :				Union[str, Any]=	self.scheduler.scale_model_input(snake_case__	,  snake_case__      )

																		lowercase__    :				Optional[int]=	t
																		if not torch.is_tensor(snake_case__      ):
																								# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
																								# This would be a good case for the `match` statement (Python 3.10+)
																								lowercase__    :				List[str]=	latent_model_input.device.type == "mps"
																								if isinstance(snake_case__	,  snake_case__      ):
																														lowercase__    :				int=	torch.floataa if is_mps else torch.floataa
																								else:
																														lowercase__    :				Dict=	torch.intaa if is_mps else torch.intaa
																								lowercase__    :				Tuple=	torch.tensor([timesteps]	,  dtype=snake_case__	,  device=latent_model_input.device      )
																		elif len(timesteps.shape      ) == 0:
																								lowercase__    :				Dict=	timesteps[None].to(latent_model_input.device      )
																		# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
																		lowercase__    :				int=	timesteps.expand(latent_model_input.shape[0]      )
																		# predict noise model_output
																		lowercase__    :				Union[str, Any]=	self.transformer(
																		    snake_case__	,  timestep=snake_case__	,  class_labels=snake_case__      ).sample

																		# perform guidance
																		if guidance_scale > 1:
																								lowercase__,     lowercase__    :				Tuple=	noise_pred[:, :latent_channels], noise_pred[:, latent_channels:]
																								lowercase__,     lowercase__    :				Union[str, Any]=	torch.split(snake_case__	,  len(snake_case__      ) // 2	,  dim=0      )

																								lowercase__    :				str=	uncond_eps + guidance_scale * (cond_eps - uncond_eps)
																								lowercase__    :				Dict=	torch.cat([half_eps, half_eps]	,  dim=0      )

																								lowercase__    :				Optional[int]=	torch.cat([eps, rest]	,  dim=1      )

																		# learned sigma
																		if self.transformer.config.out_channels // 2 == latent_channels:
																								lowercase__,     lowercase__    :				Union[str, Any]=	torch.split(snake_case__	,  snake_case__	,  dim=1      )
																		else:
																								lowercase__    :				int=	noise_pred

																		# compute previous image: x_t -> x_t-1
																		lowercase__    :				List[Any]=	self.scheduler.step(snake_case__	,  snake_case__	,  snake_case__      ).prev_sample

												if guidance_scale > 1:
																		lowercase__,     lowercase__    :				Any=	latent_model_input.chunk(2	,  dim=0      )
												else:
																		lowercase__    :				str=	latent_model_input

												lowercase__    :				Dict=	1 / self.vae.config.scaling_factor * latents
												lowercase__    :				Any=	self.vae.decode(snake_case__      ).sample

												lowercase__    :				Tuple=	(samples / 2 + 0.5).clamp(0	,  1      )

												# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
												lowercase__    :				List[Any]=	samples.cpu().permute(0	,  2	,  3	,  1      ).float().numpy()

												if output_type == "pil":
																		lowercase__    :				Optional[Any]=	self.numpy_to_pil(snake_case__      )

												if not return_dict:
																		return (samples,)

												return ImagePipelineOutput(images=snake_case__      )






