Path: blob/main/crates/wasi-nn/src/registry/in_memory.rs
2464 views
//! Implement a [`GraphRegistry`] with a hash map.12use super::{Graph, GraphRegistry};3use crate::backend::BackendFromDir;4use crate::wit::ExecutionTarget;5use anyhow::{anyhow, bail};6use std::{collections::HashMap, path::Path};78pub struct InMemoryRegistry(HashMap<String, Graph>);9impl InMemoryRegistry {10pub fn new() -> Self {11Self(HashMap::new())12}1314/// Load a graph from the files contained in the `path` directory.15///16/// This expects the backend to know how to load graphs (i.e., ML model)17/// from a directory. The name used in the registry is the directory's last18/// suffix: if the backend can find the files it expects in `/my/model/foo`,19/// the registry will contain a new graph named `foo`.20pub fn load(&mut self, backend: &mut dyn BackendFromDir, path: &Path) -> anyhow::Result<()> {21if !path.is_dir() {22bail!(23"preload directory is not a valid directory: {}",24path.display()25);26}27let name = path28.file_name()29.map(|s| s.to_string_lossy())30.ok_or(anyhow!("no file name in path"))?;3132let graph = backend.load_from_dir(path, ExecutionTarget::Cpu)?;33self.0.insert(name.into_owned(), graph);34Ok(())35}36}3738impl GraphRegistry for InMemoryRegistry {39fn get(&self, name: &str) -> Option<&Graph> {40self.0.get(name)41}42fn get_mut(&mut self, name: &str) -> Option<&mut Graph> {43self.0.get_mut(name)44}45}464748