aten.py 173.7 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
from x2paddle.core.util import *
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")
}


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 已提交
43
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
44 45 46 47 48 49 50
    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 已提交
51
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
52 53 54 55 56
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
57
        "paddle.abs", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70
    return current_inputs, current_outputs


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 已提交
71 72
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool2d", mapper.nn_name2id)
S
SunAhong1993 已提交
73
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
74
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
75 76 77 78 79 80
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.3
S
SunAhong1993 已提交
81 82
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
83 84 85 86
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%_output_size.1
    if inputs_name[1] in mapper.attrs:
S
SunAhong1993 已提交
87
        layer_attrs["output_size"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
88 89 90 91 92 93
        graph.add_layer(
            "paddle.nn.AdaptiveAvgPool2D",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name,
            **layer_attrs)
S
SunAhong1993 已提交
94 95
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
96 97
                            current_outputs, scope_name)
        layer_inputs["output_size"] = inputs_name[1]
S
SunAhong1993 已提交
98
        current_inputs.append(inputs_name[1])
S
SunAhong1993 已提交
99 100 101 102 103 104
        graph.add_layer(
            "paddle.nn.functional.adaptive_avg_pool2d",
            inputs=layer_inputs,
            outputs=layer_outputs[1:],
            scope_name=scope_name,
            **layer_attrs)
S
SunAhong1993 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    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 已提交
121
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
122 123 124 125 126 127 128 129 130
    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(
S
SunAhong1993 已提交
131
        graph, inputs_node[0], inputs_name[0], current_outputs, scope_name, add_dim=True)
S
SunAhong1993 已提交
132 133
    layer_inputs["input"] = inputs_name[0]
    # 处理输入1,即%input.3
S
SunAhong1993 已提交
134
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
135 136
    layer_inputs["x"] = inputs_name[1]
    # 处理输入2,即%156
S
SunAhong1993 已提交
137
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
138 139 140 141 142 143 144 145
    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 已提交
146
                            current_outputs, scope_name)
S
SunAhong1993 已提交
147 148 149 150 151 152 153
        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 已提交
154
                            current_outputs, scope_name)
S
SunAhong1993 已提交
155 156 157 158 159 160 161
        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 已提交
162
        scope_name=scope_name,
S
SunAhong1993 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176
        **layer_attrs)
    return current_inputs, current_outputs


def aten_add(mapper, graph, node):
    """ 构造数值相加的PaddleLayer,该节点实现out = x + y。

    TorchScript示例:
        %296 : int = aten::add(%i.12, %288)
        参数含义:
        %296 (-): 相加结果。
        %i.12 (-): 输入数值 x。
        %288 (-): 输入数值 y。
    """
S
SunAhong1993 已提交
177
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
178 179 180 181 182 183 184
    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 已提交
185
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
186 187 188
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%288
    mapper._check_input(
S
SunAhong1993 已提交
189
        graph, inputs_node[1], inputs_name[1], current_outputs, scope_name, add_dim=True)
S
SunAhong1993 已提交
190 191 192 193
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
194
    graph.add_layer("prim.add", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208
    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 已提交
209
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
210 211 212 213 214 215 216 217
    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 已提交
218
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
219 220 221
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%150
    mapper._check_input(
S
SunAhong1993 已提交
222
        graph, inputs_node[1], inputs_name[1], current_outputs, scope_name, add_dim=True)
S
SunAhong1993 已提交
223 224 225 226 227 228 229 230
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入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],
S
SunAhong1993 已提交
231
                            current_outputs, scope_name)
S
SunAhong1993 已提交
232 233 234 235
        layer_inputs["alpha"] = inputs_name[2]
        current_inputs.append(inputs_name[2])

    graph.add_layer(
S
SunAhong1993 已提交
236
        "prim.add_", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, **layer_attrs)
S
SunAhong1993 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249
    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 已提交
250
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
251 252 253 254 255 256 257
    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 已提交
258
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
259 260
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%288
S
SunAhong1993 已提交
261
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
262 263 264 265
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
266
    graph.add_layer("prim.and", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279
    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 已提交
280
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
281 282 283 284 285 286
    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 已提交
287
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
288 289
    layer_inputs["list"] = inputs_name[0]
    # 处理输入1,即v.1
S
SunAhong1993 已提交
290
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
291 292 293 294
    layer_inputs["element"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
295
    graph.add_layer("prim.append", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
296 297 298 299 300 301 302 303 304
    return current_inputs, current_outputs


def aten_arange(mapper, graph, node):
    """ 构造以步长均匀分隔给定数值区间的PaddleLayer。

    TorchScript示例:
        有三种情况,分别处理。
    """
S
SunAhong1993 已提交
305
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    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 已提交
322
                                current_outputs, scope_name)
S
SunAhong1993 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
            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 已提交
338
                                current_outputs, scope_name)
S
SunAhong1993 已提交
339 340 341 342 343 344 345
            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 已提交
346
                                current_outputs, scope_name)
S
SunAhong1993 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
            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 已提交
362
                                current_outputs, scope_name)
S
SunAhong1993 已提交
363 364 365 366 367 368 369
            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 已提交
370
                                current_outputs, scope_name)
S
SunAhong1993 已提交
371 372 373 374 375 376 377
            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 已提交
378
                                current_outputs, scope_name)
S
SunAhong1993 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
            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 已提交
394
        scope_name=scope_name,
S
SunAhong1993 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
        **layer_attrs)
    return current_inputs, current_outputs


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 已提交
414 415
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool2d", mapper.nn_name2id)
S
SunAhong1993 已提交
416
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
417
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
418 419 420 421 422 423
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.34
S
SunAhong1993 已提交
424
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%538
    layer_attrs["pool_size"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%539
    layer_attrs["pool_stride"] = mapper.attrs[inputs_name[2]]
    # 处理输入3,即%540
    layer_attrs["pool_padding"] = mapper.attrs[inputs_name[3]]
    # 处理输入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 已提交
442
        outputs=[inputs_name[6] + "_assert"],
S
SunAhong1993 已提交
443
        scope_name=scope_name if scope_name == "" else scope_name + "_assert",
S
SunAhong1993 已提交
444 445 446
        type="eq",
        key=mapper.attrs[inputs_name[6]],
        value=None)
S
SunAhong1993 已提交
447 448 449 450 451 452 453 454 455 456

                # TODO(syf): The op has diff.
#         self.paddle_graph.add_layer(
#             kernel="paddle.nn.AvgPool2D",
#             inputs={"input": input_name},
#             outputs=layer_outputs,
#             kernel_size=k_size[2:4],
#             stride=strides[2:4],
#             padding=string(pad_mode))

S
SunAhong1993 已提交
457
    layer_attrs["pool_type"] = string("avg")
