test_feature_fetcher.py 1.08 KB
Newer Older
1
2
3
4
5
6
7
import dgl
import dgl.graphbolt
import pytest
import torch


def get_graphbolt_fetch_func():
8
9
10
11
12
13
14
15
    feature_store = {
        "feature": dgl.graphbolt.feature_store.TorchBasedFeatureStore(
            torch.randn(200, 4)
        ),
        "label": dgl.graphbolt.feature_store.TorchBasedFeatureStore(
            torch.randint(0, 10, (200,))
        ),
    }
16
17

    def fetch_func(data):
18
19
        return feature_store["feature"].read(data), feature_store["label"].read(
            data
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
        )

    return fetch_func


def get_tensor_fetch_func():
    feature_store = torch.randn(200, 4)
    label = torch.randint(0, 10, (200,))

    def fetch_func(data):
        return feature_store[data], label[data]

    return fetch_func


@pytest.mark.parametrize(
    "fetch_func", [get_graphbolt_fetch_func(), get_tensor_fetch_func()]
)
def test_FeatureFetcher(fetch_func):
    itemset = dgl.graphbolt.ItemSet(torch.arange(10))
    minibatch_dp = dgl.graphbolt.MinibatchSampler(itemset, batch_size=2)
    fetcher_dp = dgl.graphbolt.FeatureFetcher(minibatch_dp, fetch_func)

    assert len(list(fetcher_dp)) == 5