



"""simple docstring"""






import collections
import types

import numpy as np

from ..utils import (
    add_end_docstrings,
    is_tensorflow_probability_available,
    is_tf_available,
    is_torch_available,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, Dataset, Pipeline, PipelineException


if is_torch_available():
		import torch

		from ..models.auto.modeling_auto import (
		    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
		    MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
		)

if is_tf_available() and is_tensorflow_probability_available():
		import tensorflow as tf
		import tensorflow_probability as tfp

		from ..models.auto.modeling_tf_auto import (
		    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
		    TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
		)

class 			__SCREAMING_SNAKE_CASE      ( lowerCAmelCase_  ):


					'''simple docstring'''





					def __call__(			self				:    Tuple,    lowerCamelCase				:    Tuple=None,    lowerCamelCase				:    Union[str, Any]=None,    **lowerCamelCase				:    Optional[Any]	)->   Union[str, Any]:
								# Returns tqa_pipeline_inputs of shape:
								# [
								#   {"table": pd.DataFrame, "query": List[str]},
								#   ...,
								#   {"table": pd.DataFrame, "query" : List[str]}
								# ]
								requires_backends(self,    '''pandas'''	)
								import pandas as pd

								if table is None:
											raise ValueError('''Keyword argument `table` cannot be None.'''	)
								elif query is None:
											if isinstance(lowerCamelCase,    lowerCamelCase	) and table.get('''query'''	) is not None and table.get('''table'''	) is not None:
														lowerCamelCase__						:						Any						      =[table]
											elif isinstance(lowerCamelCase,    lowerCamelCase	) and len(lowerCamelCase	) > 0:
														if not all(isinstance(lowerCamelCase,    lowerCamelCase	) for d in table	):
																	raise ValueError(
																	    F'''Keyword argument `table` should be a list of dict, but is {(type(lowerCamelCase	) for d in table)}'''	)

														if table[0].get('''query'''	) is not None and table[0].get('''table'''	) is not None:
																	lowerCamelCase__						:						List[str]						      =table
														else:
																	raise ValueError(
																	    '''If keyword argument `table` is a list of dictionaries, each dictionary should have a `table`'''
																	    F''' and `query` key, but only dictionary has keys {table[0].keys()} `table` and `query` keys.'''	)
											elif Dataset is not None and isinstance(lowerCamelCase,    lowerCamelCase	) or isinstance(lowerCamelCase,    types.GeneratorType	):
														return table
											else:
														raise ValueError(
														    '''Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but '''
														    F'''is {type(lowerCamelCase	)})'''	)
								else:
											lowerCamelCase__						:						Any						      =[{'''table''': table, '''query''': query}]

								for tqa_pipeline_input in tqa_pipeline_inputs:
											if not isinstance(tqa_pipeline_input['''table'''],    pd.DataFrame	):
														if tqa_pipeline_input["table"] is None:
																	raise ValueError('''Table cannot be None.'''	)

														lowerCamelCase__						:						int						      =pd.DataFrame(tqa_pipeline_input['''table''']	)

								return tqa_pipeline_inputs





@add_end_docstrings(lowerCAmelCase_  )
class 			__SCREAMING_SNAKE_CASE      ( lowerCAmelCase_  ):


					'''simple docstring'''





					_a       =				'table,query'



					def __init__(			self				:    Optional[int],    lowerCamelCase				:    Dict=TableQuestionAnsweringArgumentHandler(),    *lowerCamelCase				:    int,    **lowerCamelCase				:    Tuple	)->   Dict:
								super().__init__(*lowerCamelCase,    **lowerCamelCase	)
								lowerCamelCase__						:						Optional[Any]						      =args_parser

								self.check_model_type(
								    dict(
								        TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items()
								        + TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()	)
								    if self.framework == '''tf'''
								    else dict(
								        MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items() + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()	)	)

								lowerCamelCase__						:						Tuple						      =bool(getattr(self.model.config,    '''aggregation_labels''',    lowerCamelCase	)	) and bool(
								    getattr(self.model.config,    '''num_aggregation_labels''',    lowerCamelCase	)	)
								lowerCamelCase__						:						int						      ='''tapas''' if hasattr(self.model.config,    '''aggregation_labels'''	) else None



					def 						snake_case			(			self				:    List[str],    **lowerCamelCase				:    Optional[Any]	)->   Optional[Any]:
								return self.model(**lowerCamelCase	)



					def 						snake_case			(			self				:    Tuple,    **lowerCamelCase				:    List[Any]	)->   Dict:
								if self.framework == "pt":
											lowerCamelCase__						:						Optional[Any]						      =[]
											lowerCamelCase__						:						List[str]						      =[]
											lowerCamelCase__						:						Union[str, Any]						      =None
											lowerCamelCase__						:						Any						      =inputs['''input_ids'''].shape[0]

											lowerCamelCase__						:						int						      =inputs['''input_ids'''].to(self.device	)
											lowerCamelCase__						:						str						      =inputs['''attention_mask'''].to(self.device	)
											lowerCamelCase__						:						Union[str, Any]						      =inputs['''token_type_ids'''].to(self.device	)
											lowerCamelCase__						:						int						      =None

											for index in range(lowerCamelCase	):
														# If sequences have already been processed, the token type IDs will be created according to the previous
														# answer.
														if prev_answers is not None:
																	lowerCamelCase__						:						Union[str, Any]						      =token_type_ids_example[:, 3]  # shape (seq_len,)
																	lowerCamelCase__						:						List[Any]						      =np.zeros_like(prev_labels_example.cpu().numpy()	)  # shape (seq_len,)

																	lowerCamelCase__						:						List[str]						      =token_type_ids[index]  # shape (seq_len, 7)
																	for i in range(model_labels.shape[0]	):
																				lowerCamelCase__						:						List[Any]						      =token_type_ids_example[:, 0].tolist()[i]
																				lowerCamelCase__						:						int						      =token_type_ids_example[:, 1].tolist()[i] - 1
																				lowerCamelCase__						:						str						      =token_type_ids_example[:, 2].tolist()[i] - 1

																				if row_id >= 0 and col_id >= 0 and segment_id == 1:
																							lowerCamelCase__						:						Optional[int]						      =int(prev_answers[(col_id, row_id)]	)

																	lowerCamelCase__						:						Optional[Any]						      =torch.from_numpy(lowerCamelCase	).type(torch.long	).to(self.device	)

														lowerCamelCase__						:						Union[str, Any]						      =input_ids[index]
														lowerCamelCase__						:						str						      =attention_mask[index]  # shape (seq_len,)
														lowerCamelCase__						:						str						      =token_type_ids[index]  # shape (seq_len, 7)
														lowerCamelCase__						:						List[Any]						      =self.model(
														    input_ids=input_ids_example.unsqueeze(0	),    attention_mask=attention_mask_example.unsqueeze(0	),    token_type_ids=token_type_ids_example.unsqueeze(0	),    )
														lowerCamelCase__						:						List[str]						      =outputs.logits

														if self.aggregate:
																	all_aggregations.append(outputs.logits_aggregation	)

														all_logits.append(lowerCamelCase	)

														lowerCamelCase__						:						Any						      =torch.distributions.Bernoulli(logits=lowerCamelCase	)
														lowerCamelCase__						:						str						      =dist_per_token.probs * attention_mask_example.type(torch.floataa	).to(
														    dist_per_token.probs.device	)

														lowerCamelCase__						:						List[Any]						      =collections.defaultdict(lowerCamelCase	)
														for i, p in enumerate(probabilities.squeeze().tolist()	):
																	lowerCamelCase__						:						int						      =token_type_ids_example[:, 0].tolist()[i]
																	lowerCamelCase__						:						Any						      =token_type_ids_example[:, 1].tolist()[i] - 1
																	lowerCamelCase__						:						Tuple						      =token_type_ids_example[:, 2].tolist()[i] - 1
																	if col >= 0 and row >= 0 and segment_id == 1:
																				coords_to_probs[(col, row)].append(lowerCamelCase	)

														lowerCamelCase__						:						int						      ={key: np.array(coords_to_probs[key]	).mean() > 0.5 for key in coords_to_probs}

											lowerCamelCase__						:						List[str]						      =torch.cat(tuple(lowerCamelCase	),    0	)

											return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(lowerCamelCase	),    0	))
								else:
											lowerCamelCase__						:						Union[str, Any]						      =[]
											lowerCamelCase__						:						int						      =[]
											lowerCamelCase__						:						Tuple						      =None
											lowerCamelCase__						:						int						      =inputs['''input_ids'''].shape[0]

											lowerCamelCase__						:						List[str]						      =inputs['''input_ids''']
											lowerCamelCase__						:						Optional[int]						      =inputs['''attention_mask''']
											lowerCamelCase__						:						Any						      =inputs['''token_type_ids'''].numpy()
											lowerCamelCase__						:						Dict						      =None

											for index in range(lowerCamelCase	):
														# If sequences have already been processed, the token type IDs will be created according to the previous
														# answer.
														if prev_answers is not None:
																	lowerCamelCase__						:						Optional[int]						      =token_type_ids_example[:, 3]  # shape (seq_len,)
																	lowerCamelCase__						:						Optional[Any]						      =np.zeros_like(lowerCamelCase,    dtype=np.intaa	)  # shape (seq_len,)

																	lowerCamelCase__						:						List[Any]						      =token_type_ids[index]  # shape (seq_len, 7)
																	for i in range(model_labels.shape[0]	):
																				lowerCamelCase__						:						Any						      =token_type_ids_example[:, 0].tolist()[i]
																				lowerCamelCase__						:						Any						      =token_type_ids_example[:, 1].tolist()[i] - 1
																				lowerCamelCase__						:						Tuple						      =token_type_ids_example[:, 2].tolist()[i] - 1

																				if row_id >= 0 and col_id >= 0 and segment_id == 1:
																							lowerCamelCase__						:						Dict						      =int(prev_answers[(col_id, row_id)]	)

																	lowerCamelCase__						:						List[Any]						      =model_labels

														lowerCamelCase__						:						Any						      =input_ids[index]
														lowerCamelCase__						:						str						      =attention_mask[index]  # shape (seq_len,)
														lowerCamelCase__						:						Optional[Any]						      =token_type_ids[index]  # shape (seq_len, 7)
														lowerCamelCase__						:						List[Any]						      =self.model(
														    input_ids=np.expand_dims(lowerCamelCase,    axis=0	),    attention_mask=np.expand_dims(lowerCamelCase,    axis=0	),    token_type_ids=np.expand_dims(lowerCamelCase,    axis=0	),    )
														lowerCamelCase__						:						Optional[Any]						      =outputs.logits

														if self.aggregate:
																	all_aggregations.append(outputs.logits_aggregation	)

														all_logits.append(lowerCamelCase	)

														lowerCamelCase__						:						int						      =tfp.distributions.Bernoulli(logits=lowerCamelCase	)
														lowerCamelCase__						:						Tuple						      =dist_per_token.probs_parameter() * tf.cast(lowerCamelCase,    tf.floataa	)

														lowerCamelCase__						:						Optional[int]						      =collections.defaultdict(lowerCamelCase	)
														lowerCamelCase__						:						List[Any]						      =token_type_ids_example
														for i, p in enumerate(tf.squeeze(lowerCamelCase	).numpy().tolist()	):
																	lowerCamelCase__						:						Union[str, Any]						      =token_type_ids_example[:, 0].tolist()[i]
																	lowerCamelCase__						:						List[Any]						      =token_type_ids_example[:, 1].tolist()[i] - 1
																	lowerCamelCase__						:						Any						      =token_type_ids_example[:, 2].tolist()[i] - 1
																	if col >= 0 and row >= 0 and segment_id == 1:
																				coords_to_probs[(col, row)].append(lowerCamelCase	)

														lowerCamelCase__						:						List[Any]						      ={key: np.array(coords_to_probs[key]	).mean() > 0.5 for key in coords_to_probs}

											lowerCamelCase__						:						List[Any]						      =tf.concat(tuple(lowerCamelCase	),    0	)

											return (logits_batch,) if not self.aggregate else (logits_batch, tf.concat(tuple(lowerCamelCase	),    0	))



					def __call__(			self				:    Dict,    *lowerCamelCase				:    int,    **lowerCamelCase				:    str	)->   int:
								lowerCamelCase__						:						List[Any]						      =self._args_parser(*lowerCamelCase,    **lowerCamelCase	)

								lowerCamelCase__						:						Dict						      =super().__call__(lowerCamelCase,    **lowerCamelCase	)
								if len(lowerCamelCase	) == 1:
											return results[0]
								return results



					def 						snake_case			(			self				:    Dict,    lowerCamelCase				:    Dict=None,    lowerCamelCase				:    List[Any]=None,    lowerCamelCase				:    int=None,    **lowerCamelCase				:    int	)->   Optional[int]:
								lowerCamelCase__						:						Union[str, Any]						      ={}
								if padding is not None:
											lowerCamelCase__						:						Union[str, Any]						      =padding
								if truncation is not None:
											lowerCamelCase__						:						Any						      =truncation

								lowerCamelCase__						:						List[str]						      ={}
								if sequential is not None:
											lowerCamelCase__						:						int						      =sequential
								return preprocess_params, forward_params, {}



					def 						snake_case			(			self				:    Tuple,    lowerCamelCase				:    str,    lowerCamelCase				:    int=None,    lowerCamelCase				:    Tuple=True,    lowerCamelCase				:    Union[str, Any]=None	)->   List[str]:
								if truncation is None:
											if self.type == "tapas":
														lowerCamelCase__						:						Tuple						      ='''drop_rows_to_fit'''
											else:
														lowerCamelCase__						:						Union[str, Any]						      ='''do_not_truncate'''

								lowerCamelCase__      ,  lowerCamelCase__						:						List[str]						      =pipeline_input['''table'''], pipeline_input['''query''']
								if table.empty:
											raise ValueError('''table is empty'''	)
								if query is None or query == "":
											raise ValueError('''query is empty'''	)
								lowerCamelCase__						:						List[Any]						      =self.tokenizer(lowerCamelCase,    lowerCamelCase,    return_tensors=self.framework,    truncation=lowerCamelCase,    padding=lowerCamelCase	)
								lowerCamelCase__						:						Dict						      =table
								return inputs



					def 						snake_case			(			self				:    Tuple,    lowerCamelCase				:    Optional[int],    lowerCamelCase				:    Dict=False	)->   List[Any]:
								lowerCamelCase__						:						Union[str, Any]						      =model_inputs.pop('''table'''	)

								if self.type == "tapas":
											if sequential:
														lowerCamelCase__						:						List[Any]						      =self.sequential_inference(**lowerCamelCase	)
											else:
														lowerCamelCase__						:						List[Any]						      =self.batch_inference(**lowerCamelCase	)
								else:
											lowerCamelCase__						:						Union[str, Any]						      =self.model.generate(**lowerCamelCase	)
								lowerCamelCase__						:						Optional[int]						      ={'''model_inputs''': model_inputs, '''table''': table, '''outputs''': outputs}
								return model_outputs




					def 						snake_case			(			self				:    Optional[Any],    lowerCamelCase				:    Tuple	)->   int:
								lowerCamelCase__						:						List[str]						      =model_outputs['''model_inputs''']
								lowerCamelCase__						:						Any						      =model_outputs['''table''']
								lowerCamelCase__						:						List[Any]						      =model_outputs['''outputs''']
								if self.type == "tapas":
											if self.aggregate:
														lowerCamelCase__      ,  lowerCamelCase__						:						List[str]						      =outputs[:2]
														lowerCamelCase__						:						Dict						      =self.tokenizer.convert_logits_to_predictions(lowerCamelCase,    lowerCamelCase,    lowerCamelCase	)
														lowerCamelCase__      ,  lowerCamelCase__						:						Optional[int]						      =predictions
														lowerCamelCase__						:						Optional[Any]						      ={i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(lowerCamelCase	)}

														lowerCamelCase__						:						str						      =self.model.config.no_aggregation_label_index
														lowerCamelCase__						:						str						      ={
														    i: aggregators[i] + ''' > ''' for i, pred in enumerate(lowerCamelCase	) if pred != no_agg_label_index
														}
											else:
														lowerCamelCase__						:						Optional[int]						      =outputs[0]
														lowerCamelCase__						:						List[Any]						      =self.tokenizer.convert_logits_to_predictions(lowerCamelCase,    lowerCamelCase	)
														lowerCamelCase__						:						List[str]						      =predictions[0]
														lowerCamelCase__						:						Optional[Any]						      ={}
														lowerCamelCase__						:						str						      ={}
											lowerCamelCase__						:						Union[str, Any]						      =[]
											for index, coordinates in enumerate(lowerCamelCase	):
														lowerCamelCase__						:						str						      =[table.iat[coordinate] for coordinate in coordinates]
														lowerCamelCase__						:						List[str]						      =aggregators.get(lowerCamelCase,    ''''''	)
														lowerCamelCase__						:						Union[str, Any]						      =aggregators_prefix.get(lowerCamelCase,    ''''''	)
														lowerCamelCase__						:						Any						      ={
														    '''answer''': aggregator_prefix + ''', '''.join(lowerCamelCase	),
														    '''coordinates''': coordinates,
														    '''cells''': [table.iat[coordinate] for coordinate in coordinates],
														}
														if aggregator:
																	lowerCamelCase__						:						Optional[Any]						      =aggregator

														answers.append(lowerCamelCase	)
											if len(lowerCamelCase	) == 0:
														raise PipelineException('''Empty answer'''	)
								else:
											lowerCamelCase__						:						Any						      =[{'''answer''': answer} for answer in self.tokenizer.batch_decode(lowerCamelCase,    skip_special_tokens=lowerCamelCase	)]

								return answers if len(lowerCamelCase	) > 1 else answers[0]












