






"""simple docstring"""







import json
import os
import shutil
import tempfile
import unittest

import numpy as np

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


if is_vision_available():
 from PIL import Image

 from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor





@require_tokenizers
@require_vision
class   UpperCamelCase_ (       unittest.TestCase  ):



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



      """simple docstring"""





      UpperCAmelCase	    =			tempfile.mkdtemp()

      # fmt: off
      UpperCAmelCase	    =			["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""]
      # fmt: on
      UpperCAmelCase	    =			os.path.join(self.tmpdirname ,		VOCAB_FILES_NAMES["""vocab_file"""]				)
      with open(self.vocab_file ,		"""w""" ,		encoding="""utf-8"""				) as vocab_writer:
          vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]				)				)

      UpperCAmelCase	    =			{
          """do_resize""": True,
          """size""": {"""height""": 18, """width""": 18},
          """do_normalize""": True,
          """image_mean""": [0.5, 0.5, 0.5],
          """image_std""": [0.5, 0.5, 0.5],
      }
      UpperCAmelCase	    =			os.path.join(self.tmpdirname ,		snake_case__				)
      with open(self.image_processor_file ,		"""w""" ,		encoding="""utf-8"""				) as fp:
          json.dump(snake_case__ ,		snake_case__				)



  def 		UpperCamelCase_				(      self ,		**snake_case__				)			->			Optional[int]:



      """simple docstring"""





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



  def 		UpperCamelCase_				(      self ,		**snake_case__				)			->			int:



      """simple docstring"""





      return ViTImageProcessor.from_pretrained(self.tmpdirname ,		**snake_case__				)



  def 		UpperCamelCase_				(      self				)			->			int:



      """simple docstring"""





      shutil.rmtree(self.tmpdirname				)



  def 		UpperCamelCase_				(      self				)			->			Tuple:



      """simple docstring"""





      UpperCAmelCase	    =			[np.random.randint(2_55 ,		size=(3, 30, 4_00) ,		dtype=np.uinta				)]

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

      return image_inputs



  def 		UpperCamelCase_				(      self				)			->			Any:



      """simple docstring"""





      UpperCAmelCase	    =			self.get_tokenizer()
      UpperCAmelCase	    =			self.get_image_processor()

      UpperCAmelCase	    =			VisionTextDualEncoderProcessor(tokenizer=snake_case__ ,		image_processor=snake_case__				)

      processor.save_pretrained(self.tmpdirname				)
      UpperCAmelCase	    =			VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname				)

      self.assertEqual(processor.tokenizer.get_vocab() ,		tokenizer.get_vocab()				)
      self.assertIsInstance(processor.tokenizer ,		(BertTokenizer, BertTokenizerFast)				)

      self.assertEqual(processor.image_processor.to_json_string() ,		image_processor.to_json_string()				)
      self.assertIsInstance(processor.image_processor ,		snake_case__				)



  def 		UpperCamelCase_				(      self				)			->			Any:



      """simple docstring"""





      UpperCAmelCase	    =			VisionTextDualEncoderProcessor(
          tokenizer=self.get_tokenizer() ,		image_processor=self.get_image_processor()				)
      processor.save_pretrained(self.tmpdirname				)

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

      UpperCAmelCase	    =			VisionTextDualEncoderProcessor.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 ,		(BertTokenizer, BertTokenizerFast)				)

      self.assertEqual(processor.image_processor.to_json_string() ,		image_processor_add_kwargs.to_json_string()				)
      self.assertIsInstance(processor.image_processor ,		snake_case__				)



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



      """simple docstring"""





      UpperCAmelCase	    =			self.get_image_processor()
      UpperCAmelCase	    =			self.get_tokenizer()

      UpperCAmelCase	    =			VisionTextDualEncoderProcessor(tokenizer=snake_case__ ,		image_processor=snake_case__				)

      UpperCAmelCase	    =			self.prepare_image_inputs()

      UpperCAmelCase	    =			image_processor(snake_case__ ,		return_tensors="""np"""				)
      UpperCAmelCase	    =			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				)			->			List[Any]:



      """simple docstring"""





      UpperCAmelCase	    =			self.get_image_processor()
      UpperCAmelCase	    =			self.get_tokenizer()

      UpperCAmelCase	    =			VisionTextDualEncoderProcessor(tokenizer=snake_case__ ,		image_processor=snake_case__				)

      UpperCAmelCase	    =			"""lower newer"""

      UpperCAmelCase	    =			processor(text=snake_case__				)

      UpperCAmelCase	    =			tokenizer(snake_case__				)

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



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



      """simple docstring"""





      UpperCAmelCase	    =			self.get_image_processor()
      UpperCAmelCase	    =			self.get_tokenizer()

      UpperCAmelCase	    =			VisionTextDualEncoderProcessor(tokenizer=snake_case__ ,		image_processor=snake_case__				)

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

      UpperCAmelCase	    =			processor(text=snake_case__ ,		images=snake_case__				)

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

      # test if it raises when no input is passed
      with self.assertRaises(snake_case__				):
          processor()



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



      """simple docstring"""





      UpperCAmelCase	    =			self.get_image_processor()
      UpperCAmelCase	    =			self.get_tokenizer()

      UpperCAmelCase	    =			VisionTextDualEncoderProcessor(tokenizer=snake_case__ ,		image_processor=snake_case__				)

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

      UpperCAmelCase	    =			processor.batch_decode(snake_case__				)
      UpperCAmelCase	    =			tokenizer.batch_decode(snake_case__				)

      self.assertListEqual(snake_case__ ,		snake_case__				)



  def 		UpperCamelCase_				(      self				)			->			Tuple:



      """simple docstring"""





      UpperCAmelCase	    =			self.get_image_processor()
      UpperCAmelCase	    =			self.get_tokenizer()

      UpperCAmelCase	    =			VisionTextDualEncoderProcessor(tokenizer=snake_case__ ,		image_processor=snake_case__				)

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

      UpperCAmelCase	    =			processor(text=snake_case__ ,		images=snake_case__				)

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








