tf_op_mapper.py 55.7 KB
Newer Older
S
SunAhong1993 已提交
1
# Copyright (c) 2020  PaddlePaddle Authors. All Rights Reserved.
J
jiangjiajun 已提交
2 3 4 5 6 7 8 9 10 11 12 13
#
# 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.
J
jiangjiajun 已提交
14

S
SunAhong1993 已提交
15
from x2paddle.decoder.tf_decoder import TFGraph, TFGraphNode
S
release  
SunAhong1993 已提交
16
from x2paddle.core.program import PaddleGraph 
J
jiangjiajun 已提交
17
from x2paddle.core.op_mapper import OpMapper
J
jiangjiajun 已提交
18
from x2paddle.core.util import *
J
jiangjiajun 已提交
19 20 21
from x2paddle import program
import traceback
import math
J
jiangjiajun 已提交
22
import inspect
J
jiangjiajun 已提交
23
import numpy
J
jiangjiajun 已提交
24
import sys
25

J
jiangjiajun 已提交
26 27 28 29 30 31 32 33 34 35 36 37
name_counter = dict()


def gen_name(op_name, var_name):
    name = "{}_{}".format(op_name, var_name)
    if name not in name_counter:
        name_counter[name] = 0
    else:
        name_counter[name] += 1
    name = name + '_' + str(name_counter[name])
    return name

J
jiangjiajun 已提交
38

J
jiangjiajun 已提交
39 40 41 42
# compute padding size for SAME mode
def get_same_padding(in_size, kernel_size, stride):
    new_size = int(math.ceil(in_size * 1.0 / stride))
    pad_size = (new_size - 1) * stride + kernel_size - in_size
J
jiangjiajun 已提交
43 44
    if pad_size < 0:
        pad_size = 0
J
jiangjiajun 已提交
45 46 47 48
    pad0 = int(pad_size / 2)
    pad1 = pad_size - pad0
    return [pad0, pad1]

J
jiangjiajun 已提交
49

J
jiangjiajun 已提交
50
class TFOpMapper(OpMapper):
J
jiangjiajun 已提交
51
    directly_map_ops = {
S
SunAhong1993 已提交
52 53 54 55 56 57 58 59 60 61 62
        'Relu': ['paddle.nn.functional.relu'],
        'Relu6': ['paddle.nn.functional.relu6'],
        'Abs': ['paddle.abs'],
        'Sigmoid': ['paddle.nn.functional.sigmoid'],
        'Softmax': ['paddle.nn.functional.softmax'],
        'Exp': ['paddle.exp'],
        'Rsqrt': ['paddle.rsqrt'],
        'Sqrt': ['paddle.sqrt'],
        'swish_f32': ['paddle.nn.functional.swish'],
        'Tanh': ['paddle.tanh'],
        'Softplus': ['paddle.nn.functional.softplus'],
S
release  
SunAhong1993 已提交
63 64
        'LeakyRelu': ['paddle.nn.functional.leaky_relu', 
                     dict(alpha='negative_slope')],
S
SunAhong1993 已提交
65 66 67
        'Floor': ['paddle.floor'],
        'Erf': ['paddle.erf'],
        'Square': ['paddle.square']
J
jiangjiajun 已提交
68 69
    }
    elementwise_ops = {
S
SunAhong1993 已提交
70 71 72 73
        'Add': 'paddle.add',
        'AddV2': 'paddle.add',
        'RealDiv': 'paddle.divide',
        'DivNoNan': 'paddle.divide',
S
SunAhong1993 已提交
74
        # TODO (syf): replace
S
SunAhong1993 已提交
75
        'Sub': 'paddle.subtract',
S
SunAhong1993 已提交
76 77
        'Maximum': 'paddle.maximum',
        'Minimum': 'paddle.minimum',
S
SunAhong1993 已提交
78 79 80 81 82 83
        'Mul': 'paddle.multiply',
        'FloorDiv': 'paddle.floor_divide',
        'FloorMod': 'paddle.floor_mod',
        'LogicalAnd': 'logical_and',
    }
    bool_ops = {
S
SunAhong1993 已提交
84 85 86 87 88
        'LessEqual': 'paddle.less_equal',
        'GreaterEqual': 'paddle.greater_equal',
        'Greater': 'paddle.greater_than',
        'NotEqual': 'paddle.not_equal',
        'Equal': 'paddle.equal',
J
jiangjiajun 已提交
89 90
    }

J
jiangjiajun 已提交
91 92
    def __init__(self, decoder):
        super(TFOpMapper, self).__init__()
J
jiangjiajun 已提交
93
        self.decoder = decoder
J
jiangjiajun 已提交
94
        self.graph = decoder.tf_graph
S
SunAhong1993 已提交
95 96
        if not self.op_checker():
            raise Exception("Model is not supported yet.")
S
SunAhong1993 已提交
97
        self.params = dict()
S
release  
SunAhong1993 已提交
98
        self.paddle_graph = PaddleGraph(parent_layer=None, graph_type="static", source_type="tf")
S
SunAhong1993 已提交
99
        self.params_output2id = dict()
100

J
jiangjiajun 已提交
101 102
        not_placeholder = list()
        for name in self.graph.input_nodes:
J
jiangjiajun 已提交
103 104 105 106 107
            if self.graph.get_node(
                    name).layer_type != "Placeholder" and self.graph.get_node(
                        name
                    ).layer_type != "OneShotIterator" and self.graph.get_node(
                        name).layer_type != "IteratorV2":
J
jiangjiajun 已提交
108 109 110 111
                not_placeholder.append(name)
        for name in not_placeholder:
            idx = self.graph.input_nodes.index(name)
            del self.graph.input_nodes[idx]
J
jiangjiajun 已提交
112

S
SunAhong1993 已提交
113 114
        self.paddle_graph.inputs = self.graph.input_nodes
        self.paddle_graph.outputs = self.graph.output_nodes
J
jiangjiajun 已提交
115

S
SunAhong1993 已提交
116 117 118 119 120 121
        print("Total nodes: {}".format(
            sum([
                isinstance(node, TFGraphNode)
                for name, node in self.graph.node_map.items()
            ])))
        print("Nodes converting ...")
122
        for i, node_name in enumerate(self.graph.topo_sort):
J
jiangjiajun 已提交
123
            sys.stderr.write("\rConverting node {} ...     ".format(i + 1))
124 125
            node = self.graph.get_node(node_name)
            op = node.layer_type
J
jiangjiajun 已提交
126 127 128 129
            if op in self.directly_map_ops:
                self.directly_map(node)
            elif op in self.elementwise_ops:
                self.elementwise_map(node)
S
SunAhong1993 已提交
130 131
            elif op in self.bool_ops:
                self.bool_map(node)
J
jiangjiajun 已提交
132
            elif hasattr(self, op):
J
jiangjiajun 已提交
133
                func = getattr(self, op)
S
SunAhong1993 已提交
134 135 136 137
                func(node)
        print("\nNodes converted.")
        self.paddle_graph.set_name(self.graph.graph_name)
        self.paddle_graph.set_parameters(self.params)
S
release  
SunAhong1993 已提交
138
        
S
SunAhong1993 已提交
139 140 141 142 143 144 145
    def op_checker(self):
        unsupported_ops = set()
        for node_name in self.graph.topo_sort:
            node = self.graph.get_node(node_name)
            op = node.layer_type
            if not hasattr(self, op) and \
                op not in self.directly_map_ops and \
S
SunAhong1993 已提交
146 147
                op not in self.elementwise_ops and \
                op not in self.bool_ops:
J
jiangjiajun 已提交
148
                unsupported_ops.add(op)
S
SunAhong1993 已提交
149 150 151 152
        if len(unsupported_ops) == 0:
            return True
        else:
            if len(unsupported_ops) > 0:
S
release  
SunAhong1993 已提交
153 154
                print("\n========= {} OPs are not supported yet ===========".format(
                    len(unsupported_ops)))
J
jiangjiajun 已提交
155
            for op in unsupported_ops:
J
jiangjiajun 已提交
156
                print("========== {} ============".format(op))
S
SunAhong1993 已提交
157
            return False
J
jiangjiajun 已提交
158

J
jiangjiajun 已提交
159 160 161
    def directly_map(self, node):
        assert node.layer_type in self.directly_map_ops
        op_info = self.directly_map_ops[node.layer_type]
J
jiangjiajun 已提交
162
        input = self.graph.get_node(node.layer.input[0])
J
jiangjiajun 已提交
163 164 165 166 167 168
        attr = dict()
        for param in op_info[1:]:
            tf_param_name = list(param.keys())[0]
            pd_param_name = list(param.values())[0]
            tf_param = node.get_attr(tf_param_name)
            attr[pd_param_name] = tf_param
J
jiangjiajun 已提交
169

S
SunAhong1993 已提交
170
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
171
            kernel=op_info[0],
J
jiangjiajun 已提交
172 173 174
            inputs={"x": input.name},
            outputs=[node.name],
            **attr)
J
jiangjiajun 已提交
175

S
SunAhong1993 已提交
176 177 178 179
    def elementwise_map(self, node, op_type=None):
        if op_type is None:
            assert node.layer_type in self.elementwise_ops
            op_type = self.elementwise_ops[node.layer_type]
J
jiangjiajun 已提交
180 181
        x = self.graph.get_node(node.layer.input[0])
        y = self.graph.get_node(node.layer.input[1])
J
jiangjiajun 已提交
182 183
        x_shape = x.out_shapes[0]
        y_shape = y.out_shapes[0]
