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
//! The `no_grad` switch: whether ops attach graph nodes to their outputs.
use Cell;
thread_local!
/// Whether ops on this thread currently attach graph nodes.
/// Run `f` without building a graph.
///
/// ```
/// # use fastnn::prelude::*;
/// # let model = Linear::new(4, 2);
/// # let x = Tensor::randn(&[1, 4]);
/// let logits = no_grad(|| model.forward(&x));
/// assert!(logits.grad_fn().is_none());
/// ```
/// Guard, so a panic inside `no_grad` cannot leave tracking off.
;