run.py 20.0 KB
Newer Older
T
tangwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

T
tangwei 已提交
15
import os
T
tangwei 已提交
16
import subprocess
X
test  
xjqbest 已提交
17
import sys
T
tangwei 已提交
18
import argparse
19
import warnings
T
tangwei 已提交
20

X
fix  
xjqbest 已提交
21
import copy
22 23 24
from paddlerec.core.factory import TrainerFactory
from paddlerec.core.utils import envs
from paddlerec.core.utils import util
X
test  
xjqbest 已提交
25
from paddlerec.core.utils import validation
T
tangwei 已提交
26

T
tangwei 已提交
27 28
engines = {}
device = ["CPU", "GPU"]
J
Jinhua Liang 已提交
29 30

engine_choices = ["TRAIN", "INFER", "LOCAL_CLUSTER_TRAIN", "CLUSTER_TRAIN"]
T
tangwei 已提交
31 32


T
tangwei 已提交
33
def engine_registry():
T
tangwei 已提交
34 35 36
    engines["TRANSPILER"] = {}
    engines["PSLIB"] = {}

C
Chengmo 已提交
37 38 39
    engines["TRANSPILER"]["TRAIN"] = single_train_engine
    engines["TRANSPILER"]["INFER"] = single_infer_engine
    engines["TRANSPILER"]["LOCAL_CLUSTER_TRAIN"] = local_cluster_engine
C
Chengmo 已提交
40
    engines["TRANSPILER"]["CLUSTER_TRAIN"] = cluster_engine
T
tangwei12 已提交
41
    engines["TRANSPILER"]["ONLINE_LEARNING"] = online_learning
C
Chengmo 已提交
42 43 44
    engines["PSLIB"]["TRAIN"] = local_mpi_engine
    engines["PSLIB"]["LOCAL_CLUSTER_TRAIN"] = local_mpi_engine
    engines["PSLIB"]["CLUSTER_TRAIN"] = cluster_mpi_engine
T
tangwei 已提交
45
    engines["PSLIB"]["CLUSTER"] = cluster_mpi_engine
T
tangwei 已提交
46

T
tangwei 已提交
47

X
fix  
xjqbest 已提交
48
def get_inters_from_yaml(file, filters):
X
test  
xjqbest 已提交
49
    _envs = envs.load_yaml(file)
T
tangwei 已提交
50 51 52
    flattens = envs.flatten_environs(_envs)
    inters = {}
    for k, v in flattens.items():
X
fix  
xjqbest 已提交
53 54 55
        for f in filters:
            if k.startswith(f):
                inters[k] = v
T
tangwei 已提交
56
    return inters
T
tangwei 已提交
57 58


X
fix  
xjqbest 已提交
59
def get_all_inters_from_yaml(file, filters):
C
Chengmo 已提交
60
    _envs = envs.load_yaml(file)
X
fix  
xjqbest 已提交
61 62 63 64 65 66 67 68 69 70 71 72
    all_flattens = {}

    def fatten_env_namespace(namespace_nests, local_envs):
        for k, v in local_envs.items():
            if isinstance(v, dict):
                nests = copy.deepcopy(namespace_nests)
                nests.append(k)
                fatten_env_namespace(nests, v)
            elif (k == "dataset" or k == "phase" or
                  k == "runner") and isinstance(v, list):
                for i in v:
                    if i.get("name") is None:
C
Chengmo 已提交
73
                        raise ValueError("name must be in dataset list. ", v)
X
fix  
xjqbest 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
                    nests = copy.deepcopy(namespace_nests)
                    nests.append(k)
                    nests.append(i["name"])
                    fatten_env_namespace(nests, i)
            else:
                global_k = ".".join(namespace_nests + [k])
                all_flattens[global_k] = v

    fatten_env_namespace([], _envs)
    ret = {}
    for k, v in all_flattens.items():
        for f in filters:
            if k.startswith(f):
                ret[k] = v
    return ret


