inference_transpiler.py 24.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

15 16
from __future__ import print_function

17
import os
X
Xin Pan 已提交
18
import sys
19
import numpy as np
20 21 22
from .. import core
from ..framework import Program
from ..executor import global_scope
23 24


25
class InferenceTranspiler(object):
L
Luo Tao 已提交
26
    '''
27 28 29 30 31 32
    Convert the fluid program to optimized inference program.

    There are several optimizations:

      - fuse convolution and batch normalization
      - fuse batch normalization and relu (MKLDNN only)
L
Luo Tao 已提交
33 34

    Examples:
35

L
Luo Tao 已提交
36 37 38 39 40 41 42 43 44
    .. code-block:: python

        # As InferenceTranspiler will modify the original program,
        # please clone before use it.
        inference_transpiler_program = program.clone()
        t = fluid.InferenceTranspiler()
        t.transpile(inference_transpiler_program, place)
    '''

L
Luo Tao 已提交
45
    def transpile(self, program, place, scope=None):
46
        '''
L
Luo Tao 已提交
47 48 49 50 51 52
        Run the transpiler.

        Args:
            program (Program): program to transpile
            place (Place): inference place
            scope (Scope|None): inference Scope
L
Luo Tao 已提交
53
        '''
X
polish  
Xin Pan 已提交
54 55 56
        sys.stderr.write("InferenceTranspiler is deprecated since it's not "
                         "safe. Users should be "
                         "responsible for constructing the inference program\n")
L
Luo Tao 已提交
57 58 59 60 61 62 63
        if not isinstance(program, Program):
            raise TypeError("program should be as Program type")
        if not isinstance(place, core.CPUPlace) and not isinstance(
                place, core.CUDAPlace):
            raise TypeError("place should be as CPUPlace/CUDAPlace type")
        if scope is None:
            scope = global_scope()
S
sneaxiy 已提交
64
        if not isinstance(scope, core._Scope):
L
Luo Tao 已提交
65
            raise TypeError("scope should be as Scope type or None")
66
        use_mkldnn = bool(os.getenv("FLAGS_use_mkldnn", False))
M
Michal Gallus 已提交
67

68 69 70
        if use_mkldnn:
            self._depthwise_conv_mkldnn(program)

71
        self._fuse_batch_norm(program, place, scope)
72 73
        if use_mkldnn:
            self._fuse_conv_bias_mkldnn(program)
M
Michal Gallus 已提交
74
            self._fuse_conv_relu_mkldnn(program)
75 76 77
            self._fuse_conv_eltwise_mkldnn(program)
            self._fuse_conv_relu_mkldnn(
                program)  # ResNet residual block merging
M
Michal Gallus 已提交
78
            self._fuse_bn_relu_mkldnn(program)
79
            self._fuse_mul_add_mkldnn(program)
M
Michal Gallus 已提交
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        self._is_test_pass(program)

    def _is_test_pass(self, program):
        '''
        Transpile the program setting is_test = true for all layers and
        inserts is_test attribute to pooling and activation layers.
        As a result some operators might run faster
        :param program: program to transpile
        :type program: Program
        '''
        self.block = program.block(0)

        i = 0
        while i < len(self.block.ops):
            current_op = self.block.ops[i]
            if current_op.has_attr("is_test"):
                current_op._set_attr("is_test", True)
            elif current_op.type in [
                    "pool2d", "sigmoid", "logsigmoid", "softshrink", "exp",
                    "brelu", "pow", "leaky_relu", "stanh", "relu", "tanh",
                    "tanh_shrink", "sqrt", "abs", "ceil", "elu", "floor", "cos",
                    "sin", "round", "reciprocal", "hard_shrink", "hard_sigmoid",
                    "relu6", "soft_relu", "swish", "thresholded_relu", "log",
                    "square", "softplus", "softsign"
            ]:
                current_op._set_attr("is_test", True)
            i = i + 1
        # TODO(luotao): use clone() method to flush the program.desc in force,
        # since some large program.desc will not be flushed immediately.
        # And a better solution will be considered later.
        program = program.clone()

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
    def _depthwise_conv_mkldnn(self, program):
        '''
        Transpile the program by replacing depthwise_conv2d to conv2d for MKLDNN program.
        The result is:
            - before:
                - any_other_op->depthwise_conv->any_other_op
            - after:
                - any_other_op->conv->any_other_op
        :param program: program to transpile
        :type program: Program
        '''
        self.block = program.block(0)

        i = 0
        while i < len(self.block.ops):
            current_op = self.block.ops[i]
            if current_op.type == 'depthwise_conv2d':
                current_op.desc.set_type("conv2d")
            i = i + 1

        # TODO(luotao): use clone() method to flush the program.desc in force,
        # since some large program.desc will not be flushed immediately.
        # And a better solution will be considered later.
        program = program.clone()

