Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine quotes' format and comments & add new operators #40

Merged
merged 5 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 77 additions & 20 deletions fluid_onnx/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@


def activation_ops(act_type, operator, block):
""" Convert common activations with type specified by 'act_type', including
'abs', 'ceil', 'exp', 'floor', 'log', 'reciprocal', 'relu', 'sigmoid',
'softplus', 'softsign', 'sqrt' and 'tanh'.
"""

inputs, _, outputs = op_io_info(operator)
return make_node(
act_type, inputs=inputs.values()[0], outputs=outputs.values()[0])
Expand Down Expand Up @@ -114,16 +119,32 @@ def batch_norm_op(operator, block):
bn_node)


def cast_op():
pass
def cast_op(operator, block):
inputs, attrs, outputs = op_io_info(operator)
return make_node(
'Cast',
inputs=inputs['X'],
outputs=outputs['Out'],
to=PADDLE_TO_ONNX_DTYPE[attrs['out_dtype']])


def clip_op():
pass
def clip_op(operator, block):
inputs, attrs, outputs = op_io_info(operator)
return make_node(
'Clip',
inputs=inputs['X'],
outputs=outputs['Out'],
min=attrs['min'],
max=attrs['max'])


def concat_op():
pass
def concat_op(operator, block):
inputs, attrs, outputs = op_io_info(operator)
return make_node(
'Concat',
inputs=inputs['X'],
outputs=outputs['Out'],
axis=attrs['axis'])


def constant_op(var, scope):
Expand Down Expand Up @@ -156,8 +177,20 @@ def conv2d_op(operator, block):
return conv2d


def convtranspose_op():
pass
def conv2d_transpose_op(operator, block):
inputs, attrs, outputs = op_io_info(operator)

kernel_shape = block.vars[inputs['Filter'][0]].shape
conv2d_transpose = make_node(
'ConvTranspose',
inputs=inputs['Input'] + inputs['Filter'],
outputs=outputs['Output'],
dilations=attrs['dilations'],
kernel_shape=kernel_shape[-2:],
strides=attrs['strides'],
group=1,
pads=attrs['paddings'] + attrs['paddings'])
return conv2d_transpose


def depthtospace_op():
Expand All @@ -184,12 +217,19 @@ def dropout_op(operator, block):


def elementwise_ops(op_type, operator, block):
"""Convert elementwise operators From to ONNX. Supported elementwise
'op_type' includes 'Add', 'Div', 'Mul', 'Pow' and 'Sub'.
"""

inputs, attrs, outputs = op_io_info(operator)
rank_x = len(block.vars[inputs['X'][0]].shape)
rank_y = len(block.vars[inputs['Y'][0]].shape)
axis = rank_x - rank_y if attrs['axis'] == -1 else attrs['axis']
return make_node(
op_type,
inputs=inputs['X'] + inputs['Y'],
outputs=outputs['Out'],
axis=attrs['axis'],
axis=axis,
broadcast=1)


Expand Down Expand Up @@ -260,8 +300,21 @@ def less_op():
pass


def log_op():
pass
def binary_logical_ops(op_type, operator, block):
"""Convert binary logical operators, i.e. 'And', 'Or' and 'Xor'.
"""

inputs, _, outputs = op_io_info(operator)
return make_node(
op_type, inputs=inputs['X'] + inputs['Y'], outputs=outputs['Out'])


def unary_logical_ops(op_type, operator, block):
"""Convert unary logical operators, i.e. 'Not'.
"""

inputs, _, outputs = op_io_info(operator)
return make_node(op_type, inputs=inputs['X'], outputs=outputs['Out'])


def logsoftmax_op():
Expand Down Expand Up @@ -422,6 +475,11 @@ def randomuniformlike_op():


def reduce_ops(op_type, operator, block):
"""Convert reduce operators in Fluid to ONNX. 'op_type' specifies the
target ONNX operator type, supporting 'Reduce{Max, Mean, Min, Sum}'
right now.
"""

