helper.py 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2022 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.

15
import inspect
16
import logging
17
from collections import defaultdict
18

19 20
from paddle.jit import not_to_static, to_static
from paddle.jit.dy2static.program_translator import StaticFunction
21
from paddle.jit.dy2static.utils import as_not_paddle_func
22
from paddle.nn import Layer
23
from paddle.static import Parameter, global_scope, program_guard
24

25
from .converter import Converter
26
from .utils import get_logger, to_list
27 28 29 30 31 32 33 34 35 36


class ProxyLayer(Layer):
    """
    ProxyLayer implements all logic for converting dygraph model into
    static Program IR. Meanwhile, it provides conviential interfaces for
    auto parallel to visit feed/fetch/loss/metric variables.
    """

    def __init__(self, layer, loss_func, metrics):
37
        super().__init__()
38 39 40 41 42 43 44 45
        # NOTE: All verify logics are finished in Engine.Prepare
        self.inner_layer = layer
        self.loss_func = loss_func
        self.metrics = metrics
        # train / eval / predict
        self.mode = None

        # generated program vars
46 47 48 49 50
        self._input_vars = defaultdict(list)
        self._label_vars = defaultdict(list)
        self._output_vars = defaultdict(list)
        self._loss_vars = defaultdict(list)
        self._metric_vars = defaultdict(list)
51

52 53 54 55 56 57
        # Consider ProxyLayer as not Paddle inner function because it contains
        # user-defined layer.
        as_not_paddle_func(
            inspect.getmodule(ProxyLayer).__name__ + ".ProxyLayer"
        )

58 59 60 61 62
    def _train(self, inputs, labels):
        """
        Train process of inner_layer with forward/loss/metric logic.
        """
        # step 1. save feed variables of Program
63 64 65
        mode = 'train'
        self._input_vars[mode] = inputs
        self._label_vars[mode] = labels
66 67

        # step 2. call inner_layer.forward
68
        self._output_vars[mode] = self.inner_layer(*inputs)
69 70 71

        # step 3. calculate loss if needed
        new_inputs = self._prepare(self.output_vars, labels)
72
        self._loss_vars[mode] = self.call_loss(new_inputs)
73 74

        # step 4. calculate metrics if needed
75
        self._metric_vars[mode] = self.call_metrics(new_inputs)
76 77 78 79 80 81 82 83 84

    def _eval(self, inputs, labels):
        """
        Evaluate process of inner_layer with forward/loss/metric logic.
        """
        # TODO(dev): we can reuse codes with self._train after making
        # sure if they can.

        # step 1. save feed variables of Program
85 86 87
        mode = 'eval'
        self._input_vars[mode] = inputs
        self._label_vars[mode] = labels
88 89

        # step 2. call inner_layer.forward
90
        self._output_vars[mode] = self.inner_layer(*inputs)
91 92 93

        # step 3. calculate loss if needed
        new_inputs = self._prepare(self.output_vars, labels)
94
        self._loss_vars[mode] = self.call_loss(new_inputs)
95 96

        # step 4. calculate metrics if needed
97
        self._metric_vars[mode] = self.call_metrics(new_inputs)
98

99
    def _predict(self, inputs, labels):
100 101 102 103
        """
        Predict process of inner_layer with forward logic.
        """
        # step 1. save feed variables of Program
104 105
        mode = 'predict'
        self._input_vars[mode] = inputs
106
        self._label_vars[mode] = labels
107 108

        # step 2. call inner_layer.forward
109
        self._output_vars[mode] = self.inner_layer(*inputs)
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

    @not_to_static
    def _prepare(self, outputs, labels):
        """
        Concat outputs and labels as a single list

        NOTE(dev): We use @not_to_static to avoid AST Analysis.
        """
        return to_list(outputs) + to_list(labels)

    def call_loss(self, inputs):
        """
        Apply Loss Function on outputs and labels.

        Args:
            inputs: List[Variable]

        Returns: List[Variable]
        """
        res = []
        if self.loss_func is not None:
            res = self.loss_func(*inputs)
        return res

    def call_metrics(self, inputs):
        """
        Apply Metrics Function on outputs and labels.

        Args:
            inputs: List[Variable]

        Returns: List[Variable]
        """
        outs = []
        for metric in self.metrics:
