__main__.py 6.17 KB
Newer Older
thomwolf's avatar
thomwolf committed
1
# coding: utf8
2
def main():
thomwolf's avatar
thomwolf committed
3
    import sys
4
    if (len(sys.argv) < 4 or len(sys.argv) > 6) or sys.argv[1] not in ["bert", "gpt", "transfo_xl", "gpt2", "xlnet"]:
thomwolf's avatar
thomwolf committed
5
        print(
thomwolf's avatar
thomwolf committed
6
        "Should be used as one of: \n"
thomwolf's avatar
thomwolf committed
7
8
9
10
11
        ">> `pytorch_transformers bert TF_CHECKPOINT TF_CONFIG PYTORCH_DUMP_OUTPUT`, \n"
        ">> `pytorch_transformers gpt OPENAI_GPT_CHECKPOINT_FOLDER_PATH PYTORCH_DUMP_OUTPUT [OPENAI_GPT_CONFIG]`, \n"
        ">> `pytorch_transformers transfo_xl TF_CHECKPOINT_OR_DATASET PYTORCH_DUMP_OUTPUT [TF_CONFIG]` or \n"
        ">> `pytorch_transformers gpt2 TF_CHECKPOINT PYTORCH_DUMP_OUTPUT [GPT2_CONFIG]` or \n"
        ">> `pytorch_transformers xlnet TF_CHECKPOINT TF_CONFIG PYTORCH_DUMP_OUTPUT [FINETUNING_TASK_NAME]`")
thomwolf's avatar
thomwolf committed
12
    else:
13
        if sys.argv[1] == "bert":
thomwolf's avatar
thomwolf committed
14
            try:
thomwolf's avatar
thomwolf committed
15
                from .convert_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
thomwolf's avatar
thomwolf committed
16
            except ImportError:
thomwolf's avatar
thomwolf committed
17
                print("pytorch_transformers can only be used from the commandline to convert TensorFlow models in PyTorch, "
thomwolf's avatar
thomwolf committed
18
19
20
21
22
23
                    "In that case, it requires TensorFlow to be installed. Please see "
                    "https://www.tensorflow.org/install/ for installation instructions.")
                raise

            if len(sys.argv) != 5:
                # pylint: disable=line-too-long
thomwolf's avatar
thomwolf committed
24
                print("Should be used as `pytorch_transformers bert TF_CHECKPOINT TF_CONFIG PYTORCH_DUMP_OUTPUT`")
thomwolf's avatar
thomwolf committed
25
26
27
28
29
            else:
                PYTORCH_DUMP_OUTPUT = sys.argv.pop()
                TF_CONFIG = sys.argv.pop()
                TF_CHECKPOINT = sys.argv.pop()
                convert_tf_checkpoint_to_pytorch(TF_CHECKPOINT, TF_CONFIG, PYTORCH_DUMP_OUTPUT)
30
        elif sys.argv[1] == "gpt":
thomwolf's avatar
thomwolf committed
31
            from .convert_openai_checkpoint_to_pytorch import convert_openai_checkpoint_to_pytorch
32
33
            if len(sys.argv) < 4 or len(sys.argv) > 5:
                # pylint: disable=line-too-long
thomwolf's avatar
thomwolf committed
34
                print("Should be used as `pytorch_transformers gpt OPENAI_GPT_CHECKPOINT_FOLDER_PATH PYTORCH_DUMP_OUTPUT [OPENAI_GPT_CONFIG]`")
thomwolf's avatar
thomwolf committed
35
            else:
36
37
38
39
40
41
42
43
44
45
                OPENAI_GPT_CHECKPOINT_FOLDER_PATH = sys.argv[2]
                PYTORCH_DUMP_OUTPUT = sys.argv[3]
                if len(sys.argv) == 5:
                    OPENAI_GPT_CONFIG = sys.argv[4]
                else:
                    OPENAI_GPT_CONFIG = ""
                convert_openai_checkpoint_to_pytorch(OPENAI_GPT_CHECKPOINT_FOLDER_PATH,
                                                    OPENAI_GPT_CONFIG,
                                                    PYTORCH_DUMP_OUTPUT)
        elif sys.argv[1] == "transfo_xl":
thomwolf's avatar
thomwolf committed
46
            try:
thomwolf's avatar
thomwolf committed
47
                from .convert_transfo_xl_checkpoint_to_pytorch import convert_transfo_xl_checkpoint_to_pytorch
thomwolf's avatar
thomwolf committed
48
            except ImportError:
