
"""simple docstring"""




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast



@require_vision
class 	lowerCAmelCase_						(unittest.TestCase  ):


				"""simple docstring"""



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       str           =   tempfile.mkdtemp()

											SCREAMING_SNAKE_CASE__						:       List[str]           =   BlipImageProcessor()
											SCREAMING_SNAKE_CASE__						:       str           =   BertTokenizer.from_pretrained("""hf-internal-testing/tiny-random-BertModel"""    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   BlipProcessor(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )

											processor.save_pretrained(self.tmpdirname    )



				def 	__magic_name__    (self   ,       **SCREAMING_SNAKE_CASE__    )					->		List[str]:



											"""simple docstring"""




											return AutoProcessor.from_pretrained(self.tmpdirname   ,       **SCREAMING_SNAKE_CASE__    ).tokenizer



				def 	__magic_name__    (self   ,       **SCREAMING_SNAKE_CASE__    )					->		Union[str, Any]:



											"""simple docstring"""




											return AutoProcessor.from_pretrained(self.tmpdirname   ,       **SCREAMING_SNAKE_CASE__    ).image_processor



				def 	__magic_name__    (self    )					->		Optional[int]:



											"""simple docstring"""




											shutil.rmtree(self.tmpdirname    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   [np.random.randint(2_55   ,       size=(3, 30, 4_00)   ,       dtype=np.uinta    )]

											SCREAMING_SNAKE_CASE__						:       Tuple           =   [Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE__   ,       0   ,       -1    )    ) for x in image_inputs]

											return image_inputs



				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   BlipProcessor(tokenizer=self.get_tokenizer()   ,       image_processor=self.get_image_processor()    )
											processor.save_pretrained(self.tmpdirname    )

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.get_tokenizer(bos_token="""(BOS)"""   ,       eos_token="""(EOS)"""    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.get_image_processor(do_normalize=SCREAMING_SNAKE_CASE__   ,       padding_value=1.0    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   BlipProcessor.from_pretrained(
											    self.tmpdirname   ,       bos_token="""(BOS)"""   ,       eos_token="""(EOS)"""   ,       do_normalize=SCREAMING_SNAKE_CASE__   ,       padding_value=1.0    )

											self.assertEqual(processor.tokenizer.get_vocab()   ,       tokenizer_add_kwargs.get_vocab()    )
											self.assertIsInstance(processor.tokenizer   ,       SCREAMING_SNAKE_CASE__    )

											self.assertEqual(processor.image_processor.to_json_string()   ,       image_processor_add_kwargs.to_json_string()    )
											self.assertIsInstance(processor.image_processor   ,       SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.get_image_processor()
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   self.get_tokenizer()

											SCREAMING_SNAKE_CASE__						:       str           =   BlipProcessor(tokenizer=SCREAMING_SNAKE_CASE__   ,       image_processor=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Any           =   self.prepare_image_inputs()

											SCREAMING_SNAKE_CASE__						:       str           =   image_processor(SCREAMING_SNAKE_CASE__   ,       return_tensors="""np"""    )
											SCREAMING_SNAKE_CASE__						:       Any           =   processor(images=SCREAMING_SNAKE_CASE__   ,       return_tensors="""np"""    )

											for key in input_feat_extract.keys():
																		self.assertAlmostEqual(input_feat_extract[key].sum()   ,       input_processor[key].sum()   ,       delta=1E-2    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.get_image_processor()
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.get_tokenizer()

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   BlipProcessor(tokenizer=SCREAMING_SNAKE_CASE__   ,       image_processor=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   """lower newer"""

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   processor(text=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   tokenizer(SCREAMING_SNAKE_CASE__   ,       return_token_type_ids=SCREAMING_SNAKE_CASE__    )

											for key in encoded_tok.keys():
																		self.assertListEqual(encoded_tok[key]   ,       encoded_processor[key]    )



				def 	__magic_name__    (self    )					->		Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Tuple           =   self.get_image_processor()
											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.get_tokenizer()

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   BlipProcessor(tokenizer=SCREAMING_SNAKE_CASE__   ,       image_processor=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       str           =   """lower newer"""
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.prepare_image_inputs()

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   processor(text=SCREAMING_SNAKE_CASE__   ,       images=SCREAMING_SNAKE_CASE__    )

											self.assertListEqual(list(inputs.keys()    )   ,       ["""pixel_values""", """input_ids""", """attention_mask"""]    )

											# test if it raises when no input is passed
											with pytest.raises(SCREAMING_SNAKE_CASE__    ):
																		processor()



				def 	__magic_name__    (self    )					->		int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.get_image_processor()
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.get_tokenizer()

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   BlipProcessor(tokenizer=SCREAMING_SNAKE_CASE__   ,       image_processor=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											SCREAMING_SNAKE_CASE__						:       int           =   processor.batch_decode(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Tuple           =   tokenizer.batch_decode(SCREAMING_SNAKE_CASE__    )

											self.assertListEqual(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.get_image_processor()
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.get_tokenizer()

											SCREAMING_SNAKE_CASE__						:       Tuple           =   BlipProcessor(tokenizer=SCREAMING_SNAKE_CASE__   ,       image_processor=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Tuple           =   """lower newer"""
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.prepare_image_inputs()

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   processor(text=SCREAMING_SNAKE_CASE__   ,       images=SCREAMING_SNAKE_CASE__    )

											# For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
											self.assertListEqual(list(inputs.keys()    )   ,       ["""pixel_values""", """input_ids""", """attention_mask"""]    )






"""simple docstring"""




from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase__      :    str    							= logging.get_logger(__name__)

UpperCAmelCase__      :    Optional[int]    							= {
    'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json',
    # See all YOLOS models at https://huggingface.co/models?filter=yolos
}



class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				__UpperCamelCase      :						int					       =   '''yolos'''



				def __init__(self   ,       SCREAMING_SNAKE_CASE__=7_68   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=30_72   ,       SCREAMING_SNAKE_CASE__="gelu"   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.02   ,       SCREAMING_SNAKE_CASE__=1E-12   ,       SCREAMING_SNAKE_CASE__=[5_12, 8_64]   ,       SCREAMING_SNAKE_CASE__=16   ,       SCREAMING_SNAKE_CASE__=3   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=1_00   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=False   ,       SCREAMING_SNAKE_CASE__=1   ,       SCREAMING_SNAKE_CASE__=5   ,       SCREAMING_SNAKE_CASE__=2   ,       SCREAMING_SNAKE_CASE__=5   ,       SCREAMING_SNAKE_CASE__=2   ,       SCREAMING_SNAKE_CASE__=0.1   ,       **SCREAMING_SNAKE_CASE__   ,       )					->		Optional[Any]:



											"""simple docstring"""




											super().__init__(**SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   hidden_size
											SCREAMING_SNAKE_CASE__						:       int           =   num_hidden_layers
											SCREAMING_SNAKE_CASE__						:       str           =   num_attention_heads
											SCREAMING_SNAKE_CASE__						:       List[str]           =   intermediate_size
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   hidden_act
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   hidden_dropout_prob
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   initializer_range
											SCREAMING_SNAKE_CASE__						:       Dict           =   layer_norm_eps
											SCREAMING_SNAKE_CASE__						:       List[str]           =   image_size
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   patch_size
											SCREAMING_SNAKE_CASE__						:       List[str]           =   num_channels
											SCREAMING_SNAKE_CASE__						:       List[str]           =   qkv_bias
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   num_detection_tokens
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   use_mid_position_embeddings
											SCREAMING_SNAKE_CASE__						:       List[str]           =   auxiliary_loss
											# Hungarian matcher
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   class_cost
											SCREAMING_SNAKE_CASE__						:       List[str]           =   bbox_cost
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   giou_cost
											# Loss coefficients
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   bbox_loss_coefficient
											SCREAMING_SNAKE_CASE__						:       List[str]           =   giou_loss_coefficient
											SCREAMING_SNAKE_CASE__						:       int           =   eos_coefficient






class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				__UpperCamelCase      :						Dict					       =   version.parse('''1.11'''  )



				@property
				def 	__magic_name__    (self    )					->		Mapping[str, Mapping[int, str]]:



											"""simple docstring"""




											return OrderedDict(
											    [
											        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
											    ]    )



				@property
				def 	__magic_name__    (self    )					->		float:



											"""simple docstring"""




											return 1E-4



				@property
				def 	__magic_name__    (self    )					->		int:



											"""simple docstring"""




											return 12




