numr 0.15.0

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
Documentation
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
//! Activation checkpointing for memory-efficient training.
//!
//! Discards intermediate activations during forward and recomputes them during
//! backward. Trades ~33% extra compute for dramatically less activation memory.
//!
//! # Checkpointing is transparent to gradients
//!
//! Only memory behaviour changes. A trainable value the closure captures but the
//! caller omits from `inputs` still receives its gradient: the forward runs on
//! detached inputs, so any node the segment retains exists because it read a
//! captured value, and the segment registers each such value as an input of its
//! own graph node. Listing a value in `inputs` remains the clearer form, and is
//! the only way to feed a value the closure does not already hold.
//!
//! # Example
//!
//! ```
//! # use numr::prelude::*;
//! # use numr::autograd::{Var, backward, checkpoint, var_mul, var_sum};
//! # let device = CpuDevice::new();
//! # let client = CpuRuntime::default_client(&device);
//! let x = Var::new(Tensor::from_slice(&[3.0f32], &[1], &device)?, true);
//! let w = Var::new(Tensor::from_slice(&[2.0f32], &[1], &device)?, true);
//!
//! // `w` may be listed or simply captured; both give the same gradient.
//! let y = checkpoint(|inputs, c| var_mul(&inputs[0], &inputs[1], c), &[&x, &w])?;
//!
//! let loss = var_sum(&y, &[], false, &client)?;
//! let grads = backward(&loss, &client)?;
//! // grad_x = w = 2, grad_w = x = 3
//! # Ok::<(), numr::error::Error>(())
//! ```

use std::collections::HashSet;
use std::sync::Arc;

use super::capture::collect_captures;
use super::grad_mode::EnableGradGuard;
use crate::autograd::{GradFn, Var, backward_wrt, var_mul, var_sum};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::TensorOps;
use crate::runtime::{Runtime, RuntimeClient};
use crate::tensor::{Tensor, TensorId};

/// Run `f` on `inputs` with activation checkpointing, on the default client.
///
/// Equivalent to [`checkpoint_with_client`] with `R::default_client(device)`,
/// where `device` is the device of `inputs[0]`. Prefer
/// [`checkpoint_with_client`] whenever a client is already in hand: it keeps the
/// recompute on the caller's client, and its closure takes the caller's own
/// client type instead of `R::Client`.
///
/// During forward, `f` runs on detached copies of the inputs so no intermediate
/// graph nodes are retained. During backward, `f` is re-run with grad tracking
/// to reconstruct the graph and propagate gradients.
///
/// # Errors
///
/// - `inputs` is empty.
pub fn checkpoint<R, F>(f: F, inputs: &[&Var<R>]) -> Result<Var<R>>
where
    R: Runtime<DType = DType>,
    R::Client: TensorOps<R> + 'static,
    F: Fn(&[Var<R>], &R::Client) -> Result<Var<R>> + Send + Sync + 'static,
{
    let device = inputs
        .first()
        .ok_or_else(|| Error::InvalidArgument {
            arg: "inputs",
            reason: "checkpoint requires at least one input".to_string(),
        })?
        .tensor()
        .device();

    let client = R::default_client(device);
    checkpoint_with_client(f, inputs, &client)
}

