Skip to content
Closed
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
40 changes: 40 additions & 0 deletions model-optimizer/extensions/front/tf/softplus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Copyright (C) 2020 Intel Corporation

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 extensions.ops.activation_ops import Exp, Log
from extensions.ops.elementwise import Add
from mo.front.common.partial_infer.utils import int64_array
from mo.front.common.replacement import FrontReplacementOp
from mo.front.tf.graph_utils import create_op_node_with_second_input
from mo.graph.graph import Graph


class Softplus(FrontReplacementOp):
op = 'Softplus'
enabled = True

def replace_sub_graph(self, graph: Graph, match: dict):
softplus = match['op']

exp_node = Exp(graph, {'name': softplus.name + '/Exp'}).create_node()
add_node = create_op_node_with_second_input(graph, Add, int64_array([1]), {'name': softplus.name + '/Add'})
log_node = Log(graph, {'name': softplus.name + '/Log'}).create_node()

#exp_node.in_port(0).connect(softplus.in_port(0).get_source())
softplus.in_port(0).get_connection().set_destination(exp_node.in_port(0))
add_node.in_port(0).connect(exp_node.out_port(0))
log_node.in_port(0).connect(add_node.out_port(0))
softplus.out_port(0).get_connection().set_source(log_node.out_port(0))
32 changes: 32 additions & 0 deletions model-optimizer/extensions/front/tf/softplus_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Copyright (C) 2020 Intel Corporation

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 mo.front.extractor import FrontExtractorOp
from mo.ops.softmax import LogSoftmax, Softmax


class SoftmaxExtractor(FrontExtractorOp):
op = 'Softplus'
enabled = True

@classmethod
def extract(cls, node):
# the default value for the TF Softmax is -1
# axis = -1
# if 'axis' in node.pb.attr:
# axis = node.pb.attr['axis'].i
# Softmax.update_node_stat(node, {'axis': axis})
return cls.enabled
50 changes: 50 additions & 0 deletions model-optimizer/extensions/front/tf/softplus_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Copyright (C) 2020 Intel Corporation

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

from extensions.front.tf.softplus import Softplus
from mo.front.common.partial_infer.utils import int64_array
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph


class TestSoftPlus(unittest.TestCase):
nodes = {
'node_1': {'shape': int64_array([1, 2, 3, 4]), 'type': 'Parameter', 'kind': 'op', 'op': 'Parameter'},
'softplus': {'value': None, 'kind': 'op', 'op': 'Softplus'},
'exp': {'value': None, 'kind': 'op', 'op': 'Exp'},
'add': {'value': None, 'kind': 'op', 'op': 'Add'},
'add_const': {'value': None, 'kind': 'op', 'op': 'Const'},
'log': {'value': None, 'kind': 'op', 'op': 'Log'},
'last': {'type': None, 'value': None, 'kind': 'op', 'op': 'Result'}
}

def test_softplus_1(self):
graph = build_graph(self.nodes, [('node_1', 'softplus'),
('softplus', 'last')], nodes_with_edges_only=True)

graph_ref = build_graph(self.nodes, [('node_1', 'exp'),
('exp', 'add'),
('add_const', 'add'),
('add', 'log'),
('log', 'last')], nodes_with_edges_only=True)

graph.stage = 'front'
Softplus().find_and_replace_pattern(graph)

(flag, resp) = compare_graphs(graph, graph_ref, 'last', check_op_attrs=True)
self.assertTrue(flag, resp)