aten.py 232.8 KB
Newer Older
S
SunAhong1993 已提交
1
# -*- coding:UTF-8 -*-
S
SunAhong1993 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2020  PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

S
SunAhong1993 已提交
16
import copy
S
SunAhong1993 已提交
17
import numpy as np
S
SunAhong1993 已提交
18 19
from x2paddle.core.util import name_generator, string
from x2paddle.utils import paddle_dtypes
S
SunAhong1993 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
from x2paddle.core.program import PaddleGraph

dtype_dict = {
    0: string("uint8"),
    1: string("int8"),
    2: string("int16"),
    3: string("int32"),
    4: string("int64"),
    5: string("float16"),
    6: string("float32"),
    7: string("float64"),
    11: string("bool")
}


Y
yeliang2258 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
def aten_sum(mapper, graph, node):
    """ 构造获取元素求和的paddlelayer。
    TorchScript示例:
        %x_gap.15 : Tensor =  aten::sum(%x.58, %2166, %1450, %1453)
        参数含义:
        %x_gap.15 (Tensor): 求和后的Tensor。
        %n.3 (Tensor): 求和前的Tensor。
        %2166:axis
        %1450:keepdim
        %1453:dtype
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%n.3
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    if inputs_name[2] in mapper.attrs:
        layer_attrs["keepdim"] = mapper.attrs[inputs_name[2]]
    if inputs_name[3] in mapper.attrs:
        layer_attrs["dtype"] = mapper.attrs[inputs_name[3]]
    graph.add_layer(
        "paddle.sum",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs

W
WJJ1995 已提交
74

S
SunAhong1993 已提交
75 76 77 78 79 80 81 82
def aten_abs(mapper, graph, node):
    """ 构造获取绝对值的PaddleLayer。
    TorchScript示例:
        %n0.3 : Tensor = aten::abs(%n.3)
        参数含义:
        %n0.3 (Tensor): 绝对值后的Tensor。
        %n.3 (Tensor): 绝对值前的Tensor。
    """
S
SunAhong1993 已提交
83
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
84 85 86 87 88 89 90
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%n.3
S
SunAhong1993 已提交
91 92
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
93 94 95 96 97
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
98 99 100 101
        "paddle.abs",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
102 103 104
    return current_inputs, current_outputs


S
SunAhong1993 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
def aten_adaptive_avg_pool1d(mapper, graph, node):
    """ 构造average adaptive pool1d的PaddleLayer。
    TorchScript示例:
        %x.5 : Tensor = aten::adaptive_avg_pool1d(%x.3, %_output_size.1)
        参数含义:
        %x.5 (Tensor): 池化后结果Tensor。
        %x.3 (Tensor): 输入Tensor。
        %_output_size.1 (list): 自适应池化后的Tensor的长度大小。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool1d", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.3
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%_output_size.1
    if inputs_name[1] in mapper.attrs:
        layer_attrs["output_size"] = mapper.attrs[inputs_name[1]][0]
        graph.add_layer(
            "paddle.nn.AdaptiveAvgPool1D",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name,
            **layer_attrs)
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["output_size"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
        graph.add_layer(
            "prim.getitem",
            inputs={"list": layer_inputs["output_size"]},
            outputs=[layer_inputs["output_size"]],
            scope_name=scope_name,
            index=0)
        graph.add_layer(
            "paddle.nn.functional.adaptive_avg_pool1d",
            inputs=layer_inputs,
            outputs=layer_outputs[1:],
            scope_name=scope_name,
            **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
158 159 160 161 162 163 164 165 166
def aten_adaptive_avg_pool2d(mapper, graph, node):
    """ 构造average adaptive pool2d的PaddleLayer。
    TorchScript示例:
        %x.5 : Tensor = aten::adaptive_avg_pool2d(%x.3, %_output_size.1)
        参数含义:
        %x.5 (Tensor): 池化后结果Tensor。
        %x.3 (Tensor): 输入Tensor。
        %_output_size.1 (list): 自适应池化后的Tensor的宽、高大小。
    """
S
SunAhong1993 已提交
167 168
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool2d", mapper.nn_name2id)
S
SunAhong1993 已提交
169
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
170
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
171 172 173 174 175 176
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.3
S
SunAhong1993 已提交
177 178
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
179
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
180 181 182 183
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%_output_size.1
    if inputs_name[1] in mapper.attrs:
S
SunAhong1993 已提交
184
        layer_attrs["output_size"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
185 186 187 188 189 190
        graph.add_layer(
            "paddle.nn.AdaptiveAvgPool2D",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name,
            **layer_attrs)
S
SunAhong1993 已提交
191 192
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
193 194
                            current_outputs, scope_name)
        layer_inputs["output_size"] = inputs_name[1]
S
SunAhong1993 已提交
195
        current_inputs.append(inputs_name[1])
S
SunAhong1993 已提交
196 197 198 199 200 201
        graph.add_layer(
            "paddle.nn.functional.adaptive_avg_pool2d",
            inputs=layer_inputs,
            outputs=layer_outputs[1:],
            scope_name=scope_name,
            **layer_attrs)
S
SunAhong1993 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    return current_inputs, current_outputs


def aten_addmm(mapper, graph, node):
    """ 构造addmm的PaddleLayer,该节点实现out = alpha ∗ x ∗ y + beta ∗ input。
    TorchScript示例:
        %ret.2 : Tensor = aten::addmm(%150, %input.3, %156, %151, %152)
        参数含义:
        %ret.2 (Tensor): addmm结果Tensor。
        %150 (Tensor): 输入Tensor input。
        %input.3 (Tensor): 输入Tensor x。
        %156 (Tensor): 输入Tensor y。
        %151 (int/float): 输入alpha。
        %152 (int/float): 输入beta。
    """
S
SunAhong1993 已提交
217
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
218 219 220 221 222 223 224 225
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%150
S
SunAhong1993 已提交
226 227
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
228 229
    layer_inputs["input"] = inputs_name[0]
    # 处理输入1,即%input.3
S
SunAhong1993 已提交
230 231
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
232 233
    layer_inputs["x"] = inputs_name[1]
    # 处理输入2,即%156
S
SunAhong1993 已提交
234 235
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
236 237 238 239 240 241 242 243
    layer_inputs["y"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入3,即%152
    if inputs_name[3] in mapper.attrs:
        layer_attrs["beta"] = mapper.attrs[inputs_name[3]]
    else:
        mapper._check_input(graph, inputs_node[3], inputs_name[3],
S
SunAhong1993 已提交
244
                            current_outputs, scope_name)
S
SunAhong1993 已提交
245 246 247 248 249 250 251
        layer_inputs["beta"] = inputs_name[3]
        current_inputs.append(inputs_name[3])
    # 处理输入4,即%151
    if inputs_name[4] in mapper.attrs:
        layer_attrs["alpha"] = mapper.attrs[inputs_name[4]]
    else:
        mapper._check_input(graph, inputs_node[4], inputs_name[4],
S
SunAhong1993 已提交
252
                            current_outputs, scope_name)
S
SunAhong1993 已提交
253 254 255 256 257 258 259
        layer_inputs["alpha"] = inputs_name[4]
        current_inputs.append(inputs_name[4])

    graph.add_layer(
        "paddle.addmm",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
260
        scope_name=scope_name,
S
SunAhong1993 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274
        **layer_attrs)
    return current_inputs, current_outputs


def aten_add(mapper, graph, node):
    """ 构造数值相加的PaddleLayer,该节点实现out = x + alpha * y。
    TorchScript示例:
        %137 : Tensor = aten::add(%136, %130, %130)
        参数含义:
        %output.5 (Tensor): add结果Tensor。
        %output.2 (Tensor): 输入Tensor x。
        %150 (Tensor): 输入Tensor y。
        %151 (int/float): 输入alpha。
    """
S
SunAhong1993 已提交
275
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
276 277 278 279 280 281 282 283
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%output.2
S
SunAhong1993 已提交
284 285
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
286 287
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%150
S
SunAhong1993 已提交
288 289
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
290 291 292
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
293 294 295 296 297 298 299 300 301
    if len(inputs_name) > 2:
        # 处理输入2,即%151
        if inputs_name[2] in mapper.attrs:
            layer_attrs["alpha"] = mapper.attrs[inputs_name[2]]
        else:
            mapper._check_input(graph, inputs_node[2], inputs_name[2],
                                current_outputs, scope_name)
            layer_inputs["alpha"] = inputs_name[2]
            current_inputs.append(inputs_name[2])
S
SunAhong1993 已提交
302

303 304 305 306 307 308 309 310 311 312 313 314 315
        graph.add_layer(
            "prim.add_",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name,
            **layer_attrs)
    else:
        graph.add_layer(
            "prim.add",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name,
            **layer_attrs)
S
SunAhong1993 已提交
316 317 318 319 320 321 322 323 324 325 326 327
    return current_inputs, current_outputs


def aten___and__(mapper, graph, node):
    """ 构造与计算的PaddleLayer。
    TorchScript示例:
        %361 : bool = aten::__and__(%360, %358)
        参数含义:
        %361 (bool): 输出,与计算结果。
        %360 (-): 输入 x。
        %358 (-): 输入 y。
    """
S
SunAhong1993 已提交
328
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
329 330 331 332 333 334 335
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%i.12
S
SunAhong1993 已提交
336 337
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
338 339
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%288
S
SunAhong1993 已提交
340 341
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
342 343 344 345
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
346 347 348 349 350
    graph.add_layer(
        "prim.and",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
351 352 353 354 355 356 357 358 359 360 361 362
    return current_inputs, current_outputs


def aten_append(mapper, graph, node):
    """ 构造对list进行append的PaddleLayer。
    TorchScript示例:
        %90 : int[] = aten::append(%_output_size.1, %v.1)
        参数含义:
        %90 (list): 输出,append后的list。
        %_output_size.1 (list): 需要进行append的list。
        %v.1 (-): append的元素。
    """
S
SunAhong1993 已提交
363
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
364 365 366 367 368 369
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    layer_outputs = [inputs_name[0]]
    # 获取当前节点输出的list
    current_outputs = [inputs_name[0]]
    # 处理输入0,即_output_size.1
S
SunAhong1993 已提交
370 371
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
372 373
    layer_inputs["list"] = inputs_name[0]
    # 处理输入1,即v.1
S
SunAhong1993 已提交
374 375
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
376 377 378 379
    layer_inputs["element"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
380 381 382 383 384
    graph.add_layer(
        "prim.append",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
385 386 387 388 389 390 391 392
    return current_inputs, current_outputs


def aten_arange(mapper, graph, node):
    """ 构造以步长均匀分隔给定数值区间的PaddleLayer。
    TorchScript示例:
        有三种情况,分别处理。
    """
S
SunAhong1993 已提交
393
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    current_inputs = []
    if len(inputs_name) == 5:
        # %position_ids.1 : Tensor = aten::arange(%52, %43, %45, %42, %46)
        # 输入的后三者分别代表layout、device、是否使用梯度
        # 处理输入0,即%52,代表end
        if inputs_name[0] in mapper.attrs:
            layer_attrs["end"] = mapper.attrs[inputs_name[0]]
        else:
            mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
410
                                current_outputs, scope_name)
S
SunAhong1993 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
            layer_inputs["end"] = inputs_name[0]
            current_inputs.append(inputs_name[0])
        # 处理输入1,即%43,代表dtype
        if mapper.attrs[inputs_name[1]] is None:
            layer_attrs["dtype"] = None
        else:
            layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[1]]]
    elif len(inputs_name) == 6:
        # %position_ids.1 : Tensor = aten::arange(%51, %52, %43, %45, %42, %46)
        # 输入的后三者分别代表layout、device、是否使用梯度
        # 处理输入0,即%51,代表start
        if inputs_name[0] in mapper.attrs:
            layer_attrs["start"] = mapper.attrs[inputs_name[0]]
        else:
            mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
426
                                current_outputs, scope_name)
S
SunAhong1993 已提交
427 428 429 430 431 432 433
            layer_inputs["start"] = inputs_name[0]
            current_inputs.append(inputs_name[0])
        # 处理输入1,即%52,代表end
        if inputs_name[1] in mapper.attrs:
            layer_attrs["end"] = mapper.attrs[inputs_name[1]]
        else:
            mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
434
                                current_outputs, scope_name)
S
SunAhong1993 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
            layer_inputs["end"] = inputs_name[1]
            current_inputs.append(inputs_name[1])
        # 处理输入2,即%43,代表dtype
        if mapper.attrs[inputs_name[2]] is None:
            layer_attrs["dtype"] = None
        else:
            layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[2]]]
    elif len(inputs_name) == 7:
        # %position_ids.1 : Tensor = aten::arange(%51, %52, %53, %43, %45, %42, %46)
        # 输入的后三者分别代表layout、device、是否使用梯度
        # 处理输入0,即%51,代表start
        if inputs_name[0] in mapper.attrs:
            layer_attrs["start"] = mapper.attrs[inputs_name[0]]
        else:
            mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
450
                                current_outputs, scope_name)
S
SunAhong1993 已提交
451 452 453 454 455 456 457
            layer_inputs["start"] = inputs_name[0]
            current_inputs.append(inputs_name[0])
        # 处理输入1,即%52,代表end
        if inputs_name[1] in mapper.attrs:
            layer_attrs["end"] = mapper.attrs[inputs_name[1]]
        else:
            mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
458
                                current_outputs, scope_name)
S
SunAhong1993 已提交
459 460 461 462 463 464 465
            layer_inputs["end"] = inputs_name[1]
            current_inputs.append(inputs_name[1])
        # 处理输入2,即%53,代表step
        if inputs_name[2] in mapper.attrs:
            layer_attrs["step"] = mapper.attrs[inputs_name[2]]
        else:
            mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
466
                                current_outputs, scope_name)
S
SunAhong1993 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
            layer_inputs["step"] = inputs_name[2]
            current_inputs.append(inputs_name[2])
        # 处理输入3,即%43,代表dtype
        if mapper.attrs[inputs_name[3]] is None:
            layer_attrs["dtype"] = None
        else:
            layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[3]]]
    else:
        raise Exception("Unknown aten::arange signature taking " + str(
            len(inputs_name)) + " arguments.")

    graph.add_layer(
        "paddle.arange",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
482
        scope_name=scope_name,
S
SunAhong1993 已提交
483 484 485 486
        **layer_attrs)
    return current_inputs, current_outputs


W
WJJ1995 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
def aten_argmax(mapper, graph, node):
    """
    TorchScript:
        %x.28 : Tensor = aten::argmax(%result.1, %4967, %3, %2)
        Parameter meaning:
        %x.28 (Tensor): Output Tensor
        %result.1 (Tensor): Input Tensor
        %4967 (int/list): Axis
        %3 (bool): Keepdim
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # output list
    current_outputs = [output_name]
    # process Input Tensor
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    current_inputs = list(layer_inputs.values())
    # process Axis
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # process Keepdim
    if inputs_name[2] in mapper.attrs:
        layer_attrs["keepdim"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
        layer_inputs["keepdim"] = inputs_name[2]
        current_inputs.append(inputs_name[2])

    graph.add_layer(
        "paddle.argmax",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549
def aten_avg_pool2d(mapper, graph, node):
    """ 构造最大池化的PaddleLayer。
    TorchScript示例:
        %branch_pool.2 : Tensor = aten::avg_pool2d(%x.43, %538, %539, %540, %273, %272, %271)
        参数含义:
        %branch_pool.2 (Tensor): 输出,池化后的结果。
        %x.43 (Tensor): 需要池化的Tensor。
        %538 (list): 池化kernel的大小。
        %539 (list): 步长大小。
        %540 (list): 填充大小。
        %273 (bool): 是否用ceil函数计算输出高度和宽度。
        %272 (bool): 是否在平均池化模式不忽略填充值,False为忽略。
        %271 (int): 如果指定,它将用作除数,否则将使用池化区域的大小。
    """
S
SunAhong1993 已提交
550 551
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool2d", mapper.nn_name2id)
S
SunAhong1993 已提交
552
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
553
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
554 555 556 557 558 559
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.34
S
SunAhong1993 已提交
560 561
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
562 563 564 565
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%538
S
SunAhong1993 已提交
566
    layer_attrs["kernel_size"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
567
    # 处理输入2,即%539
S
SunAhong1993 已提交
568
    layer_attrs["stride"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
569
    # 处理输入3,即%540
S
SunAhong1993 已提交
570
    layer_attrs["padding"] = mapper.attrs[inputs_name[3]]
S
SunAhong1993 已提交
571 572 573 574 575 576 577 578
    # 处理输入4,即%273
    layer_attrs["ceil_mode"] = mapper.attrs[inputs_name[4]]
    # 处理输入5,即%272
    layer_attrs["exclusive"] = not mapper.attrs[inputs_name[5]]
    # 处理输入6,即%271
    graph.add_layer(
        "prim.assert",
        inputs={},
C
channingss 已提交
579
        outputs=[inputs_name[6] + "_assert"],
S
SunAhong1993 已提交
580
        scope_name=scope_name if scope_name == "" else scope_name + "_assert",
S
SunAhong1993 已提交
581 582 583
        type="eq",
        key=mapper.attrs[inputs_name[6]],
        value=None)
S
SunAhong1993 已提交
584 585

    graph.add_layer(
S
SunAhong1993 已提交
586
        kernel="paddle.nn.AvgPool2D",
S
SunAhong1993 已提交
587
        inputs=layer_inputs,
S
SunAhong1993 已提交
588
        outputs=layer_outputs,
S
SunAhong1993 已提交
589 590
        scope_name=scope_name,
        **layer_attrs)
S
SunAhong1993 已提交
591

S
SunAhong1993 已提交
592 593
    return current_inputs, current_outputs

S
SunAhong1993 已提交
594

S
SunAhong1993 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
def aten_avg_pool3d(mapper, graph, node):
    """ 构造最大池化的PaddleLayer。
    TorchScript示例:
        %branch_pool.2 : Tensor = aten::avg_pool2d(%x.43, %538, %539, %540, %273, %272, %271)
        参数含义:
        %branch_pool.2 (Tensor): 输出,池化后的结果。
        %x.43 (Tensor): 需要池化的Tensor。
        %538 (list): 池化kernel的大小。
        %539 (list): 步长大小。
        %540 (list): 填充大小。
        %273 (bool): 是否用ceil函数计算输出高度和宽度。
        %272 (bool): 是否在平均池化模式不忽略填充值,False为忽略。
        %271 (int): 如果指定,它将用作除数,否则将使用池化区域的大小。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool2d", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.34