138 139 140 141
    def _fuse_conv_eltwise_mkldnn(self, program):
        '''
        Transpile the program fusing elementwise_add into conv for MKLDNN
        program. Elementwise add following convolution OP can be fused by adding
142
        'fuse_residual_connection' attribute to convolution OP and replacing its output
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        Tensor with second parameter of elementwise_add.
        The result of fuse is:
            - before:
                - conv->elementwise_add->any_other_op
            - after:
                - conv->any_other_op
        :param program: program to transpile
        :type program: Program
        '''
        self.block = program.block(0)

        i = 0
        while i < len(self.block.ops):
            current_op = self.block.ops[i]
            if current_op.type in ['conv2d']:
                next_op = self.block.ops[i + 1]
                if next_op.type == 'elementwise_add':
160 161
                    self._fuse_conv_eltwise(i, current_op, next_op)
                    self.block._remove_op(i + 1)  # Remove old conv
162 163 164 165 166 167 168 169 170
                    self.block._remove_op(i + 1)  # Remove elementwise_add
            i = i + 1
        self._adjust_input()
        self._remove_unused_var()
        # TODO(luotao): use clone() method to flush the program.desc in force,
        # since some large program.desc will not be flushed immediately.
        # And a better solution will be considered later.
        program = program.clone()

M
Michal Gallus 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    def _fuse_conv_relu_mkldnn(self, program):
        '''
        Transpile the program by fused relu activation for MKLDNN program.
        Relu activation following convolution OP can be fused by adding
        'fuse_relu' attribute to convolution OP.
        The result of fuse is:
            - before:
                - conv->relu->any_other_op
            - after:
                - conv->any_other_op
        :param program: program to transpile
        :type program: Program
        '''
        self.block = program.block(0)

        i = 0
        while i < len(self.block.ops):
            current_op = self.block.ops[i]
            if current_op.type in ['conv2d']:
                next_op = self.block.ops[i + 1]
                if next_op.type == 'relu':
192
                    # modify bnorm OP to include relu
K
Krzysztof Binias 已提交
193
                    current_op._set_attr("fuse_relu", True)
194
                    # remove relu OP
M
Michal Gallus 已提交
195 196 197 198 199 200 201
                    self.block._remove_op(i + 1)
            i = i + 1

        # TODO(luotao): use clone() method to flush the program.desc in force,
        # since some large program.desc will not be flushed immediately.
        # And a better solution will be considered later.
        program = program.clone()
202

M
Michal Gallus 已提交
203
    def _fuse_bn_relu_mkldnn(self, program):
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
        '''
        Transpile the program by fused relu activation for MKLDNN program.

        Relu activation following batch norm OP can be fused by adding
        :math:`fuse_with_relu` attribute to batch norm OP.

        The result of fuse is:

        - before:

          - batch_norm->relu->any_other_op

        - after:

          - batch_norm->any_other_op

        :param program: program to transpile
        :type program: Program
        '''
        self.block = program.block(0)

        i = 0
        while i < len(self.block.ops) - 1:
            current_op = self.block.ops[i]
            if current_op.type in ['batch_norm']:
                next_op = self.block.ops[i + 1]
                if next_op.type == 'relu':
                    # modify bnorm OP to include relu
W
Wu Yi 已提交
232
                    current_op._set_attr("fuse_with_relu", True)
233
                    # remove relu OP
W
Wu Yi 已提交
234
                    self.block._remove_op(i + 1)
