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
use std::collections::BTreeSet;

use crate::{dispatch::eval_rule, Backend, Tensor, TensorIter};

pub trait EvalArgs {
    fn outputs(&self) -> impl Iterator<Item = &Tensor>;
    fn retain_graph(&self) -> bool {
        false
    }
    fn backend(&self) -> Option<Box<dyn Backend>> {
        None
    }
}

impl<T> EvalArgs for T
where
    T: TensorIter,
{
    fn outputs(&self) -> impl Iterator<Item = &Tensor> {
        self.tensor_iter()
    }
}

impl<T> EvalArgs for (T, bool)
where
    T: TensorIter,
{
    fn outputs(&self) -> impl Iterator<Item = &Tensor> {
        self.0.tensor_iter()
    }

    fn retain_graph(&self) -> bool {
        self.1
    }
}

impl<T, B> EvalArgs for (T, bool, B)
where
    T: TensorIter,
    B: Backend,
{
    fn outputs(&self) -> impl Iterator<Item = &Tensor> {
        self.0.tensor_iter()
    }

    fn retain_graph(&self) -> bool {
        self.1
    }

    fn backend(&self) -> Option<Box<dyn Backend>> {
        Some(self.2.clone_boxed())
    }
}

pub fn eval<T: EvalArgs>(args: T) {
    fn recurse(tape: &mut BTreeSet<Tensor>, t: &Tensor) {
        if t.is_evaluated() || tape.contains(t) {
            return;
        }
        for input in t.inputs().iter() {
            recurse(tape, input);
        }
        tape.insert(t.clone());
    }

    let mut tape = BTreeSet::new();
    for output in args.outputs() {
        recurse(&mut tape, output);
    }

    for t in tape.into_iter() {
        {
            let backend = args.backend().unwrap_or(t.backend().clone_boxed());
            let backend = backend.as_ref();
            let primitive = t.primitive().clone_boxed();
            let primitive = primitive.as_ref();
            let inputs = &*t.inputs();
            let rule = eval_rule(backend, primitive).unwrap_or_else(|| {
                panic!(
                    "no eval rule for backend: {:?}, primitive: {:?}",
                    backend, primitive
                )
            });
            rule.eval(backend, primitive, inputs, &t);
        }
        if !args.retain_graph() {
            t.detach();
        }
    }
}