





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 					__A   (  a			):


				def __call__(						self      ,       UpperCAmelCase_=None      ,       UpperCAmelCase_=None      ,       **UpperCAmelCase_							):
								# 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		       =[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		       =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		       =[{"""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		       =pd.DataFrame(tqa_pipeline_input["""table"""]							)

								return tqa_pipeline_inputs




@add_end_docstrings(a			)
class 					__A   (  a			):
				__A            = """table,query"""


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


				def _snake_case				(						self      ,       **UpperCAmelCase_							):
								return self.model(**UpperCAmelCase_							)


				def _snake_case				(						self      ,       **UpperCAmelCase_							):
								if self.framework == "pt":
												lowerCamelCase		       =[]
												lowerCamelCase		       =[]
												lowerCamelCase		       =None
												lowerCamelCase		       =inputs["""input_ids"""].shape[0]

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

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

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

																				lowerCamelCase		       =torch.from_numpy(UpperCAmelCase_							).type(torch.long							).to(self.device							)

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

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

																all_logits.append(UpperCAmelCase_							)

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

																lowerCamelCase		       =collections.defaultdict(UpperCAmelCase_							)
																for i, p in enumerate(probabilities.squeeze().tolist()							):
																				lowerCamelCase		       =token_type_ids_example[:, 0].tolist()[i]
																				lowerCamelCase		       =token_type_ids_example[:, 1].tolist()[i] - 1
																				lowerCamelCase		       =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		       ={key: np.array(coords_to_probs[key]							).mean() > 0.5 for key in coords_to_probs}

												lowerCamelCase		       =torch.cat(tuple(UpperCAmelCase_							)      ,       0							)

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

												lowerCamelCase		       =inputs["""input_ids"""]
												lowerCamelCase		       =inputs["""attention_mask"""]
												lowerCamelCase		       =inputs["""token_type_ids"""].numpy()
												lowerCamelCase		       =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		       =token_type_ids_example[:, 3]  # shape (seq_len,)
																				lowerCamelCase		       =np.zeros_like(UpperCAmelCase_      ,       dtype=np.intaa							)  # shape (seq_len,)

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

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

																				lowerCamelCase		       =model_labels

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

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

																all_logits.append(UpperCAmelCase_							)

																lowerCamelCase		       =tfp.distributions.Bernoulli(logits=UpperCAmelCase_							)
																lowerCamelCase		       =dist_per_token.probs_parameter() * tf.cast(UpperCAmelCase_      ,       tf.floataa							)

																lowerCamelCase		       =collections.defaultdict(UpperCAmelCase_							)
																lowerCamelCase		       =token_type_ids_example
																for i, p in enumerate(tf.squeeze(UpperCAmelCase_							).numpy().tolist()							):
																				lowerCamelCase		       =token_type_ids_example[:, 0].tolist()[i]
																				lowerCamelCase		       =token_type_ids_example[:, 1].tolist()[i] - 1
																				lowerCamelCase		       =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		       ={key: np.array(coords_to_probs[key]							).mean() > 0.5 for key in coords_to_probs}

												lowerCamelCase		       =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_							):
								lowerCamelCase		       =self._args_parser(*UpperCAmelCase_      ,       **UpperCAmelCase_							)

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


				def _snake_case				(						self      ,       UpperCAmelCase_=None      ,       UpperCAmelCase_=None      ,       UpperCAmelCase_=None      ,       **UpperCAmelCase_							):
								lowerCamelCase		       ={}
								if padding is not None:
												lowerCamelCase		       =padding
								if truncation is not None:
												lowerCamelCase		       =truncation

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


				def _snake_case				(						self      ,       UpperCAmelCase_      ,       UpperCAmelCase_=None      ,       UpperCAmelCase_=True      ,       UpperCAmelCase_=None							):
								if truncation is None:
												if self.type == "tapas":
																lowerCamelCase		       ="""drop_rows_to_fit"""
												else:
																lowerCamelCase		       ="""do_not_truncate"""

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


				def _snake_case				(						self      ,       UpperCAmelCase_      ,       UpperCAmelCase_=False							):
								lowerCamelCase		       =model_inputs.pop("""table"""							)

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


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

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

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

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






import argparse

import torch
from flax.training.checkpoints import restore_checkpoint

from transformers import FNetConfig, FNetForPreTraining
from transformers.utils import logging


logging.set_verbosity_info()






