codingutf8 2020 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license convert tapas checkpoint import argparse from transformers import tapasconfig tapasformaskedlm tapasforquestionanswering tapasforsequenceclassification tapasmodel tapastokenizer loadtfweightsintapas from transformers utils import logging logging setverbosityinfo def converttfcheckpointtopytorch task resetpositionindexpercell tfcheckpointpath tapasconfigfile pytorchdumppath initialise pytorch model if you want to convert a checkpoint that uses absolute position embeddings make sure to set resetpositionindexpercell of tapasconfig to false initialize configuration from json file config tapasconfig fromjsonfiletapasconfigfile set absoluterelative position embeddings parameter config resetpositionindexpercell resetpositionindexpercell set remaining parameters of tapasconfig as well as the model based on the task if task sqa model tapasforquestionansweringconfigconfig elif task wtq runtaskmain py hparams config numaggregationlabels 4 config useanswerassupervision true hparamutils py hparams config answerlosscutoff 0 664694 config cellselectionpreference 0 207951 config huberlossdelta 0 121194 config initcellselectionweightstozero true config selectonecolumn true config allowemptycolumnselection false config temperature 0 0352513 model tapasforquestionansweringconfigconfig elif task wikisqlsupervised runtaskmain py hparams config numaggregationlabels 4 config useanswerassupervision false hparamutils py hparams config answerlosscutoff 36 4519 config cellselectionpreference 0 903421 config huberlossdelta 222 088 config initcellselectionweightstozero true config selectonecolumn true config allowemptycolumnselection true config temperature 0 763141 model tapasforquestionansweringconfigconfig elif task tabfact model tapasforsequenceclassificationconfigconfig elif task mlm model tapasformaskedlmconfigconfig elif task intermediatepretraining model tapasmodelconfigconfig else raise valueerrorftask task not supported printfbuilding pytorch model from configuration config load weights from tf checkpoint loadtfweightsintapasmodel config tfcheckpointpath save pytorchmodel weights and configuration printfsave pytorch model to pytorchdumppath model savepretrainedpytorchdumppath save tokenizer files printfsave tokenizer files to pytorchdumppath tokenizer tapastokenizervocabfiletfcheckpointpath 10 vocab txt modelmaxlength512 tokenizer savepretrainedpytorchdumppath printused relative position embeddings model config resetpositionindexpercell if name main parser argparse argumentparser required parameters parser addargument task defaultsqa typestr helpmodel task for which to convert a checkpoint defaults to sqa parser addargument resetpositionindexpercell defaultfalse actionstoretrue helpwhether to use relative position embeddings or not defaults to true parser addargument tfcheckpointpath defaultnone typestr requiredtrue helppath to the tensorflow checkpoint path parser addargument tapasconfigfile defaultnone typestr requiredtrue help the config json file corresponding to the pretrained tapas model n this specifies the model architecture parser addargument pytorchdumppath defaultnone typestr requiredtrue helppath to the output pytorch model args parser parseargs converttfcheckpointtopytorch args task args resetpositionindexpercell args tfcheckpointpath args tapasconfigfile args pytorchdumppath coding utf 8 2020 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license convert tapas checkpoint initialise pytorch model if you want to convert a checkpoint that uses absolute position embeddings make sure to set reset_position_index_per_cell of tapasconfig to false initialize configuration from json file set absolute relative position embeddings parameter set remaining parameters of tapasconfig as well as the model based on the task run_task_main py hparams hparam_utils py hparams run_task_main py hparams hparam_utils py hparams load weights from tf checkpoint save pytorch model weights and configuration save tokenizer files required parameters
import argparse

from transformers import (
    TapasConfig,
    TapasForMaskedLM,
    TapasForQuestionAnswering,
    TapasForSequenceClassification,
    TapasModel,
    TapasTokenizer,
    load_tf_weights_in_tapas,
)
from transformers.utils import logging


logging.set_verbosity_info()


def convert_tf_checkpoint_to_pytorch(
    task, reset_position_index_per_cell, tf_checkpoint_path, tapas_config_file, pytorch_dump_path
):
    
    
    

    
    config = TapasConfig.from_json_file(tapas_config_file)
    
    config.reset_position_index_per_cell = reset_position_index_per_cell

    
    if task == "SQA":
        model = TapasForQuestionAnswering(config=config)
    elif task == "WTQ":
        
        config.num_aggregation_labels = 4
        config.use_answer_as_supervision = True
        
        config.answer_loss_cutoff = 0.664694
        config.cell_selection_preference = 0.207951
        config.huber_loss_delta = 0.121194
        config.init_cell_selection_weights_to_zero = True
        config.select_one_column = True
        config.allow_empty_column_selection = False
        config.temperature = 0.0352513

        model = TapasForQuestionAnswering(config=config)
    elif task == "WIKISQL_SUPERVISED":
        
        config.num_aggregation_labels = 4
        config.use_answer_as_supervision = False
        
        config.answer_loss_cutoff = 36.4519
        config.cell_selection_preference = 0.903421
        config.huber_loss_delta = 222.088
        config.init_cell_selection_weights_to_zero = True
        config.select_one_column = True
        config.allow_empty_column_selection = True
        config.temperature = 0.763141

        model = TapasForQuestionAnswering(config=config)
    elif task == "TABFACT":
        model = TapasForSequenceClassification(config=config)
    elif task == "MLM":
        model = TapasForMaskedLM(config=config)
    elif task == "INTERMEDIATE_PRETRAINING":
        model = TapasModel(config=config)
    else:
        raise ValueError(f"Task {task} not supported.")

    print(f"Building PyTorch model from configuration: {config}")
    
    load_tf_weights_in_tapas(model, config, tf_checkpoint_path)

    
    print(f"Save PyTorch model to {pytorch_dump_path}")
    model.save_pretrained(pytorch_dump_path)

    
    print(f"Save tokenizer files to {pytorch_dump_path}")
    tokenizer = TapasTokenizer(vocab_file=tf_checkpoint_path[:-10] + "vocab.txt", model_max_length=512)
    tokenizer.save_pretrained(pytorch_dump_path)

    print("Used relative position embeddings:", model.config.reset_position_index_per_cell)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    
    parser.add_argument(
        "--task", default="SQA", type=str, help="Model task for which to convert a checkpoint. Defaults to SQA."
    )
    parser.add_argument(
        "--reset_position_index_per_cell",
        default=False,
        action="store_true",
        help="Whether to use relative position embeddings or not. Defaults to True.",
    )
    parser.add_argument(
        "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
    )
    parser.add_argument(
        "--tapas_config_file",
        default=None,
        type=str,
        required=True,
        help=(
            "The config json file corresponding to the pre-trained TAPAS 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."
    )
    args = parser.parse_args()
    convert_tf_checkpoint_to_pytorch(
        args.task,
        args.reset_position_index_per_cell,
        args.tf_checkpoint_path,
        args.tapas_config_file,
        args.pytorch_dump_path,
    )