"""simple docstring"""







from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase_	:    int            =      logging.get_logger(__name__)

lowerCAmelCase_	:    List[Any]            =      {
    '''facebook/xlm-roberta-xl''': '''https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json''',
    '''facebook/xlm-roberta-xxl''': '''https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json''',
    # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl
}





class   UpperCamelCase_ (       a_  ):
  _A       :					int				=	'xlm-roberta-xl'



  def __init__(      self ,		snake_case__=25_08_80 ,		snake_case__=25_60 ,		snake_case__=36 ,		snake_case__=32 ,		snake_case__=1_02_40 ,		snake_case__="gelu" ,		snake_case__=0.1 ,		snake_case__=0.1 ,		snake_case__=5_14 ,		snake_case__=1 ,		snake_case__=0.02 ,		snake_case__=1e-05 ,		snake_case__=1 ,		snake_case__=0 ,		snake_case__=2 ,		snake_case__="absolute" ,		snake_case__=True ,		snake_case__=None ,		**snake_case__ ,		)			->			Union[str, Any]:



         """simple docstring"""





         super().__init__(pad_token_id=snake_case__ ,		bos_token_id=snake_case__ ,		eos_token_id=snake_case__ ,		**snake_case__				)
         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





class   UpperCamelCase_ (       a_  ):



  @property
  def 		UpperCamelCase_				(      self				)			->			Mapping[str, Mapping[int, str]]:



         """simple docstring"""





         if self.task == "multiple-choice":
                UpperCAmelCase	    =			{0: """batch""", 1: """choice""", 2: """sequence"""}
         else:
                UpperCAmelCase	    =			{0: """batch""", 1: """sequence"""}
         return OrderedDict(
             [
                 ("""input_ids""", dynamic_axis),
                 ("""attention_mask""", dynamic_axis),
             ]				)
