Skip to content

Commit

Permalink
Support 7-15 ONNX opset version (#838)
Browse files Browse the repository at this point in the history
* support 7-15 opset version

* re-lint

* re-lint
  • Loading branch information
wjj19950828 committed Jul 25, 2022
1 parent ab0cdac commit 38cd5e1
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 76 deletions.
21 changes: 14 additions & 7 deletions x2paddle/op_mapper/onnx2paddle/onnx_op_mapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 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.
Expand All @@ -13,14 +13,22 @@
# limitations under the License.

import sys
from x2paddle.op_mapper.onnx2paddle.opset9 import OpSet9
from .opset7 import OpSet7
from .opset8 import OpSet8
from .opset9 import OpSet9
from .opset10 import OpSet10
from .opset11 import OpSet11
from .opset12 import OpSet12
from .opset13 import OpSet13
from .opset14 import OpSet14
from .opset15 import OpSet15
from x2paddle.decoder.onnx_decoder import ONNXGraphNode
from x2paddle.core.program import PaddleGraph


class ONNXOpMapper():
def __init__(self, decoder):
self.support_op_sets = [9, ]
self.support_op_sets = [7, 8, 9, 10, 11, 12, 13, 14, 15]
self.default_op_set = 9
self.graph = decoder.graph
self.paddle_graph = PaddleGraph(parent_layer=None, source_type="onnx")
Expand Down Expand Up @@ -84,8 +92,7 @@ def create_opset(self, decoder):
else:
break
opset = 'OpSet' + str(run_op_set)
print(
'Now, onnx2paddle support convert onnx model opset_verison {},'
'opset_verison of your onnx model is {}, automatically treated as op_set: {}.'
.format(self.support_op_sets, decoder.op_set, run_op_set))
print('Now, onnx2paddle support convert onnx model opset_verison {}, '
'opset_verison of your onnx model is {}.'
.format(self.support_op_sets, decoder.op_set))
return eval(opset)(decoder, self.paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet10(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet10, self).__init__(decoder, paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet11(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet11, self).__init__(decoder, paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet12(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet12, self).__init__(decoder, paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet13(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet13, self).__init__(decoder, paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet14(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet14, self).__init__(decoder, paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet15(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet15, self).__init__(decoder, paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet7(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet7, self).__init__(decoder, paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet8(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet8, self).__init__(decoder, paddle_graph)
20 changes: 20 additions & 0 deletions x2paddle/op_mapper/onnx2paddle/opset9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2022 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 .opset_legacy import OpSet


class OpSet9(OpSet):
def __init__(self, decoder, paddle_graph):
super(OpSet9, self).__init__(decoder, paddle_graph)
1 change: 0 additions & 1 deletion x2paddle/op_mapper/onnx2paddle/opset9/__init__.py

This file was deleted.

137 changes: 69 additions & 68 deletions ...dle/op_mapper/onnx2paddle/opset9/opset.py → ...dle/op_mapper/onnx2paddle/opset_legacy.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 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.
Expand Down Expand Up @@ -114,74 +114,9 @@ def run_mapping(*args, **kwargs):
return run_mapping


class OpSet9():
elementwise_ops = {
'Add': 'paddle.add',
'Div': 'paddle.divide',
'Sub': 'paddle.subtract',
'Mul': 'paddle.multiply',
'Pow': 'paddle.pow',
'Less': 'paddle.less_than',
'LessOrEqual': 'paddle.less_equal',
}

directly_map_ops = {
'Ceil': ['paddle.ceil'],
# reduce function
'ReduceMean': [
'paddle.mean', dict(
axes='axis', keepdims='keepdim'), dict(
axes=None, keepdims=True)
],
'ReduceMin': [
'paddle.min', dict(
axes='axis', keepdims='keepdim'), dict(
axes=None, keepdim=True)
],
'ReduceMax': [
'paddle.max', dict(
axes='axis', keepdims='keepdim'), dict(
axes=None, keepdim=True)
],
'ReduceProd': [
'paddle.prod', dict(
axes='axis', keepdims='keepdim'), dict(
axes=None, keepdim=True)
],
# active function
'Relu': ['paddle.nn.ReLU'],
'LeakyRelu': [
'paddle.nn.LeakyReLU', dict(alpha='negative_slope'),
dict(negative_slope=.01)
],
'Elu':
['paddle.nn.functional.elu', dict(alpha='alpha'), dict(alpha=1.)],
'ThresholdedRelu': [
'paddle.nn.functional.thresholded_relu', dict(alpha='threshold'),
dict(alpha=1.)
],
'Tanh': ['paddle.nn.Tanh'],
'Sigmoid': ['paddle.nn.Sigmoid'],
'Softsign': ['paddle.nn.Softsign'],
'Softplus': [
'paddle.nn.Softplus', dict(threshold='threshold'),
dict(threshold=float(sys.maxsize))
],
'Exp': ['paddle.exp'],
'Log': ['paddle.log'],
'LogSoftmax':
['paddle.nn.functional.log_softmax', dict(axis='axis'), dict(axis=1)],
'Softmax': ['paddle.nn.Softmax', dict(axis='axis'), dict(axis=1)],
'Sqrt': ['paddle.sqrt'],
'Floor': ['paddle.floor'],
'Abs': ['paddle.abs'],
'Erf': ['paddle.erf'],
'Sin': ['paddle.sin'],
'Cos': ['paddle.cos'],
}

class OpSet():
def __init__(self, decoder, paddle_graph):
super(OpSet9, self).__init__()
super(OpSet, self).__init__()
self.graph = decoder.graph
self.paddle_graph = paddle_graph
self.inputs_info = dict()
Expand All @@ -191,6 +126,72 @@ def __init__(self, decoder, paddle_graph):
# solve for same data is used as an argument to multiple OPs.
# PR link(wangjunjie06): https://github.com/PaddlePaddle/X2Paddle/pull/728
self.rename_mapper = dict()
self.elementwise_ops = {
'Add': 'paddle.add',
'Div': 'paddle.divide',
'Sub': 'paddle.subtract',
'Mul': 'paddle.multiply',
'Pow': 'paddle.pow',
'Less': 'paddle.less_than',
'LessOrEqual': 'paddle.less_equal',
}

self.directly_map_ops = {
'Ceil': ['paddle.ceil'],
# reduce function
'ReduceMean': [
'paddle.mean', dict(
axes='axis', keepdims='keepdim'), dict(
axes=None, keepdims=True)
],
'ReduceMin': [
'paddle.min', dict(
axes='axis', keepdims='keepdim'), dict(
axes=None, keepdim=True)
],
'ReduceMax': [
'paddle.max', dict(
axes='axis', keepdims='keepdim'), dict(
axes=None, keepdim=True)
],
'ReduceProd': [
'paddle.prod', dict(
axes='axis', keepdims='keepdim'), dict(
axes=None, keepdim=True)
],
# active function
'Relu': ['paddle.nn.ReLU'],
'LeakyRelu': [
'paddle.nn.LeakyReLU', dict(alpha='negative_slope'),
dict(negative_slope=.01)
],
'Elu':
['paddle.nn.functional.elu', dict(alpha='alpha'), dict(alpha=1.)],
'ThresholdedRelu': [
'paddle.nn.functional.thresholded_relu',
dict(alpha='threshold'), dict(alpha=1.)
],
'Tanh': ['paddle.nn.Tanh'],
'Sigmoid': ['paddle.nn.Sigmoid'],
'Softsign': ['paddle.nn.Softsign'],
'Softplus': [
'paddle.nn.Softplus', dict(threshold='threshold'),
dict(threshold=float(sys.maxsize))
],
'Exp': ['paddle.exp'],
'Log': ['paddle.log'],
'LogSoftmax': [
'paddle.nn.functional.log_softmax', dict(axis='axis'),
dict(axis=1)
],
'Softmax': ['paddle.nn.Softmax', dict(axis='axis'), dict(axis=1)],
'Sqrt': ['paddle.sqrt'],
'Floor': ['paddle.floor'],
'Abs': ['paddle.abs'],
'Erf': ['paddle.erf'],
'Sin': ['paddle.sin'],
'Cos': ['paddle.cos'],
}

@print_mapping_info
def directly_map(self, node, *args, **kwargs):
Expand Down

0 comments on commit 38cd5e1

Please sign in to comment.