paddlenlp

Форк
0
/
ci_normal_case.py 
175 строк · 6.6 Кб
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
"""
15
RUN PaddleNLP CI Case
16
"""
17
import os
18
import re
19
import subprocess
20
import sys
21

22

23
def get_mode_info(case_path):
24
    """
25
    Return: model_info{path,exec_file_list}
26
    Examples:
27
        pegasus = {
28
            "path": "applications/text_summarization/pegasus/"
29
            "deploy_path": "deploy/paddle_inference/"
30
            "prepare": "run_prepare.py"
31
            "train_exec_file": "train.py"
32
            "eval_exec_file": None
33
            "predict_exec_file": predict.py
34
            “export_exec_file”: export_model.py
35
            "infer_exec_file": inference_pegasus.py
36
            }
37
    """
38
    model_info = {
39
        "path": case_path,
40
        "deploy_path": None,
41
        "prepare_exec_file": None,
42
        "train_exec_file": [],
43
        "eval_exec_file": None,
44
        "predict_exec_file": None,
45
        "export_exec_file": None,
46
        "infer_exec_file": None,
47
    }
48
    for root, dirs, files in os.walk(case_path):
49
        infer_deploy_path = case_path + "/deploy/paddle_inference"
50
        python_deploy_path = case_path + "/deploy/python"
51

52
        if files and root == case_path:
53
            for file in files:
54
                # TODO .sh file incompatible windows
55
                if file.split(".")[-1] == "py":
56
                    if re.compile("prepare.py").findall(file):
57
                        model_info["prepare_exec_file"] = file
58

59
                    elif re.compile("train.py").findall(file):
60
                        model_info["train_exec_file"].append(file)
61

62
                    elif re.compile("finetune").findall(file):
63
                        model_info["train_exec_file"].append(file)
64

65
                    elif re.compile("eval.py").findall(file):
66
                        model_info["eval_exec_file"] = file
67

68
                    elif re.compile("predict.py").findall(file):
69
                        model_info["predict_exec_file"] = file
70

71
                    elif re.compile("export_model.py").findall(file):
72
                        model_info["export_exec_file"] = file
73

74
                    elif re.compile("run_").findall(file):
75
                        model_info["train_exec_file"].append(file)
76
                    else:
77
                        continue
78
        elif files and root == infer_deploy_path:
79
            for file in files:
80
                if file.split(".")[-1] == "py":
81
                    model_info["deploy_path"] = "deploy/paddle_inference"
82
                    model_info["infer_exec_file"] = file
83
        elif files and root == python_deploy_path:
84
            for file in files:
85
                if file.split(".")[-1] == "py":
86
                    model_info["deploy_path"] = "deploy/python"
87
                    model_info["infer_exec_file"] = file
88

89
    print("model_info", model_info)
90
    return model_info
91

92

93
def save_log(exit_code, output, case_name, file_name):
94
    """
95
    save model log
96
    """
97
    root_path = "/workspace/PaddleNLP"
98
    # root_path = '/ssd1/paddlenlp/zhangjunjun/PaddleNLP'
99
    if exit_code == 0:
100
        log_file = root_path + "/model_logs/" + os.path.join(case_name + "_" + file_name + "_SUCCESS.log")
101
        print("{} SUCCESS".format(file_name))
102
        with open(log_file, "a") as flog:
103
            flog.write("%s" % (output))
104
    else:
105
        log_file = root_path + "/model_logs/" + os.path.join(case_name + "_" + file_name + "_FAIL.log")
106
        print("{} FAIL".format(file_name))
107
        with open(log_file, "a") as flog:
108
            flog.write("%s" % (output))
109

110

111
def run_normal_case(case_path):
112
    """
113
    Run new normal case
114
    params:
115
    case_path: model path based PaddleNLP from git diff
116
    """
117
    case_name = case_path.split("/")[-1]
118
    model_info = get_mode_info(case_path)
119
    depoly_path = model_info["deploy_path"]
120
    prepare_exec_file = model_info["prepare_exec_file"]
121
    eval_exec_file = model_info["eval_exec_file"]
122
    predict_exec_file = model_info["predict_exec_file"]
123
    export_exec_file = model_info["export_exec_file"]
124
    infer_exec_file = model_info["infer_exec_file"]
125

126
    os.chdir(case_path)
127

128
    if prepare_exec_file:
129
        prepare_output = subprocess.getstatusoutput("python %s " % (prepare_exec_file))
130
        save_log(prepare_output[0], prepare_output[1], case_name, prepare_exec_file.split(".")[0])
131

132
    if model_info["train_exec_file"]:
133
        for train_file in model_info["train_exec_file"]:
134
            train_output = subprocess.getstatusoutput(
135
                "python -m paddle.distributed.launch %s --device gpu --max_steps 2 \
136
                --save_steps 2 --output_dir ./output/"
137
                % (train_file)
138
            )
139
            save_log(train_output[0], train_output[1], case_name, train_file.split(".")[0])
140
    else:
141
        print("Train Skipped")
142

143
    if eval_exec_file:
144
        eval_output = subprocess.getstatusoutput("python %s --init_checkpoint_dir ./output/" % (eval_exec_file))
145
        save_log(eval_output[0], eval_output[1], case_name, eval_exec_file.split(".")[0])
146
    else:
147
        print("Evalation Skipped")
148
    if predict_exec_file:
149
        predict_output = subprocess.getstatusoutput("python %s --init_checkpoint_dir ./output/" % (predict_exec_file))
150
        save_log(predict_output[0], predict_output[1], case_name, predict_exec_file.split(".")[0])
151
    else:
152
        print("Predict Skipped")
153
    if export_exec_file:
154
        export_output = subprocess.getstatusoutput(
155
            "python %s --export_output_dir ./inference_model/" % (export_exec_file)
156
        )
157
        save_log(export_output[0], export_output[1], case_name, export_exec_file.split(".")[0])
158
    else:
159
        print("Export model Skipped")
160
    if infer_exec_file:
161
        infer_output = subprocess.getstatusoutput(
162
            "cd %s && python %s --inference_model_dir ../../inference_model/" % (depoly_path, infer_exec_file)
163
        )
164
        save_log(infer_output[0], infer_output[1], case_name, infer_exec_file.split(".")[0])
165
    else:
166
        print("python inference Skipped")
167

168

169
if __name__ == "__main__":
170
    # path ="applications/text_summarization/pegasus"
171
    path = sys.argv[1]
172
    if os.path.isdir(path):
173
        run_normal_case(path)
174
    else:
175
        print("not model file path, skepped ")
176

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.