test_subgraph_sampler.py 5.39 KB
Newer Older
1
import dgl.graphbolt as gb
2
3
4
import gb_test_utils
import pytest
import torch
5
from torchdata.datapipes.iter import Mapper
6
7


8
9
def test_SubgraphSampler_invoke():
    itemset = gb.ItemSet(torch.arange(10), names="seed_nodes")
10
    item_sampler = gb.ItemSampler(itemset, batch_size=2)
11
12

    # Invoke via class constructor.
13
    datapipe = gb.SubgraphSampler(item_sampler)
14
15
16
17
    with pytest.raises(NotImplementedError):
        next(iter(datapipe))

    # Invokde via functional form.
18
    datapipe = item_sampler.sample_subgraph()
19
20
21
22
23
24
25
26
    with pytest.raises(NotImplementedError):
        next(iter(datapipe))


@pytest.mark.parametrize("labor", [False, True])
def test_NeighborSampler_invoke(labor):
    graph = gb_test_utils.rand_csc_graph(20, 0.15)
    itemset = gb.ItemSet(torch.arange(10), names="seed_nodes")
27
    item_sampler = gb.ItemSampler(itemset, batch_size=2)
28
29
30
31
32
    num_layer = 2
    fanouts = [torch.LongTensor([2]) for _ in range(num_layer)]

    # Invoke via class constructor.
    Sampler = gb.LayerNeighborSampler if labor else gb.NeighborSampler
33
    datapipe = Sampler(item_sampler, graph, fanouts)
34
35
36
37
    assert len(list(datapipe)) == 5

    # Invokde via functional form.
    if labor:
38
        datapipe = item_sampler.sample_layer_neighbor(graph, fanouts)
39
    else:
40
        datapipe = item_sampler.sample_neighbor(graph, fanouts)
41
42
43
    assert len(list(datapipe)) == 5


44
45
@pytest.mark.parametrize("labor", [False, True])
def test_SubgraphSampler_Node(labor):
46
    graph = gb_test_utils.rand_csc_graph(20, 0.15)
47
48
    itemset = gb.ItemSet(torch.arange(10), names="seed_nodes")
    item_sampler = gb.ItemSampler(itemset, batch_size=2)
49
50
    num_layer = 2
    fanouts = [torch.LongTensor([2]) for _ in range(num_layer)]
51
    Sampler = gb.LayerNeighborSampler if labor else gb.NeighborSampler
52
    sampler_dp = Sampler(item_sampler, graph, fanouts)
53
    assert len(list(sampler_dp)) == 5
54
55


56
def to_link_batch(data):
57
    block = gb.MiniBatch(node_pairs=data)
58
    return block
59
60


61
62
@pytest.mark.parametrize("labor", [False, True])
def test_SubgraphSampler_Link(labor):
63
    graph = gb_test_utils.rand_csc_graph(20, 0.15)
64
65
    itemset = gb.ItemSet(torch.arange(0, 20).reshape(-1, 2), names="node_pairs")
    item_sampler = gb.ItemSampler(itemset, batch_size=2)
66
67
    num_layer = 2
    fanouts = [torch.LongTensor([2]) for _ in range(num_layer)]
68
    Sampler = gb.LayerNeighborSampler if labor else gb.NeighborSampler
69
    neighbor_dp = Sampler(item_sampler, graph, fanouts)
70
    assert len(list(neighbor_dp)) == 5
71
72


73
@pytest.mark.parametrize("labor", [False, True])
74
def test_SubgraphSampler_Link_With_Negative(labor):
75
    graph = gb_test_utils.rand_csc_graph(20, 0.15)
76
77
    itemset = gb.ItemSet(torch.arange(0, 20).reshape(-1, 2), names="node_pairs")
    item_sampler = gb.ItemSampler(itemset, batch_size=2)
78
79
    num_layer = 2
    fanouts = [torch.LongTensor([2]) for _ in range(num_layer)]
