





"""simple docstring"""




from typing import TYPE_CHECKING

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


a						: Tuple     	=			{
    '''configuration_mega''': ['''MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegaConfig''', '''MegaOnnxConfig'''],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a						: List[Any]     	=			[
		    '''MEGA_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''MegaForCausalLM''',
		    '''MegaForMaskedLM''',
		    '''MegaForMultipleChoice''',
		    '''MegaForQuestionAnswering''',
		    '''MegaForSequenceClassification''',
		    '''MegaForTokenClassification''',
		    '''MegaModel''',
		    '''MegaPreTrainedModel''',
		]

if TYPE_CHECKING:
		from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mega import (
				    MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MegaForCausalLM,
				    MegaForMaskedLM,
				    MegaForMultipleChoice,
				    MegaForQuestionAnswering,
				    MegaForSequenceClassification,
				    MegaForTokenClassification,
				    MegaModel,
				    MegaPreTrainedModel,
				)

else:
		import sys

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




'''simple docstring'''







import torch

from transformers import AutoModel






class  lowerCAmelCase__   (  torch.nn.Module     ):






					def __init__(   self				,							__SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased"		):


											"""simple docstring"""





											super(__SCREAMING_SNAKE_CASE				,							self		).__init__()

											lowercase_	:     Tuple													=       AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE				,							return_dict=__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Optional[Any]													=       torch.nn.CosineSimilarity(3				,							1E-0_8		)
											lowercase_	:     Optional[Any]													=       torch.nn.Softmax(dim=1		)






					def 	_snake_case    (   self				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											return self.bert(**__SCREAMING_SNAKE_CASE		).last_hidden_state






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											return token_embeddings.sum(2				,							keepdim=__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE=1		):


											"""simple docstring"""





											return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)		)






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_	:     Optional[Any]													=       W_supports['''sizes'''].tolist()
											lowercase_	:     Dict													=       W_supports['''start_token_id'''].item()
											lowercase_	:     List[Any]													=       W_supports['''end_token_id'''].item()

											del W_supports["sizes"]
											del W_supports["start_token_id"]
											del W_supports["end_token_id"]

											lowercase_	:     List[str]													=       self.BERT(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Optional[int]													=       self.BERT(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     str													=       None
											lowercase_	:     Dict													=       None

											lowercase_	:     Tuple													=       W_supports['''input_ids'''] == start_token_id
											lowercase_	:     Any													=       W_supports['''input_ids'''] == end_token_id

											for i, size in enumerate(__SCREAMING_SNAKE_CASE		):
																	if i == 0:
																							lowercase_	:     List[str]													=       0
																	else:
																							lowercase_	:     List[Any]													=       support_sizes[i - 1]

																	lowercase_	:     str													=       S[s : s + size][start_token_masks[s : s + size]]
																	lowercase_	:     Optional[int]													=       S[s : s + size][end_token_masks[s : s + size]]

																	lowercase_	:     List[str]													=       torch.matmul(q[i]				,							s_start.T		).sum(1		).softmax(0		)
																	lowercase_	:     List[str]													=       torch.matmul(q[i]				,							s_end.T		).sum(1		).softmax(0		)

																	if p_starts is not None:
																							lowercase_	:     Tuple													=       torch.vstack((p_starts, p_start)		)
																							lowercase_	:     Optional[Any]													=       torch.vstack((p_ends, p_end)		)
																	else:
																							lowercase_	:     str													=       p_start
																							lowercase_	:     int													=       p_end

											return p_starts, p_ends



