


"""simple docstring"""





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 						snake_case_(				a__							,    unittest.TestCase	):
     __UpperCamelCase     =    GPTSanJapaneseTokenizer
     __UpperCamelCase     =    False
     __UpperCamelCase     =    {'''do_clean_text''': False, '''add_prefix_space''': False}






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           super().setUp()

           # fmt: off
           lowerCAmelCase    :		List[str]				=      ['''こん''', '''こんに''', '''にちは''', '''ばんは''', '''世界,㔺界''', '''、''', '''。''', '''<BR>''', '''<SP>''', '''<TAB>''', '''<URL>''', '''<EMAIL>''', '''<TEL>''', '''<DATE>''', '''<PRICE>''', '''<BLOCK>''', '''<KIGOU>''', '''<U2000U2BFF>''', '''<|emoji1|>''', '''<unk>''', '''<|bagoftoken|>''', '''<|endoftext|>''']
           # fmt: on
           lowerCAmelCase    :		int				=      {'''emoji''': {'''\ud83d\ude00''': '''<|emoji1|>'''}, '''emoji_inv''': {'''<|emoji1|>''': '''\ud83d\ude00'''}}  # 😀
           lowerCAmelCase    :		List[str]				=      {'''unk_token''': '''<unk>'''}

           lowerCAmelCase    :		Union[str, Any]				=      os.path.join(self.tmpdirname    , VOCAB_FILES_NAMES['''vocab_file'''] )
           lowerCAmelCase    :		Any				=      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    lowerCamelCase__   (		self     :    Union[str, Any]    , **UpperCamelCase_     :    Optional[Any] ):
           kwargs.update(self.special_tokens_map )
           return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname    , **UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    str    , UpperCamelCase_     :    Optional[int] ):
           lowerCAmelCase    :		int				=      '''こんにちは、世界。 \nこんばんは、㔺界。😀'''
           lowerCAmelCase    :		List[Any]				=      '''こんにちは、世界。 \nこんばんは、世界。😀'''
           return input_text, output_text






     def    lowerCamelCase__   (		self     :    Dict    , UpperCamelCase_     :    Dict ):
           lowerCAmelCase,					lowerCAmelCase    :		List[Any]				=      self.get_input_output_texts(UpperCamelCase_ )
           lowerCAmelCase    :		Optional[Any]				=      tokenizer.encode(UpperCamelCase_    , add_special_tokens=UpperCamelCase_ )
           lowerCAmelCase    :		List[str]				=      tokenizer.decode(UpperCamelCase_    , clean_up_tokenization_spaces=UpperCamelCase_ )
           return text, ids






     def    lowerCamelCase__   (		self     :    Any ):
           pass  # TODO add if relevant






     def    lowerCamelCase__   (		self     :    Union[str, Any] ):
           pass  # TODO add if relevant






     def    lowerCamelCase__   (		self     :    Union[str, Any] ):
           pass  # TODO add if relevant






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase    :		Any				=      self.get_tokenizer()

           # Testing tokenization
           lowerCAmelCase    :		int				=      '''こんにちは、世界。　こんばんは、㔺界。'''
           lowerCAmelCase    :		Union[str, Any]				=      ['''こん''', '''にちは''', '''、''', '''世界''', '''。''', '''<SP>''', '''こん''', '''ばんは''', '''、''', '''㔺界''', '''。''']
           lowerCAmelCase    :		Dict				=      tokenizer.tokenize(UpperCamelCase_ )
           self.assertListEqual(UpperCamelCase_    , UpperCamelCase_ )

           # Testing conversion to ids without special tokens
           lowerCAmelCase    :		int				=      [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
           lowerCAmelCase    :		Tuple				=      tokenizer.convert_tokens_to_ids(UpperCamelCase_ )
           self.assertListEqual(UpperCamelCase_    , UpperCamelCase_ )

           # Testing conversion to ids with special tokens
           lowerCAmelCase    :		Tuple				=      tokens + [tokenizer.unk_token]
           lowerCAmelCase    :		Tuple				=      [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 1_9]
           lowerCAmelCase    :		List[str]				=      tokenizer.convert_tokens_to_ids(UpperCamelCase_ )
           self.assertListEqual(UpperCamelCase_    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Union[str, Any] ):
           lowerCAmelCase    :		List[Any]				=      self.get_tokenizer()

           # Testing tokenization
           lowerCAmelCase    :		str				=      '''こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。'''
           lowerCAmelCase    :		List[Any]				=      '''こんにちは、、、、世界。こんばんは、、、、世界。'''
           lowerCAmelCase    :		Dict				=      tokenizer.encode(UpperCamelCase_ )
           lowerCAmelCase    :		Dict				=      tokenizer.decode(UpperCamelCase_ )
           self.assertEqual(UpperCamelCase_    , UpperCamelCase_ )






     @slow
     def    lowerCamelCase__   (		self     :    str ):
           lowerCAmelCase    :		str				=      self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' )

           # Testing tokenization
           lowerCAmelCase    :		Optional[int]				=      '''こんにちは、世界。'''
           lowerCAmelCase    :		Dict				=      '''こんばんは、㔺界。😀'''
           lowerCAmelCase    :		int				=      '''こんにちは、世界。こんばんは、世界。😀'''
           lowerCAmelCase    :		Any				=      tokenizer.encode(prefix_text + input_text )
           lowerCAmelCase    :		List[str]				=      tokenizer.encode(''''''    , prefix_text=prefix_text + input_text )
           lowerCAmelCase    :		Any				=      tokenizer.encode(UpperCamelCase_    , prefix_text=UpperCamelCase_ )
           lowerCAmelCase    :		Tuple				=      tokenizer.decode(UpperCamelCase_ )
           lowerCAmelCase    :		List[str]				=      tokenizer.decode(UpperCamelCase_ )
           lowerCAmelCase    :		Optional[int]				=      tokenizer.decode(UpperCamelCase_ )
           self.assertEqual(UpperCamelCase_    , UpperCamelCase_ )
           self.assertEqual(UpperCamelCase_    , UpperCamelCase_ )
           self.assertEqual(UpperCamelCase_    , UpperCamelCase_ )






     @slow
     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase    :		Optional[int]				=      self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' )

           # Testing tokenization
           lowerCAmelCase    :		Optional[int]				=      '''こんにちは、世界。'''
           lowerCAmelCase    :		Union[str, Any]				=      '''こんばんは、㔺界。😀'''

           lowerCAmelCase    :		Dict				=      len(tokenizer.encode(UpperCamelCase_ ) ) - 2
           lowerCAmelCase    :		List[Any]				=      len(tokenizer.encode(UpperCamelCase_ ) ) - 2

           lowerCAmelCase    :		List[Any]				=      [1] + [0] * (len_prefix + len_text + 1)
           lowerCAmelCase    :		Tuple				=      [1] * (len_prefix + len_text + 1) + [0]
           lowerCAmelCase    :		int				=      [1] + [1] * (len_prefix) + [0] * (len_text + 1)

           lowerCAmelCase    :		Dict				=      tokenizer(prefix_text + input_text ).token_type_ids
           lowerCAmelCase    :		Optional[int]				=      tokenizer(''''''    , prefix_text=prefix_text + input_text ).token_type_ids
           lowerCAmelCase    :		Dict				=      tokenizer(UpperCamelCase_    , prefix_text=UpperCamelCase_ ).token_type_ids
           self.assertListEqual(UpperCamelCase_    , UpperCamelCase_ )
           self.assertListEqual(UpperCamelCase_    , UpperCamelCase_ )
           self.assertListEqual(UpperCamelCase_    , UpperCamelCase_ )






     @slow
     def    lowerCamelCase__   (		self     :    Dict ):
           lowerCAmelCase    :		Optional[Any]				=      self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' )

           lowerCAmelCase    :		List[Any]				=      tokenizer.encode('''あンいワ''' )
           lowerCAmelCase    :		List[Any]				=      tokenizer.encode(''''''    , prefix_text='''あンいワ''' )
           lowerCAmelCase    :		List[Any]				=      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    lowerCamelCase__   (		self     :    Union[str, Any] ):
           lowerCAmelCase    :		Optional[int]				=      self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' )

           lowerCAmelCase    :		List[Any]				=      [['''武田信玄''', '''は、'''], ['''織田信長''', '''の配下の、''']]
           lowerCAmelCase    :		List[str]				=      tokenizer(UpperCamelCase_    , padding=UpperCamelCase_ )
           lowerCAmelCase    :		Optional[Any]				=      tokenizer.batch_encode_plus(UpperCamelCase_    , padding=UpperCamelCase_ )

           # fmt: off
           lowerCAmelCase    :		Optional[Any]				=      [[3_5_9_9_3, 8_6_4_0, 2_5_9_4_8, 3_5_9_9_8, 3_0_6_4_7, 3_5_6_7_5, 3_5_9_9_9, 3_5_9_9_9], [3_5_9_9_3, 1_0_3_8_2, 9_8_6_8, 3_5_9_9_8, 3_0_6_4_6, 9_4_5_9, 3_0_6_4_6, 3_5_6_7_5]]
           lowerCAmelCase    :		Tuple				=      [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]]
           lowerCAmelCase    :		int				=      [[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    lowerCamelCase__   (		self     :    Optional[int] ):
           # Intentionally convert some words to accommodate character fluctuations unique to Japanese
           pass






     def    lowerCamelCase__   (		self     :    int ):
           # tokenizer has no padding token
           pass







'''simple docstring'''




from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
							import tensorflow as tf

							from transformers import TFBlipTextModel
							from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST

class 			UpperCamelCase_ :





	def __init__(  self		,		A		,		A=12		,		A=7		,		A=True		,		A=True		,		A=True		,		A=99		,		A=32		,		A=32		,		A=2		,		A=4		,		A=37		,		A=0.1		,		A=0.1		,		A=512		,		A=0.0_2		,		A=0		,		A=None		,		)			->			Any:
								UpperCAmelCase     :		Optional[Any]							     =		parent
								UpperCAmelCase     :		str							     =		batch_size
								UpperCAmelCase     :		Union[str, Any]							     =		seq_length
								UpperCAmelCase     :		Optional[Any]							     =		is_training
								UpperCAmelCase     :		int							     =		use_input_mask
								UpperCAmelCase     :		List[Any]							     =		use_labels
								UpperCAmelCase     :		Dict							     =		vocab_size
								UpperCAmelCase     :		str							     =		hidden_size
								UpperCAmelCase     :		List[Any]							     =		projection_dim
								UpperCAmelCase     :		Tuple							     =		num_hidden_layers
								UpperCAmelCase     :		Dict							     =		num_attention_heads
								UpperCAmelCase     :		Optional[Any]							     =		intermediate_size
								UpperCAmelCase     :		Any							     =		dropout
								UpperCAmelCase     :		List[Any]							     =		attention_dropout
								UpperCAmelCase     :		Optional[Any]							     =		max_position_embeddings
								UpperCAmelCase     :		Tuple							     =		initializer_range
								UpperCAmelCase     :		Optional[Any]							     =		scope
								UpperCAmelCase     :		Union[str, Any]							     =		bos_token_id





	def  _lowercase(  self    )			->			Tuple:
								UpperCAmelCase     :		Optional[int]							     =		ids_tensor([self.batch_size, self.seq_length]		,		self.vocab_size    )

								UpperCAmelCase     :		Union[str, Any]							     =		None
								if self.use_input_mask:
															UpperCAmelCase     :		str							     =		random_attention_mask([self.batch_size, self.seq_length]    )

								if input_mask is not None:
															UpperCAmelCase     :		Tuple							     =		input_mask.numpy()
															UpperCAmelCase     ,     UpperCAmelCase     :		int							     =		input_mask.shape
															UpperCAmelCase     :		Optional[int]							     =		np.random.randint(1		,		seq_length - 1		,		size=(batch_size,)    )
															for batch_idx, start_index in enumerate(A    ):
																						UpperCAmelCase     :		Tuple							     =		1
																						UpperCAmelCase     :		Optional[Any]							     =		0

								UpperCAmelCase     :		int							     =		self.get_config()

								return config, input_ids, tf.convert_to_tensor(A    )





	def  _lowercase(  self    )			->			int:
								return BlipTextConfig(
								    vocab_size=self.vocab_size		,		hidden_size=self.hidden_size		,		projection_dim=self.projection_dim		,		num_hidden_layers=self.num_hidden_layers		,		num_attention_heads=self.num_attention_heads		,		intermediate_size=self.intermediate_size		,		dropout=self.dropout		,		attention_dropout=self.attention_dropout		,		max_position_embeddings=self.max_position_embeddings		,		initializer_range=self.initializer_range		,		bos_token_id=self.bos_token_id		,		)





	def  _lowercase(  self		,		A		,		A		,		A    )			->			Union[str, Any]:
								UpperCAmelCase     :		int							     =		TFBlipTextModel(config=A    )
								UpperCAmelCase     :		Union[str, Any]							     =		model(A		,		attention_mask=A		,		training=A    )
								UpperCAmelCase     :		int							     =		model(A		,		training=A    )
								self.parent.assertEqual(result.last_hidden_state.shape		,		(self.batch_size, self.seq_length, self.hidden_size)    )
								self.parent.assertEqual(result.pooler_output.shape		,		(self.batch_size, self.hidden_size)    )






	def  _lowercase(  self    )			->			Optional[int]:
								UpperCAmelCase     :		Dict							     =		self.prepare_config_and_inputs()
								UpperCAmelCase     ,     UpperCAmelCase     ,     UpperCAmelCase     :		Any							     =		config_and_inputs
								UpperCAmelCase     :		Optional[Any]							     =		{"""input_ids""": input_ids, """attention_mask""": input_mask}
								return config, inputs_dict




@require_tf
class 			UpperCamelCase_ (							__magic_name__      ,     unittest.TestCase			):
	lowercase          = (TFBlipTextModel,) if is_tf_available() else ()
	lowercase          = False
	lowercase          = False
	lowercase          = False





	def  _lowercase(  self    )			->			int:
								UpperCAmelCase     :		Union[str, Any]							     =		BlipTextModelTester(self    )
								UpperCAmelCase     :		List[str]							     =		ConfigTester(self		,		config_class=A		,		hidden_size=37    )





	def  _lowercase(  self    )			->			Tuple:
								self.config_tester.run_common_tests()





	def  _lowercase(  self    )			->			List[Any]:
								UpperCAmelCase     :		List[Any]							     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*A    )





	def  _lowercase(  self    )			->			List[str]:
								pass





	def  _lowercase(  self    )			->			Optional[int]:
								pass





	@unittest.skip(reason="""Blip does not use inputs_embeds"""    )
	def  _lowercase(  self    )			->			Union[str, Any]:
								pass





	@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""    )
	def  _lowercase(  self    )			->			Optional[int]:
								pass





	@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""    )
	def  _lowercase(  self    )			->			Dict:
								pass





	@slow
	def  _lowercase(  self    )			->			Dict:
								for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCAmelCase     :		Any							     =		TFBlipTextModel.from_pretrained(A    )
															self.assertIsNotNone(A    )






	def  _lowercase(  self		,		A=True    )			->			str:
								super().test_pt_tf_model_equivalence(allow_missing_keys=A    )

