



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




							def    UpperCAmelCase__			(       self     :       str):
									lowerCAmelCase_			:     str									= tempfile.mkdtemp()

									# fmt: off
									lowerCAmelCase_			:     int									= ['''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
									lowerCAmelCase_			:     List[Any]									= dict(zip(A_				,				range(len(A_))))
									lowerCAmelCase_			:     int									= ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
									lowerCAmelCase_			:     List[str]									= {'''unk_token''': '''<unk>'''}

									lowerCAmelCase_			:     Dict									= os.path.join(self.tmpdirname				,				VOCAB_FILES_NAMES['''vocab_file'''])
									lowerCAmelCase_			:     List[Any]									= 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(A_) + '''\n''')
									with open(self.merges_file				,				'''w'''				,				encoding='''utf-8''') as fp:
											fp.write('''\n'''.join(A_))

									lowerCAmelCase_			:     Dict									= {
									    '''do_resize''': True,
									    '''size''': 2_0,
									    '''do_center_crop''': True,
									    '''crop_size''': 1_8,
									    '''do_normalize''': True,
									    '''image_mean''': [0.4814_5466, 0.457_8275, 0.4082_1073],
									    '''image_std''': [0.2686_2954, 0.2613_0258, 0.2757_7711],
									}
									lowerCAmelCase_			:     Dict									= os.path.join(self.tmpdirname				,				A_)
									with open(self.image_processor_file				,				'''w'''				,				encoding='''utf-8''') as fp:
											json.dump(A_				,				A_)




							def    UpperCAmelCase__			(       self     :       int				,				**A_     :       Optional[Any]):
									return CLIPTokenizer.from_pretrained(self.tmpdirname				,				**A_)




							def    UpperCAmelCase__			(       self     :       int				,				**A_     :       Optional[Any]):
									return CLIPTokenizerFast.from_pretrained(self.tmpdirname				,				**A_)




							def    UpperCAmelCase__			(       self     :       List[str]				,				**A_     :       Optional[Any]):
									return CLIPImageProcessor.from_pretrained(self.tmpdirname				,				**A_)




							def    UpperCAmelCase__			(       self     :       Optional[int]):
									shutil.rmtree(self.tmpdirname)




							def    UpperCAmelCase__			(       self     :       int):
									lowerCAmelCase_			:     int									= [np.random.randint(2_5_5				,				size=(3, 3_0, 4_0_0)				,				dtype=np.uinta)]

									lowerCAmelCase_			:     Union[str, Any]									= [Image.fromarray(np.moveaxis(A_				,				0				,				-1)) for x in image_inputs]

									return image_inputs




							def    UpperCAmelCase__			(       self     :       int):
									lowerCAmelCase_			:     str									= self.get_tokenizer()
									lowerCAmelCase_			:     str									= self.get_rust_tokenizer()
									lowerCAmelCase_			:     Optional[Any]									= self.get_image_processor()

									lowerCAmelCase_			:     Optional[int]									= CLIPProcessor(tokenizer=A_				,				image_processor=A_)
									processor_slow.save_pretrained(self.tmpdirname)
									lowerCAmelCase_			:     Dict									= CLIPProcessor.from_pretrained(self.tmpdirname				,				use_fast=A_)

									lowerCAmelCase_			:     List[str]									= CLIPProcessor(tokenizer=A_				,				image_processor=A_)
									processor_fast.save_pretrained(self.tmpdirname)
									lowerCAmelCase_			:     Any									= 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				,				A_)
									self.assertIsInstance(processor_fast.tokenizer				,				A_)

									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				,				A_)
									self.assertIsInstance(processor_fast.image_processor				,				A_)




							def    UpperCAmelCase__			(       self     :       int):
									lowerCAmelCase_			:     Tuple									= CLIPProcessor(tokenizer=self.get_tokenizer()				,				image_processor=self.get_image_processor())
									processor.save_pretrained(self.tmpdirname)

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

									lowerCAmelCase_			:     Tuple									= CLIPProcessor.from_pretrained(
									    self.tmpdirname				,				bos_token='''(BOS)'''				,				eos_token='''(EOS)'''				,				do_normalize=A_				,				padding_value=1.0)

									self.assertEqual(processor.tokenizer.get_vocab()				,				tokenizer_add_kwargs.get_vocab())
									self.assertIsInstance(processor.tokenizer				,				A_)

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




							def    UpperCAmelCase__			(       self     :       Dict):
									lowerCAmelCase_			:     Any									= self.get_image_processor()
									lowerCAmelCase_			:     Union[str, Any]									= self.get_tokenizer()

									lowerCAmelCase_			:     Tuple									= CLIPProcessor(tokenizer=A_				,				image_processor=A_)

									lowerCAmelCase_			:     List[Any]									= self.prepare_image_inputs()

									lowerCAmelCase_			:     Optional[Any]									= image_processor(A_				,				return_tensors='''np''')
									lowerCAmelCase_			:     str									= processor(images=A_				,				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     :       Any):
									lowerCAmelCase_			:     Optional[int]									= self.get_image_processor()
									lowerCAmelCase_			:     str									= self.get_tokenizer()

									lowerCAmelCase_			:     Any									= CLIPProcessor(tokenizer=A_				,				image_processor=A_)

									lowerCAmelCase_			:     Dict									= '''lower newer'''

									lowerCAmelCase_			:     Dict									= processor(text=A_)

									lowerCAmelCase_			:     Optional[Any]									= tokenizer(A_)

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




							def    UpperCAmelCase__			(       self     :       Tuple):
									lowerCAmelCase_			:     Any									= self.get_image_processor()
									lowerCAmelCase_			:     List[str]									= self.get_tokenizer()

									lowerCAmelCase_			:     Any									= CLIPProcessor(tokenizer=A_				,				image_processor=A_)

									lowerCAmelCase_			:     Optional[Any]									= '''lower newer'''
									lowerCAmelCase_			:     Dict									= self.prepare_image_inputs()

									lowerCAmelCase_			:     int									= processor(text=A_				,				images=A_)

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

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




							def    UpperCAmelCase__			(       self     :       Optional[Any]):
									lowerCAmelCase_			:     str									= self.get_image_processor()
									lowerCAmelCase_			:     str									= self.get_tokenizer()

									lowerCAmelCase_			:     List[Any]									= CLIPProcessor(tokenizer=A_				,				image_processor=A_)

									lowerCAmelCase_			:     str									= [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									lowerCAmelCase_			:     Optional[Any]									= processor.batch_decode(A_)
									lowerCAmelCase_			:     Optional[int]									= tokenizer.batch_decode(A_)

									self.assertListEqual(A_				,				A_)







							def    UpperCAmelCase__			(       self     :       str):
									lowerCAmelCase_			:     List[str]									= self.get_image_processor()
									lowerCAmelCase_			:     int									= self.get_tokenizer()

									lowerCAmelCase_			:     Optional[int]									= CLIPProcessor(tokenizer=A_				,				image_processor=A_)

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

									lowerCAmelCase_			:     Tuple									= processor(text=A_				,				images=A_)

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










from collections import OrderedDict
from typing import Mapping

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


A__  :  Union[str, Any]								=					logging.get_logger(__name__)

A__  :  Tuple								=					{
    '''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 				__snake_case						(     UpperCamelCase_						):
							_a			      = '''xlm-roberta-xl'''




							def __init__(       self     :       int				,				A_     :       List[str]=2_5_0_8_8_0				,				A_     :       List[str]=2_5_6_0				,				A_     :       Optional[int]=3_6				,				A_     :       List[Any]=3_2				,				A_     :       Optional[int]=1_0_2_4_0				,				A_     :       Dict="gelu"				,				A_     :       int=0.1				,				A_     :       Optional[Any]=0.1				,				A_     :       int=5_1_4				,				A_     :       Any=1				,				A_     :       Optional[Any]=0.02				,				A_     :       str=1e-05				,				A_     :       Dict=1				,				A_     :       Any=0				,				A_     :       Tuple=2				,				A_     :       str="absolute"				,				A_     :       str=True				,				A_     :       List[str]=None				,				**A_     :       Dict				,				):
									super().__init__(pad_token_id=A_				,				bos_token_id=A_				,				eos_token_id=A_				,				**A_)
									lowerCAmelCase_			:     Tuple									= vocab_size
									lowerCAmelCase_			:     List[str]									= hidden_size
									lowerCAmelCase_			:     int									= num_hidden_layers
									lowerCAmelCase_			:     int									= num_attention_heads
									lowerCAmelCase_			:     Dict									= hidden_act
									lowerCAmelCase_			:     int									= intermediate_size
									lowerCAmelCase_			:     Tuple									= hidden_dropout_prob
									lowerCAmelCase_			:     Optional[Any]									= attention_probs_dropout_prob
									lowerCAmelCase_			:     Union[str, Any]									= max_position_embeddings
									lowerCAmelCase_			:     Dict									= type_vocab_size
									lowerCAmelCase_			:     str									= initializer_range
									lowerCAmelCase_			:     str									= layer_norm_eps
									lowerCAmelCase_			:     Optional[Any]									= position_embedding_type
									lowerCAmelCase_			:     Optional[Any]									= use_cache
									lowerCAmelCase_			:     List[str]									= classifier_dropout





class 				__snake_case						(     UpperCamelCase_						):




							@property
							def    UpperCAmelCase__			(       self     :       List[str]):
									if self.task == "multiple-choice":
											lowerCAmelCase_			:     Union[str, Any]									= {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
									else:
											lowerCAmelCase_			:     List[Any]									= {0: '''batch''', 1: '''sequence'''}
									return OrderedDict(
									    [
									        ('''input_ids''', dynamic_axis),
									        ('''attention_mask''', dynamic_axis),
									    ])





