






import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
         FalconForCausalLM,
         FalconForQuestionAnswering,
         FalconForSequenceClassification,
         FalconForTokenClassification,
         FalconModel,
     )






class 		lowercase_					:






      '''simple docstring'''




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




       """simple docstring"""


       a		  =      parent
       a		  =      batch_size
       a		  =      seq_length
       a		  =      is_training
       a		  =      use_input_mask
       a		  =      use_token_type_ids
       a		  =      use_labels
       a		  =      vocab_size
       a		  =      hidden_size
       a		  =      num_hidden_layers
       a		  =      num_attention_heads
       a		  =      intermediate_size
       a		  =      hidden_act
       a		  =      hidden_dropout_prob
       a		  =      attention_probs_dropout_prob
       a		  =      max_position_embeddings
       a		  =      type_vocab_size
       a		  =      type_sequence_label_size
       a		  =      initializer_range
       a		  =      num_labels
       a		  =      num_choices
       a		  =      scope







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




       """simple docstring"""


       a		  =      ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size  )

       a		  =      None
       if self.use_input_mask:
        a		  =      random_attention_mask([self.batch_size, self.seq_length]  )

       a		  =      None

       a		  =      None
       a		  =      None
       a		  =      None
       if self.use_labels:
        a		  =      ids_tensor([self.batch_size] ,					self.type_sequence_label_size  )
        a		  =      ids_tensor([self.batch_size, self.seq_length] ,					self.num_labels  )
        a		  =      ids_tensor([self.batch_size] ,					self.num_choices  )

       a		  =      self.get_config()

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







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




       """simple docstring"""


       return FalconConfig(
           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 ,					is_decoder=__UpperCAmelCase ,					initializer_range=self.initializer_range ,					pad_token_id=1 ,					new_decoder_architecture=__UpperCAmelCase ,					)







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




       """simple docstring"""


       a		  =      FalconModel(config=__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase  )
       a		  =      model(__UpperCAmelCase  )
       self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)  )







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




       """simple docstring"""


       a		  =      True
       a		  =      FalconModel(__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      model(
           __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					)
       a		  =      model(
           __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					)
       a		  =      model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase  )
       self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)  )







      def       __lowerCAmelCase					(      self      :						List[Any] ,					__UpperCAmelCase      :						Dict ,					__UpperCAmelCase      :						int ,					__UpperCAmelCase      :						List[str] ,					__UpperCAmelCase      :						Optional[int] ,					__UpperCAmelCase      :						List[str] ,					__UpperCAmelCase      :						Any ,					__UpperCAmelCase      :						Optional[int] ,					__UpperCAmelCase      :						str ,					__UpperCAmelCase      :						List[str] ,					)    ->Optional[Any]:




       """simple docstring"""


       a		  =      FalconForCausalLM(config=__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					labels=__UpperCAmelCase  )
       self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)  )







      def       __lowerCAmelCase					(      self      :						Dict ,					__UpperCAmelCase      :						Optional[Any] ,					__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						Dict ,					__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						str ,					__UpperCAmelCase      :						Tuple ,					__UpperCAmelCase      :						int ,					__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						Optional[int] ,					)    ->str:




       """simple docstring"""


       a		  =      True
       a		  =      True
       a		  =      FalconForCausalLM(config=__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()

       # first forward pass
       a		  =      model(
           __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					use_cache=__UpperCAmelCase ,					)
       a		  =      outputs.past_key_values

       # create hypothetical multiple next token and extent to next_input_ids
       a		  =      ids_tensor((self.batch_size, 3) ,					config.vocab_size  )
       a		  =      ids_tensor((self.batch_size, 3) ,					vocab_size=2  )

       # append to next input_ids and
       a		  =      torch.cat([input_ids, next_tokens] ,					dim=-1  )
       a		  =      torch.cat([input_mask, next_mask] ,					dim=-1  )

       a		  =      model(
           __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					output_hidden_states=__UpperCAmelCase ,					)['''hidden_states'''][0]
       a		  =      model(
           __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					past_key_values=__UpperCAmelCase ,					output_hidden_states=__UpperCAmelCase ,					)['''hidden_states'''][0]

       # select random slice
       a		  =      ids_tensor((1,) ,					output_from_past.shape[-1]  ).item()
       a		  =      output_from_no_past[:, -3:, random_slice_idx].detach()
       a		  =      output_from_past[:, :, random_slice_idx].detach()

       self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]  )

       # test that outputs are equal for slice
       self.parent.assertTrue(torch.allclose(__UpperCAmelCase ,					__UpperCAmelCase ,					atol=1e-3  )  )







      def       __lowerCAmelCase					(      self      :						Any  )    ->Tuple:




       """simple docstring"""


       a		  =      self.prepare_config_and_inputs()
       (
           (
           a
       )  ,     (
           a
       )  ,     (
           a
       )  ,     (
           a
       )  ,     (
           a
       )  ,     (
           a
       )  ,     (
           a
       )  ,     
       )		  =      config_and_inputs
       a		  =      {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict






@require_torch
class 		lowercase_					(	lowercase ,  lowercase ,  lowercase ,  unittest.TestCase   ):






      '''simple docstring'''




      __snake_case       =  (
          (
              FalconModel,
              FalconForCausalLM,
              FalconForSequenceClassification,
              FalconForTokenClassification,
              FalconForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      __snake_case       =  (FalconForCausalLM,) if is_torch_available() else ()
      __snake_case       =  (
          {
              '''feature-extraction''': FalconModel,
              '''text-classification''': FalconForSequenceClassification,
              '''text-generation''': FalconForCausalLM,
              '''question-answering''': FalconForQuestionAnswering,
              '''token-classification''': FalconForTokenClassification,
              '''zero-shot''': FalconForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      __snake_case       =  False
      __snake_case       =  False







      def       __lowerCAmelCase					(      self      :						Dict  )    ->str:




       """simple docstring"""


       a		  =      FalconModelTester(self  )
       a		  =      ConfigTester(self ,					config_class=__UpperCAmelCase ,					hidden_size=37  )







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




       """simple docstring"""


       self.config_tester.run_common_tests()







      def       __lowerCAmelCase					(      self      :						List[str]  )    ->Tuple:




       """simple docstring"""


       a		  =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__UpperCAmelCase  )







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




       """simple docstring"""


       a  ,     *a		  =      self.model_tester.prepare_config_and_inputs()
       for alibi in [True, False]:
        a		  =      alibi
        self.model_tester.create_and_check_model(__UpperCAmelCase ,					*__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Dict  )    ->int:




       """simple docstring"""


       a  ,     a		  =      self.model_tester.prepare_config_and_inputs_for_common()
       a		  =      3
       a		  =      input_dict['''input_ids''']
       a		  =      input_ids.ne(1  ).to(__UpperCAmelCase  )
       a		  =      ids_tensor([self.model_tester.batch_size] ,					self.model_tester.type_sequence_label_size  )
       a		  =      FalconForSequenceClassification(__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					labels=__UpperCAmelCase  )
       self.assertEqual(result.logits.shape ,					(self.model_tester.batch_size, self.model_tester.num_labels)  )







      def       __lowerCAmelCase					(      self      :						str  )    ->Tuple:




       """simple docstring"""


       a  ,     a		  =      self.model_tester.prepare_config_and_inputs_for_common()
       a		  =      3
       a		  =      '''single_label_classification'''
       a		  =      input_dict['''input_ids''']
       a		  =      input_ids.ne(1  ).to(__UpperCAmelCase  )
       a		  =      ids_tensor([self.model_tester.batch_size] ,					self.model_tester.type_sequence_label_size  )
       a		  =      FalconForSequenceClassification(__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					labels=__UpperCAmelCase  )
       self.assertEqual(result.logits.shape ,					(self.model_tester.batch_size, self.model_tester.num_labels)  )







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




       """simple docstring"""


       a  ,     a		  =      self.model_tester.prepare_config_and_inputs_for_common()
       a		  =      input_dict['''input_ids''']
       a		  =      FalconForCausalLM(__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      model(__UpperCAmelCase ,					use_cache=__UpperCAmelCase  )
       a		  =      input_ids.shape[0]
       a		  =      model._convert_to_rw_cache(result.past_key_values  )
       a		  =      model._convert_cache_to_standard_format(__UpperCAmelCase ,					__UpperCAmelCase  )
       for layer in range(len(__UpperCAmelCase  )  ):
        for tensor_idx in range(2  ):
         self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3  )
         self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4  )
         self.assertTrue(
             torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]  )  )







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




       """simple docstring"""


       a  ,     a		  =      self.model_tester.prepare_config_and_inputs_for_common()
       a		  =      3
       a		  =      '''multi_label_classification'''
       a		  =      input_dict['''input_ids''']
       a		  =      input_ids.ne(1  ).to(__UpperCAmelCase  )
       a		  =      ids_tensor(
           [self.model_tester.batch_size, config.num_labels] ,					self.model_tester.type_sequence_label_size  ).to(torch.float  )
       a		  =      FalconForSequenceClassification(__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					labels=__UpperCAmelCase  )
       self.assertEqual(result.logits.shape ,					(self.model_tester.batch_size, self.model_tester.num_labels)  )







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




       """simple docstring"""


       for model_class in self.all_generative_model_classes:
        a  ,     a		  =      self.model_tester.prepare_config_and_inputs_for_common()

        # If it doesn't support cache, pass the test
        if not hasattr(__UpperCAmelCase ,					'''use_cache'''  ):
         return

        a		  =      model_class(__UpperCAmelCase  ).to(__UpperCAmelCase  )
        if "use_cache" not in inputs:
         a		  =      True
        a		  =      model(**__UpperCAmelCase  )

        # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
        if "past_key_values" not in outputs:
         return

        a		  =      (
            getattr(__UpperCAmelCase ,					'''decoder_layers''' ,					__UpperCAmelCase  )
            or getattr(__UpperCAmelCase ,					'''num_decoder_layers''' ,					__UpperCAmelCase  )
            or config.num_hidden_layers
        )
        a		  =      getattr(__UpperCAmelCase ,					'''num_kv_heads''' ,					config.num_attention_heads  )
        a		  =      getattr(__UpperCAmelCase ,					'''d_model''' ,					config.hidden_size  )
        a		  =      embed_dim // num_attention_heads

        a		  =      outputs['''past_key_values''']
        self.assertEqual(len(__UpperCAmelCase  ) ,					__UpperCAmelCase  )

        a  ,     a		  =      inputs['''input_ids'''].shape
        for i in range(__UpperCAmelCase  ):
         if config.new_decoder_architecture:
          a		  =      config.num_attention_heads
         elif config.multi_query:
          a		  =      1
         self.assertEqual(len(past_kv[0]  ) ,					2  )  # K V for the decoder = 2
         self.assertEqual(
             past_kv[i][0].shape ,					(batch_size, num_attention_heads, seq_length, per_head_embed_dim)  )
         self.assertEqual(
             past_kv[i][1].shape ,					(batch_size, num_attention_heads, seq_length, per_head_embed_dim)  )






@require_torch
class 		lowercase_					(	unittest.TestCase   ):






      '''simple docstring'''




      @slow
      def       __lowerCAmelCase					(      self      :						Dict  )    ->Tuple:




       """simple docstring"""


       a		  =      AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''  )
       a		  =      FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''  )
       model.eval()
       model.to(__UpperCAmelCase  )
       a		  =      tokenizer('''My favorite food is''' ,					return_tensors='''pt'''  ).to(__UpperCAmelCase  )

       a		  =      (
           '''My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'''
       )

       a		  =      model.generate(**__UpperCAmelCase ,					do_sample=__UpperCAmelCase ,					max_new_tokens=19  )
       a		  =      tokenizer.batch_decode(__UpperCAmelCase  )[0]

       self.assertEqual(__UpperCAmelCase ,					__UpperCAmelCase  )







      @slow
      def       __lowerCAmelCase					(      self      :						Union[str, Any]  )    ->List[str]:




       """simple docstring"""


       for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
        a		  =      AutoTokenizer.from_pretrained(__UpperCAmelCase  )
        a		  =      FalconForCausalLM.from_pretrained(__UpperCAmelCase  )
        model.eval()
        model.to(__UpperCAmelCase  )
        a		  =      tokenizer('''My favorite food is''' ,					return_tensors='''pt'''  ).to(__UpperCAmelCase  )

        # We just test that these run without errors - the models are randomly initialized
        # and so the actual text outputs will be garbage
        model.generate(**__UpperCAmelCase ,					do_sample=__UpperCAmelCase ,					max_new_tokens=4  )
        model.generate(**__UpperCAmelCase ,					do_sample=__UpperCAmelCase ,					max_new_tokens=4  )
        model.generate(**__UpperCAmelCase ,					num_beams=2 ,					max_new_tokens=4  )







      @slow
      def       __lowerCAmelCase					(      self      :						Tuple  )    ->Dict:




       """simple docstring"""


       with torch.no_grad():
        for repo in [
            "Rocketknight1/falcon-rw-1b",
            "Rocketknight1/tiny-random-falcon-7b",
            "Rocketknight1/tiny-random-falcon-40b",
        ]:
         a		  =      AutoTokenizer.from_pretrained(__UpperCAmelCase  )
         a		  =      FalconForCausalLM.from_pretrained(__UpperCAmelCase  )
         model.eval()
         model.to(device=__UpperCAmelCase  )
         a		  =      tokenizer('''My favorite food is''' ,					return_tensors='''pt'''  ).to(__UpperCAmelCase  )

         # Test results are the same with and without cache
         a		  =      model.generate(**__UpperCAmelCase ,					do_sample=__UpperCAmelCase ,					max_new_tokens=20 ,					use_cache=__UpperCAmelCase  )
         a		  =      model.generate(**__UpperCAmelCase ,					do_sample=__UpperCAmelCase ,					max_new_tokens=20 ,					use_cache=__UpperCAmelCase  )
         self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0  )










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






      '''simple docstring'''




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




       """simple docstring"""


       a		  =      tempfile.mkdtemp()

       # fmt: off
       a		  =      ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''']
       # fmt: on
       a		  =      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]  )  )

       a		  =      {
           '''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],
       }
       a		  =      os.path.join(self.tmpdirname ,					__UpperCAmelCase  )
       with open(self.image_processor_file ,					'''w''' ,					encoding='''utf-8'''  ) as fp:
        json.dump(__UpperCAmelCase ,					__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						List[Any] ,					**__UpperCAmelCase      :						List[Any]  )    ->int:




       """simple docstring"""


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







      def       __lowerCAmelCase					(      self      :						Union[str, Any] ,					**__UpperCAmelCase      :						Optional[int]  )    ->Union[str, Any]:




       """simple docstring"""


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







      def       __lowerCAmelCase					(      self      :						Tuple  )    ->Any:




       """simple docstring"""


       shutil.rmtree(self.tmpdirname  )







      def       __lowerCAmelCase					(      self      :						str  )    ->Dict:




       """simple docstring"""


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

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

       return image_inputs







      def       __lowerCAmelCase					(      self      :						Any  )    ->Tuple:




       """simple docstring"""


       a		  =      self.get_tokenizer()
       a		  =      self.get_image_processor()

       a		  =      VisionTextDualEncoderProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase  )

       processor.save_pretrained(self.tmpdirname  )
       a		  =      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 ,					__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						int  )    ->Dict:




       """simple docstring"""


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

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

       a		  =      VisionTextDualEncoderProcessor.from_pretrained(
           self.tmpdirname ,					bos_token='''(BOS)''' ,					eos_token='''(EOS)''' ,					do_normalize=__UpperCAmelCase ,					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 ,					__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Tuple  )    ->Dict:




       """simple docstring"""


       a		  =      self.get_image_processor()
       a		  =      self.get_tokenizer()

       a		  =      VisionTextDualEncoderProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase  )

       a		  =      self.prepare_image_inputs()

       a		  =      image_processor(__UpperCAmelCase ,					return_tensors='''np'''  )
       a		  =      processor(images=__UpperCAmelCase ,					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      :						List[str]  )    ->str:




       """simple docstring"""


       a		  =      self.get_image_processor()
       a		  =      self.get_tokenizer()

       a		  =      VisionTextDualEncoderProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase  )

       a		  =      '''lower newer'''

       a		  =      processor(text=__UpperCAmelCase  )

       a		  =      tokenizer(__UpperCAmelCase  )

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







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




       """simple docstring"""


       a		  =      self.get_image_processor()
       a		  =      self.get_tokenizer()

       a		  =      VisionTextDualEncoderProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase  )

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

       a		  =      processor(text=__UpperCAmelCase ,					images=__UpperCAmelCase  )

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







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




       """simple docstring"""


       a		  =      self.get_image_processor()
       a		  =      self.get_tokenizer()

       a		  =      VisionTextDualEncoderProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase  )

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

       a		  =      processor.batch_decode(__UpperCAmelCase  )
       a		  =      tokenizer.batch_decode(__UpperCAmelCase  )

       self.assertListEqual(__UpperCAmelCase ,					__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Optional[Any]  )    ->Dict:




       """simple docstring"""


       a		  =      self.get_image_processor()
       a		  =      self.get_tokenizer()

       a		  =      VisionTextDualEncoderProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase  )

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

       a		  =      processor(text=__UpperCAmelCase ,					images=__UpperCAmelCase  )

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


