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
#
# 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 paddle.fluid.proto import trainer_desc_pb2
16 17
from distributed import ps_pb2 as ps_pb2
from device_worker import DeviceWorkerFactory
18 19 20 21 22
from google.protobuf import text_format

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


X
xujiaqi01 已提交
23
# can be initialized from train_desc,
24 25 26 27 28 29 30 31
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)
        '''
        self.proto_desc = trainer_desc_pb2.TrainerDesc()
D
dongdaxiang 已提交
32 33 34
        import multiprocessing as mp
        # set default thread num == cpu count
        self.proto_desc.thread_num = mp.cpu_count()
D
dongdaxiang 已提交
35 36
        self.fleet_desc_ = None
        self.device_worker_ = None
D
dongdaxiang 已提交
37
        self.program_ = None
38
        self.infer_ = False
39

D
dongdaxiang 已提交
40
    def set_fetch_var_and_info(self, fetch_vars, fetch_info, print_period):
D
dongdaxiang 已提交
41 42 43 44 45
        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 已提交
46

47 48 49
    def set_debug(self, debug):
        self.proto_desc.debug = debug

50 51 52
    def set_thread(self, thread_num):
        self.proto_desc.thread_num = thread_num

D
dongdaxiang 已提交
53 54
    def set_device_worker(self, device_worker):
        self.device_worker_ = device_worker
55

56 57 58
    def set_infer(self, infer):
        self.infer_ = infer

D
dongdaxiang 已提交
59 60
    def set_fleet_desc(self, fleet_desc):
        self.fleet_desc_ = fleet_desc
61

D
dongdaxiang 已提交
62
    def gen_trainer_desc(self):
63 64
        pass

D
dongdaxiang 已提交
65 66 67
    def set_program(self, program):
        self.program_ = program

68 69 70 71 72
    def _desc(self):
        return text_format.MessageToString(self.proto_desc)


class MultiTrainer(TrainerDesc):
D
dongdaxiang 已提交
73
    def __init__(self):
74
        super(MultiTrainer, self).__init__()
D
dongdaxiang 已提交
75
        pass
76

77 78 79 80
    def set_program(self, program):
        super(MultiTrainer, self).set_program(program)
        self.program_ = program

D
dongdaxiang 已提交
81 82 83
    def gen_trainer_desc(self):
        super(MultiTrainer, self).gen_trainer_desc()
        self.proto_desc.class_name = "MultiTrainer"
84
        self.device_worker_.set_infer(self.infer_)
85
        self.device_worker_.gen_worker_desc(self.proto_desc)
86

87 88

class DistMultiTrainer(TrainerDesc):
89
    def __init__(self):
90
        super(DistMultiTrainer, self).__init__()
91
        pass
92

93 94 95 96
    def set_program(self, program):
        super(DistMultiTrainer, self).set_program(program)
        self.program_ = program

D
dongdaxiang 已提交
97 98
    def gen_trainer_desc(self):
        super(DistMultiTrainer, self).gen_trainer_desc()
99
        self.proto_desc.class_name = "DistMultiTrainer"
100 101
        if self.program_ == None:
            print("None program")
102
        self.device_worker_.set_infer(self.infer_)
D
dongdaxiang 已提交
103
        self.device_worker_.set_program(self.program_)
104
        self.device_worker_.gen_worker_desc(self.proto_desc)