Exemplo n.º 1
0
def test_match_fail():
    class MatchSuffNode(MatchNode):
        def __init__(self, name, suff):
            super().__init__(name)
            self.suff = suff

        def _match(self, G, node, edge):
            return node.name.endswith(self.suff)

    G = Graph()
    G.add_node(Node("test1a"))
    G.add_edge(Edge('test1a', Node('test2a')))
    G.add_edge(Edge('test2a', Node('test3b')))
    G.add_edge(Edge('test3b', Node('test4a')))

    fragment = Graph()
    fragment.add_node(MatchSuffNode('match1', 'a'))
    fragment.add_node(MatchSuffNode('match2', 'b'))
    fragment.add_edge(Edge('match1', 'match2'))

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert res[0].num_nodes() == 2
    assert res[0].num_edges() == 1
    assert [node.name for node in res[0].nodes()] == ['test2a', 'test3b']
Exemplo n.º 2
0
def test_match4alt():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_node(Node('test3'))
    G.add_node(Node('test4'))
    G.add_node(Node('test5'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=1))
    G.add_edge(Edge('test4', 'test5'))

    fragment = GraphMatcher()
    m2 = MatchNodeByName("test2")
    m3 = MatchNodeByName('test3')
    m4 = MatchNodeByName('test4')
    e1 = MatchEdgeInputsGroupFactory()

    fragment.add_edge(e1.get_edge(from_node=m2, to_node=m4))
    fragment.add_edge(e1.get_edge(from_node=m3, to_node=m4))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
Exemplo n.º 3
0
def test_create3():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    assert [node.name for node in G.dfs()] == ['test1', 'test2', 'test3']
    assert not G.verify_edges()
Exemplo n.º 4
0
def test_reverse_dfs1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    expect = ['test1', 'test2', 'test3']
    assert [node.name for node in G.dfs()] == expect
    expect.reverse()
    assert [node.name for node in G.dfs(reverse=True)] == expect
Exemplo n.º 5
0
    def match_function(self, G: GraphView):
        sub = GraphView()
        sub.add_node(MatchNode('0', matcher=lambda node:\
                isinstance(node, FilterParameters)))
        sub.add_node(MatchNode('1', matcher=lambda node:\
                isinstance(node, MatrixAddParameters)))
        sub.add_node(MatchNode('2', matcher=lambda node:\
                isinstance(node, ConstantInputParameters)))
        sub.add_edge(Edge('0', '1', to_idx=0))
        sub.add_edge(Edge('2', '1', to_idx=1))

        return G.match_fragment(sub)
Exemplo n.º 6
0
def test_create4():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    assert not G.verify_edges()
    G.replace_node('test2', Node('test4'))
    expect = ['test1', 'test4', 'test3']
    assert [node.name for node in G.dfs()] == expect
    expect.reverse()
    assert [node.name for node in G.dfs(reverse=True)] == expect
    assert not G.verify_edges()
Exemplo n.º 7
0
def test_create1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    expect = ['test1', 'test2', 'test3']
    assert [node.name for node in G.dfs()] == expect
    expect.reverse()
    assert [node.name for node in G.dfs(reverse=True)] == expect
    assert G.num_in_edges('test1') == 0
    assert G.num_out_edges('test1') == 1
    assert not G.verify_edges()
Exemplo n.º 8
0
def test_match_and_remove():
    G = Graph()
    G.add_node(Node("test0"))
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test0', 'test1'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4'))
    G.add_edge(Edge('test4', Node('test5')))

    fragment = Graph()
    fragment.add_node(MatchNameNode("test1"))
    fragment.add_node(MatchNameNode('test2'))
    fragment.add_edge(Edge('test1', 'test2'))
    fragment.add_edge(Edge('test1', MatchNameNode('test3')))

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
    G.remove_fragment(res[0])
    assert [node.name for node in G.dfs()] == ['test0', 'test4', 'test5']
    assert not G.verify_edges()
Exemplo n.º 9
0
def test_create5():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4'))
    assert G.num_in_edges('test4') == 2
    assert G.num_out_edges('test1') == 2
    assert [node.name
            for node in G.dfs()] == ['test1', 'test2', 'test3', 'test4']
    assert not G.verify_edges()
Exemplo n.º 10
0
    def calculate_weights(self, edges: List[Edge]) -> List[Edge]:
        '''
        Metoda przypisująca wyznaczająca wagę dla listy krawędzi,
        których waga jest różna od `0.0`.
        '''
        # Stwórz tymczasowy zbiór krawędzi z wagą 0.0
        tmp_edges_set = set(edge for edge in edges)
        for i in range(self.n):
            for j in range(i + 1, self.n):
                edge = Edge(i, j, 0.0)
                if edge not in tmp_edges_set:
                    edge.set_weight(self.calculate_weight(i, j))
                    edges.append(edge)

        return edges
