-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ershi/emit_assert' into 'main'
Support using assert in kernels See merge request omniverse/warp!915
- Loading branch information
Showing
6 changed files
with
337 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
# Copyright (c) 2024 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.kernel | ||
def expect_ones(a: wp.array(dtype=int)): | ||
i = wp.tid() | ||
|
||
assert a[i] == 1 | ||
|
||
|
||
@wp.kernel | ||
def expect_ones_with_msg(a: wp.array(dtype=int)): | ||
i = wp.tid() | ||
|
||
assert a[i] == 1, "Array element must be 1" | ||
|
||
|
||
@wp.kernel | ||
def expect_ones_compound(a: wp.array(dtype=int)): | ||
i = wp.tid() | ||
|
||
assert a[i] > 0 and a[i] < 2 | ||
|
||
|
||
@wp.func | ||
def expect_ones_function(value: int): | ||
assert value == 1, "Array element must be 1" | ||
|
||
|
||
@wp.kernel | ||
def expect_ones_call_function(a: wp.array(dtype=int)): | ||
i = wp.tid() | ||
expect_ones_function(a[i]) | ||
|
||
|
||
class TestAssertRelease(unittest.TestCase): | ||
"""Assert test cases that are to be run with Warp in release mode.""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls._saved_mode = wp.get_module_options()["mode"] | ||
cls._saved_cache_kernels = wp.config.cache_kernels | ||
|
||
wp.config.mode = "release" | ||
wp.config.cache_kernels = False | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
wp.set_module_options({"mode": cls._saved_mode}) | ||
wp.config.cache_kernels = cls._saved_cache_kernels | ||
|
||
def test_basic_assert_false_condition(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.zeros(1, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
self.assertEqual(output, "", f"Kernel should not print anything to stderr, got {output}") | ||
|
||
def test_basic_assert_with_msg(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.zeros(1, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones_with_msg, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
self.assertEqual(output, "", f"Kernel should not print anything to stderr, got {output}") | ||
|
||
def test_compound_assert_false_condition(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.full(1, value=3, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones_compound, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
self.assertEqual(output, "", f"Kernel should not print anything to stderr, got {output}") | ||
|
||
def test_basic_assert_false_condition_function(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.full(1, value=3, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones_call_function, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
self.assertEqual(output, "", f"Kernel should not print anything to stderr, got {output}") | ||
|
||
|
||
# NOTE: Failed assertions on CUDA devices leaves the CUDA context in an unrecoverable state, | ||
# so we currently do not test them. | ||
class TestAssertDebug(unittest.TestCase): | ||
"""Assert test cases that are to be run with Warp in debug mode.""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls._saved_mode = wp.get_module_options()["mode"] | ||
cls._saved_cache_kernels = wp.config.cache_kernels | ||
|
||
wp.set_module_options({"mode": "debug"}) | ||
wp.config.cache_kernels = False | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
wp.set_module_options({"mode": cls._saved_mode}) | ||
wp.config.cache_kernels = cls._saved_cache_kernels | ||
|
||
def test_basic_assert_false_condition(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.zeros(1, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
# Older Windows C runtimes have a bug where stdout sometimes does not get properly flushed. | ||
if output != "" or sys.platform != "win32": | ||
self.assertRegex(output, r"Assertion failed: .*assert a\[i\] == 1") | ||
|
||
def test_basic_assert_true_condition(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.ones(1, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
self.assertEqual(output, "", f"Kernel should not print anything to stderr, got {output}") | ||
|
||
def test_basic_assert_with_msg(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.zeros(1, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones_with_msg, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
# Older Windows C runtimes have a bug where stdout sometimes does not get properly flushed. | ||
if output != "" or sys.platform != "win32": | ||
self.assertRegex(output, r"Assertion failed: .*assert a\[i\] == 1.*Array element must be 1") | ||
|
||
def test_compound_assert_true_condition(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.ones(1, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones_compound, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
self.assertEqual(output, "", f"Kernel should not print anything to stderr, got {output}") | ||
|
||
def test_compound_assert_false_condition(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.full(1, value=3, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones_compound, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
# Older Windows C runtimes have a bug where stdout sometimes does not get properly flushed. | ||
if output != "" or sys.platform != "win32": | ||
self.assertRegex(output, r"Assertion failed: .*assert a\[i\] > 0 and a\[i\] < 2") | ||
|
||
def test_basic_assert_false_condition_function(self): | ||
with wp.ScopedDevice("cpu"): | ||
wp.load_module(device=wp.get_device()) | ||
|
||
input_array = wp.full(1, value=3, dtype=int) | ||
|
||
capture = StdErrCapture() | ||
capture.begin() | ||
|
||
wp.launch(expect_ones_call_function, input_array.shape, inputs=[input_array]) | ||
|
||
output = capture.end() | ||
|
||
# Older Windows C runtimes have a bug where stdout sometimes does not get properly flushed. | ||
if output != "" or sys.platform != "win32": | ||
self.assertRegex(output, r"Assertion failed: .*assert value == 1.*Array element must be 1") | ||
|
||
|
||
if __name__ == "__main__": | ||
wp.clear_kernel_cache() | ||
unittest.main(verbosity=2) |
Oops, something went wrong.