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)
        '''
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 33 34
        self._fleet_desc = None
        self._device_worker = None
        self._program = None
        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):
D
dongdaxiang 已提交
53
        self._infer = infer
54

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
        self._program = program
D
dongdaxiang 已提交
63

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)
D
dongdaxiang 已提交
76
        self._program = program
77

78
    def _gen_trainer_desc(self):
79
        super(MultiTrainer, self)._gen_trainer_desc()
D
dongdaxiang 已提交
80
        self.proto_desc.class_name = "MultiTrainer"
D
fix bug  
dongdaxiang 已提交
81
        self._device_worker._set_infer(self._infer)
D
dongdaxiang 已提交
82
        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)
D
dongdaxiang 已提交
92
        self._program = program
93

94
    def _gen_trainer_desc(self):
95
        super(DistMultiTrainer, self)._gen_trainer_desc()
96
        self.proto_desc.class_name = "DistMultiTrainer"
D
dongdaxiang 已提交
97
        if self._program == None:
98
            raise RuntimeError("None Program")
D
fix bug  
dongdaxiang 已提交
99 100
        self._device_worker._set_infer(self._infer)
        self._device_worker._set_program(self._program)
D
dongdaxiang 已提交
101
        self._device_worker._gen_worker_desc(self.proto_desc)