thomwolf's avatar
thomwolf committed
49
                print("pytorch_transformers can only be used from the commandline to convert TensorFlow models in PyTorch, "
thomwolf's avatar
thomwolf committed
50
51
52
                    "In that case, it requires TensorFlow to be installed. Please see "
                    "https://www.tensorflow.org/install/ for installation instructions.")
                raise
53
54
            if len(sys.argv) < 4 or len(sys.argv) > 5:
                # pylint: disable=line-too-long
thomwolf's avatar
thomwolf committed
55
                print("Should be used as `pytorch_transformers transfo_xl TF_CHECKPOINT/TF_DATASET_FILE PYTORCH_DUMP_OUTPUT [TF_CONFIG]`")
thomwolf's avatar
thomwolf committed
56
            else:
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                if 'ckpt' in sys.argv[2].lower():
                    TF_CHECKPOINT = sys.argv[2]
                    TF_DATASET_FILE = ""
                else:
                    TF_DATASET_FILE = sys.argv[2]
                    TF_CHECKPOINT = ""
                PYTORCH_DUMP_OUTPUT = sys.argv[3]
                if len(sys.argv) == 5:
                    TF_CONFIG = sys.argv[4]
                else:
                    TF_CONFIG = ""
                convert_transfo_xl_checkpoint_to_pytorch(TF_CHECKPOINT, TF_CONFIG, PYTORCH_DUMP_OUTPUT, TF_DATASET_FILE)
        elif sys.argv[1] == "gpt2":
            try:
                from .convert_gpt2_checkpoint_to_pytorch import convert_gpt2_checkpoint_to_pytorch
            except ImportError:
thomwolf's avatar
thomwolf committed
73
                print("pytorch_transformers can only be used from the commandline to convert TensorFlow models in PyTorch, "
74
75
76
77
78
79
                    "In that case, it requires TensorFlow to be installed. Please see "
                    "https://www.tensorflow.org/install/ for installation instructions.")
                raise

            if len(sys.argv) < 4 or len(sys.argv) > 5:
                # pylint: disable=line-too-long
thomwolf's avatar
thomwolf committed
80
                print("Should be used as `pytorch_transformers gpt2 TF_CHECKPOINT PYTORCH_DUMP_OUTPUT [TF_CONFIG]`")
thomwolf's avatar
thomwolf committed
81
            else:
82
83
84
85
86
87
88
                TF_CHECKPOINT = sys.argv[2]
                PYTORCH_DUMP_OUTPUT = sys.argv[3]
                if len(sys.argv) == 5:
                    TF_CONFIG = sys.argv[4]
                else:
                    TF_CONFIG = ""
                convert_gpt2_checkpoint_to_pytorch(TF_CHECKPOINT, TF_CONFIG, PYTORCH_DUMP_OUTPUT)
thomwolf's avatar
thomwolf committed
89
90
        else:
            try:
91
                from .convert_xlnet_checkpoint_to_pytorch import convert_xlnet_checkpoint_to_pytorch
thomwolf's avatar
thomwolf committed
92
            except ImportError:
thomwolf's avatar
thomwolf committed
93
                print("pytorch_transformers can only be used from the commandline to convert TensorFlow models in PyTorch, "
thomwolf's avatar
thomwolf committed
94
95
96
97
                    "In that case, it requires TensorFlow to be installed. Please see "
                    "https://www.tensorflow.org/install/ for installation instructions.")
                raise

98
99
            if len(sys.argv) < 5 or len(sys.argv) > 6:
                # pylint: disable=line-too-long
thomwolf's avatar
thomwolf committed
100
                print("Should be used as `pytorch_transformers xlnet TF_CHECKPOINT TF_CONFIG PYTORCH_DUMP_OUTPUT [FINETUNING_TASK_NAME]`")
thomwolf's avatar
thomwolf committed
101
            else:
102
103
104
105
106
107
108
109
110
111
112
                TF_CHECKPOINT = sys.argv[2]
                TF_CONFIG = sys.argv[3]
                PYTORCH_DUMP_OUTPUT = sys.argv[4]
                if len(sys.argv) == 6:
                    FINETUNING_TASK = sys.argv[5]

                convert_xlnet_checkpoint_to_pytorch(TF_CHECKPOINT,
                                                    TF_CONFIG,
                                                    PYTORCH_DUMP_OUTPUT,
                                                    FINETUNING_TASK)

113
114
if __name__ == '__main__':
    main()