/// Run `f` on `inputs` with activation checkpointing, on `client`.
///
/// Both the forward pass and the backward recompute run on `client`, so the
/// recompute uses the same stream, allocator and parallelism settings as the
/// original forward. `f` receives `client` itself, so the trait bounds the
/// closure needs land on the caller's own client type `C`, not on `R::Client`.
///
/// The client is cloned into the graph node and outlives this call.
///
/// # Captured values differentiate too
///
/// A value the closure captures rather than receives through `inputs` becomes an
/// input of the checkpoint's graph node, so it gets the gradient it would have
/// got without checkpointing. See this module's docs.
///
/// # Frozen segments
///
/// When no listed input requires grad and the closure captures nothing
/// trainable, the result is an untracked `Var`. No graph node is kept, so `f`
/// and the input tensors are freed on return.
///
/// A listed input with `requires_grad == false` gets no gradient, as in the
/// plain forward, even when other inputs train.
///
/// # Errors
///
/// - `inputs` is empty.
pub fn checkpoint_with_client<R, C, F>(f: F, inputs: &[&Var<R>], client: &C) -> Result<Var<R>>
where
    R: Runtime<DType = DType>,
    C: RuntimeClient<R> + TensorOps<R> + 'static,
    // Required by `CheckpointBackward`'s `GradFn` impl: the recompute's `var_*`
    // ops are bounded on `R::Client`, not on the caller's `C`.
    R::Client: TensorOps<R>,
    F: Fn(&[Var<R>], &C) -> Result<Var<R>> + Send + Sync + 'static,
{
    if inputs.is_empty() {
        return Err(Error::InvalidArgument {
            arg: "inputs",
            reason: "checkpoint requires at least one input".to_string(),
        });
    }

    // Save original input info for backward
    let mut input_ids: Vec<TensorId> = inputs.iter().map(|v| v.id()).collect();
    let input_tensors: Vec<Tensor<R>> = inputs.iter().map(|v| v.tensor().clone()).collect();
    let mut input_grad_fns: Vec<Option<Arc<dyn GradFn<R>>>> =
        inputs.iter().map(|v| v.grad_fn().cloned()).collect();
    let mut input_requires_grad: Vec<bool> = inputs.iter().map(|v| v.requires_grad()).collect();

    // Forward: run on detached inputs (no grad tracking inside the segment)
    let detached: Vec<Var<R>> = inputs
        .iter()
        .map(|v| Var::new(v.tensor().clone(), false))
        .collect();

    // Ids the capture walk must not report: the detached copies it will meet in
    // the graph, and the listed inputs, which already own a slot.
    let mut known: HashSet<TensorId> = detached.iter().map(|v| v.id()).collect();
    known.extend(input_ids.iter().copied());

    // Every id minted from here on belongs to the segment, so an older id the
    // retained graph reaches was captured from outside.
    let boundary = TensorId::new();

    let output = f(&detached, client)?;

    // The closure may read values it never received through `inputs`. Those are
    // real inputs of the segment: give each one a slot so the recompute
    // differentiates it and the driver accumulates its gradient. Detached inputs
    // build no node, so a segment reading nothing but frozen values finds none.
    let captures = collect_captures(&output, boundary, &known);

    // No listed input requires grad and nothing trainable was captured, so no
    // gradient can flow. The plain forward gives an untracked output, and so
    // does this one. Dropping `f` here also frees everything it captured.
    if captures.is_empty() && !inputs.iter().any(|v| v.requires_grad()) {
        return Ok(Var::new(output.tensor().clone(), false));
    }

    for capture_id in captures {
        input_ids.push(capture_id);
        // A capture is always a leaf, so the outer pass stops there.
        input_grad_fns.push(None);
        // A capture built a graph node, so it requires grad.
        input_requires_grad.push(true);
    }

    let checkpoint_backward = CheckpointBackward {
        func: Arc::new(f),
        client: client.clone(),
        input_ids,
        input_tensors,
        input_grad_fns,
        input_requires_grad,
    };

    Ok(Var::from_op(
        output.tensor().clone(),
        Arc::new(checkpoint_backward),
    ))
}

struct CheckpointBackward<R: Runtime, C: 'static> {
    func: Arc<dyn Fn(&[Var<R>], &C) -> Result<Var<R>> + Send + Sync>,
    /// The caller's client. The recompute must run where the forward ran.
    client: C,
    /// The listed inputs first, then the values the closure captured.
    input_ids: Vec<TensorId>,
    /// The listed inputs only. A capture is held by the closure itself, and is
    /// never rebuilt, so this is shorter than `input_ids` whenever the segment
    /// captured anything.
    input_tensors: Vec<Tensor<R>>,
    input_grad_fns: Vec<Option<Arc<dyn GradFn<R>>>>,
    /// One flag per slot of `input_ids`. A frozen listed input gets no gradient.
    input_requires_grad: Vec<bool>,
}

