def test_case2(self): graph = build_graph( nodes, [('Parameter1', 'Op1'), ('Op1', 'FakeOutput1', { 'in': 1, 'out': 1, 'fw_tensor_debug_info': [('Op1', 'Op1_tensor_name')] }), ('Parameter1', 'Op2'), ('Op2', 'FakeOutput2', { 'in': 2, 'out': 3, 'fw_tensor_debug_info': [('Op2', 'Op2_tensor_name')] })]) graph.graph['packed_outputs'] = None graph.graph['user_shapes'] = None graph.stage = 'front' OutputCut().find_and_replace_pattern(graph) op1 = Node(graph, 'Op1') op2 = Node(graph, 'Op2') self.assertTrue(op1.out_node(1)['type'] == 'Result') self.assertTrue(op2.out_node(3)['type'] == 'Result') self.assertTrue( op1.out_edge(1)['fw_tensor_debug_info'] == [('Op1', 'Op1_tensor_name')]) self.assertTrue( op2.out_edge(3)['fw_tensor_debug_info'] == [('Op2', 'Op2_tensor_name')]) self.assertTrue(graph.get_op_nodes(name='FakeOutput1') == []) self.assertTrue(graph.get_op_nodes(name='FakeOutput2') == [])
def find_and_replace_pattern(self, graph: Graph): graph.stage = 'front' for node_id in graph.nodes(data=False): node = Node(graph, node_id) inputs = node.get_sorted_inputs() outputs = node.get_sorted_outputs() in_ports_count = node.in_ports_count if node.has_valid( 'in_ports_count') else len(inputs) out_ports_count = node.out_ports_count if node.has_valid( 'out_ports_count') else len(outputs) if len(outputs) > out_ports_count > 1: raise Error("Node {} has more children than it should: " + "should be {} but there is {}".format( node_id, out_ports_count, len(outputs))) node['_in_ports'] = {} node['_out_ports'] = {} if in_ports_count is not None: for idx in range(in_ports_count): node.add_input_port(idx=idx) if out_ports_count is not None: for idx in range(out_ports_count): node.add_output_port(idx=idx) idx = 0 for in_node_id, edge_attrs in inputs: graph.remove_edge(in_node_id, node_id) if len(Node(graph, in_node_id).out_ports()) == 0: Node(graph, in_node_id).add_output_port(0) in_node = Node(graph, in_node_id) in_node.out_port(edge_attrs['out']).connect(node.in_port(idx)) # need to keep this attribute in edge for correct .mapping file generation and # for generation of "names" field in IR in_node.out_edge( edge_attrs['out'] )['fw_tensor_debug_info'] = edge_attrs['fw_tensor_debug_info'] if idx < in_ports_count - 1: idx = idx + 1 idx = 0 for out_node_id, edge_attrs in outputs: graph.remove_edge(node_id, out_node_id) if len(Node(graph, out_node_id).in_ports()) == 0: Node(graph, out_node_id).add_input_port(0) node.out_port(idx).connect( Node(graph, out_node_id).in_port(edge_attrs['in'])) # need to keep this attribute in edge for correct .mapping file generation and # for generation of "names" field in IR node.out_edge(idx)['fw_tensor_debug_info'] = edge_attrs[ 'fw_tensor_debug_info'] if idx < out_ports_count - 1: idx = idx + 1
def test_create_node(self): graph = build_graph(nodes, [('Op1', 'Op3', {'in': 0, 'out': 0, 'fw_tensor_debug_info': [('Op1', 'Op1')]}), ('Op2', 'Op3', {'in': 1, 'out': 0, 'fw_tensor_debug_info': [('Op2', 'Op2')]})]) graph.stage = 'front' input1 = Node(graph, 'Op1') input2 = Node(graph, 'Op2') inputs = [(input1, 0), (input2, 0)] lstm_op = LSTMCell(graph, dict(name='LSTMCell')) _ = lstm_op.create_node(inputs) self.assertTrue(input1.out_edge(0)['fw_tensor_debug_info'] == [('Op1', 'Op1')]) self.assertTrue(input2.out_edge(0)['fw_tensor_debug_info'] == [('Op2', 'Op2')])
def create_graph(): sub_graph_2 = build_graph(nodes_attrs=ti_sub_graph_2_nodes, edges=[ *connect('cond_2_int', 'cond_2_int_out'), *connect('in_2_int', 'OUT_2'), *connect('ones', 'OUT_2'), *connect('OUT_2', 'OUT_2_out'), *connect('in_2_int', 'in_2_int_out') ], nodes_with_edges_only=True) sub_graph_1 = build_graph(nodes_attrs=ti_sub_graph_1_nodes, edges=[ *connect('cond_2', '1:Loop_2'), *connect('IN_2', '0:Loop_2'), *connect('Loop_2:0', 'Loop_2_out'), *connect('in_1_int', 'in_1_int_out'), *connect('cond_1_int', 'cond_1_int_out') ], nodes_with_edges_only=True) loop_node_1 = Node(sub_graph_1, 'Loop_2') loop_node_1.body = sub_graph_2 loop_node_1.in_edge(0)['external_port_id'] = 0 loop_node_1.in_edge(1)['external_port_id'] = 1 loop_node_1.out_edge(0)['external_port_id'] = 2 main_graph = ti_create_main_graph(sub_graph_1) main_graph.graph['additional_outputs'] = ['Loop', 'Loop_2'] return main_graph, sub_graph_1
def test_case1(self): graph = build_graph(nodes, [('Parameter1', 'FakeOutput1', {'in': 0, 'out': 0, 'fw_tensor_debug_info': [('Parameter1', 'Parameter1_tensor_name')]})]) graph.graph['packed_outputs'] = None graph.graph['user_shapes'] = None graph.stage = 'front' OutputCut().find_and_replace_pattern(graph) param1 = Node(graph, 'Parameter1') self.assertTrue(param1.out_node()['type'] == 'Result') self.assertTrue(param1.out_edge()['fw_tensor_debug_info'] == [('Parameter1', 'Parameter1_tensor_name')]) self.assertTrue(graph.get_op_nodes(name='FakeOutput1') == [])
def ti_create_main_graph(body): main_graph = build_graph(nodes_attrs=ti_main_graph_nodes, edges=[ *connect('M', '0:Loop'), *connect('cond', '1:Loop'), *connect('IN_2', '2:Loop'), *connect('IN_1', "3:Loop"), *connect('Loop:0', 'OUT_1') ], nodes_with_edges_only=True) loop_node = Node(main_graph, 'Loop') loop_node.body = body loop_node.in_edge(0)['external_port_id'] = 0 loop_node.in_edge(1)['external_port_id'] = 1 loop_node.in_edge(2)['external_port_id'] = 2 loop_node.in_edge(3)['external_port_id'] = 3 loop_node.out_edge(0)['external_port_id'] = 4 return main_graph
def test_global_pooling_to_reduce(self): graph = build_graph_with_edge_attrs(nodes, edges) graph_ref = build_graph(nodes, ref_edges) graph.stage = 'front' graph.graph['layout'] = 'NCHW' node = Node(graph, 'relu') node.out_edge(0)['fw_tensor_debug_info'] = [('Relu_0', 'Relu_tensor')] GlobalPoolingToReduce().find_and_replace_pattern(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp) node = Node(graph, 'relu') edge_attrs = node.out_port(0).get_destinations()[0].get_in_edge_attrs() self.assertTrue('fw_tensor_debug_info' in edge_attrs) self.assertTrue( edge_attrs['fw_tensor_debug_info'] == [('Relu_0', 'Relu_tensor')])
def create_node_on_port(self, node: Node, out_port: int, attrs: dict = None, edge_attrs: dict = None): """ Removes an edge, that is connected to nodes out_port. Creates new_node with attrs attributes and connects it to node by edge that stores the same information as cutted edge. :param node: Input node, to cut the edge from :param out_port: output port of edge to cut :param attrs: attributes of new node :param edge_attrs: attributes to be changed/added to new edge :return: Node instance of created new_node """ if edge_attrs is None: edge_attrs = {'in': 0} prev_edge_attrs = copy.deepcopy(node.out_edge(out_port)) prev_edge_attrs.update(edge_attrs) new_edge_attrs = prev_edge_attrs if attrs is None: attrs = dict() new_node = self.add_node(attrs) self.graph.add_edge(node.id, new_node.id, **new_edge_attrs) return new_node
def get_node_top(graph: Graph, name: str): node = Node(graph, name) return node.out_edge()['name'] if node else None