def extract(cls, node): data_type = tf_dtype_extractor(node.pb.attr["T"].type) AttributedPower.update_node_stat(node, { 'power': data_type(2), 'data_type': data_type }) return cls.enabled
def extract(cls, node: Node): Shape.update_node_stat( node, { 'data_type': tf_dtype_extractor(node.pb.attr['out_type'].type, np.int32) }) return cls.enabled
def generate_feed_dict(graph: tf_v1.Graph, node: Node): """ The first value in the return tuple is True if all inputs for the node has constant values. The second returned value is mapping of placeholder tensor to the numpy arrays with the values for these placeholders. :param graph: the TensorFlow Graph to generate feed dictionary to. :param node: the node which represents TensorFlow sub-graph of operations. :return: pair where the first element is a flag that specifies that all node inputs are constants and a dictionary where key is the input Tensor object and the value is the tensor value. """ all_constants = True feed_dict = dict() for in_data_node_name, edge_attrs in node.get_inputs(): if 'control_flow_edge' in edge_attrs and edge_attrs[ 'control_flow_edge']: continue value = node.in_node(edge_attrs['in']).value if value is None: all_constants = False placeholder_pb = node['pbs'][edge_attrs['placeholder_name']] value = np.ones( shape=tf_tensor_shape(placeholder_pb.attr['shape'].shape), dtype=tf_dtype_extractor(placeholder_pb.attr['dtype'].type)) feed_dict[graph.get_tensor_by_name(edge_attrs['placeholder_name'] + ":0")] = value return all_constants, feed_dict
def extract(cls, node): BiasAdd.update_node_stat( node, { 'data_type': tf_dtype_extractor(node.pb.attr["T"].type), 'data_format': node.pb.attr["data_format"].s.decode() }) return cls.enabled
def extract(cls, node): OneHot.update_node_stat( node, { 'axis': node.pb.attr['axis'].i, 'data_type': tf_dtype_extractor(node.pb.attr["T"].type, np.float32) }) return cls.enabled
def extract(cls, node: Node): dtypes = [tf_dtype_extractor(t) for t in node.pb.attr["T"].list.type] IdentityN.update_node_stat(node, { 'data_types': dtypes, 'in_ports_count': len(dtypes), 'out_ports_count': len(dtypes), }) return cls.enabled
def tf_placeholder_ext(pb): return { 'data_type': tf_dtype_extractor(pb.attr["dtype"].type), 'shape': tf_tensor_shape(pb.attr["shape"].shape), 'type': 'Input', 'infer': lambda node: single_output_infer(node, lambda n: n.shape), 'permute_attrs': PermuteAttrs().update_attrs(attrs=[('shape', 'output:0')]) }
def extract(node): attrs = { 'data_type': tf_dtype_extractor(node.pb.attr["dtype"].type), 'shape': tf_tensor_shape(node.pb.attr["shape"].shape), 'permute_attrs': PermuteAttrs().update_attrs(attrs=[('shape', 'output:0')]) } Parameter.update_node_stat(node, attrs) return __class__.enabled
def extract(cls, node): attrs = { 'output_type': tf_dtype_extractor(node.pb.attr["dtype"].type), 'global_seed': node.pb.attr['seed'].i, 'op_seed': node.pb.attr['seed2'].i } AttributedRandomUniform.update_node_stat(node, attrs) return cls.enabled
def extract(node): attrs = { 'data_type': tf_dtype_extractor(node.pb.attr["dtype"].type), 'shape': tf_tensor_shape(node.pb.attr["shape"].shape), 'identity': True, } Op.update_node_stat(node, attrs) return __class__.enabled
def extract(cls, node): attrs = { 'data_type': tf_dtype_extractor(node.pb.attr["dtype"].type), 'shape': tf_tensor_shape(node.pb.attr["shape"].shape), 'identity': True, 'infer': lambda node: copy_shape_infer(node, value_infer=copy_value), } Op.update_node_stat(node, attrs) return cls.enabled
def extract(cls, node): pb_tensor = node.pb.attr["value"].tensor shape = tf_tensor_shape(pb_tensor.tensor_shape) attrs = { 'shape': shape, 'value': tf_tensor_content(pb_tensor.dtype, shape, pb_tensor), 'data_type': tf_dtype_extractor(pb_tensor.dtype), } Const.update_node_stat(node, attrs) return cls.enabled
def extract(cls, node): shapes = node.pb.attr['output_shapes'].list.shape tf_types = node.pb.attr['output_types'].list.type extracted_types = [] for t in tf_types: extracted_types.append(tf_dtype_extractor(t)) result_shapes = [] for shape_pb in shapes: result_shapes.append(tf_tensor_shape(shape_pb)) Op.update_node_stat(node, {'shapes': result_shapes, 'types': extracted_types, 'out_ports_count': 1}) return cls.enabled
def tf_const_ext(pb): pb_tensor = pb.attr["value"].tensor result = { 'data_type': tf_dtype_extractor(pb_tensor.dtype), 'shape': tf_tensor_shape(pb_tensor.tensor_shape), 'infer': tf_const_infer } result['value'] = tf_tensor_content(pb_tensor.dtype, result['shape'], pb_tensor) log.debug('Constant extractor for node gives shape = {} and value.shape = {}'.format(result['shape'], result['value'].shape)) return result
def extract(cls, node): shapes = node.pb.attr['output_shapes'].list.shape tf_types = node.pb.attr['output_types'].list.type extracted_types = [] for t in tf_types: extracted_types.append(tf_dtype_extractor(t)) result_shapes = [] for shape_pb in shapes: shape = shape_pb.dim result_shapes.append(int64_array([dim.size for dim in shape])) Op.update_node_stat(node, {'shapes': result_shapes, 'types': extracted_types}) return cls.enabled
def tf_eltwise_ext(pb, op=None, attrs=None): """ Generic eltwise extractor that supports n-ary operations. It supports reasonable broadcast semantics from TF/NumPy """ res = { 'data_type': tf_dtype_extractor(pb.attr["T"].type), 'infer': lambda node: eltwise_infer(node, op) } if attrs is not None: res.update(attrs) return res
def tf_fused_bn_extractor(pb): is_training = pb.attr['is_training'].b if is_training: log.warning('FusedBatchNorm doesn\'t support is_training=True') return { 'data_format': pb.attr["data_format"].s, 'data_type': tf_dtype_extractor(pb.attr["T"].type), 'eps': pb.attr['epsilon'].f, 'infer': tf_fused_bn_infer, 'is_training': is_training }
def determine_data_type(node: Node): """ Tries to determine data type of the node. The input node could be either data or op node. If we don't know the data type of the node then we recursively check the first parent of the node. :param node: node to determine data type. :return: data type of the node output in the numpy format. """ if node.has_and_set('data_type'): return node.data_type if node.has_and_set('kind') and node.kind == 'op': if node.has_and_set('pb'): if 'dtype' in node.pb.attr: return tf_dtype_extractor(node.pb.attr['dtype'].type) if 'T' in node.pb.attr: return tf_dtype_extractor(node.pb.attr['T'].type) if node.has_and_set('kind') and node.kind == 'data': if 'value' in node and node.value is not None: return node.value.dtype if len(node.in_nodes()) != 0: # try to guess data type from the first parent return determine_data_type(node.in_node(0)) log.error('Failed to determine data type for node "{}"'.format(node.name)) return None
def replace_sub_graph(self, graph: Graph, match: dict): node = match['op'] if not node.has_valid('value'): log.debug("No value in FakeConst node {}".format(node.id)) return node_value = node.value extracted_attrs = { 'data_type': tf_dtype_extractor(node.pb.attr['dtype'].type), 'shape': int64_array(node_value.shape), 'value': node_value } Const.update_node_stat(node, extracted_attrs) log.debug('FakeConst op was translated to Const op with shape = {} and value.shape = {}' ''.format(extracted_attrs['shape'], extracted_attrs['value'].shape))
def extract(cls, node): attrs = { 'top_k': 1, 'axis': None, 'keepdims': 0, 'remove_values_output': True, 'output_type': tf_dtype_extractor(node.pb.attr['output_type'].type, np.int64) } ArgMinOp.update_node_stat(node, attrs) return cls.enabled
def infer(node: Node): start = node.in_node(0) limit = node.in_node(1) delta = node.in_node(2) output = node.out_node() if not start.has_valid('value') or not limit.has_valid('value') or not delta.has_valid('value'): log.error("Range operation is supported with constant inputs only") return if 'type' in node.pb.attr: from mo.front.tf.extractors.utils import tf_dtype_extractor result_data_type = tf_dtype_extractor(node.pb.attr["type"].type) else: result_data_type = start.value.dtype output.value = np.arange(start.value, limit.value, delta.value, dtype=result_data_type) output.shape = np.array(output.value.shape, dtype=np.int64)
def extract(cls, node): ArgMaxOp.update_node_stat( node, { 'out_max_val': 0, 'top_k': 1, 'axis': None, 'dim_attrs': ['axis'], 'keepdims': 0, 'remove_values_output': True, 'output_type': tf_dtype_extractor(node.pb.attr['out_type'].type, np.int64), }) return cls.enabled
def extract(node): shapes = node.pb.attr['shapes'].list.shape tf_types = node.pb.attr['component_types'].list.type extracted_types = [] for t in tf_types: extracted_types.append(tf_dtype_extractor(t)) result_shapes = [] for shape_pb in shapes: shape = shape_pb.dim if len(shape) == 3: result_shapes.append( np.array([1, shape[0].size, shape[1].size, shape[2].size], dtype=np.int64)) else: result_shapes.append(np.array(shape, dtype=np.int64)) Op.update_node_stat(node, { 'shapes': result_shapes, 'types': extracted_types }) return __class__.enabled
def convert_graph_inputs_to_parameters(internal_graph, internal_graph_proto): # create Parameter nodes for the body graph body_parameters = [] body_parameter_names = [] for idx, pb_node in enumerate(internal_graph_proto['input_arg']): param_id = internal_graph.unique_id(pb_node.name) internal_graph.add_node(param_id, name=param_id, kind='op', op='Parameter', pb=None, shape=None) parameter_node = Node(internal_graph, pb_node.name) Parameter.update_node_stat( parameter_node, { 'data_type': tf_dtype_extractor(pb_node.type), 'permute_attrs': PermuteAttrs().update_attrs(attrs=[('shape', 'output:0')]) }) body_parameters.append(parameter_node) body_parameter_names.append(param_id) return body_parameters, body_parameter_names
def extract(cls, node): Div.update_node_stat( node, {'data_type': tf_dtype_extractor(node.pb.attr["T"].type)}) return cls.enabled
def extract(cls, node: Node): Range.update_node_stat( node, {'output_type': tf_dtype_extractor(node.pb.attr['Tidx'].type)}) return cls.enabled
def extract(node): Add.update_node_stat( node, {'data_type': tf_dtype_extractor(node.pb.attr["T"].type)}) return __class__.enabled
def extract(cls, loop_node): Loop.update_node_stat(loop_node, {}) loop_name = loop_node.soft_get('name', loop_node.id) # check that required body and condition functions exist in the graph library main_graph = loop_node.graph body_graph_name = loop_node.pb.attr['body'].func.name cond_graph_name = loop_node.pb.attr['cond'].func.name assert 'library' in main_graph.graph, 'The graph does not contain a library that is required ' \ 'by node with name "{}".'.format(loop_name) library_graph = main_graph.graph['library'] assert body_graph_name in library_graph, 'The library does not contain a function with name "{}" ' \ 'that is required by node ' \ 'with name "{}".'.format(body_graph_name, loop_name) body_graph_proto = library_graph[body_graph_name] assert cond_graph_name in library_graph, 'The library does not contain a function with name "{}" ' \ 'that is required by node ' \ 'with name "{}".'.format(cond_graph_name, loop_name) cond_graph_proto = library_graph[cond_graph_name] body_graph = Graph() # fill the body graph for attr_key in main_graph.graph.keys(): if attr_key != 'library': body_graph.graph[attr_key] = copy.deepcopy(main_graph.graph[attr_key]) else: # it is sufficient to have a link to the library body_graph.graph['library'] = main_graph.graph['library'] loop_node['body'] = body_graph # create Parameter nodes for the body graph body_parameters = [] body_parameter_names = [] for idx, pb_node in enumerate(body_graph_proto['input_arg']): param_id = body_graph.unique_id(pb_node.name) body_graph.add_node(param_id, name=param_id, kind='op', op='Parameter', pb=None, shape=None) parameter_node = Node(body_graph, pb_node.name) Parameter.update_node_stat(parameter_node, {'data_type': tf_dtype_extractor(pb_node.type), 'permute_attrs': PermuteAttrs().update_attrs(attrs=[('shape', 'output:0')])} ) body_parameters.append(parameter_node) body_parameter_names.append(param_id) # update the loop body graph with the body function graph body_results = [] update_body_graph(body_graph, body_graph_proto, body_parameter_names, body_results) # update the loop body graph with the condition function graph update_body_graph(body_graph, cond_graph_proto, body_parameter_names, body_results) # add 'internal_layer_id' attribute which is a must have attribute for the loop body node for idx, body_node in enumerate(body_graph.get_op_nodes()): body_node['internal_layer_id'] = idx body_graph.stage = 'front' # Currently, # Loop Inputs Order: # 0 - current iteration # 1 - trip count # 2.. - "loop carried" dependencies variables # # Body Inputs Order: # 0 - current iteration # 1 - trip count # 2.. - "loop carried" dependencies variables # # Body Outputs Order: # 0 - current iteration # 1 - trip count # 2.. - "loop carried" dependencies variables # # Loop Outputs Order: # 0 - current iteration # 1 - trip count # 2.. - "loop carried" dependencies variables # # so inputs must be reordered and execution condition must be created in the front transformation # to be aligned with the specification # connect external input ports with body parameter nodes except current iteration # since it must be disconnected from external port for idx in range(1, len(body_parameters)): Loop.connect_body_input(loop_node, idx, body_parameters[idx]) # mark current iteration input Parameter node and execution condition Result node Loop.mark_current_iteration_parameter_node(loop_node, body_parameters[0]) Loop.mark_execution_condition_result_node(loop_node, body_results[-1]) # connect back edges in the body except current iteration for idx in range(1, len(body_parameters)): Loop.add_back_edge(loop_node, body_parameters[idx], body_results[idx]) # connect body outputs with Loop operation output ports except the execution condition result for idx in range(len(body_results)-1): Loop.connect_body_output(loop_node, idx, body_results[idx]) # run function to parse body nodes attributes similar to the main graph extract_node_attrs(body_graph, lambda node: tf_op_extractor(node, check_for_duplicates(tf_op_extractors))) return cls.enabled
def tf_shape_ext(pb): return { 'infer': Shape.infer, 'data_type': tf_dtype_extractor(pb.attr['out_type'].type, np.int32) }
def extract(cls, node: Node): IdentityOp.update_node_stat( node, { 'data_type': tf_dtype_extractor(node.pb.attr["T"].type), }) return cls.enabled