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

import yaml
from paddle.fluid.incubate.fleet.parameter_server import version
T
tangwei 已提交
6 7 8

from fleetrec.core.factory import TrainerFactory
from fleetrec.core.utils import envs
T
tangwei 已提交
9
from fleetrec.core.utils import util
T
tangwei 已提交
10

T
tangwei 已提交
11
engines = {"TRAINSPILER": {}, "PSLIB": {}}
T
tangwei 已提交
12 13 14 15
clusters = ["SINGLE", "LOCAL_CLUSTER", "CLUSTER"]


def set_runtime_envs(cluster_envs, engine_yaml):
T
tangwei 已提交
16
    def get_engine_extras():
T
tangwei 已提交
17 18
        with open(engine_yaml, 'r') as rb:
            _envs = yaml.load(rb.read(), Loader=yaml.FullLoader)
T
tangwei 已提交
19 20 21 22 23 24 25 26

        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 已提交
27 28 29

    if cluster_envs is None:
        cluster_envs = {}
T
tangwei 已提交
30 31 32

    envs.set_runtime_environs(cluster_envs)
    envs.set_runtime_environs(get_engine_extras())
T
fix bug  
tangwei 已提交
33 34 35

    need_print = {}
    for k, v in os.environ.items():
T
fix bug  
tangwei 已提交
36
        if k.startswith("trainer."):
T
fix bug  
tangwei 已提交
37 38 39
            need_print[k] = v

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


T
tangwei 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
def get_engine(engine):
    engine = engine.upper()
    if version.is_transpiler():
        run_engine = engines["TRAINSPILER"].get(engine, None)
    else:
        run_engine = engines["PSLIB"].get(engine, None)

    if run_engine is None:
        raise ValueError("engine only support SINGLE/LOCAL_CLUSTER/CLUSTER")
    return run_engine


def single_engine(args):
T
tangwei 已提交
55
    print("use single engine to run model: {}".format(args.model))
T
fix bug  
tangwei 已提交
56 57 58 59

    single_envs = {}
    single_envs["trainer.trainer"] = "SingleTrainer"
    single_envs["trainer.threads"] = "2"
T
fix bug  
tangwei 已提交
60
    single_envs["trainer.engine"] = "single"
T
tangwei 已提交
61
    set_runtime_envs(single_envs, args.model)
T
tangwei 已提交
62 63 64 65 66
    trainer = TrainerFactory.create(args.model)
    return trainer


def cluster_engine(args):
T
tangwei 已提交
67 68
    print("launch cluster engine with cluster to run model: {}".format(args.model))

T
fix bug  
tangwei 已提交
69 70
    cluster_envs = {}
    cluster_envs["trainer.trainer"] = "ClusterTrainer"
T
fix bug  
tangwei 已提交
71
    cluster_envs["trainer.engine"] = "cluster"
T
tangwei 已提交
72
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
73 74 75 76 77 78

    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
fix bug  
tangwei 已提交
81 82
    cluster_envs = {}
    cluster_envs["trainer.trainer"] = "CtrCodingTrainer"
T
tangwei 已提交
83
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
84

T
tangwei 已提交
85 86 87 88 89
    trainer = TrainerFactory.create(args.model)
    return trainer


def local_cluster_engine(args):
T
tangwei 已提交
90
    print("launch cluster engine with cluster to run model: {}".format(args.model))
T
tangwei 已提交
91 92
    from fleetrec.core.engine.local_cluster_engine import LocalClusterEngine

T
tangwei 已提交
93 94 95 96 97
    cluster_envs = {}
    cluster_envs["server_num"] = 1
    cluster_envs["worker_num"] = 1
    cluster_envs["start_port"] = 36001
    cluster_envs["log_dir"] = "logs"
T
tangwei 已提交
98
    cluster_envs["trainer.trainer"] = "ClusterTrainer"
T
fix bug  
tangwei 已提交
99 100
    cluster_envs["trainer.strategy"] = "async"
    cluster_envs["trainer.threads"] = "2"
T
fix bug  
tangwei 已提交
101
    cluster_envs["trainer.engine"] = "local_cluster"
T
fix bug  
tangwei 已提交
102
    cluster_envs["CPU_NUM"] = "2"
T
tangwei 已提交
103

T
tangwei 已提交
104
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
105

T
tangwei 已提交
106 107
    launch = LocalClusterEngine(cluster_envs, args.model)
    return launch
T
tangwei 已提交
108

T
tangwei 已提交
109

T
tangwei 已提交
110
def local_mpi_engine(args):
T
tangwei 已提交
111
    print("launch cluster engine with cluster to run model: {}".format(args.model))
T
tangwei 已提交
112
    from fleetrec.core.engine.local_mpi_engine import LocalMPIEngine
T
tangwei 已提交
113

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

T
tangwei 已提交
116 117 118
    mpi = util.run_which("mpirun")
    if not mpi:
        raise RuntimeError("can not find mpirun, please check environment")
T
fix bug  
tangwei 已提交
119 120 121 122 123
    cluster_envs = {}
    cluster_envs["mpirun"] = mpi
    cluster_envs["trainer.trainer"] = "CtrCodingTrainer"
    cluster_envs["log_dir"] = "logs"
    cluster_envs["trainer.engine"] = "local_cluster"
T
tangwei 已提交
124

T
tangwei 已提交
125
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
126 127 128 129
    launch = LocalMPIEngine(cluster_envs, args.model)
    return launch


T
tangwei 已提交
130 131 132 133 134 135 136 137
def engine_registry():
    engines["TRAINSPILER"]["SINGLE"] = single_engine
    engines["TRAINSPILER"]["LOCAL_CLUSTER"] = local_cluster_engine
    engines["TRAINSPILER"]["CLUSTER"] = cluster_engine
    engines["PSLIB"]["SINGLE"] = local_mpi_engine
    engines["PSLIB"]["LOCAL_CLUSTER"] = local_mpi_engine
    engines["PSLIB"]["CLUSTER"] = cluster_mpi_engine

T
tangwei 已提交
138

T
tangwei 已提交
139
engine_registry()
T
tangwei 已提交
140 141 142

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='fleet-rec run')
T
tangwei 已提交
143 144
    parser.add_argument("-m", "--model", type=str)
    parser.add_argument("-e", "--engine", type=str)
T
tangwei 已提交
145 146 147 148

    args = parser.parse_args()

    if not os.path.exists(args.model) or not os.path.isfile(args.model):
T
tangwei 已提交
149 150
        raise ValueError("argument model: {} error, must specify an existed YAML file".format(args.model))

T
tangwei 已提交
151 152 153
    if args.engine.upper() not in clusters:
        raise ValueError("argument engine: {} error, must in {}".format(args.engine, clusters))

T
tangwei 已提交
154 155 156
    which_engine = get_engine(args.engine)
    engine = which_engine(args)
    engine.run()