run.py 7.1 KB
Newer Older
T
tangwei 已提交
1 2
import argparse
import os
T
tangwei 已提交
3
import subprocess
T
tangwei 已提交
4
import yaml
T
tangwei 已提交
5

T
rename  
tangwei 已提交
6 7 8
from fleetrec.core.factory import TrainerFactory
from fleetrec.core.utils import envs
from fleetrec.core.utils import util
T
tangwei 已提交
9

T
tangwei 已提交
10 11
engines = {}
device = ["CPU", "GPU"]
T
tangwei 已提交
12
clusters = ["SINGLE", "LOCAL_CLUSTER", "CLUSTER"]
C
chengmo 已提交
13
custom_model = ['tdm']
C
fix  
chengmo 已提交
14
model_name = ""
T
tangwei 已提交
15 16


T
tangwei 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
def engine_registry():
    cpu = {"TRANSPILER": {}, "PSLIB": {}}
    cpu["TRANSPILER"]["SINGLE"] = single_engine
    cpu["TRANSPILER"]["LOCAL_CLUSTER"] = local_cluster_engine
    cpu["TRANSPILER"]["CLUSTER"] = cluster_engine
    cpu["PSLIB"]["SINGLE"] = local_mpi_engine
    cpu["PSLIB"]["LOCAL_CLUSTER"] = local_mpi_engine
    cpu["PSLIB"]["CLUSTER"] = cluster_mpi_engine

    gpu = {"TRANSPILER": {}, "PSLIB": {}}
    gpu["TRANSPILER"]["SINGLE"] = single_engine

    engines["CPU"] = cpu
    engines["GPU"] = gpu


C
chengmo 已提交
33 34
def get_engine(args):
    device = args.device
T
tangwei 已提交
35 36
    d_engine = engines[device]
    transpiler = get_transpiler()
C
chengmo 已提交
37

C
fix  
chengmo 已提交
38
    engine = args.engine
T
tangwei 已提交
39 40 41
    run_engine = d_engine[transpiler].get(engine, None)

    if run_engine is None:
C
chengmo 已提交
42 43
        raise ValueError(
            "engine {} can not be supported on device: {}".format(engine, device))
T
tangwei 已提交
44 45 46 47
    return run_engine


def get_transpiler():
T
tangwei 已提交
48 49 50 51 52 53
    FNULL = open(os.devnull, 'w')
    cmd = ["python", "-c",
           "import paddle.fluid as fluid; fleet_ptr = fluid.core.Fleet(); [fleet_ptr.copy_table_by_feasign(10, 10, [2020, 1010])];"]
    proc = subprocess.Popen(cmd, stdout=FNULL, stderr=FNULL, cwd=os.getcwd())
    ret = proc.wait()
    if ret == -11:
T
tangwei 已提交
54
        return "PSLIB"
T
tangwei 已提交
55
    else:
T
tangwei 已提交
56
        return "TRANSPILER"
T
tangwei 已提交
57 58


T
tangwei 已提交
59
def set_runtime_envs(cluster_envs, engine_yaml):
T
tangwei 已提交
60
    def get_engine_extras():
T
tangwei 已提交
61 62
        with open(engine_yaml, 'r') as rb:
            _envs = yaml.load(rb.read(), Loader=yaml.FullLoader)
T
tangwei 已提交
63 64 65 66 67 68 69 70

        flattens = envs.flatten_environs(_envs)

        engine_extras = {}
        for k, v in flattens.items():
            if k.startswith("train.trainer."):
                engine_extras[k] = v
        return engine_extras
T
tangwei 已提交
71 72 73

    if cluster_envs is None:
        cluster_envs = {}
T
tangwei 已提交
74

75 76 77
    engine_extras = get_engine_extras()
    if "train.trainer.threads" in engine_extras and "CPU_NUM" in cluster_envs:
        cluster_envs["CPU_NUM"] = engine_extras["train.trainer.threads"]
T
tangwei 已提交
78
    envs.set_runtime_environs(cluster_envs)
