
import inspect
import unittest

import numpy as np

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
				import jax

				from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel




class 						lowercase__  (						unittest.TestCase  ):
	def __init__(			self							:   Optional[int]       ,   UpperCAmelCase_							:   List[Any]       ,   UpperCAmelCase_							:   Any=13       ,   UpperCAmelCase_							:   Any=30       ,   UpperCAmelCase_							:   int=2       ,   UpperCAmelCase_							:   Tuple=3       ,   UpperCAmelCase_							:   int=True       ,   UpperCAmelCase_							:   str=True       ,   UpperCAmelCase_							:   Dict=32       ,   UpperCAmelCase_							:   Optional[Any]=5       ,   UpperCAmelCase_							:   List[Any]=4       ,   UpperCAmelCase_							:   Union[str, Any]=37       ,   UpperCAmelCase_							:   Tuple="gelu"       ,   UpperCAmelCase_							:   List[Any]=0.1       ,   UpperCAmelCase_							:   Optional[int]=0.1       ,   UpperCAmelCase_							:   Union[str, Any]=10       ,   UpperCAmelCase_							:   Optional[Any]=0.02       ,   ):
						SCREAMING_SNAKE_CASE__													= parent
						SCREAMING_SNAKE_CASE__													= batch_size
						SCREAMING_SNAKE_CASE__													= image_size
						SCREAMING_SNAKE_CASE__													= patch_size
						SCREAMING_SNAKE_CASE__													= num_channels
						SCREAMING_SNAKE_CASE__													= is_training
						SCREAMING_SNAKE_CASE__													= use_labels
						SCREAMING_SNAKE_CASE__													= hidden_size
						SCREAMING_SNAKE_CASE__													= num_hidden_layers
						SCREAMING_SNAKE_CASE__													= num_attention_heads
						SCREAMING_SNAKE_CASE__													= intermediate_size
						SCREAMING_SNAKE_CASE__													= hidden_act
						SCREAMING_SNAKE_CASE__													= hidden_dropout_prob
						SCREAMING_SNAKE_CASE__													= attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE__													= type_sequence_label_size
						SCREAMING_SNAKE_CASE__													= initializer_range

						# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
						SCREAMING_SNAKE_CASE__													= (image_size // patch_size) ** 2
						SCREAMING_SNAKE_CASE__													= num_patches + 1
	def      A_				(			self							:   Any							):
						SCREAMING_SNAKE_CASE__													= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

						SCREAMING_SNAKE_CASE__													= ViTConfig(
						    image_size=self.image_size       ,   patch_size=self.patch_size       ,   num_channels=self.num_channels       ,   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       ,   is_decoder=UpperCAmelCase_       ,   initializer_range=self.initializer_range       ,   )

						return config, pixel_values
	def      A_				(			self							:   List[Any]       ,   UpperCAmelCase_							:   Optional[int]       ,   UpperCAmelCase_							:   Dict							):
						SCREAMING_SNAKE_CASE__													= FlaxViTModel(config=UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= model(UpperCAmelCase_							)
						# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
						SCREAMING_SNAKE_CASE__													= (self.image_size, self.image_size)
						SCREAMING_SNAKE_CASE__													= (self.patch_size, self.patch_size)
						SCREAMING_SNAKE_CASE__													= (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
						self.parent.assertEqual(result.last_hidden_state.shape       ,   (self.batch_size, num_patches + 1, self.hidden_size)							)
	def      A_				(			self							:   Optional[Any]       ,   UpperCAmelCase_							:   Dict       ,   UpperCAmelCase_							:   Tuple							):
						SCREAMING_SNAKE_CASE__													= self.type_sequence_label_size
						SCREAMING_SNAKE_CASE__													= FlaxViTForImageClassification(config=UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= model(UpperCAmelCase_							)
						self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.type_sequence_label_size)							)

						# test greyscale images
						SCREAMING_SNAKE_CASE__													= 1
						SCREAMING_SNAKE_CASE__													= FlaxViTForImageClassification(UpperCAmelCase_							)

						SCREAMING_SNAKE_CASE__													= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
						SCREAMING_SNAKE_CASE__													= model(UpperCAmelCase_							)





	def      A_				(			self							:   List[Any]							):
						SCREAMING_SNAKE_CASE__													= self.prepare_config_and_inputs()
						(
						    (
						    SCREAMING_SNAKE_CASE__
						)						, (
						    SCREAMING_SNAKE_CASE__
						)						, 
						)													= config_and_inputs
						SCREAMING_SNAKE_CASE__													= {'pixel_values': pixel_values}
						return config, inputs_dict





@require_flax
class 						lowercase__  (						_UpperCAmelCase     ,       unittest.TestCase  ):
	A__    :   Union[str, Any]				       =(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()
	def      A_				(			self							:   Optional[Any]							):
						SCREAMING_SNAKE_CASE__													= FlaxViTModelTester(self							)
						SCREAMING_SNAKE_CASE__													= ConfigTester(self       ,   config_class=UpperCAmelCase_       ,   has_text_modality=UpperCAmelCase_       ,   hidden_size=37							)
	def      A_				(			self							:   Optional[Any]							):
						self.config_tester.run_common_tests()
	def      A_				(			self							:   List[Any]							):
						SCREAMING_SNAKE_CASE__													= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCAmelCase_							)
	def      A_				(			self							:   Tuple							):
						SCREAMING_SNAKE_CASE__													= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_							)
	def      A_				(			self							:   Any							):
						SCREAMING_SNAKE_CASE__						, SCREAMING_SNAKE_CASE__													= self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											SCREAMING_SNAKE_CASE__													= model_class(UpperCAmelCase_							)
											SCREAMING_SNAKE_CASE__													= inspect.signature(model.__call__							)
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											SCREAMING_SNAKE_CASE__													= [*signature.parameters.keys()]

											SCREAMING_SNAKE_CASE__													= ['pixel_values']
											self.assertListEqual(arg_names[:1]       ,   UpperCAmelCase_							)
	def      A_				(			self							:   List[str]							):
						SCREAMING_SNAKE_CASE__						, SCREAMING_SNAKE_CASE__													= self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											with self.subTest(model_class.__name__							):
																SCREAMING_SNAKE_CASE__													= self._prepare_for_class(UpperCAmelCase_       ,   UpperCAmelCase_							)
																SCREAMING_SNAKE_CASE__													= model_class(UpperCAmelCase_							)

																@jax.jit
																def model_jitted(UpperCAmelCase_							:   str       ,   **UpperCAmelCase_							:   List[Any]							):
																					return model(pixel_values=UpperCAmelCase_       ,   **UpperCAmelCase_							)

																with self.subTest('JIT Enabled'							):
																					SCREAMING_SNAKE_CASE__													= model_jitted(**UpperCAmelCase_							).to_tuple()

																with self.subTest('JIT Disabled'							):
																					with jax.disable_jit():
																										SCREAMING_SNAKE_CASE__													= model_jitted(**UpperCAmelCase_							).to_tuple()

																self.assertEqual(len(UpperCAmelCase_							)       ,   len(UpperCAmelCase_							)							)
																for jitted_output, output in zip(UpperCAmelCase_       ,   UpperCAmelCase_							):
																					self.assertEqual(jitted_output.shape       ,   output.shape							)





	@slow
	def      A_				(			self							:   Optional[int]							):
						for model_class_name in self.all_model_classes:
											SCREAMING_SNAKE_CASE__													= model_class_name.from_pretrained('google/vit-base-patch16-224'							)
											SCREAMING_SNAKE_CASE__													= model(np.ones((1, 3, 224, 224)							)							)
											self.assertIsNotNone(UpperCAmelCase_							)







import json
import os
import unittest

from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import (
    VOCAB_FILES_NAMES,
    GPTSanJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class 						lowercase__  (						_UpperCAmelCase     ,       unittest.TestCase  ):
	A__    :   Any				       =GPTSanJapaneseTokenizer
	A__    :   str				       =False
	A__    :   int				       ={"""do_clean_text""": False, """add_prefix_space""": False}
	def      A_				(			self							:   Any							):
						super().setUp()

						# fmt: off
						SCREAMING_SNAKE_CASE__													= ['こん', 'こんに', 'にちは', 'ばんは', '世界,㔺界', '、', '。', '<BR>', '<SP>', '<TAB>', '<URL>', '<EMAIL>', '<TEL>', '<DATE>', '<PRICE>', '<BLOCK>', '<KIGOU>', '<U2000U2BFF>', '<|emoji1|>', '<unk>', '<|bagoftoken|>', '<|endoftext|>']
						# fmt: on
						SCREAMING_SNAKE_CASE__													= {'emoji': {'\ud83d\ude00': '<|emoji1|>'}, 'emoji_inv': {'<|emoji1|>': '\ud83d\ude00'}}  # 😀
						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['emoji_file']							)
						with open(self.vocab_file       ,   'w'       ,   encoding='utf-8'							) as vocab_writer:
											vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]							)							)
						with open(self.emoji_file       ,   'w'							) as emoji_writer:
											emoji_writer.write(json.dumps(UpperCAmelCase_							)							)
	def      A_				(			self							:   str       ,   **UpperCAmelCase_							:   Optional[Any]							):
						kwargs.update(self.special_tokens_map							)
						return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname       ,   **UpperCAmelCase_							)
	def      A_				(			self							:   int       ,   UpperCAmelCase_							:   List[str]							):
						SCREAMING_SNAKE_CASE__													= 'こんにちは、世界。 \nこんばんは、㔺界。😀'
						SCREAMING_SNAKE_CASE__													= 'こんにちは、世界。 \nこんばんは、世界。😀'
						return input_text, output_text
	def      A_				(			self							:   Any       ,   UpperCAmelCase_							:   str							):
						SCREAMING_SNAKE_CASE__						, SCREAMING_SNAKE_CASE__													= self.get_input_output_texts(UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= tokenizer.encode(UpperCAmelCase_       ,   add_special_tokens=UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= tokenizer.decode(UpperCAmelCase_       ,   clean_up_tokenization_spaces=UpperCAmelCase_							)
						return text, ids
	def      A_				(			self							:   str							):
						pass  # TODO add if relevant
	def      A_				(			self							:   Tuple							):
						pass  # TODO add if relevant
	def      A_				(			self							:   int							):
						pass  # TODO add if relevant
	def      A_				(			self							:   Optional[int]							):
						SCREAMING_SNAKE_CASE__													= self.get_tokenizer()

						# Testing tokenization
						SCREAMING_SNAKE_CASE__													= 'こんにちは、世界。　こんばんは、㔺界。'
						SCREAMING_SNAKE_CASE__													= ['こん', 'にちは', '、', '世界', '。', '<SP>', 'こん', 'ばんは', '、', '㔺界', '。']
						SCREAMING_SNAKE_CASE__													= tokenizer.tokenize(UpperCAmelCase_							)
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)

						# Testing conversion to ids without special tokens
						SCREAMING_SNAKE_CASE__													= [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
						SCREAMING_SNAKE_CASE__													= tokenizer.convert_tokens_to_ids(UpperCAmelCase_							)
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)

						# Testing conversion to ids with special tokens
						SCREAMING_SNAKE_CASE__													= tokens + [tokenizer.unk_token]
						SCREAMING_SNAKE_CASE__													= [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19]
						SCREAMING_SNAKE_CASE__													= tokenizer.convert_tokens_to_ids(UpperCAmelCase_							)
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
	def      A_				(			self							:   Union[str, Any]							):
						SCREAMING_SNAKE_CASE__													= self.get_tokenizer()

						# Testing tokenization
						SCREAMING_SNAKE_CASE__													= 'こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。'
						SCREAMING_SNAKE_CASE__													= 'こんにちは、、、、世界。こんばんは、、、、世界。'
						SCREAMING_SNAKE_CASE__													= tokenizer.encode(UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= tokenizer.decode(UpperCAmelCase_							)
						self.assertEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
	@slow
	def      A_				(			self							:   str							):
						SCREAMING_SNAKE_CASE__													= self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese'							)

						# Testing tokenization
						SCREAMING_SNAKE_CASE__													= 'こんにちは、世界。'
						SCREAMING_SNAKE_CASE__													= 'こんばんは、㔺界。😀'
						SCREAMING_SNAKE_CASE__													= 'こんにちは、世界。こんばんは、世界。😀'
						SCREAMING_SNAKE_CASE__													= tokenizer.encode(prefix_text + input_text							)
						SCREAMING_SNAKE_CASE__													= tokenizer.encode(''       ,   prefix_text=prefix_text + input_text							)
						SCREAMING_SNAKE_CASE__													= tokenizer.encode(UpperCAmelCase_       ,   prefix_text=UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= tokenizer.decode(UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= tokenizer.decode(UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= tokenizer.decode(UpperCAmelCase_							)
						self.assertEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
						self.assertEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
						self.assertEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
	@slow
	def      A_				(			self							:   Union[str, Any]							):
						SCREAMING_SNAKE_CASE__													= self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese'							)

						# Testing tokenization
						SCREAMING_SNAKE_CASE__													= 'こんにちは、世界。'
						SCREAMING_SNAKE_CASE__													= 'こんばんは、㔺界。😀'

						SCREAMING_SNAKE_CASE__													= len(tokenizer.encode(UpperCAmelCase_							)							) - 2
						SCREAMING_SNAKE_CASE__													= len(tokenizer.encode(UpperCAmelCase_							)							) - 2

						SCREAMING_SNAKE_CASE__													= [1] + [0] * (len_prefix + len_text + 1)
						SCREAMING_SNAKE_CASE__													= [1] * (len_prefix + len_text + 1) + [0]
						SCREAMING_SNAKE_CASE__													= [1] + [1] * (len_prefix) + [0] * (len_text + 1)

						SCREAMING_SNAKE_CASE__													= tokenizer(prefix_text + input_text							).token_type_ids
						SCREAMING_SNAKE_CASE__													= tokenizer(''       ,   prefix_text=prefix_text + input_text							).token_type_ids
						SCREAMING_SNAKE_CASE__													= tokenizer(UpperCAmelCase_       ,   prefix_text=UpperCAmelCase_							).token_type_ids
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
	@slow
	def      A_				(			self							:   Tuple							):
						SCREAMING_SNAKE_CASE__													= self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese'							)

						SCREAMING_SNAKE_CASE__													= tokenizer.encode('あンいワ'							)
						SCREAMING_SNAKE_CASE__													= tokenizer.encode(''       ,   prefix_text='あンいワ'							)
						SCREAMING_SNAKE_CASE__													= tokenizer.encode('いワ'       ,   prefix_text='あン'							)

						self.assertEqual(tokenizer.decode(UpperCAmelCase_							)       ,   tokenizer.decode(UpperCAmelCase_							)							)
						self.assertEqual(tokenizer.decode(UpperCAmelCase_							)       ,   tokenizer.decode(UpperCAmelCase_							)							)
						self.assertNotEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
						self.assertNotEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)
						self.assertEqual(x_token_a[1]       ,   x_token_a[-1]							)  # SEG token
						self.assertEqual(x_token_a[1]       ,   x_token_a[3]							)  # SEG token
	@slow
	def      A_				(			self							:   Union[str, Any]							):
						SCREAMING_SNAKE_CASE__													= self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese'							)

						SCREAMING_SNAKE_CASE__													= [['武田信玄', 'は、'], ['織田信長', 'の配下の、']]
						SCREAMING_SNAKE_CASE__													= tokenizer(UpperCAmelCase_       ,   padding=UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= tokenizer.batch_encode_plus(UpperCAmelCase_       ,   padding=UpperCAmelCase_							)

						# fmt: off
						SCREAMING_SNAKE_CASE__													= [[35993, 8640, 25948, 35998, 30647, 35675, 35999, 35999], [35993, 10382, 9868, 35998, 30646, 9459, 30646, 35675]]
						SCREAMING_SNAKE_CASE__													= [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]]
						SCREAMING_SNAKE_CASE__													= [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]
						# fmt: on
						self.assertListEqual(x_token.input_ids       ,   UpperCAmelCase_							)
						self.assertListEqual(x_token.token_type_ids       ,   UpperCAmelCase_							)
						self.assertListEqual(x_token.attention_mask       ,   UpperCAmelCase_							)
						self.assertListEqual(x_token_a.input_ids       ,   UpperCAmelCase_							)
						self.assertListEqual(x_token_a.token_type_ids       ,   UpperCAmelCase_							)
						self.assertListEqual(x_token_a.attention_mask       ,   UpperCAmelCase_							)
	def      A_				(			self							:   Tuple							):
						# Intentionally convert some words to accommodate character fluctuations unique to Japanese
						pass





	def      A_				(			self							:   List[str]							):
						# tokenizer has no padding token
						pass





