'''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   _lowercase:





   def __init__(							self:    int			,a:    Union[str, Any]			,a:    Tuple=13			,a:    int=7			,a:    str=False			,a:    str=True			,a:    Optional[Any]=True			,a:    List[Any]=True			,a:    List[Any]=99			,a:    Any=32			,a:    str=5			,a:    Optional[Any]=4			,a:    Dict=37			,a:    List[str]="gelu"			,a:    str=0.1			,a:    List[str]=0.1			,a:    Union[str, Any]=512			,a:    str=16			,a:    int=2			,a:    str=0.02			,a:    Optional[Any]=3			,a:    str=4			,a:    Any=None			,a:    Optional[int]=0			,):
    __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]	):
    __UpperCAmelCase         =					ids_tensor([self.batch_size, self.seq_length]			,self.vocab_size	)

    __UpperCAmelCase         =					None
    if self.use_input_mask:
     __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         =					self.get_config()

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





   def 			snake_case	(							self:    Tuple	):
    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 			snake_case	(							self:    Optional[Any]			,a:    List[str]			,a:    int			,a:    Tuple			,a:    int			,a:    List[str]			,a:    Tuple			,a:    int	):
    __UpperCAmelCase         =					DPRContextEncoder(config=lowerCamelCase__	)
    model.to(lowerCamelCase__	)
    model.eval()
    __UpperCAmelCase         =					model(lowerCamelCase__			,attention_mask=lowerCamelCase__			,token_type_ids=lowerCamelCase__	)
    __UpperCAmelCase         =					model(lowerCamelCase__			,token_type_ids=lowerCamelCase__	)
    __UpperCAmelCase         =					model(lowerCamelCase__	)
    self.parent.assertEqual(result.pooler_output.shape			,(self.batch_size, self.projection_dim or self.hidden_size)	)





   def 			snake_case	(							self:    List[str]			,a:    Tuple			,a:    List[str]			,a:    Union[str, Any]			,a:    List[Any]			,a:    Union[str, Any]			,a:    Dict			,a:    Any	):
    __UpperCAmelCase         =					DPRQuestionEncoder(config=lowerCamelCase__	)
    model.to(lowerCamelCase__	)
    model.eval()
    __UpperCAmelCase         =					model(lowerCamelCase__			,attention_mask=lowerCamelCase__			,token_type_ids=lowerCamelCase__	)
    __UpperCAmelCase         =					model(lowerCamelCase__			,token_type_ids=lowerCamelCase__	)
    __UpperCAmelCase         =					model(lowerCamelCase__	)
    self.parent.assertEqual(result.pooler_output.shape			,(self.batch_size, self.projection_dim or self.hidden_size)	)





   def 			snake_case	(							self:    str			,a:    List[Any]			,a:    Optional[Any]			,a:    Optional[int]			,a:    Union[str, Any]			,a:    int			,a:    Union[str, Any]			,a:    str	):
    __UpperCAmelCase         =					DPRReader(config=lowerCamelCase__	)
    model.to(lowerCamelCase__	)
    model.eval()
    __UpperCAmelCase         =					model(
        lowerCamelCase__			,attention_mask=lowerCamelCase__			,)

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




@require_torch
class   _lowercase(     _a   ,_a   ,unittest.TestCase				):
   __lowerCamelCase    =  (
       (
           DPRContextEncoder,
           DPRQuestionEncoder,
           DPRReader,
       )
       if is_torch_available()
       else ()
   )
   __lowerCamelCase    =  {'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

   __lowerCamelCase    =  False
   __lowerCamelCase    =  False  # why?
   __lowerCamelCase    =  False
   __lowerCamelCase    =  False





   def 			snake_case	(							self:    Union[str, Any]	):
    __UpperCAmelCase         =					DPRModelTester(self	)
    __UpperCAmelCase         =					ConfigTester(self			,config_class=lowerCamelCase__			,hidden_size=37	)





   def 			snake_case	(							self:    Optional[Any]	):
    self.config_tester.run_common_tests()





   def 			snake_case	(							self:    Optional[int]	):
    __UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_context_encoder(*lowerCamelCase__	)





   def 			snake_case	(							self:    Any	):
    __UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_question_encoder(*lowerCamelCase__	)





   def 			snake_case	(							self:    str	):
    __UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_reader(*lowerCamelCase__	)





   def 			snake_case	(							self:    Tuple	):
    __UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()[0]

    __UpperCAmelCase         =					DPRQuestionEncoder(config=lowerCamelCase__	)
    model.to(lowerCamelCase__	)
    model.eval()

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

    self.assertIsNotNone(lowerCamelCase__	)





   @slow
   def 			snake_case	(							self:    Any	):
    for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __UpperCAmelCase         =					DPRContextEncoder.from_pretrained(lowerCamelCase__	)
     self.assertIsNotNone(lowerCamelCase__	)

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

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

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




@require_torch
class   _lowercase(     unittest.TestCase				):





   @slow
   def 			snake_case	(							self:    Union[str, Any]	):
    __UpperCAmelCase         =					DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'			,return_dict=lowerCamelCase__	)
    model.to(lowerCamelCase__	)

    __UpperCAmelCase         =					torch.tensor(
        [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]			,dtype=torch.long			,device=lowerCamelCase__	)  # [CLS] hello, is my dog cute? [SEP]
    __UpperCAmelCase         =					model(lowerCamelCase__	)[0]  # embedding shape = (1, 768)
    # compare the actual values for a slice.
    __UpperCAmelCase         =					torch.tensor(
        [
            [
                0.03236253,
                0.12753335,
                0.16818509,
                0.00279786,
                0.3896933,
                0.24264945,
                0.2178971,
                -0.02335227,
                -0.08481959,
                -0.14324117,
            ]
        ]			,dtype=torch.float			,device=lowerCamelCase__			,)
    self.assertTrue(torch.allclose(output[:, :10]			,lowerCamelCase__			,atol=1e-4	)	)





   @slow
   def 			snake_case	(							self:    int	):
    __UpperCAmelCase         =					DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'	)
    __UpperCAmelCase         =					DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'	)
    model.to(lowerCamelCase__	)

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

    __UpperCAmelCase         =					model(**lowerCamelCase__	)

    # compare the actual values for a slice.
    __UpperCAmelCase         =					torch.tensor(
        [[-10.3005, -10.7765, -11.4872, -11.6841, -11.9312, -10.3002, -9.8544, -11.7378, -12.0821, -10.2975]]			,dtype=torch.float			,device=lowerCamelCase__			,)

    __UpperCAmelCase         =					torch.tensor(
        [[-11.0684, -11.7041, -11.5397, -10.3465, -10.8791, -6.8443, -11.9959, -11.0364, -10.0096, -6.8405]]			,dtype=torch.float			,device=lowerCamelCase__			,)
    self.assertTrue(torch.allclose(outputs.start_logits[:, :10]			,lowerCamelCase__			,atol=1e-4	)	)
    self.assertTrue(torch.allclose(outputs.end_logits[:, :10]			,lowerCamelCase__			,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


_UpperCamelCase				:		Any       			=		logging.get_logger(__name__)

_UpperCamelCase				:		Any       			=		{
    'apple/mobilevitv2-1.0': 'https://huggingface.co/apple/mobilevitv2-1.0/resolve/main/config.json',
}

class   _lowercase(     _lowerCamelCase				):



   """simple docstring"""





   __lowerCamelCase    =  '''mobilevitv2'''





   def __init__(							self:    Union[str, Any]			,a:    Optional[int]=3			,a:    Any=256			,a:    str=2			,a:    Any=2.0			,a:    Union[str, Any]="swish"			,a:    List[str]=3			,a:    List[str]=32			,a:    int=0.1			,a:    List[Any]=0.02			,a:    List[Any]=1e-5			,a:    Tuple=512			,a:    Optional[Any]=[6, 12, 18]			,a:    Dict=0.1			,a:    List[Any]=255			,a:    Union[str, Any]=[2, 4, 3]			,a:    str=[128, 192, 256]			,a:    int=1.0			,a:    Optional[int]=2			,a:    Optional[Any]=0.0			,a:    int=0.0			,**a:    Optional[Any]			,):
    super().__init__(**a	)

    __UpperCAmelCase         =					num_channels
    __UpperCAmelCase         =					image_size
    __UpperCAmelCase         =					patch_size
    __UpperCAmelCase         =					expand_ratio
    __UpperCAmelCase         =					hidden_act
    __UpperCAmelCase         =					conv_kernel_size
    __UpperCAmelCase         =					output_stride
    __UpperCAmelCase         =					initializer_range
    __UpperCAmelCase         =					layer_norm_eps
    __UpperCAmelCase         =					n_attn_blocks
    __UpperCAmelCase         =					base_attn_unit_dims
    __UpperCAmelCase         =					width_multiplier
    __UpperCAmelCase         =					ffn_multiplier
    __UpperCAmelCase         =					ffn_dropout
    __UpperCAmelCase         =					attn_dropout
    __UpperCAmelCase         =					classifier_dropout_prob

    # decode head attributes for semantic segmentation
    __UpperCAmelCase         =					aspp_out_channels
    __UpperCAmelCase         =					atrous_rates
    __UpperCAmelCase         =					aspp_dropout_prob
    __UpperCAmelCase         =					semantic_loss_ignore_index




class   _lowercase(     _lowerCamelCase				):



   """simple docstring"""





   __lowerCamelCase    =  version.parse('''1.11'''				)





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





   @property
   def 			snake_case	(							self:    Tuple	):
    if self.task == "image-classification":
     return OrderedDict([('logits', {0: 'batch'})]	)
    else:
     return OrderedDict([('last_hidden_state', {0: 'batch'}), ('pooler_output', {0: 'batch'})]	)





   @property
   def 			snake_case	(							self:    Union[str, Any]	):
    return 1e-4



