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
use crate::Tensor;
use std::any::Any;
use std::fmt::Debug;

pub trait Primitive: Debug {
    fn clone_boxed(&self) -> Box<dyn Primitive>;
    fn dot_label(&self) -> String {
        format!("{:?}", self)
    }
    fn as_any(&self) -> &dyn Any;
    fn jvp(&self, output: &Tensor, primals: &[Tensor], tangents: &[Tensor]) -> Tensor;
    fn vjp(&self, output: &Tensor, primals: &[Tensor], cotangent: &Tensor) -> Vec<Tensor>;
}

impl<T> From<T> for Box<dyn Primitive>
where
    T: Clone + Primitive + 'static,
{
    fn from(t: T) -> Self {
        Box::new(t.clone())
    }
}

impl Clone for Box<dyn Primitive> {
    fn clone(&self) -> Self {
        self.clone_boxed()
    }
}

impl From<&dyn Primitive> for Box<dyn Primitive> {
    fn from(t: &dyn Primitive) -> Self {
        t.clone_boxed()
    }
}

mod creation;
pub use creation::*;

mod binary;
pub use binary::*;

mod unary;
pub use unary::*;

mod transform;
pub use transform::*;

mod reduce;
pub use reduce::*;