145
            outs.append(to_list(metric.compute(*inputs)))
146 147 148 149 150 151 152

        return outs

    def set_mode(self, mode):
        self.mode = mode
        self.training = mode == 'train'

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    def clone(self):
        return ProxyLayer(self.inner_layer, self.loss_func, self.metrics)

    @property
    def input_vars(self):
        return self._input_vars[self.mode]

    @property
    def label_vars(self):
        return self._label_vars[self.mode]

    @property
    def output_vars(self):
        return self._output_vars[self.mode]

    @property
    def loss_vars(self):
        return self._loss_vars[self.mode]

    @property
    def metric_vars(self):
        return self._metric_vars[self.mode]

176 177 178 179
    @property
    def startup_program(self):
        return self.inner_layer._startup_program()

180 181

class BuildInfo:
182 183 184 185 186 187 188 189
    def __init__(self):
        self.clear()

    def has_cache(self, mode, update=False):
        is_cache = self.states[mode]
        if update:
            self.cache(mode)
        return is_cache
190

191 192 193 194 195
    def cache(self, mode):
        self.states[mode] = True

    def clear(self):
        self.states = defaultdict(bool)
196 197


198
class ProgramHelper:
199 200 201 202 203 204 205 206 207 208 209 210 211 212
    """
    A Helper class for Engine to provides different Program IR according specified 'mode'.
    """

    def __init__(self, layer, loss_func, metrics, inputs_spec, labels_spec):
        # original model config information
        # TODO(Aurelius84): Implenet append_backward and optimizer in ProxyLayer
        # after distribute engine satisify basic condition.
        self.proxy_layer = ProxyLayer(layer, loss_func, metrics)
        self.inputs_spec = inputs_spec
        self.labels_spec = labels_spec

        self.build_info = BuildInfo()
        self._logger = get_logger(logging.INFO)
213
        self.lazy_init = False
214

215 216 217 218 219 220 221
    def reset(self):
        """
        Reset all state of current Object.
        """
        self.build_info.clear()
        self.proxy_layer = self.proxy_layer.clone()

222 223 224 225 226
    def build_program(self, mode):
        """
        Convert dygraph model into static Program IR.
        """
        assert mode in ['train', 'eval', 'predict']
227
        self.proxy_layer.set_mode(mode)
228
        # skip if we has already built program.
229
        if self.build_info.has_cache(mode, True):
230
            self._logger.info(
231 232 233
                "Already build program with mode = %s, use cached program."
                % mode
            )
234 235 236
            return

        self._logger.info("start to build program for mode = %s." % mode)
237
        input_spec = [self.inputs_spec, self.labels_spec]
238 239 240 241 242 243 244 245 246
        static_func = to_static(self.static_func(), input_spec=input_spec)

        func_name = '_' + mode
        setattr(self.proxy_layer, func_name, static_func)

        # NOTE(dev): Because @to_static is a Lazy mechanism, so we explicitly call this to trigger
        # generating Program IR immediately.
        getattr(self.proxy_layer, func_name).concrete_program

247 248
        self._build_startup_program()

249 250 251 252
    def _build_startup_program(self):
        """
        Create and Sync parameters into startup program.
        """
253 254 255
        if len(self.startup_program.global_block().ops) > 1:
            self.lazy_init = True
            return
256
        for param in self.concrete_program.parameters:
257 258 259 260 261 262 263 264 265
            Parameter(
                name=param.name,
                desc=param,
                type=param.type,
                shape=param.shape,
                dtype=param.dtype,
                stop_gradient=param.stop_gradient,
                block=self.startup_program.global_block(),
            )