S
SunAhong1993 已提交
184
        layer_id = self.paddle_graph.add_layer(
S
SunAhong1993 已提交
185
            kernel=op_type,
J
jiangjiajun 已提交
186 187 188
            inputs={"x": x.name,
                    "y": y.name},
            outputs=[node.name])
S
release  
SunAhong1993 已提交
189 190
        self.paddle_graph.layers[layer_id].input_shapes = {"x": x_shape, "y": y_shape}
        
S
SunAhong1993 已提交
191 192 193 194
    def bool_map(self, node):
        op_type = self.bool_ops[node.layer_type]
        self.elementwise_map(node, op_type)
        node.set_dtype("bool")
J
jiangjiajun 已提交
195

196 197
    def Placeholder(self, node):
        shape = node.out_shapes[0]
J
jiangjiajun 已提交
198 199
        assert len(shape) != 0, "Unknown shape of input nodes[{}].".format(
            node.layer_name)
200
        dtype = node.dtype
S
SunAhong1993 已提交
201
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
202
            kernel="paddle.static.data",
J
jiangjiajun 已提交
203 204 205 206 207
            inputs={},
            outputs=[node.name],
            dtype=string(dtype),
            shape=shape,
            name=string(node.name))
J
jiangjiajun@baidu.com 已提交
208

J
jiangjiajun 已提交
209 210 211 212 213 214 215
    def Const(self, node):
        shape = node.out_shapes[0]
        dtype = node.dtype
        value = node.value
        if len(shape) == 0:
            assert value.size == 1, "Unexpected situation happend"
            shape = [1]
J
jiangjiajun 已提交
216 217
            if value == float('inf'):
                value = "float('inf')"
S
SunAhong1993 已提交
218
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
219
                kernel="paddle.full",
C
channingss 已提交
220 221 222 223
                inputs={},
                outputs=[node.name],
                dtype=string(dtype),
                shape=[1],
S
SunAhong1993 已提交
224
                fill_value=value)
C
channingss 已提交
225
            return
J
jiangjiajun 已提交
226

S
SunAhong1993 已提交
227
        self.params[node.name] = node.value
S
SunAhong1993 已提交
228
        layer_id = self.paddle_graph.add_layer(
S
SunAhong1993 已提交
229
            kernel="paddle.static.create_parameter",
J
jiangjiajun 已提交
230 231 232 233 234
            inputs={},
            outputs=[node.name],
            dtype=string(dtype),
            shape=shape,
            name=string(node.name),
S
SunAhong1993 已提交
235
            default_initializer="paddle.nn.initializer.Constant(value=0.0)")
S
SunAhong1993 已提交
236
        self.params_output2id[node.name] = layer_id
J
jiangjiajun 已提交
237 238

    def Transpose(self, node):
J
jiangjiajun 已提交
239 240
        input = self.graph.get_node(node.layer.input[0])
        perm = self.graph.get_node(node.layer.input[1])
S
SunAhong1993 已提交
241 242 243
        if perm.layer_type == "Const":
            perm = perm.value.tolist()
        else:
S
release  
SunAhong1993 已提交
244
            perm = self.decoder.infer_tensor(perm, use_diff_inputs=False).tolist()
J
jiangjiajun 已提交
245

S
SunAhong1993 已提交
246
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
247
            kernel="paddle.transpose",
J
jiangjiajun 已提交
248 249 250 251 252 253 254 255 256 257 258 259
            inputs={"x": input.name},
            outputs=[node.name],
            perm=perm)

    def Fill(self, node):
        dims = self.graph.get_node(node.layer.input[0])
        input_value = self.graph.get_node(node.layer.input[1])
        inputs = dict()
        attr = dict()
        assert input_value.layer_type == "Const", "Value of fill OP should be Const"
        if dims.layer_type == "Const":
            attr["shape"] = dims.value.tolist()
J
jiangjiajun 已提交
260
        else:
J
jiangjiajun 已提交
261 262
            inputs["shape"] = dims.name
        attr["dtype"] = string(input_value.dtype)
S
SunAhong1993 已提交
263
        attr["fill_value"] = input_value.value
J
jiangjiajun 已提交
264

S
SunAhong1993 已提交
265
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
266 267 268 269
            "paddle.full",
            inputs=inputs,
            outputs=[node.name],
            **attr)
S
SunAhong1993 已提交
270 271 272 273 274 275
        if dims.layer_type != "Const":
            self.paddle_graph.add_layer(
                "paddle.reshape",
                inputs={"x": node.name},
                outputs=[node.name],
                shape=node.out_shapes[0])
J
jiangjiajun 已提交
276

J
jiangjiajun 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289
    def DepthToSpace(self, node):
        input = self.graph.get_node(node.layer.input[0])

        block_size = node.get_attr("block_size")
        data_format = node.get_attr("data_format").decode()
        if data_format == "NHWC":
            n, h, w, c = input.out_shapes[0]
        else:
            n, c, h, w = input.out_shapes[0]

        input_name = input.name
        if data_format == "NHWC":
            transpose_name = gen_name("depth_to_space", "transpose")
S
SunAhong1993 已提交
290
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
291
                kernel="paddle.transpose",
J
jiangjiajun 已提交
292 293 294 295 296 297 298
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
            input_name = transpose_name

        shape = [0, block_size * block_size, -1, h, w]
        reshape_name = gen_name("depth_to_space", "reshape")
S
SunAhong1993 已提交
299
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
300
            kernel="paddle.reshape",
J
jiangjiajun 已提交
301 302 303 304 305
            inputs={"x": input_name},
            outputs=[reshape_name],
            shape=shape)

        transpose_name = gen_name("depth_to_space", "transpose")
S
SunAhong1993 已提交
306
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
307
            kernel="paddle.transpose",
J
jiangjiajun 已提交
308 309 310 311 312
            inputs={"x": reshape_name},
            outputs=[transpose_name],
            perm=[0, 2, 1, 3, 4])

        reshape_name = gen_name("depth_to_space", "reshape")
S
SunAhong1993 已提交
313
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
314
            kernel="paddle.reshape",
J
jiangjiajun 已提交
315 316 317 318
            inputs={"x": transpose_name},
            outputs=[reshape_name],
            shape=[0, c, h, w])

S
SunAhong1993 已提交
319
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
320
            kernel="paddle.nn.functional.pixel_shuffle",
J
jiangjiajun 已提交
321 322 323 324 325
            inputs={"x": reshape_name},
            outputs=[node.name],
            upscale_factor=block_size)

        if data_format == "NHWC":
S
SunAhong1993 已提交
326
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
327
                kernel="paddle.transpose",
J
jiangjiajun 已提交
328 329 330
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])
S
release  
SunAhong1993 已提交
331
            
S
SunAhong1993 已提交
332 333 334 335
    def Where(self, node):
        if len(node.layer.input) == 1:
            cond = self.graph.get_input_node(node, 0)
            self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
336 337 338
                "paddle.nonzero",
                inputs={"x": cond.name},
                outputs=[node.name])
S
SunAhong1993 已提交
339 340 341 342 343 344 345 346 347 348
        else:
            cond = self.graph.get_input_node(node, 0)
            x = self.graph.get_input_node(node, 1)
            y = self.graph.get_input_node(node, 2)
            self.paddle_graph.add_layer(
                "paddle.where",
                inputs={"condition": cond.name,
                        "x": x.name,
                        "y": y.name},
                outputs=[node.name])
S
release  
SunAhong1993 已提交
349
            
S
add beg  
SunAhong1993 已提交
350 351
    def Neg(self, node):
        input = self.graph.get_input_node(node, 0)
S
release  
SunAhong1993 已提交
352
        
S
add beg  
SunAhong1993 已提交
353 354 355 356 357
        self.paddle_graph.add_layer(
            "paddle.scale",
            inputs={"x": input.name},
            outputs=[node.name],
            scale=-1)
J
jiangjiajun 已提交
358 359 360

    def MaxPool(self, node):
        input = self.graph.get_node(node.layer.input[0])
J
jiangjiajun 已提交
361

J
jiangjiajun 已提交
362 363 364 365 366
        k_size = node.get_attr("ksize")
        strides = node.get_attr("strides")
        data_format = node.get_attr("data_format").decode()
        pad_mode = node.get_attr("padding").decode()

J
jiangjiajun 已提交
367 368 369
        input_name = input.name
        if data_format == "NHWC":
            transpose_name = gen_name("max_pool", "transpose")
S
SunAhong1993 已提交
370
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
371
                kernel="paddle.transpose",
J
jiangjiajun 已提交
372 373 374
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
J
jiangjiajun 已提交
375
            strides = [strides[i] for i in [0, 3, 1, 2]]
J
jiangjiajun 已提交
376
            k_size = [k_size[i] for i in [0, 3, 1, 2]]
J
jiangjiajun 已提交
377 378
            input_name = transpose_name

S
SunAhong1993 已提交
379
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
380 381
            kernel="paddle.nn.functional.max_pool2d",
            inputs={"x": input_name},
J
jiangjiajun 已提交
382
            outputs=[node.name],
S
SunAhong1993 已提交
383 384 385
            kernel_size=k_size[2:4],
            stride=strides[2:4],
            padding=string(pad_mode))
J
jiangjiajun 已提交
386 387

        if data_format == "NHWC":
S
SunAhong1993 已提交
388
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
389
                kernel="paddle.transpose",
