'''simple docstring'''


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				:							str     ,		__a				:							Union[str, Any]     ,		__a				:							List[str]=1_3     ,		__a				:							List[Any]=7     ,		__a				:							Optional[Any]=True     ,		__a				:							Tuple=True     ,		__a				:							str=True     ,		__a				:							Union[str, Any]=True     ,		__a				:							int=9_9     ,		__a				:							Optional[int]=3_2     ,		__a				:							List[Any]=2     ,		__a				:							int=4     ,		__a				:							Tuple=3_7     ,		__a				:							Optional[int]="gelu"     ,		__a				:							List[str]=0.1     ,		__a				:							int=0.1     ,		__a				:							str=5_1_2     ,		__a				:							Union[str, Any]=1_6     ,		__a				:							Tuple=2     ,		__a				:							int=0.0_2     ,		__a				:							Union[str, Any]=3     ,		__a				:							Any=4     ,		__a				:							Any=None     ,		__a				:							Union[str, Any]=0     ,		)							->	Union[str, Any]:
								__UpperCAmelCase							=			parent
								__UpperCAmelCase							=			batch_size
								__UpperCAmelCase							=			seq_length
								__UpperCAmelCase							=			is_training
								__UpperCAmelCase							=			use_input_mask
								__UpperCAmelCase							=			use_token_type_ids
								__UpperCAmelCase							=			use_labels
								__UpperCAmelCase							=			vocab_size
								__UpperCAmelCase							=			hidden_size
								__UpperCAmelCase							=			num_hidden_layers
								__UpperCAmelCase							=			num_attention_heads
								__UpperCAmelCase							=			intermediate_size
								__UpperCAmelCase							=			hidden_act
								__UpperCAmelCase							=			hidden_dropout_prob
								__UpperCAmelCase							=			attention_probs_dropout_prob
								__UpperCAmelCase							=			max_position_embeddings
								__UpperCAmelCase							=			type_vocab_size
								__UpperCAmelCase							=			type_sequence_label_size
								__UpperCAmelCase							=			initializer_range
								__UpperCAmelCase							=			num_labels
								__UpperCAmelCase							=			num_choices
								__UpperCAmelCase							=			scope
								__UpperCAmelCase							=			projection_dim





							def       snake_case__						(      self				:							Optional[int]	)							->	Optional[int]:
								__UpperCAmelCase							=			ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size	)

								__UpperCAmelCase							=			None
								if self.use_input_mask:
									# follow test_modeling_tf_ctrl.py
									__UpperCAmelCase							=			random_attention_mask([self.batch_size, self.seq_length]	)

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

								__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]     ,		self.num_choices	)

								__UpperCAmelCase							=			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							=			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       snake_case__						(      self				:							List[Any]     ,		__a				:							List[Any]     ,		__a				:							str     ,		__a				:							Dict     ,		__a				:							Union[str, Any]     ,		__a				:							List[Any]     ,		__a				:							int     ,		__a				:							Dict	)							->	str:
								__UpperCAmelCase							=			TFDPRContextEncoder(config=__a	)
								__UpperCAmelCase							=			model(__a     ,		attention_mask=__a     ,		token_type_ids=__a	)
								__UpperCAmelCase							=			model(__a     ,		token_type_ids=__a	)
								__UpperCAmelCase							=			model(__a	)
								self.parent.assertEqual(result.pooler_output.shape     ,		(self.batch_size, self.projection_dim or self.hidden_size)	)





							def       snake_case__						(      self				:							Dict     ,		__a				:							Union[str, Any]     ,		__a				:							Tuple     ,		__a				:							List[str]     ,		__a				:							List[Any]     ,		__a				:							int     ,		__a				:							List[Any]     ,		__a				:							Union[str, Any]	)							->	Optional[Any]:
								__UpperCAmelCase							=			TFDPRQuestionEncoder(config=__a	)
								__UpperCAmelCase							=			model(__a     ,		attention_mask=__a     ,		token_type_ids=__a	)
								__UpperCAmelCase							=			model(__a     ,		token_type_ids=__a	)
								__UpperCAmelCase							=			model(__a	)
								self.parent.assertEqual(result.pooler_output.shape     ,		(self.batch_size, self.projection_dim or self.hidden_size)	)





							def       snake_case__						(      self				:							Optional[int]     ,		__a				:							List[str]     ,		__a				:							int     ,		__a				:							List[str]     ,		__a				:							Tuple     ,		__a				:							Optional[int]     ,		__a				:							Dict     ,		__a				:							Tuple	)							->	Tuple:
								__UpperCAmelCase							=			TFDPRReader(config=__a	)
								__UpperCAmelCase							=			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       snake_case__						(      self				:							Optional[Any]	)							->	Optional[int]:
								__UpperCAmelCase							=			self.prepare_config_and_inputs()
								(
								    (
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			
								)							=			config_and_inputs
								__UpperCAmelCase							=			{'''input_ids''': input_ids}
								return config, inputs_dict






@require_tf
class 						A   (   UpperCAmelCase       ,	UpperCAmelCase       ,	unittest.TestCase   ):
							a_        =      (
							    (
							        TFDPRContextEncoder,
							        TFDPRQuestionEncoder,
							        TFDPRReader,
							    )
							    if is_tf_available()
							    else ()
							)
							a_        =      {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

							a_        =      False
							a_        =      False
							a_        =      False
							a_        =      False
							a_        =      False





							def       snake_case__						(      self				:							List[Any]	)							->	int:
								__UpperCAmelCase							=			TFDPRModelTester(self	)
								__UpperCAmelCase							=			ConfigTester(self     ,		config_class=__a     ,		hidden_size=3_7	)





							def       snake_case__						(      self				:							List[str]	)							->	List[str]:
								self.config_tester.run_common_tests()





							def       snake_case__						(      self				:							Union[str, Any]	)							->	Tuple:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_context_encoder(*__a	)





							def       snake_case__						(      self				:							Tuple	)							->	List[Any]:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_question_encoder(*__a	)





							def       snake_case__						(      self				:							Optional[Any]	)							->	int:
								__UpperCAmelCase							=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_reader(*__a	)






							@slow
							def       snake_case__						(      self				:							List[Any]	)							->	List[Any]:
								for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase							=			TFDPRContextEncoder.from_pretrained(__a	)
									self.assertIsNotNone(__a	)

								for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase							=			TFDPRContextEncoder.from_pretrained(__a	)
									self.assertIsNotNone(__a	)

								for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase							=			TFDPRQuestionEncoder.from_pretrained(__a	)
									self.assertIsNotNone(__a	)

								for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase							=			TFDPRReader.from_pretrained(__a	)
									self.assertIsNotNone(__a	)






@require_tf
class 						A   (   unittest.TestCase   ):





							@slow
							def       snake_case__						(      self				:							int	)							->	Dict:
								__UpperCAmelCase							=			TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''	)

								__UpperCAmelCase							=			tf.constant(
								    [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]	)  # [CLS] hello, is my dog cute? [SEP]
								__UpperCAmelCase							=			model(__a	)[0]  # embedding shape = (1, 768)
								# compare the actual values for a slice.
								__UpperCAmelCase							=			tf.constant(
								    [
								        [
								            0.0_3_2_3_6_2_5_3,
								            0.1_2_7_5_3_3_3_5,
								            0.1_6_8_1_8_5_0_9,
								            0.0_0_2_7_9_7_8_6,
								            0.3_8_9_6_9_3_3,
								            0.2_4_2_6_4_9_4_5,
								            0.2_1_7_8_9_7_1,
								            -0.0_2_3_3_5_2_2_7,
								            -0.0_8_4_8_1_9_5_9,
								            -0.1_4_3_2_4_1_1_7,
								        ]
								    ]	)
								self.assertTrue(numpy.allclose(output[:, :1_0].numpy()     ,		expected_slice.numpy()     ,		atol=1e-4	)	)

'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available


__lowerCAmelCase	:       List[Any]						       =	{
    "configuration_audio_spectrogram_transformer": [
        "AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "ASTConfig",
    ]
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowerCAmelCase	:       str						       =	[
					    "AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "ASTForAudioClassification",
					    "ASTModel",
					    "ASTPreTrainedModel",
					]

try:
					if not is_speech_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowerCAmelCase	:       Union[str, Any]						       =	["ASTFeatureExtractor"]

if TYPE_CHECKING:
					from .configuration_audio_spectrogram_transformer import (
					    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    ASTConfig,
					)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_audio_spectrogram_transformer import (
										    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    ASTForAudioClassification,
										    ASTModel,
										    ASTPreTrainedModel,
										)

					try:
										if not is_speech_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor


else:
					import sys

					__lowerCAmelCase	:       Optional[Any]						       =	_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