def       __lowerCamelCase		(						lowerCAmelCase__	,  lowerCAmelCase__	,  lowerCAmelCase__	):
       # Initialise PyTorch model
       lowerCAmelCase__     	=					FNetConfig.from_json_file(lowerCAmelCase__	)
       print(F"""Building PyTorch model from configuration: {config}"""	)
       lowerCAmelCase__     	=					FNetForPreTraining(lowerCAmelCase__	)

       lowerCAmelCase__     	=					restore_checkpoint(lowerCAmelCase__	,  lowerCAmelCase__	)
       lowerCAmelCase__     	=					checkpoint_dict['target']

       # Embeddings
       # Position IDs
       lowerCAmelCase__     	=					fnet_pretraining_model.state_dict()

       lowerCAmelCase__     	=					state_dict['fnet.embeddings.position_ids']
       lowerCAmelCase__     	=					{'fnet.embeddings.position_ids': position_ids}
       # Embedding Layers
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['encoder']['embedder']['word']['embedding']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['encoder']['embedder']['position']['embedding'][0]	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['encoder']['embedder']['type']['embedding']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['encoder']['embedder']['hidden_mapping_in']['kernel']	).T
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['encoder']['embedder']['hidden_mapping_in']['bias']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['encoder']['embedder']['layer_norm']['scale']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['encoder']['embedder']['layer_norm']['bias']	)

       # Encoder Layers
       for layer in range(config.num_hidden_layers	):
              lowerCAmelCase__     	=					torch.tensor(
                  pretrained_model_params['encoder'][F"""encoder_{layer}"""]['mixing_layer_norm']['scale']	)
              lowerCAmelCase__     	=					torch.tensor(
                  pretrained_model_params['encoder'][F"""encoder_{layer}"""]['mixing_layer_norm']['bias']	)

              lowerCAmelCase__     	=					torch.tensor(
                  pretrained_model_params['encoder'][F"""feed_forward_{layer}"""]['intermediate']['kernel']	).T
              lowerCAmelCase__     	=					torch.tensor(
                  pretrained_model_params['encoder'][F"""feed_forward_{layer}"""]['intermediate']['bias']	)

              lowerCAmelCase__     	=					torch.tensor(
                  pretrained_model_params['encoder'][F"""feed_forward_{layer}"""]['output']['kernel']	).T
              lowerCAmelCase__     	=					torch.tensor(
                  pretrained_model_params['encoder'][F"""feed_forward_{layer}"""]['output']['bias']	)

              lowerCAmelCase__     	=					torch.tensor(
                  pretrained_model_params['encoder'][F"""encoder_{layer}"""]['output_layer_norm']['scale']	)
              lowerCAmelCase__     	=					torch.tensor(
                  pretrained_model_params['encoder'][F"""encoder_{layer}"""]['output_layer_norm']['bias']	)

       # Pooler Layers
       lowerCAmelCase__     	=					torch.tensor(pretrained_model_params['encoder']['pooler']['kernel']	).T
       lowerCAmelCase__     	=					torch.tensor(pretrained_model_params['encoder']['pooler']['bias']	)

       # Masked LM Layers
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['predictions_dense']['kernel']	).T
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['predictions_dense']['bias']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['predictions_layer_norm']['scale']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['predictions_layer_norm']['bias']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['encoder']['embedder']['word']['embedding']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['predictions_output']['output_bias']	)
       lowerCAmelCase__     	=					torch.tensor(pretrained_model_params['predictions_output']['output_bias']	)

       # Seq Relationship Layers
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['classification']['output_kernel']	)
       lowerCAmelCase__     	=					torch.tensor(
           pretrained_model_params['classification']['output_bias']	)

       # Load State Dict
       fnet_pretraining_model.load_state_dict(lowerCAmelCase__	)

       # Save PreTrained
       print(F"""Saving pretrained model to {save_path}"""	)
       fnet_pretraining_model.save_pretrained(lowerCAmelCase__	)


if __name__ == "__main__":
    lowerCAmelCase__							= argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        '--flax_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
    )
    parser.add_argument(
        '--fnet_config_file',
        default=None,
        type=str,
        required=True,
        help=(
            'The config json file corresponding to the pre-trained FNet model. \n'
            'This specifies the model architecture.'
        ),
    )
    parser.add_argument('--save_path', default=None, type=str, required=True, help='Path to the output model.')
    lowerCAmelCase__							= parser.parse_args()
    convert_flax_checkpoint_to_pytorch(args.flax_checkpoint_path, args.fnet_config_file, args.save_path)