S
SunAhong1993 已提交
619 620
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
621 622 623 624
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%538
S
SunAhong1993 已提交
625
    layer_attrs["kernel_size"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
626
    # 处理输入2,即%539
S
SunAhong1993 已提交
627
    layer_attrs["stride"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
628
    # 处理输入3,即%540
S
SunAhong1993 已提交
629
    layer_attrs["padding"] = mapper.attrs[inputs_name[3]]
S
SunAhong1993 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643
    # 处理输入4,即%273
    layer_attrs["ceil_mode"] = mapper.attrs[inputs_name[4]]
    # 处理输入5,即%272
    layer_attrs["exclusive"] = not mapper.attrs[inputs_name[5]]
    # 处理输入6,即%271
    graph.add_layer(
        "prim.assert",
        inputs={},
        outputs=[inputs_name[6] + "_assert"],
        scope_name=scope_name if scope_name == "" else scope_name + "_assert",
        type="eq",
        key=mapper.attrs[inputs_name[6]],
        value=None)

S
SunAhong1993 已提交
644
    graph.add_layer(
S
SunAhong1993 已提交
645
        kernel="paddle.nn.AvgPool3D",
S
SunAhong1993 已提交
646
        inputs=layer_inputs,
S
SunAhong1993 已提交
647
        outputs=layer_outputs,
S
SunAhong1993 已提交
648 649 650 651 652
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
fix  
SunAhong1993 已提交
653
def aten_avg_pool1d(mapper, graph, node):
S
SunAhong1993 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    """ 构造最大池化的PaddleLayer。
    TorchScript示例:
        %branch_pool.2 : Tensor = aten::avg_pool1d(%x.43, %538, %539, %540, %273, %272, %271)
        参数含义:
        %branch_pool.2 (Tensor): 输出,池化后的结果。
        %x.43 (Tensor): 需要池化的Tensor。
        %538 (list): 池化kernel的大小。
        %539 (list): 步长大小。
        %540 (list): 填充大小。
        %273 (bool): 是否用ceil函数计算输出高度和宽度。
        %272 (bool): 是否在平均池化模式不忽略填充值,False为忽略。
        %271 (int): 如果指定,它将用作除数,否则将使用池化区域的大小。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool2d", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.34
S
SunAhong1993 已提交
677 678
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
679 680 681 682
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%538
S
SunAhong1993 已提交
683
    layer_attrs["kernel_size"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
684
    # 处理输入2,即%539
S
SunAhong1993 已提交
685
    layer_attrs["stride"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
686
    # 处理输入3,即%540
S
SunAhong1993 已提交
687
    layer_attrs["padding"] = mapper.attrs[inputs_name[3]]
S
SunAhong1993 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    # 处理输入4,即%273
    layer_attrs["ceil_mode"] = mapper.attrs[inputs_name[4]]
    # 处理输入5,即%272
    layer_attrs["exclusive"] = not mapper.attrs[inputs_name[5]]
    # 处理输入6,即%271
    graph.add_layer(
        "prim.assert",
        inputs={},
        outputs=[inputs_name[6] + "_assert"],
        scope_name=scope_name if scope_name == "" else scope_name + "_assert",
        type="eq",
        key=mapper.attrs[inputs_name[6]],
        value=None)

    graph.add_layer(
S
SunAhong1993 已提交
703
        kernel="paddle.nn.AvgPool1D",
S
SunAhong1993 已提交
704
        inputs=layer_inputs,
S
SunAhong1993 已提交
705
        outputs=layer_outputs,
S
SunAhong1993 已提交
706
        scope_name=scope_name,
S
SunAhong1993 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
        **layer_attrs)
    return current_inputs, current_outputs


def aten_batch_norm(mapper, graph, node):
    """ 构造BatchNorm的PaddleLayer。
    TorchScript示例:
        %input.81 : Tensor = aten::batch_norm(%input.80, %778, %779, %776, %777, %780,
                                              %exponential_average_factor.23, %766, %781)
        参数含义:
        %input.81 (Tensor): 输出,批处理后的结果。
        %input.80 (Tensor): 需要进行批处理的特征层。
        %778 (Tensor): weights。
        %779 (Tensor): bias。
        %776 (Tensor): 全局均值。
        %777 (Tensor): 全局方差。
        %780 (bool): 是否训练。
        %exponential_average_factor.23 (float): 用于计算均值和方差的比例。
        %766 (float): 为了数值稳定加在分母上的值。
        %781 (bool): 是否启用cudnn。
    """
S
SunAhong1993 已提交
728 729
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("batchnorm", mapper.nn_name2id)
S
SunAhong1993 已提交
730
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
731
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
732 733 734 735 736 737 738
    layer_inputs = {}
    layer_attrs = {}
    layer_attrs["is_test"] = True
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.80
S
SunAhong1993 已提交
739 740
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
741 742 743 744 745
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%778
    weights = mapper.pytorch_params[inputs_name[1]]
S
SunAhong1993 已提交
746
    mapper.paddle_params[op_name + ".weight"] = weights
S
SunAhong1993 已提交
747 748 749 750 751
    layer_attrs['num_channels'] = weights.shape[0]
    # 处理输入2,即%779
    if inputs_name[2] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[2]]
        if bias is not None:
S
SunAhong1993 已提交
752
            mapper.paddle_params[op_name + ".bias"] = bias
S
SunAhong1993 已提交
753
    else:
S
SunAhong1993 已提交
754
        mapper.paddle_params[op_name + ".bias"] = False
S
SunAhong1993 已提交
755 756
    # 处理输入3,即%776
    mean = mapper.pytorch_params[inputs_name[3]]
S
SunAhong1993 已提交
757
    mapper.paddle_params[op_name + "._mean"] = mean
S
SunAhong1993 已提交
758 759
    # 处理输入4,即%777
    var = mapper.pytorch_params[inputs_name[4]]
S
SunAhong1993 已提交
760
    mapper.paddle_params[op_name + "._variance"] = var
S
SunAhong1993 已提交
761 762 763 764 765 766 767 768 769
    # 处理输入6,即%exponential_average_factor.23
    layer_attrs["momentum"] = mapper.attrs[inputs_name[6]]
    # 处理输入7,即%766
    layer_attrs["epsilon"] = mapper.attrs[inputs_name[7]]

    graph.add_layer(
        "paddle.nn.BatchNorm",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
770
        scope_name=scope_name,
S
SunAhong1993 已提交
771 772 773 774
        **layer_attrs)
    return current_inputs, current_outputs


W
WJJ1995 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
def aten_bitwise_not(mapper, graph, node):
    """ 构造矩阵相乘的PaddleLayer。
    TorchScript示例:
        %x.222 : Tensor = aten::bitwise_not(%32)
        参数含义:
        %x.222 (Tensor): 输出,逻辑非运算后的结果。
        %32 (Tensor): 输入1。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%32
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.not",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


def aten_bitwise_xor(mapper, graph, node):
    """ 构造矩阵相乘的PaddleLayer。
    TorchScript示例:
        %x.222 : Tensor = aten::bitwise_xor(%32, %8)
        参数含义:
        %x.222 (Tensor): 输出,逻辑或运算后的结果。
        %32 (Tensor): 输入1。
        %8 (Tensor): 输入2。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%32
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%8
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.or",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


def aten_bitwise_and(mapper, graph, node):
    """ 构造矩阵相乘的PaddleLayer。
    TorchScript示例:
        %x.222 : Tensor = aten::bitwise_and(%32, %8)
        参数含义:
        %x.222 (Tensor): 输出,逻辑与运算后的结果。
        %32 (Tensor): 输入1。
        %8 (Tensor): 输入2。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%32
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%8
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.and",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
def aten_bmm(mapper, graph, node):
    """ 构造矩阵相乘的PaddleLayer。
    TorchScript示例:
        %x.222 : Tensor = aten::bmm(%32, %7)
        参数含义:
        %x.222 (Tensor): 输出,矩阵相乘后的结果。
        %i.12 (list): 输入1。
        %7 (int): 输入2。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%i.12
S
SunAhong1993 已提交
892 893
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
894 895
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%288
S
SunAhong1993 已提交
896 897
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
898 899 900 901
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
902 903 904 905 906
    graph.add_layer(
        "paddle.bmm",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
907 908 909
    return current_inputs, current_outputs


S
SunAhong1993 已提交
910 911 912 913 914 915 916 917 918
def aten_cat(mapper, graph, node):
    """ 构造连接Tensor的PaddleLayer。
    TorchScript示例:
        %x.222 : Tensor = aten::cat(%32, %7)
        参数含义:
        %x.222 (Tensor): 输出,连接后的结果。
        %i.12 (list): 需要连接的Tensor组成的list。
        %7 (int): 连接的轴。
    """
S
SunAhong1993 已提交
919
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
920 921 922 923 924 925 926 927
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%13
S
SunAhong1993 已提交
928 929
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
930
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
931 932 933 934 935 936 937
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%12
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
938
                            current_outputs, scope_name)
S
SunAhong1993 已提交
939 940 941
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
S
SunAhong1993 已提交
942
        "paddle.concat",
S
SunAhong1993 已提交
943 944
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
945
        scope_name=scope_name,
S
SunAhong1993 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959
        **layer_attrs)
    return current_inputs, current_outputs


def aten_chunk(mapper, graph, node):
    """构造分割Tensor的PaddleLayer。
    TorchScript示例:
        %724 : Tensor[] = aten::chunk(%input.170, %720, %719)
        参数含义:
        %724 (Tensor): 输出,分割后的结果。
        %input.170 (Tensor): 需要进行分割的Tensor。
        %720 (int): 分割的块数。
        %719 (int): 分割的维度。
    """
S
SunAhong1993 已提交
960
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
961 962 963 964 965 966 967 968
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.170
S
SunAhong1993 已提交
969 970
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
971
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
972 973 974 975 976 977 978
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%720
    if inputs_name[1] in mapper.attrs:
        layer_attrs["num_or_sections"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
979
                            current_outputs, scope_name)
S
SunAhong1993 已提交
980 981 982 983
        layer_inputs["num_or_sections"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%719
    if inputs_name[2] in mapper.attrs:
S
SunAhong1993 已提交
984
        layer_attrs["axis"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
985 986
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
987 988
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[2]
S
SunAhong1993 已提交
989 990
        current_inputs.append(inputs_name[2])
    graph.add_layer(
S
SunAhong1993 已提交
991
        "paddle.split",
S
SunAhong1993 已提交
992 993
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
994
        scope_name=scope_name,
S
SunAhong1993 已提交
995 996 997 998
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
def aten_clamp(mapper, graph, node):
    """ 构造元素剪裁的PaddleLayer。
    TorchScript示例:
        %56 : Tensor = aten::clamp(%input.1, %46, %48, %49)
        参数含义:
        %56 (Tensor): 输出,累加后的结果。
        %input.1 (Tensor): 输入,需要剪裁的Tensor。
        %46 (float/Tensor): 最小值。
        %48 (float/Tensor): 最大值。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.1
S
SunAhong1993 已提交
1018 1019
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%46
    if inputs_name[1] in mapper.attrs:
        layer_attrs["min"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["min"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%48,代表dtype
    if inputs_name[2] in mapper.attrs:
        layer_attrs["max"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
        layer_inputs["max"] = inputs_name[2]
        current_inputs.append(inputs_name[2])

    graph.add_layer(
        "paddle.clip",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
def aten_clamp_min(mapper, graph, node):
    """ 构造元素剪裁的PaddleLayer。
    TorchScript示例:
        %56 : Tensor = aten::clamp_min(%input.1, %46)
        参数含义:
        %56 (Tensor): 输出,累加后的结果。
        %input.1 (Tensor): 输入,需要剪裁的Tensor。
        %46 (float/Tensor): 最小值。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.1
S
SunAhong1993 已提交
1067 1068
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%46
    if inputs_name[1] in mapper.attrs:
        layer_attrs["min"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["min"] = inputs_name[1]
        current_inputs.append(inputs_name[1])

    graph.add_layer(
        "paddle.clip",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


W
wjj19950828 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
def aten_complex(mapper, graph, node):
    """
    TorchScript示例:
        %ret.2 : Tensor = aten::complex(%150, %156)
        参数含义:
        %ret.2 (Tensor): complex结果Tensor。
        %150 (Tensor): 实部输入Tensor。
        %156 (Tensor): 虚部输入Tensor。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%150
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["real"] = inputs_name[0]
    # 处理输入1,即%156
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
    layer_inputs["imag"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.complex",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


W
WJJ1995 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
def aten_copy(mapper, graph, node):
    """
    TorchScript Code:
        %107 : Tensor = aten::copy(%new_mem.1)
        Parameter meaning:
        %107 (Tensor): Output Tensor
        %new_mem.1 (Tensor): Input Tensor
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # output list
    current_outputs = [output_name]
    # process Input Tensor
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["input"] = inputs_name[0]
    current_inputs = list(layer_inputs.values())
    graph.add_layer(
        "prim.equal",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)

    return current_inputs, current_outputs


S
SunAhong1993 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164
def aten___contains__(mapper, graph, node):
    """ 构造in的PaddleLayer。
    TorchScript示例:
        %51 : bool = aten::__contains__(%50, %name.1)
        参数含义:
        %51 (bool): 输出,第一个元素是否包含第二个元素。
        %50 (-): 需对比的输入1。
        %name.1 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
1165
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1166 1167 1168 1169 1170 1171 1172
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%50
S
SunAhong1993 已提交
1173 1174
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1175 1176
    layer_inputs["input"] = inputs_name[0]
    # 处理输入1,即%name.1
S
SunAhong1993 已提交
1177 1178
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1179 1180 1181 1182
    layer_inputs["element"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1183 1184 1185 1186 1187
    graph.add_layer(
        "prim.contain",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1188 1189 1190 1191
    return current_inputs, current_outputs


def aten_constant_pad_nd(mapper, graph, node):
W
wjj19950828 已提交
1192 1193
    """
    TorchScript Code:
S
SunAhong1993 已提交
1194
        %58 : Tensor = aten::constant_pad_nd(%input1.24, %4876, %42)
W
wjj19950828 已提交
1195 1196 1197 1198 1199
        Parameter meaning:
        %58 (Tensor): Output Tensor
        %input1.24 (Tensor): Input Tensor
        %4876 (list): pad
        %42 (-): value
S
SunAhong1993 已提交
1200
    """
S
SunAhong1993 已提交
1201 1202
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pad", mapper.nn_name2id)
S
SunAhong1993 已提交
1203 1204 1205 1206
    output_name = mapper._get_outputs_name(node)[0]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
W
wjj19950828 已提交
1207
    # Output list
S
SunAhong1993 已提交
1208
    current_outputs = [output_name]
W
wjj19950828 已提交
1209
    # process Input Tensor
S
SunAhong1993 已提交
1210 1211
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
W
wjj19950828 已提交
1212 1213
    # process pad
    padding_attr = None
1214
    if inputs_name[1] in mapper.attrs:
W
wjj19950828 已提交
1215
        padding_attr = mapper.attrs[inputs_name[1]]
1216
    else:
W
WJJ1995 已提交
1217 1218
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
1219
        layer_inputs["pad"] = inputs_name[1]
W
wjj19950828 已提交
1220
    # process value
S
SunAhong1993 已提交
1221
    layer_attrs["value"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
1222

W
wjj19950828 已提交
1223
    if padding_attr is not None:
W
wjj19950828 已提交
1224 1225 1226
        ## convert torch pad attr to paddle pad attr, eg:(x1,x2,x3,x4)->(x3,x4,x1,x2)
        padding_attr = np.array(padding_attr).reshape((-1, 2))
        padding_attr = np.flip(padding_attr, axis=0).flatten().tolist()
W
wjj19950828 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
        layer_inputs["x"] = inputs_name[0]
        kernel_name = "paddle.nn.functional.pad"
        if len(padding_attr) == 2:
            layer_attrs["pad"] = [0, 0, 0, 0, 0, 0] + padding_attr
        elif len(padding_attr) == 4:
            layer_attrs["pad"] = [0, 0, 0, 0] + padding_attr
        elif len(padding_attr) == 6:
            layer_attrs["pad"] = [0, 0] + padding_attr
        else:
            layer_attrs["pad"] = padding_attr
S
SunAhong1993 已提交
1237
        graph.add_layer(
W
wjj19950828 已提交
1238
            kernel_name,
S
SunAhong1993 已提交
1239
            inputs=layer_inputs,
W
wjj19950828 已提交
1240
            outputs=[output_name],
S
SunAhong1993 已提交
1241 1242
            scope_name=scope_name,
            **layer_attrs)
S
SunAhong1993 已提交
1243
    else:
W
wjj19950828 已提交
1244
        layer_inputs["input"] = inputs_name[0]
1245 1246 1247 1248 1249 1250
        graph.add_layer(
            "custom_layer:Pad",
            inputs=layer_inputs,
            outputs=[output_name],
            scope_name=scope_name,
            **layer_attrs)
W
wjj19950828 已提交
1251
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    return current_inputs, current_outputs


def aten_contiguous(mapper, graph, node):
    """ 构造在内存中连续存储的PaddleLayer。
    TorchScript示例:
        %x.7 : Tensor = aten::contiguous(%4058, %4046)
        参数含义:
        %x.7 (Tensor): 输出,在内存中连续存储的Tensor。
        %4058 (Tensor): 原始Tensor。
        %4046 (int): 存储的形式。
    【注意】Paddle中无此用法,所以此处翻译成赋值。
    """
S
SunAhong1993 已提交
1265
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1266 1267 1268 1269 1270 1271 1272
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%4058
S
SunAhong1993 已提交
1273 1274
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1275 1276 1277 1278
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1279 1280 1281 1282 1283
    graph.add_layer(
        "prim.equal",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
    return current_inputs, current_outputs


def aten_conv2d(mapper, graph, node):
    """ 构造conv2d的PaddleLayer。
    TorchScript示例:
        %input.10 : Tensor = aten::conv2d(%input.8, %25, %27, %28, %29, %30, %26)
        参数含义:
        %input.10 (Tensor): 输出,卷积后的结果。
        %input.8 (Tensor): 需要进行卷积的特征层。
        %25 (Tensor): weights。
        %27 (Tensor): bias。
        %28 (int): 步长大小。
        %29 (int): 填充大小。
S
SunAhong1993 已提交
1298
        %30 (int): 空洞大小。
S
SunAhong1993 已提交
1299 1300
        %26 (int): 卷积的组数。
    """
S
SunAhong1993 已提交
1301 1302
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("conv2d", mapper.nn_name2id)
S
SunAhong1993 已提交
1303
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
1304
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
1305 1306 1307 1308 1309 1310
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.8
S
SunAhong1993 已提交
1311 1312
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1313 1314 1315 1316 1317
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%25
    weights = mapper.pytorch_params[inputs_name[1]]
S
SunAhong1993 已提交
1318
    mapper.paddle_params[op_name + ".weight"] = weights
S
SunAhong1993 已提交
1319 1320 1321 1322 1323 1324
    layer_attrs["out_channels"] = weights.shape[0]
    layer_attrs["kernel_size"] = weights.shape[2:]
    # 处理输入2,即%27
    if inputs_name[2] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[2]]
        if bias is not None:
S
SunAhong1993 已提交
1325
            mapper.paddle_params[op_name + ".bias"] = bias
S
SunAhong1993 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
        else:
            layer_attrs["bias_attr"] = False
    else:
        layer_attrs["bias_attr"] = False
    # 处理输入3,即%28
    layer_attrs["stride"] = mapper.attrs[inputs_name[3]]
    # 处理输入4,即%29
    layer_attrs["padding"] = mapper.attrs[inputs_name[4]]
    # 处理输入5,即%30
    layer_attrs["dilation"] = mapper.attrs[inputs_name[5]]
    # 处理输入6,即%26
    layer_attrs["groups"] = mapper.attrs[inputs_name[6]]
    layer_attrs['in_channels'] = weights.shape[1] * mapper.attrs[inputs_name[6]]

    graph.add_layer(
S
SunAhong1993 已提交
1341
        "paddle.nn.Conv2D",
S
SunAhong1993 已提交
1342 1343
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
1344
        scope_name=scope_name,
S
SunAhong1993 已提交
1345 1346 1347 1348 1349 1350 1351
        **layer_attrs)
    return current_inputs, current_outputs


def aten__convolution(mapper, graph, node):
    """ 构造conv2d的PaddleLayer。
    TorchScript示例:
S
SunAhong1993 已提交
1352
        %input.10 : Tensor = aten::_convolution(%input.1, %18, %10, %19, %20, %21, %13, %22, %12, %13, %13, %15)
S
SunAhong1993 已提交
1353 1354 1355
        参数含义:
        %input.10 (Tensor): 输出,卷积后的结果。
        %input.8 (Tensor): 需要进行卷积的特征层。
S
SunAhong1993 已提交
1356 1357 1358 1359
        %18 (Tensor): weights。
        %10 (Tensor): bias。
        %19 (list): 步长大小。
        %20 (list): 填充大小。
S
SunAhong1993 已提交
1360
        %21 (list): 空洞大小。
S
SunAhong1993 已提交
1361 1362 1363
        %13 (bool): 是否进行转置卷积。
        %22 (list): 输出形状上一侧额外添加的大小。
        %12 (int): 卷积的组数。
S
SunAhong1993 已提交
1364
    """
S
SunAhong1993 已提交
1365
    scope_name = mapper.normalize_scope_name(node)
W
WJJ1995 已提交
1366 1367 1368 1369
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    weights = mapper.pytorch_params[inputs_name[1]]
    if len(weights.shape) == 3:
        op_name = name_generator("conv1d", mapper.nn_name2id)
W
wjj19950828 已提交
1370
    elif len(weights.shape) == 4:
W
WJJ1995 已提交
1371
        op_name = name_generator("conv2d", mapper.nn_name2id)
W
wjj19950828 已提交
1372 1373
    else:
        op_name = name_generator("conv3d", mapper.nn_name2id)
S
SunAhong1993 已提交
1374
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
1375
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
1376 1377 1378 1379 1380
    layer_inputs = {}
    layer_attrs = {}
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.8
S
SunAhong1993 已提交
1381 1382
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1383 1384 1385
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1386
    # 处理输入1,即%18
S
SunAhong1993 已提交
1387 1388
    mapper.paddle_params[op_name +
                         ".weight"] = weights  #np.swapaxes(weights, 0, 1)
S
SunAhong1993 已提交
1389 1390 1391 1392
    if mapper.attrs[inputs_name[6]]:
        layer_attrs["out_channels"] = weights.shape[1]
    else:
        layer_attrs["out_channels"] = weights.shape[0]
S
SunAhong1993 已提交
1393
    layer_attrs["kernel_size"] = weights.shape[2:]
S
SunAhong1993 已提交
1394
    # 处理输入2,即%10
S
SunAhong1993 已提交
1395 1396 1397
    if inputs_name[2] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[2]]
        if bias is not None:
S
SunAhong1993 已提交
1398
            mapper.paddle_params[op_name + ".bias"] = bias
S
SunAhong1993 已提交
1399 1400 1401 1402
        else:
            layer_attrs["bias_attr"] = False
    else:
        layer_attrs["bias_attr"] = False
S
SunAhong1993 已提交
1403
    # 处理输入3,即%19
S
SunAhong1993 已提交
1404
    layer_attrs["stride"] = mapper.attrs[inputs_name[3]]
S
SunAhong1993 已提交
1405
    # 处理输入4,即%20
S
SunAhong1993 已提交
1406
    layer_attrs["padding"] = mapper.attrs[inputs_name[4]]
S
SunAhong1993 已提交
1407
    # 处理输入5,即%21
S
SunAhong1993 已提交
1408
    layer_attrs["dilation"] = mapper.attrs[inputs_name[5]]
S
SunAhong1993 已提交
1409 1410 1411 1412 1413 1414
    # 处理输入6,即%13
    if mapper.attrs[inputs_name[6]]:
        # 处理输入7,即%22
        layer_attrs["output_padding"] = mapper.attrs[inputs_name[7]]
    # 处理输入8,即%12
    layer_attrs["groups"] = mapper.attrs[inputs_name[8]]
S
SunAhong1993 已提交
1415
    if mapper.attrs[inputs_name[6]]:
S
SunAhong1993 已提交
1416 1417
        layer_attrs['in_channels'] = weights.shape[0] * mapper.attrs[
            inputs_name[8]]
S
SunAhong1993 已提交
1418
    else:
S
SunAhong1993 已提交
1419 1420
        layer_attrs['in_channels'] = weights.shape[1] * mapper.attrs[
            inputs_name[8]]
W
wjj19950828 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
    if len(weights.shape) == 3:
        if mapper.attrs[inputs_name[6]]:
            graph.add_layer(
                "paddle.nn.Conv1DTranspose",
                inputs=layer_inputs,
                outputs=layer_outputs,
                scope_name=scope_name,
                **layer_attrs)
        else:
            graph.add_layer(
                "paddle.nn.Conv1D",
                inputs=layer_inputs,
                outputs=layer_outputs,
                scope_name=scope_name,
                **layer_attrs)
    elif len(weights.shape) == 4:
W
WJJ1995 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
        if mapper.attrs[inputs_name[6]]:
            graph.add_layer(
                "paddle.nn.Conv2DTranspose",
                inputs=layer_inputs,
                outputs=layer_outputs,
                scope_name=scope_name,
                **layer_attrs)
        else:
            graph.add_layer(
                "paddle.nn.Conv2D",
                inputs=layer_inputs,
                outputs=layer_outputs,
                scope_name=scope_name,
                **layer_attrs)
S
SunAhong1993 已提交
1451
    else:
W
WJJ1995 已提交
1452 1453
        if mapper.attrs[inputs_name[6]]:
            graph.add_layer(
W
wjj19950828 已提交
1454
                "paddle.nn.Conv3DTranspose",
W
WJJ1995 已提交
1455 1456 1457 1458 1459 1460
                inputs=layer_inputs,
                outputs=layer_outputs,
                scope_name=scope_name,
                **layer_attrs)
        else:
            graph.add_layer(
W
wjj19950828 已提交
1461
                "paddle.nn.Conv3D",
W
WJJ1995 已提交
1462 1463 1464 1465
                inputs=layer_inputs,
                outputs=layer_outputs,
                scope_name=scope_name,
                **layer_attrs)
S
SunAhong1993 已提交
1466 1467 1468
    return current_inputs, current_outputs


S
SunAhong1993 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
def aten_conv_transpose2d(mapper, graph, node):
    """ 构造conv_transpose2d的PaddleLayer。
    TorchScript示例:
        %input.10 : Tensor = aten::conv_transpose2d(%input.1, %18, %10, %19, %20, %21, %13, %22)
        参数含义:
        %input.10 (Tensor): 输出,卷积后的结果。
        %input.8 (Tensor): 需要进行卷积的特征层。
        %18 (Tensor): weights。
        %10 (Tensor): bias。
        %19 (list): 步长大小。
        %20 (list): 填充大小。
        %21 (int/tuple): 输出形状上一侧额外添加的大小。
        %13 (int): 二维卷积层的组数。
        %22 (int/tuple): 空洞大小。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("conv2d", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.8
S
SunAhong1993 已提交
1494 1495
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%18
    weights = mapper.pytorch_params[inputs_name[1]]
    mapper.paddle_params[op_name + ".weight"] = weights
    layer_attrs["out_channels"] = weights.shape[1]
    layer_attrs["kernel_size"] = weights.shape[2:]
    # 处理输入2,即%10
    if inputs_name[2] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[2]]
        if bias is not None:
            mapper.paddle_params[op_name + ".bias"] = bias
        else:
            layer_attrs["bias_attr"] = False
    else:
        layer_attrs["bias_attr"] = False
    # 处理输入3,即%19
    layer_attrs["stride"] = mapper.attrs[inputs_name[3]]
    # 处理输入4,即%20
    layer_attrs["padding"] = mapper.attrs[inputs_name[4]]
    # 处理输入5,即%21
    layer_attrs["output_padding"] = mapper.attrs[inputs_name[5]]
    # 处理输入6,即%13
    layer_attrs["groups"] = mapper.attrs[inputs_name[6]]
    # 处理输入7,即%22
    layer_attrs["dilation"] = mapper.attrs[inputs_name[7]]
S
SunAhong1993 已提交
1523
    layer_attrs['in_channels'] = weights.shape[0] * mapper.attrs[inputs_name[6]]
S
SunAhong1993 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532
    graph.add_layer(
        "paddle.nn.Conv2DTranspose",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
1533 1534 1535 1536 1537 1538 1539 1540
def aten_cos(mapper, graph, node):
    """ 构造数学计算cos的PaddleLayer。
    TorchScript示例:
        %94 : Tensor = aten::cos(%sinusoid_inp.1)
        参数含义:
        %94 (Tensor): 输出,cos之后的结果。
        %sinusoid_inp.1 (Tensor): 需要进行shape的Tensor。
    """
S
SunAhong1993 已提交
1541
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1542 1543 1544 1545 1546 1547 1548
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%sinusoid_inp.1
S
SunAhong1993 已提交
1549 1550
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1551 1552 1553 1554
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1555 1556 1557 1558 1559
    graph.add_layer(
        "paddle.cos",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
    return current_inputs, current_outputs


def aten_cumsum(mapper, graph, node):
    """ 构造与前一个元素累加的PaddleLayer。
    TorchScript示例:
        %56 : Tensor = aten::cumsum(%mask.1, %46, %48)
        参数含义:
        %56 (Tensor): 输出,累加后的结果。
        %mask.1 (Tensor): 输入,需要累加的Tensor。
        %46 (int): 累加的维度。
        %48 (int/None): Tensor的类型。
    """
S
SunAhong1993 已提交
1573
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1574 1575 1576 1577 1578 1579 1580 1581
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%mask.1
S
SunAhong1993 已提交
1582 1583
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1584 1585 1586 1587 1588 1589 1590 1591
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%46
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
1592
                            current_outputs, scope_name)
S
SunAhong1993 已提交
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入1,即%48,代表dtype
    if mapper.attrs[inputs_name[2]] is None:
        layer_attrs["dtype"] = None
    else:
        layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[2]]]

    graph.add_layer(
        "paddle.cumsum",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
1605
        scope_name=scope_name,
S
SunAhong1993 已提交
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
        **layer_attrs)
    return current_inputs, current_outputs


def aten_detach(mapper, graph, node):
    """ 构造返回一个新的Tensor,从当前计算图中分离下来的,但是仍指向原变量的存放位置的PaddleLayer。
    TorchScript示例:
        %107 : Tensor = aten::detach(%new_mem.1)
        参数含义:
        %107 (Tensor): 输出,得到的Scalar。
        %new_mem.1 (Tensor): 输入。
    【注意】由于Paddle无此操作,所以此处制转换为赋值。
    """
S
SunAhong1993 已提交
1619
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1620 1621 1622 1623 1624 1625 1626
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%end.1
S
SunAhong1993 已提交
1627 1628
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1629 1630 1631
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1632 1633 1634 1635 1636
    graph.add_layer(
        "prim.equal",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

    return current_inputs, current_outputs


def aten_dict(mapper, graph, node):
    """ 构造初始化dict的PaddleLayer。
    TorchScript示例:
        %features.1 : Dict(str, Tensor) = aten::dict()
        参数含义:
        %features.1: 输出,初始化的dict。
    """
S
SunAhong1993 已提交
1648
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1649 1650 1651 1652 1653 1654 1655
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    current_inputs = {}
    # 获取当前节点输出的list
    current_outputs = [output_name]

S
SunAhong1993 已提交
1656 1657 1658 1659 1660
    graph.add_layer(
        "prim.dict",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
    return current_inputs, current_outputs


def aten_dim(mapper, graph, node):
    """ 构造获取维度的PaddleLayer。
    TorchScript示例:
        %106 : int = aten::dim(%101)
        参数含义:
        %106 (int): 输出,Tensor的维度。
        %101 (Tensor): 输入的Tensor。
    """
S
SunAhong1993 已提交
1672
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1673 1674 1675 1676 1677 1678
    output_name = mapper._get_outputs_name(node)[0]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.8
S
SunAhong1993 已提交
1679 1680
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1681
    layer_inputs["input"] = inputs_name[0]
S
SunAhong1993 已提交
1682 1683 1684 1685
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
1686 1687 1688 1689
        "prim.shape",
        inputs=layer_inputs,
        outputs=[output_name],
        scope_name=scope_name)
S
SunAhong1993 已提交
1690
    graph.add_layer(
S
SunAhong1993 已提交
1691 1692 1693 1694
        "prim.len",
        inputs={"input": output_name},
        outputs=[output_name],
        scope_name=scope_name)
S
SunAhong1993 已提交
1695 1696 1697 1698 1699 1700
    return current_inputs, current_outputs


def aten_div(mapper, graph, node):
    """ 构造除法的PaddleLayer。
    TorchScript示例:
W
WJJ1995 已提交
1701
        %bx_bw0.3 : Tensor = aten::div(%bx_bw.3, %2678)
S
SunAhong1993 已提交
1702 1703 1704 1705 1706
        参数含义:
        %bx_bw0.3 (-): 除后的结果。
        %bx_bw.3 (-): 被除数。
        %2678 (int): 除数。
    """
S
SunAhong1993 已提交
1707
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1708 1709 1710 1711 1712 1713 1714
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%124
S
SunAhong1993 已提交
1715 1716
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1717 1718
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
1719 1720
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1721 1722 1723 1724
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1725 1726 1727 1728 1729
    graph.add_layer(
        "prim.div",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
    return current_inputs, current_outputs


def aten_dropout(mapper, graph, node):
    """ 构造Dropout的PaddleLayer。
    TorchScript示例:
        %119 : Tensor = aten::dropout(%result.3, %117, %118)
        参数含义:
        %119 (Tensor): Dropout后的Tensor。
        %result.3 (Tensor): 输入Tensor。
        %118 (bool): 是否是训练阶段。
    """
S
SunAhong1993 已提交
1742 1743
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("dropout", mapper.nn_name2id)
S
SunAhong1993 已提交
1744
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
1745
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
1746 1747 1748 1749 1750
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%119
S
SunAhong1993 已提交
1751 1752
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1753 1754 1755 1756 1757
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
1758 1759 1760 1761 1762
        "paddle.nn.Dropout",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        p=0.0)
S
SunAhong1993 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
    return current_inputs, current_outputs


def aten_embedding(mapper, graph, node):
    """ 构造embedding的PaddleLayer。
    TorchScript示例:
        %inputs_embeds.1 : Tensor = aten::embedding(%57, %input_ids.1, %45, %46, %46)
        参数含义:
        %inputs_embeds.1 (Tensor): 输出,embedding后的结果。
        %57 (Tensor): weights。
        %input_ids.1 (Tensor): 需要进行embedding的特征层。
        %45 (int): padding_idx。
        %46 (bool): scale_grad_by_freq。
        %46 (bool): sparse。
    """
S
SunAhong1993 已提交
1778 1779
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("embedding", mapper.nn_name2id)
S
SunAhong1993 已提交
1780
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
1781
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
1782 1783 1784 1785 1786 1787 1788
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%57
    weights = mapper.pytorch_params[inputs_name[0]]
S
SunAhong1993 已提交
1789 1790 1791
    mapper.paddle_params[op_name + ".weight"] = weights
    layer_attrs["num_embeddings"] = weights.shape[0]
    layer_attrs["embedding_dim"] = weights.shape[1]
S
SunAhong1993 已提交
1792
    # 处理输入1,即%input_ids.1
S
SunAhong1993 已提交
1793 1794
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803
    layer_inputs["input"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入2,即%45
    if mapper.attrs[inputs_name[2]] == -1:
        layer_attrs["padding_idx"] = None
    else:
        layer_attrs["padding_idx"] = mapper.attrs[inputs_name[2]]
    # 处理输入4,即%46
S
SunAhong1993 已提交
1804
    layer_attrs["sparse"] = mapper.attrs[inputs_name[4]]
S
SunAhong1993 已提交
1805 1806

    graph.add_layer(
S
SunAhong1993 已提交
1807
        "paddle.nn.Embedding",
S
SunAhong1993 已提交
1808 1809
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
1810
        scope_name=scope_name,
S
SunAhong1993 已提交
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
        **layer_attrs)
    return current_inputs, current_outputs


def aten_eq(mapper, graph, node):
    """ 构造判断数值是否相等的PaddleLayer。
    TorchScript示例:
        %125 : bool = aten::eq(%124, %123)
        参数含义:
        %125 (bool): 对比后结果。
        %124 (-): 需对比的输入1。
        %123 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
1824
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1825 1826 1827 1828 1829 1830 1831
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%124
S
SunAhong1993 已提交
1832 1833
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1834 1835 1836 1837
    layer_inputs["x"] = inputs_name[0]
    x_value = list(node.inputs())[0]
    x_type = x_value.type()
    # 处理输入1,即%123
S
SunAhong1993 已提交
1838 1839
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1840 1841 1842 1843 1844
    layer_inputs["y"] = inputs_name[1]
    y_value = list(node.inputs())[1]
    y_type = y_value.type()
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1845 1846 1847 1848 1849
    graph.add_layer(
        "prim.eq",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1850 1851 1852
    return current_inputs, current_outputs


S
SunAhong1993 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
def aten_erf(mapper, graph, node):
    """ 构造逐元素计算 Erf 激活函数的PaddleLayer。
    TorchScript示例:
        %94 : Tensor = aten::erf(%sinusoid_inp.1)
        参数含义:
        %94 (Tensor): 输出,erf之后的结果。
        %sinusoid_inp.1 (Tensor): 需要进行erf的Tensor。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%sinusoid_inp.1
S
SunAhong1993 已提交
1869 1870
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1871 1872 1873 1874
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1875 1876 1877 1878 1879
    graph.add_layer(
        "paddle.erf",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1880 1881 1882
    return current_inputs, current_outputs


S
SunAhong1993 已提交
1883 1884 1885 1886 1887 1888 1889 1890
def aten_exp(mapper, graph, node):
    """ 构造以自然数e为底指数运算的PaddleLayer。
    TorchScript示例:
        %55 : Tensor = aten::tanh(%54)
        参数含义:
        %55 (Tensor): 输出,运算后的结果。
        %54 (Tensor): 需要指数运算的Tensor。
    """
S
SunAhong1993 已提交
1891
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1892 1893 1894 1895 1896 1897 1898
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
1899 1900
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1901 1902 1903 1904 1905
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
1906 1907 1908 1909
        "paddle.exp",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
    return current_inputs, current_outputs


def aten_expand(mapper, graph, node):
    """ 构造对某维度进行广播的PaddleLayer。
    TorchScript示例:
        %1889 : Tensor = aten::expand(%1875, %1888, %1567)
        参数含义:
        %1889 (Tensor): 广播后的结果。
        %1875 (Tensor): 需要广播的Tensor。
        %1888 (int): 广播的维度。
        %1567 (bool): 未使用。
    """
S
SunAhong1993 已提交
1923
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1924 1925 1926
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
S
SunAhong1993 已提交
1927
    layer_attrs = {}
S
SunAhong1993 已提交
1928 1929 1930 1931
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%1875
S
SunAhong1993 已提交
1932 1933
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1934
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%51
    if inputs_name[1] in mapper.attrs:
        layer_attrs["shape"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["shape"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
S
SunAhong1993 已提交
1944
    graph.add_layer(
S
SunAhong1993 已提交
1945 1946 1947
        "paddle.expand",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
1948
        scope_name=scope_name,
S
SunAhong1993 已提交
1949
        **layer_attrs)
S
SunAhong1993 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
    return current_inputs, current_outputs


def aten_expand_as(mapper, graph, node):
    """ 构造广播的PaddleLayer。
    TorchScript示例:
        %1889 : Tensor = aten::expand_as(%1875, %1888)
        参数含义:
        %1889 (Tensor): 广播后的结果。
        %1875 (Tensor): 需要广播的Tensor。
        %1888 (Tensor): 广播的示例。
    """
S
SunAhong1993 已提交
1962
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1963 1964 1965 1966 1967 1968 1969
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%1875
S
SunAhong1993 已提交
1970 1971
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1972 1973
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%1888
S
SunAhong1993 已提交
1974 1975
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
1976
    layer_inputs["y"] = inputs_name[1]
S
SunAhong1993 已提交
1977 1978
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1979

S
SunAhong1993 已提交
1980 1981 1982
    graph.add_layer(
        "prim.type",
        inputs={"input": inputs_name[0]},
S
SunAhong1993 已提交
1983 1984
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
1985 1986 1987 1988
    graph.add_layer(
        "prim.eq",
        inputs={"x": inputs_name[0] + "_type"},
        outputs=[inputs_name[0] + "_cond"],
S
SunAhong1993 已提交
1989
        scope_name=scope_name,
S
SunAhong1993 已提交
1990
        y=paddle_dtypes.t_bool)
S
SunAhong1993 已提交
1991 1992
    graph.add_layer(
        "prim.if", {'input': inputs_name[0] + "_cond"},
W
WJJ1995 已提交
1993
        outputs=[inputs_name[0] + "_if1", inputs_name[0]],
S
SunAhong1993 已提交
1994
        scope_name=scope_name)
S
SunAhong1993 已提交
1995
    if_layer = graph.layers[list(graph.layers.keys())[-1]]
W
WJJ1995 已提交
1996
    block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
1997 1998 1999
    block.add_layer(
        "prim.type",
        inputs={"input": inputs_name[1]},
S
SunAhong1993 已提交
2000 2001
        outputs=[inputs_name[1] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2002
    block.add_layer(
S
SunAhong1993 已提交
2003
        "paddle.cast",
S
SunAhong1993 已提交
2004 2005
        inputs={"x": inputs_name[0]},
        outputs=[inputs_name[0]],
S
SunAhong1993 已提交
2006
        scope_name=scope_name,
S
SunAhong1993 已提交
2007 2008
        dtype=inputs_name[1] + "_type")
    if_layer.add_block(block)
W
WJJ1995 已提交
2009
    block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
2010 2011 2012 2013
    if_layer.add_block(block)
    if_layer.inputs["input-0"] = inputs_name[0]
    if_layer.inputs["input-1"] = inputs_name[1]
    graph.add_layer(
S
SunAhong1993 已提交
2014 2015 2016 2017
        "paddle.expand_as",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2018 2019
    graph.add_layer(
        "prim.if", {'input': inputs_name[0] + "_cond"},
W
WJJ1995 已提交
2020
        outputs=[inputs_name[0] + "_if2", output_name],
S
SunAhong1993 已提交
2021
        scope_name=scope_name)
S
SunAhong1993 已提交
2022
    if_layer = graph.layers[list(graph.layers.keys())[-1]]
W
WJJ1995 已提交
2023
    block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
2024
    block.add_layer(
S
SunAhong1993 已提交
2025
        "paddle.cast",
S
SunAhong1993 已提交
2026
        inputs={"x": layer_outputs[0]},
S
SunAhong1993 已提交
2027 2028
        outputs=copy.deepcopy(layer_outputs),
        scope_name=scope_name,
S
SunAhong1993 已提交
2029 2030
        dtype=string("bool"))
    if_layer.add_block(block)
W
WJJ1995 已提交
2031
    block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
2032 2033
    if_layer.add_block(block)
    if_layer.inputs["input-0"] = layer_outputs[0]
S
SunAhong1993 已提交
2034
    # TODO(syf): check expand_as
S
SunAhong1993 已提交
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
    #     # 处理输入0,即%1875
    #     mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    #     layer_inputs["x"] = inputs_name[0]
    #     # 处理输入1,即%1888
    #     mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
    #     layer_inputs["y"] = inputs_name[1]
    #     # 获取当前节点输入的list
    #     current_inputs = list(layer_inputs.values())
    #     graph.add_layer(
    #         "paddle.expand_as", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
    return current_inputs, current_outputs


def aten_eye(mapper, graph, node):
    """ 构造批次二维矩阵的PaddleLayer。
    TorchScript示例:
        %68 : Tensor = aten::eye(%49, %_50, %_51, %15, %9, %67, %7)
        参数含义:
        %68 (Tensor): 输出,构造的矩阵。
        %49 (int): 行数。
        %_50 (int): 列数,非必须。
        %_51 (Tensor): 非必须。
        %9 (int): layout。
        %67 (str): 设备。
        %7 (bool): 是否计算梯度。
    """
S
SunAhong1993 已提交
2061
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2062 2063 2064 2065 2066 2067 2068 2069
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%49
S
SunAhong1993 已提交
2070 2071
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2072 2073 2074 2075
    layer_inputs["num_rows"] = inputs_name[0]
    if len(inputs_name) > 5:
        # 处理输入1,即%_50
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
2076
                            current_outputs, scope_name)
S
SunAhong1993 已提交
2077 2078 2079 2080 2081 2082 2083
        layer_inputs["num_columns"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理倒数第4个输入,即%15
    layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[-4]]]

    graph.add_layer(
S
SunAhong1993 已提交
2084
        "paddle.eye",
S
SunAhong1993 已提交
2085 2086
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
2087
        scope_name=scope_name,
S
SunAhong1993 已提交
2088 2089 2090
        **layer_attrs)
    return current_inputs, current_outputs

S
SunAhong1993 已提交
2091

S
SunAhong1993 已提交
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
def aten_feature_dropout(mapper, graph, node):
    """ 构造Dropout的PaddleLayer。
    TorchScript示例:
        %119 : Tensor = aten::feature_dropout(%result.3, %117, %118)
        参数含义:
        %119 (Tensor): Dropout后的Tensor。
        %result.3 (Tensor): 输入Tensor。
        %118 (bool): 是否是训练阶段。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("dropout", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%119
S
SunAhong1993 已提交
2110 2111
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2112 2113 2114 2115 2116
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
2117 2118 2119 2120 2121
        "paddle.nn.Dropout",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        p=0.0)
S
SunAhong1993 已提交
2122 2123
    return current_inputs, current_outputs

S
SunAhong1993 已提交
2124

W
wjj19950828 已提交
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 2167 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
def aten_fft_rfftn(mapper, graph, node):
    """
    TorchScript示例:
        %x_gap.15 : Tensor =  aten::fft_rfftn(%x.58, %2166, %1450, %1453)
        参数含义:
        %x_gap.15 (Tensor): Output Tensor。
        %x.58 (Tensor): Input Tensor。
        %2166:Sequence Length
        %1450:axes
        %1453:norm mode
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%n.3
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    if inputs_name[1] in mapper.attrs:
        layer_attrs["s"] = mapper.attrs[inputs_name[1]]
    if inputs_name[2] in mapper.attrs:
        layer_attrs["axes"] = mapper.attrs[inputs_name[2]]
    if inputs_name[3] in mapper.attrs:
        layer_attrs["norm"] = mapper.attrs[inputs_name[3]]
    graph.add_layer(
        "paddle.fft.rfftn",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


def aten_fft_irfftn(mapper, graph, node):
    """
    TorchScript示例:
        %x_gap.15 : Tensor =  aten::fft_irfftn(%x.58, %2166, %1450, %1453)
        参数含义:
        %x_gap.15 (Tensor): Output Tensor。
        %x.58 (Tensor): Input Tensor。
        %2166:Sequence Length
        %1450:axes
        %1453:norm mode
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%n.3
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    if inputs_name[1] in mapper.attrs:
        layer_attrs["s"] = mapper.attrs[inputs_name[1]]
    if inputs_name[2] in mapper.attrs:
        layer_attrs["axes"] = mapper.attrs[inputs_name[2]]
    if inputs_name[3] in mapper.attrs:
        layer_attrs["norm"] = mapper.attrs[inputs_name[3]]
    graph.add_layer(
        "paddle.fft.irfftn",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
def aten_flatten(mapper, graph, node):
    """ 构造flatten的PaddleLayer。
    TorchScript示例:
        %x.8 : Tensor = aten::flatten(%x, %4, %2)
        参数含义:
        %x.8 (Tensor): flatten后结果。
        %x (Tensor): 输入Tensor。
        %4 (int): flatten的开始维度。
        %2 (int): flatten的结束维度。
    """
S
SunAhong1993 已提交
2215
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2216 2217 2218
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
S
SunAhong1993 已提交
2219
    layer_attrs = {}
S
SunAhong1993 已提交
2220 2221 2222 2223
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x
S
SunAhong1993 已提交
2224 2225
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2226 2227 2228 2229
    # 处理输入1,即%4
    layer_attrs["start_axis"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%20
    layer_attrs["stop_axis"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
2230 2231 2232 2233 2234
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
2235
        "paddle.flatten",
S
SunAhong1993 已提交
2236 2237
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
2238 2239
        scope_name=scope_name,
        **layer_attrs)
S
SunAhong1993 已提交
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
    return current_inputs, current_outputs


def aten_Float(mapper, graph, node):
    """ 构造取浮点型的PaddleLayer。
    TorchScript示例:
        %3992 : float = aten::Float(%3991)
        参数含义:
        %3992 (int): 向上取整后的整数。
        %3991 (float): 需要取整的浮点数。
    """
S
SunAhong1993 已提交
2251
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2252 2253 2254 2255 2256 2257 2258
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%3991
S
SunAhong1993 已提交
2259 2260
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2261 2262 2263 2264
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2265 2266 2267 2268 2269
    graph.add_layer(
        "prim.float",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
    return current_inputs, current_outputs


def aten_floor(mapper, graph, node):
    """ 构造向上取整的PaddleLayer。
    TorchScript示例:
        %3978 : int = aten::floor(%scale.18)
        参数含义:
        %3978 (int): 向上取整后的整数。
        %scale.18 (float): 需要取整的浮点数。
    """
S
SunAhong1993 已提交
2281
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2282 2283 2284 2285 2286 2287 2288
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%scale.18
S
SunAhong1993 已提交
2289 2290
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2291
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
2292 2293
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
2294
    graph.add_layer(
S
SunAhong1993 已提交
2295
        "prim.type", {'input': inputs_name[0]},
S
SunAhong1993 已提交
2296 2297 2298
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
    graph.add_layer(
S
SunAhong1993 已提交
2299
        "prim.str", {'input': inputs_name[0] + "_type"},
S
SunAhong1993 已提交
2300 2301 2302
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
    graph.add_layer(
S
SunAhong1993 已提交
2303 2304
        "prim.eq",
        inputs={"x": inputs_name[0] + "_type"},
S
SunAhong1993 已提交
2305 2306
        outputs=[inputs_name[0] + "_cond"],
        scope_name=scope_name,
S
SunAhong1993 已提交
2307
        y=paddle_dtypes.t_bool)
S
SunAhong1993 已提交
2308
    graph.add_layer(
S
SunAhong1993 已提交
2309
        "prim.if", {'input': inputs_name[0] + "_cond"},
S
SunAhong1993 已提交
2310 2311 2312
        outputs=[inputs_name[0] + "_if"],
        scope_name=scope_name)
    if_layer = graph.layers[list(graph.layers.keys())[-1]]
W
WJJ1995 已提交
2313
    block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
2314 2315 2316 2317 2318
    block.add_layer(
        "paddle.floor",
        inputs=copy.deepcopy(layer_inputs),
        outputs=copy.deepcopy(layer_outputs),
        scope_name=scope_name)
S
SunAhong1993 已提交
2319
    if_layer.add_block(block)
W
WJJ1995 已提交
2320
    block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
2321 2322 2323 2324 2325
    block.add_layer(
        "prim.floor",
        inputs=copy.deepcopy(layer_inputs),
        outputs=copy.deepcopy(layer_outputs),
        scope_name=scope_name)
S
SunAhong1993 已提交
2326 2327 2328
    if_layer.add_block(block)
    if_layer.inputs["input-0"] = inputs_name[0]
    if_layer.outputs.append(output_name)
S
SunAhong1993 已提交
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
    return current_inputs, current_outputs


def aten_floordiv(mapper, graph, node):
    """ 构造向上取整除法的PaddleLayer。
    TorchScript示例:
        %channels_per_group.2 : int = aten::floordiv(%num_channels.2, %3690)
        参数含义:
        %channels_per_group.2 (-): 除后的结果。
        %num_channels.2 (-): 被除数。
        %2 (int): 除数。
    """
S
SunAhong1993 已提交
2341
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2342 2343 2344 2345 2346 2347 2348
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%124
S
SunAhong1993 已提交
2349 2350
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2351 2352
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
2353 2354
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2355 2356 2357 2358
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2359 2360 2361 2362 2363
    graph.add_layer(
        "prim.floordiv",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
    return current_inputs, current_outputs


def aten_floor_divide(mapper, graph, node):
    """ 构造向上取整除法的PaddleLayer。
    TorchScript示例:
        %channels_per_group.2 : int = aten::floor_divide(%num_channels.2, %3690)
        参数含义:
        %channels_per_group.2 (-): 除后的结果。
        %num_channels.2 (-): 被除数。
        %2 (int): 除数。
    """
S
SunAhong1993 已提交
2376
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2377 2378 2379 2380 2381 2382 2383
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%124
S
SunAhong1993 已提交
2384 2385
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2386 2387
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
2388 2389
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2390 2391 2392 2393
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2394 2395 2396 2397 2398
    graph.add_layer(
        "prim.floordiv",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2399 2400 2401
    return current_inputs, current_outputs


2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
def aten_format(mapper, graph, node):
    """ 构造取浮点型的PaddleLayer。
    TorchScript示例:
        %628 : str = aten::format(%8, %627)
        参数含义:
        %628 (str): 输出,为一个字符串
        %8 (str): 输入字符串
        %627 (-): format后的参数
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入
    for i in range(len(inputs_node)):
        mapper._check_input(graph, inputs_node[i], inputs_name[i],
                            current_outputs, scope_name)
        layer_inputs["input" + str(i)] = inputs_name[i]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.format",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


W
wjj19950828 已提交
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
def aten_full(mapper, graph, node):
    """
    TorchScript Code:
        %159 : Tensor = aten::full(%775, %50, %49, %56, %48, %53)
        Parameter meaning:
        %159 (Tensor): Output Tensor
        %775 (Tensor): size
        %50 (int/float/bool): fill_value
        %49 (int): dtype
        %56 (int): layout
        %48 (int): device
        %53 (bool): requires_grad
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # output list
    current_outputs = [output_name]
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["shape"] = inputs_name[0]
    # input list
    current_inputs = list(layer_inputs.values())

    if inputs_name[1] in mapper.attrs:
        layer_attrs["fill_value"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["fill_value"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # dtype
    if mapper.attrs[inputs_name[2]] is not None:
        layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[2]]]

    graph.add_layer(
        "paddle.full",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
def aten_full_like(mapper, graph, node):
    """ 构造创建一个与输入具有相同的形状并且数据类型固定的Tensor的PaddleLayer。
    TorchScript示例:
        %159 : Tensor = aten::full_like(%val_if_large.3, %51, %50, %62, %53, %65, %66)
        参数含义:
        %159 (Tensor): 输出,全为固定值的Tensor。
        %val_if_large.3 (Tensor): 类似形状的Tensor。
        %51 (int/float/bool): 填充值。
        %50 (int): dtype。
        %62 (int): layout。
        %53 (int): device。
        %65 (bool): 是否计算梯度。
        %66 (int): 内存形式。
    """
S
SunAhong1993 已提交
2495
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2496 2497 2498 2499 2500 2501 2502 2503
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%val_if_large.3
S
SunAhong1993 已提交
2504 2505
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2506 2507 2508 2509 2510 2511 2512 2513
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%51
    if inputs_name[1] in mapper.attrs:
        layer_attrs["fill_value"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
2514
                            current_outputs, scope_name)
S
SunAhong1993 已提交
2515 2516 2517 2518 2519 2520 2521 2522 2523
        layer_inputs["fill_value"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%50,代表dtype
    layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[2]]]

    graph.add_layer(
        "paddle.full_like",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
2524
        scope_name=scope_name,
S
SunAhong1993 已提交
2525 2526 2527 2528
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
def aten_gather(mapper, graph, node):
    """ 构造gather激活的PaddleLayer。
    TorchScript示例:
        %result.3 : Tensor = aten::gather(%input.5, %18, %19, %20, %21)
        参数含义:
        %result.3 (Tensor): 输出,gather后的结果。
        %result.5 (Tensor): 需要gather的Tensor。
        %18 (int): 需要gather的维度。
        %19 (Tensor): 需要gather的索引。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("gather", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
2549 2550
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2551 2552 2553 2554
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%18
    layer_attrs["dim"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%19
S
SunAhong1993 已提交
2555 2556
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2557 2558 2559
    layer_inputs["index"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
2560

S
SunAhong1993 已提交
2561
    graph.add_layer(
S
SunAhong1993 已提交
2562 2563 2564
        "custom_layer:Gather",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
2565 2566 2567 2568 2569
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
2570 2571 2572 2573 2574 2575 2576 2577 2578
def aten_gelu(mapper, graph, node):
    """ 构造GeLU激活的PaddleLayer。
    TorchScript示例:
        %result.3 : Tensor = aten::gelu(%input.5)
        参数含义:
        %result.3 (Tensor): 输出,GELU后的结果。
        %result.5 (Tensor): 需要GELU的Tensor。
    注意: inplace这个参数在paddle中未实现
    """
S
SunAhong1993 已提交
2579 2580
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("gelu", mapper.nn_name2id)
S
SunAhong1993 已提交
2581
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
2582
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
2583 2584 2585 2586 2587
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
2588 2589
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2590 2591 2592 2593 2594
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
2595 2596 2597 2598
        "paddle.nn.GELU",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
    return current_inputs, current_outputs


def aten___getitem__(mapper, graph, node):
    """ 构造获取list中元素的PaddleLayer。
    TorchScript示例:
        %v.1 : int = aten::__getitem__(%72, %88)
        参数含义:
        %v.1 (-): 输出,list中的元素。
        %72 (list): 需要获取元素的list。
        %88 (int): 索引。
    """
S
SunAhong1993 已提交
2611
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2612 2613 2614 2615 2616 2617 2618
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%72
S
SunAhong1993 已提交
2619 2620
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2621 2622
    layer_inputs["list"] = inputs_name[0]
    # 处理输入1,即%88
S
SunAhong1993 已提交
2623 2624
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2625 2626 2627 2628
    layer_inputs["index"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2629 2630 2631 2632 2633
    graph.add_layer(
        "prim.getitem",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645
    return current_inputs, current_outputs


def aten_gt(mapper, graph, node):
    """ 构造对比大小的PaddleLayer。
    TorchScript示例:
        %83 : bool = aten::gt(%82, %78)
        参数含义:
        %83 (bool): 输出,第一个元素是否大于第二个元素。
        %82 (-): 需对比的输入1。
        %78 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
2646
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2647 2648 2649 2650 2651 2652 2653
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%82
S
SunAhong1993 已提交
2654 2655
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2656 2657
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%78
S
SunAhong1993 已提交
2658 2659
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2660 2661 2662 2663
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2664 2665 2666 2667 2668
    graph.add_layer(
        "prim.gt",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2669 2670 2671
    return current_inputs, current_outputs


W
WJJ1995 已提交
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 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 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
def aten_group_norm(mapper, graph, node):
    """
    TorchScript Code:
        %input.81 : Tensor = aten::group_norm(%input.2, %25, %60, %59, %26, %30)
        Parameter meaning:
        %input.81 (Tensor): Output Tensor
        %input.2 (Tensor): Input Tensor
        %25 (Tensor): num_groups
        %60 (Tensor): weight
        %59 (Tensor): bias
        %26 (Tensor): eps
        %30 (bool): enabled cudnn
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("groupnorm", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # output list
    current_outputs = [output_name]
    # process Input Tensor
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["input"] = inputs_name[0]
    # input list
    current_inputs = list(layer_inputs.values())
    # process num_groups
    layer_attrs['num_groups'] = mapper.attrs[inputs_name[1]]
    # process weight
    weights = mapper.pytorch_params[inputs_name[2]]
    mapper.paddle_params[op_name + ".weight"] = weights
    layer_attrs['num_channels'] = weights.shape[0]
    # process bias
    if inputs_name[2] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[3]]
        if bias is not None:
            mapper.paddle_params[op_name + ".bias"] = bias
    else:
        mapper.paddle_params[op_name + ".bias"] = False
    # process eps
    layer_attrs["epsilon"] = mapper.attrs[inputs_name[4]]

    graph.add_layer(
        "paddle.nn.GroupNorm",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
add gru  
SunAhong1993 已提交
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
def aten_gru(mapper, graph, node):
    """ 构造门控循环单元网络(GRU)的PaddleLayer。
    TorchScript示例:
        %21, %22 = aten::gru(%input, %hx, %20, %11, %10, %9, %11, %8, %11)
        参数含义:
        %21 (Tensor): 输出,由前向和后向cell的输出拼接得到。
        %22 (Tensor): 输出,最终状态。
        %input (Tensor): 网络输入。
        %hx (Tensor): 网络的初始状态。
        %20 (list): 所有权重组合成的list。
        %11 (bool): 是否使用bias。
        %10 (int): 网络层数。
        %9 (float): dropout概率。
        %11 (bool): 是否为训练阶段。
        %8 (bool): 是否使用双向LSTM。
        %11 (bool): 第一个维度是否为batch size。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("gru", mapper.nn_name2id)
    output_names = mapper._get_outputs_name(node)
    layer_outputs = [op_name]
    layer_outputs.extend(output_names)
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = output_names
    # 处理输入0,即%input.95
S
SunAhong1993 已提交
2753 2754
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
add gru  
SunAhong1993 已提交
2755 2756
    layer_inputs["input0"] = inputs_name[0]
    # 处理输入1,即%734
S
SunAhong1993 已提交
2757 2758
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
add gru  
SunAhong1993 已提交
2759 2760 2761 2762
    layer_inputs["input1"] = inputs_name[1]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入2,即%734
S
SunAhong1993 已提交
2763 2764
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
add gru  
SunAhong1993 已提交
2765 2766 2767 2768 2769
    graph.layers.pop(mapper.output2id[inputs_name[2]])
    param_inputs_name, _ = mapper._get_inputs_name(inputs_node[2])
    new_param_inputs_name = list()
    for i, param_name in enumerate(param_inputs_name):
        if i == 0:
S
SunAhong1993 已提交
2770 2771 2772 2773
            layer_attrs["hidden_size"] = int(
                mapper.paddle_params[param_name].shape[0] / 3)
            layer_attrs["input_size"] = int(mapper.paddle_params[param_name]
                                            .shape[1])
S
add gru  
SunAhong1993 已提交
2774 2775
        if len(mapper.paddle_params[param_name].shape) > 1:
            part_name = param_name.split("_weight_")[-1]
S
SunAhong1993 已提交
2776 2777 2778 2779
            mapper.paddle_params["{}.weight_{}".format(
                op_name, part_name)] = mapper.paddle_params[param_name]
            new_param_inputs_name.append("{}.weight_{}".format(op_name,
                                                               part_name))
S
add gru  
SunAhong1993 已提交
2780 2781
        else:
            part_name = param_name.split("_bias_")[-1]
S
SunAhong1993 已提交
2782 2783
            mapper.paddle_params["{}.bias_{}".format(
                op_name, part_name)] = mapper.paddle_params[param_name]
S
add gru  
SunAhong1993 已提交
2784
        mapper.paddle_params.pop(param_name)
S
SunAhong1993 已提交
2785

S
add gru  
SunAhong1993 已提交
2786 2787 2788 2789 2790
    # 处理输入3,即%526
    is_bias = mapper.attrs[inputs_name[3]]
    if not is_bias:
        for param_name in new_param_inputs_name:
            bias_name = param_name.replace("weight", "bias")
S
SunAhong1993 已提交
2791 2792 2793
            bias_shape = mapper.paddle_params[param_name].shape[:1]
            mapper.paddle_params[bias_name] = np.zeros(bias_shape).astype(
                "float32")
S
add gru  
SunAhong1993 已提交
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
    # 处理输入4,即%525
    layer_attrs["num_layers"] = mapper.attrs[inputs_name[4]]
    # 处理输入5,即%524
    layer_attrs["dropout"] = mapper.attrs[inputs_name[5]]
    # 处理输入7,即%526
    is_bidirectional = mapper.attrs[inputs_name[7]]
    if is_bidirectional:
        layer_attrs["direction"] = string("bidirectional")
    # 处理输入8,即%526
    batch_first = mapper.attrs[inputs_name[8]]
    if not batch_first:
        layer_attrs["time_major"] = True
    graph.add_layer(
        "paddle.nn.GRU",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


2815
def aten_hardtanh(mapper, graph, node):
S
SunAhong1993 已提交
2816 2817
    """ 构造hardtanh激活的PaddleLayer。
    TorchScript示例:
2818
        %result.9 : Tensor = aten::hardtanh(%input.20, %67, %66)
S
SunAhong1993 已提交
2819 2820 2821 2822 2823 2824
        参数含义:
        %result.9 (Tensor): 输出,hardtanh激活后的Tensor。
        %input.20 (Tensor): 需要hardtanh激活的Tensor。
        %67 (float): hardtanh激活的最小阈值。
        %66 (float): hardtanh激活的最大阈值。
    """
S
SunAhong1993 已提交
2825 2826
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("hardtanh", mapper.nn_name2id)
S
SunAhong1993 已提交
2827
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
2828
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
2829 2830 2831 2832 2833 2834
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.20
S
SunAhong1993 已提交
2835 2836
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
2837 2838 2839 2840 2841 2842 2843 2844
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%67
    layer_attrs["min"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%66
    layer_attrs["max"] = mapper.attrs[inputs_name[2]]

S
SunAhong1993 已提交
2845
    if layer_attrs["min"] == 0 and layer_attrs["max"] == 6:
S
SunAhong1993 已提交
2846
        graph.add_layer(
S
SunAhong1993 已提交
2847 2848 2849 2850
            "paddle.nn.ReLU6",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name)
S
SunAhong1993 已提交
2851 2852 2853 2854 2855 2856 2857
    else:
        graph.add_layer(
            'paddle.nn.Hardtanh',
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name,
            **layer_attrs)
S
SunAhong1993 已提交
2858 2859 2860
    return current_inputs, current_outputs


W
wjj19950828 已提交
2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
def aten_hardsigmoid(mapper, graph, node):
    """
    TorchScript Code:
        %55 : Tensor = aten::hardsigmoid(%54)
        Parameter meaning:
        %55 (Tensor): output
        %54 (Tensor): input tensor
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("hardsigmoid", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # outputs list
    current_outputs = [output_name]
    # inputs list
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]

    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.nn.Hardsigmoid",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


def aten_hardswish(mapper, graph, node):
    """
    TorchScript Code:
        %55 : Tensor = aten::hardswish(%54)
        Parameter meaning:
        %55 (Tensor): output
        %54 (Tensor): input tensor
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("hardswish", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # outputs list
    current_outputs = [output_name]
    # inputs list
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]

    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.nn.Hardswish",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
2923
def aten_index(mapper, graph, node):
W
WJJ1995 已提交
2924 2925
    """
    TorchScript Code:
S
SunAhong1993 已提交
2926
        %1681 : Float = aten::index(%1653, %1680)
W
WJJ1995 已提交
2927 2928 2929 2930
        Parameter meaning:
        %1681 (Tensor): Output Tensor
        %1653 (Tensor): Input Tensor
        %1680 (int): Index
S
SunAhong1993 已提交
2931 2932 2933 2934 2935 2936
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
W
WJJ1995 已提交
2937
    # output list
S
SunAhong1993 已提交
2938
    current_outputs = [output_name]
W
WJJ1995 已提交
2939
    # process Input Tensor
S
SunAhong1993 已提交
2940 2941 2942
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
W
WJJ1995 已提交
2943
    # process Index
S
SunAhong1993 已提交
2944 2945 2946
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
    layer_inputs["index"] = inputs_name[1]
W
WJJ1995 已提交
2947

S
SunAhong1993 已提交
2948 2949 2950 2951
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.getitem",
W
WJJ1995 已提交
2952
        inputs={"list": layer_inputs["x"]},
S
SunAhong1993 已提交
2953 2954
        outputs=layer_outputs,
        scope_name=scope_name,
W
WJJ1995 已提交
2955
        index=layer_inputs["index"])
S
SunAhong1993 已提交
2956
    return current_inputs, current_outputs
S
SunAhong1993 已提交
2957

S
SunAhong1993 已提交
2958

W
wjj19950828 已提交
2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
def aten_imag(mapper, graph, node):
    """ 构造获取绝对值的PaddleLayer。
    TorchScript示例:
        %n0.3 : Tensor = aten::imag(%1)
        参数含义:
        %1 (Tensor): Complex Tensor。
        %n0.3 (Tensor): 返回虚部 Tensor。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%n.3
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.imag",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
2989 2990
def aten_index_select(mapper, graph, node):
    """ 构造选择元素的PaddleLayer。
S
SunAhong1993 已提交
2991 2992 2993 2994 2995 2996 2997 2998
    TorchScript示例:
        %bd.3 : Tensor = aten::index_select(%x2.3, %320, %371)
        参数含义:
        %bd.3 (Tensor): 输出,选择后的Tensor。
        %x2.3 (Tensor): 需要选择的Tensor。
        %320 (int): 维度。
        %371 (Tensor): 选择的索引。
    """
S
SunAhong1993 已提交
2999
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3000 3001 3002 3003 3004 3005 3006 3007
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x2.3
S
SunAhong1993 已提交
3008 3009
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3010 3011 3012 3013 3014 3015
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%320
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
3016
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3017 3018
        layer_inputs["axis"] = inputs_name[1]
    # 处理输入2,即%371
S
SunAhong1993 已提交
3019 3020
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3021 3022 3023 3024 3025 3026 3027
    layer_inputs["index"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.index_select",
        inputs=layer_inputs,
3028
        outputs=layer_outputs,
S
SunAhong1993 已提交
3029
        scope_name=scope_name,
S
SunAhong1993 已提交
3030 3031 3032 3033
        **layer_attrs)
    return current_inputs, current_outputs


S
add gru  
SunAhong1993 已提交
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
def aten_instance_norm(mapper, graph, node):
    """构造InstanceNorm的PaddleLayer
    TorchScript示例:
        %res.7 : Tensor = aten::instance_norm(%res.5, %88, %85, %84, %83, %87, %91, %92, %87)
        参数含义:
        %res.7 (Tensor): 输出,InstanceNorm的结果。
        %res.5 (Tensor): 需要进行InstanceNorm的特征层。
        %88 (Tensor): weights。
        %85 (Tensor): bias。
        %84 (Tensor): 全局均值。
        %83 (Tensor): 全局方差。
        %87 (bool): 是否使用输入的统计。
        %91 (float): momentum。
        %92 (float): eps。
        %87 (bool): 是否启用cudnn。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("instance_norm", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.80
S
SunAhong1993 已提交
3060 3061
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
add gru  
SunAhong1993 已提交
3062 3063 3064 3065 3066 3067
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%88
    if inputs_name[1] in mapper.pytorch_params:
        weights = mapper.pytorch_params[inputs_name[1]]
3068
        mapper.paddle_params[op_name + ".scale"] = weights
S
add gru  
SunAhong1993 已提交
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095
        layer_attrs['num_features'] = weights.shape[0]
    # 处理输入2,即%85
    if inputs_name[2] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[2]]
        mapper.paddle_params[op_name + ".bias"] = bias
    # 处理输入3,即%84
    if inputs_name[3] in mapper.pytorch_params:
        mean = mapper.pytorch_params[inputs_name[3]]
        mapper.paddle_params[op_name + "._mean"] = mean
    # 处理输入4,即%83
    if inputs_name[4] in mapper.pytorch_params:
        var = mapper.pytorch_params[inputs_name[4]]
        mapper.paddle_params[op_name + "._variance"] = var
    # 处理输入6,即%91
    layer_attrs["momentum"] = 1 - mapper.attrs[inputs_name[6]]
    # 处理输入7,即%92
    layer_attrs["epsilon"] = mapper.attrs[inputs_name[7]]

    graph.add_layer(
        "custom_layer:InstanceNorm",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
3096 3097 3098 3099 3100 3101 3102 3103
def aten_Int(mapper, graph, node):
    """ 构造强转为int的PaddleLayer。
    TorchScript示例:
        %1739 : int = aten::Int(%1738)
        参数含义:
        %1739 (int): 输出,int型数据。
        %1738 (-): 需要强转的数据。
    """
S
SunAhong1993 已提交
3104
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3105 3106 3107 3108 3109 3110 3111
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%1738
S
SunAhong1993 已提交
3112 3113
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3114 3115 3116 3117
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3118 3119 3120 3121 3122
    graph.add_layer(
        "prim.int",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134
    return current_inputs, current_outputs


def aten___is__(mapper, graph, node):
    """ 构造is not的PaddleLayer。
    TorchScript示例:
        %3949 : bool = aten::__isnot__(%size.122, %3931)
        参数含义:
        %3949 (bool): 输出,第一个元素是否不是第二个元素。
        %size.122 (-): 需对比的输入1。
        %3931 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
3135
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3136 3137 3138 3139 3140 3141 3142
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%size.122
S
SunAhong1993 已提交
3143 3144
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3145 3146
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%3931
S
SunAhong1993 已提交
3147 3148
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3149 3150 3151 3152
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3153 3154 3155 3156 3157
    graph.add_layer(
        "prim.is",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169
    return current_inputs, current_outputs


def aten___isnot__(mapper, graph, node):
    """ 构造is not的PaddleLayer。
    TorchScript示例:
        %3949 : bool = aten::__isnot__(%size.122, %3931)
        参数含义:
        %3949 (bool): 输出,第一个元素是否不是第二个元素。
        %size.122 (-): 需对比的输入1。
        %3931 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
3170
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3171 3172 3173 3174 3175 3176 3177
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%size.122
S
SunAhong1993 已提交
3178 3179
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3180 3181
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%3931
S
SunAhong1993 已提交
3182 3183
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3184 3185 3186 3187
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3188 3189 3190 3191 3192
    graph.add_layer(
        "prim.isnot",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
    return current_inputs, current_outputs


def aten_layer_norm(mapper, graph, node):
    """ 构造层归一化的PaddleLayer。
    TorchScript示例:
        %input0.4 : Tensor = aten::layer_norm(%input.6, %1181, %174, %173, %70, %71)
        参数含义:
        %input0.4 (Tensor): 输出,层归一化后的结果。
        %input.6 (Tensor): 需要进行层归一化的特征层。
        %1181 (list/int/tuple): 需规范化的shape。
        %174 (Tensor): weights。
        %173 (Tensor): bias。
        %70 (float): 指明在计算过程中是否添加较小的值到方差中以防止除零。
        %71 (bool): 是否启用cudnn。
    """
S
SunAhong1993 已提交
3209 3210
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("layernorm", mapper.nn_name2id)
S
SunAhong1993 已提交
3211
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3212
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3213 3214 3215 3216 3217 3218
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.6
S
SunAhong1993 已提交
3219 3220
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3221 3222 3223 3224 3225 3226 3227
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%1181
    layer_attrs["normalized_shape"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%174
    weights = mapper.pytorch_params[inputs_name[2]]
S
SunAhong1993 已提交
3228
    mapper.paddle_params[op_name + ".weight"] = weights
S
SunAhong1993 已提交
3229 3230 3231 3232
    # 处理输入3,即%173
    if inputs_name[3] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[3]]
        if bias is not None:
S
SunAhong1993 已提交
3233
            mapper.paddle_params[op_name + ".bias"] = bias
S
SunAhong1993 已提交
3234
    else:
S
SunAhong1993 已提交
3235
        mapper.paddle_params[op_name + ".bias"] = False
S
SunAhong1993 已提交
3236 3237 3238 3239 3240 3241 3242
    # 处理输入4,即%70
    layer_attrs["epsilon"] = mapper.attrs[inputs_name[4]]

    graph.add_layer(
        "paddle.nn.LayerNorm",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
3243
        scope_name=scope_name,
S
SunAhong1993 已提交
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256
        **layer_attrs)
    return current_inputs, current_outputs


def aten_le(mapper, graph, node):
    """ 构造对比大小的PaddleLayer。
    TorchScript示例:
        %80 : bool = aten::le(%78, %79)
        参数含义:
        %80 (bool): 输出,第一个元素是否小于等于第二个元素。
        %78 (-): 需对比的输入1。
        %79 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
3257
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3258 3259 3260 3261 3262 3263 3264
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%78
S
SunAhong1993 已提交
3265 3266
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3267 3268
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%79
S
SunAhong1993 已提交
3269 3270
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3271 3272 3273 3274
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3275 3276 3277 3278 3279
    graph.add_layer(
        "prim.le",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3280 3281 3282
    return current_inputs, current_outputs


3283 3284 3285 3286 3287 3288 3289
def aten_leaky_relu(mapper, graph, node):
    """ 构造leaky relu激活的PaddleLayer。
    TorchScript示例:
        %input.117 : Tensor = aten::leaky_relu(%input.114, %1570)
        参数含义:
        %input.117 (Tensor): 输出,leaky relu后的结果。
        %input.114 (Tensor): 需要leaky relu的Tensor。
S
SunAhong1993 已提交
3290 3291
        %1570 (float): 输入中的元素小于0时的斜率。
    """
S
SunAhong1993 已提交
3292 3293
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("leakly_relu", mapper.nn_name2id)
S
SunAhong1993 已提交
3294
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3295
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3296 3297 3298 3299 3300 3301
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
3302 3303
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%1570
    layer_attrs["negative_slope"] = mapper.attrs[inputs_name[1]]

    graph.add_layer(
        "paddle.nn.LeakyReLU",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
3314
        scope_name=scope_name,
S
SunAhong1993 已提交
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326
        **layer_attrs)
    return current_inputs, current_outputs


def aten_len(mapper, graph, node):
    """ 构造获取list长度的PaddleLayer。
    TorchScript示例:
        %85 : int = aten::len(%83)
        参数含义:
        %85 (int): 输出,list的长度。
        %72 (list): 需要获取长度的list。
    """
S
SunAhong1993 已提交
3327
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3328 3329 3330 3331 3332 3333 3334
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%72
S
SunAhong1993 已提交
3335 3336
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3337 3338 3339 3340
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3341 3342 3343 3344 3345
    graph.add_layer(
        "prim.len",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3346 3347 3348
    return current_inputs, current_outputs


W
wjj19950828 已提交
3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373
def aten_linear(mapper, graph, node):
    """
    TorchScript Code:
        %x.6 : Float(1, 128, strides=[128, 1]) = aten::linear(%input.305, %weight.629, %bias.317)
        Parameter meaning:
        %x.6 (Tensor): output
        %input.305 (Tensor): input tensor
        %weight.629 (Tensor): weight tensor
        %bias.317 (Tensor): bias tensor
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # outputs list
    current_outputs = [output_name]
    # inputs list
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # transpose weight
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
W
WJJ1995 已提交
3374 3375
    layer_inputs["y"] = inputs_name[1]
    layer_attrs["transpose_y"] = True
W
wjj19950828 已提交
3376
    graph.add_layer(
W
WJJ1995 已提交
3377 3378 3379
        "paddle.matmul",
        inputs=layer_inputs,
        outputs=layer_outputs,
W
wjj19950828 已提交
3380
        scope_name=scope_name,
W
WJJ1995 已提交
3381
        **layer_attrs)
W
wjj19950828 已提交
3382 3383 3384
    if len(inputs_name) == 3:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
W
WJJ1995 已提交
3385 3386 3387 3388 3389 3390
        graph.add_layer(
            "paddle.add",
            inputs={"x": output_name,
                    "y": inputs_name[2]},
            outputs=layer_outputs,
            scope_name=scope_name)
W
wjj19950828 已提交
3391 3392 3393 3394 3395
    current_inputs = list(layer_inputs.values())

    return current_inputs, current_outputs


S
SunAhong1993 已提交
3396 3397 3398 3399 3400 3401 3402 3403
def aten_log(mapper, graph, node):
    """ 构构造log的PaddleLayer。
    TorchScript示例:
        %787 : Tensor = aten::log(%786)
        参数含义:
        %787 (Tensor): 输出,取log的Tensor。
        %786 (Tensor): 需要获取log的Tensor。
    """
S
SunAhong1993 已提交
3404
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3405 3406 3407 3408 3409 3410 3411
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%786
S
SunAhong1993 已提交
3412 3413
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3414 3415 3416 3417 3418
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
3419 3420 3421 3422
        "paddle.log",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3423 3424 3425
    return current_inputs, current_outputs


S
SunAhong1993 已提交
3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
def aten_log_softmax(mapper, graph, node):
    """ 构造log_softmax的PaddleLayer。
    TorchScript示例:
        %4 = aten::log_softmax(%input, %2, %3)
        参数含义:
        %4 (Tensor): 输出的Tensor。
        %input (Tensor): 输入的Tensor。
        %2 (int): 指定对输入进行运算的轴。
        %3 (int): 输入Tensor的数据类型。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    current_inputs = []
    # 处理输入0,即%input
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%2,代表dtype
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[1]
    # 处理输入2,即%3,代表dtype
    if mapper.attrs[inputs_name[2]] is not None:
        layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[2]]]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.nn.functional.log_softmax",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
def aten_lstm(mapper, graph, node):
    """ 构造长短期记忆网络(LSTM)的PaddleLayer。
    TorchScript示例:
        %input.96, %551, %552 = aten::lstm(%input.95, %734, %549, %526, %525, %524, %526, %526, %526)
        参数含义:
        %input.96 (Tensor): 输出,由前向和后向cell的输出拼接得到。
        %551 (Tensor): cell state。
        %552 (Tensor): hidden state。
        %input.95 (Tensor): 网络输入。
        %734 (Tensor): 网络的初始状态。
        %549 (list): 所有权重组合成的list。
        %526 (bool): 是否使用bias。
        %525 (int): 网络层数。
        %524 (float): dropout概率。
        %526 (bool): 是否为训练阶段。
        %526 (bool): 是否使用双向LSTM。
        %526 (bool): 第一个维度是否为batch size。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("lstm", mapper.nn_name2id)
    output_names = mapper._get_outputs_name(node)
    layer_outputs = [op_name]
    layer_outputs.extend(output_names)
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = output_names
    # 处理输入0,即%input.95
S
SunAhong1993 已提交
3500 3501
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3502 3503
    layer_inputs["input0"] = inputs_name[0]
    # 处理输入1,即%734
S
SunAhong1993 已提交
3504 3505
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3506 3507 3508 3509
    layer_inputs["input1"] = inputs_name[1]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入2,即%734
S
SunAhong1993 已提交
3510 3511
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3512 3513 3514 3515 3516
    graph.layers.pop(mapper.output2id[inputs_name[2]])
    param_inputs_name, _ = mapper._get_inputs_name(inputs_node[2])
    new_param_inputs_name = list()
    for i, param_name in enumerate(param_inputs_name):
        if i == 0:
S
SunAhong1993 已提交
3517 3518 3519 3520
            layer_attrs["hidden_size"] = int(
                mapper.paddle_params[param_name].shape[0] / 4)
            layer_attrs["input_size"] = int(mapper.paddle_params[param_name]
                                            .shape[1])
S
SunAhong1993 已提交
3521 3522
        if len(mapper.paddle_params[param_name].shape) > 1:
            part_name = param_name.split("_weight_")[-1]
S
SunAhong1993 已提交
3523 3524 3525 3526
            mapper.paddle_params["{}.weight_{}".format(
                op_name, part_name)] = mapper.paddle_params[param_name]
            new_param_inputs_name.append("{}.weight_{}".format(op_name,
                                                               part_name))
S
SunAhong1993 已提交
3527 3528
        else:
            part_name = param_name.split("_bias_")[-1]
S
SunAhong1993 已提交
3529 3530
            mapper.paddle_params["{}.bias_{}".format(
                op_name, part_name)] = mapper.paddle_params[param_name]
S
SunAhong1993 已提交
3531
        mapper.paddle_params.pop(param_name)
S
SunAhong1993 已提交
3532

S
SunAhong1993 已提交
3533 3534 3535 3536 3537
    # 处理输入3,即%526
    is_bias = mapper.attrs[inputs_name[3]]
    if not is_bias:
        for param_name in new_param_inputs_name:
            bias_name = param_name.replace("weight", "bias")
S
SunAhong1993 已提交
3538 3539 3540
            bias_shape = mapper.paddle_params[param_name].shape[:1]
            mapper.paddle_params[bias_name] = np.zeros(bias_shape).astype(
                "float32")
S
SunAhong1993 已提交
3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561
    # 处理输入4,即%525
    layer_attrs["num_layers"] = mapper.attrs[inputs_name[4]]
    # 处理输入5,即%524
    layer_attrs["dropout"] = mapper.attrs[inputs_name[5]]
    # 处理输入7,即%526
    is_bidirectional = mapper.attrs[inputs_name[7]]
    if is_bidirectional:
        layer_attrs["direction"] = string("bidirectional")
    # 处理输入8,即%526
    batch_first = mapper.attrs[inputs_name[8]]
    if not batch_first:
        layer_attrs["time_major"] = True
    graph.add_layer(
        "paddle.nn.LSTM",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
3562 3563 3564 3565 3566 3567 3568 3569 3570
def aten_lt(mapper, graph, node):
    """ 构造对比大小的PaddleLayer。
    TorchScript示例:
        %80 : bool = aten::lt(%78, %79)
        参数含义:
        %80 (bool): 输出,第一个元素是否小于第二个元素。
        %78 (-): 需对比的输入1。
        %79 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
3571
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3572 3573 3574 3575 3576 3577 3578
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%78
S
SunAhong1993 已提交
3579 3580
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3581 3582
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%79
S
SunAhong1993 已提交
3583 3584
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3585 3586 3587 3588
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3589 3590 3591 3592 3593
    graph.add_layer(
        "prim.lt",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3594 3595 3596 3597
    return current_inputs, current_outputs


def aten_masked_fill(mapper, graph, node):
W
wjj19950828 已提交
3598 3599
    """
    TorchScript Code:
S
SunAhong1993 已提交
3600
        %input.4 : Tensor = aten::masked_fill(%scores.2, %mask.2, %46)
W
wjj19950828 已提交
3601 3602 3603 3604 3605
        Parameter meaning:
        %input.4 (Tensor): Output Tensor
        %scores.2 (Tensor): Input Tensor
        %mask.2 (Tensor): bool mask
        %46 (-): fill value
S
SunAhong1993 已提交
3606
    """
S
SunAhong1993 已提交
3607
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3608 3609 3610
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    inputs_name, inputs_node = mapper._get_inputs_name(node)
W
wjj19950828 已提交
3611 3612 3613
    layer_full_inputs = {}
    layer_full_attrs = {}
    layer_where_inputs = {}
S
SunAhong1993 已提交
3614 3615
    current_inputs = []
    current_outputs = [output_name]
W
wjj19950828 已提交
3616
    # input list
S
SunAhong1993 已提交
3617 3618
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3619
    current_inputs.append(inputs_name[0])
W
wjj19950828 已提交
3620
    # paddle.full
S
SunAhong1993 已提交
3621
    graph.add_layer(
W
wjj19950828 已提交
3622
        "prim.shape",
S
SunAhong1993 已提交
3623
        inputs={"input": inputs_name[0]},
W
wjj19950828 已提交
3624
        outputs=[inputs_name[0] + "_shape"],
S
SunAhong1993 已提交
3625
        scope_name=scope_name)
W
wjj19950828 已提交
3626 3627 3628 3629 3630 3631 3632 3633 3634
    layer_full_inputs["shape"] = inputs_name[0] + "_shape"
    if inputs_name[2] in mapper.attrs:
        layer_full_attrs["fill_value"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
        layer_full_inputs["fill_value"] = inputs_name[2]
        current_inputs.append(inputs_name[2])

S
SunAhong1993 已提交
3635
    graph.add_layer(
W
wjj19950828 已提交
3636 3637 3638
        "prim.type",
        inputs={"input": inputs_name[0]},
        outputs=[inputs_name[0] + "_type"],
S
SunAhong1993 已提交
3639
        scope_name=scope_name)
W
wjj19950828 已提交
3640
    layer_full_attrs["dtype"] = inputs_name[0] + "_type"
S
SunAhong1993 已提交
3641
    graph.add_layer(
W
wjj19950828 已提交
3642 3643 3644
        "paddle.full",
        inputs=layer_full_inputs,
        outputs=[inputs_name[0] + "_full"],
S
SunAhong1993 已提交
3645
        scope_name=scope_name,
W
wjj19950828 已提交
3646 3647 3648 3649 3650 3651 3652 3653
        **layer_full_attrs)
    # paddle.where
    layer_where_inputs["condition"] = inputs_name[1]
    layer_where_inputs["x"] = inputs_name[0] + "_full"
    layer_where_inputs["y"] = inputs_name[0]
    graph.add_layer(
        "paddle.where",
        inputs=layer_where_inputs,
S
SunAhong1993 已提交
3654 3655
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
    return current_inputs, current_outputs


def aten_max(mapper, graph, node):
    """ 构造获取最大值的PaddleLayer。
    TorchScript示例:
        %val_if_large0.3 : Tensor = aten::max(%val_if_large.3, %159)
        参数含义:
        %val_if_large0.3 (Tensor): 输出,对比后的结果。
        %val_if_large.3 (Tensor): 输入,需要对比的Tensor1。
        %159 (Tensor): 输入,需要对比的Tensor2。
    """
S
SunAhong1993 已提交
3668
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    input_type = list(node.inputs())[1].type()
    if str(input_type) == "Tensor":
        # 处理输入0,即%val_if_large.3
        mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
3679
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3680 3681 3682
        layer_inputs["x"] = inputs_name[0]
        # 处理输入1,即%159
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
3683
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3684 3685 3686 3687
        layer_inputs["y"] = inputs_name[1]
        # 获取当前节点输入的list
        current_inputs = list(layer_inputs.values())
        graph.add_layer(
S
SunAhong1993 已提交
3688 3689 3690 3691
            "paddle.maximum",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name)
S
SunAhong1993 已提交
3692 3693 3694 3695 3696
    else:
        pass
    return current_inputs, current_outputs


W
WJJ1995 已提交
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
def aten_max_pool1d(mapper, graph, node):
    """ 构造最大池化的PaddleLayer。
    TorchScript示例:
        %input.8 : Tensor = aten::max_pool1d(%result.11, %20, %23, %21, %22, %19)
        参数含义:
        %input.8 (Tensor): 输出,池化后的结果。
        %result.11 (Tensor): 需要池化的Tensor。
        %20 (list): 池化kernel的大小。
        %23 (list): 步长大小。
        %21 (list): 填充大小。
        %22 (list): 膨胀系数大小。
        %19 (bool): 是否用ceil函数计算输出高度和宽度。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool1d", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.11
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%20
    layer_attrs["kernel_size"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%23
    layer_attrs["stride"] = mapper.attrs[inputs_name[2]]
    # 处理输入3,即%21
    layer_attrs["padding"] = mapper.attrs[inputs_name[3]]
    # 处理输入5,即%19
    layer_attrs["ceil_mode"] = mapper.attrs[inputs_name[5]]

    graph.add_layer(
        "paddle.nn.MaxPool1D",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755
def aten_max_pool2d(mapper, graph, node):
    """ 构造最大池化的PaddleLayer。
    TorchScript示例:
        %input.8 : Tensor = aten::max_pool2d(%result.11, %20, %23, %21, %22, %19)
        参数含义:
        %input.8 (Tensor): 输出,池化后的结果。
        %result.11 (Tensor): 需要池化的Tensor。
        %20 (list): 池化kernel的大小。
        %23 (list): 步长大小。
        %21 (list): 填充大小。
        %22 (list): 膨胀系数大小。
        %19 (bool): 是否用ceil函数计算输出高度和宽度。
    """
S
SunAhong1993 已提交
3756 3757
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool2d", mapper.nn_name2id)
S
SunAhong1993 已提交
3758
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3759
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3760 3761
    layer_inputs = {}
    layer_attrs = {}
S
SunAhong1993 已提交
3762
    layer_attrs_tmp = {}
S
SunAhong1993 已提交
3763 3764 3765 3766
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.11
S
SunAhong1993 已提交
3767 3768
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3769 3770 3771 3772
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%20
S
SunAhong1993 已提交
3773 3774
    layer_attrs["kernel_size"] = mapper.attrs[inputs_name[1]]
    layer_attrs_tmp["pool_size"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
3775
    # 处理输入2,即%23
S
SunAhong1993 已提交
3776 3777
    layer_attrs["stride"] = mapper.attrs[inputs_name[2]]
    layer_attrs_tmp["pool_stride"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
3778
    # 处理输入3,即%21
S
SunAhong1993 已提交
3779 3780
    layer_attrs["padding"] = mapper.attrs[inputs_name[3]]
    layer_attrs_tmp["pool_padding"] = mapper.attrs[inputs_name[3]]
S
SunAhong1993 已提交
3781 3782 3783 3784
    # 处理输入4,即%22
    graph.add_layer(
        "prim.assert",
        inputs={},
C
channingss 已提交
3785
        outputs=[inputs_name[4] + "_assert"],
S
SunAhong1993 已提交
3786
        scope_name=scope_name + "_assert",
S
SunAhong1993 已提交
3787 3788 3789 3790 3791
        type="eq",
        key=mapper.attrs[inputs_name[4]],
        value=[1, [1, 1]])
    # 处理输入5,即%19
    layer_attrs["ceil_mode"] = mapper.attrs[inputs_name[5]]
S
SunAhong1993 已提交
3792
    layer_attrs_tmp["ceil_mode"] = mapper.attrs[inputs_name[5]]
S
SunAhong1993 已提交
3793

S
SunAhong1993 已提交
3794 3795 3796 3797 3798 3799
    graph.add_layer(
        "paddle.nn.MaxPool2D",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
S
SunAhong1993 已提交
3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811
    return current_inputs, current_outputs


def aten_matmul(mapper, graph, node):
    """ 构造矩阵相乘的PaddleLayer。
    TorchScript示例:
        %output.2 : Tensor = aten::matmul(%101, %111)
        参数含义:
        %output.2 (Tensor): 输出,相乘后的结果。
        %101 (Tensor): 矩阵1。
        %102 (Tensor): 矩阵2。
    """
S
SunAhong1993 已提交
3812
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3813 3814 3815 3816 3817 3818 3819
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%101
S
SunAhong1993 已提交
3820 3821
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3822 3823
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%102
S
SunAhong1993 已提交
3824 3825
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3826 3827 3828 3829
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3830 3831 3832 3833 3834
    graph.add_layer(
        "paddle.matmul",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846
    return current_inputs, current_outputs


def aten_min(mapper, graph, node):
    """ 构造获取最小值的PaddleLayer。
    TorchScript示例:
        %val_if_large0.3 : Tensor = aten::min(%val_if_large.3, %159)
        参数含义:
        %val_if_large0.3 (Tensor): 输出,对比后的结果。
        %val_if_large.3 (Tensor): 输入,需要对比的Tensor1。
        %159 (Tensor): 输入,需要对比的Tensor2。
    """
S
SunAhong1993 已提交
3847
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3848 3849 3850 3851 3852 3853 3854 3855 3856 3857
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    input_type = list(node.inputs())[1].type()
    if str(input_type) == "Tensor":
        # 处理输入0,即%val_if_large.3
        mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
3858
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3859 3860 3861
        layer_inputs["x"] = inputs_name[0]
        # 处理输入1,即%159
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
3862
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3863 3864 3865 3866
        layer_inputs["y"] = inputs_name[1]
        # 获取当前节点输入的list
        current_inputs = list(layer_inputs.values())
        graph.add_layer(
S
SunAhong1993 已提交
3867 3868 3869 3870
            "paddle.minimum",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name)
S
SunAhong1993 已提交
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886
    else:
        pass
    return current_inputs, current_outputs


def aten_mean(mapper, graph, node):
    """ 构造求均值的PaddleLayer。
    TorchScript示例:
        %x.28 : Tensor = aten::mean(%result.1, %4967, %3, %2)
        参数含义:
        %x.28 (Tensor): 输出,求均值后的结果。
        %result.1 (Tensor): 输入,需要求均值的Tensor。
        %4967 (int/list): 求平均值运算的维度。
        %3 (bool): 是否在输出Tensor中保留减小的维度。
        %2 (Tensor): 结果Tensor。
    """
S
SunAhong1993 已提交
3887
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3888 3889 3890 3891 3892 3893 3894 3895
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.1
S
SunAhong1993 已提交
3896 3897
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3898
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
3899 3900 3901
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4967
    if inputs_name[1] in mapper.attrs:
S
SunAhong1993 已提交
3902
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
3903 3904
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
3905 3906
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[1]
S
SunAhong1993 已提交
3907 3908 3909
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%3
    if inputs_name[1] in mapper.attrs:
S
SunAhong1993 已提交
3910
        layer_attrs["keepdim"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
3911 3912
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
3913 3914
                            current_outputs, scope_name)
        layer_inputs["keepdim"] = inputs_name[2]
S
SunAhong1993 已提交
3915 3916 3917
        current_inputs.append(inputs_name[2])

    graph.add_layer(
S
SunAhong1993 已提交
3918
        "paddle.mean",
S
SunAhong1993 已提交
3919 3920
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
3921
        scope_name=scope_name,
S
SunAhong1993 已提交
3922 3923
        **layer_attrs)
    return current_inputs, current_outputs
S
SunAhong1993 已提交
3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941


def aten_meshgrid(mapper, graph, node):
    """ 构造对每个张量做扩充操作的PaddleLayer。
    TorchScript示例:
        %out.39 : int = aten::mshgrid(%input.1)
        参数含义:
        %out.39 (Tensor): 输出,扩充后的结果。
        %input.1 (Tensor): 输入。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.1
S
SunAhong1993 已提交
3942 3943
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3944 3945 3946 3947 3948
    layer_inputs["args"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = layer_inputs.values()
    current_outputs = layer_outputs

S
SunAhong1993 已提交
3949 3950 3951 3952 3953
    graph.add_layer(
        "paddle.meshgrid",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3954
    return current_inputs, current_outputs
S
SunAhong1993 已提交
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965


def aten_mul(mapper, graph, node):
    """ 构造数值相乘的PaddleLayer。
    TorchScript示例:
        %size_prods.39 : int = aten::mul(%size_prods.38, %114)
        参数含义:
        %size_prods.39 (Tensor): 输出,相乘后的结果。
        %size_prods.38 (-): 数值1。
        %114 (-): 数值2。
    """
S
SunAhong1993 已提交
3966
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3967 3968 3969 3970 3971 3972 3973
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%size_prods.38
S
SunAhong1993 已提交
3974 3975
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3976 3977
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%114
S
SunAhong1993 已提交
3978 3979
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
3980 3981 3982 3983 3984
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    current_outputs = layer_outputs

S
SunAhong1993 已提交
3985 3986 3987 3988 3989
    graph.add_layer(
        "prim.mul",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001
    return current_inputs, current_outputs


def aten_ne(mapper, graph, node):
    """ 构造判断数值是否不相等的PaddleLayer。
    TorchScript示例:
        %134 : bool = aten::ne(%133, %132)
        参数含义:
        %134 (bool): 对比后结果。
        %133 (-): 需对比的输入1。
        %132 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
4002
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4003 4004 4005 4006 4007 4008 4009
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%124
S
SunAhong1993 已提交
4010 4011
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4012 4013
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
4014 4015
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4016 4017 4018 4019
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
4020 4021 4022 4023 4024
    graph.add_layer(
        "prim.ne",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
    return current_inputs, current_outputs


def aten_neg(mapper, graph, node):
    """ 构造对数值取负的PaddleLayer。
    TorchScript示例:
        %909 : int = aten::neg(%908)
        参数含义:
        %909 (int): 取负后结果。
        %908 (int): 需取负的输入。
    """
S
SunAhong1993 已提交
4036
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4037 4038 4039 4040 4041 4042 4043
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%124
S
SunAhong1993 已提交
4044 4045
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4046 4047 4048 4049
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
4050 4051 4052 4053 4054
    graph.add_layer(
        "prim.neg",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
4055 4056 4057
    return current_inputs, current_outputs


W
WJJ1995 已提交
4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107
def aten_frobenius_norm(mapper, graph, node):
    """ 构造计算范数的PaddleLayer。
    TorchScript示例:
        %25 = aten::frobenius_norm(%input, %58, %24)
        参数含义:
        %25 (Tensor): 取范数后的结果。
        %input (Tensor): 输入。
        %58 (int): 使用范数计算的轴。
        %24 (bool): 是否在输出的Tensor中保留和输入一样的维度。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    current_inputs = list(layer_inputs.values())
    layer_attrs["p"] = 2
    # 处理输入1,即%58
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%24
    if inputs_name[1] in mapper.attrs:
        layer_attrs["keepdim"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
        layer_inputs["keepdim"] = inputs_name[2]
        current_inputs.append(inputs_name[2])

    graph.add_layer(
        "paddle.norm",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127
def aten_norm(mapper, graph, node):
    """ 构造计算范数的PaddleLayer。
    TorchScript示例:
        %25 = aten::norm(%input, %21, %58, %24)
        参数含义:
        %25 (Tensor): 取范数后的结果。
        %input (Tensor): 输入。
        %21 (int): 范数的种类。
        %58 (int): 使用范数计算的轴。
        %24 (bool): 是否在输出的Tensor中保留和输入一样的维度。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input
S
SunAhong1993 已提交
4128 4129
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
    layer_inputs["x"] = inputs_name[0]
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%21
    if inputs_name[1] in mapper.attrs:
        layer_attrs["p"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["p"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%58
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[2]
        current_inputs.append(inputs_name[2])
    # 处理输入3,即%24
    if inputs_name[1] in mapper.attrs:
        layer_attrs["keepdim"] = mapper.attrs[inputs_name[3]]
    else:
        mapper._check_input(graph, inputs_node[3], inputs_name[3],
                            current_outputs, scope_name)
        layer_inputs["keepdim"] = inputs_name[3]
        current_inputs.append(inputs_name[3])

    graph.add_layer(
        "paddle.norm",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4166 4167 4168 4169 4170 4171 4172 4173
def aten___not__(mapper, graph, node):
    """ 构造对bool型取负的PaddleLayer。
    TorchScript示例:
        %4498 : bool = aten::__not__(%aux_defined.2)
        参数含义:
        %4498 (bool): 取负后结果。
        %aux_defined.2 (bool): 需取负的输入。
    """
S
SunAhong1993 已提交
4174
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4175 4176 4177 4178 4179 4180 4181
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%124
S
SunAhong1993 已提交
4182 4183
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4184 4185 4186 4187
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
4188 4189 4190 4191 4192
    graph.add_layer(
        "prim.not",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
    return current_inputs, current_outputs


def aten_ones(mapper, graph, node):
    """ 构造创建固定形状、数据类型且值全为0的Tensor的PaddleLayer。
    TorchScript示例:
        %input.49 : Tensor = aten::ones(%23, %8, %6, %24, %5)
        参数含义:
        %input.49 (Tensor): 输出,全0的Tensor。
        %23 (list): 形状。
        %8 (int): 类型dtype。
        %6 (int): layout。
        %4995 (Device): 设备。
        %4995 (bool): 是否计算梯度。
    """
S
SunAhong1993 已提交
4208
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    current_inputs = []
    # 处理输入0,即%23,代表end
    if inputs_name[0] in mapper.attrs:
        layer_attrs["shape"] = mapper.attrs[inputs_name[0]]
    else:
        mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
4222
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4223 4224 4225 4226 4227 4228 4229 4230 4231
        layer_inputs["shape"] = inputs_name[0]
        current_inputs.append(inputs_name[0])
    # 处理输入1,即%8,代表dtype
    layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[1]]]

    graph.add_layer(
        "paddle.ones",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4232
        scope_name=scope_name,
S
SunAhong1993 已提交
4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245
        **layer_attrs)
    return current_inputs, current_outputs


def aten_permute(mapper, graph, node):
    """ 构造对bool型取负的PaddleLayer。
    TorchScript示例:
        %2385 : Tensor = aten::permute(%cls_confs0.2, %2384)
        参数含义:
        %2385 (Tensor): 重排后的结果。
        %cls_confs0.2 (Tensor): 需要重排的Tensor。
        %2348 (list): 依照此参数进行重排。
    """
S
SunAhong1993 已提交
4246
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4247 4248 4249 4250 4251 4252 4253 4254
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%cls_confs0.2
S
SunAhong1993 已提交
4255 4256
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4257 4258 4259 4260 4261 4262 4263 4264
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%2348
    if inputs_name[1] in mapper.attrs:
        layer_attrs["perm"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
4265
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4266 4267 4268 4269
        layer_inputs["perm"] = inputs_name[1]
        current_inputs.append(inputs_name[1])

    graph.add_layer(
S
SunAhong1993 已提交
4270
        "paddle.transpose",
S
SunAhong1993 已提交
4271 4272
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4273
        scope_name=scope_name,
S
SunAhong1993 已提交
4274 4275 4276 4277
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295
def aten_pixel_shuffle(mapper, graph, node):
    """ 构造以像素的方式重排的PaddleLayer。
    TorchScript示例:
        %x.6 : aten::pixel_shuffle(%input.101, %726)
        参数含义:
        %x.6 (Tensor): 输出,重排后的Tensor。
        %input.101 (Tensor): 需要重排的Tensor。
        %726 (int): 增大空间分辨率的增大因子。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.101
S
SunAhong1993 已提交
4296 4297
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
    layer_inputs["x"] = inputs_name[0]
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%726
    layer_attrs["upscale_factor"] = mapper.attrs[inputs_name[1]]

    graph.add_layer(
        "paddle.nn.functional.pixel_shuffle",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs

S
SunAhong1993 已提交
4311

S
SunAhong1993 已提交
4312 4313 4314 4315 4316 4317 4318 4319
def aten_pow(mapper, graph, node):
    """ 构造指数激活的PaddleLayer。
    TorchScript示例:
        %x.6 : Tensor = aten::pow(%4700, %4703)
        参数含义:
        %x.6 (Tensor): 输出,指数激活后的Tensor。
        %4700 (Tensor): 需要指数激活的Tensor。
    """
S
SunAhong1993 已提交
4320
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4321 4322 4323 4324 4325 4326 4327 4328
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%4700
S
SunAhong1993 已提交
4329 4330
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4331 4332 4333 4334 4335
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4703
    if inputs_name[1] in mapper.attrs:
S
SunAhong1993 已提交
4336
        layer_attrs["y"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
4337 4338
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
4339 4340
                            current_outputs, scope_name)
        layer_inputs["y"] = inputs_name[1]
S
SunAhong1993 已提交
4341 4342 4343
        current_inputs.append(inputs_name[1])

    graph.add_layer(
S
SunAhong1993 已提交
4344
        "paddle.pow",
S
SunAhong1993 已提交
4345 4346
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4347
        scope_name=scope_name,
S
SunAhong1993 已提交
4348 4349 4350 4351
        **layer_attrs)
    return current_inputs, current_outputs


4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369
def aten_prelu(mapper, graph, node):
    """ 构造prelu激活的PaddleLayer。
    TorchScript示例:
        %result.3 : aten::prelu(%input.150, %999)
        参数含义:
        %result.3 (Tensor): 输出,prelu后的结果。
        %input.150 (Tensor): 需要prelu的Tensor。
        %999 (Tnsor): 权重。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("relu", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.150
S
SunAhong1993 已提交
4370 4371
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
4372 4373 4374 4375 4376 4377 4378 4379
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%999
    weight = mapper.pytorch_params[inputs_name[1]]
    mapper.paddle_params[op_name + "._weight"] = weight
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
4380 4381 4382 4383
        "paddle.nn.PReLU",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
4384 4385 4386 4387
        num_parameters=weight.shape[0])
    return current_inputs, current_outputs


W
wjj19950828 已提交
4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417
def aten_real(mapper, graph, node):
    """
    TorchScript示例:
        %n0.3 : Tensor = aten::real(%n.3)
        参数含义:
        %n0.3 (Tensor): Return Real Tensor。
        %n.3 (Tensor): Input Complex Tensor。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%n.3
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.real",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


S
add gru  
SunAhong1993 已提交
4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436
def aten_reflection_pad1d(mapper, graph, node):
    """ 构造1维映射填充的PaddleLayer。
    TorchScript示例:
        %6 = aten::reflection_pad1d(%input, %7)
        参数含义:
        %6 (Tensor): 输出,填充后的Tensor。
        %input (Tensor): 需要填充的Tensor。
        %7 (list|Tensor): 填充大小。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pad1d", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input
S
SunAhong1993 已提交
4437 4438
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
add gru  
SunAhong1993 已提交
4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%7
    if inputs_name[1] in mapper.attrs:
        layer_attrs["padding"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        ipt_node = inputs_node[1]
        while ipt_node.kind() != "prim::GetAttr":
            inputs_name, inputs_node = mapper._get_inputs_name(ipt_node)
            ipt_node = inputs_node[0]
        layer_attrs["padding"] = list(mapper.pytorch_params[inputs_name[0]])
    layer_attrs["mode"] = string("reflect")
S
SunAhong1993 已提交
4454

S
add gru  
SunAhong1993 已提交
4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482
    graph.add_layer(
        "paddle.nn.Pad1D",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


def aten_reflection_pad2d(mapper, graph, node):
    """ 构造2维映射填充的PaddleLayer。
    TorchScript示例:
        %6 = aten::reflection_pad2d(%input, %7)
        参数含义:
        %6 (Tensor): 输出,填充后的Tensor。
        %input (Tensor): 需要填充的Tensor。
        %7 (list|Tensor): 填充大小。
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pad2d", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input
S
SunAhong1993 已提交
4483 4484
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
add gru  
SunAhong1993 已提交
4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%7
    if inputs_name[1] in mapper.attrs:
        layer_attrs["padding"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        ipt_node = inputs_node[1]
        while ipt_node.kind() != "prim::GetAttr":
            inputs_name, inputs_node = mapper._get_inputs_name(ipt_node)
            ipt_node = inputs_node[0]
        layer_attrs["padding"] = list(mapper.pytorch_params[inputs_name[0]])
    layer_attrs["mode"] = string("reflect")
S
SunAhong1993 已提交
4500

S
add gru  
SunAhong1993 已提交
4501 4502 4503 4504 4505 4506 4507 4508 4509
    graph.add_layer(
        "paddle.nn.Pad2D",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4510 4511 4512 4513 4514 4515 4516 4517 4518
def aten_relu(mapper, graph, node):
    """ 构造ReLU激活的PaddleLayer。
    TorchScript示例:
        %result.3 : Tensor = aten::relu(%input.5)
        参数含义:
        %result.3 (Tensor): 输出,ReLU后的结果。
        %result.5 (Tensor): 需要ReLU的Tensor。
    注意: inplace这个参数在paddle中未实现
    """
S
SunAhong1993 已提交
4519 4520
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("relu", mapper.nn_name2id)
S
SunAhong1993 已提交
4521
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
4522
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
4523 4524 4525 4526 4527
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
4528 4529
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4530 4531 4532 4533 4534
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
4535 4536 4537 4538
        "paddle.nn.ReLU",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550
    return current_inputs, current_outputs


def aten_relu6(mapper, graph, node):
    """ 构造ReLU6激活的PaddleLayer。
    TorchScript示例:
        %result.3 : Tensor = aten::relu6(%input.5)
        参数含义:
        %result.3 (Tensor): 输出,ReLU6后的结果。
        %result.5 (Tensor): 需要ReLU6的Tensor。
    注意: inplace这个参数在paddle中未实现
    """
S
SunAhong1993 已提交
4551 4552
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("relu6", mapper.nn_name2id)
S
SunAhong1993 已提交
4553
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
4554
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
4555 4556 4557 4558 4559
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
4560 4561
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4562 4563 4564 4565 4566
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
4567 4568 4569 4570
        "paddle.nn.ReLU6",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
4571 4572 4573
    return current_inputs, current_outputs


4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599
def aten_remainder(mapper, graph, node):
    """ 构造取余数的PaddleLayer。
    TorchScript示例:
        %701 : Tensor = aten::remainder(%661, %139)
        参数含义:
        %701 (Tensor): 输出,取余结果的Tensor。
        %661 (Tensor): 需要取余的Tensor。
        %139 (Tensor): 除数Tensor。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%661
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%139
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
W
WJJ1995 已提交
4600

4601 4602 4603 4604 4605 4606 4607 4608
    graph.add_layer(
        "prim.remainder",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4609 4610 4611
def aten_repeat(mapper, graph, node):
    """ 构造根据参数对输入各维度进行复制的PaddleLayer。
    TorchScript示例:
4612
        %701 : Tensor = aten::repeat(%699, %700)
S
SunAhong1993 已提交
4613 4614 4615 4616 4617
        参数含义:
        %701 (Tensor): 输出,复制后的Tensor。
        %699 (Tensor): 需要复制的Tensor。
        %700 (list): 指定每个维度复制的次数。
    """
S
SunAhong1993 已提交
4618
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4619 4620 4621 4622 4623 4624 4625 4626
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%699
S
SunAhong1993 已提交
4627 4628
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4629 4630 4631 4632 4633 4634 4635 4636
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%700
    if inputs_name[1] in mapper.attrs:
        layer_attrs["repeat_times"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
4637
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4638 4639 4640 4641 4642 4643 4644
        layer_inputs["repeat_times"] = inputs_name[1]
        current_inputs.append(inputs_name[1])

    graph.add_layer(
        "paddle.tile",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4645
        scope_name=scope_name,
S
SunAhong1993 已提交
4646 4647 4648 4649
        **layer_attrs)
    return current_inputs, current_outputs


W
WJJ1995 已提交
4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719
def aten_repeat_interleave(mapper, graph, node):
    """ 构造根据参数对输入各维度进行复制的PaddleLayer。
    TorchScript示例:
        %701 : Tensor = aten::repeat(%699, %700, %702)
        参数含义:
        %701 (Tensor): 输出,复制后的Tensor。
        %699 (Tensor): 需要复制的Tensor。
        %700 (int | Tensor): 指定每个维度复制的次数。
        %702 (int): 指定在哪个轴上进行复制。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%699
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%700
    if inputs_name[1] in mapper.attrs:
        layer_attrs["repeat_times"] = [int(mapper.attrs[inputs_name[1]])]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["repeat_times"] = inputs_name[1]
        current_inputs.append(inputs_name[1])

    graph.add_layer(
        "paddle.tile",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)

    layer_attrs_reshape = {}
    layer_attrs_reshape["shape"] = [0, int(mapper.attrs[inputs_name[1]]), -1]
    graph.add_layer(
        "paddle.reshape",
        inputs={"x": layer_outputs[0]},
        outputs=[layer_outputs[0] + "_reshape"],
        scope_name=scope_name,
        **layer_attrs_reshape)

    layer_attrs_transpose = {}
    layer_attrs_transpose["perm"] = [0, 2, 1]
    graph.add_layer(
        "paddle.transpose",
        inputs={"x": layer_outputs[0] + "_reshape"},
        outputs=[layer_outputs[0] + "_transpose"],
        scope_name=scope_name,
        **layer_attrs_transpose)

    layer_attrs_reshape = {}
    layer_attrs_reshape["shape"] = [0, -1]
    graph.add_layer(
        "paddle.reshape",
        inputs={"x": layer_outputs[0] + "_transpose"},
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs_reshape)

    return current_inputs, current_outputs


W
WJJ1995 已提交
4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755
def aten_replication_pad1d(mapper, graph, node):
    """
    TorchScript Code:
        %58 : Tensor = aten::replication_pad1d(%input.1, %152)
        Parameter meaning:
        %58 (Tensor): Output Tensor
        %input.1 (Tensor): Input Tensor
        %%152 (list): Padding size
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pad", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # output list
    current_outputs = [output_name]
    # input list
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["input"] = inputs_name[0]
    layer_attrs["padding"] = mapper.attrs[inputs_name[1]]
    layer_attrs["mode"] = string("replicate")
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.nn.Pad1D",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)

    return current_inputs, current_outputs


S
SunAhong1993 已提交
4756 4757 4758 4759 4760 4761 4762 4763 4764
def aten_reshape(mapper, graph, node):
    """ 构造调整大小的PaddleLayer。
    TorchScript示例:
        %x.6 : Tensor = aten::reshape(%4700, %4703)
        参数含义:
        %x.6 (Tensor): 输出,reshape后的Tensor。
        %4700 (Tensor): 需要reshape的Tensor。
        %4703 (list): 形状大小组成的list。
    """
S
SunAhong1993 已提交
4765
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4766 4767 4768 4769 4770 4771 4772 4773
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%4700
S
SunAhong1993 已提交
4774 4775
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4776 4777 4778 4779 4780 4781 4782 4783
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4703
    if inputs_name[1] in mapper.attrs:
        layer_attrs["shape"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
4784
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4785 4786
        layer_inputs["shape"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
S
SunAhong1993 已提交
4787

S
SunAhong1993 已提交
4788
    graph.add_layer(
S
SunAhong1993 已提交
4789
        "paddle.reshape",
S
SunAhong1993 已提交
4790 4791
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4792
        scope_name=scope_name,
S
SunAhong1993 已提交
4793 4794 4795 4796
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
def aten_roll(mapper, graph, node):
    """ 构造循环滚动的PaddleLayer。
    TorchScript示例:
        %x.87 : Float = aten::roll(%x.86, %1862, %1863)
        参数含义:
        %x.87 (Tensor): 输出Tensor。
        %x.86 (Tensor): 输入Tensor。
        %1862 (int/list/tuple): 滚动位移。
        %1863 (int/list/tuple): 滚动轴。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.86
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%1862
    if inputs_name[1] in mapper.attrs:
        layer_attrs["shifts"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["shifts"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%1863
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[2]
        current_inputs.append(inputs_name[2])

    graph.add_layer(
        "paddle.roll",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4847 4848 4849 4850 4851 4852 4853 4854 4855 4856
def aten_rsub(mapper, graph, node):
    """ 构造数值相减的PaddleLayer,计算公式为:out = y - alpha * x。
    TorchScript示例:
        %31 : Tensor = aten::rsub(%30, %13, %7)
        参数含义:
        %31 (Tensor): 相减结果。
        %30 (Tensor): 输入Tensor x。
        %13 (int/float): 输入数值 y。
        %7 (int/float): alpha。
    """
S
SunAhong1993 已提交
4857
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4858 4859 4860 4861 4862 4863 4864
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%30
S
SunAhong1993 已提交
4865 4866
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4867 4868
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%13
S
SunAhong1993 已提交
4869 4870
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4871 4872
    layer_inputs["y"] = inputs_name[1]
    # 处理输入2,即%7
S
SunAhong1993 已提交
4873 4874
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4875 4876 4877 4878
    layer_inputs["alpha"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
4879 4880 4881 4882 4883
    graph.add_layer(
        "prim.rsub",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
4884 4885 4886
    return current_inputs, current_outputs


W
wjj19950828 已提交
4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916
def aten_rsqrt(mapper, graph, node):
    """
    TorchScript Code:
        %n0.3 : Tensor = aten::rsqrt(%n.3)
        Parameter meaning:
        %n0.3 (Tensor): output tensor
        %n.3 (Tensor): input tensor
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # outputs list
    current_outputs = [output_name]
    # inputs list
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]

    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.rsqrt",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4917 4918 4919 4920 4921 4922 4923 4924 4925
def aten_ScalarImplicit(mapper, graph, node):
    """ 构造获取scalar的PaddleLayer。
    TorchScript示例:
        %89 : Scalar = aten::ScalarImplicit(%end.1)
        参数含义:
        %89 (Scalar): 输出,得到的Scalar。
        %end.1 (-): 组要转换的数据。
    【注意】由于Paddle无Scalar,所以最后转换为Tensor。
    """
S
SunAhong1993 已提交
4926
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4927 4928 4929 4930 4931 4932 4933
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%end.1
S
SunAhong1993 已提交
4934 4935
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4936 4937 4938 4939 4940 4941
    layer_inputs["input"] = inputs_name[0]
    input_type = list(node.inputs())[0].type()
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    if str(input_type) == "Tensor":
        graph.add_layer(
S
SunAhong1993 已提交
4942 4943 4944 4945
            "prim.equal",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name)
S
SunAhong1993 已提交
4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962
    else:
        raise Exception(
            "The input type {} of aten::ScalarImplicit is not implemented yet!"
        ).format(input_type)
    return current_inputs, current_outputs


def aten_select(mapper, graph, node):
    """ 构造选取特定维度Variable的PaddleLayer。
    TorchScript示例:
        %19 : Tensor = aten::select(%18, %8, %7)
        参数含义:
        %19 (Tensor): 输出,选取的Tensor。
        %18 (Tensor): 需要选取的Tensor。
        %8 (int): select的维度。
        %7 (int): select的第n个向量。
    """
S
SunAhong1993 已提交
4963
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4964 4965 4966 4967 4968 4969 4970 4971
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%18
S
SunAhong1993 已提交
4972 4973
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4974 4975 4976 4977
    layer_inputs["input"] = inputs_name[0]
    # 处理输入1,即%8
    layer_attrs["dim"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%75
S
SunAhong1993 已提交
4978 4979
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
4980 4981 4982 4983 4984 4985 4986
    layer_inputs["index"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.select",
        inputs=layer_inputs,
4987
        outputs=layer_outputs,
S
SunAhong1993 已提交
4988
        scope_name=scope_name,
S
SunAhong1993 已提交
4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001
        **layer_attrs)
    return current_inputs, current_outputs


def aten__set_item(mapper, graph, node):
    """ 构造对dict加入元素的PaddleLayer。
    TorchScript示例:
        = aten::_set_item(%features.1, %out_name.1, %x.3)
        参数含义:
        %features.1 (list): dict。
        %out_name.1 (-): dict的key。
        %x.3 (-): dict的value。
    """
S
SunAhong1993 已提交
5002
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5003 5004 5005 5006 5007
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = []
    # 处理输入0,即%features.1
S
SunAhong1993 已提交
5008 5009
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5010 5011
    layer_inputs["dict"] = inputs_name[0]
    # 处理输入1,即%out_name.1
S
SunAhong1993 已提交
5012 5013
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5014 5015
    layer_inputs["key"] = inputs_name[1]
    # 处理输入2,即%x.3
S
SunAhong1993 已提交
5016 5017
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5018 5019 5020 5021
    layer_inputs["value"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
5022 5023
    graph.add_layer(
        "prim.set_item", inputs=layer_inputs, outputs=[], scope_name=scope_name)
S
SunAhong1993 已提交
5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034
    return current_inputs, current_outputs


def aten_sigmoid(mapper, graph, node):
    """ 构造sigmoid激活的PaddleLayer。
    TorchScript示例:
        %55 : Tensor = aten::sigmoid(%54)
        参数含义:
        %55 (Tensor): 输出,sigmoid后的结果。
        %54 (Tensor): 需要tanh的Tensor。
    """
S
SunAhong1993 已提交
5035 5036
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("sigmoid", mapper.nn_name2id)
S
SunAhong1993 已提交
5037
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
5038
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
5039 5040 5041 5042 5043
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%54
S
SunAhong1993 已提交
5044 5045
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5046 5047 5048 5049 5050
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
5051 5052 5053 5054
        "paddle.nn.Sigmoid",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
5055 5056 5057
    return current_inputs, current_outputs


5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089
def aten_silu(mapper, graph, node):
    """ 构造Silu激活的PaddleLayer。
    TorchScript示例:
        %result.3 : Tensor = aten::silu(%input.5)
        参数含义:
        %result.3 (Tensor): 输出,Silu后的结果。
        %input.5 (Tensor): 需要Silu的Tensor。
    注意: inplace这个参数在paddle中未实现
    """
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("silu", mapper.nn_name2id)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [op_name, output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.5
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.nn.Silu",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
5090 5091 5092 5093 5094 5095 5096 5097
def aten_sin(mapper, graph, node):
    """ 构造数学计算sin的PaddleLayer。
    TorchScript示例:
        %94 : Tensor = aten::sin(%sinusoid_inp.1)
        参数含义:
        %94 (Tensor): 输出,sin之后的结果。
        %sinusoid_inp.1 (Tensor): 需要进行shape的Tensor。
    """
S
SunAhong1993 已提交
5098
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5099 5100 5101 5102 5103 5104 5105
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%sinusoid_inp.1
S
SunAhong1993 已提交
5106 5107
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5108 5109 5110 5111
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
5112 5113 5114 5115 5116
    graph.add_layer(
        "paddle.sin",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128
    return current_inputs, current_outputs


def aten_size(mapper, graph, node):
    """ 构造获取shape的PaddleLayer。
    TorchScript示例:
        %73 : int[] = aten::size(%x.12, %10)
        参数含义:
        %73 (list): 输出,shape的list。
        %x.12 (Tensor): 需要获取shape的Tensor。
        %10 (int): 非必须,代表维度。
    """
S
SunAhong1993 已提交
5129
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5130 5131 5132 5133 5134 5135 5136 5137
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.12
S
SunAhong1993 已提交
5138 5139
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5140 5141 5142 5143 5144 5145 5146 5147 5148
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    if len(inputs_name) > 1:
        # 处理输入1,即%12
        if inputs_name[1] in mapper.attrs:
            layer_attrs["dim"] = mapper.attrs[inputs_name[1]]
        else:
            mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
5149
                                current_outputs, scope_name)
S
SunAhong1993 已提交
5150 5151 5152 5153 5154 5155
            layer_inputs["dim"] = inputs_name[1]
            current_inputs.append(inputs_name[1])
        graph.add_layer(
            "prim.shape_dim",
            inputs=layer_inputs,
            outputs=layer_outputs,
S
SunAhong1993 已提交
5156
            scope_name=scope_name,
S
SunAhong1993 已提交
5157 5158 5159 5160
            **layer_attrs)
        return current_inputs, current_outputs

    graph.add_layer(
S
SunAhong1993 已提交
5161 5162 5163 5164
        "prim.shape",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179
    return current_inputs, current_outputs


def aten_slice(mapper, graph, node):
    """ 构造切分list或Variable的PaddleLayer。
    TorchScript示例:
        %83 : int[] = aten::slice(%73, %_81, %82, %75, %77)
        参数含义:
        %83 (list/Tensor): 输出,切分后的list。
        %73 (list/Tensor): 需要切分的list。
        %_81 (int): 切分的维度,不一定存在。
        %82 (int): 切分的开始索引。
        %75 (int): 切分的结束索引。
        %77 (int): 切分的步长。
    """
S
SunAhong1993 已提交
5180
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5181 5182 5183 5184 5185 5186 5187 5188 5189
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    if len(inputs_name) == 5:
        # 处理输入0,即%73
        mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
5190 5191
                            current_outputs, scope_name)
        layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
5192 5193 5194 5195 5196 5197 5198 5199 5200

        # 获取当前节点输入的list
        current_inputs = list(layer_inputs.values())
        # 处理输入1,即%_81
        if inputs_name[1] in mapper.attrs:
            graph.add_layer(
                "prim.list",
                inputs={},
                outputs=[inputs_name[1] + "_list"],
S
SunAhong1993 已提交
5201
                scope_name=scope_name,
S
SunAhong1993 已提交
5202 5203 5204
                input0=mapper.attrs[inputs_name[1]])
        else:
            mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
5205
                                current_outputs, scope_name)
S
SunAhong1993 已提交
5206 5207 5208
            graph.add_layer(
                "prim.list",
                inputs={"input0": inputs_name[1]},
S
SunAhong1993 已提交
5209 5210
                outputs=[inputs_name[1] + "_list"],
                scope_name=scope_name)
S
SunAhong1993 已提交
5211 5212 5213 5214 5215 5216 5217 5218 5219 5220
            current_inputs.append(inputs_name[1])
        layer_inputs["axes"] = inputs_name[1] + "_list"
        current_inputs.append(inputs_name[1] + "_list")
        current_outputs.append(inputs_name[1] + "_list")
        # 处理输入2,即%82
        if inputs_name[2] in mapper.attrs:
            graph.add_layer(
                "prim.list",
                inputs={},
                outputs=[inputs_name[2] + "_list"],
S
SunAhong1993 已提交
5221
                scope_name=scope_name,
S
SunAhong1993 已提交
5222 5223 5224
                input0=mapper.attrs[inputs_name[2]])
        else:
            mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
5225
                                current_outputs, scope_name)
S
SunAhong1993 已提交
5226 5227 5228
            graph.add_layer(
                "prim.list",
                inputs={"input0": inputs_name[2]},
S
SunAhong1993 已提交
5229 5230
                outputs=[inputs_name[2] + "_list"],
                scope_name=scope_name)
S
SunAhong1993 已提交
5231 5232 5233 5234 5235 5236 5237 5238 5239 5240
            current_inputs.append(inputs_name[2])
        layer_inputs["starts"] = inputs_name[2] + "_list"
        current_inputs.append(inputs_name[2] + "_list")
        current_outputs.append(inputs_name[2] + "_list")
        # 处理输入3,即%85
        if inputs_name[3] in mapper.attrs:
            graph.add_layer(
                "prim.list",
                inputs={},
                outputs=[inputs_name[3] + "_list"],
S
SunAhong1993 已提交
5241
                scope_name=scope_name,
S
SunAhong1993 已提交
5242 5243 5244
                input0=mapper.attrs[inputs_name[3]])
        else:
            mapper._check_input(graph, inputs_node[3], inputs_name[3],
S
SunAhong1993 已提交
5245
                                current_outputs, scope_name)
S
SunAhong1993 已提交
5246 5247 5248
            graph.add_layer(
                "prim.list",
                inputs={"input0": inputs_name[3]},
S
SunAhong1993 已提交
5249 5250
                outputs=[inputs_name[3] + "_list"],
                scope_name=scope_name)
S
SunAhong1993 已提交
5251 5252 5253 5254 5255 5256 5257 5258 5259 5260
            current_inputs.append(inputs_name[3])
        layer_inputs["ends"] = inputs_name[3] + "_list"
        current_inputs.append(inputs_name[3] + "_list")
        current_outputs.append(inputs_name[3] + "_list")
        # 处理输入4,即%77
        if inputs_name[4] in mapper.attrs:
            graph.add_layer(
                "prim.list",
                inputs={},
                outputs=[inputs_name[4] + "_list"],
S
SunAhong1993 已提交
5261
                scope_name=scope_name,
S
SunAhong1993 已提交
5262 5263 5264
                input0=mapper.attrs[inputs_name[4]])
        else:
            mapper._check_input(graph, inputs_node[4], inputs_name[4],
S
SunAhong1993 已提交
5265
                                current_outputs, scope_name)
S
SunAhong1993 已提交
5266 5267 5268
            graph.add_layer(
                "prim.list",
                inputs={"input0": inputs_name[4]},
S
SunAhong1993 已提交
5269 5270
                outputs=[inputs_name[4] + "_list"],
                scope_name=scope_name)
S
SunAhong1993 已提交
5271 5272 5273 5274 5275 5276
            current_inputs.append(inputs_name[4])
        layer_inputs["strides"] = inputs_name[4] + "_list"
        current_inputs.append(inputs_name[4] + "_list")
        current_outputs.append(inputs_name[4] + "_list")

        graph.add_layer(
S
SunAhong1993 已提交
5277
            "paddle.strided_slice",
S
SunAhong1993 已提交
5278
            inputs=layer_inputs,
S
SunAhong1993 已提交
5279 5280
            outputs=layer_outputs,
            scope_name=scope_name)
S
SunAhong1993 已提交
5281 5282 5283
    else:
        # 处理输入0,即%73
        mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
5284
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5285 5286 5287
        layer_inputs["input"] = inputs_name[0]
        # 处理输入1,即%82
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
5288
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5289 5290 5291
        layer_inputs["start"] = inputs_name[1]
        # 处理输入2,即%75
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
5292
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5293 5294 5295
        layer_inputs["end"] = inputs_name[2]
        # 处理输入3,即%77
        mapper._check_input(graph, inputs_node[3], inputs_name[3],
S
SunAhong1993 已提交
5296
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5297 5298 5299 5300 5301
        layer_inputs["step"] = inputs_name[3]
        # 获取当前节点输入的list
        current_inputs = list(layer_inputs.values())

        graph.add_layer(
S
SunAhong1993 已提交
5302 5303 5304 5305
            "prim.slice",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name)
S
SunAhong1993 已提交
5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318
    return current_inputs, current_outputs


def aten_softmax(mapper, graph, node):
    """ 构造softmax激活的PaddleLayer。
    TorchScript示例:
        %input2.1 : Tensor = aten::softmax(%input.5, %80, %72)
        参数含义:
        %input2.1 (Tensor): 激活后结果。
        %input.5 (Tensor): 需要激活的Tensor。
        %80 (int): 指定对输入Tensor进行运算的轴。
        %72 (str): 类型,默认为None。
    """
S
SunAhong1993 已提交
5319 5320
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("softmax", mapper.nn_name2id)
S
SunAhong1993 已提交
5321
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
5322
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
5323 5324 5325 5326 5327 5328
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.31
S
SunAhong1993 已提交
5329 5330
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5331 5332 5333 5334 5335 5336 5337 5338 5339
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    layer_attrs["axis"] = mapper.attrs[inputs_name[1]]

    graph.add_layer(
        "paddle.nn.Softmax",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5340
        scope_name=scope_name,
S
SunAhong1993 已提交
5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
        **layer_attrs)
    return current_inputs, current_outputs


def aten_softplus(mapper, graph, node):
    """ 构造softplus激活的PaddleLayer。
    TorchScript示例:
        %54 : Tensor = aten::softplus(%x.31, %30, %29)
        参数含义:
        %54 (Tensor): 激活后结果。
        %x.31 (Tensor): 需要激活的Tensor。
        %30 (int): beta。
        %29 (int): 阈值。
    """
S
SunAhong1993 已提交
5355 5356
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("softplus", mapper.nn_name2id)
S
SunAhong1993 已提交
5357
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
5358
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
5359 5360 5361 5362 5363 5364
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.31
S
SunAhong1993 已提交
5365 5366
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5367 5368 5369 5370 5371 5372 5373 5374 5375 5376
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    layer_attrs["beta"] = mapper.attrs[inputs_name[1]]
    layer_attrs["threshold"] = mapper.attrs[inputs_name[2]]

    graph.add_layer(
        "paddle.nn.Softplus",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5377
        scope_name=scope_name,
S
SunAhong1993 已提交
5378 5379 5380 5381
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400
def aten_split_with_sizes(mapper, graph, node):
    """ 构构造split的PaddleLayer。
    TorchScript示例:
        %1450 : Tensor[] = aten::split_with_sizes(%1446, %1750, %41)
        参数含义:
        %1450 (Tensor): 输出,split后的Tensor。
        %1446 (Tensor): 需要获取split的Tensor。
        %1750 (list): 子Tensor的数量列表。
        %41 (int): 需要分割的维度。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%1446
S
SunAhong1993 已提交
5401 5402
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%1750
    if inputs_name[1] in mapper.attrs:
        layer_attrs["num_or_sections"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["num_or_sections"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%135
    if inputs_name[2] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[2]
        current_inputs.append(inputs_name[2])
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "paddle.split",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
5432 5433 5434 5435 5436 5437 5438 5439
def aten_sqrt(mapper, graph, node):
    """ 构构造sqrt的PaddleLayer。
    TorchScript示例:
        %787 : Tensor = aten::sqrt(%786)
        参数含义:
        %787 (Tensor): 输出,取sqrt的Tensor。
        %786 (Tensor): 需要获取sqrt的Tensor。
    """
S
SunAhong1993 已提交
5440
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5441 5442 5443 5444 5445 5446 5447
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%786
S
SunAhong1993 已提交
5448 5449
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5450 5451 5452 5453 5454
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
5455 5456 5457 5458
        "paddle.sqrt",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470
    return current_inputs, current_outputs


def aten_squeeze(mapper, graph, node):
    """ 构造删除位数为1的维度的PaddleLayer。
    TorchScript示例:
        %12 : Tensor = aten::squeeze(%start_logits.1, %4)
        参数含义:
        %12 (Tensor): 输出,删除维度后的Tensor。
        %start_logits.1 (Tensor): 需要删除维度的Tensor。
        %4 (int): 维度。
    """
S
SunAhong1993 已提交
5471
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5472 5473 5474 5475 5476 5477 5478 5479
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%start_logits.1
S
SunAhong1993 已提交
5480 5481
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5482 5483 5484 5485 5486 5487 5488 5489
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
5490
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5491 5492 5493
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
S
SunAhong1993 已提交
5494
        "paddle.squeeze",
S
SunAhong1993 已提交
5495 5496
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5497
        scope_name=scope_name,
S
SunAhong1993 已提交
5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510
        **layer_attrs)
    return current_inputs, current_outputs


def aten_stack(mapper, graph, node):
    """ 构造堆叠Tensor的PaddleLayer。
    TorchScript示例:
        %x.222 : Tensor = aten::stack(%32, %7)
        参数含义:
        %x.222 (Tensor): 输出,堆叠后的结果。
        %i.12 (Tensor): 需要堆叠的Tensor组成的Tensor。
        %7 (int): 堆叠的轴。
    """
S
SunAhong1993 已提交
5511
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5512 5513 5514 5515 5516 5517 5518 5519
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%13
S
SunAhong1993 已提交
5520 5521
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5522 5523 5524 5525 5526 5527 5528 5529
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%12
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
5530
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5531 5532 5533 5534 5535 5536
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
        "paddle.stack",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5537
        scope_name=scope_name,
S
SunAhong1993 已提交
5538 5539 5540 5541 5542 5543 5544
        **layer_attrs)
    return current_inputs, current_outputs


def aten_sub(mapper, graph, node):
    """ 构造数值相减的PaddleLayer。
    TorchScript示例:
S
SunAhong1993 已提交
5545
        %840 : int = aten::sub(%839, %836, %3)
S
SunAhong1993 已提交
5546 5547 5548 5549
        参数含义:
        %840 (-): 相减结果。
        %839 (-): 输入数值 x。
        %836 (-): 输入数值 y。
S
SunAhong1993 已提交
5550
        %3 (-): alpha。
S
SunAhong1993 已提交
5551
    """
S
SunAhong1993 已提交
5552
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5553 5554 5555
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
S
SunAhong1993 已提交
5556
    layer_attrs = {}
S
SunAhong1993 已提交
5557 5558 5559 5560
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%839
S
SunAhong1993 已提交
5561 5562
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5563 5564
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%836
S
SunAhong1993 已提交
5565 5566
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5567
    layer_inputs["y"] = inputs_name[1]
S
SunAhong1993 已提交
5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578
    # 处理输入2,即%3
    if len(inputs_node) > 2:
        if inputs_name[2] in mapper.attrs:
            layer_attrs["alpha"] = mapper.attrs[inputs_name[2]]
        else:
            mapper._check_input(graph, inputs_node[2], inputs_name[2],
                                current_outputs, scope_name)
            layer_inputs["alpha"] = inputs_name[2]
            current_inputs.append(inputs_name[2])
    else:
        layer_attrs["alpha"] = 1.0
S
SunAhong1993 已提交
5579 5580 5581
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
5582 5583 5584 5585 5586 5587
    graph.add_layer(
        "prim.sub",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
S
SunAhong1993 已提交
5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598
    return current_inputs, current_outputs


def aten_t(mapper, graph, node):
    """ 构造矩阵转置的PaddleLayer。
    TorchScript示例:
        %840 : int = aten::sub(%839, %836)
        参数含义:
        %109 (Tensor): 输出,转置后的矩阵。
        %102 (Tensor): 需要转置的Tensor。
    """
S
SunAhong1993 已提交
5599
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5600 5601 5602 5603 5604 5605 5606
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.12
S
SunAhong1993 已提交
5607 5608
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5609 5610 5611 5612 5613
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
5614
        "paddle.transpose",
S
SunAhong1993 已提交
5615 5616
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5617
        scope_name=scope_name,
S
SunAhong1993 已提交
5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629
        perm=[1, 0])
    return current_inputs, current_outputs


def aten_tanh(mapper, graph, node):
    """ 构造tanh激活的PaddleLayer。
    TorchScript示例:
        %55 : Tensor = aten::tanh(%54)
        参数含义:
        %55 (Tensor): 输出,tanh后的结果。
        %54 (Tensor): 需要tanh的Tensor。
    """
S
SunAhong1993 已提交
5630 5631
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("tanh", mapper.nn_name2id)
S
SunAhong1993 已提交
5632
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
5633
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
5634 5635 5636 5637 5638
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
5639 5640
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5641 5642 5643 5644 5645
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
5646 5647 5648 5649
        "paddle.nn.Tanh",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660
    return current_inputs, current_outputs


def aten_split(mapper, graph, node):
    """ 构造分割Tensor的PaddleLayer。
    TorchScript示例:
        %160 : Tensor[] = aten::split(%159, %135, %123)
        参数含义:
        %160 (Tensor): 输出,分割后的矩阵。
        %159 (Tensor): 需要分割的Tensor。
        %135 (int): 分割的数量。
W
WJJ1995 已提交
5661
        %123 (int): 轴。
S
SunAhong1993 已提交
5662
    """
S
SunAhong1993 已提交
5663
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5664 5665 5666 5667 5668 5669 5670 5671
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%159
S
SunAhong1993 已提交
5672 5673
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5674
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
5675
    # 处理输入2,即%723
S
SunAhong1993 已提交
5676 5677
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5678
    layer_inputs["axis"] = inputs_name[2]
S
SunAhong1993 已提交
5679
    # 处理输入1,即%135
S
SunAhong1993 已提交
5680 5681
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5682 5683 5684 5685
    input_type = list(node.inputs())[0].type()
    if "[]" in str(input_type):
        layer_inputs["num_or_sections"] = inputs_name[1]
    else:
W
WJJ1995 已提交
5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704
        index = mapper.attrs[inputs_name[2]]
        graph.add_layer(
            "prim.shape",
            inputs={"input": inputs_name[0]},
            outputs=[inputs_name[0] + '_shape'],
            scope_name=scope_name)
        graph.add_layer(
            "prim.getitem",
            inputs={"list": inputs_name[0] + '_shape'},
            outputs=[inputs_name[0] + '_dim'],
            scope_name=scope_name,
            index=index)
        graph.add_layer(
            "prim.floordiv",
            inputs={'x': inputs_name[0] + '_dim',
                    'y': inputs_name[1]},
            outputs=[inputs_name[1] + '_div'],
            scope_name=scope_name)
        layer_attrs["num_or_sections"] = inputs_name[1] + '_div'
S
SunAhong1993 已提交
5705 5706 5707 5708
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
5709
        "paddle.split",
S
SunAhong1993 已提交
5710 5711
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5712
        scope_name=scope_name,
S
SunAhong1993 已提交
5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726
        **layer_attrs)
    return current_inputs, current_outputs


def aten_transpose(mapper, graph, node):
    """ 构造矩阵转置的PaddleLayer。
    TorchScript示例:
        %715 : Tensor = aten::transpose(%x.21, %704, %705)
        参数含义:
        %715 (Tensor): 输出,转置后的矩阵。
        %x.21 (Tensor): 需要转置的Tensor。
        %704 (int): 转置的维度1。
        %705 (int): 转置的维度2。
    """
S
SunAhong1993 已提交
5727
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5728 5729 5730 5731 5732 5733 5734
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.21
S
SunAhong1993 已提交
5735 5736
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5737 5738
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%704
S
SunAhong1993 已提交
5739 5740
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5741 5742
    dim1 = inputs_name[1]
    # 处理输入2,即%705
S
SunAhong1993 已提交
5743 5744
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5745 5746
    dim2 = inputs_name[2]
    # 获取当前节点输入的list
S
SunAhong1993 已提交
5747
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
5748
    graph.add_layer(
S
SunAhong1993 已提交
5749
        "prim.shape",
S
SunAhong1993 已提交
5750
        inputs={"input": inputs_name[0]},
S
SunAhong1993 已提交
5751 5752
        outputs=[output_name + "_shape"],
        scope_name=scope_name)
S
SunAhong1993 已提交
5753 5754 5755 5756
    current_outputs.append(output_name + "_shape")
    graph.add_layer(
        "prim.len",
        inputs={"input": output_name + "_shape"},
S
SunAhong1993 已提交
5757 5758
        outputs=[output_name + "_len"],
        scope_name=scope_name)
S
SunAhong1993 已提交
5759 5760 5761 5762 5763
    current_outputs.append(output_name + "_len")
    current_inputs.append(output_name + "_shape")
    graph.add_layer(
        "prim.len2list",
        inputs={"len": output_name + "_len"},
S
SunAhong1993 已提交
5764 5765
        outputs=[output_name + "_list"],
        scope_name=scope_name)
S
SunAhong1993 已提交
5766 5767 5768 5769 5770 5771
    current_outputs.append(output_name + "_list")
    current_inputs.append(output_name + "_len")
    graph.add_layer(
        "prim.check_dim",
        inputs={"len": output_name + "_len",
                "dim": dim1},
S
SunAhong1993 已提交
5772 5773
        outputs=[dim1 + "_new"],
        scope_name=scope_name)
S
SunAhong1993 已提交
5774 5775 5776 5777
    graph.add_layer(
        "prim.check_dim",
        inputs={"len": output_name + "_len",
                "dim": dim2},
S
SunAhong1993 已提交
5778 5779
        outputs=[dim2 + "_new"],
        scope_name=scope_name)
S
SunAhong1993 已提交
5780 5781 5782 5783 5784 5785 5786
    graph.add_layer(
        "prim.replaceitem",
        inputs={
            "list": output_name + "_list",
            "index": dim1 + "_new",
            "item": dim2 + "_new"
        },
S
SunAhong1993 已提交
5787 5788
        outputs=[],
        scope_name=scope_name)
S
SunAhong1993 已提交
5789 5790 5791 5792 5793 5794 5795
    graph.add_layer(
        "prim.replaceitem",
        inputs={
            "list": output_name + "_list",
            "index": dim2 + "_new",
            "item": dim1 + "_new"
        },
S
SunAhong1993 已提交
5796 5797
        outputs=[],
        scope_name=scope_name)
S
SunAhong1993 已提交
5798
    graph.add_layer(
S
SunAhong1993 已提交
5799
        "paddle.transpose",
S
SunAhong1993 已提交
5800 5801
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5802
        scope_name=scope_name,
S
SunAhong1993 已提交
5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815
        perm=output_name + "_list")
    return current_inputs, current_outputs


def aten_to(mapper, graph, node):
    """ 构造类型转换的PaddleLayer。
    TorchScript示例:
        %30 : Tensor = aten::to(%extended_attention_mask.1, %12, %5, %5, %4)
        参数含义:
        %30 (Tensor): 转换后的Tensor。
        %extended_attention_mask.1 (Tensor): 需要转换的Tensor。
        %12 (int): 转换的类型。
    """
S
SunAhong1993 已提交
5816
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5817 5818 5819 5820 5821 5822 5823 5824
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%13
S
SunAhong1993 已提交
5825 5826
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5827 5828 5829 5830 5831 5832 5833 5834 5835 5836
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%12
    if len(inputs_name) == 6:
        layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[2]]]
    else:
        layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[1]]]

    graph.add_layer(
S
SunAhong1993 已提交
5837
        "paddle.cast",
S
SunAhong1993 已提交
5838 5839
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5840
        scope_name=scope_name,
S
SunAhong1993 已提交
5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853
        **layer_attrs)
    return current_inputs, current_outputs


def aten_type_as(mapper, graph, node):
    """ 构造转换Tensor类型的PaddleLayer。
    TorchScript示例:
        %57 : Tensor = aten::type_as(%56, %mask.1)
        参数含义:
        %57 (Tensor): 输出,改变类型后的Tensor。
        %56 (Tensor): 需要改变类型的Tensor。
        %mask.1 (Tensor): 转换成与该Tensor相一致的类型。
    """
S
SunAhong1993 已提交
5854
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5855 5856 5857 5858 5859 5860 5861
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%56
S
SunAhong1993 已提交
5862 5863
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5864 5865 5866 5867
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入0,即%mask.1
S
SunAhong1993 已提交
5868 5869
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5870 5871 5872
    graph.add_layer(
        "prim.type",
        inputs={"input": inputs_name[1]},
S
SunAhong1993 已提交
5873 5874
        outputs=[inputs_name[1] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
5875 5876 5877 5878
    layer_inputs["dtype"] = inputs_name[1] + "_type"
    current_inputs.append(inputs_name[1])

    graph.add_layer(
S
SunAhong1993 已提交
5879 5880 5881 5882
        "paddle.cast",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894
    return current_inputs, current_outputs


def aten_unsqueeze(mapper, graph, node):
    """ 构造插入维度的PaddleLayer。
    TorchScript示例:
        %13 : Tensor = aten::unsqueeze(%12, %7)
        参数含义:
        %13 (Tensor): 输出,插入维度后的Tensor。
        %12 (Tensor): 需要插入维度的Tensor。
        %7 (int): 维度。
    """
S
SunAhong1993 已提交
5895
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5896 5897 5898 5899 5900 5901 5902 5903
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%13
S
SunAhong1993 已提交
5904 5905
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5906 5907 5908 5909 5910 5911 5912 5913
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%12
    if inputs_name[1] in mapper.attrs:
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
5914
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5915 5916 5917
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
S
SunAhong1993 已提交
5918
        "paddle.unsqueeze",
S
SunAhong1993 已提交
5919 5920
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
5921
        scope_name=scope_name,
S
SunAhong1993 已提交
5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935
        **layer_attrs)
    return current_inputs, current_outputs


def aten_upsample_bilinear2d(mapper, graph, node):
    """ 构造使用bilinear上采样的PaddleLayer。
    TorchScript示例:
        %4997 : Tensor = aten::upsample_bilinear2d(%x.13, %4963, %5421, %4995, %4996)
        参数含义:
        %4997 (Tensor): 输出,上采样后的Tensor。
        %x.13 (Tensor): 需要上采样的Tensor。
        %4963 (list): 上采样后的大小。
        %5421 (bool): 若为True,则将输入和输出张量的4个角落像素的中心对齐,并保留角点像素的值。
        %4995 (float): 高度的乘数因子。
W
WJJ1995 已提交
5936
        %4996 (float): 宽度的乘数因子。
S
SunAhong1993 已提交
5937
    """
S
SunAhong1993 已提交
5938
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
5939 5940 5941 5942 5943 5944 5945 5946
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.13
S
SunAhong1993 已提交
5947 5948
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
5949 5950 5951 5952 5953 5954 5955 5956
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4963
    if inputs_name[1] in mapper.attrs:
        layer_attrs["size"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
5957
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5958 5959 5960 5961 5962 5963
        layer_inputs["size"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
        graph.add_layer(
            "prim.isinstance",
            inputs={"input": inputs_name[1]},
            outputs=[inputs_name[1] + "_isinstance"],
S
SunAhong1993 已提交
5964
            scope_name=scope_name,
W
wjj19950828 已提交
5965
            cls="paddle.static.Variable")
S
SunAhong1993 已提交
5966
        # TODO(syf): paddle.Variable
S
SunAhong1993 已提交
5967 5968
        graph.add_layer(
            "prim.if", {"input": inputs_name[1] + "_isinstance"},
S
SunAhong1993 已提交
5969 5970
            outputs=[inputs_name[0] + "_if1"],
            scope_name=scope_name)
S
SunAhong1993 已提交
5971
        if_layer = graph.layers[list(graph.layers.keys())[-1]]
W
WJJ1995 已提交
5972
        block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
5973 5974 5975
        block.add_layer(
            "prim.var2list",
            inputs={"input": inputs_name[1]},
S
SunAhong1993 已提交
5976 5977
            outputs=[inputs_name[1]],
            scope_name=scope_name)
S
SunAhong1993 已提交
5978
        if_layer.add_block(block)
W
WJJ1995 已提交
5979
        block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
5980 5981 5982 5983 5984 5985 5986
        if_layer.add_block(block)
        if_layer.inputs["input-0"] = inputs_name[1]
    # 处理输入2,即%5421
    if inputs_name[2] in mapper.attrs:
        layer_attrs["align_corners"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
5987
                            current_outputs, scope_name)
S
SunAhong1993 已提交
5988 5989
        layer_inputs["align_corners"] = inputs_name[2]
        current_inputs.append(inputs_name[2])
S
fix2  
SunAhong1993 已提交
5990 5991 5992 5993
    if "size" in layer_attrs and layer_attrs["size"] is None:
        mapper._check_input(graph, inputs_node[3], inputs_name[3],
                            current_outputs, scope_name)
        layer_inputs["scale_factor"] = inputs_name[3]
S
SunAhong1993 已提交
5994
    layer_attrs["align_mode"] = 0
C
channingss 已提交
5995
    layer_attrs["mode"] = string("bilinear")
S
SunAhong1993 已提交
5996 5997 5998 5999
    graph.add_layer(
        "paddle.nn.functional.interpolate",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
6000
        scope_name=scope_name,
S
SunAhong1993 已提交
6001 6002 6003
        **layer_attrs)
    return current_inputs, current_outputs

S
SunAhong1993 已提交
6004

W
wjj19950828 已提交
6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064
def aten_upsample_trilinear3d(mapper, graph, node):
    """
    TorchScript Code:
        %4997 : Tensor = aten::upsample_trilinear3d(%x.13, %4963, %5421, %4995)
        Parameter meaning:
        %4997 (Tensor): Output Tensor
        %x.13 (Tensor): Input Tensor
        %4963 (list): output_size
        %5421 (bool): align_corners
        %4995 (float): scale_factors
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # Output list
    current_outputs = [output_name]
    # process Input Tensor
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
    layer_inputs["x"] = inputs_name[0]
    current_inputs = list(layer_inputs.values())
    # process output_size
    if inputs_name[1] in mapper.attrs:
        layer_attrs["size"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["size"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # process align_corners
    if inputs_name[2] in mapper.attrs:
        layer_attrs["align_corners"] = mapper.attrs[inputs_name[2]]
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
                            current_outputs, scope_name)
        layer_inputs["align_corners"] = inputs_name[2]
        current_inputs.append(inputs_name[2])
    # process scale_factor
    if inputs_name[3] in mapper.attrs:
        layer_attrs["scale_factor"] = mapper.attrs[inputs_name[3]]
    else:
        mapper._check_input(graph, inputs_node[3], inputs_name[3],
                            current_outputs, scope_name)
        layer_inputs["scale_factor"] = inputs_name[3]
        current_inputs.append(inputs_name[3])
    layer_attrs["align_mode"] = 0
    layer_attrs["mode"] = string("trilinear")
    layer_attrs["data_format"] = string("NCDHW")
    graph.add_layer(
        "paddle.nn.functional.interpolate",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
6065 6066 6067 6068 6069 6070 6071 6072
def aten_upsample_nearest2d(mapper, graph, node):
    """ 构造使用nearest上采样的PaddleLayer。
    TorchScript示例:
        %4997 : Tensor = aten::upsample_nearest2d(%x.13, %4963, %5421, %4995)
        参数含义:
        %4997 (Tensor): 输出,上采样后的Tensor。
        %x.13 (Tensor): 需要上采样的Tensor。
        %4963 (list): 上采样后的大小。
W
WJJ1995 已提交
6073
        %5421 (float): 高度的乘数因子。
S
SunAhong1993 已提交
6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084
        %4995 (float): 宽度的乘数因子。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.13
S
SunAhong1993 已提交
6085 6086
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4963
    if inputs_name[1] in mapper.attrs:
        layer_attrs["size"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
                            current_outputs, scope_name)
        layer_inputs["size"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
        graph.add_layer(
            "prim.isinstance",
            inputs={"input": inputs_name[1]},
            outputs=[inputs_name[1] + "_isinstance"],
            scope_name=scope_name,
W
wjj19950828 已提交
6103
            cls="paddle.static.Variable")
S
SunAhong1993 已提交
6104 6105 6106 6107 6108 6109
        # TODO(syf): paddle.Variable
        graph.add_layer(
            "prim.if", {"input": inputs_name[1] + "_isinstance"},
            outputs=[inputs_name[0] + "_if1"],
            scope_name=scope_name)
        if_layer = graph.layers[list(graph.layers.keys())[-1]]
W
WJJ1995 已提交
6110
        block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
6111 6112 6113 6114 6115 6116
        block.add_layer(
            "prim.var2list",
            inputs={"input": inputs_name[1]},
            outputs=[inputs_name[1]],
            scope_name=scope_name)
        if_layer.add_block(block)
W
WJJ1995 已提交
6117
        block = PaddleGraph(source_type="pytorch", parent_layer=if_layer)
S
SunAhong1993 已提交
6118 6119
        if_layer.add_block(block)
        if_layer.inputs["input-0"] = inputs_name[1]
S
fix  
SunAhong1993 已提交
6120
    if "size" in layer_attrs and layer_attrs["size"] is None:
6121
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
fix  
SunAhong1993 已提交
6122
                            current_outputs, scope_name)
6123
        layer_inputs["scale_factor"] = inputs_name[2]
S
SunAhong1993 已提交
6124 6125 6126 6127 6128 6129 6130 6131 6132 6133
    layer_attrs["align_mode"] = 0
    layer_attrs["mode"] = string("nearest")
    graph.add_layer(
        "paddle.nn.functional.interpolate",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs

S
SunAhong1993 已提交
6134

S
SunAhong1993 已提交
6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150
def aten_values(mapper, graph, node):
    """ 构造对比大小的PaddleLayer。
    TorchScript示例:
        %5 : Float(1, *, 1024, 2048)[] = aten::values(%1)
        参数含义:
        %5 (list): 输出,由字典获取的values的list。
        %1 (dict): 字典。
    """
    scope_name = mapper.normalize_scope_name(node)
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%78
S
SunAhong1993 已提交
6151 6152
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
6153 6154 6155 6156
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
6157 6158 6159 6160 6161
    graph.add_layer(
        "prim.dict2values",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
6162 6163 6164
    return current_inputs, current_outputs


S
SunAhong1993 已提交
6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178
def aten_view(mapper, graph, node):
    """ 构造调整大小的PaddleLayer。
    TorchScript示例:
        %input.152 : Tensor = aten::view(%x.20, %430)
        参数含义:
        %input.152 (Tensor): 输出,view后的Tensor。
        %x.20 (Tensor): 需要view的Tensor。
        %430 (list): 形状大小组成的list。
    【注意】view 函数只能用于contiguous后的Tensor上,
          也就是只能用于内存中连续存储的Tensor。
          如果对Tensor调用过transpose,permute等操作的话会使该Tensor在内存中变得不再连续,
          此时就不能再调用view函数。因此,需要先使用contiguous来返回一个contiguous copy。
          reshape则不需要依赖目标Tensor是否在内存中是连续的。
    """
S
SunAhong1993 已提交
6179
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
6180 6181 6182 6183 6184 6185 6186 6187
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.20
S
SunAhong1993 已提交
6188 6189
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
6190 6191 6192 6193 6194 6195 6196 6197
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%430
    if inputs_name[1] in mapper.attrs:
        layer_attrs["shape"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
6198
                            current_outputs, scope_name)
S
SunAhong1993 已提交
6199 6200 6201
        layer_inputs["shape"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
S
SunAhong1993 已提交
6202
        "paddle.reshape",
S
SunAhong1993 已提交
6203 6204
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
6205
        scope_name=scope_name,
S
SunAhong1993 已提交
6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217
        **layer_attrs)
    return current_inputs, current_outputs


def aten_warn(mapper, graph, node):
    """ 构造warning的PaddleLayer。
    TorchScript示例:
        = aten::warn(%3, %2)
        参数含义:
        %3 (str): warning的提示字符串。
        %2 (int): warning的stacklevel。
    """
S
SunAhong1993 已提交
6218
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
6219 6220 6221 6222 6223 6224 6225 6226
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%3
S
SunAhong1993 已提交
6227 6228
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
6229 6230 6231 6232 6233 6234 6235 6236
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%2
    if inputs_name[1] in mapper.attrs:
        layer_attrs["stacklevel"] = mapper.attrs[inputs_name[1]]
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
6237
                            current_outputs, scope_name)
S
SunAhong1993 已提交
6238 6239 6240 6241 6242 6243 6244
        layer_inputs["stacklevel"] = inputs_name[1]
        current_inputs.append(inputs_name[1])

    graph.add_layer(
        "prim.warnings",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
6245
        scope_name=scope_name,
S
SunAhong1993 已提交
6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259
        **layer_attrs)
    return current_inputs, current_outputs


def aten_where(mapper, graph, node):
    """ 构造返回一个根据输入condition, 选择x或y的元素组成的多维Tensor的PaddleLayer,该节点实现out = x + y。
    TorchScript示例:
        %input.4 : Tensor = aten::where(%209, %w0.2, %210)
        参数含义:
        %input.4 (Tensor): 选择的结果。
        %209 (Tensor): 条件。
        %w0.2 (Tensor): 输入数值 x。
        %210 (Tensor): 输入数值 y。
    """
S
SunAhong1993 已提交
6260
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
6261 6262 6263 6264 6265 6266 6267
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%209
S
SunAhong1993 已提交
6268 6269
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
6270 6271
    layer_inputs["condition"] = inputs_name[0]
    # 处理输入1,即%w0.2
S
SunAhong1993 已提交
6272 6273
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
6274 6275
    layer_inputs["x"] = inputs_name[1]
    # 处理输入1,即%w0.2
S
SunAhong1993 已提交
6276 6277
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
6278 6279 6280 6281
    layer_inputs["y"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
6282 6283 6284 6285 6286
    graph.add_layer(
        "paddle.where",
        inputs=layer_inputs,
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301
    return current_inputs, current_outputs


def aten_zeros(mapper, graph, node):
    """ 构造创建固定形状、数据类型且值全为0的Tensor的PaddleLayer。
    TorchScript示例:
        %input.49 : Tensor = aten::zeros(%23, %8, %6, %24, %5)
        参数含义:
        %input.49 (Tensor): 输出,全0的Tensor。
        %23 (list): 形状。
        %8 (int): 类型dtype。
        %6 (int): layout。
        %4995 (Device): 设备。
        %4995 (bool): 是否计算梯度。
    """
S
SunAhong1993 已提交
6302
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    current_inputs = []
    # 处理输入0,即%23,代表end
    if inputs_name[0] in mapper.attrs:
        layer_attrs["shape"] = mapper.attrs[inputs_name[0]]
    else:
        mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
6316
                            current_outputs, scope_name)
S
SunAhong1993 已提交
6317 6318 6319 6320 6321 6322 6323 6324 6325
        layer_inputs["shape"] = inputs_name[0]
        current_inputs.append(inputs_name[0])
    # 处理输入1,即%8,代表dtype
    layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[1]]]

    graph.add_layer(
        "paddle.zeros",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
6326
        scope_name=scope_name,
S
SunAhong1993 已提交
6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343
        **layer_attrs)
    return current_inputs, current_outputs


def aten_zeros_like(mapper, graph, node):
    """ 构造创建与输入Tensor形状一致的、数据类型且值全为0的Tensor的PaddleLayer。
    TorchScript示例:
        %782 : Tensor = aten::zeros_like(%n.2, %655, %670, %662, %671, %672)
        参数含义:
        %782 (Tensor): 输出,全0的Tensor。
        %n.2 (Tensor): 标准Tensor。
        %655 (int): 类型dtype。
        %670 (int): layout。
        %662 (Device): 设备。
        %671 (bool): 是否计算梯度。
        %672 (memory_format): 存储类型。
    """
S
SunAhong1993 已提交
6344
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
6345 6346 6347 6348 6349 6350 6351 6352
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%n.2
S
SunAhong1993 已提交
6353 6354
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs,
                        scope_name)
S
SunAhong1993 已提交
6355 6356 6357 6358 6359 6360 6361 6362 6363 6364
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%655,代表dtype
    layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[1]]]

    graph.add_layer(
        "paddle.zeros_like",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
6365
        scope_name=scope_name,
S
SunAhong1993 已提交
6366 6367
        **layer_attrs)
    return current_inputs, current_outputs