



import unittest

import numpy as np

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
				from transformers.models.bert.modeling_flax_bert import (
				    FlaxBertForMaskedLM,
				    FlaxBertForMultipleChoice,
				    FlaxBertForNextSentencePrediction,
				    FlaxBertForPreTraining,
				    FlaxBertForQuestionAnswering,
				    FlaxBertForSequenceClassification,
				    FlaxBertForTokenClassification,
				    FlaxBertModel,
				)



class 	_lowercase							(   unittest.TestCase	):





	"""simple docstring"""




	def __init__(self		,       lowerCamelCase_		,       lowerCamelCase_=13		,       lowerCamelCase_=7		,       lowerCamelCase_=True		,       lowerCamelCase_=True		,       lowerCamelCase_=True		,       lowerCamelCase_=True		,       lowerCamelCase_=99		,       lowerCamelCase_=32		,       lowerCamelCase_=5		,       lowerCamelCase_=4		,       lowerCamelCase_=37		,       lowerCamelCase_="gelu"		,       lowerCamelCase_=0.1		,       lowerCamelCase_=0.1		,       lowerCamelCase_=512		,       lowerCamelCase_=16		,       lowerCamelCase_=2		,       lowerCamelCase_=0.02		,       lowerCamelCase_=4		,       ):
						"""simple docstring"""

						a					     =   parent
						a					     =   batch_size
						a					     =   seq_length
						a					     =   is_training
						a					     =   use_attention_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_choices




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

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

						a					     =   None
						if self.use_token_type_ids:
											a					     =   ids_tensor([self.batch_size, self.seq_length]		,       self.type_vocab_size		)

						a					     =   BertConfig(
						    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=lowerCamelCase_		,       initializer_range=self.initializer_range		,       )

						return config, input_ids, token_type_ids, attention_mask




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   self.prepare_config_and_inputs()
						a     ,				a     ,				a     ,				a					     =   config_and_inputs

						a					     =   True
						a					     =   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
						a					     =   ids_tensor([self.batch_size, self.seq_length]		,       vocab_size=2		)

						return (
						    config,
						    input_ids,
						    attention_mask,
						    encoder_hidden_states,
						    encoder_attention_mask,
						)




@require_flax
class 	_lowercase							(   lowerCAmelCase,      unittest.TestCase	):





	"""simple docstring"""
	__A							=							True

	__A							=							(
	    (
	        FlaxBertModel,
	        FlaxBertForPreTraining,
	        FlaxBertForMaskedLM,
	        FlaxBertForMultipleChoice,
	        FlaxBertForQuestionAnswering,
	        FlaxBertForNextSentencePrediction,
	        FlaxBertForSequenceClassification,
	        FlaxBertForTokenClassification,
	        FlaxBertForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   FlaxBertModelTester(self		)




	@slow
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   FlaxBertModel.from_pretrained("bert-base-cased"		)
						a					     =   model(np.ones((1, 1)		)		)
						self.assertIsNotNone(lowerCamelCase_		)





import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
				from PIL import Image

				from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor



@require_vision
class 	_lowercase							(   unittest.TestCase	):





	"""simple docstring"""




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   tempfile.mkdtemp()

						a					     =   [
						    "[UNK]",
						    "[CLS]",
						    "[SEP]",
						    "[PAD]",
						    "[MASK]",
						    "的",
						    "价",
						    "格",
						    "是",
						    "15",
						    "便",
						    "alex",
						    "##andra",
						    "，",
						    "。",
						    "-",
						    "t",
						    "shirt",
						]
						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": 224, "width": 224},
						    "do_center_crop": True,
						    "crop_size": {"height": 18, "width": 18},
						    "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],
						    "do_convert_rgb": True,
						}
						a					     =   os.path.join(self.tmpdirname		,       lowerCamelCase_		)
						with open(self.image_processor_file		,       "w"		,       encoding="utf-8"		) as fp:
											json.dump(lowerCamelCase_		,       lowerCamelCase_		)




	def      UpperCamelCase_       (self		,       **lowerCamelCase_		):
						"""simple docstring"""

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




	def      UpperCamelCase_       (self		,       **lowerCamelCase_		):
						"""simple docstring"""

						return BertTokenizerFast.from_pretrained(self.tmpdirname		,       **lowerCamelCase_		)




	def      UpperCamelCase_       (self		,       **lowerCamelCase_		):
						"""simple docstring"""

						return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname		,       **lowerCamelCase_		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						shutil.rmtree(self.tmpdirname		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

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

						return image_inputs




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

						a					     =   ChineseCLIPProcessor(tokenizer=lowerCamelCase_		,       image_processor=lowerCamelCase_		)
						processor_slow.save_pretrained(self.tmpdirname		)
						a					     =   ChineseCLIPProcessor.from_pretrained(self.tmpdirname		,       use_fast=lowerCamelCase_		)

						a					     =   ChineseCLIPProcessor(tokenizer=lowerCamelCase_		,       image_processor=lowerCamelCase_		)
						processor_fast.save_pretrained(self.tmpdirname		)
						a					     =   ChineseCLIPProcessor.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		,       lowerCamelCase_		)
						self.assertIsInstance(processor_fast.tokenizer		,       lowerCamelCase_		)

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

						a					     =   self.get_tokenizer(cls_token="(CLS)"		,       sep_token="(SEP)"		)
						a					     =   self.get_image_processor(do_normalize=lowerCamelCase_		)

						a					     =   ChineseCLIPProcessor.from_pretrained(
						    self.tmpdirname		,       cls_token="(CLS)"		,       sep_token="(SEP)"		,       do_normalize=lowerCamelCase_		)

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

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

						a					     =   ChineseCLIPProcessor(tokenizer=lowerCamelCase_		,       image_processor=lowerCamelCase_		)

						a					     =   self.prepare_image_inputs()

						a					     =   image_processor(lowerCamelCase_		,       return_tensors="np"		)
						a					     =   processor(images=lowerCamelCase_		,       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      UpperCamelCase_       (self		):
						"""simple docstring"""

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

						a					     =   ChineseCLIPProcessor(tokenizer=lowerCamelCase_		,       image_processor=lowerCamelCase_		)

						a					     =   "Alexandra，T-shirt的价格是15便士。"

						a					     =   processor(text=lowerCamelCase_		)

						a					     =   tokenizer(lowerCamelCase_		)

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

						a					     =   ChineseCLIPProcessor(tokenizer=lowerCamelCase_		,       image_processor=lowerCamelCase_		)

						a					     =   "Alexandra，T-shirt的价格是15便士。"
						a					     =   self.prepare_image_inputs()

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

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

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

						a					     =   ChineseCLIPProcessor(tokenizer=lowerCamelCase_		,       image_processor=lowerCamelCase_		)

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

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

						self.assertListEqual(lowerCamelCase_		,       lowerCamelCase_		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

						a					     =   ChineseCLIPProcessor(tokenizer=lowerCamelCase_		,       image_processor=lowerCamelCase_		)

						a					     =   "Alexandra，T-shirt的价格是15便士。"
						a					     =   self.prepare_image_inputs()

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

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