inputs, attrs, outputs = op_io_info(operator)
rank = len(block.vars[inputs['X'][0]].shape)
dim = attrs['dim']
Expand Down Expand Up @@ -549,19 +607,17 @@ def xor_op():
node_maker = {
# Paddle op name : (ONNX op name, modifier)
'abs': partial(activation_ops, 'Abs'),
# '': 'And', # ?
# 'ArgMax', NEEDS ATTENTION.
# 'ArgMin', NEEDS ATTENTION.
'batch_norm': batch_norm_op,
'cast': ('Cast', cast_op),
'cast': cast_op,
'ceil': partial(activation_ops, 'Ceil'),
'clip': ('Clip', clip_op),
'concat': ('Concat', concat_op),
'clip': clip_op,
'concat': concat_op,
'constant': constant_op,
'conv2d': conv2d_op,

# Need to continue the mapping below.
'': 'ConvTranspose',
'conv2d_transpose': conv2d_transpose_op,
'': 'DepthToSpace',
'depthwise_conv2d': conv2d_op,
'dropout': dropout_op,
Expand All @@ -588,6 +644,10 @@ def xor_op():
'': 'LeakyRelu',
'': 'Less',
'log': partial(activation_ops, 'Log'),
'logical_and': partial(binary_logical_ops, 'And'),
'logical_or': partial(binary_logical_ops, 'Or'),
'logical_not': partial(unary_logical_ops, 'Not'),
'logical_xor': partial(binary_logical_ops, 'Xor'),
',': 'LogSoftmax',
'': 'LpNormalization',
'': 'LpPool',
Expand All @@ -599,8 +659,6 @@ def xor_op():
'': 'Min',
'mul': mul_op,
',': 'Neg',
'': 'Not',
'': 'Or',
'': 'PRelu',
'': 'Pad',
'pool2d': pool2d_op,
Expand Down Expand Up @@ -640,7 +698,6 @@ def xor_op():
'': 'TopK',
'': 'Transpose',
# 'Unsqueeze', NEEDS ATTENTION.
'': 'Xor',
# 'experimental ATen'
# ',': 'experimental Affine'
# 'experimental ConstantFill'
Expand Down
2 changes: 1 addition & 1 deletion fluid_onnx/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def paddle_onnx_shape(paddle_shape):

PADDLE_TO_ONNX_DTYPE = {
core.VarDesc.VarType.FP32: onnx_pb2.TensorProto.FLOAT,
core.VarDesc.VarType.FP64: onnx_pb2.TensorProto.FLOAT16,
core.VarDesc.VarType.FP64: onnx_pb2.TensorProto.DOUBLE,
# '': onnx_pb2.TensorProto.DOUBLE,
core.VarDesc.VarType.INT32: onnx_pb2.TensorProto.INT32,
core.VarDesc.VarType.INT16: onnx_pb2.TensorProto.INT16,
Expand Down
12 changes: 10 additions & 2 deletions tests/op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def create_var(block, name, np_list, var_proto):

persistable = True if name in persistable_list else False
return block.create_var(
dtype="float32",
dtype='float32',
shape=shape,
persistable=persistable,
lod_level=lod_level,
Expand Down Expand Up @@ -263,9 +263,17 @@ def eval_onnx_node(self):
onnx_graph = make_graph(node_list, self.op_type, inputs, outputs)
onnx_model = make_model(onnx_graph, producer_name='unittest')

# Expand input dictionary if there are tensor arrays
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E.g. the input of concat_op:

self.inputs = {'X': [('x0', self.x0), ('x1', self.x1), ('x2', self.x2)]}

input_map = {}
for v in self.inputs:
if isinstance(self.inputs[v], list):
input_map.update(self.inputs[v])
else:
input_map[v] = self.inputs[v]

# Run the Caffe2Backend with the ONNX model.
rep = Caffe2Backend.prepare(onnx_model, device='CPU')
in_vals = [self.inputs[input.name] for input in inputs]
in_vals = [input_map[input.name] for input in inputs]
outs = rep.run(in_vals)

return outs
Expand Down
4 changes: 2 additions & 2 deletions tests/test_activation_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

class TestAbsOp(OpTest):
def setUp(self):
X = np.random.random((13, 15)).astype("float32")
X = np.random.random((13, 15)).astype('float32')
self.inputs = {'X': X}
self.outputs = {'Out': np.zeros((1, 1)).astype("float32")}
self.outputs = {'Out': np.zeros((1, 1)).astype('float32')}
self.init_op_type()

def init_op_type(self):
Expand Down
37 changes: 37 additions & 0 deletions tests/test_cast_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) 2018 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 unittest
import numpy as np
import paddle.fluid.core as core
from op_test import OpTest


class TestCastOp(OpTest):
def setUp(self):
input = np.random.random((10, 10))
self.inputs = {'X': input.astype('float32')}
self.outputs = {'Out': input.astype('float64')}
self.attrs = {
'in_dtype': int(core.VarDesc.VarType.FP32),
'out_dtype': int(core.VarDesc.VarType.FP64)
}
self.op_type = 'cast'

def test_check_output(self):
self.check_output()


if __name__ == '__main__':
unittest.main()
33 changes: 33 additions & 0 deletions tests/test_clip_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2018 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 unittest
import numpy as np
from op_test import OpTest


class TestClipOp(OpTest):
def setUp(self):
input = np.random.random((4, 5, 6)).astype('float32')
self.op_type = 'clip'
self.inputs = {'X': input}
self.attrs = {'min': 0.2, 'max': 0.8}
self.outputs = {'Out': np.zeros((1, 1)).astype('float32')}

def test_check_output(self):
self.check_output()


if __name__ == '__main__':
unittest.main()
47 changes: 47 additions & 0 deletions tests/test_concat_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (c) 2018 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 unittest
import numpy as np
from op_test import OpTest


class TestConcatOp(OpTest):
def setUp(self):
self.op_type = 'concat'
self.init_test_data()
self.inputs = {'X': [('x0', self.x0), ('x1', self.x1), ('x2', self.x2)]}
self.attrs = {'axis': self.axis}
self.outputs = {'Out': np.zeros((1, 1)).astype('float32')}

def test_check_output(self):
self.check_output()

def init_test_data(self):
self.x0 = np.random.random((2, 1, 4, 5)).astype('float32')
self.x1 = np.random.random((2, 2, 4, 5)).astype('float32')
self.x2 = np.random.random((2, 3, 4, 5)).astype('float32')
self.axis = 1


class TestConcatOp2(OpTest):
def init_test_data(self):
self.x0 = np.random.random((2, 3, 4, 5)).astype('float32')
self.x1 = np.random.random((2, 3, 4, 5)).astype('float32')
self.x2 = np.random.random((2, 3, 4, 5)).astype('float32')
self.axis = 2


if __name__ == '__main__':
unittest.main()
4 changes: 2 additions & 2 deletions tests/test_conv2d_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ def setUp(self):
self.outputs = {'Output': output}

def init_conv_type(self):
self.op_type = "conv2d"
self.op_type = 'conv2d'

def test_check_output(self):
self.check_output(decimal=5)


class TestDepthwiseConv2dOp(TestConv2dOp):
def init_conv_type(self):
self.op_type = "depthwise_conv2d"
self.op_type = 'depthwise_conv2d'


if __name__ == '__main__':
Expand Down
Loading