J
jiangjiajun 已提交
390 391 392
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])
J
jiangjiajun 已提交
393 394

    def Conv2D(self, node):
J
jiangjiajun 已提交
395 396
        input = self.graph.get_node(node.layer.input[0])
        kernel = self.graph.get_node(node.layer.input[1])
J
jiangjiajun 已提交
397

J
jiangjiajun 已提交
398 399 400 401 402
        k_size = kernel.out_shapes[0]
        strides = node.get_attr("strides")
        dilations = node.get_attr("dilations")
        data_format = node.get_attr("data_format").decode()
        pad_mode = node.get_attr("padding").decode()
J
jiangjiajun 已提交
403 404 405 406
        if data_format == "NHWC":
            n, h, w, c = input.out_shapes[0]
        else:
            n, c, h, w = input.out_shapes[0]
J
jiangjiajun 已提交
407

J
jiangjiajun 已提交
408 409 410 411
        if kernel.layer_type == 'Const':
            kernel_value = kernel.value
            kernel_weight_name = kernel.name.replace('/', '_')
        else:
S
release  
SunAhong1993 已提交
412
            kernel_value = self.decoder.infer_tensor(kernel, use_diff_inputs=False)
J
jiangjiajun 已提交
413 414 415 416 417
            if kernel.layer_type == 'Split':
                kernel_weight_name = "{}_{}_kernel".format(node.name,
                                                           kernel.name)
            else:
                kernel_weight_name = kernel.name.replace('/', '_')
S
SunAhong1993 已提交
418
        self.params[kernel_weight_name] = numpy.transpose(kernel_value,
S
SunAhong1993 已提交
419 420 421 422 423 424 425 426
                                                          (3, 2, 0, 1))
        self.paddle_graph.add_layer(
            kernel="paddle.static.nn.create_parameter",
            inputs={},
            outputs=[kernel_weight_name],
            shape=self.params[kernel_weight_name].shape,
            dtype=string(str(self.params[kernel_weight_name].dtype)),
            name=string(kernel_weight_name))
S
release  
SunAhong1993 已提交
427
        
J
jiangjiajun 已提交
428 429
        input_name = input.name
        if data_format == "NHWC":
J
jiangjiajun 已提交
430 431
            strides = [strides[i] for i in [0, 3, 1, 2]]
            dilations = [dilations[i] for i in [0, 3, 1, 2]]
J
jiangjiajun 已提交
432
            transpose_name = gen_name("conv2d", "transpose")
S
SunAhong1993 已提交
433
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
434
                kernel="paddle.transpose",
J
jiangjiajun 已提交
435 436 437 438 439 440 441
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
            input_name = transpose_name

        if c == -1:
            attr = {"shape": [0, k_size[2], 0, 0]}
S
SunAhong1993 已提交
442
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
443
                kernel="paddle.reshape",
J
jiangjiajun 已提交
444 445 446 447
                inputs={"x": input_name},
                outputs=[input_name],
                shape=[0, k_size[2], 0, 0])

S
SunAhong1993 已提交
448
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
449
            kernel="paddle.nn.functional.conv2d",
S
release  
SunAhong1993 已提交
450
            inputs={"x": input_name, "weight": kernel_weight_name},
J
jiangjiajun 已提交
451
            outputs=[node.name],
S
SunAhong1993 已提交
452
            bias=None,
J
jiangjiajun 已提交
453 454 455 456 457
            stride=strides[2:4],
            dilation=dilations[2:4],
            padding=string(pad_mode))

        if data_format == "NHWC":
S
SunAhong1993 已提交
458
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
459
                kernel="paddle.transpose",
J
jiangjiajun 已提交
460 461 462
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])
S
release  
SunAhong1993 已提交
463
            
S
SunAhong1993 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    def Conv3D(self, node):
        input = self.graph.get_input_node(node, 0)
        kernel = self.graph.get_input_node(node, 1)

        k_size = kernel.out_shapes[0]
        strides = node.get_attr("strides")
        dilations = node.get_attr("dilations")
        data_format = node.get_attr("data_format").decode()
        pad_mode = node.get_attr("padding").decode()
        if data_format == "NDHWC":
            n, d, h, w, c = input.out_shapes[0]
        else:
            n, c, d, h, w = input.out_shapes[0]

        if kernel.layer_type == 'Const':
            kernel_value = kernel.value
            kernel_weight_name = kernel.name.replace('/', '_')
        else:
S
release  
SunAhong1993 已提交
482
            kernel_value = self.decoder.infer_tensor(kernel, use_diff_inputs=False)
S
SunAhong1993 已提交
483 484 485 486 487
            if kernel.layer_type == 'Split':
                kernel_weight_name = "{}_{}_kernel".format(node.name,
                                                           kernel.name)
            else:
                kernel_weight_name = kernel.name.replace('/', '_')
S
SunAhong1993 已提交
488 489 490 491 492 493 494 495 496
        self.params[kernel_weight_name] = numpy.transpose(kernel_value,
                                                          (4, 3, 0, 1, 2))
        self.paddle_graph.add_layer(
            kernel="paddle.static.nn.create_parameter",
            inputs={},
            outputs=[kernel_weight_name],
            shape=self.params[kernel_weight_name].shape,
            dtype=string(str(self.params[kernel_weight_name].dtype)),
            name=string(kernel_weight_name))
S
release  
SunAhong1993 已提交
497
        
S
SunAhong1993 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
        input_name = input.name
        if data_format == "NDHWC":
            strides = [strides[i] for i in [0, 4, 1, 2, 3]]
            dilations = [dilations[i] for i in [0, 4, 1, 2, 3]]
            transpose_name = gen_name("conv3d", "transpose")
            self.paddle_graph.add_layer(
                kernel="paddle.transpose",
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 4, 1, 2, 3])
            input_name = transpose_name

        if c == -1:
            attr = {"shape": [0, k_size[2], 0, 0, 0]}
            self.paddle_graph.add_layer(
                kernel="paddle.reshape",
                inputs={"x": input_name},
                outputs=[input_name],
S
release  
SunAhong1993 已提交
516 517
                shape=[0, k_size[2], 0, 0, 0])        
            
S
SunAhong1993 已提交
518 519
        self.paddle_graph.add_layer(
            kernel="paddle.nn.functional.conv3d",
S
release  
SunAhong1993 已提交
520
            inputs={"x": input_name,  "weight": kernel_weight_name},
S
SunAhong1993 已提交
521 522 523 524 525 526 527 528 529 530 531 532
            outputs=[node.name],
            bias=None,
            stride=strides[2:5],
            dilation=dilations[2:5],
            padding=string(pad_mode))

        if data_format == "NDHWC":
            self.paddle_graph.add_layer(
                kernel="paddle.transpose",
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 4, 1])
J
jiangjiajun 已提交
533

J
jiangjiajun 已提交
534
    def BiasAdd(self, node):
J
jiangjiajun 已提交
535 536
        input = self.graph.get_node(node.layer.input[0])
        bias = self.graph.get_node(node.layer.input[1])
S
SunAhong1993 已提交
537
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
538
            kernel="paddle.add",
J
jiangjiajun 已提交
539 540 541
            inputs={"x": input.name,
                    "y": bias.name},
            outputs=[node.name])
J
jiangjiajun 已提交
542 543

    def FusedBatchNorm(self, node):
J
jiangjiajun 已提交
544 545 546 547 548
        input = self.graph.get_node(node.layer.input[0])
        gamma = self.graph.get_node(node.layer.input[1])
        beta = self.graph.get_node(node.layer.input[2])
        moving_mean = self.graph.get_node(node.layer.input[3])
        moving_var = self.graph.get_node(node.layer.input[4])
J
jiangjiajun 已提交
549
        data_format = node.get_attr("data_format").decode()
J
jiangjiajun 已提交
550 551 552 553 554

        assert gamma.layer_type == "Const"
        assert beta.layer_type == "Const"
        assert moving_mean.layer_type == "Const"
        assert moving_var.layer_type == "Const"
J
jiangjiajun 已提交
555 556 557 558

        input_name = input.name
        if data_format == "NHWC":
            transpose_name = gen_name("batch_norm", "transpose")
S
SunAhong1993 已提交
559
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
560
                kernel="paddle.transpose",
J
jiangjiajun 已提交
561 562 563 564 565
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
            input_name = transpose_name

S
SunAhong1993 已提交
566
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
567
            kernel="paddle.nn.functional.batch_norm",
S
release  
SunAhong1993 已提交
568 569 570 571 572
            inputs={"x": input_name,
                    "running_mean": moving_mean.name,
                    "running_var": moving_var.name,
                    "weight": gamma.name,
                    "bias": beta.name},
J
jiangjiajun 已提交
573
            outputs=[node.name],
S
SunAhong1993 已提交
574
            epsilon=node.get_attr("epsilon"))
J
jiangjiajun 已提交
575 576

        if data_format == "NHWC":
S
SunAhong1993 已提交
577
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
578
                kernel="paddle.transpose",
J
jiangjiajun 已提交
579 580 581
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])
S
release  
SunAhong1993 已提交
582
            
S
SunAhong1993 已提交
583 584
    def FusedBatchNormV3(self, node):
        self.FusedBatchNorm(node)
J
jiangjiajun 已提交
585 586 587 588 589 590 591 592

    def Mean(self, node):
        input = self.graph.get_node(node.layer.input[0])
        reduce_idx = self.graph.get_node(node.layer.input[1])
        assert reduce_idx.layer_type == "Const", "Only support Const parameter[reduce_idx]"
        dims = reduce_idx.value.tolist()
        keep_dims = node.get_attr("keep_dims")