'''simple docstring'''







import math

import torch
from torch import nn
from torch.nn import init

from .binarizer import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer




class 					__UpperCAmelCase (		nn.Linear	):

      '''simple docstring'''





      def __init__(					self  ,		_UpperCAmelCase  ,		_UpperCAmelCase  ,		_UpperCAmelCase = True  ,		_UpperCAmelCase = "constant"  ,		_UpperCAmelCase = 0.0  ,		_UpperCAmelCase = "topK"  ,		):
             super(_UpperCAmelCase  ,		self						).__init__(in_features=_UpperCAmelCase  ,		out_features=_UpperCAmelCase  ,		bias=_UpperCAmelCase						)
             assert pruning_method in ["topK", "threshold", "sigmoied_threshold", "magnitude", "l0"]
             UpperCAmelCase__	:   Union[str, Any]												=	pruning_method

             if self.pruning_method in ["topK", "threshold", "sigmoied_threshold", "l0"]:
                    UpperCAmelCase__	:   Union[str, Any]												=	mask_scale
                    UpperCAmelCase__	:   Optional[int]												=	mask_init
                    UpperCAmelCase__	:   List[str]												=	nn.Parameter(torch.empty(self.weight.size()						)						)
                    self.init_mask()




      def 							lowerCamelCase     (					self						):
             if self.mask_init == "constant":
                    init.constant_(self.mask_scores  ,		val=self.mask_scale						)
             elif self.mask_init == "uniform":
                    init.uniform_(self.mask_scores  ,		a=-self.mask_scale  ,		b=self.mask_scale						)
             elif self.mask_init == "kaiming":
                    init.kaiming_uniform_(self.mask_scores  ,		a=math.sqrt(5						)						)




      def 							lowerCamelCase     (					self  ,		_UpperCAmelCase  ,		_UpperCAmelCase						):
             # Get the mask
             if self.pruning_method == "topK":
                    UpperCAmelCase__	:   Union[str, Any]												=	TopKBinarizer.apply(self.mask_scores  ,		_UpperCAmelCase						)
             elif self.pruning_method in ["threshold", "sigmoied_threshold"]:
                    UpperCAmelCase__	:   List[Any]												=	'''sigmoied''' in self.pruning_method
                    UpperCAmelCase__	:   Tuple												=	ThresholdBinarizer.apply(self.mask_scores  ,		_UpperCAmelCase  ,		_UpperCAmelCase						)
             elif self.pruning_method == "magnitude":
                    UpperCAmelCase__	:   Optional[int]												=	MagnitudeBinarizer.apply(self.weight  ,		_UpperCAmelCase						)
             elif self.pruning_method == "l0":
                    UpperCAmelCase__							,							UpperCAmelCase__							,							UpperCAmelCase__	:   int												=	-0.1, 1.1, 2 / 3
                    if self.training:
                           UpperCAmelCase__	:   Optional[int]												=	torch.zeros_like(self.mask_scores						).uniform_().clamp(0.00_01  ,		0.99_99						)
                           UpperCAmelCase__	:   Tuple												=	torch.sigmoid((u.log() - (1 - u).log() + self.mask_scores) / b						)
                    else:
                           UpperCAmelCase__	:   Any												=	torch.sigmoid(self.mask_scores						)
                    UpperCAmelCase__	:   List[Any]												=	s * (r - l) + l
                    UpperCAmelCase__	:   Optional[int]												=	s_bar.clamp(min=0.0  ,		max=1.0						)
             # Mask weights with computed mask
             UpperCAmelCase__	:   Union[str, Any]												=	mask * self.weight
             # Compute output (linear layer) with masked weights
             return nn.functional.linear(_UpperCAmelCase  ,		_UpperCAmelCase  ,		self.bias						)