T
tangwei 已提交
91 92 93 94 95 96 97 98
def get_modes(running_config):
    if not isinstance(running_config, dict):
        raise ValueError("get_modes arguments must be [dict]")

    modes = running_config.get("mode")
    if not modes:
        raise ValueError("yaml mast have config: mode")

T
tangwei 已提交
99 100 101
    if isinstance(modes, str):
        modes = [modes]

T
tangwei 已提交
102 103 104 105
    return modes


def get_engine(args, running_config, mode):
T
tangwei 已提交
106
    transpiler = get_transpiler()
T
tangwei 已提交
107

T
tangwei 已提交
108 109
    engine_class = ".".join(["runner", mode, "class"])
    engine_device = ".".join(["runner", mode, "device"])
C
Chengmo 已提交
110
    device_gpu_choices = ".".join(["runner", mode, "selected_gpus"])
T
tangwei 已提交
111 112

    engine = running_config.get(engine_class, None)
X
fix  
xjqbest 已提交
113
    if engine is None:
C
Chengmo 已提交
114 115
        raise ValueError("not find {} in engine_class , please check".format(
            engine))
T
tangwei 已提交
116 117
    device = running_config.get(engine_device, None)

T
tangwei 已提交
118
    engine = engine.upper()
T
tangwei 已提交
119
    device = device.upper()
C
chengmo 已提交
120

T
tangwei 已提交
121 122 123
    if device is None:
        print("not find device be specified in yaml, set CPU as default")
        device = "CPU"
T
tangwei 已提交
124

T
tangwei 已提交
125
    if device == "GPU":
T
tangwei 已提交
126
        selected_gpus = running_config.get(device_gpu_choices, None)
T
tangwei 已提交
127

T
tangwei 已提交
128 129 130 131
        if selected_gpus is None:
            print(
                "not find selected_gpus be specified in yaml, set `0` as default"
            )
C
Chengmo 已提交
132
            selected_gpus = "0"
T
tangwei 已提交
133 134 135
        else:
            print("selected_gpus {} will be specified for running".format(
                selected_gpus))
T
tangwei 已提交
136

C
Chengmo 已提交
137 138
        selected_gpus_num = len(selected_gpus.split(","))
        if selected_gpus_num > 1:
J
Jinhua Liang 已提交
139
            engine = "LOCAL_CLUSTER_TRAIN"
C
Chengmo 已提交
140

T
tangwei 已提交
141
    if engine not in engine_choices:
C
Chengmo 已提交
142 143
        raise ValueError("{} can only be chosen in {}".format(engine_class,
                                                              engine_choices))
T
tangwei 已提交
144

T
tangwei 已提交
145
    run_engine = engines[transpiler].get(engine, None)
T
tangwei 已提交
146 147 148 149
    return run_engine


def get_transpiler():
T
tangwei 已提交
150
    FNULL = open(os.devnull, 'w')
T
tangwei 已提交
151 152 153 154
    cmd = [
        "python", "-c",
        "import paddle.fluid as fluid; fleet_ptr = fluid.core.Fleet(); [fleet_ptr.copy_table_by_feasign(10, 10, [2020, 1010])];"
    ]
T
tangwei 已提交
155 156 157
    proc = subprocess.Popen(cmd, stdout=FNULL, stderr=FNULL, cwd=os.getcwd())
    ret = proc.wait()
    if ret == -11:
T
tangwei 已提交
158
        return "PSLIB"
T
tangwei 已提交
159
    else:
T
tangwei 已提交
160
        return "TRANSPILER"
T
tangwei 已提交
161 162


T
tangwei 已提交
163 164 165
def set_runtime_envs(cluster_envs, engine_yaml):
    if cluster_envs is None:
        cluster_envs = {}
T
tangwei 已提交
166 167

    envs.set_runtime_environs(cluster_envs)
T
fix bug  
tangwei 已提交
168 169 170

    need_print = {}
    for k, v in os.environ.items():
T
tangwei 已提交
171
        if k.startswith("train.trainer."):
T
fix bug  
tangwei 已提交
172 173 174
            need_print[k] = v

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


