publish master branch snapshot, revision 59af1853ca21ea08acf17b177da0b239753deb46
This commit is contained in:
1 parent
0a5a63bc0c
commit
ba0a339888
223 files changed
+4329
-1326
No files matched your search
@@ -0,0 +1,84 @@
|
||||
"""
|
||||
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 extensions.ops.split import VariadicSplit
|
||||
from mo.back.replacement import BackReplacementPattern
|
||||
from mo.front.common.partial_infer.utils import int64_array
|
||||
from mo.front.tf.graph_utils import create_op_node_with_second_input, create_op_with_const_inputs
|
||||
from mo.graph.graph import Graph
|
||||
from mo.ops.const import Const
|
||||
from mo.ops.reshape import Reshape
|
||||
|
||||
|
||||
class CellNormalizer(BackReplacementPattern):
|
||||
# This class splits WR input on W and R for LSTMCell, GRUCell, RNNCell
|
||||
|
||||
enabled = True
|
||||
force_clean_up = True
|
||||
|
||||
graph_condition = [lambda graph: graph.graph['cmd_params'].generate_experimental_IR_V10]
|
||||
|
||||
def pattern(self):
|
||||
return dict(
|
||||
nodes=[
|
||||
('cell', dict(type=lambda type: type in ['LSTMCell', 'GRUCell', 'RNNCell']))
|
||||
],
|
||||
edges=[]
|
||||
)
|
||||
|
||||
def replace_pattern(self, graph: Graph, match: dict):
|
||||
node = match['cell']
|
||||
cell_name = node.soft_get('name', node.id)
|
||||
cell_type = node.soft_get('type')
|
||||
WR_input_id = node.soft_get('wr_input_id')
|
||||
hidden_size_coef = node.soft_get('gates_count')
|
||||
hidden_size = node.get_attrs()["hidden_size"]
|
||||
|
||||
# default values for RNNCell/GRUCell
|
||||
additional_port_id = 4
|
||||
if cell_type == "LSTMCell":
|
||||
additional_port_id = 5
|
||||
|
||||
WR_shape = node.in_port(WR_input_id).data.get_shape()
|
||||
assert WR_shape is not None, "Undefined 'WR' input shape for Cell node '{}'".format(cell_name)
|
||||
|
||||
num_elements_in_WR = np.prod(WR_shape)
|
||||
input_size = (num_elements_in_WR / (hidden_size_coef * hidden_size)) - hidden_size
|
||||
|
||||
# Reshape
|
||||
reshape = create_op_node_with_second_input(graph, Reshape,
|
||||
int64_array([hidden_size_coef * hidden_size,
|
||||
hidden_size + input_size]),
|
||||
{'name': cell_name + '/Dims'})
|
||||
|
||||
# VariadicSplit
|
||||
split = create_op_with_const_inputs(graph, VariadicSplit, {1: int64_array(1),
|
||||
2: int64_array([input_size, hidden_size])},
|
||||
{'out_ports_count': 2, 'name': cell_name + '/Split'},
|
||||
reshape)
|
||||
|
||||
# Cell
|
||||
node.in_port(WR_input_id).get_connection().set_destination(reshape.in_port(0))
|
||||
|
||||
node.add_input_port(additional_port_id, skip_if_exist=True)
|
||||
assert node.in_port(additional_port_id).disconnected()
|
||||
|
||||
# (x, y, WR, B) -> (x, y, W, R, B(additional_port))
|
||||
node.in_port(additional_port_id - 1).get_connection().set_destination(node.in_port(additional_port_id))
|
||||
split.out_port(0).connect(node.in_port(additional_port_id - 2))
|
||||
split.out_port(1).connect(node.in_port(additional_port_id - 1))
|
||||
@@ -1,74 +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 extensions.ops.split import VariadicSplit
|
||||
from mo.back.replacement import BackReplacementPattern
|
||||
from mo.front.common.partial_infer.utils import int64_array
|
||||
from mo.front.tf.graph_utils import create_op_node_with_second_input
|
||||
from mo.graph.graph import Graph
|
||||
from mo.ops.const import Const
|
||||
from mo.ops.reshape import Reshape
|
||||
|
||||
|
||||
class LSTMCellNormalizer(BackReplacementPattern):
|
||||
enabled = True
|
||||
force_clean_up = True
|
||||
|
||||
graph_condition = [lambda graph: graph.graph['cmd_params'].generate_experimental_IR_V10]
|
||||
|
||||
def pattern(self):
|
||||
return dict(
|
||||
nodes=[
|
||||
('lstm_cell', {'type': 'LSTMCell'})
|
||||
],
|
||||
edges=[]
|
||||
)
|
||||
|
||||
def replace_pattern(self, graph: Graph, match: dict):
|
||||
node = match['lstm_cell']
|
||||
lstm_cell_name = node.soft_get('name', node.id)
|
||||
hidden_size = node.get_attrs()["hidden_size"]
|
||||
|
||||
WR_shape = node.in_port(3).data.get_shape()
|
||||
assert WR_shape is not None, "Undefined 'WR' input shape for LSTM Cell node '{}'".format(lstm_cell_name)
|
||||
|
||||
num_elements_in_WR = np.prod(WR_shape)
|
||||
input_size = (num_elements_in_WR / (4 * hidden_size)) - hidden_size
|
||||
|
||||
# Reshape
|
||||
reshape = create_op_node_with_second_input(graph, Reshape,
|
||||
int64_array([4 * hidden_size, hidden_size + input_size]),
|
||||
{'name': lstm_cell_name + '/Dims'})
|
||||
|
||||
# VariadicSplit
|
||||
const_axis = Const(graph, {'value': 1}).create_node()
|
||||
const_size_splits = Const(graph, {'value': int64_array([input_size, hidden_size])}).create_node()
|
||||
split = VariadicSplit(graph, {'name': lstm_cell_name + '/Split', 'out_ports_count': 2}).create_node()
|
||||
const_axis.out_port(0).connect(split.in_port(1))
|
||||
const_size_splits.out_port(0).connect(split.in_port(2))
|
||||
|
||||
# LSTM Cell
|
||||
node.in_port(3).get_connection().set_destination(reshape.in_port(0))
|
||||
reshape.out_port(0).connect(split.in_port(0))
|
||||
|
||||
node.add_input_port(5, skip_if_exist=True)
|
||||
assert node.in_port(5).disconnected()
|
||||
node.in_port(4).get_connection().set_destination(node.in_port(5))
|
||||
|
||||
split.out_port(0).connect(node.in_port(3))
|
||||
split.out_port(1).connect(node.in_port(4))
|
||||
@@ -63,8 +63,10 @@ class BlobNormalizer(BackReplacementPattern):
|
||||
def find_and_replace_pattern(self, graph: Graph):
|
||||
if graph.graph['cmd_params'].generate_experimental_IR_V10:
|
||||
for node in graph.get_op_nodes():
|
||||
if node.soft_get('type').lower() not in OpVersioning.opset_1_types:
|
||||
if node.soft_get('type').lower() not in OpVersioning.opset_1_types and \
|
||||
not node.soft_get('version') in ["opset2", "opset3"]:
|
||||
continue
|
||||
|
||||
for _, d in node.in_edges().items():
|
||||
if 'bin' in d:
|
||||
del d['bin']
|
||||
|
||||
@@ -137,8 +137,6 @@ class OpVersioning(BackReplacementPattern):
|
||||
]))
|
||||
|
||||
opset_1_experimental_ops = set(map(lambda s: s.lower(), [
|
||||
"RNNCell",
|
||||
"GRUCell",
|
||||
"SimplerNMS",
|
||||
"SpatialTransformer",
|
||||
"ExperimentalDetectronGenerateProposalsSingleImage",
|
||||
|
||||
Reference in new issue
Block a user