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
use std::{any::TypeId, collections::HashMap, sync::Mutex};

use dyn_clone::DynClone;
use once_cell::sync::Lazy;

use crate::{backend::Cpu, primitives, Backend, Primitive, Tensor};

pub trait Eval<B, P>: DynClone + Sync + Send + 'static
where
    B: ?Sized,
    P: ?Sized,
{
    fn eval(&self, backend: &B, primitive: &P, inputs: &[Tensor], output: &Tensor);
}
dyn_clone::clone_trait_object!(<B, P> Eval<B, P> where B: ?Sized, P: ?Sized);

#[derive(Debug, Clone)]
pub struct Dispatch<B, P> {
    phantom: std::marker::PhantomData<(B, P)>,
}

impl<B, P> Eval<dyn Backend, dyn Primitive> for Dispatch<B, P>
where
    B: Backend + 'static + Sync + Send,
    P: Primitive + 'static + Sync + Send,
    Self: Eval<B, P> + 'static,
{
    #[inline]
    fn eval(
        &self,
        backend: &dyn Backend,
        primitive: &dyn Primitive,
        inputs: &[Tensor],
        output: &Tensor,
    ) {
        let backend = backend.as_any().downcast_ref::<B>().unwrap();
        let primitive = primitive.as_any().downcast_ref::<P>().unwrap();
        self.eval(backend, primitive, inputs, output);
    }
}

impl<B, P> Eval<Box<dyn Backend>, Box<dyn Primitive>> for Dispatch<B, P>
where
    B: Backend + 'static + Sync + Send,
    P: Primitive + 'static + Sync + Send,
    Self: Eval<B, P> + 'static,
{
    #[inline]
    fn eval(
        &self,
        backend: &Box<dyn Backend>,
        primitive: &Box<dyn Primitive>,
        inputs: &[Tensor],
        output: &Tensor,
    ) {
        let backend = backend.as_any().downcast_ref::<B>().unwrap();
        let primitive = primitive.as_any().downcast_ref::<P>().unwrap();
        self.eval(backend, primitive, inputs, output);
    }
}

type ErasedEval = Box<dyn Eval<Box<dyn Backend>, Box<dyn Primitive>>>;

static EVAL_DISPATCHER: Lazy<Mutex<HashMap<(TypeId, TypeId), ErasedEval>>> = Lazy::new(|| {
    let mut rules: HashMap<(TypeId, TypeId), ErasedEval> = HashMap::new();

    _register::<Cpu, primitives::Full>(&mut rules);
    _register::<Cpu, primitives::Normal>(&mut rules);
    _register::<Cpu, primitives::Add>(&mut rules);
    _register::<Cpu, primitives::Sub>(&mut rules);
    _register::<Cpu, primitives::Mul>(&mut rules);
    _register::<Cpu, primitives::Div>(&mut rules);
    _register::<Cpu, primitives::Sin>(&mut rules);
    _register::<Cpu, primitives::Cos>(&mut rules);
    _register::<Cpu, primitives::Negative>(&mut rules);
    _register::<Cpu, primitives::ReduceSum>(&mut rules);
    _register::<Cpu, primitives::MatMul>(&mut rules);
    _register::<Cpu, primitives::Square>(&mut rules);
    _register::<Cpu, primitives::Transpose>(&mut rules);
    _register::<Cpu, primitives::Reshape>(&mut rules);
    _register::<Cpu, primitives::Broadcast>(&mut rules);

    Mutex::new(rules)
});

pub fn register<B, P>()
where
    B: Backend + 'static + Sync + Send,
    P: Primitive + 'static + Sync + Send,
    Dispatch<B, P>: Eval<B, P> + 'static,
{
    let mut rules = EVAL_DISPATCHER.lock().unwrap();
    _register::<B, P>(&mut rules)
}

pub fn register_custom<B, P, R>(rule: R)
where
    B: Backend + 'static + Sync + Send,
    P: Primitive + 'static + Sync + Send,
    R: Eval<B, P> + 'static + Eval<Box<(dyn Backend)>, Box<(dyn Primitive)>>,
{
    let mut rules = EVAL_DISPATCHER.lock().unwrap();
    rules.insert((TypeId::of::<B>(), TypeId::of::<P>()), Box::new(rule));
}

pub fn eval_rule(backend: &Box<dyn Backend>, primitive: &Box<dyn Primitive>) -> Option<ErasedEval> {
    let rules = EVAL_DISPATCHER.lock().unwrap();
    rules
        .get(&(backend.as_any().type_id(), primitive.as_any().type_id()))
        .cloned()
}

fn _register<B, P>(rules: &mut HashMap<(TypeId, TypeId), ErasedEval>)
where
    B: Backend + 'static + Sync + Send,
    P: Primitive + 'static + Sync + Send,
    Dispatch<B, P>: Eval<B, P> + 'static,
{
    rules.insert(
        (TypeId::of::<B>(), TypeId::of::<P>()),
        Box::new(Dispatch {
            phantom: std::marker::PhantomData::<(B, P)>,
        }),
    );
}