S
SunAhong1993 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 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
    graph.add_layer(
        "fluid.layers.pool2d",
        inputs=layer_inputs,
        outputs=layer_outputs[1:],
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs

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
    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,即%538
    layer_attrs["pool_size"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%539
    layer_attrs["pool_stride"] = mapper.attrs[inputs_name[2]]
    # 处理输入3,即%540
    layer_attrs["pool_padding"] = mapper.attrs[inputs_name[3]]
    # 处理输入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)

                # TODO(syf): The op has diff.
#         self.paddle_graph.add_layer(
#             kernel="paddle.nn.AvgPool2D",
#             inputs={"input": input_name},
#             outputs=layer_outputs,
#             kernel_size=k_size[2:4],
#             stride=strides[2:4],
#             padding=string(pad_mode))
S
SunAhong1993 已提交
523

S
SunAhong1993 已提交
524
    layer_attrs["pool_type"] = string("avg")
S
SunAhong1993 已提交
525
    graph.add_layer(
S
SunAhong1993 已提交
526
        "fluid.layers.pool3d",
S
SunAhong1993 已提交
527
        inputs=layer_inputs,
S
SunAhong1993 已提交
528 529 530 531 532 533
        outputs=layer_outputs[1:],
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
fix  
SunAhong1993 已提交
534
def aten_avg_pool1d(mapper, graph, node):
S
SunAhong1993 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    """ 构造最大池化的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
    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,即%538
    layer_attrs["pool_size"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%539
    layer_attrs["pool_stride"] = mapper.attrs[inputs_name[2]]
    # 处理输入3,即%540
    layer_attrs["pool_padding"] = mapper.attrs[inputs_name[3]]
    # 处理输入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)

                # TODO(syf): The op has diff.
#         self.paddle_graph.add_layer(
#             kernel="paddle.nn.AvgPool2D",
#             inputs={"input": input_name},
#             outputs=layer_outputs,
#             kernel_size=k_size[2:4],
#             stride=strides[2:4],
#             padding=string(pad_mode))

    layer_attrs["pool_type"] = string("avg")
    graph.add_layer(
        "fluid.layers.pool1d",
        inputs=layer_inputs,
        outputs=layer_outputs[1:],
        scope_name=scope_name,
S
SunAhong1993 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
        **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 已提交
620 621
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("batchnorm", mapper.nn_name2id)
S
SunAhong1993 已提交
622
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
623
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
624 625 626 627 628 629 630
    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 已提交
631
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
632 633 634 635 636
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%778
    weights = mapper.pytorch_params[inputs_name[1]]
S
SunAhong1993 已提交
637
    mapper.paddle_params[op_name + ".weight"] = weights
S
SunAhong1993 已提交
638 639 640 641 642
    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 已提交
643
            mapper.paddle_params[op_name + ".bias"] = bias
S
SunAhong1993 已提交
644
    else:
S
SunAhong1993 已提交
645
        mapper.paddle_params[op_name + ".bias"] = False
S
SunAhong1993 已提交
646 647
    # 处理输入3,即%776
    mean = mapper.pytorch_params[inputs_name[3]]
S
SunAhong1993 已提交
648
    mapper.paddle_params[op_name + "._mean"] = mean
S
SunAhong1993 已提交
649 650
    # 处理输入4,即%777
    var = mapper.pytorch_params[inputs_name[4]]
S
SunAhong1993 已提交
651
    mapper.paddle_params[op_name + "._variance"] = var
S
SunAhong1993 已提交
652 653 654 655 656 657 658 659 660
    # 处理输入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 已提交
661
        scope_name=scope_name,
S
SunAhong1993 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675
        **layer_attrs)
    return current_inputs, current_outputs


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 已提交
676
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
677 678 679 680 681 682 683 684
    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 已提交
685 686
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
687 688 689 690 691 692 693
    # 获取当前节点输入的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 已提交
694
                            current_outputs, scope_name)
S
SunAhong1993 已提交
695 696 697
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
S
SunAhong1993 已提交
698
        "paddle.concat",
S
SunAhong1993 已提交
699 700
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
701
        scope_name=scope_name,
S
SunAhong1993 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
        **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 已提交
717
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
718 719 720 721 722 723 724 725
    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 已提交
726 727
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
728 729 730 731 732 733 734
    # 获取当前节点输入的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 已提交
735
                            current_outputs, scope_name)
S
SunAhong1993 已提交
736 737 738 739
        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 已提交
740
        layer_attrs["axis"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
741 742
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
743 744
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[2]
S
SunAhong1993 已提交
745 746
        current_inputs.append(inputs_name[2])
    graph.add_layer(
S
SunAhong1993 已提交
747
        "paddle.split",
S
SunAhong1993 已提交
748 749
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
750
        scope_name=scope_name,
S
SunAhong1993 已提交
751 752 753 754
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
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
    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,即%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 已提交
805 806 807 808 809 810 811 812 813 814
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 已提交
815
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
816 817 818 819 820 821 822
    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 已提交
823
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
824 825
    layer_inputs["input"] = inputs_name[0]
    # 处理输入1,即%name.1
S
SunAhong1993 已提交
826
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
827 828 829 830
    layer_inputs["element"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
831
    graph.add_layer("prim.contain", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845
    return current_inputs, current_outputs


def aten_constant_pad_nd(mapper, graph, node):
    """ 构造填充固定值的PaddleLayer。

    TorchScript示例:
        %58 : Tensor = aten::constant_pad_nd(%input1.24, %4876, %42)
        参数含义:
        %58 (Tensor): 输出,填充后的Tensor。
        %input1.24 (Tensor): 需要填充的Tensor。
        %4876 (list): 填充大小。
        %42 (-): 填充值。
    """
S
SunAhong1993 已提交
846 847
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pad", mapper.nn_name2id)
S
SunAhong1993 已提交
848
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
849
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
850 851 852 853 854 855
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input1.24
S
SunAhong1993 已提交
856
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
857 858 859 860 861 862
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4876
    layer_attrs["padding"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%42
S
SunAhong1993 已提交
863
    layer_attrs["value"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
864 865

    graph.add_layer(
S
SunAhong1993 已提交
866
        "prim.shape",
S
SunAhong1993 已提交
867
        inputs={"input": inputs_name[0]},
S
SunAhong1993 已提交
868 869
        outputs=[inputs_name[0] + "_shape"],
        scope_name=scope_name)
S
SunAhong1993 已提交
870 871 872
    graph.add_layer(
        "prim.len",
        inputs={"input": inputs_name[0] + "_shape"},
S
SunAhong1993 已提交
873 874
        outputs=[inputs_name[0] + "_len"],
        scope_name=scope_name)
S
SunAhong1993 已提交
875 876 877 878 879 880

    def add_pad_layers(kernel, dim):
        graph.add_layer(
            "prim.ne",
            inputs={"x": inputs_name[0] + "_len"},
            outputs=[inputs_name[0] + "_cond"],
S
SunAhong1993 已提交
881
            scope_name=scope_name,
S
SunAhong1993 已提交
882 883 884
            y=dim)
        graph.add_layer(
            "prim.if", {'input': inputs_name[0] + "_cond"},
S
SunAhong1993 已提交
885 886
            outputs=[inputs_name[0] + "_if", output_name],
            scope_name=scope_name)
S
SunAhong1993 已提交
887
        if_layer = graph.layers[list(graph.layers.keys())[-1]]
S
SunAhong1993 已提交
888
        block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
889 890 891 892
        block.add_layer(
            "prim.sub",
            inputs={"y": inputs_name[0] + "_len"},
            outputs=[inputs_name[0] + "_len0"],
S
SunAhong1993 已提交
893
            scope_name=scope_name,
S
SunAhong1993 已提交
894 895 896 897
            x=dim)
        block.add_layer(
            "prim.len2list",
            inputs={"len": inputs_name[0] + "_len0"},
S
SunAhong1993 已提交
898 899
            outputs=[inputs_name[0] + "_list"],
            scope_name=scope_name)
S
SunAhong1993 已提交
900 901 902 903
        block.add_layer(
            "paddle.tensor.unsqueeze",
            inputs={"x": inputs_name[0],
                    "axis": inputs_name[0] + "_list"},
S
SunAhong1993 已提交
904 905
            outputs=[inputs_name[0] + "_var"],
            scope_name=scope_name)
S
SunAhong1993 已提交
906 907 908
        block.add_layer(
            kernel,
            inputs={"input": inputs_name[0] + "_var"},
S
SunAhong1993 已提交
909
            outputs=copy.deepcopy(layer_outputs),
S
SunAhong1993 已提交
910
            scope_name=scope_name,
S
SunAhong1993 已提交
911 912 913 914 915
            **layer_attrs)
        block.add_layer(
            "paddle.tensor.squeeze",
            inputs={"x": output_name,
                    "axis": inputs_name[0] + "_list"},
S
SunAhong1993 已提交
916 917
            outputs=[output_name],
            scope_name=scope_name)
S
SunAhong1993 已提交
918
        if_layer.add_block(block)
S
SunAhong1993 已提交
919
        block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
920 921
        layer_inputs["input"] = inputs_name[0]
        block.add_layer(
S
SunAhong1993 已提交
922
            kernel, inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, **layer_attrs)
S
SunAhong1993 已提交
923 924 925 926 927
        if_layer.add_block(block)
        if_layer.inputs["input-0"] = inputs_name[0]
        if_layer.inputs["input-1"] = inputs_name[0] + "_len"

    if len(layer_attrs["padding"]) == 2:
S
SunAhong1993 已提交
928
        add_pad_layers("paddle.nn.Pad1D", 3)
S
SunAhong1993 已提交
929
    elif len(layer_attrs["padding"]) == 4:
S
SunAhong1993 已提交
930
        add_pad_layers("paddle.nn.Pad2D", 4)
S
SunAhong1993 已提交
931
    elif len(layer_attrs["padding"]) == 6:
S
SunAhong1993 已提交
932
        add_pad_layers("paddle.nn.Pad3D", 5)
S
SunAhong1993 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
    else:
        raise Exception("The lenght of padding list must be 2, 4 or 6!")
    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 已提交
950
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
951 952 953 954 955 956 957 958
    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,即%4058
S
SunAhong1993 已提交
959
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
960 961 962 963
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
964
    graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
    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 已提交
980
        %30 (int): 空洞大小。
S
SunAhong1993 已提交
981 982
        %26 (int): 卷积的组数。
    """
S
SunAhong1993 已提交
983 984
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("conv2d", mapper.nn_name2id)
S
SunAhong1993 已提交
985
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
986
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
987 988 989 990 991 992
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.8
S
SunAhong1993 已提交
993
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
994 995 996 997 998
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%25
    weights = mapper.pytorch_params[inputs_name[1]]
S
SunAhong1993 已提交
999
    mapper.paddle_params[op_name + ".weight"] = weights
S
SunAhong1993 已提交
1000 1001 1002 1003 1004 1005
    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 已提交
1006
            mapper.paddle_params[op_name + ".bias"] = bias
S
SunAhong1993 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        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 已提交
1022
        "paddle.nn.Conv2D",
S
SunAhong1993 已提交
1023 1024
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
1025
        scope_name=scope_name,
S
SunAhong1993 已提交
1026 1027 1028 1029 1030 1031 1032 1033
        **layer_attrs)
    return current_inputs, current_outputs


def aten__convolution(mapper, graph, node):
    """ 构造conv2d的PaddleLayer。

    TorchScript示例:
S
SunAhong1993 已提交
1034
        %input.10 : Tensor = aten::_convolution(%input.1, %18, %10, %19, %20, %21, %13, %22, %12, %13, %13, %15)
S
SunAhong1993 已提交
1035 1036 1037
        参数含义:
        %input.10 (Tensor): 输出,卷积后的结果。
        %input.8 (Tensor): 需要进行卷积的特征层。
S
SunAhong1993 已提交
1038 1039 1040 1041
        %18 (Tensor): weights。
        %10 (Tensor): bias。
        %19 (list): 步长大小。
        %20 (list): 填充大小。
S
SunAhong1993 已提交
1042
        %21 (list): 空洞大小。
S
SunAhong1993 已提交
1043 1044 1045
        %13 (bool): 是否进行转置卷积。
        %22 (list): 输出形状上一侧额外添加的大小。
        %12 (int): 卷积的组数。
S
SunAhong1993 已提交
1046
    """
S
SunAhong1993 已提交
1047 1048
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("conv2d", mapper.nn_name2id)
S
SunAhong1993 已提交
1049
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
1050
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
1051 1052 1053 1054 1055 1056
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.8
S
SunAhong1993 已提交
1057
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1058 1059 1060
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1061
    # 处理输入1,即%18
S
SunAhong1993 已提交
1062
    weights = mapper.pytorch_params[inputs_name[1]]
S
SunAhong1993 已提交
1063 1064 1065 1066 1067 1068
    mapper.paddle_params[op_name + ".weight"] = weights #np.swapaxes(weights, 0, 1)
    if mapper.attrs[inputs_name[6]]:
        layer_attrs["out_channels"] = weights.shape[1]
    else:
        layer_attrs["out_channels"] = weights.shape[0]
    layer_attrs["kernel_size"] = weights.shape[2:]    
S
SunAhong1993 已提交
1069
    # 处理输入2,即%10
S
SunAhong1993 已提交
1070 1071 1072
    if inputs_name[2] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[2]]
        if bias is not None:
S
SunAhong1993 已提交
1073
            mapper.paddle_params[op_name + ".bias"] = bias
S
SunAhong1993 已提交
1074 1075 1076 1077
        else:
            layer_attrs["bias_attr"] = False
    else:
        layer_attrs["bias_attr"] = False
S
SunAhong1993 已提交
1078
    # 处理输入3,即%19
S
SunAhong1993 已提交
1079
    layer_attrs["stride"] = mapper.attrs[inputs_name[3]]
S
SunAhong1993 已提交
1080
    # 处理输入4,即%20
S
SunAhong1993 已提交
1081
    layer_attrs["padding"] = mapper.attrs[inputs_name[4]]
S
SunAhong1993 已提交
1082
    # 处理输入5,即%21
S
SunAhong1993 已提交
1083
    layer_attrs["dilation"] = mapper.attrs[inputs_name[5]]
S
SunAhong1993 已提交
1084 1085 1086 1087 1088 1089
    # 处理输入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 已提交
1090 1091 1092 1093 1094 1095
    if mapper.attrs[inputs_name[6]]:
        layer_attrs['in_channels'] = weights.shape[0] * mapper.attrs[inputs_name[
            8]]
    else:
        layer_attrs['in_channels'] = weights.shape[1] * mapper.attrs[inputs_name[
            8]]
S
SunAhong1993 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
    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 已提交
1110 1111 1112
    return current_inputs, current_outputs


S
SunAhong1993 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 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 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
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
    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,即%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]]
    layer_attrs['in_channels'] = weights.shape[0] * mapper.attrs[inputs_name[
            6]]
    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 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186
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 已提交
1187
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1188 1189 1190 1191 1192 1193 1194
    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 已提交