Exemplo n.º 11
0
def extract_node(G: NNGraph, keep_node: Parameters):
    if not isinstance(keep_node, SingleInputAndOutput):
        raise NotImplementedError("exclude only works with single input and output nodes at present")
    LOG.info("extracting node %s into new graph", keep_node.name)
    for node in list(G.nodes()):
        if node != keep_node:
            G.remove(node)
    G.reset_inout_counts()
    if isinstance(keep_node, SingleInputAndOutput):
        input_node = G.add_input(keep_node.in_dims[0])
        output_node = G.add_output()
        G.add_edge(Edge(input_node, keep_node))
        G.add_edge(Edge(keep_node, output_node))
        G.add_dimensions()
    else:
        raise NotImplementedError()
Exemplo n.º 12
0
 def move_activation(G, activation, edges):
     ain_edge = G.in_edges(activation.name)[0]
     aout_edge = G.out_edges(activation.name)[0]
     G.remove(activation)
     new_edge = Edge(from_node=ain_edge.from_node,
                     to_node=aout_edge.to_node,
                     from_idx=ain_edge.from_idx,
                     to_idx=aout_edge.to_idx)
     G.add_edge(new_edge)
     cnt = 0
     for edge in edges:
         LOG.info("Moving activation %s between %s and %s", activation.name,
                  edge.from_node.name, edge.to_node.name)
         if cnt > 0:
             new_activation = activation.clone("{}_{}".format(
                 new_activation.name, cnt))
         else:
             new_activation = activation
         cnt += 1
         new_activation.in_dims = [
             edge.from_node.out_dims[edge.from_idx].clone()
         ]
         new_activation.out_dims = [
             edge.to_node.in_dims[edge.to_idx].clone()
         ]
         G.insert_node(new_activation,
                       edge.from_node,
                       edge.to_node,
                       from_idx=edge.from_idx,
                       to_idx=edge.to_idx)
Exemplo n.º 13
0
    def apply_connected_cities(self, edges: List[Edge]) -> List[Edge]:
        '''
        Metoda przypisująca połączonym miastom wagę `0.0`
        '''
        for v_1, v_2 in self.connected_cities:
            edge = Edge(v_1, v_2, 0.0)
            edges.append(edge)

        return edges
Exemplo n.º 14
0
 def match_function(self, G: GraphView):
     sub = GraphView()
     sub.add_node(MatchNode('0', matcher=lambda node:\
             isinstance(node, PadParameters)))
     sub.add_node(MatchNode('1', matcher=lambda node:\
             isinstance(node, FilterLikeParameters) and\
             self.has_no_padding(node)))
     sub.add_edge(Edge('0', '1'))
     return G.match_fragment(sub)
Exemplo n.º 15
0
def test_all_predecessors():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=1))
    preds = set()
    for node in G.all_predecessors('test4'):
        assert node.name not in preds
        preds.add(node.name)
    test_fn = lambda name: set(node.name for node in G.all_predecessors(name))
    assert set(['test1', 'test2', 'test3']) == test_fn('test4')
    assert set(['test1']) == test_fn('test2')
    assert set(['test1']) == test_fn('test3')
    assert not test_fn('test1')
Exemplo n.º 16
0
def test_match1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test3', 'test4'))
    assert not G.verify_edges()

    fragment = GraphMatcher()
    fragment.add_edge(
        MatchEdgeByIdx(from_node=MatchNodeByName("test1"),
                       to_node=MatchNodeByName('test2')))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert len(res[0]) == 2
    assert res[0].num_edges() == 1
Exemplo n.º 17
0
def test_insert():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=2))

    G.insert_node(Node('test5'), 'test2', 'test4')

    nodes = [node.name for node in G.dfs()]
    assert nodes == ['test1', 'test2', 'test5', 'test3', 'test4']
    suc = G.successor_names('test5')
    pred = G.predecessor_names('test5')
    assert len(suc) == 1 and 'test4' in suc
    assert len(pred) == 1 and 'test2' in pred
    assert not G.verify_edges()
Exemplo n.º 18
0
def test_match1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test3', 'test4'))
    assert not G.verify_edges()

    fragment = Graph()
    fragment.add_node(MatchNameNode("test1"))
    fragment.add_node(MatchNameNode('test2'))
    fragment.add_edge(Edge('test1', 'test2'))
    assert not fragment.verify_edges()

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert len(res[0]) == 2
    assert res[0].num_edges() == 1
