



'''simple docstring'''





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 					_A (			unittest.TestCase					):



       def __init__(	self							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  Any  ,	__SCREAMING_SNAKE_CASE							:  List[str]=13  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=7  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=True  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]=True  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=True  ,	__SCREAMING_SNAKE_CASE							:  List[str]=True  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]=99  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=32  ,	__SCREAMING_SNAKE_CASE							:  Tuple=5  ,	__SCREAMING_SNAKE_CASE							:  Any=4  ,	__SCREAMING_SNAKE_CASE							:  Tuple=37  ,	__SCREAMING_SNAKE_CASE							:  Tuple="gelu"  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=0.1  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]=0.1  ,	__SCREAMING_SNAKE_CASE							:  Tuple=512  ,	__SCREAMING_SNAKE_CASE							:  str=16  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=2  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=0.02  ,	__SCREAMING_SNAKE_CASE							:  str=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 	_lowerCamelCase    (	self							:  int):


           '''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=__SCREAMING_SNAKE_CASE  ,	initializer_range=self.initializer_range  ,	)

           return config, input_ids, token_type_ids, attention_mask



       def 	_lowerCamelCase    (	self							:  str):


           '''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 	_lowerCamelCase    (	self							:  List[Any]):


           '''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 					_A (			__UpperCAmelCase   ,unittest.TestCase					):
       UpperCamelCase__						:					Union[str, Any]		      =		True

       UpperCamelCase__						:					Optional[Any]		      =		(
           (
               FlaxBertModel,
               FlaxBertForPreTraining,
               FlaxBertForMaskedLM,
               FlaxBertForMultipleChoice,
               FlaxBertForQuestionAnswering,
               FlaxBertForNextSentencePrediction,
               FlaxBertForSequenceClassification,
               FlaxBertForTokenClassification,
               FlaxBertForQuestionAnswering,
           )
           if is_flax_available()
           else ()
       )



       def 	_lowerCamelCase    (	self							:  str):


           '''simple docstring'''

           __a		 =       FlaxBertModelTester(self)





       @slow
       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           __a		 =       FlaxBertModel.from_pretrained('''bert-base-cased''')
           __a		 =       model(np.ones((1, 1)))
           self.assertIsNotNone(__SCREAMING_SNAKE_CASE)










import unittest

from transformers import GPTSwaTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


__snake_case		:Dict   	=  get_tests_dir('''fixtures/test_sentencepiece_with_bytefallback.model''')

@require_sentencepiece
@require_tokenizers
class 					_A (			__UpperCAmelCase   ,unittest.TestCase					):
       UpperCamelCase__						:					List[str]		      =		GPTSwaTokenizer
       UpperCamelCase__						:					Dict		      =		False
       UpperCamelCase__						:					int		      =		True
       UpperCamelCase__						:					List[Any]		      =		False



       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           super().setUp()

           # We have a SentencePiece fixture for testing
           __a		 =       GPTSwaTokenizer(__SCREAMING_SNAKE_CASE  ,	eos_token='''<unk>'''  ,	bos_token='''<unk>'''  ,	pad_token='''<unk>''')

           tokenizer.save_pretrained(self.tmpdirname)



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  int):


           '''simple docstring'''

           __a		 =       '''This is a test'''
           __a		 =       '''This is a test'''
           return input_text, output_text



       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           __a		 =       '''<s>'''
           __a		 =       1

           self.assertEqual(self.get_tokenizer()._convert_token_to_id(__SCREAMING_SNAKE_CASE)  ,	__SCREAMING_SNAKE_CASE)
           self.assertEqual(self.get_tokenizer()._convert_id_to_token(__SCREAMING_SNAKE_CASE)  ,	__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           __a		 =       list(self.get_tokenizer().get_vocab().keys())

           self.assertEqual(vocab_keys[0]  ,	'''<unk>''')
           self.assertEqual(vocab_keys[1]  ,	'''<s>''')
           self.assertEqual(vocab_keys[-1]  ,	'''j''')
           self.assertEqual(len(__SCREAMING_SNAKE_CASE)  ,	2_000)



       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           self.assertEqual(self.get_tokenizer().vocab_size  ,	2_000)



       def 	_lowerCamelCase    (	self							:  List[str]):


           '''simple docstring'''

           __a		 =       GPTSwaTokenizer(__SCREAMING_SNAKE_CASE)

           __a		 =       tokenizer.tokenize('''This is a test''')
           self.assertListEqual(__SCREAMING_SNAKE_CASE  ,	['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''])

           self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE)  ,	[465, 287, 265, 631, 842])

           __a		 =       tokenizer.tokenize('''I was born in 92000, and this is falsé.''')
           # fmt: off
           self.assertListEqual(
               __SCREAMING_SNAKE_CASE  ,	['''▁I''', '''▁was''', '''▁bor''', '''n''', '''▁in''', '''▁''', '''<0x39>''', '''2''', '''0''', '''0''', '''0''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁f''', '''al''', '''s''', '''<0xC3>''', '''<0xA9>''', '''.''']  ,	)
           # fmt: on

           __a		 =       tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE)
           self.assertListEqual(
               __SCREAMING_SNAKE_CASE  ,	[262, 272, 1_525, 286, 271, 268, 60, 916, 633, 633, 633, 259, 266, 301, 287, 384, 367, 263, 198, 172, 260]  ,	)

           __a		 =       tokenizer.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE)
           # fmt: off
           self.assertListEqual(
               __SCREAMING_SNAKE_CASE  ,	['''▁I''', '''▁was''', '''▁bor''', '''n''', '''▁in''', '''▁''', '''<0x39>''', '''2''', '''0''', '''0''', '''0''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁f''', '''al''', '''s''', '''<0xC3>''', '''<0xA9>''', '''.'''])
           # fmt: on



       def 	_lowerCamelCase    (	self							:  Any):


           '''simple docstring'''

           __a		 =       GPTSwaTokenizer(__SCREAMING_SNAKE_CASE)
           __a		 =       ['''This is a test''', '''I was born in 92000, and this is falsé.''']
           __a		 =       [
               [465, 287, 265, 631, 842],
               [262, 272, 1_525, 286, 271, 268, 60, 916, 633, 633, 633, 259, 266, 301, 287, 384, 367, 263, 198, 172, 260],
           ]

           # Test that encode_fast returns the same as tokenize + convert_tokens_to_ids
           for text, expected_ids in zip(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE):
               self.assertListEqual(tokenizer.encode_fast(__SCREAMING_SNAKE_CASE)  ,	__SCREAMING_SNAKE_CASE)

           # Test that decode_fast returns the input text
           for text, token_ids in zip(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE):
               self.assertEqual(tokenizer.decode_fast(__SCREAMING_SNAKE_CASE)  ,	__SCREAMING_SNAKE_CASE)





       @slow
       def 	_lowerCamelCase    (	self							:  Any):


           '''simple docstring'''

           __a		 =       [
               '''<|python|>def fibonacci(n)\n    if n < 0:\n        print(\'Incorrect input\')''',
               '''Hey there, how are you doing this fine day?''',
               '''This is a text with a trailing spaces followed by a dot     .''',
               '''Häj sväjs lillebrör! =)''',
               '''Det är inget fel på Mr. Cool''',
           ]

           # fmt: off
           __a		 =       {'''input_ids''': [[63_423, 5, 6_811, 14_954, 282, 816, 3_821, 63_466, 63_425, 63_462, 18, 63_978, 678, 301, 1_320, 63_423, 63_455, 63_458, 18, 63_982, 4_246, 3_940, 1_901, 47_789, 5_547, 18_994], [19_630, 1_100, 63_446, 1_342, 633, 544, 4_488, 593, 5_102, 2_416, 63_495, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1_652, 428, 268, 1_936, 515, 268, 58_593, 22_413, 9_106, 546, 268, 33_213, 63_979, 698, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [55_130, 63_450, 924, 63_449, 2_249, 4_062, 1_558, 318, 63_504, 21_498, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [509, 377, 2_827, 2_559, 332, 6_575, 63_443, 26_801, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''token_type_ids''': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}
           # fmt: on
           self.tokenizer_integration_test_util(
               expected_encoding=__SCREAMING_SNAKE_CASE  ,	model_name='''AI-Sweden/gpt-sw3-126m'''  ,	sequences=__SCREAMING_SNAKE_CASE  ,	)





