Files
openvino/model-optimizer/extensions/ops/gatherelements.py
T
Pavel Esir d2a23680f2 nGraph 'shell' implementation for GatherElements-6 and MO 'shell' implementation (#3467)
* Initial support of GatherElements in MO and nGraph

* apply_style

* added lost extractor for GatherElements

* Corrected GatherElements::validate_and_infer_types

* updated package_BOM.txt

* Type_t added

* started to implement ngraph shape_type_infer unit-tests

* finally implemented all ngraph shape_inference unit-tests

* updated Supported_Frameworks_Layers.md

* added correct handling of dynamic shapes in nGraph, added unit-tests for dynamic cases, fixed dump typos in MO, replaced axis type from int -> int64_t

* implemented shape infer for dynamic shapes with intervals

* finalized MO implementation

* applied comment from review

* style-apply

* spec correction

* removed conflict

* fixed typos

* removed obsolete comments form type_prop

* significant corrections in validate_and_infer_types

* style-apply

* data_rank check for axis
2020-12-21 15:45:15 +03:00

76 lines
2.9 KiB
Python

"""
Copyright (C) 2017-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, PermuteAttrs
class GatherElements(Op):
op = 'GatherElements'
def __init__(self, graph: Graph, attrs: dict):
super().__init__(graph, {
'op': self.op,
'type': self.op,
'version': 'opset6',
'infer': self.infer,
'in_ports_count': 2,
'out_ports_count': 1,
'axis': 0,
}, attrs)
def backend_attrs(self):
return ['axis']
@staticmethod
def infer(node: Node):
data_shape = node.in_port(0).data.get_shape()
indices_shape = node.in_port(1).data.get_shape()
axis = node.axis
data_rank = len(data_shape)
assert data_rank >= 1, 'data_rank must be >= 1'
assert data_rank == len(indices_shape), 'data and indices inputs for node {} must be of the ' \
'same rank. Instead got {} and {}'.\
format(node.name, data_rank, len(indices_shape))
assert -data_rank <= axis < data_rank, 'axis for node {0} must be within interval ' \
'[-{1}}, {1} - 1]. Instead got: axis={2}'.\
format(node.name, data_rank, axis)
if axis < 0:
axis += data_rank
for idx, (data_sz, ind_sz) in enumerate(zip(data_shape, indices_shape)):
if idx != axis and data_sz != ind_sz:
raise ValueError('Sizes along axis {} for node {} do not match. data and indices must have '
'equal size along all axes except for axis {}'.format(idx, node.name, axis))
data = node.in_port(0).data.get_value()
indices = node.in_port(1).data.get_value()
if data is not None and indices is not None:
out_value = np.empty(indices_shape, dtype=data.dtype)
for idx in np.ndindex(*indices_shape):
data_idx = list(idx)
data_idx[node.axis] = indices[idx]
out_value[idx] = data[tuple(data_idx)]
node.out_port(0).data.set_value(out_value)
else:
node.out_port(0).data.set_shape(indices_shape)
PermuteAttrs.create_permute_attrs(node, attrs=[('axis', 'input:0')])