
"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


A__   :						Optional[int] 							=     {
    'configuration_dpr': ['DPR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DPRConfig'],
    'tokenization_dpr': [
        'DPRContextEncoderTokenizer',
        'DPRQuestionEncoderTokenizer',
        'DPRReaderOutput',
        'DPRReaderTokenizer',
    ],
}


try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    A__   :						Dict 							=     [
        'DPRContextEncoderTokenizerFast',
        'DPRQuestionEncoderTokenizerFast',
        'DPRReaderTokenizerFast',
    ]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    A__   :						str 							=     [
        'DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
        'DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
        'DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST',
        'DPRContextEncoder',
        'DPRPretrainedContextEncoder',
        'DPRPreTrainedModel',
        'DPRPretrainedQuestionEncoder',
        'DPRPretrainedReader',
        'DPRQuestionEncoder',
        'DPRReader',
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    A__   :						Dict 							=     [
        'TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
        'TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
        'TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST',
        'TFDPRContextEncoder',
        'TFDPRPretrainedContextEncoder',
        'TFDPRPretrainedQuestionEncoder',
        'TFDPRPretrainedReader',
        'TFDPRQuestionEncoder',
        'TFDPRReader',
    ]


if TYPE_CHECKING:
    from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
    from .tokenization_dpr import (
        DPRContextEncoderTokenizer,
        DPRQuestionEncoderTokenizer,
        DPRReaderOutput,
        DPRReaderTokenizer,
    )

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_dpr_fast import (
            DPRContextEncoderTokenizerFast,
            DPRQuestionEncoderTokenizerFast,
            DPRReaderTokenizerFast,
        )

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_dpr import (
            DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
            DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
            DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
            DPRContextEncoder,
            DPRPretrainedContextEncoder,
            DPRPreTrainedModel,
            DPRPretrainedQuestionEncoder,
            DPRPretrainedReader,
            DPRQuestionEncoder,
            DPRReader,
        )

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_dpr import (
            TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
            TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
            TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFDPRContextEncoder,
            TFDPRPretrainedContextEncoder,
            TFDPRPretrainedQuestionEncoder,
            TFDPRPretrainedReader,
            TFDPRQuestionEncoder,
            TFDPRReader,
        )

else:
    import sys

    A__   :						Tuple 							=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)







"""simple docstring"""
import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import (
    TaBlock,
    TaConfig,
    TaLayerNorm,
)

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin





class   __A  (snake_case__    ,		snake_case__    ,		snake_case__):





							'''simple docstring'''

							@register_to_config
							def __init__(					self				:   List[str]		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   float		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   str		,							UpperCAmelCase_				:   bool = False		,							)       ->Dict:



														"""simple docstring"""



														super().__init__()

														snake_case_     							=		nn.Linear(UpperCAmelCase_		,							UpperCAmelCase_		,							bias=UpperCAmelCase_							)

														snake_case_     							=		nn.Embedding(UpperCAmelCase_		,							UpperCAmelCase_							)
														snake_case_     							=		False

														snake_case_     							=		nn.Dropout(p=UpperCAmelCase_							)

														snake_case_     							=		TaConfig(
														    d_model=UpperCAmelCase_		,							num_heads=UpperCAmelCase_		,							d_kv=UpperCAmelCase_		,							d_ff=UpperCAmelCase_		,							feed_forward_proj=UpperCAmelCase_		,							dropout_rate=UpperCAmelCase_		,							is_decoder=UpperCAmelCase_		,							is_encoder_decoder=UpperCAmelCase_		,							)
														snake_case_     							=		nn.ModuleList()
														for lyr_num in range(UpperCAmelCase_							):
																					snake_case_     							=		TaBlock(UpperCAmelCase_							)
																					self.encoders.append(UpperCAmelCase_							)

														snake_case_     							=		TaLayerNorm(UpperCAmelCase_							)
														snake_case_     							=		nn.Dropout(p=UpperCAmelCase_							)



							def 		lowerCAmelCase							(					self				:   Optional[Any]		,							UpperCAmelCase_				:   Any		,							UpperCAmelCase_				:   List[str]							)       ->List[Any]:



														"""simple docstring"""



														snake_case_     							=		self.input_proj(UpperCAmelCase_							)

														# terminal relative positional encodings
														snake_case_     							=		encoder_inputs.shape[1]
														snake_case_     							=		torch.arange(UpperCAmelCase_		,							device=encoder_inputs.device							)

														snake_case_     							=		encoder_inputs_mask.sum(-1							)
														snake_case_     							=		torch.roll(input_positions.unsqueeze(0							)		,							tuple(seq_lens.tolist()							)		,							dims=0							)
														x += self.position_encoding(UpperCAmelCase_							)

														snake_case_     							=		self.dropout_pre(UpperCAmelCase_							)

														# inverted the attention mask
														snake_case_     							=		encoder_inputs.size()
														snake_case_     							=		self.get_extended_attention_mask(UpperCAmelCase_		,							UpperCAmelCase_							)

														for lyr in self.encoders:
																					snake_case_     							=		lyr(UpperCAmelCase_		,							UpperCAmelCase_							)[0]
														snake_case_     							=		self.layer_norm(UpperCAmelCase_							)

														return self.dropout_post(UpperCAmelCase_							), encoder_inputs_mask



