"vscode:/vscode.git/clone" did not exist on "e1bdcc7af38ff9d1d8fbdc223aa7b306b19bfdff"
test_feature_fetcher.py 1019 Bytes
Newer Older
1
2
3
4
5
6
7
import dgl
import dgl.graphbolt
import pytest
import torch


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

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

    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