test_softmax.py 1.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
from itertools import product

import numpy as np
import pytest
import torch
from torch_scatter.composite import scatter_log_softmax, scatter_softmax

from .utils import devices, tensor

SUPPORTED_FLOAT_DTYPES = {torch.float32, torch.float64}

12

13
14
@pytest.mark.parametrize('dtype,device',
                         product(SUPPORTED_FLOAT_DTYPES, devices))
15
def test_log_softmax(dtype, device):
16
17
    src = tensor([0.25, 0, 0.25, -2.1, 3.2, 7, -1, float('-inf')],
                 dtype, device)
18
19
20
21
22
23
    index = tensor([0, 1, 0, 1, 1, 2, 4, 4], torch.long, device)

    out = scatter_log_softmax(src, index)

    # Expected results per index
    idx0 = [np.log(0.5), np.log(0.5)]
24
25
26
    idx1 = torch.log_softmax(
        torch.tensor([0.0, -2.1, 3.2], dtype=dtype),
        dim=-1).tolist()
27
28
29
30
31
32
33
34
35
36
37
    idx2 = 0.0   # Single element, has logprob=0
    # index=3 is empty. Should not matter.
    idx4 = [0.0, float('-inf')]   # log_softmax with -inf preserves the -inf

    np.testing.assert_allclose(
        out.tolist(),
        [idx0[0], idx1[0], idx0[1], idx1[1], idx1[2], idx2, idx4[0], idx4[1]],
        rtol=1e-05, atol=1e-10
        )


38
39
@pytest.mark.parametrize('dtype,device',
                         product(SUPPORTED_FLOAT_DTYPES, devices))
40
def test_softmax(dtype, device):
41
42
    src = tensor([0.25, 0, 0.25, -2.1, 3.2, 7, -1, float('-inf')],
                 dtype, device)
43
44
45
46
47
48
    index = tensor([0, 1, 0, 1, 1, 2, 4, 4], torch.long, device)

    out = scatter_softmax(src, index)

    # Expected results per index
    idx0 = [0.5, 0.5]
49
50
51
    idx1 = torch.softmax(
        torch.tensor([0.0, -2.1, 3.2], dtype=dtype),
        dim=-1).tolist()
52
53
54
55
56
57
58
59
    idx2 = 1   # Single element, has prob=1
    # index=3 is empty. Should not matter.
    idx4 = [1.0, 0.0]   # softmax with -inf yields zero probability

    np.testing.assert_allclose(
        out.tolist(),
        [idx0[0], idx1[0], idx0[1], idx1[1], idx1[2], idx2, idx4[0], idx4[1]],
        rtol=1e-05, atol=1e-10
60
        )