memra-engine 0.135.0

From-scratch CUDA LLM inference engine for NVIDIA RTX 50-series (sm_120a) and Hopper (sm_90a) - custom kernels, no frameworks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Full-layer capture instrument. Replay is gate-only and requires the caller to
//! own the stable workspace/scalar sources captured by the graph.
use cudarc::driver::{CudaGraph, CudaStream, sys};
use std::{collections::BTreeMap, sync::Arc};

use cudarc::driver::{CudaSlice, DevicePtr, DevicePtrMut};
use std::ffi::c_void;

/// An unsuccessful stream drain is not a completion receipt. Never unwind or
/// return through owners of peer-visible storage when either rank is unproven.
/// Try both ranks, print every failure, then fail-stop without running Drop.
pub(crate) fn require_pair_completion(
    context: &str,
    mut drain: impl FnMut(usize) -> Result<(), String>,
) {
    let mut errors = Vec::new();
    for rank in 0..2 {
        if let Err(error) = drain(rank) {
            errors.push(format!("rank {rank}: {error}"));
        }
    }
    if !errors.is_empty() {
        replay_fail_stop(context, &errors.join("; "));
    }
}

fn replay_fail_stop(context: &str, error: &str) -> ! {
    use std::io::Write;
    let _ = writeln!(
        std::io::stderr(),
        "FATAL full-token replay completion unproven ({context}): {error}; aborting before captured storage release"
    );
    let _ = std::io::stderr().flush();
    std::process::abort();
}

/// Capture-only owner. This deliberately does not use `capture_layer`, which
/// executes immediately and drains before its paired rank can launch.
struct ReplayGraph {
    graph: *mut c_void,
    executable: *mut c_void,
    stream: Arc<CudaStream>,
    census: [u64; 7],
}
// The request and TP walk mutex serialize every launch. Handles are immutable
// after instantiate; each operation explicitly binds its owning CUDA context.
unsafe impl Send for ReplayGraph {}
unsafe impl Sync for ReplayGraph {}
impl ReplayGraph {
    fn begin(stream: Arc<CudaStream>) -> Result<Self, String> {
        if stream.context().is_event_tracking() {
            return Err("full replay requires tracking disabled before allocation".into());
        }
        let mut result = Self {
            graph: std::ptr::null_mut(),
            executable: std::ptr::null_mut(),
            stream,
            census: [0; 7],
        };
        result
            .stream
            .context()
            .bind_to_thread()
            .map_err(|e| e.to_string())?;
        unsafe {
            crate::dsv4_ffi::ck(
                "replay capture begin",
                crate::dsv4_ffi::memra_dsv4_replay_capture_begin(
                    &mut result.graph,
                    result.stream.cu_stream().cast(),
                ),
            )?;
        }
        Ok(result)
    }
    fn end(&mut self) -> Result<(), String> {
        self.stream
            .context()
            .bind_to_thread()
            .map_err(|e| e.to_string())?;
        unsafe {
            crate::dsv4_ffi::ck(
                "replay capture end",
                crate::dsv4_ffi::memra_dsv4_replay_capture_end(
                    self.graph,
                    &mut self.executable,
                    self.stream.cu_stream().cast(),
                ),
            )?;
            crate::dsv4_ffi::ck(
                "replay census",
                crate::dsv4_ffi::memra_dsv4_replay_census(self.graph, self.census.as_mut_ptr()),
            )
        }
    }
    fn launch(&self) -> Result<(), String> {
        if self.executable.is_null() {
            return Err("uninstantiated replay graph".into());
        }
        self.stream
            .context()
            .bind_to_thread()
            .map_err(|e| e.to_string())?;
        unsafe {
            crate::dsv4_ffi::ck(
                "replay launch",
                crate::dsv4_ffi::memra_dsv4_replay_launch(
                    self.executable,
                    self.stream.cu_stream().cast(),
                ),
            )
        }
    }
}
impl Drop for ReplayGraph {
    fn drop(&mut self) {
        let result = self
            .stream
            .context()
            .bind_to_thread()
            .map_err(|e| e.to_string())
            .and_then(|()| unsafe {
                crate::dsv4_ffi::ck(
                    "replay graph destroy",
                    crate::dsv4_ffi::memra_dsv4_replay_destroy(
                        self.graph,
                        self.executable,
                        self.stream.cu_stream().cast(),
                    ),
                )
            });
        if let Err(error) = result {
            eprintln!("{error}");
        }
    }
}

