55import numpy as np
66import pytest
77
8+ try :
9+ from cuda .bindings import driver
10+ except ImportError :
11+ from cuda import cuda as driver
812from cuda .core .experimental import (
913 CompleteOptions ,
1014 DebugPrintOptions ,
1721 launch_graph ,
1822)
1923from cuda .core .experimental ._memory import _DefaultPinnedMemorySource
24+ from cuda .core .experimental ._utils .cuda_utils import NVRTCError , handle_return
2025
2126
2227def _common_kernels ():
28+ code = """
29+ __global__ void empty_kernel() {}
30+ __global__ void add_one(int *a) { *a += 1; }
31+ """
32+ arch = "" .join (f"{ i } " for i in Device ().compute_capability )
33+ program_options = ProgramOptions (std = "c++17" , arch = f"sm_{ arch } " )
34+ prog = Program (code , code_type = "c++" , options = program_options )
35+ mod = prog .compile ("cubin" , name_expressions = ("empty_kernel" , "add_one" ))
36+ return mod
37+
38+
39+ def _common_kernels_conditional ():
2340 code = """
2441 extern "C" __device__ __cudart_builtin__ void CUDARTAPI cudaGraphSetConditional(cudaGraphConditionalHandle handle,
2542 unsigned int value);
@@ -35,7 +52,13 @@ def _common_kernels():
3552 arch = "" .join (f"{ i } " for i in Device ().compute_capability )
3653 program_options = ProgramOptions (std = "c++17" , arch = f"sm_{ arch } " )
3754 prog = Program (code , code_type = "c++" , options = program_options )
38- mod = prog .compile ("cubin" , name_expressions = ("empty_kernel" , "add_one" , "set_handle" , "loop_kernel" ))
55+ try :
56+ mod = prog .compile ("cubin" , name_expressions = ("empty_kernel" , "add_one" , "set_handle" , "loop_kernel" ))
57+ except NVRTCError as e :
58+ with pytest .raises (RuntimeError , match = 'error: identifier "cudaGraphConditionalHandle" is undefined' ):
59+ raise e
60+ nvrtcVersion = handle_return (driver .nvrtcVersion ())
61+ pytest .skip (f"NVRTC version { nvrtcVersion } does not support conditionals" )
3962 return mod
4063
4164
@@ -186,7 +209,7 @@ def test_graph_capture_errors(init_cuda):
186209@pytest .mark .parametrize ("condition_value" , [True , False ])
187210@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
188211def test_graph_conditional_if (init_cuda , condition_value ):
189- mod = _common_kernels ()
212+ mod = _common_kernels_conditional ()
190213 add_one = mod .get_kernel ("add_one" )
191214 set_handle = mod .get_kernel ("set_handle" )
192215
@@ -237,7 +260,7 @@ def test_graph_conditional_if(init_cuda, condition_value):
237260@pytest .mark .parametrize ("condition_value" , [True , False ])
238261@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
239262def test_graph_conditional_if_else (init_cuda , condition_value ):
240- mod = _common_kernels ()
263+ mod = _common_kernels_conditional ()
241264 add_one = mod .get_kernel ("add_one" )
242265 set_handle = mod .get_kernel ("set_handle" )
243266
@@ -304,7 +327,7 @@ def test_graph_conditional_if_else(init_cuda, condition_value):
304327@pytest .mark .parametrize ("condition_value" , [0 , 1 , 2 , 3 ])
305328@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
306329def test_graph_conditional_switch (init_cuda , condition_value ):
307- mod = _common_kernels ()
330+ mod = _common_kernels_conditional ()
308331 add_one = mod .get_kernel ("add_one" )
309332 set_handle = mod .get_kernel ("set_handle" )
310333
@@ -390,7 +413,7 @@ def test_graph_conditional_switch(init_cuda, condition_value):
390413@pytest .mark .parametrize ("condition_value" , [True , False ])
391414@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
392415def test_graph_conditional_while (init_cuda , condition_value ):
393- mod = _common_kernels ()
416+ mod = _common_kernels_conditional ()
394417 add_one = mod .get_kernel ("add_one" )
395418 loop_kernel = mod .get_kernel ("loop_kernel" )
396419 empty_kernel = mod .get_kernel ("empty_kernel" )
@@ -457,7 +480,16 @@ def test_graph_child_graph(init_cuda):
457480 launch (gb_parent , LaunchConfig (grid = 1 , block = 1 ), add_one , arr .ctypes .data )
458481
459482 ## Add child
460- launch_graph (gb_parent , gb_child )
483+ try :
484+ launch_graph (gb_parent , gb_child )
485+ except NotImplementedError as e :
486+ with pytest .raises (
487+ NotImplementedError ,
488+ match = "^Launching child graphs is not implemented for versions older than CUDA 12. Found driver version is" ,
489+ ):
490+ raise e
491+ gb_parent .end_building ()
492+ pytest .skip ("Launching child graphs is not implemented for versions older than CUDA 12" )
461493
462494 launch (gb_parent , LaunchConfig (grid = 1 , block = 1 ), add_one , arr .ctypes .data )
463495 graph = gb_parent .end_building ().complete ()
@@ -473,7 +505,7 @@ def test_graph_child_graph(init_cuda):
473505
474506@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
475507def test_graph_update (init_cuda ):
476- mod = _common_kernels ()
508+ mod = _common_kernels_conditional ()
477509 add_one = mod .get_kernel ("add_one" )
478510
479511 # Allocate memory
@@ -586,7 +618,7 @@ def test_graph_stream_lifetime(init_cuda):
586618
587619
588620def test_graph_dot_print_options (init_cuda , tmp_path ):
589- mod = _common_kernels ()
621+ mod = _common_kernels_conditional ()
590622 set_handle = mod .get_kernel ("set_handle" )
591623 empty_kernel = mod .get_kernel ("empty_kernel" )
592624
0 commit comments