@@ -203,6 +203,99 @@ def test_to_native_launch_config_pdl():
203203 )
204204
205205
206+ @skipif_need_cuda_headers
207+ def test_pdl_primary_secondary_overlap_same_stream ():
208+ """Primary + secondary PDL launch on one stream can overlap on Hopper+.
209+
210+ Secondary is launched with ``programmatic_stream_serialization=True``. After
211+ the primary triggers completion, it spins until it observes a flag written by
212+ the secondary's independent preamble — proving both grids were resident at
213+ once. Without PDL, the secondary cannot start until the primary exits.
214+ """
215+ dev = Device ()
216+ if dev .compute_capability < (9 , 0 ):
217+ pytest .skip ("Programmatic Dependent Launch requires compute capability >= 9.0" )
218+ dev .set_current ()
219+ stream = dev .create_stream (options = {"nonblocking" : True })
220+
221+ # clock64 budgets are in GPU cycles; keep the post-trigger window long enough
222+ # for the secondary to boot, but short enough for a unit test.
223+ code = r"""
224+ #include <cuda_device_runtime_api.h>
225+
226+ extern "C" __global__ void primary_kernel(int* secondary_started, int* overlapped) {
227+ cudaTriggerProgrammaticLaunchCompletion();
228+
229+ const long long deadline = clock64() + 100000000LL; // ~post-trigger overlap window
230+ if (threadIdx.x == 0 && blockIdx.x == 0) {
231+ while (clock64() < deadline) {
232+ if (atomicAdd(secondary_started, 0) != 0) {
233+ atomicExch(overlapped, 1);
234+ return;
235+ }
236+ __nanosleep(1000);
237+ }
238+ }
239+ }
240+
241+ extern "C" __global__ void secondary_kernel(int* secondary_started) {
242+ if (threadIdx.x == 0 && blockIdx.x == 0) {
243+ atomicExch(secondary_started, 1);
244+ }
245+ __syncthreads();
246+
247+ // Remain concurrent with primary long enough to be observed.
248+ const long long deadline = clock64() + 20000000LL;
249+ while (clock64() < deadline) {
250+ __nanosleep(1000);
251+ }
252+
253+ cudaGridDependencySynchronize();
254+ }
255+ """
256+
257+ arch = "" .join (f"{ i } " for i in dev .compute_capability )
258+ pro_opts = ProgramOptions (std = "c++17" , arch = f"sm_{ arch } " , include_path = helpers .CUDA_INCLUDE_PATH )
259+ prog = Program (code , code_type = "c++" , options = pro_opts )
260+ mod = prog .compile ("cubin" )
261+ primary = mod .get_kernel ("primary_kernel" )
262+ secondary = mod .get_kernel ("secondary_kernel" )
263+
264+ mr = LegacyPinnedMemoryResource ()
265+ secondary_started = np .from_dlpack (mr .allocate (4 )).view (np .int32 )
266+ overlapped = np .from_dlpack (mr .allocate (4 )).view (np .int32 )
267+
268+ primary_cfg = LaunchConfig (grid = 1 , block = 1 )
269+ secondary_cfg = LaunchConfig (grid = 1 , block = 1 , programmatic_stream_serialization = True )
270+ secondary_serial_cfg = LaunchConfig (grid = 1 , block = 1 )
271+
272+ def _run (secondary_launch_cfg : LaunchConfig ) -> int :
273+ secondary_started [0 ] = 0
274+ overlapped [0 ] = 0
275+ launch (stream , primary_cfg , primary , secondary_started .ctypes .data , overlapped .ctypes .data )
276+ launch (stream , secondary_launch_cfg , secondary , secondary_started .ctypes .data )
277+ stream .sync ()
278+ return int (overlapped [0 ])
279+
280+ # Without the PDL attribute, same-stream kernels stay serialized.
281+ assert _run (secondary_serial_cfg ) == 0 , (
282+ "Expected no overlap when programmatic_stream_serialization is False"
283+ )
284+
285+ # PDL overlap is opportunistic; retry a few times on a quiet GPU.
286+ saw_overlap = False
287+ for _ in range (5 ):
288+ if _run (secondary_cfg ) == 1 :
289+ saw_overlap = True
290+ break
291+
292+ assert saw_overlap , (
293+ "Expected primary and secondary kernels to overlap on the same stream via PDL; "
294+ "primary never observed secondary_started while still running after "
295+ "cudaTriggerProgrammaticLaunchCompletion()"
296+ )
297+
298+
206299def test_launch_config_cluster_accepts_hopper_cc (monkeypatch ):
207300 """LaunchConfig accepts ``cluster`` when the device reports compute
208301 capability >= 9.0. Device is mocked so the cluster-cast branch runs on any
0 commit comments