trainer_desc.py 4.1 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
#
# 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.

H
hutuxian 已提交
15
__all__ = ['TrainerDesc', 'MultiTrainer', 'DistMultiTrainer', 'PipelineTrainer']
16 17


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 65 66
    def _set_use_cvm(self, use_cvm=False):
        self.proto_desc.use_cvm = use_cvm

67
    def _desc(self):
D
dongdaxiang 已提交
68
        from google.protobuf import text_format
H
hutuxian 已提交
69
        return self.proto_desc.SerializeToString()
70 71 72


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

77
    def _set_program(self, program):
78
        super(MultiTrainer, self)._set_program(program)
D
dongdaxiang 已提交
79
        self._program = program
80

81
    def _gen_trainer_desc(self):
82
        super(MultiTrainer, self)._gen_trainer_desc()
D
dongdaxiang 已提交
83
        self.proto_desc.class_name = "MultiTrainer"
D
fix bug  
dongdaxiang 已提交
84
        self._device_worker._set_infer(self._infer)
D
dongdaxiang 已提交
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
    def _set_program(self, program):
94
        super(DistMultiTrainer, self)._set_program(program)
D
dongdaxiang 已提交
95
        self._program = program
96

97
    def _gen_trainer_desc(self):
98
        super(DistMultiTrainer, self)._gen_trainer_desc()
99
        self.proto_desc.class_name = "DistMultiTrainer"
D
dongdaxiang 已提交
100
        if self._program == None:
101
            raise RuntimeError("None Program")
D
fix bug  
dongdaxiang 已提交
102 103
        self._device_worker._set_infer(self._infer)
        self._device_worker._set_program(self._program)
D
dongdaxiang 已提交
104
        self._device_worker._gen_worker_desc(self.proto_desc)
H
hutuxian 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123


class PipelineTrainer(TrainerDesc):
    def __init__(self):
        super(PipelineTrainer, self).__init__()
        pass

    def _set_program(self, program):
        super(PipelineTrainer, self)._set_program(program)
        self._program = program

    def _gen_trainer_desc(self):
        super(PipelineTrainer, self)._gen_trainer_desc()
        self.proto_desc.class_name = "PipelineTrainer"
        if self._program == None:
            raise RuntimeError("None Program")
        self._device_worker._set_infer(self._infer)
        self._device_worker._set_program(self._program)
        self._device_worker._gen_worker_desc(self.proto_desc)