


'''simple docstring'''



from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow

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


if is_tf_available():
		import numpy as np
		import tensorflow as tf

		from transformers import (
		    TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    FlaubertConfig,
		    TFFlaubertForMultipleChoice,
		    TFFlaubertForQuestionAnsweringSimple,
		    TFFlaubertForSequenceClassification,
		    TFFlaubertForTokenClassification,
		    TFFlaubertModel,
		    TFFlaubertWithLMHeadModel,
		)


class 						_snake_case   :




					def __init__( self							,							_lowerCamelCase							,							):
								UpperCAmelCase__					:   List[str]       				=       parent
								UpperCAmelCase__					:   List[str]       				=       13
								UpperCAmelCase__					:   str       				=       7
								UpperCAmelCase__					:   str       				=       True
								UpperCAmelCase__					:   List[str]       				=       True
								UpperCAmelCase__					:   Tuple       				=       True
								UpperCAmelCase__					:   str       				=       True
								UpperCAmelCase__					:   Dict       				=       True
								UpperCAmelCase__					:   Optional[int]       				=       False
								UpperCAmelCase__					:   Dict       				=       False
								UpperCAmelCase__					:   List[str]       				=       False
								UpperCAmelCase__					:   int       				=       2
								UpperCAmelCase__					:   Dict       				=       99
								UpperCAmelCase__					:   Optional[Any]       				=       0
								UpperCAmelCase__					:   int       				=       32
								UpperCAmelCase__					:   Tuple       				=       2
								UpperCAmelCase__					:   Any       				=       4
								UpperCAmelCase__					:   Union[str, Any]       				=       0.1
								UpperCAmelCase__					:   List[Any]       				=       0.1
								UpperCAmelCase__					:   Union[str, Any]       				=       512
								UpperCAmelCase__					:   Dict       				=       16
								UpperCAmelCase__					:   Optional[Any]       				=       2
								UpperCAmelCase__					:   int       				=       0.02
								UpperCAmelCase__					:   Any       				=       3
								UpperCAmelCase__					:   str       				=       4
								UpperCAmelCase__					:   str       				=       """last"""
								UpperCAmelCase__					:   Tuple       				=       True
								UpperCAmelCase__					:   Optional[Any]       				=       None
								UpperCAmelCase__					:   Dict       				=       0




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   str       				=       ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size)
								UpperCAmelCase__					:   int       				=       random_attention_mask([self.batch_size, self.seq_length]							,							dtype=tf.floataa)

								UpperCAmelCase__					:   List[str]       				=       None
								if self.use_input_lengths:
											UpperCAmelCase__					:   List[Any]       				=       (
											    ids_tensor([self.batch_size]							,							vocab_size=2) + self.seq_length - 2
											)  # small variation of seq_length

								UpperCAmelCase__					:   List[str]       				=       None
								if self.use_token_type_ids:
											UpperCAmelCase__					:   Tuple       				=       ids_tensor([self.batch_size, self.seq_length]							,							self.n_langs)

								UpperCAmelCase__					:   Optional[Any]       				=       None
								UpperCAmelCase__					:   Optional[Any]       				=       None
								UpperCAmelCase__					:   int       				=       None
								if self.use_labels:
											UpperCAmelCase__					:   List[Any]       				=       ids_tensor([self.batch_size]							,							self.type_sequence_label_size)
											UpperCAmelCase__					:   str       				=       ids_tensor([self.batch_size, self.seq_length]							,							self.num_labels)
											UpperCAmelCase__					:   Any       				=       ids_tensor([self.batch_size]							,							2							,							dtype=tf.floataa)
											UpperCAmelCase__					:   List[Any]       				=       ids_tensor([self.batch_size]							,							self.num_choices)

								UpperCAmelCase__					:   List[str]       				=       FlaubertConfig(
								    vocab_size=self.vocab_size							,							n_special=self.n_special							,							emb_dim=self.hidden_size							,							n_layers=self.num_hidden_layers							,							n_heads=self.num_attention_heads							,							dropout=self.hidden_dropout_prob							,							attention_dropout=self.attention_probs_dropout_prob							,							gelu_activation=self.gelu_activation							,							sinusoidal_embeddings=self.sinusoidal_embeddings							,							asm=self.asm							,							causal=self.causal							,							n_langs=self.n_langs							,							max_position_embeddings=self.max_position_embeddings							,							initializer_range=self.initializer_range							,							summary_type=self.summary_type							,							use_proj=self.use_proj							,							bos_token_id=self.bos_token_id							,							)

								return (
								    config,
								    input_ids,
								    token_type_ids,
								    input_lengths,
								    sequence_labels,
								    token_labels,
								    is_impossible_labels,
								    choice_labels,
								    input_mask,
								)




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							):
								UpperCAmelCase__					:   List[Any]       				=       TFFlaubertModel(config=_lowerCamelCase)
								UpperCAmelCase__					:   Union[str, Any]       				=       {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
								UpperCAmelCase__					:   Optional[int]       				=       model(_lowerCamelCase)

								UpperCAmelCase__					:   List[str]       				=       [input_ids, input_mask]
								UpperCAmelCase__					:   Optional[Any]       				=       model(_lowerCamelCase)
								self.parent.assertEqual(result.last_hidden_state.shape							,							(self.batch_size, self.seq_length, self.hidden_size))




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							):
								UpperCAmelCase__					:   Optional[int]       				=       TFFlaubertWithLMHeadModel(_lowerCamelCase)

								UpperCAmelCase__					:   List[Any]       				=       {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
								UpperCAmelCase__					:   Dict       				=       model(_lowerCamelCase)

								self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.vocab_size))




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							):
								UpperCAmelCase__					:   str       				=       TFFlaubertForQuestionAnsweringSimple(_lowerCamelCase)

								UpperCAmelCase__					:   Tuple       				=       {"""input_ids""": input_ids, """lengths""": input_lengths}

								UpperCAmelCase__					:   Optional[int]       				=       model(_lowerCamelCase)

								self.parent.assertEqual(result.start_logits.shape							,							(self.batch_size, self.seq_length))
								self.parent.assertEqual(result.end_logits.shape							,							(self.batch_size, self.seq_length))




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							):
								UpperCAmelCase__					:   List[Any]       				=       TFFlaubertForSequenceClassification(_lowerCamelCase)

								UpperCAmelCase__					:   Any       				=       {"""input_ids""": input_ids, """lengths""": input_lengths}

								UpperCAmelCase__					:   str       				=       model(_lowerCamelCase)

								self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.type_sequence_label_size))




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							):
								UpperCAmelCase__					:   str       				=       self.num_labels
								UpperCAmelCase__					:   List[Any]       				=       TFFlaubertForTokenClassification(config=_lowerCamelCase)
								UpperCAmelCase__					:   List[str]       				=       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								UpperCAmelCase__					:   str       				=       model(_lowerCamelCase)
								self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.num_labels))




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							):
								UpperCAmelCase__					:   int       				=       self.num_choices
								UpperCAmelCase__					:   int       				=       TFFlaubertForMultipleChoice(config=_lowerCamelCase)
								UpperCAmelCase__					:   Tuple       				=       tf.tile(tf.expand_dims(_lowerCamelCase							,							1)							,							(1, self.num_choices, 1))
								UpperCAmelCase__					:   int       				=       tf.tile(tf.expand_dims(_lowerCamelCase							,							1)							,							(1, self.num_choices, 1))
								UpperCAmelCase__					:   Union[str, Any]       				=       tf.tile(tf.expand_dims(_lowerCamelCase							,							1)							,							(1, self.num_choices, 1))
								UpperCAmelCase__					:   List[Any]       				=       {
								    """input_ids""": multiple_choice_inputs_ids,
								    """attention_mask""": multiple_choice_input_mask,
								    """token_type_ids""": multiple_choice_token_type_ids,
								}
								UpperCAmelCase__					:   Tuple       				=       model(_lowerCamelCase)
								self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_choices))




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Optional[int]       				=       self.prepare_config_and_inputs()
								(
								    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    
								)					:   List[Any]       				=       config_and_inputs
								UpperCAmelCase__					:   Union[str, Any]       				=       {
								    """input_ids""": input_ids,
								    """token_type_ids""": token_type_ids,
								    """langs""": token_type_ids,
								    """lengths""": input_lengths,
								}
								return config, inputs_dict