S
SunAhong1993 已提交
593
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
594 595
            kernel="paddle.mean",
            inputs={"x": input.name},
J
jiangjiajun 已提交
596
            outputs=[node.name],
S
SunAhong1993 已提交
597 598
            axis=dims,
            keepdim=keep_dims)
J
jiangjiajun 已提交
599 600

    def Reshape(self, node):
S
SunAhong1993 已提交
601 602
        input = self.graph.get_input_node(node, 0)
        param = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
603 604 605 606 607

        input_name = input.name

        if param.layer_type == "Const":
            shape = param.value.tolist()
S
SunAhong1993 已提交
608
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
609
                kernel="paddle.reshape",
J
jiangjiajun 已提交
610 611 612 613
                inputs={"x": input_name},
                outputs=[node.name],
                shape=shape)
        else:
S
SunAhong1993 已提交
614
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
615
                kernel="paddle.reshape",
J
jiangjiajun 已提交
616 617 618 619 620 621 622
                inputs={"x": input_name,
                        "shape": param.name},
                outputs=[node.name])
        if param.layer_type != "Const":
            out_shape = numpy.array(node.out_shapes[0])
            if (out_shape > 0).any():
                out_shape[out_shape < 0] = 0
S
SunAhong1993 已提交
623
                self.paddle_graph.add_layer(
S
SunAhong1993 已提交
624
                    kernel="paddle.reshape",
J
jiangjiajun 已提交
625 626 627 628 629
                    inputs={"x": node.name},
                    outputs=[node.name],
                    shape=out_shape.tolist())

    def Pad(self, node):
S
SunAhong1993 已提交
630 631
        input = self.graph.get_input_node(node, 0)
        paddings = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
632 633
        assert paddings.layer_type == "Const", "Padding should be Const"
        paddings = paddings.value.flatten().tolist()
S
SunAhong1993 已提交
634 635 636 637 638
        constant_values = 0
        if len(node.layer.input) > 2:
            constant_values = self.graph.get_input_node(node, 2)
            assert constant_values.layer_type == "Const", "Padding should be Const"
            constant_values = constant_values.value
J
jiangjiajun 已提交
639

S
SunAhong1993 已提交
640
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
641
            kernel="paddle.nn.functional.pad",
S
SunAhong1993 已提交
642
            inputs={"x": input.name},
J
jiangjiajun 已提交
643
            outputs=[node.name],
S
SunAhong1993 已提交
644 645
            pad=paddings,
            value=constant_values)
S
release  
SunAhong1993 已提交
646
        
S
SunAhong1993 已提交
647
    def MirrorPad(self, node):
S
SunAhong1993 已提交
648
        self.Pad(node)
S
release  
SunAhong1993 已提交
649 650
        
        
S
SunAhong1993 已提交
651 652
    def PadV2(self, node):
        self.Pad(node)
J
jiangjiajun 已提交
653 654

    def Squeeze(self, node):
S
SunAhong1993 已提交
655
        input = self.graph.get_input_node(node, 0)
J
jiangjiajun 已提交
656
        squeeze_dims = node.get_attr('squeeze_dims')
S
SunAhong1993 已提交
657
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
658 659
            kernel="paddle.squeeze",
            inputs={"x": input.name},
J
jiangjiajun 已提交
660
            outputs=[node.name],
S
SunAhong1993 已提交
661
            axis=squeeze_dims)
J
jiangjiajun 已提交
662 663

    def Shape(self, node):
S
SunAhong1993 已提交
664
        input = self.graph.get_input_node(node, 0)
J
jiangjiajun 已提交
665
        input_name = input.name
S
SunAhong1993 已提交
666
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
667
            kernel="paddle.shape",
J
jiangjiajun 已提交
668 669 670
            inputs={"input": input_name},
            outputs=[node.name])

S
SunAhong1993 已提交
671 672 673 674
    def Size(self, node):
        input = self.graph.get_input_node(node, 0)
        input_name = input.name
        self.paddle_graph.add_layer(
S
fix  
SunAhong1993 已提交
675
            kernel="paddle.shape",
S
SunAhong1993 已提交
676 677
            inputs={"input": input_name},
            outputs=[node.name])
S
fix  
SunAhong1993 已提交
678
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
679 680 681 682
            kernel="paddle.prod",
            inputs={"x": node.name},
            outputs=[node.name])
        
S
SunAhong1993 已提交
683 684 685
    def Ceil(self, node):
        input = self.graph.get_input_node(node, 0)
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
686 687
            kernel="paddle.ceil",
            inputs={"x": input.name},
S
SunAhong1993 已提交
688 689
            outputs=[node.name])

J
jiangjiajun 已提交
690
    def ArgMax(self, node):
S
SunAhong1993 已提交
691 692
        input = self.graph.get_input_node(node, 0)
        axis = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
693 694
        assert axis.layer_type == "Const", "ArgMax only support Const parameter"
        axis = axis.value
S
SunAhong1993 已提交
695
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
696
            kernel="paddle.argmax",
J
jiangjiajun 已提交
697 698 699
            inputs={"x": input.name},
            outputs=[node.name],
            axis=axis)
S
release  
SunAhong1993 已提交
700
        
S
SunAhong1993 已提交
701 702 703 704 705 706 707 708 709 710 711 712
    def TopKV2(self, node):
        input = self.graph.get_input_node(node, 0)
        k = self.graph.get_input_node(node, 1)
        assert k.layer_type == "Const", "ArgMax only support Const parameter"
        k = k.value
        sort = node.get_attr('sorted')
        self.paddle_graph.add_layer(
            kernel="paddle.topk",
            inputs={"x": input.name},
            outputs=[node.name],
            k=k,
            sorted=sort)
J
jiangjiajun 已提交
713 714

    def MatMul(self, node):
S
SunAhong1993 已提交
715 716
        x = self.graph.get_input_node(node, 0)
        y = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
717 718 719 720 721 722
        transpose_a = node.get_attr('transpose_a')
        transpose_b = node.get_attr('transpose_b')
        if transpose_a is None:
            transpose_a = node.get_attr('adj_x')
        if transpose_b is None:
            transpose_b = node.get_attr('adj_y')
S
SunAhong1993 已提交
723
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
724
            kernel="paddle.matmul",
J
jiangjiajun 已提交
725 726 727 728 729 730 731 732 733 734 735
            inputs={"x": x.name,
                    "y": y.name},
            outputs=[node.name],
            transpose_x=transpose_a,
            transpose_y=transpose_b)

    def BatchMatMul(self, node):
        return self.MatMul(node)

    def BatchMatMulV2(self, node):
        return self.MatMul(node)
J
jiangjiajun@baidu.com 已提交
736

J
jiangjiajun 已提交
737
    def DepthwiseConv2dNative(self, node):
J
jiangjiajun 已提交
738 739
        input = self.graph.get_node(node.layer.input[0])
        kernel = self.graph.get_node(node.layer.input[1])
J
jiangjiajun 已提交
740
        assert kernel.layer_type == "Const", "Kernel of DepthwiseConv2DNative should be Const"
J
jiangjiajun 已提交
741

J
jiangjiajun 已提交
742 743 744 745 746 747
        in_shape = input.out_shapes[0]
        k_size = kernel.out_shapes[0]
        strides = node.get_attr("strides")
        dilations = node.get_attr("dilations")
        data_format = node.get_attr("data_format").decode()
        pad_mode = node.get_attr("padding").decode()
J
jiangjiajun 已提交
748

S
SunAhong1993 已提交
749 750
        if len(kernel.outputs) == 1:
            self.params[kernel.name] = numpy.transpose(self.params[kernel.name],
S
release  
SunAhong1993 已提交
751 752
                                                          (2, 3, 0, 1))
            layer = self.paddle_graph.layers[self.params_output2id[kernel.name]] 
S
SunAhong1993 已提交
753 754 755 756 757 758 759
            layer.attrs["shape"] = self.params[kernel.name].shape
        else:
            self.paddle_graph.add_layer(
                kernel="paddle.transpose",
                inputs={"x": kernel.name},
                outputs=[kernel.name],
                perm=[2, 3, 0, 1])
J
jiangjiajun 已提交
760

J
jiangjiajun 已提交
761 762
        input_name = input.name
        if data_format == "NHWC":
J
jiangjiajun 已提交
763 764 765
            in_shape = [in_shape[i] for i in [0, 3, 1, 2]]
            strides = [strides[i] for i in [0, 3, 1, 2]]
            dilations = [dilations[i] for i in [0, 3, 1, 2]]
J
jiangjiajun 已提交
766
            transpose_name = gen_name('depthwise_conv2d', 'transpose')
S
SunAhong1993 已提交
767
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
768
                kernel="paddle.transpose",
J
jiangjiajun 已提交
769 770 771 772 773
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
            input_name = transpose_name

S
SunAhong1993 已提交
774
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
775 776 777
            kernel="paddle.nn.functional.conv2d",
            inputs={"x": input_name,
                    "weight": kernel.name},
J
jiangjiajun 已提交
778 779 780 781 782
            outputs=[node.name],
            stride=strides[2:4],
            dilation=dilations[2:4],
            groups=k_size[3] * in_shape[1],
            padding=string(pad_mode),
S
SunAhong1993 已提交
783
            bias=None)
J
jiangjiajun 已提交
784 785

        if data_format == "NHWC":
S
SunAhong1993 已提交
786
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
787
                kernel="paddle.transpose",
