run.py 4.5 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 16 17 18
clusters = ["SINGLE", "LOCAL_CLUSTER", "CLUSTER"]


def set_runtime_envs(cluster_envs, engine_yaml):
    if engine_yaml is not None:
        with open(engine_yaml, 'r') as rb:
            _envs = yaml.load(rb.read(), Loader=yaml.FullLoader)
T
tangwei 已提交
19 20
    else:
        _envs = {}
T
tangwei 已提交
21 22 23 24 25 26

    if cluster_envs is None:
        cluster_envs = {}
    cluster_envs.update(_envs)
    envs.set_runtime_envions(cluster_envs)
    print(envs.pretty_print_envs(cluster_envs, ("Runtime Envs", "Value")))
T
tangwei 已提交
27 28


T
tangwei 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
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 已提交
42
    print("use single engine to run model: {}".format(args.model))
T
tangwei 已提交
43
    single_envs = {"trainer.trainer": "SingleTrainer", "trainer.threads": "2"}
T
tangwei 已提交
44
    set_runtime_envs(single_envs, args.engine_extras)
T
tangwei 已提交
45 46 47 48 49
    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
tangwei 已提交
52
    cluster_envs = {"trainer.trainer": "ClusterTrainer"}
T
tangwei 已提交
53
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
54 55 56 57 58 59 60

    envs.set_runtime_envions(cluster_envs)
    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
tangwei 已提交
63
    cluster_envs = {"trainer.trainer": "CtrCodingTrainer"}
T
tangwei 已提交
64
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
65

T
tangwei 已提交
66 67 68 69 70
    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
tangwei 已提交
74 75 76 77 78
    cluster_envs = {}
    cluster_envs["server_num"] = 1
    cluster_envs["worker_num"] = 1
    cluster_envs["start_port"] = 36001
    cluster_envs["log_dir"] = "logs"
T
tangwei 已提交
79
    cluster_envs["trainer.trainer"] = "ClusterTrainer"
T
tangwei 已提交
80
    cluster_envs["trainer.strategy.mode"] = "async"
T
tangwei 已提交
81

T
tangwei 已提交
82
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
83

T
tangwei 已提交
84 85
    launch = LocalClusterEngine(cluster_envs, args.model)
    return launch
T
tangwei 已提交
86

T
tangwei 已提交
87

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

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

T
tangwei 已提交
94 95 96
    mpi = util.run_which("mpirun")
    if not mpi:
        raise RuntimeError("can not find mpirun, please check environment")
T
tangwei 已提交
97

T
tangwei 已提交
98
    cluster_envs = {"mpirun": mpi, "trainer.trainer": "CtrCodingTrainer", "log_dir": "logs"}
T
tangwei 已提交
99
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
100 101 102 103
    launch = LocalMPIEngine(cluster_envs, args.model)
    return launch


T
tangwei 已提交
104 105 106 107 108 109 110 111
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 已提交
112

T
tangwei 已提交
113
engine_registry()
T
tangwei 已提交
114 115 116

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='fleet-rec run')
T
tangwei 已提交
117 118
    parser.add_argument("-m", "--model", type=str)
    parser.add_argument("-e", "--engine", type=str)
T
tangwei 已提交
119
    parser.add_argument("-ex", "--engine_extras", default=None, type=str)
T
tangwei 已提交
120 121 122 123

    args = parser.parse_args()

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

T
tangwei 已提交
126 127 128 129 130 131 132 133
    if args.engine.upper() not in clusters:
        raise ValueError("argument engine: {} error, must in {}".format(args.engine, clusters))

    if args.engine_extras is not None:
        if not os.path.exists(args.engine_extras) or not os.path.isfile(args.engine_extras):
            raise ValueError(
                "argument engine_extras: {} error, must specify an existed YAML file".format(args.engine_extras))

T
tangwei 已提交
134 135 136
    which_engine = get_engine(args.engine)
    engine = which_engine(args)
    engine.run()