80
    negative_dp = gb.UniformNegativeSampler(item_sampler, 1, graph)
81
82
    Sampler = gb.LayerNeighborSampler if labor else gb.NeighborSampler
    neighbor_dp = Sampler(negative_dp, graph, fanouts)
83
    assert len(list(neighbor_dp)) == 5
84
85


86
87
88
89
90
91
92
def get_hetero_graph():
    # COO graph:
    # [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
    # [2, 4, 2, 3, 0, 1, 1, 0, 0, 1]
    # [1, 1, 1, 1, 0, 0, 0, 0, 0] - > edge type.
    # num_nodes = 5, num_n1 = 2, num_n2 = 3
    ntypes = {"n1": 0, "n2": 1}
93
    etypes = {"n1:e1:n2": 0, "n2:e2:n1": 1}
94
95
96
97
98
99
100
101
102
103
104
105
    metadata = gb.GraphMetadata(ntypes, etypes)
    indptr = torch.LongTensor([0, 2, 4, 6, 8, 10])
    indices = torch.LongTensor([2, 4, 2, 3, 0, 1, 1, 0, 0, 1])
    type_per_edge = torch.LongTensor([1, 1, 1, 1, 0, 0, 0, 0, 0, 0])
    node_type_offset = torch.LongTensor([0, 2, 5])
    return gb.from_csc(
        indptr,
        indices,
        node_type_offset=node_type_offset,
        type_per_edge=type_per_edge,
        metadata=metadata,
    )
106
107


108
109
@pytest.mark.parametrize("labor", [False, True])
def test_SubgraphSampler_Link_Hetero(labor):
110
111
112
    graph = get_hetero_graph()
    itemset = gb.ItemSetDict(
        {
113
            "n1:e1:n2": gb.ItemSet(
114
115
                torch.LongTensor([[0, 0, 1, 1], [0, 2, 0, 1]]).T,
                names="node_pairs",
116
            ),
117
            "n2:e2:n1": gb.ItemSet(
118
119
                torch.LongTensor([[0, 0, 1, 1, 2, 2], [0, 1, 1, 0, 0, 1]]).T,
                names="node_pairs",
120
121
122
            ),
        }
    )
123

124
    item_sampler = gb.ItemSampler(itemset, batch_size=2)
125
126
    num_layer = 2
    fanouts = [torch.LongTensor([2]) for _ in range(num_layer)]
127
    Sampler = gb.LayerNeighborSampler if labor else gb.NeighborSampler
128
    neighbor_dp = Sampler(item_sampler, graph, fanouts)
129
    assert len(list(neighbor_dp)) == 5
130
131


132
@pytest.mark.parametrize("labor", [False, True])
133
def test_SubgraphSampler_Link_Hetero_With_Negative(labor):
134
135
136
    graph = get_hetero_graph()
    itemset = gb.ItemSetDict(
        {
137
            "n1:e1:n2": gb.ItemSet(
138
139
                torch.LongTensor([[0, 0, 1, 1], [0, 2, 0, 1]]).T,
                names="node_pairs",
140
            ),
141
            "n2:e2:n1": gb.ItemSet(
142
143
                torch.LongTensor([[0, 0, 1, 1, 2, 2], [0, 1, 1, 0, 0, 1]]).T,
                names="node_pairs",
144
145
146
147
            ),
        }
    )

148
    item_sampler = gb.ItemSampler(itemset, batch_size=2)
149
150
    num_layer = 2
    fanouts = [torch.LongTensor([2]) for _ in range(num_layer)]
151
    negative_dp = gb.UniformNegativeSampler(item_sampler, 1, graph)
152
153
    Sampler = gb.LayerNeighborSampler if labor else gb.NeighborSampler
    neighbor_dp = Sampler(negative_dp, graph, fanouts)
154
    assert len(list(neighbor_dp)) == 5