J
jiangjiajun 已提交
788 789 790
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])
J
jiangjiajun 已提交
791 792

    def AvgPool(self, node):
S
SunAhong1993 已提交
793
        input = self.graph.get_input_node(node, 0)
J
jiangjiajun 已提交
794

J
jiangjiajun 已提交
795 796 797 798 799
        k_size = node.get_attr("ksize")
        strides = node.get_attr("strides")
        data_format = node.get_attr("data_format").decode()
        pad_mode = node.get_attr("padding").decode()

J
jiangjiajun 已提交
800 801 802
        input_name = input.name
        if data_format == "NHWC":
            transpose_name = gen_name("avg_pool", "transpose")
S
SunAhong1993 已提交
803
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
804
                kernel="paddle.transpose",
J
jiangjiajun 已提交
805 806 807
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
J
jiangjiajun 已提交
808
            strides = [strides[i] for i in [0, 3, 1, 2]]
J
jiangjiajun 已提交
809
            k_size = [k_size[i] for i in [0, 3, 1, 2]]
J
jiangjiajun 已提交
810
            input_name = transpose_name
S
release  
SunAhong1993 已提交
811
        
S
SunAhong1993 已提交
812
        # TODO(syf): The op has diff.
J
jiangjiajun 已提交
813

S
SunAhong1993 已提交
814
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
815 816
            kernel="paddle.nn.functional.avg_pool2d",
            inputs={"x": input_name},
J
jiangjiajun 已提交
817
            outputs=[node.name],
S
SunAhong1993 已提交
818 819 820
            kernel_size=k_size[2:4],
            stride=strides[2:4],
            padding=string(pad_mode))
J
jiangjiajun 已提交
821 822

        if data_format == "NHWC":
S
SunAhong1993 已提交
823
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
824
                kernel="paddle.transpose",
J
jiangjiajun 已提交
825 826 827
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])
J
jiangjiajun 已提交
828 829

    def Pack(self, node):
S
SunAhong1993 已提交
830 831 832 833
        inputs_list = list()
        for i in range(len(node.inputs)):
            inputs_list.append(self.graph.get_input_node(node, i))
        input_names = [i.name for i in inputs_list]
J
jiangjiajun 已提交
834
        axis = node.get_attr("axis")
S
SunAhong1993 已提交
835
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
836
            kernel="paddle.stack",
J
jiangjiajun 已提交
837 838 839 840
            inputs={"x": input_names},
            outputs=[node.name],
            axis=axis)
        if len(node.out_shapes[0]) == 1:
S
SunAhong1993 已提交
841
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
842
                kernel="paddle.reshape",
J
jiangjiajun 已提交
843 844 845 846 847
                inputs={"x": node.name},
                outputs=[node.name],
                shape=[-1])

    def Unpack(self, node):
S
SunAhong1993 已提交
848
        input = self.graph.get_input_node(node, 0)
J
jiangjiajun 已提交
849 850 851 852 853 854
        axis = node.get_attr("axis")
        num = node.get_attr("num")
        shape = input.out_shapes[0]
        input_name = input.name
        if len(shape) == 1:
            if shape[0] > 0 and num == shape[0]:
S
SunAhong1993 已提交
855
                self.paddle_graph.add_layer(
S
SunAhong1993 已提交
856 857
                    kernel="paddle.unsqueeze",
                    inputs={"x": input.name},
J
jiangjiajun 已提交
858
                    outputs=[node.name],
S
SunAhong1993 已提交
859
                    axis=[0])
J
jiangjiajun 已提交
860 861 862 863
                input_name = node.name
                axis = 1
            else:
                raise Exception("Unexpected situation happend in Unpack OP")
S
release  
SunAhong1993 已提交
864
        layer_outputs = ["{}_p{}".format(node.layer_name, i) for i in range(num)]
S
SunAhong1993 已提交
865 866
        if len(layer_outputs) == 1:
            layer_outputs[0] = "[{}]".format(node.layer_name)
S
SunAhong1993 已提交
867
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
868
            kernel="paddle.unstack",
J
jiangjiajun 已提交
869
            inputs={"x": input_name},
S
SunAhong1993 已提交
870
            outputs=layer_outputs,
J
jiangjiajun 已提交
871 872
            axis=axis,
            num=num)
J
jiangjiajun 已提交
873

J
jiangjiajun 已提交
874
    def ConcatV2(self, node):
S
SunAhong1993 已提交
875 876 877 878
        inputs_list = list()
        for i in range(len(node.inputs) - 1):
            inputs_list.append(self.graph.get_input_node(node, i))
        axis = self.graph.get_input_node(node, -1)
J
jiangjiajun 已提交
879 880 881
        assert axis.layer_type == "Const", "axis for ConcatV2 must be type Const"
        axis = axis.value
        if axis < 0:
S
SunAhong1993 已提交
882
            axis += len(inputs_list[0].out_shapes[0])
J
jiangjiajun 已提交
883

S
SunAhong1993 已提交
884
        input_names = [i.name for i in inputs_list]
S
SunAhong1993 已提交
885
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
886 887 888 889
            kernel="paddle.concat",
            inputs={"x": input_names},
            outputs=[node.name],
            axis=axis)
S
release  
SunAhong1993 已提交
890
        
S
SunAhong1993 已提交
891 892 893 894 895 896 897 898 899
    def Concat(self, node):
        inputs_list = list()
        for i in range(1, len(node.inputs)):
            inputs_list.append(self.graph.get_input_node(node, i))
        axis = self.graph.get_input_node(node, 0)
        assert axis.layer_type == "Const", "axis for ConcatV2 must be type Const"
        axis = axis.value
        if axis < 0:
            axis += len(inputs_list[0].out_shapes[0])
S
release  
SunAhong1993 已提交
900
            
S
SunAhong1993 已提交
901 902 903 904
        input_names = [i.name for i in inputs_list]
        self.paddle_graph.add_layer(
            kernel="paddle.concat",
            inputs={"x": input_names},
J
jiangjiajun 已提交
905 906
            outputs=[node.name],
            axis=axis)
S
release  
SunAhong1993 已提交
907
            
S
SunAhong1993 已提交
908 909 910 911 912 913 914 915 916 917
    def AddN(self, node):
        inputs_list = list()
        for i in range(len(node.inputs) - 1):
            inputs_list.append(self.graph.get_input_node(node, i))

        input_names = [i.name for i in inputs_list]
        self.paddle_graph.add_layer(
            kernel="paddle.add_n",
            inputs={"inputs": input_names},
            outputs=[node.name])
J
jiangjiajun 已提交
918

J
jiangjiajun 已提交
919
    def StridedSlice(self, node):
S
SunAhong1993 已提交
920 921 922 923
        input = self.graph.get_input_node(node, 0)
        begin = self.graph.get_input_node(node, 1)
        end = self.graph.get_input_node(node, 2)
        strides = self.graph.get_input_node(node, 3)
J
jiangjiajun 已提交
924

J
jiangjiajun 已提交
925 926
        if strides.layer_type == "Const":
            strides = strides.value.tolist()
927
        else:
S
SunAhong1993 已提交
928
            strides = self.decoder.infer_tensor(strides)
J
jiangjiajun 已提交
929 930
        if begin.layer_type == "Const":
            begin = begin.value.tolist()
931
        else:
S
SunAhong1993 已提交
932
            begin = self.decoder.infer_tensor(begin)
J
jiangjiajun 已提交
933 934
        if end.layer_type == "Const":
            end = end.value.tolist()
935
        else:
S
SunAhong1993 已提交
936
            end = self.decoder.infer_tensor(end)
937

J
jiangjiajun 已提交
938 939
        assert len(set(strides)) == 1 and strides[
            0] == 1, "Only support strides be 1 in StridedSlice OP"
J
jiangjiajun 已提交
940

J
jiangjiajun 已提交
941 942 943 944
        if len(begin) < len(input.out_shapes[0]):
            begin = begin + [0] * (len(input.out_shapes[0]) - len(begin))
        if len(end) < len(input.out_shapes[0]):
            end = end + [0] * (len(input.out_shapes[0]) - len(end))
J
jiangjiajun 已提交
945 946 947 948
        for i in range(len(end)):
            if end[i] == 0:
                end[i] = 999999

J
jiangjiajun 已提交
949 950 951 952
        begin_mask = node.get_attr('begin_mask')
        end_mask = node.get_attr('end_mask')
        ellipsis_mask = node.get_attr('ellipsis_mask')
        new_axis_mask = node.get_attr('new_axis_mask')
J
jiangjiajun 已提交
953
        shrink_axis_mask = node.get_attr('shrink_axis_mask')
J
jiangjiajun 已提交
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

        assert ellipsis_mask == 0, "(OP:{} Name:{})Only support ellipsis_mask be 0[now: {}] n StridedSlice OP".format(
            node.layer_type, node.layer.name, ellipsis_mask)

        # TODO codes without validation
        # Use it carefully
        new_begin = list()
        new_end = list()
        new_axes = list()
        shrink_axes = list()
        for i, item in enumerate(begin):
            mask = (new_axis_mask >> i) & 1
            if mask != 0:
                new_axes.append(i)
                continue

            mask = (shrink_axis_mask >> i) & 1
            if mask != 0:
                shrink_axes.append(i)

            mask = (begin_mask >> i) & 1
            if mask != 0:
                new_begin.append(0)
            else:
                new_begin.append(item)

            mask = (end_mask >> i) & 1
            if mask != 0:
                new_end.append(999999)
            else:
                new_end.append(end[i])
