






'''simple docstring'''







from ...configuration_utils import PretrainedConfig


__lowerCAmelCase       		=      {
    'google/tapas-base-finetuned-sqa': (
        'https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-wtq': (
        'https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-wikisql-supervised': (
        'https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json'
    ),
    'google/tapas-base-finetuned-tabfact': (
        'https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json'
    ),
}





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				"tapas"






			def __init__( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Any=3_0522  ,						__SCREAMING_SNAKE_CASE      :			Tuple=768  ,						__SCREAMING_SNAKE_CASE      :			str=12  ,						__SCREAMING_SNAKE_CASE      :			Dict=12  ,						__SCREAMING_SNAKE_CASE      :			int=3072  ,						__SCREAMING_SNAKE_CASE      :			Any="gelu"  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=0.1  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=0.1  ,						__SCREAMING_SNAKE_CASE      :			Dict=1024  ,						__SCREAMING_SNAKE_CASE      :			Dict=[3, 256, 256, 2, 256, 256, 10]  ,						__SCREAMING_SNAKE_CASE      :			Any=0.02  ,						__SCREAMING_SNAKE_CASE      :			Any=1e-12  ,						__SCREAMING_SNAKE_CASE      :			int=0  ,						__SCREAMING_SNAKE_CASE      :			int=10.0  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=0  ,						__SCREAMING_SNAKE_CASE      :			Any=1.0  ,						__SCREAMING_SNAKE_CASE      :			int=None  ,						__SCREAMING_SNAKE_CASE      :			List[str]=1.0  ,						__SCREAMING_SNAKE_CASE      :			str=False  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=None  ,						__SCREAMING_SNAKE_CASE      :			Tuple=1.0  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=1.0  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=False  ,						__SCREAMING_SNAKE_CASE      :			List[Any]="ratio"  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=None  ,						__SCREAMING_SNAKE_CASE      :			Any=None  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=64  ,						__SCREAMING_SNAKE_CASE      :			str=32  ,						__SCREAMING_SNAKE_CASE      :			Tuple=False  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=True  ,						__SCREAMING_SNAKE_CASE      :			Tuple=False  ,						__SCREAMING_SNAKE_CASE      :			Tuple=False  ,						__SCREAMING_SNAKE_CASE      :			Dict=True  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=False  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=None  ,						__SCREAMING_SNAKE_CASE      :			int=None  ,						**__SCREAMING_SNAKE_CASE      :			List[str]  ,						)			->			str:
							super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)

							# BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes)
							a_		:  Union[str, Any]											=      vocab_size
							a_		:  Optional[Any]											=      hidden_size
							a_		:  str											=      num_hidden_layers
							a_		:  Dict											=      num_attention_heads
							a_		:  Any											=      hidden_act
							a_		:  Dict											=      intermediate_size
							a_		:  Optional[int]											=      hidden_dropout_prob
							a_		:  str											=      attention_probs_dropout_prob
							a_		:  Tuple											=      max_position_embeddings
							a_		:  int											=      type_vocab_sizes
							a_		:  Tuple											=      initializer_range
							a_		:  Any											=      layer_norm_eps

							# Fine-tuning task hyperparameters
							a_		:  Any											=      positive_label_weight
							a_		:  int											=      num_aggregation_labels
							a_		:  Union[str, Any]											=      aggregation_loss_weight
							a_		:  List[str]											=      use_answer_as_supervision
							a_		:  Any											=      answer_loss_importance
							a_		:  int											=      use_normalized_answer_loss
							a_		:  Optional[int]											=      huber_loss_delta
							a_		:  List[str]											=      temperature
							a_		:  Union[str, Any]											=      aggregation_temperature
							a_		:  str											=      use_gumbel_for_cells
							a_		:  List[Any]											=      use_gumbel_for_aggregation
							a_		:  Optional[Any]											=      average_approximation_function
							a_		:  Dict											=      cell_selection_preference
							a_		:  str											=      answer_loss_cutoff
							a_		:  Union[str, Any]											=      max_num_rows
							a_		:  Optional[int]											=      max_num_columns
							a_		:  str											=      average_logits_per_cell
							a_		:  Tuple											=      select_one_column
							a_		:  List[str]											=      allow_empty_column_selection
							a_		:  List[str]											=      init_cell_selection_weights_to_zero
							a_		:  Union[str, Any]											=      reset_position_index_per_cell
							a_		:  Dict											=      disable_per_token_loss

							# Aggregation hyperparameters
							a_		:  List[str]											=      aggregation_labels
							a_		:  Dict											=      no_aggregation_label_index

							if isinstance(self.aggregation_labels  ,						__SCREAMING_SNAKE_CASE					):
											a_		:  str											=      {int(__SCREAMING_SNAKE_CASE					): v for k, v in aggregation_labels.items()}












'''simple docstring'''







import torch

from transformers import AutoModel





class 	SCREAMING_SNAKE_CASE					(					torch.nn.Module       ):






			def __init__( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			int="sayef/fsner-bert-base-uncased"					)			->			str:
							super(__SCREAMING_SNAKE_CASE  ,						self					).__init__()

							a_		:  str											=      AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE  ,						return_dict=__SCREAMING_SNAKE_CASE					)
							a_		:  Optional[int]											=      torch.nn.CosineSimilarity(3  ,						1e-08					)
							a_		:  Dict											=      torch.nn.Softmax(dim=1					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						**__SCREAMING_SNAKE_CASE      :			int					)			->			str:
							return self.bert(**__SCREAMING_SNAKE_CASE					).last_hidden_state






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			List[Any]					)			->			Optional[int]:
							return token_embeddings.sum(2  ,						keepdim=__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			int=1					)			->			Dict:
							return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]					)			->			Any:
							a_		:  Dict											=      W_supports['''sizes'''].tolist()
							a_		:  Tuple											=      W_supports['''start_token_id'''].item()
							a_		:  List[Any]											=      W_supports['''end_token_id'''].item()

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

							a_		:  int											=      self.BERT(**__SCREAMING_SNAKE_CASE					)
							a_		:  Any											=      self.BERT(**__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[Any]											=      None
							a_		:  Tuple											=      None

							a_		:  List[str]											=      W_supports['''input_ids'''] == start_token_id
							a_		:  Dict											=      W_supports['''input_ids'''] == end_token_id

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

											a_		:  Union[str, Any]											=      S[s : s + size][start_token_masks[s : s + size]]
											a_		:  Tuple											=      S[s : s + size][end_token_masks[s : s + size]]

											a_		:  Tuple											=      torch.matmul(q[i]  ,						s_start.T					).sum(1					).softmax(0					)
											a_		:  Optional[Any]											=      torch.matmul(q[i]  ,						s_end.T					).sum(1					).softmax(0					)

											if p_starts is not None:
															a_		:  Any											=      torch.vstack((p_starts, p_start)					)
															a_		:  Dict											=      torch.vstack((p_ends, p_end)					)
											else:
															a_		:  Optional[int]											=      p_start
															a_		:  List[Any]											=      p_end

							return p_starts, p_ends




