


'''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 CLIPSegProcessor, ViTImageProcessor





@require_vision
class 							__UpperCamelCase				(		unittest.TestCase					):




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  tempfile.mkdtemp()

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

						__a			:       str					    =  os.path.join(self.tmpdirname						,				VOCAB_FILES_NAMES['vocab_file']							)
						__a			:       Dict					    =  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							)							)

						__a			:       List[str]					    =  {
						    '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],
						}
						__a			:       Optional[Any]					    =  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						,				**__a							):

						'''simple docstring'''





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




	def       __UpperCAmelCase							(   self						,				**__a							):

						'''simple docstring'''





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




	def       __UpperCAmelCase							(   self						,				**__a							):

						'''simple docstring'''





						return ViTImageProcessor.from_pretrained(self.tmpdirname						,				**__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						shutil.rmtree(self.tmpdirname							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Union[str, Any]					    =  [np.random.randint(255						,				size=(3, 30, 400)						,				dtype=np.uinta							)]

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

						return image_inputs




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[int]					    =  self.get_tokenizer()
						__a			:       Optional[Any]					    =  self.get_rust_tokenizer()
						__a			:       Dict					    =  self.get_image_processor()

						__a			:       Any					    =  CLIPSegProcessor(tokenizer=__a						,				image_processor=__a							)
						processor_slow.save_pretrained(self.tmpdirname							)
						__a			:       Union[str, Any]					    =  CLIPSegProcessor.from_pretrained(self.tmpdirname						,				use_fast=__a							)

						__a			:       Union[str, Any]					    =  CLIPSegProcessor(tokenizer=__a						,				image_processor=__a							)
						processor_fast.save_pretrained(self.tmpdirname							)
						__a			:       Optional[Any]					    =  CLIPSegProcessor.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							):

						'''simple docstring'''





						__a			:       Dict					    =  CLIPSegProcessor(tokenizer=self.get_tokenizer()						,				image_processor=self.get_image_processor()							)
						processor.save_pretrained(self.tmpdirname							)

						__a			:       Any					    =  self.get_tokenizer(bos_token='(BOS)'						,				eos_token='(EOS)'							)
						__a			:       int					    =  self.get_image_processor(do_normalize=__a						,				padding_value=1.0							)

						__a			:       Union[str, Any]					    =  CLIPSegProcessor.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							):

						'''simple docstring'''





						__a			:       Optional[int]					    =  self.get_image_processor()
						__a			:       Optional[int]					    =  self.get_tokenizer()

						__a			:       str					    =  CLIPSegProcessor(tokenizer=__a						,				image_processor=__a							)

						__a			:       Dict					    =  self.prepare_image_inputs()

						__a			:       Tuple					    =  image_processor(__a						,				return_tensors='np'							)
						__a			:       List[Any]					    =  processor(images=__a						,				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			:       str					    =  self.get_image_processor()
						__a			:       List[str]					    =  self.get_tokenizer()

						__a			:       Optional[Any]					    =  CLIPSegProcessor(tokenizer=__a						,				image_processor=__a							)

						__a			:       Optional[Any]					    =  'lower newer'

						__a			:       Union[str, Any]					    =  processor(text=__a							)

						__a			:       str					    =  tokenizer(__a							)

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




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[int]					    =  self.get_image_processor()
						__a			:       Dict					    =  self.get_tokenizer()

						__a			:       List[Any]					    =  CLIPSegProcessor(tokenizer=__a						,				image_processor=__a							)

						__a			:       List[str]					    =  'lower newer'
						__a			:       Optional[int]					    =  self.prepare_image_inputs()

						__a			:       Tuple					    =  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							):

						'''simple docstring'''





						__a			:       List[str]					    =  self.get_image_processor()
						__a			:       Dict					    =  self.get_tokenizer()

						__a			:       Tuple					    =  CLIPSegProcessor(tokenizer=__a						,				image_processor=__a							)

						__a			:       Dict					    =  self.prepare_image_inputs()
						__a			:       List[str]					    =  self.prepare_image_inputs()

						__a			:       int					    =  processor(images=__a						,				visual_prompt=__a							)

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

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





	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Tuple					    =  self.get_image_processor()
						__a			:       Union[str, Any]					    =  self.get_tokenizer()

						__a			:       Union[str, Any]					    =  CLIPSegProcessor(tokenizer=__a						,				image_processor=__a							)

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

						__a			:       Any					    =  processor.batch_decode(__a							)
						__a			:       Optional[Any]					    =  tokenizer.batch_decode(__a							)

						self.assertListEqual(__a						,				__a							)










import unittest

import numpy as np

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

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
					import jax.numpy as jnp

					from transformers.models.albert.modeling_flax_albert import (
					    FlaxAlbertForMaskedLM,
					    FlaxAlbertForMultipleChoice,
					    FlaxAlbertForPreTraining,
					    FlaxAlbertForQuestionAnswering,
					    FlaxAlbertForSequenceClassification,
					    FlaxAlbertForTokenClassification,
					    FlaxAlbertModel,
					)


class   A			(unittest.TestCase    ):
			'''simple docstring'''




			def __init__(			self   :    List[str]							,	__lowerCAmelCase   :    int							,	__lowerCAmelCase   :    List[str]=13							,	__lowerCAmelCase   :    int=7							,	__lowerCAmelCase   :    Tuple=True							,	__lowerCAmelCase   :    int=True							,	__lowerCAmelCase   :    Dict=True							,	__lowerCAmelCase   :    Union[str, Any]=True							,	__lowerCAmelCase   :    List[Any]=99							,	__lowerCAmelCase   :    Optional[Any]=32							,	__lowerCAmelCase   :    Optional[Any]=5							,	__lowerCAmelCase   :    Tuple=4							,	__lowerCAmelCase   :    Any=37							,	__lowerCAmelCase   :    str="gelu"							,	__lowerCAmelCase   :    Optional[Any]=0.1							,	__lowerCAmelCase   :    Union[str, Any]=0.1							,	__lowerCAmelCase   :    List[str]=5_12							,	__lowerCAmelCase   :    int=16							,	__lowerCAmelCase   :    Optional[int]=2							,	__lowerCAmelCase   :    List[Any]=0.0_2							,	__lowerCAmelCase   :    Tuple=4							,	)	->	Dict:







					"""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    a_       (			self   :    Any   )	->	str:







					"""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__        =   AlbertConfig(
					    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    a_       (			self   :    Optional[int]   )	->	Optional[int]:







					"""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


@require_flax
class   A			(SCREAMING_SNAKE_CASE   ,     unittest.TestCase    ):
			'''simple docstring'''




			__lowerCamelCase	:       str							=    (
			    (
			        FlaxAlbertModel,
			        FlaxAlbertForPreTraining,
			        FlaxAlbertForMaskedLM,
			        FlaxAlbertForMultipleChoice,
			        FlaxAlbertForQuestionAnswering,
			        FlaxAlbertForSequenceClassification,
			        FlaxAlbertForTokenClassification,
			        FlaxAlbertForQuestionAnswering,
			    )
			    if is_flax_available()
			    else ()
			)






			def    a_       (			self   :    str   )	->	Optional[int]:







					"""simple docstring"""






					A__        =   FlaxAlbertModelTester(self   )







			@slow
			def    a_       (			self   :    int   )	->	Tuple:







					"""simple docstring"""






					for model_class_name in self.all_model_classes:
							A__        =   model_class_name.from_pretrained("""albert-base-v2"""   )
							A__        =   model(np.ones((1, 1)   )   )
							self.assertIsNotNone(__lowerCAmelCase   )


@require_flax
class   A			(unittest.TestCase    ):
			'''simple docstring'''




			@slow
			def    a_       (			self   :    Dict   )	->	List[Any]:







					"""simple docstring"""






					A__        =   FlaxAlbertModel.from_pretrained("""albert-base-v2"""   )
					A__        =   np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
					A__        =   np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
					A__        =   model(__lowerCAmelCase							,	attention_mask=__lowerCAmelCase   )[0]
					A__        =   (1, 11, 7_68)
					self.assertEqual(output.shape							,	__lowerCAmelCase   )
					A__        =   np.array(
					    [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]]   )

					self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]							,	__lowerCAmelCase							,	atol=1e-4   )   )



