run.py 4.7 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

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


T
tangwei 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
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 已提交
43
    print("use single engine to run model: {}".format(args.model))
T
fix bug  
tangwei 已提交
44 45 46 47

    single_envs = {}
    single_envs["trainer.trainer"] = "SingleTrainer"
    single_envs["trainer.threads"] = "2"
T
tangwei 已提交
48
    set_runtime_envs(single_envs, args.engine_extras)
T
tangwei 已提交
49 50 51 52 53
    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
fix bug  
tangwei 已提交
56 57
    cluster_envs = {}
    cluster_envs["trainer.trainer"] = "ClusterTrainer"
T
tangwei 已提交
58
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
59 60 61 62 63 64 65

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


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

T
fix bug  
tangwei 已提交
68 69
    cluster_envs = {}
    cluster_envs["trainer.trainer"] = "CtrCodingTrainer"
T
tangwei 已提交
70
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
71

T
tangwei 已提交
72 73 74 75 76
    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
tangwei 已提交
80 81 82 83 84
    cluster_envs = {}
    cluster_envs["server_num"] = 1
    cluster_envs["worker_num"] = 1
    cluster_envs["start_port"] = 36001
    cluster_envs["log_dir"] = "logs"
T
tangwei 已提交
85
    cluster_envs["trainer.trainer"] = "ClusterTrainer"
T
fix bug  
tangwei 已提交
86 87 88
    cluster_envs["trainer.strategy"] = "async"
    cluster_envs["trainer.threads"] = "2"
    cluster_envs["CPU_NUM"] = "2"
T
tangwei 已提交
89

T
tangwei 已提交
90
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
91

T
tangwei 已提交
92 93
    launch = LocalClusterEngine(cluster_envs, args.model)
    return launch
T
tangwei 已提交
94

T
tangwei 已提交
95

T
tangwei 已提交
96
def local_mpi_engine(args):
T
tangwei 已提交
97
    print("launch cluster engine with cluster to run model: {}".format(args.model))
T
tangwei 已提交
98
    from fleetrec.core.engine.local_mpi_engine import LocalMPIEngine
T
tangwei 已提交
99

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

T
tangwei 已提交
102 103 104
    mpi = util.run_which("mpirun")
    if not mpi:
        raise RuntimeError("can not find mpirun, please check environment")
T
tangwei 已提交
105

T
tangwei 已提交
106
    cluster_envs = {"mpirun": mpi, "trainer.trainer": "CtrCodingTrainer", "log_dir": "logs"}
T
tangwei 已提交
107
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
108 109 110 111
    launch = LocalMPIEngine(cluster_envs, args.model)
    return launch


T
tangwei 已提交
112 113 114 115 116 117 118 119
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 已提交
120

T
tangwei 已提交
121
engine_registry()
T
tangwei 已提交
122 123 124

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='fleet-rec run')
T
tangwei 已提交
125 126
    parser.add_argument("-m", "--model", type=str)
    parser.add_argument("-e", "--engine", type=str)
T
tangwei 已提交
127
    parser.add_argument("-ex", "--engine_extras", default=None, type=str)
T
tangwei 已提交
128 129 130 131

    args = parser.parse_args()

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

T
tangwei 已提交
134 135 136 137 138 139 140 141
    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 已提交
142 143 144
    which_engine = get_engine(args.engine)
    engine = which_engine(args)
    engine.run()