C
Chengmo 已提交
177
def single_train_engine(args):
T
tangwei 已提交
178 179 180 181 182 183
    run_extras = get_all_inters_from_yaml(args.model, ["runner."])
    mode = envs.get_runtime_environ("mode")
    trainer_class = ".".join(["runner", mode, "trainer_class"])
    fleet_class = ".".join(["runner", mode, "fleet_mode"])
    device_class = ".".join(["runner", mode, "device"])
    selected_gpus_class = ".".join(["runner", mode, "selected_gpus"])
T
tangwei 已提交
184

T
tangwei 已提交
185 186 187 188 189
    trainer = run_extras.get(trainer_class, "GeneralTrainer")
    fleet_mode = run_extras.get(fleet_class, "ps")
    device = run_extras.get(device_class, "cpu")
    selected_gpus = run_extras.get(selected_gpus_class, "0")
    executor_mode = "train"
C
chengmo 已提交
190 191

    single_envs = {}
C
Chengmo 已提交
192 193

    if device.upper() == "GPU":
T
tangwei 已提交
194 195 196 197 198 199 200 201
        selected_gpus_num = len(selected_gpus.split(","))
        if selected_gpus_num != 1:
            raise ValueError(
                "Single Mode Only Support One GPU, Set Local Cluster Mode to use Multi-GPUS"
            )

        single_envs["selsected_gpus"] = selected_gpus
        single_envs["FLAGS_selected_gpus"] = selected_gpus
C
chengmo 已提交
202

C
chengmo 已提交
203
    single_envs["train.trainer.trainer"] = trainer
C
Chengmo 已提交
204 205
    single_envs["fleet_mode"] = fleet_mode
    single_envs["train.trainer.executor_mode"] = executor_mode
C
chengmo 已提交
206 207 208
    single_envs["train.trainer.threads"] = "2"
    single_envs["train.trainer.platform"] = envs.get_platform()
    single_envs["train.trainer.engine"] = "single"
C
Chengmo 已提交
209

X
fix  
xjqbest 已提交
210 211 212
    set_runtime_envs(single_envs, args.model)
    trainer = TrainerFactory.create(args.model)
    return trainer
X
fix  
xjqbest 已提交
213

X
fix  
xjqbest 已提交
214 215

def single_infer_engine(args):
T
tangwei 已提交
216
    run_extras = get_all_inters_from_yaml(args.model, ["runner."])
C
Chengmo 已提交
217

T
tangwei 已提交
218 219 220 221 222
    mode = envs.get_runtime_environ("mode")
    trainer_class = ".".join(["runner", mode, "trainer_class"])
    fleet_class = ".".join(["runner", mode, "fleet_mode"])
    device_class = ".".join(["runner", mode, "device"])
    selected_gpus_class = ".".join(["runner", mode, "selected_gpus"])
C
Chengmo 已提交
223

224 225 226 227 228 229 230 231
    epochs_class = ".".join(["runner", mode, "epochs"])
    epochs = run_extras.get(epochs_class, 1)
    if epochs > 1:
        warnings.warn(
            "It makes no sense to predict the same model for multiple epochs",
            category=UserWarning,
            stacklevel=2)

T
tangwei 已提交
232 233 234 235
    trainer = run_extras.get(trainer_class, "GeneralTrainer")
    fleet_mode = run_extras.get(fleet_class, "ps")
    device = run_extras.get(device_class, "cpu")
    selected_gpus = run_extras.get(selected_gpus_class, "0")
C
Chengmo 已提交
236 237
    executor_mode = "infer"

T
tangwei 已提交
238 239
    single_envs = {}

C
Chengmo 已提交
240
    if device.upper() == "GPU":
T
tangwei 已提交
241 242 243 244 245 246 247 248
        selected_gpus_num = len(selected_gpus.split(","))
        if selected_gpus_num != 1:
            raise ValueError(
                "Single Mode Only Support One GPU, Set Local Cluster Mode to use Multi-GPUS"
            )

        single_envs["selsected_gpus"] = selected_gpus
        single_envs["FLAGS_selected_gpus"] = selected_gpus
C
Chengmo 已提交
249

X
fix  
xjqbest 已提交
250
    single_envs["train.trainer.trainer"] = trainer
