



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,
	    BertTokenizerFast,
	    BlipImageProcessor,
	    GPTaTokenizer,
	    InstructBlipProcessor,
	    PreTrainedTokenizerFast,
	)





@require_vision
class 					__lowerCAmelCase (     unittest.TestCase       ):




				def 							snake_case   (							self		):



									"""simple docstring"""





									_lowerCAmelCase						=    tempfile.mkdtemp()

									_lowerCAmelCase						=    BlipImageProcessor()
									_lowerCAmelCase						=    GPTaTokenizer.from_pretrained("""hf-internal-testing/tiny-random-GPT2Model"""		)
									_lowerCAmelCase						=    BertTokenizerFast.from_pretrained("""hf-internal-testing/tiny-random-bert"""		)

									_lowerCAmelCase						=    InstructBlipProcessor(_snake_case						,				_snake_case						,				_snake_case		)

									processor.save_pretrained(self.tmpdirname		)




				def 							snake_case   (							self						,				**_snake_case		):



									"""simple docstring"""





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




				def 							snake_case   (							self						,				**_snake_case		):



									"""simple docstring"""





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




				def 							snake_case   (							self						,				**_snake_case		):



									"""simple docstring"""





									return AutoProcessor.from_pretrained(self.tmpdirname						,				**_snake_case		).qformer_tokenizer




				def 							snake_case   (							self		):



									"""simple docstring"""





									shutil.rmtree(self.tmpdirname		)




				def 							snake_case   (							self		):



									"""simple docstring"""





									_lowerCAmelCase						=    [np.random.randint(255						,				size=(3, 30, 400)						,				dtype=np.uinta		)]

									_lowerCAmelCase						=    [Image.fromarray(np.moveaxis(_snake_case						,				0						,				-1		)		) for x in image_inputs]

									return image_inputs




				def 							snake_case   (							self		):



									"""simple docstring"""





									_lowerCAmelCase						=    InstructBlipProcessor(
									    tokenizer=self.get_tokenizer()						,				image_processor=self.get_image_processor()						,				qformer_tokenizer=self.get_qformer_tokenizer()						,				)
									processor.save_pretrained(self.tmpdirname		)

									_lowerCAmelCase						=    self.get_tokenizer(bos_token="""(BOS)"""						,				eos_token="""(EOS)"""		)
									_lowerCAmelCase						=    self.get_image_processor(do_normalize=_snake_case						,				padding_value=1.0		)

									_lowerCAmelCase						=    InstructBlipProcessor.from_pretrained(
									    self.tmpdirname						,				bos_token="""(BOS)"""						,				eos_token="""(EOS)"""						,				do_normalize=_snake_case						,				padding_value=1.0		)

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

									self.assertEqual(processor.image_processor.to_json_string()						,				image_processor_add_kwargs.to_json_string()		)
									self.assertIsInstance(processor.image_processor						,				_snake_case		)
									self.assertIsInstance(processor.qformer_tokenizer						,				_snake_case		)




				def 							snake_case   (							self		):



									"""simple docstring"""





									_lowerCAmelCase						=    self.get_image_processor()
									_lowerCAmelCase						=    self.get_tokenizer()
									_lowerCAmelCase						=    self.get_qformer_tokenizer()

									_lowerCAmelCase						=    InstructBlipProcessor(
									    tokenizer=_snake_case						,				image_processor=_snake_case						,				qformer_tokenizer=_snake_case		)

									_lowerCAmelCase						=    self.prepare_image_inputs()

									_lowerCAmelCase						=    image_processor(_snake_case						,				return_tensors="""np"""		)
									_lowerCAmelCase						=    processor(images=_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 							snake_case   (							self		):



									"""simple docstring"""





									_lowerCAmelCase						=    self.get_image_processor()
									_lowerCAmelCase						=    self.get_tokenizer()
									_lowerCAmelCase						=    self.get_qformer_tokenizer()

									_lowerCAmelCase						=    InstructBlipProcessor(
									    tokenizer=_snake_case						,				image_processor=_snake_case						,				qformer_tokenizer=_snake_case		)

									_lowerCAmelCase						=    """lower newer"""

									_lowerCAmelCase						=    processor(text=_snake_case		)

									_lowerCAmelCase						=    tokenizer(_snake_case						,				return_token_type_ids=_snake_case		)
									_lowerCAmelCase						=    qformer_tokenizer(_snake_case						,				return_token_type_ids=_snake_case		)

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

									for key in encoded_tokens_qformer.keys():
														self.assertListEqual(encoded_tokens_qformer[key]						,				encoded_processor["""qformer_""" + key]		)




				def 							snake_case   (							self		):



									"""simple docstring"""





									_lowerCAmelCase						=    self.get_image_processor()
									_lowerCAmelCase						=    self.get_tokenizer()
									_lowerCAmelCase						=    self.get_qformer_tokenizer()

									_lowerCAmelCase						=    InstructBlipProcessor(
									    tokenizer=_snake_case						,				image_processor=_snake_case						,				qformer_tokenizer=_snake_case		)

									_lowerCAmelCase						=    """lower newer"""
									_lowerCAmelCase						=    self.prepare_image_inputs()

									_lowerCAmelCase						=    processor(text=_snake_case						,				images=_snake_case		)

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

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




				def 							snake_case   (							self		):



									"""simple docstring"""





									_lowerCAmelCase						=    self.get_image_processor()
									_lowerCAmelCase						=    self.get_tokenizer()
									_lowerCAmelCase						=    self.get_qformer_tokenizer()

									_lowerCAmelCase						=    InstructBlipProcessor(
									    tokenizer=_snake_case						,				image_processor=_snake_case						,				qformer_tokenizer=_snake_case		)

									_lowerCAmelCase						=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									_lowerCAmelCase						=    processor.batch_decode(_snake_case		)
									_lowerCAmelCase						=    tokenizer.batch_decode(_snake_case		)

									self.assertListEqual(_snake_case						,				_snake_case		)




				def 							snake_case   (							self		):



									"""simple docstring"""





									_lowerCAmelCase						=    self.get_image_processor()
									_lowerCAmelCase						=    self.get_tokenizer()
									_lowerCAmelCase						=    self.get_qformer_tokenizer()

									_lowerCAmelCase						=    InstructBlipProcessor(
									    tokenizer=_snake_case						,				image_processor=_snake_case						,				qformer_tokenizer=_snake_case		)

									_lowerCAmelCase						=    """lower newer"""
									_lowerCAmelCase						=    self.prepare_image_inputs()

									_lowerCAmelCase						=    processor(text=_snake_case						,				images=_snake_case		)

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




import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


__a  :     int       =  logging.get_logger(__name__)

__a  :     str       =  {
    """ut/deta""": """https://huggingface.co/ut/deta/resolve/main/config.json""",
}




class    _UpperCamelCase      (						_UpperCAmelCase ):


			"""simple docstring"""






			__a  : Optional[int]			 =						'''deta'''
			__a  : Optional[int]			 =						{
			    '''hidden_size''': '''d_model''',
			    '''num_attention_heads''': '''encoder_attention_heads''',
			}





			def __init__(   self      ,   lowerCAmelCase__=None      ,   lowerCAmelCase__=9_00      ,   lowerCAmelCase__=20_48      ,   lowerCAmelCase__=6      ,   lowerCAmelCase__=20_48      ,   lowerCAmelCase__=8      ,   lowerCAmelCase__=6      ,   lowerCAmelCase__=10_24      ,   lowerCAmelCase__=8      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__="relu"      ,   lowerCAmelCase__=2_56      ,   lowerCAmelCase__=0.1      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=0.02      ,   lowerCAmelCase__=1.0      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=False      ,   lowerCAmelCase__="sine"      ,   lowerCAmelCase__=5      ,   lowerCAmelCase__=4      ,   lowerCAmelCase__=4      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=3_00      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=1      ,   lowerCAmelCase__=5      ,   lowerCAmelCase__=2      ,   lowerCAmelCase__=1      ,   lowerCAmelCase__=1      ,   lowerCAmelCase__=5      ,   lowerCAmelCase__=2      ,   lowerCAmelCase__=0.1      ,   lowerCAmelCase__=0.25      ,   **lowerCAmelCase__      ,   )     ->					List[Any]:

							'''simple docstring'''


							if backbone_config is None:
											logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'''						)
											__lowercase 			=							CONFIG_MAPPING['''resnet'''](out_features=['''stage2''', '''stage3''', '''stage4''']						)
							else:
											if isinstance(lowerCAmelCase__      ,   lowerCAmelCase__						):
															__lowercase 			=							backbone_config.pop('''model_type'''						)
															__lowercase 			=							CONFIG_MAPPING[backbone_model_type]
															__lowercase 			=							config_class.from_dict(lowerCAmelCase__						)

							__lowercase 			=							backbone_config
							__lowercase 			=							num_queries
							__lowercase 			=							max_position_embeddings
							__lowercase 			=							d_model
							__lowercase 			=							encoder_ffn_dim
							__lowercase 			=							encoder_layers
							__lowercase 			=							encoder_attention_heads
							__lowercase 			=							decoder_ffn_dim
							__lowercase 			=							decoder_layers
							__lowercase 			=							decoder_attention_heads
							__lowercase 			=							dropout
							__lowercase 			=							attention_dropout
							__lowercase 			=							activation_dropout
							__lowercase 			=							activation_function
							__lowercase 			=							init_std
							__lowercase 			=							init_xavier_std
							__lowercase 			=							encoder_layerdrop
							__lowercase 			=							auxiliary_loss
							__lowercase 			=							position_embedding_type
							# deformable attributes
							__lowercase 			=							num_feature_levels
							__lowercase 			=							encoder_n_points
							__lowercase 			=							decoder_n_points
							__lowercase 			=							two_stage
							__lowercase 			=							two_stage_num_proposals
							__lowercase 			=							with_box_refine
							__lowercase 			=							assign_first_stage
							if two_stage is True and with_box_refine is False:
											raise ValueError('''If two_stage is True, with_box_refine must be True.'''						)
							# Hungarian matcher
							__lowercase 			=							class_cost
							__lowercase 			=							bbox_cost
							__lowercase 			=							giou_cost
							# Loss coefficients
							__lowercase 			=							mask_loss_coefficient
							__lowercase 			=							dice_loss_coefficient
							__lowercase 			=							bbox_loss_coefficient
							__lowercase 			=							giou_loss_coefficient
							__lowercase 			=							eos_coefficient
							__lowercase 			=							focal_alpha
							super().__init__(is_encoder_decoder=lowerCAmelCase__      ,   **lowerCAmelCase__						)





			@property
			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					int:

							'''simple docstring'''


							return self.encoder_attention_heads





			@property
			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					int:

							'''simple docstring'''


							return self.d_model





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

							'''simple docstring'''


							__lowercase 			=							copy.deepcopy(self.__dict__						)
							__lowercase 			=							self.backbone_config.to_dict()
							__lowercase 			=							self.__class__.model_type
							return output