

'''simple docstring'''



import torch
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.bert.modeling_bert import (
    BERT_INPUTS_DOCSTRING,
    BERT_START_DOCSTRING,
    BertEmbeddings,
    BertLayer,
    BertPooler,
    BertPreTrainedModel,
)







def    a__       (  a__						):




     """simple docstring"""


     __SCREAMING_SNAKE_CASE     =      torch.exp(a__						)
     __SCREAMING_SNAKE_CASE     =      torch.sum(a__      ,    dim=1						)  # sum of exp(x_i)
     __SCREAMING_SNAKE_CASE     =      torch.sum(x * exp_x      ,    dim=1						)  # sum of x_i * exp(x_i)
     return torch.log(a__						) - B / A







class 						lowerCAmelCase__   (      nn.Module					):



 """simple docstring"""



 def __init__(		self					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       List[Any]			)  -> Optional[int]:


      """simple docstring"""




      super().__init__()
      __SCREAMING_SNAKE_CASE     =      config.output_attentions
      __SCREAMING_SNAKE_CASE     =      config.output_hidden_states
      __SCREAMING_SNAKE_CASE     =      nn.ModuleList([BertLayer(__SCREAMING_SNAKE_CASE			) for _ in range(config.num_hidden_layers			)]			)
      __SCREAMING_SNAKE_CASE     =      nn.ModuleList([BertHighway(__SCREAMING_SNAKE_CASE			) for _ in range(config.num_hidden_layers			)]			)

      __SCREAMING_SNAKE_CASE     =      [-1 for _ in range(config.num_hidden_layers			)]
 def       UpperCAmelCase__		(		self					:       int					,							__SCREAMING_SNAKE_CASE					:       List[Any]			)  -> int:


      """simple docstring"""




      if (type(__SCREAMING_SNAKE_CASE			) is float) or (type(__SCREAMING_SNAKE_CASE			) is int):
           for i in range(len(self.early_exit_entropy			)			):
                __SCREAMING_SNAKE_CASE     =      x
      else:
           __SCREAMING_SNAKE_CASE     =      x
 def       UpperCAmelCase__		(		self					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       int			)  -> Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      pooler.state_dict()
      for highway in self.highway:
           for name, param in highway.pooler.state_dict().items():
                param.copy_(loaded_model[name]			)


 def       UpperCAmelCase__		(		self					:       Optional[int]					,							__SCREAMING_SNAKE_CASE					:       int					,							__SCREAMING_SNAKE_CASE					:       str=None					,							__SCREAMING_SNAKE_CASE					:       Tuple=None					,							__SCREAMING_SNAKE_CASE					:       int=None					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]=None					,							)  -> Optional[int]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      ()
      __SCREAMING_SNAKE_CASE     =      ()
      __SCREAMING_SNAKE_CASE     =      ()
      for i, layer_module in enumerate(self.layer			):
           if self.output_hidden_states:
                __SCREAMING_SNAKE_CASE     =      all_hidden_states + (hidden_states,)

           __SCREAMING_SNAKE_CASE     =      layer_module(
               __SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE					,							head_mask[i]					,							__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE			)
           __SCREAMING_SNAKE_CASE     =      layer_outputs[0]

           if self.output_attentions:
                __SCREAMING_SNAKE_CASE     =      all_attentions + (layer_outputs[1],)

           __SCREAMING_SNAKE_CASE     =      (hidden_states,)
           if self.output_hidden_states:
                __SCREAMING_SNAKE_CASE     =      current_outputs + (all_hidden_states,)
           if self.output_attentions:
                __SCREAMING_SNAKE_CASE     =      current_outputs + (all_attentions,)

           __SCREAMING_SNAKE_CASE     =      self.highway[i](__SCREAMING_SNAKE_CASE			)
           # logits, pooled_output

           if not self.training:
                __SCREAMING_SNAKE_CASE     =      highway_exit[0]
                __SCREAMING_SNAKE_CASE     =      entropy(__SCREAMING_SNAKE_CASE			)
                __SCREAMING_SNAKE_CASE     =      highway_exit + (highway_entropy,)  # logits, hidden_states(?), entropy
                __SCREAMING_SNAKE_CASE     =      all_highway_exits + (highway_exit,)

                if highway_entropy < self.early_exit_entropy[i]:
                     __SCREAMING_SNAKE_CASE     =      (highway_logits,) + current_outputs[1:] + (all_highway_exits,)
                     raise HighwayException(__SCREAMING_SNAKE_CASE					,							i + 1			)
           else:
                __SCREAMING_SNAKE_CASE     =      all_highway_exits + (highway_exit,)

        # Add last layer
      if self.output_hidden_states:
           __SCREAMING_SNAKE_CASE     =      all_hidden_states + (hidden_states,)

      __SCREAMING_SNAKE_CASE     =      (hidden_states,)
      if self.output_hidden_states:
           __SCREAMING_SNAKE_CASE     =      outputs + (all_hidden_states,)
      if self.output_attentions:
           __SCREAMING_SNAKE_CASE     =      outputs + (all_attentions,)

      __SCREAMING_SNAKE_CASE     =      outputs + (all_highway_exits,)
      return outputs  # last-layer hidden state, (all hidden states), (all attentions), all highway exits







