Skip to content

Commit a0f1507

Browse files
committed
initial version of testing ObjectCode.from_library
1 parent dad6a42 commit a0f1507

4 files changed

Lines changed: 100 additions & 2 deletions

File tree

‎.github/workflows/build-wheel.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,10 @@ jobs:
469469
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
470470
with:
471471
name: ${{ env.CUDA_CORE_ARTIFACT_NAME }}-test-binaries
472-
path: ${{ env.CUDA_CORE_TEST_BINARIES_DIR }}/*.o
472+
path: |
473+
${{ env.CUDA_CORE_TEST_BINARIES_DIR }}/*.o
474+
${{ env.CUDA_CORE_TEST_BINARIES_DIR }}/*.a
475+
${{ env.CUDA_CORE_TEST_BINARIES_DIR }}/*.lib
473476
if-no-files-found: error
474477

475478
- name: Download cuda.bindings build artifacts from the prior branch

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ cache_nvrtc
2121

2222
# cuda.core test object fixtures built locally / downloaded as CI artifacts
2323
cuda_core/tests/test_binaries/*.o
24+
cuda_core/tests/test_binaries/*.a
25+
cuda_core/tests/test_binaries/*.lib
2426

2527
# CUDA Python specific (auto-generated)
2628
cuda_bindings/cuda/bindings/_bindings/cyruntime.pxd

‎cuda_core/tests/test_binaries/build_test_binaries.sh‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ fi
1717
nvcc -dc "${NVCC_EXTRA_FLAGS[@]}" -arch=all-major \
1818
-o "${SCRIPTPATH}/saxpy.o" "${SCRIPTPATH}/saxpy.cu"
1919

20-
ls -lah "${SCRIPTPATH}/saxpy.o"
20+
if [[ "${OS:-}" == "Windows_NT" ]]; then
21+
nvcc -lib -o "${SCRIPTPATH}/saxpy.lib" "${SCRIPTPATH}/saxpy.o"
22+
ls -lah "${SCRIPTPATH}/saxpy.o" "${SCRIPTPATH}/saxpy.lib"
23+
else
24+
nvcc -lib -o "${SCRIPTPATH}/saxpy.a" "${SCRIPTPATH}/saxpy.o"
25+
ls -lah "${SCRIPTPATH}/saxpy.o" "${SCRIPTPATH}/saxpy.a"
26+
fi

‎cuda_core/tests/test_module.py‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,33 @@ def get_saxpy_object():
203203
return obj_path.read_bytes()
204204

205205

206+
@pytest.fixture(scope="module")
207+
def get_saxpy_library():
208+
"""Read the pre-built saxpy.a (Linux) or saxpy.lib (Windows).
209+
210+
In CI: produced by build stage alongside saxpy.o.
211+
In local dev: auto-built on demand if nvcc is available; if you edit
212+
saxpy.cu, remove stale saxpy.o / saxpy.a to force a rebuild.
213+
"""
214+
binaries_dir = Path(__file__).parent / "test_binaries"
215+
lib_name = "saxpy.lib" if os.name == "nt" else "saxpy.a"
216+
lib_path = binaries_dir / lib_name
217+
218+
if not lib_path.is_file():
219+
if find_nvidia_binary_utility("nvcc") is None:
220+
pytest.skip(
221+
f"{lib_name} not found at {lib_path} and nvcc is unavailable. "
222+
"In CI this is downloaded from the build stage."
223+
)
224+
subprocess.run( # noqa: S603
225+
["bash", str(binaries_dir / "build_test_binaries.sh")], # noqa: S607
226+
check=True,
227+
env=os.environ,
228+
)
229+
230+
return lib_path.read_bytes()
231+
232+
206233
def test_get_kernel(init_cuda):
207234
kernel = """extern "C" __global__ void ABC() { }"""
208235

@@ -420,6 +447,66 @@ def test_object_code_load_object_with_linker(get_saxpy_object, init_cuda):
420447
assert result[0] == 10.0
421448

422449

450+
def test_object_code_load_library(get_saxpy_library):
451+
lib = get_saxpy_library
452+
assert isinstance(lib, bytes)
453+
mod_obj = ObjectCode.from_library(lib)
454+
assert mod_obj.code == lib
455+
assert mod_obj.code_type == "library"
456+
with pytest.raises(RuntimeError, match=r'Unsupported code type "library"'):
457+
mod_obj.get_kernel("saxpy<float>")
458+
459+
460+
def test_object_code_load_library_from_file(get_saxpy_library, tmp_path):
461+
lib_ext = ".lib" if os.name == "nt" else ".a"
462+
lib_file = tmp_path / f"test{lib_ext}"
463+
lib_file.write_bytes(get_saxpy_library)
464+
arg = str(lib_file)
465+
mod_obj = ObjectCode.from_library(arg)
466+
assert mod_obj.code == arg
467+
assert mod_obj.code_type == "library"
468+
469+
470+
def test_object_code_load_library_with_linker(get_saxpy_library, init_cuda):
471+
arch = f"sm_{init_cuda.arch}"
472+
kernel_code = Program(
473+
r"""
474+
extern __device__ float saxpy_step(float a, float x, float y);
475+
extern "C" __global__ void linked_kernel(float a, float x, float y, float* out) {
476+
if (threadIdx.x == 0 && blockIdx.x == 0) *out = saxpy_step(a, x, y);
477+
}
478+
""",
479+
"c++",
480+
ProgramOptions(relocatable_device_code=True, arch=arch),
481+
).compile("cubin")
482+
linked = Linker(
483+
kernel_code,
484+
ObjectCode.from_library(get_saxpy_library),
485+
options=LinkerOptions(arch=arch),
486+
).link("cubin")
487+
kernel = linked.get_kernel("linked_kernel")
488+
489+
stream = init_cuda.create_stream()
490+
host_buf = cuda.core.LegacyPinnedMemoryResource().allocate(4)
491+
result = np.from_dlpack(host_buf).view(np.float32)
492+
result[:] = 0.0
493+
dev_buf = init_cuda.memory_resource.allocate(4, stream=init_cuda.default_stream)
494+
495+
cuda.core.launch(
496+
stream,
497+
cuda.core.LaunchConfig(grid=1, block=1),
498+
kernel,
499+
np.float32(2.0),
500+
np.float32(3.0),
501+
np.float32(4.0),
502+
dev_buf,
503+
)
504+
dev_buf.copy_to(host_buf, stream=stream)
505+
stream.sync()
506+
507+
assert result[0] == 10.0
508+
509+
423510
def test_saxpy_arguments(get_saxpy_kernel_cubin, cuda12_4_prerequisite_check):
424511
krn, _ = get_saxpy_kernel_cubin
425512

0 commit comments

Comments
 (0)