



"""simple docstring"""







from importlib import import_module

from .logging import get_logger


lowerCAmelCase_   						=   get_logger(__name__)
class        __A       :







		'''simple docstring'''





		def __init__(	self						:  Tuple			,_snake_case						:  List[Any]			,_snake_case						:  Tuple=None						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Dict				=			attrs or []
				if module is not None:
						for key in module.__dict__:
								if key in attrs or not key.startswith('''__'''						):
										setattr(self			,_snake_case			,getattr(_snake_case			,_snake_case						)						)
				lowercase__   :    List[Any]				=			module._original_module if isinstance(_snake_case			,_PatchedModuleObj						) else module





class        __A       :







		'''simple docstring'''





		lowerCAmelCase			:					str   	=				[]







		def __init__(	self						:  int			,_snake_case						:  List[Any]			,_snake_case						:  str			,_snake_case						:  Union[str, Any]			,_snake_case						:  Dict=None						)					->		int:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			obj
				lowercase__   :    Tuple				=			target
				lowercase__   :    str				=			new
				lowercase__   :    Union[str, Any]				=			target.split('''.'''						)[0]
				lowercase__   :    str				=			{}
				lowercase__   :    str				=			attrs or []







		def __enter__(	self						:  Any						)					->		Any:






				"""simple docstring"""



				*lowercase__		,			lowercase__   :    Optional[Any]				=			self.target.split('''.'''						)

				# Patch modules:
				# it's used to patch attributes of submodules like "os.path.join";
				# in this case we need to patch "os" and "os.path"

				for i in range(len(_snake_case						)						):
						try:
								lowercase__   :    int				=			import_module('''.'''.join(submodules[: i + 1]						)						)
						except ModuleNotFoundError:
								continue
						# We iterate over all the globals in self.obj in case we find "os" or "os.path"
						for attr in self.obj.__dir__():
								lowercase__   :    Any				=			getattr(self.obj			,_snake_case						)
								# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
								# This allows to patch renamed modules like "from os import path as ospath".
								if obj_attr is submodule or (
								    (isinstance(_snake_case			,_PatchedModuleObj						) and obj_attr._original_module is submodule)
								):
										lowercase__   :    Optional[Any]				=			obj_attr
										# patch at top level
										setattr(self.obj			,_snake_case			,_PatchedModuleObj(_snake_case			,attrs=self.attrs						)						)
										lowercase__   :    Optional[int]				=			getattr(self.obj			,_snake_case						)
										# construct lower levels patches
										for key in submodules[i + 1 :]:
												setattr(_snake_case			,_snake_case			,_PatchedModuleObj(getattr(_snake_case			,_snake_case			,_snake_case						)			,attrs=self.attrs						)						)
												lowercase__   :    str				=			getattr(_snake_case			,_snake_case						)
										# finally set the target attribute
										setattr(_snake_case			,_snake_case			,self.new						)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

				if submodules:  # if it's an attribute of a submodule like "os.path.join"
						try:
								lowercase__   :    Tuple				=			getattr(import_module('''.'''.join(_snake_case						)						)			,_snake_case						)
						except (AttributeError, ModuleNotFoundError):
								return
						# We iterate over all the globals in self.obj in case we find "os.path.join"
						for attr in self.obj.__dir__():
								# We don't check for the name of the global, but rather if its value *is* "os.path.join".
								# This allows to patch renamed attributes like "from os.path import join as pjoin".
								if getattr(self.obj			,_snake_case						) is attr_value:
										lowercase__   :    Union[str, Any]				=			getattr(self.obj			,_snake_case						)
										setattr(self.obj			,_snake_case			,self.new						)
				elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
						lowercase__   :    Tuple				=			globals()['''__builtins__'''][target_attr]
						setattr(self.obj			,_snake_case			,self.new						)
				else:
						raise RuntimeError(f"""Tried to patch attribute {target_attr} instead of a submodule."""						)







		def __exit__(	self						:  Optional[int]			,*_snake_case						:  int						)					->		Any:






				"""simple docstring"""



				for attr in list(self.original						):
						setattr(self.obj			,_snake_case			,self.original.pop(_snake_case						)						)







		def 					UpperCAmelCase    (	self						:  List[str]						)					->		List[Any]:






				"""simple docstring"""



				self.__enter__()
				self._active_patches.append(self						)







		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		Optional[int]:






				"""simple docstring"""



				try:
						self._active_patches.remove(self						)
				except ValueError:
						# If the patch hasn't been started this will fail
						return None

				return self.__exit__()


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



