File size: 1,564 Bytes
66c9c8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright (c) 2022 NVIDIA CORPORATION.  All rights reserved.
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.

import unittest

import warp as wp
from warp.tests.unittest_utils import *

wp.init()


@wp.kernel
def test_pow(x: float, e: float, result: float):
    tid = wp.tid()

    y = wp.pow(-2.0, e)

    wp.expect_eq(y, result)


def test_fast_math(test, device):
    # on all systems pow() should handle negative base correctly
    wp.set_module_options({"fast_math": False})
    wp.launch(test_pow, dim=1, inputs=[-2.0, 2.0, 4.0], device=device)

    # on CUDA with --fast-math enabled taking the pow()
    # of a negative number will result in a NaN
    if wp.get_device(device).is_cuda:
        wp.set_module_options({"fast_math": True})

        with test.assertRaises(Exception):
            with CheckOutput():
                wp.launch(test_pow, dim=1, inputs=[-2.0, 2.0, 2.0], device=device)

        # Turn fast math back off
        wp.set_module_options({"fast_math": False})


class TestFastMath(unittest.TestCase):
    pass


devices = get_test_devices()

add_function_test(TestFastMath, "test_fast_math", test_fast_math, devices=devices)


if __name__ == "__main__":
    wp.build.clear_kernel_cache()
    unittest.main(verbosity=2)