


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




			def   lowerCamelCase_					(						self							:		List[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					tempfile.mkdtemp()

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

						UpperCamelCase         =					InstructBlipProcessor(__magic_name__       ,       __magic_name__       ,       __magic_name__						)

						processor.save_pretrained(self.tmpdirname						)




			def   lowerCamelCase_					(						self							:		Optional[int]       ,       **__magic_name__							:		int						):







						"""simple docstring"""







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




			def   lowerCamelCase_					(						self							:		Any       ,       **__magic_name__							:		Any						):







						"""simple docstring"""







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




			def   lowerCamelCase_					(						self							:		Tuple       ,       **__magic_name__							:		List[str]						):







						"""simple docstring"""







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




			def   lowerCamelCase_					(						self							:		List[str]						):







						"""simple docstring"""







						shutil.rmtree(self.tmpdirname						)




			def   lowerCamelCase_					(						self							:		List[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					[np.random.randint(2_5_5       ,       size=(3, 3_0, 4_0_0)       ,       dtype=np.uinta						)]

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

						return image_inputs




			def   lowerCamelCase_					(						self							:		List[str]						):







						"""simple docstring"""







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

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

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

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

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




			def   lowerCamelCase_					(						self							:		List[Any]						):







						"""simple docstring"""







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

						UpperCamelCase         =					InstructBlipProcessor(
						    tokenizer=__magic_name__       ,       image_processor=__magic_name__       ,       qformer_tokenizer=__magic_name__						)

						UpperCamelCase         =					self.prepare_image_inputs()

						UpperCamelCase         =					image_processor(__magic_name__       ,       return_tensors="""np"""						)
						UpperCamelCase         =					processor(images=__magic_name__       ,       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   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







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

						UpperCamelCase         =					InstructBlipProcessor(
						    tokenizer=__magic_name__       ,       image_processor=__magic_name__       ,       qformer_tokenizer=__magic_name__						)

						UpperCamelCase         =					"""lower newer"""

						UpperCamelCase         =					processor(text=__magic_name__						)

						UpperCamelCase         =					tokenizer(__magic_name__       ,       return_token_type_ids=__magic_name__						)
						UpperCamelCase         =					qformer_tokenizer(__magic_name__       ,       return_token_type_ids=__magic_name__						)

						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   lowerCamelCase_					(						self							:		Optional[int]						):







						"""simple docstring"""







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

						UpperCamelCase         =					InstructBlipProcessor(
						    tokenizer=__magic_name__       ,       image_processor=__magic_name__       ,       qformer_tokenizer=__magic_name__						)

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

						UpperCamelCase         =					processor(text=__magic_name__       ,       images=__magic_name__						)

						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(__magic_name__						):
									processor()




			def   lowerCamelCase_					(						self							:		Union[str, Any]						):







						"""simple docstring"""







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

						UpperCamelCase         =					InstructBlipProcessor(
						    tokenizer=__magic_name__       ,       image_processor=__magic_name__       ,       qformer_tokenizer=__magic_name__						)

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

						UpperCamelCase         =					processor.batch_decode(__magic_name__						)
						UpperCamelCase         =					tokenizer.batch_decode(__magic_name__						)

						self.assertListEqual(__magic_name__       ,       __magic_name__						)





			def   lowerCamelCase_					(						self							:		Union[str, Any]						):







						"""simple docstring"""







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

						UpperCamelCase         =					InstructBlipProcessor(
						    tokenizer=__magic_name__       ,       image_processor=__magic_name__       ,       qformer_tokenizer=__magic_name__						)

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

						UpperCamelCase         =					processor(text=__magic_name__       ,       images=__magic_name__						)

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








from ...configuration_utils import PretrainedConfig
from ...utils import logging


__snake_case            =   logging.get_logger(__name__)

__snake_case            =   {
    "microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json",
    "microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json",
}




class    UpperCAmelCase					(       __snake_case    ):
			lowercase						= """markuplm"""




			def __init__(						self							:		Optional[Any]       ,       __magic_name__							:		List[Any]=3_0_5_2_2       ,       __magic_name__							:		int=7_6_8       ,       __magic_name__							:		List[Any]=1_2       ,       __magic_name__							:		List[Any]=1_2       ,       __magic_name__							:		str=3_0_7_2       ,       __magic_name__							:		Optional[int]="gelu"       ,       __magic_name__							:		Dict=0.1       ,       __magic_name__							:		Optional[int]=0.1       ,       __magic_name__							:		Any=5_1_2       ,       __magic_name__							:		List[str]=2       ,       __magic_name__							:		Dict=0.02       ,       __magic_name__							:		List[str]=1e-12       ,       __magic_name__							:		str=0       ,       __magic_name__							:		List[str]=0       ,       __magic_name__							:		Optional[Any]=2       ,       __magic_name__							:		Dict=2_5_6       ,       __magic_name__							:		Tuple=1_0_2_4       ,       __magic_name__							:		Any=2_1_6       ,       __magic_name__							:		str=1_0_0_1       ,       __magic_name__							:		Dict=3_2       ,       __magic_name__							:		Optional[int]=5_0       ,       __magic_name__							:		List[Any]="absolute"       ,       __magic_name__							:		Any=True       ,       __magic_name__							:		Optional[Any]=None       ,       **__magic_name__							:		List[str]       ,       ):







						"""simple docstring"""







						super().__init__(
						    pad_token_id=__magic_name__       ,       bos_token_id=__magic_name__       ,       eos_token_id=__magic_name__       ,       **__magic_name__       ,       )
						UpperCamelCase         =					vocab_size
						UpperCamelCase         =					hidden_size
						UpperCamelCase         =					num_hidden_layers
						UpperCamelCase         =					num_attention_heads
						UpperCamelCase         =					hidden_act
						UpperCamelCase         =					intermediate_size
						UpperCamelCase         =					hidden_dropout_prob
						UpperCamelCase         =					attention_probs_dropout_prob
						UpperCamelCase         =					max_position_embeddings
						UpperCamelCase         =					type_vocab_size
						UpperCamelCase         =					initializer_range
						UpperCamelCase         =					layer_norm_eps
						UpperCamelCase         =					position_embedding_type
						UpperCamelCase         =					use_cache
						UpperCamelCase         =					classifier_dropout
						# additional properties
						UpperCamelCase         =					max_depth
						UpperCamelCase         =					max_xpath_tag_unit_embeddings
						UpperCamelCase         =					max_xpath_subs_unit_embeddings
						UpperCamelCase         =					tag_pad_id
						UpperCamelCase         =					subs_pad_id
						UpperCamelCase         =					xpath_unit_hidden_size