@require_tf
class 						_snake_case   (       a__    ,				a__    ,				unittest.TestCase		):
					lowerCAmelCase :Optional[int]						=   (
					    (
					        TFFlaubertModel,
					        TFFlaubertWithLMHeadModel,
					        TFFlaubertForSequenceClassification,
					        TFFlaubertForQuestionAnsweringSimple,
					        TFFlaubertForTokenClassification,
					        TFFlaubertForMultipleChoice,
					    )
					    if is_tf_available()
					    else ()
					)
					lowerCAmelCase :Union[str, Any]						=   (
					    (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
					)  # TODO (PVP): Check other models whether language generation is also applicable
					lowerCAmelCase :List[Any]						=   (
					    {
					        '''feature-extraction''': TFFlaubertModel,
					        '''fill-mask''': TFFlaubertWithLMHeadModel,
					        '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
					        '''text-classification''': TFFlaubertForSequenceClassification,
					        '''token-classification''': TFFlaubertForTokenClassification,
					        '''zero-shot''': TFFlaubertForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					lowerCAmelCase :Tuple						=   False
					lowerCAmelCase :str						=   False




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase):
								if (
								    pipeline_test_casse_name == "QAPipelineTests"
								    and tokenizer_name is not None
								    and not tokenizer_name.endswith("""Fast""")
								):
											# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
											# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
											# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
											return True

								return False




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Any       				=       TFFlaubertModelTester(self)
								UpperCAmelCase__					:   Any       				=       ConfigTester(self							,							config_class=_lowerCamelCase							,							emb_dim=37)




					def 			snake_case__  ( self):
								self.config_tester.run_common_tests()




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   int       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   int       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[Any]       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[Any]       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Optional[Any]       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_for_token_classification(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Optional[Any]       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_for_multiple_choice(*_lowerCamelCase)




					@slow
					def 			snake_case__  ( self):
								for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCAmelCase__					:   Dict       				=       TFFlaubertModel.from_pretrained(_lowerCamelCase)
											self.assertIsNotNone(_lowerCamelCase)


@require_tf
@require_sentencepiece
@require_tokenizers
class 						_snake_case   (       unittest.TestCase		):




					@slow
					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Tuple       				=       TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""")

								UpperCAmelCase__					:   List[Any]       				=       tf.convert_to_tensor(
								    [[0, 158, 735, 2592, 1424, 6727, 82, 1]]							,							dtype=tf.intaa							,							)  # "J'aime flaubert !"

								UpperCAmelCase__					:   int       				=       model(_lowerCamelCase)[0]
								UpperCAmelCase__					:   str       				=       tf.TensorShape((1, 8, 512))
								self.assertEqual(output.shape							,							_lowerCamelCase)
								# compare the actual values for a slice.
								UpperCAmelCase__					:   Any       				=       tf.convert_to_tensor(
								    [
								        [
								            [-1.8768773, -1.566555, 0.27072418],
								            [-1.6920038, -0.5873505, 1.9329599],
								            [-2.9563985, -1.6993835, 1.7972052],
								        ]
								    ]							,							dtype=tf.floataa							,							)

								self.assertTrue(np.allclose(output[:, :3, :3].numpy()							,							expected_slice.numpy()							,							atol=1e-4))



'''simple docstring'''



import unittest

import numpy as np

from transformers import RoFormerConfig, 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.roformer.modeling_flax_roformer import (
		    FlaxRoFormerForMaskedLM,
		    FlaxRoFormerForMultipleChoice,
		    FlaxRoFormerForQuestionAnswering,
		    FlaxRoFormerForSequenceClassification,
		    FlaxRoFormerForTokenClassification,
		    FlaxRoFormerModel,
		)


class 						_snake_case   (       unittest.TestCase		):




					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							,							):
								UpperCAmelCase__					:   Union[str, Any]       				=       parent
								UpperCAmelCase__					:   str       				=       batch_size
								UpperCAmelCase__					:   Tuple       				=       seq_length
								UpperCAmelCase__					:   Any       				=       is_training
								UpperCAmelCase__					:   Tuple       				=       use_attention_mask
								UpperCAmelCase__					:   Optional[Any]       				=       use_token_type_ids
								UpperCAmelCase__					:   Optional[Any]       				=       use_labels
								UpperCAmelCase__					:   str       				=       vocab_size
								UpperCAmelCase__					:   Dict       				=       hidden_size
								UpperCAmelCase__					:   int       				=       num_hidden_layers
								UpperCAmelCase__					:   Optional[int]       				=       num_attention_heads
								UpperCAmelCase__					:   Optional[int]       				=       intermediate_size
								UpperCAmelCase__					:   Tuple       				=       hidden_act
								UpperCAmelCase__					:   Optional[Any]       				=       hidden_dropout_prob
								UpperCAmelCase__					:   Union[str, Any]       				=       attention_probs_dropout_prob
								UpperCAmelCase__					:   List[Any]       				=       max_position_embeddings
								UpperCAmelCase__					:   Optional[int]       				=       type_vocab_size
								UpperCAmelCase__					:   str       				=       type_sequence_label_size
								UpperCAmelCase__					:   Optional[int]       				=       initializer_range
								UpperCAmelCase__					:   str       				=       num_choices




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[str]       				=       ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size)

								UpperCAmelCase__					:   Union[str, Any]       				=       None
								if self.use_attention_mask:
											UpperCAmelCase__					:   Tuple       				=       random_attention_mask([self.batch_size, self.seq_length])

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

								UpperCAmelCase__					:   Optional[Any]       				=       RoFormerConfig(
								    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 			snake_case__  ( self):
								UpperCAmelCase__					:   List[Any]       				=       self.prepare_config_and_inputs()
								UpperCAmelCase__				,    UpperCAmelCase__				,    UpperCAmelCase__				,    UpperCAmelCase__					:   Optional[Any]       				=       config_and_inputs
								UpperCAmelCase__					:   Dict       				=       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
								return config, inputs_dict


@require_flax
class 						_snake_case   (       a__    ,				unittest.TestCase		):
					lowerCAmelCase :Optional[int]						=   True

					lowerCAmelCase :Any						=   (
					    (
					        FlaxRoFormerModel,
					        FlaxRoFormerForMaskedLM,
					        FlaxRoFormerForSequenceClassification,
					        FlaxRoFormerForTokenClassification,
					        FlaxRoFormerForMultipleChoice,
					        FlaxRoFormerForQuestionAnswering,
					    )
					    if is_flax_available()
					    else ()
					)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[str]       				=       FlaxRoFormerModelTester(self)




					@slow
					def 			snake_case__  ( self):
								for model_class_name in self.all_model_classes:
											UpperCAmelCase__					:   List[str]       				=       model_class_name.from_pretrained("""junnyu/roformer_chinese_small"""							,							from_pt=_lowerCamelCase)
											UpperCAmelCase__					:   Dict       				=       model(np.ones((1, 1)))
											self.assertIsNotNone(_lowerCamelCase)


@require_flax
class 						_snake_case   (       unittest.TestCase		):




					@slow
					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Any       				=       FlaxRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""")
								UpperCAmelCase__					:   int       				=       jnp.array([[0, 1, 2, 3, 4, 5]])
								UpperCAmelCase__					:   Optional[int]       				=       model(_lowerCamelCase)[0]

								UpperCAmelCase__					:   Union[str, Any]       				=       5_0000

								UpperCAmelCase__					:   Any       				=       (1, 6, vocab_size)
								self.assertEqual(output.shape							,							_lowerCamelCase)

								UpperCAmelCase__					:   Union[str, Any]       				=       jnp.array(
								    [[[-0.1205, -1.0265, 0.2922], [-1.5134, 0.1974, 0.1519], [-5.0135, -3.9003, -0.8404]]])

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