C
Chengmo 已提交
251 252
    single_envs["train.trainer.executor_mode"] = executor_mode
    single_envs["fleet_mode"] = fleet_mode
X
fix  
xjqbest 已提交
253
    single_envs["train.trainer.threads"] = "2"
C
chengmo 已提交
254
    single_envs["train.trainer.platform"] = envs.get_platform()
C
Chengmo 已提交
255
    single_envs["train.trainer.engine"] = "single"
C
chengmo 已提交
256 257 258 259 260

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

S
seiriosPlus 已提交
261

T
tangwei12 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274
def online_learning(args):
    trainer = "OnlineLearningTrainer"
    single_envs = {}
    single_envs["train.trainer.trainer"] = trainer
    single_envs["train.trainer.threads"] = "2"
    single_envs["train.trainer.engine"] = "online_learning"
    single_envs["train.trainer.platform"] = envs.get_platform()
    print("use {} engine to run model: {}".format(trainer, args.model))

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

C
chengmo 已提交
275

T
tangwei 已提交
276
def cluster_engine(args):
T
tangwei 已提交
277
    def master():
278
        from paddlerec.core.engine.cluster.cluster import ClusterEngine
T
tangwei 已提交
279

C
Chengmo 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        # Get fleet_mode & device
        run_extras = get_all_inters_from_yaml(args.model, ["runner."])
        mode = envs.get_runtime_environ("mode")
        fleet_class = ".".join(["runner", mode, "fleet_mode"])
        device_class = ".".join(["runner", mode, "device"])
        fleet_mode = run_extras.get(fleet_class, "ps")
        device = run_extras.get(device_class, "cpu")
        device = device.upper()
        fleet_mode = fleet_mode.upper()

        if fleet_mode == "COLLECTIVE" and device != "GPU":
            raise ValueError("COLLECTIVE can not be used without GPU")

        # Get Thread nums
        model_envs = envs.load_yaml(args.model)
        phases_class = ".".join(["runner", mode, "phases"])
        phase_names = run_extras.get(phases_class)
        phases = []
        all_phases = model_envs.get("phase")
        if phase_names is None:
            phases = all_phases
        else:
            for phase in all_phases:
                if phase["name"] in phase_names:
                    phases.append(phase)

        thread_num = []
        for phase in phases:
            thread_num.append(int(phase["thread_num"]))
        max_thread_num = max(thread_num)

        backend_envs = envs.load_yaml(args.backend)
        flattens = envs.flatten_environs(backend_envs, "_")
J
Jinhua Liang 已提交
313 314
        flattens["engine_role"] = "MASTER"
        flattens["engine_mode"] = envs.get_runtime_environ("mode")
T
tangwei 已提交
315
        flattens["engine_run_config"] = args.model
C
Chengmo 已提交
316 317 318 319
        flattens["max_thread_num"] = max_thread_num
        flattens["fleet_mode"] = fleet_mode
        flattens["device"] = device
        flattens["backend_yaml"] = args.backend
T
tangwei 已提交
320 321 322 323 324
        envs.set_runtime_environs(flattens)

        launch = ClusterEngine(None, args.model)
        return launch

J
Jinhua Liang 已提交
325 326 327
    def worker(mode):
        if not mode:
            raise ValueError("mode: {} can not be recognized")
C
Chengmo 已提交
328
        from paddlerec.core.engine.cluster.cluster import ClusterEngine
J
Jinhua Liang 已提交
329 330 331 332 333 334 335 336 337 338 339 340

        run_extras = get_all_inters_from_yaml(args.model, ["runner."])

        trainer_class = ".".join(["runner", mode, "trainer_class"])
        fleet_class = ".".join(["runner", mode, "fleet_mode"])
        device_class = ".".join(["runner", mode, "device"])
        strategy_class = ".".join(["runner", mode, "distribute_strategy"])
        trainer = run_extras.get(trainer_class, "GeneralTrainer")
        fleet_mode = run_extras.get(fleet_class, "ps")
        device = run_extras.get(device_class, "cpu")
        distributed_strategy = run_extras.get(strategy_class, "async")
        executor_mode = "train"
