factory.py 3.1 KB
Newer Older
T
tangwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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.

import os
T
tangwei 已提交
16 17
import sys

T
tangwei 已提交
18
import yaml
T
tangwei 已提交
19

20
from paddlerec.core.utils import envs
T
tangwei 已提交
21

T
tangwei 已提交
22 23
trainer_abs = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), "trainers")
T
tangwei 已提交
24 25 26 27
trainers = {}


def trainer_registry():
T
tangwei 已提交
28 29 30 31 32 33 34 35 36 37 38
    trainers["SingleTrainer"] = os.path.join(trainer_abs, "single_trainer.py")
    trainers["ClusterTrainer"] = os.path.join(trainer_abs,
                                              "cluster_trainer.py")
    trainers["CtrCodingTrainer"] = os.path.join(trainer_abs,
                                                "ctr_coding_trainer.py")
    trainers["CtrModulTrainer"] = os.path.join(trainer_abs,
                                               "ctr_modul_trainer.py")
    trainers["TDMSingleTrainer"] = os.path.join(trainer_abs,
                                                "tdm_single_trainer.py")
    trainers["TDMClusterTrainer"] = os.path.join(trainer_abs,
                                                 "tdm_cluster_trainer.py")
X
fix  
xjqbest 已提交
39 40
    trainers["SingleTrainerYamlOpt"] = os.path.join(trainer_abs,
                                                 "single_trainer_yamlopt.py")
X
fix  
xjqbest 已提交
41 42
    trainers["SingleAucYamlOpt"] = os.path.join(trainer_abs,
                                                 "single_auc_yamlopt.py")
T
tangwei 已提交
43 44 45

trainer_registry()

T
tangwei 已提交
46

T
tangwei 已提交
47 48 49 50 51
class TrainerFactory(object):
    def __init__(self):
        pass

    @staticmethod
T
tangwei 已提交
52
    def _build_trainer(yaml_path):
T
tangwei 已提交
53 54
        print(envs.pretty_print_envs(envs.get_global_envs()))

T
tangwei 已提交
55 56 57 58
        train_mode = envs.get_trainer()
        trainer_abs = trainers.get(train_mode, None)

        if trainer_abs is None:
T
tangwei 已提交
59
            if not os.path.isfile(train_mode):
T
tangwei 已提交
60 61
                raise IOError("trainer {} can not be recognized".format(
                    train_mode))
T
tangwei 已提交
62 63 64
            trainer_abs = train_mode
            train_mode = "UserDefineTrainer"

T
tangwei 已提交
65
        trainer_class = envs.lazy_instance_by_fliename(trainer_abs, train_mode)
T
tangwei 已提交
66
        trainer = trainer_class(yaml_path)
T
tangwei 已提交
67 68 69 70 71
        return trainer

    @staticmethod
    def create(config):
        _config = None
T
tangwei 已提交
72
        if os.path.isfile(config):
T
tangwei 已提交
73
            with open(config, 'r') as rb:
T
tangwei12 已提交
74
                _config = yaml.load(rb.read(), Loader=yaml.FullLoader)
T
tangwei 已提交
75
        else:
76
            raise ValueError("paddlerec's config only support yaml")
T
tangwei 已提交
77 78

        envs.set_global_envs(_config)
T
tangwei 已提交
79 80
        envs.update_workspace()

T
tangwei 已提交
81
        trainer = TrainerFactory._build_trainer(config)
T
tangwei 已提交
82
        return trainer
T
tangwei12 已提交
83

T
tangwei 已提交
84 85 86 87 88

# server num, worker num
if __name__ == "__main__":
    if len(sys.argv) != 2:
        raise ValueError("need a yaml file path argv")
T
tangwei12 已提交
89 90
    trainer = TrainerFactory.create(sys.argv[1])
    trainer.run()