235 236 237 238 239 240 241
            i = i + 1

        self._remove_unused_var()
        # TODO(luotao): use clone() method to flush the program.desc in force,
        # since some large program.desc will not be flushed immediately.
        # And a better solution will be considered later.
        program = program.clone()
L
Luo Tao 已提交
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 293 294 295 296 297 298 299 300 301 302 303 304
    def _fuse_conv_bias_mkldnn(self, program):
        '''
        Transpile the program by fused convolution and elementwise_add.

        Replace conv2d and elementwise_add ops with a new conv2d op
        based on an old conv2d op and the :math:`Bias` taken from
        elementwise_add.

        For input :math:`X`:

        - Conv process:            :math:`X = input * W`
        - Elementwise_add process: :math` X = X + bias`

        After fuse into one operation:

        .. math::

            X = input * W + bias

        The operator transformation is:

        - before:

          - conv->elementwise_add->any_other_op

        - after:

          - conv->any_other_op

        The transpile stages are:

        1. Extract bias and output variables from elementwise_add.
        2. Extract Input, Weight and attributes from conv op.
        3. Create a new convolution op based on extracted params.
        4. Remove old conv op.
        5. Remove elementwise_add.
        5. Remove unused variables.

        Args:
            program (Program): program to transpile

        '''
        self.block = program.block(0)

        i = 0
        while i < len(self.block.ops) - 2:
            current_op = self.block.ops[i]
            next_op = self.block.ops[i + 1]
            # conv2d with bias
            if current_op.type in ['conv2d'] and \
               next_op.type in ['elementwise_add']:
                self._fuse_conv_bias(i, current_op, next_op)
                self.block._remove_op(i + 1)  # Remove old conv
                self.block._remove_op(i + 1)  # Remove elementwise_add
            i = i + 1

        self._remove_unused_var()
        # TODO(luotao): use clone() method to flush the program.desc in force,
        # since some large program.desc will not be flushed immediately.
        # And a better solution will be considered later.
        program = program.clone()

W
Wu Yi 已提交
305
    def _fuse_batch_norm(self, program, place, scope):
L
Luo Tao 已提交
306 307
        '''
        Transpile the program by fused batch normalization.
308 309 310

        The batch normalization followed the convolution or fully connected layer
        can be integrated with them. Doing so will give us a forward acceleration,
311
        especially in environments like mobile or embedded.
312

L
Luo Tao 已提交
313 314
        For input :math:`X`:

315 316
        - Conv process:        :math:`X = input * W + bias`
        - Batch norm process:  :math:`X' = (X - mean) / std`
L
Luo Tao 已提交
317
        - Scale Process:       :math:`Y = a * X' + b`
318 319 320

        After fuse into one operation:

L
Luo Tao 已提交
321 322 323 324
        .. math::

            Y &= (input * W + bias - mean) / std * a + b \\\\
              &= input * a * W / std + ((bias - mean) / std * a + b)
325

326
        The operator transformation is:
L
Luo Tao 已提交
327

328
        - before:
L
Luo Tao 已提交
329

330 331
          - conv->batch_norm->any_other_op (bias == 0)
          - conv->elementwise_add->batch_norm->any_other_op (bias != 0)
332 333

        - after:
L
Luo Tao 已提交
334

335
          - conv->elementwise_add->any_other_op
336

337
        The transpile stages are:
L
Luo Tao 已提交
338

339
        1. insert elementwise_add op when bias == 0.
340
        2. fuse the batch_norm's parameters to conv and elementwise_add operators.
341 342 343
        3. remove batch_norm ops which are not used in any other ops.
        4. adjust the input of any_other_op to be the output of elementwise_add operator.
        5. remove unused variables.
344

L
Luo Tao 已提交
345 346 347 348
        Args:
            program (Program): program to transpile
            place (Place): inference place
            scope (Scope): inference Scope
349

350 351 352
        '''
        self.scope = scope
        self.place = place
353
        self.block = program.block(0)
354
        self.input_map = {}  # store the input names should be adjusted
355

356
        i = 0
357
        while i < len(self.block.ops) - 2:
358
            current_op = self.block.ops[i]
359
            # TODO(luotao1): consider only conv2d now. fc would be delt later.