Exemplo n.º 19
0
def test_match3():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    # not the same - look here

    fragment = Graph()
    fragment.add_node(MatchNameNode("test1"))
    fragment.add_node(MatchNameNode('test2'))
    fragment.add_edge(Edge('test1', 'test2'))
    fragment.add_edge(Edge('test1', MatchNameNode('test3')))

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
Exemplo n.º 20
0
    def match_function(self, G: GraphView):
        sub = GraphView()
        sub.add_node(
            MatchNode(
                '0',
                matcher=lambda node: isinstance(node, ReluActivationParameters
                                                ) and node.upper_bound == 6))
        sub.add_node(
            MatchNode(
                '1',
                matcher=lambda node: isinstance(node, MatrixMulParameters)))
        sub.add_node(
            MatchNode(
                '2',
                matcher=lambda node: isinstance(node, ConstantInputParameters)
                and check_equals(G, node, 1.0 / 6.0)))
        sub.add_edge(Edge('0', '1', to_idx=0))
        sub.add_edge(Edge('2', '1', to_idx=1))

        return G.match_fragment(sub)
Exemplo n.º 21
0
 def match_function(self, G: GraphView):
     sub = GraphView()
     sub.add_node(MatchNode('0',
                            matcher=lambda node:
                            isinstance(node, FcParameters) and
                            self.valid_linear(node)))
     sub.add_node(MatchNode('1', matcher=lambda node:
                            isinstance(node, ActivationParameters) and
                            self.valid_activation(node)))
     sub.add_edge(Edge('0', '1'))
     return G.match_fragment(sub)
Exemplo n.º 22
0
def test_remove_edge():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    assert [node.name for node in G.dfs()] == ['test1', 'test2', 'test3']
    assert G.num_edges() == 2
    edge12 = G.edge('test1', 'test2')
    assert edge12.from_node.name == 'test1' and edge12.from_idx == 0
    assert edge12.to_node.name == 'test2' and edge12.to_idx == 0
    assert not G.verify_edges()
    G.remove_edge(edge12)
    assert G.num_edges() == 1
    assert G.num_in_edges('test1') == 0
    assert G.num_out_edges('test1') == 0
    assert G.num_in_edges('test2') == 0
    assert G.num_out_edges('test2') == 1
    assert G.num_in_edges('test3') == 1
    assert G.num_out_edges('test3') == 0
    assert not G.verify_edges()
Exemplo n.º 23
0
def test_match3(caplog):
    caplog.set_level(logging.DEBUG)
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    # not the same - look here

    fragment = GraphMatcher()
    n1 = MatchNodeByName('test1')
    fragment.add_edge(
        MatchEdgeByIdx(from_node=n1, to_node=MatchNodeByName('test2')))
    fragment.add_edge(
        MatchEdgeByIdx(from_node=n1, to_node=MatchNodeByName('test3')))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
Exemplo n.º 24
0
def test_match_fail(caplog):
    caplog.set_level(logging.DEBUG)

    class MatchSuffNode(NodeMatch):
        def __init__(self, suff):
            self._suff = suff
            self._has_matched = False

        def match(self, G, node, state):
            if self._has_matched:
                return False
            if node.name.endswith(self._suff):
                self._has_matched = True
                return True
            return False

        def commit_match(self, G, node, state):
            pass

        def reset_match(self, G, state, node=None, init=False):
            self._has_matched = False

    G = Graph()
    G.add_node(Node("test1a"))
    G.add_edge(Edge('test1a', Node('test2a')))
    G.add_edge(Edge('test2a', Node('test3b')))
    G.add_edge(Edge('test3b', Node('test4a')))

    fragment = GraphMatcher()
    fragment.add_edge(
        MatchEdgeByIdx(from_node=MatchSuffNode('a'),
                       to_node=MatchSuffNode('b')))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert res[0].num_nodes() == 2
    assert res[0].num_edges() == 1
    assert set([node.name
                for node in res[0].nodes()]) == set(['test2a', 'test3b'])
Exemplo n.º 25
0
    def match_function(self, G: GraphView):
        sub = GraphView()
        sub.add_node(MatchNode('0', matcher=lambda node:\
                isinstance(node, Conv2DParameters) and\
                self.valid_activation(node)))
        if self.match_activation and self.match_pool:
            if self.pool_after_activation:
                self.add_activation('1', sub)
                self.add_pooling('2', sub)
            else:
                self.add_pooling('1', sub)
                self.add_activation('2', sub)
            sub.add_edge(Edge('0', '1'))
            sub.add_edge(Edge('1', '2'))
        elif self.match_activation:
            self.add_activation('1', sub)
            sub.add_edge(Edge('0', '1'))
        elif self.match_pool:
            self.add_pooling('1', sub)
            sub.add_edge(Edge('0', '1'))

        return G.match_fragment(sub)
