test_degree.py 1 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
import pytest
rusty1s's avatar
tests  
rusty1s committed
2
3
4
5
import torch
from torch_cluster.functions.utils.degree import node_degree


rusty1s's avatar
rusty1s committed
6
def test_node_degree_cpu():
rusty1s's avatar
tests  
rusty1s committed
7
8
9
10
    row = torch.LongTensor([0, 1, 1, 0, 0, 3, 0])
    expected_degree = [4, 2, 0, 1]

    degree = node_degree(row, 4)
rusty1s's avatar
rusty1s committed
11
    assert degree.type() == torch.LongTensor().type()
rusty1s's avatar
tests  
rusty1s committed
12
13
    assert degree.tolist() == expected_degree

rusty1s's avatar
rusty1s committed
14
15
    degree = node_degree(row, 4, out=torch.FloatTensor())
    assert degree.type() == torch.FloatTensor().type()
rusty1s's avatar
tests  
rusty1s committed
16
    assert degree.tolist() == expected_degree
rusty1s's avatar
rusty1s committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30


@pytest.mark.skipif(not torch.cuda.is_available(), reason='no CUDA')
def test_node_degree_gpu():  # pragma: no cover
    row = torch.cuda.LongTensor([0, 1, 1, 0, 0, 3, 0])
    expected_degree = [4, 2, 0, 1]

    degree = node_degree(row, 4)
    assert degree.type() == torch.cuda.LongTensor().type()
    assert degree.cpu().tolist() == expected_degree

    degree = node_degree(row, 4, out=torch.cuda.FloatTensor())
    assert degree.type() == torch.cuda.FloatTensor().type()
    assert degree.cpu().tolist() == expected_degree