360
            if current_op.type in ['conv2d']:
361 362
                # TODO(luotao1): consider single chain network now.
                # For branch network, we counldn't use block.ops[i + 1] as
L
Luo Tao 已提交
363
                # the judgment condition.
364
                next_op = self.block.ops[i + 1]
365
                # conv2d without bias
366
                if (next_op.type == 'batch_norm'):
367 368 369
                    # insert bias op
                    bias_op = self._insert_bias_op(i + 1, current_op, next_op)
                    # fuse batch_norm
370
                    self._fuse_param(current_op, next_op, bias_op, 0)
371
                    # remove batch_norm_op
W
Wu Yi 已提交
372
                    self.block._remove_op(i + 2)
373
                    i = i + 1
374 375 376 377 378 379 380
                # conv2d with bias, the next_op.type is elementwise_add
                elif (next_op.type == 'elementwise_add'):
                    next_next_op = self.block.ops[i + 2]
                    if (next_next_op.type == 'batch_norm'):
                        # fuse batch_norm
                        self._fuse_param(current_op, next_next_op, next_op, 1)
                        # remove batch_norm_op
W
Wu Yi 已提交
381
                        self.block._remove_op(i + 2)
382
                        i = i + 1
383
            i = i + 1
384
        self._adjust_input()
385
        self._remove_unused_var()
386 387
        # TODO(luotao): use clone() method to flush the program.desc in force,
        # since some large program.desc will not be flushed immediately.
L
Luo Tao 已提交
388
        # And a better solution will be considered later.
L
Luo Tao 已提交
389
        program = program.clone()
390

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    def _fuse_mul_add_mkldnn(self, program):
        '''
        Transpile the program by fusing Mul+Add layers to FC layer with the MKL-DNN inner product.
        The MUL following a Elementwise_add layer can be replaced by the MKL-DNN FC.
        The Elementwise add's bias input 'Y' has to be added into the
        MKL-DNN-based FC input 'Bias'.
         The operator transformation is:
         - before:
           - MUL->elementwise_add -> any_other_op
         - after:
           - FC -> any_other_op
         The transpile stages are:
         1. insert a new MKL-DNN-based FC operator with `Bias` input
            taken from the Elementwise add's input 'Y' (bias),
        2. fuse the parameters of MUL and Elemenwise add,
        3. remove the MUL, elementwise_add operators,
        4. make the input of the deleted Elementwise add operator to be the input of the
           new FC operator,
        5. remove unused variables,
         Args:
            program (Program): program to transpile
         '''

        self.block = program.block(0)
        self.input_map = {}  # store the input names should be adjusted
        i = 0
        while i < len(self.block.ops):
            # find a elementwise add op
            if self.block.ops[i].type == 'elementwise_add':
                add_op = self.block.ops[i]
                add_idx = i
                mul_idx = -1
                # find the preceding mul op
                for j in reversed(range(add_idx)):
                    if self.block.ops[j].type == 'mul':
                        mul_out_name = self.block.ops[j].output_arg_names[0]
                        if self.block.ops[j].output_arg_names[
                                0] in add_op.input_arg_names:
                            mul_op = self.block.ops[j]
                            mul_idx = j
                            break
                if mul_idx < 0:
                    i += 1
                    continue
                # create and insert a new fc op
                fc_op_new = self._insert_fc_op(add_idx + 1, mul_op, add_op)
                # remove the old operators
                self.block._remove_op(add_idx)
                self.block._remove_op(mul_idx)
                # restart scanning for elementwise add from the deleted mul's index
                i = mul_idx
            i += 1
        self._adjust_input()
        self._remove_unused_var()
        program = program.clone()

447 448 449
    # ====================== private transpiler functions =====================
    def _insert_bias_op(self, index, current_op, bn_op):
        '''
450
        Construct elementwise_add operator for adding bias
451
        and insert it into program.
452

453 454 455 456 457 458 459 460 461 462 463
        :param index: insert location of bias_op
        :type index: Int
        :param current_op: current operator (conv or fc)
        :type current_op: Operator
        :param bn_op: batch norm operator
        :type bn_op: Operator
        :return: bias_op
        :rtype: Operator
        '''
        # The input of bias_op is current_op's output and Bias of bn_op
        # The output of bias_op is bn_op's output
