run.py 8.6 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 13 14
clusters = ["SINGLE", "LOCAL_CLUSTER", "CLUSTER"]


T
tangwei 已提交
15 16 17 18 19
def engine_registry():
    cpu = {"TRANSPILER": {}, "PSLIB": {}}
    cpu["TRANSPILER"]["SINGLE"] = single_engine
    cpu["TRANSPILER"]["LOCAL_CLUSTER"] = local_cluster_engine
    cpu["TRANSPILER"]["CLUSTER"] = cluster_engine
C
chengmo 已提交
20
    cpu["TRANSPILER"]["TDM_SINGLE"] = tdm_single_engine
C
chengmo 已提交
21 22
    cpu["TRANSPILER"]["TDM_LOCAL_CLUSTER"] = tdm_local_cluster_engine
    cpu["TRANSPILER"]["TDM_CLUSTER"] = tdm_cluster_engine
T
tangwei 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    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


def get_engine(engine, device):
    d_engine = engines[device]
    transpiler = get_transpiler()
    run_engine = d_engine[transpiler].get(engine, None)

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


def get_transpiler():
T
tangwei 已提交
46 47 48 49 50 51
    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 已提交
52
        return "PSLIB"
T
tangwei 已提交
53
    else:
T
tangwei 已提交
54
        return "TRANSPILER"
T
tangwei 已提交
55 56


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

        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 已提交
69 70 71

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

    envs.set_runtime_environs(cluster_envs)
    envs.set_runtime_environs(get_engine_extras())
T
fix bug  
tangwei 已提交
75 76 77

    need_print = {}
    for k, v in os.environ.items():
T
tangwei 已提交
78
        if k.startswith("train.trainer."):
T
fix bug  
tangwei 已提交
79 80 81
            need_print[k] = v

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


T
tangwei 已提交
84
def single_engine(args):
T
tangwei 已提交
85
    print("use single engine to run model: {}".format(args.model))
T
fix bug  
tangwei 已提交
86 87

    single_envs = {}
T
tangwei 已提交
88 89 90
    single_envs["train.trainer.trainer"] = "SingleTrainer"
    single_envs["train.trainer.threads"] = "2"
    single_envs["train.trainer.engine"] = "single"
T
tangwei 已提交
91
    single_envs["train.trainer.device"] = args.device
T
tangwei 已提交
92
    single_envs["train.trainer.platform"] = envs.get_platform()
T
tangwei 已提交
93

T
tangwei 已提交
94
    set_runtime_envs(single_envs, args.model)
T
tangwei 已提交
95 96 97 98
    trainer = TrainerFactory.create(args.model)
    return trainer


C
chengmo 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
def tdm_single_engine(args):
    print("use tdm single engine to run model: {}".format(args.model))

    single_envs = {}
    single_envs["train.trainer.trainer"] = "TDMSingleTrainer"
    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()

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


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

T
fix bug  
tangwei 已提交
117
    cluster_envs = {}
T
tangwei 已提交
118 119
    cluster_envs["train.trainer.trainer"] = "ClusterTrainer"
    cluster_envs["train.trainer.engine"] = "cluster"
T
tangwei 已提交
120
    cluster_envs["train.trainer.device"] = args.device
T
tangwei 已提交
121
    cluster_envs["train.trainer.platform"] = envs.get_platform()
T
tangwei 已提交
122

T
tangwei 已提交
123
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
124 125 126 127 128

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


C
chengmo 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
def tdm_cluster_engine(args):
    print("launch tdm cluster engine with cluster to run model: {}".format(args.model))

    cluster_envs = {}
    cluster_envs["train.trainer.trainer"] = "TDMClusterTrainer"
    cluster_envs["train.trainer.engine"] = "cluster"
    cluster_envs["train.trainer.device"] = args.device
    cluster_envs["train.trainer.platform"] = envs.get_platform()

    set_runtime_envs(cluster_envs, args.model)

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


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

T
fix bug  
tangwei 已提交
147
    cluster_envs = {}
T
tangwei 已提交
148
    cluster_envs["train.trainer.trainer"] = "CtrCodingTrainer"
T
tangwei 已提交
149
    cluster_envs["train.trainer.device"] = args.device
T
tangwei 已提交
150
    cluster_envs["train.trainer.platform"] = envs.get_platform()
T
tangwei 已提交
151

T
tangwei 已提交
152
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
153

T
tangwei 已提交
154 155 156 157 158
    trainer = TrainerFactory.create(args.model)
    return trainer


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

