


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 				A       (			unittest.TestCase				):
					'''simple docstring'''






					def     lowerCamelCase__	(self       :		Tuple						)		->  Optional[int]:




							"""simple docstring"""


							lowercase__	     =						tempfile.mkdtemp()

							# fmt: off
							lowercase__	     =						["""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
							lowercase__	     =						dict(zip(_UpperCAmelCase ,   range(len(_UpperCAmelCase						)						)						)						)
							lowercase__	     =						["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
							lowercase__	     =						{"""unk_token""": """<unk>"""}

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

							lowercase__	     =						{
							    """do_resize""": True,
							    """size""": 20,
							    """do_center_crop""": True,
							    """crop_size""": 18,
							    """do_normalize""": True,
							    """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
							    """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
							}
							lowercase__	     =						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       :		Union[str, Any] ,   **_UpperCAmelCase       :		Optional[int]						)		->  int:




							"""simple docstring"""


							return CLIPTokenizer.from_pretrained(self.tmpdirname ,   **_UpperCAmelCase						)

					def     lowerCamelCase__	(self       :		List[Any] ,   **_UpperCAmelCase       :		str						)		->  Any:




							"""simple docstring"""


							return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,   **_UpperCAmelCase						)

					def     lowerCamelCase__	(self       :		Union[str, Any] ,   **_UpperCAmelCase       :		str						)		->  Union[str, Any]:




							"""simple docstring"""


							return CLIPImageProcessor.from_pretrained(self.tmpdirname ,   **_UpperCAmelCase						)

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




							"""simple docstring"""


							shutil.rmtree(self.tmpdirname						)

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




							"""simple docstring"""


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

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

							return image_inputs

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




							"""simple docstring"""


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

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

							lowercase__	     =						CLIPProcessor(tokenizer=_UpperCAmelCase ,   image_processor=_UpperCAmelCase						)
							processor_fast.save_pretrained(self.tmpdirname						)
							lowercase__	     =						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 ,   _UpperCAmelCase						)
							self.assertIsInstance(processor_fast.tokenizer ,   _UpperCAmelCase						)

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

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




							"""simple docstring"""


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

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

							lowercase__	     =						CLIPProcessor.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 ,   _UpperCAmelCase						)

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

					def     lowerCamelCase__	(self       :		str						)		->  Tuple:




							"""simple docstring"""


							lowercase__	     =						self.get_image_processor()
							lowercase__	     =						self.get_tokenizer()

							lowercase__	     =						CLIPProcessor(tokenizer=_UpperCAmelCase ,   image_processor=_UpperCAmelCase						)

							lowercase__	     =						self.prepare_image_inputs()

							lowercase__	     =						image_processor(_UpperCAmelCase ,   return_tensors="""np"""						)
							lowercase__	     =						processor(images=_UpperCAmelCase ,   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     lowerCamelCase__	(self       :		Tuple						)		->  str:




							"""simple docstring"""


							lowercase__	     =						self.get_image_processor()
							lowercase__	     =						self.get_tokenizer()

							lowercase__	     =						CLIPProcessor(tokenizer=_UpperCAmelCase ,   image_processor=_UpperCAmelCase						)

							lowercase__	     =						"""lower newer"""

							lowercase__	     =						processor(text=_UpperCAmelCase						)

							lowercase__	     =						tokenizer(_UpperCAmelCase						)

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

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




							"""simple docstring"""


							lowercase__	     =						self.get_image_processor()
							lowercase__	     =						self.get_tokenizer()

							lowercase__	     =						CLIPProcessor(tokenizer=_UpperCAmelCase ,   image_processor=_UpperCAmelCase						)

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

							lowercase__	     =						processor(text=_UpperCAmelCase ,   images=_UpperCAmelCase						)

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

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

					def     lowerCamelCase__	(self       :		int						)		->  Dict:




							"""simple docstring"""


							lowercase__	     =						self.get_image_processor()
							lowercase__	     =						self.get_tokenizer()

							lowercase__	     =						CLIPProcessor(tokenizer=_UpperCAmelCase ,   image_processor=_UpperCAmelCase						)

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

							lowercase__	     =						processor.batch_decode(_UpperCAmelCase						)
							lowercase__	     =						tokenizer.batch_decode(_UpperCAmelCase						)

							self.assertListEqual(_UpperCAmelCase ,   _UpperCAmelCase						)


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




							"""simple docstring"""


							lowercase__	     =						self.get_image_processor()
							lowercase__	     =						self.get_tokenizer()

							lowercase__	     =						CLIPProcessor(tokenizer=_UpperCAmelCase ,   image_processor=_UpperCAmelCase						)

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

							lowercase__	     =						processor(text=_UpperCAmelCase ,   images=_UpperCAmelCase						)

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








"""simple docstring"""
from collections import OrderedDict
from typing import Mapping

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


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

__UpperCamelCase     :						Tuple						       =  {
    '''bert-base-uncased''': '''https://huggingface.co/bert-base-uncased/resolve/main/config.json''',
    '''bert-large-uncased''': '''https://huggingface.co/bert-large-uncased/resolve/main/config.json''',
    '''bert-base-cased''': '''https://huggingface.co/bert-base-cased/resolve/main/config.json''',
    '''bert-large-cased''': '''https://huggingface.co/bert-large-cased/resolve/main/config.json''',
    '''bert-base-multilingual-uncased''': '''https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json''',
    '''bert-base-multilingual-cased''': '''https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json''',
    '''bert-base-chinese''': '''https://huggingface.co/bert-base-chinese/resolve/main/config.json''',
    '''bert-base-german-cased''': '''https://huggingface.co/bert-base-german-cased/resolve/main/config.json''',
    '''bert-large-uncased-whole-word-masking''': (
        '''https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json'''
    ),
    '''bert-large-cased-whole-word-masking''': (
        '''https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json'''
    ),
    '''bert-large-uncased-whole-word-masking-finetuned-squad''': (
        '''https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json'''
    ),
    '''bert-large-cased-whole-word-masking-finetuned-squad''': (
        '''https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json'''
    ),
    '''bert-base-cased-finetuned-mrpc''': '''https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json''',
    '''bert-base-german-dbmdz-cased''': '''https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json''',
    '''bert-base-german-dbmdz-uncased''': '''https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json''',
    '''cl-tohoku/bert-base-japanese''': '''https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json''',
    '''cl-tohoku/bert-base-japanese-whole-word-masking''': (
        '''https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json'''
    ),
    '''cl-tohoku/bert-base-japanese-char''': (
        '''https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json'''
    ),
    '''cl-tohoku/bert-base-japanese-char-whole-word-masking''': (
        '''https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json'''
    ),
    '''TurkuNLP/bert-base-finnish-cased-v1''': (
        '''https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json'''
    ),
    '''TurkuNLP/bert-base-finnish-uncased-v1''': (
        '''https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json'''
    ),
    '''wietsedv/bert-base-dutch-cased''': '''https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json''',
    # See all BERT models at https://huggingface.co/models?filter=bert
}







class       a							(   a__  ):
 snake_case__             =       '''bert'''



 def __init__(     self				,       _snake_case=3_05_22				,       _snake_case=7_68				,       _snake_case=12				,       _snake_case=12				,       _snake_case=30_72				,       _snake_case="gelu"				,       _snake_case=0.1				,       _snake_case=0.1				,       _snake_case=5_12				,       _snake_case=2				,       _snake_case=0.02				,       _snake_case=1E-12				,       _snake_case=0				,       _snake_case="absolute"				,       _snake_case=True				,       _snake_case=None				,       **_snake_case				,       ):







  """simple docstring"""






  super().__init__(pad_token_id=_snake_case				,       **_snake_case     )

  lowerCAmelCase    				= vocab_size
  lowerCAmelCase    				= hidden_size
  lowerCAmelCase    				= num_hidden_layers
  lowerCAmelCase    				= num_attention_heads
  lowerCAmelCase    				= hidden_act
  lowerCAmelCase    				= intermediate_size
  lowerCAmelCase    				= hidden_dropout_prob
  lowerCAmelCase    				= attention_probs_dropout_prob
  lowerCAmelCase    				= max_position_embeddings
  lowerCAmelCase    				= type_vocab_size
  lowerCAmelCase    				= initializer_range
  lowerCAmelCase    				= layer_norm_eps
  lowerCAmelCase    				= position_embedding_type
  lowerCAmelCase    				= use_cache
  lowerCAmelCase    				= classifier_dropout







class       a							(   a__  ):



 @property
 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






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





