





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					,			snake_case					,			)				->  Optional[Any]:
      """simple docstring"""





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





   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      a__	:						Optional[int]      		=  ids_tensor([self.batch_size, self.seq_length]					,			self.vocab_size      )
      a__	:						int      		=  random_attention_mask([self.batch_size, self.seq_length]					,			dtype=tf.floataa      )

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

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

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

      a__	:						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					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			)				->  int:
      """simple docstring"""





      a__	:						Optional[Any]      		=  TFFlaubertModel(config=snake_case      )
      a__	:						List[str]      		=  {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
      a__	:						Dict      		=  model(snake_case      )

      a__	:						List[str]      		=  [input_ids, input_mask]
      a__	:						str      		=  model(snake_case      )
      self.parent.assertEqual(result.last_hidden_state.shape					,			(self.batch_size, self.seq_length, self.hidden_size)      )





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			)				->  Optional[Any]:
      """simple docstring"""





      a__	:						int      		=  TFFlaubertWithLMHeadModel(snake_case      )

      a__	:						Optional[int]      		=  {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
      a__	:						Optional[Any]      		=  model(snake_case      )

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





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			)				->  List[str]:
      """simple docstring"""





      a__	:						str      		=  TFFlaubertForQuestionAnsweringSimple(snake_case      )

      a__	:						str      		=  {"input_ids": input_ids, "lengths": input_lengths}

      a__	:						Any      		=  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 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			)				->  str:
      """simple docstring"""





      a__	:						Tuple      		=  TFFlaubertForSequenceClassification(snake_case      )

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

      a__	:						List[Any]      		=  model(snake_case      )

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





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			)				->  str:
      """simple docstring"""





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





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			)				->  Any:
      """simple docstring"""





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





   def 	_snake_case ( self      )				->  Tuple:
      """simple docstring"""





      a__	:						Optional[int]      		=  self.prepare_config_and_inputs()
      (
          a__     
      )	:						Optional[int]      		=  config_and_inputs
      a__	:						int      		=  {
          "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			(					_UpperCamelCase		,_UpperCamelCase		,unittest.TestCase  ):
   _UpperCamelCase :						Tuple     		=				(
       (
           TFFlaubertModel,
           TFFlaubertWithLMHeadModel,
           TFFlaubertForSequenceClassification,
           TFFlaubertForQuestionAnsweringSimple,
           TFFlaubertForTokenClassification,
           TFFlaubertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   _UpperCamelCase :						Union[str, Any]     		=				(
       (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
   )  # TODO (PVP): Check other models whether language generation is also applicable
   _UpperCamelCase :						Dict     		=				(
       {
           """feature-extraction""": TFFlaubertModel,
           """fill-mask""": TFFlaubertWithLMHeadModel,
           """question-answering""": TFFlaubertForQuestionAnsweringSimple,
           """text-classification""": TFFlaubertForSequenceClassification,
           """token-classification""": TFFlaubertForTokenClassification,
           """zero-shot""": TFFlaubertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   _UpperCamelCase :						Union[str, Any]     		=				False
   _UpperCamelCase :						str     		=				False





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  List[Any]:
      """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 	_snake_case ( self      )				->  List[Any]:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  TFFlaubertModelTester(self      )
      a__	:						Tuple      		=  ConfigTester(self					,			config_class=snake_case					,			emb_dim=37      )





   def 	_snake_case ( self      )				->  Union[str, Any]:
      """simple docstring"""





      self.config_tester.run_common_tests()





   def 	_snake_case ( self      )				->  Any:
      """simple docstring"""





      a__	:						Optional[Any]      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_model(*snake_case      )





   def 	_snake_case ( self      )				->  List[str]:
      """simple docstring"""





      a__	:						Dict      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_lm_head(*snake_case      )





   def 	_snake_case ( self      )				->  str:
      """simple docstring"""





      a__	:						List[Any]      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_qa(*snake_case      )





   def 	_snake_case ( self      )				->  Optional[int]:
      """simple docstring"""





      a__	:						int      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case      )





   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      a__	:						Any      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_for_token_classification(*snake_case      )





   def 	_snake_case ( self      )				->  Optional[Any]:
      """simple docstring"""





      a__	:						Any      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_for_multiple_choice(*snake_case      )





   @slow
   def 	_snake_case ( self      )				->  Any:
      """simple docstring"""





      for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a__	:						List[Any]      		=  TFFlaubertModel.from_pretrained(snake_case      )
         self.assertIsNotNone(snake_case      )







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





   @slow
   def 	_snake_case ( self      )				->  int:
      """simple docstring"""





      a__	:						Dict      		=  TFFlaubertModel.from_pretrained("jplu/tf-flaubert-small-cased"      )

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

      a__	:						Any      		=  model(snake_case      )[0]
      a__	:						Union[str, Any]      		=  tf.TensorShape((1, 8, 512)      )
      self.assertEqual(output.shape					,			snake_case      )
      # compare the actual values for a slice.
      a__	:						int      		=  tf.convert_to_tensor(
          [
              [
                  [-1.8_768_773, -1.566_555, 0.27_072_418],
                  [-1.6_920_038, -0.5_873_505, 1.9_329_599],
                  [-2.9_563_985, -1.6_993_835, 1.7_972_052],
              ]
          ]					,			dtype=tf.floataa					,			)

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








from typing import List, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE__       :			Any			      =						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__       :			str			      =						{
    """huggingface/informer-tourism-monthly""": (
        """https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json"""
    ),
    # See all Informer models at https://huggingface.co/models?filter=informer
}







class   __lowerCAmelCase			(					_UpperCamelCase  ):
   _UpperCamelCase :						Optional[Any]     		=				"""informer"""
   _UpperCamelCase :						Any     		=				{
       """hidden_size""": """d_model""",
       """num_attention_heads""": """encoder_attention_heads""",
       """num_hidden_layers""": """encoder_layers""",
   }





   def __init__( self					,			snake_case = None					,			snake_case = None					,			snake_case = "student_t"					,			snake_case = "nll"					,			snake_case = 1					,			snake_case = None					,			snake_case = "mean"					,			snake_case = 0					,			snake_case = 0					,			snake_case = 0					,			snake_case = 0					,			snake_case = None					,			snake_case = None					,			snake_case = 64					,			snake_case = 32					,			snake_case = 32					,			snake_case = 2					,			snake_case = 2					,			snake_case = 2					,			snake_case = 2					,			snake_case = True					,			snake_case = "gelu"					,			snake_case = 0.05					,			snake_case = 0.1					,			snake_case = 0.1					,			snake_case = 0.1					,			snake_case = 0.1					,			snake_case = 100					,			snake_case = 0.02					,			snake_case=True					,			snake_case = "prob"					,			snake_case = 5					,			snake_case = True					,			**snake_case					,			)				->  Union[str, Any]:
      """simple docstring"""





      a__	:						Optional[Any]      		=  prediction_length
      a__	:						Optional[int]      		=  context_length or prediction_length
      a__	:						Optional[int]      		=  distribution_output
      a__	:						str      		=  loss
      a__	:						Optional[Any]      		=  input_size
      a__	:						int      		=  num_time_features
      a__	:						Optional[int]      		=  lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
      a__	:						Optional[int]      		=  scaling
      a__	:						List[str]      		=  num_dynamic_real_features
      a__	:						Optional[int]      		=  num_static_real_features
      a__	:						Optional[int]      		=  num_static_categorical_features

      # set cardinality
      if cardinality and num_static_categorical_features > 0:
         if len(snake_case      ) != num_static_categorical_features:
            raise ValueError(
                "The cardinality should be a list of the same length as `num_static_categorical_features`"      )
         a__	:						List[Any]      		=  cardinality
      else:
         a__	:						Tuple      		=  [0]

      # set embedding_dimension
      if embedding_dimension and num_static_categorical_features > 0:
         if len(snake_case      ) != num_static_categorical_features:
            raise ValueError(
                "The embedding dimension should be a list of the same length as `num_static_categorical_features`"      )
         a__	:						Tuple      		=  embedding_dimension
      else:
         a__	:						int      		=  [min(50					,			(cat + 1) // 2      ) for cat in self.cardinality]

      a__	:						Optional[Any]      		=  num_parallel_samples

      # Transformer architecture configuration
      a__	:						int      		=  input_size * len(self.lags_sequence      ) + self._number_of_features
      a__	:						Union[str, Any]      		=  d_model
      a__	:						Any      		=  encoder_attention_heads
      a__	:						Optional[Any]      		=  decoder_attention_heads
      a__	:						int      		=  encoder_ffn_dim
      a__	:						List[Any]      		=  decoder_ffn_dim
      a__	:						List[str]      		=  encoder_layers
      a__	:						Any      		=  decoder_layers

      a__	:						List[str]      		=  dropout
      a__	:						int      		=  attention_dropout
      a__	:						List[Any]      		=  activation_dropout
      a__	:						Optional[int]      		=  encoder_layerdrop
      a__	:						Tuple      		=  decoder_layerdrop

      a__	:						Any      		=  activation_function
      a__	:						Tuple      		=  init_std

      a__	:						Optional[int]      		=  use_cache

      # Informer
      a__	:						Union[str, Any]      		=  attention_type
      a__	:						List[str]      		=  sampling_factor
      a__	:						Optional[int]      		=  distil

      super().__init__(is_encoder_decoder=snake_case					,			**snake_case      )





   @property
   def 	_snake_case ( self      )				->  int:
      """simple docstring"""





      return (
          sum(self.embedding_dimension      )
          + self.num_dynamic_real_features
          + self.num_time_features
          + self.num_static_real_features
          + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
      )