Exemplo n.º 26
0
def test_match4():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_node(Node('test3'))
    G.add_node(Node('test4'))
    G.add_node(Node('test5'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=1))
    G.add_edge(Edge('test4', 'test5'))

    fragment = GraphMatcher()
    m2 = MatchNodeByName("test2")
    m3 = MatchNodeByName('test3')
    m4 = MatchNodeByName('test4')
    fragment.add_edge(MatchEdgeByIdx(from_node=m2, to_node=m4))
    fragment.add_edge(MatchEdgeByIdx(from_node=m3, to_node=m4, to_idx=1))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
Exemplo n.º 27
0
 def __init__(self, nodes):
     self._nodes = nodes
     self._in_edges = {}
     self._out_edges = {}
     last_edge = None
     for idx, node in enumerate(nodes):
         node.step_idx = idx
         if last_edge:
             last_edge.to_idx = 0
             last_edge.to_node = node
             last_edge.params = node
             self._in_edges[node.name] = [last_edge]
             self._out_edges[last_edge.from_node.name] = [[last_edge]]
         last_edge = Edge(from_node=node, to_node=None)
     self._out_edges[self._nodes[-1].name] = []
Exemplo n.º 28
0
def remove_formatter(G, out_edge):
    input_node = out_edge.from_node
    fmt_node = out_edge.to_node
    fmt_edge = G.out_edges(fmt_node.name)[0]
    fmt_qrec = G.quantization and G.quantization.get(NodeId(fmt_node))
    G.remove(fmt_node)
    input_node.dims = fmt_node.out_dims[0]
    input_node.out_dims_hint = fmt_node.out_dims_hint

    G.add_edge(Edge(input_node, fmt_edge.to_node, to_idx=fmt_edge.to_idx))
    if fmt_qrec:
        input_qrec = G.quantization[NodeId(input_node)]
        input_qrec.out_qs = fmt_qrec.out_qs
        input_qrec.in_qs = fmt_qrec.out_qs
        G.quantization.remove_node(fmt_node)
Exemplo n.º 29
0
def read_graph(graphclass, f: IO[str]) -> Tuple[Graph, List[str], bool]:
    """
    Read a graph from a file
    :param graphclass: The class of the graph
    :param f: The file
    :return: The graph
    """
    options = []

    while True:
        try:
            line = read_line(f)
            n = int(line)
            graph = graphclass(directed=False, n=n)
            break
        except ValueError:
            if len(line) > 0 and line[-1] == '\n':
                options.append(line[:-1])
            else:
                options.append(line)

    line = read_line(f)
    edges = []

    try:
        while True:
            comma = line.find(',')
            if ':' in line:
                colon = line.find(':')
                edges.append((int(line[:comma]), int(line[comma + 1:colon]),
                              int(line[colon + 1:])))
            else:
                edges.append((int(line[:comma]), int(line[comma + 1:]), None))
            line = read_line(f)
    except Exception:
        pass

    indexed_nodes = list(graph.vertices)

    for edge in edges:
        graph += Edge(indexed_nodes[edge[0]], indexed_nodes[edge[1]], edge[2])

    if line != '' and line[0] == '-':
        return graph, options, True
    else:
        return graph, options, False
Exemplo n.º 30
0
 def move_activation(G, activation, edges):
     nid = NodeId(activation)
     qrec = G.quantization[
         nid] if G.quantization and nid in G.quantization else None
     ain_edge = G.in_edges(activation.name)[0]
     aout_edge = G.out_edges(activation.name)[0]
     G.remove(activation)
     new_edge = Edge(from_node=ain_edge.from_node,
                     to_node=aout_edge.to_node,
                     from_idx=ain_edge.from_idx,
                     to_idx=aout_edge.to_idx)
     G.add_edge(new_edge)
     cnt = 0
     for edge in edges:
         LOG.info("Moving activation %s between %s and %s", activation.name,
                  edge.from_node.name, edge.to_node.name)
         if cnt > 0:
             new_activation = activation.clone("{}_{}".format(
                 new_activation.name, cnt))
         else:
             new_activation = activation
         cnt += 1
         new_activation.in_dims = [
             edge.from_node.out_dims[edge.from_idx].clone()
         ]
         new_activation.out_dims = [
             edge.to_node.in_dims[edge.to_idx].clone()
         ]
         G.insert_node(new_activation,
                       edge.from_node,
                       edge.to_node,
                       from_idx=edge.from_idx,
                       to_idx=edge.to_idx)
         if qrec:
             from_qrec = G.quantization[NodeId(edge.from_node)]
             new_qrec = deepcopy(qrec)
             new_qrec.in_qs[0] = deepcopy(from_qrec.out_qs[edge.from_idx])
             G.quantization[NodeId(new_activation)] = new_qrec
             G.quantization.propagate(G,
                                      new_activation,
                                      new_edge.from_node,
                                      qtype=new_qrec.out_qs[0])