464 465 466 467
        x_var = self.block.var(current_op.output("Output")[0])
        y_var = self.block.var(bn_op.input("Bias")[0])
        out_var = self.block.var(bn_op.output("Y")[0])

W
Wu Yi 已提交
468
        bias_op = self.block._insert_op(
469 470 471 472 473 474
            index,
            type="elementwise_add",
            inputs={"X": x_var,
                    "Y": y_var},
            outputs={"Out": out_var},
            attrs={"axis": 1})  # dim_start=1
475 476
        return bias_op

477
    def _fuse_param(self, current_op, bn_op, bias_op, with_bias):
478 479
        '''
        fuse the batch_norm_op' parameters to current_op (conv or fc)
480

481 482 483 484 485 486
        :param current_op: current operator (conv or fc)
        :type current_op: Operator
        :param bn_op: batch norm operator
        :type bn_op: Operator
        :param bias_op: elementwise_add operator for adding bias
        :type bias_op: Operator
487
        :param with_bias: If current operator has bias, with_bias = 1; otherwise 0.
488
        :type with_bias: Int
489 490
        '''

L
Luo Tao 已提交
491 492 493 494 495 496 497 498 499 500 501
        def _update_param(op, old_param_name, new_param):
            # For the sake of remaining the original variables the same as before,
            # create new variables in scope to store the new parameters.
            old_param_name = old_param_name[0]
            old_var = self.block.vars[old_param_name]
            new_param_name = old_param_name + '_fuse_bn'
            new_var = self.block.create_parameter(
                name=new_param_name.encode('ascii'),
                type=old_var.type,
                dtype=old_var.dtype,
                shape=old_var.shape)
W
Wu Yi 已提交
502
            op._rename_input(old_param_name, new_param_name)
L
Luo Tao 已提交
503 504 505 506
            self.scope.var(new_param_name)

            tensor = self.scope.find_var(new_param_name).get_tensor()
            tensor.set(np.array(new_param), self.place)
507 508

        def _load_param(param_name):
L
Luo Tao 已提交
509
            return np.array(self.scope.find_var(param_name[0]).get_tensor())
510 511 512 513 514 515 516 517 518 519 520 521

        bias_bn = _load_param(bn_op.input("Bias"))  #Bias
        scale_bn = _load_param(bn_op.input("Scale"))  #Scale
        mean_bn = _load_param(bn_op.input("Mean"))  #Mean
        var_bn = _load_param(bn_op.input("Variance"))  #Variance

        # TODO(luotao1): consider only conv2d now. fc would be delt later.
        current_param = _load_param(current_op.input("Filter"))
        std_bn = np.float32(np.sqrt(np.add(var_bn, 1e-5)))
        tmp = np.float32(np.divide(scale_bn, std_bn))

        # add bias of batch_norm_op to conv2d
522 523 524 525
        if with_bias:
            bias = _load_param(bias_op.input("Y"))
        else:
            bias = np.zeros(bias_bn.shape)
526 527 528 529 530 531 532 533 534
        bias = np.float32(
            np.add(np.multiply(np.subtract(bias, mean_bn), tmp), bias_bn))

        # re-compute weight of conv2d
        tmp = tmp.reshape(tmp.shape[0], -1)
        dst_param = current_param.reshape((tmp.shape[0], -1))
        dst_param = np.float32(np.multiply(dst_param, tmp))
        dst_param = dst_param.reshape(current_param.shape)

L
Luo Tao 已提交
535 536 537
        # update parameters
        _update_param(current_op, current_op.input("Filter"), dst_param)
        _update_param(bias_op, bias_op.input("Y"), bias)
538