79
    envs.set_runtime_environs(engine_extras)
T
fix bug  
tangwei 已提交
80 81 82

    need_print = {}
    for k, v in os.environ.items():
T
tangwei 已提交
83
        if k.startswith("train.trainer."):
T
fix bug  
tangwei 已提交
84 85 86
            need_print[k] = v

    print(envs.pretty_print_envs(need_print, ("Runtime Envs", "Value")))
T
tangwei 已提交
87 88


C
chengmo 已提交
89 90 91 92
def get_trainer_prefix(args):
    if model_name in custom_model:
        return model_name.upper()
    return ""
T
tangwei 已提交
93

C
chengmo 已提交
94

C
chengmo 已提交
95 96
def single_engine(args):
    trainer = get_trainer_prefix(args) + "SingleTrainer"
C
chengmo 已提交
97
    single_envs = {}
C
chengmo 已提交
98
    single_envs["train.trainer.trainer"] = trainer
C
chengmo 已提交
99 100 101 102
    single_envs["train.trainer.threads"] = "2"
    single_envs["train.trainer.engine"] = "single"
    single_envs["train.trainer.device"] = args.device
    single_envs["train.trainer.platform"] = envs.get_platform()
C
chengmo 已提交
103
    print("use {} engine to run model: {}".format(trainer, args.model))
C
chengmo 已提交
104 105 106 107 108 109

    set_runtime_envs(single_envs, args.model)
    trainer = TrainerFactory.create(args.model)
    return trainer


T
tangwei 已提交
110
def cluster_engine(args):
C
chengmo 已提交
111
    trainer = get_trainer_prefix(args) + "ClusterTrainer"
C
chengmo 已提交
112
    cluster_envs = {}
C
chengmo 已提交
113
    cluster_envs["train.trainer.trainer"] = trainer
C
chengmo 已提交
114 115 116
    cluster_envs["train.trainer.engine"] = "cluster"
    cluster_envs["train.trainer.device"] = args.device
    cluster_envs["train.trainer.platform"] = envs.get_platform()
C
chengmo 已提交
117
    print("launch {} engine with cluster to run model: {}".format(trainer, args.model))
C
chengmo 已提交
118 119 120 121 122 123

    set_runtime_envs(cluster_envs, args.model)
    trainer = TrainerFactory.create(args.model)
    return trainer


T
tangwei 已提交
124
def cluster_mpi_engine(args):
T
tangwei 已提交
125 126
    print("launch cluster engine with cluster to run model: {}".format(args.model))

T
fix bug  
tangwei 已提交
127
    cluster_envs = {}
T
tangwei 已提交
128
    cluster_envs["train.trainer.trainer"] = "CtrCodingTrainer"
T
tangwei 已提交
129
    cluster_envs["train.trainer.device"] = args.device
T
tangwei 已提交
130
    cluster_envs["train.trainer.platform"] = envs.get_platform()
T
tangwei 已提交
131

T
tangwei 已提交
132
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
133

T
tangwei 已提交
134 135 136 137 138
    trainer = TrainerFactory.create(args.model)
    return trainer


def local_cluster_engine(args):
C
chengmo 已提交
139 140
    from fleetrec.core.engine.local_cluster_engine import LocalClusterEngine

C
chengmo 已提交
141
    trainer = get_trainer_prefix(args) + "ClusterTrainer"
C
chengmo 已提交
142 143 144
    cluster_envs = {}
    cluster_envs["server_num"] = 1
    cluster_envs["worker_num"] = 1
C
chengmo 已提交
145
    cluster_envs["start_port"] = envs.find_free_port()
C
chengmo 已提交
146
    cluster_envs["log_dir"] = "logs"
C
chengmo 已提交
147
    cluster_envs["train.trainer.trainer"] = trainer