S
release  
SunAhong1993 已提交
985
            
S
SunAhong1993 已提交
986 987 988 989 990 991
        if input.dtype == "bool":
            self.paddle_graph.add_layer(
                "paddle.cast",
                inputs={"x": input.name},
                outputs=[input.name],
                dtype=string("int32"))
J
jiangjiajun 已提交
992

S
SunAhong1993 已提交
993
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
994
            kernel="paddle.slice",
J
jiangjiajun 已提交
995 996 997 998 999
            inputs={"input": input.name},
            outputs=[node.name],
            axes=[i for i in range(len(new_begin))],
            starts=new_begin,
            ends=new_end)
S
release  
SunAhong1993 已提交
1000
        
S
SunAhong1993 已提交
1001 1002 1003 1004 1005 1006 1007
        if input.dtype == "bool":
            self.paddle_graph.add_layer(
                "paddle.cast",
                inputs={"x": node.name},
                outputs=[node.name],
                dtype=string("bool"))

J
jiangjiajun 已提交
1008
        if len(new_axes) > 0:
S
SunAhong1993 已提交
1009
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1010 1011
                kernel="paddle.unsqueeze",
                inputs={"x": node.name},
J
jiangjiajun 已提交
1012
                outputs=[node.name],
S
SunAhong1993 已提交
1013
                axis=new_axes)
J
jiangjiajun 已提交
1014 1015 1016 1017
        if len(shrink_axes) > 0:
            if len(input.out_shapes[0]) + len(new_axes) <= 1:
                pass
            else:
S
SunAhong1993 已提交
1018
                self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1019 1020
                    kernel="paddle.squeeze",
                    inputs={"x": node.name},
J
jiangjiajun 已提交
1021
                    outputs=[node.name],
S
SunAhong1993 已提交
1022
                    axis=shrink_axes)
S
release  
SunAhong1993 已提交
1023
                
S
SunAhong1993 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    def Prod(self, node):
        input = self.graph.get_input_node(node, 0)
        reduction_indices = self.graph.get_input_node(node, 1)
        assert reduction_indices.layer_type == "Const"
        keep_dims = node.get_attr('keep_dims')
        axis = reduction_indices.value

        self.paddle_graph.add_layer(
            kernel="paddle.prod",
            inputs={"x": input.name},
            outputs=[node.layer_name],
            keepdim=keep_dims,
            axis=axis)
J
jiangjiajun 已提交
1037 1038

    def Split(self, node):
S
SunAhong1993 已提交
1039 1040
        dim = self.graph.get_input_node(node, 0)
        input = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
1041 1042 1043 1044
        assert dim.layer_type == "Const"
        num_split = node.get_attr('num_split')
        dim = dim.value

S
SunAhong1993 已提交
1045
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1046 1047
            kernel="paddle.split",
            inputs={"x": input.name},
J
jiangjiajun 已提交
1048 1049 1050 1051
            outputs=[
                "{}_p{}".format(node.layer_name, i) for i in range(num_split)
            ],
            num_or_sections=num_split,
S
SunAhong1993 已提交
1052
            axis=dim)
S
release  
SunAhong1993 已提交
1053
        
S
SunAhong1993 已提交
1054 1055 1056 1057 1058 1059 1060 1061
    def SplitV(self, node):
        input = self.graph.get_input_node(node, 0)
        size_splits = self.graph.get_input_node(node, 1)
        assert size_splits.layer_type == "Const", "size_splits of SplitV OP should be Const"
        size_splits = size_splits.value.tolist()
        dim = self.graph.get_input_node(node, 2)
        assert dim.layer_type == "Const", "dim of SplitV OP should be Const"
        dim = dim.value
S
release  
SunAhong1993 已提交
1062
        
S
SunAhong1993 已提交
1063 1064 1065 1066
        self.paddle_graph.add_layer(
            kernel="paddle.split",
            inputs={"x": input.name},
            outputs=[
S
release  
SunAhong1993 已提交
1067
                "{}_p{}".format(node.layer_name, i) for i in range(len(size_splits))
S
SunAhong1993 已提交
1068 1069 1070
            ],
            num_or_sections=size_splits,
            axis=dim)
1071 1072

    def Slice(self, node):
S
SunAhong1993 已提交
1073 1074 1075
        input = self.graph.get_input_node(node, 0)
        begin = self.graph.get_input_node(node, 1)
        size = self.graph.get_input_node(node, 2)
J
jiangjiajun 已提交
1076 1077 1078

        inputs = {"x": input.name}
        attrs = {}
J
jiangjiajun 已提交
1079 1080
        if begin.layer_type == "Const":
            begin = begin.value.tolist()
J
jiangjiajun 已提交
1081
            attrs['offsets'] = begin
J
jiangjiajun 已提交
1082
        else:
S
release  
SunAhong1993 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091
            #             shape = begin.out_shapes[0]
            #             reshape_name = gen_name("slice", "reshape")
            #             self.paddle_graph.add_layer(
            #                 kernel="fluid.layers.reshape",
            #                 inputs={"x": begin.name},
            #                 outputs=[reshape_name],
            #                 shape=shape)
            #             inputs['offsets'] = reshape_name
            begin = self.decoder.infer_tensor(begin, use_diff_inputs=False).tolist()
J
jiangjiajun 已提交
1092 1093
            attrs['offsets'] = begin
        if size.layer_type == "Const":
J
jiangjiajun 已提交
1094
            size = size.value.tolist()
J
jiangjiajun 已提交
1095 1096 1097 1098
            attrs['shape'] = size
        else:
            shape = size.out_shapes[0]
            reshape_name = gen_name("slice", "reshape")
S
SunAhong1993 已提交
1099
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1100
                kernel="paddle.reshape",
J
jiangjiajun 已提交
1101 1102 1103 1104
                inputs={"x": size.name},
                outputs=[reshape_name],
                shape=shape)
            inputs['shape'] = reshape_name
S
SunAhong1993 已提交
1105
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
1106 1107 1108 1109
            kernel="paddle.crop",
            inputs=inputs,
            outputs=[node.name],
            **attrs)
J
jiangjiajun 已提交
1110 1111

    def ResizeNearestNeighbor(self, node):
S
SunAhong1993 已提交
1112 1113
        input = self.graph.get_input_node(node, 0)
        resize_shape = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
1114
        data_format = "NHWC"
S
SunAhong1993 已提交
1115
        inputs = {"x": input.name}
S
release  
SunAhong1993 已提交
1116 1117 1118
        attrs = {"align_corners": node.get_attr("align_corners"),
                 "mode": string("nearest"),
                 "align_mode": 1}
J
jiangjiajun 已提交
1119 1120 1121

        if resize_shape.layer_type == "Const":
            resize_shape = resize_shape.value.tolist()
S
SunAhong1993 已提交
1122
            attrs["size"] = resize_shape
J
jiangjiajun 已提交
1123
        else:
J
jiangjiajun 已提交
1124 1125
            shape = resize_shape.out_shapes[0]
            reshape_name = gen_name("resize_nearest", "reshape")
S
SunAhong1993 已提交
1126
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1127
                kernel="paddle.reshape",
J
jiangjiajun 已提交
1128 1129 1130
                inputs={"x": resize_shape.name},
                outputs=[reshape_name],
                shape=shape)
S
SunAhong1993 已提交
1131
            inputs["size"] = reshape_name
J
jiangjiajun 已提交
1132 1133 1134

        if data_format == "NHWC":
            transpose_name = gen_name("resize_nearest", "reshape")
S
SunAhong1993 已提交
1135
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1136
                kernel="paddle.transpose",
J
jiangjiajun 已提交
1137 1138 1139
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
S
SunAhong1993 已提交
1140
            inputs["x"] = transpose_name
J
jiangjiajun 已提交
1141

S
SunAhong1993 已提交
1142
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1143
            kernel="paddle.nn.functional.interpolate",
J
jiangjiajun 已提交
1144 1145 1146 1147 1148
            inputs=inputs,
            outputs=[node.name],
            **attrs)

        if data_format == "NHWC":
S
SunAhong1993 已提交
1149
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1150
                kernel="paddle.transpose",
J
jiangjiajun 已提交
1151 1152 1153
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])
1154

J
jiangjiajun 已提交
1155
    def ResizeBilinear(self, node):
S
SunAhong1993 已提交
1156 1157
        input = self.graph.get_input_node(node, 0)
        resize_shape = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
1158
        data_format = "NHWC"
S
SunAhong1993 已提交
1159
        inputs = {"x": input.name}
S
release  
SunAhong1993 已提交
1160 1161 1162
        attrs = {"align_corners": node.get_attr("align_corners"),
                 "mode": string("bilinear"),
                 "align_mode": 1}
J
jiangjiajun 已提交
1163

J
jiangjiajun 已提交
1164 1165
        if resize_shape.layer_type == "Const":
            resize_shape = resize_shape.value.tolist()
S
SunAhong1993 已提交
1166
            attrs["size"] = resize_shape
J
jiangjiajun 已提交
1167 1168 1169
        else:
            shape = resize_shape.out_shapes[0]
            reshape_name = gen_name("resize_bilinear", "reshape")
S
SunAhong1993 已提交
1170
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1171
                kernel="paddle.reshape",
J
jiangjiajun 已提交
1172 1173 1174
                inputs={"x": resize_shape.name},
                outputs=[reshape_name],
                shape=shape)