C
Chengmo 已提交
341

J
Jinhua Liang 已提交
342 343 344
        device = device.upper()
        fleet_mode = fleet_mode.upper()
        if fleet_mode == "COLLECTIVE" and device != "GPU":
C
Chengmo 已提交
345
            raise ValueError("COLLECTIVE can not be used without GPU")
C
Chengmo 已提交
346

T
tangwei 已提交
347
        cluster_envs = {}
C
Chengmo 已提交
348 349

        cluster_envs["fleet_mode"] = fleet_mode
C
Chengmo 已提交
350
        cluster_envs["engine_role"] = "WORKER"
T
tangwei 已提交
351 352
        cluster_envs["train.trainer.trainer"] = trainer
        cluster_envs["train.trainer.engine"] = "cluster"
J
Jinhua Liang 已提交
353
        cluster_envs["train.trainer.executor_mode"] = executor_mode
C
Chengmo 已提交
354
        cluster_envs["train.trainer.strategy"] = distributed_strategy
T
tangwei 已提交
355 356
        cluster_envs["train.trainer.threads"] = envs.get_runtime_environ(
            "CPU_NUM")
T
tangwei 已提交
357
        cluster_envs["train.trainer.platform"] = envs.get_platform()
C
chengmo 已提交
358 359
        print("launch {} engine with cluster to with model: {}".format(
            trainer, args.model))
T
tangwei 已提交
360

C
Chengmo 已提交
361 362 363
        set_runtime_envs(cluster_envs, args.model)
        launch = ClusterEngine(None, args.model)
        return launch
T
tangwei 已提交
364

T
tangwei 已提交
365 366 367
    role = os.getenv("PADDLE_PADDLEREC_ROLE", "MASTER")

    if role == "WORKER":
C
Chengmo 已提交
368
        mode = os.getenv("mode", None)
J
Jinhua Liang 已提交
369
        return worker(mode)
T
tangwei 已提交
370 371
    else:
        return master()
C
chengmo 已提交
372 373


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

T
fix bug  
tangwei 已提交
378
    cluster_envs = {}
T
tangwei 已提交
379
    cluster_envs["train.trainer.trainer"] = "CtrCodingTrainer"
T
tangwei 已提交
380
    cluster_envs["train.trainer.platform"] = envs.get_platform()
T
tangwei 已提交
381

T
tangwei 已提交
382
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
383

T
tangwei 已提交
384 385 386 387 388
    trainer = TrainerFactory.create(args.model)
    return trainer


def local_cluster_engine(args):
J
Jinhua Liang 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    def get_worker_num(run_extras, workers):
        _envs = envs.load_yaml(args.model)
        mode = envs.get_runtime_environ("mode")
        workspace = envs.get_runtime_environ("workspace")
        phases_class = ".".join(["runner", mode, "phases"])
        phase_names = run_extras.get(phases_class)
        phases = []
        all_phases = _envs.get("phase")
        if phase_names is None:
            phases = all_phases
        else:
            for phase in all_phases:
                if phase["name"] in phase_names:
                    phases.append(phase)

        dataset_names = []
        for phase in phases:
            dataset_names.append(phase["dataset_name"])

        datapaths = []
        for dataset in _envs.get("dataset"):
            if dataset["name"] in dataset_names:
                datapaths.append(dataset["data_path"])

        if not datapaths:
            raise ValueError("data path must exist for training/inference")

        datapaths = [
            envs.workspace_adapter_by_specific(path, workspace)
            for path in datapaths
        ]
420

J
Jinhua Liang 已提交
421 422
        all_workers = [len(os.listdir(path)) for path in datapaths]
        all_workers.append(workers)
423 424 425 426 427 428 429 430 431 432
        max_worker_num = min(all_workers)

        if max_worker_num >= workers:
            return workers

        print(
            "phases do not have enough datas for training, set worker/gpu cards num from {} to {}".
            format(workers, max_worker_num))

        return max_worker_num
C
chengmo 已提交
433

434
    from paddlerec.core.engine.local_cluster import LocalClusterEngine
