-
Notifications
You must be signed in to change notification settings - Fork 297
cuda.core: add tests for ObjectCode.from_object #2193
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #!/bin/bash | ||
|
|
||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Build .o test fixtures. Invoked at CI build stage | ||
|
|
||
| SCRIPTPATH=$(dirname "$(realpath "$0")") | ||
|
|
||
| nvcc -dc -o "${SCRIPTPATH}/saxpy.o" "${SCRIPTPATH}/saxpy.cu" | ||
|
|
||
| ls -lah "${SCRIPTPATH}/saxpy.o" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| template<typename T> | ||
| __global__ void saxpy(const T a, const T* x, const T* y, T* out, size_t N) { | ||
| const unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x; | ||
| for (size_t i = tid; i < N; i += gridDim.x * blockDim.x) { | ||
| out[tid] = a * x[tid] + y[tid]; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,6 +172,35 @@ def get_saxpy_fatbin(init_cuda): | |
| return bytes(fatbin), sym_map | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def get_saxpy_object(): | ||
| """Read the pre-built saxpy.o. | ||
|
|
||
| In CI: produced by build stage into a test wheel file. | ||
| In local dev: auto-built on demand if nvcc is available; if you edit | ||
| saxpy.cu, remove the stale saxpy.o to force a rebuild. | ||
| """ | ||
| import shutil | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| binaries_dir = Path(__file__).parent / "test_binaries" | ||
| obj_path = binaries_dir / "saxpy.o" | ||
|
|
||
| if not obj_path.is_file(): | ||
| if shutil.which("nvcc") is None: | ||
| pytest.skip( | ||
| f"saxpy.o not found at {obj_path} and nvcc is unavailable. " | ||
| "In CI this is downloaded from the build stage." | ||
| ) | ||
|
Comment on lines
+191
to
+195
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| subprocess.run( # noqa: S603 | ||
| ["bash", str(binaries_dir / "build_test_binaries.sh")], # noqa: S607 | ||
| check=True, | ||
| ) | ||
|
|
||
| return obj_path.read_bytes() | ||
|
|
||
|
|
||
| def test_get_kernel(init_cuda): | ||
| kernel = """extern "C" __global__ void ABC() { }""" | ||
|
|
||
|
|
@@ -330,6 +359,26 @@ def test_object_code_load_fatbin_from_file(get_saxpy_fatbin, tmp_path, convert_p | |
| mod_obj.get_kernel("saxpy<double>") # force loading | ||
|
|
||
|
|
||
| def test_object_code_load_object(get_saxpy_object): | ||
| obj = get_saxpy_object | ||
| assert isinstance(obj, bytes) | ||
| mod_obj = ObjectCode.from_object(obj) | ||
| assert mod_obj.code == obj | ||
| assert mod_obj.code_type == "object" | ||
| # object code is only valid as linker input; get_kernel is unsupported | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: Is it possible to test this? Maybe write 1 kernel in Python and 1 device function in C++, and link them together? |
||
| with pytest.raises(RuntimeError, match=r'Unsupported code type "object"'): | ||
| mod_obj.get_kernel("saxpy<float>") | ||
|
|
||
|
|
||
| def test_object_code_load_object_from_file(get_saxpy_object, tmp_path): | ||
| obj_file = tmp_path / "test.o" | ||
| obj_file.write_bytes(get_saxpy_object) | ||
| arg = str(obj_file) | ||
| mod_obj = ObjectCode.from_object(arg) | ||
| assert mod_obj.code == arg | ||
| assert mod_obj.code_type == "object" | ||
|
|
||
|
|
||
| def test_saxpy_arguments(get_saxpy_kernel_cubin, cuda12_4_prerequisite_check): | ||
| krn, _ = get_saxpy_kernel_cubin | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is device code specific, let's use
cuda/std/cstddef.