1195
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1196 1197 1198 1199
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1200
    graph.add_layer("paddle.cos", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
    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 已提交
1215
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1216 1217 1218 1219 1220 1221 1222 1223
    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 已提交
1224
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1225 1226 1227 1228 1229 1230 1231 1232
    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 已提交
1233
                            current_outputs, scope_name)
S
SunAhong1993 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
        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 已提交
1246
        scope_name=scope_name,
S
SunAhong1993 已提交
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
        **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 已提交
1262
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1263 1264 1265 1266 1267 1268 1269 1270
    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,即%end.1
S
SunAhong1993 已提交
1271
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1272 1273 1274
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1275
    graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

    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 已提交
1288
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1289 1290 1291 1292 1293 1294 1295
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
    current_inputs = {}
    # 获取当前节点输出的list
    current_outputs = [output_name]

S
SunAhong1993 已提交
1296
    graph.add_layer("prim.dict", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
    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 已提交
1309
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1310 1311 1312 1313 1314 1315 1316
    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.8
S
SunAhong1993 已提交
1317
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1318
    layer_inputs["input"] = inputs_name[0]
S
SunAhong1993 已提交
1319 1320 1321 1322
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
1323
        "prim.shape", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1324
    graph.add_layer(
S
SunAhong1993 已提交
1325
        "prim.len", inputs={"input": output_name}, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
    return current_inputs, current_outputs


def aten_div_(mapper, graph, node):
    """ 构造除法的PaddleLayer。

    TorchScript示例:
        %bx_bw0.3 : Tensor = aten::div_(%bx_bw.3, %2678)
        参数含义:
        %bx_bw0.3 (-): 除后的结果。
        %bx_bw.3 (-): 被除数。
        %2678 (int): 除数。
    """
    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 已提交
1346
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1347 1348
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
1349
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
1350 1351 1352 1353
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1354
    graph.add_layer("prim.div", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    return current_inputs, current_outputs


def aten_div(mapper, graph, node):
    """ 构造除法的PaddleLayer。

    TorchScript示例:
        %bx_bw0.3 : Tensor = aten::div_(%bx_bw.3, %2678)
        参数含义:
        %bx_bw0.3 (-): 除后的结果。
        %bx_bw.3 (-): 被除数。
        %2678 (int): 除数。
    """
S
SunAhong1993 已提交
1368
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1369 1370 1371 1372 1373 1374 1375
    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 已提交
1376
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1377 1378
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
1379
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
1380 1381 1382 1383
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1384
    graph.add_layer("prim.div", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
    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 已提交
1398 1399
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("dropout", mapper.nn_name2id)
S
SunAhong1993 已提交
1400
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
1401
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
1402 1403 1404 1405 1406
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%119
S
SunAhong1993 已提交
1407
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1408 1409 1410 1411 1412
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
1413
        "paddle.nn.Dropout", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, p=0.0)
S
SunAhong1993 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
    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 已提交
1427 1428
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("dropout", mapper.nn_name2id)
S
SunAhong1993 已提交
1429
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
1430
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
1431 1432 1433 1434 1435
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%119
S
SunAhong1993 已提交
1436
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1437 1438 1439 1440 1441
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
1442
        "paddle.nn.Dropout", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, p=0.0)
S
SunAhong1993 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
    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 已提交
1459 1460
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("embedding", mapper.nn_name2id)
S
SunAhong1993 已提交
1461
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
1462
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
1463 1464 1465 1466 1467 1468 1469
    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 已提交
1470 1471 1472
    mapper.paddle_params[op_name + ".weight"] = weights
    layer_attrs["num_embeddings"] = weights.shape[0]
    layer_attrs["embedding_dim"] = weights.shape[1]
S
SunAhong1993 已提交
1473
    # 处理输入1,即%input_ids.1
S
SunAhong1993 已提交
1474
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483
    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 已提交
1484
    layer_attrs["sparse"] = mapper.attrs[inputs_name[4]]
S
SunAhong1993 已提交
1485 1486

    graph.add_layer(
S
SunAhong1993 已提交
1487
        "paddle.nn.Embedding",
S
SunAhong1993 已提交
1488 1489
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
1490
        scope_name=scope_name,
S
SunAhong1993 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
        **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 已提交
1505
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1506 1507 1508 1509 1510 1511 1512
    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 已提交
1513
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1514 1515 1516 1517
    layer_inputs["x"] = inputs_name[0]
    x_value = list(node.inputs())[0]
    x_type = x_value.type()
    # 处理输入1,即%123
S
SunAhong1993 已提交
1518
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
1519 1520 1521 1522 1523
    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 已提交
1524
    graph.add_layer("prim.eq", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
    return current_inputs, current_outputs


def aten_exp(mapper, graph, node):
    """ 构造以自然数e为底指数运算的PaddleLayer。

    TorchScript示例:
        %55 : Tensor = aten::tanh(%54)
        参数含义:
        %55 (Tensor): 输出,运算后的结果。
        %54 (Tensor): 需要指数运算的Tensor。
    """
S
SunAhong1993 已提交
1537
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1538 1539 1540 1541 1542 1543 1544
    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 已提交
1545
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1546 1547 1548 1549 1550
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
1551
        "paddle.exp", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
    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 已提交
1566
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1567 1568 1569
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
S
SunAhong1993 已提交
1570
    layer_attrs = {}
S
SunAhong1993 已提交
1571 1572 1573 1574
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%1875
S
SunAhong1993 已提交
1575
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1576
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585
    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 已提交
1586
    graph.add_layer(
S
SunAhong1993 已提交
1587 1588 1589
        "paddle.expand", 
        inputs=layer_inputs, 
        outputs=layer_outputs, 
S
SunAhong1993 已提交
1590
        scope_name=scope_name,
S
SunAhong1993 已提交
1591
        **layer_attrs)
S
SunAhong1993 已提交
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
    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 已提交
1605
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1606 1607 1608 1609 1610 1611 1612
    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 已提交
1613
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1614 1615
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%1888
S
SunAhong1993 已提交
1616 1617
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
    layer_inputs["y"] = inputs_name[1]
S
SunAhong1993 已提交
1618 1619
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1620
    
S
SunAhong1993 已提交
1621 1622 1623
    graph.add_layer(
        "prim.type",
        inputs={"input": inputs_name[0]},
S
SunAhong1993 已提交
1624 1625
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
1626 1627 1628
    graph.add_layer(
        "prim.str",
        inputs={"input": inputs_name[0] + "_type"},
S
SunAhong1993 已提交
1629 1630
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
1631 1632 1633 1634
    graph.add_layer(
        "prim.eq",
        inputs={"x": inputs_name[0] + "_type"},
        outputs=[inputs_name[0] + "_cond"],
S
SunAhong1993 已提交
1635
        scope_name=scope_name,
S
SunAhong1993 已提交
1636 1637 1638
        y=string("VarType.BOOL"))
    graph.add_layer(
        "prim.if", {'input': inputs_name[0] + "_cond"},
S
SunAhong1993 已提交
1639 1640
        outputs=[inputs_name[0] + "_if1"],
        scope_name=scope_name)
S
SunAhong1993 已提交
1641
    if_layer = graph.layers[list(graph.layers.keys())[-1]]
S
SunAhong1993 已提交
1642
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
1643 1644 1645
    block.add_layer(
        "prim.type",
        inputs={"input": inputs_name[1]},
S
SunAhong1993 已提交
1646 1647
        outputs=[inputs_name[1] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
1648 1649 1650 1651
    block.add_layer(
        "fluid.layers.cast",
        inputs={"x": inputs_name[0]},
        outputs=[inputs_name[0]],
S
SunAhong1993 已提交
1652
        scope_name=scope_name,
S
SunAhong1993 已提交
1653 1654
        dtype=inputs_name[1] + "_type")
    if_layer.add_block(block)
S
SunAhong1993 已提交
1655
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
1656 1657 1658 1659
    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 已提交
1660
        "paddle.expand_as", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1661 1662
    graph.add_layer(
        "prim.if", {'input': inputs_name[0] + "_cond"},
S
SunAhong1993 已提交
1663 1664
        outputs=[inputs_name[0] + "_if2"],
        scope_name=scope_name)
S
SunAhong1993 已提交
1665
    if_layer = graph.layers[list(graph.layers.keys())[-1]]
S
SunAhong1993 已提交
1666
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
1667 1668 1669
    block.add_layer(
        "fluid.layers.cast",
        inputs={"x": layer_outputs[0]},
S
SunAhong1993 已提交
1670 1671
        outputs=copy.deepcopy(layer_outputs),
        scope_name=scope_name,
S
SunAhong1993 已提交
1672 1673 1674 1675 1676
        dtype=string("bool"))
    if_layer.add_block(block)
    block = PaddleGraph(if_layer, graph_type="dygraph")
    if_layer.add_block(block)
    if_layer.inputs["input-0"] = layer_outputs[0]
S
SunAhong1993 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
    # TODO(syf): check expand_as
#     # 处理输入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 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
    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 已提交
1705
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1706 1707 1708 1709 1710 1711 1712 1713
    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 已提交
1714
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1715 1716 1717 1718
    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 已提交
1719
                            current_outputs, scope_name)
S
SunAhong1993 已提交
1720 1721 1722 1723 1724 1725 1726
        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 已提交
1727
        "paddle.eye",
S
SunAhong1993 已提交
1728 1729
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
1730
        scope_name=scope_name,
S
SunAhong1993 已提交
1731 1732 1733
        **layer_attrs)
    return current_inputs, current_outputs

S
SunAhong1993 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
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
    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(
        "paddle.nn.Dropout", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, p=0.0)
    return current_inputs, current_outputs

S
SunAhong1993 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774

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 已提交
1775
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1776 1777 1778
    output_name = mapper._get_outputs_name(node)[0]
    layer_outputs = [output_name]
    layer_inputs = {}
S
SunAhong1993 已提交
1779
    layer_attrs = {}
S
SunAhong1993 已提交
1780 1781 1782 1783
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x
S
SunAhong1993 已提交
1784 1785 1786 1787 1788
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    # 处理输入1,即%4
    layer_attrs["start_axis"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%20
    layer_attrs["stop_axis"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
1789 1790 1791 1792 1793
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
1794
        "paddle.flatten",
S
SunAhong1993 已提交
1795 1796
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
1797 1798
        scope_name=scope_name,
        **layer_attrs)
S
SunAhong1993 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
    return current_inputs, current_outputs


def aten_Float(mapper, graph, node):
    """ 构造取浮点型的PaddleLayer。

    TorchScript示例:
        %3992 : float = aten::Float(%3991)
        参数含义:
        %3992 (int): 向上取整后的整数。
        %3991 (float): 需要取整的浮点数。
    """
S
SunAhong1993 已提交
1811
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1812 1813 1814 1815 1816 1817 1818
    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 已提交
1819
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1820 1821 1822 1823
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1824
    graph.add_layer("prim.float", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
    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 已提交
1837
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1838 1839 1840 1841 1842 1843 1844
    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 已提交
1845
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1846
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
1847 1848
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
S
SunAhong1993 已提交
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
    graph.add_layer(
        "prim.type", 
        {'input': inputs_name[0]},
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
    graph.add_layer(
        "prim.str", 
        {'input': inputs_name[0] + "_type"},
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
    graph.add_layer(
        "prim.startswith", 
        {'input': inputs_name[0] + "_type"},
        outputs=[inputs_name[0] + "_cond"],
        scope_name=scope_name,
        start_str=string("VarType")) 
    graph.add_layer(
        "prim.if", 
        {'input': inputs_name[0] + "_cond"},
        outputs=[inputs_name[0] + "_if"],
        scope_name=scope_name)
    if_layer = graph.layers[list(graph.layers.keys())[-1]]
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
    block.add_layer("paddle.floor", inputs=copy.deepcopy(layer_inputs), outputs=copy.deepcopy(layer_outputs), scope_name=scope_name)
    if_layer.add_block(block)
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
    block.add_layer("prim.floor", inputs=copy.deepcopy(layer_inputs), outputs=copy.deepcopy(layer_outputs), scope_name=scope_name)
    if_layer.add_block(block)
    if_layer.inputs["input-0"] = inputs_name[0]
    if_layer.outputs.append(output_name)
S
SunAhong1993 已提交
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
    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 已提交
1892
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1893 1894 1895 1896 1897 1898 1899
    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 已提交
1900
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1901 1902
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
1903
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
1904 1905 1906 1907
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1908
    graph.add_layer("prim.floordiv", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
    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 已提交
1922
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1923 1924 1925 1926 1927 1928 1929
    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 已提交
1930
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1931 1932
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
1933
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
1934 1935 1936 1937
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
1938
    graph.add_layer("prim.floordiv", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
    return current_inputs, current_outputs


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 已提交
1957
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
1958 1959 1960 1961 1962 1963 1964 1965
    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 已提交
1966
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
1967 1968 1969 1970 1971 1972 1973 1974
    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 已提交
1975
                            current_outputs, scope_name)
S
SunAhong1993 已提交
1976 1977 1978 1979 1980 1981 1982 1983 1984
        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 已提交
1985
        scope_name=scope_name,
S
SunAhong1993 已提交
1986 1987 1988 1989
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
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
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%18
    layer_attrs["dim"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%19
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
    layer_inputs["index"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    
    graph.add_layer(
        "custom_layer:Gather", 
        inputs=layer_inputs, 
        outputs=layer_outputs, 
        scope_name=scope_name,
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
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 已提交
2041 2042
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("gelu", mapper.nn_name2id)
S
SunAhong1993 已提交
2043
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
2044
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
2045 2046 2047 2048 2049
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
2050
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2051 2052 2053 2054 2055
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
2056
        "paddle.nn.GELU", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
    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 已提交
2070
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2071 2072 2073 2074 2075 2076 2077
    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 已提交
2078
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2079 2080
    layer_inputs["list"] = inputs_name[0]
    # 处理输入1,即%88
S
SunAhong1993 已提交
2081
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2082 2083 2084 2085
    layer_inputs["index"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2086
    graph.add_layer("prim.getitem", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
    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 已提交
2100
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2101 2102 2103 2104 2105 2106 2107
    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 已提交
2108
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2109 2110
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%78
S
SunAhong1993 已提交
2111
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2112 2113 2114 2115
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2116
    graph.add_layer("prim.gt", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
    return current_inputs, current_outputs


def aten_hardtanh_(mapper, graph, node):
    """ 构造hardtanh激活的PaddleLayer。

    TorchScript示例:
        %result.9 : Tensor = aten::hardtanh_(%input.20, %67, %66)
        参数含义:
        %result.9 (Tensor): 输出,hardtanh激活后的Tensor。
        %input.20 (Tensor): 需要hardtanh激活的Tensor。
        %67 (float): hardtanh激活的最小阈值。
        %66 (float): hardtanh激活的最大阈值。
    """
S
SunAhong1993 已提交
2131 2132
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("hardtanh", mapper.nn_name2id)
S
SunAhong1993 已提交
2133
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
2134
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
2135 2136 2137 2138 2139 2140
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.20
S
SunAhong1993 已提交
2141
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
    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]]

    graph.add_layer(
        'paddle.nn.Hardtanh',
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
2154
        scope_name=scope_name,
S
SunAhong1993 已提交
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
        **layer_attrs)
    return current_inputs, current_outputs


def aten_index_select(mapper, graph, node):
    """ 构造对dict加入元素的PaddleLayer。

    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 已提交
2170
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2171 2172 2173 2174 2175 2176 2177 2178
    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 已提交
2179
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2180 2181 2182 2183 2184 2185
    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 已提交
2186
                            current_outputs, scope_name)
S
SunAhong1993 已提交
2187 2188 2189
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%371
S
SunAhong1993 已提交
2190
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
2191 2192 2193 2194 2195 2196 2197 2198
    layer_inputs["index"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.index_select",
        inputs=layer_inputs,
        outputs=current_outputs,
S
SunAhong1993 已提交
2199
        scope_name=scope_name,
S
SunAhong1993 已提交
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
        **layer_attrs)
    return current_inputs, current_outputs


def aten_Int(mapper, graph, node):
    """ 构造强转为int的PaddleLayer。

    TorchScript示例:
        %1739 : int = aten::Int(%1738)
        参数含义:
        %1739 (int): 输出,int型数据。
        %1738 (-): 需要强转的数据。
    """
S
SunAhong1993 已提交
2213
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2214 2215 2216 2217 2218 2219 2220
    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 已提交
2221
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2222 2223 2224 2225
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2226
    graph.add_layer("prim.int", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
    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 已提交
2240
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2241 2242 2243 2244 2245 2246 2247
    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 已提交
2248
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2249 2250
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%3931
S
SunAhong1993 已提交
2251
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2252 2253 2254 2255
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2256
    graph.add_layer("prim.is", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
    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 已提交
2270
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2271 2272 2273 2274 2275 2276 2277
    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 已提交
2278
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2279 2280
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%3931
S
SunAhong1993 已提交
2281
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2282 2283 2284 2285
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2286
    graph.add_layer("prim.isnot", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
    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 已提交
2304 2305
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("layernorm", mapper.nn_name2id)
S
SunAhong1993 已提交
2306
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
2307
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
2308 2309 2310 2311 2312 2313
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.6
S
SunAhong1993 已提交
2314
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2315 2316 2317 2318 2319 2320 2321
    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 已提交
2322
    mapper.paddle_params[op_name + ".weight"] = weights
S
SunAhong1993 已提交
2323 2324 2325 2326
    # 处理输入3,即%173
    if inputs_name[3] in mapper.pytorch_params:
        bias = mapper.pytorch_params[inputs_name[3]]
        if bias is not None:
S
SunAhong1993 已提交
2327
            mapper.paddle_params[op_name + ".bias"] = bias
S
SunAhong1993 已提交
2328
    else:
S
SunAhong1993 已提交
2329
        mapper.paddle_params[op_name + ".bias"] = False
S
SunAhong1993 已提交
2330 2331 2332 2333 2334 2335 2336
    # 处理输入4,即%70
    layer_attrs["epsilon"] = mapper.attrs[inputs_name[4]]

    graph.add_layer(
        "paddle.nn.LayerNorm",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
2337
        scope_name=scope_name,
S
SunAhong1993 已提交
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
        **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 已提交
2352
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2353 2354 2355 2356 2357 2358 2359
    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 已提交
2360
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2361 2362
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%79
S
SunAhong1993 已提交
2363
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2364 2365 2366 2367
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2368
    graph.add_layer("prim.le", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
    return current_inputs, current_outputs


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。
        %1570 (float): 输入中的元素小于0时的斜率。
    """
S
SunAhong1993 已提交
2382 2383
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("leakly_relu", mapper.nn_name2id)
S
SunAhong1993 已提交
2384
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
2385
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
2386 2387 2388 2389 2390 2391
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
2392
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
    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 已提交
2403
        scope_name=scope_name,
S
SunAhong1993 已提交
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
        **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 已提交
2417
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2418 2419 2420 2421 2422 2423 2424
    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 已提交
2425
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2426 2427 2428 2429
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2430
    graph.add_layer("prim.len", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
    return current_inputs, current_outputs


def aten_log(mapper, graph, node):
    """ 构构造log的PaddleLayer。

    TorchScript示例:
        %787 : Tensor = aten::log(%786)
        参数含义:
        %787 (Tensor): 输出,取log的Tensor。
        %786 (Tensor): 需要获取log的Tensor。
    """
S
SunAhong1993 已提交
2443
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2444 2445 2446 2447 2448 2449 2450
    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 已提交
2451
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2452 2453 2454 2455 2456
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
2457
        "paddle.log", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
    return current_inputs, current_outputs


def aten_lt(mapper, graph, node):
    """ 构造对比大小的PaddleLayer。

    TorchScript示例:
        %80 : bool = aten::lt(%78, %79)
        参数含义:
        %80 (bool): 输出,第一个元素是否小于第二个元素。
        %78 (-): 需对比的输入1。
        %79 (-): 需对比的输入2。
    """
S
SunAhong1993 已提交
2471
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2472 2473 2474 2475 2476 2477 2478
    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 已提交
2479
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2480 2481
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%79
S
SunAhong1993 已提交
2482
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2483 2484 2485 2486
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2487
    graph.add_layer("prim.lt", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
    return current_inputs, current_outputs


def aten_masked_fill_(mapper, graph, node):
    """ 构造填充mask的PaddleLayer。

    TorchScript示例:
        %input.4 : Tensor = aten::masked_fill_(%scores.2, %mask.2, %46)
        参数含义:
        %input.4 (Tensor): 输出,填充后的结果。
        %scores.2 (Tensor): 需要填充的Tensor。
        %mask.2 (Tensor): bool型的Tensor,哪些位置需要填充。
        %46 (-): 填充的值。
    """
S
SunAhong1993 已提交
2502
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2503 2504 2505 2506 2507 2508 2509 2510 2511
    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_inputs = []
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.4
S
SunAhong1993 已提交
2512
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2513 2514 2515 2516
    current_inputs.append(inputs_name[0])
    graph.add_layer(
        "prim.type",
        inputs={"input": inputs_name[0]},
S
SunAhong1993 已提交
2517 2518
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2519
    # 处理输入1,即%scores.2
S
SunAhong1993 已提交
2520
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2521 2522 2523 2524
    current_inputs.append(inputs_name[1])
    graph.add_layer(
        "paddle.logical_not",
        inputs={"x": inputs_name[1]},
S
SunAhong1993 已提交
2525 2526
        outputs=[inputs_name[1] + "_not"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2527
    graph.add_layer(
S
SunAhong1993 已提交
2528
        "paddle.cast",
S
SunAhong1993 已提交
2529 2530
        inputs={"x": inputs_name[1]},
        outputs=[inputs_name[1] + "_mask"],
S
SunAhong1993 已提交
2531
        scope_name=scope_name,
S
SunAhong1993 已提交
2532 2533
        dtype=inputs_name[0] + "_type")
    graph.add_layer(
S
SunAhong1993 已提交
2534
        "paddle.cast",
S
SunAhong1993 已提交
2535 2536
        inputs={"x": inputs_name[1] + "_not"},
        outputs=[inputs_name[1] + "_not_mask"],
S
SunAhong1993 已提交
2537
        scope_name=scope_name,
S
SunAhong1993 已提交
2538 2539 2540 2541 2542
        dtype=inputs_name[0] + "_type")
    graph.add_layer(
        "paddle.multiply",
        inputs={"x": inputs_name[0],
                "y": inputs_name[1] + "_not_mask"},
S
SunAhong1993 已提交
2543 2544
        outputs=[inputs_name[0] + "_not_mask"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2545
    # 处理输入2,即%46
S
SunAhong1993 已提交
2546
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
2547 2548 2549 2550
    graph.add_layer(
        "prim.eq",
        inputs={"x": inputs_name[2]},
        outputs=[inputs_name[2] + "_cond1"],
S
SunAhong1993 已提交
2551
        scope_name=scope_name,
S
SunAhong1993 已提交
2552 2553 2554 2555 2556
        y="-float('inf')")
    graph.add_layer(
        "prim.eq",
        inputs={"x": inputs_name[2]},
        outputs=[inputs_name[2] + "_cond2"],
S
SunAhong1993 已提交
2557
        scope_name=scope_name,
S
SunAhong1993 已提交
2558 2559 2560 2561 2562 2563 2564
        y="float('inf')")
    graph.add_layer(
        "prim.or",
        inputs={
            "x": inputs_name[2] + "_cond1",
            "y": inputs_name[2] + "_cond2"
        },
S
SunAhong1993 已提交
2565 2566
        outputs=[inputs_name[2] + "_cond"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2567 2568
    graph.add_layer(
        "prim.if", {'input': inputs_name[2] + "_cond"},
S
SunAhong1993 已提交
2569 2570
        outputs=[inputs_name[2] + "_if"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2571
    if_layer = graph.layers[list(graph.layers.keys())[-1]]
S
SunAhong1993 已提交
2572
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
2573 2574 2575
    block.add_layer(
        "prim.equal",
        inputs={"input": inputs_name[1] + "_mask"},
S
SunAhong1993 已提交
2576 2577
        outputs=[inputs_name[2] + "_1"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2578
    if_layer.add_block(block)
S
SunAhong1993 已提交
2579
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
2580 2581 2582 2583
    block.add_layer(
        "prim.mul",
        inputs={"x": inputs_name[1] + "_mask",
                "y": inputs_name[2]},
S
SunAhong1993 已提交
2584 2585
        outputs=[inputs_name[2] + "_1"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2586 2587 2588 2589 2590
    if_layer.add_block(block)
    if_layer.inputs["input-0"] = inputs_name[1] + "_mask"
    if_layer.inputs["input-1"] = inputs_name[2]
    if_layer.outputs.append(inputs_name[2] + "_1")
    graph.add_layer(
S
SunAhong1993 已提交
2591
        "paddle.add",
S
SunAhong1993 已提交
2592 2593
        inputs={"x": inputs_name[2] + "_1",
                "y": inputs_name[0] + "_not_mask"},
S
SunAhong1993 已提交
2594 2595
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
    return current_inputs, current_outputs


def aten_masked_fill(mapper, graph, node):
    """ 构造填充mask的PaddleLayer。

    TorchScript示例:
        %input.4 : Tensor = aten::masked_fill(%scores.2, %mask.2, %46)
        参数含义:
        %input.4 (Tensor): 输出,填充后的结果。
        %scores.2 (Tensor): 需要填充的Tensor。
        %mask.2 (Tensor): bool型的Tensor,哪些位置需要填充。
        %46 (-): 填充的值。
    """
S
SunAhong1993 已提交
2610
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2611 2612 2613 2614 2615 2616 2617 2618 2619
    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_inputs = []
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%input.4
S
SunAhong1993 已提交
2620
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2621 2622 2623 2624
    current_inputs.append(inputs_name[0])
    graph.add_layer(
        "prim.type",
        inputs={"input": inputs_name[0]},
S
SunAhong1993 已提交
2625 2626
        outputs=[inputs_name[0] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2627
    # 处理输入1,即%scores.2
S
SunAhong1993 已提交
2628
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2629 2630 2631 2632
    current_inputs.append(inputs_name[1])
    graph.add_layer(
        "paddle.logical_not",
        inputs={"x": inputs_name[1]},
S
SunAhong1993 已提交
2633 2634
        outputs=[inputs_name[1] + "_not"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2635
    graph.add_layer(
S
SunAhong1993 已提交
2636
        "paddle.cast",
S
SunAhong1993 已提交
2637 2638
        inputs={"x": inputs_name[1]},
        outputs=[inputs_name[1] + "_mask"],
S
SunAhong1993 已提交
2639
        scope_name=scope_name,
S
SunAhong1993 已提交
2640 2641
        dtype=inputs_name[0] + "_type")
    graph.add_layer(
S
SunAhong1993 已提交
2642
        "paddle.cast",
S
SunAhong1993 已提交
2643 2644
        inputs={"x": inputs_name[1] + "_not"},
        outputs=[inputs_name[1] + "_not_mask"],
S
SunAhong1993 已提交
2645
        scope_name=scope_name,
S
SunAhong1993 已提交
2646 2647 2648 2649 2650
        dtype=inputs_name[0] + "_type")
    graph.add_layer(
        "paddle.multiply",
        inputs={"x": inputs_name[0],
                "y": inputs_name[1] + "_not_mask"},
S
SunAhong1993 已提交
2651 2652
        outputs=[inputs_name[0] + "_not_mask"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2653
    # 处理输入2,即%46
S
SunAhong1993 已提交
2654
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
2655 2656 2657 2658
    graph.add_layer(
        "prim.eq",
        inputs={"x": inputs_name[2]},
        outputs=[inputs_name[2] + "_cond1"],
S
SunAhong1993 已提交
2659
        scope_name=scope_name,
S
SunAhong1993 已提交
2660 2661 2662 2663 2664
        y="-float('inf')")
    graph.add_layer(
        "prim.eq",
        inputs={"x": inputs_name[2]},
        outputs=[inputs_name[2] + "_cond2"],
S
SunAhong1993 已提交
2665
        scope_name=scope_name,
S
SunAhong1993 已提交
2666 2667 2668 2669 2670 2671 2672
        y="float('inf')")
    graph.add_layer(
        "prim.or",
        inputs={
            "x": inputs_name[2] + "_cond1",
            "y": inputs_name[2] + "_cond2"
        },
S
SunAhong1993 已提交
2673 2674
        outputs=[inputs_name[2] + "_cond"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2675 2676
    graph.add_layer(
        "prim.if", {'input': inputs_name[2] + "_cond"},
S
SunAhong1993 已提交
2677 2678
        outputs=[inputs_name[2] + "_if"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2679
    if_layer = graph.layers[list(graph.layers.keys())[-1]]
S
SunAhong1993 已提交
2680
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
2681 2682 2683
    block.add_layer(
        "prim.equal",
        inputs={"input": inputs_name[1] + "_mask"},
S
SunAhong1993 已提交
2684 2685
        outputs=[inputs_name[2] + "_1"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2686
    if_layer.add_block(block)
S
SunAhong1993 已提交
2687
    block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
2688 2689 2690 2691
    block.add_layer(
        "prim.mul",
        inputs={"x": inputs_name[1] + "_mask",
                "y": inputs_name[2]},
S
SunAhong1993 已提交
2692 2693
        outputs=[inputs_name[2] + "_1"],
        scope_name=scope_name)
S
SunAhong1993 已提交
2694 2695 2696 2697 2698
    if_layer.add_block(block)
    if_layer.inputs["input-0"] = inputs_name[1] + "_mask"
    if_layer.inputs["input-1"] = inputs_name[2]
    if_layer.outputs.append(inputs_name[2] + "_1")
    graph.add_layer(
S
SunAhong1993 已提交
2699
        "paddle.add",
S
SunAhong1993 已提交
2700 2701
        inputs={"x": inputs_name[2] + "_1",
                "y": inputs_name[0] + "_not_mask"},
S
SunAhong1993 已提交
2702 2703
        outputs=layer_outputs,
        scope_name=scope_name)
S
SunAhong1993 已提交
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
    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 已提交
2717
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727
    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 已提交
2728
                            current_outputs, scope_name)
S
SunAhong1993 已提交
2729 2730 2731
        layer_inputs["x"] = inputs_name[0]
        # 处理输入1,即%159
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
2732
                            current_outputs, scope_name)
S
SunAhong1993 已提交
2733 2734 2735 2736
        layer_inputs["y"] = inputs_name[1]
        # 获取当前节点输入的list
        current_inputs = list(layer_inputs.values())
        graph.add_layer(
S
SunAhong1993 已提交
2737
            "paddle.maximum", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
    else:
        pass
    return current_inputs, current_outputs


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 已提交
2757 2758
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("pool2d", mapper.nn_name2id)
S
SunAhong1993 已提交
2759
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
2760
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
2761 2762
    layer_inputs = {}
    layer_attrs = {}
S
SunAhong1993 已提交
2763
    layer_attrs_tmp = {}
S
SunAhong1993 已提交
2764 2765 2766 2767
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.11
S
SunAhong1993 已提交
2768
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2769 2770 2771 2772
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%20
S
SunAhong1993 已提交
2773 2774
    layer_attrs["kernel_size"] = mapper.attrs[inputs_name[1]]
    layer_attrs_tmp["pool_size"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
2775
    # 处理输入2,即%23
S
SunAhong1993 已提交
2776 2777
    layer_attrs["stride"] = mapper.attrs[inputs_name[2]]
    layer_attrs_tmp["pool_stride"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
2778
    # 处理输入3,即%21
S
SunAhong1993 已提交
2779 2780
    layer_attrs["padding"] = mapper.attrs[inputs_name[3]]
    layer_attrs_tmp["pool_padding"] = mapper.attrs[inputs_name[3]]
S
SunAhong1993 已提交
2781 2782 2783 2784
    # 处理输入4,即%22
    graph.add_layer(
        "prim.assert",
        inputs={},
C
channingss 已提交
2785
        outputs=[inputs_name[4] + "_assert"],
S
SunAhong1993 已提交
2786
        scope_name=scope_name + "_assert",
S
SunAhong1993 已提交
2787 2788 2789 2790 2791
        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 已提交
2792
    layer_attrs_tmp["ceil_mode"] = mapper.attrs[inputs_name[5]]
S
SunAhong1993 已提交
2793

S
SunAhong1993 已提交
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808
    if mapper.attrs[inputs_name[5]] == True:
        layer_attrs["pool_type"] = string("max")
        graph.add_layer(
            "fluid.layers.pool2d",
            inputs=layer_inputs,
            outputs=layer_outputs[1:],
            scope_name=scope_name,
            **layer_attrs_tmp)
    else:
        graph.add_layer(
            "paddle.nn.MaxPool2D",
            inputs=layer_inputs,
            outputs=layer_outputs,
            scope_name=scope_name,
            **layer_attrs)
S
SunAhong1993 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821
    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 已提交
2822
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2823 2824 2825 2826 2827 2828 2829
    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 已提交
2830
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2831 2832
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%102
S
SunAhong1993 已提交
2833
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2834 2835 2836 2837
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
2838
    graph.add_layer("paddle.matmul", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851
    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 已提交
2852
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
    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 已提交
2863
                            current_outputs, scope_name)
S
SunAhong1993 已提交
2864 2865 2866
        layer_inputs["x"] = inputs_name[0]
        # 处理输入1,即%159
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
2867
                            current_outputs, scope_name)
S
SunAhong1993 已提交
2868 2869 2870 2871
        layer_inputs["y"] = inputs_name[1]
        # 获取当前节点输入的list
        current_inputs = list(layer_inputs.values())
        graph.add_layer(
S
SunAhong1993 已提交
2872
            "paddle.minimum", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
    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 已提交
2890
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2891 2892 2893 2894 2895 2896 2897 2898
    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 已提交
2899 2900
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
2901 2902 2903
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4967
    if inputs_name[1] in mapper.attrs:
S
SunAhong1993 已提交
2904
        layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
2905 2906
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
2907 2908
                            current_outputs, scope_name)
        layer_inputs["axis"] = inputs_name[1]
S
SunAhong1993 已提交
2909 2910 2911
        current_inputs.append(inputs_name[1])
    # 处理输入2,即%3
    if inputs_name[1] in mapper.attrs:
S
SunAhong1993 已提交
2912
        layer_attrs["keepdim"] = mapper.attrs[inputs_name[2]]
S
SunAhong1993 已提交
2913 2914
    else:
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
2915 2916
                            current_outputs, scope_name)
        layer_inputs["keepdim"] = inputs_name[2]
S
SunAhong1993 已提交
2917 2918 2919
        current_inputs.append(inputs_name[2])

    graph.add_layer(
S
SunAhong1993 已提交
2920
        "paddle.mean",
S
SunAhong1993 已提交
2921 2922
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
2923
        scope_name=scope_name,
S
SunAhong1993 已提交
2924 2925
        **layer_attrs)
    return current_inputs, current_outputs
S
SunAhong1993 已提交
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952


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
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    layer_inputs["args"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = layer_inputs.values()
    current_outputs = layer_outputs

    graph.add_layer("paddle.meshgrid", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
    return current_inputs, current_outputs
S
SunAhong1993 已提交
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964


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 已提交
2965
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2966 2967 2968 2969 2970 2971 2972
    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 已提交
2973
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
2974 2975
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%114
S
SunAhong1993 已提交
2976
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
2977 2978 2979 2980 2981
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    current_outputs = layer_outputs

S
SunAhong1993 已提交
2982
    graph.add_layer("prim.mul", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
    return current_inputs, current_outputs


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 已提交
2996
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
2997 2998 2999 3000 3001 3002 3003
    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 已提交
3004
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3005 3006
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%114
S
SunAhong1993 已提交
3007
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
3008 3009 3010 3011 3012
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    current_outputs = layer_outputs

S
SunAhong1993 已提交
3013
    graph.add_layer("prim.mul", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
    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 已提交
3027
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3028 3029 3030 3031 3032 3033 3034
    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 已提交
3035
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3036 3037
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%123
S
SunAhong1993 已提交
3038
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
3039 3040 3041 3042
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3043
    graph.add_layer("prim.ne", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055
    return current_inputs, current_outputs


def aten_neg(mapper, graph, node):
    """ 构造对数值取负的PaddleLayer。

    TorchScript示例:
        %909 : int = aten::neg(%908)
        参数含义:
        %909 (int): 取负后结果。
        %908 (int): 需取负的输入。
    """
S
SunAhong1993 已提交
3056
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3057 3058 3059 3060 3061 3062 3063
    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 已提交
3064
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3065 3066 3067 3068
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3069
    graph.add_layer("prim.neg", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
    return current_inputs, current_outputs


def aten___not__(mapper, graph, node):
    """ 构造对bool型取负的PaddleLayer。

    TorchScript示例:
        %4498 : bool = aten::__not__(%aux_defined.2)
        参数含义:
        %4498 (bool): 取负后结果。
        %aux_defined.2 (bool): 需取负的输入。
    """
S
SunAhong1993 已提交
3082
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3083 3084 3085 3086 3087 3088 3089
    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 已提交
3090
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3091 3092 3093 3094
    layer_inputs["input"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3095
    graph.add_layer("prim.not", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111
    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 已提交
3112
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
    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 已提交
3126
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3127 3128 3129 3130 3131 3132 3133 3134 3135
        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 已提交
3136
        scope_name=scope_name,
S
SunAhong1993 已提交
3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150
        **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 已提交
3151
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3152 3153 3154 3155 3156 3157 3158 3159
    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 已提交
3160
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3161 3162 3163 3164 3165 3166 3167 3168
    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 已提交
3169
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3170 3171 3172 3173
        layer_inputs["perm"] = inputs_name[1]
        current_inputs.append(inputs_name[1])

    graph.add_layer(
S
SunAhong1993 已提交
3174
        "paddle.transpose",
S
SunAhong1993 已提交
3175 3176
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
3177
        scope_name=scope_name,
S
SunAhong1993 已提交
3178 3179 3180 3181
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214
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
    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())
    # 处理输入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 已提交
3215 3216 3217 3218 3219 3220 3221 3222 3223
def aten_pow(mapper, graph, node):
    """ 构造指数激活的PaddleLayer。

    TorchScript示例:
        %x.6 : Tensor = aten::pow(%4700, %4703)
        参数含义:
        %x.6 (Tensor): 输出,指数激活后的Tensor。
        %4700 (Tensor): 需要指数激活的Tensor。
    """
S
SunAhong1993 已提交
3224
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3225 3226 3227 3228 3229 3230 3231 3232
    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 已提交
3233
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3234 3235 3236 3237 3238
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())
    # 处理输入1,即%4703
    if inputs_name[1] in mapper.attrs:
S
SunAhong1993 已提交
3239
        layer_attrs["y"] = mapper.attrs[inputs_name[1]]
S
SunAhong1993 已提交
3240 3241
    else:
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
3242 3243
                            current_outputs, scope_name)
        layer_inputs["y"] = inputs_name[1]
S
SunAhong1993 已提交
3244 3245 3246
        current_inputs.append(inputs_name[1])

    graph.add_layer(
S
SunAhong1993 已提交
3247
        "paddle.pow",
S
SunAhong1993 已提交
3248 3249
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
3250
        scope_name=scope_name,
S
SunAhong1993 已提交
3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265
        **layer_attrs)
    return current_inputs, current_outputs


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 已提交
3266 3267
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("relu", mapper.nn_name2id)
S
SunAhong1993 已提交
3268
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3269
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3270 3271 3272 3273 3274
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
3275
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3276 3277 3278 3279 3280
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
3281
        "paddle.nn.ReLU", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295
    return current_inputs, current_outputs


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 已提交
3296 3297
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("relu", mapper.nn_name2id)
S
SunAhong1993 已提交
3298
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3299
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3300 3301 3302 3303 3304
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
3305
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3306 3307 3308 3309 3310
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
3311
        "paddle.nn.ReLU", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
    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 已提交
3326 3327
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("relu6", mapper.nn_name2id)
S
SunAhong1993 已提交
3328
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3329
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3330 3331 3332 3333 3334
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
3335
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3336 3337 3338 3339 3340
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
3341
        "paddle.nn.ReLU6", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
    return current_inputs, current_outputs


def aten_repeat(mapper, graph, node):
    """ 构造根据参数对输入各维度进行复制的PaddleLayer。

    TorchScript示例:
        701 : Tensor = aten::repeat(%699, %700)
        参数含义:
        %701 (Tensor): 输出,复制后的Tensor。
        %699 (Tensor): 需要复制的Tensor。
        %700 (list): 指定每个维度复制的次数。
    """
S
SunAhong1993 已提交
3355
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3356 3357 3358 3359 3360 3361 3362 3363
    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 已提交
3364
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3365 3366 3367 3368 3369 3370 3371 3372
    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 已提交
3373
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3374 3375 3376 3377 3378 3379 3380
        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 已提交
3381
        scope_name=scope_name,
S
SunAhong1993 已提交
3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395
        **layer_attrs)
    return current_inputs, current_outputs


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 已提交
3396
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3397 3398 3399 3400 3401 3402 3403 3404
    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 已提交
3405
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3406 3407 3408 3409 3410 3411 3412 3413
    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 已提交
3414
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3415 3416
        layer_inputs["shape"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
S
SunAhong1993 已提交
3417
        
S
SunAhong1993 已提交
3418
    graph.add_layer(
S
SunAhong1993 已提交
3419
        "paddle.reshape",
S
SunAhong1993 已提交
3420 3421
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
3422
        scope_name=scope_name,
S
SunAhong1993 已提交
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
        **layer_attrs)
    return current_inputs, current_outputs


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 已提交
3438
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3439 3440 3441 3442 3443 3444 3445 3446
    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,即%30
S
SunAhong1993 已提交
3447
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3448 3449
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%13
S
SunAhong1993 已提交
3450
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
3451 3452
    layer_inputs["y"] = inputs_name[1]
    # 处理输入2,即%7
S
SunAhong1993 已提交
3453
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
3454 3455 3456 3457
    layer_inputs["alpha"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3458
    graph.add_layer("prim.rsub", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472
    return current_inputs, current_outputs


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 已提交
3473
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3474 3475 3476 3477 3478 3479 3480 3481
    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,即%end.1
S
SunAhong1993 已提交
3482
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3483 3484 3485 3486 3487 3488
    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 已提交
3489
            "prim.equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
    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 已提交
3508
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3509 3510 3511 3512 3513 3514 3515 3516
    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 已提交
3517
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3518 3519 3520 3521
    layer_inputs["input"] = inputs_name[0]
    # 处理输入1,即%8
    layer_attrs["dim"] = mapper.attrs[inputs_name[1]]
    # 处理输入2,即%75
S
SunAhong1993 已提交
3522
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
3523 3524 3525 3526 3527 3528 3529 3530
    layer_inputs["index"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
        "prim.select",
        inputs=layer_inputs,
        outputs=current_outputs,
S
SunAhong1993 已提交
3531
        scope_name=scope_name,
S
SunAhong1993 已提交
3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545
        **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 已提交
3546
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3547 3548 3549 3550 3551
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = []
    # 处理输入0,即%features.1
S
SunAhong1993 已提交
3552
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3553 3554
    layer_inputs["dict"] = inputs_name[0]
    # 处理输入1,即%out_name.1
S
SunAhong1993 已提交
3555
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
3556 3557
    layer_inputs["key"] = inputs_name[1]
    # 处理输入2,即%x.3
S
SunAhong1993 已提交
3558
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
3559 3560 3561 3562
    layer_inputs["value"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3563
    graph.add_layer("prim.set_item", inputs=layer_inputs, outputs=[], scope_name=scope_name)
S
SunAhong1993 已提交
3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
    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 已提交
3576 3577
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("sigmoid", mapper.nn_name2id)
S
SunAhong1993 已提交
3578
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3579
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3580 3581 3582 3583 3584
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%54
S
SunAhong1993 已提交
3585
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3586 3587 3588 3589 3590
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
3591
        "paddle.nn.Sigmoid", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603
    return current_inputs, current_outputs


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 已提交
3604
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3605 3606 3607 3608 3609 3610 3611
    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 已提交
3612
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3613 3614 3615 3616
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
3617
    graph.add_layer("paddle.sin", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630
    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 已提交
3631
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3632 3633 3634 3635 3636 3637 3638 3639
    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 已提交
3640
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3641 3642 3643 3644 3645 3646 3647 3648 3649
    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 已提交
3650
                                current_outputs, scope_name)
S
SunAhong1993 已提交
3651 3652 3653 3654 3655 3656
            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 已提交
3657
            scope_name=scope_name,
S
SunAhong1993 已提交
3658 3659 3660 3661
            **layer_attrs)
        return current_inputs, current_outputs

    graph.add_layer(
S
SunAhong1993 已提交
3662
        "prim.shape", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
    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 已提交
3679
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3680 3681 3682 3683 3684 3685 3686 3687 3688
    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 已提交
3689 3690
                            current_outputs, scope_name)
        layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
3691 3692 3693 3694 3695 3696 3697 3698 3699

        # 获取当前节点输入的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 已提交
3700
                scope_name=scope_name,
S
SunAhong1993 已提交
3701 3702 3703
                input0=mapper.attrs[inputs_name[1]])
        else:
            mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
3704
                                current_outputs, scope_name)
S
SunAhong1993 已提交
3705 3706 3707
            graph.add_layer(
                "prim.list",
                inputs={"input0": inputs_name[1]},
S
SunAhong1993 已提交
3708 3709
                outputs=[inputs_name[1] + "_list"],
                scope_name=scope_name)
S
SunAhong1993 已提交
3710 3711 3712 3713 3714 3715 3716 3717 3718 3719
            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 已提交
3720
                scope_name=scope_name,
S
SunAhong1993 已提交
3721 3722 3723
                input0=mapper.attrs[inputs_name[2]])
        else:
            mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
3724
                                current_outputs, scope_name)
S
SunAhong1993 已提交
3725 3726 3727
            graph.add_layer(
                "prim.list",
                inputs={"input0": inputs_name[2]},
S
SunAhong1993 已提交
3728 3729
                outputs=[inputs_name[2] + "_list"],
                scope_name=scope_name)
S
SunAhong1993 已提交
3730 3731 3732 3733 3734 3735 3736 3737 3738 3739
            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 已提交
3740
                scope_name=scope_name,
S
SunAhong1993 已提交
3741 3742 3743
                input0=mapper.attrs[inputs_name[3]])
        else:
            mapper._check_input(graph, inputs_node[3], inputs_name[3],
S
SunAhong1993 已提交
3744
                                current_outputs, scope_name)
S
SunAhong1993 已提交
3745 3746 3747
            graph.add_layer(
                "prim.list",
                inputs={"input0": inputs_name[3]},
S
SunAhong1993 已提交
3748 3749
                outputs=[inputs_name[3] + "_list"],
                scope_name=scope_name)
S
SunAhong1993 已提交
3750 3751 3752 3753 3754 3755 3756 3757 3758 3759
            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 已提交
3760
                scope_name=scope_name,
S
SunAhong1993 已提交
3761 3762 3763
                input0=mapper.attrs[inputs_name[4]])
        else:
            mapper._check_input(graph, inputs_node[4], inputs_name[4],
S
SunAhong1993 已提交
3764
                                current_outputs, scope_name)
S
SunAhong1993 已提交
3765 3766 3767
            graph.add_layer(
                "prim.list",
                inputs={"input0": inputs_name[4]},
S
SunAhong1993 已提交
3768 3769
                outputs=[inputs_name[4] + "_list"],
                scope_name=scope_name)
S
SunAhong1993 已提交
3770 3771 3772 3773 3774 3775
            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 已提交
3776
            "paddle.strided_slice",
S
SunAhong1993 已提交
3777
            inputs=layer_inputs,
S
SunAhong1993 已提交
3778 3779
            outputs=layer_outputs,
            scope_name=scope_name)
S
SunAhong1993 已提交
3780 3781 3782
    else:
        # 处理输入0,即%73
        mapper._check_input(graph, inputs_node[0], inputs_name[0],
S
SunAhong1993 已提交
3783
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3784 3785 3786
        layer_inputs["input"] = inputs_name[0]
        # 处理输入1,即%82
        mapper._check_input(graph, inputs_node[1], inputs_name[1],
S
SunAhong1993 已提交
3787
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3788 3789 3790
        layer_inputs["start"] = inputs_name[1]
        # 处理输入2,即%75
        mapper._check_input(graph, inputs_node[2], inputs_name[2],
S
SunAhong1993 已提交
3791
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3792 3793 3794
        layer_inputs["end"] = inputs_name[2]
        # 处理输入3,即%77
        mapper._check_input(graph, inputs_node[3], inputs_name[3],
S
SunAhong1993 已提交
3795
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3796 3797 3798 3799 3800
        layer_inputs["step"] = inputs_name[3]
        # 获取当前节点输入的list
        current_inputs = list(layer_inputs.values())

        graph.add_layer(
S
SunAhong1993 已提交
3801
            "prim.slice", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815
    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 已提交
3816 3817
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("softmax", mapper.nn_name2id)
S
SunAhong1993 已提交
3818
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3819
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3820 3821 3822 3823 3824 3825
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.31
S
SunAhong1993 已提交
3826
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3827 3828 3829 3830 3831 3832 3833 3834 3835
    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 已提交
3836
        scope_name=scope_name,
S
SunAhong1993 已提交
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851
        **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 已提交
3852 3853
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("softplus", mapper.nn_name2id)
S
SunAhong1993 已提交
3854
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
3855
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
3856 3857 3858 3859 3860 3861
    layer_inputs = {}
    layer_attrs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%x.31
S
SunAhong1993 已提交
3862
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3863 3864 3865 3866 3867 3868 3869 3870 3871 3872
    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 已提交
3873
        scope_name=scope_name,
S
SunAhong1993 已提交
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886
        **layer_attrs)
    return current_inputs, current_outputs


def aten_sqrt(mapper, graph, node):
    """ 构构造sqrt的PaddleLayer。

    TorchScript示例:
        %787 : Tensor = aten::sqrt(%786)
        参数含义:
        %787 (Tensor): 输出,取sqrt的Tensor。
        %786 (Tensor): 需要获取sqrt的Tensor。
    """
S
SunAhong1993 已提交
3887
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3888 3889 3890 3891 3892 3893 3894
    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 已提交
3895
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3896 3897 3898 3899 3900
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
3901
        "paddle.sqrt", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914
    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 已提交
3915
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3916 3917 3918 3919 3920 3921 3922 3923
    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 已提交
3924
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3925 3926 3927 3928 3929 3930 3931 3932
    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 已提交
3933
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3934 3935 3936 3937 3938 3939
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
        "paddle.tensor.squeeze",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
3940
        scope_name=scope_name,
S
SunAhong1993 已提交
3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
        **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 已提交
3955
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3956 3957 3958 3959 3960 3961 3962 3963
    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 已提交
3964
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
3965 3966 3967 3968 3969 3970 3971 3972
    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 已提交
3973
                            current_outputs, scope_name)
S
SunAhong1993 已提交
3974 3975 3976 3977 3978 3979
        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 已提交
3980
        scope_name=scope_name,
S
SunAhong1993 已提交
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994
        **layer_attrs)
    return current_inputs, current_outputs


def aten_sub(mapper, graph, node):
    """ 构造数值相减的PaddleLayer。

    TorchScript示例:
        %840 : int = aten::sub(%839, %836)
        参数含义:
        %840 (-): 相减结果。
        %839 (-): 输入数值 x。
        %836 (-): 输入数值 y。
    """
S
SunAhong1993 已提交
3995
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
3996 3997 3998 3999 4000 4001 4002
    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,即%839
S
SunAhong1993 已提交
4003
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4004 4005 4006
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%836
    mapper._check_input(
S
SunAhong1993 已提交
4007
        graph, inputs_node[1], inputs_name[1], current_outputs, scope_name, add_dim=True)
S
SunAhong1993 已提交
4008 4009 4010 4011
    layer_inputs["y"] = inputs_name[1]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
4012
    graph.add_layer("prim.sub", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024
    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 已提交
4025
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4026 4027 4028 4029 4030 4031 4032
    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 已提交
4033
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4034 4035 4036 4037 4038
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
4039
        "paddle.transpose",
S
SunAhong1993 已提交
4040 4041
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4042
        scope_name=scope_name,
S
SunAhong1993 已提交
4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055
        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 已提交
4056 4057
    scope_name = mapper.normalize_scope_name(node)
    op_name = name_generator("tanh", mapper.nn_name2id)
S
SunAhong1993 已提交
4058
    output_name = mapper._get_outputs_name(node)[0]
S
SunAhong1993 已提交
4059
    layer_outputs = [op_name, output_name]
S
SunAhong1993 已提交
4060 4061 4062 4063 4064
    layer_inputs = {}
    inputs_name, inputs_node = mapper._get_inputs_name(node)
    # 获取当前节点输出的list
    current_outputs = [output_name]
    # 处理输入0,即%result.5
S
SunAhong1993 已提交
4065
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4066 4067 4068 4069 4070
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入、输出的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
4071
        "paddle.nn.Tanh", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085
    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): 分割的数量。
        %723 (int): 轴。
    """
S
SunAhong1993 已提交
4086
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4087 4088 4089 4090 4091 4092 4093 4094
    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 已提交
4095 4096
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
    layer_inputs["x"] = inputs_name[0]
S
SunAhong1993 已提交
4097
    # 处理输入2,即%723
S
SunAhong1993 已提交
4098 4099
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
    layer_inputs["axis"] = inputs_name[2]
S
SunAhong1993 已提交
4100
    # 处理输入1,即%135
S
SunAhong1993 已提交
4101
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
4102 4103 4104 4105 4106 4107 4108 4109 4110
    input_type = list(node.inputs())[0].type()
    if "[]" in str(input_type):
        layer_inputs["num_or_sections"] = inputs_name[1]
    else:
        layer_attrs["num_or_sections"] = 1
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

    graph.add_layer(
S
SunAhong1993 已提交
4111
        "paddle.split",
S
SunAhong1993 已提交
4112 4113
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4114
        scope_name=scope_name,
S
SunAhong1993 已提交
4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129
        **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 已提交
4130
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4131 4132 4133 4134 4135 4136 4137 4138
    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.21
S
SunAhong1993 已提交
4139
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4140 4141
    layer_inputs["x"] = inputs_name[0]
    # 处理输入1,即%704
S
SunAhong1993 已提交
4142
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
4143 4144
    dim1 = inputs_name[1]
    # 处理输入2,即%705
S
SunAhong1993 已提交
4145
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
4146 4147
    dim2 = inputs_name[2]
    # 获取当前节点输入的list
S
SunAhong1993 已提交
4148
    current_inputs = list(layer_inputs.values())  
S
SunAhong1993 已提交
4149
    graph.add_layer(
S
SunAhong1993 已提交
4150
        "prim.shape",
S
SunAhong1993 已提交
4151
        inputs={"input": inputs_name[0]},
S
SunAhong1993 已提交
4152 4153
        outputs=[output_name + "_shape"],
        scope_name=scope_name)
S
SunAhong1993 已提交
4154 4155 4156 4157
    current_outputs.append(output_name + "_shape")
    graph.add_layer(
        "prim.len",
        inputs={"input": output_name + "_shape"},
S
SunAhong1993 已提交
4158 4159
        outputs=[output_name + "_len"],
        scope_name=scope_name)
S
SunAhong1993 已提交
4160 4161 4162 4163 4164
    current_outputs.append(output_name + "_len")
    current_inputs.append(output_name + "_shape")
    graph.add_layer(
        "prim.len2list",
        inputs={"len": output_name + "_len"},
S
SunAhong1993 已提交
4165 4166
        outputs=[output_name + "_list"],
        scope_name=scope_name)
S
SunAhong1993 已提交
4167 4168 4169 4170 4171 4172
    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 已提交
4173 4174
        outputs=[dim1 + "_new"],
        scope_name=scope_name)
S
SunAhong1993 已提交
4175 4176 4177 4178
    graph.add_layer(
        "prim.check_dim",
        inputs={"len": output_name + "_len",
                "dim": dim2},
S
SunAhong1993 已提交
4179 4180
        outputs=[dim2 + "_new"],
        scope_name=scope_name)
S
SunAhong1993 已提交
4181 4182 4183 4184 4185 4186 4187
    graph.add_layer(
        "prim.replaceitem",
        inputs={
            "list": output_name + "_list",
            "index": dim1 + "_new",
            "item": dim2 + "_new"
        },
S
SunAhong1993 已提交
4188 4189
        outputs=[],
        scope_name=scope_name)
S
SunAhong1993 已提交
4190 4191 4192 4193 4194 4195 4196
    graph.add_layer(
        "prim.replaceitem",
        inputs={
            "list": output_name + "_list",
            "index": dim2 + "_new",
            "item": dim1 + "_new"
        },
S
SunAhong1993 已提交
4197 4198
        outputs=[],
        scope_name=scope_name)
S
SunAhong1993 已提交
4199
    graph.add_layer(
S
SunAhong1993 已提交
4200
        "paddle.transpose",
S
SunAhong1993 已提交
4201 4202
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4203
        scope_name=scope_name,
S
SunAhong1993 已提交
4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217
        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 已提交
4218
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4219 4220 4221 4222 4223 4224 4225 4226
    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 已提交
4227
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4228 4229 4230 4231 4232 4233 4234 4235 4236 4237
    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 已提交
4238
        "paddle.cast",
S
SunAhong1993 已提交
4239 4240
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4241
        scope_name=scope_name,
S
SunAhong1993 已提交
4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255
        **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 已提交
4256
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4257 4258 4259 4260 4261 4262 4263
    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 已提交
4264
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4265 4266 4267 4268
    layer_inputs["x"] = inputs_name[0]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())
    # 处理输入0,即%mask.1
S
SunAhong1993 已提交
4269
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
4270 4271 4272
    graph.add_layer(
        "prim.type",
        inputs={"input": inputs_name[1]},
S
SunAhong1993 已提交
4273 4274
        outputs=[inputs_name[1] + "_type"],
        scope_name=scope_name)
S
SunAhong1993 已提交
4275 4276 4277 4278
    layer_inputs["dtype"] = inputs_name[1] + "_type"
    current_inputs.append(inputs_name[1])

    graph.add_layer(
S
SunAhong1993 已提交
4279
        "paddle.cast", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292
    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 已提交
4293
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4294 4295 4296 4297 4298 4299 4300 4301
    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 已提交
4302
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4303 4304 4305 4306 4307 4308 4309 4310
    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 已提交
4311
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4312 4313 4314
        layer_inputs["axis"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
S
SunAhong1993 已提交
4315
        "paddle.unsqueeze",
S
SunAhong1993 已提交
4316 4317
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4318
        scope_name=scope_name,
S
SunAhong1993 已提交
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335
        **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): 高度的乘数因子。
        %4995 (float): 宽度的乘数因子。
    """
S
SunAhong1993 已提交
4336
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4337 4338 4339 4340 4341 4342 4343 4344
    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 已提交
4345
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4346 4347 4348 4349 4350 4351 4352 4353
    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 已提交
4354
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4355 4356 4357 4358 4359 4360
        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 已提交
4361
            scope_name=scope_name,
S
SunAhong1993 已提交
4362
            cls="paddle.fluid.Variable")
S
SunAhong1993 已提交
4363
        # TODO(syf): paddle.Variable
S
SunAhong1993 已提交
4364 4365
        graph.add_layer(
            "prim.if", {"input": inputs_name[1] + "_isinstance"},
S
SunAhong1993 已提交
4366 4367
            outputs=[inputs_name[0] + "_if1"],
            scope_name=scope_name)
S
SunAhong1993 已提交
4368
        if_layer = graph.layers[list(graph.layers.keys())[-1]]
S
SunAhong1993 已提交
4369
        block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
4370 4371 4372
        block.add_layer(
            "prim.var2list",
            inputs={"input": inputs_name[1]},
S
SunAhong1993 已提交
4373 4374
            outputs=[inputs_name[1]],
            scope_name=scope_name)
S
SunAhong1993 已提交
4375
        if_layer.add_block(block)
S
SunAhong1993 已提交
4376
        block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
S
SunAhong1993 已提交
4377 4378 4379 4380 4381 4382 4383
        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 已提交
4384
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4385 4386
        layer_inputs["align_corners"] = inputs_name[2]
        current_inputs.append(inputs_name[2])
S
SunAhong1993 已提交
4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400
#     # 处理输入3和4,构造assert
#     list_layer_inputs = {}
#     mapper._check_input(graph, inputs_node[3], inputs_name[3], current_outputs, scope_name)
#     list_layer_inputs["key"] = inputs_name[3]
#     current_inputs.append(inputs_name[3])
#     mapper._check_input(graph, inputs_node[4], inputs_name[4], current_outputs, scope_name)
#     list_layer_inputs["value"] = inputs_name[4]
#     current_inputs.append(inputs_name[4])
#     graph.add_layer(
#         "prim.assert",
#         inputs=list_layer_inputs,
#         outputs=[output_name + "_assert"],
#         scope_name=scope_name,
#         type="eq")
S
SunAhong1993 已提交
4401 4402
    layer_inputs["scale_factor"] = inputs_name[3]
    layer_attrs["align_mode"] = 0
C
channingss 已提交
4403
    layer_attrs["mode"] = string("bilinear")
S
SunAhong1993 已提交
4404 4405 4406 4407
    graph.add_layer(
        "paddle.nn.functional.interpolate",
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4408
        scope_name=scope_name,
S
SunAhong1993 已提交
4409 4410 4411 4412
        **layer_attrs)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438
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
    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("prim.dict2values", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
    return current_inputs, current_outputs


S
SunAhong1993 已提交
4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454
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 已提交
4455
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4456 4457 4458 4459 4460 4461 4462 4463
    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 已提交
4464
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4465 4466 4467 4468 4469 4470 4471 4472
    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 已提交
4473
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4474 4475 4476
        layer_inputs["shape"] = inputs_name[1]
        current_inputs.append(inputs_name[1])
    graph.add_layer(
S
SunAhong1993 已提交
4477
        "paddle.reshape",
S
SunAhong1993 已提交
4478 4479
        inputs=layer_inputs,
        outputs=layer_outputs,
S
SunAhong1993 已提交
4480
        scope_name=scope_name,
S
SunAhong1993 已提交
4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493
        **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 已提交
4494
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4495 4496 4497 4498 4499 4500 4501 4502
    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 已提交
4503
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4504 4505 4506 4507 4508 4509 4510 4511
    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 已提交
4512
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4513 4514 4515 4516 4517 4518 4519
        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 已提交
4520
        scope_name=scope_name,
S
SunAhong1993 已提交
4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535
        **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 已提交
4536
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4537 4538 4539 4540 4541 4542 4543
    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 已提交
4544
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4545 4546
    layer_inputs["condition"] = inputs_name[0]
    # 处理输入1,即%w0.2
S
SunAhong1993 已提交
4547
    mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
S
SunAhong1993 已提交
4548 4549
    layer_inputs["x"] = inputs_name[1]
    # 处理输入1,即%w0.2
S
SunAhong1993 已提交
4550
    mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
S
SunAhong1993 已提交
4551 4552 4553 4554
    layer_inputs["y"] = inputs_name[2]
    # 获取当前节点输入的list
    current_inputs = list(layer_inputs.values())

S
SunAhong1993 已提交
4555
    graph.add_layer("paddle.where", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
S
SunAhong1993 已提交
4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571
    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 已提交
4572
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585
    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 已提交
4586
                            current_outputs, scope_name)
S
SunAhong1993 已提交
4587 4588 4589 4590 4591 4592 4593 4594 4595
        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 已提交
4596
        scope_name=scope_name,
S
SunAhong1993 已提交
4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614
        **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 已提交
4615
    scope_name = mapper.normalize_scope_name(node)
S
SunAhong1993 已提交
4616 4617 4618 4619 4620 4621 4622 4623
    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 已提交
4624
    mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
S
SunAhong1993 已提交
4625 4626 4627 4628 4629 4630 4631 4632 4633 4634
    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 已提交
4635
        scope_name=scope_name,
S
SunAhong1993 已提交
4636 4637
        **layer_attrs)
    return current_inputs, current_outputs