fl_trainer.py 6.1 KB
Newer Older
G
guru4elephant 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2019 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 paddle.fluid as fluid
G
guru4elephant 已提交
15
import logging
16
import numpy
G
guru4elephant 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30

class FLTrainerFactory(object):
    def __init__(self):
        pass

    def create_fl_trainer(self, job):
        strategy = job._strategy
        trainer = None
        if strategy._fed_avg == True:
            trainer = FedAvgTrainer()
            trainer.set_trainer_job(job)
        elif strategy._dpsgd == True:
            trainer = FLTrainer()
            trainer.set_trainer_job(job)
31 32 33
        elif strategy._sec_agg == True:
            trainer = SecAggTrainer()
            trainer.set_trainer_job(job)
G
guru4elephant 已提交
34 35 36 37 38 39
        trainer.set_trainer_job(job)
        return trainer


class FLTrainer(object):
    def __init__(self):
G
guru4elephant 已提交
40
        self._logger = logging.getLogger("FLTrainer")
G
guru4elephant 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        pass

    def set_trainer_job(self, job):
        self._startup_program = \
            job._trainer_startup_program
        self._main_program = \
            job._trainer_main_program
        self._step = job._strategy._inner_step
        self._feed_names = job._feed_names
        self._target_names = job._target_names

    def start(self):
        self.exe = fluid.Executor(fluid.CPUPlace())
        self.exe.run(self._startup_program)

G
guru4elephant 已提交
56 57 58 59 60 61
    def run(self, feed, fetch):
        self._logger.debug("begin to run")
        self.exe.run(self._main_program,
                     feed=feed,
                     fetch_list=fetch)
        self._logger.debug("end to run current batch")
G
guru4elephant 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    def save_inference_program(self, output_folder):
        target_vars = []
        infer_program = self._main_program.clone(for_test=True)
        for name in self._target_names:
            tmp_var = self._main_program.block(0)._find_var_recursive(name)
            target_vars.append(tmp_var)
        fluid.io.save_inference_model(
            output_folder,
            self._feed_names,
            target_vars,
            self.exe,
            main_program=infer_program)

    def stop(self):
        # ask for termination with master endpoint
        # currently not open sourced, will release the code later
        # TODO(guru4elephant): add connection with master
        return False

class FedAvgTrainer(FLTrainer):
    def __init__(self):
        super(FedAvgTrainer, self).__init__()
        pass

    def start(self):
        self.exe = fluid.Executor(fluid.CPUPlace())
        self.exe.run(self._startup_program)
G
guru4elephant 已提交
90
        self.cur_step = 0
G
guru4elephant 已提交
91 92 93 94 95 96

    def set_trainer_job(self, job):
        super(FedAvgTrainer, self).set_trainer_job(job)
        self._send_program = job._trainer_send_program
        self._recv_program = job._trainer_recv_program

G
guru4elephant 已提交
97 98 99
    def reset(self):
        self.cur_step = 0

100
    def run(self, feed, fetch, train_id, mask):
G
guru4elephant 已提交
101 102
        self._logger.debug("begin to run FedAvgTrainer, cur_step=%d, inner_step=%d" %
                           (self.cur_step, self._step))
G
guru4elephant 已提交
103
        if self.cur_step % self._step == 0:
G
guru4elephant 已提交
104
            self._logger.debug("begin to run recv program")
G
guru4elephant 已提交
105
            self.exe.run(self._recv_program)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        scope = fluid.global_scope()
        print("****** fc_0.b_0: {0}".format(numpy.array(scope.find_var("fc_0.b_0").get_tensor())))
        print("****** fc_0.w_0: {0}".format(numpy.array(scope.find_var("fc_0.w_0").get_tensor())))
        self._logger.debug("begin to run current step")
        loss = self.exe.run(self._main_program, 
                     feed=feed,
                     fetch_list=fetch)
        if self.cur_step % self._step == 0:
            self._logger.debug("begin to run send program")
            scope = fluid.global_scope()
            name1 = "fc_0.b_0.opti.trainer_" + str(train_id)
            name2 = "fc_0.w_0.opti.trainer_" + str(train_id)
            fluid.global_scope().var(name1).get_tensor().set(numpy.array(scope.find_var(name1).get_tensor()) + mask, fluid.CPUPlace())
            fluid.global_scope().var(name2).get_tensor().set(numpy.array(scope.find_var(name2).get_tensor()) + mask, fluid.CPUPlace())
            self.exe.run(self._send_program)
        self.cur_step += 1
        return loss

    def stop(self):
        return False
       
 
class SecAggTrainer(FLTrainer):
    def __init__(self):
        super(SecAggTrainer, self).__init__()
        pass

    def start(self):
        self.exe = fluid.Executor(fluid.CPUPlace())
        self.exe.run(self._startup_program)
        self.cur_step = 0

    def set_trainer_job(self, job):
        super(SecAggTrainer, self).set_trainer_job(job)
        self._send_program = job._trainer_send_program
        self._recv_program = job._trainer_recv_program

    def reset(self):
        self.cur_step = 0

    def run(self, feed, fetch, param_name_list, mask):
        self._logger.debug("begin to run SecAggTrainer, cur_step=%d, inner_step=%d" %
                           (self.cur_step, self._step))
        if self.cur_step % self._step == 0:
            self._logger.debug("begin to run recv program")
            self.exe.run(self._recv_program)
        scope = fluid.global_scope()
G
guru4elephant 已提交
153
        self._logger.debug("begin to run current step")
F
frankwhzhang 已提交
154
        loss = self.exe.run(self._main_program, 
G
guru4elephant 已提交
155 156 157
                     feed=feed,
                     fetch_list=fetch)
        if self.cur_step % self._step == 0:
G
guru4elephant 已提交
158
            self._logger.debug("begin to run send program")
159 160 161
            scope = fluid.global_scope()
            for param_name in param_name_list:
                fluid.global_scope().var(param_name).get_tensor().set(numpy.array(scope.find_var(param_name).get_tensor()) + mask, fluid.CPUPlace())
G
guru4elephant 已提交
162 163
            self.exe.run(self._send_program)
        self.cur_step += 1
F
frankwhzhang 已提交
164
        return loss
G
guru4elephant 已提交
165

G
guru4elephant 已提交
166 167
    def stop(self):
        return False
168