impl<R, C> GradFn<R> for CheckpointBackward<R, C>
where
    R: Runtime<DType = DType>,
    C: RuntimeClient<R> + TensorOps<R> + 'static,
    // The `var_*` ops the recompute runs are bounded on `R::Client`, not on
    // the caller's `C`, so this bound is required even though the recompute
    // itself uses `self.client`.
    R::Client: TensorOps<R>,
{
    fn backward(&self, grad_output: &Tensor<R>, needed: &[bool]) -> Result<Vec<Option<Tensor<R>>>> {
        // The mask narrows the re-entrant pass below: only the wanted segment
        // inputs are asked for, so the recomputed graph prunes to them and the
        // rest of the segment's backward work is never run. A frozen slot is
        // never wanted, whatever the caller's mask says.
        let needed: Vec<bool> = needed
            .iter()
            .zip(self.input_requires_grad.iter())
            .map(|(&want, &trainable)| want && trainable)
            .collect();
        if !needed.iter().any(|&n| n) {
            return Ok(vec![None; self.input_ids.len()]);
        }

        let client = &self.client;

        // The recompute must record its graph even when backward runs inside
        // `no_grad`. Without a graph the re-entrant pass has nothing to walk.
        let _grad_mode = EnableGradGuard::new();

        // Reconstruct input Vars as LEAF nodes with original IDs.
        // They have no grad_fn so backward stops here — the outer backward
        // pass handles continuing through input_grad_fns() returned below.
        // The zip stops at `input_tensors`, so the trailing capture slots are
        // skipped: the closure supplies those values itself. Each input keeps
        // its own flag, so a frozen input stays out of the recomputed graph.
        let reconstructed: Vec<Var<R>> = self
            .input_ids
            .iter()
            .zip(self.input_tensors.iter())
            .zip(self.input_requires_grad.iter())
            .map(|((id, tensor), &trainable)| Var::with_id(tensor.clone(), *id, trainable))
            .collect();

        // Re-run forward WITH grad tracking — rebuilds the intermediate graph
        let recomputed_output = (self.func)(&reconstructed, client)?;

        // Backprop grad_output through the recomputed graph.
        // loss = sum(recomputed * grad_output) is a scalar whose gradient w.r.t.
        // each input is exactly the VJP: sum_j(grad_output_j * d(output_j)/d(input_i))
        let grad_output_var = Var::new(grad_output.clone(), false);
        let product = var_mul(&recomputed_output, &grad_output_var, client)?;
        let loss = var_sum(&product, &[], false, client)?;

        // Only the wanted segment inputs are read back below, so prune the
        // re-entrant pass to exactly those — the recomputed intermediates never
        // enter the store, and an unwanted input's cone is never walked.
        let wanted: Vec<TensorId> = self
            .input_ids
            .iter()
            .zip(&needed)
            .filter_map(|(id, &want)| want.then_some(*id))
            .collect();
        let grads = backward_wrt(&loss, &wanted, client)?;

        Ok(self
            .input_ids
            .iter()
            .zip(&needed)
            .map(|(id, &want)| want.then(|| grads.get(*id).cloned()).flatten())
            .collect())
    }

    fn inputs(&self) -> &[TensorId] {
        &self.input_ids
    }

    fn input_grad_fns(&self) -> Vec<Option<Arc<dyn GradFn<R>>>> {
        self.input_grad_fns.clone()
    }

    fn name(&self) -> &'static str {
        "CheckpointBackward"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::autograd::{
        BackwardHook, backward, backward_with_hooks, backward_wrt, var_add, var_mul, var_sum,
    };
    use crate::runtime::cpu::{CpuClient, CpuDevice, CpuRuntime, ParallelismConfig};
    use std::sync::Mutex;

    fn device_and_client() -> (CpuDevice, <CpuRuntime as Runtime>::Client) {
        let device = CpuDevice::new();
        let client = CpuRuntime::default_client(&device);
        (device, client)
    }

    #[test]
    fn test_checkpoint_x_squared() {
        // f(x) = x^2, df/dx = 2x
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );

        // Without checkpoint
        let y_normal = var_mul(&x, &x, &client).unwrap();
        let loss_normal = var_sum(&y_normal, &[], false, &client).unwrap();
        let grads_normal = backward(&loss_normal, &client).unwrap();

        // With checkpoint
        let y_ckpt = checkpoint(|inputs, c| var_mul(&inputs[0], &inputs[0], c), &[&x]).unwrap();
        let loss_ckpt = var_sum(&y_ckpt, &[], false, &client).unwrap();
        let grads_ckpt = backward(&loss_ckpt, &client).unwrap();

        let g_normal: Vec<f32> = grads_normal.get(x.id()).unwrap().to_vec();
        let g_ckpt: Vec<f32> = grads_ckpt.get(x.id()).unwrap().to_vec();

        assert!(
            (g_normal[0] - g_ckpt[0]).abs() < 1e-6,
            "normal={}, checkpoint={}",
            g_normal[0],
            g_ckpt[0]
        );
        assert!((g_ckpt[0] - 6.0).abs() < 1e-6);
    }

    #[test]
    fn test_checkpoint_multi_input() {
        // f(x, y) = x * y
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );
        let y = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[5.0f32], &[1], &device).unwrap(),
            true,
        );

        let out = checkpoint(|inputs, c| var_mul(&inputs[0], &inputs[1], c), &[&x, &y]).unwrap();

        let grads = backward(&out, &client).unwrap();

        // d(x*y)/dx = y = 5
        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 5.0).abs() < 1e-6);

        // d(x*y)/dy = x = 2
        let gy: Vec<f32> = grads.get(y.id()).unwrap().to_vec();
        assert!((gy[0] - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_checkpoint_chained() {
        // checkpoint(f1) -> checkpoint(f2)
        // f1(x) = x^2, f2(z) = z^2, so total = x^4
        // d(x^4)/dx = 4x^3 = 4*8 = 32 at x=2
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );

        let z = checkpoint(|inputs, c| var_mul(&inputs[0], &inputs[0], c), &[&x]).unwrap();

        let w = checkpoint(|inputs, c| var_mul(&inputs[0], &inputs[0], c), &[&z]).unwrap();

        let loss = var_sum(&w, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 32.0).abs() < 1e-4, "expected 32.0, got {}", gx[0]);
    }

    #[test]
    fn test_checkpoint_matches_normal_complex() {
        // More complex: f(x) = (x + x) * x = 2x^2
        // df/dx = 4x = 12 at x=3
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );

        let y = checkpoint(
            |inputs, c| {
                let sum = var_add(&inputs[0], &inputs[0], c)?;
                var_mul(&sum, &inputs[0], c)
            },
            &[&x],
        )
        .unwrap();

        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 12.0).abs() < 1e-5, "expected 12.0, got {}", gx[0]);
    }

    #[test]
    fn test_checkpoint_with_backward_hooks() {
        // Verify leaf hooks still fire through checkpointed segments
        use std::cell::RefCell;
        use std::rc::Rc;

        struct RecordingHook {
            leaf_ids: Rc<RefCell<Vec<TensorId>>>,
        }

        unsafe impl Send for RecordingHook {}

        impl BackwardHook<CpuRuntime> for RecordingHook {
            fn on_leaf_grad_ready(&mut self, id: TensorId, _grad: &Tensor<CpuRuntime>) {
                self.leaf_ids.borrow_mut().push(id);
            }
        }

        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );

        let y = checkpoint(|inputs, c| var_mul(&inputs[0], &inputs[0], c), &[&x]).unwrap();

        let loss = var_sum(&y, &[], false, &client).unwrap();

        let ids = Rc::new(RefCell::new(Vec::new()));
        let mut hook = RecordingHook {
            leaf_ids: ids.clone(),
        };
        let _grads = backward_with_hooks(&loss, &client, &mut hook).unwrap();

        let recorded = ids.borrow();
        assert!(
            recorded.contains(&x.id()),
            "leaf hook should have fired for x"
        );
    }

    #[test]
    fn test_checkpoint_vector_output() {
        // f(x) = x * x where x is a vector [2, 3]
        // loss = sum(f(x)) = 4 + 9 = 13
        // d(loss)/dx = [4, 6]
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32, 3.0], &[2], &device).unwrap(),
            true,
        );

        let y = checkpoint(|inputs, c| var_mul(&inputs[0], &inputs[0], c), &[&x]).unwrap();

        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 4.0).abs() < 1e-6);
        assert!((gx[1] - 6.0).abs() < 1e-6);
    }

    #[test]
    fn test_checkpoint_recompute_uses_caller_client() {
        // Defect 1: forward AND recompute must run on the client the caller
        // supplied, not on `R::default_client(device)`.
        let device = CpuDevice::new();
        let marked = CpuClient::new(device.clone())
            .with_parallelism(ParallelismConfig::new(Some(1), Some(7)));

        // The marker must distinguish `marked` from the default client.
        assert_eq!(
            CpuRuntime::default_client(&device).parallelism().chunk_size,
            None
        );

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );

        let seen: Arc<Mutex<Vec<Option<usize>>>> = Arc::new(Mutex::new(Vec::new()));
        let recorder = Arc::clone(&seen);

        let y = checkpoint_with_client(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| {
                recorder
                    .lock()
                    .unwrap_or_else(|p| p.into_inner())
                    .push(c.parallelism().chunk_size);
                var_mul(&inputs[0], &inputs[0], c)
            },
            &[&x],
            &marked,
        )
        .unwrap();

        let loss = var_sum(&y, &[], false, &marked).unwrap();
        let grads = backward(&loss, &marked).unwrap();

        let calls = seen.lock().unwrap_or_else(|p| p.into_inner()).clone();
        assert_eq!(calls.len(), 2, "expected one forward and one recompute");
        assert!(
            calls.iter().all(|chunk| *chunk == Some(7)),
            "segment ran on a client that is not the caller's: {calls:?}"
        );

        // Gradient is still correct: d(x^2)/dx = 2x = 6
        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 6.0).abs() < 1e-6);
    }

    #[test]
    fn test_checkpoint_unlisted_parameter_gets_its_gradient() {
        // Checkpointing is transparent: a trainable parameter the closure
        // captures but the caller never listed gets the gradient it would have
        // got from a plain forward.
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let w = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );
        let w_id = w.id();
        let captured = w.alias();

        let y = checkpoint(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| var_mul(&inputs[0], &captured, c),
            &[&x],
        )
        .unwrap();
        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        // Reference: the same segment with no checkpoint at all.
        let plain = var_mul(&x, &w, &client).unwrap();
        let plain_loss = var_sum(&plain, &[], false, &client).unwrap();
        let plain_grads = backward(&plain_loss, &client).unwrap();

        // d(x*w)/dx = w = 2
        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        let gx_plain: Vec<f32> = plain_grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 2.0).abs() < 1e-6);
        assert!((gx[0] - gx_plain[0]).abs() < 1e-6);

        // d(x*w)/dw = x = 3
        let gw: Vec<f32> = grads.get(w_id).unwrap().to_vec();
        let gw_plain: Vec<f32> = plain_grads.get(w_id).unwrap().to_vec();
        assert!((gw[0] - 3.0).abs() < 1e-6, "expected 3.0, got {}", gw[0]);
        assert!((gw[0] - gw_plain[0]).abs() < 1e-6);
    }

    #[test]
    fn test_checkpoint_unlisted_parameter_honours_the_needed_mask() {
        // `backward_wrt` asks for x only. The capture slot must stay unwanted,
        // so no gradient is computed for it and none reaches the store.
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let w = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );
        let w_id = w.id();
        let captured = w.alias();

        let y = checkpoint(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| var_mul(&inputs[0], &captured, c),
            &[&x],
        )
        .unwrap();
        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward_wrt(&loss, &[x.id()], &client).unwrap();

        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 2.0).abs() < 1e-6);
        assert!(grads.get(w_id).is_none(), "w was not asked for");
    }

    #[test]
    fn test_checkpoint_accumulates_across_two_runs() {
        // The same parameter captured by two segments collects both gradients.
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let w = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );
        let w_id = w.id();
        let (first, second) = (w.alias(), w.alias());

        let y1 = checkpoint(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| var_mul(&inputs[0], &first, c),
            &[&x],
        )
        .unwrap();
        let y2 = checkpoint(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| var_mul(&inputs[0], &second, c),
            &[&x],
        )
        .unwrap();

        let s1 = var_sum(&y1, &[], false, &client).unwrap();
        let s2 = var_sum(&y2, &[], false, &client).unwrap();
        let loss = var_add(&s1, &s2, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        // loss = 2*x*w, so d/dx = 2w = 4 and d/dw = 2x = 6.
        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        let gw: Vec<f32> = grads.get(w_id).unwrap().to_vec();
        assert!((gx[0] - 4.0).abs() < 1e-6, "expected 4.0, got {}", gx[0]);
        assert!((gw[0] - 6.0).abs() < 1e-6, "expected 6.0, got {}", gw[0]);
    }

    #[test]
    fn test_checkpoint_parameter_used_inside_and_outside() {
        // w is captured by the segment AND read by the surrounding graph. Both
        // contributions must land, and neither may be counted twice.
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let w = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );
        let w_id = w.id();
        let captured = w.alias();

        let inside = checkpoint(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| var_mul(&inputs[0], &captured, c),
            &[&x],
        )
        .unwrap();
        let outside = var_mul(&w, &w, &client).unwrap();

        let s_inside = var_sum(&inside, &[], false, &client).unwrap();
        let s_outside = var_sum(&outside, &[], false, &client).unwrap();
        let loss = var_add(&s_inside, &s_outside, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        // loss = x*w + w^2, so d/dx = w = 2 and d/dw = x + 2w = 7.
        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        let gw: Vec<f32> = grads.get(w_id).unwrap().to_vec();
        assert!((gx[0] - 2.0).abs() < 1e-6, "expected 2.0, got {}", gx[0]);
        assert!((gw[0] - 7.0).abs() < 1e-6, "expected 7.0, got {}", gw[0]);
    }

    #[test]
    fn test_checkpoint_listed_and_captured_together() {
        // A listed input must keep behaving exactly as before when a capture
        // shares the segment with it.
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let listed = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[5.0f32], &[1], &device).unwrap(),
            true,
        );
        let w = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );
        let w_id = w.id();
        let captured = w.alias();

        let y = checkpoint(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| {
                let scaled = var_mul(&inputs[0], &inputs[1], c)?;
                var_mul(&scaled, &captured, c)
            },
            &[&x, &listed],
        )
        .unwrap();

        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        // loss = x*listed*w = 30, so d/dx = 10, d/dlisted = 6, d/dw = 15.
        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        let gl: Vec<f32> = grads.get(listed.id()).unwrap().to_vec();
        let gw: Vec<f32> = grads.get(w_id).unwrap().to_vec();
        assert!((gx[0] - 10.0).abs() < 1e-5, "expected 10.0, got {}", gx[0]);
        assert!((gl[0] - 6.0).abs() < 1e-5, "expected 6.0, got {}", gl[0]);
        assert!((gw[0] - 15.0).abs() < 1e-5, "expected 15.0, got {}", gw[0]);
    }

    #[test]
    fn test_checkpoint_accepts_frozen_capture() {
        // A captured value with requires_grad = false builds no graph node, so
        // the segment claims no slot for it and it receives no gradient.
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let frozen = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            false,
        );
        let frozen_id = frozen.id();

        let y = checkpoint(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| var_mul(&inputs[0], &frozen, c),
            &[&x],
        )
        .unwrap();

        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        // d(2x)/dx = 2
        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 2.0).abs() < 1e-6);
        assert!(
            grads.get(frozen_id).is_none(),
            "a frozen capture claims no slot"
        );
    }

    #[test]
    fn test_checkpoint_of_frozen_values_is_untracked() {
        // Regression: the segment always returned a tracked node. A frozen
        // forward then reported `requires_grad` and held its closure and inputs.
        let (device, client) = device_and_client();
        let tensor = |v: f32| Tensor::<CpuRuntime>::from_slice(&[v], &[1], &device).unwrap();
        let x = Var::new(tensor(3.0), false);
        let w = Var::new(tensor(2.0), false);
        let captured = Var::new(tensor(5.0), false);

        let y = checkpoint_with_client(
            move |inputs: &[Var<CpuRuntime>], c: &CpuClient| {
                let xw = var_mul(&inputs[0], &inputs[1], c)?;
                var_mul(&xw, &captured, c)
            },
            &[&x, &w],
            &client,
        )
        .unwrap();

        let plain = var_mul(&x, &w, &client).unwrap();
        assert!(!plain.requires_grad());
        assert!(!y.requires_grad(), "frozen inputs give an untracked output");
        assert!(y.grad_fn().is_none(), "no graph node is kept");
        let got: Vec<f32> = y.tensor().to_vec();
        assert_eq!(got, [30.0]);
    }

    #[test]
    fn test_checkpoint_listed_parameter_gets_gradient() {
        // Listing a parameter stays the clearer form, and still trains it.
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let w = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );

        let y = checkpoint(
            |inputs: &[Var<CpuRuntime>], c: &CpuClient| var_mul(&inputs[0], &inputs[1], c),
            &[&x, &w],
        )
        .unwrap();

        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        let gw: Vec<f32> = grads.get(w.id()).unwrap().to_vec();
        assert!((gx[0] - 2.0).abs() < 1e-6);
        assert!((gw[0] - 3.0).abs() < 1e-6);
    }

    #[test]
    fn test_checkpoint_nested() {
        // A nested checkpoint retains its own graph node, yet every leaf it
        // reaches is a listed input, so the outer segment finds no capture.
        // f(x) = (x^2)^2 = x^4, df/dx = 4x^3 = 32 at x = 2
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );

        let y = checkpoint_with_client(
            |inputs: &[Var<CpuRuntime>], c: &CpuClient| {
                let z = checkpoint_with_client(
                    |inner: &[Var<CpuRuntime>], ic: &CpuClient| var_mul(&inner[0], &inner[0], ic),
                    &[&inputs[0]],
                    c,
                )?;
                var_mul(&z, &z, c)
            },
            &[&x],
            &client,
        )
        .unwrap();

        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward(&loss, &client).unwrap();

        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        assert!((gx[0] - 32.0).abs() < 1e-4, "expected 32.0, got {}", gx[0]);
    }

    #[test]
    fn test_checkpoint_trains_when_backward_runs_inside_no_grad() {
        // The forward records outside `no_grad`. The recompute runs inside it
        // and must still rebuild a graph. d(x^2 * w)/dx = 2xw = 12 at x=3, w=2.
        let (device, client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let w = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[2.0f32], &[1], &device).unwrap(),
            true,
        );

        let y = checkpoint(
            |inputs, c| {
                let sq = var_mul(&inputs[0], &inputs[0], c)?;
                var_mul(&sq, &inputs[1], c)
            },
            &[&x, &w],
        )
        .unwrap();
        let loss = var_sum(&y, &[], false, &client).unwrap();

        let grads = crate::autograd::no_grad(|| backward(&loss, &client)).unwrap();

        let gx: Vec<f32> = grads.get(x.id()).unwrap().to_vec();
        let gw: Vec<f32> = grads.get(w.id()).unwrap().to_vec();
        assert!((gx[0] - 12.0).abs() < 1e-5, "expected 12.0, got {}", gx[0]);
        assert!((gw[0] - 9.0).abs() < 1e-5, "expected 9.0, got {}", gw[0]);
    }

    #[test]
    fn test_checkpoint_recompute_records_inside_no_grad() {
        // A direct `GradFn::backward` call bypasses the driver's guard. The
        // checkpoint node holds its own guard, so the result is still correct.
        let (device, _client) = device_and_client();

        let x = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[3.0f32], &[1], &device).unwrap(),
            true,
        );
        let y = checkpoint(|inputs, c| var_mul(&inputs[0], &inputs[0], c), &[&x]).unwrap();
        let grad_fn = y.grad_fn().unwrap().clone();
        let seed = Tensor::<CpuRuntime>::from_slice(&[1.0f32], &[1], &device).unwrap();

        let grads = crate::autograd::no_grad(|| grad_fn.backward(&seed, &[true])).unwrap();
        let gx: Vec<f32> = grads[0].as_ref().unwrap().to_vec();
        assert!((gx[0] - 6.0).abs() < 1e-6, "expected 6.0, got {}", gx[0]);
    }

    #[test]
    fn test_checkpoint_frozen_listed_input_gets_no_gradient() {
        // A frozen listed input beside a trainable one must get no gradient,
        // even when the caller asks for it. The trainable gradient keeps its bits.
        let (device, client) = device_and_client();
        let frozen = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[1.5f32, -0.25, 3.0], &[3], &device).unwrap(),
            false,
        );
        let w = Var::new(
            Tensor::<CpuRuntime>::from_slice(&[0.7f32, 1.3, -2.1], &[3], &device).unwrap(),
            true,
        );
        let segment = |xs: &[Var<CpuRuntime>], c: &CpuClient| {
            let xw = var_mul(&xs[0], &xs[1], c)?;
            var_mul(&xw, &xs[1], c)
        };

        let plain = segment(&[frozen.alias(), w.alias()], &client).unwrap();
        let plain_loss = var_sum(&plain, &[], false, &client).unwrap();
        let wanted = [frozen.id(), w.id()];
        let plain_grads = backward_wrt(&plain_loss, &wanted, &client).unwrap();

        let y = checkpoint_with_client(segment, &[&frozen, &w], &client).unwrap();
        let loss = var_sum(&y, &[], false, &client).unwrap();
        let grads = backward_wrt(&loss, &wanted, &client).unwrap();

        assert!(
            grads.get(frozen.id()).is_none(),
            "a frozen listed input must get no gradient"
        );
        let bits = |t: &Tensor<CpuRuntime>| -> Vec<u32> {
            t.to_vec::<f32>().iter().map(|v| v.to_bits()).collect()
        };
        assert_eq!(
            bits(grads.get(w.id()).unwrap()),
            bits(plain_grads.get(w.id()).unwrap())
        );
    }

    #[test]
    fn test_checkpoint_empty_inputs_errors() {
        let inputs: [&Var<CpuRuntime>; 0] = [];
        let err = checkpoint(
            |ins: &[Var<CpuRuntime>], c: &CpuClient| var_mul(&ins[0], &ins[0], c),
            &inputs,
        )
        .unwrap_err();
        assert!(err.to_string().contains("at least one input"));
    }
}