C
chengmo 已提交
435

J
Jinhua Liang 已提交
436 437 438 439 440 441 442 443 444
    run_extras = get_all_inters_from_yaml(args.model, ["runner."])
    mode = envs.get_runtime_environ("mode")
    trainer_class = ".".join(["runner", mode, "trainer_class"])
    fleet_class = ".".join(["runner", mode, "fleet_mode"])
    device_class = ".".join(["runner", mode, "device"])
    selected_gpus_class = ".".join(["runner", mode, "selected_gpus"])
    strategy_class = ".".join(["runner", mode, "distribute_strategy"])
    worker_class = ".".join(["runner", mode, "worker_num"])
    server_class = ".".join(["runner", mode, "server_num"])
C
Chengmo 已提交
445

J
Jinhua Liang 已提交
446 447 448 449 450
    trainer = run_extras.get(trainer_class, "GeneralTrainer")
    fleet_mode = run_extras.get(fleet_class, "ps")
    device = run_extras.get(device_class, "cpu")
    selected_gpus = run_extras.get(selected_gpus_class, "0")
    distributed_strategy = run_extras.get(strategy_class, "async")
C
Chengmo 已提交
451
    executor_mode = "train"
J
Jinhua Liang 已提交
452 453 454 455 456 457 458

    worker_num = run_extras.get(worker_class, 1)
    server_num = run_extras.get(server_class, 1)

    device = device.upper()
    fleet_mode = fleet_mode.upper()

C
chengmo 已提交
459
    cluster_envs = {}
460 461 462 463 464 465 466 467 468

    # Todo: delete follow hard code when paddle support ps-gpu.
    if device == "CPU":
        fleet_mode = "PS"
    elif device == "GPU":
        fleet_mode = "COLLECTIVE"
    if fleet_mode == "PS" and device != "CPU":
        raise ValueError("PS can not be used with GPU")

J
Jinhua Liang 已提交
469
    if fleet_mode == "COLLECTIVE" and device != "GPU":
470
        raise ValueError("COLLECTIVE can not be used without GPU")
C
Chengmo 已提交
471

472 473
    if fleet_mode == "PS":
        worker_num = get_worker_num(run_extras, worker_num)
J
Jinhua Liang 已提交
474

475
    if fleet_mode == "COLLECTIVE":
J
Jinhua Liang 已提交
476
        cluster_envs["selected_gpus"] = selected_gpus
477
        gpus = selected_gpus.split(",")
C
Chengmo 已提交
478 479
        worker_num = get_worker_num(run_extras, len(gpus))
        cluster_envs["selected_gpus"] = ','.join(gpus[:worker_num])
J
Jinhua Liang 已提交
480

C
Chengmo 已提交
481 482
    cluster_envs["server_num"] = server_num
    cluster_envs["worker_num"] = worker_num
C
chengmo 已提交
483
    cluster_envs["start_port"] = envs.find_free_port()
C
Chengmo 已提交
484
    cluster_envs["fleet_mode"] = fleet_mode
C
chengmo 已提交
485
    cluster_envs["log_dir"] = "logs"
C
chengmo 已提交
486
    cluster_envs["train.trainer.trainer"] = trainer
C
Chengmo 已提交
487 488
    cluster_envs["train.trainer.executor_mode"] = executor_mode
    cluster_envs["train.trainer.strategy"] = distributed_strategy
C
chengmo 已提交
489
    cluster_envs["train.trainer.threads"] = "2"
J
Jinhua Liang 已提交
490
    cluster_envs["CPU_NUM"] = cluster_envs["train.trainer.threads"]
C
chengmo 已提交
491 492 493
    cluster_envs["train.trainer.engine"] = "local_cluster"
    cluster_envs["train.trainer.platform"] = envs.get_platform()

T
tangwei 已提交
494 495
    print("launch {} engine with cluster to run model: {}".format(trainer,
                                                                  args.model))
C
chengmo 已提交
496 497 498 499 500 501

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


T
tangwei 已提交
502
def local_mpi_engine(args):
T
tangwei 已提交
503 504
    print("launch cluster engine with cluster to run model: {}".format(
        args.model))
