2 Copyright (c) 2018 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
20 from tensorflow.core.framework import types_pb2 as tf_types # pylint: disable=no-name-in-module
22 from mo.graph.graph import Node
23 from mo.middle.passes.fusing.helpers import get_next_operation
24 from mo.utils.error import Error
25 from mo.utils.utils import refer_to_faq_msg
28 def change_placeholders_types_to_FP32(graph: nx.MultiDiGraph):
29 for node_name, node_attrs in list(graph.nodes(data=True)):
30 node = Node(graph, node_name)
31 pb = node_attrs.get('pb')
32 if pb is not None and pb.op == 'Placeholder' and pb.attr['dtype'].type != tf_types.DT_FLOAT:
33 log.info('Placeholder "{}" has type that is different from DT_FLOAT'.format(node_name))
34 next_ops = get_next_operation(node)
35 # check that all output nodes are nodes of type 'ToFloat'
36 if all([is_node_casts_to_float(op) and len(op.in_nodes()) == 1 for op in next_ops]):
37 change_node_type(node, tf_types.DT_FLOAT)
38 remove_node_preserving_edges(node, next_ops) # remove 'Cast' nodes
39 elif all([is_node_gather(op) for op in next_ops] for op in next_ops):
40 change_node_type(node, tf_types.DT_FLOAT)
43 ('Cannot convert type of placeholder "{}" because not all of its outputs are "Cast" to float '
45 refer_to_faq_msg(49)),
46 node.soft_get('name'),
47 [op.soft_get('name') for op in next_ops]
52 def is_node_casts_to_float(node: Node):
53 attrs = node.graph.node[node.id]
54 return 'pb' in attrs and attrs['pb'].op == 'Cast' and attrs['pb'].attr['DstT'].type == tf_types.DT_FLOAT
57 def is_node_gather(node: Node):
58 attrs = node.graph.node[node.id]
59 return 'pb' in attrs and attrs['pb'].op == 'GatherV2' and attrs['precision'] == 'FP32'
62 def change_node_type(node: Node, new_type: type):
63 node.graph.node[node.id]['pb'].attr['dtype'].type = new_type
66 def remove_node_preserving_edges(pl_node: Node, nodes: list):
68 pl_node_data = pl_node.out_node()
70 # Disconnect Placeholder data node from Cast nodes
71 for out_node in pl_node.out_node().out_nodes():
72 graph.remove_edge(pl_node_data.id, out_node.id)
74 # Move edges from Cast data nodes to Placeholder data node
75 for cast_node in nodes:
76 # it is necessary to create a list from the result of function "graph.out_edges()" because we modify the graph
77 # during iteration over the list. networkx version 2.1 raises error without creating a list
78 for u, v, d in list(graph.out_edges(cast_node.out_node().id, data=True)):
79 graph.remove_edge(u, v)
80 graph.add_edges_from([(pl_node_data.id, v, d)])