






"""simple docstring"""







import copy
from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


if TYPE_CHECKING:
			from ... import PreTrainedTokenizerBase, TensorType

a			:    Dict				 =		logging.get_logger(__name__)


class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						__lowerCamelCase    	= "vision-encoder-decoder"
						__lowerCamelCase    	= True




						def __init__(       self	,  **snake_case__      ):
												'''simple docstring'''
												super().__init__(**snake_case__      )
												if "encoder" not in kwargs or "decoder" not in kwargs:
																		raise ValueError(
																		    F'''A configuraton of type {self.model_type} cannot be instantiated because '''
																		    F'''not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}'''      )

												lowercase__    :				Any=	kwargs.pop("encoder"      )
												lowercase__    :				List[Any]=	encoder_config.pop("model_type"      )
												lowercase__    :				Optional[int]=	kwargs.pop("decoder"      )
												lowercase__    :				int=	decoder_config.pop("model_type"      )

												lowercase__    :				Any=	AutoConfig.for_model(snake_case__	,  **snake_case__      )
												lowercase__    :				str=	AutoConfig.for_model(snake_case__	,  **snake_case__      )
												lowercase__    :				Optional[int]=	True




						@classmethod
						def 		UpperCAmelCase_	(       cls	,  snake_case__	,  snake_case__	,  **snake_case__      ):
												'''simple docstring'''
												logger.info("Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config"      )
												lowercase__    :				Union[str, Any]=	True
												lowercase__    :				Optional[Any]=	True

												return cls(encoder=encoder_config.to_dict()	,  decoder=decoder_config.to_dict()	,  **snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	copy.deepcopy(self.__dict__      )
												lowercase__    :				Union[str, Any]=	self.encoder.to_dict()
												lowercase__    :				Optional[Any]=	self.decoder.to_dict()
												lowercase__    :				int=	self.__class__.model_type
												return output


class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						__lowerCamelCase    	= version.parse("1.11"							)




						@property
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return OrderedDict(
												    [
												        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
												    ]      )




						@property
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return 1e-4




						@property
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return OrderedDict({"last_hidden_state": {0: "batch", 1: "encoder_sequence"}}      )


class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						@property
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				int=	OrderedDict()
												lowercase__    :				List[str]=	{0: "batch", 1: "past_decoder_sequence + sequence"}
												lowercase__    :				str=	{0: "batch", 1: "past_decoder_sequence + sequence"}
												lowercase__    :				List[str]=	{0: "batch", 1: "encoder_sequence"}

												return common_inputs




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__ = -1	,  snake_case__ = -1	,  snake_case__ = False	,  snake_case__ = None	,  ):
												'''simple docstring'''
												import torch

												lowercase__    :				str=	OrderedDict()

												lowercase__    :				Tuple=	super().generate_dummy_inputs(
												    snake_case__	,  batch_size=snake_case__	,  seq_length=snake_case__	,  is_pair=snake_case__	,  framework=snake_case__      )

												lowercase__,     lowercase__    :				Union[str, Any]=	dummy_input["input_ids"].shape
												lowercase__    :				Union[str, Any]=	(batch, encoder_sequence, self._config.encoder_hidden_size)
												lowercase__    :				List[Any]=	dummy_input.pop("input_ids"      )
												lowercase__    :				Optional[Any]=	dummy_input.pop("attention_mask"      )
												lowercase__    :				Union[str, Any]=	torch.zeros(snake_case__      )

												return common_inputs


class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						@property
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												pass




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




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__	,  snake_case__ = "default"      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	encoder_config.hidden_size
												return VisionEncoderDecoderDecoderOnnxConfig(snake_case__	,  snake_case__      )














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


@require_vision
class    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	tempfile.mkdtemp()

												lowercase__    :				Union[str, Any]=	BlipImageProcessor()
												lowercase__    :				Any=	GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model"      )
												lowercase__    :				Any=	BertTokenizerFast.from_pretrained("hf-internal-testing/tiny-random-bert"      )

												lowercase__    :				str=	InstructBlipProcessor(snake_case__	,  snake_case__	,  snake_case__      )

												processor.save_pretrained(self.tmpdirname      )




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).tokenizer




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).image_processor




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).qformer_tokenizer




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												shutil.rmtree(self.tmpdirname      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Union[str, Any]=	[np.random.randint(255	,  size=(3, 30, 400)	,  dtype=np.uinta      )]

												lowercase__    :				Union[str, Any]=	[Image.fromarray(np.moveaxis(snake_case__	,  0	,  -1      )      ) for x in image_inputs]

												return image_inputs




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				str=	InstructBlipProcessor(
												    tokenizer=self.get_tokenizer()	,  image_processor=self.get_image_processor()	,  qformer_tokenizer=self.get_qformer_tokenizer()	,  )
												processor.save_pretrained(self.tmpdirname      )

												lowercase__    :				Optional[Any]=	self.get_tokenizer(bos_token="(BOS)"	,  eos_token="(EOS)"      )
												lowercase__    :				Optional[Any]=	self.get_image_processor(do_normalize=snake_case__	,  padding_value=1.0      )

												lowercase__    :				Optional[Any]=	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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Any=	self.get_qformer_tokenizer()

												lowercase__    :				Any=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Tuple=	self.prepare_image_inputs()

												lowercase__    :				int=	image_processor(snake_case__	,  return_tensors="np"      )
												lowercase__    :				List[str]=	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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	self.get_image_processor()
												lowercase__    :				List[str]=	self.get_tokenizer()
												lowercase__    :				Optional[Any]=	self.get_qformer_tokenizer()

												lowercase__    :				Optional[Any]=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Optional[Any]=	"lower newer"

												lowercase__    :				Any=	processor(text=snake_case__      )

												lowercase__    :				Union[str, Any]=	tokenizer(snake_case__	,  return_token_type_ids=snake_case__      )
												lowercase__    :				List[Any]=	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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				int=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Any=	self.get_qformer_tokenizer()

												lowercase__    :				Optional[int]=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

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

												lowercase__    :				Dict=	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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[Any]=	self.get_image_processor()
												lowercase__    :				int=	self.get_tokenizer()
												lowercase__    :				Optional[int]=	self.get_qformer_tokenizer()

												lowercase__    :				Any=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Dict=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

												lowercase__    :				Optional[Any]=	processor.batch_decode(snake_case__      )
												lowercase__    :				List[Any]=	tokenizer.batch_decode(snake_case__      )

												self.assertListEqual(snake_case__	,  snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				str=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Optional[Any]=	self.get_qformer_tokenizer()

												lowercase__    :				int=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

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

												lowercase__    :				Dict=	processor(text=snake_case__	,  images=snake_case__      )

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






