



"""simple docstring"""







import tempfile
import unittest

from transformers import TaConfig, is_torch_available
from transformers.testing_utils import (
    require_sentencepiece,
    require_tokenizers,
    require_torch,
    slow,
    torch_device,
)

from ...generation.test_utils import GenerationTesterMixin
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel
class        __A       :







		'''simple docstring'''





		def __init__(	self						:  int			,_snake_case						:  Optional[int]			,_snake_case						:  Dict=99			,_snake_case						:  Dict=13			,_snake_case						:  str=7			,_snake_case						:  int=9			,_snake_case						:  List[Any]=True			,_snake_case						:  Dict=True			,_snake_case						:  Optional[int]=False			,_snake_case						:  Union[str, Any]=32			,_snake_case						:  List[Any]=5			,_snake_case						:  Optional[Any]=4			,_snake_case						:  List[Any]=37			,_snake_case						:  Any=8			,_snake_case						:  Any=0.1			,_snake_case						:  Any=0.002			,_snake_case						:  List[str]=1			,_snake_case						:  Optional[int]=0			,_snake_case						:  str=0			,_snake_case						:  List[Any]=None			,_snake_case						:  Optional[int]=None			,)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    str				=			parent
				lowercase__   :    List[Any]				=			batch_size
				lowercase__   :    Any				=			encoder_seq_length
				lowercase__   :    str				=			decoder_seq_length
				# For common tests
				lowercase__   :    Any				=			self.decoder_seq_length
				lowercase__   :    str				=			is_training
				lowercase__   :    List[Any]				=			use_attention_mask
				lowercase__   :    Union[str, Any]				=			use_labels
				lowercase__   :    int				=			vocab_size
				lowercase__   :    Optional[int]				=			hidden_size
				lowercase__   :    Optional[Any]				=			num_hidden_layers
				lowercase__   :    List[str]				=			num_attention_heads
				lowercase__   :    Optional[Any]				=			d_ff
				lowercase__   :    List[Any]				=			relative_attention_num_buckets
				lowercase__   :    Optional[Any]				=			dropout_rate
				lowercase__   :    Union[str, Any]				=			initializer_factor
				lowercase__   :    Tuple				=			eos_token_id
				lowercase__   :    List[Any]				=			pad_token_id
				lowercase__   :    List[Any]				=			decoder_start_token_id
				lowercase__   :    int				=			None
				lowercase__   :    Union[str, Any]				=			decoder_layers







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






				"""simple docstring"""



				return TaConfig.from_pretrained('''google/umt5-base'''						)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  Union[str, Any]			,_snake_case						:  Tuple			,_snake_case						:  str			,_snake_case						:  int=None			,_snake_case						:  List[Any]=None			,_snake_case						:  Any=None			,_snake_case						:  List[str]=None			,_snake_case						:  Optional[Any]=None			,)					->		Tuple:






				"""simple docstring"""



				if attention_mask is None:
						lowercase__   :    str				=			input_ids.ne(config.pad_token_id						)
				if decoder_attention_mask is None:
						lowercase__   :    int				=			decoder_input_ids.ne(config.pad_token_id						)
				if head_mask is None:
						lowercase__   :    int				=			torch.ones(config.num_hidden_layers			,config.num_attention_heads			,device=__SCREAMING_SNAKE_CASE						)
				if decoder_head_mask is None:
						lowercase__   :    Tuple				=			torch.ones(config.num_decoder_layers			,config.num_attention_heads			,device=__SCREAMING_SNAKE_CASE						)
				if cross_attn_head_mask is None:
						lowercase__   :    int				=			torch.ones(
						    config.num_decoder_layers			,config.num_attention_heads			,device=__SCREAMING_SNAKE_CASE						)
				return {
				    "input_ids": input_ids,
				    "decoder_input_ids": decoder_input_ids,
				    "attention_mask": attention_mask,
				    "decoder_attention_mask": decoder_attention_mask,
				    "head_mask": head_mask,
				    "decoder_head_mask": decoder_head_mask,
				    "cross_attn_head_mask": cross_attn_head_mask,
				}







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






				"""simple docstring"""



				lowercase__   :    int				=			ids_tensor([self.batch_size, self.encoder_seq_length]			,self.vocab_size						)
				lowercase__   :    List[Any]				=			ids_tensor([self.batch_size, self.decoder_seq_length]			,self.vocab_size						)

				# we need to clamp the input ids here to avoid having pad token in between
				# this is because for NllbMoe the position_ids are prepared such that
				# all pad tokens have pos id = 2 and rest are between 2..seq_length
				# and the seq_length here is seq_length - num_pad_tokens
				# but when using past, there is no way of knowing if the past input ids had
				# pad tokens in them, which results in incorrect seq_lenth and which in turn results in
				# position_ids being off by num_pad_tokens in past input
				lowercase__   :    Optional[int]				=			input_ids.clamp(self.pad_token_id + 1						)
				lowercase__   :    Tuple				=			decoder_input_ids.clamp(self.pad_token_id + 1						)

				lowercase__   :    Tuple				=			self.get_config()
				lowercase__   :    str				=			config.num_attention_heads
				lowercase__   :    str				=			self.prepare_inputs_dict(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE						)
				return config, input_dict







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		int:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    Dict				=			self.prepare_config_and_inputs()
				return config, inputs_dict







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



				return TaConfig(
				    vocab_size=166			,d_model=self.hidden_size			,d_ff=self.d_ff			,d_kv=self.hidden_size // self.num_attention_heads			,num_layers=self.num_hidden_layers			,num_decoder_layers=self.decoder_layers			,num_heads=self.num_attention_heads			,relative_attention_num_buckets=self.relative_attention_num_buckets			,dropout_rate=self.dropout_rate			,initializer_factor=self.initializer_factor			,eos_token_id=self.eos_token_id			,bos_token_id=self.pad_token_id			,pad_token_id=self.pad_token_id			,decoder_start_token_id=self.decoder_start_token_id			,)







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






				"""simple docstring"""



				return TaConfig(
				    vocab_size=self.vocab_size			,d_model=self.hidden_size			,d_ff=self.d_ff			,d_kv=self.hidden_size // self.num_attention_heads			,num_layers=self.num_hidden_layers			,num_decoder_layers=self.decoder_layers			,num_heads=self.num_attention_heads			,relative_attention_num_buckets=self.relative_attention_num_buckets			,dropout_rate=self.dropout_rate			,initializer_factor=self.initializer_factor			,eos_token_id=self.eos_token_id			,bos_token_id=self.pad_token_id			,pad_token_id=self.pad_token_id			,decoder_start_token_id=self.decoder_start_token_id			,)







		def 					UpperCAmelCase    (	self						:  List[str]			,_snake_case						:  List[str]			,_snake_case						:  Optional[int]			,_snake_case						:  int			,_snake_case						:  Tuple			,_snake_case						:  int			,_snake_case						:  List[str]			,)					->		Tuple:






				"""simple docstring"""



				lowercase__   :    List[str]				=			UMTaModel(config=__SCREAMING_SNAKE_CASE						)
				model.to(__SCREAMING_SNAKE_CASE						)
				model.eval()
				lowercase__   :    int				=			model(
				    input_ids=__SCREAMING_SNAKE_CASE			,decoder_input_ids=__SCREAMING_SNAKE_CASE			,attention_mask=__SCREAMING_SNAKE_CASE			,decoder_attention_mask=__SCREAMING_SNAKE_CASE			,)
				lowercase__   :    Optional[int]				=			model(input_ids=__SCREAMING_SNAKE_CASE			,decoder_input_ids=__SCREAMING_SNAKE_CASE						)
				lowercase__   :    Optional[Any]				=			result.last_hidden_state
				lowercase__   :    Dict				=			result.past_key_values
				lowercase__   :    str				=			result.encoder_last_hidden_state

				self.parent.assertEqual(encoder_output.size()			,(self.batch_size, self.encoder_seq_length, self.hidden_size)						)
				self.parent.assertEqual(decoder_output.size()			,(self.batch_size, self.decoder_seq_length, self.hidden_size)						)
				# There should be `num_layers` key value embeddings stored in decoder_past
				self.parent.assertEqual(len(__SCREAMING_SNAKE_CASE						)			,config.num_layers						)
				# There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple
				self.parent.assertEqual(len(decoder_past[0]						)			,4						)







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






				"""simple docstring"""



				lowercase__   :    int				=			UMTaModel(config=__SCREAMING_SNAKE_CASE						).get_decoder().to(__SCREAMING_SNAKE_CASE						).eval()
				# first forward pass
				lowercase__   :    Optional[Any]				=			model(__SCREAMING_SNAKE_CASE			,use_cache=__SCREAMING_SNAKE_CASE						)
				lowercase__   :    List[str]				=			model(__SCREAMING_SNAKE_CASE						)
				lowercase__   :    str				=			model(__SCREAMING_SNAKE_CASE			,use_cache=__SCREAMING_SNAKE_CASE						)

				self.parent.assertTrue(len(__SCREAMING_SNAKE_CASE						) == len(__SCREAMING_SNAKE_CASE						)						)
				self.parent.assertTrue(len(__SCREAMING_SNAKE_CASE						) == len(__SCREAMING_SNAKE_CASE						) + 1						)

				lowercase__		,			lowercase__   :    List[Any]				=			outputs.to_tuple()

				# create hypothetical next token and extent to next_input_ids
				lowercase__   :    Union[str, Any]				=			ids_tensor((self.batch_size, 1)			,config.vocab_size						)

				# append to next input_ids and
				lowercase__   :    Optional[Any]				=			torch.cat([input_ids, next_tokens]			,dim=-1						)

				lowercase__   :    str				=			model(__SCREAMING_SNAKE_CASE						)['''last_hidden_state''']
				lowercase__   :    Union[str, Any]				=			model(__SCREAMING_SNAKE_CASE			,past_key_values=__SCREAMING_SNAKE_CASE						)['''last_hidden_state''']

				# select random slice
				lowercase__   :    Optional[Any]				=			ids_tensor((1,)			,output_from_past.shape[-1]						).item()
				lowercase__   :    List[Any]				=			output_from_no_past[:, -1, random_slice_idx].detach()
				lowercase__   :    Tuple				=			output_from_past[:, 0, random_slice_idx].detach()

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







		def 					UpperCAmelCase    (	self						:  Tuple			,_snake_case						:  Union[str, Any]			,_snake_case						:  Tuple			,)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			UMTaModel(config=__SCREAMING_SNAKE_CASE						).to(__SCREAMING_SNAKE_CASE						).half().eval()
				lowercase__   :    List[Any]				=			model(**__SCREAMING_SNAKE_CASE						)['''last_hidden_state''']
				self.parent.assertFalse(torch.isnan(__SCREAMING_SNAKE_CASE						).any().item()						)





@require_torch
class        __A       (	A_     ,A_     ,A_     ,unittest.TestCase    ):







		'''simple docstring'''





		lowerCAmelCase			:					Optional[int]   	=				(
		    (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else ()
		)
		lowerCAmelCase			:					Tuple   	=				(UMTaForConditionalGeneration,) if is_torch_available() else ()
		lowerCAmelCase			:					List[Any]   	=				(
		    {
		        "conversational": UMTaForConditionalGeneration,
		        "feature-extraction": UMTaModel,
		        "summarization": UMTaForConditionalGeneration,
		        "text2text-generation": UMTaForConditionalGeneration,
		        "translation": UMTaForConditionalGeneration,
		        "question-answering": UMTaForQuestionAnswering,
		    }
		    if is_torch_available()
		    else {}
		)
		lowerCAmelCase			:					int   	=				True
		lowerCAmelCase			:					List[str]   	=				False
		lowerCAmelCase			:					Optional[int]   	=				False
		lowerCAmelCase			:					Optional[int]   	=				True
		lowerCAmelCase			:					Dict   	=				True
		# The small UMT5 model needs higher percentages for CPU/MP tests
		lowerCAmelCase			:					Optional[int]   	=				[0.8, 0.9]







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






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			UMTaModelTester(self						)







		@unittest.skip('''Test has a segmentation fault on torch 1.8.0'''						)
		def 					UpperCAmelCase    (	self						:  str						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			self.model_tester.prepare_config_and_inputs()
				lowercase__   :    Optional[int]				=			UMTaModel(config_and_inputs[0]						).to(__SCREAMING_SNAKE_CASE						)
				with tempfile.TemporaryDirectory() as tmpdirname:
						torch.onnx.export(
						    __SCREAMING_SNAKE_CASE			,(config_and_inputs[1], config_and_inputs[3], config_and_inputs[2])			,f"""{tmpdirname}/t5_test.onnx"""			,export_params=__SCREAMING_SNAKE_CASE			,opset_version=9			,input_names=['''input_ids''', '''decoder_input_ids''']			,)







		@unittest.skipIf(torch_device == '''cpu'''			,'''Cant do half precision'''						)
		def 					UpperCAmelCase    (	self						:  Any						)					->		Tuple:






				"""simple docstring"""



				lowercase__   :    int				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model_fpaa_forward(*__SCREAMING_SNAKE_CASE						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Any				=			['''encoder_attentions''', '''decoder_attentions''', '''cross_attentions''']
				lowercase__   :    Tuple				=			self.model_tester.prepare_config_and_inputs()
				lowercase__   :    int				=			config_and_inputs[0]
				lowercase__   :    int				=			UMTaForConditionalGeneration(__SCREAMING_SNAKE_CASE						).eval()
				model.to(__SCREAMING_SNAKE_CASE						)

				lowercase__   :    Any				=			{
				    '''head_mask''': torch.zeros(config.num_layers			,config.num_heads			,device=__SCREAMING_SNAKE_CASE						),
				    '''decoder_head_mask''': torch.zeros(config.num_decoder_layers			,config.num_heads			,device=__SCREAMING_SNAKE_CASE						),
				    '''cross_attn_head_mask''': torch.zeros(config.num_decoder_layers			,config.num_heads			,device=__SCREAMING_SNAKE_CASE						),
				}

				for attn_name, (name, mask) in zip(__SCREAMING_SNAKE_CASE			,head_masking.items()						):
						lowercase__   :    Any				=			{name: mask}
						# Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified
						if name == "head_mask":
								lowercase__   :    Union[str, Any]				=			torch.ones(
								    config.num_decoder_layers			,config.num_heads			,device=__SCREAMING_SNAKE_CASE						)

						lowercase__   :    Any				=			model.generate(
						    config_and_inputs[1]['''input_ids''']			,num_beams=1			,max_length=3			,output_attentions=__SCREAMING_SNAKE_CASE			,return_dict_in_generate=__SCREAMING_SNAKE_CASE			,**__SCREAMING_SNAKE_CASE			,)
						# We check the state of decoder_attentions and cross_attentions just from the last step
						lowercase__   :    int				=			out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1]
						self.assertEqual(sum([w.sum().item() for w in attn_weights]						)			,0.0						)







		@unittest.skip('''Does not work on the tiny model as we keep hitting edge cases.'''						)
		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		Optional[Any]:






				"""simple docstring"""



				pass





@require_torch
@require_sentencepiece
@require_tokenizers
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		@slow
		@unittest.skip(
		    '''Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged'''						)
		def 					UpperCAmelCase    (	self						:  Union[str, Any]						)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    List[str]				=			UMTaForConditionalGeneration.from_pretrained('''google/umt5-small'''			,return_dict=__SCREAMING_SNAKE_CASE						).to(__SCREAMING_SNAKE_CASE						)
				lowercase__   :    Optional[int]				=			AutoTokenizer.from_pretrained('''google/umt5-small'''			,use_fast=__SCREAMING_SNAKE_CASE			,legacy=__SCREAMING_SNAKE_CASE						)
				lowercase__   :    List[str]				=			[
				    '''Bonjour monsieur <extra_id_0> bien <extra_id_1>.''',
				    '''No se como puedo <extra_id_0>.''',
				    '''This is the reason why we <extra_id_0> them.''',
				    '''The <extra_id_0> walks in <extra_id_1>, seats''',
				    '''A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.''',
				]
				lowercase__   :    str				=			tokenizer(__SCREAMING_SNAKE_CASE			,return_tensors='''pt'''			,padding=__SCREAMING_SNAKE_CASE						).input_ids
				# fmt: off
				lowercase__   :    int				=			torch.tensor(
				    [
				        [ 38_530, 210_703, 256_299, 1_410, 256_298, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0],
				        [   826, 321, 671, 25_922, 256_299, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0],
				        [  1_460, 339, 312, 19_014, 10_620, 758, 256_299, 2_355,274, 1, 0, 0, 0, 0, 0, 0,0, 0],
				        [   517, 256_299, 14_869, 281, 301, 256_298, 275, 119_983,1, 0, 0, 0, 0, 0, 0, 0,0, 0],
				        [   320, 256_299, 14_869, 281, 2_234, 289, 2_275, 333,61_391, 289, 256_298, 543, 256_297, 168_714, 329, 256_296,274, 1],
				    ]						)
				# fmt: on
				torch.testing.assert_allclose(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE						)

				lowercase__   :    Union[str, Any]				=			model.generate(input_ids.to(__SCREAMING_SNAKE_CASE						)						)
				lowercase__   :    Optional[int]				=			[
				    '''<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>''',
				    '''<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''',
				    '''<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''',
				    '''<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''',
				    '''<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''',
				]
				lowercase__   :    Optional[Any]				=			tokenizer.batch_decode(__SCREAMING_SNAKE_CASE						)
				self.assertEqual(__SCREAMING_SNAKE_CASE			,__SCREAMING_SNAKE_CASE						)



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






