forked from pytorch/translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonnx_full_export.py
74 lines (64 loc) · 1.91 KB
/
onnx_full_export.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
import argparse
from pytorch_translate import rnn # noqa
from pytorch_translate.ensemble_export import BeamSearch
def main():
parser = argparse.ArgumentParser(
description=("Export PyTorch-trained FBTranslate models to caffe2")
)
parser.add_argument(
"--checkpoint",
action="append",
nargs="+",
help="PyTorch checkpoint file (at least one required)",
)
parser.add_argument(
"--output_file",
default="",
help="File name to which to save beam search network",
)
parser.add_argument(
"--src_dict",
required=True,
help="File encoding PyTorch dictionary for source language",
)
parser.add_argument(
"--dst_dict",
required=True,
help="File encoding PyTorch dictionary for source language",
)
parser.add_argument(
"--beam_size",
type=int,
default=6,
help="Number of top candidates returned by each decoder step",
)
parser.add_argument(
"--word_reward",
type=float,
default=0.0,
help="Value to add for each word (besides EOS)",
)
parser.add_argument(
"--unk_reward",
type=float,
default=0.0,
help="Value to add for each word UNK token",
)
args = parser.parse_args()
if args.output_file == "":
print("No action taken. Need output_file to be specified.")
parser.print_help()
return
checkpoint_filenames = [arg[0] for arg in args.checkpoint]
beam_search = BeamSearch.build_from_checkpoints(
checkpoint_filenames=checkpoint_filenames,
src_dict_filename=args.src_dict,
dst_dict_filename=args.dst_dict,
beam_size=args.beam_size,
word_reward=args.word_reward,
unk_reward=args.unk_reward,
)
beam_search.save_to_db(args.output_file)
if __name__ == "__main__":
main()