


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     __lowerCamelCase     (  snake_case_     ):


							"""simple docstring"""

							def __call__(  self						,       UpperCAmelCase=None						,       UpperCAmelCase=None						,       **UpperCAmelCase  )     ->       Tuple:






								'''simple docstring'''






								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(UpperCAmelCase						,       UpperCAmelCase  ) and table.get("query"  ) is not None and table.get("table"  ) is not None:
										lowercase_			 = [table]
									elif isinstance(UpperCAmelCase						,       UpperCAmelCase  ) and len(UpperCAmelCase  ) > 0:
										if not all(isinstance(UpperCAmelCase						,       UpperCAmelCase  ) for d in table  ):
											raise ValueError(
											    F'Keyword argument `table` should be a list of dict, but is {(type(UpperCAmelCase  ) for d in table)}'  )

										if table[0].get("query"  ) is not None and table[0].get("table"  ) is not None:
											lowercase_			 = 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(UpperCAmelCase						,       UpperCAmelCase  ) or isinstance(UpperCAmelCase						,       types.GeneratorType  ):
										return table
									else:
										raise ValueError(
										    "Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but "
										    F'is {type(UpperCAmelCase  )})'  )
								else:
									lowercase_			 = [{"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."  )

										lowercase_			 = pd.DataFrame(tqa_pipeline_input["table"]  )

								return tqa_pipeline_inputs







@add_end_docstrings(snake_case_     )
class     __lowerCamelCase     (  snake_case_     ):


							"""simple docstring"""

							lowerCAmelCase__          =				"table,query"




							def __init__(  self						,       UpperCAmelCase=TableQuestionAnsweringArgumentHandler()						,       *UpperCAmelCase						,       **UpperCAmelCase  )     ->       Optional[int]:






								'''simple docstring'''






								super().__init__(*UpperCAmelCase						,       **UpperCAmelCase  )
								lowercase_			 = 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()  )  )

								lowercase_			 = bool(getattr(self.model.config						,       "aggregation_labels"						,       UpperCAmelCase  )  ) and bool(
								    getattr(self.model.config						,       "num_aggregation_labels"						,       UpperCAmelCase  )  )
								lowercase_			 = "tapas" if hasattr(self.model.config						,       "aggregation_labels"  ) else None




							def 				A__      (  self						,       **UpperCAmelCase  )     ->       Dict:






								'''simple docstring'''






								return self.model(**UpperCAmelCase  )




							def 				A__      (  self						,       **UpperCAmelCase  )     ->       Any:






								'''simple docstring'''






								if self.framework == "pt":
									lowercase_			 = []
									lowercase_			 = []
									lowercase_			 = None
									lowercase_			 = inputs["input_ids"].shape[0]

									lowercase_			 = inputs["input_ids"].to(self.device  )
									lowercase_			 = inputs["attention_mask"].to(self.device  )
									lowercase_			 = inputs["token_type_ids"].to(self.device  )
									lowercase_			 = None

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

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

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

											lowercase_			 = torch.from_numpy(UpperCAmelCase  ).type(torch.long  ).to(self.device  )

										lowercase_			 = input_ids[index]
										lowercase_			 = attention_mask[index]  # shape (seq_len,)
										lowercase_			 = token_type_ids[index]  # shape (seq_len, 7)
										lowercase_			 = 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  )						,       )
										lowercase_			 = outputs.logits

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

										all_logits.append(UpperCAmelCase  )

										lowercase_			 = torch.distributions.Bernoulli(logits=UpperCAmelCase  )
										lowercase_			 = dist_per_token.probs * attention_mask_example.type(torch.floataa  ).to(
										    dist_per_token.probs.device  )

										lowercase_			 = collections.defaultdict(UpperCAmelCase  )
										for i, p in enumerate(probabilities.squeeze().tolist()  ):
											lowercase_			 = token_type_ids_example[:, 0].tolist()[i]
											lowercase_			 = token_type_ids_example[:, 1].tolist()[i] - 1
											lowercase_			 = token_type_ids_example[:, 2].tolist()[i] - 1
											if col >= 0 and row >= 0 and segment_id == 1:
												coords_to_probs[(col, row)].append(UpperCAmelCase  )

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

									lowercase_			 = torch.cat(tuple(UpperCAmelCase  )						,       0  )

									return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(UpperCAmelCase  )						,       0  ))
								else:
									lowercase_			 = []
									lowercase_			 = []
									lowercase_			 = None
									lowercase_			 = inputs["input_ids"].shape[0]

									lowercase_			 = inputs["input_ids"]
									lowercase_			 = inputs["attention_mask"]
									lowercase_			 = inputs["token_type_ids"].numpy()
									lowercase_			 = None

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

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

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

											lowercase_			 = model_labels

										lowercase_			 = input_ids[index]
										lowercase_			 = attention_mask[index]  # shape (seq_len,)
										lowercase_			 = token_type_ids[index]  # shape (seq_len, 7)
										lowercase_			 = self.model(
										    input_ids=np.expand_dims(UpperCAmelCase						,       axis=0  )						,       attention_mask=np.expand_dims(UpperCAmelCase						,       axis=0  )						,       token_type_ids=np.expand_dims(UpperCAmelCase						,       axis=0  )						,       )
										lowercase_			 = outputs.logits

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

										all_logits.append(UpperCAmelCase  )

										lowercase_			 = tfp.distributions.Bernoulli(logits=UpperCAmelCase  )
										lowercase_			 = dist_per_token.probs_parameter() * tf.cast(UpperCAmelCase						,       tf.floataa  )

										lowercase_			 = collections.defaultdict(UpperCAmelCase  )
										lowercase_			 = token_type_ids_example
										for i, p in enumerate(tf.squeeze(UpperCAmelCase  ).numpy().tolist()  ):
											lowercase_			 = token_type_ids_example[:, 0].tolist()[i]
											lowercase_			 = token_type_ids_example[:, 1].tolist()[i] - 1
											lowercase_			 = token_type_ids_example[:, 2].tolist()[i] - 1
											if col >= 0 and row >= 0 and segment_id == 1:
												coords_to_probs[(col, row)].append(UpperCAmelCase  )

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

									lowercase_			 = tf.concat(tuple(UpperCAmelCase  )						,       0  )

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




							def __call__(  self						,       *UpperCAmelCase						,       **UpperCAmelCase  )     ->       Optional[Any]:






								'''simple docstring'''






								lowercase_			 = self._args_parser(*UpperCAmelCase						,       **UpperCAmelCase  )

								lowercase_			 = super().__call__(UpperCAmelCase						,       **UpperCAmelCase  )
								if len(UpperCAmelCase  ) == 1:
									return results[0]
								return results




							def 				A__      (  self						,       UpperCAmelCase=None						,       UpperCAmelCase=None						,       UpperCAmelCase=None						,       **UpperCAmelCase  )     ->       Optional[int]:






								'''simple docstring'''






								lowercase_			 = {}
								if padding is not None:
									lowercase_			 = padding
								if truncation is not None:
									lowercase_			 = truncation

								lowercase_			 = {}
								if sequential is not None:
									lowercase_			 = sequential
								return preprocess_params, forward_params, {}




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase=None						,       UpperCAmelCase=True						,       UpperCAmelCase=None  )     ->       Optional[Any]:






								'''simple docstring'''






								if truncation is None:
									if self.type == "tapas":
										lowercase_			 = "drop_rows_to_fit"
									else:
										lowercase_			 = "do_not_truncate"

								lowercase_ ,	lowercase_			 = 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"  )
								lowercase_			 = self.tokenizer(UpperCAmelCase						,       UpperCAmelCase						,       return_tensors=self.framework						,       truncation=UpperCAmelCase						,       padding=UpperCAmelCase  )
								lowercase_			 = table
								return inputs




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase=False  )     ->       List[Any]:






								'''simple docstring'''






								lowercase_			 = model_inputs.pop("table"  )

								if self.type == "tapas":
									if sequential:
										lowercase_			 = self.sequential_inference(**UpperCAmelCase  )
									else:
										lowercase_			 = self.batch_inference(**UpperCAmelCase  )
								else:
									lowercase_			 = self.model.generate(**UpperCAmelCase  )
								lowercase_			 = {"model_inputs": model_inputs, "table": table, "outputs": outputs}
								return model_outputs




							def 				A__      (  self						,       UpperCAmelCase  )     ->       int:






								'''simple docstring'''






								lowercase_			 = model_outputs["model_inputs"]
								lowercase_			 = model_outputs["table"]
								lowercase_			 = model_outputs["outputs"]
								if self.type == "tapas":
									if self.aggregate:
										lowercase_ ,	lowercase_			 = outputs[:2]
										lowercase_			 = self.tokenizer.convert_logits_to_predictions(UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase  )
										lowercase_ ,	lowercase_			 = predictions
										lowercase_			 = {i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(UpperCAmelCase  )}

										lowercase_			 = self.model.config.no_aggregation_label_index
										lowercase_			 = {
										    i: aggregators[i] + " > " for i, pred in enumerate(UpperCAmelCase  ) if pred != no_agg_label_index
										}
									else:
										lowercase_			 = outputs[0]
										lowercase_			 = self.tokenizer.convert_logits_to_predictions(UpperCAmelCase						,       UpperCAmelCase  )
										lowercase_			 = predictions[0]
										lowercase_			 = {}
										lowercase_			 = {}
									lowercase_			 = []
									for index, coordinates in enumerate(UpperCAmelCase  ):
										lowercase_			 = [table.iat[coordinate] for coordinate in coordinates]
										lowercase_			 = aggregators.get(UpperCAmelCase						,       ""  )
										lowercase_			 = aggregators_prefix.get(UpperCAmelCase						,       ""  )
										lowercase_			 = {
										    "answer": aggregator_prefix + ", ".join(UpperCAmelCase  ),
										    "coordinates": coordinates,
										    "cells": [table.iat[coordinate] for coordinate in coordinates],
										}
										if aggregator:
											lowercase_			 = aggregator

										answers.append(UpperCAmelCase  )
									if len(UpperCAmelCase  ) == 0:
										raise PipelineException("Empty answer"  )
								else:
									lowercase_			 = [{"answer": answer} for answer in self.tokenizer.batch_decode(UpperCAmelCase						,       skip_special_tokens=UpperCAmelCase  )]

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





