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
use std::fmt::Debug;

use crate::internal::*;

pub trait Runtime: Debug {
    fn name(&self) -> Cow<str>;
    fn prepare(&self, model: TypedModel) -> TractResult<Box<dyn Runnable>>;
}

pub trait Runnable: Debug {
    fn run(&self, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
        self.spawn()?.run(inputs)
    }
    fn spawn(&self) -> TractResult<Box<dyn State>>;
}

pub trait State {
    fn run(&mut self, inputs: TVec<TValue>) -> TractResult<TVec<TValue>>;
}

#[derive(Debug)]
pub struct DefaultRuntime;

impl Runtime for DefaultRuntime {
    fn name(&self) -> Cow<str> {
        Cow::Borrowed("default")
    }

    fn prepare(&self, model: TypedModel) -> TractResult<Box<dyn Runnable>> {
        Ok(Box::new(Arc::new(model.into_optimized()?.into_runnable()?)))
    }
}

impl Runnable for Arc<TypedRunnableModel<TypedModel>> {
    fn spawn(&self) -> TractResult<Box<dyn State>> {
        Ok(Box::new(SimpleState::new(self.clone())?))
    }
}

impl State for TypedSimpleState<TypedModel, Arc<TypedRunnableModel<TypedModel>>> {
    fn run(&mut self, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
        self.run(inputs)
    }
}