use ::rlx::{CompileOptions, CompiledGraph, Device, Dim, Graph, Session};
use crate::textures::{Texture, TextureFormat};
use super::tensor::{
frame_to_tensor, tensor_to_frame, tensor_to_texture, ColorSpace, Layout, Tensor, TensorError,
};
pub fn preferred_device() -> Device {
if ::rlx::is_available(Device::Cpu) {
Device::Cpu
} else {
::rlx::fastest_device()
}
}
pub struct GraphRunner {
compiled: CompiledGraph,
device: Device,
output_dims: Vec<Option<Vec<usize>>>,
}
impl GraphRunner {
pub fn new(graph: Graph, device: Device) -> Self {
let output_dims = declared_output_dims(&graph);
Self {
compiled: Session::new(device).compile(graph),
device,
output_dims,
}
}
pub fn with_options(graph: Graph, device: Device, options: &CompileOptions) -> Self {
let output_dims = declared_output_dims(&graph);
Self {
compiled: Session::new(device).compile_with(graph, options),
device,
output_dims,
}
}
pub fn device(&self) -> Device {
self.device
}
pub fn set_param(&mut self, name: &str, data: &[f32]) {
self.compiled.set_param(name, data);
}
pub fn set_param_tensor(&mut self, name: &str, tensor: &Tensor) {
self.compiled.set_param(name, tensor.data());
}
pub fn run(&mut self, inputs: &[(&str, &Tensor)]) -> Vec<Tensor> {
let flat: Vec<(&str, &[f32])> = inputs.iter().map(|(n, t)| (*n, t.data())).collect();
let outputs = self.compiled.run(&flat);
outputs
.into_iter()
.enumerate()
.map(
|(i, values)| match self.output_dims.get(i).and_then(|d| d.as_ref()) {
Some(dims) if dims.iter().product::<usize>() == values.len() => {
Tensor::new(values, dims).expect("product checked above")
}
_ => Tensor::from_flat(values),
},
)
.collect()
}
pub fn run_flat(&mut self, inputs: &[(&str, &[f32])]) -> Vec<Vec<f32>> {
self.compiled.run(inputs)
}
pub fn compiled_mut(&mut self) -> &mut CompiledGraph {
&mut self.compiled
}
}
pub struct FrameFilter {
runner: GraphRunner,
input: String,
layout: Layout,
space: ColorSpace,
}
impl FrameFilter {
pub fn new(graph: Graph, device: Device, input: impl Into<String>) -> Self {
Self {
runner: GraphRunner::new(graph, device),
input: input.into(),
layout: Layout::Nhwc,
space: ColorSpace::Linear,
}
}
pub fn layout(mut self, layout: Layout) -> Self {
self.layout = layout;
self
}
pub fn color_space(mut self, space: ColorSpace) -> Self {
self.space = space;
self
}
pub fn apply(
&mut self,
rgba: &[u8],
width: u32,
height: u32,
) -> Result<(Vec<u8>, u32, u32), TensorError> {
let out = self.run(rgba, width, height)?;
tensor_to_frame(&out, self.layout, self.space)
}
pub fn apply_to_texture(
&mut self,
rgba: &[u8],
width: u32,
height: u32,
) -> Result<Texture, TensorError> {
let out = self.run(rgba, width, height)?;
tensor_to_texture(&out, TextureFormat::Rgba8UnormSrgb, self.layout, self.space)
}
pub fn run(&mut self, rgba: &[u8], width: u32, height: u32) -> Result<Tensor, TensorError> {
let input = frame_to_tensor(rgba, width, height, self.layout, self.space)?;
let mut outputs = self.runner.run(&[(self.input.as_str(), &input)]);
if outputs.is_empty() {
return Ok(Tensor::from_flat(Vec::new()));
}
Ok(outputs.remove(0))
}
pub fn runner_mut(&mut self) -> &mut GraphRunner {
&mut self.runner
}
}
fn declared_output_dims(graph: &Graph) -> Vec<Option<Vec<usize>>> {
graph
.outputs
.iter()
.map(|id| {
graph
.node(*id)
.shape
.dims()
.iter()
.map(|d| match d {
Dim::Static(n) => Some(*n),
Dim::Dynamic(_) => None,
})
.collect::<Option<Vec<usize>>>()
})
.collect()
}