from typing import Callable, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE__       			=				logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__       			=				{
    """microsoft/prophetnet-large-uncased""": (
        """https://huggingface.co/microsoft/prophetnet-large-uncased/resolve/main/config.json"""
    ),
}







class     __lowerCamelCase     (  snake_case_     ):


							"""simple docstring"""

							lowerCAmelCase__          =				"prophetnet"
							lowerCAmelCase__          =				["past_key_values"]
							lowerCAmelCase__          =				{
							    "num_attention_heads": "num_encoder_attention_heads",
							}




							def __init__(  self						,       UpperCAmelCase = 0.1						,       UpperCAmelCase = "gelu"						,       UpperCAmelCase = 30522						,       UpperCAmelCase = 1024						,       UpperCAmelCase = 4096						,       UpperCAmelCase = 12						,       UpperCAmelCase = 16						,       UpperCAmelCase = 4096						,       UpperCAmelCase = 12						,       UpperCAmelCase = 16						,       UpperCAmelCase = 0.1						,       UpperCAmelCase = 0.1						,       UpperCAmelCase = 512						,       UpperCAmelCase = 0.02						,       UpperCAmelCase = True						,       UpperCAmelCase = True						,       UpperCAmelCase = 0						,       UpperCAmelCase = 2						,       UpperCAmelCase = 32						,       UpperCAmelCase = 128						,       UpperCAmelCase = False						,       UpperCAmelCase = 0.0						,       UpperCAmelCase = True						,       UpperCAmelCase = 0						,       UpperCAmelCase = 1						,       UpperCAmelCase = 2						,       **UpperCAmelCase						,       )     ->       Dict:






								'''simple docstring'''






								lowercase_			 = vocab_size
								lowercase_			 = hidden_size
								lowercase_			 = encoder_ffn_dim
								lowercase_			 = num_encoder_layers
								lowercase_			 = num_encoder_attention_heads
								lowercase_			 = decoder_ffn_dim
								lowercase_			 = num_decoder_layers
								lowercase_			 = num_decoder_attention_heads
								lowercase_			 = max_position_embeddings
								lowercase_			 = init_std  # Normal(0, this parameter)
								lowercase_			 = activation_function

								# parameters for prophetnet
								lowercase_			 = ngram
								lowercase_			 = num_buckets
								lowercase_			 = relative_max_distance
								lowercase_			 = disable_ngram_loss
								lowercase_			 = eps

								# 3 Types of Dropout
								lowercase_			 = attention_dropout
								lowercase_			 = activation_dropout
								lowercase_			 = dropout

								lowercase_			 = use_cache

								super().__init__(
								    pad_token_id=UpperCAmelCase						,       bos_token_id=UpperCAmelCase						,       eos_token_id=UpperCAmelCase						,       is_encoder_decoder=UpperCAmelCase						,       add_cross_attention=UpperCAmelCase						,       decoder_start_token_id=UpperCAmelCase						,       **UpperCAmelCase						,       )




							@property
							def 				A__      (  self  )     ->       int:






								'''simple docstring'''






								return self.num_encoder_layers + self.num_decoder_layers




							@num_hidden_layers.setter
							def 				A__      (  self						,       UpperCAmelCase  )     ->       Any:






								'''simple docstring'''






								raise NotImplementedError(
								    "This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and"
								    " `num_decoder_layers`."  )