266

267 268 269 270 271
    def apply_optimizer(self, optimizer):
        """
        Append backward and generate optimizer operations.
        """
        self._verify_optimizer(optimizer)
272 273 274
        self._logger.info(
            "start to apply optimizer: %s ", type(optimizer).__name__
        )
275 276 277 278 279 280 281 282 283 284 285 286
        # clear optimizer parameters
        original_params = optimizer._parameter_list
        optimizer._parameter_list = None
        with program_guard(self.main_program, self.startup_program):
            res = optimizer.minimize(self.loss_vars[0])

        # restore optimizer parameters
        optimizer._parameter_list = original_params
        return res

    def _verify_optimizer(self, optimizer):
        assert optimizer is not None
287 288 289 290 291 292 293 294 295 296 297
        assert hasattr(
            optimizer, "minimize"
        ), "Optimizer must have minimize() method."
        assert self.proxy_layer.mode == 'train', (
            "Required mode == 'train', but received '%s'"
            % self.proxy_layer.mode
        )
        assert len(self.loss_vars) == 1, (
            "Required len(loss_vars) == 1, but received len(loss_vars) = %s"
            % len(self.loss_vars)
        )
298 299 300 301 302 303 304 305

    def to(self, mode):
        """
        Switch underly proxy layer mode into target mode.
        """
        assert mode in ['train', 'eval', 'predict']
        func = getattr(self.proxy_layer, '_' + mode)
        assert isinstance(
306 307
            func, StaticFunction
        ), "Please call build_program(mode) firstly."
308 309
        self.proxy_layer.set_mode(mode)

310 311
    def static_func(self):
        """
312
        Return StaticFunction instance with underly target mode.
313 314
        """
        assert self.proxy_layer.mode in [
315 316 317
            'train',
            'eval',
            'predict',
318 319 320 321
        ], "Please call build_program(mode) firstly."
        func_name = '_' + self.proxy_layer.mode
        return getattr(self.proxy_layer, func_name)

322 323 324 325 326 327 328 329 330 331 332 333
    def init(self, main_program, place, dist_context):
        if self.lazy_init:
            return
        for param in self.concrete_program.parameters:
            # create var in scope and share parameters to scope
            if param.name not in main_program.global_block().vars:
                continue
            # get param_var's dist_attr
            var = main_program.global_block().vars[param.name]
            var_dist_attr = dist_context.get_tensor_dist_attr_for_program(var)
            dist_attr = {
                "dims_mapping": var_dist_attr.dims_mapping,
334 335
                "process_shape": var_dist_attr.process_mesh.shape,
                "process_group": var_dist_attr.process_mesh.process_ids,
336 337 338 339 340
            }
            # slice param_value with dist_attr
            # share sliced_param_value with param_tensor in global_scope
            param_tensor = global_scope().var(param.name).get_tensor()
            sliced_param = Converter.slice_with_dist_attr(
341 342
                param.numpy(), dist_attr
            )
343 344
            param_tensor.set(sliced_param, place)

345 346 347 348 349 350 351 352 353 354
    @property
    def concrete_program(self):
        return self.static_func().concrete_program

    @property
    def main_program(self):
        return self.concrete_program.main_program

    @property
    def startup_program(self):
355 356 357
        try:
            return self.proxy_layer.startup_program
        except Exception as err:
358
            self._logger.warning("`lazy init` failed.")
359 360 361
            if isinstance(err, AssertionError):
                return self.concrete_program.startup_program
            raise err
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

    @property
    def input_vars(self):
        return to_list(self.proxy_layer.input_vars)

    @property
    def output_vars(self):
        return to_list(self.proxy_layer.output_vars)

    @property
    def label_vars(self):
        return to_list(self.proxy_layer.label_vars)

    @property
    def loss_vars(self):
        return to_list(self.proxy_layer.loss_vars)

    @property
    def metric_vars(self):
        return to_list(self.proxy_layer.metric_vars)