/// One request owns both ranks, both segments, stable inputs, device counters
/// and the real sampler. Drop drains BOTH ranks before any captured resource
/// is released. MatrixStep declares this before its workspace/cache references.
pub(crate) struct ReplayPair {
    graphs: [[Option<ReplayGraph>; 2]; 2],
    pub sampler: crate::dsv4_sampler::Dsv4DeviceSampler,
    input: [CudaSlice<u64>; 2],
    host: [crate::PinnedHostBuf; 2],
    counters: [CudaSlice<u64>; 2],
    streams: [Arc<CudaStream>; 2],
    pub cfg: crate::dsv4_gpu::Dsv4SampleCfg,
    pub ready: bool,
    pub owner: usize,
    pub captures: [u64; 2],
    pub ar_blocks: [i32; 2],
}
impl ReplayPair {
    pub fn new(
        streams: [Arc<CudaStream>; 2],
        sampler: crate::dsv4_sampler::Dsv4DeviceSampler,
        cfg: crate::dsv4_gpu::Dsv4SampleCfg,
        owner: usize,
    ) -> Result<Self, String> {
        let mut inputs = Vec::new();
        let mut hosts = Vec::new();
        let mut counts = Vec::new();
        for stream in &streams {
            stream
                .context()
                .bind_to_thread()
                .map_err(|e| e.to_string())?;
            inputs.push(stream.alloc_zeros(3).map_err(|e| e.to_string())?);
            counts.push(stream.alloc_zeros(2).map_err(|e| e.to_string())?);
            hosts.push(crate::PinnedHostBuf::new(24).map_err(|e| e.to_string())?);
            stream.synchronize().map_err(|e| e.to_string())?;
        }
        Ok(Self {
            graphs: [[None, None], [None, None]],
            sampler,
            input: inputs.try_into().map_err(|_| "replay input rank count")?,
            host: hosts.try_into().map_err(|_| "replay host rank count")?,
            counters: counts.try_into().map_err(|_| "replay counter rank count")?,
            streams,
            cfg,
            ready: false,
            owner,
            captures: [0; 2],
            ar_blocks: [
                crate::tp_ar::ar_blocks_for(4096),
                crate::tp_ar::ar_blocks_for(6 * 4096),
            ],
        })
    }
    pub fn upload(&mut self, token: u32, pos: usize, fault: u64) -> Result<(), String> {
        let words = [
            u64::from(token) | ((pos as u64) << 32),
            crate::dsv4_gpu::dsv4_pos_uniform(self.cfg.seed, pos + 1).to_bits(),
            fault,
        ];
        for rank in 0..2 {
            let stream = &self.streams[rank];
            stream
                .context()
                .bind_to_thread()
                .map_err(|e| e.to_string())?;
            let src = unsafe {
                std::slice::from_raw_parts_mut(
                    self.host[rank].as_mut_slice().as_mut_ptr().cast::<u64>(),
                    3,
                )
            };
            src.copy_from_slice(&words);
            stream
                .memcpy_htod(&src[..], &mut self.input[rank])
                .map_err(|e| e.to_string())?;
        }
        Ok(())
    }
    pub fn begin(&mut self, segment: usize) -> Result<(), String> {
        for rank in 0..2 {
            if self.graphs[rank][segment].is_some() {
                return Err("replay recapture refused".into());
            }
            self.graphs[rank][segment] = Some(ReplayGraph::begin(self.streams[rank].clone())?);
        }
        Ok(())
    }
    pub fn end(&mut self, segment: usize) -> Result<(), String> {
        for rank in 0..2 {
            self.graphs[rank][segment]
                .as_mut()
                .ok_or("replay capture missing")?
                .end()?;
            let census = self.graphs[rank][segment].as_ref().expect("capture").census;
            if census[6] != 0
                || (segment == 0 && (census[2] != 86 || census[3] != 1 || census[4] != 86))
            {
                return Err(format!(
                    "incomplete full-token graph rank {rank} segment {segment}: {census:?}"
                ));
            }
        }
        self.captures[segment] += 1;
        Ok(())
    }
    pub fn launch(&self, segment: usize) -> Result<(), String> {
        // Never drain the first rank before submission of the second.
        for rank in 0..2 {
            self.graphs[rank][segment]
                .as_ref()
                .ok_or("replay graph missing")?
                .launch()?;
        }
        Ok(())
    }
    pub fn census(&self) -> [[[u64; 7]; 2]; 2] {
        std::array::from_fn(|r| {
            std::array::from_fn(|s| self.graphs[r][s].as_ref().map_or([0; 7], |g| g.census))
        })
    }
    pub fn dump(&self, directory: &std::path::Path) -> Result<(), String> {
        self.drain_both()?;
        for rank in 0..2 {
            for segment in 0..2 {
                let graph = self.graphs[rank][segment]
                    .as_ref()
                    .ok_or("graph dump before complete capture")?;
                let name = format!("full-token-rank{rank}-segment{segment}.dot");
                let path = directory.join(name);
                let path = std::ffi::CString::new(path.as_os_str().as_encoded_bytes())
                    .map_err(|e| e.to_string())?;
                graph
                    .stream
                    .context()
                    .bind_to_thread()
                    .map_err(|e| e.to_string())?;
                unsafe {
                    crate::dsv4_ffi::ck(
                        "replay graph dump",
                        crate::dsv4_ffi::memra_dsv4_replay_dump(graph.graph, path.as_ptr()),
                    )?;
                }
            }
        }
        Ok(())
    }
    pub fn input_ptr(&self, rank: usize) -> *const u64 {
        self.input[rank].device_ptr(&self.streams[rank]).0 as *const u64
    }
    pub fn counter_ptr(&mut self, rank: usize, segment: usize) -> *mut u64 {
        unsafe {
            (self.counters[rank].device_ptr_mut(&self.streams[rank]).0 as *mut u64).add(segment)
        }
    }
    pub fn counts(&self) -> Result<[[u64; 2]; 2], String> {
        let mut out = [[0; 2]; 2];
        for (rank, row) in out.iter_mut().enumerate() {
            self.streams[rank]
                .context()
                .bind_to_thread()
                .map_err(|e| e.to_string())?;
            self.streams[rank]
                .memcpy_dtoh(&self.counters[rank], row)
                .map_err(|e| e.to_string())?;
            self.streams[rank]
                .synchronize()
                .map_err(|e| e.to_string())?;
        }
        Ok(out)
    }
    pub fn abort_capture_both(&self) -> Result<(), String> {
        let mut errors = Vec::new();
        for (rank, stream) in self.streams.iter().enumerate() {
            let result = stream
                .context()
                .bind_to_thread()
                .map_err(|e| e.to_string())
                .and_then(|()| unsafe {
                    crate::dsv4_ffi::ck(
                        "replay capture abort",
                        crate::dsv4_ffi::memra_dsv4_replay_abort(stream.cu_stream().cast()),
                    )
                });
            if let Err(error) = result {
                errors.push(format!("rank {rank}: {error}"));
            }
        }
        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors.join("; "))
        }
    }
    pub fn drain_both(&self) -> Result<(), String> {
        require_pair_completion("ReplayPair drain", |rank| {
            let stream = &self.streams[rank];
            stream
                .context()
                .bind_to_thread()
                .and_then(|()| stream.synchronize())
                .map_err(|e| e.to_string())
        });
        Ok(())
    }
}
impl Drop for ReplayPair {
    fn drop(&mut self) {
        // Abort capture first: synchronizing a captured stream is prohibited.
        if let Err(error) = self.abort_capture_both() {
            replay_fail_stop("ReplayPair drop capture abort", &error);
        }
        if let Err(error) = self.drain_both() {
            replay_fail_stop("ReplayPair drop drain", &error);
        }
    }
}

