提交 d6e28406 编写于 作者: S SunAhong1993

add onnx op

上级 015d8f2c
......@@ -322,6 +322,9 @@ class PaddleGraph(object):
if self.source_type == "caffe":
custom_import = "from x2paddle.op_mapper.static.caffe2paddle " + \
"import caffe_custom_layer as x2paddle_nn"
if self.source_type == "onnx":
custom_import = "from x2paddle.op_mapper.static.onnx2paddle " + \
"import onnx_custom_layer as x2paddle_nn"
else:
custom_import = ""
......
......@@ -16,8 +16,6 @@ import paddle
class CustomPad(object):
def __init__(self, value, mode):
self.value = value
self.assume_pad2d = assume_pad2d
self.layer_attrs = {}
self.layer_attrs['mode'] = string(mode)
self.layer_attrs['data_format'] = string('NCHW')
......
# 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.
from .one_hot import one_hot
from .pad import custom_pad
\ No newline at end of file
# 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.
import paddle
def one_hot(self, indices, depth, values, axis):
indices_shape = paddle.shape(indices)
tmp = paddle.ones_like(indices_shape, dtype="int32")
rank = paddle.sum(tmp)
depth_range = paddle.arange(end=depth)
zero = paddle.zeros([1], dtype="int32")
one = paddle.ones([1], dtype="int32")
axis = axis * one
new_axis = axis + rank + 1
cond = paddle.less_than(axis, zero)
real_axis = paddle.where(cond, new_axis, axis)
ls = paddle.slice(indices_shape, axes=[0], starts=[0], ends=real_axis)
rs = paddle.slice(indices_shape, axes=[0], starts=real_axis, ends=rank)
tmp = paddle.ones_like(ls, dtype="int32")
ls_len = paddle.sum(tmp)
ls_list = paddle.ones(ls_len, dtype="int32")
tmp = paddle.ones_like(rs, dtype="int32")
rs_len = paddle.sum(tmp)
rs_list = paddle.ones(rs_len, dtype="int32")
depth_range_shape = paddle.shape(depth_range)
targets_shape = paddle.concat([ls_list, depth_range_shape, rs_list], axis=0)
targets = paddle.reshape(depth_range, targets_shape)
mod = paddle.mod(indices, depth)
v_shape = paddle.concat([ls, paddle.shape(one), rs], axis=0)
v = paddle.reshape(mod, v_shape)
out = targets == v
out = paddle.cast(out, "float32")
on_value = paddle.slice(values, axes=[0], starts=[1], ends=[2])
off_value = paddle.slice(values, axes=[0], starts=[0], ends=[1])
out = out * (on_value - off_value) + off_value
return out
\ No newline at end of file
# 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.
import paddle
def custom_pad(self, x, pad, value, mode):
layer_attrs = {}
layer_attrs['mode'] = string(mode)
layer_attrs['data_format'] = string('NCHW')
layer_attrs['value'] = value
pad = paddle.reshape(pad, shape=[2, -1])
pad = paddle.transpose(pad, perm=[1, 0])
pad = paddle.reverse(pad, axis=[0])
pad = paddle.flatten(pad)
out = paddle.nn.functional.pad(x=x, pad=pad, **self.layer_attrs)
return out
\ No newline at end of file
......@@ -368,6 +368,14 @@ class OpSet9():
def Pad(self, node, op_independent=True):
val_x = self.graph.get_input_node(node, idx=0, copy=True)
pads = node.get_attr('pads')
is_pads_attr = True
if pads is None:
val_pad = self.graph.get_input_node(node, idx=1, copy=True)
pad_shape = val_pad.out_shapes[0]
is_pads_attr = False
pads = _const_weight_or_none(val_pad)
if pads is not None:
is_pads_attr = True
mode = node.get_attr('mode', 'constant')
value = node.get_attr('value', 0.)
data_shape = val_x.out_shapes[0]
......@@ -375,6 +383,7 @@ class OpSet9():
assume_pad2d = False
layer_attrs = {}
layer_attrs['mode'] = string(mode)
if is_pads_attr:
paddings = []
if len(pads) == 4:
assume_pad2d |= mode != 'constant'
......@@ -423,6 +432,23 @@ class OpSet9():
outputs=[node.name + '_paded'],
**layer_attrs)
return node.name + '_paded'
else:
if pad_shape[0] == 4:
assume_pad2d |= mode != 'constant'
if data_shape:
assume_pad2d |= data_shape and len(data_shape) == 4 # NCHW
if output_shape:
assume_pad2d |= output_shape and len(output_shape) == 4 # NCHW
if pad_shape[0] == 8 or not assume_pad2d:
raise Exception("When the pad shape is 8 and pad is tensor, the op is not supported yet!")
layer_attrs['value'] = value
self.paddle_graph.add_layer(
"custom_layer:custom_pad",
inputs={'x': val_x.name, 'pad': val_pad.name},
outputs=[node.name + '_paded'],
**layer_attrs)
if not op_independent:
return node.name + '_paded'
@print_mapping_info
def Unsqueeze(self, node):
......@@ -622,7 +648,7 @@ class OpSet9():
self.paddle_graph.add_layer(
'paddle.cast',
inputs={"x": indices.name},
outputs=indices_cast,
outputs=[indices_cast],
dtype=string('int64'))
op_name = name_generator("embedding", self.nn_name2id)
output_name = node.name
......@@ -817,7 +843,7 @@ class OpSet9():
"starts": starts.name,
"ends": ends.name
}
if starts_value is not None and ends_value is not None:
if starts_value is not None and ends_value is not None and axes is not None:
starts_value = starts_value.copy()
ends_value = ends_value.copy()
#for idx in range(len(ends_value)):
......@@ -990,7 +1016,7 @@ class OpSet9():
'paddle.reshape',
inputs={'x': val_x.name,
'shape': val_shape.name},
outputs=node)
outputs=[node.name])
@print_mapping_info
def Cast(self, node):
......@@ -1588,3 +1614,45 @@ class OpSet9():
inputs={"x": val_x.name},
outputs=[node.name],
**layer_attrs)
@print_mapping_info
def Size(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True)
self.paddle_graph.add_layer(
"paddle.shape",
inputs={"x": val_x.name},
outputs=[node.name])
self.paddle_graph.add_layer(
"paddle.prod",
inputs={"x": node.name},
outputs=[node.name])
@print_mapping_info
def Sign(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True)
self.paddle_graph.add_layer(
"paddle.sign",
inputs={"x": val_x.name},
outputs=[node.name])
@print_mapping_info
def OneHot(self, node):
indices = self.graph.get_input_node(node, idx=0, copy=True)
depth = self.graph.get_input_node(node, idx=1, copy=True)
values = self.graph.get_input_node(node, idx=2, copy=True)
axis = node.get_attr('axis', -1)
self.paddle_graph.add_layer(
"custom_layer:one_hot",
inputs={"indices": indices.name,
"depth": depth.name,
"values": values.name},
outputs=[node.name],
axis=axis)
@print_mapping_info
def Reciprocal(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True)
self.paddle_graph.add_layer(
"paddle.reciprocal",
inputs={"x": val_x.name},
outputs=[node.name])
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册