test_degree_padding2.py 6.25 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
7
8
9
10
11
import pytest
import torch
from torch_sparse import SparseTensor
from torch_geometric.datasets import Planetoid
from torch_geometric.utils import degree

devices = [torch.device('cuda')]


@pytest.mark.parametrize('device', devices)
def test_padded_index_select(device):
rusty1s's avatar
rusty1s committed
12
13
14
    start = torch.cuda.Event(enable_timing=True)
    end = torch.cuda.Event(enable_timing=True)

rusty1s's avatar
rusty1s committed
15
16
17
18
19
20
    row = torch.tensor([0, 0, 0, 0, 1, 1, 1, 2, 2, 3])
    col = torch.tensor([0, 1, 2, 3, 0, 2, 3, 1, 3, 2])
    idx = torch.tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    adj = SparseTensor(row=row, col=col).to(device)
    binptr = torch.tensor([0, 3, 5], device=device)

rusty1s's avatar
DONE  
rusty1s committed
21
22
23
24
    data = torch.ops.torch_sparse.padded_index(adj.storage.rowptr(),
                                               adj.storage.col(),
                                               adj.storage.rowcount(), binptr)
    node_perm, row_perm, col_perm, mask, size, length = data
rusty1s's avatar
rusty1s committed
25

rusty1s's avatar
DONE  
rusty1s committed
26
27
28
29
30
31
    print('node perm', node_perm)
    print('row perm', row_perm)
    print('col perm', col_perm)
    print('mask', mask)
    print('size', size)
    print('length', length)
rusty1s's avatar
rusty1s committed
32

rusty1s's avatar
rusty1s committed
33
34
35
36
37
38
39
40
41
42
43
    x = torch.tensor([[0], [1], [2], [3]], dtype=torch.float, device=device)
    x.requires_grad_()
    out = torch.ops.torch_sparse.padded_index_select(x, col_perm,
                                                     torch.tensor(0.))
    print(out)

    grad_out = torch.tensor(
        [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]],
        dtype=torch.float, device=device)
    out.backward(grad_out)
    print(x.grad)
rusty1s's avatar
rusty1s committed
44

rusty1s's avatar
rusty1s committed
45
46
47
48
49
50
51
    dataset = Planetoid('/tmp/Planetoid', name='PubMed')
    data = dataset[0]
    row, col = data.edge_index.to(device)

    adj = SparseTensor(row=row, col=col)
    rowcount = adj.storage.rowcount().to(device)
    rowptr = adj.storage.rowptr().to(device)
rusty1s's avatar
rusty1s committed
52
    binptr = torch.tensor([0, 4, 11, 30, 50, 80, 120, 140, 2000]).to(device)
rusty1s's avatar
rusty1s committed
53

rusty1s's avatar
DONE  
rusty1s committed
54
55
    # deg = degree(row, dtype=torch.long)
    # bins = torch.bincount(deg)
rusty1s's avatar
rusty1s committed
56
57
58
59
60
61
62
63
64
65
    # print(bins.size())
    # print(bins[:200])
    # for i in range(110):
    #     if i == 10:
    #         start.record()
    #     perms, lengths = torch.ops.torch_sparse.bin_assignment(
    #         rowcount, binptr)
    # end.record()
    # torch.cuda.synchronize()
    # print('bin assignment', start.elapsed_time(end))
rusty1s's avatar
DONE  
rusty1s committed
66
67
68
69
70
71
72
    # idx, mask, size, length, offset = torch.ops.torch_sparse.padded_index(
    #     rowptr, rowcount, binptr)
    # print(size)
    # print(length)
    # print(offset)
    # print(mask[:10])
    # print(idx[:10])
rusty1s's avatar
rusty1s committed
73

rusty1s's avatar
rusty1s committed
74
75
76
77
78
79
80
81
82
83
84
    x = torch.randn(adj.size(0), 512).to(device)

    data = torch.ops.torch_sparse.padded_index(rowptr, col, rowcount, binptr)
    node_perm, row_perm, col_perm, mask, node_sizes, edge_sizes = data

    out = torch.ops.torch_sparse.padded_index_select(x, col_perm,
                                                     torch.tensor(0.))
    outs = out.split(edge_sizes)
    for out, size in zip(outs, node_sizes):
        print(out.view(size, -1, x.size(-1)).shape)

rusty1s's avatar
rusty1s committed
85
86
87
    for i in range(110):
        if i == 10:
            start.record()