505
    from paddlerec.core.engine.local_mpi import LocalMPIEngine
T
tangwei 已提交
506

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

T
tangwei 已提交
510 511 512
    mpi = util.run_which("mpirun")
    if not mpi:
        raise RuntimeError("can not find mpirun, please check environment")
C
Chengmo 已提交
513

J
Jinhua Liang 已提交
514 515 516 517 518 519
    run_extras = get_all_inters_from_yaml(args.model, ["runner."])

    mode = envs.get_runtime_environ("mode")
    trainer_class = ".".join(["runner", mode, "trainer_class"])
    fleet_class = ".".join(["runner", mode, "fleet_mode"])
    distributed_strategy = "async"
C
Chengmo 已提交
520 521
    executor_mode = "train"

J
Jinhua Liang 已提交
522 523
    trainer = run_extras.get(trainer_class, "GeneralTrainer")
    fleet_mode = run_extras.get(fleet_class, "ps")
C
Chengmo 已提交
524

T
fix bug  
tangwei 已提交
525 526
    cluster_envs = {}
    cluster_envs["mpirun"] = mpi
C
Chengmo 已提交
527
    cluster_envs["train.trainer.trainer"] = trainer
T
fix bug  
tangwei 已提交
528
    cluster_envs["log_dir"] = "logs"
T
tangwei 已提交
529
    cluster_envs["train.trainer.engine"] = "local_cluster"
C
Chengmo 已提交
530 531 532 533
    cluster_envs["train.trainer.executor_mode"] = executor_mode
    cluster_envs["fleet_mode"] = fleet_mode
    cluster_envs["train.trainer.strategy"] = distributed_strategy
    cluster_envs["train.trainer.threads"] = "2"
T
tangwei 已提交
534
    cluster_envs["train.trainer.platform"] = envs.get_platform()
T
tangwei 已提交
535

T
tangwei 已提交
536
    set_runtime_envs(cluster_envs, args.model)
T
tangwei 已提交
537 538 539 540
    launch = LocalMPIEngine(cluster_envs, args.model)
    return launch


T
tangwei 已提交
541
def get_abs_model(model):
542
    if model.startswith("paddlerec."):
T
tangwei 已提交
543
        dir = envs.paddlerec_adapter(model)
T
tangwei 已提交
544
        path = os.path.join(dir, "config.yaml")
T
tangwei 已提交
545 546 547 548 549 550 551
    else:
        if not os.path.isfile(model):
            raise IOError("model config: {} invalid".format(model))
        path = model
    return path


T
tangwei 已提交
552
if __name__ == "__main__":
553
    parser = argparse.ArgumentParser(description='paddle-rec run')
T
tangwei 已提交
554
    parser.add_argument("-m", "--model", type=str)
T
tangwei 已提交
555
    parser.add_argument("-b", "--backend", type=str, default=None)
T
tangwei 已提交
556

T
tangwei 已提交
557 558 559
    abs_dir = os.path.dirname(os.path.abspath(__file__))
    envs.set_runtime_environs({"PACKAGE_BASE": abs_dir})

T
tangwei 已提交
560
    args = parser.parse_args()
T
tangwei 已提交
561
    args.model = get_abs_model(args.model)
T
tangwei 已提交
562

X
test  
xjqbest 已提交
563 564
    if not validation.yaml_validation(args.model):
        sys.exit(-1)
T
tangwei 已提交
565

T
tangwei 已提交
566
    engine_registry()
J
Jinhua Liang 已提交
567 568
    running_config = get_all_inters_from_yaml(
        args.model, ["workspace", "mode", "runner."])
T
tangwei 已提交
569 570 571
    modes = get_modes(running_config)

    for mode in modes:
J
Jinhua Liang 已提交
572 573 574 575
        envs.set_runtime_environs({
            "mode": mode,
            "workspace": running_config["workspace"]
        })
T
tangwei 已提交
576 577 578
        which_engine = get_engine(args, running_config, mode)
        engine = which_engine(args)
        engine.run()