






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 						lowerCAmelCase   :

					'''simple docstring'''

					def __init__(     self :		str						,   __a :		Tuple						,   )	->     List[Any]:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   parent
												__lowercase					:							List[Any]			=   13
												__lowercase					:							List[str]			=   7
												__lowercase					:							str			=   True
												__lowercase					:							Optional[int]			=   True
												__lowercase					:							Tuple			=   True
												__lowercase					:							Optional[Any]			=   True
												__lowercase					:							Any			=   True
												__lowercase					:							Optional[Any]			=   False
												__lowercase					:							Union[str, Any]			=   False
												__lowercase					:							Union[str, Any]			=   False
												__lowercase					:							Dict			=   2
												__lowercase					:							List[str]			=   99
												__lowercase					:							List[str]			=   0
												__lowercase					:							Tuple			=   32
												__lowercase					:							List[Any]			=   2
												__lowercase					:							Tuple			=   4
												__lowercase					:							Optional[Any]			=   0.1
												__lowercase					:							Any			=   0.1
												__lowercase					:							Union[str, Any]			=   512
												__lowercase					:							List[Any]			=   16
												__lowercase					:							List[Any]			=   2
												__lowercase					:							str			=   0.02
												__lowercase					:							Optional[Any]			=   3
												__lowercase					:							str			=   4
												__lowercase					:							List[str]			=   """last"""
												__lowercase					:							int			=   True
												__lowercase					:							List[Any]			=   None
												__lowercase					:							Optional[int]			=   0



					def 					lowerCAmelCase (     self :		Union[str, Any]							)	->     List[Any]:


												"""simple docstring"""

												__lowercase					:							List[str]			=   ids_tensor([self.batch_size, self.seq_length]						,   self.vocab_size							)
												__lowercase					:							str			=   random_attention_mask([self.batch_size, self.seq_length]						,   dtype=tf.floataa							)

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

												__lowercase					:							Optional[int]			=   None
												if self.use_token_type_ids:
																			__lowercase					:							Optional[Any]			=   ids_tensor([self.batch_size, self.seq_length]						,   self.n_langs							)

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

												__lowercase					:							List[Any]			=   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 					lowerCAmelCase (     self :		List[Any]						,   __a :		Any						,   __a :		Tuple						,   __a :		str						,   __a :		Optional[Any]						,   __a :		int						,   __a :		Union[str, Any]						,   __a :		Optional[int]						,   __a :		List[str]						,   __a :		Dict						,   )	->     List[Any]:


												"""simple docstring"""

												__lowercase					:							List[str]			=   TFFlaubertModel(config=__a							)
												__lowercase					:							Dict			=   {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
												__lowercase					:							Dict			=   model(__a							)

												__lowercase					:							Optional[int]			=   [input_ids, input_mask]
												__lowercase					:							Union[str, Any]			=   model(__a							)
												self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.hidden_size)							)



					def 					lowerCAmelCase (     self :		str						,   __a :		int						,   __a :		Union[str, Any]						,   __a :		Tuple						,   __a :		Union[str, Any]						,   __a :		Any						,   __a :		Dict						,   __a :		Optional[Any]						,   __a :		int						,   __a :		Optional[int]						,   )	->     Union[str, Any]:


												"""simple docstring"""

												__lowercase					:							List[str]			=   TFFlaubertWithLMHeadModel(__a							)

												__lowercase					:							Optional[int]			=   {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
												__lowercase					:							List[Any]			=   model(__a							)

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



					def 					lowerCAmelCase (     self :		Dict						,   __a :		Optional[Any]						,   __a :		List[Any]						,   __a :		Optional[Any]						,   __a :		List[Any]						,   __a :		int						,   __a :		str						,   __a :		Optional[Any]						,   __a :		List[Any]						,   __a :		Optional[Any]						,   )	->     Union[str, Any]:


												"""simple docstring"""

												__lowercase					:							Dict			=   TFFlaubertForQuestionAnsweringSimple(__a							)

												__lowercase					:							Dict			=   {"""input_ids""": input_ids, """lengths""": input_lengths}

												__lowercase					:							Optional[int]			=   model(__a							)

												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 					lowerCAmelCase (     self :		Dict						,   __a :		Any						,   __a :		List[str]						,   __a :		str						,   __a :		Tuple						,   __a :		Optional[int]						,   __a :		Any						,   __a :		int						,   __a :		Any						,   __a :		Optional[Any]						,   )	->     List[str]:


												"""simple docstring"""

												__lowercase					:							str			=   TFFlaubertForSequenceClassification(__a							)

												__lowercase					:							int			=   {"""input_ids""": input_ids, """lengths""": input_lengths}

												__lowercase					:							List[str]			=   model(__a							)

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



					def 					lowerCAmelCase (     self :		int						,   __a :		Union[str, Any]						,   __a :		List[str]						,   __a :		Tuple						,   __a :		Optional[int]						,   __a :		List[Any]						,   __a :		Optional[Any]						,   __a :		Dict						,   __a :		Union[str, Any]						,   __a :		int						,   )	->     Dict:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.num_labels
												__lowercase					:							Dict			=   TFFlaubertForTokenClassification(config=__a							)
												__lowercase					:							Tuple			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__lowercase					:							Any			=   model(__a							)
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.seq_length, self.num_labels)							)



					def 					lowerCAmelCase (     self :		Optional[Any]						,   __a :		List[Any]						,   __a :		Dict						,   __a :		Optional[Any]						,   __a :		Tuple						,   __a :		List[Any]						,   __a :		Any						,   __a :		Any						,   __a :		Dict						,   __a :		str						,   )	->     Optional[int]:


												"""simple docstring"""

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



					def 					lowerCAmelCase (     self :		int							)	->     Optional[int]:


												"""simple docstring"""

												__lowercase					:							Union[str, Any]			=   self.prepare_config_and_inputs()
												(
												    (
												    __lowercase
												)	,	(
												    __lowercase
												)	,	(
												    __lowercase
												)	,	(
												    __lowercase
												)	,	(
												    __lowercase
												)	,	(
												    __lowercase
												)	,	(
												    __lowercase
												)	,	(
												    __lowercase
												)	,	(
												    __lowercase
												)	,	
												)					:							str			=   config_and_inputs
												__lowercase					:							Optional[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 						lowerCAmelCase   (       __a					,				__a					,				unittest.TestCase	):

					'''simple docstring'''

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



					def 					lowerCAmelCase (     self :		Optional[Any]						,   __a :		Union[str, Any]						,   __a :		int						,   __a :		List[str]						,   __a :		Any						,   __a :		Union[str, Any]							)	->     int:


												"""simple docstring"""

												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 					lowerCAmelCase (     self :		Tuple							)	->     str:


												"""simple docstring"""

												__lowercase					:							Any			=   TFFlaubertModelTester(self							)
												__lowercase					:							Optional[int]			=   ConfigTester(self						,   config_class=__a						,   emb_dim=37							)



					def 					lowerCAmelCase (     self :		Dict							)	->     Optional[Any]:


												"""simple docstring"""

												self.config_tester.run_common_tests()



					def 					lowerCAmelCase (     self :		Union[str, Any]							)	->     Any:


												"""simple docstring"""

												__lowercase					:							str			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_model(*__a							)



					def 					lowerCAmelCase (     self :		Any							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							List[str]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_lm_head(*__a							)



					def 					lowerCAmelCase (     self :		Dict							)	->     str:


												"""simple docstring"""

												__lowercase					:							Union[str, Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_qa(*__a							)



					def 					lowerCAmelCase (     self :		int							)	->     str:


												"""simple docstring"""

												__lowercase					:							Tuple			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_sequence_classif(*__a							)



					def 					lowerCAmelCase (     self :		Dict							)	->     Any:


												"""simple docstring"""

												__lowercase					:							List[str]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_for_token_classification(*__a							)



					def 					lowerCAmelCase (     self :		Tuple							)	->     Optional[int]:


												"""simple docstring"""

												__lowercase					:							str			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_for_multiple_choice(*__a							)



					@slow
					def 					lowerCAmelCase (     self :		Tuple							)	->     Dict:


												"""simple docstring"""

												for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowercase					:							Optional[int]			=   TFFlaubertModel.from_pretrained(__a							)
																			self.assertIsNotNone(__a							)







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

					'''simple docstring'''

					@slow
					def 					lowerCAmelCase (     self :		Any							)	->     Any:


												"""simple docstring"""

												__lowercase					:							int			=   TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased"""							)

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

												__lowercase					:							Union[str, Any]			=   model(__a							)[0]
												__lowercase					:							List[Any]			=   tf.TensorShape((1, 8, 512)							)
												self.assertEqual(output.shape						,   __a							)
												# compare the actual values for a slice.
												__lowercase					:							Optional[int]			=   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							)							)







from itertools import permutations
def 					snake_case_						(   lowerCAmelCase_				:  tuple					):
							if num[3] % 2 != 0:
														return False

							if (num[2] + num[3] + num[4]) % 3 != 0:
														return False

							if num[5] % 5 != 0:
														return False

							__lowercase					:							Dict			=   [7, 11, 13, 17]
							for i, test in enumerate(lowerCAmelCase_					):
														if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0:
																					return False
							return True
def 					snake_case_						(   lowerCAmelCase_				:  int = 10					):
							return sum(
							    int("""""".join(map(lowerCAmelCase_				,				lowerCAmelCase_					)					)					)
							    for num in permutations(range(lowerCAmelCase_					)					)
							    if is_substring_divisible(lowerCAmelCase_					)					)


if __name__ == "__main__":
	print(f'''{solution() = }''')