mp_distiller.py 9.0 KB
Newer Older
B
baiyfbupt 已提交
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.

B
baiyfbupt 已提交
15 16 17 18 19 20 21 22 23
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import logging
import numpy as np
from six.moves.queue import Queue

B
baiyfbupt 已提交
24 25 26 27 28
import paddle.fluid as fluid
from paddle.fluid.framework import Variable
from paddle.fluid.reader import DataLoaderBase
from paddle.fluid.core import EOFException
from paddle.fluid.incubate.fleet.utils.hdfs import HDFSClient
B
baiyfbupt 已提交
29 30 31

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
B
baiyfbupt 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

__all__ = ['Knowledge']


class Knowledge(object):
    """
    The knowledge class describes how to extract and store the dark knowledge
    of the teacher model, and how the student model learns these dark knowledge.
    """

    def __init__(self,
                 path,
                 items,
                 reduce_strategy={'type': 'sum',
                                  'key': 'image'}):
        """Init a knowledge instance.
        Args:
            path(list<str>, str, optional): Specifies the storage path of the knowledge,
                       supports AFS/HDFS, local file system, and memory.
            items(list<str>): Save the tensor of the specified name
            reduce_strategy(dict, optional): The policy for performing the reduce
                                   operation. If it is set to None,
                                   the reduce operation is not performed.
            reduce_strategy.type(str): Type of reduce operation.
            reduce_strategy.key(str): The key of the reduce operation.
                                      It is an element in the item.
        """
        assert (isinstance(path, list) or isinstance(path, str) or
                (path is None)), "path type should be list or str or None"
        assert (isinstance(items, list)), "items should be a list"
        assert (isinstance(reduce_strategy,
                           dict)), "reduce_strategy should be a dict"
        self.path = path
        if isinstance(self.path, list):
            self.write_type = 'HDFS/AFS'
B
baiyfbupt 已提交
67 68 69
            assert (
                len(self.path) == 4 and isinstance(self.path[0], str) and
                isinstance(self.path[1], str) and
B
baiyfbupt 已提交
70
                isinstance(self.path[2], str) and isinstance(self.path[3], str)
B
baiyfbupt 已提交
71
            ), "path should contains four str, ['local hadoop home', 'fs.default.name', 'hadoop.job.ugi', 'FS path']"
B
baiyfbupt 已提交
72 73 74 75 76 77 78

            hadoop_home = self.path[0]
            configs = {
                "fs.default.name": self.path[1],
                "hadoop.job.ugi": self.path[2]
            }
            self.client = HDFSClient(hadoop_home, configs)
B
baiyfbupt 已提交
79 80 81
            assert (
                self.client.is_exist(self.path[3]) == True
            ), "Plese make sure your hadoop confiuration is correct and FS path exists"
B
baiyfbupt 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

            self.hdfs_local_path = "./teacher_knowledge"
            if not os.path.exists(self.hdfs_local_path):
                os.mkdir(self.hdfs_local_path)
        elif isinstance(self.path, str):
            self.write_type = "LocalFS"
            if not os.path.exists(path):
                raise ValueError("The local path [%s] does not exist." %
                                 (path))
        else:
            self.write_type = "MEM"
            self.knowledge_queue = Queue(64)

        self.items = items
        self.reduce_strategy = reduce_strategy

    def _write(self, data):
        if self.write_type == 'HDFS/AFS':
            file_name = 'knowledge_' + str(self.file_cnt)
B
baiyfbupt 已提交
101 102
            file_path = os.path.join(self.hdfs_local_path, file_name)
            file_path += ".npy"
B
baiyfbupt 已提交
103 104
            np.save(file_path, data)
            self.file_cnt += 1
B
baiyfbupt 已提交
105
            self.client.upload(self.path[3], file_path)
B
baiyfbupt 已提交
106 107
            logger.info('{}.npy pushed to HDFS/AFS: {}'.format(file_name,
                                                               self.path[3]))
B
baiyfbupt 已提交
108 109 110 111 112

        elif self.write_type == 'LocalFS':
            file_name = 'knowledge_' + str(self.file_cnt)
            file_path = os.path.join(self.path, file_name)
            np.save(file_path, data)
