test_segment.py 3.82 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
from itertools import product

import pytest
import torch
rusty1s's avatar
rusty1s committed
5
from torch_scatter import segment_coo, segment_csr
rusty1s's avatar
rusty1s committed
6
7
8
9
10
11
12
13
14

from .utils import tensor

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


@pytest.mark.parametrize('dtype,device', product(dtypes, devices))
def test_forward(dtype, device):
rusty1s's avatar
rusty1s committed
15
16
    # src = tensor([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]], dtype,
    #              device)
rusty1s's avatar
rusty1s committed
17

rusty1s's avatar
rusty1s committed
18
    src = tensor([1, 2, 3, 4, 5, 6], dtype, device)
rusty1s's avatar
rusty1s committed
19

rusty1s's avatar
rusty1s committed
20
    indptr = tensor([0, 2, 5, 5, 6], torch.long, device)
rusty1s's avatar
rusty1s committed
21
    index = tensor([0, 0, 1, 1, 1, 3], torch.long, device)
rusty1s's avatar
rusty1s committed
22
23
    # out = segment_coo(src, index)
    # print('COO', out)
rusty1s's avatar
rusty1s committed
24

rusty1s's avatar
rusty1s committed
25
26
27
28
29
30
31
32
    out = segment_csr(src, indptr, reduce='add')
    print('CSR', out)
    out = segment_csr(src, indptr, reduce='mean')
    print('CSR', out)
    out = segment_csr(src, indptr, reduce='min')
    print('CSR', out)
    out = segment_csr(src, indptr, reduce='max')
    print('CSR', out)
rusty1s's avatar
rusty1s committed
33

rusty1s's avatar
rusty1s committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

# @pytest.mark.parametrize('dtype,device', product(dtypes, devices))
# def test_benchmark(dtype, device):
#     from torch_geometric.datasets import Planetoid, Reddit  # noqa
#     # data = Planetoid('/tmp/Cora', 'Cora')[0].to(device)
#     data = Planetoid('/tmp/PubMed', 'PubMed')[0].to(device)
#     row, col = data.edge_index
#     print(data.num_edges)
#     print(row.size(0) / data.num_nodes)

#     num_repeats = 1
#     row = row.view(-1, 1).repeat(1, num_repeats).view(-1).contiguous()
#     col = col.view(-1, 1).repeat(1, num_repeats).view(-1).contiguous()

#     # Warmup
#     for _ in range(10):
#         torch.randn(100, 100, device=device).sum()

#     x = torch.randn(row.size(0), device=device)

#     torch.cuda.synchronize()
#     t = time.perf_counter()
#     for _ in range(100):
#         out1 = scatter_add(x, row, dim=0, dim_size=data.num_nodes)
#     torch.cuda.synchronize()
#     print('Scatter Row', time.perf_counter() - t)

#     torch.cuda.synchronize()
#     t = time.perf_counter()
#     for _ in range(100):
#         scatter_add(x, col, dim=0, dim_size=data.num_nodes)
#     torch.cuda.synchronize()
#     print('Scatter Col', time.perf_counter() - t)

#     rowcount = segment_add(torch.ones_like(row), row)
#     rowptr = torch.cat([rowcount.new_zeros(1), rowcount.cumsum(0)], dim=0)
#     torch.cuda.synchronize()

#     torch.cuda.synchronize()
#     t = time.perf_counter()
#     for _ in range(100):
#         out3 = segment_add_csr(x, rowptr)
#     torch.cuda.synchronize()
#     print('CSR', time.perf_counter() - t)

#     torch.cuda.synchronize()
#     t = time.perf_counter()
#     for _ in range(100):
#         out4 = segment_add_coo(x, row, dim_size=data.num_nodes)
#     torch.cuda.synchronize()
#     print('COO', time.perf_counter() - t)

#     assert torch.allclose(out1, out3, atol=1e-2)
#     assert torch.allclose(out1, out4, atol=1e-2)

#     x = torch.randn((row.size(0), 64), device=device)

#     torch.cuda.synchronize()
#     t = time.perf_counter()
#     for _ in range(100):
#         out5 = scatter_add(x, row, dim=0, dim_size=data.num_nodes)
#     torch.cuda.synchronize()
#     print('Scatter Row + Dim', time.perf_counter() - t)

#     torch.cuda.synchronize()
#     t = time.perf_counter()
#     for _ in range(100):
#         scatter_add(x, col, dim=0, dim_size=data.num_nodes)
#     torch.cuda.synchronize()
#     print('Scatter Col + Dim', time.perf_counter() - t)

#     torch.cuda.synchronize()
#     t = time.perf_counter()
#     for _ in range(100):
#         out6 = segment_add_csr(x, rowptr)
#     torch.cuda.synchronize()
#     print('CSR + Dim', time.perf_counter() - t)

#     torch.cuda.synchronize()
#     t = time.perf_counter()
#     for _ in range(100):
#         out7 = segment_add_coo(x, row, dim_size=data.num_nodes)
#     torch.cuda.synchronize()
#     print('COO + Dim', time.perf_counter() - t)

#     assert torch.allclose(out5, out6, atol=1e-2)
#     assert torch.allclose(out5, out7, atol=1e-2)