use crate::graph::{self, WorkingSections};
use crate::{Encoding, MemoryBreakdown, Registry, Target};
use core::alloc::Layout;
use nanoglia_core::backend::{BackendKind, KernelPlan};
use nanoglia_core::{ALIGN, Allocator, Block, Detail, Error, OpCode, Result, TensorDesc, align_up};
use nanoglia_tflite::{OperatorHeader, SubGraph};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct SelectedKernel {
pub name: &'static str,
pub backend: &'static str,
pub kind: BackendKind,
pub plan: KernelPlan,
}
#[derive(Clone, Copy)]
pub struct OperatorReport<'a> {
subgraph: SubGraph<'a>,
index: usize,
header: OperatorHeader<'a>,
kernel: Result<SelectedKernel>,
}
impl core::fmt::Debug for OperatorReport<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("OperatorReport")
.field("index", &self.index)
.field("opcode", &self.header.code)
.field("version", &self.header.version)
.field("kernel", &self.kernel)
.finish()
}
}
impl<'a> OperatorReport<'a> {
pub fn index(&self) -> usize {
self.index
}
pub fn opcode(&self) -> OpCode<'a> {
self.header.code
}
pub fn version(&self) -> i32 {
self.header.version
}
pub fn input_count(&self) -> usize {
self.header.inputs.len()
}
pub fn output_count(&self) -> usize {
self.header.outputs.len()
}
pub fn input(&self, i: usize) -> Result<Option<TensorDesc<'a>>> {
self.tensor(self.header.inputs.get(i), i)
}
pub fn output(&self, i: usize) -> Result<TensorDesc<'a>> {
self.tensor(self.header.outputs.get(i), i)?
.ok_or_else(|| Error::malformed(0))
}
pub fn kernel(&self) -> Result<SelectedKernel> {
self.kernel
}
fn tensor(&self, raw: Option<i32>, i: usize) -> Result<Option<TensorDesc<'a>>> {
let raw = raw.ok_or_else(|| {
Error::invalid_argument(Detail::IndexOutOfRange {
index: u32::try_from(i).unwrap_or(u32::MAX),
})
})?;
match graph::tensor_index(raw, self.subgraph.tensor_count())? {
Some(t) => self.subgraph.tensor(t).map(Some),
None => Ok(None),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ModelReport {
pub operators: usize,
pub unsupported: usize,
pub memory: Result<MemoryBreakdown>,
}
impl ModelReport {
pub fn runs(&self) -> bool {
self.memory.is_ok()
}
pub fn check_in<'m, A: Allocator, F: FnMut(&OperatorReport<'m>)>(
builders: &[&'m [u8]],
encoding: Encoding,
target: Target,
alloc: A,
visit: F,
) -> Result<Self> {
Self::check_with(
&Registry::default(),
builders,
encoding,
target,
alloc,
visit,
)
}
pub fn check_with<'m, A: Allocator, F: FnMut(&OperatorReport<'m>)>(
registry: &Registry<'_>,
builders: &[&'m [u8]],
encoding: Encoding,
target: Target,
alloc: A,
mut visit: F,
) -> Result<Self> {
let (_, subgraph) = graph::open(builders, encoding)?;
let operators = subgraph.operator_count();
let mut persistent = 0usize;
for index in 0..operators {
if let Ok(choice) = graph::choose(registry, target, &subgraph, index, None) {
persistent = persistent.max(choice.plan.persistent);
}
}
let overflow = || Error::out_of_memory(usize::MAX);
let mut layout = Layout::from_size_align(0, ALIGN).map_err(|_| overflow())?;
let working = WorkingSections::new(&mut layout, subgraph.tensor_count())?;
let persist_start = align_up(layout.size()).ok_or_else(overflow)?;
let size = persist_start.checked_add(persistent).ok_or_else(overflow)?;
let mut block = Block::new_zeroed(size, alloc)?;
let (planning, persist) = block
.as_mut_slice()
.split_at_mut_checked(persist_start)
.ok_or_else(overflow)?;
let mut unsupported = 0usize;
let mut first_error = None;
for index in 0..operators {
let header = subgraph.operator_header(index)?;
let kernel = graph::choose(registry, target, &subgraph, index, Some(&mut *persist))
.map(|choice| SelectedKernel {
name: choice.kernel.name(),
backend: choice.backend,
kind: choice.kind,
plan: choice.plan,
});
if let Err(error) = kernel {
unsupported += 1;
first_error.get_or_insert(error);
}
visit(&OperatorReport {
subgraph,
index,
header,
kernel,
});
}
let memory = graph::measure(registry, target, &subgraph, &working, planning)
.and_then(|memory| first_error.map_or(Ok(memory), Err));
Ok(Self {
operators,
unsupported,
memory,
})
}
#[cfg(feature = "alloc")]
pub fn check<'m, F: FnMut(&OperatorReport<'m>)>(
builders: &[&'m [u8]],
encoding: Encoding,
target: Target,
visit: F,
) -> Result<Self> {
Self::check_in(
builders,
encoding,
target,
nanoglia_core::allocator_api2::alloc::Global,
visit,
)
}
}