539 540 541
        # collect the renamed input
        self.input_map[bn_op.output("Y")[0]] = bias_op.output("Out")[0]

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    def _fuse_conv_bias(self, index, conv_op, elementwise_add_op):
        '''
        fuse the conv op with elementwise_add

        :param index: index of the conv_op in ops list
        :type index: Int
        :param conv_op: convolution operator
        :type conv_op: Operator
        :param elementwise_add_op: convolution's bias operator
        :type elementwise_add_op: Operator
        '''

        bias_var = self.block.var(elementwise_add_op.input("Y")[0])
        out_var = self.block.var(elementwise_add_op.output("Out")[0])
        filter_var = self.block.var(conv_op.input("Filter")[0])
        in_var = self.block.var(conv_op.input("Input")[0])
        attrs = {name: conv_op.attr(name) for name in conv_op.attr_names}

        self.block._insert_op(
            index,
            type="conv2d",
            inputs={"Input": in_var,
                    "Filter": filter_var,
                    "Bias": bias_var},
            outputs={"Output": out_var},
            attrs=attrs)

569 570 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
    def _insert_fc_op(self, index, mul_op, add_op):
        '''
        Construct a new FC operator by copying the old Mul and adding the
        'Y' input taken from the Elementwise add's input 'Y'.
        :param index: insert location of FC
        :type  index: Int
        :param mul_op: MUL operator to be copied
        :type  mul_op: Operator
        :param add_op: Elementwise add operator taken bias from
        :type  add_op: Operator
        :return: fc_op_new
        :type:   Operator
        '''

        def get_op_outputs(op, names):
            result = {}
            for name in names:
                result[name] = self.block.var(op.output(name)[0])
            return result

        fc_inputs = {}
        fc_inputs['Input'] = self.block.var(mul_op.input('X')[0])
        fc_inputs['W'] = self.block.var(mul_op.input('Y')[0])
        fc_inputs['Bias'] = self.block.var(add_op.input('Y')[0])
        fc_outputs = get_op_outputs(add_op, ['Out'])
        fc_attrs = {}
        fc_attrs['use_mkldnn'] = True

        fc_op_new = self.block._insert_op(
            index,
            type='fc',
            inputs=fc_inputs,
            outputs=fc_outputs,
            attrs=fc_attrs)
        return fc_op_new

605
    def _fuse_conv_eltwise(self, index, conv_op, eltwise_op):
606 607 608 609 610 611 612 613 614
        '''
        fuse the conv op with elementwise_add

        :param conv_op: convolution operator
        :type conv_op: Operator
        :param eltwise_op: operator adding data from skip connection
        :type eltwise_op: Operator
        '''

615 616 617 618 619 620 621 622 623
        eltwise_input = "X"
        if eltwise_op.input("X")[0] == conv_op.output("Output")[0]:
            eltwise_input = "Y"

        residual_var = self.block.vars[eltwise_op.input(eltwise_input)[0]]
        out_var = self.block.vars[eltwise_op.output("Out")[0]]
        filter_var = self.block.vars[conv_op.input("Filter")[0]]
        in_var = self.block.vars[conv_op.input("Input")[0]]
        bias_var = self.block.vars[conv_op.input("Bias")[0]]
624

625
        conv_op._set_attr("fuse_residual_connection", True)
626 627 628 629 630 631 632 633 634 635 636 637 638
        attrs = {name: conv_op.attr(name) for name in conv_op.attr_names}

        self.block._insert_op(
            index,
            type="conv2d",
            inputs={
                "Input": in_var,
                "Filter": filter_var,
                "Bias": bias_var,
                "ResidualData": residual_var
            },
            outputs={"Output": out_var},
            attrs=attrs)
639

640
    def _adjust_input(self):
641 642 643 644
        for i in range(len(self.block.ops)):
            current_op = self.block.ops[i]
            for input_arg in current_op.input_arg_names:
                if input_arg in self.input_map:
W
Wu Yi 已提交
645 646
                    current_op._rename_input(input_arg,
                                             self.input_map[input_arg])
647

648 649
    def _remove_unused_var(self):
        '''
650
        remove unused varibles in program
651 652
        '''
        args = []
653 654 655 656
        for i in range(len(self.block.ops)):
            current_op = self.block.ops[i]
            args += current_op.input_arg_names
            args += current_op.output_arg_names
657 658
        args = list(set(args))  # unique the input and output arguments

659
        for var in list(self.block.vars.keys()):
660
            if var not in args:
W
Wu Yi 已提交
661
                self.block._remove_var(var)