S
SunAhong1993 已提交
1175
            inputs["size"] = reshape_name
J
jiangjiajun 已提交
1176 1177 1178

        if data_format == "NHWC":
            transpose_name = gen_name("resize_bilinear", "reshape")
S
SunAhong1993 已提交
1179
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1180
                kernel="paddle.transpose",
J
jiangjiajun 已提交
1181 1182 1183
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
S
SunAhong1993 已提交
1184
            inputs["x"] = transpose_name
J
jiangjiajun 已提交
1185

S
SunAhong1993 已提交
1186
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1187
            kernel="paddle.nn.functional.interpolate",
J
jiangjiajun 已提交
1188 1189 1190 1191 1192
            inputs=inputs,
            outputs=[node.name],
            **attrs)

        if data_format == "NHWC":
S
SunAhong1993 已提交
1193
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1194
                kernel="paddle.transpose",
J
jiangjiajun 已提交
1195 1196 1197 1198 1199
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])

    def Cast(self, node):
S
SunAhong1993 已提交
1200
        input = self.graph.get_input_node(node, 0)
J
jiangjiajun 已提交
1201
        dtype = node.dtype
S
SunAhong1993 已提交
1202
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1203
            kernel="paddle.cast",
J
jiangjiajun 已提交
1204 1205 1206 1207 1208
            inputs={"x": input.name},
            outputs=[node.name],
            dtype=string(dtype))

    def Sum(self, node):
S
SunAhong1993 已提交
1209 1210
        input = self.graph.get_input_node(node, 0)
        reduce_idx = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
1211 1212 1213 1214
        assert reduce_idx.layer_type == "Const", "Only support Const parameter[reduce_idx]"
        keep_dims = node.get_attr("keep_dims")
        dim = reduce_idx.value.tolist()

S
SunAhong1993 已提交
1215
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1216 1217
            kernel="paddle.sum",
            inputs={"x": input.name},
J
jiangjiajun 已提交
1218
            outputs=[node.name],
S
SunAhong1993 已提交
1219 1220
            axis=dim,
            keepdim=keep_dims)
J
jiangjiajun 已提交
1221 1222

    def Max(self, node):
S
SunAhong1993 已提交
1223 1224
        input = self.graph.get_input_node(node, 0)
        reduce_idx = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
1225 1226 1227
        assert reduce_idx.layer_type == "Const", "Only support Const parameter[reduce_idx]"
        keep_dims = node.get_attr("keep_dims")
        dim = reduce_idx.value.tolist()
S
SunAhong1993 已提交
1228
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1229 1230
            kernel="paddle.max",
            inputs={"x": input.name},
J
jiangjiajun 已提交
1231
            outputs=[node.name],
S
SunAhong1993 已提交
1232 1233
            axis=dim,
            keepdim=keep_dims)
1234

J
jiangjiajun 已提交
1235
    def RandomUniform(self, node):
S
SunAhong1993 已提交
1236
        shape = self.graph.get_input_node(node, 0)
J
jiangjiajun 已提交
1237 1238
        if shape.layer_type == "Const":
            shape = shape.value.tolist()
S
SunAhong1993 已提交
1239
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1240
                kernel="paddle.uniform",
J
jiangjiajun 已提交
1241 1242 1243 1244 1245 1246
                inputs={},
                outputs=[node.name],
                shape=shape,
                min=0.0,
                max=0.9999)
        else:
S
SunAhong1993 已提交
1247
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1248
                kernel="paddle.uniform",
J
jiangjiajun 已提交
1249 1250 1251 1252
                inputs={'shape': shape.name},
                outputs=[node.name],
                min=0.0,
                max=0.9999)
1253 1254

    def Conv2DBackpropInput(self, node):
S
SunAhong1993 已提交
1255 1256 1257
        out_shape = self.graph.get_input_node(node, 0)
        kernel = self.graph.get_input_node(node, 1)
        input = self.graph.get_input_node(node, 2)
1258

1259
        assert kernel.layer_type == "Const", "Kernel of Conv2DBackpropInput should be Const"
1260

J
jiangjiajun 已提交
1261 1262 1263
        if out_shape.layer_type == "Const":
            out_shape = out_shape.value.tolist()
        else:
S
release  
SunAhong1993 已提交
1264 1265
            out_shape = self.decoder.infer_tensor(out_shape,
                                                  out_shape=node.out_shapes[0])
J
jiangjiajun 已提交
1266

1267
        in_shape = input.out_shapes[0]
J
jiangjiajun 已提交
1268
        if in_shape.count(-1) > 2:
S
release  
SunAhong1993 已提交
1269
            in_shape = self.decoder.infer_tensor(input, use_diff_inputs=False).shape
1270
        k_size = kernel.out_shapes[0]
J
jiangjiajun 已提交
1271
        if k_size.count(-1) > 2:
S
release  
SunAhong1993 已提交
1272
            k_size = self.decoder.infer_tensor(kernel, use_diff_inputs=False).shape
J
jiangjiajun 已提交
1273

J
jiangjiajun 已提交
1274
        pad_mode = node.get_attr("padding").decode()
1275 1276 1277
        strides = node.get_attr("strides")
        dilations = node.get_attr("dilations")
        data_format = node.get_attr("data_format").decode()
1278

S
SunAhong1993 已提交
1279 1280
        kernel_name = node.name + ".weight"
        self.params[kernel_name] = numpy.transpose(kernel.value, (3, 2, 0, 1))
J
jiangjiajun 已提交
1281 1282 1283

        input_name = input.name
        if data_format == "NHWC":
1284 1285 1286
            in_shape = [in_shape[i] for i in [0, 3, 1, 2]]
            strides = [strides[i] for i in [0, 3, 1, 2]]
            dilations = [dilations[i] for i in [0, 3, 1, 2]]
J
jiangjiajun 已提交
1287
            transpose_name = gen_name("conv2dbackpropinput", "transpose")
S
SunAhong1993 已提交
1288
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1289
                kernel="paddle.transpose",
J
jiangjiajun 已提交
1290 1291 1292 1293 1294
                inputs={"x": input.name},
                outputs=[transpose_name],
                perm=[0, 3, 1, 2])
            input_name = transpose_name

S
SunAhong1993 已提交
1295
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1296 1297 1298 1299 1300 1301
            kernel="paddle.static.create_parameter",
            inputs={},
            outputs=["{}_{}".format(node.name, kernel_name).replace(".", "_")],
            dtype=string(str(self.params[kernel_name].dtype)),
            shape=self.params[kernel_name].shape,
            name=string(kernel_name))
S
release  
SunAhong1993 已提交
1302
    
S
SunAhong1993 已提交
1303 1304
        self.paddle_graph.add_layer(
            kernel="paddle.nn.functional.conv2d_transpose",
S
release  
SunAhong1993 已提交
1305 1306
            inputs={"x": input_name,
                    "weight": "{}_{}".format(node.name, kernel_name).replace(".", "_")},
J
jiangjiajun 已提交
1307
            outputs=[node.name],
S
SunAhong1993 已提交
1308
            bias=None,
J
jiangjiajun 已提交
1309 1310 1311 1312 1313 1314
            stride=strides[2:4],
            dilation=dilations[2:4],
            padding=string(pad_mode),
            output_size=out_shape[1:3])

        if data_format == "NHWC":
S
SunAhong1993 已提交
1315
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1316
                kernel="paddle.transpose",
J
jiangjiajun 已提交
1317 1318 1319
                inputs={"x": node.name},
                outputs=[node.name],
                perm=[0, 2, 3, 1])
1320

J
jiangjiajun 已提交
1321 1322
    def Tile(self, node):
        input = self.graph.get_node(node.layer.input[0])
S
SunAhong1993 已提交
1323
        repeat_times = self.graph.get_node(node.layer.input[1])
J
jiangjiajun 已提交
1324 1325
        inputs = {"x": input.name}
        attr = dict()
S
SunAhong1993 已提交
1326 1327 1328
        if repeat_times.layer_type == "Const":
            repeat_times = repeat_times.value.tolist()
            attr["repeat_times"] = repeat_times
J
jiangjiajun 已提交
1329
        else:
S
SunAhong1993 已提交
1330
            inputs["repeat_times"] = repeat_times.name
S
release  
SunAhong1993 已提交
1331
            
S
SunAhong1993 已提交
1332
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
1333 1334 1335 1336 1337 1338
            kernel="paddle.tile",
            inputs=inputs,
            outputs=[node.name],
            **attr)
        
        if not isinstance(repeat_times, list) and repeat_times.layer_type != "Const":
S
SunAhong1993 已提交
1339 1340 1341 1342 1343
            self.paddle_graph.add_layer(
                kernel="paddle.reshape",
                inputs={"x": node.name},
                outputs=[node.name],
                shape=node.out_shapes[0])
J
jiangjiajun 已提交
1344

J
jiangjiajun 已提交
1345 1346 1347 1348 1349 1350
    def Range(self, node):
        start = self.graph.get_node(node.layer.input[0])
        limit = self.graph.get_node(node.layer.input[1])
        delta = self.graph.get_node(node.layer.input[2])
        inputs = dict()
        attr = dict()
1351

C
channingss 已提交
1352 1353 1354
        dtype = 'int32'
        if start.dtype.startswith('float'):
            dtype = start.dtype
J
jiangjiajun 已提交
1355 1356
        if start.layer_type == "Const":
            attr["start"] = start.value
1357
        else:
J
jiangjiajun 已提交
1358
            inputs["start"] = start.name