rusty1s's avatar
DONE  
rusty1s committed
88
        torch.ops.torch_sparse.padded_index(rowptr, col, rowcount, binptr)
rusty1s's avatar
rusty1s committed
89
90
    end.record()
    torch.cuda.synchronize()
rusty1s's avatar
rusty1s committed
91
    print('padded index', start.elapsed_time(end))
rusty1s's avatar
rusty1s committed
92

rusty1s's avatar
rusty1s committed
93
94
95
    for i in range(110):
        if i == 10:
            start.record()
rusty1s's avatar
rusty1s committed
96
97
98
        out = torch.ops.torch_sparse.padded_index_select(
            x, col_perm, torch.tensor(0.))
        out.split(edge_sizes)
rusty1s's avatar
rusty1s committed
99
100
    end.record()
    torch.cuda.synchronize()
rusty1s's avatar
rusty1s committed
101
    print('padded index select', start.elapsed_time(end))
rusty1s's avatar
rusty1s committed
102

rusty1s's avatar
rusty1s committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    for i in range(110):
        if i == 10:
            start.record()
        torch.repeat_interleave(rowcount, rowcount)
    end.record()
    torch.cuda.synchronize()
    print('repeat', start.elapsed_time(end))

    for i in range(110):
        if i == 10:
            start.record()
        rowcount.cumsum(0)
    end.record()
    torch.cuda.synchronize()
    print('cumsum', start.elapsed_time(end))

    rowcount2 = rowcount.unsqueeze(1).repeat(1, 5).contiguous()
    for i in range(110):
        if i == 10:
            start.record()
        rowcount2.cumsum(0)
    end.record()
    torch.cuda.synchronize()
    print('cumsum', start.elapsed_time(end))

    for i in range(110):
        if i == 10:
            start.record()
        rowcount.sort()
    end.record()
    torch.cuda.synchronize()
    print('sort', start.elapsed_time(end))
rusty1s's avatar
rusty1s committed
135
136
137
138
139
140
141

    for i in range(110):
        if i == 10:
            start.record()
        x.index_select(0, col)
    end.record()
    torch.cuda.synchronize()
rusty1s's avatar
rusty1s committed
142
143
    print('index_select', start.elapsed_time(end))
    return
rusty1s's avatar
rusty1s committed
144
145
146
147
148
149
150
151
152
153

    for i in range(110):
        if i == 10:
            start.record()
        for perm, length in zip(perms, lengths):
            torch.ops.torch_sparse.padded_index_select(x, rowptr, col,
                                                       perm, length,
                                                       torch.tensor(0.))
    end.record()
    torch.cuda.synchronize()
rusty1s's avatar
rusty1s committed
154
    print('padded_index_select', start.elapsed_time(end))
rusty1s's avatar
rusty1s committed
155
156
157
158
159
160
161

    for perm, length in zip(perms, lengths):
        out, mask = torch.ops.torch_sparse.padded_index_select(
            x, rowptr, col, perm, length, torch.tensor(0.))
        print(out.size(), mask.size(), out.numel(), (out != 0).sum().item())

    lengths = bin_strategy[:, 1].view(-1).tolist()
rusty1s's avatar
rusty1s committed
162
163
164
165
166
167
168
169

    for dim in [32, 64, 128, 256, 512, 1024]:
        print(f'--- Dim: {dim} ---')
        x = torch.randn(adj.size(0), dim).to(device)

        for i in range(110):
            if i == 10:
                start.record()
rusty1s's avatar
rusty1s committed
170
171
172
173
            perms = torch.ops.torch_sparse.bin_assignment(
                rowcount, bin_strategy)
            print(perms)
            return
rusty1s's avatar
rusty1s committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
            for perm, length in zip(perms, lengths):
                out1, _ = torch.ops.torch_sparse.padded_index_select(
                    x, rowptr, col, perm, length, torch.tensor(0.))
        end.record()
        torch.cuda.synchronize()
        print(start.elapsed_time(end))

        for i in range(110):
            if i == 10:
                start.record()
            out2 = x.index_select(0, row)
        end.record()
        torch.cuda.synchronize()
        print(start.elapsed_time(end))

        for i in range(110):
            if i == 10:
                start.record()
            out3 = x.index_select(0, col)
        end.record()
        torch.cuda.synchronize()
        print(start.elapsed_time(end))