File size: 1,678 Bytes
4321e7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import os
import sys
import argparse
import torch
import shutil
# Append the utils module path
sys.path.append("../")
from models import PLTNUM
def parse_args():
"""
Parse command line arguments.
"""
parser = argparse.ArgumentParser(
description="Convert the model implemented with nn.Module to a model implemented with transformers' PreTrainedModel."
)
parser.add_argument(
"--model_path",
type=str,
help="The path to a model weight which you want to convert.",
)
parser.add_argument(
"--config_and_tokenizer_path",
type=str,
help="The path to a config and tokenizer of the model which you want to convert.",
)
parser.add_argument(
"--model",
type=str,
help="The name of the base model of the finetuned model",
)
parser.add_argument(
"--output_dir",
type=str,
default="./",
help="Directory to save the prediction.",
)
parser.add_argument(
"--task",
type=str,
default="classification",
)
return parser.parse_args()
if __name__ == "__main__":
config = parse_args()
if not os.path.exists(config.output_dir):
os.makedirs(config.output_dir)
model = PLTNUM(config)
model.load_state_dict(torch.load(config.model_path, map_location="cpu"))
torch.save(model.state_dict(), os.path.join(config.output_dir, "pytorch_model.bin"))
for filename in ["config.json", "special_tokens_map.json", "tokenizer_config.json", "vocab.txt"]:
shutil.copy(os.path.join(config.config_and_tokenizer_path, filename), os.path.join(config.output_dir, filename)) |