@add_start_docstrings(
    "The Bert Model transformer with early exiting (DeeBERT). " ,					a ,					)
class 						lowerCAmelCase__   (      a					):



 """simple docstring"""



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


      """simple docstring"""




      super().__init__(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      config

      __SCREAMING_SNAKE_CASE     =      BertEmbeddings(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      DeeBertEncoder(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      BertPooler(__SCREAMING_SNAKE_CASE			)

      self.init_weights()
 def       UpperCAmelCase__		(		self					:       Any			)  -> Optional[int]:


      """simple docstring"""




      self.encoder.init_highway_pooler(self.pooler			)
 def       UpperCAmelCase__		(		self					:       Dict			)  -> str:


      """simple docstring"""




      return self.embeddings.word_embeddings
 def       UpperCAmelCase__		(		self					:       int					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]			)  -> List[str]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      value
 def       UpperCAmelCase__		(		self					:       Dict					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]			)  -> Optional[int]:


      """simple docstring"""




      for layer, heads in heads_to_prune.items():
           self.encoder.layer[layer].attention.prune_heads(__SCREAMING_SNAKE_CASE			)


 @add_start_docstrings_to_model_forward(__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       List[Any]					,							__SCREAMING_SNAKE_CASE					:       int=None					,							__SCREAMING_SNAKE_CASE					:       Any=None					,							__SCREAMING_SNAKE_CASE					:       str=None					,							__SCREAMING_SNAKE_CASE					:       int=None					,							__SCREAMING_SNAKE_CASE					:       List[str]=None					,							__SCREAMING_SNAKE_CASE					:       List[str]=None					,							__SCREAMING_SNAKE_CASE					:       Optional[int]=None					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]=None					,							)  -> Optional[Any]:


      """simple docstring"""




      if input_ids is not None and inputs_embeds is not None:
           raise ValueError("""You cannot specify both input_ids and inputs_embeds at the same time"""			)
      elif input_ids is not None:
           __SCREAMING_SNAKE_CASE     =      input_ids.size()
      elif inputs_embeds is not None:
           __SCREAMING_SNAKE_CASE     =      inputs_embeds.size()[:-1]
      else:
           raise ValueError("""You have to specify either input_ids or inputs_embeds"""			)

      __SCREAMING_SNAKE_CASE     =      input_ids.device if input_ids is not None else inputs_embeds.device

      if attention_mask is None:
           __SCREAMING_SNAKE_CASE     =      torch.ones(__SCREAMING_SNAKE_CASE					,							device=__SCREAMING_SNAKE_CASE			)
      if encoder_attention_mask is None:
           __SCREAMING_SNAKE_CASE     =      torch.ones(__SCREAMING_SNAKE_CASE					,							device=__SCREAMING_SNAKE_CASE			)
      if token_type_ids is None:
           __SCREAMING_SNAKE_CASE     =      torch.zeros(__SCREAMING_SNAKE_CASE					,							dtype=torch.long					,							device=__SCREAMING_SNAKE_CASE			)

      # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
      # ourselves in which case we just need to make it broadcastable to all heads.
      __SCREAMING_SNAKE_CASE     =      self.get_extended_attention_mask(__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE			)

      # If a 2D ou 3D attention mask is provided for the cross-attention
      # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
      if encoder_attention_mask.dim() == 3:
           __SCREAMING_SNAKE_CASE     =      encoder_attention_mask[:, None, :, :]
      if encoder_attention_mask.dim() == 2:
           __SCREAMING_SNAKE_CASE     =      encoder_attention_mask[:, None, None, :]

      __SCREAMING_SNAKE_CASE     =      encoder_extended_attention_mask.to(
          dtype=next(self.parameters()			).dtype			)  # fp16 compatibility
      __SCREAMING_SNAKE_CASE     =      (1.0 - encoder_extended_attention_mask) * -10000.0

      # Prepare head mask if needed
      # 1.0 in head_mask indicate we keep the head
      # attention_probs has shape bsz x n_heads x N x N
      # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
      # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
      __SCREAMING_SNAKE_CASE     =      self.get_head_mask(__SCREAMING_SNAKE_CASE					,							self.config.num_hidden_layers			)

      __SCREAMING_SNAKE_CASE     =      self.embeddings(
          input_ids=__SCREAMING_SNAKE_CASE					,							position_ids=__SCREAMING_SNAKE_CASE					,							token_type_ids=__SCREAMING_SNAKE_CASE					,							inputs_embeds=__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      self.encoder(
          __SCREAMING_SNAKE_CASE					,							attention_mask=__SCREAMING_SNAKE_CASE					,							head_mask=__SCREAMING_SNAKE_CASE					,							encoder_hidden_states=__SCREAMING_SNAKE_CASE					,							encoder_attention_mask=__SCREAMING_SNAKE_CASE					,							)
      __SCREAMING_SNAKE_CASE     =      encoder_outputs[0]
      __SCREAMING_SNAKE_CASE     =      self.pooler(__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      (
          sequence_output,
          pooled_output,
      ) + encoder_outputs[
          1:
      ]  # add hidden_states and attentions if they are here
      return outputs  # sequence_output, pooled_output, (hidden_states), (attentions), highway exits







class 						lowerCAmelCase__   (      a					):



 """simple docstring"""



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


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      message
      __SCREAMING_SNAKE_CASE     =      exit_layer  # start from 1!







class 						lowerCAmelCase__   (      nn.Module					):



 """simple docstring"""



 def __init__(		self					:       Dict					,							__SCREAMING_SNAKE_CASE					:       int			)  -> str:


      """simple docstring"""




      super().__init__()
      __SCREAMING_SNAKE_CASE     =      BertPooler(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      nn.Dropout(config.hidden_dropout_prob			)
      __SCREAMING_SNAKE_CASE     =      nn.Linear(config.hidden_size					,							config.num_labels			)


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


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      encoder_outputs[0]
      __SCREAMING_SNAKE_CASE     =      self.pooler(__SCREAMING_SNAKE_CASE			)
      # "return" pooler_output

      # BertModel
      __SCREAMING_SNAKE_CASE     =      (pooler_input, pooler_output) + encoder_outputs[1:]
      # "return" bmodel_output

      # Dropout and classification
      __SCREAMING_SNAKE_CASE     =      bmodel_output[1]

      __SCREAMING_SNAKE_CASE     =      self.dropout(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      self.classifier(__SCREAMING_SNAKE_CASE			)

      return logits, pooled_output







@add_start_docstrings(
    "Bert Model (with early exiting - DeeBERT) with a classifier on top,\n    also takes care of multi-layer training. " ,					a ,					)
class 						lowerCAmelCase__   (      a					):



 """simple docstring"""



 def __init__(		self					:       Union[str, Any]					,							__SCREAMING_SNAKE_CASE					:       List[str]			)  -> Optional[Any]:


      """simple docstring"""




      super().__init__(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      config.num_labels
      __SCREAMING_SNAKE_CASE     =      config.num_hidden_layers

      __SCREAMING_SNAKE_CASE     =      DeeBertModel(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      nn.Dropout(config.hidden_dropout_prob			)
      __SCREAMING_SNAKE_CASE     =      nn.Linear(config.hidden_size					,							self.config.num_labels			)

      self.init_weights()


 @add_start_docstrings_to_model_forward(__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       List[Any]=None					,							__SCREAMING_SNAKE_CASE					:       Any=None					,							__SCREAMING_SNAKE_CASE					:       Any=None					,							__SCREAMING_SNAKE_CASE					:       Any=None					,							__SCREAMING_SNAKE_CASE					:       int=None					,							__SCREAMING_SNAKE_CASE					:       Optional[int]=None					,							__SCREAMING_SNAKE_CASE					:       str=None					,							__SCREAMING_SNAKE_CASE					:       Optional[int]=-1					,							__SCREAMING_SNAKE_CASE					:       int=False					,							)  -> Any:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.num_layers
      try:
           __SCREAMING_SNAKE_CASE     =      self.bert(
               __SCREAMING_SNAKE_CASE					,							attention_mask=__SCREAMING_SNAKE_CASE					,							token_type_ids=__SCREAMING_SNAKE_CASE					,							position_ids=__SCREAMING_SNAKE_CASE					,							head_mask=__SCREAMING_SNAKE_CASE					,							inputs_embeds=__SCREAMING_SNAKE_CASE					,							)
           # sequence_output, pooled_output, (hidden_states), (attentions), highway exits

           __SCREAMING_SNAKE_CASE     =      outputs[1]

           __SCREAMING_SNAKE_CASE     =      self.dropout(__SCREAMING_SNAKE_CASE			)
           __SCREAMING_SNAKE_CASE     =      self.classifier(__SCREAMING_SNAKE_CASE			)
           __SCREAMING_SNAKE_CASE     =      (logits,) + outputs[2:]  # add hidden states and attention if they are here
      except HighwayException as e:
           __SCREAMING_SNAKE_CASE     =      e.message
           __SCREAMING_SNAKE_CASE     =      e.exit_layer
           __SCREAMING_SNAKE_CASE     =      outputs[0]

      if not self.training:
           __SCREAMING_SNAKE_CASE     =      entropy(__SCREAMING_SNAKE_CASE			)
           __SCREAMING_SNAKE_CASE     =      []
           __SCREAMING_SNAKE_CASE     =      []
      if labels is not None:
           if self.num_labels == 1:
                #  We are doing regression
                __SCREAMING_SNAKE_CASE     =      MSELoss()
                __SCREAMING_SNAKE_CASE     =      loss_fct(logits.view(-1			)					,							labels.view(-1			)			)
           else:
                __SCREAMING_SNAKE_CASE     =      CrossEntropyLoss()
                __SCREAMING_SNAKE_CASE     =      loss_fct(logits.view(-1					,							self.num_labels			)					,							labels.view(-1			)			)

           # work with highway exits
           __SCREAMING_SNAKE_CASE     =      []
           for highway_exit in outputs[-1]:
                __SCREAMING_SNAKE_CASE     =      highway_exit[0]
                if not self.training:
                     highway_logits_all.append(__SCREAMING_SNAKE_CASE			)
                     highway_entropy.append(highway_exit[2]			)
                if self.num_labels == 1:
                     #  We are doing regression
                     __SCREAMING_SNAKE_CASE     =      MSELoss()
                     __SCREAMING_SNAKE_CASE     =      loss_fct(highway_logits.view(-1			)					,							labels.view(-1			)			)
                else:
                     __SCREAMING_SNAKE_CASE     =      CrossEntropyLoss()
                     __SCREAMING_SNAKE_CASE     =      loss_fct(highway_logits.view(-1					,							self.num_labels			)					,							labels.view(-1			)			)
                highway_losses.append(__SCREAMING_SNAKE_CASE			)

           if train_highway:
                __SCREAMING_SNAKE_CASE     =      (sum(highway_losses[:-1]			),) + outputs
                # exclude the final highway, of course
           else:
                __SCREAMING_SNAKE_CASE     =      (loss,) + outputs
      if not self.training:
           __SCREAMING_SNAKE_CASE     =      outputs + ((original_entropy, highway_entropy), exit_layer)
           if output_layer >= 0:
                __SCREAMING_SNAKE_CASE     =      (
                    (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
                )  # use the highway of the last layer

      return outputs  # (loss), logits, (hidden_states), (attentions), (highway_exits)









'''simple docstring'''



import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip 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 CLIPImageProcessor, CLIPProcessor







@require_vision
class 						lowerCAmelCase__   (      unittest.TestCase					):



 """simple docstring"""



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


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      tempfile.mkdtemp()

      # fmt: off
      __SCREAMING_SNAKE_CASE     =      ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
      # fmt: on
      __SCREAMING_SNAKE_CASE     =      dict(zip(__SCREAMING_SNAKE_CASE					,							range(len(__SCREAMING_SNAKE_CASE			)			)			)			)
      __SCREAMING_SNAKE_CASE     =      ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
      __SCREAMING_SNAKE_CASE     =      {"""unk_token""": """<unk>"""}

      __SCREAMING_SNAKE_CASE     =      os.path.join(self.tmpdirname					,							VOCAB_FILES_NAMES["""vocab_file"""]			)
      __SCREAMING_SNAKE_CASE     =      os.path.join(self.tmpdirname					,							VOCAB_FILES_NAMES["""merges_file"""]			)
      with open(self.vocab_file					,							"""w"""					,							encoding="""utf-8"""			) as fp:
           fp.write(json.dumps(__SCREAMING_SNAKE_CASE			) + """\n"""			)
      with open(self.merges_file					,							"""w"""					,							encoding="""utf-8"""			) as fp:
           fp.write("""\n""".join(__SCREAMING_SNAKE_CASE			)			)

      __SCREAMING_SNAKE_CASE     =      {
          """do_resize""": True,
          """size""": 20,
          """do_center_crop""": True,
          """crop_size""": 18,
          """do_normalize""": True,
          """image_mean""": [0.48145466, 0.4578275, 0.40821073],
          """image_std""": [0.26862954, 0.26130258, 0.27577711],
      }
      __SCREAMING_SNAKE_CASE     =      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       UpperCAmelCase__		(		self					:       Optional[int]					,							**__SCREAMING_SNAKE_CASE					:       Optional[int]			)  -> str:


      """simple docstring"""




      return CLIPTokenizer.from_pretrained(self.tmpdirname					,							**__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Tuple					,							**__SCREAMING_SNAKE_CASE					:       Any			)  -> int:


      """simple docstring"""




      return CLIPTokenizerFast.from_pretrained(self.tmpdirname					,							**__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Optional[Any]					,							**__SCREAMING_SNAKE_CASE					:       Optional[int]			)  -> List[str]:


      """simple docstring"""




      return CLIPImageProcessor.from_pretrained(self.tmpdirname					,							**__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Tuple			)  -> str:


      """simple docstring"""




      shutil.rmtree(self.tmpdirname			)
 def       UpperCAmelCase__		(		self					:       Optional[Any]			)  -> str:


      """simple docstring"""




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

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

      return image_inputs
 def       UpperCAmelCase__		(		self					:       Optional[int]			)  -> Any:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()
      __SCREAMING_SNAKE_CASE     =      self.get_rust_tokenizer()
      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)
      processor_slow.save_pretrained(self.tmpdirname			)
      __SCREAMING_SNAKE_CASE     =      CLIPProcessor.from_pretrained(self.tmpdirname					,							use_fast=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)
      processor_fast.save_pretrained(self.tmpdirname			)
      __SCREAMING_SNAKE_CASE     =      CLIPProcessor.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       UpperCAmelCase__		(		self					:       Optional[int]			)  -> Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=self.get_tokenizer()					,							image_processor=self.get_image_processor()			)
      processor.save_pretrained(self.tmpdirname			)

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

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor.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       UpperCAmelCase__		(		self					:       List[str]			)  -> Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE     =      image_processor(__SCREAMING_SNAKE_CASE					,							return_tensors="""np"""			)
      __SCREAMING_SNAKE_CASE     =      processor(images=__SCREAMING_SNAKE_CASE					,							return_tensors="""np"""			)

      for key in input_image_proc.keys():
           self.assertAlmostEqual(input_image_proc[key].sum()					,							input_processor[key].sum()					,							delta=1E-2			)
 def       UpperCAmelCase__		(		self					:       List[Any]			)  -> int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      """lower newer"""

      __SCREAMING_SNAKE_CASE     =      processor(text=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      tokenizer(__SCREAMING_SNAKE_CASE			)

      for key in encoded_tok.keys():
           self.assertListEqual(encoded_tok[key]					,							encoded_processor[key]			)
 def       UpperCAmelCase__		(		self					:       Dict			)  -> Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

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

      __SCREAMING_SNAKE_CASE     =      processor(text=__SCREAMING_SNAKE_CASE					,							images=__SCREAMING_SNAKE_CASE			)

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

      # test if it raises when no input is passed
      with pytest.raises(__SCREAMING_SNAKE_CASE			):
           processor()
 def       UpperCAmelCase__		(		self					:       List[str]			)  -> Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

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

      __SCREAMING_SNAKE_CASE     =      processor.batch_decode(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      tokenizer.batch_decode(__SCREAMING_SNAKE_CASE			)

      self.assertListEqual(__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE			)


 def       UpperCAmelCase__		(		self					:       int			)  -> str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

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

      __SCREAMING_SNAKE_CASE     =      processor(text=__SCREAMING_SNAKE_CASE					,							images=__SCREAMING_SNAKE_CASE			)

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






