test_contraction.py 2.7 KB
Newer Older
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
1
2
3
import pytest
import torch

4
5
import nerfacc.cuda as _C
from nerfacc import ContractionType, contract, contract_inv
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
6
7

device = "cuda:0"
8
9
10
11
12
13
14
15
16
17
18
19
batch_size = 32
eps = 1e-6


@pytest.mark.skipif(not torch.cuda.is_available, reason="No CUDA device")
def test_ContractionType():
    ctype = ContractionType.AABB.to_cpp_version()
    assert ctype == _C.ContractionTypeGetter(0)
    ctype = ContractionType.UN_BOUNDED_TANH.to_cpp_version()
    assert ctype == _C.ContractionTypeGetter(1)
    ctype = ContractionType.UN_BOUNDED_SPHERE.to_cpp_version()
    assert ctype == _C.ContractionTypeGetter(2)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
20
21
22
23


@pytest.mark.skipif(not torch.cuda.is_available, reason="No CUDA device")
def test_identity():
24
    x = torch.rand([batch_size, 3], device=device)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
25
    roi = torch.tensor([0, 0, 0, 1, 1, 1], dtype=torch.float32, device=device)
26
27
28
29
    x_out = contract(x, roi=roi, type=ContractionType.AABB)
    assert torch.allclose(x_out, x, atol=eps)
    x_inv = contract_inv(x_out, roi=roi, type=ContractionType.AABB)
    assert torch.allclose(x_inv, x, atol=eps)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
30
31
32


@pytest.mark.skipif(not torch.cuda.is_available, reason="No CUDA device")
33
34
def test_aabb():
    x = torch.rand([batch_size, 3], device=device)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
35
36
37
    roi = torch.tensor(
        [-1, -1, -1, 1, 1, 1], dtype=torch.float32, device=device
    )
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    x_out = contract(x, roi=roi, type=ContractionType.AABB)
    x_out_tgt = x * 0.5 + 0.5
    assert torch.allclose(x_out, x_out_tgt, atol=eps)
    x_inv = contract_inv(x_out, roi=roi, type=ContractionType.AABB)
    assert torch.allclose(x_inv, x, atol=eps)


@pytest.mark.skipif(not torch.cuda.is_available, reason="No CUDA device")
def test_tanh():
    x = torch.randn([batch_size, 3], device=device)
    roi = torch.tensor(
        [-0.2, -0.3, -0.4, 0.7, 0.8, 0.6], dtype=torch.float32, device=device
    )
    x_out = contract(x, roi=roi, type=ContractionType.UN_BOUNDED_TANH)
    x_out_tgt = (
        torch.tanh((x - roi[:3]) / (roi[3:] - roi[:3]) - 0.5) * 0.5 + 0.5
    )
    assert torch.allclose(x_out, x_out_tgt, atol=eps)
    x_inv = contract_inv(x_out, roi=roi, type=ContractionType.UN_BOUNDED_TANH)
    assert torch.allclose(x_inv, x, atol=eps)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
58
59
60


@pytest.mark.skipif(not torch.cuda.is_available, reason="No CUDA device")
61
62
def test_sphere():
    x = torch.randn([batch_size, 3], device=device)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
63
    roi = torch.tensor(
64
        [-0.2, -0.3, -0.4, 0.7, 0.8, 0.6], dtype=torch.float32, device=device
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
65
    )
66
67
68
69
    x_out = contract(x, roi=roi, type=ContractionType.UN_BOUNDED_SPHERE)
    assert ((x_out - 0.5).norm(dim=-1) < 0.5).all()
    x_inv = contract_inv(x_out, roi=roi, type=ContractionType.UN_BOUNDED_SPHERE)
    assert torch.allclose(x_inv, x, atol=eps)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
70
71
72


if __name__ == "__main__":
73
    test_ContractionType()
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
74
    test_identity()
75
76
77
    test_aabb()
    test_tanh()
    test_sphere()