





"""simple docstring"""




from ...configuration_utils import PretrainedConfig


UpperCamelCase_									={
    """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 			_a						(				_lowerCAmelCase    ):
				UpperCamelCase								=   '''tapas'''






				def __init__(			self		:   Optional[int],			lowerCAmelCase__		:   int=3_0_5_2_2,			lowerCAmelCase__		:   Optional[int]=7_6_8,			lowerCAmelCase__		:   str=1_2,			lowerCAmelCase__		:   Optional[Any]=1_2,			lowerCAmelCase__		:   Tuple=3_0_7_2,			lowerCAmelCase__		:   Any="gelu",			lowerCAmelCase__		:   List[str]=0.1,			lowerCAmelCase__		:   Union[str, Any]=0.1,			lowerCAmelCase__		:   Dict=1_0_2_4,			lowerCAmelCase__		:   Optional[Any]=[3, 2_5_6, 2_5_6, 2, 2_5_6, 2_5_6, 1_0],			lowerCAmelCase__		:   List[Any]=0.02,			lowerCAmelCase__		:   Any=1e-1_2,			lowerCAmelCase__		:   List[Any]=0,			lowerCAmelCase__		:   Tuple=10.0,			lowerCAmelCase__		:   str=0,			lowerCAmelCase__		:   Tuple=1.0,			lowerCAmelCase__		:   Optional[Any]=None,			lowerCAmelCase__		:   Tuple=1.0,			lowerCAmelCase__		:   List[Any]=False,			lowerCAmelCase__		:   Tuple=None,			lowerCAmelCase__		:   Optional[int]=1.0,			lowerCAmelCase__		:   Optional[Any]=1.0,			lowerCAmelCase__		:   List[str]=False,			lowerCAmelCase__		:   List[str]=False,			lowerCAmelCase__		:   str="ratio",			lowerCAmelCase__		:   List[str]=None,			lowerCAmelCase__		:   Tuple=None,			lowerCAmelCase__		:   Tuple=6_4,			lowerCAmelCase__		:   List[str]=3_2,			lowerCAmelCase__		:   Optional[int]=False,			lowerCAmelCase__		:   Optional[Any]=True,			lowerCAmelCase__		:   Tuple=False,			lowerCAmelCase__		:   List[Any]=False,			lowerCAmelCase__		:   Any=True,			lowerCAmelCase__		:   List[str]=False,			lowerCAmelCase__		:   int=None,			lowerCAmelCase__		:   str=None,			**lowerCAmelCase__		:   Dict,			)     ->				Tuple:




										'''simple docstring'''



										super().__init__(pad_token_id=lowerCAmelCase__,			**lowerCAmelCase__							)

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

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

										# Aggregation hyperparameters
										_UpperCamelCase      : List[Any]         =   aggregation_labels
										_UpperCamelCase      : Union[str, Any]         =   no_aggregation_label_index

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










"""simple docstring"""




import math

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from .attention_processor import Attention
from .embeddings import get_timestep_embedding
from .modeling_utils import ModelMixin




class 			_a						(				_lowerCAmelCase     ,    _lowerCAmelCase    ):






				@register_to_config
				def __init__(			self		:   Any,			lowerCAmelCase__		:   int = 1_2_8,			lowerCAmelCase__		:   int = 2_5_6,			lowerCAmelCase__		:   float = 2_000.0,			lowerCAmelCase__		:   int = 7_6_8,			lowerCAmelCase__		:   int = 1_2,			lowerCAmelCase__		:   int = 1_2,			lowerCAmelCase__		:   int = 6_4,			lowerCAmelCase__		:   int = 2_0_4_8,			lowerCAmelCase__		:   float = 0.1,			)     ->				Any:




										'''simple docstring'''



										super().__init__()

										_UpperCamelCase      : Any         =   nn.Sequential(
										    nn.Linear(lowerCAmelCase__,			d_model * 4,			bias=lowerCAmelCase__							),			nn.SiLU(),			nn.Linear(d_model * 4,			d_model * 4,			bias=lowerCAmelCase__							),			nn.SiLU(),			)

										_UpperCamelCase      : List[Any]         =   nn.Embedding(lowerCAmelCase__,			lowerCAmelCase__							)
										_UpperCamelCase      : List[Any]         =   False

										_UpperCamelCase      : Optional[Any]         =   nn.Linear(lowerCAmelCase__,			lowerCAmelCase__,			bias=lowerCAmelCase__							)

										_UpperCamelCase      : Any         =   nn.Dropout(p=lowerCAmelCase__							)

										_UpperCamelCase      : List[str]         =   nn.ModuleList()
										for lyr_num in range(lowerCAmelCase__							):
																# FiLM conditional T5 decoder
																_UpperCamelCase      : Any         =   DecoderLayer(d_model=lowerCAmelCase__,			d_kv=lowerCAmelCase__,			num_heads=lowerCAmelCase__,			d_ff=lowerCAmelCase__,			dropout_rate=lowerCAmelCase__							)
																self.decoders.append(lowerCAmelCase__							)

										_UpperCamelCase      : Optional[int]         =   TaLayerNorm(lowerCAmelCase__							)

										_UpperCamelCase      : Dict         =   nn.Dropout(p=lowerCAmelCase__							)
										_UpperCamelCase      : Dict         =   nn.Linear(lowerCAmelCase__,			lowerCAmelCase__,			bias=lowerCAmelCase__							)






				def     snake_case     (			self		:   Optional[Any],			lowerCAmelCase__		:   Union[str, Any],			lowerCAmelCase__		:   Optional[int]							)     ->				List[str]:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   torch.mul(query_input.unsqueeze(-1							),			key_input.unsqueeze(-2							)							)
										return mask.unsqueeze(-3							)






				def     snake_case     (			self		:   str,			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   Optional[Any]							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase      : Union[str, Any]         =   decoder_input_tokens.shape
										assert decoder_noise_time.shape == (batch,)

										# decoder_noise_time is in [0, 1), so rescale to expected timing range.
										_UpperCamelCase      : Any         =   get_timestep_embedding(
										    decoder_noise_time * self.config.max_decoder_noise_time,			embedding_dim=self.config.d_model,			max_period=self.config.max_decoder_noise_time,			).to(dtype=self.dtype							)

										_UpperCamelCase      : Union[str, Any]         =   self.conditioning_emb(lowerCAmelCase__							).unsqueeze(1							)

										assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4)

										_UpperCamelCase      : int         =   decoder_input_tokens.shape[1]

										# If we want to use relative positions for audio context, we can just offset
										# this sequence by the length of encodings_and_masks.
										_UpperCamelCase      : int         =   torch.broadcast_to(
										    torch.arange(lowerCAmelCase__,			device=decoder_input_tokens.device							),			(batch, seq_length),			)

										_UpperCamelCase      : Dict         =   self.position_encoding(lowerCAmelCase__							)

										_UpperCamelCase      : List[str]         =   self.continuous_inputs_projection(lowerCAmelCase__							)
										inputs += position_encodings
										_UpperCamelCase      : Dict         =   self.dropout(lowerCAmelCase__							)

										# decoder: No padding present.
										_UpperCamelCase      : Tuple         =   torch.ones(
										    decoder_input_tokens.shape[:2],			device=decoder_input_tokens.device,			dtype=inputs.dtype							)

										# Translate encoding masks to encoder-decoder masks.
										_UpperCamelCase      : Tuple         =   [(x, self.encoder_decoder_mask(lowerCAmelCase__,			lowerCAmelCase__							)) for x, y in encodings_and_masks]

										# cross attend style: concat encodings
										_UpperCamelCase      : int         =   torch.cat([x[0] for x in encodings_and_encdec_masks],			dim=1							)
										_UpperCamelCase      : Dict         =   torch.cat([x[1] for x in encodings_and_encdec_masks],			dim=-1							)

										for lyr in self.decoders:
																_UpperCamelCase      : List[Any]         =   lyr(
																    lowerCAmelCase__,			conditioning_emb=lowerCAmelCase__,			encoder_hidden_states=lowerCAmelCase__,			encoder_attention_mask=lowerCAmelCase__,			)[0]

										_UpperCamelCase      : Any         =   self.decoder_norm(lowerCAmelCase__							)
										_UpperCamelCase      : Tuple         =   self.post_dropout(lowerCAmelCase__							)

										_UpperCamelCase      : Union[str, Any]         =   self.spec_out(lowerCAmelCase__							)
										return spec_out




class 			_a						(				nn.Module    ):






				def __init__(			self		:   Union[str, Any],			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   Union[str, Any]=1e-6							)     ->				Optional[int]:




										'''simple docstring'''



										super().__init__()
										_UpperCamelCase      : Optional[int]         =   nn.ModuleList()

										# cond self attention: layer 0
										self.layer.append(
										    TaLayerSelfAttentionCond(d_model=lowerCAmelCase__,			d_kv=lowerCAmelCase__,			num_heads=lowerCAmelCase__,			dropout_rate=lowerCAmelCase__							)							)

										# cross attention: layer 1
										self.layer.append(
										    TaLayerCrossAttention(
										        d_model=lowerCAmelCase__,			d_kv=lowerCAmelCase__,			num_heads=lowerCAmelCase__,			dropout_rate=lowerCAmelCase__,			layer_norm_epsilon=lowerCAmelCase__,			)							)

										# Film Cond MLP + dropout: last layer
										self.layer.append(
										    TaLayerFFCond(d_model=lowerCAmelCase__,			d_ff=lowerCAmelCase__,			dropout_rate=lowerCAmelCase__,			layer_norm_epsilon=lowerCAmelCase__							)							)






				def     snake_case     (			self		:   Union[str, Any],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   List[Any]=None,			lowerCAmelCase__		:   List[str]=None,			lowerCAmelCase__		:   List[Any]=None,			lowerCAmelCase__		:   Optional[Any]=None,			lowerCAmelCase__		:   Any=None,			)     ->				List[Any]:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.layer[0](
										    lowerCAmelCase__,			conditioning_emb=lowerCAmelCase__,			attention_mask=lowerCAmelCase__,			)

										if encoder_hidden_states is not None:
																_UpperCamelCase      : Any         =   torch.where(encoder_attention_mask > 0,			0,			-1e1_0							).to(
																    encoder_hidden_states.dtype							)

																_UpperCamelCase      : int         =   self.layer[1](
																    lowerCAmelCase__,			key_value_states=lowerCAmelCase__,			attention_mask=lowerCAmelCase__,			)

										# Apply Film Conditional Feed Forward layer
										_UpperCamelCase      : Optional[int]         =   self.layer[-1](lowerCAmelCase__,			lowerCAmelCase__							)

										return (hidden_states,)




class 			_a						(				nn.Module    ):






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




										'''simple docstring'''



										super().__init__()
										_UpperCamelCase      : int         =   TaLayerNorm(lowerCAmelCase__							)
										_UpperCamelCase      : List[Any]         =   TaFiLMLayer(in_features=d_model * 4,			out_features=lowerCAmelCase__							)
										_UpperCamelCase      : Any         =   Attention(query_dim=lowerCAmelCase__,			heads=lowerCAmelCase__,			dim_head=lowerCAmelCase__,			out_bias=lowerCAmelCase__,			scale_qk=lowerCAmelCase__							)
										_UpperCamelCase      : List[str]         =   nn.Dropout(lowerCAmelCase__							)






				def     snake_case     (			self		:   Optional[int],			lowerCAmelCase__		:   Tuple,			lowerCAmelCase__		:   str=None,			lowerCAmelCase__		:   Union[str, Any]=None,			)     ->				List[str]:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self.layer_norm(lowerCAmelCase__							)

										if conditioning_emb is not None:
																_UpperCamelCase      : str         =   self.FiLMLayer(lowerCAmelCase__,			lowerCAmelCase__							)

										# Self-attention block
										_UpperCamelCase      : Tuple         =   self.attention(lowerCAmelCase__							)

										_UpperCamelCase      : Dict         =   hidden_states + self.dropout(lowerCAmelCase__							)

										return hidden_states




class 			_a						(				nn.Module    ):






				def __init__(			self		:   Tuple,			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   Tuple							)     ->				Tuple:




										'''simple docstring'''



										super().__init__()
										_UpperCamelCase      : Any         =   Attention(query_dim=lowerCAmelCase__,			heads=lowerCAmelCase__,			dim_head=lowerCAmelCase__,			out_bias=lowerCAmelCase__,			scale_qk=lowerCAmelCase__							)
										_UpperCamelCase      : Tuple         =   TaLayerNorm(lowerCAmelCase__,			eps=lowerCAmelCase__							)
										_UpperCamelCase      : int         =   nn.Dropout(lowerCAmelCase__							)






				def     snake_case     (			self		:   Optional[Any],			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   str=None,			lowerCAmelCase__		:   Union[str, Any]=None,			)     ->				List[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Optional[int]         =   self.layer_norm(lowerCAmelCase__							)
										_UpperCamelCase      : Tuple         =   self.attention(
										    lowerCAmelCase__,			encoder_hidden_states=lowerCAmelCase__,			attention_mask=attention_mask.squeeze(1							),			)
										_UpperCamelCase      : str         =   hidden_states + self.dropout(lowerCAmelCase__							)
										return layer_output




class 			_a						(				nn.Module    ):






				def __init__(			self		:   Tuple,			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   Tuple,			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   str							)     ->				Tuple:




										'''simple docstring'''



										super().__init__()
										_UpperCamelCase      : Any         =   TaDenseGatedActDense(d_model=lowerCAmelCase__,			d_ff=lowerCAmelCase__,			dropout_rate=lowerCAmelCase__							)
										_UpperCamelCase      : Optional[int]         =   TaFiLMLayer(in_features=d_model * 4,			out_features=lowerCAmelCase__							)
										_UpperCamelCase      : Union[str, Any]         =   TaLayerNorm(lowerCAmelCase__,			eps=lowerCAmelCase__							)
										_UpperCamelCase      : List[Any]         =   nn.Dropout(lowerCAmelCase__							)






				def     snake_case     (			self		:   Tuple,			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   List[str]=None							)     ->				Tuple:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self.layer_norm(lowerCAmelCase__							)
										if conditioning_emb is not None:
																_UpperCamelCase      : Dict         =   self.film(lowerCAmelCase__,			lowerCAmelCase__							)

										_UpperCamelCase      : str         =   self.DenseReluDense(lowerCAmelCase__							)
										_UpperCamelCase      : str         =   hidden_states + self.dropout(lowerCAmelCase__							)
										return hidden_states




class 			_a						(				nn.Module    ):






				def __init__(			self		:   str,			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   Dict							)     ->				Tuple:




										'''simple docstring'''



										super().__init__()
										_UpperCamelCase      : List[str]         =   nn.Linear(lowerCAmelCase__,			lowerCAmelCase__,			bias=lowerCAmelCase__							)
										_UpperCamelCase      : Optional[int]         =   nn.Linear(lowerCAmelCase__,			lowerCAmelCase__,			bias=lowerCAmelCase__							)
										_UpperCamelCase      : Optional[int]         =   nn.Linear(lowerCAmelCase__,			lowerCAmelCase__,			bias=lowerCAmelCase__							)
										_UpperCamelCase      : List[str]         =   nn.Dropout(lowerCAmelCase__							)
										_UpperCamelCase      : Union[str, Any]         =   NewGELUActivation()






				def     snake_case     (			self		:   str,			lowerCAmelCase__		:   Tuple							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.act(self.wi_a(lowerCAmelCase__							)							)
										_UpperCamelCase      : Dict         =   self.wi_a(lowerCAmelCase__							)
										_UpperCamelCase      : Any         =   hidden_gelu * hidden_linear
										_UpperCamelCase      : Any         =   self.dropout(lowerCAmelCase__							)

										_UpperCamelCase      : Optional[int]         =   self.wo(lowerCAmelCase__							)
										return hidden_states




class 			_a						(				nn.Module    ):






				def __init__(			self		:   Union[str, Any],			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   Optional[Any]=1e-6							)     ->				Optional[Any]:




										'''simple docstring'''



										super().__init__()
										_UpperCamelCase      : Optional[Any]         =   nn.Parameter(torch.ones(lowerCAmelCase__							)							)
										_UpperCamelCase      : Tuple         =   eps






				def     snake_case     (			self		:   Dict,			lowerCAmelCase__		:   Tuple							)     ->				Any:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   hidden_states.to(torch.floataa							).pow(2							).mean(-1,			keepdim=lowerCAmelCase__							)
										_UpperCamelCase      : Union[str, Any]         =   hidden_states * torch.rsqrt(variance + self.variance_epsilon							)

										# convert into half-precision if necessary
										if self.weight.dtype in [torch.floataa, torch.bfloataa]:
																_UpperCamelCase      : int         =   hidden_states.to(self.weight.dtype							)

										return self.weight * hidden_states




class 			_a						(				nn.Module    ):






				def     snake_case     (			self		:   Optional[Any],			lowerCAmelCase__		:   torch.Tensor							)     ->				torch.Tensor:




										'''simple docstring'''



										return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi							) * (input + 0.044_715 * torch.pow(lowerCAmelCase__,			3.0							))							))




class 			_a						(				nn.Module    ):






				def __init__(			self		:   List[str],			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   int							)     ->				List[Any]:




										'''simple docstring'''



										super().__init__()
										_UpperCamelCase      : Tuple         =   nn.Linear(lowerCAmelCase__,			out_features * 2,			bias=lowerCAmelCase__							)






				def     snake_case     (			self		:   int,			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   Any							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.scale_bias(lowerCAmelCase__							)
										_UpperCamelCase       ,							_UpperCamelCase      : Union[str, Any]         =   torch.chunk(lowerCAmelCase__,			2,			-1							)
										_UpperCamelCase      : Optional[int]         =   x * (1 + scale) + shift
										return x



