
"""simple docstring"""


import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
						from transformers.models.dpr.modeling_dpr import (
						    DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
						    DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
						    DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
						)




class    __lowerCamelCase     :




				def __init__(							self       ,	UpperCAmelCase       ,	UpperCAmelCase=13       ,	UpperCAmelCase=7       ,	UpperCAmelCase=False       ,	UpperCAmelCase=True       ,	UpperCAmelCase=True       ,	UpperCAmelCase=True       ,	UpperCAmelCase=99       ,	UpperCAmelCase=32       ,	UpperCAmelCase=5       ,	UpperCAmelCase=4       ,	UpperCAmelCase=37       ,	UpperCAmelCase="gelu"       ,	UpperCAmelCase=0.1       ,	UpperCAmelCase=0.1       ,	UpperCAmelCase=512       ,	UpperCAmelCase=16       ,	UpperCAmelCase=2       ,	UpperCAmelCase=0.0_2       ,	UpperCAmelCase=3       ,	UpperCAmelCase=4       ,	UpperCAmelCase=None       ,	UpperCAmelCase=0       ,	):
							lowerCamelCase_ 		=     parent
							lowerCamelCase_ 		=     batch_size
							lowerCamelCase_ 		=     seq_length
							lowerCamelCase_ 		=     is_training
							lowerCamelCase_ 		=     use_input_mask
							lowerCamelCase_ 		=     use_token_type_ids
							lowerCamelCase_ 		=     use_labels
							lowerCamelCase_ 		=     vocab_size
							lowerCamelCase_ 		=     hidden_size
							lowerCamelCase_ 		=     num_hidden_layers
							lowerCamelCase_ 		=     num_attention_heads
							lowerCamelCase_ 		=     intermediate_size
							lowerCamelCase_ 		=     hidden_act
							lowerCamelCase_ 		=     hidden_dropout_prob
							lowerCamelCase_ 		=     attention_probs_dropout_prob
							lowerCamelCase_ 		=     max_position_embeddings
							lowerCamelCase_ 		=     type_vocab_size
							lowerCamelCase_ 		=     type_sequence_label_size
							lowerCamelCase_ 		=     initializer_range
							lowerCamelCase_ 		=     num_labels
							lowerCamelCase_ 		=     num_choices
							lowerCamelCase_ 		=     scope
							lowerCamelCase_ 		=     projection_dim




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     ids_tensor([self.batch_size, self.seq_length]       ,	self.vocab_size      )

							lowerCamelCase_ 		=     None
							if self.use_input_mask:
										lowerCamelCase_ 		=     random_attention_mask([self.batch_size, self.seq_length]      )

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

							lowerCamelCase_ 		=     None
							lowerCamelCase_ 		=     None
							lowerCamelCase_ 		=     None
							if self.use_labels:
										lowerCamelCase_ 		=     ids_tensor([self.batch_size]       ,	self.type_sequence_label_size      )
										lowerCamelCase_ 		=     ids_tensor([self.batch_size, self.seq_length]       ,	self.num_labels      )
										lowerCamelCase_ 		=     ids_tensor([self.batch_size]       ,	self.num_choices      )

							lowerCamelCase_ 		=     self.get_config()

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




				def 					UpperCAmelCase__	(							self      ):
							return DPRConfig(
							    projection_dim=self.projection_dim       ,	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       ,	initializer_range=self.initializer_range       ,	)




				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase      ):
							lowerCamelCase_ 		=     DPRContextEncoder(config=UpperCAmelCase      )
							model.to(UpperCAmelCase      )
							model.eval()
							lowerCamelCase_ 		=     model(UpperCAmelCase       ,	attention_mask=UpperCAmelCase       ,	token_type_ids=UpperCAmelCase      )
							lowerCamelCase_ 		=     model(UpperCAmelCase       ,	token_type_ids=UpperCAmelCase      )
							lowerCamelCase_ 		=     model(UpperCAmelCase      )
							self.parent.assertEqual(result.pooler_output.shape       ,	(self.batch_size, self.projection_dim or self.hidden_size)      )




				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase      ):
							lowerCamelCase_ 		=     DPRQuestionEncoder(config=UpperCAmelCase      )
							model.to(UpperCAmelCase      )
							model.eval()
							lowerCamelCase_ 		=     model(UpperCAmelCase       ,	attention_mask=UpperCAmelCase       ,	token_type_ids=UpperCAmelCase      )
							lowerCamelCase_ 		=     model(UpperCAmelCase       ,	token_type_ids=UpperCAmelCase      )
							lowerCamelCase_ 		=     model(UpperCAmelCase      )
							self.parent.assertEqual(result.pooler_output.shape       ,	(self.batch_size, self.projection_dim or self.hidden_size)      )




				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase      ):
							lowerCamelCase_ 		=     DPRReader(config=UpperCAmelCase      )
							model.to(UpperCAmelCase      )
							model.eval()
							lowerCamelCase_ 		=     model(
							    UpperCAmelCase       ,	attention_mask=UpperCAmelCase       ,	)

							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      ):
							lowerCamelCase_ 		=     self.prepare_config_and_inputs()
							(
							    (
							    lowerCamelCase_
							)		,   (
							    lowerCamelCase_
							)		,   (
							    lowerCamelCase_
							)		,   (
							    lowerCamelCase_
							)		,   (
							    lowerCamelCase_
							)		,   (
							    lowerCamelCase_
							)		,   (
							    lowerCamelCase_
							)		,   
							) 		=     config_and_inputs
							lowerCamelCase_ 		=     {'''input_ids''': input_ids}
							return config, inputs_dict