pub struct Dsv4LayerCapture {
    pub layer: usize,
    pub nodes: BTreeMap<String, usize>,
    pub kernels: Vec<String>,
    graph: CudaGraph,
}
impl std::fmt::Debug for Dsv4LayerCapture {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Dsv4LayerCapture")
            .field("layer", &self.layer)
            .field("nodes", &self.nodes)
            .field("kernels", &self.kernels)
            .finish_non_exhaustive()
    }
}

impl Dsv4LayerCapture {
    /// Replay the retained graph on its capture stream. This is intentionally a
    /// gate-only API: callers must keep the captured workspace and scalar-source
    /// buffers alive for the graph lifetime.
    pub(crate) fn replay(&self) -> Result<(), String> {
        self.graph
            .launch()
            .map_err(|e| format!("layer {} graph replay: {e}", self.layer))
    }
}

struct CaptureScope {
    stream: Arc<CudaStream>,
    begun: bool,
}

impl Drop for CaptureScope {
    fn drop(&mut self) {
        if self.begun {
            // A failed/panicking body must not leave the owning stream capturing.
            let _ = self.stream.end_capture(
                sys::CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH,
            );
        }
    }
}

pub(crate) fn capture_layer(
    stream: Arc<CudaStream>,
    layer: usize,
    body: impl FnOnce() -> Result<(), String>,
) -> Result<Dsv4LayerCapture, String> {
    // Cudarc's switch prevents creation of new per-buffer events, but existing
    // buffers retain theirs and their pointer guards still record those events.
    // Disabling here is too late. DSV4 disables tracking before loading tensors; refuse
    // the tracking-on mode rather than silently making an unsafe transition.
    if stream.context().is_event_tracking() {
        return Err(format!(
            "layer {layer} capture requires event tracking disabled before buffer allocation"
        ));
    }
    let fail = |step: &str, error: &dyn std::fmt::Display| {
        format!("layer {layer} capture {step}: {error}")
    };
    if stream.capture_status().map_err(|e| fail("status", &e))?
        != sys::CUstreamCaptureStatus::CU_STREAM_CAPTURE_STATUS_NONE
    {
        return Err(format!(
            "layer {layer} capture requires an uncaptured stream"
        ));
    }
    stream.synchronize().map_err(|e| fail("pre-drain", &e))?;
    let mut scope = CaptureScope {
        stream: stream.clone(),
        begun: false,
    };
    stream
        .begin_capture(sys::CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)
        .map_err(|e| fail("begin", &e))?;
    scope.begun = true;
    // No warmup: capture records the body but does not execute cache mutations.
    let result = body();
    scope.begun = false;
    let ended = stream.end_capture(
        sys::CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH,
    );
    result.map_err(|e| fail("body", &e))?;
    let graph = ended
        .map_err(|e| fail("end/instantiate", &e))?
        .ok_or_else(|| format!("layer {layer} capture produced no graph"))?;
    let nodes = crate::graph_update::node_census(&graph).map_err(|e| fail("node census", &e))?;
    let kernels = crate::graph_update::kernel_nodes(&graph)
        .map_err(|e| fail("kernel census", &e))?
        .into_iter()
        .map(|node| node.name)
        .collect();
    graph.upload().map_err(|e| fail("upload", &e))?;
    graph.launch().map_err(|e| fail("launch", &e))?;
    stream.synchronize().map_err(|e| fail("completion", &e))?;
    Ok(Dsv4LayerCapture {
        layer,
        nodes,
        kernels,
        graph,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use cudarc::driver::{CudaContext, DevicePtr, DevicePtrMut};

    #[test]
    fn replay_completion_policy_fail_stop_subprocess() {
        const CHILD: &str = "MEMRA_TEST_REPLAY_DRAIN_FAILURE_CHILD";
        if let Ok(value) = std::env::var(CHILD) {
            struct CapturedStorage;
            impl Drop for CapturedStorage {
                fn drop(&mut self) {
                    eprintln!("CAPTURED_STORAGE_RELEASED");
                }
            }
            let _request_workspace = CapturedStorage;
            let (scenario, rank) = value.split_once(':').unwrap();
            let failed_rank = rank.parse::<usize>().unwrap();
            let drain = |rank| {
                eprintln!("DRAIN_ATTEMPTED_{rank}");
                if rank == failed_rank {
                    Err("injected drain failure".into())
                } else {
                    Ok(())
                }
            };
            if scenario == "drop" {
                struct CompletionGuard<F: FnMut(usize) -> Result<(), String>>(F);
                impl<F: FnMut(usize) -> Result<(), String>> Drop for CompletionGuard<F> {
                    fn drop(&mut self) {
                        require_pair_completion("destructor unwind", &mut self.0);
                    }
                }
                let _guard = CompletionGuard(drain);
                panic!("original submission exception");
            }
            require_pair_completion("injected failed CUDA completion", drain);
            panic!("failed completion returned instead of aborting");
        }
        for scenario in ["error", "drop"] {
            for rank in 0..2 {
                use std::os::unix::process::ExitStatusExt;
                let output = std::process::Command::new(std::env::current_exe().unwrap())
                    .args([
                        "--exact",
                        "dsv4_graph::tests::replay_completion_policy_fail_stop_subprocess",
                        "--nocapture",
                    ])
                    .env(CHILD, format!("{scenario}:{rank}"))
                    .output()
                    .unwrap();
                let stderr = String::from_utf8_lossy(&output.stderr);
                assert_eq!(output.status.signal(), Some(libc::SIGABRT), "{stderr}");
                assert!(
                    stderr.contains("DRAIN_ATTEMPTED_0") && stderr.contains("DRAIN_ATTEMPTED_1"),
                    "{stderr}"
                );
                assert!(
                    stderr.contains("injected drain failure")
                        && stderr.contains("completion unproven"),
                    "{stderr}"
                );
                assert!(
                    !stderr.contains("CAPTURED_STORAGE_RELEASED"),
                    "destructor ran: {stderr}"
                );
            }
        }
        let mut attempts = Vec::new();
        require_pair_completion("successful drains after ordinary refusal", |rank| {
            attempts.push(rank);
            Ok(())
        });
        assert_eq!(attempts, [0, 1]);
    }

    #[test]
    #[ignore = "requires the exclusively locked development pair; runtime lifetime gate, not a model gate"]
    fn replay_rust_partial_submission_and_capture_cleanup() {
        let contexts = [CudaContext::new(0).unwrap(), CudaContext::new(1).unwrap()];
        for ctx in &contexts {
            unsafe {
                ctx.disable_event_tracking();
            }
        }
        let streams = [
            contexts[0].new_stream().unwrap(),
            contexts[1].new_stream().unwrap(),
        ];
        let make_pair = || {
            let sampler =
                crate::dsv4_sampler::Dsv4DeviceSampler::new(streams[1].clone(), 257).unwrap();
            ReplayPair::new(
                streams.clone(),
                sampler,
                crate::dsv4_gpu::Dsv4SampleCfg {
                    temperature: 1.0,
                    top_p: 1.0,
                    top_k: 0,
                    seed: 1,
                },
                0,
            )
            .unwrap()
        };
        let assert_uncaptured = || {
            for stream in &streams {
                stream.context().bind_to_thread().unwrap();
                assert_eq!(
                    stream.capture_status().unwrap(),
                    sys::CUstreamCaptureStatus::CU_STREAM_CAPTURE_STATUS_NONE
                );
                stream.synchronize().unwrap();
            }
        };
        // Actual Rust owner unwinding from a capture-body failure. No execution
        // occurred; Drop must end BOTH capture scopes before its paired drain.
        let capture_error = (|| -> Result<(), String> {
            let mut pair = make_pair();
            pair.begin(0)?;
            Err("injected capture body failure".into())
        })()
        .unwrap_err();
        assert_eq!(capture_error, "injected capture body failure");
        assert_uncaptured();
        // End/instantiate error at the real Rust capture_end FFI boundary.
        {
            let mut pair = make_pair();
            pair.begin(0).unwrap();
            pair.abort_capture_both().unwrap();
            let error = pair.graphs[0][0].as_mut().unwrap().end().unwrap_err();
            assert!(error.contains("replay capture end"), "{error}");
            // This deliberately calls EndCapture on a stream already ended by
            // abort_capture_both. Consume only its asserted CUDA API last error
            // before testing later launches; do not mask an unexpected failure.
            assert!(error.ends_with("rc=10401"), "unexpected end error: {error}");
            unsafe extern "C" {
                fn cudaGetLastError() -> i32;
            }
            assert_eq!(
                unsafe { cudaGetLastError() },
                401,
                "expected illegal-state error"
            );
            eprintln!("PASS Rust capture-end failure rc=10401 asserted and consumed");
        }
        assert_uncaptured();
        // Use the runtime's actual pair-launch loop and real graph counters.
        // These are tiny no-peer graphs; they cover Rust ownership/submission,
        // not the C++ fixture's peer barriers or full-model layer coverage.
        for segment in 0..2 {
            let mut pair = make_pair();
            pair.begin(segment).unwrap();
            for (rank, stream) in streams.iter().enumerate() {
                stream.context().bind_to_thread().unwrap();
                unsafe {
                    crate::dsv4_ffi::ck(
                        "test replay tick",
                        crate::dsv4_ffi::memra_dsv4_replay_tick(
                            pair.counter_ptr(rank, segment),
                            stream.cu_stream().cast(),
                        ),
                    )
                    .unwrap();
                }
            }
            // Finish primitive graphs directly: the full-forward census gate
            // deliberately rejects tiny fixtures. Production census is untouched.
            for rank in 0..2 {
                pair.graphs[rank][segment].as_mut().unwrap().end().unwrap();
            }
            let peer = pair.graphs[1][segment].take();
            let error = pair.launch(segment).unwrap_err();
            assert!(error.contains("replay graph missing"), "{error}");
            pair.drain_both().unwrap();
            let counts = pair.counts().unwrap();
            assert_eq!(counts[0][segment], 1, "first rank never submitted");
            assert_eq!(counts[1][segment], 0, "second rank unexpectedly submitted");
            pair.graphs[1][segment] = peer;
            drop(pair);
            assert_uncaptured();
            eprintln!(
                "PASS Rust partial segment={segment} first_submit=1 second_submit=0 paired_completion=1"
            );
        }
    }

    #[test]
    #[ignore = "requires an exclusively locked non-serving CUDA device"]
    fn cuda_capture_runs_once_and_restores_scope_after_failure() {
        let context = CudaContext::new(0).expect("context");
        let stream = context.new_stream().expect("stream");
        let refusal = capture_layer(stream.clone(), 0, || {
            panic!("tracking-on body must not run")
        });
        assert!(refusal.unwrap_err().contains("before buffer allocation"));
        assert!(
            context.is_event_tracking(),
            "refusal must not change context policy"
        );
        // Same policy and ordering as Dsv4Gpu::load: disable before
        // any buffers are allocated, not around the capture itself.
        unsafe { context.disable_event_tracking() };
        let values: Vec<f32> = (0..64).map(|x| x as f32 + 1.0).collect();
        let input = stream.clone_htod(&values).expect("input");
        let mut output = stream.alloc_zeros::<f32>(64).expect("output");
        let tracking = context.is_event_tracking();
        let body_calls = std::cell::Cell::new(0);
        let captured = capture_layer(stream.clone(), 0, || {
            body_calls.set(body_calls.get() + 1);
            unsafe {
                crate::dsv4_ffi::ck(
                    "capture add",
                    crate::dsv4_ffi::memra_dsv4_add_inplace(
                        output.device_ptr_mut(&stream).0 as *mut f32,
                        input.device_ptr(&stream).0 as *const f32,
                        64,
                        stream.cu_stream().cast(),
                    ),
                )
            }
        })
        .expect("capture and execute");
        assert_eq!(
            body_calls.get(),
            1,
            "capture must not warm up stateful operations"
        );
        assert_eq!(
            stream.clone_dtoh(&output).unwrap(),
            values,
            "one GPU execution, not warmup plus replay"
        );
        assert_eq!(captured.kernels.len(), 1);
        assert!(captured.kernels[0].contains("dsv4_add_inplace"));
        assert_eq!(context.is_event_tracking(), tracking);

        let failure = capture_layer(stream.clone(), 1, || Err("deliberate body failure".into()));
        let failure = failure.unwrap_err();
        assert!(
            failure.contains("deliberate body failure"),
            "unexpected capture failure: {failure}"
        );
        assert_eq!(
            stream.capture_status().unwrap(),
            sys::CUstreamCaptureStatus::CU_STREAM_CAPTURE_STATUS_NONE
        );
        assert_eq!(context.is_event_tracking(), tracking);
        let recovered = capture_layer(stream.clone(), 2, || {
            stream
                .memcpy_dtod(&input, &mut output)
                .map_err(|e| e.to_string())
        })
        .expect("capture after failure");
        assert!(!recovered.nodes.is_empty());
        assert_eq!(stream.clone_dtoh(&output).unwrap(), values);
        println!(
            "PASS layer capture scope: tracking-on refusal, one execution, kernel census, body-error cleanup and subsequent capture"
        );
    }
}