






"""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  lowerCamelCase__ :






    def __init__(			self     ,A     ,):
          UpperCAmelCase          =	parent
          UpperCAmelCase          =	13
          UpperCAmelCase          =	7
          UpperCAmelCase          =	True
          UpperCAmelCase          =	True
          UpperCAmelCase          =	True
          UpperCAmelCase          =	True
          UpperCAmelCase          =	True
          UpperCAmelCase          =	False
          UpperCAmelCase          =	False
          UpperCAmelCase          =	False
          UpperCAmelCase          =	2
          UpperCAmelCase          =	99
          UpperCAmelCase          =	0
          UpperCAmelCase          =	32
          UpperCAmelCase          =	2
          UpperCAmelCase          =	4
          UpperCAmelCase          =	0.1
          UpperCAmelCase          =	0.1
          UpperCAmelCase          =	512
          UpperCAmelCase          =	16
          UpperCAmelCase          =	2
          UpperCAmelCase          =	0.02
          UpperCAmelCase          =	3
          UpperCAmelCase          =	4
          UpperCAmelCase          =	"""last"""
          UpperCAmelCase          =	True
          UpperCAmelCase          =	None
          UpperCAmelCase          =	0






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	ids_tensor([self.batch_size, self.seq_length]     ,self.vocab_size )
          UpperCAmelCase          =	random_attention_mask([self.batch_size, self.seq_length]     ,dtype=tf.floataa )

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

          UpperCAmelCase          =	None
          if self.use_token_type_ids:
                UpperCAmelCase          =	ids_tensor([self.batch_size, self.seq_length]     ,self.n_langs )

          UpperCAmelCase          =	None
          UpperCAmelCase          =	None
          UpperCAmelCase          =	None
          if self.use_labels:
                UpperCAmelCase          =	ids_tensor([self.batch_size]     ,self.type_sequence_label_size )
                UpperCAmelCase          =	ids_tensor([self.batch_size, self.seq_length]     ,self.num_labels )
                UpperCAmelCase          =	ids_tensor([self.batch_size]     ,2     ,dtype=tf.floataa )
                UpperCAmelCase          =	ids_tensor([self.batch_size]     ,self.num_choices )

          UpperCAmelCase          =	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     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,):
          UpperCAmelCase          =	TFFlaubertModel(config=_snake_case )
          UpperCAmelCase          =	{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
          UpperCAmelCase          =	model(_snake_case )

          UpperCAmelCase          =	[input_ids, input_mask]
          UpperCAmelCase          =	model(_snake_case )
          self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size) )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,):
          UpperCAmelCase          =	TFFlaubertWithLMHeadModel(_snake_case )

          UpperCAmelCase          =	{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
          UpperCAmelCase          =	model(_snake_case )

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






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,):
          UpperCAmelCase          =	TFFlaubertForQuestionAnsweringSimple(_snake_case )

          UpperCAmelCase          =	{"""input_ids""": input_ids, """lengths""": input_lengths}

          UpperCAmelCase          =	model(_snake_case )

          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     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,):
          UpperCAmelCase          =	TFFlaubertForSequenceClassification(_snake_case )

          UpperCAmelCase          =	{"""input_ids""": input_ids, """lengths""": input_lengths}

          UpperCAmelCase          =	model(_snake_case )

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






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,):
          UpperCAmelCase          =	self.num_labels
          UpperCAmelCase          =	TFFlaubertForTokenClassification(config=_snake_case )
          UpperCAmelCase          =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          UpperCAmelCase          =	model(_snake_case )
          self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.num_labels) )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,A     ,):
          UpperCAmelCase          =	self.num_choices
          UpperCAmelCase          =	TFFlaubertForMultipleChoice(config=_snake_case )
          UpperCAmelCase          =	tf.tile(tf.expand_dims(_snake_case     ,1 )     ,(1, self.num_choices, 1) )
          UpperCAmelCase          =	tf.tile(tf.expand_dims(_snake_case     ,1 )     ,(1, self.num_choices, 1) )
          UpperCAmelCase          =	tf.tile(tf.expand_dims(_snake_case     ,1 )     ,(1, self.num_choices, 1) )
          UpperCAmelCase          =	{
              """input_ids""": multiple_choice_inputs_ids,
              """attention_mask""": multiple_choice_input_mask,
              """token_type_ids""": multiple_choice_token_type_ids,
          }
          UpperCAmelCase          =	model(_snake_case )
          self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_choices) )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.prepare_config_and_inputs()
          (
              (
              UpperCAmelCase	
          )      , (
              UpperCAmelCase	
          )      , (
              UpperCAmelCase	
          )      , (
              UpperCAmelCase	
          )      , (
              UpperCAmelCase	
          )      , (
              UpperCAmelCase	
          )      , (
              UpperCAmelCase	
          )      , (
              UpperCAmelCase	
          )      , (
              UpperCAmelCase	
          )      , 	
          )          =	config_and_inputs
          UpperCAmelCase          =	{
              """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__ (	snake_case   ,					snake_case   ,					unittest.TestCase			):
    SCREAMING_SNAKE_CASE		=      (
        (
            TFFlaubertModel,
            TFFlaubertWithLMHeadModel,
            TFFlaubertForSequenceClassification,
            TFFlaubertForQuestionAnsweringSimple,
            TFFlaubertForTokenClassification,
            TFFlaubertForMultipleChoice,
        )
        if is_tf_available()
        else ()
    )
    SCREAMING_SNAKE_CASE		=      (
        (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
    )  # TODO (PVP): Check other models whether language generation is also applicable
    SCREAMING_SNAKE_CASE		=      (
        {
            '''feature-extraction''': TFFlaubertModel,
            '''fill-mask''': TFFlaubertWithLMHeadModel,
            '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
            '''text-classification''': TFFlaubertForSequenceClassification,
            '''token-classification''': TFFlaubertForTokenClassification,
            '''zero-shot''': TFFlaubertForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    SCREAMING_SNAKE_CASE		=      False
    SCREAMING_SNAKE_CASE		=      False






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A ):


          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     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	TFFlaubertModelTester(self )
          UpperCAmelCase          =	ConfigTester(self     ,config_class=_snake_case     ,emb_dim=37 )






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






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_model(*_snake_case )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_lm_head(*_snake_case )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_qa(*_snake_case )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_sequence_classif(*_snake_case )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_for_token_classification(*_snake_case )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_for_multiple_choice(*_snake_case )






    @slow
    def     _UpperCamelCase							(			self ):


          for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase          =	TFFlaubertModel.from_pretrained(_snake_case )
                self.assertIsNotNone(_snake_case )







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






    @slow
    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" )

          UpperCAmelCase          =	tf.convert_to_tensor(
              [[0, 158, 735, 2_592, 1_424, 6_727, 82, 1]]     ,dtype=tf.intaa     ,)  # "J'aime flaubert !"

          UpperCAmelCase          =	model(_snake_case )[0]
          UpperCAmelCase          =	tf.TensorShape((1, 8, 512) )
          self.assertEqual(output.shape     ,_snake_case )
          # compare the actual values for a slice.
          UpperCAmelCase          =	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"""







_UpperCamelCase					      =  [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
_UpperCamelCase					      =  [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
_UpperCamelCase					      =  {
    0: """Sunday""",
    1: """Monday""",
    2: """Tuesday""",
    3: """Wednesday""",
    4: """Thursday""",
    5: """Friday""",
    6: """Saturday""",
}

def _a			(		_snake_case				,	_snake_case				,	_snake_case							):







      """simple docstring"""






      assert len(str(_snake_case							)							) > 2, "year should be in YYYY format"
      assert 1 <= month <= 12, "month should be between 1 to 12"
      assert 1 <= day <= 31, "day should be between 1 to 31"

      # Doomsday algorithm:
      UpperCAmelCase          =	year // 100
      UpperCAmelCase          =	(5 * (century % 4) + 2) % 7
      UpperCAmelCase          =	year % 100
      UpperCAmelCase          =	centurian % 12
      UpperCAmelCase          =	(
          (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor
      ) % 7
      UpperCAmelCase          =	(
          DOOMSDAY_NOT_LEAP[month - 1]
          if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0)
          else DOOMSDAY_LEAP[month - 1]
      )
      UpperCAmelCase          =	(dooms_day + day - day_anchor) % 7
      return WEEK_DAY_NAMES[week_day]


if __name__ == "__main__":
  import doctest

  doctest.testmod()



