




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


SCREAMING_SNAKE_CASE__ :   List[str]						=     logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__ :   Any						=     {
    'openbmb/cpm-ant-10b': 'https://huggingface.co/openbmb/cpm-ant-10b/blob/main/config.json'
    # See all CPMAnt models at https://huggingface.co/models?filter=cpmant
}




class 	UpperCamelCase__			(UpperCamelCase__  ):

    '''simple docstring'''



    lowerCamelCase_   :			List[Any]			  =   """cpmant"""







    def __init__(					self  ,						UpperCamelCase__ = 3_0720  ,						UpperCamelCase__ = 4096  ,						UpperCamelCase__ = 32  ,						UpperCamelCase__ = 128  ,						UpperCamelCase__ = 1_0240  ,						UpperCamelCase__ = 48  ,						UpperCamelCase__ = 0.0  ,						UpperCamelCase__ = 512  ,						UpperCamelCase__ = 2048  ,						UpperCamelCase__ = 1e-6  ,						UpperCamelCase__ = 1.0  ,						UpperCamelCase__ = 32  ,						UpperCamelCase__ = 32  ,						UpperCamelCase__ = 32  ,						UpperCamelCase__ = True  ,						**UpperCamelCase__  ,						)  ->   Union[str, Any]:
           super().__init__(**snake_case_  )
           lowerCamelCase     :   Tuple    				=      prompt_types
           lowerCamelCase     :   Tuple    				=      prompt_length
           lowerCamelCase     :   Optional[Any]    				=      segment_types
           lowerCamelCase     :   Tuple    				=      hidden_size
           lowerCamelCase     :   Optional[Any]    				=      num_attention_heads
           lowerCamelCase     :   str    				=      dim_head
           lowerCamelCase     :   Tuple    				=      dim_ff
           lowerCamelCase     :   Any    				=      num_hidden_layers
           lowerCamelCase     :   Tuple    				=      position_bias_num_buckets
           lowerCamelCase     :   Tuple    				=      position_bias_max_distance
           lowerCamelCase     :   Optional[int]    				=      dropout_p
           lowerCamelCase     :   int    				=      eps
           lowerCamelCase     :   Any    				=      use_cache
           lowerCamelCase     :   Optional[int]    				=      vocab_size
           lowerCamelCase     :   List[Any]    				=      init_std












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 	UpperCamelCase__			(lowerCAmelCase__  ):

    '''simple docstring'''



    def __call__(					self  ,						UpperCamelCase__=None  ,						UpperCamelCase__=None  ,						**UpperCamelCase__  )  ->   List[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(UpperCamelCase__  ,						UpperCamelCase__  ) and table.get("query"  ) is not None and table.get("table"  ) is not None:
                         lowerCamelCase     :   Optional[int]    				=      [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:
                                lowerCamelCase     :   int    				=      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:
                  lowerCamelCase     :   Tuple    				=      [{"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     :   Optional[Any]    				=      pd.DataFrame(tqa_pipeline_input["table"]  )

           return tqa_pipeline_inputs




@add_end_docstrings(lowerCAmelCase__  )
class 	UpperCamelCase__			(lowerCAmelCase__  ):

    '''simple docstring'''



    lowerCamelCase_   :			Optional[Any]			  =   """table,query"""







    def __init__(					self  ,						UpperCamelCase__=TableQuestionAnsweringArgumentHandler()  ,						*UpperCamelCase__  ,						**UpperCamelCase__  )  ->   Dict:
           super().__init__(*UpperCamelCase__  ,						**UpperCamelCase__  )
           lowerCamelCase     :   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     :   Any    				=      bool(getattr(self.model.config  ,						"aggregation_labels"  ,						UpperCamelCase__  )  ) and bool(
               getattr(self.model.config  ,						"num_aggregation_labels"  ,						UpperCamelCase__  )  )
           lowerCamelCase     :   List[Any]    				=      "tapas" if hasattr(self.model.config  ,						"aggregation_labels"  ) else None







    def 					_lowercase  (					self  ,						**UpperCamelCase__  )  ->   int:
           return self.model(**UpperCamelCase__  )







    def 					_lowercase  (					self  ,						**UpperCamelCase__  )  ->   Union[str, Any]:
           if self.framework == "pt":
                  lowerCamelCase     :   List[str]    				=      []
                  lowerCamelCase     :   List[Any]    				=      []
                  lowerCamelCase     :   List[Any]    				=      None
                  lowerCamelCase     :   List[str]    				=      inputs["input_ids"].shape[0]

                  lowerCamelCase     :   Optional[int]    				=      inputs["input_ids"].to(self.device  )
                  lowerCamelCase     :   List[str]    				=      inputs["attention_mask"].to(self.device  )
                  lowerCamelCase     :   str    				=      inputs["token_type_ids"].to(self.device  )
                  lowerCamelCase     :   str    				=      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:
                                lowerCamelCase     :   List[Any]    				=      token_type_ids_example[:, 3]  # shape (seq_len,)
                                lowerCamelCase     :   Optional[int]    				=      np.zeros_like(prev_labels_example.cpu().numpy()  )  # shape (seq_len,)

                                lowerCamelCase     :   Optional[Any]    				=      token_type_ids[index]  # shape (seq_len, 7)
                                for i in range(model_labels.shape[0]  ):
                                       lowerCamelCase     :   Tuple    				=      token_type_ids_example[:, 0].tolist()[i]
                                       lowerCamelCase     :   Union[str, Any]    				=      token_type_ids_example[:, 1].tolist()[i] - 1
                                       lowerCamelCase     :   Optional[int]    				=      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     :   Dict    				=      torch.from_numpy(UpperCamelCase__  ).type(torch.long  ).to(self.device  )

                         lowerCamelCase     :   int    				=      input_ids[index]
                         lowerCamelCase     :   Union[str, Any]    				=      attention_mask[index]  # shape (seq_len,)
                         lowerCamelCase     :   Dict    				=      token_type_ids[index]  # shape (seq_len, 7)
                         lowerCamelCase     :   Dict    				=      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     :   str    				=      outputs.logits

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

                         all_logits.append(UpperCamelCase__  )

                         lowerCamelCase     :   int    				=      torch.distributions.Bernoulli(logits=UpperCamelCase__  )
                         lowerCamelCase     :   List[Any]    				=      dist_per_token.probs * attention_mask_example.type(torch.floataa  ).to(
                             dist_per_token.probs.device  )

                         lowerCamelCase     :   Optional[int]    				=      collections.defaultdict(UpperCamelCase__  )
                         for i, p in enumerate(probabilities.squeeze().tolist()  ):
                                lowerCamelCase     :   Optional[int]    				=      token_type_ids_example[:, 0].tolist()[i]
                                lowerCamelCase     :   List[Any]    				=      token_type_ids_example[:, 1].tolist()[i] - 1
                                lowerCamelCase     :   str    				=      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__  )

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

                  lowerCamelCase     :   Optional[Any]    				=      torch.cat(tuple(UpperCamelCase__  )  ,						0  )

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

                  lowerCamelCase     :   Dict    				=      inputs["input_ids"]
                  lowerCamelCase     :   Tuple    				=      inputs["attention_mask"]
                  lowerCamelCase     :   Optional[int]    				=      inputs["token_type_ids"].numpy()
                  lowerCamelCase     :   Union[str, Any]    				=      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:
                                lowerCamelCase     :   Tuple    				=      token_type_ids_example[:, 3]  # shape (seq_len,)
                                lowerCamelCase     :   Optional[Any]    				=      np.zeros_like(UpperCamelCase__  ,						dtype=np.intaa  )  # shape (seq_len,)

                                lowerCamelCase     :   Optional[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     :   int    				=      token_type_ids_example[:, 1].tolist()[i] - 1
                                       lowerCamelCase     :   Union[str, Any]    				=      token_type_ids_example[:, 2].tolist()[i] - 1

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

                                lowerCamelCase     :   Any    				=      model_labels

                         lowerCamelCase     :   Optional[int]    				=      input_ids[index]
                         lowerCamelCase     :   Optional[Any]    				=      attention_mask[index]  # shape (seq_len,)
                         lowerCamelCase     :   Optional[int]    				=      token_type_ids[index]  # shape (seq_len, 7)
                         lowerCamelCase     :   Union[str, Any]    				=      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  )  ,						)
                         lowerCamelCase     :   Dict    				=      outputs.logits

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

                         all_logits.append(UpperCamelCase__  )

                         lowerCamelCase     :   Optional[int]    				=      tfp.distributions.Bernoulli(logits=UpperCamelCase__  )
                         lowerCamelCase     :   Any    				=      dist_per_token.probs_parameter() * tf.cast(UpperCamelCase__  ,						tf.floataa  )

                         lowerCamelCase     :   List[Any]    				=      collections.defaultdict(UpperCamelCase__  )
                         lowerCamelCase     :   Optional[int]    				=      token_type_ids_example
                         for i, p in enumerate(tf.squeeze(UpperCamelCase__  ).numpy().tolist()  ):
                                lowerCamelCase     :   Union[str, Any]    				=      token_type_ids_example[:, 0].tolist()[i]
                                lowerCamelCase     :   Any    				=      token_type_ids_example[:, 1].tolist()[i] - 1
                                lowerCamelCase     :   List[str]    				=      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__  )

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

                  lowerCamelCase     :   int    				=      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__  )  ->   int:
           lowerCamelCase     :   Union[str, Any]    				=      self._args_parser(*UpperCamelCase__  ,						**UpperCamelCase__  )

           lowerCamelCase     :   Optional[int]    				=      super().__call__(UpperCamelCase__  ,						**UpperCamelCase__  )
           if len(UpperCamelCase__  ) == 1:
                  return results[0]
           return results







    def 					_lowercase  (					self  ,						UpperCamelCase__=None  ,						UpperCamelCase__=None  ,						UpperCamelCase__=None  ,						**UpperCamelCase__  )  ->   Optional[int]:
           lowerCamelCase     :   int    				=      {}
           if padding is not None:
                  lowerCamelCase     :   Dict    				=      padding
           if truncation is not None:
                  lowerCamelCase     :   Tuple    				=      truncation

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







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__=None  ,						UpperCamelCase__=True  ,						UpperCamelCase__=None  )  ->   str:
           if truncation is None:
                  if self.type == "tapas":
                         lowerCamelCase     :   Optional[Any]    				=      "drop_rows_to_fit"
                  else:
                         lowerCamelCase     :   str    				=      "do_not_truncate"

           lowerCamelCase				,   lowerCamelCase     :   Tuple    				=      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     :   Any    				=      self.tokenizer(UpperCamelCase__  ,						UpperCamelCase__  ,						return_tensors=self.framework  ,						truncation=UpperCamelCase__  ,						padding=UpperCamelCase__  )
           lowerCamelCase     :   str    				=      table
           return inputs







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__=False  )  ->   Dict:
           lowerCamelCase     :   Union[str, Any]    				=      model_inputs.pop("table"  )

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







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

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

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

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