B
baiyfbupt 已提交
113
            logger.info('{}.npy saved'.format(file_name))
B
baiyfbupt 已提交
114 115 116 117
            self.file_cnt += 1

        else:
            self.knowledge_queue.put(data)
B
baiyfbupt 已提交
118
            logger.info('{} pushed to Queue'.format(file_name))
B
baiyfbupt 已提交
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 153

    def run(self, teacher_program, exe, place, scope, reader, inputs, outputs,
            call_back):
        """Start teacher model to do information.
        Args:
            teacher_program(Program): teacher program.
            scope(Scope): The scope used to execute the teacher,
                          which contains the initialized variables.
            reader(reader): The data reader used by the teacher.
            inputs(list<str>): The name of variables to feed the teacher program.
            outputs(list<str>): Need to write to the variable instance's names of
                                the Knowledge instance, which needs to correspond
                                to the Knowledge's items.
            call_back(func, optional): The callback function that handles the
                          outputs of the teacher, which is none by default,
                          that is, the output of the teacher is concat directly.
        Return:
            (bool): Whether the teacher task was successfully registered and started
        """
        assert (isinstance(
            teacher_program,
            fluid.Program)), "teacher_program should be a fluid.Program"
        assert (isinstance(inputs, list)), "inputs should be a list"
        assert (isinstance(outputs, list)), "outputs should be a list"
        assert (len(self.items) == len(outputs)
                ), "the length of outputs list should be equal with items list"
        assert (callable(call_back) or (call_back is None)
                ), "call_back should be a callable function or NoneType."

        for var in teacher_program.list_vars():
            var.stop_gradient = True

        compiled_teacher_program = fluid.compiler.CompiledProgram(
            teacher_program)
        self.file_cnt = 0
B
baiyfbupt 已提交
154 155
        if isinstance(reader, Variable) or (
                isinstance(reader, DataLoaderBase) and (not reader.iterable)):
B
baiyfbupt 已提交
156 157 158
            reader.start()
            try:
                while True:
B
baiyfbupt 已提交
159 160 161 162
                    logits = exe.run(compiled_teacher_program,
                                     scope=scope,
                                     fetch_list=outputs,
                                     feed=None)
B
baiyfbupt 已提交
163 164 165
                    knowledge = dict()
                    for index, array in enumerate(logits):
                        knowledge[self.items[index]] = array
B
baiyfbupt 已提交
166
                    self._write(knowledge)
B
baiyfbupt 已提交
167 168 169 170
            except EOFException:
                reader.reset()

        else:
B
baiyfbupt 已提交
171 172 173
            if not isinstance(reader, DataLoaderBase):
                feeder = fluid.DataFeeder(
                    feed_list=inputs, place=place, program=teacher_program)
B
baiyfbupt 已提交
174
            for batch_id, data in enumerate(reader()):
B
baiyfbupt 已提交
175 176 177 178 179 180
                if not isinstance(reader, DataLoaderBase):
                    data = feeder.feed(data)
                logits = exe.run(compiled_teacher_program,
                                 scope=scope,
                                 fetch_list=outputs,
                                 feed=data)
B
baiyfbupt 已提交
181 182 183
                knowledge = dict()
                for index, array in enumerate(logits):
                    knowledge[self.items[index]] = array
B
baiyfbupt 已提交
184
                self._write(knowledge)
B
baiyfbupt 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        return True

    def dist(self, student_program, losses):
        """Building the distillation network
        Args:
            student_program(Program): student program.
            losses(list<Variable>, optional): The losses need to add. If set to None
                              does not add any loss.
        Return:
            (Program): Program for distillation.
            (startup_program): Program for initializing distillation network.
            (reader): Data reader for distillation training.
            (Variable): Loss of distillation training
        """

    def loss(self, loss_func, *variables):
        """User-defined loss
        Args:
            loss_func(func): Function used to define loss.
            *variables(list<str>): Variable name list.
        Return:
            (Variable): Distillation loss.
        """
        pass

    def fsp_loss(self):
        """fsp loss
        """
        pass

    def l2_loss(self):
        """l2 loss
        """
        pass

    def softlabel_loss(self):
        """softlabel_loss
        """
        pass