T
tangwei 已提交
162 163 164 165 166
    cluster_envs = {}
    cluster_envs["server_num"] = 1
    cluster_envs["worker_num"] = 1
    cluster_envs["start_port"] = 36001
    cluster_envs["log_dir"] = "logs"
T
tangwei 已提交
167 168 169 170
    cluster_envs["train.trainer.trainer"] = "ClusterTrainer"
    cluster_envs["train.trainer.strategy"] = "async"
    cluster_envs["train.trainer.threads"] = "2"
    cluster_envs["train.trainer.engine"] = "local_cluster"
T
tangwei 已提交
171

T
tangwei 已提交
172
    cluster_envs["train.trainer.device"] = args.device
T
tangwei 已提交
173 174
    cluster_envs["train.trainer.platform"] = envs.get_platform()

T
fix bug  
tangwei 已提交
175
    cluster_envs["CPU_NUM"] = "2"
T
tangwei 已提交
176

T
tangwei 已提交
177
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
178

T
tangwei 已提交
179 180
    launch = LocalClusterEngine(cluster_envs, args.model)
    return launch
T
tangwei 已提交
181

T
tangwei 已提交
182

C
chengmo 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
def tdm_local_cluster_engine(args):
    print("launch tdm cluster engine with cluster to run model: {}".format(args.model))
    from fleetrec.core.engine.local_cluster_engine import LocalClusterEngine

    cluster_envs = {}
    cluster_envs["server_num"] = 1
    cluster_envs["worker_num"] = 1
    cluster_envs["start_port"] = 36001
    cluster_envs["log_dir"] = "logs"
    cluster_envs["train.trainer.trainer"] = "TDMClusterTrainer"
    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"

    set_runtime_envs(cluster_envs, args.model)

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


T
tangwei 已提交
208
def local_mpi_engine(args):
T
tangwei 已提交
209
    print("launch cluster engine with cluster to run model: {}".format(args.model))
T
rename  
tangwei 已提交
210
    from fleetrec.core.engine.local_mpi_engine import LocalMPIEngine
T
tangwei 已提交
211

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

T
tangwei 已提交
214 215 216
    mpi = util.run_which("mpirun")
    if not mpi:
        raise RuntimeError("can not find mpirun, please check environment")
T
fix bug  
tangwei 已提交
217 218
    cluster_envs = {}
    cluster_envs["mpirun"] = mpi
T
tangwei 已提交
219
    cluster_envs["train.trainer.trainer"] = "CtrCodingTrainer"
T
fix bug  
tangwei 已提交
220
    cluster_envs["log_dir"] = "logs"
T
tangwei 已提交
221
    cluster_envs["train.trainer.engine"] = "local_cluster"
T
tangwei 已提交
222

T
tangwei 已提交
223
    cluster_envs["train.trainer.device"] = args.device
T
tangwei 已提交
224
    cluster_envs["train.trainer.platform"] = envs.get_platform()
T
tangwei 已提交
225

T
tangwei 已提交
226
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
227 228 229 230
    launch = LocalMPIEngine(cluster_envs, args.model)
    return launch


T
tangwei 已提交
231 232 233 234 235 236 237 238 239 240 241 242
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 已提交
243 244
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='fleet-rec run')
T
tangwei 已提交
245
    parser.add_argument("-m", "--model", type=str)
C
chengmo 已提交
246
    parser.add_argument("-e", "--engine", type=str,
C
chengmo 已提交
247 248
                        choices=["single", "local_cluster", "cluster",
                                 "tdm_single", "tdm_local_cluster", "tdm_cluster"])
C
chengmo 已提交
249 250
    parser.add_argument("-d", "--device", type=str,
                        choices=["cpu", "gpu"], default="cpu")
T
tangwei 已提交
251

T
tangwei 已提交
252 253 254
    abs_dir = os.path.dirname(os.path.abspath(__file__))
    envs.set_runtime_environs({"PACKAGE_BASE": abs_dir})

T
tangwei 已提交
255
    args = parser.parse_args()
T
tangwei 已提交
256 257
    args.engine = args.engine.upper()
    args.device = args.device.upper()
T
tangwei 已提交
258
    args.model = get_abs_model(args.model)
T
tangwei 已提交
259
    engine_registry()
T
tangwei 已提交
260

T
tangwei 已提交
261
    which_engine = get_engine(args.engine, args.device)
T
bug fix  
tangwei12 已提交
262

T
tangwei 已提交
263 264
    engine = which_engine(args)
    engine.run()