55import weakref
66from dataclasses import dataclass
77from typing import List , Optional , Tuple , Union
8+ from warnings import warn
89
910from cuda .core .experimental ._device import Device
1011from cuda .core .experimental ._linker import Linker , LinkerOptions
1112from cuda .core .experimental ._module import ObjectCode
1213from cuda .core .experimental ._utils import (
1314 _handle_boolean_option ,
1415 check_or_create_options ,
16+ driver ,
1517 handle_return ,
1618 is_nested_sequence ,
1719 is_sequence ,
@@ -378,6 +380,7 @@ def __init__(self, code, code_type, options: ProgramOptions = None):
378380 raise TypeError ("c++ Program expects code argument to be a string" )
379381 # TODO: support pre-loaded headers & include names
380382 # TODO: allow tuples once NVIDIA/cuda-python#72 is resolved
383+
381384 self ._mnff .handle = handle_return (nvrtc .nvrtcCreateProgram (code .encode (), b"" , 0 , [], []))
382385 self ._backend = "nvrtc"
383386 self ._linker = None
@@ -414,6 +417,11 @@ def close(self):
414417 self ._linker .close ()
415418 self ._mnff .close ()
416419
420+ def _can_load_generated_ptx (self ):
421+ driver_ver = handle_return (driver .cuDriverGetVersion ())
422+ nvrtc_major , nvrtc_minor = handle_return (nvrtc .nvrtcVersion ())
423+ return nvrtc_major * 1000 + nvrtc_minor * 10 <= driver_ver
424+
417425 def compile (self , target_type , name_expressions = (), logs = None ):
418426 """Compile the program with a specific compilation type.
419427
@@ -440,6 +448,13 @@ def compile(self, target_type, name_expressions=(), logs=None):
440448 raise NotImplementedError
441449
442450 if self ._backend == "nvrtc" :
451+ if target_type == "ptx" and not self ._can_load_generated_ptx ():
452+ warn (
453+ "The CUDA driver version is older than the backend version. "
454+ "The generated ptx will not be loadable by the current driver." ,
455+ stacklevel = 1 ,
456+ category = RuntimeWarning ,
457+ )
443458 if name_expressions :
444459 for n in name_expressions :
445460 handle_return (
0 commit comments