

from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, 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
					import tensorflow as tf

					from transformers import (
					    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
					    BertConfig,
					    DPRConfig,
					    TFDPRContextEncoder,
					    TFDPRQuestionEncoder,
					    TFDPRReader,
					)

class 			a					:


						def __init__(    self  ,   A_  ,   A_=13  ,   A_=7  ,   A_=True  ,   A_=True  ,   A_=True  ,   A_=True  ,   A_=99  ,   A_=32  ,   A_=2  ,   A_=4  ,   A_=37  ,   A_="gelu"  ,   A_=0.1  ,   A_=0.1  ,   A_=512  ,   A_=16  ,   A_=2  ,   A_=0.02  ,   A_=3  ,   A_=4  ,   A_=None  ,   A_=0  ,   ):







										'''simple docstring'''


										_UpperCAmelCase    :						Tuple							=      parent
										_UpperCAmelCase    :						int							=      batch_size
										_UpperCAmelCase    :						Tuple							=      seq_length
										_UpperCAmelCase    :						Tuple							=      is_training
										_UpperCAmelCase    :						Dict							=      use_input_mask
										_UpperCAmelCase    :						List[Any]							=      use_token_type_ids
										_UpperCAmelCase    :						Dict							=      use_labels
										_UpperCAmelCase    :						str							=      vocab_size
										_UpperCAmelCase    :						Optional[int]							=      hidden_size
										_UpperCAmelCase    :						Tuple							=      num_hidden_layers
										_UpperCAmelCase    :						str							=      num_attention_heads
										_UpperCAmelCase    :						Dict							=      intermediate_size
										_UpperCAmelCase    :						int							=      hidden_act
										_UpperCAmelCase    :						Tuple							=      hidden_dropout_prob
										_UpperCAmelCase    :						Union[str, Any]							=      attention_probs_dropout_prob
										_UpperCAmelCase    :						Optional[Any]							=      max_position_embeddings
										_UpperCAmelCase    :						Union[str, Any]							=      type_vocab_size
										_UpperCAmelCase    :						Optional[int]							=      type_sequence_label_size
										_UpperCAmelCase    :						Tuple							=      initializer_range
										_UpperCAmelCase    :						List[str]							=      num_labels
										_UpperCAmelCase    :						Optional[int]							=      num_choices
										_UpperCAmelCase    :						str							=      scope
										_UpperCAmelCase    :						Optional[int]							=      projection_dim


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						str							=      ids_tensor([self.batch_size, self.seq_length]  ,   self.vocab_size		)

										_UpperCAmelCase    :						Optional[int]							=      None
										if self.use_input_mask:
														# follow test_modeling_tf_ctrl.py
														_UpperCAmelCase    :						List[str]							=      random_attention_mask([self.batch_size, self.seq_length]		)

										_UpperCAmelCase    :						str							=      None
										if self.use_token_type_ids:
														_UpperCAmelCase    :						Tuple							=      ids_tensor([self.batch_size, self.seq_length]  ,   self.type_vocab_size		)

										_UpperCAmelCase    :						Tuple							=      None
										_UpperCAmelCase    :						List[str]							=      None
										_UpperCAmelCase    :						Dict							=      None
										if self.use_labels:
														_UpperCAmelCase    :						Optional[Any]							=      ids_tensor([self.batch_size]  ,   self.type_sequence_label_size		)
														_UpperCAmelCase    :						List[str]							=      ids_tensor([self.batch_size, self.seq_length]  ,   self.num_labels		)
														_UpperCAmelCase    :						Optional[int]							=      ids_tensor([self.batch_size]  ,   self.num_choices		)

										_UpperCAmelCase    :						Optional[int]							=      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=A_  ,   initializer_range=self.initializer_range  ,   )
										_UpperCAmelCase    :						Optional[Any]							=      DPRConfig(projection_dim=self.projection_dim  ,   **config.to_dict()		)

										return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      TFDPRContextEncoder(config=A_		)
										_UpperCAmelCase    :						Union[str, Any]							=      model(A_  ,   attention_mask=A_  ,   token_type_ids=A_		)
										_UpperCAmelCase    :						Dict							=      model(A_  ,   token_type_ids=A_		)
										_UpperCAmelCase    :						Any							=      model(A_		)
										self.parent.assertEqual(result.pooler_output.shape  ,   (self.batch_size, self.projection_dim or self.hidden_size)		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Dict							=      TFDPRQuestionEncoder(config=A_		)
										_UpperCAmelCase    :						Tuple							=      model(A_  ,   attention_mask=A_  ,   token_type_ids=A_		)
										_UpperCAmelCase    :						Dict							=      model(A_  ,   token_type_ids=A_		)
										_UpperCAmelCase    :						Dict							=      model(A_		)
										self.parent.assertEqual(result.pooler_output.shape  ,   (self.batch_size, self.projection_dim or self.hidden_size)		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      TFDPRReader(config=A_		)
										_UpperCAmelCase    :						Union[str, Any]							=      model(A_  ,   attention_mask=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)		)
										self.parent.assertEqual(result.relevance_logits.shape  ,   (self.batch_size,)		)





						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      self.prepare_config_and_inputs()
										(
										    (
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							
										)    :						int							=      config_and_inputs
										_UpperCAmelCase    :						int							=      {"input_ids": input_ids}
										return config, inputs_dict







@require_tf
class 			a					(		UpperCAmelCase							,	UpperCAmelCase							,	unittest.TestCase			):
						_lowercase        =     (
						    (
						        TFDPRContextEncoder,
						        TFDPRQuestionEncoder,
						        TFDPRReader,
						    )
						    if is_tf_available()
						    else ()
						)
						_lowercase        =     {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

						_lowercase        =     False
						_lowercase        =     False
						_lowercase        =     False
						_lowercase        =     False
						_lowercase        =     False


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[Any]							=      TFDPRModelTester(self		)
										_UpperCAmelCase    :						List[Any]							=      ConfigTester(self  ,   config_class=A_  ,   hidden_size=37		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										self.config_tester.run_common_tests()


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						str							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_context_encoder(*A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Tuple							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_question_encoder(*A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Union[str, Any]							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_reader(*A_		)





						@slow
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_UpperCAmelCase    :						int							=      TFDPRContextEncoder.from_pretrained(A_		)
														self.assertIsNotNone(A_		)

										for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_UpperCAmelCase    :						List[str]							=      TFDPRContextEncoder.from_pretrained(A_		)
														self.assertIsNotNone(A_		)

										for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_UpperCAmelCase    :						Union[str, Any]							=      TFDPRQuestionEncoder.from_pretrained(A_		)
														self.assertIsNotNone(A_		)

										for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_UpperCAmelCase    :						Optional[int]							=      TFDPRReader.from_pretrained(A_		)
														self.assertIsNotNone(A_		)







@require_tf
class 			a					(		unittest.TestCase			):


						@slow
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[int]							=      TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"		)

										_UpperCAmelCase    :						Tuple							=      tf.constant(
										    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]		)  # [CLS] hello, is my dog cute? [SEP]
										_UpperCAmelCase    :						Optional[int]							=      model(A_		)[0]  # embedding shape = (1, 768)
										# compare the actual values for a slice.
										_UpperCAmelCase    :						List[str]							=      tf.constant(
										    [
										        [
										            0.03_23_62_53,
										            0.12_75_33_35,
										            0.16_81_85_09,
										            0.00_27_97_86,
										            0.3_89_69_33,
										            0.24_26_49_45,
										            0.2_17_89_71,
										            -0.02_33_52_27,
										            -0.08_48_19_59,
										            -0.14_32_41_17,
										        ]
										    ]		)
										self.assertTrue(numpy.allclose(output[:, :10].numpy()  ,   expected_slice.numpy()  ,   atol=1e-4		)		)





from typing import List, Optional

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


SCREAMING_SNAKE_CASE_            = logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_            = {
    'huggingface/autoformer-tourism-monthly': 'https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json',
}

class 			a					(		UpperCAmelCase			):
						_lowercase        =     "autoformer"
						_lowercase        =     {
						    "hidden_size": "d_model",
						    "num_attention_heads": "encoder_attention_heads",
						    "num_hidden_layers": "encoder_layers",
						}


						def __init__(    self  ,   A_ = None  ,   A_ = None  ,   A_ = "student_t"  ,   A_ = "nll"  ,   A_ = 1  ,   A_ = [1, 2, 3, 4, 5, 6, 7]  ,   A_ = True  ,   A_ = 0  ,   A_ = 0  ,   A_ = 0  ,   A_ = 0  ,   A_ = None  ,   A_ = None  ,   A_ = 64  ,   A_ = 2  ,   A_ = 2  ,   A_ = 2  ,   A_ = 2  ,   A_ = 32  ,   A_ = 32  ,   A_ = "gelu"  ,   A_ = 0.1  ,   A_ = 0.1  ,   A_ = 0.1  ,   A_ = 0.1  ,   A_ = 0.1  ,   A_ = 100  ,   A_ = 0.02  ,   A_ = True  ,   A_=True  ,   A_ = 10  ,   A_ = 25  ,   A_ = 3  ,   **A_  ,   ):







										'''simple docstring'''


										_UpperCAmelCase    :						List[Any]							=      prediction_length
										_UpperCAmelCase    :						Dict							=      context_length if context_length is not None else prediction_length
										_UpperCAmelCase    :						Tuple							=      distribution_output
										_UpperCAmelCase    :						List[Any]							=      loss
										_UpperCAmelCase    :						Optional[Any]							=      input_size
										_UpperCAmelCase    :						int							=      num_time_features
										_UpperCAmelCase    :						str							=      lags_sequence
										_UpperCAmelCase    :						Union[str, Any]							=      scaling
										_UpperCAmelCase    :						Union[str, Any]							=      num_dynamic_real_features
										_UpperCAmelCase    :						int							=      num_static_real_features
										_UpperCAmelCase    :						int							=      num_static_categorical_features
										if cardinality is not None and num_static_categorical_features > 0:
														if len(A_		) != num_static_categorical_features:
																		raise ValueError(
																		    "The cardinality should be a list of the same length as `num_static_categorical_features`"		)
														_UpperCAmelCase    :						Union[str, Any]							=      cardinality
										else:
														_UpperCAmelCase    :						Any							=      [0]
										if embedding_dimension is not None and num_static_categorical_features > 0:
														if len(A_		) != num_static_categorical_features:
																		raise ValueError(
																		    "The embedding dimension should be a list of the same length as `num_static_categorical_features`"		)
														_UpperCAmelCase    :						int							=      embedding_dimension
										else:
														_UpperCAmelCase    :						int							=      [min(50  ,   (cat + 1) // 2		) for cat in self.cardinality]
										_UpperCAmelCase    :						Union[str, Any]							=      num_parallel_samples

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

										_UpperCAmelCase    :						Optional[Any]							=      dropout
										_UpperCAmelCase    :						Union[str, Any]							=      attention_dropout
										_UpperCAmelCase    :						Dict							=      activation_dropout
										_UpperCAmelCase    :						Dict							=      encoder_layerdrop
										_UpperCAmelCase    :						int							=      decoder_layerdrop

										_UpperCAmelCase    :						Union[str, Any]							=      activation_function
										_UpperCAmelCase    :						str							=      init_std

										_UpperCAmelCase    :						Tuple							=      use_cache

										# Autoformer
										_UpperCAmelCase    :						str							=      label_length
										_UpperCAmelCase    :						Any							=      moving_average
										_UpperCAmelCase    :						Optional[Any]							=      autocorrelation_factor

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





						@property
						def     _UpperCAmelCase			(    self		):







										'''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
										)


