opset_legacy.py 112.2 KB
Newer Older
W
wjj19950828 已提交
1
# Copyright (c) 2022  PaddlePaddle Authors. All Rights Reserved.
S
SunAhong1993 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#
# 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 x2paddle.decoder.onnx_decoder import ONNXGraph, ONNXGraphNode, ONNXGraphDataNode
from x2paddle.core.graph import GraphNode
from x2paddle.core.util import *
from functools import reduce
import numpy as np
import onnx
import onnx.numpy_helper as numpy_helper
from onnx.mapping import TENSOR_TYPE_TO_NP_TYPE
import logging as _logging
from collections import OrderedDict
import math
import os
import copy
import sys
import shutil

W
wjj19950828 已提交
31
_logger = _logging.getLogger()
S
SunAhong1993 已提交
32 33 34 35 36 37 38 39 40


def _const_weight_or_none(node, necessary=False):
    if 'Constant' in node.layer_type:
        return node.value
    if isinstance(node, ONNXGraphDataNode):
        return node.weight
    if necessary:
        assert '{} should be an initializer or Constant operator.'.format(
S
SunAhong1993 已提交
41
            node.name)
S
SunAhong1993 已提交
42 43 44
    return None


45 46 47
def _rename_or_remove_weight(weights,
                             origin_name,
                             target_name=None,
48 49
                             is_remove=True,
                             rename_mapper=None):
50
    '''
51 52 53 54
    Rename parameters by Paddle's naming rule of parameters.

    Args:
        weights(dict[String:np.ndarray]): Dict stored paramters, the key in weights is name of parameter.
55
        origin_name(String): Name of parameter to rename or remove.
56 57
        target_name(String, optional): if target_name is not None, add new key-value pair
            {target_name:weights[origin_name]} to weights, and target_name must follow paddle's
58
            naming rule of parameters. Default: None.
59
        is_remove: if is_remove is True, remove origin key-value pair. Default: True.
60
        rename_mapper: Solved the same data is used for multiple OPs, key is old_name, value is new_name.
61 62
    Returns:
        None
63
    '''
64 65 66
    if rename_mapper is not None and origin_name in rename_mapper:
        origin_name = rename_mapper[origin_name]
        is_remove = False
C
Channingss 已提交
67
    if origin_name not in weights:
68
        raise KeyError('{} not a key in {}'.format(origin_name, weights.keys()))
Y
yeliang2258 已提交
69 70 71 72 73
    if is_remove:
        # remove weight
        data = weights.pop(origin_name)
    else:
        data = weights[origin_name]
C
Channingss 已提交
74 75 76
    if target_name is not None:
        # rename weight
        weights[target_name] = data
77
        rename_mapper[origin_name] = target_name
C
Channingss 已提交
78

79

S
SunAhong1993 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
def _is_static_shape(shape):
    negtive_dims = 0
    error_dims = 0
    for dim in shape:
        if dim < 0:
            negtive_dims += 1
        if dim < -1:
            error_dims += 1
    if negtive_dims > 1:
        return False
    if error_dims > 0:
        return False
    return True


W
wjj19950828 已提交
95
def _get_same_padding(in_size, kernel_size, stride, autopad):
S
SunAhong1993 已提交
96 97 98 99
    new_size = int(math.ceil(in_size * 1.0 / stride))
    pad_size = (new_size - 1) * stride + kernel_size - in_size
    pad0 = int(pad_size / 2)
    pad1 = pad_size - pad0
W
wjj19950828 已提交
100 101 102 103
    if autopad == "SAME_UPPER":
        return [pad0, pad1]
    if autopad == "SAME_LOWER":
        return [pad1, pad0]
S
SunAhong1993 已提交
104 105 106 107 108 109 110 111


def print_mapping_info(func):
    def run_mapping(*args, **kwargs):
        node = args[1]
        try:
            res = func(*args, **kwargs)
        except:
112
            raise Exception("convert failed node:{}, op_type is {}".format(
S
SunAhong1993 已提交
113
                node.name[9:], node.layer_type))
S
SunAhong1993 已提交
114 115 116 117 118 119
        else:
            return res

    return run_mapping


W
wjj19950828 已提交
120
class OpSet():
S
SunAhong1993 已提交
121
    def __init__(self, decoder, paddle_graph):
W
wjj19950828 已提交
122
        super(OpSet, self).__init__()
S
SunAhong1993 已提交
123 124 125 126 127
        self.graph = decoder.graph
        self.paddle_graph = paddle_graph
        self.inputs_info = dict()
        self.weights = dict()
        self.nn_name2id = dict()
S
fix  
SunAhong1993 已提交
128
        self.done_weight_list = list()
129 130 131
        # solve for same data is used as an argument to multiple OPs.
        # PR link(wangjunjie06): https://github.com/PaddlePaddle/X2Paddle/pull/728
        self.rename_mapper = dict()
W
wjj19950828 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 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
        self.elementwise_ops = {
            'Add': 'paddle.add',
            'Div': 'paddle.divide',
            'Sub': 'paddle.subtract',
            'Mul': 'paddle.multiply',
            'Pow': 'paddle.pow',
            'Less': 'paddle.less_than',
            'LessOrEqual': 'paddle.less_equal',
        }

        self.directly_map_ops = {
            'Ceil': ['paddle.ceil'],
            # reduce function
            'ReduceMean': [
                'paddle.mean', dict(
                    axes='axis', keepdims='keepdim'), dict(
                        axes=None, keepdims=True)
            ],
            'ReduceMin': [
                'paddle.min', dict(
                    axes='axis', keepdims='keepdim'), dict(
                        axes=None, keepdim=True)
            ],
            'ReduceMax': [
                'paddle.max', dict(
                    axes='axis', keepdims='keepdim'), dict(
                        axes=None, keepdim=True)
            ],
            'ReduceProd': [
                'paddle.prod', dict(
                    axes='axis', keepdims='keepdim'), dict(
                        axes=None, keepdim=True)
            ],
            # active function
            'Relu': ['paddle.nn.ReLU'],
            'LeakyRelu': [
                'paddle.nn.LeakyReLU', dict(alpha='negative_slope'),
                dict(negative_slope=.01)
            ],
            'Elu':
            ['paddle.nn.functional.elu', dict(alpha='alpha'), dict(alpha=1.)],
            'ThresholdedRelu': [
                'paddle.nn.functional.thresholded_relu',
                dict(alpha='threshold'), dict(alpha=1.)
            ],
            'Tanh': ['paddle.nn.Tanh'],
            'Sigmoid': ['paddle.nn.Sigmoid'],
            'Softsign': ['paddle.nn.Softsign'],
            'Softplus': [
                'paddle.nn.Softplus', dict(threshold='threshold'),
                dict(threshold=float(sys.maxsize))
            ],
            'Exp': ['paddle.exp'],
            'Log': ['paddle.log'],
            'LogSoftmax': [
                'paddle.nn.functional.log_softmax', dict(axis='axis'),
                dict(axis=1)
            ],
            'Softmax': ['paddle.nn.Softmax', dict(axis='axis'), dict(axis=1)],
            'Sqrt': ['paddle.sqrt'],
            'Floor': ['paddle.floor'],
            'Abs': ['paddle.abs'],
            'Erf': ['paddle.erf'],
            'Sin': ['paddle.sin'],
            'Cos': ['paddle.cos'],
        }
S
SunAhong1993 已提交
198 199 200 201 202 203

    @print_mapping_info
    def directly_map(self, node, *args, **kwargs):
        inputs = node.layer.input
        assert len(inputs) == 1, 'directly_map error with multi inputs'
        input = self.graph.get_input_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
204 205 206 207 208 209 210 211 212 213 214 215
        onnx_attrs = node.attr_map
        if '' in onnx_attrs:
            onnx_attrs.pop('')
        if '_' in onnx_attrs:
            onnx_attrs.pop('_')
        op_info = self.directly_map_ops[node.layer_type]
        paddle_op = op_info[0]
        layer_attrs = dict()
        if len(op_info) > 1:
            attrs_name_map_dict = op_info[1]
            for onnx_attr_name, pd_attr_name in attrs_name_map_dict.items():
                if onnx_attr_name in onnx_attrs:
W
WJJ1995 已提交
216 217 218 219 220
                    # convert for dynamic code, mv 0 to False, 1 to True
                    if pd_attr_name == "keepdim":
                        keepdims = False if onnx_attrs[
                            onnx_attr_name] == 0 else True
                        onnx_attrs[onnx_attr_name] = keepdims
S
SunAhong1993 已提交
221 222 223
                    layer_attrs[pd_attr_name] = onnx_attrs[onnx_attr_name]
                else:
                    layer_attrs[pd_attr_name] = op_info[2][onnx_attr_name]
224
        if paddle_op.startswith("paddle.nn") and 'functional' not in paddle_op:
S
SunAhong1993 已提交
225 226
            op_name = paddle_op[10:].lower()
            op_name = name_generator(op_name, self.nn_name2id)
S
SunAhong1993 已提交
227
            output_name = node.name
S
SunAhong1993 已提交
228
            layer_outputs = [op_name, output_name]
229

S
SunAhong1993 已提交
230 231
            self.paddle_graph.add_layer(
                kernel=paddle_op,
S
SunAhong1993 已提交
232
                inputs={"x": input.name},
S
SunAhong1993 已提交
233 234 235 236 237
                outputs=layer_outputs,
                **layer_attrs)
        else:
            self.paddle_graph.add_layer(
                kernel=paddle_op,
S
SunAhong1993 已提交
238 239
                inputs={"x": input.name},
                outputs=[node.name],
240 241
                **layer_attrs)

S
SunAhong1993 已提交
242 243 244 245 246
    @print_mapping_info
    def elementwise_map(self, node):
        op_type = self.elementwise_ops[node.layer_type]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_y = self.graph.get_input_node(node, idx=1, copy=True)
247
        inputs_dict = {'x': val_x.name, 'y': val_y.name}
S
SunAhong1993 已提交
248
        self.paddle_graph.add_layer(
249
            op_type, inputs=inputs_dict, outputs=[node.name])
S
SunAhong1993 已提交
250 251 252 253 254 255 256 257 258 259 260 261

    @print_mapping_info
    def place_holder(self, node):
        shape = node.out_shapes[0]
        for i, dim_shape in enumerate(shape):
            if dim_shape == 0 and i == 0:
                shape[i] = 1
            if dim_shape == 0 and i != 0:
                assert 'shape of input is not assigned'
        self.paddle_graph.add_layer(
            kernel="paddle.to_tensor",
            inputs={},
S
SunAhong1993 已提交
262
            outputs=[node.name],
S
SunAhong1993 已提交
263 264
            data=node.name)
        self.inputs_info[node.name] = [shape, node.dtype]
S
SunAhong1993 已提交
265 266 267 268 269 270 271

    @print_mapping_info
    def create_parameter(self, node, parameter=None):
        if parameter is not None:
            node = parameter
        dtype = node.dtype
        shape = node.out_shapes[0]
Y
yeliang2258 已提交
272

S
fix  
SunAhong1993 已提交
273
        if hasattr(node.weight, "shape") and len(node.weight.shape) == 0:
W
WJJ1995 已提交
274 275
            if node.weight == float('inf') or node.weight == float('-inf'):
                node.weight = string(node.weight)
S
SunAhong1993 已提交
276
            self.paddle_graph.add_layer(
277 278
                "paddle.full",
                inputs={},
S
SunAhong1993 已提交
279
                outputs=[node.name],
S
SunAhong1993 已提交
280 281 282 283
                dtype=string(dtype),
                shape=[1],
                fill_value=node.weight)
        else:
S
SunAhong1993 已提交
284
            self.weights[node.name] = node.weight
S
SunAhong1993 已提交
285 286 287
            self.paddle_graph.add_layer(
                "self.create_parameter",
                inputs={},
S
SunAhong1993 已提交
288
                outputs=[node.name],
S
SunAhong1993 已提交
289
                shape=shape,
S
SunAhong1993 已提交
290
                attr=string(node.name),
S
SunAhong1993 已提交
291
                dtype=string(dtype),
292
                default_initializer="paddle.nn.initializer.Constant(value=0.0)")