C
channingss 已提交
1359 1360
        if limit.dtype.startswith('float'):
            dtype = limit.dtype
J
jiangjiajun 已提交
1361 1362
        if limit.layer_type == "Const":
            attr["end"] = limit.value
J
jiangjiajun 已提交
1363
        else:
J
jiangjiajun 已提交
1364
            inputs["end"] = limit.name
C
channingss 已提交
1365 1366
        if delta.dtype.startswith('float'):
            dtype = delta.dtype
J
jiangjiajun 已提交
1367 1368
        if delta.layer_type == "Const":
            attr["step"] = delta.value
J
jiangjiajun 已提交
1369
        else:
J
jiangjiajun 已提交
1370
            inputs["step"] = delta.name
C
channingss 已提交
1371
        node.set_dtype(dtype)
J
jiangjiajun 已提交
1372 1373
        attr["dtype"] = string(node.dtype)

S
SunAhong1993 已提交
1374
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
1375 1376 1377 1378
            kernel="paddle.arange",
            inputs=inputs,
            outputs=[node.name],
            **attr)
S
SunAhong1993 已提交
1379 1380 1381 1382 1383 1384 1385 1386
        if start.layer_type != "Const" or \
                limit.layer_type != "Const" or \
                delta.layer_type != "Const":
            self.paddle_graph.add_layer(
                kernel="paddle.reshape",
                inputs={"x": node.name},
                outputs=[node.name],
                shape=node.out_shapes[0])
J
jiangjiajun 已提交
1387 1388

    def SquaredDifference(self, node):
S
SunAhong1993 已提交
1389 1390
        x = self.graph.get_input_node(node, 0)
        y = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
1391 1392 1393
        inputs = {"x": x.name, "y": y.name}
        x_shape = x.out_shapes[0]
        y_shape = y.out_shapes[0]
S
SunAhong1993 已提交
1394
        # TODO(syf)
S
SunAhong1993 已提交
1395
        layer_id = self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1396
            "paddle.subtract", inputs=inputs, outputs=[node.name])
S
release  
SunAhong1993 已提交
1397
        self.paddle_graph.layers[layer_id].input_shapes = {"x": x_shape, "y": y_shape}
J
jiangjiajun 已提交
1398 1399 1400 1401

        inputs = {"x": node.name, "y": node.name}
        x_shape = node.out_shapes[0]
        y_shape = node.out_shapes[0]
S
SunAhong1993 已提交
1402
        layer_id = self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1403
            "paddle.multiply", inputs=inputs, outputs=[node.name])
S
release  
SunAhong1993 已提交
1404
        self.paddle_graph.layers[layer_id].input_shapes = {"x": x_shape, "y": y_shape}
J
jiangjiajun 已提交
1405 1406

    def OneHot(self, node):
S
SunAhong1993 已提交
1407 1408 1409 1410
        input = self.graph.get_input_node(node, 0)
        depth = self.graph.get_input_node(node, 1)
        on_value = self.graph.get_input_node(node, 2)
        off_value = self.graph.get_input_node(node, 3)
J
jiangjiajun 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
        assert depth.layer_type == 'Const', 'Parameter depth should be Const in OneHot'
        assert on_value.layer_type == 'Const', 'Parameter on_value should be Const in OneHot'
        assert off_value.layer_type == 'Const', 'Parameter off_value should be Const in OneHot'

        attr = {'depth': depth.value}
        on_value = on_value.value
        off_value = off_value.value
        assert math.fabs(on_value -
                         1.0) < 1e-06, "on_value should be 1 in OneHot"
        assert math.fabs(off_value -
                         0.0) < 1e-06, "off_value should be 0 in OneHot"

S
SunAhong1993 已提交
1423
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1424 1425
            "paddle.nn.functional.one_hot",
            inputs={"x": input.name},
J
jiangjiajun 已提交
1426
            outputs=[node.name],
S
SunAhong1993 已提交
1427
            num_classes=depth.value)
J
jiangjiajun 已提交
1428 1429

    def Pow(self, node):
S
SunAhong1993 已提交
1430 1431
        x = self.graph.get_input_node(node, 0)
        factor = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
1432 1433 1434
        inputs = {"x": x.name}
        attr = dict()
        if factor.layer_type == 'Const':
S
SunAhong1993 已提交
1435
            attr["y"] = factor.value.tolist()
J
jiangjiajun 已提交
1436
        else:
S
SunAhong1993 已提交
1437
            inputs["y"] = factor.name
S
SunAhong1993 已提交
1438
        self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1439
            "paddle.pow", inputs=inputs, outputs=[node.name], **attr)
J
jiangjiajun 已提交
1440 1441

    def All(self, node):
S
SunAhong1993 已提交
1442 1443
        input = self.graph.get_input_node(node, 0)
        reduce_idx = self.graph.get_input_node(node, 1)
J
jiangjiajun 已提交
1444 1445
        assert reduce_idx.layer_type == "Const", "Only support Const parameter[reduce_idx]"
        attr = dict()
S
SunAhong1993 已提交
1446 1447
        attr["axis"] = reduce_idx.value.tolist()
        attr["keepdim"] = node.get_attr("keep_dims")
J
jiangjiajun 已提交
1448

J
jiangjiajun 已提交
1449 1450 1451
        input_name = input.name
        if input.dtype != "bool":
            input_name = gen_name("all", "cast")
S
SunAhong1993 已提交
1452
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1453
                "paddle.cast",
J
jiangjiajun 已提交
1454 1455 1456
                inputs={"x": input.name},
                outputs=[input_name],
                dtype=string("bool"))
S
SunAhong1993 已提交
1457
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
1458 1459 1460 1461
            "paddle.all",
            inputs={"x": input_name},
            outputs=[node.name],
            **attr)
J
jiangjiajun 已提交
1462 1463 1464 1465

        node.layer.attr['dtype'].type = 10

    def GatherV2(self, node):
S
SunAhong1993 已提交
1466 1467 1468
        embeddings = self.graph.get_input_node(node, 0)
        index = self.graph.get_input_node(node, 1)
        axis = self.graph.get_input_node(node, 2)
J
jiangjiajun 已提交
1469
        assert axis.layer_type == 'Const', "Only support Const parameter[axis]"
S
SunAhong1993 已提交
1470
        axis = axis.value
J
jiangjiajun 已提交
1471 1472 1473 1474
        index_name = index.name
        if len(index.out_shapes[0]) != 1:
            reshape_name = gen_name("gather", "reshape")
            index_name = reshape_name
S
SunAhong1993 已提交
1475
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1476
                "paddle.reshape",
J
jiangjiajun 已提交
1477 1478 1479
                inputs={"x": index.name},
                outputs=[reshape_name],
                shape=[-1])
S
SunAhong1993 已提交
1480
        inputs = {'x': embeddings.name, 'index': index_name}
S
SunAhong1993 已提交
1481
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
1482 1483 1484 1485
            "paddle.gather",
            inputs=inputs,
            outputs=[node.name],
            axis=axis)
J
jiangjiajun 已提交
1486 1487
        if len(index.out_shapes[0]) != 1:
            out_shape = node.out_shapes[0]
S
SunAhong1993 已提交
1488
            self.paddle_graph.add_layer(
S
SunAhong1993 已提交
1489
                kernel="paddle.reshape",
J
jiangjiajun 已提交
1490 1491 1492
                inputs={"x": node.name},
                outputs=[node.name],
                shape=out_shape)
S
release  
SunAhong1993 已提交
1493
            
S
SunAhong1993 已提交
1494 1495 1496 1497 1498
    def GatherNd(self, node):
        x = self.graph.get_input_node(node, 0)
        index = self.graph.get_input_node(node, 1)
        inputs = {'x': x.name, 'index': index.name}
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
1499 1500 1501
            "paddle.gather_nd",
            inputs=inputs,
            outputs=[node.name])
J
jiangjiajun 已提交
1502 1503

    def ExpandDims(self, node):
S
SunAhong1993 已提交
1504 1505 1506
        x = self.graph.get_input_node(node, 0, copy=True)
        y = self.graph.get_input_node(node, 1, copy=True)
        inputs = {"x": x.name}
J
jiangjiajun 已提交
1507 1508 1509 1510 1511
        attr = dict()
        if y.layer_type == 'Const':
            dim = y.value.tolist()
            if not isinstance(dim, list):
                dim = [dim]
S
SunAhong1993 已提交
1512
            attr['axis'] = dim
J
jiangjiajun 已提交
1513
        else:
S
SunAhong1993 已提交
1514
            inputs['axis'] = y.name
S
SunAhong1993 已提交
1515
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
1516 1517 1518 1519 1520
            "paddle.unsqueeze",
            inputs=inputs,
            outputs=[node.name],
            **attr)
        
S
SunAhong1993 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
    def ReverseV2(self, node):
        x = self.graph.get_input_node(node, 0)
        axis = self.graph.get_input_node(node, 1)
        inputs = {"x": x.name}
        attr = dict()
        if axis.layer_type == 'Const':
            axis = axis.value.tolist()
            if not isinstance(axis, list):
                axis = [axis]
            attr['axis'] = axis
        else:
            inputs['axis'] = axis.name
        self.paddle_graph.add_layer(
S
release  
SunAhong1993 已提交
1534 1535
            "paddle.flip",
            inputs=inputs,
S
SunAhong1993 已提交
1536
            outputs=[node.name],
S
release  
SunAhong1993 已提交
1537
            **attr)
S
SunAhong1993 已提交
1538