




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       a_       (     unittest.TestCase     ):





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




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						tempfile.mkdtemp()

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

				SCREAMING_SNAKE_CASE_								=						InstructBlipProcessor(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )

				processor.save_pretrained(self.tmpdirname      )





	def 						A_(							self      ,     **SCREAMING_SNAKE_CASE      )  ->					List[str]:




				"""simple docstring"""



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





	def 						A_(							self      ,     **SCREAMING_SNAKE_CASE      )  ->					Optional[int]:




				"""simple docstring"""



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





	def 						A_(							self      ,     **SCREAMING_SNAKE_CASE      )  ->					Optional[int]:




				"""simple docstring"""



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





	def 						A_(							self      )  ->					Tuple:




				"""simple docstring"""



				shutil.rmtree(self.tmpdirname      )





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




				"""simple docstring"""



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

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

				return image_inputs





	def 						A_(							self      )  ->					Tuple:




				"""simple docstring"""



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

				SCREAMING_SNAKE_CASE_								=						self.get_tokenizer(bos_token='(BOS)'      ,     eos_token='(EOS)'      )
				SCREAMING_SNAKE_CASE_								=						self.get_image_processor(do_normalize=SCREAMING_SNAKE_CASE      ,     padding_value=1.0      )

				SCREAMING_SNAKE_CASE_								=						InstructBlipProcessor.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      )
				self.assertIsInstance(processor.qformer_tokenizer      ,     SCREAMING_SNAKE_CASE      )





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




				"""simple docstring"""



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

				SCREAMING_SNAKE_CASE_								=						InstructBlipProcessor(
				    tokenizer=SCREAMING_SNAKE_CASE      ,     image_processor=SCREAMING_SNAKE_CASE      ,     qformer_tokenizer=SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						self.prepare_image_inputs()

				SCREAMING_SNAKE_CASE_								=						image_processor(SCREAMING_SNAKE_CASE      ,     return_tensors='np'      )
				SCREAMING_SNAKE_CASE_								=						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 						A_(							self      )  ->					Any:




				"""simple docstring"""



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

				SCREAMING_SNAKE_CASE_								=						InstructBlipProcessor(
				    tokenizer=SCREAMING_SNAKE_CASE      ,     image_processor=SCREAMING_SNAKE_CASE      ,     qformer_tokenizer=SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						'lower newer'

				SCREAMING_SNAKE_CASE_								=						processor(text=SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						tokenizer(SCREAMING_SNAKE_CASE      ,     return_token_type_ids=SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						qformer_tokenizer(SCREAMING_SNAKE_CASE      ,     return_token_type_ids=SCREAMING_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 						A_(							self      )  ->					Any:




				"""simple docstring"""



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

				SCREAMING_SNAKE_CASE_								=						InstructBlipProcessor(
				    tokenizer=SCREAMING_SNAKE_CASE      ,     image_processor=SCREAMING_SNAKE_CASE      ,     qformer_tokenizer=SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						'lower newer'
				SCREAMING_SNAKE_CASE_								=						self.prepare_image_inputs()

				SCREAMING_SNAKE_CASE_								=						processor(text=SCREAMING_SNAKE_CASE      ,     images=SCREAMING_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(SCREAMING_SNAKE_CASE      ):
							processor()





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




				"""simple docstring"""



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

				SCREAMING_SNAKE_CASE_								=						InstructBlipProcessor(
				    tokenizer=SCREAMING_SNAKE_CASE      ,     image_processor=SCREAMING_SNAKE_CASE      ,     qformer_tokenizer=SCREAMING_SNAKE_CASE      )

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

				SCREAMING_SNAKE_CASE_								=						processor.batch_decode(SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						tokenizer.batch_decode(SCREAMING_SNAKE_CASE      )

				self.assertListEqual(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )





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




				"""simple docstring"""



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

				SCREAMING_SNAKE_CASE_								=						InstructBlipProcessor(
				    tokenizer=SCREAMING_SNAKE_CASE      ,     image_processor=SCREAMING_SNAKE_CASE      ,     qformer_tokenizer=SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						'lower newer'
				SCREAMING_SNAKE_CASE_								=						self.prepare_image_inputs()

				SCREAMING_SNAKE_CASE_								=						processor(text=SCREAMING_SNAKE_CASE      ,     images=SCREAMING_SNAKE_CASE      )

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






from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
				import torch

if is_torch_tpu_available(check_device=False):
				import torch_xla.core.xla_model as xm


SCREAMING_SNAKE_CASE__     :     int		    =       logging.get_logger(__name__)



@dataclass
class       a_       (     SCREAMING_SNAKE_CASE__     ):
	A    							= [
	    '''no_inference''',
	    '''no_cuda''',
	    '''no_tpu''',
	    '''no_speed''',
	    '''no_memory''',
	    '''no_env_print''',
	    '''no_multi_process''',
	]





	def __init__(							self      ,     **SCREAMING_SNAKE_CASE      )  ->					Dict:




				"""simple docstring"""



				for deprecated_arg in self.deprecated_args:
							if deprecated_arg in kwargs:
										SCREAMING_SNAKE_CASE_								=						deprecated_arg[3:]
										setattr(self      ,     SCREAMING_SNAKE_CASE      ,     not kwargs.pop(SCREAMING_SNAKE_CASE      )      )
										logger.warning(
										    f'{deprecated_arg} is depreciated. Please use --no_{positive_arg} or'
										    f' {positive_arg}={kwargs[positive_arg]}'      )

				SCREAMING_SNAKE_CASE_								=						kwargs.pop('torchscript'      ,     self.torchscript      )
				SCREAMING_SNAKE_CASE_								=						kwargs.pop('torch_xla_tpu_print_metrics'      ,     self.torch_xla_tpu_print_metrics      )
				SCREAMING_SNAKE_CASE_								=						kwargs.pop('fp16_opt_level'      ,     self.fpaa_opt_level      )
				super().__init__(**SCREAMING_SNAKE_CASE      )

	A    							= field(default=SCREAMING_SNAKE_CASE__  ,    metadata={'''help''': '''Trace the models using torchscript'''}     )
	A    							= field(default=SCREAMING_SNAKE_CASE__  ,    metadata={'''help''': '''Print Xla/PyTorch tpu metrics'''}     )
	A    							= field(
	    default='''O1'''  ,    metadata={
	        '''help''': (
	            '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\']. '''
	            '''See details at https://nvidia.github.io/apex/amp.html'''
	        )
	    }  ,    )





	@cached_property
	def 						A_(							self      )  ->					Tuple["torch.device", int]:




				"""simple docstring"""



				requires_backends(self      ,     ['torch']      )
				logger.info('PyTorch: setting up devices'      )
				if not self.cuda:
							SCREAMING_SNAKE_CASE_								=						torch.device('cpu'      )
							SCREAMING_SNAKE_CASE_								=						0
				elif is_torch_tpu_available():
							SCREAMING_SNAKE_CASE_								=						xm.xla_device()
							SCREAMING_SNAKE_CASE_								=						0
				else:
							SCREAMING_SNAKE_CASE_								=						torch.device('cuda' if torch.cuda.is_available() else 'cpu'      )
							SCREAMING_SNAKE_CASE_								=						torch.cuda.device_count()
				return device, n_gpu





	@property
	def 						A_(							self      )  ->					Union[str, Any]:




				"""simple docstring"""



				return is_torch_tpu_available() and self.tpu





	@property
	def 						A_(							self      )  ->					int:




				"""simple docstring"""



				requires_backends(self      ,     ['torch']      )
				# TODO(PVP): currently only single GPU is supported
				return torch.cuda.current_device()





	@property
	def 						A_(							self      )  ->					"torch.device":




				"""simple docstring"""



				requires_backends(self      ,     ['torch']      )
				return self._setup_devices[0]





	@property
	def 						A_(							self      )  ->					Optional[Any]:




				"""simple docstring"""



				requires_backends(self      ,     ['torch']      )
				return self._setup_devices[1]





	@property
	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				return self.n_gpu > 0
