-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_pt.py
55 lines (47 loc) · 1.58 KB
/
convert_to_pt.py
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
import argparse
import collections
import json
import os
import shutil
import numpy as np
from transformers import AutoModel, AutoModelForMaskedLM
TOKENIZER_FILES_NAMES = [
"added_tokens.json",
"tokenizer_config.json",
"tokenizer.json",
"spm.model",
"special_tokens_map.json",
]
def convert_tf_ckpt(args):
print("Converting TF checkpoint to PT checkpoint")
print("Loading TF checkpoint from {}".format(args.tf_checkpoint_dir))
if args.is_mlm:
print("Loading MLM model")
model = AutoModelForMaskedLM.from_pretrained(
args.tf_checkpoint_dir, from_tf=True
)
else:
print("Loading Non-MLM model")
model = AutoModel.from_pretrained(
args.tf_checkpoint_dir,
from_tf=True,
)
output_dir = (
args.pt_checkpoint_dir
if args.pt_checkpoint_dir
else os.path.join(args.tf_checkpoint_dir, "pytorch_model")
)
print("Saving PT checkpoint to {}".format(output_dir))
model.save_pretrained(output_dir)
for files in os.listdir(args.tf_checkpoint_dir):
if files in TOKENIZER_FILES_NAMES:
print("Copying {}".format(files))
shutil.copy(os.path.join(args.tf_checkpoint_dir, files), output_dir)
if __name__ == "__main__":
# Parse essential args
parser = argparse.ArgumentParser()
parser.add_argument("--tf_checkpoint_dir", required=True, type=str)
parser.add_argument("--is_mlm", type=bool, default=False)
parser.add_argument("--pt_checkpoint_dir", type=str)
args = parser.parse_args()
convert_tf_ckpt(args)