





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_   ):

       def __call__(						self:       Any    ,      _SCREAMING_SNAKE_CASE:       Union[str, Any]=None    ,      _SCREAMING_SNAKE_CASE:       Any=None    ,      **_SCREAMING_SNAKE_CASE:       str						)				->       int:




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

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

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

              return tqa_pipeline_inputs



@add_end_docstrings(lowerCAmelCase_   )
class 							_UpperCamelCase    (		lowerCAmelCase_   ):
       _UpperCamelCase :    Optional[Any]   			=      '''table,query'''

       def __init__(						self:       Dict    ,      _SCREAMING_SNAKE_CASE:       Union[str, Any]=TableQuestionAnsweringArgumentHandler()    ,      *_SCREAMING_SNAKE_CASE:       Dict    ,      **_SCREAMING_SNAKE_CASE:       Optional[int]						)				->       Optional[Any]:




              """simple docstring"""


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

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

       def lowercase       (						self:       Optional[Any]    ,      **_SCREAMING_SNAKE_CASE:       int						)				->       str:




              """simple docstring"""


              return self.model(**_SCREAMING_SNAKE_CASE						)

       def lowercase       (						self:       int    ,      **_SCREAMING_SNAKE_CASE:       Optional[Any]						)				->       Optional[Any]:




              """simple docstring"""


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

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

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

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

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

                                   UpperCamelCase_      				=     torch.from_numpy(_SCREAMING_SNAKE_CASE						).type(torch.long						).to(self.device						)

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

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

                            all_logits.append(_SCREAMING_SNAKE_CASE						)

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

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

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

                     UpperCamelCase_      				=     torch.cat(tuple(_SCREAMING_SNAKE_CASE						)    ,      0						)

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

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

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

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

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

                                   UpperCamelCase_      				=     model_labels

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

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

                            all_logits.append(_SCREAMING_SNAKE_CASE						)

                            UpperCamelCase_      				=     tfp.distributions.Bernoulli(logits=_SCREAMING_SNAKE_CASE						)
                            UpperCamelCase_      				=     dist_per_token.probs_parameter() * tf.cast(_SCREAMING_SNAKE_CASE    ,      tf.floataa						)

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

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

                     UpperCamelCase_      				=     tf.concat(tuple(_SCREAMING_SNAKE_CASE						)    ,      0						)

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

       def __call__(						self:       Optional[Any]    ,      *_SCREAMING_SNAKE_CASE:       Dict    ,      **_SCREAMING_SNAKE_CASE:       int						)				->       Dict:




              """simple docstring"""


              UpperCamelCase_      				=     self._args_parser(*_SCREAMING_SNAKE_CASE    ,      **_SCREAMING_SNAKE_CASE						)

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

       def lowercase       (						self:       Tuple    ,      _SCREAMING_SNAKE_CASE:       List[Any]=None    ,      _SCREAMING_SNAKE_CASE:       List[str]=None    ,      _SCREAMING_SNAKE_CASE:       Any=None    ,      **_SCREAMING_SNAKE_CASE:       Union[str, Any]						)				->       List[str]:




              """simple docstring"""


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

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

       def lowercase       (						self:       Any    ,      _SCREAMING_SNAKE_CASE:       Any    ,      _SCREAMING_SNAKE_CASE:       Tuple=None    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=True    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=None						)				->       List[str]:




              """simple docstring"""


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

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

       def lowercase       (						self:       Any    ,      _SCREAMING_SNAKE_CASE:       Optional[int]    ,      _SCREAMING_SNAKE_CASE:       List[Any]=False						)				->       Tuple:




              """simple docstring"""


              UpperCamelCase_      				=     model_inputs.pop("table"						)

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



       def lowercase       (						self:       Union[str, Any]    ,      _SCREAMING_SNAKE_CASE:       Dict						)				->       str:




              """simple docstring"""


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

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

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

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








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


_UpperCAmelCase				       = logging.get_logger(__name__)

_UpperCAmelCase				       = {
    'MCG-NJU/videomae-base': 'https://huggingface.co/MCG-NJU/videomae-base/resolve/main/config.json',
}



class 							_UpperCamelCase    (		lowerCAmelCase_   ):
       _UpperCamelCase :    List[Any]   			=      '''videomae'''

       def __init__(						self:       Optional[Any]    ,      _SCREAMING_SNAKE_CASE:       List[str]=224    ,      _SCREAMING_SNAKE_CASE:       Dict=16    ,      _SCREAMING_SNAKE_CASE:       List[Any]=3    ,      _SCREAMING_SNAKE_CASE:       Tuple=16    ,      _SCREAMING_SNAKE_CASE:       Tuple=2    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=768    ,      _SCREAMING_SNAKE_CASE:       str=12    ,      _SCREAMING_SNAKE_CASE:       List[Any]=12    ,      _SCREAMING_SNAKE_CASE:       Dict=3072    ,      _SCREAMING_SNAKE_CASE:       Tuple="gelu"    ,      _SCREAMING_SNAKE_CASE:       List[Any]=0.0    ,      _SCREAMING_SNAKE_CASE:       List[Any]=0.0    ,      _SCREAMING_SNAKE_CASE:       Tuple=0.02    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=1e-12    ,      _SCREAMING_SNAKE_CASE:       Dict=True    ,      _SCREAMING_SNAKE_CASE:       Dict=True    ,      _SCREAMING_SNAKE_CASE:       List[str]=6    ,      _SCREAMING_SNAKE_CASE:       Dict=384    ,      _SCREAMING_SNAKE_CASE:       Any=4    ,      _SCREAMING_SNAKE_CASE:       Dict=1536    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=True    ,      **_SCREAMING_SNAKE_CASE:       str    ,      )				->       Dict:




              """simple docstring"""


              super().__init__(**_SCREAMING_SNAKE_CASE						)

              UpperCamelCase_      				=     image_size
              UpperCamelCase_      				=     patch_size
              UpperCamelCase_      				=     num_channels
              UpperCamelCase_      				=     num_frames
              UpperCamelCase_      				=     tubelet_size

              UpperCamelCase_      				=     hidden_size
              UpperCamelCase_      				=     num_hidden_layers
              UpperCamelCase_      				=     num_attention_heads
              UpperCamelCase_      				=     intermediate_size
              UpperCamelCase_      				=     hidden_act
              UpperCamelCase_      				=     hidden_dropout_prob
              UpperCamelCase_      				=     attention_probs_dropout_prob
              UpperCamelCase_      				=     initializer_range
              UpperCamelCase_      				=     layer_norm_eps
              UpperCamelCase_      				=     qkv_bias
              UpperCamelCase_      				=     use_mean_pooling

              UpperCamelCase_      				=     decoder_num_attention_heads
              UpperCamelCase_      				=     decoder_hidden_size
              UpperCamelCase_      				=     decoder_num_hidden_layers
              UpperCamelCase_      				=     decoder_intermediate_size
              UpperCamelCase_      				=     norm_pix_loss

