Ejemplo n.º 1
0
def basic_graph(basic_graph_no_v12) -> Tuple[MutableEdge, Vertex, Vertex,
                                             Vertex, Vertex]:
    complex_operation, vertex1, vertex2, vertex3, vertex4 = basic_graph_no_v12
    edge = IdentityOperation()
    vertex1.out_bound_edges.append(edge)
    edge.end_vertex = vertex2
    return complex_operation, vertex1, vertex2, vertex3, vertex4
Ejemplo n.º 2
0
def test_sort_vertices(basic_graph):
    complex_operation, vertex1, vertex2, vertex3, vertex4 = basic_graph

    complex_operation.sort_vertices()

    assert len(complex_operation.vertices_topo_order) == 4
    assert (complex_operation.vertices_topo_order[0] is
            complex_operation.output_vertex)
    assert (complex_operation.vertices_topo_order[1] is vertex2)
    assert (complex_operation.vertices_topo_order[2] is vertex1)
    assert (complex_operation.vertices_topo_order[3] is
            complex_operation.input_vertex)
    assert len(complex_operation.input_vertex.out_bound_edges) == 2

    edge8 = IdentityOperation()
    vertex2.out_bound_edges.append(edge8)
    edge8.end_vertex = vertex3

    complex_operation.sort_vertices()

    assert vertex3 in complex_operation.vertices_topo_order
    assert complex_operation.output_vertex.order == 0
    assert vertex3.order == 1
    assert vertex2.order == 2
    assert vertex1.order == 3
    assert complex_operation.input_vertex.order == 4

    assert len(complex_operation.output_vertex.out_bound_edges) == 0
    assert len(complex_operation.input_vertex.out_bound_edges) == 2
    assert len(vertex1.out_bound_edges) == 2
    assert len(vertex2.out_bound_edges) == 2
    assert len(vertex3.out_bound_edges) == 1
Ejemplo n.º 3
0
    def build_graph(self) -> None:
        edge1 = PointConv2D((0, 3))
        edge2 = ReLU()
        edge3 = IdentityOperation()
        vertex1 = Vertex(name='V1')

        self.input_vertex.out_bound_edges.append(edge1)
        self.input_vertex.out_bound_edges.append(edge3)
        vertex1.out_bound_edges.append(edge2)
        edge1.end_vertex = vertex1
        edge2.end_vertex = self.output_vertex
        edge3.end_vertex = self.output_vertex
Ejemplo n.º 4
0
    def build_graph(self) -> None:
        conv_edge1 = MutableEdge((BatchNorm(), PointConv2D(
            (20, 40)), DepthwiseConv2D(), IdentityOperation(),
                                  SeparableConv2D(
                                      (20, 40)), Dropout(0.25), ReLU()),
                                 max_vertices=10,
                                 initialize_with_identity=False)
        conv_edge2 = conv_edge1.deep_copy()
        vertex1 = Vertex(name='V1')
        vertex2 = Vertex(name='V2')
        self.input_vertex.add_edge(conv_edge1, vertex1)
        vertex7 = Vertex(name='V7')
        vertex1.add_edge(MaxPool2D(), vertex7)
        vertex7.add_edge(conv_edge2, vertex2)

        vertex3 = Vertex(name='V3')
        vertex2.add_edge(Flatten(), vertex3)
        vertex4 = Vertex(name='V4')
        vertex3.add_edge(Dense(512), vertex4)
        vertex5 = Vertex(name='V5')
        vertex4.add_edge(ReLU(), vertex5)
        vertex6 = Vertex(name='V6')

        vertex5.add_edge(Dropout(0.5), vertex6)
        vertex6.add_edge(Dense(num_classes), self.output_vertex)
Ejemplo n.º 5
0
    def build_graph(self) -> None:
        vertex1 = Vertex()
        vertex2 = Vertex()

        edge1 = IdentityOperation()
        edge2 = IdentityOperation()
        edge3 = IdentityOperation()

        self.input_vertex.out_bound_edges.extend([edge1, edge2])
        edge1.end_vertex = vertex1
        edge2.end_vertex = vertex2
        vertex2.out_bound_edges.append(edge3)
        edge3.end_vertex = vertex1
Ejemplo n.º 6
0
def test_remove_edge_success():
    complex_operation = MutableEdge((PointConv2D((1, 4)), MaxPool2D()))
    edge1 = IdentityOperation()
    edge2 = IdentityOperation()
    complex_operation.input_vertex.out_bound_edges.clear()
    complex_operation.input_vertex.out_bound_edges.append(edge1)
    middle_vertex = Vertex()
    complex_operation.vertices_topo_order.append(middle_vertex)
    edge1.end_vertex = middle_vertex
    middle_vertex.out_bound_edges.append(edge2)
    edge2.end_vertex = complex_operation.output_vertex

    # Edge from input to output. So now we can remove one edge
    edge3 = IdentityOperation()
    complex_operation.input_vertex.out_bound_edges.append(edge3)
    edge3.end_vertex = complex_operation.output_vertex

    assert complex_operation.mutation_remove_edge()
    assert len(complex_operation.input_vertex.out_bound_edges) == 1
