



"""simple docstring"""

import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    ErnieMForInformationExtraction,
							    ErnieMForMultipleChoice,
							    ErnieMForQuestionAnswering,
							    ErnieMForSequenceClassification,
							    ErnieMForTokenClassification,
							    ErnieMModel,
							)
							from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST
class 						lowerCAmelCase__	:


	'''simple docstring'''







	def __init__(			self							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					int=13    ,					_SCREAMING_SNAKE_CASE							:					Tuple=7    ,					_SCREAMING_SNAKE_CASE							:					Tuple=True    ,					_SCREAMING_SNAKE_CASE							:					int=True    ,					_SCREAMING_SNAKE_CASE							:					str=True    ,					_SCREAMING_SNAKE_CASE							:					str=99    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=32    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=5    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=4    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=37    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]="gelu"    ,					_SCREAMING_SNAKE_CASE							:					Tuple=0.1    ,					_SCREAMING_SNAKE_CASE							:					Any=0.1    ,					_SCREAMING_SNAKE_CASE							:					List[str]=512    ,					_SCREAMING_SNAKE_CASE							:					Tuple=16    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=2    ,					_SCREAMING_SNAKE_CASE							:					str=0.0_2    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=3    ,					_SCREAMING_SNAKE_CASE							:					Dict=4    ,					_SCREAMING_SNAKE_CASE							:					List[str]=None    ,					)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    parent
					SCREAMING_SNAKE_CASE	:       List[str] 							=    batch_size
					SCREAMING_SNAKE_CASE	:       int 							=    seq_length
					SCREAMING_SNAKE_CASE	:       List[str] 							=    is_training
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    use_input_mask
					SCREAMING_SNAKE_CASE	:       List[str] 							=    use_labels
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    vocab_size
					SCREAMING_SNAKE_CASE	:       Dict 							=    hidden_size
					SCREAMING_SNAKE_CASE	:       str 							=    num_hidden_layers
					SCREAMING_SNAKE_CASE	:       Tuple 							=    num_attention_heads
					SCREAMING_SNAKE_CASE	:       Dict 							=    intermediate_size
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    hidden_act
					SCREAMING_SNAKE_CASE	:       str 							=    hidden_dropout_prob
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    max_position_embeddings
					SCREAMING_SNAKE_CASE	:       Any 							=    type_vocab_size
					SCREAMING_SNAKE_CASE	:       int 							=    type_sequence_label_size
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    initializer_range
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    num_labels
					SCREAMING_SNAKE_CASE	:       Tuple 							=    num_choices
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    scope





	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size						)

					SCREAMING_SNAKE_CASE	:       Any 							=    None
					if self.use_input_mask:
									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    random_attention_mask([self.batch_size, self.seq_length]						)

					SCREAMING_SNAKE_CASE	:       Tuple 							=    None
					SCREAMING_SNAKE_CASE	:       Dict 							=    None
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    None
					if self.use_labels:
									SCREAMING_SNAKE_CASE	:       Any 							=    ids_tensor([self.batch_size]    ,					self.type_sequence_label_size						)
									SCREAMING_SNAKE_CASE	:       List[str] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					self.num_labels						)
									SCREAMING_SNAKE_CASE	:       Optional[int] 							=    ids_tensor([self.batch_size]    ,					self.num_choices						)

					SCREAMING_SNAKE_CASE	:       Dict 							=    self.get_config()

					return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels





	def  _lowerCAmelCase    (			self							:					List[str]						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size						)

					SCREAMING_SNAKE_CASE	:       Dict 							=    None
					if self.use_input_mask:
									SCREAMING_SNAKE_CASE	:       str 							=    random_attention_mask([self.batch_size, self.seq_length]						)
					SCREAMING_SNAKE_CASE	:       str 							=    self.get_config()

					return config, input_ids, input_mask





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





					"""simple docstring"""


					return ErnieMConfig(
					    vocab_size=self.vocab_size    ,					hidden_size=self.hidden_size    ,					num_hidden_layers=self.num_hidden_layers    ,					num_attention_heads=self.num_attention_heads    ,					intermediate_size=self.intermediate_size    ,					hidden_act=self.hidden_act    ,					hidden_dropout_prob=self.hidden_dropout_prob    ,					attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,					max_position_embeddings=self.max_position_embeddings    ,					type_vocab_size=self.type_vocab_size    ,					initializer_range=self.initializer_range    ,					)





	def  _lowerCAmelCase    (			self							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Tuple						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    ErnieMModel(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Tuple 							=    model(_SCREAMING_SNAKE_CASE    ,					return_dict=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, self.seq_length, self.hidden_size)						)





	def  _lowerCAmelCase    (			self							:					int    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					str						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    ErnieMForQuestionAnswering(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Dict 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					start_positions=_SCREAMING_SNAKE_CASE    ,					end_positions=_SCREAMING_SNAKE_CASE    ,					)
					self.parent.assertEqual(result.start_logits.shape    ,					(self.batch_size, self.seq_length)						)
					self.parent.assertEqual(result.end_logits.shape    ,					(self.batch_size, self.seq_length)						)





	def  _lowerCAmelCase    (			self							:					str    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					int						)	->  Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    ErnieMForInformationExtraction(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       int 							=    torch.ones_like(_SCREAMING_SNAKE_CASE    ,					dtype=torch.floataa						)
					SCREAMING_SNAKE_CASE	:       Any 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					start_positions=_SCREAMING_SNAKE_CASE    ,					end_positions=_SCREAMING_SNAKE_CASE    ,					)
					self.parent.assertEqual(result.start_logits.shape    ,					(self.batch_size, self.seq_length)						)
					self.parent.assertEqual(result.end_logits.shape    ,					(self.batch_size, self.seq_length)						)





	def  _lowerCAmelCase    (			self							:					int    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					int						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       Tuple 							=    ErnieMForSequenceClassification(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       Any 							=    ErnieMForTokenClassification(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					input_ids.to(_SCREAMING_SNAKE_CASE						)
					input_mask.to(_SCREAMING_SNAKE_CASE						)
					token_labels.to(_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       str 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)

					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.num_labels)						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					int						)	->  Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.num_choices
					SCREAMING_SNAKE_CASE	:       int 							=    ErnieMForMultipleChoice(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Dict 							=    input_ids.unsqueeze(1						).expand(-1    ,					self.num_choices    ,					-1						).contiguous()
					SCREAMING_SNAKE_CASE	:       Any 							=    input_mask.unsqueeze(1						).expand(-1    ,					self.num_choices    ,					-1						).contiguous()
					SCREAMING_SNAKE_CASE	:       int 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE    ,					)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_choices)						)







	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.prepare_config_and_inputs()
					(
					    (
					    SCREAMING_SNAKE_CASE
					)							,	(
					    SCREAMING_SNAKE_CASE
					)							,	(
					    SCREAMING_SNAKE_CASE
					)							,	(
					    SCREAMING_SNAKE_CASE
					)							,	(
					    SCREAMING_SNAKE_CASE
					)							,	(
					    SCREAMING_SNAKE_CASE
					)							,	
					)	:       Union[str, Any] 							=    config_and_inputs
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    {'input_ids': input_ids, 'attention_mask': input_mask}
					return config, inputs_dict




@require_torch
class 						lowerCAmelCase__	(  _lowerCamelCase       ,       _lowerCamelCase       ,       unittest.TestCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		Dict     	=   (
	    (
	        ErnieMModel,
	        ErnieMForMultipleChoice,
	        ErnieMForQuestionAnswering,
	        ErnieMForSequenceClassification,
	        ErnieMForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	_SCREAMING_SNAKE_CASE  :		Tuple     	=   ()
	_SCREAMING_SNAKE_CASE  :		int     	=   (
	    {
	        '''feature-extraction''': ErnieMModel,
	        '''question-answering''': ErnieMForQuestionAnswering,
	        '''text-classification''': ErnieMForSequenceClassification,
	        '''token-classification''': ErnieMForTokenClassification,
	        '''zero-shot''': ErnieMForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_SCREAMING_SNAKE_CASE  :		Optional[int]     	=   False





	def  _lowerCAmelCase    (			self							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]						)	->  Union[str, Any]:





					"""simple docstring"""


					if pipeline_test_casse_name == "QAPipelineTests":
									return True

					return False





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    ErnieMModelTester(self						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    ConfigTester(self    ,					config_class=_SCREAMING_SNAKE_CASE    ,					hidden_size=37						)





	def  _lowerCAmelCase    (			self							:					List[str]						)	->  Dict:





					"""simple docstring"""


					self.config_tester.run_common_tests()





	def  _lowerCAmelCase    (			self							:					int						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									SCREAMING_SNAKE_CASE	:       Any 							=    type
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					List[Any]						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Dict						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_information_extraction(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  List[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE						)







	@slow
	def  _lowerCAmelCase    (			self							:					int						)	->  Union[str, Any]:





					"""simple docstring"""


					for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE	:       Tuple 							=    ErnieMModel.from_pretrained(_SCREAMING_SNAKE_CASE						)
									self.assertIsNotNone(_SCREAMING_SNAKE_CASE						)




@require_torch
class 						lowerCAmelCase__	(  unittest.TestCase   ):


	'''simple docstring'''







	@slow
	def  _lowerCAmelCase    (			self							:					int						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Tuple 							=    torch.tensor([[0, 1, 2, 3, 4, 5]]						)
					SCREAMING_SNAKE_CASE	:       Tuple 							=    model(_SCREAMING_SNAKE_CASE						)[0]

					# TODO Replace vocab size
					SCREAMING_SNAKE_CASE	:       int 							=    768

					SCREAMING_SNAKE_CASE	:       str 							=    torch.Size((1, 6, hidden_size)						)
					self.assertEqual(output.shape    ,					_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Any 							=    torch.tensor(
					    [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]]						)

					self.assertTrue(torch.allclose(output[:, :3, :3]    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-3						)						)










"""simple docstring"""

import json
import os
import random
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
							from PIL import Image

							from transformers import FlavaImageProcessor, FlavaProcessor
							from transformers.models.flava.image_processing_flava import (
							    FLAVA_CODEBOOK_MEAN,
							    FLAVA_CODEBOOK_STD,
							    FLAVA_IMAGE_MEAN,
							    FLAVA_IMAGE_STD,
							)
@require_vision
class 						lowerCAmelCase__	(  unittest.TestCase   ):


	'''simple docstring'''







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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    tempfile.mkdtemp()

					# fmt: off
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest']
					# fmt: on
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    os.path.join(self.tmpdirname    ,					VOCAB_FILES_NAMES['vocab_file']						)

					with open(self.vocab_file    ,					'w'    ,					encoding='utf-8'						) as fp:
									fp.write(''.join([x + '\n' for x in vocab_tokens]						)						)

					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    {
					    'image_mean': FLAVA_IMAGE_MEAN,
					    'image_std': FLAVA_IMAGE_STD,
					    'do_normalize': True,
					    'do_resize': True,
					    'size': 224,
					    'do_center_crop': True,
					    'crop_size': 224,
					    'input_size_patches': 14,
					    'total_mask_patches': 75,
					    'mask_group_max_patches': None,
					    'mask_group_min_patches': 16,
					    'mask_group_min_aspect_ratio': 0.3,
					    'mask_group_max_aspect_ratio': None,
					    'codebook_do_resize': True,
					    'codebook_size': 112,
					    'codebook_do_center_crop': True,
					    'codebook_crop_size': 112,
					    'codebook_do_map_pixels': True,
					    'codebook_do_normalize': True,
					    'codebook_image_mean': FLAVA_CODEBOOK_MEAN,
					    'codebook_image_std': FLAVA_CODEBOOK_STD,
					}

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    os.path.join(self.tmpdirname    ,					_SCREAMING_SNAKE_CASE						)
					with open(self.image_processor_file    ,					'w'    ,					encoding='utf-8'						) as fp:
									json.dump(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Any    ,					**_SCREAMING_SNAKE_CASE							:					str						)	->  str:





					"""simple docstring"""


					return BertTokenizer.from_pretrained(self.tmpdirname    ,					**_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					int    ,					**_SCREAMING_SNAKE_CASE							:					Any						)	->  Union[str, Any]:





					"""simple docstring"""


					return BertTokenizerFast.from_pretrained(self.tmpdirname    ,					**_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]    ,					**_SCREAMING_SNAKE_CASE							:					Dict						)	->  str:





					"""simple docstring"""


					return FlavaImageProcessor.from_pretrained(self.tmpdirname    ,					**_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Dict						)	->  str:





					"""simple docstring"""


					shutil.rmtree(self.tmpdirname						)





	def  _lowerCAmelCase    (			self							:					int						)	->  Optional[Any]:





					"""simple docstring"""


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

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

					return image_inputs





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    self.get_tokenizer()
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.get_rust_tokenizer()
					SCREAMING_SNAKE_CASE	:       str 							=    self.get_image_processor()

					SCREAMING_SNAKE_CASE	:       int 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)
					processor_slow.save_pretrained(self.tmpdirname						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    FlavaProcessor.from_pretrained(self.tmpdirname    ,					use_fast=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       str 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)
					processor_fast.save_pretrained(self.tmpdirname						)
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    FlavaProcessor.from_pretrained(self.tmpdirname						)

					self.assertEqual(processor_slow.tokenizer.get_vocab()    ,					tokenizer_slow.get_vocab()						)
					self.assertEqual(processor_fast.tokenizer.get_vocab()    ,					tokenizer_fast.get_vocab()						)
					self.assertEqual(tokenizer_slow.get_vocab()    ,					tokenizer_fast.get_vocab()						)
					self.assertIsInstance(processor_slow.tokenizer    ,					_SCREAMING_SNAKE_CASE						)
					self.assertIsInstance(processor_fast.tokenizer    ,					_SCREAMING_SNAKE_CASE						)

					self.assertEqual(processor_slow.image_processor.to_json_string()    ,					image_processor.to_json_string()						)
					self.assertEqual(processor_fast.image_processor.to_json_string()    ,					image_processor.to_json_string()						)
					self.assertIsInstance(processor_slow.image_processor    ,					_SCREAMING_SNAKE_CASE						)
					self.assertIsInstance(processor_fast.image_processor    ,					_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    FlavaProcessor(tokenizer=self.get_tokenizer()    ,					image_processor=self.get_image_processor()						)
					processor.save_pretrained(self.tmpdirname						)

					SCREAMING_SNAKE_CASE	:       Dict 							=    self.get_tokenizer(bos_token='(BOS)'    ,					eos_token='(EOS)'						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.get_image_processor(do_normalize=_SCREAMING_SNAKE_CASE    ,					padding_value=1.0						)

					SCREAMING_SNAKE_CASE	:       List[str] 							=    FlavaProcessor.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  _lowerCAmelCase    (			self							:					List[Any]						)	->  Tuple:





					"""simple docstring"""


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

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.prepare_image_inputs()

					SCREAMING_SNAKE_CASE	:       Any 							=    image_processor(_SCREAMING_SNAKE_CASE    ,					return_tensors='np'						)
					SCREAMING_SNAKE_CASE	:       str 							=    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						)

					# With rest of the args
					random.seed(1_234						)
					SCREAMING_SNAKE_CASE	:       str 							=    image_processor(
					    _SCREAMING_SNAKE_CASE    ,					return_image_mask=_SCREAMING_SNAKE_CASE    ,					return_codebook_pixels=_SCREAMING_SNAKE_CASE    ,					return_tensors='np'						)
					random.seed(1_234						)
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    processor(
					    images=_SCREAMING_SNAKE_CASE    ,					return_image_mask=_SCREAMING_SNAKE_CASE    ,					return_codebook_pixels=_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  _lowerCAmelCase    (			self							:					Any						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    self.get_image_processor()
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.get_tokenizer()

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    'lower newer'

					SCREAMING_SNAKE_CASE	:       List[str] 							=    processor(text=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       List[str] 							=    tokenizer(_SCREAMING_SNAKE_CASE						)

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





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.get_image_processor()
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.get_tokenizer()

					SCREAMING_SNAKE_CASE	:       str 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    'lower newer'
					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.prepare_image_inputs()

					SCREAMING_SNAKE_CASE	:       List[str] 							=    processor(text=_SCREAMING_SNAKE_CASE    ,					images=_SCREAMING_SNAKE_CASE						)

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

					# add extra args
					SCREAMING_SNAKE_CASE	:       Any 							=    processor(text=_SCREAMING_SNAKE_CASE    ,					images=_SCREAMING_SNAKE_CASE    ,					return_codebook_pixels=_SCREAMING_SNAKE_CASE    ,					return_image_mask=_SCREAMING_SNAKE_CASE						)

					self.assertListEqual(
					    list(inputs.keys()						)    ,					[
					        'input_ids',
					        'token_type_ids',
					        'attention_mask',
					        'pixel_values',
					        'codebook_pixel_values',
					        'bool_masked_pos',
					    ]    ,					)

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





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





					"""simple docstring"""


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

					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    processor.batch_decode(_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    tokenizer.batch_decode(_SCREAMING_SNAKE_CASE						)

					self.assertListEqual(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)







	def  _lowerCAmelCase    (			self							:					int						)	->  Any:





					"""simple docstring"""


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

					SCREAMING_SNAKE_CASE	:       int 							=    FlavaProcessor(tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Any 							=    'lower newer'
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.prepare_image_inputs()

					SCREAMING_SNAKE_CASE	:       Dict 							=    processor(text=_SCREAMING_SNAKE_CASE    ,					images=_SCREAMING_SNAKE_CASE						)

					self.assertListEqual(list(inputs.keys()						)    ,					processor.model_input_names						)