C
chengmo 已提交
148 149 150 151 152 153 154 155
    cluster_envs["train.trainer.strategy"] = "async"
    cluster_envs["train.trainer.threads"] = "2"
    cluster_envs["train.trainer.engine"] = "local_cluster"

    cluster_envs["train.trainer.device"] = args.device
    cluster_envs["train.trainer.platform"] = envs.get_platform()

    cluster_envs["CPU_NUM"] = "2"
C
chengmo 已提交
156
    print("launch {} engine with cluster to run model: {}".format(trainer, args.model))
C
chengmo 已提交
157 158 159 160 161 162

    set_runtime_envs(cluster_envs, args.model)
    launch = LocalClusterEngine(cluster_envs, args.model)
    return launch


T
tangwei 已提交
163
def local_mpi_engine(args):
T
tangwei 已提交
164
    print("launch cluster engine with cluster to run model: {}".format(args.model))
T
rename  
tangwei 已提交
165
    from fleetrec.core.engine.local_mpi_engine import LocalMPIEngine
T
tangwei 已提交
166

T
tangwei 已提交
167
    print("use 1X1 MPI ClusterTraining at localhost to run model: {}".format(args.model))
T
tangwei 已提交
168

T
tangwei 已提交
169 170 171
    mpi = util.run_which("mpirun")
    if not mpi:
        raise RuntimeError("can not find mpirun, please check environment")
T
fix bug  
tangwei 已提交
172 173
    cluster_envs = {}
    cluster_envs["mpirun"] = mpi
T
tangwei 已提交
174
    cluster_envs["train.trainer.trainer"] = "CtrCodingTrainer"
T
fix bug  
tangwei 已提交
175
    cluster_envs["log_dir"] = "logs"
T
tangwei 已提交
176
    cluster_envs["train.trainer.engine"] = "local_cluster"
T
tangwei 已提交
177

T
tangwei 已提交
178
    cluster_envs["train.trainer.device"] = args.device
T
tangwei 已提交
179
    cluster_envs["train.trainer.platform"] = envs.get_platform()
T
tangwei 已提交
180

T
tangwei 已提交
181
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
182 183 184 185
    launch = LocalMPIEngine(cluster_envs, args.model)
    return launch


T
tangwei 已提交
186 187 188 189 190 191 192 193 194 195 196 197
def get_abs_model(model):
    if model.startswith("fleetrec."):
        fleet_base = envs.get_runtime_environ("PACKAGE_BASE")
        workspace_dir = model.split("fleetrec.")[1].replace(".", "/")
        path = os.path.join(fleet_base, workspace_dir, "config.yaml")
    else:
        if not os.path.isfile(model):
            raise IOError("model config: {} invalid".format(model))
        path = model
    return path


T
tangwei 已提交
198 199
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='fleet-rec run')
T
tangwei 已提交
200
    parser.add_argument("-m", "--model", type=str)
C
chengmo 已提交
201
    parser.add_argument("-e", "--engine", type=str,
C
chengmo 已提交
202 203
                        choices=["single", "local_cluster", "cluster",
                                 "tdm_single", "tdm_local_cluster", "tdm_cluster"])
C
chengmo 已提交
204 205
    parser.add_argument("-d", "--device", type=str,
                        choices=["cpu", "gpu"], default="cpu")
T
tangwei 已提交
206

T
tangwei 已提交
207 208 209
    abs_dir = os.path.dirname(os.path.abspath(__file__))
    envs.set_runtime_environs({"PACKAGE_BASE": abs_dir})

T
tangwei 已提交
210
    args = parser.parse_args()
T
tangwei 已提交
211 212
    args.engine = args.engine.upper()
    args.device = args.device.upper()
C
fix  
chengmo 已提交
213
    model_name = args.model.split('.')[-1]
T
tangwei 已提交
214
    args.model = get_abs_model(args.model)
T
tangwei 已提交
215
    engine_registry()
T
tangwei 已提交
216

C
chengmo 已提交
217
    which_engine = get_engine(args)
T
bug fix  
tangwei12 已提交
218

T
tangwei 已提交
219 220
    engine = which_engine(args)
    engine.run()