cluster.py 2.4 KB
Newer Older
T
tangwei 已提交
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
T
tangwei 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# 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.

from __future__ import print_function
from __future__ import unicode_literals
T
tangwei 已提交
17

T
tangwei 已提交
18
import copy
T
tangwei 已提交
19 20
import os
import subprocess
T
tangwei 已提交
21

22 23 24
from paddlerec.core.engine.engine import Engine
from paddlerec.core.factory import TrainerFactory
from paddlerec.core.utils import envs
T
tangwei 已提交
25 26


T
tangwei 已提交
27
class ClusterEngine(Engine):
T
tangwei 已提交
28 29
    def __init_impl__(self):
        abs_dir = os.path.dirname(os.path.abspath(__file__))
T
tangwei 已提交
30

T
tangwei 已提交
31
        backend = envs.get_runtime_environ("engine_backend")
J
Jinhua Liang 已提交
32 33 34 35
        if not backend:
            backend = ""
        backend = backend.upper()
        if backend == "PADDLECLOUD":
T
tangwei 已提交
36
            self.submit_script = os.path.join(abs_dir, "cloud/cluster.sh")
J
Jinhua Liang 已提交
37 38
        elif backend == "KUBERNETES":
            self.submit_script = os.path.join(abs_dir, "k8s/cluster.sh")
T
tangwei 已提交
39
        else:
T
tangwei 已提交
40
            raise ValueError("{} can not be supported now".format(backend))
T
tangwei 已提交
41

T
tangwei 已提交
42
    def start_worker_procs(self):
T
tangwei 已提交
43 44
        trainer = TrainerFactory.create(self.trainer)
        trainer.run()
T
tangwei 已提交
45 46

    def start_master_procs(self):
T
tangwei 已提交
47 48 49 50
        default_env = os.environ.copy()
        current_env = copy.copy(default_env)
        current_env.pop("http_proxy", None)
        current_env.pop("https_proxy", None)
T
tangwei 已提交
51 52 53 54 55

        cmd = ("bash {}".format(self.submit_script)).split(" ")
        proc = subprocess.Popen(cmd, env=current_env, cwd=os.getcwd())
        proc.wait()

J
Jinhua Liang 已提交
56 57 58 59 60 61 62 63
    @staticmethod
    def workspace_replace():
        workspace = envs.get_runtime_environ("engine_workspace")

        for k, v in os.environ.items():
            v = v.replace("{workspace}", workspace)
            os.environ[k] = str(v)

T
tangwei 已提交
64
    def run(self):
T
tangwei 已提交
65 66 67 68 69 70 71 72 73
        role = envs.get_runtime_environ("engine_role")

        if role == "MASTER":
            self.start_master_procs()

        elif role == "WORKER":
            self.start_worker_procs()

        else:
T
tangwei 已提交
74 75
            raise ValueError("role {} error, must in MASTER/WORKER".format(
                role))