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.
21 from mo.front.common.replacement import FrontReplacementSubgraph
22 from mo.graph.graph import Graph
23 from mo.middle.passes.convert_data_type import SUPPORTED_DATA_TYPES
24 from mo.ops.const import Const
25 from mo.utils.error import Error
28 class FreezePlaceholderValue(FrontReplacementSubgraph):
30 Replaces existing placeholder to Constant node with provided value. It takes value from freeze_placeholder as
31 a string and casts it to actual node data type
34 graph_condition = [lambda graph: graph.graph['freeze_placeholder'] is not None]
37 from extensions.front.restore_ports import RestorePorts
41 from extensions.front.pass_separator import FrontStart
47 nodes=[('placeholder', dict(kind='op', op='Placeholder'))],
51 def replace_sub_graph(self, graph: Graph, match: dict):
52 ph = match['placeholder']
53 if ph.name in graph.graph['freeze_placeholder']:
55 if ph.has_and_set('data_type'):
56 data_type = ph.data_type
58 data_type = SUPPORTED_DATA_TYPES[graph.graph['cmd_params'].data_type][0]
59 string_value = graph.graph['freeze_placeholder'][name]
61 if data_type != np.bool:
62 value = np.array(string_value, dtype=data_type)
63 elif data_type == np.bool and graph.graph['fw'] == 'tf':
64 from mo.front.tf.common import tf_data_type_cast
65 if isinstance(string_value, list):
67 for v in np.array(string_value):
68 casted_list.append(tf_data_type_cast[ph.data_type](v))
69 value = np.array(string_value, dtype=data_type)
71 value = tf_data_type_cast[ph.data_type](string_value)
73 raise Error("Can not cast value {} to {} data_type".format(string_value, data_type))
75 raise Error("Can not cast value {} to {} data_type".format(string_value, data_type))
77 value = np.reshape(a=value, newshape=ph.shape)
79 raise Error("Can not reshape value {} to shape {}".format(value, ph.shape))
80 out_edges = list(graph.out_edges(ph.id, data=True))
81 new_node = Const(graph).create_node(
82 attrs={'value': value, 'data_type': type(value), 'name': name + '/const_placeholder',
85 graph.add_edges_from([(new_node.id, v, attrs) for u, v, attrs in out_edges])
86 log.info("Placeholder node \"{}\" was replaced with Const node \"{}\" with value \"{}\"".format(
87 name, new_node.name, value))