oxfuse_runtime/backend.rs
1//! The portable backend interface. Kept minimal on purpose: it assumes host
2//! tensors, so a device-memory backend would need to revise it.
3
4use crate::Tensor;
5use oxfuse_core::{Error, Graph};
6
7pub trait Backend {
8 fn compile(&self, graph: &Graph) -> Result<Box<dyn Executable>, Error>;
9}
10
11pub trait Executable {
12 /// `inputs` follow the graph's declared input order; the result follows
13 /// the graph's output order.
14 fn run(&self, inputs: &[Tensor]) -> Result<Vec<Tensor>, Error>;
15
16 /// Runs the graph once per element of `batches` and returns the results in
17 /// the same order; every result equals what `run` returns for that element.
18 /// The default runs them one after another. The GPU backend overrides it to
19 /// submit the whole batch at once and wait once, which hides the per-run
20 /// wait; its memory use grows with the batch.
21 fn run_many(&self, batches: &[Vec<Tensor>]) -> Result<Vec<Vec<Tensor>>, Error> {
22 batches.iter().map(|inputs| self.run(inputs)).collect()
23 }
24}