Implement ScatterND operation in MO and transform for SparseToDense (#584)
SparseToDense used in Wide and Deep model is expressed through ScatterND operation. ScatterND is more functional than SparseToDense. Hence, it was decided to replace SparseToDense with ScatterND. ScatterND is more useful for other models. Remove SparseToDense from the previous opset Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
This commit is contained in:
1 parent
297c9f5272
commit
7b65ba365e
11 files changed
+405
-202
No files matched your search
@@ -111,7 +111,6 @@ declared in `namespace opset3`.
|
||||
* [ROIAlign](detection/ROIAlign_3.md)
|
||||
* [ROIPooling](detection/ROIPooling_1.md)
|
||||
* [ScatterElementsUpdate](movement/ScatterElementsUpdate_3.md)
|
||||
* [ScatterNDUpdate](movement/ScatterNDUpdate_3.md)
|
||||
* [ScatterUpdate](movement/ScatterUpdate_3.md)
|
||||
* [Select](condition/Select_1.md)
|
||||
* [Selu](arithmetic/Selu_1.md)
|
||||
|
||||
@@ -428,7 +428,7 @@ extensions/front/tf/sparse_fill_empty_rows_ext.py
|
||||
extensions/front/tf/sparse_segment_mean_ext.py
|
||||
extensions/front/tf/sparse_segment_sqrtn_ext.py
|
||||
extensions/front/tf/sparse_segment_sum_ext.py
|
||||
extensions/front/tf/sparse_to_dense_ext.py
|
||||
extensions/front/tf/sparse_to_dense_replacer.py
|
||||
extensions/front/tf/split_ext.py
|
||||
extensions/front/tf/ssd_support.json
|
||||
extensions/front/tf/ssd_support_api_v1.14.json
|
||||
@@ -654,6 +654,7 @@ extensions/ops/RNNCell.py
|
||||
extensions/ops/roialign.py
|
||||
extensions/ops/roifeatureextractor_onnx.py
|
||||
extensions/ops/scatter.py
|
||||
extensions/ops/scatternd.py
|
||||
extensions/ops/select.py
|
||||
extensions/ops/shufflechannel.py
|
||||
extensions/ops/simplernms.py
|
||||
@@ -665,7 +666,6 @@ extensions/ops/sparse_reshape.py
|
||||
extensions/ops/sparse_segment_mean.py
|
||||
extensions/ops/sparse_segment_sqrtn.py
|
||||
extensions/ops/sparse_segment_sum.py
|
||||
extensions/ops/sparse_to_dense.py
|
||||
extensions/ops/spatial_transformer.py
|
||||
extensions/ops/splice.py
|
||||
extensions/ops/split.py
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
"""
|
||||
Copyright (C) 2018-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.sparse_to_dense import SparseToDense
|
||||
from mo.front.extractor import FrontExtractorOp
|
||||
|
||||
|
||||
class SparseToDenseFrontExtractor(FrontExtractorOp):
|
||||
op = 'SparseToDense'
|
||||
enabled = True
|
||||
|
||||
@classmethod
|
||||
def extract(cls, node):
|
||||
SparseToDense.update_node_stat(node)
|
||||
return cls.enabled
|
||||
@@ -0,0 +1,66 @@
|
||||
"""
|
||||
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 numpy as np
|
||||
|
||||
from extensions.ops.Cast import Cast
|
||||
from extensions.ops.scatternd import ScatterNDUpdate
|
||||
from mo.front.common.replacement import FrontReplacementOp
|
||||
from mo.graph.graph import Node, Graph, rename_nodes
|
||||
from mo.ops.broadcast import Broadcast
|
||||
from mo.ops.const import Const
|
||||
|
||||
|
||||
class SparseToDenseReplacer(FrontReplacementOp):
|
||||
"""
|
||||
This replacer substitutes TensorFlow SparseToDense operation with Broadcast -> ScatterND chain.
|
||||
The Broadcast operation creates a tensor filled with default value and of required shape.
|
||||
The ScatterND operation updates the created tensor with required values at required locations.
|
||||
"""
|
||||
op = "SparseToDense"
|
||||
enabled = True
|
||||
|
||||
def run_after(self):
|
||||
from extensions.front.tf.CTCGreedyDecoder import CTCGreedyDecoderReplacement
|
||||
return [CTCGreedyDecoderReplacement]
|
||||
|
||||
def replace_op(self, graph: Graph, node: Node):
|
||||
node_name = node.soft_get('name', node.id)
|
||||
|
||||
# broadcast default value to required shape
|
||||
broadcast_node = Broadcast(graph, {'name': node_name + '/Broadcast_'}).create_node()
|
||||
node.in_port(1).get_connection().set_destination(broadcast_node.in_port(1))
|
||||
if not node.in_port(3).disconnected():
|
||||
# TODO: remove casting once we start to support I64 model input
|
||||
# cast default value to I32 due limitation about I64 input support
|
||||
# so that input parameter and default value will be of the same I32 type as required ScatterNDUpdate
|
||||
cast_default_value = Cast(graph, {'name': node_name + '/CastDefaultValue', 'dst_type': np.int32}).create_node()
|
||||
node.in_port(3).get_connection().set_destination(cast_default_value.in_port(0))
|
||||
broadcast_node.in_port(0).connect(cast_default_value.out_port(0))
|
||||
else:
|
||||
broadcast_node.in_port(0).connect(Const(graph, {'name': broadcast_node.name + '/FillValue_',
|
||||
'value': np.float32(0)}
|
||||
).create_node().out_port(0))
|
||||
|
||||
# update broadcasted tensor with required values at required locations
|
||||
scatternd_node = ScatterNDUpdate(graph, {'name': node_name + '/ScatterNDUpdate_'}).create_node()
|
||||
scatternd_node.in_port(0).connect(broadcast_node.out_port(0))
|
||||
node.in_port(0).get_connection().set_destination(scatternd_node.in_port(1))
|
||||
node.in_port(2).get_connection().set_destination(scatternd_node.in_port(2))
|
||||
|
||||
rename_nodes([(node, node_name + "/AbandonedName"), (scatternd_node, node_name)])
|
||||
|
||||
return [scatternd_node.id]
|
||||
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
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.sparse_to_dense_replacer import SparseToDenseReplacer
|
||||
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
|
||||
from mo.utils.unittest.graph import build_graph, const
|
||||
|
||||
|
||||
class SparseToDenseFrontReplacersTest(unittest.TestCase):
|
||||
def test1(self):
|
||||
nodes_attributes = {
|
||||
'input_indices': {'shape': int64_array([5, 2]), 'type': 'Parameter', 'kind': 'op', 'op': 'Parameter'},
|
||||
'input_values' : {'shape': int64_array([5]), 'type': 'Parameter', 'kind': 'op', 'op': 'Parameter'},
|
||||
|
||||
'sparse_to_dense' : {'kind': 'op', 'op': 'SparseToDense'},
|
||||
'broadcast' : {'kind': 'op', 'op': 'Broadcast'},
|
||||
'scatternd' : {'kind': 'op', 'op': 'ScatterNDUpdate'},
|
||||
'cast_default_value': {'kind': 'op', 'op': 'Cast'},
|
||||
|
||||
'last': {'type': None, 'value': None, 'kind': 'op', 'op': 'Result'},
|
||||
|
||||
**const('input_dense_shape', int64_array([50, 40])),
|
||||
**const('input_default_value', int64_array(0))}
|
||||
|
||||
graph = build_graph(nodes_attributes,
|
||||
[('input_indices', 'sparse_to_dense', {'out': 0, 'in': 0}),
|
||||
('input_dense_shape', 'sparse_to_dense', {'out': 0, 'in': 1}),
|
||||
('input_values', 'sparse_to_dense', {'out': 0, 'in': 2}),
|
||||
('input_default_value', 'sparse_to_dense', {'out': 0, 'in': 3}),
|
||||
('sparse_to_dense', 'last', {'out': 0, 'in': 0})],
|
||||
nodes_with_edges_only=True)
|
||||
graph.stage = 'front'
|
||||
SparseToDenseReplacer().find_and_replace_pattern(graph)
|
||||
|
||||
graph_ref = build_graph(nodes_attributes,
|
||||
[('input_default_value', 'cast_default_value', {'in': 0}),
|
||||
('cast_default_value', 'broadcast', {'in': 0}),
|
||||
('input_dense_shape', 'broadcast', {'in': 1}),
|
||||
('broadcast', 'scatternd', {'in': 0}),
|
||||
('input_indices', 'scatternd', {'in': 1}),
|
||||
('input_values', 'scatternd', {'in': 2}),
|
||||
('scatternd', 'last', {'in': 0})],
|
||||
nodes_with_edges_only=True)
|
||||
|
||||
(flag, resp) = compare_graphs(graph, graph_ref, 'last', check_op_attrs=True)
|
||||
self.assertTrue(flag, resp)
|
||||
@@ -0,0 +1,103 @@
|
||||
"""
|
||||
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 numpy as np
|
||||
|
||||
from mo.front.common.partial_infer.utils import int64_array
|
||||
from mo.graph.graph import Node, Graph
|
||||
from mo.ops.op import Op
|
||||
|
||||
|
||||
class ScatterNDBase(Op):
|
||||
enabled = False
|
||||
|
||||
op = op_type = None
|
||||
version = None
|
||||
|
||||
def __init__(self, graph: Graph, attrs: dict):
|
||||
assert self.op is not None and self.op_type is not None and self.version is not None, \
|
||||
'Please use specialized ScatterNDBase operation class, ScatterNDBase is base class'
|
||||
|
||||
mandatory_props = {
|
||||
'op': self.op,
|
||||
'type': self.op_type,
|
||||
'version': self.version,
|
||||
|
||||
'infer': self.infer,
|
||||
|
||||
'in_ports_count': 3,
|
||||
'out_ports_count': 1,
|
||||
}
|
||||
super().__init__(graph, mandatory_props, attrs)
|
||||
|
||||
@staticmethod
|
||||
def infer(node: Node):
|
||||
node_name = node.soft_get('name', node.id)
|
||||
|
||||
input_shape = node.in_port(0).data.get_shape()
|
||||
indices_shape = node.in_port(1).data.get_shape()
|
||||
updates_shape = node.in_port(2).data.get_shape()
|
||||
assert input_shape is not None and updates_shape is not None and indices_shape is not None, \
|
||||
'The node "{}" input shape is None'.format(node_name)
|
||||
|
||||
# check that shapes are correct
|
||||
# 1. ranks of both input and indices must be at least 1
|
||||
assert len(input_shape) >= 1 and len(indices_shape) >= 1, \
|
||||
'The node "{}" input and indices ranks must be at least 1'.format(node_name)
|
||||
|
||||
# 2. the last dimension of indices shape must be at most a rank of input
|
||||
assert indices_shape[-1] <= len(input_shape), \
|
||||
'The last dimension of indices shape must be at most a rank of input for the node "{}"'.format(node_name)
|
||||
|
||||
# 3. updates is a tensor of shape indices_shape[:-1] + input_shape[indices_shape[-1]:]
|
||||
expected_updates_shape = np.concatenate((indices_shape[:-1], input_shape[indices_shape[-1]:]), axis=0)
|
||||
assert np.array_equal(updates_shape, expected_updates_shape), \
|
||||
'The updates shape must be equal to indices_shape[:-1] + input_shape[indices_shape[-1]:] for the node "{}"'.format(node_name)
|
||||
|
||||
node.out_port(0).data.set_shape(input_shape)
|
||||
|
||||
@staticmethod
|
||||
def type_infer(node: Node):
|
||||
assert node.in_port(0).get_source().get_data_type() == node.in_port(2).get_source().get_data_type(), \
|
||||
'The data type of the first and the third inputs must be equal for the node {}'.format(node.name)
|
||||
node.out_port(0).set_data_type(node.in_port(0).get_data_type())
|
||||
|
||||
|
||||
class ScatterNDUpdate(ScatterNDBase):
|
||||
op = op_type = 'ScatterNDUpdate'
|
||||
version = 'opset4'
|
||||
|
||||
@staticmethod
|
||||
def infer(node: Node):
|
||||
ScatterNDBase.infer(node)
|
||||
|
||||
input_value = node.in_port(0).data.get_value()
|
||||
indices_shape = node.in_port(1).data.get_shape()
|
||||
indices_value = node.in_port(1).data.get_value()
|
||||
updates_value = node.in_port(2).data.get_value()
|
||||
|
||||
# compute output value if all inputs are constant
|
||||
if input_value is not None and indices_value is not None and updates_value is not None:
|
||||
output_value = input_value.copy()
|
||||
indx_range = int64_array(indices_shape[:-1])
|
||||
for indx in np.ndindex(tuple(indx_range)):
|
||||
if indx == ():
|
||||
# a case when updates is a scalar
|
||||
indx = 0
|
||||
updates_value = [updates_value]
|
||||
output_value[indices_value[indx]] = updates_value[indx]
|
||||
|
||||
node.out_port(0).data.set_value(output_value)
|
||||
@@ -0,0 +1,166 @@
|
||||
"""
|
||||
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
|
||||
|
||||
import numpy as np
|
||||
|
||||
from extensions.ops.scatternd import ScatterNDUpdate
|
||||
from mo.front.common.partial_infer.utils import int64_array
|
||||
from mo.graph.graph import Node
|
||||
from mo.utils.unittest.graph import build_graph
|
||||
|
||||
nodes_attributes = {'input': {'shape': None, 'value': None, 'kind': 'data'},
|
||||
'indices': {'shape': None, 'value': None, 'kind': 'data'},
|
||||
'updates': {'shape': None, 'value': None, 'kind': 'data'},
|
||||
'scatternd_node': {'op': 'ScatterNDUpdate', 'kind': 'op'},
|
||||
'output': {'shape': None, 'value': None, 'kind': 'data'}}
|
||||
|
||||
# graph 1
|
||||
edges = [('input', 'scatternd_node', {'in': 0}),
|
||||
('indices', 'scatternd_node', {'in': 1}),
|
||||
('updates', 'scatternd_node', {'in': 2}),
|
||||
('scatternd_node', 'output', {'out': 0})]
|
||||
|
||||
# test data for partial infer
|
||||
inputs1 = {'input': {'shape': int64_array([10, 40]), 'value': None},
|
||||
'indices': {'shape': int64_array([3, 2]), 'value': None},
|
||||
'updates': {'shape': int64_array([3]), 'value': None}}
|
||||
|
||||
inputs2 = {'input': {'shape': int64_array([20, 30]), 'value': None},
|
||||
'indices': {'shape': int64_array([2]), 'value': None},
|
||||
'updates': {'shape': int64_array([]), 'value': None}}
|
||||
|
||||
inputs3 = {'input': {'shape': int64_array([20, 30, 5]), 'value': None},
|
||||
'indices': {'shape': int64_array([2]), 'value': None},
|
||||
'updates': {'shape': int64_array([5]), 'value': None}}
|
||||
|
||||
inputs4 = {'input': {'shape': int64_array([10, 40, 50]), 'value': None},
|
||||
'indices': {'shape': int64_array([7, 3, 2]), 'value': None},
|
||||
'updates': {'shape': int64_array([7, 3, 50]), 'value': None}}
|
||||
|
||||
# test data for constant folding
|
||||
inputs5 = {'input': {'shape': int64_array([8]), 'value': int64_array([1, 2, 3, 4, 5, 6, 7, 8])},
|
||||
'indices': {'shape': int64_array([4, 1]), 'value': int64_array([[4], [3], [1], [7]])},
|
||||
'updates': {'shape': int64_array([4]), 'value': int64_array([9, 10, 11, 12])}}
|
||||
output5 = int64_array([1, 11, 3, 10, 9, 6, 7, 12])
|
||||
|
||||
inputs6 = {'input': {'shape': int64_array([4, 4, 4]), 'value': int64_array([[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
|
||||
[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
|
||||
[[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],
|
||||
[[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]])},
|
||||
'indices': {'shape': int64_array([2, 1]), 'value': int64_array([[0], [2]])},
|
||||
'updates': {'shape': int64_array([2, 4, 4]), 'value': int64_array([[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
|
||||
[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]])}}
|
||||
output6 = int64_array([[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
|
||||
[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
|
||||
[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],
|
||||
[[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]])
|
||||
|
||||
inputs7 = {'input': {'shape': int64_array([8]), 'value': int64_array([1, 2, 3, 4, 5, 6, 7, 8])},
|
||||
'indices': {'shape': int64_array([1]), 'value': int64_array([4])},
|
||||
'updates': {'shape': int64_array([]), 'value': 9}}
|
||||
output7 = int64_array([1, 2, 3, 4, 9, 6, 7, 8])
|
||||
|
||||
class TestScatterNDUpdate(unittest.TestCase):
|
||||
def test_partial_infer1(self):
|
||||
graph = build_graph(nodes_attributes, edges, inputs1)
|
||||
scatternd_node = Node(graph, 'scatternd_node')
|
||||
ScatterNDUpdate.infer(scatternd_node)
|
||||
|
||||
# prepare reference results
|
||||
ref_output_shape = np.array([10, 40], dtype=np.int32)
|
||||
|
||||
# get the result
|
||||
res_output_shape = graph.node['output']['shape']
|
||||
|
||||
self.assertTrue(np.array_equal(ref_output_shape, res_output_shape),
|
||||
'values do not match expected: {} and given: {}'.format(ref_output_shape, res_output_shape))
|
||||
|
||||
def test_partial_infer2(self):
|
||||
graph = build_graph(nodes_attributes, edges, inputs2)
|
||||
scatternd_node = Node(graph, 'scatternd_node')
|
||||
ScatterNDUpdate.infer(scatternd_node)
|
||||
|
||||
# prepare reference results
|
||||
ref_output_shape = np.array([20, 30], dtype=np.int32)
|
||||
|
||||
# get the result
|
||||
res_output_shape = graph.node['output']['shape']
|
||||
|
||||
self.assertTrue(np.array_equal(ref_output_shape, res_output_shape),
|
||||
'values do not match expected: {} and given: {}'.format(ref_output_shape, res_output_shape))
|
||||
|
||||
def test_partial_infer3(self):
|
||||
graph = build_graph(nodes_attributes, edges, inputs3)
|
||||
scatternd_node = Node(graph, 'scatternd_node')
|
||||
ScatterNDUpdate.infer(scatternd_node)
|
||||
|
||||
# prepare reference results
|
||||
ref_output_shape = np.array([20, 30, 5], dtype=np.int32)
|
||||
|
||||
# get the result
|
||||
res_output_shape = graph.node['output']['shape']
|
||||
|
||||
self.assertTrue(np.array_equal(ref_output_shape, res_output_shape),
|
||||
'values do not match expected: {} and given: {}'.format(ref_output_shape, res_output_shape))
|
||||
|
||||
def test_partial_infer4(self):
|
||||
graph = build_graph(nodes_attributes, edges, inputs4)
|
||||
scatternd_node = Node(graph, 'scatternd_node')
|
||||
ScatterNDUpdate.infer(scatternd_node)
|
||||
|
||||
# prepare reference results
|
||||
ref_output_shape = np.array([10, 40, 50], dtype=np.int32)
|
||||
|
||||
# get the result
|
||||
res_output_shape = graph.node['output']['shape']
|
||||
|
||||
self.assertTrue(np.array_equal(ref_output_shape, res_output_shape),
|
||||
'values do not match expected: {} and given: {}'.format(ref_output_shape, res_output_shape))
|
||||
|
||||
def test_infer5(self):
|
||||
graph = build_graph(nodes_attributes, edges, inputs5)
|
||||
scatternd_node = Node(graph, 'scatternd_node')
|
||||
ScatterNDUpdate.infer(scatternd_node)
|
||||
|
||||
# get the result
|
||||
res_output_value = graph.node['output']['value']
|
||||
|
||||
self.assertTrue(np.array_equal(output5, res_output_value),
|
||||
'values do not match expected: {} and given: {}'.format(output5, res_output_value))
|
||||
|
||||
def test_infer6(self):
|
||||
graph = build_graph(nodes_attributes, edges, inputs6)
|
||||
scatternd_node = Node(graph, 'scatternd_node')
|
||||
ScatterNDUpdate.infer(scatternd_node)
|
||||
|
||||
# get the result
|
||||
res_output_value = graph.node['output']['value']
|
||||
|
||||
self.assertTrue(np.array_equal(output6, res_output_value),
|
||||
'values do not match expected: {} and given: {}'.format(output5, res_output_value))
|
||||
|
||||
def test_infer7_scalar(self):
|
||||
graph = build_graph(nodes_attributes, edges, inputs7)
|
||||
scatternd_node = Node(graph, 'scatternd_node')
|
||||
ScatterNDUpdate.infer(scatternd_node)
|
||||
|
||||
# get the result
|
||||
res_output_value = graph.node['output']['value']
|
||||
|
||||
self.assertTrue(np.array_equal(output7, res_output_value),
|
||||
'values do not match expected: {} and given: {}'.format(output5, res_output_value))
|
||||
@@ -1,84 +0,0 @@
|
||||
"""
|
||||
Copyright (C) 2018-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 numpy as np
|
||||
|
||||
from mo.graph.graph import Node, Graph
|
||||
from mo.ops.op import Op
|
||||
|
||||
|
||||
class SparseToDense(Op):
|
||||
""" The operation converts a sparse tensor to a dense tensor.
|
||||
For more details see https://www.tensorflow.org/api_docs/python/tf/sparse/to_dense
|
||||
|
||||
4 inputs:
|
||||
- [0, required] input indices of the sparse tensor (2D),
|
||||
- [1, required] shape of the sparse tensor. Value of this input is required for the Model Optimizer (1D),
|
||||
- [2, required] input values of the sparse tensor (1D),
|
||||
- [3, optional] default value to insert at missing positions (0D). If it is not specified, zero value is used.
|
||||
|
||||
output:
|
||||
- dense tensor (2D)
|
||||
"""
|
||||
|
||||
op = 'SparseToDense'
|
||||
enabled = True
|
||||
|
||||
def __init__(self, graph: Graph, attrs: dict):
|
||||
super().__init__(graph, {
|
||||
'type': __class__.op,
|
||||
'op': __class__.op,
|
||||
'version': 'experimental',
|
||||
'type_infer': self.type_infer,
|
||||
'infer': self.infer,
|
||||
'in_ports_count': 4,
|
||||
'out_ports_count': 1,
|
||||
}, attrs)
|
||||
|
||||
def supported_attrs(self):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def type_infer(node):
|
||||
# output data type must be the same as input values type
|
||||
values_type = node.in_port(1).get_data_type()
|
||||
node.out_port(0).set_data_type(values_type)
|
||||
|
||||
@staticmethod
|
||||
def infer(node: Node):
|
||||
assert len(node.in_nodes()) == 4 or len(node.in_nodes()) == 3, \
|
||||
"Incorrect number of inputs for {} node".format(node.id)
|
||||
|
||||
dense_shape = node.in_port(1).data.get_value()
|
||||
|
||||
# check that shape value is defined that is needed for shape inference
|
||||
assert dense_shape is not None, \
|
||||
"SparseToDense is supported only with constant shape value"
|
||||
|
||||
node.out_port(0).data.set_shape(np.array(dense_shape, dtype=np.int64))
|
||||
|
||||
input_indices = node.in_port(0).data.get_value()
|
||||
input_values = node.in_port(2).data.get_value()
|
||||
default_value = np.float32(0.0)
|
||||
if not node.in_port(3).disconnected():
|
||||
default_value = node.in_port(3).data.get_value()
|
||||
|
||||
# compute the output value if all input is constant
|
||||
if input_indices is not None and input_values is not None and default_value is not None:
|
||||
output_value = np.full(dense_shape, default_value)
|
||||
for input_index, input_value in zip(input_indices, input_values):
|
||||
output_value[tuple(input_index)] = input_value
|
||||
node.out_port(0).data.set_value(output_value)
|
||||
@@ -1,86 +0,0 @@
|
||||
"""
|
||||
Copyright (C) 2018-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
|
||||
|
||||
import numpy as np
|
||||
|
||||
from extensions.ops.sparse_to_dense import SparseToDense
|
||||
from mo.front.common.partial_infer.utils import int64_array
|
||||
from mo.graph.graph import Node
|
||||
from mo.utils.unittest.graph import build_graph
|
||||
|
||||
# graph 1
|
||||
nodes_attributes = {
|
||||
'input_indices': {'kind': 'op', 'op': 'Parameter', 'shape': int64_array([5, 2])},
|
||||
'input_indices_data': {'kind': 'data', 'shape': int64_array([5, 2]), 'value': None},
|
||||
'input_dense_shape': {'kind': 'op', 'op': 'Const', 'shape': int64_array([2])},
|
||||
'input_dense_shape_data': {'kind': 'data', 'shape': int64_array([2]), 'value': int64_array([4, 3])},
|
||||
'input_values': {'kind': 'op', 'op': 'Parameter', 'shape': int64_array([5])},
|
||||
'input_values_data': {'kind': 'data', 'shape': int64_array([5]), 'value': None},
|
||||
'input_default_value': {'kind': 'op', 'op': 'Parameter', 'shape': int64_array([])},
|
||||
'input_default_value_data': {'kind': 'data', 'shape': int64_array([]), 'value': 100},
|
||||
'sparse_to_dense_node': {'type': 'SparseToDense', 'op': 'SparseToDense', 'kind': 'op'},
|
||||
'output_data': {'kind': 'data', 'shape': None, 'value': None}
|
||||
}
|
||||
|
||||
edges = [('input_indices', 'input_indices_data'),
|
||||
('input_dense_shape', 'input_dense_shape_data'),
|
||||
('input_values', 'input_values_data'),
|
||||
('input_default_value', 'input_default_value_data'),
|
||||
('input_indices_data', 'sparse_to_dense_node', {'in': 0}),
|
||||
('input_dense_shape_data', 'sparse_to_dense_node', {'in': 1}),
|
||||
('input_values_data', 'sparse_to_dense_node', {'in': 2}),
|
||||
('input_default_value_data', 'sparse_to_dense_node', {'in': 3}),
|
||||
('sparse_to_dense_node', 'output_data', {'out': 0})]
|
||||
|
||||
inputs = {'input_indices_data': {'shape': int64_array([5, 2]), 'value': int64_array([[0, 0], [0, 1], [2, 1], [2, 2], [3, 0]])},
|
||||
'input_dense_shape_data': {'shape': int64_array([2]), 'value': int64_array([4, 3])},
|
||||
'input_values_data': {'shape': int64_array([5]), 'value': np.array([1.0, 2.0, 10.0, 4.0, 7.0])},
|
||||
'input_default_value_data': {'shape': int64_array([]), 'value': 100.0}}
|
||||
|
||||
|
||||
class TestSparseToDense(unittest.TestCase):
|
||||
def test_partial_infer1(self):
|
||||
graph = build_graph(nodes_attributes, edges)
|
||||
sparse_to_dense_node = Node(graph, 'sparse_to_dense_node')
|
||||
SparseToDense.infer(sparse_to_dense_node)
|
||||
|
||||
# prepare reference results
|
||||
ref_output_shape = np.array([4, 3], dtype=np.int32)
|
||||
|
||||
# get the result
|
||||
res_output_shape = graph.node['output_data']['shape']
|
||||
|
||||
self.assertTrue(np.array_equal(ref_output_shape, res_output_shape),
|
||||
'shapes do not match expected: {} and given: {}'.format(ref_output_shape, res_output_shape))
|
||||
|
||||
def test_infer1(self):
|
||||
graph = build_graph(nodes_attributes, edges, inputs)
|
||||
sparse_to_dense_node = Node(graph, 'sparse_to_dense_node')
|
||||
SparseToDense.infer(sparse_to_dense_node)
|
||||
|
||||
# prepare reference results
|
||||
ref_output_value = np.array([[1.0, 2.0, 100.0],
|
||||
[100.0, 100.0, 100.0],
|
||||
[100.0, 10.0, 4.0],
|
||||
[7.0, 100.0, 100.0]])
|
||||
|
||||
# get the result
|
||||
res_output_value = graph.node['output_data']['value']
|
||||
|
||||
self.assertTrue(np.array_equal(ref_output_value, res_output_value),
|
||||
'values do not match expected: {} and given: {}'.format(ref_output_value, res_output_value))
|
||||
@@ -24,8 +24,10 @@ from extensions.ops.Cast import Cast
|
||||
from extensions.ops.ReduceOps import ReduceOp
|
||||
from extensions.ops.activation_ops import Activation
|
||||
from extensions.ops.elementwise import Elementwise, LogicalElementwise, BiasAdd, Div, Mul, Pow, Sub
|
||||
from extensions.ops.embedding_bag import EmbeddingBagBase
|
||||
from extensions.ops.psroipooling import DeformablePSROIPoolingOp
|
||||
from extensions.ops.scatter import Scatter
|
||||
from extensions.ops.scatternd import ScatterNDBase
|
||||
from extensions.ops.split import Split, VariadicSplit
|
||||
from mo.front.common.partial_infer.utils import int64_array
|
||||
from mo.graph.graph import Graph, Node
|
||||
@@ -67,7 +69,8 @@ def collect_ops(path: str):
|
||||
"""
|
||||
import_by_path(os.path.join(path, 'mo', 'ops'), ['mo', 'ops'])
|
||||
import_by_path(os.path.join(path, 'extensions', 'ops'), ['extensions', 'ops'])
|
||||
update_registration(classes=[Op, Activation, Elementwise, LogicalElementwise, ReduceOp, Scatter],
|
||||
update_registration(classes=[Op, Activation, Elementwise, EmbeddingBagBase,
|
||||
LogicalElementwise, ReduceOp, Scatter, ScatterNDBase],
|
||||
enabled_transforms=[], disabled_transforms=[])
|
||||
|
||||
|
||||
|
||||
@@ -107,6 +107,7 @@ NGRAPH_OP(Reshape, ngraph::op::v1)
|
||||
NGRAPH_OP(Result, ngraph::op::v0)
|
||||
NGRAPH_OP(ReverseSequence, ngraph::op::v0)
|
||||
NGRAPH_OP(ROIPooling, ngraph::op::v0)
|
||||
NGRAPH_OP(ScatterNDUpdate, ngraph::op::v3)
|
||||
NGRAPH_OP(Select, ngraph::op::v1)
|
||||
NGRAPH_OP(Selu, ngraph::op::v0)
|
||||
NGRAPH_OP(Sign, ngraph::op::v0)
|
||||
|
||||
Reference in new issue
Block a user