Ejemplo n.º 7
0
def test_remove_node_fail():
    complex_operation = MutableEdge((PointConv2D((1, 4)), ))
    assert not complex_operation.mutation_remove_vertex()

    complex_operation.input_vertex.out_bound_edges.clear()

    vertex1 = Vertex()
    vertex2 = Vertex()
    edge1 = IdentityOperation()
    edge2 = IdentityOperation()
    edge3 = IdentityOperation()

    complex_operation.input_vertex.out_bound_edges.append(edge1)
    edge1.end_vertex = vertex1
    vertex1.out_bound_edges.append(edge2)
    edge2.end_vertex = vertex2
    vertex2.out_bound_edges.append(edge3)
    edge3.end_vertex = complex_operation.output_vertex

    complex_operation.sort_vertices()
    assert len(complex_operation.vertices_topo_order) == 4
    assert not complex_operation.mutation_remove_vertex()
Ejemplo n.º 8
0
def test_remove_edge_fail2():
    complex_operation = MutableEdge((PointConv2D((1, 4)), MaxPool2D()))
    edge1 = IdentityOperation()
    edge2 = IdentityOperation()
    complex_operation.input_vertex.out_bound_edges.clear()
    complex_operation.input_vertex.out_bound_edges.append(edge1)
    middle_vertex = Vertex()
    complex_operation.vertices_topo_order.append(middle_vertex)
    edge1.end_vertex = middle_vertex
    middle_vertex.out_bound_edges.append(edge2)
    edge2.end_vertex = complex_operation.output_vertex

    assert not complex_operation.mutation_remove_edge()
Ejemplo n.º 9
0
    def __init__(self,
                 available_operations: Tuple[Edge, ...],
                 name: str = '',
                 initialize_with_identity: bool = True,
                 max_vertices: int = -1) -> None:
        super().__init__(name)

        if 0 < max_vertices < 2:
            raise RuntimeError(
                'Max vertices: %d is too small. Must be at least 2 if '
                'enabled.' % max_vertices)

        self.available_operations = available_operations
        self.max_vertices = max_vertices

        if initialize_with_identity:
            edge: Edge = IdentityOperation()
        else:
            edge, = np.random.choice(self.available_operations, size=1)
        self.input_vertex.out_bound_edges.append(edge)
        edge.end_vertex = self.output_vertex

        self.sort_vertices()
Ejemplo n.º 10
0
def test_remove_node_success(basic_graph_no_v12, mocker):
    complex_operation, vertex1, vertex2, vertex3, vertex4 = basic_graph_no_v12

    vertex = Vertex()
    edge1 = IdentityOperation()
    edge2 = IdentityOperation()

    vertex1.out_bound_edges.append(edge1)
    edge1.end_vertex = vertex
    vertex.out_bound_edges.append(edge2)
    edge2.end_vertex = vertex2

    complex_operation.sort_vertices()

    mocker.patch('numpy.random.permutation', return_value=[vertex, vertex2])

    assert vertex in complex_operation.vertices_topo_order

    assert complex_operation.mutation_remove_vertex()

    assert vertex2 in complex_operation.vertices_topo_order
    assert vertex not in complex_operation.vertices_topo_order

    assert len(vertex1.out_bound_edges) == 1
Ejemplo n.º 11
0
def basic_graph_no_v12() -> Tuple[MutableEdge, Vertex, Vertex, Vertex,
                                  Vertex]:
    complex_operation = MutableEdge((PointConv2D((1, 4)), MaxPool2D()))
    vertex1 = Vertex()
    vertex2 = Vertex()
    vertex3 = Vertex()
    vertex4 = Vertex()
    edge1 = IdentityOperation()
    edge2 = IdentityOperation()
    edge3 = IdentityOperation()
    edge4 = IdentityOperation()
    edge5 = IdentityOperation()
    edge6 = IdentityOperation()
    complex_operation.input_vertex.out_bound_edges.clear()
    complex_operation.input_vertex.out_bound_edges.extend([edge1, edge2, edge3])
    edge1.end_vertex = vertex1
    edge2.end_vertex = vertex2
    edge3.end_vertex = vertex4
    vertex1.out_bound_edges.append(edge6)
    edge6.end_vertex = complex_operation.output_vertex
    vertex2.out_bound_edges.append(edge4)
    edge4.end_vertex = complex_operation.output_vertex
    vertex3.out_bound_edges.append(edge5)
    edge5.end_vertex = complex_operation.output_vertex

    return complex_operation, vertex1, vertex2, vertex3, vertex4
Ejemplo n.º 12
0
 def build_graph(self) -> None:
     vertex = Vertex()
     edge1 = MaxPool2D()
     edge2 = IdentityOperation()
     self.input_vertex.add_edge(edge1, vertex)
     vertex.add_edge(edge2, self.output_vertex)