code: 
  
  
  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 (_a	):
  
  
  
  
  
  
  
         def __call__( self:   str,A_:   Dict=None,A_:   int=None,**A_:   Optional[Any]				):
  
  
  
  
  
  
              '''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(A_,A_				) and table.get('query'				) is not None and table.get('table'				) is not None:
                        __UpperCamelCase   = [table]
                   elif isinstance(A_,A_				) and len(A_				) > 0:
                        if not all(isinstance(A_,A_				) for d in table				):
                             raise ValueError(
                                 F'''Keyword argument `table` should be a list of dict, but is {(type(A_				) 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(A_,A_				) or isinstance(A_,types.GeneratorType				):
                        return table
                   else:
                        raise ValueError(
                            'Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but '
                            F'''is {type(A_				)})'''				)
              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(_a	)
  class   __lowerCamelCase (_a	):
         _lowercase       							=    """table,query"""
  
  
  
  
  
  
  
         def __init__( self:   str,A_:   Union[str, Any]=TableQuestionAnsweringArgumentHandler(),*A_:   List[Any],**A_:   Optional[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              super().__init__(*A_,**A_				)
              __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',A_				)				) and bool(
                  getattr(self.model.config,'num_aggregation_labels',A_				)				)
              __UpperCamelCase   = 'tapas' if hasattr(self.model.config,'aggregation_labels'				) else None
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[Any],**A_:   int				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return self.model(**A_				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   int,**A_:   str				):
  
  
  
  
  
  
              '''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(A_				):
                        # 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(A_				).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(A_				)
  
                        __UpperCamelCase   = torch.distributions.Bernoulli(logits=A_				)
                        __UpperCamelCase   = dist_per_token.probs * attention_mask_example.type(torch.floataa				).to(
                            dist_per_token.probs.device				)
  
                        __UpperCamelCase   = collections.defaultdict(A_				)
                        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(A_				)
  
                        __UpperCamelCase   = {key: np.array(coords_to_probs[key]				).mean() > 0.5 for key in coords_to_probs}
  
                   __UpperCamelCase   = torch.cat(tuple(A_				),0				)
  
                   return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(A_				),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(A_				):
                        # 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(A_,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(A_,axis=0				),attention_mask=np.expand_dims(A_,axis=0				),token_type_ids=np.expand_dims(A_,axis=0				),)
                        __UpperCamelCase   = outputs.logits
  
                        if self.aggregate:
                             all_aggregations.append(outputs.logits_aggregation				)
  
                        all_logits.append(A_				)
  
                        __UpperCamelCase   = tfp.distributions.Bernoulli(logits=A_				)
                        __UpperCamelCase   = dist_per_token.probs_parameter() * tf.cast(A_,tf.floataa				)
  
                        __UpperCamelCase   = collections.defaultdict(A_				)
                        __UpperCamelCase   = token_type_ids_example
                        for i, p in enumerate(tf.squeeze(A_				).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(A_				)
  
                        __UpperCamelCase   = {key: np.array(coords_to_probs[key]				).mean() > 0.5 for key in coords_to_probs}
  
                   __UpperCamelCase   = tf.concat(tuple(A_				),0				)
  
                   return (logits_batch,) if not self.aggregate else (logits_batch, tf.concat(tuple(A_				),0				))
  
  
  
  
  
  
  
         def __call__( self:   Any,*A_:   int,**A_:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self._args_parser(*A_,**A_				)
  
              __UpperCamelCase   = super().__call__(A_,**A_				)
              if len(A_				) == 1:
                   return results[0]
              return results
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[str],A_:   str=None,A_:   str=None,A_:   int=None,**A_:   Optional[Any]				):
  
  
  
  
  
  
              '''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 		snake_case_  ( self:   int,A_:   Dict,A_:   Tuple=None,A_:   List[Any]=True,A_:   List[str]=None				):
  
  
  
  
  
  
              '''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(A_,A_,return_tensors=self.framework,truncation=A_,padding=A_				)
              __UpperCamelCase   = table
              return inputs
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   int,A_:   Optional[int],A_:   Union[str, Any]=False				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = model_inputs.pop('table'				)
  
              if self.type == "tapas":
                   if sequential:
                        __UpperCamelCase   = self.sequential_inference(**A_				)
                   else:
                        __UpperCamelCase   = self.batch_inference(**A_				)
              else:
                   __UpperCamelCase   = self.model.generate(**A_				)
              __UpperCamelCase   = {'model_inputs': model_inputs, 'table': table, 'outputs': outputs}
              return model_outputs
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any,A_:   Tuple				):
  
  
  
  
  
  
              '''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(A_,A_,A_				)
                        __UpperCamelCase,						__UpperCamelCase   = predictions
                        __UpperCamelCase   = {i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(A_				)}
  
                        __UpperCamelCase   = self.model.config.no_aggregation_label_index
                        __UpperCamelCase   = {
                            i: aggregators[i] + ' > ' for i, pred in enumerate(A_				) if pred != no_agg_label_index
                        }
                   else:
                        __UpperCamelCase   = outputs[0]
                        __UpperCamelCase   = self.tokenizer.convert_logits_to_predictions(A_,A_				)
                        __UpperCamelCase   = predictions[0]
                        __UpperCamelCase   = {}
                        __UpperCamelCase   = {}
                   __UpperCamelCase   = []
                   for index, coordinates in enumerate(A_				):
                        __UpperCamelCase   = [table.iat[coordinate] for coordinate in coordinates]
                        __UpperCamelCase   = aggregators.get(A_,''				)
                        __UpperCamelCase   = aggregators_prefix.get(A_,''				)
                        __UpperCamelCase   = {
                            'answer': aggregator_prefix + ', '.join(A_				),
                            'coordinates': coordinates,
                            'cells': [table.iat[coordinate] for coordinate in coordinates],
                        }
                        if aggregator:
                             __UpperCamelCase   = aggregator
  
                        answers.append(A_				)
                   if len(A_				) == 0:
                        raise PipelineException('Empty answer'				)
              else:
                   __UpperCamelCase   = [{'answer': answer} for answer in self.tokenizer.batch_decode(A_,skip_special_tokens=A_				)]
  
              return answers if len(A_				) > 1 else answers[0]
  
  
  
style_context: 
  
  
  import argparse
  import json
  
  from transformers import GPTNeoConfig, GPTNeoForCausalLM, load_tf_weights_in_gpt_neo
  from transformers.utils import logging
  
  
  logging.set_verbosity_info()
  
  
  
  
  
  
  def 		_A		(		_lowercase				,     _lowercase				,     _lowercase   )				->   List[str]:
  
  
       """simple docstring"""
  
  
       __UpperCamelCase   = json.load(open(_lowercase				,     'r'   )   )
       __UpperCamelCase   = GPTNeoConfig(
           hidden_size=config_json['n_embd']				,     num_layers=config_json['n_layer']				,     num_heads=config_json['n_head']				,     attention_types=config_json['attention_types']				,     max_position_embeddings=config_json['n_positions']				,     resid_dropout=config_json['res_dropout']				,     embed_dropout=config_json['embed_dropout']				,     attention_dropout=config_json['attn_dropout']				,     )
       print(f'''Building PyTorch model from configuration: {config}'''   )
       __UpperCamelCase   = GPTNeoForCausalLM(_lowercase   )
  
       # Load weights from tf checkpoint
       load_tf_weights_in_gpt_neo(_lowercase				,     _lowercase				,     _lowercase   )
  
       # Save pytorch-model
       print(f'''Save PyTorch model to {pytorch_dump_path}'''   )
       model.save_pretrained(_lowercase   )
  
  
  if __name__ == "__main__":
      __snake_case             =	argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
      )
      parser.add_argument(
          '''--config_file''',
          default=None,
          type=str,
          required=True,
          help=(
              '''The config json file corresponding to the pre-trained mesh-tf model. \n'''
              '''This specifies the model architecture.'''
          ),
      )
      parser.add_argument(
          '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
      )
      __snake_case             =	parser.parse_args()
      convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
  
  
  