prune_strategy.py 41.5 KB
Newer Older
W
whs 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2018 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.

from ..core.strategy import Strategy
16 17
from ..graph import VarWrapper, OpWrapper, GraphWrapper
from ....framework import Program, program_guard, Parameter
18
from ....log_helper import get_logger
W
whs 已提交
19
from .... import layers
20
import prettytable as pt
W
whs 已提交
21
import numpy as np
22 23 24 25 26 27 28
from scipy.optimize import leastsq
import copy
import re
import os
import pickle
import logging
import sys
W
whs 已提交
29

W
whs 已提交
30
__all__ = ['SensitivePruneStrategy', 'UniformPruneStrategy', 'PruneStrategy']
W
whs 已提交
31

32 33
_logger = get_logger(
    __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s')
34 35 36 37 38 39


class PruneStrategy(Strategy):
    """
    The base class of all pruning strategies.
    """
W
whs 已提交
40 41 42 43

    def __init__(self,
                 pruner=None,
                 start_epoch=0,
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
                 end_epoch=0,
                 target_ratio=0.5,
                 metric_name=None,
                 pruned_params='conv.*_weights'):
        """
        Args:
            pruner(slim.Pruner): The pruner used to prune the parameters.
            start_epoch(int): The 'on_epoch_begin' function will be called in start_epoch. default: 0
            end_epoch(int): The 'on_epoch_end' function will be called in end_epoch. default: 0
            target_ratio(float): The flops ratio to be pruned from current model.
            metric_name(str): The metric used to evaluate the model.
                         It should be one of keys in out_nodes of graph wrapper.
            pruned_params(str): The pattern str to match the parameter names to be pruned.
        """
        super(PruneStrategy, self).__init__(start_epoch, end_epoch)
W
whs 已提交
59
        self.pruner = pruner
60 61 62 63
        self.target_ratio = target_ratio
        self.metric_name = metric_name
        self.pruned_params = pruned_params
        self.pruned_list = []
W
whs 已提交
64

65 66 67 68 69 70 71 72 73 74 75
    def _eval_graph(self, context, sampled_rate=None, cached_id=0):
        """
        Evaluate the current mode in context.
        Args:
            context(slim.core.Context): The context storing all information used to evaluate the current model.
            sampled_rate(float): The sampled rate used to sample partial data for evaluation. None means using all data in eval_reader. default: None.
            cached_id(int): The id of dataset sampled. Evaluations with same cached_id use the same sampled dataset. default: 0.
        """
        results, names = context.run_eval_graph(sampled_rate, cached_id)
        metric = np.mean(results[list(names).index(self.metric_name)])
        return metric
W
whs 已提交
76

77 78 79 80 81 82
    def _prune_filters_by_ratio(self,
                                scope,
                                params,
                                ratio,
                                place,
                                lazy=False,
W
whs 已提交
83 84 85
                                only_graph=False,
                                param_shape_backup=None,
                                param_backup=None):
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        """
        Pruning filters by given ratio.
        Args:
            scope(fluid.core.Scope): The scope used to pruning filters.
            params(list<VarWrapper>): A list of filter parameters.
            ratio(float): The ratio to be pruned.
            place(fluid.Place): The device place of filter parameters.
            lazy(bool): True means setting the pruned elements to zero.
                        False means cutting down the pruned elements.
            only_graph(bool): True means only modifying the graph.
                              False means modifying graph and variables in  scope.
        """
        if params[0].name() in self.pruned_list[0]:
            return
        param_t = scope.find_var(params[0].name()).get_tensor()
        pruned_idx = self.pruner.cal_pruned_idx(
            params[0].name(), np.array(param_t), ratio, axis=0)
        for param in params:
            assert isinstance(param, VarWrapper)
            param_t = scope.find_var(param.name()).get_tensor()
W
whs 已提交
106 107
            if param_backup is not None and (param.name() not in param_backup):
                param_backup[param.name()] = copy.deepcopy(np.array(param_t))
108 109 110 111 112
            pruned_param = self.pruner.prune_tensor(
                np.array(param_t), pruned_idx, pruned_axis=0, lazy=lazy)
            if not only_graph:
                param_t.set(pruned_param, place)
            ori_shape = param.shape()
W
whs 已提交
113 114 115
            if param_shape_backup is not None and (
                    param.name() not in param_shape_backup):
                param_shape_backup[param.name()] = copy.deepcopy(param.shape())
116 117 118 119 120 121 122
            new_shape = list(param.shape())
            new_shape[0] = pruned_param.shape[0]
            param.set_shape(new_shape)
            _logger.debug(
                '|----------------------------------------+----+------------------------------+------------------------------|'
            )
            _logger.debug('|{:^40}|{:^4}|{:^30}|{:^30}|'.format(
W
whs 已提交
123 124
                str(param.name()),
                str(ratio), str(ori_shape), str(param.shape())))
125 126 127 128 129 130 131 132 133 134
            self.pruned_list[0].append(param.name())
        return pruned_idx

    def _prune_parameter_by_idx(self,
                                scope,
                                params,
                                pruned_idx,
                                pruned_axis,
                                place,
                                lazy=False,
W
whs 已提交
135 136 137
                                only_graph=False,
                                param_shape_backup=None,
                                param_backup=None):
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        """
        Pruning parameters in given axis.
        Args:
            scope(fluid.core.Scope): The scope storing paramaters to be pruned.
            params(VarWrapper): The parameter to be pruned.
            pruned_idx(list): The index of elements to be pruned.
            pruned_axis(int): The pruning axis.
            place(fluid.Place): The device place of filter parameters.
            lazy(bool): True means setting the pruned elements to zero.
                        False means cutting down the pruned elements.
            only_graph(bool): True means only modifying the graph.
                              False means modifying graph and variables in  scope.
        """
        if params[0].name() in self.pruned_list[pruned_axis]:
            return
        for param in params:
            assert isinstance(param, VarWrapper)
            param_t = scope.find_var(param.name()).get_tensor()
W
whs 已提交
156 157
            if param_backup is not None and (param.name() not in param_backup):
                param_backup[param.name()] = copy.deepcopy(np.array(param_t))
158 159 160 161 162
            pruned_param = self.pruner.prune_tensor(
                np.array(param_t), pruned_idx, pruned_axis, lazy=lazy)
            if not only_graph:
                param_t.set(pruned_param, place)
            ori_shape = param.shape()
W
whs 已提交
163 164 165 166

            if param_shape_backup is not None and (
                    param.name() not in param_shape_backup):
                param_shape_backup[param.name()] = copy.deepcopy(param.shape())
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
            new_shape = list(param.shape())
            new_shape[pruned_axis] = pruned_param.shape[pruned_axis]
            param.set_shape(new_shape)
            _logger.debug(
                '|----------------------------------------+----+------------------------------+------------------------------|'
            )
            _logger.debug('|{:^40}|{:^4}|{:^30}|{:^30}|'.format(
                str(param.name()),
                str(pruned_axis), str(ori_shape), str(param.shape())))
            self.pruned_list[pruned_axis].append(param.name())

    def _forward_search_related_op(self, graph, param):
        """
        Forward search operators that will be affected by pruning of param.
        Args:
            graph(GraphWrapper): The graph to be searched.
            param(VarWrapper): The current pruned parameter.
        Returns:
            list<OpWrapper>: A list of operators.
        """
        assert isinstance(param, VarWrapper)
        visited = {}
        for op in graph.ops():
            visited[op.idx()] = False
        stack = []
        for op in graph.ops():
            if (not op.is_bwd_op()) and (param in op.all_inputs()):
                stack.append(op)
        visit_path = []
        while len(stack) > 0:
            top_op = stack[len(stack) - 1]
            if visited[top_op.idx()] == False:
                visit_path.append(top_op)
                visited[top_op.idx()] = True
            next_ops = None
            if top_op.type() == "conv2d" and param not in top_op.all_inputs():
                next_ops = None
            elif top_op.type() == "mul":
                next_ops = None
            else:
                next_ops = self._get_next_unvisited_op(graph, visited, top_op)
            if next_ops == None:
                stack.pop()
            else:
                stack += next_ops
        return visit_path

    def _get_next_unvisited_op(self, graph, visited, top_op):
        """
        Get next unvisited adjacent operators of given operators.
        Args:
            graph(GraphWrapper): The graph used to search. 
            visited(list): The ids of operators that has been visited.
            top_op: The given operator.
        Returns:
            list<OpWrapper>: A list of operators. 
        """
        assert isinstance(top_op, OpWrapper)
        next_ops = []
        for op in graph.next_ops(top_op):
            if (visited[op.idx()] == False) and (not op.is_bwd_op()):
                next_ops.append(op)
        return next_ops if len(next_ops) > 0 else None

    def _get_accumulator(self, graph, param):
        """
        Get accumulators of given parameter. The accumulator was created by optimizer.
        Args:
            graph(GraphWrapper): The graph used to search.
            param(VarWrapper): The given parameter.
        Returns:
            list<VarWrapper>: A list of accumulators which are variables.
        """
        assert isinstance(param, VarWrapper)
        params = []
        for op in param.outputs():
            if op.is_opt_op():
                for out_var in op.all_outputs():
                    if graph.is_persistable(out_var) and out_var.name(
                    ) != param.name():
                        params.append(out_var)
        return params

    def _forward_pruning_ralated_params(self,
                                        graph,
                                        scope,
                                        param,
                                        place,
                                        ratio=None,
                                        pruned_idxs=None,
                                        lazy=False,
W
whs 已提交
258 259 260
                                        only_graph=False,
                                        param_backup=None,
                                        param_shape_backup=None):
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        """
        Pruning all the parameters affected by the pruning of given parameter.
        Args:
            graph(GraphWrapper): The graph to be searched.
            scope(fluid.core.Scope): The scope storing paramaters to be pruned.
            param(VarWrapper): The given parameter.
            place(fluid.Place): The device place of filter parameters.
            ratio(float): The target ratio to be pruned.
            pruned_idx(list): The index of elements to be pruned.
            lazy(bool): True means setting the pruned elements to zero.
                        False means cutting down the pruned elements.
            only_graph(bool): True means only modifying the graph.
                              False means modifying graph and variables in  scope.
        """
        assert isinstance(
            graph,
            GraphWrapper), "graph must be instance of slim.core.GraphWrapper"
        assert isinstance(
            param, VarWrapper), "param must be instance of slim.core.VarWrapper"

        if param.name() in self.pruned_list[0]:
            return
        related_ops = self._forward_search_related_op(graph, param)

        if ratio is None:
            assert pruned_idxs is not None
            self._prune_parameter_by_idx(
                scope, [param] + self._get_accumulator(graph, param),
                pruned_idxs,
                pruned_axis=0,
                place=place,
                lazy=lazy,
W
whs 已提交
293 294 295
                only_graph=only_graph,
                param_backup=param_backup,
                param_shape_backup=param_shape_backup)
296 297 298 299 300 301 302

        else:
            pruned_idxs = self._prune_filters_by_ratio(
                scope, [param] + self._get_accumulator(graph, param),
                ratio,
                place,
                lazy=lazy,
W
whs 已提交
303 304 305
                only_graph=only_graph,
                param_backup=param_backup,
                param_shape_backup=param_shape_backup)
306 307 308 309 310 311 312 313 314 315 316 317 318 319
        corrected_idxs = pruned_idxs[:]

        for idx, op in enumerate(related_ops):
            if op.type() == "conv2d" and (param not in op.all_inputs()):
                for in_var in op.all_inputs():
                    if graph.is_parameter(in_var):
                        conv_param = in_var
                        self._prune_parameter_by_idx(
                            scope, [conv_param] + self._get_accumulator(
                                graph, conv_param),
                            corrected_idxs,
                            pruned_axis=1,
                            place=place,
                            lazy=lazy,
W
whs 已提交
320 321 322
                            only_graph=only_graph,
                            param_backup=param_backup,
                            param_shape_backup=param_shape_backup)
323 324 325 326 327 328 329 330 331 332 333
            if op.type() == "depthwise_conv2d":
                for in_var in op.all_inputs():
                    if graph.is_parameter(in_var):
                        conv_param = in_var
                        self._prune_parameter_by_idx(
                            scope, [conv_param] + self._get_accumulator(
                                graph, conv_param),
                            corrected_idxs,
                            pruned_axis=0,
                            place=place,
                            lazy=lazy,
W
whs 已提交
334 335 336
                            only_graph=only_graph,
                            param_backup=param_backup,
                            param_shape_backup=param_shape_backup)
337 338 339 340 341 342 343 344 345 346 347 348
            elif op.type() == "elementwise_add":
                # pruning bias
                for in_var in op.all_inputs():
                    if graph.is_parameter(in_var):
                        bias_param = in_var
                        self._prune_parameter_by_idx(
                            scope, [bias_param] + self._get_accumulator(
                                graph, bias_param),
                            pruned_idxs,
                            pruned_axis=0,
                            place=place,
                            lazy=lazy,
W
whs 已提交
349 350 351
                            only_graph=only_graph,
                            param_backup=param_backup,
                            param_shape_backup=param_shape_backup)
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
            elif op.type() == "mul":  # pruning fc layer
                fc_input = None
                fc_param = None
                for in_var in op.all_inputs():
                    if graph.is_parameter(in_var):
                        fc_param = in_var
                    else:
                        fc_input = in_var

                idx = []
                feature_map_size = fc_input.shape()[2] * fc_input.shape()[3]
                range_idx = np.array(range(feature_map_size))
                for i in corrected_idxs:
                    idx += list(range_idx + i * feature_map_size)
                corrected_idxs = idx
                self._prune_parameter_by_idx(
                    scope, [fc_param] + self._get_accumulator(graph, fc_param),
                    corrected_idxs,
                    pruned_axis=0,
                    place=place,
                    lazy=lazy,
W
whs 已提交
373 374 375
                    only_graph=only_graph,
                    param_backup=param_backup,
                    param_shape_backup=param_shape_backup)
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

            elif op.type() == "concat":
                concat_inputs = op.all_inputs()
                last_op = related_ops[idx - 1]
                for out_var in last_op.all_outputs():
                    if out_var in concat_inputs:
                        concat_idx = concat_inputs.index(out_var)
                offset = 0
                for ci in range(concat_idx):
                    offset += concat_inputs[ci].shape()[1]
                corrected_idxs = [x + offset for x in pruned_idxs]
            elif op.type() == "batch_norm":
                bn_inputs = op.all_inputs()
                mean = bn_inputs[2]
                variance = bn_inputs[3]
                alpha = bn_inputs[0]
                beta = bn_inputs[1]
                self._prune_parameter_by_idx(
                    scope, [mean] + self._get_accumulator(graph, mean),
                    corrected_idxs,
                    pruned_axis=0,
                    place=place,
                    lazy=lazy,
W
whs 已提交
399 400 401
                    only_graph=only_graph,
                    param_backup=param_backup,
                    param_shape_backup=param_shape_backup)
402 403 404 405 406 407
                self._prune_parameter_by_idx(
                    scope, [variance] + self._get_accumulator(graph, variance),
                    corrected_idxs,
                    pruned_axis=0,
                    place=place,
                    lazy=lazy,
W
whs 已提交
408 409 410
                    only_graph=only_graph,
                    param_backup=param_backup,
                    param_shape_backup=param_shape_backup)
411 412 413 414 415 416
                self._prune_parameter_by_idx(
                    scope, [alpha] + self._get_accumulator(graph, alpha),
                    corrected_idxs,
                    pruned_axis=0,
                    place=place,
                    lazy=lazy,
W
whs 已提交
417 418 419
                    only_graph=only_graph,
                    param_backup=param_backup,
                    param_shape_backup=param_shape_backup)
420 421 422 423 424 425
                self._prune_parameter_by_idx(
                    scope, [beta] + self._get_accumulator(graph, beta),
                    corrected_idxs,
                    pruned_axis=0,
                    place=place,
                    lazy=lazy,
W
whs 已提交
426 427 428
                    only_graph=only_graph,
                    param_backup=param_backup,
                    param_shape_backup=param_shape_backup)
429 430 431 432 433 434 435 436

    def _prune_parameters(self,
                          graph,
                          scope,
                          params,
                          ratios,
                          place,
                          lazy=False,
W
whs 已提交
437 438 439
                          only_graph=False,
                          param_backup=None,
                          param_shape_backup=None):
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
        """
        Pruning the given parameters.
        Args:
            graph(GraphWrapper): The graph to be searched.
            scope(fluid.core.Scope): The scope storing paramaters to be pruned.
            params(list<str>): A list of parameter names to be pruned.
            ratios(list<float>): A list of ratios to be used to pruning parameters.
            place(fluid.Place): The device place of filter parameters.
            pruned_idx(list): The index of elements to be pruned.
            lazy(bool): True means setting the pruned elements to zero.
                        False means cutting down the pruned elements.
            only_graph(bool): True means only modifying the graph.
                              False means modifying graph and variables in  scope.

        """
        _logger.debug('\n################################')
        _logger.debug('#       pruning parameters       #')
        _logger.debug('################################\n')
        _logger.debug(
            '|----------------------------------------+----+------------------------------+------------------------------|'
        )
        _logger.debug('|{:^40}|{:^4}|{:^30}|{:^30}|'.format('parameter', 'axis',
                                                            'from', 'to'))
        assert len(params) == len(ratios)
        self.pruned_list = [[], []]
        for param, ratio in zip(params, ratios):
            assert isinstance(param, str) or isinstance(param, unicode)
            param = graph.var(param)
            self._forward_pruning_ralated_params(
                graph,
                scope,
                param,
                place,
                ratio=ratio,
                lazy=lazy,
W
whs 已提交
475 476 477
                only_graph=only_graph,
                param_backup=param_backup,
                param_shape_backup=param_shape_backup)
478 479 480 481 482 483 484 485 486 487 488 489 490
            ops = param.outputs()
            for op in ops:
                if op.type() == 'conv2d':
                    brother_ops = self._search_brother_ops(graph, op)
                    for broher in brother_ops:
                        for p in graph.get_param_by_op(broher):
                            self._forward_pruning_ralated_params(
                                graph,
                                scope,
                                p,
                                place,
                                ratio=ratio,
                                lazy=lazy,
W
whs 已提交
491 492 493
                                only_graph=only_graph,
                                param_backup=param_backup,
                                param_shape_backup=param_shape_backup)
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
        _logger.debug(
            '|----------------------------------------+----+------------------------------+------------------------------|'
        )

    def _search_brother_ops(self, graph, op_node):
        """
        Search brother operators that was affected by pruning of given operator.
        Args:
            graph(GraphWrapper): The graph to be searched.
            op_node(OpWrapper): The start node for searching.
        Returns: 
            list<VarWrapper>: A list of operators.
        """
        visited = [op_node.idx()]
        stack = []
        brothers = []
        for op in graph.next_ops(op_node):
            if (op.type() != 'conv2d') and (op.type() != 'fc') and (
                    not op._is_bwd_op()):
                stack.append(op)
                visited.append(op.idx())
        while len(stack) > 0:
            top_op = stack.pop()
            for parent in graph.pre_ops(top_op):
                if parent.idx() not in visited and (not parent._is_bwd_op()):
                    if ((parent.type == 'conv2d') or (parent.type == 'fc')):
                        brothers.append(parent)
                    else:
                        stack.append(parent)
                    visited.append(parent.idx())

            for child in graph.next_ops(top_op):
                if (child.type != 'conv2d') and (child.type != 'fc') and (
                        child.idx() not in visited) and (
                            not child._is_bwd_op()):
                    stack.append(child)
                    visited.append(child.idx())
        return brothers

    def _prune_graph(self, graph, target_graph):
        """
        Pruning parameters of graph according to target graph.
        Args:
            graph(GraphWrapper): The graph to be pruned.
            target_graph(GraphWrapper): The reference graph.
        Return: None
        """
        count = 1
        _logger.debug(
            '|----+----------------------------------------+------------------------------+------------------------------|'
        )
        _logger.debug('|{:^4}|{:^40}|{:^30}|{:^30}|'.format('id', 'parammeter',
                                                            'from', 'to'))
        for param in target_graph.all_parameters():
            var = graph.var(param.name())
            ori_shape = var.shape()
            var.set_shape(param.shape())
            _logger.debug(
                '|----+----------------------------------------+------------------------------+------------------------------|'
            )
            _logger.debug('|{:^4}|{:^40}|{:^30}|{:^30}|'.format(
                str(count),
                str(param.name()), str(ori_shape), str(param.shape())))
            count += 1
        _logger.debug(
            '|----+----------------------------------------+------------------------------+------------------------------|'
        )


class UniformPruneStrategy(PruneStrategy):
W
whs 已提交
564
    """
565
    The uniform pruning strategy. The parameters will be pruned by uniform ratio.
W
whs 已提交
566 567 568
    """

    def __init__(self,
569
                 pruner=None,
W
whs 已提交
570
                 start_epoch=0,
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
                 end_epoch=0,
                 target_ratio=0.5,
                 metric_name=None,
                 pruned_params='conv.*_weights'):
        """
        Args:
            pruner(slim.Pruner): The pruner used to prune the parameters.
            start_epoch(int): The 'on_epoch_begin' function will be called in start_epoch. default: 0
            end_epoch(int): The 'on_epoch_end' function will be called in end_epoch. default: 0
            target_ratio(float): The flops ratio to be pruned from current model.
            metric_name(str): The metric used to evaluate the model.
                         It should be one of keys in out_nodes of graph wrapper.
            pruned_params(str): The pattern str to match the parameter names to be pruned.
        """
        super(UniformPruneStrategy, self).__init__(pruner, start_epoch,
                                                   end_epoch, target_ratio,
                                                   metric_name, pruned_params)

    def _get_best_ratios(self, context):
        """
        Search a group of ratios for pruning target flops.
        """
        _logger.info('_get_best_ratios')
        pruned_params = []
        for param in context.eval_graph.all_parameters():
            if re.match(self.pruned_params, param.name()):
                pruned_params.append(param.name())

        min_ratio = 0.
        max_ratio = 1.

        flops = context.eval_graph.flops()
        model_size = context.eval_graph.numel_params()

        while min_ratio < max_ratio:
            ratio = (max_ratio + min_ratio) / 2
            _logger.debug(
                '-----------Try pruning ratio: {:.2f}-----------'.format(ratio))
            ratios = [ratio] * len(pruned_params)
W
whs 已提交
610
            param_shape_backup = {}
611 612 613 614 615 616
            self._prune_parameters(
                context.eval_graph,
                context.scope,
                pruned_params,
                ratios,
                context.place,
W
whs 已提交
617 618
                only_graph=True,
                param_shape_backup=param_shape_backup)
619 620 621 622 623 624

            pruned_flops = 1 - (float(context.eval_graph.flops()) / flops)
            pruned_size = 1 - (float(context.eval_graph.numel_params()) /
                               model_size)
            _logger.debug('Pruned flops: {:.2f}'.format(pruned_flops))
            _logger.debug('Pruned model size: {:.2f}'.format(pruned_size))
W
whs 已提交
625 626
            for param in param_shape_backup.keys():
                context.eval_graph.var(param).set_shape(param_shape_backup[
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
                    param])

            if abs(pruned_flops - self.target_ratio) < 1e-2:
                break
            if pruned_flops > self.target_ratio:
                max_ratio = ratio
            else:
                min_ratio = ratio
        _logger.info('Get ratios: {}'.format([round(r, 2) for r in ratios]))
        return pruned_params, ratios

    def on_epoch_begin(self, context):
        if context.epoch_id == self.start_epoch:
            params, ratios = self._get_best_ratios(context)

            self._prune_parameters(context.optimize_graph, context.scope,
                                   params, ratios, context.place)

            model_size = context.eval_graph.numel_params()
            flops = context.eval_graph.flops()
            _logger.debug('\n################################')
            _logger.debug('#          pruning eval graph    #')
            _logger.debug('################################\n')
            self._prune_graph(context.eval_graph, context.optimize_graph)
            context.optimize_graph.update_groups_of_conv()
            context.eval_graph.update_groups_of_conv()

            _logger.info(
                '------------------finish pruning--------------------------------'
            )
            _logger.info('Pruned size: {:.2f}'.format(1 - (float(
                context.eval_graph.numel_params()) / model_size)))
            _logger.info('Pruned flops: {:.2f}'.format(1 - (float(
                context.eval_graph.flops()) / flops)))
            #            metric = self._eval_graph(context)
            #            _logger.info('Metric after pruning: {:.2f}'.format(metric))
            _logger.info(
                '------------------UniformPruneStrategy.on_compression_begin finish--------------------------------'
            )


class SensitivePruneStrategy(PruneStrategy):
    """
    Sensitive pruning strategy. Different pruned ratio was applied on each layer.
    """

    def __init__(self,
                 pruner=None,
                 start_epoch=0,
                 end_epoch=0,
                 delta_rate=0.20,
                 target_ratio=0.5,
                 metric_name='top1_acc',
                 pruned_params='conv.*_weights',
                 sensitivities_file='./sensitivities.data',
                 sensitivities={},
                 num_steps=1,
                 eval_rate=None):
        """
        Args:
            pruner(slim.Pruner): The pruner used to prune the parameters.
            start_epoch(int): The 'on_epoch_begin' function will be called in start_epoch. default: 0.
            end_epoch(int): The 'on_epoch_end' function will be called in end_epoch. default: 10.
            delta_rate(float): The delta used to generate ratios when calculating sensitivities. default: 0.2
            target_ratio(float): The flops ratio to be pruned from current model. default: 0.5
            metric_name(str): The metric used to evaluate the model.
                         It should be one of keys in out_nodes of graph wrapper. default: 'top1_acc'
            pruned_params(str): The pattern str to match the parameter names to be pruned. default: 'conv.*_weights'.
            sensitivities_file(str): The sensitivities file. default: './sensitivities.data'
            sensitivities(dict): The user-defined sensitivities. default: {}.
            num_steps(int): The number of pruning steps. default: 1.
            eval_rate(float): The rate of sampled data used to calculate sensitivities.
                              None means using all the data. default: None.
        """
        super(SensitivePruneStrategy, self).__init__(pruner, start_epoch,
                                                     end_epoch, target_ratio,
                                                     metric_name, pruned_params)
        self.delta_rate = delta_rate
        self.pruned_list = []
        self.sensitivities = sensitivities
        self.sensitivities_file = sensitivities_file
        self.num_steps = num_steps
        self.eval_rate = eval_rate
        self.pruning_step = 1 - pow((1 - target_ratio), 1.0 / self.num_steps)

    def _save_sensitivities(self, sensitivities, sensitivities_file):
        """
        Save sensitivities into file.
        """
        with open(sensitivities_file, 'wb') as f:
            pickle.dump(sensitivities, f)

    def _load_sensitivities(self, sensitivities_file):
        """
        Load sensitivities from file.
        """
        sensitivities = {}
        if sensitivities_file and os.path.exists(sensitivities_file):
            with open(sensitivities_file, 'rb') as f:
                if sys.version_info < (3, 0):
                    sensitivities = pickle.load(f)
                else:
                    sensitivities = pickle.load(f, encoding='bytes')

        for param in sensitivities:
            sensitivities[param]['pruned_percent'] = [
                round(p, 2) for p in sensitivities[param]['pruned_percent']
            ]
        self._format_sensitivities(sensitivities)
        return sensitivities

    def _format_sensitivities(self, sensitivities):
        """
        Print formated sensitivities in debug log level.
        """
        tb = pt.PrettyTable()
        tb.field_names = ["parameter", "size"] + [
            str(round(i, 2))
            for i in np.arange(self.delta_rate, 1, self.delta_rate)
        ]
        for param in sensitivities:
            if len(sensitivities[param]['loss']) == (len(tb.field_names) - 2):
                tb.add_row([param, sensitivities[param]['size']] + [
                    round(loss, 2) for loss in sensitivities[param]['loss']
                ])
        _logger.debug('\n################################')
        _logger.debug('#      sensitivities table     #')
        _logger.debug('################################\n')
        _logger.debug(tb)

    def _compute_sensitivities(self, context):
        """
        Computing the sensitivities of all parameters.
        """
        _logger.info("calling _compute_sensitivities.")
        cached_id = np.random.randint(1000)
        if self.start_epoch == context.epoch_id:
            sensitivities_file = self.sensitivities_file
        else:
            sensitivities_file = self.sensitivities_file + ".epoch" + str(
                context.epoch_id)
        sensitivities = self._load_sensitivities(sensitivities_file)

        for param in context.eval_graph.all_parameters():
            if not re.match(self.pruned_params, param.name()):
                continue
            if param.name() not in sensitivities:
                sensitivities[param.name()] = {
                    'pruned_percent': [],
                    'loss': [],
                    'size': param.shape()[0]
                }

        metric = None

        for param in sensitivities.keys():
            ratio = self.delta_rate
            while ratio < 1:
                ratio = round(ratio, 2)
                if ratio in sensitivities[param]['pruned_percent']:
                    _logger.debug('{}, {} has computed.'.format(param, ratio))
                    ratio += self.delta_rate
                    continue
                if metric is None:
                    metric = self._eval_graph(context, self.eval_rate,
                                              cached_id)
W
whs 已提交
793 794

                param_backup = {}
795 796 797 798 799
                # prune parameter by ratio
                self._prune_parameters(
                    context.eval_graph,
                    context.scope, [param], [ratio],
                    context.place,
W
whs 已提交
800 801
                    lazy=True,
                    param_backup=param_backup)
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
                self.pruned_list[0]
                # get accuracy after pruning and update self.sensitivities
                pruned_metric = self._eval_graph(context, self.eval_rate,
                                                 cached_id)
                loss = metric - pruned_metric
                _logger.info("pruned param: {}; {}; loss={}".format(
                    param, ratio, loss))
                for brother in self.pruned_list[0]:
                    if re.match(self.pruned_params, brother):
                        if brother not in sensitivities:
                            sensitivities[brother] = {
                                'pruned_percent': [],
                                'loss': []
                            }
                        sensitivities[brother]['pruned_percent'].append(ratio)
                        sensitivities[brother]['loss'].append(loss)

                self._save_sensitivities(sensitivities, sensitivities_file)

                # restore pruned parameters
W
whs 已提交
822
                for param_name in param_backup.keys():
823
                    param_t = context.scope.find_var(param_name).get_tensor()
W
whs 已提交
824
                    param_t.set(self.param_backup[param_name], context.place)
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

#                pruned_metric = self._eval_graph(context)

                ratio += self.delta_rate
        return sensitivities

    def _get_best_ratios(self, context, sensitivities, target_ratio):
        """
        Search a group of ratios for pruning target flops.
        """
        _logger.info('_get_best_ratios for pruning ratie: {}'.format(
            target_ratio))

        def func(params, x):
            a, b, c, d = params
            return a * x * x * x + b * x * x + c * x + d

        def error(params, x, y):
            return func(params, x) - y

        def slove_coefficient(x, y):
            init_coefficient = [10, 10, 10, 10]
            coefficient, loss = leastsq(error, init_coefficient, args=(x, y))
            return coefficient

        min_loss = 0.
        max_loss = 0.

        # step 1: fit curve by sensitivities
        coefficients = {}
        for param in sensitivities:
            losses = np.array([0] * 5 + sensitivities[param]['loss'])
            precents = np.array([0] * 5 + sensitivities[param][
                'pruned_percent'])
            coefficients[param] = slove_coefficient(precents, losses)
            loss = np.max(losses)
            max_loss = np.max([max_loss, loss])

        # step 2: Find a group of ratios by binary searching.
        flops = context.eval_graph.flops()
        model_size = context.eval_graph.numel_params()
        ratios = []
        while min_loss < max_loss:
            loss = (max_loss + min_loss) / 2
            _logger.info(
                '-----------Try pruned ratios while acc loss={:.4f}-----------'.
                format(loss))
            ratios = []
            # step 2.1: Get ratios according to current loss
            for param in sensitivities:
                coefficient = copy.deepcopy(coefficients[param])
                coefficient[-1] = coefficient[-1] - loss
                roots = np.roots(coefficient)
                for root in roots:
                    min_root = 1
                    if np.isreal(root) and root > 0 and root < 1:
                        selected_root = min(root.real, min_root)
                ratios.append(selected_root)
            _logger.info('Pruned ratios={}'.format(
                [round(ratio, 3) for ratio in ratios]))
            # step 2.2: Pruning by current ratios
W
whs 已提交
886
            param_shape_backup = {}
887 888 889 890 891 892
            self._prune_parameters(
                context.eval_graph,
                context.scope,
                sensitivities.keys(),
                ratios,
                context.place,
W
whs 已提交
893 894
                only_graph=True,
                param_shape_backup=param_shape_backup)
895 896 897 898 899 900

            pruned_flops = 1 - (float(context.eval_graph.flops()) / flops)
            pruned_size = 1 - (float(context.eval_graph.numel_params()) /
                               model_size)
            _logger.info('Pruned flops: {:.4f}'.format(pruned_flops))
            _logger.info('Pruned model size: {:.4f}'.format(pruned_size))
W
whs 已提交
901 902
            for param in param_shape_backup.keys():
                context.eval_graph.var(param).set_shape(param_shape_backup[
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
                    param])

            # step 2.3: Check whether current ratios is enough
            if abs(pruned_flops - target_ratio) < 0.015:
                break
            if pruned_flops > target_ratio:
                max_loss = loss
            else:
                min_loss = loss
        return sensitivities.keys(), ratios

    def _current_pruning_target(self, context):
        '''
        Get the target pruning rate in current epoch.
        '''
        _logger.info('Left number of pruning steps: {}'.format(self.num_steps))
        if self.num_steps <= 0:
            return None
        if (self.start_epoch == context.epoch_id) or context.eval_converged(
                self.metric_name, 0.005):
            self.num_steps -= 1
            return self.pruning_step

    def on_epoch_begin(self, context):
        current_ratio = self._current_pruning_target(context)
        if current_ratio is not None:
            sensitivities = self._compute_sensitivities(context)
            params, ratios = self._get_best_ratios(context, sensitivities,
                                                   current_ratio)
            self._prune_parameters(context.optimize_graph, context.scope,
                                   params, ratios, context.place)

            model_size = context.eval_graph.numel_params()
            flops = context.eval_graph.flops()
            _logger.debug('################################')
            _logger.debug('#          pruning eval graph    #')
            _logger.debug('################################')
            self._prune_graph(context.eval_graph, context.optimize_graph)
            context.optimize_graph.update_groups_of_conv()
            context.eval_graph.update_groups_of_conv()
            context.optimize_graph.compile()  # to update the compiled program
            context.eval_graph.compile(
                for_parallel=False,
                for_test=True)  # to update the compiled program
            _logger.info(
                '------------------finish pruning--------------------------------'
            )
            _logger.info('Pruned size: {:.3f}'.format(1 - (float(
                context.eval_graph.numel_params()) / model_size)))
            _logger.info('Pruned flops: {:.3f}'.format(1 - (float(
                context.eval_graph.flops()) / flops)))
            metric = self._eval_graph(context)
            _logger.info('Metric after pruning: {:.2f}'.format(metric))
            _logger.info(
                '------------------SensitivePruneStrategy.on_epoch_begin finish--------------------------------'
            )