test_feature_fetcher.py 6.36 KB
Newer Older
1
2
import dgl.graphbolt as gb
import gb_test_utils
3
import torch
4
from torchdata.datapipes.iter import Mapper
5
6


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def test_FeatureFetcher_invoke():
    # Prepare graph and required datapipes.
    graph = gb_test_utils.rand_csc_graph(20, 0.15)
    a = torch.randint(0, 10, (graph.num_nodes,))
    b = torch.randint(0, 10, (graph.num_edges,))

    features = {}
    keys = [("node", None, "a"), ("edge", None, "b")]
    features[keys[0]] = gb.TorchBasedFeature(a)
    features[keys[1]] = gb.TorchBasedFeature(b)
    feature_store = gb.BasicFeatureStore(features)

    itemset = gb.ItemSet(torch.arange(10), names="seed_nodes")
    datapipe = gb.ItemSampler(itemset, batch_size=2)
    num_layer = 2
    fanouts = [torch.LongTensor([2]) for _ in range(num_layer)]

    # Invoke FeatureFetcher via class constructor.
    datapipe = gb.NeighborSampler(datapipe, graph, fanouts)
    datapipe = gb.FeatureFetcher(datapipe, feature_store, ["a"], ["b"])
    assert len(list(datapipe)) == 5

    # Invoke FeatureFetcher via functional form.
    datapipe = datapipe.sample_neighbor(graph, fanouts).fetch_feature(
        feature_store, ["a"], ["b"]
    )
    assert len(list(datapipe)) == 5


36
37
38
39
def test_FeatureFetcher_homo():
    graph = gb_test_utils.rand_csc_graph(20, 0.15)
    a = torch.randint(0, 10, (graph.num_nodes,))
    b = torch.randint(0, 10, (graph.num_edges,))
40

41
42
43
44
45
46
    features = {}
    keys = [("node", None, "a"), ("edge", None, "b")]
    features[keys[0]] = gb.TorchBasedFeature(a)
    features[keys[1]] = gb.TorchBasedFeature(b)
    feature_store = gb.BasicFeatureStore(features)

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_dp = gb.NeighborSampler(item_sampler, graph, fanouts)
52
    fetcher_dp = gb.FeatureFetcher(sampler_dp, feature_store, ["a"], ["b"])
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

    assert len(list(fetcher_dp)) == 5


def test_FeatureFetcher_with_edges_homo():
    graph = gb_test_utils.rand_csc_graph(20, 0.15)
    a = torch.randint(0, 10, (graph.num_nodes,))
    b = torch.randint(0, 10, (graph.num_edges,))

    def add_node_and_edge_ids(seeds):
        subgraphs = []
        for _ in range(3):
            subgraphs.append(
                gb.SampledSubgraphImpl(
                    node_pairs=(torch.tensor([]), torch.tensor([])),
                    reverse_edge_ids=torch.randint(0, graph.num_edges, (10,)),
                )
            )
71
        data = gb.MiniBatch(input_nodes=seeds, sampled_subgraphs=subgraphs)
72
73
74
75
76
77
78
79
80
        return data

    features = {}
    keys = [("node", None, "a"), ("edge", None, "b")]
    features[keys[0]] = gb.TorchBasedFeature(a)
    features[keys[1]] = gb.TorchBasedFeature(b)
    feature_store = gb.BasicFeatureStore(features)

    itemset = gb.ItemSet(torch.arange(10))
81
82
    item_sampler_dp = gb.ItemSampler(itemset, batch_size=2)
    converter_dp = Mapper(item_sampler_dp, add_node_and_edge_ids)
83
    fetcher_dp = gb.FeatureFetcher(converter_dp, feature_store, ["a"], ["b"])
84
85
86

    assert len(list(fetcher_dp)) == 5
    for data in fetcher_dp:
87
88
89
90
        assert data.node_features["a"].size(0) == 2
        assert len(data.edge_features) == 3
        for edge_feature in data.edge_features:
            assert edge_feature["b"].size(0) == 10
91
92
93
94
95
96
97
98
99


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}
100
    etypes = {"n1:e1:n2": 0, "n2:e2:n1": 1}
101
102
103
104
105
106
107
108
109
110
111
112
    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,
    )
113
114


115
116
117
118
def test_FeatureFetcher_hetero():
    graph = get_hetero_graph()
    a = torch.randint(0, 10, (2,))
    b = torch.randint(0, 10, (3,))
119

120
121
122
123
124
    features = {}
    keys = [("node", "n1", "a"), ("node", "n2", "a")]
    features[keys[0]] = gb.TorchBasedFeature(a)
    features[keys[1]] = gb.TorchBasedFeature(b)
    feature_store = gb.BasicFeatureStore(features)
125

126
127
    itemset = gb.ItemSetDict(
        {
128
129
            "n1": gb.ItemSet(torch.LongTensor([0, 1]), names="seed_nodes"),
            "n2": gb.ItemSet(torch.LongTensor([0, 1, 2]), names="seed_nodes"),
130
131
        }
    )
132
    item_sampler = gb.ItemSampler(itemset, batch_size=2)
133
134
    num_layer = 2
    fanouts = [torch.LongTensor([2]) for _ in range(num_layer)]
135
    sampler_dp = gb.NeighborSampler(item_sampler, graph, fanouts)
136
137
138
    fetcher_dp = gb.FeatureFetcher(
        sampler_dp, feature_store, {"n1": ["a"], "n2": ["a"]}
    )
139

140
141
142
143
144
145
146
147
148
149
    assert len(list(fetcher_dp)) == 3


def test_FeatureFetcher_with_edges_hetero():
    a = torch.randint(0, 10, (20,))
    b = torch.randint(0, 10, (50,))

    def add_node_and_edge_ids(seeds):
        subgraphs = []
        reverse_edge_ids = {
150
151
            "n1:e1:n2": torch.randint(0, 50, (10,)),
            "n2:e2:n1": torch.randint(0, 50, (10,)),
152
153
154
155
156
157
158
159
        }
        for _ in range(3):
            subgraphs.append(
                gb.SampledSubgraphImpl(
                    node_pairs=(torch.tensor([]), torch.tensor([])),
                    reverse_edge_ids=reverse_edge_ids,
                )
            )
160
        data = gb.MiniBatch(input_nodes=seeds, sampled_subgraphs=subgraphs)
161
        return data
162

163
164
165
166
167
    features = {}
    keys = [("node", "n1", "a"), ("edge", "n1:e1:n2", "a")]
    features[keys[0]] = gb.TorchBasedFeature(a)
    features[keys[1]] = gb.TorchBasedFeature(b)
    feature_store = gb.BasicFeatureStore(features)
168

169
170
171
172
173
    itemset = gb.ItemSetDict(
        {
            "n1": gb.ItemSet(torch.randint(0, 20, (10,))),
        }
    )
174
175
    item_sampler_dp = gb.ItemSampler(itemset, batch_size=2)
    converter_dp = Mapper(item_sampler_dp, add_node_and_edge_ids)
176
177
178
    fetcher_dp = gb.FeatureFetcher(
        converter_dp, feature_store, {"n1": ["a"]}, {"n1:e1:n2": ["a"]}
    )
179
180

    assert len(list(fetcher_dp)) == 5
181
    for data in fetcher_dp:
182
183
184
        assert data.node_features[("n1", "a")].size(0) == 2
        assert len(data.edge_features) == 3
        for edge_feature in data.edge_features:
185
            assert edge_feature[("n1:e1:n2", "a")].size(0) == 10