run.py 5.0 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 25 26 27 28
    envs.set_runtime_envions(cluster_envs)
    envs.set_runtime_envions(_envs)

    need_print = {}
    for k, v in os.environ.items():
T
fix bug  
tangwei 已提交
29
        if k.startswith("trainer."):
T
fix bug  
tangwei 已提交
30 31 32
            need_print[k] = v

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


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

    single_envs = {}
    single_envs["trainer.trainer"] = "SingleTrainer"
    single_envs["trainer.threads"] = "2"
T
fix bug  
tangwei 已提交
53
    single_envs["trainer.engine"] = "single"
T
tangwei 已提交
54
    set_runtime_envs(single_envs, args.engine_extras)
T
tangwei 已提交
55 56 57 58 59
    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
fix bug  
tangwei 已提交
62 63
    cluster_envs = {}
    cluster_envs["trainer.trainer"] = "ClusterTrainer"
T
fix bug  
tangwei 已提交
64
    cluster_envs["trainer.engine"] = "cluster"
T
tangwei 已提交
65
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
66 67 68 69 70 71 72

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


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

T
fix bug  
tangwei 已提交
75 76
    cluster_envs = {}
    cluster_envs["trainer.trainer"] = "CtrCodingTrainer"
T
tangwei 已提交
77
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
78

T
tangwei 已提交
79 80 81 82 83
    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
tangwei 已提交
87 88 89 90 91
    cluster_envs = {}
    cluster_envs["server_num"] = 1
    cluster_envs["worker_num"] = 1
    cluster_envs["start_port"] = 36001
    cluster_envs["log_dir"] = "logs"
T
tangwei 已提交
92
    cluster_envs["trainer.trainer"] = "ClusterTrainer"
T
fix bug  
tangwei 已提交
93 94
    cluster_envs["trainer.strategy"] = "async"
    cluster_envs["trainer.threads"] = "2"
T
fix bug  
tangwei 已提交
95
    cluster_envs["trainer.engine"] = "local_cluster"
T
fix bug  
tangwei 已提交
96
    cluster_envs["CPU_NUM"] = "2"
T
tangwei 已提交
97

T
tangwei 已提交
98
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
99

T
tangwei 已提交
100 101
    launch = LocalClusterEngine(cluster_envs, args.model)
    return launch
T
tangwei 已提交
102

T
tangwei 已提交
103

T
tangwei 已提交
104
def local_mpi_engine(args):
T
tangwei 已提交
105
    print("launch cluster engine with cluster to run model: {}".format(args.model))
T
tangwei 已提交
106
    from fleetrec.core.engine.local_mpi_engine import LocalMPIEngine
T
tangwei 已提交
107

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

T
tangwei 已提交
110 111 112
    mpi = util.run_which("mpirun")
    if not mpi:
        raise RuntimeError("can not find mpirun, please check environment")
T
fix bug  
tangwei 已提交
113 114 115 116 117
    cluster_envs = {}
    cluster_envs["mpirun"] = mpi
    cluster_envs["trainer.trainer"] = "CtrCodingTrainer"
    cluster_envs["log_dir"] = "logs"
    cluster_envs["trainer.engine"] = "local_cluster"
T
tangwei 已提交
118

T
tangwei 已提交
119
    set_runtime_envs(cluster_envs, args.engine_extras)
T
tangwei 已提交
120 121 122 123
    launch = LocalMPIEngine(cluster_envs, args.model)
    return launch


T
tangwei 已提交
124 125 126 127 128 129 130 131
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 已提交
132

T
tangwei 已提交
133
engine_registry()
T
tangwei 已提交
134 135 136

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='fleet-rec run')
T
tangwei 已提交
137 138
    parser.add_argument("-m", "--model", type=str)
    parser.add_argument("-e", "--engine", type=str)
T
tangwei 已提交
139
    parser.add_argument("-ex", "--engine_extras", default=None, type=str)
T
tangwei 已提交
140 141 142 143

    args = parser.parse_args()

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

T
tangwei 已提交
146 147 148 149 150 151 152 153
    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 已提交
154 155 156
    which_engine = get_engine(args.engine)
    engine = which_engine(args)
    engine.run()