2 Copyright (c) 2018-2019 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 mo.front.common.replacement import FrontReplacementSubgraph
21 from mo.graph.graph import Graph, Node
22 from mo.ops.input import Input
25 class FIFOQueue(FrontReplacementSubgraph):
29 from extensions.front.override_batch import OverrideBatch
30 return [OverrideBatch]
33 def pattern(**kwargs):
36 ('placeholder', dict(op='Placeholder', data_type=np.int32)),
37 ('fifo_queue', dict(op='FIFOQueueV2')),
38 ('batch_join', dict(op='QueueDequeueUpToV2')),
39 ('image_batch', dict(op='Identity', data_type=np.float32))
42 ('placeholder', 'batch_join', {'out': 0}),
43 ('fifo_queue', 'batch_join', {'out': 0}),
44 ('batch_join', 'image_batch', {'out': 0})
49 def replace_sub_graph(graph: Graph, match: dict, **kwargs):
51 Usually graph looks like:
56 image_batch label_batch
60 placeholder fifo_queue
62 Replacer works for both cases (that's why we have loop - 68 line):
63 label_batch was marked as output
64 there is no label_batch node
66 true_placeholder_shape = match['placeholder'].shape
67 placeholder_shape = match['fifo_queue'].shapes[0]
68 assert true_placeholder_shape.ndim <= 1
69 if true_placeholder_shape.ndim == 1 and len(true_placeholder_shape) > 1:
71 'Placeholder \'{}\' got non 0-dimensional shape {} in FIFOQueue pattern. Placeholder will have the '
72 'same shape after folding the pattern instead of {} shape which is original for the network.'
73 ''.format(match['placeholder'].id, true_placeholder_shape, placeholder_shape))
74 placeholder_shape = true_placeholder_shape
75 placeholder_name = match['fifo_queue'].name
76 graph.erase_node(match['fifo_queue'])
77 graph.erase_node(match['placeholder'])
78 for _, out in match['batch_join'].out_nodes().items():
79 if out.id != match['image_batch'].id:
80 if out.out_node().op == 'OpOutput':
81 graph.remove_node(out.out_node().id)
82 graph.remove_node(out.id)
83 graph.remove_node(match['batch_join'].id)
84 placeholder = Input(graph, {'name': placeholder_name, 'shape': placeholder_shape}).create_node()
85 graph.create_edge(placeholder, match['image_batch'])
86 log.info("FIFOQueueV2 pattern was detected. New shape of placeholder {} is {}. Use -b to set batch size if "
87 "needed".format(placeholder.id, placeholder['shape']))
90 class QueueDequeueManyV2(FrontReplacementSubgraph):
92 Replaces the combination of the FIFOQueueV2 + QueueDequeueManyV2 operations with a number of Placeholders.
97 from extensions.front.override_batch import OverrideBatch
98 return [OverrideBatch]
101 def pattern(**kwargs):
104 ('fifo_queue', dict(op='FIFOQueueV2')),
105 ('queue_deque', dict(op='QueueDequeueManyV2')),
108 ('fifo_queue', 'queue_deque', {'out': 0}),
113 def replace_sub_graph(graph: Graph, match: dict, **kwargs):
115 for u, v, edge_attrs in graph.out_edges(match['queue_deque'].id, data=True):
116 out_port = edge_attrs['out']
117 shape = match['fifo_queue'].shapes[out_port]
118 if out_port not in inputs_dict:
119 input_op = Input(graph, {'shape': shape.copy()})
120 inputs_dict[out_port] = input_op.create_node([])
121 graph.create_edge(inputs_dict[out_port], Node(graph, v), edge_attrs['out'], edge_attrs['in'], edge_attrs)
123 graph.remove_node(match['queue_deque'].id)
124 graph.remove_node(match['fifo_queue'].id)