rai_core/primitives/
mod.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
use crate::Tensor;
use std::{any::Any, 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<'a> From<&'a dyn Primitive> for Box<dyn Primitive> {
    fn from(t: &'a 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::*;

mod indexing;
pub use indexing::*;

mod convolution;
pub use convolution::*;

mod pooling;
pub use pooling::*;

mod vision;
pub use vision::*;

mod others;
pub use others::*;