trainer_desc.py 3.4 KB
Newer Older
1
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# 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.

__all__ = ['TrainerDesc', 'MultiTrainer', 'DistMultiTrainer']


X
xujiaqi01 已提交
18
# can be initialized from train_desc,
19 20 21 22 23 24 25
class TrainerDesc(object):
    def __init__(self):
        '''
        self.proto_desc = data_feed_pb2.DataFeedDesc()
        with open(proto_file, 'r') as f:
            text_format.Parse(f.read(), self.proto_desc)
        '''
X
xjqbest 已提交
26
        from .proto import trainer_desc_pb2
27
        self.proto_desc = trainer_desc_pb2.TrainerDesc()
D
dongdaxiang 已提交
28 29 30
        import multiprocessing as mp
        # set default thread num == cpu count
        self.proto_desc.thread_num = mp.cpu_count()
D
dongdaxiang 已提交
31 32
        self.fleet_desc_ = None
        self.device_worker_ = None
D
dongdaxiang 已提交
33
        self.program_ = None
34
        self.infer_ = False
35

36
    def _set_fetch_var_and_info(self, fetch_vars, fetch_info, print_period):
D
dongdaxiang 已提交
37 38 39 40 41
        for i, v in enumerate(fetch_vars):
            self.proto_desc.fetch_config.fetch_var_names.extend([v.name])
            self.proto_desc.fetch_config.fetch_var_str_format.extend(
                [fetch_info[i]])
        self.proto_desc.fetch_config.print_period = print_period
D
dongdaxiang 已提交
42

43
    def _set_debug(self, debug):
44 45
        self.proto_desc.debug = debug

46
    def _set_thread(self, thread_num):
47 48
        self.proto_desc.thread_num = thread_num

49
    def _set_device_worker(self, device_worker):
D
dongdaxiang 已提交
50
        self.device_worker_ = device_worker
51

52
    def _set_infer(self, infer):
53 54
        self.infer_ = infer

55
    def _set_fleet_desc(self, fleet_desc):
D
dongdaxiang 已提交
56
        self.fleet_desc_ = fleet_desc
57

58
    def _gen_trainer_desc(self):
59 60
        pass

61
    def _set_program(self, program):
D
dongdaxiang 已提交
62 63
        self.program_ = program

64
    def _desc(self):
D
dongdaxiang 已提交
65
        from google.protobuf import text_format
66 67 68 69
        return text_format.MessageToString(self.proto_desc)


class MultiTrainer(TrainerDesc):
D
dongdaxiang 已提交
70
    def __init__(self):
71
        super(MultiTrainer, self).__init__()
D
dongdaxiang 已提交
72
        pass
73

74
    def _set_program(self, program):
75
        super(MultiTrainer, self)._set_program(program)
76 77
        self.program_ = program

78
    def _gen_trainer_desc(self):
79
        super(MultiTrainer, self)._gen_trainer_desc()
D
dongdaxiang 已提交
80
        self.proto_desc.class_name = "MultiTrainer"
81 82
        self.device_worker_._set_infer(self.infer_)
        self.device_worker_._gen_worker_desc(self.proto_desc)
83

84 85

class DistMultiTrainer(TrainerDesc):
86
    def __init__(self):
87
        super(DistMultiTrainer, self).__init__()
88
        pass
89

90
    def _set_program(self, program):
91
        super(DistMultiTrainer, self)._set_program(program)
92 93
        self.program_ = program

94
    def _gen_trainer_desc(self):
95
        super(DistMultiTrainer, self)._gen_trainer_desc()
96
        self.proto_desc.class_name = "DistMultiTrainer"
97
        if self.program_ == None:
98
            raise RuntimeError("None Program")
99 100 101
        self.device_worker_._set_infer(self.infer_)
        self.device_worker_._set_program(self.program_)
        self.device_worker_._gen_worker_desc(self.proto_desc)