-
Notifications
You must be signed in to change notification settings - Fork 57
CUDA: Add device-side tests for Python builtins (abs, min, max, bool)… #729
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
11
commits into
NVIDIA:main
Choose a base branch
from
CodersAcademy006:cuda-builtins-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 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dfbc979
CUDA: Add device-side tests for Python builtins (abs, min, max, bool)…
CodersAcademy006 b181d29
Update numba_cuda/numba/cuda/tests/test_builtins.py
CodersAcademy006 16aed6f
Update numba_cuda/numba/cuda/tests/test_builtins.py
CodersAcademy006 22f73d4
Update numba_cuda/numba/cuda/tests/test_builtins.py
CodersAcademy006 257ce6a
Update numba_cuda/numba/cuda/tests/test_builtins.py
CodersAcademy006 73f8512
Update numba_cuda/numba/cuda/tests/test_builtins.py
CodersAcademy006 6481fe2
Update numba_cuda/numba/cuda/tests/test_builtins.py
CodersAcademy006 773f1a5
Optimize test_builtins.py: fix anti-patterns and add edge cases
CodersAcademy006 3f7e60e
Fix duplicate test_abs_int and simplify min/max kernels\n\n- Remove d…
CodersAcademy006 8ea22b2
style: Remove extra blank lines between test methods for consistency
CodersAcademy006 0366753
Update numba_cuda/numba/cuda/tests/test_builtins.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,109 @@ | ||
| # 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 | ||
|
|
||
|
|
||
| @cuda.jit | ||
| def abs_kernel(inp, out): | ||
| i = cuda.grid(1) | ||
| if i < inp.size: | ||
CodersAcademy006 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| out[i] = abs(inp[i]) | ||
|
|
||
|
|
||
| @cuda.jit | ||
| def min_kernel(a, b, out): | ||
| i = cuda.grid(1) | ||
| if i < min(a.size, b.size, out.size): | ||
| out[i] = min(a[i], b[i]) | ||
|
|
||
|
|
||
| @cuda.jit | ||
| def max_kernel(a, b, out): | ||
| i = cuda.grid(1) | ||
| if i < out.size: | ||
| out[i] = max(a[i], b[i]) | ||
CodersAcademy006 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @cuda.jit | ||
| def bool_kernel(inp, out): | ||
| i = cuda.grid(1) | ||
| if i < out.size: | ||
| out[i] = bool(inp[i]) | ||
|
|
||
|
|
||
| @skip_on_cudasim("Builtins semantics differ under cudasim") | ||
| class TestCudaBuiltins(CUDATestCase): | ||
|
|
||
| def _launch_1d(self, kernel, args, size): | ||
| threadsperblock = 128 | ||
| blockspergrid = (size + threadsperblock - 1) // threadsperblock | ||
| kernel[blockspergrid, threadsperblock](*args) | ||
| cuda.synchronize() | ||
|
|
||
CodersAcademy006 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def test_abs_int(self): | ||
| src = np.array([-1, 2, -3, 4], dtype=np.int32) | ||
| dst = np.zeros_like(src) | ||
|
|
||
| d_src = cuda.to_device(src) | ||
| d_dst = cuda.to_device(dst) | ||
|
|
||
| self._launch_1d(abs_kernel, (d_src, d_dst), src.size) | ||
|
|
||
| np.testing.assert_array_equal(d_dst.copy_to_host(), np.abs(src)) | ||
|
|
||
|
|
||
CodersAcademy006 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def test_abs_float(self): | ||
| src = np.array([-1.5, 2.0, -3.25], dtype=np.float32) | ||
| dst = np.zeros_like(src) | ||
|
|
||
| d_src = cuda.to_device(src) | ||
| d_dst = cuda.to_device(dst) | ||
|
|
||
| self._launch_1d(abs_kernel, (d_src, d_dst), src.size) | ||
|
|
||
| np.testing.assert_array_equal(d_dst.copy_to_host(), np.abs(src)) | ||
|
|
||
|
|
||
CodersAcademy006 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def test_min(self): | ||
| a = np.array([1, 5, 3, 7], dtype=np.int32) | ||
| b = np.array([2, 4, 6, 0], dtype=np.int32) | ||
| out = np.zeros_like(a) | ||
|
|
||
| da = cuda.to_device(a) | ||
| db = cuda.to_device(b) | ||
| dout = cuda.to_device(out) | ||
|
|
||
| self._launch_1d(min_kernel, (da, db, dout), a.size) | ||
|
|
||
| np.testing.assert_array_equal(dout.copy_to_host(), np.minimum(a, b)) | ||
CodersAcademy006 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
CodersAcademy006 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def test_max(self): | ||
| a = np.array([1, 5, 3, 7], dtype=np.int32) | ||
| b = np.array([2, 4, 6, 0], dtype=np.int32) | ||
| out = np.zeros_like(a) | ||
|
|
||
| da = cuda.to_device(a) | ||
| db = cuda.to_device(b) | ||
| dout = cuda.to_device(out) | ||
|
|
||
| self._launch_1d(max_kernel, (da, db, dout), a.size) | ||
|
|
||
| np.testing.assert_array_equal(dout.copy_to_host(), np.maximum(a, b)) | ||
|
|
||
|
|
||
CodersAcademy006 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def test_bool(self): | ||
| src = np.array([0, 1, -1, 3], dtype=np.int32) | ||
| dst = np.zeros(src.size, dtype=np.bool_) | ||
|
|
||
| d_src = cuda.to_device(src) | ||
| d_dst = cuda.to_device(dst) | ||
|
|
||
| self._launch_1d(bool_kernel, (d_src, d_dst), src.size) | ||
|
|
||
| np.testing.assert_array_equal(d_dst.copy_to_host(), src.astype(bool)) | ||
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.