@@ -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+
206233def 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+
423510def test_saxpy_arguments (get_saxpy_kernel_cubin , cuda12_4_prerequisite_check ):
424511 krn , _ = get_saxpy_kernel_cubin
425512
0 commit comments