@require_torch
class    __lowerCamelCase     (   lowerCAmelCase	,  lowerCAmelCase	,  unittest.TestCase ):
				a__:			Optional[Any]									=						(
				    (
				        DPRContextEncoder,
				        DPRQuestionEncoder,
				        DPRReader,
				    )
				    if is_torch_available()
				    else ()
				)
				a__:			Union[str, Any]									=						{'feature-extraction': DPRQuestionEncoder} if is_torch_available() else {}

				a__:			Any									=						False
				a__:			Union[str, Any]									=						False  # why?
				a__:			int									=						False
				a__:			Union[str, Any]									=						False




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     DPRModelTester(self      )
							lowerCamelCase_ 		=     ConfigTester(self       ,	config_class=UpperCAmelCase       ,	hidden_size=37      )




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




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_context_encoder(*UpperCAmelCase      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_question_encoder(*UpperCAmelCase      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_reader(*UpperCAmelCase      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.model_tester.prepare_config_and_inputs()[0]

							lowerCamelCase_ 		=     DPRQuestionEncoder(config=UpperCAmelCase      )
							model.to(UpperCAmelCase      )
							model.eval()

							with tempfile.TemporaryDirectory() as tmp_dirname:
										model.save_pretrained(UpperCAmelCase      )
										lowerCamelCase_ 		=     DPRQuestionEncoder.from_pretrained(UpperCAmelCase       ,	projection_dim=512      )

							self.assertIsNotNone(UpperCAmelCase      )





				@slow
				def 					UpperCAmelCase__	(							self      ):
							for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCamelCase_ 		=     DPRContextEncoder.from_pretrained(UpperCAmelCase      )
										self.assertIsNotNone(UpperCAmelCase      )

							for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCamelCase_ 		=     DPRContextEncoder.from_pretrained(UpperCAmelCase      )
										self.assertIsNotNone(UpperCAmelCase      )

							for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCamelCase_ 		=     DPRQuestionEncoder.from_pretrained(UpperCAmelCase      )
										self.assertIsNotNone(UpperCAmelCase      )

							for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCamelCase_ 		=     DPRReader.from_pretrained(UpperCAmelCase      )
										self.assertIsNotNone(UpperCAmelCase      )




@require_torch
class    __lowerCamelCase     (   unittest.TestCase ):




				@slow
				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     DPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''       ,	return_dict=UpperCAmelCase      )
							model.to(UpperCAmelCase      )

							lowerCamelCase_ 		=     torch.tensor(
							    [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]       ,	dtype=torch.long       ,	device=UpperCAmelCase      )  # [CLS] hello, is my dog cute? [SEP]
							lowerCamelCase_ 		=     model(UpperCAmelCase      )[0]  # embedding shape = (1, 768)
							# compare the actual values for a slice.
							lowerCamelCase_ 		=     torch.tensor(
							    [
							        [
							            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,
							        ]
							    ]       ,	dtype=torch.float       ,	device=UpperCAmelCase       ,	)
							self.assertTrue(torch.allclose(output[:, :10]       ,	UpperCAmelCase       ,	atol=1e-4      )      )





				@slow
				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     DPRReaderTokenizer.from_pretrained('''facebook/dpr-reader-single-nq-base'''      )
							lowerCamelCase_ 		=     DPRReader.from_pretrained('''facebook/dpr-reader-single-nq-base'''      )
							model.to(UpperCAmelCase      )

							lowerCamelCase_ 		=     tokenizer(
							    questions='''What is love ?'''       ,	titles='''Haddaway'''       ,	texts='''What Is Love is a song recorded by the artist Haddaway'''       ,	padding=UpperCAmelCase       ,	return_tensors='''pt'''       ,	)
							encoded_inputs.to(UpperCAmelCase      )

							lowerCamelCase_ 		=     model(**UpperCAmelCase      )

							# compare the actual values for a slice.
							lowerCamelCase_ 		=     torch.tensor(
							    [[-1_0.3_0_0_5, -1_0.7_7_6_5, -1_1.4_8_7_2, -1_1.6_8_4_1, -1_1.9_3_1_2, -1_0.3_0_0_2, -9.8_5_4_4, -1_1.7_3_7_8, -1_2.0_8_2_1, -1_0.2_9_7_5]]       ,	dtype=torch.float       ,	device=UpperCAmelCase       ,	)

							lowerCamelCase_ 		=     torch.tensor(
							    [[-1_1.0_6_8_4, -1_1.7_0_4_1, -1_1.5_3_9_7, -1_0.3_4_6_5, -1_0.8_7_9_1, -6.8_4_4_3, -1_1.9_9_5_9, -1_1.0_3_6_4, -1_0.0_0_9_6, -6.8_4_0_5]]       ,	dtype=torch.float       ,	device=UpperCAmelCase       ,	)
							self.assertTrue(torch.allclose(outputs.start_logits[:, :10]       ,	UpperCAmelCase       ,	atol=1e-4      )      )
							self.assertTrue(torch.allclose(outputs.end_logits[:, :10]       ,	UpperCAmelCase       ,	atol=1e-4      )      )





"""simple docstring"""


from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A_						       =				logging.get_logger(__name__)

A_						       =				{
    """MBZUAI/swiftformer-xs""": """https://huggingface.co/MBZUAI/swiftformer-xs/resolve/main/config.json""",
}




class    __lowerCamelCase     (   lowerCAmelCase ):
				a__:			Any									=						'swiftformer'




				def __init__(							self       ,	UpperCAmelCase=3       ,	UpperCAmelCase=[3, 3, 6, 4]       ,	UpperCAmelCase=[48, 56, 112, 220]       ,	UpperCAmelCase=4       ,	UpperCAmelCase=[True, True, True, True]       ,	UpperCAmelCase="gelu"       ,	UpperCAmelCase=3       ,	UpperCAmelCase=2       ,	UpperCAmelCase=1       ,	UpperCAmelCase=0.0       ,	UpperCAmelCase=True       ,	UpperCAmelCase=1e-5       ,	UpperCAmelCase=1e-5       ,	**UpperCAmelCase       ,	):
							super().__init__(**UpperCAmelCase      )
							lowerCamelCase_ 		=     num_channels
							lowerCamelCase_ 		=     depths
							lowerCamelCase_ 		=     embed_dims
							lowerCamelCase_ 		=     mlp_ratio
							lowerCamelCase_ 		=     downsamples
							lowerCamelCase_ 		=     hidden_act
							lowerCamelCase_ 		=     down_patch_size
							lowerCamelCase_ 		=     down_stride
							lowerCamelCase_ 		=     down_pad
							lowerCamelCase_ 		=     drop_path_rate
							lowerCamelCase_ 		=     use_layer_scale
							lowerCamelCase_ 		=     layer_scale_init_value
							lowerCamelCase_ 		=     batch_norm_eps




class    __lowerCamelCase     (   lowerCAmelCase ):
				a__:			str									=						version.parse('1.11' )




				@property
				def 					UpperCAmelCase__	(							self      ):
							return OrderedDict(
							    [
							        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
							    ]      )





				@property
				def 					UpperCAmelCase__	(							self      ):
							return 1e-4



