-
Notifications
You must be signed in to change notification settings - Fork 57
CUDA: add closure tests\n\nAdds kernel-based tests for device-side cl… #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CodersAcademy006
wants to merge
5
commits into
NVIDIA:main
Choose a base branch
from
CodersAcademy006:cuda-closure-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c31c494
CUDA: add closure tests\n\nAdds kernel-based tests for device-side cl…
CodersAcademy006 7dc1fab
Update numba_cuda/numba/cuda/tests/test_closure.py
CodersAcademy006 34d5222
Update numba_cuda/numba/cuda/tests/test_closure.py
CodersAcademy006 20f47b5
Update numba_cuda/numba/cuda/tests/test_closure.py
CodersAcademy006 905efbf
Update numba_cuda/numba/cuda/tests/test_closure.py
CodersAcademy006 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,110 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: BSD-2-Clause | ||
|
|
||
| import numpy as np | ||
|
|
||
| from numba import cuda | ||
| from numba.cuda.testing import CUDATestCase, skip_on_cudasim | ||
|
|
||
| # ----------------------------- | ||
| # Kernel factories with closures | ||
| # ----------------------------- | ||
|
|
||
| def make_add_kernel(const): | ||
| @cuda.jit | ||
| def kernel(inp, out): | ||
| i = cuda.grid(1) | ||
| if i < inp.size: | ||
| out[i] = inp[i] + const | ||
| return kernel | ||
|
|
||
| def make_mul_kernel(factor): | ||
| @cuda.jit | ||
| def kernel(inp, out): | ||
| i = cuda.grid(1) | ||
| if i < inp.size: | ||
| out[i] = inp[i] * factor | ||
| return kernel | ||
|
|
||
| def make_bool_closure_kernel(flag): | ||
| @cuda.jit | ||
| def kernel(out): | ||
| i = cuda.grid(1) | ||
| if i < out.size: | ||
| if flag: | ||
| out[i] = 1 | ||
| else: | ||
| out[i] = 0 | ||
| return kernel | ||
|
|
||
| def make_nested_constant_kernel(): | ||
| x = 3 | ||
| y = 4 | ||
| @cuda.jit | ||
| def kernel(out): | ||
| i = cuda.grid(1) | ||
| if i < out.size: | ||
| out[i] = x + y | ||
| return kernel | ||
|
|
||
| @skip_on_cudasim("Closure capture semantics differ under cudasim") | ||
| class TestCudaClosure(CUDATestCase): | ||
|
|
||
| def _launch_1d(self, kernel, args, size): | ||
| threadsperblock = 128 | ||
| blockspergrid = (size + threadsperblock - 1) // threadsperblock | ||
| kernel[blockspergrid, threadsperblock](*args) | ||
| cuda.synchronize() | ||
|
|
||
| def test_closure_add_constant(self): | ||
| add5 = make_add_kernel(5) | ||
| inp = np.arange(10, dtype=np.int32) | ||
| out = np.zeros_like(inp) | ||
| d_inp = cuda.to_device(inp) | ||
| d_out = cuda.to_device(out) | ||
| self._launch_1d(add5, (d_inp, d_out), inp.size) | ||
| np.testing.assert_array_equal( | ||
| d_out.copy_to_host(), | ||
| inp + 5, | ||
| ) | ||
|
|
||
| def test_closure_multiply_constant(self): | ||
| mul3 = make_mul_kernel(3) | ||
| inp = np.array([1, 2, 3, 4], dtype=np.int32) | ||
| out = np.zeros_like(inp) | ||
| d_inp = cuda.to_device(inp) | ||
| d_out = cuda.to_device(out) | ||
| self._launch_1d(mul3, (d_inp, d_out), inp.size) | ||
| np.testing.assert_array_equal( | ||
| d_out.copy_to_host(), | ||
| inp * 3, | ||
| ) | ||
|
|
||
| def test_bool_closure_true(self): | ||
| kernel = make_bool_closure_kernel(True) | ||
| out = np.zeros(6, dtype=np.int32) | ||
| d_out = cuda.to_device(out) | ||
| self._launch_1d(kernel, (d_out,), out.size) | ||
| np.testing.assert_array_equal( | ||
| d_out.copy_to_host(), | ||
| np.ones_like(out), | ||
| ) | ||
|
|
||
| def test_bool_closure_false(self): | ||
| out = np.ones(6, dtype=np.int32) | ||
| d_out = cuda.to_device(out) | ||
| self._launch_1d(kernel, (d_out,), out.size) | ||
CodersAcademy006 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| np.testing.assert_array_equal( | ||
| d_out.copy_to_host(), | ||
| np.zeros_like(out), | ||
| ) | ||
|
|
||
| def test_multiple_constant_capture(self): | ||
| kernel = make_nested_constant_kernel() | ||
| out = np.zeros(8, dtype=np.int32) | ||
| d_out = cuda.to_device(out) | ||
| self._launch_1d(kernel, (d_out,), out.size) | ||
| np.testing.assert_array_equal( | ||
| d_out.copy_to_host(), | ||
| np.full_like(out, 7), | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.