S
SunAhong1993 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

    def _pad_if_asymmetric(self, node, pads, val_name):  # pads: SSEE
        assert len(pads) & 1 == 0
        symmetric = True
        ndims = len(pads) // 2
        for idx_dim in range(ndims):
            if pads[idx_dim] != pads[ndims + idx_dim]:
                symmetric = False
                break
        if symmetric:
            return pads[:ndims], val_name
        val_padded = self.Pad(node, op_independent=False)
        return [0] * ndims, val_padded

    def _interpolate(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
309
        inputs = {'x': val_x.name}
S
fix  
SunAhong1993 已提交
310
        attrs = dict()
W
WJJ1995 已提交
311
        val_x_shape = val_x.out_shapes[0]
S
SunAhong1993 已提交
312 313 314 315
        if node.layer_type == 'Resize':
            if len(node.layer.input) == 2:
                # opset 10
                val_scales = self.graph.get_input_node(node, idx=1, copy=True)
316
                # TODO(syf): paddle.nn.functional.interpolate will support the length
S
fix  
SunAhong1993 已提交
317
                # which is the same as the rank of input.
W
WJJ1995 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
                scale_values = _const_weight_or_none(val_scales)
                if scale_values is not None:
                    attrs['scale_factor'] = self.weights[
                        val_scales.name].tolist()[2:]
                else:
                    var_nc, var_hw = val_scales.name + '_nc', val_scales.name + '_hw'
                    self.paddle_graph.add_layer(
                        'paddle.split',
                        inputs={"x": val_scales.name},
                        outputs=[var_nc, var_hw],
                        num_or_sections=[2, 2],
                        axis=0)
                    inputs['scale_factor'] = var_hw
                mode = node.get_attr('mode', 'nearest')
                attrs.update({
                    "align_corners": False,
                    "mode": string(mode),
                    "align_mode": 1
                })
                if mode == "linear" and len(val_x_shape) == 4:
                    attrs["mode"] = string("bilinear")
                self.paddle_graph.add_layer(
                    kernel="paddle.nn.functional.interpolate",
                    inputs=inputs,
                    outputs=[node.name],
                    **attrs)
                return
S
SunAhong1993 已提交
345 346
            elif len(node.layer.input) == 3:
                # opset 11
Q
qqj1130247885 已提交
347
                try:
W
WJJ1995 已提交
348
                    # to avoid the error causeed by NULL value of resize inputs.
Q
qqj1130247885 已提交
349 350 351 352 353
                    val_scales = self.graph.get_input_node(
                        node, idx=2, copy=True)
                except:
                    val_scales = self.graph.get_input_node(
                        node, idx=1, copy=True)
354
                # TODO(syf): paddle.nn.functional.interpolate will support the length
S
fix  
SunAhong1993 已提交
355
                # which is the same as the rank of input.
356 357
                attrs['scale_factor'] = self.weights[val_scales.name].tolist()[
                    2:]
S
SunAhong1993 已提交
358 359 360
            elif len(node.layer.input) == 4:
                # opset 11
                val_sizes = self.graph.get_input_node(node, idx=3, copy=True)
W
WJJ1995 已提交
361
                size_values = _const_weight_or_none(val_sizes)
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
                if len(val_x_shape) == 3:
                    var_n, var_hw = val_sizes.name + '_n', val_sizes.name + '_hw'
                    self.paddle_graph.add_layer(
                        'paddle.split',
                        inputs={"x": val_sizes.name},
                        outputs=[var_n, var_hw],
                        num_or_sections=[1, 2],
                        axis=0)
                    self.paddle_graph.add_layer(
                        "paddle.cast",
                        inputs={"x": var_hw},
                        outputs=[var_hw],
                        dtype=string('int32'))
                    inputs['size'] = var_hw
                    attrs = {
                        "align_corners": False,
                        "mode": string(node.get_attr('mode', 'nearest'))
                    }
                    mode = node.get_attr('mode', 'nearest')
                    if mode == "linear":
                        attrs["mode"] = string("bilinear")
                    if node.get_attr('coordinate_transformation_mode',
                                     'half_pixel') == 'pytorch_half_pixel':
                        attrs["align_corners"] = False
                        attrs["align_mode"] = 0
                    if node.get_attr('coordinate_transformation_mode',
                                     'half_pixel') == 'align_corners':
                        attrs["align_corners"] = True
                    self.paddle_graph.add_layer(
                        'paddle.unsqueeze',
                        inputs={"x": val_x.name},
                        outputs=[val_x.name],
                        axis=0)
                    self.paddle_graph.add_layer(
                        kernel="paddle.nn.functional.interpolate",
                        inputs=inputs,
                        outputs=[node.name],
                        **attrs)
                    self.paddle_graph.add_layer(
                        'paddle.squeeze',
                        inputs={"x": node.name},
                        outputs=[node.name],
                        axis=0)
                else:
W
WJJ1995 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
                    if size_values is not None:
                        attrs["size"] = [size_values[2], size_values[3]]
                    else:
                        var_nc, var_hw = val_sizes.name + '_nc', val_sizes.name + '_hw'
                        self.paddle_graph.add_layer(
                            'paddle.split',
                            inputs={"x": val_sizes.name},
                            outputs=[var_nc, var_hw],
                            num_or_sections=[2, 2],
                            axis=0)
                        self.paddle_graph.add_layer(
                            "paddle.cast",
                            inputs={"x": var_hw},
                            outputs=[var_hw],
                            dtype=string('int32'))
                        inputs['size'] = var_hw
                    attrs.update({
423 424
                        "align_corners": False,
                        "mode": string(node.get_attr('mode', 'nearest'))
W
WJJ1995 已提交
425
                    })
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                    mode = node.get_attr('mode', 'nearest')
                    if mode == "linear":
                        attrs["mode"] = string("bilinear")
                    if node.get_attr('coordinate_transformation_mode',
                                     'half_pixel') == 'pytorch_half_pixel':
                        attrs["align_corners"] = False
                        attrs["align_mode"] = 0
                    if node.get_attr('coordinate_transformation_mode',
                                     'half_pixel') == 'align_corners':
                        attrs["align_corners"] = True
                    self.paddle_graph.add_layer(
                        kernel="paddle.nn.functional.interpolate",
                        inputs=inputs,
                        outputs=[node.name],
                        **attrs)
S
SunAhong1993 已提交
441
                return
S
SunAhong1993 已提交
442
        elif node.layer_type == 'Upsample':
Y
yeliang2258 已提交
443 444 445 446 447 448 449 450 451 452 453 454
            if len(node.layer.input) == 2:
                val_scales = self.graph.get_input_node(node, idx=1, copy=True)
                self.paddle_graph.add_layer(
                    "paddle.slice",
                    inputs={"input": val_scales.name},
                    outputs=[val_scales.name],
                    axes=[0],
                    starts=[2],
                    ends=[4])
                inputs['scale_factor'] = val_scales.name
            else:
                val_scales = node.get_attr('scales')[2:]
455

S
SunAhong1993 已提交
456
        mode = node.get_attr('mode', 'nearest')
457 458 459 460 461
        attrs.update({
            "align_corners": False,
            "mode": string(mode),
            "align_mode": 1
        })
Y
yeliang2258 已提交
462 463
        if len(node.layer.input) == 1:
            attrs["scale_factor"] = val_scales
S
SunAhong1993 已提交
464 465
        if mode == "linear" and len(val_x_shape) == 4:
            attrs["mode"] = string("bilinear")
466 467 468 469 470 471
            if node.get_attr('coordinate_transformation_mode',
                             'half_pixel') == 'pytorch_half_pixel':
                attrs["align_corners"] = False
                attrs["align_mode"] = 0
            else:
                attrs["align_corners"] = True
S
SunAhong1993 已提交
472 473 474
        self.paddle_graph.add_layer(
            kernel="paddle.nn.functional.interpolate",
            inputs=inputs,
S
SunAhong1993 已提交
475
            outputs=[node.name],
S
SunAhong1993 已提交
476
            **attrs)
477

W
WJJ1995 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490
    @print_mapping_info
    def CumSum(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        axis = self.graph.get_input_node(node, idx=1, copy=True)
        axis_values = _const_weight_or_none(axis)
        assert axis_values is not None, 'Axis only support constant tensor!'
        layer_attrs = {'axis': axis_values}
        self.paddle_graph.add_layer(
            'paddle.cumsum',
            inputs={"x": val_x.name},
            outputs=[node.name],
            **layer_attrs)

S
SunAhong1993 已提交
491 492 493 494 495 496 497
    @print_mapping_info
    def HardSigmoid(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        alpha = node.get_attr('alpha', 0.2)
        beta = node.get_attr('beta', 0.5)
        self.paddle_graph.add_layer(
            kernel="paddle.scale",
S
SunAhong1993 已提交
498 499
            inputs={"x": val_x.name},
            outputs=[node.name + "_val"],
S
SunAhong1993 已提交
500 501 502 503
            scale=alpha,
            bias=beta)
        self.paddle_graph.add_layer(
            kernel="paddle.clip",
S
SunAhong1993 已提交
504 505
            inputs={"x": node.name + "_val"},
            outputs=[node.name],
S
SunAhong1993 已提交
506
            min=0.0,
507 508
            max=1.0)

S
SunAhong1993 已提交
509 510 511 512 513 514 515 516
    @print_mapping_info
    def Shape(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        self.paddle_graph.add_layer(
            kernel="paddle.shape",
            inputs={"input": val_x.name},
            outputs=[node.name])
        self.paddle_graph.add_layer(
517 518 519 520
            'paddle.cast',
            inputs={"x": node.name},
            outputs=[node.name],
            dtype=string('int64'))
S
SunAhong1993 已提交
521 522 523 524 525 526 527 528 529 530

    @print_mapping_info
    def RoiAlign(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_rois = self.graph.get_input_node(node, idx=1, copy=True)

        pooled_height = node.get_attr('output_height')
        pooled_width = node.get_attr('output_width')
        spatial_scale = node.get_attr('spatial_scale')
        sampling_ratio = node.get_attr('sampling_ratio')
531 532 533 534 535 536
        val_rois_shape = val_rois.name + '_shape'
        self.paddle_graph.add_layer(
            kernel="paddle.shape",
            inputs={"input": val_rois.name},
            outputs=[val_rois_shape])
        val_rois_num = val_rois.name + '_num'
537 538 539 540 541 542 543 544 545 546 547 548 549 550
        if len(val_rois.out_shapes[0]) == 4:
            self.paddle_graph.add_layer(
                'paddle.split',
                inputs={"x": val_rois_shape},
                outputs=[val_rois_num, ' _', ' _', ' _'],
                num_or_sections=[1, 1, 1, 1],
                axis=0)
        elif len(val_rois.out_shapes[0]) == 2:
            self.paddle_graph.add_layer(
                'paddle.split',
                inputs={"x": val_rois_shape},
                outputs=[val_rois_num, ' _'],
                num_or_sections=[1, 1],
                axis=0)
S
SunAhong1993 已提交
551 552 553 554 555 556 557
        layer_attrs = {
            'pooled_height': pooled_height,
            'pooled_width': pooled_width,
            'spatial_scale': spatial_scale,
            'sampling_ratio': sampling_ratio,
        }
        self.paddle_graph.add_layer(
W
wjj19950828 已提交
558
            'custom_layer:ROIAlign',
W
wjj19950828 已提交
559 560 561 562 563
            inputs={
                'input': val_x.name,
                'rois': val_rois.name,
                'rois_num': val_rois_num
            },
S
SunAhong1993 已提交
564
            outputs=[node.name],
S
SunAhong1993 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
            **layer_attrs)

    @print_mapping_info
    def MaxRoiPool(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_rois = self.graph.get_input_node(node, idx=1, copy=True)

        spatial_scale = node.get_attr('spatial_scale')
        pooled_height, pooled_width = node.get_attr('pooled_shape')
        layer_attrs = {
            'pooled_height': pooled_height,
            'pooled_width': pooled_width,
            'spatial_scale': spatial_scale,
        }
        self.paddle_graph.add_layer(
W
wjj19950828 已提交
580
            'custom_layer:ROIPooling',
S
SunAhong1993 已提交
581 582 583
            inputs={'input': val_x.name,
                    'rois': val_rois.name},
            outputs=[node.name],
S
SunAhong1993 已提交
584 585 586 587 588 589
            **layer_attrs)

    @print_mapping_info
    def Pad(self, node, op_independent=True):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        pads = node.get_attr('pads')
S
SunAhong1993 已提交
590 591 592 593 594 595 596 597
        is_pads_attr = True
        if pads is None:
            val_pad = self.graph.get_input_node(node, idx=1, copy=True)
            pad_shape = val_pad.out_shapes[0]
            is_pads_attr = False
            pads = _const_weight_or_none(val_pad)
            if pads is not None:
                is_pads_attr = True
S
SunAhong1993 已提交
598
        mode = node.get_attr('mode', 'constant')
599 600
        if mode in ["edge"]:
            mode = "replicate"
S
SunAhong1993 已提交
601 602 603
        value = node.get_attr('value', 0.)
        data_shape = val_x.out_shapes[0]
        output_shape = node.out_shapes[0]
S
fix  
SunAhong1993 已提交
604
        assume_pad = False
S
SunAhong1993 已提交
605 606
        layer_attrs = {}
        layer_attrs['mode'] = string(mode)
S
fix  
SunAhong1993 已提交
607 608 609
        layer_attrs['value'] = value
        if not op_independent:
            output_name = node.name + '_paded'
S
SunAhong1993 已提交
610
        else:
S
fix  
SunAhong1993 已提交
611 612 613
            output_name = node.name
        nn_op_name = name_generator("pad", self.nn_name2id)
        layer_outputs = [nn_op_name, output_name]
S
SunAhong1993 已提交
614 615
        if is_pads_attr:
            paddings = []
S
SunAhong1993 已提交
616
            if len(pads) == 10 and sum(pads) == 0:
617
                pads = pads[0:6]
S
fix  
SunAhong1993 已提交
618
            if len(pads) in [2, 4, 6]:
S
SunAhong1993 已提交
619
                if data_shape:
620 621
                    assume_pad |= data_shape and 2 * (len(data_shape) - 2
                                                      ) == len(pads)  # NCHW
S
SunAhong1993 已提交
622
                if output_shape:
623 624
                    assume_pad |= output_shape and 2 * (len(output_shape) - 2
                                                        ) == len(pads)  # NCHW
S
fix  
SunAhong1993 已提交
625 626 627 628
                if assume_pad:
                    paddle_op = 'paddle.nn.Pad{}D'.format(len(output_shape) - 2)
                    paddings = np.array(pads).reshape(
                        (2, -1)).transpose().astype("int32")
S
for pad  
SunAhong1993 已提交
629
                    paddings = np.flip(paddings, axis=0).flatten().tolist()
S
fix  
SunAhong1993 已提交
630 631 632
                    layer_attrs['padding'] = paddings
                else:
                    if data_shape:
633 634
                        assume_pad |= data_shape and 2 * len(data_shape) == len(
                            pads)  # NCHW
S
fix  
SunAhong1993 已提交
635
                    if output_shape:
636 637
                        assume_pad |= output_shape and 2 * len(
                            output_shape) == len(pads)  # NCHW
S
fix  
SunAhong1993 已提交
638 639 640
                    if assume_pad:
                        paddle_op = 'paddle.nn.functional.pad'
                        paddings = np.array(pads).reshape(
641 642
                            (2,
                             -1)).transpose().astype("int32").flatten().tolist()
S
fix  
SunAhong1993 已提交
643 644
                        layer_attrs['pad'] = paddings
                    else:
645 646
                        raise Exception("The padding value {} is wrong!".format(
                            pads))
S
SunAhong1993 已提交
647
            elif len(pads) == 8:
S
fix  
SunAhong1993 已提交
648
                if data_shape:
649 650
                    assume_pad |= data_shape and 2 * len(data_shape) == len(
                        pads)  # NCHW
S
fix  
SunAhong1993 已提交
651
                if output_shape:
652 653
                    assume_pad |= output_shape and 2 * len(output_shape) == len(
                        pads)  # NCHW
S
fix  
SunAhong1993 已提交
654
                if assume_pad:
S
for pad  
SunAhong1993 已提交
655
                    paddle_op = 'paddle.nn.Pad2D'
W
wjj19950828 已提交
656
                    # x1_begin,x2_begin,x3_begin,x4_begin,x1_end,x2_end,x3_end,x4_end->x1_begin,x1_end,x2_begin,x2_end,x3_begin,x3_end,x4_begin,x4_end
S
fix  
SunAhong1993 已提交
657
                    paddings = np.array(pads).reshape(
S
for pad  
SunAhong1993 已提交
658
                        (2, -1)).transpose().astype("int32")
W
wjj19950828 已提交
659 660
                    if mode == 'constant':
                        paddings = paddings.flatten().tolist()
S
for pad  
SunAhong1993 已提交
661 662
                        layer_attrs['padding'] = paddings
                    else:
W
wjj19950828 已提交
663 664 665 666 667 668 669 670 671 672
                        paddings = np.flip(paddings, axis=0).flatten().tolist()
                        if sum(paddings[:4]) == 0:
                            paddings = paddings[4:]
                            layer_attrs['padding'] = paddings
                        else:
                            layer_attrs["pad"] = paddings
                            paddle_op = "custom_layer:PadAllDim4WithOneInput"
                else:
                    paddle_op = 'paddle.nn.functional.pad'
                    layer_attrs["pad"] = np.array(pads).tolist()
S
SunAhong1993 已提交
673
            else:
W
wjj19950828 已提交
674
                pad_data_temp = pads[0::2]
675
                pad_data_all = []
W
wjj19950828 已提交
676 677 678
                for i in range(len(pad_data_temp)):
                    pad_data_all.append(pads[i])
                    pad_data_all.append(pads[len(pad_data_temp) + i])
679 680 681 682 683 684 685 686 687

                layer_attrs["pad"] = pad_data_all
                self.paddle_graph.add_layer(
                    'paddle.nn.functional.pad',
                    inputs={'x': val_x.name},
                    outputs=layer_outputs[1:],
                    **layer_attrs)
                return

S
SunAhong1993 已提交
688
            self.paddle_graph.add_layer(
689 690 691 692
                paddle_op,
                inputs={'x': val_x.name},
                outputs=layer_outputs[1:]
                if paddle_op == 'paddle.nn.functional.pad' else layer_outputs,
S
SunAhong1993 已提交
693
                **layer_attrs)
S
fix  
SunAhong1993 已提交
694
            if not op_independent:
S
SunAhong1993 已提交
695
                return node.name + '_paded'
S
SunAhong1993 已提交
696
        else:
S
fix  
SunAhong1993 已提交
697 698
            pads_len = val_pad.out_shapes[0][0]
            if pads_len in [2, 4, 6]:
S
SunAhong1993 已提交
699
                if data_shape:
700 701
                    assume_pad |= data_shape and 2 * (len(data_shape) - 2
                                                      ) == pads_len  # NCHW
S
SunAhong1993 已提交
702
                if output_shape:
703 704
                    assume_pad |= output_shape and 2 * (len(output_shape) - 2
                                                        ) == pads_len  # NCHW
S
fix  
SunAhong1993 已提交
705 706 707 708 709 710 711 712
                if assume_pad:
                    if pads_len == 2:
                        data_format = "NCL"
                    elif pads_len == 4:
                        data_format = "NCHW"
                    else:
                        data_format = "NCDHW"
                    self.paddle_graph.add_layer(
713 714 715
                        "custom_layer:PadWithTwoInput",
                        inputs={'x': val_x.name,
                                'pad': val_pad.name},
S
fix  
SunAhong1993 已提交
716 717 718 719 720 721
                        outputs=layer_outputs,
                        value=value,
                        mode=string(mode),
                        data_format=string(data_format))
                else:
                    if data_shape:
722 723
                        assume_pad |= data_shape and 2 * len(
                            data_shape) == pads_len  # NCHW
S
fix  
SunAhong1993 已提交
724
                    if output_shape:
725 726
                        assume_pad |= output_shape and 2 * len(
                            output_shape) == pads_len  # NCHW
S
fix  
SunAhong1993 已提交
727 728 729
                    if assume_pad:
                        if pads_len == 4:
                            self.paddle_graph.add_layer(
730 731 732 733
                                "custom_layer:PadAllDim2",
                                inputs={'x': val_x.name,
                                        'pad': val_pad.name},
                                outputs=layer_outputs,
S
fix  
SunAhong1993 已提交
734 735 736 737 738 739
                                value=value,
                                mode=string(mode))
                        else:
                            raise Exception("The padding value is wrong!")
            elif pads_len == 8:
                if data_shape:
740 741
                    assume_pad |= data_shape and 2 * len(
                        data_shape) == pads_len  # NCHW
S
fix  
SunAhong1993 已提交
742
                if output_shape:
743 744
                    assume_pad |= output_shape and 2 * len(
                        output_shape) == pads_len  # NCHW
S
fix  
SunAhong1993 已提交
745 746
                if assume_pad:
                    self.paddle_graph.add_layer(
747 748 749 750
                        "custom_layer:PadAllDim4",
                        inputs={'x': val_x.name,
                                'pad': val_pad.name},
                        outputs=layer_outputs,
S
fix  
SunAhong1993 已提交
751 752 753
                        value=value,
                        mode=string(mode))
            else:
754
                raise Exception("The padding value is wrong!")
S
SunAhong1993 已提交
755 756
            if not op_independent:
                return node.name + '_paded'
S
SunAhong1993 已提交
757 758 759 760 761

    @print_mapping_info
    def Unsqueeze(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        axes = node.get_attr('axes')
762
        if axes is None:
W
WJJ1995 已提交
763 764 765
            axes_node = self.graph.get_input_node(node, idx=1, copy=True)
            axes = _const_weight_or_none(axes_node, necessary=True)
        # deal with scalar(0D) tensor
Y
fix  
yeliang2258 已提交
766
        if len(val_x.out_shapes[0]) == 0 and len(axes) == 1 and axes[0] == 0:
W
WJJ1995 已提交
767 768 769 770 771
            self.paddle_graph.add_layer(
                'paddle.reshape',
                inputs={"x": val_x.name},
                outputs=[node.name],
                shape=[1])
S
SunAhong1993 已提交
772
        else:
W
WJJ1995 已提交
773 774 775 776 777
            self.paddle_graph.add_layer(
                'paddle.unsqueeze',
                inputs={"x": val_x.name},
                axis=axes,
                outputs=[node.name])
S
SunAhong1993 已提交
778 779 780 781 782 783 784 785

    @print_mapping_info
    def Shrink(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        bias = node.get_attr('bias')
        lambd = node.get_attr('lambd')
        assert bias == 0.0, 'not support bias!=0'
        self.paddle_graph.add_layer(
786 787 788
            'paddle.nn.functional.hardshrink',
            inputs={"x": val_x.name},
            outputs=[node.name],
S
SunAhong1993 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
            threshold=lambd)

    @print_mapping_info
    def Constant(self, node):
        val_output = self.graph.get_node(node.layer.output[0], copy=True)

        value = node.get_attr('value')
        dtype = np.dtype(value.dtype)
        output_dtype = val_output.dtype
        if output_dtype:
            assert dtype == output_dtype, 'tensor dtype unmatches storage dtype'

        shape = node.get_attr('shape', None)

        if shape is None:
            shape = val_output.out_shapes[0]
        if shape is None:
            shape = list(value.shape)
            _logger.warning('in (Constant -> %s): '
                            'attribute "shape" of %s not inferred, '
                            'using value as 1-D tensor may lead to fails',
S
SunAhong1993 已提交
810
                            val_output.name, val_output.name)
S
SunAhong1993 已提交
811 812 813
        if len(value) == 1:
            value = value.tolist()
            value = value[0]
W
WJJ1995 已提交
814 815
            if value == float('inf') or value == float('-inf'):
                value = string(value)
S
SunAhong1993 已提交
816
            self.paddle_graph.add_layer(
817 818
                "paddle.full",
                inputs={},
S
SunAhong1993 已提交
819
                outputs=[node.name],
S
SunAhong1993 已提交
820 821 822 823 824
                dtype=string(dtype),
                shape=[1],
                fill_value=value)
        else:
            value = np.reshape(value, shape)
S
SunAhong1993 已提交
825
            self.weights[node.name] = value
S
SunAhong1993 已提交
826 827 828
            self.paddle_graph.add_layer(
                "self.create_parameter",
                inputs={},
S
SunAhong1993 已提交
829
                outputs=[node.name],
S
SunAhong1993 已提交
830
                shape=shape,
S
SunAhong1993 已提交
831
                attr=string(node.name),
S
SunAhong1993 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845
                dtype=string(dtype),
                default_initializer="paddle.nn.initializer.Constant(value=0.0)")

    @print_mapping_info
    def Resize(self, node):
        self._interpolate(node)

    @print_mapping_info
    def Upsample(self, node):
        self._interpolate(node)

    @print_mapping_info
    def InstanceNormalization(self, node):
        op_name = name_generator("instanse_norm", self.nn_name2id)
S
SunAhong1993 已提交
846
        output_name = node.name
S
SunAhong1993 已提交
847 848 849 850 851
        layer_outputs = [op_name, output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_scale = self.graph.get_input_node(node, idx=1, copy=True)
        val_b = self.graph.get_input_node(node, idx=2, copy=True)
        epsilon = node.get_attr('epsilon', 1e-5)
852 853
        self.weights[op_name + '.scale'] = self.weights[val_scale.name]
        self.weights[op_name + '.bias'] = self.weights[val_b.name]
S
SunAhong1993 已提交
854 855 856 857 858
        layer_attrs = {
            'num_features': node.out_shapes[0][1],
            'epsilon': epsilon,
        }
        dim = len(val_x.out_shapes[0])
S
SunAhong1993 已提交
859
        if dim == 3:
S
SunAhong1993 已提交
860 861 862 863 864 865
            paddle_op = "paddle.nn.InstanceNorm1D"
        elif dim == 4:
            paddle_op = "paddle.nn.InstanceNorm2D"
        elif dim == 5:
            paddle_op = "paddle.nn.InstanceNorm3D"
        else:
866 867 868
            raise Exception(
                "The paddle only support 2D, 3D, 4D or 5D input in InstanceNormalization."
            )
S
SunAhong1993 已提交
869
        self.paddle_graph.add_layer(
870 871 872
            paddle_op,
            inputs={"x": val_x.name},
            outputs=layer_outputs,
S
SunAhong1993 已提交
873 874 875 876 877 878 879
            **layer_attrs)

    @print_mapping_info
    def Expand(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_shape = self.graph.get_input_node(node, idx=1, copy=True)
        val_x_dtype = val_x.dtype
S
SunAhong1993 已提交
880
        name_ones = node.name + '_ones'
Y
yeliang2258 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893
        shape_values = _const_weight_or_none(val_shape)
        if shape_values is None:
            attr_ones = {
                'shape': val_shape.name,
                'dtype': string(val_x_dtype),
                'fill_value': 1
            }
        else:
            attr_ones = {
                'shape': shape_values.tolist(),
                'dtype': string(val_x_dtype),
                'fill_value': 1
            }
S
SunAhong1993 已提交
894
        self.paddle_graph.add_layer(
895 896
            'paddle.full', inputs={}, outputs=[name_ones], **attr_ones)
        inputs_dict = {'x': name_ones, 'y': val_x.name}
S
SunAhong1993 已提交
897
        self.paddle_graph.add_layer(
898
            'paddle.multiply', inputs=inputs_dict, outputs=[node.name])
S
SunAhong1993 已提交
899

Y
yeliang2258 已提交
900 901 902 903 904 905 906 907
    @print_mapping_info
    def GatherND(self, node):
        x = self.graph.get_input_node(node, idx=0, copy=True)
        index = self.graph.get_input_node(node, idx=1, copy=True)
        inputs = {'x': x.name, 'index': index.name}
        self.paddle_graph.add_layer(
            "paddle.gather_nd", inputs=inputs, outputs=[node.name])

S
SunAhong1993 已提交
908 909 910 911
    @print_mapping_info
    def Gather(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        indices = self.graph.get_input_node(node, idx=1, copy=True)
W
WJJ1995 已提交
912 913 914
        indices_values = _const_weight_or_none(indices, necessary=True)
        if isinstance(indices_values, np.ndarray):
            indices_values = indices_values.tolist()
S
SunAhong1993 已提交
915
        indices_shape = indices.out_shapes[0]
W
WJJ1995 已提交
916
        val_x_shape = val_x.out_shapes[0]
S
SunAhong1993 已提交
917
        axis = node.get_attr('axis', 0)
W
WJJ1995 已提交
918 919
        if len(indices_shape) == 1 or \
            (indices_values is not None and isinstance(indices_values, int)) or \
W
WJJ1995 已提交
920
                (indices_values is not None and len(indices_values) == 1):
S
SunAhong1993 已提交
921 922
            self.paddle_graph.add_layer(
                'paddle.gather',
W
WJJ1995 已提交
923
                inputs={'x': val_x.name,
S
SunAhong1993 已提交
924
                        'index': indices.name},
925
                outputs=[node.name],
W
WJJ1995 已提交
926 927
                axis=axis)
            # deal with indice is scalar(0D) Tensor
W
WJJ1995 已提交
928
            if isinstance(indices_values, int) and len(val_x_shape) != 1:
S
SunAhong1993 已提交
929 930
                self.paddle_graph.add_layer(
                    'paddle.squeeze',
S
SunAhong1993 已提交
931 932
                    inputs={'x': node.name},
                    outputs=[node.name],
S
SunAhong1993 已提交
933
                    axis=[axis])
W
WJJ1995 已提交
934 935 936
        else:
            # if val_x is DataNode, convert gather to embedding
            if axis == 0 and isinstance(val_x, ONNXGraphDataNode):
S
SunAhong1993 已提交
937
                indices_cast = indices.name + '_cast'
S
SunAhong1993 已提交
938 939
                self.paddle_graph.add_layer(
                    'paddle.cast',
S
SunAhong1993 已提交
940
                    inputs={"x": indices.name},
S
SunAhong1993 已提交
941
                    outputs=[indices_cast],
S
SunAhong1993 已提交
942 943
                    dtype=string('int64'))
                op_name = name_generator("embedding", self.nn_name2id)
S
SunAhong1993 已提交
944
                output_name = node.name
S
SunAhong1993 已提交
945
                layer_outputs = [op_name, output_name]
C
Channingss 已提交
946
                self.weights[op_name + '.weight'] = _const_weight_or_none(val_x)
S
SunAhong1993 已提交
947 948 949 950
                self.paddle_graph.add_layer(
                    'paddle.nn.Embedding',
                    inputs={"x": indices_cast},
                    outputs=layer_outputs,
W
WJJ1995 已提交
951 952
                    num_embeddings=val_x_shape[0],
                    embedding_dim=val_x_shape[1])
S
SunAhong1993 已提交
953 954 955
            else:
                self.paddle_graph.add_layer(
                    'paddle.reshape',
S
SunAhong1993 已提交
956
                    inputs={"x": indices.name},
W
WJJ1995 已提交
957 958 959
                    outputs=[indices.name + "_reshape"],
                    shape=[-1])
                gather_1d = node.name + '_1D'
S
SunAhong1993 已提交
960 961
                self.paddle_graph.add_layer(
                    'paddle.gather',
W
WJJ1995 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
                    inputs={
                        'x': val_x.name,
                        'index': indices.name + "_reshape"
                    },
                    outputs=[gather_1d],
                    axis=axis)
                # if shape is known
                if len(indices_shape) != 0 and len(val_x_shape) != 0:
                    self.paddle_graph.add_layer(
                        'paddle.reshape',
                        inputs={'x': gather_1d},
                        outputs=[node.name],
                        shape=val_x_shape[:axis] + indices_shape +
                        val_x_shape[axis + 1:])
                else:
                    all_shape_name = list()
                    self.paddle_graph.add_layer(
                        kernel="paddle.shape",
                        inputs={"input": val_x.name},
                        outputs=[val_x.name + "_shape"])
                    self.paddle_graph.add_layer(
                        kernel="paddle.shape",
                        inputs={"input": indices.name},
                        outputs=[indices.name + "_shape"])
                    self.paddle_graph.add_layer(
                        "paddle.slice",
                        inputs={"input": val_x.name + "_shape"},
                        outputs=[val_x.name + "_shape_slice_start"],
                        axes=[0],
                        starts=[0],
                        ends=[axis])
                    all_shape_name.append(val_x.name + "_shape_slice_start")
                    all_shape_name.append(indices.name + "_shape")
                    self.paddle_graph.add_layer(
                        "paddle.slice",
                        inputs={"input": val_x.name + "_shape"},
                        outputs=[val_x.name + "_shape_slice_end"],
                        axes=[0],
                        starts=[axis + 1],
                        ends=[2147483647])
                    all_shape_name.append(val_x.name + "_shape_slice_end")
                    self.paddle_graph.add_layer(
                        'paddle.concat',
                        inputs={"x": all_shape_name},
                        outputs=[node.name + "_all_shape"],
                        axis=0)
                    self.paddle_graph.add_layer(
                        'paddle.reshape',
                        inputs={'x': gather_1d},
                        outputs=[node.name],
                        shape=node.name + "_all_shape")
S
SunAhong1993 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021

    @print_mapping_info
    def ScatterND(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        indices = self.graph.get_input_node(node, idx=1, copy=True)
        updates = self.graph.get_input_node(node, idx=2, copy=True)
        if len(indices.out_shapes[0]) == 1:
            self.paddle_graph.add_layer(
                'paddle.scatter',
1022 1023 1024 1025 1026
                inputs={
                    'x': val_x.name,
                    'index': indices.name,
                    'updates': updates.name
                },
S
SunAhong1993 已提交
1027
                outputs=[node.name])
S
SunAhong1993 已提交
1028
        else:
S
SunAhong1993 已提交
1029
            input_inner_indices = node.name + '_input_inner_indices'
S
SunAhong1993 已提交
1030 1031 1032
            shape = val_x.out_shapes[0]
            self.paddle_graph.add_layer(
                'paddle.reshape',
S
SunAhong1993 已提交
1033 1034
                inputs={"x": indices.name},
                outputs=[indices.name],
S
SunAhong1993 已提交
1035 1036
                shape=indices.out_shapes[0])

S
SunAhong1993 已提交
1037
            zeros_like_val_x = val_x.name + '_zeros'
S
SunAhong1993 已提交
1038 1039
            self.paddle_graph.add_layer(
                'paddle.zeros_like',
S
SunAhong1993 已提交
1040
                inputs={"x": val_x.name},
S
SunAhong1993 已提交
1041 1042 1043 1044 1045
                outputs=[zeros_like_val_x])
            self.paddle_graph.add_layer(
                'paddle.scatter_nd_add',
                inputs={
                    'x': zeros_like_val_x,
S
SunAhong1993 已提交
1046 1047
                    'index': indices.name,
                    'updates': updates.name
S
SunAhong1993 已提交
1048 1049
                },
                outputs=[input_inner_indices])
S
SunAhong1993 已提交
1050 1051
            indices_mask = node.name + '_indices_mask'
            constant_minus_one = node.name + '_constant_minus_one'
S
SunAhong1993 已提交
1052 1053 1054
            # full_like support create tensor shape like input tensor
            self.paddle_graph.add_layer(
                'paddle.full_like',
S
SunAhong1993 已提交
1055
                inputs={"x": updates.name},
S
SunAhong1993 已提交
1056 1057 1058 1059 1060 1061 1062
                outputs=[constant_minus_one],
                dtype=string(updates.dtype),
                fill_value=-1)
            self.paddle_graph.add_layer(
                'paddle.scatter_nd_add',
                inputs={
                    'x': zeros_like_val_x,
S
SunAhong1993 已提交
1063
                    'index': indices.name,
S
SunAhong1993 已提交
1064 1065 1066
                    'updates': constant_minus_one
                },
                outputs=[indices_mask])
S
SunAhong1993 已提交
1067
            constant_one = node.name + '_constant_1'
S
SunAhong1993 已提交
1068 1069 1070
            # full_like support create tensor shape like input tensor
            self.paddle_graph.add_layer(
                'paddle.full_like',
S
SunAhong1993 已提交
1071
                inputs={"x": val_x.name},
S
SunAhong1993 已提交
1072 1073 1074
                outputs=[constant_one],
                dtype=string(val_x.dtype),
                fill_value=1)
S
SunAhong1993 已提交
1075
            input_out_indices_mask = node.name + '_input_out_indices_mask'
S
SunAhong1993 已提交
1076 1077 1078 1079 1080 1081
            self.paddle_graph.add_layer(
                "paddle.add",
                inputs={"x": indices_mask,
                        "y": constant_one},
                outputs=[input_out_indices_mask])

S
SunAhong1993 已提交
1082
            input_out_indices = node.name + '_input_out_indices'
S
SunAhong1993 已提交
1083 1084
            self.paddle_graph.add_layer(
                "paddle.multiply",
S
SunAhong1993 已提交
1085
                inputs={"x": val_x.name,
S
SunAhong1993 已提交
1086 1087 1088 1089 1090 1091 1092
                        "y": input_out_indices_mask},
                outputs=[input_out_indices])

            self.paddle_graph.add_layer(
                "paddle.add",
                inputs={"x": input_inner_indices,
                        "y": input_out_indices},
S
SunAhong1993 已提交
1093
                outputs=[node.name])
S
SunAhong1993 已提交
1094 1095 1096 1097 1098 1099 1100

    @print_mapping_info
    def Range(self, node):
        val_start = self.graph.get_input_node(node, idx=0, copy=True)
        val_limit = self.graph.get_input_node(node, idx=1, copy=True)
        val_delta = self.graph.get_input_node(node, idx=2, copy=True)
        dtype = val_start.dtype
1101 1102 1103 1104 1105
        inputs = {
            'start': val_start.name,
            'end': val_limit.name,
            'step': val_delta.name
        }
S
SunAhong1993 已提交
1106 1107 1108
        self.paddle_graph.add_layer(
            'paddle.arange',
            inputs=inputs,
S
SunAhong1993 已提交
1109
            outputs=[node.name],
S
SunAhong1993 已提交
1110 1111 1112 1113 1114 1115 1116
            dtype=string(dtype))

    @print_mapping_info
    def Slice(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        starts, ends, axes, steps = None, None, None, None
        layer_attrs = {}
W
WJJ1995 已提交
1117 1118 1119 1120 1121 1122
        if val_x.dtype == 'uint8':
            self.paddle_graph.add_layer(
                'paddle.cast',
                inputs={"x": val_x.name},
                outputs=[val_x.name],
                dtype=string('int32'))
S
SunAhong1993 已提交
1123 1124 1125 1126
        if len(node.inputs) > 1:
            starts = self.graph.get_input_node(node, idx=1, copy=True)
            ends = self.graph.get_input_node(node, idx=2, copy=True)
            starts_value = _const_weight_or_none(starts)
S
fix  
SunAhong1993 已提交
1127 1128
            if starts_value is not None:
                starts_value = starts_value.tolist()
S
SunAhong1993 已提交
1129
            ends_value = _const_weight_or_none(ends)
S
fix  
SunAhong1993 已提交
1130 1131 1132 1133 1134
            if ends_value is not None:
                ends_value = ends_value.tolist()
            if len(node.inputs) > 2:
                s_len = len(val_x.out_shapes[0])
                axes = list(range(s_len))
S
SunAhong1993 已提交
1135
            if len(node.inputs) > 3:
S
fix  
SunAhong1993 已提交
1136 1137
                axes_node = self.graph.get_input_node(node, idx=3, copy=True)
                axes = _const_weight_or_none(axes_node, necessary=True).tolist()
S
SunAhong1993 已提交
1138 1139
            if len(node.inputs) > 4:
                steps = self.graph.get_input_node(node, idx=4, copy=True)
S
fix  
SunAhong1993 已提交
1140
                steps = _const_weight_or_none(steps).tolist()
1141

S
SunAhong1993 已提交
1142 1143
            layer_attrs = {
                "axes": axes,
S
SunAhong1993 已提交
1144 1145
                "starts": starts.name,
                "ends": ends.name
S
SunAhong1993 已提交
1146
            }
S
SunAhong1993 已提交
1147
            if starts_value is not None and ends_value is not None and axes is not None:
S
SunAhong1993 已提交
1148 1149 1150
                starts_value = starts_value.copy()
                ends_value = ends_value.copy()
                for idx in range(len(ends_value)):
W
WJJ1995 已提交
1151 1152 1153
                    if len(val_x.out_shapes[0]) != 0 and starts_value[
                            idx] >= val_x.out_shapes[0][axes[
                                idx]] and val_x.out_shapes[0][axes[idx]] > 0:
S
SunAhong1993 已提交
1154 1155 1156 1157
                        starts_value[idx] = val_x.out_shapes[0][axes[idx]] - 1
                        ends_value[idx] = val_x.out_shapes[0][axes[idx]]
                    elif ends_value[idx] > 2**31 - 1:
                        ends_value[idx] = 2**31 - 1
W
WJJ1995 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
                    elif ends_value[idx] < -2**31:
                        ends_value[idx] = -2**31
                # If stride is -1 and starts and ends meet the conditions, just reverse it directly
                if steps == [-1] and len(starts_value) == 1 and len(
                        ends_value) == 1 and starts_value[
                            0] == -1 and ends_value[0] == -2**31:
                    self.paddle_graph.add_layer(
                        "paddle.flip",
                        inputs={"x": val_x.name},
                        outputs=[node.name],
                        axis=axes)
                    return
S
SunAhong1993 已提交
1170 1171 1172 1173 1174 1175 1176
                layer_attrs = {
                    "axes": axes,
                    "starts": starts_value,
                    "ends": ends_value
                }
            else:
                if starts.dtype != 'int32':
S
SunAhong1993 已提交
1177
                    starts_cast = starts.name + '_cast'
S
SunAhong1993 已提交
1178 1179
                    self.paddle_graph.add_layer(
                        'paddle.cast',
S
SunAhong1993 已提交
1180
                        inputs={"x": starts.name},
S
SunAhong1993 已提交
1181 1182 1183 1184
                        outputs=[starts_cast],
                        dtype=string('int32'))
                    layer_attrs['starts'] = starts_cast
                if ends.dtype != 'int32':
S
SunAhong1993 已提交
1185
                    ends_cast = ends.name + '_cast'
S
SunAhong1993 已提交
1186 1187
                else:
                    ends_cast = ends.name
S
SunAhong1993 已提交
1188 1189
                self.paddle_graph.add_layer(
                    'paddle.cast',
S
SunAhong1993 已提交
1190
                    inputs={"x": ends.name},
S
SunAhong1993 已提交
1191 1192 1193 1194 1195 1196 1197
                    outputs=[ends_cast],
                    dtype=string('int32'))
                layer_attrs['ends'] = ends_cast
        else:
            starts = node.get_attr('starts')
            ends = node.get_attr('ends')
            axes = node.get_attr('axes')
Y
yeliang2258 已提交
1198 1199 1200 1201
            output_shape = val_x.out_shapes[0]

            if axes is None:
                axes = [i for i in range(len(starts))]
S
SunAhong1993 已提交
1202 1203 1204
            for idx in range(len(ends)):
                if ends[idx] > 2**31 - 1:
                    ends[idx] = 2**31 - 1
W
WJJ1995 已提交
1205 1206
                elif ends[idx] < -2**31:
                    ends[idx] = 0
S
SunAhong1993 已提交
1207 1208 1209 1210 1211
            layer_attrs = {"axes": axes, "starts": starts, "ends": ends}

        if steps is not None:
            layer_attrs['strides'] = steps
            self.paddle_graph.add_layer(
1212 1213 1214
                'paddle.strided_slice',
                inputs={"x": val_x.name},
                outputs=[node.name],
S
SunAhong1993 已提交
1215 1216 1217
                **layer_attrs)
        else:
            self.paddle_graph.add_layer(
1218 1219 1220
                'paddle.slice',
                inputs={"input": val_x.name},
                outputs=[node.name],
S
SunAhong1993 已提交
1221
                **layer_attrs)
W
WJJ1995 已提交
1222 1223 1224 1225 1226 1227
        if val_x.dtype == 'uint8':
            self.paddle_graph.add_layer(
                'paddle.cast',
                inputs={"x": node.name},
                outputs=[node.name],
                dtype=string('uint8'))
S
SunAhong1993 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

    @print_mapping_info
    def ConstantOfShape(self, node):
        val_shape = self.graph.get_input_node(node, idx=0, copy=True)

        value = node.get_attr('value')
        dtype = value.dtype
        value = value.tolist()
        assert len(value) == 1, ('given value not Scalar, shape of value > 1, '
                                 'this is not supported')
        if len(value) == 1:
            value = value[0]
W
WJJ1995 已提交
1240 1241
            if value == float('inf') or value == float('-inf'):
                value = string(value)
1242
            layer_attrs = {'dtype': string(dtype), 'fill_value': value}
S
SunAhong1993 已提交
1243
            self.paddle_graph.add_layer(
1244 1245
                "paddle.full",
                inputs={'shape': val_shape.name},
S
SunAhong1993 已提交
1246
                outputs=[node.name],
S
SunAhong1993 已提交
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
                **layer_attrs)

    @print_mapping_info
    def Clip(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_y = self.graph.get_node(node.layer.output[0], copy=True)
        max_value, min_value = None, None
        if len(node.inputs) == 1:
            max_value = node.get_attr('max')
            min_value = node.get_attr('min')
            layer_attrs = {
                'max': max_value,
                'min': min_value,
            }
1261

S
SunAhong1993 已提交
1262
            self.paddle_graph.add_layer(
1263 1264 1265
                'paddle.clip',
                inputs={"x": val_x.name},
                outputs=[node.name],
S
SunAhong1993 已提交
1266 1267
                **layer_attrs)
        else:
Y
yeliang2258 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
            if len(node.inputs) == 2:
                val_ipt = self.graph.get_input_node(node, idx=1, copy=True)

                index = node.get_input_index(val_ipt.name)

                val_value = _const_weight_or_none(val_ipt)
                if val_value.shape == (1, ):
                    val_value = val_value[0]

                if index == 1:
                    layer_attrs = {'min': val_value}

                if index == 2:
                    layer_attrs = {'max': val_value}

1283 1284 1285 1286 1287 1288
                self.paddle_graph.add_layer(
                    'paddle.clip',
                    inputs={"x": val_x.name},
                    outputs=[node.name],
                    **layer_attrs)
            else:
Y
yeliang2258 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
                if len(node.inputs) == 3:
                    min_ipt = self.graph.get_input_node(node, idx=1, copy=True)
                    max_ipt = self.graph.get_input_node(node, idx=2, copy=True)
                    self.paddle_graph.add_layer(
                        'paddle.clip',
                        inputs={
                            "x": val_x.name,
                            "min": min_ipt.name,
                            "max": max_ipt.name
                        },
                        outputs=[node.name])
                else:
                    raise Exception("max_value or min_value can't be None")
S
SunAhong1993 已提交
1302

1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
    @print_mapping_info
    def ReduceSum(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        if len(node.inputs) == 1:
            keepdims = node.get_attr('keepdims')
            if keepdims is None:
                keepdims = True
            axes_value = node.get_attr('axes')
            layer_attrs = {'axis': axes_value, 'keepdim': keepdims}
            self.paddle_graph.add_layer(
                'paddle.sum',
                inputs={"x": val_x.name},
                outputs=[node.name],
                **layer_attrs)
        else:
            axes = self.graph.get_input_node(node, idx=1, copy=True)
            axes_value = _const_weight_or_none(axes)
            if axes_value.shape == (1, ):
                axes_value = axes_value[0]
            keepdims = node.get_attr('keepdims')
            if keepdims is None:
                layer_attrs = {'axis': axes_value}
            else:
                layer_attrs = {'axis': axes_value, 'keepdim': keepdims}

            self.paddle_graph.add_layer(
                'paddle.sum',
                inputs={"x": val_x.name},
                outputs=[node.name],
                **layer_attrs)

    @print_mapping_info
    def Max(self, node):
        if len(node.inputs) == 2:
            val_x = self.graph.get_input_node(node, idx=0, copy=True)
            val_y = self.graph.get_input_node(node, idx=1, copy=True)
            self.paddle_graph.add_layer(
                "paddle.maximum",
                inputs={"x": val_x.name,
                        "y": val_y.name},
                outputs=[node.name])
        else:
            val_x = self.graph.get_input_node(node, idx=0, copy=True)
            temp_name = "max_"
            for i in range(1, len(node.inputs)):
                val_y = self.graph.get_input_node(node, idx=i, copy=True)
                temp_name = temp_name + str(i)
                if i == len(node.inputs) - 1:
                    self.paddle_graph.add_layer(
                        "paddle.maximum",
                        inputs={"x": val_x.name,
                                "y": val_y.name},
                        outputs=[node.name])
                else:
                    self.paddle_graph.add_layer(
                        "paddle.maximum",
                        inputs={"x": val_x.name,
                                "y": val_y.name},
                        outputs=[temp_name])
                val_x.name = temp_name

    @print_mapping_info
    def Min(self, node):
        if len(node.inputs) == 2:
            val_x = self.graph.get_input_node(node, idx=0, copy=True)
            val_y = self.graph.get_input_node(node, idx=1, copy=True)
            self.paddle_graph.add_layer(
                "paddle.minimum",
                inputs={"x": val_x.name,
                        "y": val_y.name},
                outputs=[node.name])
        else:
            val_x = self.graph.get_input_node(node, idx=0, copy=True)
            temp_name = "min_"
            for i in range(1, len(node.inputs)):
                val_y = self.graph.get_input_node(node, idx=i, copy=True)
                temp_name = temp_name + str(i)
                if i == len(node.inputs) - 1:
                    self.paddle_graph.add_layer(
                        "paddle.minimum",
                        inputs={"x": val_x.name,
                                "y": val_y.name},
                        outputs=[node.name])
                else:
                    self.paddle_graph.add_layer(
                        "paddle.minimum",
                        inputs={"x": val_x.name,
                                "y": val_y.name},
                        outputs=[temp_name])
                val_x.name = temp_name

    @print_mapping_info
    def GreaterOrEqual(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_y = self.graph.get_input_node(node, idx=1, copy=True)
        self.paddle_graph.add_layer(
            "paddle.greater_equal",
            inputs={"x": val_x.name,
                    "y": val_y.name},
            outputs=[node.name])

    @print_mapping_info
    def And(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_y = self.graph.get_input_node(node, idx=1, copy=True)
        self.paddle_graph.add_layer(
            "paddle.logical_and",
            inputs={"x": val_x.name,
                    "y": val_y.name},
            outputs=[node.name])

S
SunAhong1993 已提交
1414 1415 1416 1417 1418
    @print_mapping_info
    def Split(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        split = node.get_attr('split')
        axis = node.get_attr('axis', 0)
Y
yeliang2258 已提交
1419 1420
        if split is None:
            split_num = len(node.layer.output)
Q
qqj1130247885 已提交
1421
            try:
W
WJJ1995 已提交
1422
                # split is an input of this node
Q
qqj1130247885 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
                split_node = self.graph.get_input_node(node, idx=1, copy=True)
                split_value = _const_weight_or_none(split_node)
                layer_attrs = {
                    'num_or_sections': split_value.tolist(),
                    'axis': axis,
                }
            except:
                layer_attrs = {
                    'num_or_sections': split_num,
                    'axis': axis,
                }
Y
yeliang2258 已提交
1434 1435 1436
            outputs_list = list()
            for i in range(len(node.layer.output)):
                if hasattr(node, 'index'):
S
SunAhong1993 已提交
1437
                    outputs_list.append("{}_p{}".format(node.layer_name, i))
Y
yeliang2258 已提交
1438
                else:
W
WJJ1995 已提交
1439
                    outputs_list.append("{}".format(node.layer.output[i]))
Y
yeliang2258 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
            if split_num > 1:
                self.paddle_graph.add_layer(
                    'paddle.split',
                    inputs={"x": val_x.name},
                    outputs=outputs_list,
                    **layer_attrs)
            else:
                self.paddle_graph.add_layer(
                    "paddle.cast",
                    inputs={"x": val_x.name},
                    outputs=outputs_list,
                    dtype=string(val_x.dtype))

S
SunAhong1993 已提交
1453
        else:
Y
yeliang2258 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
            layer_attrs = {
                'num_or_sections': split,
                'axis': axis,
            }
            outputs_list = list()
            if isinstance(split, list) or isinstance(split, tuple):
                if len(split) == 1:
                    outputs_list.append(node.name)
                else:
                    for i in range(len(split)):
                        outputs_list.append("{}_p{}".format(node.layer_name, i))
1465
            else:
Y
yeliang2258 已提交
1466
                outputs_list.append(node.name)
W
wjj19950828 已提交
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
            if len(split) > 1:
                self.paddle_graph.add_layer(
                    'paddle.split',
                    inputs={"x": val_x.name},
                    outputs=outputs_list,
                    **layer_attrs)
            else:
                self.paddle_graph.add_layer(
                    "paddle.cast",
                    inputs={"x": val_x.name},
                    outputs=outputs_list,
                    dtype=string(val_x.dtype))
S
SunAhong1993 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

    @print_mapping_info
    def Reshape(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_shape = self.graph.get_input_node(node, idx=1, copy=True)
        val_reshaped = self.graph.get_node(node.layer.output[0], copy=True)
        shape_value = _const_weight_or_none(val_shape)
        shape_dims = len(val_shape.out_shapes[0])

        if shape_value is not None:
            self.paddle_graph.add_layer(
                'paddle.reshape',
S
SunAhong1993 已提交
1491 1492
                inputs={'x': val_x.name},
                outputs=[node.name],
S
SunAhong1993 已提交
1493 1494 1495 1496 1497
                shape=shape_value.tolist())
        elif len(node.out_shapes[0]) > 0 and _is_static_shape(node.out_shapes[
                0]):
            self.paddle_graph.add_layer(
                'paddle.reshape',
S
SunAhong1993 已提交
1498 1499
                inputs={'x': val_x.name},
                outputs=[node.name],
S
SunAhong1993 已提交
1500 1501 1502 1503 1504 1505
                shape=node.out_shapes[0])
        else:
            # shape may be [], come form Gather by scalar indices
            if len(val_shape.out_shapes[0]) > 0:
                self.paddle_graph.add_layer(
                    'paddle.reshape',
S
SunAhong1993 已提交
1506 1507
                    inputs={'x': val_shape.name},
                    outputs=[val_shape.name],
S
SunAhong1993 已提交
1508
                    shape=val_shape.out_shapes[0])
S
fix  
SunAhong1993 已提交
1509 1510 1511 1512 1513 1514
            if val_shape.dtype != "int32":
                self.paddle_graph.add_layer(
                    'paddle.cast',
                    inputs={'x': val_shape.name},
                    outputs=[val_shape.name],
                    dtype=string("int32"))
S
SunAhong1993 已提交
1515 1516
            self.paddle_graph.add_layer(
                'paddle.reshape',
S
SunAhong1993 已提交
1517 1518
                inputs={'x': val_x.name,
                        'shape': val_shape.name},
S
SunAhong1993 已提交
1519
                outputs=[node.name])
S
SunAhong1993 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533

    @print_mapping_info
    def Cast(self, node):
        val_input = self.graph.get_input_node(node, idx=0, copy=True)
        val_output = self.graph.get_node(node.layer.output[0], copy=True)

        dtype = node.get_attr('to')
        if not isinstance(dtype, np.dtype):
            dtype = TENSOR_TYPE_TO_NP_TYPE[dtype]

        output_dtype = val_output.dtype
        if output_dtype:
            assert dtype == output_dtype, 'dtype of to unmatches output'
        self.paddle_graph.add_layer(
1534 1535 1536
            'paddle.cast',
            inputs={'x': val_input.name},
            outputs=[node.name],
S
SunAhong1993 已提交
1537 1538 1539 1540 1541
            dtype=string(dtype))

    @print_mapping_info
    def Not(self, node):
        val_input = self.graph.get_input_node(node, idx=0, copy=True)
1542 1543 1544 1545
        self.paddle_graph.add_layer(
            'paddle.logical_not',
            inputs={'x': val_input.name},
            outputs=[node.name])
S
SunAhong1993 已提交
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563

    @print_mapping_info
    def AveragePool(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)

        auto_pad = node.get_attr('auto_pad', 'NOTSET')
        kernel_shape = node.get_attr("kernel_shape")
        poolnd = len(kernel_shape)
        strides = node.get_attr("strides")
        pad_mode = node.get_attr("pads")
        ceil_mode = bool(node.get_attr('ceil_mode', 0))
        pads = node.get_attr('pads', [0] * (poolnd * 2))

        paddings, val_x = self._pad_if_asymmetric(node, pads, val_x)

        if auto_pad == "SAME_UPPER" or auto_pad == "SAME_LOWER":
            input_shape = val_x.out_shapes[0]
            pad_h = _get_same_padding(input_shape[2], kernel_shape[0],
W
wjj19950828 已提交
1564
                                      strides[0], auto_pad)
S
SunAhong1993 已提交
1565
            pad_w = _get_same_padding(input_shape[3], kernel_shape[1],
W
wjj19950828 已提交
1566
                                      strides[1], auto_pad)
S
SunAhong1993 已提交
1567 1568
            paddings = pad_h + pad_w

S
SunAhong1993 已提交
1569 1570 1571 1572 1573
        op_name = name_generator("pool", self.nn_name2id)
        output_name = node.name
        layer_outputs = [op_name, output_name]
        paddle_op = 'paddle.nn.AvgPool{}D'.format(poolnd)
        assert 1 <= poolnd <= 3, 'only Pool1D, Pool2D and Pool3D are supported'
S
SunAhong1993 已提交
1574
        layer_attrs = {
S
SunAhong1993 已提交
1575 1576 1577
            "kernel_size": kernel_shape,
            "stride": strides,
            "padding": paddings,
S
SunAhong1993 已提交
1578 1579 1580 1581
            "ceil_mode": ceil_mode,
            "exclusive": 'True',
        }
        self.paddle_graph.add_layer(
1582 1583 1584
            paddle_op,
            inputs={'x': val_x if isinstance(val_x, str) else val_x.name},
            outputs=layer_outputs,
S
SunAhong1993 已提交
1585 1586 1587 1588 1589 1590 1591 1592
            **layer_attrs)

    @print_mapping_info
    def Concat(self, node):
        inputs_list = []
        dtypes = set()
        for i in range(len(node.layer.input)):
            ipt = self.graph.get_input_node(node, idx=i, copy=True)
S
SunAhong1993 已提交
1593
            inputs_list.append(ipt.name)
S
SunAhong1993 已提交
1594 1595 1596 1597 1598
            dtypes.add(ipt.dtype)
        if len(dtypes) > 1:
            assert 'Unspported situation happened, please create issue on https://github.com/PaddlePaddle/X2Paddle/issues.'
        axis = node.get_attr('axis')
        self.paddle_graph.add_layer(
1599 1600 1601
            'paddle.concat',
            inputs={"x": inputs_list},
            outputs=[node.name],
S
SunAhong1993 已提交
1602 1603 1604 1605 1606
            axis=axis)

    @print_mapping_info
    def Flatten(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
1607
        output_shape = val_x.out_shapes[0]
S
SunAhong1993 已提交
1608 1609
        axis = node.get_attr('axis', 1)
        if axis == 0:
W
WJJ1995 已提交
1610 1611 1612 1613 1614
            self.paddle_graph.add_layer(
                'paddle.reshape',
                inputs={"x": val_x.name},
                outputs=[node.name],
                shape=[1, -1])
S
SunAhong1993 已提交
1615
        else:
W
WJJ1995 已提交
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
            if len(output_shape) != 0:
                shape_list = [1, 1]
                for s in output_shape[:axis]:
                    shape_list[0] *= s
                for s in output_shape[axis:]:
                    shape_list[1] *= s
                self.paddle_graph.add_layer(
                    'paddle.reshape',
                    inputs={"x": val_x.name},
                    outputs=[node.name],
                    shape=shape_list)
            else:
                # flatten + reshape
                self.paddle_graph.add_layer(
                    "paddle.flatten",
                    inputs={"input": val_x.name},
                    outputs=[val_x.name + "_flatten"],
                    start_axis=[0],
                    stop_axis=[axis])
                self.paddle_graph.add_layer(
                    'paddle.reshape',
                    inputs={'x': val_x.name + "_flatten"},
                    outputs=[node.name],
                    shape=[0, -1])
S
SunAhong1993 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650

    @print_mapping_info
    def Gemm(self, node):
        val_a = self.graph.get_input_node(node, idx=0, copy=True)
        val_b = self.graph.get_input_node(node, idx=1, copy=True)
        val_c = self.graph.get_input_node(node, idx=2, copy=True)

        alpha = node.get_attr('alpha', 1.)  # optional
        beta = node.get_attr('beta', 1.)  # optional
        trans_a = bool(node.get_attr('transA', 0))  # optional
        trans_b = bool(node.get_attr('transB', 0))  # optional
S
SunAhong1993 已提交
1651
        val_mm = node.name + '_mm'
1652
        matmul_inputs = {"x": val_a.name, "y": val_b.name}
S
SunAhong1993 已提交
1653 1654 1655 1656
        attr_matmul = {
            "transpose_x": trans_a,
            "transpose_y": trans_b,
        }
W
WJJ1995 已提交
1657 1658
        if abs(alpha - 1.0) < 1e-5:
            if abs(beta - 0.0) < 1e-5:
S
SunAhong1993 已提交
1659
                self.paddle_graph.add_layer(
W
WJJ1995 已提交
1660 1661 1662 1663
                    'paddle.matmul',
                    inputs=matmul_inputs,
                    outputs=[node.name],
                    **attr_matmul)
S
SunAhong1993 已提交
1664
            else:
W
WJJ1995 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
                self.paddle_graph.add_layer(
                    'paddle.matmul',
                    inputs=matmul_inputs,
                    outputs=[val_mm],
                    **attr_matmul)
                if abs(beta - 1.0) < 1e-5:
                    add_inputs = {"x": val_mm, "y": val_c.name}
                    self.paddle_graph.add_layer(
                        "paddle.add", inputs=add_inputs, outputs=[node.name])
                else:
                    var_beta = node.name + '_beta'
                    self.paddle_graph.add_layer(
                        "paddle.scale",
                        inputs={"x": val_c.name},
                        outputs=[var_beta],
                        scale=beta)
                    add_inputs = {"x": val_mm, "y": var_beta}
                    self.paddle_graph.add_layer(
                        "paddle.add", inputs=add_inputs, outputs=[node.name])
        else:
            if abs(beta - 0.0) < 1e-5:
                self.paddle_graph.add_layer(
                    'paddle.matmul',
                    inputs=matmul_inputs,
                    outputs=[val_mm],
                    **attr_matmul)
S
SunAhong1993 已提交
1691 1692
                self.paddle_graph.add_layer(
                    "paddle.scale",
W
WJJ1995 已提交
1693 1694 1695 1696
                    inputs={"x": val_mm},
                    outputs=[node.name],
                    scale=alpha)
            else:
S
SunAhong1993 已提交
1697
                self.paddle_graph.add_layer(
W
WJJ1995 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
                    'paddle.matmul',
                    inputs=[matmul_inputs],
                    outputs=[val_mm],
                    **attr_matmul)
                self.paddle_graph.add_layer(
                    "paddle.scale",
                    inputs={"x": val_mm},
                    outputs=[val_mm],
                    scale=alpha)
                if abs(beta - 1.0) < 1e-5:
                    add_inputs = {"x": val_mm, "y": val_c.name}
                    self.paddle_graph.add_layer(
                        "paddle.add", inputs=add_inputs, outputs=[node.name])
                else:
                    var_beta = node.name + '_beta'
                    self.paddle_graph.add_layer(
                        "paddle.scale",
                        inputs={"x": val_c.name},
                        outputs=[var_beta],
                        scale=beta)
                    add_inputs = {"x": val_mm, "y": var_beta}
                    self.paddle_graph.add_layer(
                        "paddle.add", inputs=add_inputs, outputs=[node.name])
S
SunAhong1993 已提交
1721 1722 1723 1724 1725

    @print_mapping_info
    def Sum(self, node):
        val_inps = node.layer.input
        inputs_dict = {
S
SunAhong1993 已提交
1726 1727 1728 1729
            "x": self.graph.get_input_node(
                node, idx=0, copy=True).name,
            "y": self.graph.get_input_node(
                node, idx=1, copy=True).name,
S
SunAhong1993 已提交
1730
        }
1731 1732
        self.paddle_graph.add_layer(
            "paddle.add", inputs=inputs_dict, outputs=[node.name])
S
SunAhong1993 已提交
1733 1734 1735 1736

        for idx, ipt in enumerate(val_inps[2:]):
            y = self.graph.get_input_node(node, idx=idx, copy=True)
            inputs_dict = {
S
SunAhong1993 已提交
1737 1738
                "x": node.name,
                "y": y.name,
S
SunAhong1993 已提交
1739 1740
            }
            self.paddle_graph.add_layer(
1741
                "paddle.add", inputs=inputs_dict, outputs=[node.name])
S
SunAhong1993 已提交
1742 1743 1744 1745 1746 1747 1748

    @print_mapping_info
    def MatMul(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_y = self.graph.get_input_node(node, idx=1, copy=True)
        x_shape = val_x.out_shapes[0]
        y_shape = val_y.out_shapes[0]
1749
        inputs_dict = {"x": val_x.name, "y": val_y.name}
W
WJJ1995 已提交
1750 1751
        if len(y_shape) != 0 and y_shape[0] == 1 and len(
                x_shape) != 0 and x_shape[-1] != 1 and x_shape[0] != 1:
S
SunAhong1993 已提交
1752
            y_squeeze = val_y.name + '_squeeze'
S
SunAhong1993 已提交
1753 1754
            self.paddle_graph.add_layer(
                "paddle.squeeze",
S
SunAhong1993 已提交
1755
                inputs={"x": val_y.name},
S
SunAhong1993 已提交
1756 1757 1758 1759
                outputs=[y_squeeze],
                axis=[0])
            inputs_dict['y'] = y_squeeze
            self.paddle_graph.add_layer(
1760
                "paddle.matmul", inputs=inputs_dict, outputs=[node.name])
S
SunAhong1993 已提交
1761 1762
        else:
            self.paddle_graph.add_layer(
1763
                "paddle.matmul", inputs=inputs_dict, outputs=[node.name])
S
SunAhong1993 已提交
1764 1765 1766 1767

    @print_mapping_info
    def BatchNormalization(self, node):
        op_name = name_generator("batchnorm", self.nn_name2id)
S
SunAhong1993 已提交
1768
        output_name = node.name
S
SunAhong1993 已提交
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
        layer_outputs = [op_name, output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_scale = self.graph.get_input_node(node, idx=1, copy=True)
        val_b = self.graph.get_input_node(node, idx=2, copy=True)
        val_mean = self.graph.get_input_node(node, idx=3, copy=True)
        val_var = self.graph.get_input_node(node, idx=4, copy=True)

        momentum = node.get_attr('momentum', .9)
        epsilon = node.get_attr('epsilon', 1e-5)
        c = val_x.out_shapes[0][1]

1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
        # solved the same data is used as an argument to multiple OPs.
        _rename_or_remove_weight(
            self.weights,
            val_scale.name,
            op_name + '.weight',
            rename_mapper=self.rename_mapper)
        _rename_or_remove_weight(
            self.weights,
            val_b.name,
            op_name + '.bias',
            rename_mapper=self.rename_mapper)
        _rename_or_remove_weight(
            self.weights,
            val_var.name,
            op_name + '._variance',
            rename_mapper=self.rename_mapper)
        _rename_or_remove_weight(
            self.weights,
            val_mean.name,
            op_name + '._mean',
            rename_mapper=self.rename_mapper)
C
Channingss 已提交
1801

S
SunAhong1993 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
        # Attribute: spatial is used in BatchNormalization-1,6,7
        spatial = bool(node.get_attr('spatial'))
        layer_attrs = {
            "num_channels": c,
            "momentum": momentum,
            "epsilon": epsilon,
            "is_test": True,
            "use_global_stats": False,
        }
        self.paddle_graph.add_layer(
1812 1813 1814
            "paddle.nn.BatchNorm",
            inputs={"x": val_x.name},
            outputs=layer_outputs,
S
SunAhong1993 已提交
1815 1816 1817 1818 1819
            **layer_attrs)

    @print_mapping_info
    def Transpose(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
S
fix  
SunAhong1993 已提交
1820 1821 1822 1823
        s_len = len(val_x.out_shapes[0])
        perm_default = list(range(s_len))
        perm_default.reverse()
        perm = node.get_attr('perm', perm_default)
S
SunAhong1993 已提交
1824
        self.paddle_graph.add_layer(
1825
            "paddle.transpose",
S
SunAhong1993 已提交
1826
            inputs={"x": val_x.name},
1827
            outputs=[node.name],
S
SunAhong1993 已提交
1828 1829 1830 1831 1832
            perm=perm)

    @print_mapping_info
    def PRelu(self, node):
        op_name = name_generator("prelu", self.nn_name2id)
S
SunAhong1993 已提交
1833
        output_name = node.name
S
SunAhong1993 已提交
1834 1835 1836 1837 1838 1839
        layer_outputs = [op_name, output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_slope = self.graph.get_input_node(node, idx=1, copy=True)

        mode = 'channel'
        shape_slope = val_slope.out_shapes[0]
1840
        if shape_slope == [1] * len(shape_slope):
S
SunAhong1993 已提交
1841 1842
            mode = 'all'

S
SunAhong1993 已提交
1843 1844 1845
        if mode == "element":
            self.paddle_graph.add_layer(
                "paddle.zeros",
1846 1847
                inputs={},
                outputs=[output_name + "__zeros"],
S
SunAhong1993 已提交
1848 1849 1850 1851
                shape=shape_slope,
                dtype=string(node.dtype))
            self.paddle_graph.add_layer(
                "paddle.maximum",
1852 1853
                inputs={"x": val_x.name,
                        "y": output_name + "__zeros"},
S
SunAhong1993 已提交
1854 1855 1856
                outputs=[output_name + "__max"])
            self.paddle_graph.add_layer(
                "paddle.minimum",
1857 1858
                inputs={"x": val_x.name,
                        "y": output_name + "__zeros"},
1859
                outputs=[output_name + "__min"])
S
SunAhong1993 已提交
1860 1861
            self.paddle_graph.add_layer(
                "paddle.multiply",
1862 1863
                inputs={"x": val_slope.name,
                        "y": output_name + "__min"},
S
SunAhong1993 已提交
1864 1865 1866
                outputs=[output_name + "__mul"])
            self.paddle_graph.add_layer(
                "paddle.add",
1867 1868 1869 1870
                inputs={
                    "x": output_name + "__max",
                    "y": output_name + "__mul"
                },
S
SunAhong1993 已提交
1871
                outputs=[output_name])
S
SunAhong1993 已提交
1872
        else:
S
fix  
SunAhong1993 已提交
1873
            if mode == 'channel':
S
SunAhong1993 已提交
1874
                slope_data = _const_weight_or_none(val_slope)
S
SunAhong1993 已提交
1875 1876
                if slope_data is None:
                    self.paddle_graph.add_layer(
1877 1878
                        "paddle.reshape",
                        inputs={"x": val_slope.name},
S
SunAhong1993 已提交
1879 1880 1881
                        outputs=[val_slope.name],
                        shape=[shape_slope[0]])
                    self.paddle_graph.add_layer(
1882
                        "paddle.nn.functional.prelu",
S
SunAhong1993 已提交
1883
                        inputs={"x": val_x.name,
1884
                                "weight": val_slope.name},
S
SunAhong1993 已提交
1885 1886
                        outputs=[node.name])
                    return
C
Channingss 已提交
1887
                _rename_or_remove_weight(self.weights, val_slope.name)
S
fix  
SunAhong1993 已提交
1888
                if len(shape_slope) > 1:
1889 1890
                    self.weights[op_name + '._weight'] = np.reshape(
                        slope_data, shape_slope[0])
S
SunAhong1993 已提交
1891 1892 1893
                num_parameters = val_x.out_shapes[0][1]
            else:
                num_parameters = 1
Y
yeliang2258 已提交
1894
                slope_data = self.weights[val_slope.name]
C
Channingss 已提交
1895
                _rename_or_remove_weight(self.weights, val_slope.name)
Y
yeliang2258 已提交
1896
                self.weights[op_name + '._weight'] = np.reshape(slope_data, [1])
S
SunAhong1993 已提交
1897
            self.paddle_graph.add_layer(
1898 1899 1900
                "paddle.nn.PReLU",
                inputs={"x": val_x.name},
                outputs=layer_outputs,
1901
                num_parameters=num_parameters)
S
SunAhong1993 已提交
1902 1903 1904 1905 1906

    @print_mapping_info
    def Squeeze(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        axes = node.get_attr('axes')
W
WJJ1995 已提交
1907 1908 1909 1910 1911
        if axes is None:
            axes_node = self.graph.get_input_node(node, idx=1, copy=True)
            axes = _const_weight_or_none(axes_node, necessary=True)
        # deal with scalar(0D) tensor
        if len(val_x.out_shapes[0]) <= 1 and len(axes) == 1 and axes[0] == 0:
S
SunAhong1993 已提交
1912 1913
            self.paddle_graph.add_layer(
                "paddle.cast",
S
SunAhong1993 已提交
1914 1915
                inputs={"x": val_x.name},
                outputs=[node.name],
S
SunAhong1993 已提交
1916 1917 1918
                dtype=string(val_x.dtype))
        else:
            self.paddle_graph.add_layer(
1919 1920 1921
                "paddle.squeeze",
                inputs={"x": val_x.name},
                outputs=[node.name],
S
SunAhong1993 已提交
1922 1923 1924 1925 1926 1927 1928 1929
                axis=axes)

    @print_mapping_info
    def Equal(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_y = self.graph.get_input_node(node, idx=1, copy=True)
        self.paddle_graph.add_layer(
            "paddle.equal",
S
SunAhong1993 已提交
1930 1931 1932
            inputs={'x': val_x.name,
                    'y': val_y.name},
            outputs=[node.name])
S
SunAhong1993 已提交
1933 1934 1935 1936 1937 1938 1939

    @print_mapping_info
    def Greater(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_y = self.graph.get_input_node(node, idx=1, copy=True)
        self.paddle_graph.add_layer(
            "paddle.greater_than",
S
SunAhong1993 已提交
1940 1941
            inputs={'x': val_x.name,
                    'y': val_y.name},
1942
            outputs=[node.name])
S
SunAhong1993 已提交
1943 1944 1945 1946 1947 1948 1949 1950

    @print_mapping_info
    def Where(self, node):
        condition = self.graph.get_input_node(node, idx=0, copy=True)
        val_x = self.graph.get_input_node(node, idx=1, copy=True)
        val_y = self.graph.get_input_node(node, idx=2, copy=True)

        self.paddle_graph.add_layer(
W
WJJ1995 已提交
1951 1952 1953 1954 1955 1956
            "paddle.where",
            inputs={
                'condition': condition.name,
                'x': val_x.name,
                'y': val_y.name
            },
S
SunAhong1993 已提交
1957
            outputs=[node.name])
S
SunAhong1993 已提交
1958 1959 1960 1961

    @print_mapping_info
    def NonZero(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
W
wjj19950828 已提交
1962 1963 1964 1965
        self.paddle_graph.add_layer(
            "paddle.nonzero",
            inputs={"x": val_x.name},
            outputs=[val_x.name],
W
WJJ1995 已提交
1966
            as_tuple=False)
W
wjj19950828 已提交
1967
        self.paddle_graph.add_layer(
W
WJJ1995 已提交
1968 1969 1970 1971
            'paddle.transpose',
            inputs={"x": val_x.name},
            outputs=[node.name],
            perm=[1, 0])
S
SunAhong1993 已提交
1972 1973 1974 1975 1976

    @print_mapping_info
    def Identity(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        self.paddle_graph.add_layer(
1977
            "paddle.assign", inputs={"x": val_x.name}, outputs=[node.name])
S
SunAhong1993 已提交
1978 1979 1980 1981 1982 1983 1984 1985

    @print_mapping_info
    def Tile(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_repeats = self.graph.get_input_node(node, idx=1, copy=True)
        repeats = _const_weight_or_none(val_repeats)

        if repeats is None:
S
SunAhong1993 已提交
1986
            repeats = val_repeats.name
S
SunAhong1993 已提交
1987 1988 1989 1990
            if val_repeats.dtype != 'int32':
                self.paddle_graph.add_layer(
                    "paddle.cast",
                    inputs={"x": repeats},
1991
                    outputs=["{}_tmp".format(repeats)],
S
SunAhong1993 已提交
1992
                    dtype=string("int32"))
1993
                repeats = "{}_tmp".format(repeats)
S
SunAhong1993 已提交
1994 1995 1996 1997

        elif isinstance(repeats, int):
            repeats = [repeats]

1998 1999 2000
        elif type(repeats) is np.ndarray:
            repeats = repeats.tolist()

S
SunAhong1993 已提交
2001 2002
        attr = {
            'expand_times': repeats,
S
SunAhong1993 已提交
2003
            "name": string(node.name),
S
SunAhong1993 已提交
2004 2005
        }
        self.paddle_graph.add_layer(
2006 2007 2008 2009
            "paddle.tile",
            inputs={"x": val_x.name},
            outputs=[node.name],
            repeat_times=repeats)
S
SunAhong1993 已提交
2010 2011 2012 2013

    @print_mapping_info
    def MaxPool(self, node):
        op_name = name_generator("pool", self.nn_name2id)
S
SunAhong1993 已提交
2014
        output_name = node.name
S
SunAhong1993 已提交
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
        layer_outputs = [op_name, output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        auto_pad = node.get_attr('auto_pad', 'NOTSET')
        assert node.get_attr(
            "dilations") is None, 'only dilations = 0 is supported'  # optional

        kernel_shape = node.get_attr("kernel_shape")
        poolnd = len(kernel_shape)
        strides = node.get_attr("strides")
        pad_mode = node.get_attr("pads")
        ceil_mode = bool(node.get_attr('ceil_mode', 0))  # optional
        pads = node.get_attr('pads', [0] * (poolnd * 2))  # optional
        paddle_op = 'paddle.nn.MaxPool{}D'.format(poolnd)
        assert 1 <= poolnd <= 3, 'only Pool1D, Pool2D and Pool3D are supported'

        paddings, val_x = self._pad_if_asymmetric(node, pads, val_x)

        if auto_pad == "SAME_UPPER" or auto_pad == "SAME_LOWER":
            input_shape = val_x.out_shapes[0]
            pad_h = _get_same_padding(input_shape[2], kernel_shape[0],
W
wjj19950828 已提交
2035
                                      strides[0], auto_pad)
S
SunAhong1993 已提交
2036
            pad_w = _get_same_padding(input_shape[3], kernel_shape[1],
W
wjj19950828 已提交
2037
                                      strides[1], auto_pad)
S
SunAhong1993 已提交
2038
            paddings = pad_h + pad_w
2039

S
SunAhong1993 已提交
2040 2041 2042 2043 2044 2045 2046
        layer_attrs = {
            "kernel_size": kernel_shape,
            "stride": strides,
            "padding": paddings,
            "ceil_mode": ceil_mode,
        }
        self.paddle_graph.add_layer(
2047 2048 2049
            paddle_op,
            inputs={'x': val_x if isinstance(val_x, str) else val_x.name},
            outputs=layer_outputs,
S
SunAhong1993 已提交
2050 2051 2052 2053 2054
            **layer_attrs)

    @print_mapping_info
    def GlobalMaxPool(self, node):
        op_name = name_generator("pool", self.nn_name2id)
S
SunAhong1993 已提交
2055
        output_name = node.name
S
SunAhong1993 已提交
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
        layer_outputs = [op_name, output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        input_shape = val_x.out_shapes[0]
        if len(input_shape) == 4:
            poolnd = 2
        elif len(input_shape) == 5:
            poolnd = 3
        elif len(input_shape) == 3:
            poolnd = 1
        paddle_op = 'paddle.nn.AdaptiveMaxPool{}D'.format(poolnd)
        assert 1 <= poolnd <= 3, 'only Pool1D, Pool2D and Pool3D are supported'
        output_shape = node.out_shapes[0]
        self.paddle_graph.add_layer(
2069 2070 2071
            paddle_op,
            inputs={'x': val_x.name},
            outputs=layer_outputs,
S
SunAhong1993 已提交
2072 2073
            output_size=output_shape[2:])

Y
yeliang2258 已提交
2074 2075
    @print_mapping_info
    def Neg(self, node):
Y
fix  
yeliang2258 已提交
2076
        import paddle
Y
yeliang2258 已提交
2077
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
Y
fix neg  
yeliang2258 已提交
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
        v0, v1, v2 = paddle.__version__.split('.')
        if int(v0) >= 2 and int(v1) >= 2:
            self.paddle_graph.add_layer(
                "paddle.neg", inputs={'x': val_x.name}, outputs=[node.name])
        else:
            val_y = node.name + "_y"
            dtype = np.dtype(val_x.dtype)
            self.paddle_graph.add_layer(
                "paddle.full",
                inputs={},
                outputs=[val_y],
                dtype=string(dtype),
                shape=[1],
                fill_value=-1)
            self.paddle_graph.add_layer(
                "paddle.multiply",
                inputs={'x': val_x.name,
                        'y': val_y},
                outputs=[node.name])
Y
yeliang2258 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123

    @print_mapping_info
    def SpaceToDepth(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        blocksize = node.get_attr('blocksize')
        val_x_shape = val_x.out_shapes[0]
        b, c, h, w = val_x_shape
        self.paddle_graph.add_layer(
            'paddle.reshape',
            inputs={"x": val_x.name},
            outputs=[node.name],
            shape=[b, c, h // blocksize, blocksize, w // blocksize, blocksize])
        self.paddle_graph.add_layer(
            'paddle.transpose',
            inputs={"x": node.name},
            outputs=[node.name],
            perm=[0, 3, 5, 1, 2, 4])
        self.paddle_graph.add_layer(
            'paddle.reshape',
            inputs={"x": node.name},
            outputs=[node.name],
            shape=[b, c * (blocksize**2), h // blocksize, w // blocksize])

    @print_mapping_info
    def GatherElements(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        indices = self.graph.get_input_node(node, idx=1, copy=True)
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
        axis = node.get_attr('axis')
        val_x_shape = val_x.out_shapes[0]
        indices_shape = indices.out_shapes[0]
        axis = axis if axis >= 0 else axis + len(val_x_shape)
        if axis == 0:
            axis_perm = [i for i in range(len(val_x_shape))]
            data_swaped = val_x.name
            index_swaped = indices.name
        else:
            axis_perm = [i for i in range(len(val_x_shape))]
            axis_perm[axis] = 0
            axis_perm[0] = axis
            data_swaped = val_x.name + "_transpose"
            self.paddle_graph.add_layer(
                "paddle.transpose",
                inputs={'x': val_x.name},
                perm=axis_perm,
                outputs=[data_swaped])
            index_swaped = indices.name + "_transpose"
            self.paddle_graph.add_layer(
                "paddle.transpose",
                inputs={'x': indices.name},
                perm=axis_perm,
                outputs=[index_swaped])
            temp = indices_shape[0]
            indices_shape[0] = indices_shape[axis]
            indices_shape[axis] = temp

        idx_tensors_per_axis_pre = [
            indices_shape[i] for i in range(len(indices_shape))
        ]
        name_list = list()
        for i in range(len(idx_tensors_per_axis_pre)):
            tensor_name = val_x.name + "_meshgrid_" + str(i)
            self.paddle_graph.add_layer(
                kernel="paddle.linspace",
                inputs={},
                outputs=[tensor_name],
                start=0,
                stop=idx_tensors_per_axis_pre[i] - 1,
                num=idx_tensors_per_axis_pre[i])
            name_list.append(tensor_name)

Y
yeliang2258 已提交
2167
        self.paddle_graph.add_layer(
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
            "paddle.meshgrid", inputs=name_list, outputs=name_list)

        self.paddle_graph.add_layer(
            "paddle.cast",
            inputs={"x": index_swaped},
            outputs=[index_swaped],
            dtype=string("float32"))
        import copy
        copy_name_list = copy.copy(name_list)
        copy_name_list[0] = index_swaped
        new_name_list = list()
        for i in range(len(copy_name_list)):
            unsqueeze_name = copy_name_list[i] + "_unsqueeze"
            self.paddle_graph.add_layer(
                "paddle.unsqueeze",
                inputs={"x": copy_name_list[i]},
                axis=-1,
                outputs=[unsqueeze_name])
            new_name_list.append(unsqueeze_name)
        concat_name = val_x.name + "_concated_layer"
        self.paddle_graph.add_layer(
            "paddle.concat",
            inputs={'x': new_name_list},
            axis=-1,
            outputs=[concat_name])
        self.paddle_graph.add_layer(
            "paddle.cast",
            inputs={"x": concat_name},
            outputs=[concat_name],
            dtype=string("int32"))
        gather_nd_name = "gather_nd_layer"
        self.paddle_graph.add_layer(
            "paddle.gather_nd",
            inputs={'x': data_swaped,
                    "index": concat_name},
            outputs=[gather_nd_name])

        self.paddle_graph.add_layer(
            "paddle.transpose",
            inputs={'x': gather_nd_name},
            perm=axis_perm,
Y
yeliang2258 已提交
2209 2210
            outputs=[node.name])

S
SunAhong1993 已提交
2211 2212 2213
    @print_mapping_info
    def GlobalAveragePool(self, node):
        op_name = name_generator("pool", self.nn_name2id)
S
SunAhong1993 已提交
2214
        output_name = node.name
S
SunAhong1993 已提交
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
        layer_outputs = [op_name, output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        input_shape = val_x.out_shapes[0]
        if len(input_shape) == 4:
            poolnd = 2
        elif len(input_shape) == 5:
            poolnd = 3
        elif len(input_shape) == 3:
            poolnd = 1
        paddle_op = 'paddle.nn.AdaptiveAvgPool{}D'.format(poolnd)
        assert 1 <= poolnd <= 3, 'only Pool1D, Pool2D and Pool3D are supported'
        output_shape = node.out_shapes[0]
        self.paddle_graph.add_layer(
2228 2229 2230
            paddle_op,
            inputs={'x': val_x.name},
            outputs=layer_outputs,
S
SunAhong1993 已提交
2231 2232 2233 2234
            output_size=output_shape[2:])

    @print_mapping_info
    def Conv(self, node):
S
SunAhong1993 已提交
2235
        output_name = node.name
S
SunAhong1993 已提交
2236 2237
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_w = self.graph.get_input_node(node, idx=1, copy=True)
2238 2239 2240 2241 2242 2243 2244 2245

        if val_w.name in self.weights.keys():
            op_name = name_generator("conv", self.nn_name2id)
        else:
            op_name = output_name

        layer_outputs = [op_name, output_name]

S
SunAhong1993 已提交
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
        has_bias = len(node.layer.input) == 3
        if has_bias:
            val_b = self.graph.get_input_node(node, idx=2, copy=True)
        auto_pad = node.get_attr('auto_pad', 'NOTSET')

        kernel_shape = node.get_attr('kernel_shape')
        convnd = len(kernel_shape)
        num_out_channels = val_w.out_shapes[0][0]
        num_in_channels = val_w.out_shapes[0][1]
        paddle_op = 'paddle.nn.Conv{}D'.format(convnd)

        num_groups = node.get_attr('group', 1)
        strides = node.get_attr('strides', [1] * convnd)
        dilations = node.get_attr('dilations', [1] * convnd)
        pads = node.get_attr('pads', [0] * (convnd * 2))

        input_shape = val_x.out_shapes[0]
W
wjj19950828 已提交
2263 2264
        paddings = np.array(pads).reshape((2, -1)).transpose().astype("int32")
        paddings = paddings.flatten().tolist()
S
SunAhong1993 已提交
2265

W
wjj19950828 已提交
2266 2267 2268 2269 2270 2271
        if auto_pad in ["SAME_UPPER", "SAME_LOWER"]:
            # Warning: SAME_UPPER and SAME_LOWER does not yet support dynamic shapes
            if input_shape[2] == -1 or input_shape[3] == -1:
                _logger.warning(
                    'SAME_UPPER and SAME_LOWER does not yet support dynamic shapes, the conversion result may have a diff!!!'
                )
S
SunAhong1993 已提交
2272
            pad_h = _get_same_padding(input_shape[2], kernel_shape[0],
W
wjj19950828 已提交
2273
                                      strides[0], auto_pad)
S
SunAhong1993 已提交
2274
            pad_w = _get_same_padding(input_shape[3], kernel_shape[1],
W
wjj19950828 已提交
2275
                                      strides[1], auto_pad)
S
SunAhong1993 已提交
2276 2277
            paddings = pad_h + pad_w

S
fix  
SunAhong1993 已提交
2278
        layer_inputs = {'x': val_x if isinstance(val_x, str) else val_x.name}
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
        if val_w.name not in self.weights.keys():
            layer_attrs = {
                "stride": strides,
                "padding": paddings,
                "dilation": dilations,
                "groups": num_groups,
            }
            layer_inputs['weight'] = val_w.name
            if has_bias:
                layer_inputs['bias'] = val_b.name

            paddle_op = 'paddle.nn.functional.conv{}d'.format(convnd)
            self.paddle_graph.add_layer(
                paddle_op,
                inputs=layer_inputs,
                outputs=[node.name],
                **layer_attrs)
            return

S
SunAhong1993 已提交
2298 2299 2300 2301 2302 2303 2304 2305 2306
        layer_attrs = {
            "in_channels": num_in_channels * num_groups,
            "out_channels": num_out_channels,
            "kernel_size": kernel_shape,
            "stride": strides,
            "padding": paddings,
            "dilation": dilations,
            "groups": num_groups,
        }
2307
        remove_weight = True if val_w.name in self.done_weight_list else False
C
Channingss 已提交
2308 2309
        if remove_weight:
            self.done_weight_list.append(val_w.name)
2310 2311 2312 2313 2314 2315
        _rename_or_remove_weight(
            self.weights,
            val_w.name,
            op_name + '.weight',
            remove_weight,
            rename_mapper=self.rename_mapper)
S
SunAhong1993 已提交
2316
        if has_bias:
C
Channingss 已提交
2317 2318
            remove_bias = True if val_b.name in self.done_weight_list else False
            if remove_bias:
2319 2320 2321 2322 2323 2324 2325
                self.done_weight_list.append(val_b.name)
            _rename_or_remove_weight(
                self.weights,
                val_b.name,
                op_name + '.bias',
                remove_bias,
                rename_mapper=self.rename_mapper)
S
SunAhong1993 已提交
2326 2327
        else:
            layer_attrs["bias_attr"] = False
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
        # deal with dynamic shape
        if len(input_shape) == 0:
            self.paddle_graph.add_layer(
                "paddle.reshape",
                inputs=layer_inputs,
                outputs=[layer_inputs["x"]],
                shape=[0, num_in_channels * num_groups, 0, -1])
        if len(input_shape) != 0 and reduce(
                lambda x, y: x * y,
                input_shape) in [1, -1] and 1 not in input_shape:
S
fix  
SunAhong1993 已提交
2338 2339 2340 2341
            input_shape[1] = num_in_channels * num_groups
            input_shape[0] = 0
            input_shape[2] = 0
            self.paddle_graph.add_layer(
2342 2343 2344
                "paddle.reshape",
                inputs=layer_inputs,
                outputs=[layer_inputs["x"]],
S
fix  
SunAhong1993 已提交
2345
                shape=input_shape)
S
SunAhong1993 已提交
2346
        self.paddle_graph.add_layer(
2347 2348 2349
            paddle_op,
            inputs=layer_inputs,
            outputs=layer_outputs,
S
SunAhong1993 已提交
2350 2351 2352 2353
            **layer_attrs)

    @print_mapping_info
    def ConvTranspose(self, node):
2354
        output_name = node.name
S
SunAhong1993 已提交
2355 2356
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_w = self.graph.get_input_node(node, idx=1, copy=True)
2357 2358 2359 2360 2361 2362 2363 2364

        if val_w.name in self.weights.keys():
            op_name = name_generator("conv_trans", self.nn_name2id)
        else:
            op_name = output_name

        layer_outputs = [op_name, output_name]

S
SunAhong1993 已提交
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
        val_b = None
        if len(node.layer.input) > 2:
            val_b = self.graph.get_input_node(node, idx=2, copy=True)
        auto_pad = node.get_attr('auto_pad', 'NOTSET')
        out_padding = node.get_attr('output_padding', [0, 0])
        kernel_shape = node.get_attr('kernel_shape')
        assert kernel_shape, 'kernel_shape not inferred'
        convnd = len(kernel_shape)
        assert 2 <= convnd <= 3, 'only Conv2DTranspose and Conv3DTranspose supported'
        num_in_channels = val_w.out_shapes[0][0]
        num_out_channels = val_w.out_shapes[0][1]
2376
        paddle_op = 'paddle.nn.Conv{}DTranspose'.format(convnd)
S
SunAhong1993 已提交
2377 2378 2379 2380 2381 2382 2383

        num_groups = node.get_attr('group', 1)
        strides = node.get_attr('strides', [1] * convnd)
        dilations = node.get_attr('dilations', [1] * convnd)
        output_size = node.get_attr('output_shape', [])
        pads = node.get_attr('pads', [0] * (convnd * 2))

W
wjj19950828 已提交
2384 2385
        paddings = np.array(pads).reshape((2, -1)).transpose().astype("int32")
        paddings = paddings.flatten().tolist()
S
SunAhong1993 已提交
2386

W
wjj19950828 已提交
2387
        if len(output_size) != 0:
W
wjj19950828 已提交
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
            paddings = [0] * 4
            total_paddings = list()
            total_paddings.append((val_x.out_shapes[0][2] - 1) * strides[
                0] + dilations[0] * (kernel_shape[0] - 1) + 1 + out_padding[0] -
                                  output_size[0])
            total_paddings.append((val_x.out_shapes[0][3] - 1) * strides[
                1] + dilations[1] * (kernel_shape[1] - 1) + 1 + out_padding[1] -
                                  output_size[1])
            if auto_pad == "SAME_UPPER":
                for i in range(len(total_paddings)):
W
WJJ1995 已提交
2398 2399
                    paddings[2 * i] = total_paddings[0] - \
                        total_paddings[0] // 2
W
wjj19950828 已提交
2400 2401 2402 2403 2404 2405 2406 2407
                    paddings[2 * i + 1] = total_paddings[0] // 2
            else:
                for i in range(len(total_paddings)):
                    paddings[2 * i] = total_paddings[0] // 2
                    paddings[2 * i + 1] = total_paddings[0] - total_paddings[
                        0] // 2
        else:
            output_size = [0, 0]
S
SunAhong1993 已提交
2408

W
wjj19950828 已提交
2409 2410 2411 2412 2413 2414 2415 2416
            output_size[0] = (
                val_x.out_shapes[0][2] - 1
            ) * strides[0] - 2 * paddings[0] + dilations[0] * (
                kernel_shape[0] - 1) + 1 + out_padding[0]
            output_size[1] = (
                val_x.out_shapes[0][3] - 1
            ) * strides[1] - 2 * paddings[1] + dilations[1] * (
                kernel_shape[1] - 1) + 1 + out_padding[1]
2417

S
fix  
SunAhong1993 已提交
2418
        # Conv2DTranspose缺少output_size,只能在forward里头传进output_size
2419
        inputs_dict = {'x': val_x if isinstance(val_x, str) else val_x.name}
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
        if val_w.name not in self.weights.keys():
            layer_attrs = {
                "stride": strides,
                "dilation": dilations,
                "padding": paddings,
                "groups": num_groups,
                "output_padding": out_padding
            }
            paddle_op = 'paddle.nn.functional.conv{}d_transpose'.format(convnd)

            inputs_dict['weight'] = val_w.name
            if len(node.layer.input) > 2:
                inputs_dict['bias'] = val_b.name

            self.paddle_graph.add_layer(
                paddle_op,
                inputs=inputs_dict,
                outputs=[node.name],
                **layer_attrs)
            return

S
SunAhong1993 已提交
2441
        layer_attrs = {
2442
            "in_channels": num_in_channels,
S
SunAhong1993 已提交
2443
            "out_channels": num_out_channels * num_groups,
2444
            "kernel_size": kernel_shape,
S
fix  
SunAhong1993 已提交
2445 2446 2447
            "stride": strides,
            "dilation": dilations,
            "padding": paddings,
2448
            "groups": num_groups,
2449 2450 2451 2452 2453 2454
            "output_padding": out_padding
        }

        _rename_or_remove_weight(
            self.weights,
            val_w.name,
2455 2456
            op_name + '.weight',
            rename_mapper=self.rename_mapper)
S
fix  
SunAhong1993 已提交
2457
        if val_b is not None:
2458 2459 2460 2461 2462
            _rename_or_remove_weight(
                self.weights,
                val_b.name,
                op_name + '.bias',
                rename_mapper=self.rename_mapper)
W
wjj19950828 已提交
2463 2464
        else:
            layer_attrs["bias_attr"] = False
S
SunAhong1993 已提交
2465
        self.paddle_graph.add_layer(
2466
            kernel=paddle_op,
S
fix  
SunAhong1993 已提交
2467
            inputs=inputs_dict,
2468
            outputs=layer_outputs,
S
SunAhong1993 已提交
2469
            **layer_attrs)
2470

S
fix  
SunAhong1993 已提交
2471 2472 2473 2474 2475
    @print_mapping_info
    def ArgMax(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        axis = node.get_attr('axis')
        keepdims = False if node.get_attr('keepdims') == 0 else True
2476
        layer_attrs = {'axis': axis, 'keepdim': keepdims}
S
fix  
SunAhong1993 已提交
2477
        self.paddle_graph.add_layer(
2478 2479
            'paddle.argmax',
            inputs={"x": val_x.name},
S
fix  
SunAhong1993 已提交
2480
            outputs=[node.name],
C
Channingss 已提交
2481 2482 2483
            **layer_attrs)

    @print_mapping_info
S
SunAhong1993 已提交
2484 2485 2486
    def Size(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        self.paddle_graph.add_layer(
2487
            "paddle.shape", inputs={"input": val_x.name}, outputs=[node.name])
S
fix  
SunAhong1993 已提交
2488 2489 2490 2491
        self.paddle_graph.add_layer(
            'paddle.cast',
            inputs={"x": node.name},
            outputs=[node.name],
2492
            dtype=string('int64'))
S
SunAhong1993 已提交
2493
        self.paddle_graph.add_layer(
2494 2495
            "paddle.prod", inputs={"x": node.name}, outputs=[node.name])

S
SunAhong1993 已提交
2496 2497 2498
    @print_mapping_info
    def Sign(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
S
fix  
SunAhong1993 已提交
2499 2500
        if node.dtype not in ["float16", "float32", "float64"]:
            self.paddle_graph.add_layer(
2501 2502
                "paddle.cast",
                inputs={"x": val_x.name},
S
fix  
SunAhong1993 已提交
2503 2504
                outputs=[val_x.name],
                dtype=string("float32"))
S
SunAhong1993 已提交
2505
        self.paddle_graph.add_layer(
2506
            "paddle.sign", inputs={"x": val_x.name}, outputs=[node.name])
S
fix  
SunAhong1993 已提交
2507 2508
        if node.dtype not in ["float16", "float32", "float64"]:
            self.paddle_graph.add_layer(
2509 2510
                "paddle.cast",
                inputs={"x": node.name},
S
fix  
SunAhong1993 已提交
2511 2512
                outputs=[node.name],
                dtype=string(node.dtype))
2513

S
SunAhong1993 已提交
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523
    @print_mapping_info
    def OneHot(self, node):
        nn_op_name = name_generator("onehot", self.nn_name2id)
        output_name = node.name
        layer_outputs = [nn_op_name, output_name]
        indices = self.graph.get_input_node(node, idx=0, copy=True)
        depth = self.graph.get_input_node(node, idx=1, copy=True)
        values = self.graph.get_input_node(node, idx=2, copy=True)
        axis = node.get_attr('axis', -1)
        self.paddle_graph.add_layer(
2524 2525 2526 2527 2528 2529
            "custom_layer:OneHot",
            inputs={
                "indices": indices.name,
                "depth": depth.name,
                "values": values.name
            },
S
SunAhong1993 已提交
2530 2531
            outputs=layer_outputs,
            axis=axis)
2532

S
SunAhong1993 已提交
2533 2534 2535 2536
    @print_mapping_info
    def Reciprocal(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        self.paddle_graph.add_layer(
2537
            "paddle.reciprocal", inputs={"x": val_x.name}, outputs=[node.name])
C
Channingss 已提交
2538

2539 2540
    @print_mapping_info
    def LSTM(self, node):
C
Channingss 已提交
2541 2542 2543 2544 2545 2546
        x = self.graph.get_input_node(node, idx=0, copy=True)
        input_weight = self.graph.get_input_node(node, idx=1, copy=True)
        hidden_weight = self.graph.get_input_node(node, idx=2, copy=True)

        input_nums = len(node.layer.input)
        exist_input_nums = 3
2547
        have_bias = False
C
Channingss 已提交
2548
        if input_nums > 3 and node.layer.input[3] != '':
2549 2550
            bias = self.graph.get_input_node(
                node, idx=exist_input_nums, copy=True)
2551
            have_bias = True
C
Channingss 已提交
2552 2553
            exist_input_nums += 1
        if input_nums > 4 and node.layer.input[4] != '':
2554 2555
            sequence_lens = self.graph.get_input_node(
                node, idx=exist_input_nums, copy=True)
C
Channingss 已提交
2556 2557
            exist_input_nums += 1
        if input_nums > 5 and node.layer.input[5] != '':
2558 2559
            init_h = self.graph.get_input_node(
                node, idx=exist_input_nums, copy=True)
W
WJJ1995 已提交
2560 2561 2562 2563 2564 2565 2566 2567
            init_h_shape = init_h.out_shapes[0]
            if len(init_h_shape) != 0 and reduce(lambda x, y: x * y,
                                                 init_h_shape) not in [1, -1]:
                self.paddle_graph.add_layer(
                    'paddle.reshape',
                    inputs={"x": init_h.name},
                    outputs=[init_h.name],
                    shape=init_h.out_shapes[0])
C
Channingss 已提交
2568 2569
            exist_input_nums += 1
        if input_nums > 6 and node.layer.input[6] != '':
2570 2571
            init_c = self.graph.get_input_node(
                node, idx=exist_input_nums, copy=True)
W
WJJ1995 已提交
2572 2573 2574 2575 2576 2577 2578 2579
            init_c_shape = init_c.out_shapes[0]
            if len(init_c_shape) != 0 and reduce(lambda x, y: x * y,
                                                 init_c_shape) not in [1, -1]:
                self.paddle_graph.add_layer(
                    'paddle.reshape',
                    inputs={"x": init_c.name},
                    outputs=[init_c.name],
                    shape=init_c.out_shapes[0])
C
Channingss 已提交
2580 2581

        input_weight_np = _const_weight_or_none(input_weight)
C
Channingss 已提交
2582
        _rename_or_remove_weight(self.weights, input_weight.name)
2583
        hidden_size = node.get_attr('hidden_size', input_weight_np.shape[1] / 4)
C
Channingss 已提交
2584 2585
        input_size = input_weight_np.shape[2]
        hidden_weight_np = _const_weight_or_none(hidden_weight)
C
Channingss 已提交
2586
        _rename_or_remove_weight(self.weights, hidden_weight.name)
C
Channingss 已提交
2587
        bias_np = _const_weight_or_none(bias)
C
Channingss 已提交
2588
        _rename_or_remove_weight(self.weights, bias.name)
2589 2590
        input_bias_np = bias_np[:, :4 * hidden_size]
        hidden_bias_np = bias_np[:, 4 * hidden_size:]
2591 2592 2593 2594 2595 2596

        # parameters order in paddle:lstm:
        # 1. gate order in paddle is: input, forget, cell, output.
        # 2. gate orfer in onnx is: input, output, forget, cell.

        def reform_weights(w, n, intervals):
2597
            slices = [w[:, x * n:y * n] for x, y in intervals]
2598
            return np.concatenate(slices, axis=1)
C
Channingss 已提交
2599

2600 2601 2602 2603
        def transform_weight_with_bias(weights, n, intervals):
            return [reform_weights(w, n, intervals) for w in weights]

        reform_permutation = [(0, 1), (2, 4), (1, 2)]
C
Channingss 已提交
2604

C
Channingss 已提交
2605
        weights = transform_weight_with_bias(
C
Channingss 已提交
2606 2607 2608 2609 2610
            [input_weight_np, hidden_weight_np, input_bias_np, hidden_bias_np],
            hidden_size, reform_permutation)

        op_name = name_generator("lstm", self.nn_name2id)
        y_out = node.output(0)
2611
        yh_out = node.output(1)
C
Channingss 已提交
2612
        yc_out = node.output(2)
2613
        direction = node.get_attr('direction', 'forward')
C
Channingss 已提交
2614 2615 2616 2617

        def generate_paddle_param_names(op_name, suffix=''):
            param_names = []
            param_names.extend(['{}.weight_ih_l0{}', '{}.weight_hh_l0{}'])
W
WJJ1995 已提交
2618 2619 2620 2621
            if have_bias != False:
                param_names.append('{}.bias_ih_l0{}')
            if have_bias != False:
                param_names.append('{}.bias_hh_l0{}')
C
Channingss 已提交
2622 2623 2624 2625 2626 2627 2628 2629
            param_names = [x.format(op_name, suffix) for x in param_names]
            return param_names

        def assign_params(op_name, weights, weight_idx=0, suffix=''):
            param_names = generate_paddle_param_names(op_name, suffix)
            for param_name, weight in zip(param_names, weights):
                self.weights[param_name] = weight[weight_idx]

2630
        if direction == 'backward':
2631 2632 2633
            raise Exception(
                "LSTM support 'forward' or 'bidirectional', except '{}'.".
                format(direction))
2634
        else:
C
Channingss 已提交
2635 2636 2637
            assign_params(op_name, weights)
            if direction == 'bidirectional':
                assign_params(op_name, weights, 1, '_reverse')
2638

C
Channingss 已提交
2639
        self.paddle_graph.add_layer(
2640 2641 2642 2643 2644
            'paddle.nn.LSTM',
            inputs={
                'input': x.name,
                'initial_states': (init_h.name, init_c.name)
            },
C
Channingss 已提交
2645 2646 2647 2648
            outputs=[op_name, y_out, yh_out, yc_out],
            input_size=input_size,
            hidden_size=hidden_size,
            num_layers=1,
2649
            direction=string(direction),
C
Channingss 已提交
2650 2651 2652 2653 2654 2655
            time_major=True)

        self.paddle_graph.add_layer(
            'paddle.reshape',
            inputs={"x": y_out},
            outputs=[y_out],
2656
            shape=[0, 0, -1, hidden_size])
C
Channingss 已提交
2657 2658 2659 2660
        self.paddle_graph.add_layer(
            'paddle.transpose',
            inputs={"x": y_out},
            outputs=[y_out],
2661 2662
            perm=[0, 2, 1, 3])

S
SunAhong1993 已提交
2663 2664 2665 2666
    @print_mapping_info
    def TopK(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        val_k = self.graph.get_input_node(node, idx=1, copy=True)
W
WJJ1995 已提交
2667 2668 2669 2670 2671 2672 2673 2674
        # If the topk result is the entire graph output, modify the graph result
        graph_output_new = list()
        if node.layer_name in self.graph.output_nodes:
            graph_output_new = [
                "{}_p{}".format(node.layer_name, 0)
                if x == node.layer_name else x for x in self.graph.output_nodes
            ]
            self.paddle_graph.outputs = graph_output_new
S
SunAhong1993 已提交
2675 2676
        layer_attrs = dict()
        layer_attrs["axis"] = node.get_attr('axis', -1)
2677 2678 2679 2680
        layer_attrs["largest"] = True if node.get_attr('largest',
                                                       1) == 1 else False
        layer_attrs["sorted"] = True if node.get_attr('sorted',
                                                      1) == 1 else False
W
wjj19950828 已提交
2681 2682 2683
        k = _const_weight_or_none(val_k)
        if isinstance(k, (list, tuple, np.ndarray)):
            k = k[0]
W
wjj19950828 已提交
2684
        # If k can get the value directly, it is used as an attribute; otherwise it is used as an input tensor
W
wjj19950828 已提交
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
        if k is not None:
            layer_attrs["k"] = k
            self.paddle_graph.add_layer(
                "paddle.topk",
                inputs={"x": val_x.name},
                outputs=[
                    "{}_p{}".format(node.layer_name, 0),
                    "{}_p{}".format(node.layer_name, 1)
                ],
                **layer_attrs)
        else:
            if val_k.dtype != "int32":
                self.paddle_graph.add_layer(
                    "paddle.cast",
                    inputs={"x": val_k.name},
                    outputs=[val_k.name],
                    dtype=string('int32'))
            self.paddle_graph.add_layer(
                "paddle.topk",
                inputs={"x": val_x.name,
                        "k": val_k.name},
                outputs=[
                    "{}_p{}".format(node.layer_name, 0),
                    "{}_p{}".format(node.layer_name, 1)
                ],
                **layer_attrs)
2711

S
add lrn  
SunAhong1993 已提交
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
    @print_mapping_info
    def LRN(self, node):
        op_name = name_generator("lrn", self.nn_name2id)
        output_name = node.name
        layer_outputs = [op_name, output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        alpha = node.get_attr('alpha', 0.0001)
        beta = node.get_attr('beta', 0.75)
        bias = node.get_attr('bias', 1.0)
        size = node.get_attr('size')
2722
        layer_attrs = {'size': size, 'alpha': alpha, 'beta': beta, 'k': bias}
S
add lrn  
SunAhong1993 已提交
2723
        self.paddle_graph.add_layer(
W
WJJ1995 已提交
2724
            "paddle.nn.LocalResponseNorm",
2725 2726
            inputs={"x": val_x.name},
            outputs=layer_outputs,
S
add lrn  
SunAhong1993 已提交
2727
            **layer_attrs)
2728

S
SunAhong1993 已提交
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
    @print_mapping_info
    def DepthToSpace(self, node):
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        blocksize = node.get_attr('blocksize')
        mode = node.get_attr('mode', "DCR")
        val_x_shape = val_x.out_shapes[0]
        b, c, h, w = val_x_shape
        if mode == "DCR":
            self.paddle_graph.add_layer(
                'paddle.reshape',
                inputs={"x": val_x.name},
                outputs=[node.name],
2741
                shape=[b, blocksize, blocksize, c // (blocksize**2), h, w])
S
SunAhong1993 已提交
2742 2743 2744 2745
            self.paddle_graph.add_layer(
                'paddle.transpose',
                inputs={"x": node.name},
                outputs=[node.name],
2746
                perm=[0, 3, 4, 1, 5, 2])
S
SunAhong1993 已提交
2747 2748 2749 2750
            self.paddle_graph.add_layer(
                'paddle.reshape',
                inputs={"x": node.name},
                outputs=[node.name],
2751
                shape=[b, c // (blocksize**2), h * blocksize, w * blocksize])
S
SunAhong1993 已提交
2752 2753 2754 2755 2756
        else:
            self.paddle_graph.add_layer(
                'paddle.reshape',
                inputs={"x": val_x.name},
                outputs=[node.name],
2757
                shape=[b, c // (blocksize**2), blocksize, blocksize, h, w])
S
SunAhong1993 已提交
2758 2759 2760 2761
            self.paddle_graph.add_layer(
                'paddle.transpose',
                inputs={"x": node.name},
                outputs=[node.name],
2762
                perm=[0, 1, 4, 2, 5, 3])
S
SunAhong1993 已提交
2763 2764 2765 2766
            self.paddle_graph.add_layer(
                'paddle.reshape',
                inputs={"x": node.name},
                outputs=[node.name],
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
                shape=[b, c // (blocksize**2), h * blocksize, w * blocksize])

    @print_mapping_info
    def NonMaxSuppression(self, node):
        nn_op_name = name_generator("nms", self.nn_name2id)
        output_name = node.name
        layer_outputs = [nn_op_name, output_name]
        boxes = self.graph.get_input_node(node, idx=0, copy=True)
        scores = self.graph.get_input_node(node, idx=1, copy=True)
        inputs_len = len(node.layer.input)
        layer_attrs = dict()
W
wjj19950828 已提交
2778 2779 2780
        layer_attrs["keep_top_k"] = -1
        layer_attrs["nms_threshold"] = 0.0
        layer_attrs["score_threshold"] = 0.0
2781 2782 2783
        if inputs_len > 2:
            max_output_boxes_per_class = self.graph.get_input_node(
                node, idx=2, copy=True)
W
wjj19950828 已提交
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
            max_output_boxes_per_class = _const_weight_or_none(
                max_output_boxes_per_class)
            if len(scores.out_shapes[0]) != 0:
                num_classes = scores.out_shapes[0][1]
            else:
                num_classes = 1
            if max_output_boxes_per_class is not None:
                max_output_boxes_per_class = max_output_boxes_per_class.tolist()
                if isinstance(max_output_boxes_per_class, int):
                    layer_attrs[
                        "keep_top_k"] = max_output_boxes_per_class * num_classes
                else:
                    layer_attrs["keep_top_k"] = max_output_boxes_per_class[
                        0] * num_classes
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
        if inputs_len > 3:
            iou_threshold = self.graph.get_input_node(node, idx=3, copy=True)
            layer_attrs["nms_threshold"] = _const_weight_or_none(
                iou_threshold).tolist()[0]
        if inputs_len > 4:
            score_threshold = self.graph.get_input_node(node, idx=4, copy=True)
            layer_attrs["score_threshold"] = _const_weight_or_none(
                score_threshold).tolist()[0]
        self.paddle_graph.add_layer(
            "custom_layer:NMS",
            inputs={"bboxes": boxes.name,
                    "scores": scores.name},
            outputs=layer_outputs,
            **layer_attrs)
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839

    @print_mapping_info
    def ReduceL1(self, node):
        output_name = node.name
        layer_outputs = [output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        axes = node.get_attr('axes')
        keepdims = False if node.get_attr('keepdims') == 0 else True
        layer_attrs = {'p': 1, 'axis': axes, 'keepdim': keepdims}
        self.paddle_graph.add_layer(
            "paddle.norm",
            inputs={"x": val_x.name},
            outputs=layer_outputs,
            **layer_attrs)

    @print_mapping_info
    def ReduceL2(self, node):
        output_name = node.name
        layer_outputs = [output_name]
        val_x = self.graph.get_input_node(node, idx=0, copy=True)
        axes = node.get_attr('axes')
        keepdims = False if node.get_attr('keepdims') == 0 else True
        layer_attrs = {'p': 2, 'axis': axes, 'keepdim': keepdims}
        self.paddle_graph.add_layer(
            "paddle.norm",
            inputs={"x": val_x.name},
            outputs=layer_outputs,
            **layer_attrs)