





'''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 							__lowercase       :







	'''simple docstring'''

	def __init__(self     ,_lowerCamelCase     ,_lowerCamelCase=13     ,_lowerCamelCase=[30, 30]     ,_lowerCamelCase=2     ,_lowerCamelCase=3     ,_lowerCamelCase=True     ,_lowerCamelCase=True     ,_lowerCamelCase=32     ,_lowerCamelCase=5     ,_lowerCamelCase=4     ,_lowerCamelCase=37     ,_lowerCamelCase="gelu"     ,_lowerCamelCase=0.1     ,_lowerCamelCase=0.1     ,_lowerCamelCase=10     ,_lowerCamelCase=0.0_2     ,_lowerCamelCase=3     ,_lowerCamelCase=None     ,_lowerCamelCase=8     ,_lowerCamelCase=10     ,)							->							Tuple:







				'''simple docstring'''





				__lowercase										=							parent
				__lowercase										=							batch_size
				__lowercase										=							image_size
				__lowercase										=							patch_size
				__lowercase										=							num_channels
				__lowercase										=							is_training
				__lowercase										=							use_labels
				__lowercase										=							hidden_size
				__lowercase										=							num_hidden_layers
				__lowercase										=							num_attention_heads
				__lowercase										=							intermediate_size
				__lowercase										=							hidden_act
				__lowercase										=							hidden_dropout_prob
				__lowercase										=							attention_probs_dropout_prob
				__lowercase										=							type_sequence_label_size
				__lowercase										=							initializer_range
				__lowercase										=							num_labels
				__lowercase										=							scope
				__lowercase										=							n_targets
				__lowercase										=							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										=							(image_size[1] // patch_size) * (image_size[0] // patch_size)
				__lowercase										=							num_patches + 1 + self.num_detection_tokens


	def 						_UpperCAmelCase     (self )							->							Dict:







				'''simple docstring'''





				__lowercase										=							floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] )

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

				__lowercase										=							self.get_config()

				return config, pixel_values, labels


	def 						_UpperCAmelCase     (self )							->							List[Any]:







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


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )							->							int:







				'''simple docstring'''





				__lowercase										=							YolosModel(config=_lowerCamelCase )
				model.to(_lowerCamelCase )
				model.eval()
				__lowercase										=							model(_lowerCamelCase )
				self.parent.assertEqual(
				    result.last_hidden_state.shape     ,(self.batch_size, self.expected_seq_len, self.hidden_size) )


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase										=							YolosForObjectDetection(_lowerCamelCase )
				model.to(_lowerCamelCase )
				model.eval()

				__lowercase										=							model(pixel_values=_lowerCamelCase )
				__lowercase										=							model(_lowerCamelCase )

				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										=							model(pixel_values=_lowerCamelCase     ,labels=_lowerCamelCase )

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







				'''simple docstring'''





				__lowercase										=							self.prepare_config_and_inputs()
				__lowercase						,		__lowercase						,		__lowercase										=							config_and_inputs
				__lowercase										=							{'''pixel_values''': pixel_values}
				return config, inputs_dict






@require_torch
class 							__lowercase       (      lowercase_ ,      lowercase_ ,      unittest.TestCase						):







	'''simple docstring'''

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

	a    :				Optional[Any]											=       False
	a    :				Dict											=       False
	a    :				List[str]											=       False
	a    :				Optional[int]											=       False


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase=False )							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase										=							super()._prepare_for_class(_lowerCamelCase     ,_lowerCamelCase     ,return_labels=_lowerCamelCase )

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

				return inputs_dict


	def 						_UpperCAmelCase     (self )							->							Dict:







				'''simple docstring'''





				__lowercase										=							YolosModelTester(self )
				__lowercase										=							ConfigTester(self     ,config_class=_lowerCamelCase     ,has_text_modality=_lowerCamelCase     ,hidden_size=37 )


	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				self.config_tester.run_common_tests()


	def 						_UpperCAmelCase     (self )							->							List[Any]:







				'''simple docstring'''





				pass


	def 						_UpperCAmelCase     (self )							->							List[Any]:







				'''simple docstring'''





				__lowercase						,		__lowercase										=							self.model_tester.prepare_config_and_inputs_for_common()

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


	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				__lowercase						,		__lowercase										=							self.model_tester.prepare_config_and_inputs_for_common()

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

							__lowercase										=							['''pixel_values''']
							self.assertListEqual(arg_names[:1]     ,_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							List[str]:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_lowerCamelCase )


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







				'''simple docstring'''





				__lowercase						,		__lowercase										=							self.model_tester.prepare_config_and_inputs_for_common()
				__lowercase										=							True

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

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

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

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

							__lowercase										=							1
							self.assertEqual(out_len + added_hidden_states     ,len(_lowerCamelCase ) )

							__lowercase										=							outputs.attentions

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







				'''simple docstring'''





				def check_hidden_states_output(_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase ):
							__lowercase										=							model_class(_lowerCamelCase )
							model.to(_lowerCamelCase )
							model.eval()

							with torch.no_grad():
										__lowercase										=							model(**self._prepare_for_class(_lowerCamelCase     ,_lowerCamelCase ) )

							__lowercase										=							outputs.hidden_states

							__lowercase										=							getattr(
							    self.model_tester     ,'''expected_num_hidden_layers'''     ,self.model_tester.num_hidden_layers + 1 )
							self.assertEqual(len(_lowerCamelCase )     ,_lowerCamelCase )

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

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

				__lowercase						,		__lowercase										=							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							__lowercase										=							True
							check_hidden_states_output(_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )

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

							check_hidden_states_output(_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							List[Any]:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_object_detection(*_lowerCamelCase )


	@slow
	def 						_UpperCAmelCase     (self )							->							int:







				'''simple docstring'''





				for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__lowercase										=							YolosModel.from_pretrained(_lowerCamelCase )
							self.assertIsNotNone(_lowerCamelCase )





def    _lowerCAmelCase						(				):
			__lowercase										=							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
			return image






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







	'''simple docstring'''

	@cached_property
	def 						_UpperCAmelCase     (self )							->							List[str]:







				'''simple docstring'''





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


	@slow
	def 						_UpperCAmelCase     (self )							->							Optional[Any]:







				'''simple docstring'''





				__lowercase										=							YolosForObjectDetection.from_pretrained('''hustvl/yolos-small''' ).to(_lowerCamelCase )

				__lowercase										=							self.default_image_processor
				__lowercase										=							prepare_img()
				__lowercase										=							image_processor(images=_lowerCamelCase     ,return_tensors='''pt''' ).to(_lowerCamelCase )

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

				# verify outputs
				__lowercase										=							torch.Size((1, 100, 92) )
				self.assertEqual(outputs.logits.shape     ,_lowerCamelCase )

				__lowercase										=							torch.tensor(
				    [[-2_4.0_2_4_8, -1_0.3_0_2_4, -1_4.8_2_9_0], [-4_2.0_3_9_2, -1_6.8_2_0_0, -2_7.4_3_3_4], [-2_7.2_7_4_3, -1_1.8_1_5_4, -1_8.7_1_4_8]]     ,device=_lowerCamelCase     ,)
				__lowercase										=							torch.tensor(
				    [[0.2_5_5_9, 0.5_4_5_5, 0.4_7_0_6], [0.2_9_8_9, 0.7_2_7_9, 0.1_8_7_5], [0.7_7_3_2, 0.4_0_1_7, 0.4_4_6_2]]     ,device=_lowerCamelCase )
				self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]     ,_lowerCamelCase     ,atol=1E-4 ) )
				self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]     ,_lowerCamelCase     ,atol=1E-4 ) )

				# verify postprocessing
				__lowercase										=							image_processor.post_process_object_detection(
				    _lowerCamelCase     ,threshold=0.3     ,target_sizes=[image.size[::-1]] )[0]
				__lowercase										=							torch.tensor([0.9_9_9_4, 0.9_7_9_0, 0.9_9_6_4, 0.9_9_7_2, 0.9_8_6_1] ).to(_lowerCamelCase )
				__lowercase										=							[75, 75, 17, 63, 17]
				__lowercase										=							torch.tensor([335.0609, 7_9.3_8_4_8, 375.4216, 187.2495] ).to(_lowerCamelCase )

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












'''simple docstring'''



import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging


if TYPE_CHECKING:
	from transformers.pipelines.conversational import Conversation


_SCREAMING_SNAKE_CASE					=    logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE					=    {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

_SCREAMING_SNAKE_CASE					=    {
    '''tokenizer_file''': {
        '''EleutherAI/gpt-neox-20b''': '''https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json''',
    },
}

_SCREAMING_SNAKE_CASE					=    {
    '''gpt-neox-20b''': 2_0_4_8,
}






class 							__lowercase       (      lowerCAmelCase__						):







	'''simple docstring'''

	a    :				List[Any]											=       VOCAB_FILES_NAMES
	a    :				Optional[Any]											=       PRETRAINED_VOCAB_FILES_MAP
	a    :				Optional[int]											=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	a    :				List[str]											=       ["input_ids", "attention_mask"]


	def __init__(self     ,_lowerCamelCase=None     ,_lowerCamelCase=None     ,_lowerCamelCase=None     ,_lowerCamelCase="<|endoftext|>"     ,_lowerCamelCase="<|endoftext|>"     ,_lowerCamelCase="<|endoftext|>"     ,_lowerCamelCase=False     ,**_lowerCamelCase     ,)							->							Optional[Any]:







				'''simple docstring'''





				super().__init__(
				    _lowerCamelCase     ,_lowerCamelCase     ,tokenizer_file=_lowerCamelCase     ,unk_token=_lowerCamelCase     ,bos_token=_lowerCamelCase     ,eos_token=_lowerCamelCase     ,add_prefix_space=_lowerCamelCase     ,**_lowerCamelCase     ,)

				__lowercase										=							json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() )
				if pre_tok_state.get('''add_prefix_space'''     ,_lowerCamelCase ) != add_prefix_space:
							__lowercase										=							getattr(_lowerCamelCase     ,pre_tok_state.pop('''type''' ) )
							__lowercase										=							add_prefix_space
							__lowercase										=							pre_tok_class(**_lowerCamelCase )

				__lowercase										=							add_prefix_space


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase = None )							->							Tuple[str]:







				'''simple docstring'''





				__lowercase										=							self._tokenizer.model.save(_lowerCamelCase     ,name=_lowerCamelCase )
				return tuple(_lowerCamelCase )


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase )							->							List[int]:







				'''simple docstring'''





				__lowercase										=							[]
				for is_user, text in conversation.iter_texts():
							input_ids.extend(self.encode(_lowerCamelCase     ,add_special_tokens=_lowerCamelCase ) + [self.eos_token_id] )

				if len(_lowerCamelCase ) > self.model_max_length:
							__lowercase										=							input_ids[-self.model_max_length :]
				return input_ids





