rai_core/transforms/
optimize.rs

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
use crate::{
    primitives::{Full, ToDType, ToDevice},
    Cpu, Cuda, Func, Metal, Shape, Tensor, TensorIter, Value, BF16, F16, F32, F64, I64, U32, U8,
};
use std::{
    any::TypeId,
    collections::{BTreeSet, HashMap},
};

fn is_full(t: &Tensor) -> Option<String> {
    if let Some(f) = t.primitive().as_any().downcast_ref::<Full<F32>>() {
        Some(format!("{:?}", f.val))
    } else if let Some(f) = t.primitive().as_any().downcast_ref::<Full<F64>>() {
        Some(format!("{:?}", f.val))
    } else if let Some(f) = t.primitive().as_any().downcast_ref::<Full<BF16>>() {
        Some(format!("{:?}", f.val))
    } else if let Some(f) = t.primitive().as_any().downcast_ref::<Full<F16>>() {
        Some(format!("{:?}", f.val))
    } else if let Some(f) = t.primitive().as_any().downcast_ref::<Full<U32>>() {
        Some(format!("{:?}", f.val))
    } else if let Some(f) = t.primitive().as_any().downcast_ref::<Full<U8>>() {
        Some(format!("{:?}", f.val))
    } else if let Some(f) = t.primitive().as_any().downcast_ref::<Full<I64>>() {
        Some(format!("{:?}", f.val))
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDType<F32>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDType<F64>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDType<BF16>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDType<F16>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDType<U32>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDType<U8>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDType<I64>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDevice<Cpu>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDevice<Cuda>>() {
        is_full(t.inputs().first().unwrap())
    } else if let Some(_) = t.primitive().as_any().downcast_ref::<ToDevice<Metal>>() {
        is_full(t.inputs().first().unwrap())
    } else {
        None
    }
}

// TODO: optimize cache
pub fn optimize<'a, K, IN, OUT, F>(func: F) -> impl Fn(IN) -> OUT + 'a
where
    F: Func<K, IN, OUT> + 'a,
    IN: Value,
    OUT: Value,
{
    move |input| {
        let output = func.invoke(input);

        let mut tape = BTreeSet::new();
        let mut stack = Vec::new();

        // use iterative instead of recursive to avoid stack overflow
        // TODO: use proper topo sort algorithm, now sort by id in BTreeSet
        for output in output.tensors().tensor_iter() {
            stack.push(output.clone());
        }

        while let Some(t) = stack.pop() {
            if tape.contains(&t) {
                continue;
            }
            tape.insert(t.clone());
            for input in t.inputs().iter() {
                stack.push(input.clone());
            }
        }

        // (device, dtype, val:shape) -> Tensor
        let mut constants = HashMap::<(TypeId, TypeId, String), Tensor>::new();
        for t in tape.iter() {
            if let Some(val) = is_full(&t) {
                let key = (
                    t.device().as_any().type_id(),
                    t.dtype().as_any().type_id(),
                    format!("{:?}{:?}", val, t.shape()),
                );
                if constants.get(&key).is_none() {
                    constants.insert(key, t.clone());
                }
            }

            if !t.inputs().is_empty() {
                // constant literal sharing
                let inputs = t
                    .inputs()
                    .iter()
                    .map(|x| {
                        if let Some(v) = is_full(x) {
                            let key = (
                                x.device().as_any().type_id(),
                                x.dtype().as_any().type_id(),
                                format!("{:?}{:?}", v, x.shape()),
                            );
                            constants.get(&key).unwrap().clone()
                        } else {
                            x.clone()
                        }
                    })
                    .collect();
                t.set_inputs(inputs);
            }
        }

        output
    }
}

#[cfg(test)]
mod tests {
    use crate::{Cpu, Tensor, F32};

    fn loss_fn(x: &Tensor, y: &Tensor) -> Tensor {
        x * 2.0 * 2.0 + y
    }

    #[test]
    fn test_optimize() {
        let f = |x: &Tensor, y: &Tensor| x * 2.0 * 2.0 + y;
        let g = super::optimize(f);
        let x = Tensor::rand([], F32, Cpu);
        let y = Tensor::rand([], F32, Cpu);
        let out = g((&x, &y));
        println!("{}", out.dot_graph());

        let x = Tensor::rand([], F32, Cpu);
        let y = Tensor::rand([], F32, Cpu);
        let out = g((&x, &y));
        println!("{}", out.dot_graph());

        let g = super::optimize(loss_fn);
        let x = Tensor::rand([], F32, Cpu);
        let y = Tensor::rand([], F32, Cpu);
        let out = g((&x, &y));
        println!("{}", out.dot_graph());

        let x = Tensor::rand([], F32, Cpu);
        let y = Tensor::rand([], F32, Cpu);
        let out = g((&x, &y));
        println!("{}", out.dot_graph());
    }
}