test_fps.py 1.73 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_cluster import fps
rusty1s's avatar
rusty1s committed
6

rusty1s's avatar
rusty1s committed
7
from .utils import tensor, grad_dtypes
rusty1s's avatar
rusty1s committed
8
9
10
11

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


rusty1s's avatar
rusty1s committed
12
@pytest.mark.skipif(not torch.cuda.is_available(), reason='CUDA not available')
rusty1s's avatar
rusty1s committed
13
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
rusty1s's avatar
rusty1s committed
14
def test_fps(dtype, device):
rusty1s's avatar
rusty1s committed
15
16
17
18
19
20
21
22
23
24
    x = tensor([
        [-1, -1],
        [-1, +1],
        [+1, +1],
        [+1, -1],
        [-2, -2],
        [-2, +2],
        [+2, +2],
        [+2, -2],
    ], dtype, device)
rusty1s's avatar
rusty1s committed
25
26
    batch = tensor([0, 0, 0, 0, 1, 1, 1, 1], torch.long, device)

rusty1s's avatar
rusty1s committed
27
28
29
30
    out = fps(x, batch, ratio=0.5, random_start=False)
    assert out.tolist() == [0, 2, 4, 6]


rusty1s's avatar
rusty1s committed
31
@pytest.mark.skipif(not torch.cuda.is_available(), reason='CUDA not available')
rusty1s's avatar
rusty1s committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
def test_fps_speed(dtype, device):
    return
    batch_size, num_nodes = 100, 10000
    x = torch.randn((batch_size * num_nodes, 3), dtype=dtype, device=device)
    batch = torch.arange(batch_size, dtype=torch.long, device=device)
    batch = batch.view(-1, 1).repeat(1, num_nodes).view(-1)

    out = fps(x, batch, ratio=0.5, random_start=True)
    assert out.size(0) == batch_size * num_nodes * 0.5
    assert out.min().item() >= 0 and out.max().item() < batch_size * num_nodes

    batch_size, num_nodes, dim = 100, 300, 128
    x = torch.randn((batch_size * num_nodes, dim), dtype=dtype, device=device)
    batch = torch.arange(batch_size, dtype=torch.long, device=device)
    batch = batch.view(-1, 1).repeat(1, num_nodes).view(-1)
    out = fps(x, batch, ratio=0.5, random_start=True)
    assert out.size(0) == batch_size * num_nodes * 0.5
    assert out.min().item() >= 0 and out.max().item() < batch_size * num_nodes